Bug 1050667 - fix the non-synchronous waiting state between MediaDecoderStataMachine and MediaOmxReader and OmxDecoder. r=sotaro

This commit is contained in:
Benjamin Chen 2014-10-06 11:03:14 +08:00
parent fcb59ba611
commit 2455f0ac2b
7 changed files with 71 additions and 25 deletions

View File

@ -88,6 +88,9 @@ public:
virtual bool HasAudio() = 0;
virtual bool HasVideo() = 0;
// A function that is called before ReadMetadata() call.
virtual void PreReadMetadata() {};
// Read header data for all bitstreams in the file. Fills aInfo with
// the data required to present the media, and optionally fills *aTags
// with tag metadata from the file.

View File

@ -1421,11 +1421,21 @@ void MediaDecoderStateMachine::StartWaitForResources()
void MediaDecoderStateMachine::NotifyWaitingForResourcesStatusChanged()
{
AssertCurrentThreadInMonitor();
if (mState != DECODER_STATE_WAIT_FOR_RESOURCES ||
mReader->IsWaitingMediaResources()) {
DECODER_LOG("NotifyWaitingForResourcesStatusChanged");
RefPtr<nsIRunnable> task(
NS_NewRunnableMethod(this,
&MediaDecoderStateMachine::DoNotifyWaitingForResourcesStatusChanged));
mDecodeTaskQueue->Dispatch(task);
}
void MediaDecoderStateMachine::DoNotifyWaitingForResourcesStatusChanged()
{
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
if (mState != DECODER_STATE_WAIT_FOR_RESOURCES) {
return;
}
DECODER_LOG("NotifyWaitingForResourcesStatusChanged");
DECODER_LOG("DoNotifyWaitingForResourcesStatusChanged");
// The reader is no longer waiting for resources (say a hardware decoder),
// we can now proceed to decode metadata.
SetState(DECODER_STATE_DECODING_NONE);
@ -1910,6 +1920,8 @@ nsresult MediaDecoderStateMachine::DecodeMetadata()
MOZ_ASSERT(mState == DECODER_STATE_DECODING_METADATA);
DECODER_LOG("Decoding Media Headers");
mReader->PreReadMetadata();
if (mReader->IsWaitingMediaResources()) {
StartWaitForResources();
return NS_OK;

View File

@ -327,9 +327,9 @@ public:
// be held.
bool IsPlaying();
// Dispatch DoNotifyWaitingForResourcesStatusChanged task to mDecodeTaskQueue.
// Called when the reader may have acquired the hardware resources required
// to begin decoding. The state machine may move into DECODING_METADATA if
// appropriate. The decoder monitor must be held while calling this.
// to begin decoding. The decoder monitor must be held while calling this.
void NotifyWaitingForResourcesStatusChanged();
// Notifies the state machine that should minimize the number of samples
@ -639,6 +639,10 @@ protected:
// Called by the AudioSink to signal errors.
void OnAudioSinkError();
// The state machine may move into DECODING_METADATA if we are in
// DECODER_STATE_WAIT_FOR_RESOURCES.
void DoNotifyWaitingForResourcesStatusChanged();
// The decoder object that created this state machine. The state machine
// holds a strong reference to the decoder to ensure that the decoder stays
// alive once media element has started the decoder shutdown process, and has

View File

@ -147,6 +147,7 @@ MediaOmxReader::MediaOmxReader(AbstractMediaDecoder *aDecoder)
, mUseParserDuration(false)
, mLastParserDuration(-1)
, mIsShutdown(false)
, mIsWaitingResources(false)
{
#ifdef PR_LOGGING
if (!gMediaDecoderLog) {
@ -188,10 +189,16 @@ void MediaOmxReader::Shutdown()
bool MediaOmxReader::IsWaitingMediaResources()
{
if (!mOmxDecoder.get()) {
return false;
return mIsWaitingResources;
}
void MediaOmxReader::UpdateIsWaitingMediaResources()
{
if (mOmxDecoder.get()) {
mIsWaitingResources = mOmxDecoder->IsWaitingMediaResources();
} else {
mIsWaitingResources = false;
}
return mOmxDecoder->IsWaitingMediaResources();
}
bool MediaOmxReader::IsDormantNeeded()
@ -238,6 +245,11 @@ nsresult MediaOmxReader::InitOmxDecoder()
return NS_OK;
}
void MediaOmxReader::PreReadMetadata()
{
UpdateIsWaitingMediaResources();
}
nsresult MediaOmxReader::ReadMetadata(MediaInfo* aInfo,
MetadataTags** aTags)
{
@ -261,13 +273,21 @@ nsresult MediaOmxReader::ReadMetadata(MediaInfo* aInfo,
ProcessCachedData(0, true);
}
if (!mOmxDecoder->TryLoad()) {
if (!mOmxDecoder->AllocateMediaResources()) {
return NS_ERROR_FAILURE;
}
// Bug 1050667, both MediaDecoderStateMachine and MediaOmxReader
// relies on IsWaitingMediaResources() function. And the waiting state will be
// changed by binder thread, so we store the waiting state in a cache value to
// make them in consistent state.
UpdateIsWaitingMediaResources();
if (IsWaitingMediaResources()) {
return NS_OK;
}
// After resources are available, set the metadata.
if (!mOmxDecoder->EnsureMetadata()) {
return NS_ERROR_FAILURE;
}
if (isMP3 && mMP3FrameParser.IsMP3()) {
int64_t duration = mMP3FrameParser.GetDuration();

View File

@ -47,6 +47,11 @@ protected:
android::sp<android::OmxDecoder> mOmxDecoder;
android::sp<android::MediaExtractor> mExtractor;
MP3FrameParser mMP3FrameParser;
// A cache value updated by UpdateIsWaitingMediaResources(), makes the
// "waiting resources state" is synchronous to StateMachine.
bool mIsWaitingResources;
// Called by ReadMetadata() during MediaDecoderStateMachine::DecodeMetadata()
// on decode thread. It create and initialize the OMX decoder including
// setting up custom extractor. The extractor provide the essential
@ -57,6 +62,11 @@ protected:
// to activate the decoder automatically.
virtual void EnsureActive();
// Check the underlying HW resources are available and store the result in
// mIsWaitingResources. The result might be changed by binder thread,
// Can only called by ReadMetadata.
void UpdateIsWaitingMediaResources();
public:
MediaOmxReader(AbstractMediaDecoder* aDecoder);
~MediaOmxReader();
@ -79,11 +89,13 @@ public:
return mHasVideo;
}
virtual bool IsWaitingMediaResources();
// Return mIsWaitingResources.
virtual bool IsWaitingMediaResources() MOZ_OVERRIDE;
virtual bool IsDormantNeeded();
virtual void ReleaseMediaResources();
virtual void PreReadMetadata() MOZ_OVERRIDE;
virtual nsresult ReadMetadata(MediaInfo* aInfo,
MetadataTags** aTags);
virtual nsresult Seek(int64_t aTime, int64_t aStartTime, int64_t aEndTime, int64_t aCurrentTime);

View File

@ -162,19 +162,7 @@ bool OmxDecoder::Init(sp<MediaExtractor>& extractor) {
return true;
}
bool OmxDecoder::TryLoad() {
if (!AllocateMediaResources()) {
return false;
}
//check if video is waiting resources
if (mVideoSource.get()) {
if (mVideoSource->IsWaitingResources()) {
return true;
}
}
bool OmxDecoder::EnsureMetadata() {
// calculate duration
int64_t totalDurationUs = 0;
int64_t durationUs = 0;

View File

@ -152,9 +152,16 @@ public:
// Note: RTSP requires a custom extractor because it doesn't have a container.
bool Init(sp<MediaExtractor>& extractor);
bool TryLoad();
bool IsDormantNeeded();
// Called after resources(video/audio codec) are allocated, set the
// mDurationUs and video/audio metadata.
bool EnsureMetadata();
// Only called by MediaOmxDecoder, do not call this function arbitrarily.
// See bug 1050667.
bool IsWaitingMediaResources();
bool AllocateMediaResources();
void ReleaseMediaResources();
bool SetVideoFormat();