2008-07-29 23:50:14 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: ML 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
/*
|
2009-05-12 16:47:52 -07:00
|
|
|
Each video element has two threads. The first thread, called the Decode thread,
|
2008-10-19 00:39:21 -07:00
|
|
|
owns the resources for downloading and reading the video file. It goes through the
|
2009-05-12 16:47:52 -07:00
|
|
|
file, prcessing any decoded theora and vorbis data. It handles the sending of the
|
|
|
|
audio data to the sound device and the presentation of the video data at the correct
|
|
|
|
frame rate.
|
|
|
|
|
|
|
|
The second thread is the step decode thread. It uses OggPlay to decode the video and
|
|
|
|
audio data. It indirectly uses an nsMediaStream to do the file reading and seeking via
|
|
|
|
Oggplay.
|
|
|
|
|
|
|
|
All file reads and seeks must occur on these two threads. Synchronisation is done via
|
|
|
|
liboggplay internal mutexes to ensure that access to the liboggplay structures is
|
|
|
|
done correctly in the presence of the threads.
|
|
|
|
|
|
|
|
The step decode thread is created and destroyed in the decode thread. When decoding
|
|
|
|
needs to be done it is created and event dispatched to it to start the decode loop.
|
|
|
|
This event exits when decoding is completed or no longer required (during seeking
|
|
|
|
or shutdown).
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
When the decode thread is created an event is dispatched to it. The event
|
|
|
|
runs for the lifetime of the playback of the resource. The decode thread
|
|
|
|
synchronises with the main thread via a single monitor held by the
|
|
|
|
nsOggDecoder object.
|
|
|
|
|
|
|
|
The event contains a Run method which consists of an infinite loop
|
|
|
|
that checks the state that the state machine is in and processes
|
|
|
|
operations on that state.
|
|
|
|
|
|
|
|
The nsOggDecodeStateMachine class is the event that gets dispatched to
|
|
|
|
the decode thread. It has the following states:
|
|
|
|
|
|
|
|
DECODING_METADATA
|
|
|
|
The Ogg headers are being loaded, and things like framerate, etc are
|
2009-05-18 22:25:51 -07:00
|
|
|
being determined, and the first frame of audio/video data is being decoded.
|
2008-10-19 00:39:21 -07:00
|
|
|
DECODING
|
|
|
|
Video/Audio frames are being decoded.
|
|
|
|
SEEKING
|
|
|
|
A seek operation is in progress.
|
|
|
|
BUFFERING
|
|
|
|
Decoding is paused while data is buffered for smooth playback.
|
|
|
|
COMPLETED
|
|
|
|
The resource has completed decoding.
|
|
|
|
SHUTDOWN
|
|
|
|
The decoder object is about to be destroyed.
|
|
|
|
|
|
|
|
The following result in state transitions.
|
|
|
|
|
|
|
|
Shutdown()
|
|
|
|
Clean up any resources the nsOggDecodeStateMachine owns.
|
|
|
|
Decode()
|
|
|
|
Start decoding video frames.
|
|
|
|
Buffer
|
|
|
|
This is not user initiated. It occurs when the
|
|
|
|
available data in the stream drops below a certain point.
|
|
|
|
Complete
|
|
|
|
This is not user initiated. It occurs when the
|
|
|
|
stream is completely decoded.
|
|
|
|
Seek(float)
|
|
|
|
Seek to the time position given in the resource.
|
|
|
|
|
|
|
|
A state transition diagram:
|
|
|
|
|
|
|
|
DECODING_METADATA
|
2009-05-18 22:25:51 -07:00
|
|
|
| |
|
|
|
|
v | Shutdown()
|
|
|
|
| |
|
2008-10-19 00:39:21 -07:00
|
|
|
v -->-------------------->--------------------------|
|
2009-05-18 22:25:51 -07:00
|
|
|
|---------------->----->------------------------| v
|
2008-10-19 00:39:21 -07:00
|
|
|
DECODING | | | | |
|
|
|
|
^ v Seek(t) | | | |
|
|
|
|
| Decode() | v | | |
|
|
|
|
^-----------<----SEEKING | v Complete v v
|
|
|
|
| | | | | |
|
|
|
|
| | | COMPLETED SHUTDOWN-<-|
|
|
|
|
^ ^ | |Shutdown() |
|
|
|
|
| | | >-------->-----^
|
|
|
|
| Decode() |Seek(t) |Buffer() |
|
|
|
|
-----------<--------<-------BUFFERING |
|
|
|
|
| ^
|
|
|
|
v Shutdown() |
|
|
|
|
| |
|
|
|
|
------------>-----|
|
|
|
|
|
|
|
|
The Main thread controls the decode state machine by setting the value
|
|
|
|
of a mPlayState variable and notifying on the monitor
|
|
|
|
based on the high level player actions required (Seek, Pause, Play, etc).
|
|
|
|
|
|
|
|
The player states are the states requested by the client through the
|
|
|
|
DOM API. They represent the desired state of the player, while the
|
|
|
|
decoder's state represents the actual state of the decoder.
|
|
|
|
|
|
|
|
The high level state of the player is maintained via a PlayState value.
|
|
|
|
It can have the following states:
|
|
|
|
|
|
|
|
START
|
|
|
|
The decoder has been initialized but has no resource loaded.
|
|
|
|
PAUSED
|
|
|
|
A request via the API has been received to pause playback.
|
|
|
|
LOADING
|
|
|
|
A request via the API has been received to load a resource.
|
|
|
|
PLAYING
|
|
|
|
A request via the API has been received to start playback.
|
|
|
|
SEEKING
|
|
|
|
A request via the API has been received to start seeking.
|
|
|
|
COMPLETED
|
|
|
|
Playback has completed.
|
|
|
|
SHUTDOWN
|
|
|
|
The decoder is about to be destroyed.
|
|
|
|
|
|
|
|
State transition occurs when the Media Element calls the Play, Seek,
|
|
|
|
etc methods on the nsOggDecoder object. When the transition occurs
|
|
|
|
nsOggDecoder then calls the methods on the decoder state machine
|
|
|
|
object to cause it to behave appropriate to the play state.
|
|
|
|
|
|
|
|
The following represents the states that the player can be in, and the
|
|
|
|
valid states the decode thread can be in at that time:
|
|
|
|
|
2009-05-18 22:25:51 -07:00
|
|
|
player LOADING decoder DECODING_METADATA
|
2008-10-19 00:39:21 -07:00
|
|
|
player PLAYING decoder DECODING, BUFFERING, SEEKING, COMPLETED
|
|
|
|
player PAUSED decoder DECODING, BUFFERING, SEEKING, COMPLETED
|
|
|
|
player SEEKING decoder SEEKING
|
|
|
|
player COMPLETED decoder SHUTDOWN
|
|
|
|
player SHUTDOWN decoder SHUTDOWN
|
|
|
|
|
|
|
|
The general sequence of events with these objects is:
|
|
|
|
|
|
|
|
1) The video element calls Load on nsMediaDecoder. This creates the
|
|
|
|
decode thread and starts the channel for downloading the file. It
|
|
|
|
instantiates and starts the Decode state machine. The high level
|
|
|
|
LOADING state is entered, which results in the decode state machine
|
|
|
|
to start decoding metadata. These are the headers that give the
|
|
|
|
video size, framerate, etc. It returns immediately to the calling
|
|
|
|
video element.
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
2) When the Ogg metadata has been loaded by the decode thread it will
|
2008-10-19 00:39:21 -07:00
|
|
|
call a method on the video element object to inform it that this
|
|
|
|
step is done, so it can do the things required by the video
|
|
|
|
specification at this stage. The decoder then continues to decode
|
|
|
|
the first frame of data.
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
3) When the first frame of Ogg data has been successfully decoded it
|
|
|
|
calls a method on the video element object to inform it that this
|
|
|
|
step has been done, once again so it can do the required things by
|
|
|
|
the video specification at this stage.
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
This results in the high level state changing to PLAYING or PAUSED
|
|
|
|
depending on any user action that may have occurred.
|
|
|
|
|
|
|
|
The decode thread, while in the DECODING state, plays audio and
|
|
|
|
video, if the correct frame time comes around and the decoder
|
|
|
|
play state is PLAYING.
|
|
|
|
|
|
|
|
a/v synchronisation is done by a combination of liboggplay and the
|
|
|
|
Decoder state machine. liboggplay ensures that a decoded frame of data
|
|
|
|
has both the audio samples and the YUV data for that period of time.
|
|
|
|
|
|
|
|
When a frame is decoded by the decode state machine it converts the
|
|
|
|
YUV encoded video to RGB and copies the sound data to an internal
|
|
|
|
FrameData object. This is stored in a queue of available decoded frames.
|
|
|
|
Included in the FrameData object is the time that that frame should
|
|
|
|
be displayed.
|
|
|
|
|
|
|
|
The display state machine keeps track of the time since the last frame it
|
|
|
|
played. After decoding a frame it checks if it is time to display the next
|
|
|
|
item in the decoded frame queue. If so, it pops the item off the queue
|
|
|
|
and displays it.
|
|
|
|
|
|
|
|
Ideally a/v sync would take into account the actual audio clock of the
|
|
|
|
audio hardware for the sync rather than using the system clock.
|
|
|
|
Unfortunately getting valid time data out of the audio hardware has proven
|
|
|
|
to be unreliable across platforms (and even distributions in Linux) depending
|
|
|
|
on audio hardware, audio backend etc. The current approach works fine in practice
|
|
|
|
and is a compromise until this issue can be sorted. The plan is to eventually
|
|
|
|
move to synchronising using the audio hardware.
|
|
|
|
|
|
|
|
To prevent audio skipping and framerate dropping it is very important to
|
|
|
|
make sure no blocking occurs during the decoding process and minimise
|
|
|
|
expensive time operations at the time a frame is to be displayed. This is
|
|
|
|
managed by immediately converting video data to RGB on decode (an expensive
|
|
|
|
operation to do at frame display time) and checking if the sound device will
|
|
|
|
not block before writing sound data to it.
|
|
|
|
|
|
|
|
Shutdown needs to ensure that the event posted to the decode
|
|
|
|
thread is completed. The decode thread can potentially block internally
|
|
|
|
inside liboggplay when reading, seeking, or its internal buffers containing
|
|
|
|
decoded data are full. When blocked in this manner a call from the main thread
|
|
|
|
to Shutdown() will hang.
|
|
|
|
|
|
|
|
This is fixed with a protocol to ensure that the decode event cleanly
|
|
|
|
completes. The nsMediaStream that the nsChannelReader uses has a
|
|
|
|
Cancel() method. Calling this before Shutdown() will close any
|
|
|
|
internal streams or listeners resulting in blocked i/o completing with
|
|
|
|
an error, and all future i/o on the stream having an error.
|
|
|
|
|
|
|
|
This causes the decode thread to exit and Shutdown() can occur.
|
|
|
|
|
|
|
|
If the decode thread is seeking then the same Cancel() operation
|
|
|
|
causes an error to be returned from the seek call to liboggplay which
|
|
|
|
exits out of the seek operation, and stops the seek state running on the
|
|
|
|
decode thread.
|
|
|
|
|
|
|
|
If the decode thread is blocked due to internal decode buffers being
|
|
|
|
full, it is unblocked during the shutdown process by calling
|
|
|
|
oggplay_prepare_for_close.
|
|
|
|
|
|
|
|
In practice the OggPlay internal buffer should never fill as we retrieve and
|
|
|
|
process the frame immediately on decoding.
|
|
|
|
|
|
|
|
The Shutdown method on nsOggDecoder can spin the event loop as it waits
|
|
|
|
for threads to complete. Spinning the event loop is a bad thing to happen
|
|
|
|
during certain times like destruction of the media element. To work around
|
|
|
|
this the Shutdown method does nothing by queue an event to the main thread
|
|
|
|
to perform the actual Shutdown. This way the shutdown can occur at a safe
|
|
|
|
time.
|
|
|
|
|
|
|
|
This means the owning object of a nsOggDecoder object *MUST* call Shutdown
|
|
|
|
when destroying the nsOggDecoder object.
|
2008-07-29 23:50:14 -07:00
|
|
|
*/
|
2008-10-19 00:39:21 -07:00
|
|
|
#if !defined(nsOggDecoder_h_)
|
|
|
|
#define nsOggDecoder_h_
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-09-29 14:32:44 -07:00
|
|
|
#include "nsMediaDecoder.h"
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsChannelReader.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsSize.h"
|
|
|
|
#include "prlog.h"
|
2008-10-19 00:39:21 -07:00
|
|
|
#include "prmon.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxRect.h"
|
|
|
|
#include "oggplay/oggplay.h"
|
|
|
|
|
|
|
|
class nsAudioStream;
|
2008-10-19 00:39:21 -07:00
|
|
|
class nsOggDecodeStateMachine;
|
2009-05-12 16:47:52 -07:00
|
|
|
class nsOggStepDecodeEvent;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
class nsOggDecoder : public nsMediaDecoder
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2008-10-19 00:39:21 -07:00
|
|
|
friend class nsOggDecodeStateMachine;
|
2009-05-12 16:47:52 -07:00
|
|
|
friend class nsOggStepDecodeEvent;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
// ISupports
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
public:
|
2008-10-19 00:39:21 -07:00
|
|
|
// Enumeration for the valid play states (see mPlayState)
|
|
|
|
enum PlayState {
|
|
|
|
PLAY_STATE_START,
|
|
|
|
PLAY_STATE_LOADING,
|
|
|
|
PLAY_STATE_PAUSED,
|
|
|
|
PLAY_STATE_PLAYING,
|
|
|
|
PLAY_STATE_SEEKING,
|
|
|
|
PLAY_STATE_ENDED,
|
|
|
|
PLAY_STATE_SHUTDOWN
|
|
|
|
};
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
nsOggDecoder();
|
2008-10-19 00:39:21 -07:00
|
|
|
~nsOggDecoder();
|
2009-09-14 19:30:44 -07:00
|
|
|
|
|
|
|
virtual nsMediaDecoder* Clone() { return new nsOggDecoder(); }
|
|
|
|
|
2009-01-15 23:57:37 -08:00
|
|
|
virtual PRBool Init(nsHTMLMediaElement* aElement);
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
// This method must be called by the owning object before that
|
|
|
|
// object disposes of this decoder object.
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual void Shutdown();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual float GetCurrentTime();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-09-14 19:30:43 -07:00
|
|
|
virtual nsresult Load(nsMediaStream* aStream,
|
|
|
|
nsIStreamListener** aListener);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
// Start playback of a video. 'Load' must have previously been
|
|
|
|
// called.
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual nsresult Play();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
// Seek to the time position in (seconds) from the start of the video.
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual nsresult Seek(float time);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual nsresult PlaybackRateChanged();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-29 22:20:08 -07:00
|
|
|
virtual void Pause();
|
|
|
|
virtual void SetVolume(float volume);
|
|
|
|
virtual float GetDuration();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-09-14 19:28:08 -07:00
|
|
|
virtual nsMediaStream* GetCurrentStream();
|
2009-03-31 17:52:56 -07:00
|
|
|
virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
virtual void NotifySuspendedStatusChanged();
|
|
|
|
virtual void NotifyBytesDownloaded();
|
2009-02-05 00:02:21 -08:00
|
|
|
virtual void NotifyDownloadEnded(nsresult aStatus);
|
2009-03-31 17:52:56 -07:00
|
|
|
// Called by nsChannelReader on the decoder thread
|
|
|
|
void NotifyBytesConsumed(PRInt64 aBytes);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Called when the video file has completed downloading.
|
|
|
|
// Call on the main thread only.
|
|
|
|
void ResourceLoaded();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Called if the media file encounters a network error.
|
|
|
|
// Call on the main thread only.
|
2008-11-18 00:19:32 -08:00
|
|
|
virtual void NetworkError();
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Call from any thread safely. Return PR_TRUE if we are currently
|
|
|
|
// seeking in the media resource.
|
|
|
|
virtual PRBool IsSeeking() const;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-12-14 10:02:54 -08:00
|
|
|
// Return PR_TRUE if the decoder has reached the end of playback.
|
|
|
|
// Call on the main thread only.
|
|
|
|
virtual PRBool IsEnded() const;
|
|
|
|
|
2009-02-07 02:10:34 -08:00
|
|
|
// Set the duration of the media resource in units of milliseconds.
|
|
|
|
// This is called via a channel listener if it can pick up the duration
|
|
|
|
// from a content header. Must be called from the main thread only.
|
|
|
|
virtual void SetDuration(PRInt64 aDuration);
|
|
|
|
|
2008-11-09 17:38:02 -08:00
|
|
|
// Set a flag indicating whether seeking is supported
|
|
|
|
virtual void SetSeekable(PRBool aSeekable);
|
|
|
|
|
|
|
|
// Return PR_TRUE if seeking is supported.
|
|
|
|
virtual PRBool GetSeekable();
|
|
|
|
|
2008-11-18 00:19:32 -08:00
|
|
|
// Returns the channel reader.
|
|
|
|
nsChannelReader* GetReader() { return mReader; }
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
virtual Statistics GetStatistics();
|
|
|
|
|
2009-01-21 15:54:40 -08:00
|
|
|
// Suspend any media downloads that are in progress. Called by the
|
|
|
|
// media element when it is sent to the bfcache. Call on the main
|
|
|
|
// thread only.
|
|
|
|
virtual void Suspend();
|
|
|
|
|
|
|
|
// Resume any media downloads that have been suspended. Called by the
|
|
|
|
// media element when it is restored from the bfcache. Call on the
|
|
|
|
// main thread only.
|
|
|
|
virtual void Resume();
|
|
|
|
|
2009-04-09 18:28:24 -07:00
|
|
|
// Tells our nsMediaStream to put all loads in the background.
|
|
|
|
virtual void MoveLoadsToBackground();
|
|
|
|
|
2009-05-18 16:08:08 -07:00
|
|
|
// Stop the state machine thread and drop references to the thread,
|
|
|
|
// state machine and channel reader.
|
|
|
|
void Stop();
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// Returns the monitor for other threads to synchronise access to
|
2008-11-18 00:19:32 -08:00
|
|
|
// state.
|
2008-10-19 00:39:21 -07:00
|
|
|
PRMonitor* GetMonitor()
|
|
|
|
{
|
|
|
|
return mMonitor;
|
|
|
|
}
|
|
|
|
|
2008-11-18 00:19:32 -08:00
|
|
|
// Return the current state. Can be called on any thread. If called from
|
|
|
|
// a non-main thread, the decoder monitor must be held.
|
2008-10-19 00:39:21 -07:00
|
|
|
PlayState GetState()
|
|
|
|
{
|
|
|
|
return mPlayState;
|
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
// Stop updating the bytes downloaded for progress notifications. Called
|
|
|
|
// when seeking to prevent wild changes to the progress notification.
|
|
|
|
// Must be called with the decoder monitor held.
|
|
|
|
void StopProgressUpdates();
|
|
|
|
|
|
|
|
// Allow updating the bytes downloaded for progress notifications. Must
|
|
|
|
// be called with the decoder monitor held.
|
|
|
|
void StartProgressUpdates();
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
// Something has changed that could affect the computed playback rate,
|
|
|
|
// so recompute it. The monitor must be held.
|
|
|
|
void UpdatePlaybackRate();
|
|
|
|
|
|
|
|
// The actual playback rate computation. The monitor must be held.
|
|
|
|
double ComputePlaybackRate(PRPackedBool* aReliable);
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
/******
|
|
|
|
* The following methods must only be called on the main
|
|
|
|
* thread.
|
|
|
|
******/
|
|
|
|
|
2008-11-18 00:19:32 -08:00
|
|
|
// Change to a new play state. This updates the mState variable and
|
|
|
|
// notifies any thread blocking on this object's monitor of the
|
|
|
|
// change. Call on the main thread only.
|
|
|
|
void ChangeState(PlayState aState);
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
// Called when the metadata from the Ogg file has been read.
|
|
|
|
// Call on the main thread only.
|
|
|
|
void MetadataLoaded();
|
|
|
|
|
|
|
|
// Called when the first frame has been loaded.
|
|
|
|
// Call on the main thread only.
|
|
|
|
void FirstFrameLoaded();
|
|
|
|
|
|
|
|
// Called when the video has completed playing.
|
|
|
|
// Call on the main thread only.
|
2008-10-19 00:39:21 -07:00
|
|
|
void PlaybackEnded();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Seeking has stopped. Inform the element on the main
|
|
|
|
// thread.
|
|
|
|
void SeekingStopped();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-05-31 03:02:17 -07:00
|
|
|
// Seeking has stopped at the end of the resource. Inform the element on the main
|
|
|
|
// thread.
|
|
|
|
void SeekingStoppedAtEnd();
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Seeking has started. Inform the element on the main
|
|
|
|
// thread.
|
|
|
|
void SeekingStarted();
|
|
|
|
|
2008-10-23 01:02:18 -07:00
|
|
|
// Called when the backend has changed the current playback
|
|
|
|
// position. It dispatches a timeupdate event and invalidates the frame.
|
|
|
|
// This must be called on the main thread only.
|
|
|
|
void PlaybackPositionChanged();
|
|
|
|
|
2009-02-10 17:43:45 -08:00
|
|
|
// Calls mElement->UpdateReadyStateForData, telling it whether we have
|
|
|
|
// data for the next frame and if we're buffering. Main thread only.
|
|
|
|
void UpdateReadyStateForData();
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
// Find the end of the cached data starting at the current decoder
|
|
|
|
// position.
|
|
|
|
PRInt64 GetDownloadPosition();
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
private:
|
|
|
|
// Register/Unregister with Shutdown Observer.
|
|
|
|
// Call on main thread only.
|
|
|
|
void RegisterShutdownObserver();
|
|
|
|
void UnregisterShutdownObserver();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2009-09-21 17:08:13 -07:00
|
|
|
// Notifies the element that decoding has failed.
|
|
|
|
void DecodeError();
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
/******
|
2009-02-05 00:02:21 -08:00
|
|
|
* The following members should be accessed with the decoder lock held.
|
2008-07-29 23:50:14 -07:00
|
|
|
******/
|
2009-02-05 00:02:21 -08:00
|
|
|
|
|
|
|
// Current decoding position in the stream. This is where the decoder
|
2009-03-31 17:52:56 -07:00
|
|
|
// is up to consuming the stream. This is not adjusted during decoder
|
|
|
|
// seek operations, but it's updated at the end when we start playing
|
|
|
|
// back again.
|
2009-02-05 00:02:21 -08:00
|
|
|
PRInt64 mDecoderPosition;
|
|
|
|
// Current playback position in the stream. This is (approximately)
|
2009-03-31 17:52:56 -07:00
|
|
|
// where we're up to playing back the stream. This is not adjusted
|
|
|
|
// during decoder seek operations, but it's updated at the end when we
|
|
|
|
// start playing back again.
|
2009-02-05 00:02:21 -08:00
|
|
|
PRInt64 mPlaybackPosition;
|
|
|
|
// Data needed to estimate playback data rate. The timeline used for
|
|
|
|
// this estimate is "decode time" (where the "current time" is the
|
|
|
|
// time of the last decoded video frame).
|
2009-03-31 17:52:56 -07:00
|
|
|
nsChannelStatistics mPlaybackStatistics;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Thread to handle decoding of Ogg data.
|
|
|
|
nsCOMPtr<nsIThread> mDecodeThread;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-23 01:02:18 -07:00
|
|
|
// The current playback position of the media resource in units of
|
|
|
|
// seconds. This is updated approximately at the framerate of the
|
|
|
|
// video (if it is a video) or the callback period of the audio.
|
|
|
|
// It is read and written from the main thread only.
|
|
|
|
float mCurrentTime;
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Volume that playback should start at. 0.0 = muted. 1.0 = full
|
|
|
|
// volume. Readable/Writeable from the main thread. Read from the
|
|
|
|
// audio thread when it is first started to get the initial volume
|
|
|
|
// level.
|
|
|
|
float mInitialVolume;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Position to seek to when the seek notification is received by the
|
|
|
|
// decoding thread. Written by the main thread and read via the
|
|
|
|
// decoding thread. Synchronised using mPlayStateMonitor. If the
|
|
|
|
// value is negative then no seek has been requested. When a seek is
|
|
|
|
// started this is reset to negative.
|
|
|
|
float mRequestedSeekTime;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-11-09 17:38:02 -08:00
|
|
|
// Duration of the media resource. Set to -1 if unknown.
|
|
|
|
// Set when the Ogg metadata is loaded. Accessed on the main thread
|
|
|
|
// only.
|
|
|
|
PRInt64 mDuration;
|
|
|
|
|
|
|
|
// True if the media resource is seekable (server supports byte range
|
|
|
|
// requests).
|
|
|
|
PRPackedBool mSeekable;
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
/******
|
|
|
|
* The following member variables can be accessed from any thread.
|
|
|
|
******/
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// The state machine object for handling the decoding via
|
|
|
|
// oggplay. It is safe to call methods of this object from other
|
|
|
|
// threads. Its internal data is synchronised on a monitor. The
|
|
|
|
// lifetime of this object is after mPlayState is LOADING and before
|
|
|
|
// mPlayState is SHUTDOWN. It is safe to access it during this
|
|
|
|
// period.
|
|
|
|
nsCOMPtr<nsOggDecodeStateMachine> mDecodeStateMachine;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
// OggPlay object used to read data from a channel. Created on main
|
|
|
|
// thread. Passed to liboggplay and the locking for multithreaded
|
2008-10-19 00:39:21 -07:00
|
|
|
// access is handled by that library. Some methods are called from
|
|
|
|
// the decoder thread, and the state machine for that thread keeps
|
|
|
|
// a pointer to this reader. This is safe as the only methods called
|
|
|
|
// are threadsafe (via the threadsafe nsMediaStream).
|
2008-11-18 00:19:32 -08:00
|
|
|
nsAutoPtr<nsChannelReader> mReader;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
// Monitor for detecting when the video play state changes. A call
|
|
|
|
// to Wait on this monitor will block the thread until the next
|
|
|
|
// state change.
|
|
|
|
PRMonitor* mMonitor;
|
|
|
|
|
|
|
|
// Set to one of the valid play states. It is protected by the
|
|
|
|
// monitor mMonitor. This monitor must be acquired when reading or
|
|
|
|
// writing the state. Any change to the state on the main thread
|
|
|
|
// must call NotifyAll on the monitor so the decode thread can wake up.
|
|
|
|
PlayState mPlayState;
|
|
|
|
|
2008-11-18 00:19:32 -08:00
|
|
|
// The state to change to after a seek or load operation. It must only
|
|
|
|
// be changed from the main thread. The decoder monitor must be acquired
|
|
|
|
// when writing to the state, or when reading from a non-main thread.
|
|
|
|
// Any change to the state must call NotifyAll on the monitor.
|
|
|
|
PlayState mNextState;
|
2009-01-15 12:26:51 -08:00
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
// True when we have fully loaded the resource and reported that
|
|
|
|
// to the element (i.e. reached NETWORK_LOADED state).
|
|
|
|
// Accessed on the main thread only.
|
2009-01-15 12:26:51 -08:00
|
|
|
PRPackedBool mResourceLoaded;
|
|
|
|
|
|
|
|
// True when seeking or otherwise moving the play position around in
|
|
|
|
// such a manner that progress event data is inaccurate. This is set
|
2009-02-05 00:02:21 -08:00
|
|
|
// during seek and duration operations to prevent the progress indicator
|
2009-01-15 12:26:51 -08:00
|
|
|
// from jumping around. Read/Write from any thread. Must have decode monitor
|
|
|
|
// locked before accessing.
|
|
|
|
PRPackedBool mIgnoreProgressData;
|
2008-07-29 23:50:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|