Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / content / media / MediaDecoderStateMachine.h
blobb82374b10284976c24d1d668ff1187f8086555a3
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 Each video element for a media file has two threads:
9 1) The Audio thread writes the decoded audio data to the audio
10 hardware. This is done in a separate thread to ensure that the
11 audio hardware gets a constant stream of data without
12 interruption due to decoding or display. At some point
13 AudioStream will be refactored to have a callback interface
14 where it asks for data and an extra thread will no longer be
15 needed.
17 2) The decode thread. This thread reads from the media stream and
18 decodes the Theora and Vorbis data. It places the decoded data into
19 queues for the other threads to pull from.
21 All file reads, seeks, and all decoding must occur on the decode thread.
22 Synchronisation of state between the thread is done via a monitor owned
23 by MediaDecoder.
25 The lifetime of the decode and audio threads is controlled by the state
26 machine when it runs on the shared state machine thread. When playback
27 needs to occur they are created and events dispatched to them to run
28 them. These events exit when decoding/audio playback is completed or
29 no longer required.
31 A/V synchronisation is handled by the state machine. It examines the audio
32 playback time and compares this to the next frame in the queue of video
33 frames. If it is time to play the video frame it is then displayed, otherwise
34 it schedules the state machine to run again at the time of the next frame.
36 Frame skipping is done in the following ways:
38 1) The state machine will skip all frames in the video queue whose
39 display time is less than the current audio time. This ensures
40 the correct frame for the current time is always displayed.
42 2) The decode thread will stop decoding interframes and read to the
43 next keyframe if it determines that decoding the remaining
44 interframes will cause playback issues. It detects this by:
45 a) If the amount of audio data in the audio queue drops
46 below a threshold whereby audio may start to skip.
47 b) If the video queue drops below a threshold where it
48 will be decoding video data that won't be displayed due
49 to the decode thread dropping the frame immediately.
51 When hardware accelerated graphics is not available, YCbCr conversion
52 is done on the decode thread when video frames are decoded.
54 The decode thread pushes decoded audio and videos frames into two
55 separate queues - one for audio and one for video. These are kept
56 separate to make it easy to constantly feed audio data to the audio
57 hardware while allowing frame skipping of video data. These queues are
58 threadsafe, and neither the decode, audio, or state machine should
59 be able to monopolize them, and cause starvation of the other threads.
61 Both queues are bounded by a maximum size. When this size is reached
62 the decode thread will no longer decode video or audio depending on the
63 queue that has reached the threshold. If both queues are full, the decode
64 thread will wait on the decoder monitor.
66 When the decode queues are full (they've reaced their maximum size) and
67 the decoder is not in PLAYING play state, the state machine may opt
68 to shut down the decode thread in order to conserve resources.
70 During playback the audio thread will be idle (via a Wait() on the
71 monitor) if the audio queue is empty. Otherwise it constantly pops
72 audio data off the queue and plays it with a blocking write to the audio
73 hardware (via AudioStream).
76 #if !defined(MediaDecoderStateMachine_h__)
77 #define MediaDecoderStateMachine_h__
79 #include "mozilla/Attributes.h"
80 #include "nsThreadUtils.h"
81 #include "MediaDecoder.h"
82 #include "AudioAvailableEventManager.h"
83 #include "mozilla/ReentrantMonitor.h"
84 #include "MediaDecoderReader.h"
85 #include "MediaDecoderOwner.h"
86 #include "MediaMetadataManager.h"
88 class nsITimer;
90 namespace mozilla {
92 class AudioSegment;
93 class VideoSegment;
96 The state machine class. This manages the decoding and seeking in the
97 MediaDecoderReader on the decode thread, and A/V sync on the shared
98 state machine thread, and controls the audio "push" thread.
100 All internal state is synchronised via the decoder monitor. State changes
101 are either propagated by NotifyAll on the monitor (typically when state
102 changes need to be propagated to non-state machine threads) or by scheduling
103 the state machine to run another cycle on the shared state machine thread.
105 See MediaDecoder.h for more details.
107 class MediaDecoderStateMachine : public nsRunnable
109 public:
110 typedef MediaDecoder::DecodedStreamData DecodedStreamData;
111 MediaDecoderStateMachine(MediaDecoder* aDecoder,
112 MediaDecoderReader* aReader,
113 bool aRealTime = false);
114 ~MediaDecoderStateMachine();
116 // nsDecoderStateMachine interface
117 nsresult Init(MediaDecoderStateMachine* aCloneDonor);
119 // Enumeration for the valid decoding states
120 enum State {
121 DECODER_STATE_DECODING_METADATA,
122 DECODER_STATE_WAIT_FOR_RESOURCES,
123 DECODER_STATE_DORMANT,
124 DECODER_STATE_DECODING,
125 DECODER_STATE_SEEKING,
126 DECODER_STATE_BUFFERING,
127 DECODER_STATE_COMPLETED,
128 DECODER_STATE_SHUTDOWN
131 State GetState() {
132 AssertCurrentThreadInMonitor();
133 return mState;
136 // Set the audio volume. The decoder monitor must be obtained before
137 // calling this.
138 void SetVolume(double aVolume);
139 void SetAudioCaptured(bool aCapture);
141 // Check if the decoder needs to become dormant state.
142 bool IsDormantNeeded();
143 // Set/Unset dormant state.
144 void SetDormant(bool aDormant);
145 void Shutdown();
147 // Called from the main thread to get the duration. The decoder monitor
148 // must be obtained before calling this. It is in units of microseconds.
149 int64_t GetDuration();
151 // Called from the main thread to set the duration of the media resource
152 // if it is able to be obtained via HTTP headers. Called from the
153 // state machine thread to set the duration if it is obtained from the
154 // media metadata. The decoder monitor must be obtained before calling this.
155 // aDuration is in microseconds.
156 void SetDuration(int64_t aDuration);
158 // Called while decoding metadata to set the end time of the media
159 // resource. The decoder monitor must be obtained before calling this.
160 // aEndTime is in microseconds.
161 void SetMediaEndTime(int64_t aEndTime);
163 // Called from main thread to update the duration with an estimated value.
164 // The duration is only changed if its significantly different than the
165 // the current duration, as the incoming duration is an estimate and so
166 // often is unstable as more data is read and the estimate is updated.
167 // Can result in a durationchangeevent. aDuration is in microseconds.
168 void UpdateEstimatedDuration(int64_t aDuration);
170 // Functions used by assertions to ensure we're calling things
171 // on the appropriate threads.
172 bool OnDecodeThread() const {
173 return IsCurrentThread(mDecodeThread);
175 bool OnStateMachineThread() const;
176 bool OnAudioThread() const {
177 return IsCurrentThread(mAudioThread);
180 MediaDecoderOwner::NextFrameStatus GetNextFrameStatus();
182 // Cause state transitions. These methods obtain the decoder monitor
183 // to synchronise the change of state, and to notify other threads
184 // that the state has changed.
185 void Play();
187 // Seeks to aTime in seconds.
188 void Seek(double aTime);
190 // Returns the current playback position in seconds.
191 // Called from the main thread to get the current frame time. The decoder
192 // monitor must be obtained before calling this.
193 double GetCurrentTime() const;
195 // Clear the flag indicating that a playback position change event
196 // is currently queued. This is called from the main thread and must
197 // be called with the decode monitor held.
198 void ClearPositionChangeFlag();
200 // Called from the main thread or the decoder thread to set whether the media
201 // resource can seek into unbuffered ranges. The decoder monitor must be
202 // obtained before calling this.
203 void SetTransportSeekable(bool aSeekable);
205 // Called from the main thread or the decoder thread to set whether the media
206 // can seek to random location. This is not true for chained ogg and WebM
207 // media without index. The decoder monitor must be obtained before calling
208 // this.
209 void SetMediaSeekable(bool aSeekable);
211 // Update the playback position. This can result in a timeupdate event
212 // and an invalidate of the frame being dispatched asynchronously if
213 // there is no such event currently queued.
214 // Only called on the decoder thread. Must be called with
215 // the decode monitor held.
216 void UpdatePlaybackPosition(int64_t aTime);
218 // Causes the state machine to switch to buffering state, and to
219 // immediately stop playback and buffer downloaded data. Must be called
220 // with the decode monitor held. Called on the state machine thread and
221 // the main thread.
222 void StartBuffering();
224 // State machine thread run function. Defers to RunStateMachine().
225 NS_IMETHOD Run() MOZ_OVERRIDE;
227 // This is called on the state machine thread and audio thread.
228 // The decoder monitor must be obtained before calling this.
229 bool HasAudio() const {
230 AssertCurrentThreadInMonitor();
231 return mInfo.HasAudio();
234 // This is called on the state machine thread and audio thread.
235 // The decoder monitor must be obtained before calling this.
236 bool HasVideo() const {
237 AssertCurrentThreadInMonitor();
238 return mInfo.HasVideo();
241 // Should be called by main thread.
242 bool HaveNextFrameData() const;
244 // Must be called with the decode monitor held.
245 bool IsBuffering() const {
246 AssertCurrentThreadInMonitor();
248 return mState == DECODER_STATE_BUFFERING;
251 // Must be called with the decode monitor held.
252 bool IsSeeking() const {
253 AssertCurrentThreadInMonitor();
255 return mState == DECODER_STATE_SEEKING;
258 nsresult GetBuffered(dom::TimeRanges* aBuffered);
260 void SetPlaybackRate(double aPlaybackRate);
261 void SetPreservesPitch(bool aPreservesPitch);
263 int64_t VideoQueueMemoryInUse() {
264 if (mReader) {
265 return mReader->VideoQueueMemoryInUse();
267 return 0;
270 int64_t AudioQueueMemoryInUse() {
271 if (mReader) {
272 return mReader->AudioQueueMemoryInUse();
274 return 0;
277 void NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset);
279 int64_t GetEndMediaTime() const {
280 AssertCurrentThreadInMonitor();
281 return mEndTime;
284 bool IsTransportSeekable() {
285 AssertCurrentThreadInMonitor();
286 return mTransportSeekable;
289 bool IsMediaSeekable() {
290 AssertCurrentThreadInMonitor();
291 return mMediaSeekable;
294 // Sets the current frame buffer length for the MozAudioAvailable event.
295 // Accessed on the main and state machine threads.
296 void SetFrameBufferLength(uint32_t aLength);
298 // Returns the shared state machine thread.
299 static nsIThread* GetStateMachineThread();
301 // Calls ScheduleStateMachine() after taking the decoder lock. Also
302 // notifies the decoder thread in case it's waiting on the decoder lock.
303 void ScheduleStateMachineWithLockAndWakeDecoder();
305 // Schedules the shared state machine thread to run the state machine
306 // in aUsecs microseconds from now, if it's not already scheduled to run
307 // earlier, in which case the request is discarded.
308 nsresult ScheduleStateMachine(int64_t aUsecs = 0);
310 // Creates and starts a new decode thread. Don't call this directly,
311 // request a new decode thread by calling
312 // StateMachineTracker::RequestCreateDecodeThread().
313 // The decoder monitor must not be held. Called on the state machine thread.
314 nsresult StartDecodeThread();
316 // Timer function to implement ScheduleStateMachine(aUsecs).
317 void TimeoutExpired();
319 // Set the media fragment end time. aEndTime is in microseconds.
320 void SetFragmentEndTime(int64_t aEndTime);
322 // Drop reference to decoder. Only called during shutdown dance.
323 void ReleaseDecoder() {
324 MOZ_ASSERT(mReader);
325 if (mReader) {
326 mReader->ReleaseDecoder();
328 mDecoder = nullptr;
331 // If we're playing into a MediaStream, record the current point in the
332 // MediaStream and the current point in our media resource so later we can
333 // convert MediaStream playback positions to media resource positions. Best to
334 // call this while we're not playing (while the MediaStream is blocked). Can
335 // be called on any thread with the decoder monitor held.
336 void SetSyncPointForMediaStream();
337 int64_t GetCurrentTimeViaMediaStreamSync();
339 // Called when a "MozAudioAvailable" event listener is added to the media
340 // element. Called on the main thread.
341 void NotifyAudioAvailableListener();
343 // Copy queued audio/video data in the reader to any output MediaStreams that
344 // need it.
345 void SendStreamData();
346 void FinishStreamData();
347 bool HaveEnoughDecodedAudio(int64_t aAmpleAudioUSecs);
348 bool HaveEnoughDecodedVideo();
350 // Returns true if the state machine has shutdown or is in the process of
351 // shutting down. The decoder monitor must be held while calling this.
352 bool IsShutdown();
354 void QueueMetadata(int64_t aPublishTime, int aChannels, int aRate, bool aHasAudio, bool aHasVideo, MetadataTags* aTags);
356 // Returns true if we're currently playing. The decoder monitor must
357 // be held.
358 bool IsPlaying();
360 protected:
361 virtual uint32_t GetAmpleVideoFrames() { return mAmpleVideoFrames; }
363 void AssertCurrentThreadInMonitor() const { mDecoder->GetReentrantMonitor().AssertCurrentThreadIn(); }
365 private:
366 class WakeDecoderRunnable : public nsRunnable {
367 public:
368 WakeDecoderRunnable(MediaDecoderStateMachine* aSM)
369 : mMutex("WakeDecoderRunnable"), mStateMachine(aSM) {}
370 NS_IMETHOD Run() MOZ_OVERRIDE
372 nsRefPtr<MediaDecoderStateMachine> stateMachine;
374 // Don't let Run() (called by media stream graph thread) race with
375 // Revoke() (called by decoder state machine thread)
376 MutexAutoLock lock(mMutex);
377 if (!mStateMachine)
378 return NS_OK;
379 stateMachine = mStateMachine;
381 stateMachine->ScheduleStateMachineWithLockAndWakeDecoder();
382 return NS_OK;
384 void Revoke()
386 MutexAutoLock lock(mMutex);
387 mStateMachine = nullptr;
390 Mutex mMutex;
391 // Protected by mMutex.
392 // We don't use an owning pointer here, because keeping mStateMachine alive
393 // would mean in some cases we'd have to destroy mStateMachine from this
394 // object, which would be problematic since MediaDecoderStateMachine can
395 // only be destroyed on the main thread whereas this object can be destroyed
396 // on the media stream graph thread.
397 MediaDecoderStateMachine* mStateMachine;
399 WakeDecoderRunnable* GetWakeDecoderRunnable();
401 // Returns true if we've got less than aAudioUsecs microseconds of decoded
402 // and playable data. The decoder monitor must be held.
403 bool HasLowDecodedData(int64_t aAudioUsecs) const;
405 // Returns true if we're running low on data which is not yet decoded.
406 // The decoder monitor must be held.
407 bool HasLowUndecodedData() const;
409 // Returns true if we have less than aUsecs of undecoded data available.
410 bool HasLowUndecodedData(double aUsecs) const;
412 // Returns the number of unplayed usecs of audio we've got decoded and/or
413 // pushed to the hardware waiting to play. This is how much audio we can
414 // play without having to run the audio decoder. The decoder monitor
415 // must be held.
416 int64_t AudioDecodedUsecs() const;
418 // Returns true when there's decoded audio waiting to play.
419 // The decoder monitor must be held.
420 bool HasFutureAudio() const;
422 // Returns true if we recently exited "quick buffering" mode.
423 bool JustExitedQuickBuffering();
425 // Waits on the decoder ReentrantMonitor for aUsecs microseconds. If the decoder
426 // monitor is awoken by a Notify() call, we'll continue waiting, unless
427 // we've moved into shutdown state. This enables us to ensure that we
428 // wait for a specified time, and that the myriad of Notify()s we do on
429 // the decoder monitor don't cause the audio thread to be starved. aUsecs
430 // values of less than 1 millisecond are rounded up to 1 millisecond
431 // (see bug 651023). The decoder monitor must be held. Called only on the
432 // audio thread.
433 void Wait(int64_t aUsecs);
435 // Dispatches an asynchronous event to update the media element's ready state.
436 void UpdateReadyState();
438 // Resets playback timing data. Called when we seek, on the decode thread.
439 void ResetPlayback();
441 // Returns the audio clock, if we have audio, or -1 if we don't.
442 // Called on the state machine thread.
443 int64_t GetAudioClock();
445 // Get the video stream position, taking the |playbackRate| change into
446 // account. This is a position in the media, not the duration of the playback
447 // so far.
448 int64_t GetVideoStreamPosition();
450 // Return the current time, either the audio clock if available (if the media
451 // has audio, and the playback is possible), or a clock for the video.
452 // Called on the state machine thread.
453 int64_t GetClock();
455 // Returns the presentation time of the first audio or video frame in the
456 // media. If the media has video, it returns the first video frame. The
457 // decoder monitor must be held with exactly one lock count. Called on the
458 // state machine thread.
459 VideoData* FindStartTime();
461 // Update only the state machine's current playback position (and duration,
462 // if unknown). Does not update the playback position on the decoder or
463 // media element -- use UpdatePlaybackPosition for that. Called on the state
464 // machine thread, caller must hold the decoder lock.
465 void UpdatePlaybackPositionInternal(int64_t aTime);
467 // Pushes the image down the rendering pipeline. Called on the shared state
468 // machine thread. The decoder monitor must *not* be held when calling this.
469 void RenderVideoFrame(VideoData* aData, TimeStamp aTarget);
471 // If we have video, display a video frame if it's time for display has
472 // arrived, otherwise sleep until it's time for the next frame. Update the
473 // current frame time as appropriate, and trigger ready state update. The
474 // decoder monitor must be held with exactly one lock count. Called on the
475 // state machine thread.
476 void AdvanceFrame();
478 // Write aFrames of audio frames of silence to the audio hardware. Returns
479 // the number of frames actually written. The write size is capped at
480 // SILENCE_BYTES_CHUNK (32kB), so must be called in a loop to write the
481 // desired number of frames. This ensures that the playback position
482 // advances smoothly, and guarantees that we don't try to allocate an
483 // impossibly large chunk of memory in order to play back silence. Called
484 // on the audio thread.
485 uint32_t PlaySilence(uint32_t aFrames,
486 uint32_t aChannels,
487 uint64_t aFrameOffset);
489 // Pops an audio chunk from the front of the audio queue, and pushes its
490 // audio data to the audio hardware. MozAudioAvailable data is also queued
491 // here. Called on the audio thread.
492 uint32_t PlayFromAudioQueue(uint64_t aFrameOffset, uint32_t aChannels);
494 // Stops the decode thread, and if we have a pending request for a new
495 // decode thread it is canceled. The decoder monitor must be held with exactly
496 // one lock count. Called on the state machine thread.
497 void StopDecodeThread();
499 // Stops the audio thread. The decoder monitor must be held with exactly
500 // one lock count. Called on the state machine thread.
501 void StopAudioThread();
503 // Ensures the decode thread is running if it already exists, or requests
504 // a new decode thread be started if there currently is no decode thread.
505 // The decoder monitor must be held with exactly one lock count. Called on
506 // the state machine thread.
507 nsresult ScheduleDecodeThread();
509 // Starts the audio thread. The decoder monitor must be held with exactly
510 // one lock count. Called on the state machine thread.
511 nsresult StartAudioThread();
513 // The main loop for the audio thread. Sent to the thread as
514 // an nsRunnableMethod. This continually does blocking writes to
515 // to audio stream to play audio data.
516 void AudioLoop();
518 // Sets internal state which causes playback of media to pause.
519 // The decoder monitor must be held.
520 void StopPlayback();
522 // Sets internal state which causes playback of media to begin or resume.
523 // Must be called with the decode monitor held.
524 void StartPlayback();
526 // Moves the decoder into decoding state. Called on the state machine
527 // thread. The decoder monitor must be held.
528 void StartDecoding();
530 void StartWaitForResources();
532 void StartDecodeMetadata();
534 // Returns the "media time". This is the absolute time which the media
535 // playback has reached. i.e. this returns values in the range
536 // [mStartTime, mEndTime], and mStartTime will not be 0 if the media does
537 // not start at 0. Note this is different to the value returned
538 // by GetCurrentTime(), which is in the range [0,duration].
539 int64_t GetMediaTime() const {
540 AssertCurrentThreadInMonitor();
541 return mStartTime + mCurrentFrameTime;
544 // Returns an upper bound on the number of microseconds of audio that is
545 // decoded and playable. This is the sum of the number of usecs of audio which
546 // is decoded and in the reader's audio queue, and the usecs of unplayed audio
547 // which has been pushed to the audio hardware for playback. Note that after
548 // calling this, the audio hardware may play some of the audio pushed to
549 // hardware, so this can only be used as a upper bound. The decoder monitor
550 // must be held when calling this. Called on the decode thread.
551 int64_t GetDecodedAudioDuration();
553 // Load metadata. Called on the decode thread. The decoder monitor
554 // must be held with exactly one lock count.
555 nsresult DecodeMetadata();
557 // Seeks to mSeekTarget. Called on the decode thread. The decoder monitor
558 // must be held with exactly one lock count.
559 void DecodeSeek();
561 // Decode loop, decodes data until EOF or shutdown.
562 // Called on the decode thread.
563 void DecodeLoop();
565 // Decode thread run function. Determines which of the Decode*() functions
566 // to call.
567 void DecodeThreadRun();
569 // Copy audio from an AudioData packet to aOutput. This may require
570 // inserting silence depending on the timing of the audio packet.
571 void SendStreamAudio(AudioData* aAudio, DecodedStreamData* aStream,
572 AudioSegment* aOutput);
574 // State machine thread run function. Defers to RunStateMachine().
575 nsresult CallRunStateMachine();
577 // Performs one "cycle" of the state machine. Polls the state, and may send
578 // a video frame to be displayed, and generally manages the decode. Called
579 // periodically via timer to ensure the video stays in sync.
580 nsresult RunStateMachine();
582 bool IsStateMachineScheduled() const {
583 AssertCurrentThreadInMonitor();
584 return !mTimeout.IsNull() || mRunAgain;
587 // Returns true if we're not playing and the decode thread has filled its
588 // decode buffers and is waiting. We can shut the decode thread down in this
589 // case as it may not be needed again.
590 bool IsPausedAndDecoderWaiting();
592 // The decoder object that created this state machine. The state machine
593 // holds a strong reference to the decoder to ensure that the decoder stays
594 // alive once media element has started the decoder shutdown process, and has
595 // dropped its reference to the decoder. This enables the state machine to
596 // keep using the decoder's monitor until the state machine has finished
597 // shutting down, without fear of the monitor being destroyed. After
598 // shutting down, the state machine will then release this reference,
599 // causing the decoder to be destroyed. This is accessed on the decode,
600 // state machine, audio and main threads.
601 nsRefPtr<MediaDecoder> mDecoder;
603 // The decoder monitor must be obtained before modifying this state.
604 // NotifyAll on the monitor must be called when the state is changed so
605 // that interested threads can wake up and alter behaviour if appropriate
606 // Accessed on state machine, audio, main, and AV thread.
607 State mState;
609 // Thread for pushing audio onto the audio hardware.
610 // The "audio push thread".
611 nsCOMPtr<nsIThread> mAudioThread;
613 // Thread for decoding video in background. The "decode thread".
614 nsCOMPtr<nsIThread> mDecodeThread;
616 // Timer to call the state machine Run() method. Used by
617 // ScheduleStateMachine(). Access protected by decoder monitor.
618 nsCOMPtr<nsITimer> mTimer;
620 // Timestamp at which the next state machine Run() method will be called.
621 // If this is non-null, a call to Run() is scheduled, either by a timer,
622 // or via an event. Access protected by decoder monitor.
623 TimeStamp mTimeout;
625 // The time that playback started from the system clock. This is used for
626 // timing the presentation of video frames when there's no audio.
627 // Accessed only via the state machine thread.
628 TimeStamp mPlayStartTime;
630 // When we start writing decoded data to a new DecodedDataStream, or we
631 // restart writing due to PlaybackStarted(), we record where we are in the
632 // MediaStream and what that corresponds to in the media.
633 StreamTime mSyncPointInMediaStream;
634 int64_t mSyncPointInDecodedStream; // microseconds
636 // When the playbackRate changes, and there is no audio clock, it is necessary
637 // to reset the mPlayStartTime. This is done next time the clock is queried,
638 // when this member is true. Access protected by decoder monitor.
639 bool mResetPlayStartTime;
641 // The amount of time we've spent playing already the media. The current
642 // playback position is therefore |Now() - mPlayStartTime +
643 // mPlayDuration|, which must be adjusted by mStartTime if used with media
644 // timestamps. Accessed only via the state machine thread.
645 int64_t mPlayDuration;
647 // Time that buffering started. Used for buffering timeout and only
648 // accessed on the state machine thread. This is null while we're not
649 // buffering.
650 TimeStamp mBufferingStart;
652 // Start time of the media, in microseconds. This is the presentation
653 // time of the first frame decoded from the media, and is used to calculate
654 // duration and as a bounds for seeking. Accessed on state machine, decode,
655 // and main threads. Access controlled by decoder monitor.
656 int64_t mStartTime;
658 // Time of the last frame in the media, in microseconds. This is the
659 // end time of the last frame in the media. Accessed on state
660 // machine, decode, and main threads. Access controlled by decoder monitor.
661 int64_t mEndTime;
663 // Position to seek to in microseconds when the seek state transition occurs.
664 // The decoder monitor lock must be obtained before reading or writing
665 // this value. Accessed on main and decode thread.
666 int64_t mSeekTime;
668 // Media Fragment end time in microseconds. Access controlled by decoder monitor.
669 int64_t mFragmentEndTime;
671 // The audio stream resource. Used on the state machine, and audio threads.
672 // This is created and destroyed on the audio thread, while holding the
673 // decoder monitor, so if this is used off the audio thread, you must
674 // first acquire the decoder monitor and check that it is non-null.
675 nsAutoPtr<AudioStream> mAudioStream;
677 // The reader, don't call its methods with the decoder monitor held.
678 // This is created in the play state machine's constructor, and destroyed
679 // in the play state machine's destructor.
680 nsAutoPtr<MediaDecoderReader> mReader;
682 // Accessed only on the state machine thread.
683 // Not an nsRevocableEventPtr since we must Revoke() it well before
684 // this object is destroyed, anyway.
685 // Protected by decoder monitor except during the SHUTDOWN state after the
686 // decoder thread has been stopped.
687 nsRevocableEventPtr<WakeDecoderRunnable> mPendingWakeDecoder;
689 // The time of the current frame in microseconds. This is referenced from
690 // 0 which is the initial playback position. Set by the state machine
691 // thread, and read-only from the main thread to get the current
692 // time value. Synchronised via decoder monitor.
693 int64_t mCurrentFrameTime;
695 // The presentation time of the first audio frame that was played in
696 // microseconds. We can add this to the audio stream position to determine
697 // the current audio time. Accessed on audio and state machine thread.
698 // Synchronized by decoder monitor.
699 int64_t mAudioStartTime;
701 // The end time of the last audio frame that's been pushed onto the audio
702 // hardware in microseconds. This will approximately be the end time of the
703 // audio stream, unless another frame is pushed to the hardware.
704 int64_t mAudioEndTime;
706 // The presentation end time of the last video frame which has been displayed
707 // in microseconds. Accessed from the state machine thread.
708 int64_t mVideoFrameEndTime;
710 // Volume of playback. 0.0 = muted. 1.0 = full volume. Read/Written
711 // from the state machine and main threads. Synchronised via decoder
712 // monitor.
713 double mVolume;
715 // Playback rate. 1.0 : normal speed, 0.5 : two times slower. Synchronized via
716 // decoder monitor.
717 double mPlaybackRate;
719 // Pitch preservation for the playback rate. Synchronized via decoder monitor.
720 bool mPreservesPitch;
722 // Position at which the last playback rate change occured, used to compute
723 // the actual position in the stream when the playback rate changes and there
724 // is no audio to be sync-ed to. Synchronized via decoder monitor.
725 int64_t mBasePosition;
727 // Time at which we started decoding. Synchronised via decoder monitor.
728 TimeStamp mDecodeStartTime;
730 // The maximum number of second we spend buffering when we are short on
731 // unbuffered data.
732 uint32_t mBufferingWait;
733 int64_t mLowDataThresholdUsecs;
735 // If we've got more than mAmpleVideoFrames decoded video frames waiting in
736 // the video queue, we will not decode any more video frames until some have
737 // been consumed by the play state machine thread.
738 uint32_t mAmpleVideoFrames;
739 // True if we shouldn't play our audio (but still write it to any capturing
740 // streams). When this is true, mStopAudioThread is always true and
741 // the audio thread will never start again after it has stopped.
742 bool mAudioCaptured;
744 // True if the media resource can be seeked on a transport level. Accessed
745 // from the state machine and main threads. Synchronised via decoder monitor.
746 bool mTransportSeekable;
748 // True if the media can be seeked. Accessed from the state machine and main
749 // threads. Synchronised via decoder monitor.
750 bool mMediaSeekable;
752 // True if an event to notify about a change in the playback
753 // position has been queued, but not yet run. It is set to false when
754 // the event is run. This allows coalescing of these events as they can be
755 // produced many times per second. Synchronised via decoder monitor.
756 // Accessed on main and state machine threads.
757 bool mPositionChangeQueued;
759 // True if the audio playback thread has finished. It is finished
760 // when either all the audio frames have completed playing, or we've moved
761 // into shutdown state, and the threads are to be
762 // destroyed. Written by the audio playback thread and read and written by
763 // the state machine thread. Synchronised via decoder monitor.
764 // When data is being sent to a MediaStream, this is true when all data has
765 // been written to the MediaStream.
766 bool mAudioCompleted;
768 // True if mDuration has a value obtained from an HTTP header, or from
769 // the media index/metadata. Accessed on the state machine thread.
770 bool mGotDurationFromMetaData;
772 // False while decode thread should be running. Accessed state machine
773 // and decode threads. Syncrhonised by decoder monitor.
774 bool mStopDecodeThread;
776 // True when the decode thread run function has finished, but the thread
777 // has not necessarily been shut down yet. This can happen if we switch
778 // from COMPLETED state to SEEKING before the state machine has a chance
779 // to run in the COMPLETED state and shutdown the decode thread.
780 // Synchronised by the decoder monitor.
781 bool mDecodeThreadIdle;
783 // False while audio thread should be running. Accessed state machine
784 // and audio threads. Syncrhonised by decoder monitor.
785 bool mStopAudioThread;
787 // If this is true while we're in buffering mode, we can exit early,
788 // as it's likely we may be able to playback. This happens when we enter
789 // buffering mode soon after the decode starts, because the decode-ahead
790 // ran fast enough to exhaust all data while the download is starting up.
791 // Synchronised via decoder monitor.
792 bool mQuickBuffering;
794 // True if the shared state machine thread is currently running this
795 // state machine.
796 bool mIsRunning;
798 // True if we should run the state machine again once the current
799 // state machine run has finished.
800 bool mRunAgain;
802 // True if we've dispatched an event to run the state machine. It's
803 // imperative that we don't dispatch multiple events to run the state
804 // machine at the same time, as our code assume all events are synchronous.
805 // If we dispatch multiple events, the second event can run while the
806 // first is shutting down a thread, causing inconsistent state.
807 bool mDispatchedRunEvent;
809 // True if the decode thread has gone filled its buffers and is now
810 // waiting to be awakened before it continues decoding. Synchronized
811 // by the decoder monitor.
812 bool mDecodeThreadWaiting;
814 // True is we are decoding a realtime stream, like a camera stream
815 bool mRealTime;
817 // Record whether audio and video decoding were throttled during the
818 // previous iteration of DecodeLooop. When we transition from
819 // throttled to not-throttled we need to pump decoding.
820 bool mDidThrottleAudioDecoding;
821 bool mDidThrottleVideoDecoding;
823 // True if we've requested a new decode thread, but it has not yet been
824 // created. Synchronized by the decoder monitor.
825 bool mRequestedNewDecodeThread;
827 // Manager for queuing and dispatching MozAudioAvailable events. The
828 // event manager is accessed from the state machine and audio threads,
829 // and takes care of synchronizing access to its internal queue.
830 AudioAvailableEventManager mEventManager;
832 // Stores presentation info required for playback. The decoder monitor
833 // must be held when accessing this.
834 MediaInfo mInfo;
836 mozilla::MediaMetadataManager mMetadataManager;
838 MediaDecoderOwner::NextFrameStatus mLastFrameStatus;
841 } // namespace mozilla;
842 #endif