From 2703867eb28417d35c2128066f9312cea53b9d3d Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Fri, 19 Jul 2013 16:40:57 +0200 Subject: [PATCH] Bug 882543 - Don't spam the main thread when rendering an offline graph. r=ehsan,roc --- content/media/MediaStreamGraph.cpp | 53 ++++++++++++++++++++++++------------ content/media/MediaStreamGraphImpl.h | 10 +++++++ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/content/media/MediaStreamGraph.cpp b/content/media/MediaStreamGraph.cpp index a750f714b862..eed493eedc15 100644 --- a/content/media/MediaStreamGraph.cpp +++ b/content/media/MediaStreamGraph.cpp @@ -898,28 +898,47 @@ MediaStreamGraphImpl::PlayVideo(MediaStream* aStream) } } +bool +MediaStreamGraphImpl::ShouldUpdateMainThread() +{ + if (mRealtime) { + return true; + } + + TimeStamp now = TimeStamp::Now(); + if ((now - mLastMainThreadUpdate).ToMilliseconds() > MEDIA_GRAPH_TARGET_PERIOD_MS) { + mLastMainThreadUpdate = now; + return true; + } + return false; +} + void MediaStreamGraphImpl::PrepareUpdatesToMainThreadState(bool aFinalUpdate) { mMonitor.AssertCurrentThreadOwns(); - mStreamUpdates.SetCapacity(mStreamUpdates.Length() + mStreams.Length()); - for (uint32_t i = 0; i < mStreams.Length(); ++i) { - MediaStream* stream = mStreams[i]; - if (!stream->MainThreadNeedsUpdates()) { - continue; + // We don't want to update the main thread about timing update when we are not + // running in realtime. + if (ShouldUpdateMainThread()) { + mStreamUpdates.SetCapacity(mStreamUpdates.Length() + mStreams.Length()); + for (uint32_t i = 0; i < mStreams.Length(); ++i) { + MediaStream* stream = mStreams[i]; + if (!stream->MainThreadNeedsUpdates()) { + continue; + } + StreamUpdate* update = mStreamUpdates.AppendElement(); + update->mGraphUpdateIndex = stream->mGraphUpdateIndices.GetAt(mCurrentTime); + update->mStream = stream; + update->mNextMainThreadCurrentTime = + GraphTimeToStreamTime(stream, mCurrentTime); + update->mNextMainThreadFinished = + stream->mFinished && + StreamTimeToGraphTime(stream, stream->GetBufferEnd()) <= mCurrentTime; + } + if (!mPendingUpdateRunnables.IsEmpty()) { + mUpdateRunnables.MoveElementsFrom(mPendingUpdateRunnables); } - StreamUpdate* update = mStreamUpdates.AppendElement(); - update->mGraphUpdateIndex = stream->mGraphUpdateIndices.GetAt(mCurrentTime); - update->mStream = stream; - update->mNextMainThreadCurrentTime = - GraphTimeToStreamTime(stream, mCurrentTime); - update->mNextMainThreadFinished = - stream->mFinished && - StreamTimeToGraphTime(stream, stream->GetBufferEnd()) <= mCurrentTime; - } - if (!mPendingUpdateRunnables.IsEmpty()) { - mUpdateRunnables.MoveElementsFrom(mPendingUpdateRunnables); } // Don't send the message to the main thread if it's not going to have @@ -2178,7 +2197,7 @@ MediaStreamGraphImpl::MediaStreamGraphImpl(bool aRealtime) } #endif - mCurrentTimeStamp = mInitialTimeStamp = TimeStamp::Now(); + mCurrentTimeStamp = mInitialTimeStamp = mLastMainThreadUpdate = TimeStamp::Now(); } NS_IMPL_ISUPPORTS1(MediaStreamGraphShutdownObserver, nsIObserver) diff --git a/content/media/MediaStreamGraphImpl.h b/content/media/MediaStreamGraphImpl.h index 4a1f4c8ed475..dcc9ba719aa9 100644 --- a/content/media/MediaStreamGraphImpl.h +++ b/content/media/MediaStreamGraphImpl.h @@ -190,6 +190,12 @@ public: * mMonitor must be held. */ void PrepareUpdatesToMainThreadState(bool aFinalUpdate); + /** + * If we are rendering in non-realtime mode, we don't want to send messages to + * the main thread at each iteration for performance reasons. We instead + * notify the main thread at the same rate + */ + bool ShouldUpdateMainThread(); // The following methods are the various stages of RunThread processing. /** * Compute a new current time for the graph and advance all on-graph-thread @@ -395,6 +401,10 @@ public: */ TimeStamp mCurrentTimeStamp; /** + * Date of the last time we updated the main thread with the graph state. + */ + TimeStamp mLastMainThreadUpdate; + /** * Which update batch we are currently processing. */ int64_t mProcessingGraphUpdateIndex; -- 2.11.4.GIT