Bug 599089 - Remote Audio to parent process. r=kinetik a=blocking-fennec
[mozilla-central.git] / content / media / nsBuiltinDecoderStateMachine.h
blob925e2b0b4202e209d3370ac60822dce18e255548
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 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: ML 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is the Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Chris Double <chris.double@double.co.nz>
24 * Chris Pearce <chris@pearce.org.nz>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 Each video element for a media file has two additional threads beyond
41 those needed by nsBuiltinDecoder.
43 1) The Audio thread writes the decoded audio data to the audio
44 hardware. This is done in a seperate thread to ensure that the
45 audio hardware gets a constant stream of data without
46 interruption due to decoding or display. At some point
47 libsydneyaudio will be refactored to have a callback interface
48 where it asks for data and an extra thread will no longer be
49 needed.
51 2) The decode thread. This thread reads from the media stream and
52 decodes the Theora and Vorbis data. It places the decoded data in
53 a queue for the other threads to pull from.
55 All file reads and seeks must occur on either the state machine thread
56 or the decode thread. Synchronisation is done via a monitor owned by
57 nsBuiltinDecoder.
59 The decode thread and the audio thread are created and destroyed in
60 the state machine thread. When playback needs to occur they are
61 created and events dispatched to them to start them. These events exit
62 when decoding is completed or no longer required (during seeking or
63 shutdown).
65 The decode thread has its own monitor to ensure that its internal
66 state is independent of the other threads, and to ensure that it's not
67 hogging the nsBuiltinDecoder monitor while decoding.
69 a/v synchronisation is handled by the state machine thread. It
70 examines the audio playback time and compares this to the next frame
71 in the queue of frames. If it is time to play the video frame it is
72 then displayed.
74 Frame skipping is done in the following ways:
76 1) The state machine thread will skip all frames in the video queue whose
77 display time is less than the current audio time. This ensures
78 the correct frame for the current time is always displayed.
80 2) The decode thread will stop decoding interframes and read to the
81 next keyframe if it determines that decoding the remaining
82 interframes will cause playback issues. It detects this by:
83 a) If the amount of audio data in the audio queue drops
84 below a threshold whereby audio may start to skip.
85 b) If the video queue drops below a threshold where it
86 will be decoding video data that won't be displayed due
87 to the decode thread dropping the frame immediately.
89 YCbCr conversion is done on the decode thread when it is time to display
90 the video frame. This means frames that are skipped will not have the
91 YCbCr conversion done, improving playback.
93 The decode thread pushes decoded audio and videos frames into two
94 separate queues - one for audio and one for video. These are kept
95 separate to make it easy to constantly feed audio data to the sound
96 hardware while allowing frame skipping of video data. These queues are
97 threadsafe, and neither the decode, audio, or state machine thread should
98 be able to monopolize them, and cause starvation of the other threads.
100 Both queues are bounded by a maximum size. When this size is reached
101 the decode thread will no longer decode video or audio depending on the
102 queue that has reached the threshold.
104 During playback the audio thread will be idle (via a Wait() on the
105 monitor) if the audio queue is empty. Otherwise it constantly pops an
106 item off the queue and plays it with a blocking write to the audio
107 hardware (via nsAudioStream and libsydneyaudio).
109 The decode thread idles if the video queue is empty or if it is
110 not yet time to display the next frame.
112 #if !defined(nsBuiltinDecoderStateMachine_h__)
113 #define nsBuiltinDecoderStateMachine_h__
115 #include "prmem.h"
116 #include "nsThreadUtils.h"
117 #include "nsBuiltinDecoder.h"
118 #include "nsBuiltinDecoderReader.h"
119 #include "nsAudioAvailableEventManager.h"
120 #include "nsHTMLMediaElement.h"
121 #include "mozilla/Monitor.h"
124 The playback state machine class. This manages the decoding in the
125 nsBuiltinDecoderReader on the decode thread, seeking and in-sync-playback on the
126 state machine thread, and controls the audio "push" thread.
128 All internal state is synchronised via the decoder monitor. NotifyAll
129 on the monitor is called when the state of the state machine is changed
130 by the main thread. The following changes to state cause a notify:
132 mState and data related to that state changed (mSeekTime, etc)
133 Metadata Loaded
134 First Frame Loaded
135 Frame decoded
136 data pushed or popped from the video and audio queues
138 See nsBuiltinDecoder.h for more details.
140 class nsBuiltinDecoderStateMachine : public nsDecoderStateMachine
142 public:
143 typedef mozilla::Monitor Monitor;
144 typedef mozilla::TimeStamp TimeStamp;
145 typedef mozilla::TimeDuration TimeDuration;
147 nsBuiltinDecoderStateMachine(nsBuiltinDecoder* aDecoder, nsBuiltinDecoderReader* aReader);
148 ~nsBuiltinDecoderStateMachine();
150 // nsDecoderStateMachine interface
151 virtual nsresult Init(nsDecoderStateMachine* aCloneDonor);
152 State GetState()
154 mDecoder->GetMonitor().AssertCurrentThreadIn();
155 return mState;
157 virtual void SetVolume(float aVolume);
158 virtual void Shutdown();
159 virtual PRInt64 GetDuration();
160 virtual void SetDuration(PRInt64 aDuration);
161 virtual PRBool OnDecodeThread() {
162 return IsCurrentThread(mDecodeThread);
165 virtual nsHTMLMediaElement::NextFrameStatus GetNextFrameStatus();
166 virtual void Decode();
167 virtual void Seek(float aTime);
168 virtual float GetCurrentTime();
169 virtual void ClearPositionChangeFlag();
170 virtual void SetSeekable(PRBool aSeekable);
171 virtual void UpdatePlaybackPosition(PRInt64 aTime);
172 virtual void StartBuffering();
175 // Load metadata Called on the state machine thread. The decoder monitor must be held with
176 // exactly one lock count.
177 virtual void LoadMetadata();
179 // State machine thread run function. Polls the state, sends frames to be
180 // displayed at appropriate times, and generally manages the decode.
181 NS_IMETHOD Run();
183 // This is called on the state machine thread and audio thread.
184 // The decoder monitor must be obtained before calling this.
185 PRBool HasAudio() const {
186 mDecoder->GetMonitor().AssertCurrentThreadIn();
187 return mReader->GetInfo().mHasAudio;
190 // This is called on the state machine thread and audio thread.
191 // The decoder monitor must be obtained before calling this.
192 PRBool HasVideo() const {
193 mDecoder->GetMonitor().AssertCurrentThreadIn();
194 return mReader->GetInfo().mHasVideo;
197 // Should be called by main thread.
198 PRBool HaveNextFrameData() const;
200 // Must be called with the decode monitor held.
201 PRBool IsBuffering() const {
202 mDecoder->GetMonitor().AssertCurrentThreadIn();
204 return mState == nsBuiltinDecoderStateMachine::DECODER_STATE_BUFFERING;
207 // Must be called with the decode monitor held.
208 PRBool IsSeeking() const {
209 mDecoder->GetMonitor().AssertCurrentThreadIn();
211 return mState == nsBuiltinDecoderStateMachine::DECODER_STATE_SEEKING;
214 // Functions used by assertions to ensure we're calling things
215 // on the appropriate threads.
216 PRBool OnAudioThread() {
217 return IsCurrentThread(mAudioThread);
220 PRBool OnStateMachineThread() {
221 return mDecoder->OnStateMachineThread();
224 // Decode loop, called on the decode thread.
225 void DecodeLoop();
227 // The decoder object that created this state machine. The decoder
228 // always outlives us since it controls our lifetime. This is accessed
229 // read only on the AV, state machine, audio and main thread.
230 nsBuiltinDecoder* mDecoder;
232 // The decoder monitor must be obtained before modifying this state.
233 // NotifyAll on the monitor must be called when the state is changed by
234 // the main thread so the decoder thread can wake up.
235 // Accessed on state machine, audio, main, and AV thread.
236 State mState;
238 nsresult GetBuffered(nsTimeRanges* aBuffered) {
239 NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
240 return mReader->GetBuffered(aBuffered, mStartTime);
243 void NotifyDataArrived(const char* aBuffer, PRUint32 aLength, PRUint32 aOffset) {
244 NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
245 mReader->NotifyDataArrived(aBuffer, aLength, aOffset);
248 PRInt64 GetEndMediaTime() const {
249 mDecoder->GetMonitor().AssertCurrentThreadIn();
250 return mEndTime;
253 protected:
255 // Returns the number of unplayed ms of audio we've got decoded and/or
256 // pushed to the hardware waiting to play. This is how much audio we can
257 // play without having to run the audio decoder.
258 PRInt64 AudioDecodedMs() const;
260 // Returns PR_TRUE if we're running low on decoded data.
261 PRBool HasLowDecodedData() const;
263 // Returns PR_TRUE if we've got plenty of decoded data.
264 PRBool HasAmpleDecodedData() const;
266 // Returns PR_TRUE when there's decoded audio waiting to play.
267 // The decoder monitor must be held.
268 PRBool HasFutureAudio() const;
270 // Waits on the decoder Monitor for aMs. If the decoder monitor is awoken
271 // by a Notify() call, we'll continue waiting, unless we've moved into
272 // shutdown state. This enables us to ensure that we wait for a specified
273 // time, and that the myriad of Notify()s we do an the decoder monitor
274 // don't cause the audio thread to be starved. The decoder monitor must
275 // be locked.
276 void Wait(PRUint32 aMs);
278 // Dispatches an asynchronous event to update the media element's ready state.
279 void UpdateReadyState();
281 // Resets playback timing data. Called when we seek, on the state machine
282 // thread.
283 void ResetPlayback();
285 // Returns the audio clock, if we have audio, or -1 if we don't.
286 // Called on the state machine thread.
287 PRInt64 GetAudioClock();
289 // Returns the presentation time of the first sample or frame in the media.
290 // If the media has video, it returns the first video frame. The decoder
291 // monitor must be held with exactly one lock count. Called on the state
292 // machine thread.
293 VideoData* FindStartTime();
295 // Finds the end time of the last frame of data in the file, storing the value
296 // in mEndTime if successful. The decoder must be held with exactly one lock
297 // count. Called on the state machine thread.
298 void FindEndTime();
300 // Performs YCbCr to RGB conversion, and pushes the image down the
301 // rendering pipeline. Called on the state machine thread.
302 void RenderVideoFrame(VideoData* aData);
304 // If we have video, display a video frame if it's time for display has
305 // arrived, otherwise sleep until it's time for the next sample. Update
306 // the current frame time as appropriate, and trigger ready state update.
307 // The decoder monitor must be held with exactly one lock count. Called
308 // on the state machine thread.
309 void AdvanceFrame();
311 // Pushes up to aSamples samples of silence onto the audio hardware. Returns
312 // the number of samples acutally pushed to the hardware. This pushes up to
313 // 32KB worth of samples to the hardware before returning, so must be called
314 // in a loop to ensure that the desired number of samples are pushed to the
315 // hardware. This ensures that the playback position advances smoothly, and
316 // guarantees that we don't try to allocate an impossibly large chunk of
317 // memory in order to play back silence. Called on the audio thread.
318 PRUint32 PlaySilence(PRUint32 aSamples, PRUint32 aChannels,
319 PRUint64 aSampleOffset);
321 // Pops an audio chunk from the front of the audio queue, and pushes its
322 // sound data to the audio hardware. MozAudioAvailable sample data is also
323 // queued here. Called on the audio thread.
324 PRUint32 PlayFromAudioQueue(PRUint64 aSampleOffset, PRUint32 aChannels);
326 // Stops the decode threads. The decoder monitor must be held with exactly
327 // one lock count. Called on the state machine thread.
328 void StopDecodeThreads();
330 // Starts the decode threads. The decoder monitor must be held with exactly
331 // one lock count. Called on the state machine thread.
332 nsresult StartDecodeThreads();
334 // The main loop for the audio thread. Sent to the thread as
335 // an nsRunnableMethod. This continually does blocking writes to
336 // to audio stream to play audio data.
337 void AudioLoop();
339 // Stop or pause playback of media. This has two modes, denoted by
340 // aMode being either AUDIO_PAUSE or AUDIO_SHUTDOWN.
342 // AUDIO_PAUSE: Suspends the audio stream to be resumed later.
343 // This does not close the OS based audio stream
345 // AUDIO_SHUTDOWN: Closes and destroys the audio stream and
346 // releases any OS resources.
348 // The decoder monitor must be held with exactly one lock count. Called
349 // on the state machine thread.
350 enum eStopMode {AUDIO_PAUSE, AUDIO_SHUTDOWN};
351 void StopPlayback(eStopMode aMode);
353 // Resume playback of media. Must be called with the decode monitor held.
354 // This resumes a paused audio stream. The decoder monitor must be held with
355 // exactly one lock count. Called on the state machine thread.
356 void StartPlayback();
358 // Returns PR_TRUE if we're currently playing. The decoder monitor must
359 // be held.
360 PRBool IsPlaying();
362 // Returns the "media time". This is the absolute time which the media
363 // playback has reached. i.e. this returns values in the range
364 // [mStartTime, mEndTime], and mStartTime will not be 0 if the media does
365 // not start at 0. Note this is different to the value returned
366 // by GetCurrentTime(), which is in the range [0,duration].
367 PRInt64 GetMediaTime() const {
368 mDecoder->GetMonitor().AssertCurrentThreadIn();
369 return mStartTime + mCurrentFrameTime;
372 // Monitor on mAudioStream. This monitor must be held in order to delete
373 // or use the audio stream. This stops us destroying the audio stream
374 // while it's being used on another thread (typically when it's being
375 // written to on the audio thread).
376 Monitor mAudioMonitor;
378 // The size of the decoded YCbCr frame.
379 // Accessed on state machine thread.
380 PRUint32 mCbCrSize;
382 // Accessed on state machine thread.
383 nsAutoArrayPtr<unsigned char> mCbCrBuffer;
385 // Thread for pushing audio onto the audio hardware.
386 // The "audio push thread".
387 nsCOMPtr<nsIThread> mAudioThread;
389 // Thread for decoding video in background. The "decode thread".
390 nsCOMPtr<nsIThread> mDecodeThread;
392 // The time that playback started from the system clock. This is used
393 // for timing the display of audio frames when there's no audio.
394 // Accessed only via the state machine thread.
395 TimeStamp mPlayStartTime;
397 // The amount of time we've spent playing already the media. The current
398 // playback position is therefore (mPlayDuration + (now - mPlayStartTime)).
399 // Accessed only via the state machine thread.
400 TimeDuration mPlayDuration;
402 // Time that buffering started. Used for buffering timeout and only
403 // accessed on the state machine thread.
404 TimeStamp mBufferingStart;
406 // Download position where we should stop buffering. Only
407 // accessed on the state machine thread.
408 PRInt64 mBufferingEndOffset;
410 // Start time of the media, in milliseconds. This is the presentation
411 // time of the first sample decoded from the media, and is used to calculate
412 // duration and as a bounds for seeking. Accessed on state machine and
413 // main thread. Access controlled by decoder monitor.
414 PRInt64 mStartTime;
416 // Time of the last page in the media, in milliseconds. This is the
417 // end time of the last sample in the media. Accessed on state
418 // machine and main thread. Access controlled by decoder monitor.
419 PRInt64 mEndTime;
421 // Position to seek to in milliseconds when the seek state transition occurs.
422 // The decoder monitor lock must be obtained before reading or writing
423 // this value. Accessed on main and state machine thread.
424 PRInt64 mSeekTime;
426 // The audio stream resource. Used on the state machine, audio, and main
427 // threads. You must hold the mAudioMonitor, and must NOT hold the decoder
428 // monitor when using the audio stream!
429 nsRefPtr<nsAudioStream> mAudioStream;
431 // The reader, don't call its methods with the decoder monitor held.
432 // This is created in the play state machine's constructor, and destroyed
433 // in the play state machine's destructor.
434 nsAutoPtr<nsBuiltinDecoderReader> mReader;
436 // The time of the current frame in milliseconds. This is referenced from
437 // 0 which is the initial playback position. Set by the state machine
438 // thread, and read-only from the main thread to get the current
439 // time value. Synchronised via decoder monitor.
440 PRInt64 mCurrentFrameTime;
442 // The presentation time of the first audio sample that was played. We can
443 // add this to the audio stream position to determine the current audio time.
444 // Accessed on audio and state machine thread. Synchronized by decoder monitor.
445 PRInt64 mAudioStartTime;
447 // The end time of the last audio sample that's been pushed onto the audio
448 // hardware. This will approximately be the end time of the audio stream,
449 // unless another sample is pushed to the hardware.
450 PRInt64 mAudioEndTime;
452 // The presentation end time of the last video frame which has been displayed.
453 // Accessed from the state machine thread.
454 PRInt64 mVideoFrameEndTime;
456 // Volume of playback. 0.0 = muted. 1.0 = full volume. Read/Written
457 // from the state machine and main threads. Synchronised via decoder
458 // monitor.
459 float mVolume;
461 // PR_TRUE if the media resource can be seeked. Accessed from the state
462 // machine and main threads. Synchronised via decoder monitor.
463 PRPackedBool mSeekable;
465 // PR_TRUE if an event to notify about a change in the playback
466 // position has been queued, but not yet run. It is set to PR_FALSE when
467 // the event is run. This allows coalescing of these events as they can be
468 // produced many times per second. Synchronised via decoder monitor.
469 // Accessed on main and state machine threads.
470 PRPackedBool mPositionChangeQueued;
472 // PR_TRUE if the audio playback thread has finished. It is finished
473 // when either all the audio samples in the Vorbis bitstream have completed
474 // playing, or we've moved into shutdown state, and the threads are to be
475 // destroyed. Written by the audio playback thread and read and written by
476 // the state machine thread. Synchronised via decoder monitor.
477 PRPackedBool mAudioCompleted;
479 // PR_TRUE if mDuration has a value obtained from an HTTP header, or from
480 // the media index/metadata. Accessed on the state machine thread.
481 PRPackedBool mGotDurationFromMetaData;
483 // PR_FALSE while decode threads should be running. Accessed on audio,
484 // state machine and decode threads. Syncrhonised by decoder monitor.
485 PRPackedBool mStopDecodeThreads;
487 private:
488 // Manager for queuing and dispatching MozAudioAvailable events. The
489 // event manager is accessed from the state machine and audio threads,
490 // and takes care of synchronizing access to its internal queue.
491 nsAudioAvailableEventManager mEventManager;
494 #endif