Bug 1163467. Part 4 - move MediaDecoder::UpdateStreamBlockingForStateMachinePlaying to MediaDecoderStateMachine. r=roc.

This commit is contained in:
JW Wang 2015-05-28 14:16:42 +08:00
parent 9b051192c9
commit ee6ccb5856
5 changed files with 57 additions and 44 deletions

View File

@ -86,6 +86,8 @@ DecodedStreamData::DecodedStreamData(int64_t aInitialTime,
{
mListener = new DecodedStreamGraphListener(mStream);
mStream->AddListener(mListener);
// Block the stream until the initialization is done.
mStream->ChangeExplicitBlockerCount(1);
}
DecodedStreamData::~DecodedStreamData()

View File

@ -304,28 +304,6 @@ void MediaDecoder::UpdateStreamBlockingForPlayState()
}
}
void MediaDecoder::UpdateStreamBlockingForStateMachinePlaying()
{
GetReentrantMonitor().AssertCurrentThreadIn();
if (!GetDecodedStream()) {
return;
}
bool blockForStateMachineNotPlaying =
mDecoderStateMachine && !mDecoderStateMachine->IsPlaying();
if (blockForStateMachineNotPlaying != GetDecodedStream()->mHaveBlockedForStateMachineNotPlaying) {
GetDecodedStream()->mHaveBlockedForStateMachineNotPlaying = blockForStateMachineNotPlaying;
int32_t delta = blockForStateMachineNotPlaying ? 1 : -1;
if (NS_IsMainThread()) {
GetDecodedStream()->mStream->ChangeExplicitBlockerCount(delta);
} else {
nsCOMPtr<nsIRunnable> runnable =
NS_NewRunnableMethodWithArg<int32_t>(GetDecodedStream()->mStream.get(),
&MediaStream::ChangeExplicitBlockerCount, delta);
NS_DispatchToMainThread(runnable);
}
}
}
void MediaDecoder::RecreateDecodedStream(int64_t aStartTimeUSecs,
MediaStreamGraph* aGraph)
{
@ -334,7 +312,6 @@ void MediaDecoder::RecreateDecodedStream(int64_t aStartTimeUSecs,
DECODER_LOG("RecreateDecodedStream aStartTimeUSecs=%lld!", aStartTimeUSecs);
mDecodedStream.RecreateData(aStartTimeUSecs, aGraph);
UpdateStreamBlockingForStateMachinePlaying();
UpdateStreamBlockingForPlayState();
}

View File

@ -405,11 +405,6 @@ public:
*/
void RecreateDecodedStream(int64_t aStartTimeUSecs,
MediaStreamGraph* aGraph = nullptr);
/**
* Call this when mDecoderStateMachine or mDecoderStateMachine->IsPlaying() changes.
* Decoder monitor must be held.
*/
void UpdateStreamBlockingForStateMachinePlaying();
DecodedStreamData* GetDecodedStream()
{

View File

@ -422,6 +422,19 @@ static bool ZeroDurationAtLastChunk(VideoSegment& aInput)
return lastVideoStratTime == aInput.GetDuration();
}
static void
UpdateStreamBlocking(MediaStream* aStream, bool aBlocking)
{
int32_t delta = aBlocking ? 1 : -1;
if (NS_IsMainThread()) {
aStream->ChangeExplicitBlockerCount(delta);
} else {
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethodWithArg<int32_t>(
aStream, &MediaStream::ChangeExplicitBlockerCount, delta);
AbstractThread::MainThread()->Dispatch(r.forget());
}
}
void MediaDecoderStateMachine::SendStreamData()
{
MOZ_ASSERT(OnTaskQueue());
@ -463,6 +476,11 @@ void MediaDecoderStateMachine::SendStreamData()
}
mediaStream->FinishAddTracks();
stream->mStreamInitialized = true;
// Make sure stream blocking is updated before sending stream data so we
// don't 'leak' data when the stream is supposed to be blocked.
UpdateStreamBlockingForStateMachinePlaying();
UpdateStreamBlocking(mediaStream, false);
}
if (mInfo.HasAudio()) {
@ -1271,7 +1289,7 @@ void MediaDecoderStateMachine::StopPlayback()
// so it can pause audio playback.
mDecoder->GetReentrantMonitor().NotifyAll();
NS_ASSERTION(!IsPlaying(), "Should report not playing at end of StopPlayback()");
mDecoder->UpdateStreamBlockingForStateMachinePlaying();
UpdateStreamBlockingForStateMachinePlaying();
DispatchDecodeTasksIfNeeded();
}
@ -1309,7 +1327,7 @@ void MediaDecoderStateMachine::MaybeStartPlayback()
NS_ENSURE_SUCCESS_VOID(rv);
mDecoder->GetReentrantMonitor().NotifyAll();
mDecoder->UpdateStreamBlockingForStateMachinePlaying();
UpdateStreamBlockingForStateMachinePlaying();
DispatchDecodeTasksIfNeeded();
}
@ -3487,6 +3505,37 @@ uint32_t MediaDecoderStateMachine::GetAmpleVideoFrames() const
: std::max<uint32_t>(sVideoQueueDefaultSize, MIN_VIDEO_QUEUE_SIZE);
}
void MediaDecoderStateMachine::DispatchAudioCaptured()
{
nsRefPtr<MediaDecoderStateMachine> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([self] () -> void
{
MOZ_ASSERT(self->OnTaskQueue());
ReentrantMonitorAutoEnter mon(self->mDecoder->GetReentrantMonitor());
if (!self->mAudioCaptured) {
self->mAudioCaptured = true;
self->ScheduleStateMachine();
}
});
TaskQueue()->Dispatch(r.forget());
}
void MediaDecoderStateMachine::UpdateStreamBlockingForStateMachinePlaying()
{
AssertCurrentThreadInMonitor();
auto stream = mDecoder->GetDecodedStream();
if (!stream) {
return;
}
bool blocking = !IsPlaying();
if (blocking != stream->mHaveBlockedForStateMachineNotPlaying) {
stream->mHaveBlockedForStateMachineNotPlaying = blocking;
UpdateStreamBlocking(stream->mStream, blocking);
}
}
} // namespace mozilla
// avoid redefined macro in unified build

View File

@ -145,20 +145,7 @@ public:
return mState;
}
void DispatchAudioCaptured()
{
nsRefPtr<MediaDecoderStateMachine> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([self] () -> void
{
MOZ_ASSERT(self->OnTaskQueue());
ReentrantMonitorAutoEnter mon(self->mDecoder->GetReentrantMonitor());
if (!self->mAudioCaptured) {
self->mAudioCaptured = true;
self->ScheduleStateMachine();
}
});
TaskQueue()->Dispatch(r.forget());
}
void DispatchAudioCaptured();
// Check if the decoder needs to become dormant state.
bool IsDormantNeeded();
@ -171,6 +158,9 @@ private:
// constructor immediately after the task queue is created.
void InitializationTask();
// Call this IsPlaying() changes. Decoder monitor must be held.
void UpdateStreamBlockingForStateMachinePlaying();
void Shutdown();
public: