diff options
Diffstat (limited to 'indra/llaudio/llstreamingaudio_fmodstudio.cpp')
-rw-r--r-- | indra/llaudio/llstreamingaudio_fmodstudio.cpp | 124 |
1 files changed, 96 insertions, 28 deletions
diff --git a/indra/llaudio/llstreamingaudio_fmodstudio.cpp b/indra/llaudio/llstreamingaudio_fmodstudio.cpp index 08d19209aa..1ad29a3f59 100644 --- a/indra/llaudio/llstreamingaudio_fmodstudio.cpp +++ b/indra/llaudio/llstreamingaudio_fmodstudio.cpp @@ -63,7 +63,8 @@ LLStreamingAudio_FMODSTUDIO::LLStreamingAudio_FMODSTUDIO(FMOD::System *system) : mSystem(system), mCurrentInternetStreamp(NULL), mFMODInternetStreamChannelp(NULL), -mGain(1.0f) +mGain(1.0f), +mRetryCount(0) { // Number of milliseconds of audio to buffer for the audio card. // Must be larger than the usual Second Life frame stutter time. @@ -83,9 +84,64 @@ mGain(1.0f) LLStreamingAudio_FMODSTUDIO::~LLStreamingAudio_FMODSTUDIO() { - // nothing interesting/safe to do. + if (mCurrentInternetStreamp) + { + // Isn't supposed to hapen, stream should be clear by now, + // and if it does, we are likely going to crash. + LL_WARNS("FMOD") << "mCurrentInternetStreamp not null on shutdown!" << LL_ENDL; + stop(); + } + + // Kill dead internet streams, if possible + killDeadStreams(); + + if (!mDeadStreams.empty()) + { + // LLStreamingAudio_FMODSTUDIO was inited on startup + // and should be destroyed on shutdown, it should + // wait for streams to die to not cause crashes or + // leaks. + // Ideally we need to wait on some kind of callback + // to release() streams correctly, but 200 ms should + // be enough and we can't wait forever. + LL_INFOS("FMOD") << "Waiting for " << (S32)mDeadStreams.size() << " streams to stop" << LL_ENDL; + for (S32 i = 0; i < 20; i++) + { + const U32 ms_delay = 10; + ms_sleep(ms_delay); // rude, but not many options here + killDeadStreams(); + if (mDeadStreams.empty()) + { + LL_INFOS("FMOD") << "All streams stopped after " << (S32)((i + 1) * ms_delay) << "ms" << LL_ENDL; + break; + } + } + } + + if (!mDeadStreams.empty()) + { + LL_WARNS("FMOD") << "Failed to kill some audio streams" << LL_ENDL; + } } +void LLStreamingAudio_FMODSTUDIO::killDeadStreams() +{ + std::list<LLAudioStreamManagerFMODSTUDIO *>::iterator iter; + for (iter = mDeadStreams.begin(); iter != mDeadStreams.end();) + { + LLAudioStreamManagerFMODSTUDIO *streamp = *iter; + if (streamp->stopStream()) + { + LL_INFOS("FMOD") << "Closed dead stream" << LL_ENDL; + delete streamp; + mDeadStreams.erase(iter++); + } + else + { + iter++; + } + } +} void LLStreamingAudio_FMODSTUDIO::start(const std::string& url) { @@ -100,36 +156,24 @@ void LLStreamingAudio_FMODSTUDIO::start(const std::string& url) if (!url.empty()) { - LL_INFOS() << "Starting internet stream: " << url << LL_ENDL; + LL_INFOS("FMOD") << "Starting internet stream: " << url << LL_ENDL; mCurrentInternetStreamp = new LLAudioStreamManagerFMODSTUDIO(mSystem, url); mURL = url; } else { - LL_INFOS() << "Set internet stream to null" << LL_ENDL; + LL_INFOS("FMOD") << "Set internet stream to null" << LL_ENDL; mURL.clear(); } + + mRetryCount = 0; } void LLStreamingAudio_FMODSTUDIO::update() { // Kill dead internet streams, if possible - std::list<LLAudioStreamManagerFMODSTUDIO *>::iterator iter; - for (iter = mDeadStreams.begin(); iter != mDeadStreams.end();) - { - LLAudioStreamManagerFMODSTUDIO *streamp = *iter; - if (streamp->stopStream()) - { - LL_INFOS() << "Closed dead stream" << LL_ENDL; - delete streamp; - mDeadStreams.erase(iter++); - } - else - { - iter++; - } - } + killDeadStreams(); // Don't do anything if there are no streams playing if (!mCurrentInternetStreamp) @@ -154,10 +198,33 @@ void LLStreamingAudio_FMODSTUDIO::update() setGain(getGain()); mFMODInternetStreamChannelp->setPaused(false); } + mRetryCount = 0; } else if (open_state == FMOD_OPENSTATE_ERROR) { - stop(); + LL_INFOS("FMOD") << "State: FMOD_OPENSTATE_ERROR" + << " Progress: " << U32(progress) + << " Starving: " << S32(starving) + << " Diskbusy: " << S32(diskbusy) << LL_ENDL; + if (mRetryCount < 2) + { + // Retry + std::string url = mURL; + stop(); // might drop mURL, drops mCurrentInternetStreamp + + mRetryCount++; + + if (!url.empty()) + { + LL_INFOS("FMOD") << "Restarting internet stream: " << url << ", attempt " << (mRetryCount + 1) << LL_ENDL; + mCurrentInternetStreamp = new LLAudioStreamManagerFMODSTUDIO(mSystem, url); + mURL = url; + } + } + else + { + stop(); + } return; } @@ -181,7 +248,7 @@ void LLStreamingAudio_FMODSTUDIO::update() { if (!strcmp(tag.name, "Sample Rate Change")) { - LL_INFOS() << "Stream forced changing sample rate to " << *((float *)tag.data) << LL_ENDL; + LL_INFOS("FMOD") << "Stream forced changing sample rate to " << *((float *)tag.data) << LL_ENDL; mFMODInternetStreamChannelp->setFrequency(*((float *)tag.data)); } continue; @@ -195,9 +262,9 @@ void LLStreamingAudio_FMODSTUDIO::update() mFMODInternetStreamChannelp->getPaused(&paused); if (!paused) { - LL_INFOS() << "Stream starvation detected! Pausing stream until buffer nearly full." << LL_ENDL; - LL_INFOS() << " (diskbusy=" << diskbusy << ")" << LL_ENDL; - LL_INFOS() << " (progress=" << progress << ")" << LL_ENDL; + LL_INFOS("FMOD") << "Stream starvation detected! Pausing stream until buffer nearly full." << LL_ENDL; + LL_INFOS("FMOD") << " (diskbusy=" << diskbusy << ")" << LL_ENDL; + LL_INFOS("FMOD") << " (progress=" << progress << ")" << LL_ENDL; mFMODInternetStreamChannelp->setPaused(true); } } @@ -220,14 +287,14 @@ void LLStreamingAudio_FMODSTUDIO::stop() if (mCurrentInternetStreamp) { - LL_INFOS() << "Stopping internet stream: " << mCurrentInternetStreamp->getURL() << LL_ENDL; + LL_INFOS("FMOD") << "Stopping internet stream: " << mCurrentInternetStreamp->getURL() << LL_ENDL; if (mCurrentInternetStreamp->stopStream()) { delete mCurrentInternetStreamp; } else { - LL_WARNS() << "Pushing stream to dead list: " << mCurrentInternetStreamp->getURL() << LL_ENDL; + LL_WARNS("FMOD") << "Pushing stream to dead list: " << mCurrentInternetStreamp->getURL() << LL_ENDL; mDeadStreams.push_back(mCurrentInternetStreamp); } mCurrentInternetStreamp = NULL; @@ -246,6 +313,7 @@ void LLStreamingAudio_FMODSTUDIO::pause(int pauseopt) { if (mCurrentInternetStreamp) { + LL_INFOS("FMOD") << "Pausing internet stream" << LL_ENDL; stop(); } } @@ -314,7 +382,7 @@ mReady(false) if (result != FMOD_OK) { - LL_WARNS() << "Couldn't open fmod stream, error " + LL_WARNS("FMOD") << "Couldn't open fmod stream, error " << FMOD_ErrorString(result) << LL_ENDL; mReady = false; @@ -329,7 +397,7 @@ FMOD::Channel *LLAudioStreamManagerFMODSTUDIO::startStream() // We need a live and opened stream before we try and play it. if (!mInternetStream || getOpenState() != FMOD_OPENSTATE_READY) { - LL_WARNS() << "No internet stream to start playing!" << LL_ENDL; + LL_WARNS("FMOD") << "No internet stream to start playing!" << LL_ENDL; return NULL; } |