diff --git a/livesupport/modules/storage/src/TestStorageClient.cxx b/livesupport/modules/storage/src/TestStorageClient.cxx index 6dbb62e87..df71d829c 100644 --- a/livesupport/modules/storage/src/TestStorageClient.cxx +++ b/livesupport/modules/storage/src/TestStorageClient.cxx @@ -21,8 +21,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Author : $Author: maroy $ - Version : $Revision: 1.9 $ + Author : $Author: fgerlits $ + Version : $Revision: 1.10 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.cxx,v $ ------------------------------------------------------------------------------*/ @@ -33,6 +33,12 @@ #include "configure.h" #endif +#if HAVE_UNISTD_H +#include +#else +#error "Need unistd.h" +#endif + #include #include "TestStorageClient.h" @@ -55,7 +61,7 @@ const std::string TestStorageClient::configElementNameStr = "testStorage"; /*------------------------------------------------------------------------------ * The path to the local temp storage *----------------------------------------------------------------------------*/ -const std::string TestStorageClient::localTempStoragePath = "tmp/"; +const std::string TestStorageClient::localTempStoragePath = "var/"; /*------------------------------------------------------------------------------ * The XML version used to create the SMIL file. @@ -192,7 +198,7 @@ TestStorageClient :: getPlaylist(Ptr::Ref id) const /*------------------------------------------------------------------------------ * Acquire resources for a playlist. *----------------------------------------------------------------------------*/ -Ptr::Ref +Ptr::Ref TestStorageClient :: acquirePlaylist(Ptr::Ref id) const throw (std::logic_error) { @@ -202,8 +208,11 @@ TestStorageClient :: acquirePlaylist(Ptr::Ref id) const throw std::invalid_argument("no such playlist"); } - Ptr::Ref playlist = playlistMapIt->second; - + Ptr::Ref oldPlaylist = playlistMapIt->second; + Ptr::Ref playlength(new time_duration( + *(oldPlaylist->getPlaylength()) )); + Ptr::Ref newPlaylist(new Playlist(UniqueId::generateId(), + playlength)); Ptr::Ref smilDocument(new xmlpp::Document(xmlVersion)); xmlpp::Element * smilRootNode @@ -218,25 +227,39 @@ TestStorageClient :: acquirePlaylist(Ptr::Ref id) const xmlpp::Element * smilSeqNode = smilBodyNode->add_child(smilSeqNodeName); - Playlist::const_iterator it = playlist->begin(); + Playlist::const_iterator it = oldPlaylist->begin(); + + while (it != oldPlaylist->end()) { + Ptr::Ref audioClip = acquireAudioClip( it->second + ->getAudioClip() + ->getId() ); + Ptr::Ref relativeOffset(new time_duration( + *(it->second->getRelativeOffset()) )); + Ptr::Ref oldFadeInfo = it->second->getFadeInfo(); + Ptr::Ref newFadeInfo; + if (oldFadeInfo) { // careful: fadeInfo may be 0 + newFadeInfo.reset(new FadeInfo(*oldFadeInfo)); + } + + newPlaylist->addAudioClip(audioClip, + relativeOffset, + newFadeInfo); - while (it != playlist->end()) { xmlpp::Element * smilAudioClipNode = smilSeqNode->add_child(smilAudioClipNodeName); smilAudioClipNode->set_attribute( smilAudioClipUriAttrName, - * acquireAudioClip(it->second->getAudioClip()->getId())); - ++it; // may throw exception + *(audioClip->getUri()) ); + ++it; } - std::stringstream fileNameBuffer; - fileNameBuffer << localTempStoragePath << "tempPlaylist" - << id->getId() - << ".smil"; - Ptr::Ref fileName(new std::string(fileNameBuffer.str())); + std::string smilFileName = "file://"; + smilFileName += tempnam(0, "smil"); + smilDocument->write_to_file(smilFileName, "UTF-8"); - smilDocument->write_to_file(*fileName, "UTF-8"); - return fileName; + Ptr::Ref playlistUri(new std::string(smilFileName)); + newPlaylist->setUri(playlistUri); + return newPlaylist; } @@ -244,38 +267,43 @@ TestStorageClient :: acquirePlaylist(Ptr::Ref id) const * Release a playlist. *----------------------------------------------------------------------------*/ void -TestStorageClient :: releasePlaylist(Ptr::Ref id) const +TestStorageClient :: releasePlaylist(Ptr::Ref playlist) const throw (std::logic_error) { - PlaylistMap::const_iterator playlistMapIt = playlistMap.find(id->getId()); - - if (playlistMapIt == playlistMap.end()) { - throw std::invalid_argument("no such playlist"); + if (! playlist->getUri()) { + throw std::logic_error("playlist URI not found"); } - - Ptr::Ref playlist = playlistMapIt->second; + + try { + std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r"); + if (!f) { + throw std::logic_error("playlist temp file not found"); + } + std::fclose(f); + } + catch (std::exception &e) { + throw std::logic_error("playlist temp file I/O error"); + } + + std::remove(playlist->getUri()->substr(7).c_str()); - bool success = true; + int badAudioClips = 0; Playlist::const_iterator it = playlist->begin(); while (it != playlist->end()) { try { - releaseAudioClip(it->second->getAudioClip()->getId()); + releaseAudioClip(it->second->getAudioClip()); } catch (std::invalid_argument &e) { - success = false; + ++badAudioClips; } ++it; } - std::stringstream fileNameBuffer; - fileNameBuffer << localTempStoragePath << "tempPlaylist" - << id->getId() - << ".smil"; - std::remove(fileNameBuffer.str().c_str()); - - if (!success) { - throw std::logic_error("could not release some audio clips" - " in playlist"); + if (badAudioClips) { + std::stringstream eMsg; + eMsg << "could not release " << badAudioClips + << " audio clips in playlist"; + throw std::logic_error(eMsg.str()); } } @@ -368,7 +396,7 @@ TestStorageClient :: getAudioClip(Ptr::Ref id) const /*------------------------------------------------------------------------------ * Acquire resources for an audio clip. *----------------------------------------------------------------------------*/ -Ptr::Ref +Ptr::Ref TestStorageClient :: acquireAudioClip(Ptr::Ref id) const throw (std::logic_error) { @@ -378,12 +406,36 @@ TestStorageClient :: acquireAudioClip(Ptr::Ref id) const throw std::invalid_argument("no such audio clip"); } - std::stringstream fileNameBuffer; - fileNameBuffer << localTempStoragePath << "test" - << id->getId() - << ".mp3"; - Ptr::Ref fileName(new std::string(fileNameBuffer.str())); - return fileName; + Ptr::Ref storedAudioClip = it->second; + + if (! storedAudioClip->getUri()) { + throw std::logic_error("audio clip URI not found"); + } + // cut the "file:" off + std::string audioClipFileName = storedAudioClip->getUri()->substr(5); + + try { + std::FILE * f = fopen(audioClipFileName.c_str(), "r"); + if (f) { + std::fclose(f); + } + else { + throw std::logic_error("could not read audio clip"); + } + } + catch (std::exception &e) { + throw std::logic_error("could not read audio clip"); + } + + Ptr::Ref audioClip(new AudioClip(*storedAudioClip)); + + Ptr::Ref audioClipUri(new std::string("file://")); + *audioClipUri += get_current_dir_name(); // doesn't work if current + *audioClipUri += "/"; // dir = /, but OK for now + *audioClipUri += audioClipFileName; + + audioClip->setUri(audioClipUri); + return audioClip; } @@ -391,13 +443,11 @@ TestStorageClient :: acquireAudioClip(Ptr::Ref id) const * Release an audio clip. *----------------------------------------------------------------------------*/ void -TestStorageClient :: releaseAudioClip(Ptr::Ref id) const +TestStorageClient :: releaseAudioClip(Ptr::Ref audioClip) const throw (std::logic_error) { - AudioClipMap::const_iterator it = audioClipMap.find(id->getId()); - - if (it == audioClipMap.end()) { - throw std::invalid_argument("no such audio clip"); + if (*(audioClip->getUri()) == "") { + throw std::logic_error("audio clip URI not found"); } } diff --git a/livesupport/modules/storage/src/TestStorageClient.h b/livesupport/modules/storage/src/TestStorageClient.h index 88019dd57..8c3b6e7dc 100644 --- a/livesupport/modules/storage/src/TestStorageClient.h +++ b/livesupport/modules/storage/src/TestStorageClient.h @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.10 $ + Version : $Revision: 1.11 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.h,v $ ------------------------------------------------------------------------------*/ @@ -67,7 +67,7 @@ using namespace LiveSupport::Core; * A dummy storage client, only used for test purposes. * * @author $Author: fgerlits $ - * @version $Revision: 1.10 $ + * @version $Revision: 1.11 $ */ class TestStorageClient : virtual public Configurable, @@ -168,27 +168,30 @@ class TestStorageClient : /** * Acquire the resources for the playlist. * + * Produces absolute paths prefixed by "file://"; e.g., + * "file:///some/dir/test1.mp3" and "file:///tmp/tempfileXXXX.smil". + * * @param id the id of the playlist to acquire. - * @return a path (in the local storage) to the Playlist SMIL - * temp file. + * @return a new Playlist instance containing a uri field which + * points to an executable (playable) SMIL representation of + * the playlist (in the local storage). * @exception std::invalid_argument if no playlist with the specified * specified id exists. */ - virtual Ptr::Ref + virtual Ptr::Ref acquirePlaylist(Ptr::Ref id) const throw (std::logic_error); /** * Release the resources (audio clips, other playlists) used * in a playlist. - * At this point, this does not do anything. * - * @param id the id of the playlist to release. - * @exception std::invalid_argument if no playlist with the specified - * specified id exists. + * @param playlist the playlist to release. + * @exception std::logic_error if the playlist has no uri field, + * or the file does not exist, etc. */ virtual void - releasePlaylist(Ptr::Ref id) const + releasePlaylist(Ptr::Ref playlist) const throw (std::logic_error); /** @@ -244,25 +247,28 @@ class TestStorageClient : /** * Acquire the resources for the audio clip with the specified id. * + * Assumes URIs in the config file are relative paths prefixed by + * "file:"; e.g., "file:var/test1.mp3". + * * @param id the id of the audio clip to acquire. - * @return a URI to the audio clip. + * @return a new AudioClip instance, containing a uri field which + * points to (a way of getting) the sound file. * @exception std::invalid_argument if no audio clip with the * specified id exists. */ - virtual Ptr::Ref + virtual Ptr::Ref acquireAudioClip(Ptr::Ref id) const throw (std::logic_error); - + /** - * Release the lock on an audio clip with the specified id. - * At this point, this does not do anything. + * Release the resource (sound file) used by an audio clip. * * @param id the id of the audio clip to release. - * @exception std::invalid_argument if no audio clip with the - * specified id exists. + * @exception std::logic_error if the audio clip has no uri field, + * or the file does not exist, etc. */ virtual void - releaseAudioClip(Ptr::Ref id) const + releaseAudioClip(Ptr::Ref audioClip) const throw (std::logic_error); /** diff --git a/livesupport/modules/storage/src/TestStorageClientTest.cxx b/livesupport/modules/storage/src/TestStorageClientTest.cxx index b12268856..5cb4b68e5 100644 --- a/livesupport/modules/storage/src/TestStorageClientTest.cxx +++ b/livesupport/modules/storage/src/TestStorageClientTest.cxx @@ -22,7 +22,7 @@ Author : $Author: fgerlits $ - Version : $Revision: 1.9 $ + Version : $Revision: 1.10 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClientTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -215,20 +215,23 @@ TestStorageClientTest :: acquireAudioClipTest(void) { Ptr::Ref id2(new UniqueId(10002)); Ptr::Ref id77(new UniqueId(10077)); - Ptr::Ref audioClipPath; + Ptr::Ref audioClip; try { - audioClipPath = tsc->acquireAudioClip(id2); + audioClip = tsc->acquireAudioClip(id2); } catch (std::logic_error &e) { std::string eMsg = "could not acquire audio clip:\n"; eMsg += e.what(); CPPUNIT_FAIL(eMsg); } - CPPUNIT_ASSERT(*audioClipPath == "var/test10002.mp3"); + string audioClipUri("file://"); + audioClipUri += get_current_dir_name(); + audioClipUri += "/var/test2.mp3"; + CPPUNIT_ASSERT(*(audioClip->getUri()) == audioClipUri); try { - tsc->releaseAudioClip(id2); + tsc->releaseAudioClip(audioClip); } catch (std::logic_error &e) { std::string eMsg = "could not release audio clip:\n"; @@ -237,11 +240,11 @@ TestStorageClientTest :: acquireAudioClipTest(void) } try { - audioClipPath = tsc->acquireAudioClip(id77); + audioClip = tsc->acquireAudioClip(id77); CPPUNIT_FAIL("allowed to acquire non-existent audio clip"); } catch (std::logic_error &e) { - } + } } @@ -254,29 +257,31 @@ TestStorageClientTest :: acquirePlaylistTest(void) { Ptr::Ref id1(new UniqueId(1)); Ptr::Ref id77(new UniqueId(77)); - Ptr::Ref playlistPath; + Ptr::Ref playlist; try { - playlistPath = tsc->acquirePlaylist(id1); + playlist = tsc->acquirePlaylist(id1); } catch (std::logic_error &e) { std::string eMsg = "could not acquire playlist:\n"; eMsg += e.what(); CPPUNIT_FAIL(eMsg); } - CPPUNIT_ASSERT(*playlistPath == "var/tempPlaylist1.smil"); + CPPUNIT_ASSERT(playlist->getUri()); + CPPUNIT_ASSERT(playlist->getUri()->substr(0,7) == "file://"); try { - std::FILE * f = fopen(playlistPath->c_str(), "r"); + std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r"); CPPUNIT_ASSERT(f); std::fclose(f); } catch (std::exception &e) { CPPUNIT_FAIL("temp file not created correctly"); } - + + string savedTempFilePath = playlist->getUri()->substr(7); try { - tsc->releasePlaylist(id1); + tsc->releasePlaylist(playlist); } catch (std::logic_error &e) { std::string eMsg = "could not release playlist:\n"; @@ -284,17 +289,17 @@ TestStorageClientTest :: acquirePlaylistTest(void) CPPUNIT_FAIL(eMsg); } try { - std::FILE * f = fopen(playlistPath->c_str(), "r"); + std::FILE * f = fopen(savedTempFilePath.c_str(), "r"); CPPUNIT_ASSERT(!f); } catch (std::exception &e) { - CPPUNIT_FAIL("temp file not created correctly"); + CPPUNIT_FAIL("temp file not destroyed correctly"); } try { - playlistPath = tsc->acquirePlaylist(id77); + playlist = tsc->acquirePlaylist(id77); CPPUNIT_FAIL("allowed to acquire non-existent playlist"); } catch (std::logic_error &e) { - } + } } diff --git a/livesupport/modules/storage/var/test1.mp3 b/livesupport/modules/storage/var/test1.mp3 new file mode 100644 index 000000000..db102996b Binary files /dev/null and b/livesupport/modules/storage/var/test1.mp3 differ diff --git a/livesupport/modules/storage/var/test2.mp3 b/livesupport/modules/storage/var/test2.mp3 new file mode 100644 index 000000000..1219a9056 Binary files /dev/null and b/livesupport/modules/storage/var/test2.mp3 differ diff --git a/livesupport/modules/storage/var/test3.mp3 b/livesupport/modules/storage/var/test3.mp3 new file mode 100644 index 000000000..c0685a294 Binary files /dev/null and b/livesupport/modules/storage/var/test3.mp3 differ