2008-11-06 12:53:20 -08: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) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Matthew Gregan <kinetik@flim.org>
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
#include "limits"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsIDOMHTMLMediaElement.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsAudioStream.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsHTMLMediaElement.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsWaveDecoder.h"
|
|
|
|
|
2009-04-11 02:39:24 -07:00
|
|
|
using mozilla::TimeDuration;
|
|
|
|
using mozilla::TimeStamp;
|
|
|
|
|
2009-09-29 14:32:44 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo* gWaveDecoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gWaveDecoderLog, type, msg)
|
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Maximum number of seconds to wait when buffering.
|
|
|
|
#define BUFFERING_TIMEOUT 3
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Duration the playback loop will sleep after refilling the backend's audio
|
|
|
|
// buffers. The loop's goal is to keep AUDIO_BUFFER_LENGTH milliseconds of
|
|
|
|
// audio buffered to allow time to refill before the backend underruns.
|
|
|
|
// Should be a multiple of 10 to deal with poor timer granularity on some
|
|
|
|
// platforms.
|
|
|
|
#define AUDIO_BUFFER_WAKEUP 100
|
|
|
|
#define AUDIO_BUFFER_LENGTH (2 * AUDIO_BUFFER_WAKEUP)
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
// Magic values that identify RIFF chunks we're interested in.
|
|
|
|
#define RIFF_CHUNK_MAGIC 0x52494646
|
|
|
|
#define WAVE_CHUNK_MAGIC 0x57415645
|
|
|
|
#define FRMT_CHUNK_MAGIC 0x666d7420
|
|
|
|
#define DATA_CHUNK_MAGIC 0x64617461
|
|
|
|
|
|
|
|
// Size of RIFF chunk header. 4 byte chunk header type and 4 byte size field.
|
|
|
|
#define RIFF_CHUNK_HEADER_SIZE 8
|
|
|
|
|
|
|
|
// Size of RIFF header. RIFF chunk and 4 byte RIFF type.
|
|
|
|
#define RIFF_INITIAL_SIZE (RIFF_CHUNK_HEADER_SIZE + 4)
|
|
|
|
|
|
|
|
// Size of required part of format chunk. Actual format chunks may be
|
|
|
|
// extended (for non-PCM encodings), but we skip any extended data.
|
|
|
|
#define WAVE_FORMAT_CHUNK_SIZE 16
|
|
|
|
|
|
|
|
// PCM encoding type from format chunk. Linear PCM is the only encoding
|
|
|
|
// supported by nsAudioStream.
|
|
|
|
#define WAVE_FORMAT_ENCODING_PCM 1
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
enum State {
|
|
|
|
STATE_LOADING_METADATA,
|
|
|
|
STATE_BUFFERING,
|
|
|
|
STATE_PLAYING,
|
|
|
|
STATE_SEEKING,
|
|
|
|
STATE_PAUSED,
|
|
|
|
STATE_ENDED,
|
|
|
|
STATE_ERROR,
|
|
|
|
STATE_SHUTDOWN
|
|
|
|
};
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
/*
|
|
|
|
A single nsWaveStateMachine instance is owned by the decoder, created
|
|
|
|
on-demand at load time. Upon creation, the decoder immediately
|
|
|
|
dispatches the state machine event to the decode thread to begin
|
|
|
|
execution. Once running, metadata loading begins immediately. If this
|
|
|
|
completes successfully, the state machine will move into a paused state
|
|
|
|
awaiting further commands. The state machine provides a small set of
|
|
|
|
threadsafe methods enabling the main thread to play, pause, seek, and
|
|
|
|
query parameters.
|
|
|
|
|
|
|
|
An weak (raw) pointer to the decoder's nsMediaStream is used by the state
|
|
|
|
machine to read data, seek, and query stream information. The decoder is
|
|
|
|
responsible for creating and opening the stream, and may also cancel it.
|
|
|
|
Every other stream operation is performed on the playback thread by the
|
|
|
|
state machine. A cancel from the main thread will force any in-flight
|
|
|
|
stream operations to abort.
|
|
|
|
*/
|
|
|
|
class nsWaveStateMachine : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2009-03-29 18:19:10 -07:00
|
|
|
nsWaveStateMachine(nsWaveDecoder* aDecoder,
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeDuration aBufferWaitTime, float aInitialVolume);
|
2008-11-06 12:53:20 -08:00
|
|
|
~nsWaveStateMachine();
|
|
|
|
|
2009-03-29 18:19:10 -07:00
|
|
|
void SetStream(nsMediaStream* aStream) { mStream = aStream; }
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Set specified volume. aVolume must be in range [0.0, 1.0].
|
|
|
|
// Threadsafe.
|
|
|
|
void SetVolume(float aVolume);
|
|
|
|
|
|
|
|
/*
|
|
|
|
The following four member functions initiate the appropriate state
|
|
|
|
transition suggested by the function name. Threadsafe.
|
|
|
|
*/
|
|
|
|
void Play();
|
|
|
|
void Pause();
|
|
|
|
void Seek(float aTime);
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
// Returns the playback length of the audio data in seconds, calculated
|
|
|
|
// from the length extracted from the metadata. Returns NaN if called
|
|
|
|
// before metadata validation has completed. Threadsafe.
|
|
|
|
float GetDuration();
|
|
|
|
|
|
|
|
// Returns true if the state machine is seeking. Threadsafe.
|
|
|
|
PRBool IsSeeking();
|
|
|
|
|
2008-12-14 10:02:54 -08:00
|
|
|
// Returns true if the state machine has reached the end of playback. Threadsafe.
|
|
|
|
PRBool IsEnded();
|
|
|
|
|
2009-01-16 00:22:08 -08:00
|
|
|
// Main state machine loop. Runs forever, until shutdown state is reached.
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_IMETHOD Run();
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
// Called by the decoder, on the main thread.
|
|
|
|
nsMediaDecoder::Statistics GetStatistics();
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
// Called on the decoder thread
|
|
|
|
void NotifyBytesConsumed(PRInt64 aBytes);
|
2009-02-05 00:02:21 -08:00
|
|
|
|
2009-02-10 17:43:45 -08:00
|
|
|
// Called by the main thread only
|
|
|
|
nsHTMLMediaElement::NextFrameStatus GetNextFrameStatus();
|
2009-02-05 00:02:21 -08:00
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Clear the flag indicating that a playback position change event is
|
|
|
|
// currently queued and return the current time. This is called from the
|
|
|
|
// main thread.
|
|
|
|
float GetTimeForPositionChange();
|
2009-02-15 17:05:28 -08:00
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
private:
|
2009-02-15 17:05:28 -08:00
|
|
|
// Returns PR_TRUE if we're in shutdown state. Threadsafe.
|
|
|
|
PRBool IsShutdown();
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Reads from the media stream. Returns PR_FALSE on failure or EOF. If
|
|
|
|
// aBytesRead is non-null, the number of bytes read will be returned via
|
|
|
|
// this.
|
|
|
|
PRBool ReadAll(char* aBuf, PRInt64 aSize, PRInt64* aBytesRead);
|
2009-02-15 17:05:28 -08:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Change the current state and wake the playback thread if it is waiting
|
|
|
|
// on mMonitor. Used by public member functions called from both threads,
|
|
|
|
// so must hold mMonitor. Threadsafe.
|
|
|
|
void ChangeState(State aState);
|
|
|
|
|
|
|
|
// Create and initialize audio stream using current audio parameters.
|
|
|
|
void OpenAudioStream();
|
|
|
|
|
|
|
|
// Shut down and dispose audio stream.
|
|
|
|
void CloseAudioStream();
|
|
|
|
|
|
|
|
// Read RIFF_INITIAL_SIZE from the beginning of the stream and verify that
|
|
|
|
// the stream data is a RIFF bitstream containing WAVE data.
|
|
|
|
PRBool LoadRIFFChunk();
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Read forward in the stream until aWantedChunk is found. Return chunk
|
|
|
|
// size in aChunkSize. aChunkSize will not be rounded up if the chunk
|
|
|
|
// size is odd.
|
|
|
|
PRBool ScanForwardUntil(PRUint32 aWantedChunk, PRUint32* aChunkSize);
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Scan forward in the stream looking for the WAVE format chunk. If
|
|
|
|
// found, parse and validate required metadata, then use it to set
|
2008-11-10 00:12:13 -08:00
|
|
|
// mSampleRate, mChannels, mSampleSize, and mSampleFormat.
|
2008-11-06 12:53:20 -08:00
|
|
|
PRBool LoadFormatChunk();
|
|
|
|
|
|
|
|
// Scan forward in the stream looking for the start of the PCM data. If
|
|
|
|
// found, record the data length and offset in mWaveLength and
|
|
|
|
// mWavePCMOffset.
|
|
|
|
PRBool FindDataOffset();
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Return the length of the PCM data.
|
|
|
|
PRInt64 GetDataLength();
|
|
|
|
|
|
|
|
// Fire a PlaybackPositionChanged event. If aCoalesce is true and a
|
|
|
|
// PlaybackPositionChanged event is already pending, an event is not
|
|
|
|
// fired.
|
|
|
|
void FirePositionChanged(PRBool aCoalesce);
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Returns the number of seconds that aBytes represents based on the
|
|
|
|
// current audio parameters. e.g. 176400 bytes is 1 second at 16-bit
|
|
|
|
// stereo 44.1kHz.
|
2009-02-16 20:56:16 -08:00
|
|
|
float BytesToTime(PRInt64 aBytes) const
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mMetadataValid, "Requires valid metadata");
|
2009-02-16 20:56:16 -08:00
|
|
|
NS_ABORT_IF_FALSE(aBytes >= 0, "Must be >= 0");
|
2008-11-06 12:53:20 -08:00
|
|
|
return float(aBytes) / mSampleRate / mSampleSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of bytes that aTime represents based on the current
|
|
|
|
// audio parameters. e.g. 1 second is 176400 bytes at 16-bit stereo
|
|
|
|
// 44.1kHz.
|
2009-02-16 20:56:16 -08:00
|
|
|
PRInt64 TimeToBytes(float aTime) const
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mMetadataValid, "Requires valid metadata");
|
2009-02-16 20:56:16 -08:00
|
|
|
NS_ABORT_IF_FALSE(aTime >= 0.0, "Must be >= 0");
|
|
|
|
return RoundDownToSample(PRInt64(aTime * mSampleRate * mSampleSize));
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rounds aBytes down to the nearest complete sample. Assumes beginning
|
|
|
|
// of byte range is already sample aligned by caller.
|
2009-02-16 20:56:16 -08:00
|
|
|
PRInt64 RoundDownToSample(PRInt64 aBytes) const
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mMetadataValid, "Requires valid metadata");
|
2009-02-16 20:56:16 -08:00
|
|
|
NS_ABORT_IF_FALSE(aBytes >= 0, "Must be >= 0");
|
2008-11-06 12:53:20 -08:00
|
|
|
return aBytes - (aBytes % mSampleSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weak (raw) pointer to our decoder instance. The decoder manages the
|
|
|
|
// lifetime of the state machine object, so it is guaranteed that the
|
|
|
|
// state machine will not outlive the decoder. The decoder is not
|
|
|
|
// threadsafe, so this pointer must only be used to create runnable events
|
|
|
|
// targeted at the main thread.
|
|
|
|
nsWaveDecoder* mDecoder;
|
|
|
|
|
|
|
|
// Weak (raw) pointer to a media stream. The decoder manages the lifetime
|
|
|
|
// of the stream, so it is guaranteed that the stream will live as long as
|
|
|
|
// the state machine. The stream is threadsafe, but is only used on the
|
|
|
|
// playback thread except for create, open, and cancel, which are called
|
|
|
|
// from the main thread.
|
|
|
|
nsMediaStream* mStream;
|
|
|
|
|
|
|
|
// Our audio stream. Created on demand when entering playback state. It
|
|
|
|
// is destroyed when seeking begins and will not be reinitialized until
|
|
|
|
// playback resumes, so it is possible for this to be null.
|
|
|
|
nsAutoPtr<nsAudioStream> mAudioStream;
|
|
|
|
|
2009-04-11 02:39:24 -07:00
|
|
|
// Maximum time to spend waiting for data during buffering.
|
|
|
|
TimeDuration mBufferingWait;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
// Machine time that buffering began, used with mBufferingWait to time out
|
|
|
|
// buffering.
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeStamp mBufferingStart;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Download position where we should stop buffering. Only accessed
|
|
|
|
// in the decoder thread.
|
|
|
|
PRInt64 mBufferingEndOffset;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Metadata extracted from the WAVE header. Used to initialize the audio
|
|
|
|
stream, and for byte<->time domain conversions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Number of samples per second. Limited to range [100, 96000] in LoadFormatChunk.
|
|
|
|
PRUint32 mSampleRate;
|
|
|
|
|
|
|
|
// Number of channels. Limited to range [1, 2] in LoadFormatChunk.
|
|
|
|
PRUint32 mChannels;
|
|
|
|
|
|
|
|
// Size of a single sample segment, which includes a sample for each
|
2008-11-10 00:12:13 -08:00
|
|
|
// channel (interleaved).
|
2008-11-06 12:53:20 -08:00
|
|
|
PRUint32 mSampleSize;
|
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
// The sample format of the PCM data.
|
|
|
|
nsAudioStream::SampleFormat mSampleFormat;
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Size of PCM data stored in the WAVE as reported by the data chunk in
|
|
|
|
// the media.
|
2009-02-16 20:56:16 -08:00
|
|
|
PRInt64 mWaveLength;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
// Start offset of the PCM data in the media stream. Extends mWaveLength
|
|
|
|
// bytes.
|
2009-02-16 20:56:16 -08:00
|
|
|
PRInt64 mWavePCMOffset;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
All member variables following this comment are accessed by both
|
|
|
|
threads and must be synchronized via mMonitor.
|
|
|
|
*/
|
|
|
|
PRMonitor* mMonitor;
|
|
|
|
|
|
|
|
// The state to enter when the state machine loop iterates next.
|
|
|
|
State mState;
|
|
|
|
|
|
|
|
// A queued state transition. This is used to record the next state
|
|
|
|
// transition when play or pause is requested during seeking or metadata
|
|
|
|
// loading to ensure a completed metadata load or seek returns to the most
|
|
|
|
// recently requested state on completion.
|
|
|
|
State mNextState;
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
// Current playback position in the stream.
|
|
|
|
PRInt64 mPlaybackPosition;
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Volume that the audio backend will be initialized with.
|
|
|
|
float mInitialVolume;
|
|
|
|
|
2008-12-14 06:54:45 -08:00
|
|
|
// Time position (in seconds) to seek to. Set by Seek(float).
|
|
|
|
float mSeekTime;
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// True once metadata has been parsed and validated. Users of mSampleRate,
|
2008-11-10 00:12:13 -08:00
|
|
|
// mChannels, mSampleSize, mSampleFormat, mWaveLength, mWavePCMOffset must
|
|
|
|
// check this flag before assuming the values are valid.
|
2008-11-06 12:53:20 -08:00
|
|
|
PRPackedBool mMetadataValid;
|
2009-02-16 20:56:16 -08:00
|
|
|
|
|
|
|
// True if an event to notify about a change in the playback position has
|
|
|
|
// been queued, but not yet run. It is set to false when the event is
|
|
|
|
// run. This allows coalescing of these events as they can be produced
|
|
|
|
// many times per second.
|
|
|
|
PRPackedBool mPositionChangeQueued;
|
2009-02-23 18:34:36 -08:00
|
|
|
|
|
|
|
// True if paused. Tracks only the play/paused state.
|
|
|
|
PRPackedBool mPaused;
|
2008-11-06 12:53:20 -08:00
|
|
|
};
|
|
|
|
|
2009-03-29 18:19:10 -07:00
|
|
|
nsWaveStateMachine::nsWaveStateMachine(nsWaveDecoder* aDecoder,
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeDuration aBufferWaitTime,
|
|
|
|
float aInitialVolume)
|
2008-11-06 12:53:20 -08:00
|
|
|
: mDecoder(aDecoder),
|
2009-03-29 18:19:10 -07:00
|
|
|
mStream(nsnull),
|
2008-11-06 12:53:20 -08:00
|
|
|
mBufferingWait(aBufferWaitTime),
|
2009-04-11 02:39:24 -07:00
|
|
|
mBufferingStart(),
|
2009-02-16 20:56:16 -08:00
|
|
|
mBufferingEndOffset(0),
|
2008-11-06 12:53:20 -08:00
|
|
|
mSampleRate(0),
|
|
|
|
mChannels(0),
|
|
|
|
mSampleSize(0),
|
2008-11-10 00:12:13 -08:00
|
|
|
mSampleFormat(nsAudioStream::FORMAT_S16_LE),
|
2008-11-06 12:53:20 -08:00
|
|
|
mWaveLength(0),
|
|
|
|
mWavePCMOffset(0),
|
|
|
|
mMonitor(nsnull),
|
|
|
|
mState(STATE_LOADING_METADATA),
|
|
|
|
mNextState(STATE_PAUSED),
|
2009-02-05 00:02:21 -08:00
|
|
|
mPlaybackPosition(0),
|
2008-11-06 12:53:20 -08:00
|
|
|
mInitialVolume(aInitialVolume),
|
2008-12-14 06:54:45 -08:00
|
|
|
mSeekTime(0.0),
|
2009-02-16 20:56:16 -08:00
|
|
|
mMetadataValid(PR_FALSE),
|
2009-02-23 18:34:36 -08:00
|
|
|
mPositionChangeQueued(PR_FALSE),
|
|
|
|
mPaused(mNextState == STATE_PAUSED)
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
|
|
|
mMonitor = nsAutoMonitor::NewMonitor("nsWaveStateMachine");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWaveStateMachine::~nsWaveStateMachine()
|
|
|
|
{
|
|
|
|
nsAutoMonitor::DestroyMonitor(mMonitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::Shutdown()
|
|
|
|
{
|
|
|
|
ChangeState(STATE_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::Play()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2009-02-23 18:34:36 -08:00
|
|
|
mPaused = PR_FALSE;
|
2009-03-31 18:07:17 -07:00
|
|
|
if (mState == STATE_ENDED) {
|
|
|
|
Seek(0);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mState == STATE_LOADING_METADATA || mState == STATE_SEEKING) {
|
|
|
|
mNextState = STATE_PLAYING;
|
|
|
|
} else {
|
|
|
|
ChangeState(STATE_PLAYING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::SetVolume(float aVolume)
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
mInitialVolume = aVolume;
|
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->SetVolume(aVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::Pause()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2009-02-23 18:34:36 -08:00
|
|
|
mPaused = PR_TRUE;
|
2009-03-31 18:07:17 -07:00
|
|
|
if (mState == STATE_LOADING_METADATA || mState == STATE_SEEKING ||
|
2009-05-27 20:49:22 -07:00
|
|
|
mState == STATE_BUFFERING || mState == STATE_ENDED) {
|
2008-11-06 12:53:20 -08:00
|
|
|
mNextState = STATE_PAUSED;
|
2009-03-31 18:07:17 -07:00
|
|
|
} else if (mState == STATE_PLAYING) {
|
2008-11-06 12:53:20 -08:00
|
|
|
ChangeState(STATE_PAUSED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::Seek(float aTime)
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2008-12-14 12:46:28 -08:00
|
|
|
mSeekTime = aTime;
|
2008-12-14 06:54:45 -08:00
|
|
|
if (mSeekTime < 0.0) {
|
|
|
|
mSeekTime = 0.0;
|
2008-12-02 17:38:15 -08:00
|
|
|
}
|
2008-12-14 12:46:28 -08:00
|
|
|
if (mState == STATE_LOADING_METADATA) {
|
|
|
|
mNextState = STATE_SEEKING;
|
|
|
|
} else if (mState != STATE_SEEKING) {
|
2009-03-31 18:07:17 -07:00
|
|
|
if (mState == STATE_ENDED) {
|
|
|
|
mNextState = mPaused ? STATE_PAUSED : STATE_PLAYING;
|
|
|
|
} else {
|
|
|
|
mNextState = mState;
|
|
|
|
}
|
2008-12-14 12:46:28 -08:00
|
|
|
ChangeState(STATE_SEEKING);
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
nsWaveStateMachine::GetDuration()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
if (mMetadataValid) {
|
2009-02-16 20:56:16 -08:00
|
|
|
return BytesToTime(GetDataLength());
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::IsSeeking()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2008-12-14 10:02:54 -08:00
|
|
|
return mState == STATE_SEEKING || mNextState == STATE_SEEKING;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::IsEnded()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
return mState == STATE_ENDED || mState == STATE_SHUTDOWN;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-10 17:43:45 -08:00
|
|
|
nsHTMLMediaElement::NextFrameStatus
|
|
|
|
nsWaveStateMachine::GetNextFrameStatus()
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2009-02-10 17:43:45 -08:00
|
|
|
if (mState == STATE_BUFFERING)
|
|
|
|
return nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE_BUFFERING;
|
2009-09-29 14:32:41 -07:00
|
|
|
// If mMetadataValid is false then we can't call GetDataLength because
|
|
|
|
// we haven't got the length from the Wave header yet. But we know that
|
|
|
|
// if we haven't read the metadata then we don't have playable data.
|
|
|
|
if (mMetadataValid &&
|
|
|
|
mPlaybackPosition < mStream->GetCachedDataEnd(mPlaybackPosition) &&
|
|
|
|
mPlaybackPosition < mWavePCMOffset + GetDataLength())
|
2009-02-16 20:56:16 -08:00
|
|
|
return nsHTMLMediaElement::NEXT_FRAME_AVAILABLE;
|
2009-02-10 17:43:45 -08:00
|
|
|
return nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
float
|
|
|
|
nsWaveStateMachine::GetTimeForPositionChange()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
mPositionChangeQueued = PR_FALSE;
|
2009-04-05 17:20:51 -07:00
|
|
|
return BytesToTime(mPlaybackPosition - mWavePCMOffset);
|
2009-02-16 20:56:16 -08:00
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWaveStateMachine::Run()
|
|
|
|
{
|
|
|
|
// Monitor is held by this thread almost permanently, but must be manually
|
|
|
|
// dropped during long operations to prevent the main thread from blocking
|
|
|
|
// when calling methods on the state machine object.
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (mState) {
|
|
|
|
case STATE_LOADING_METADATA:
|
|
|
|
{
|
|
|
|
monitor.Exit();
|
|
|
|
PRBool loaded = LoadRIFFChunk() && LoadFormatChunk() && FindDataOffset();
|
|
|
|
monitor.Enter();
|
|
|
|
|
2009-09-21 17:08:13 -07:00
|
|
|
if (!loaded) {
|
|
|
|
ChangeState(STATE_ERROR);
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-09-21 17:08:13 -07:00
|
|
|
if (mState == STATE_LOADING_METADATA) {
|
|
|
|
mMetadataValid = PR_TRUE;
|
|
|
|
if (mNextState != STATE_SEEKING) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, MetadataLoaded);
|
2008-12-14 10:02:54 -08:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2009-09-21 17:08:13 -07:00
|
|
|
ChangeState(mNextState);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
case STATE_BUFFERING: {
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
if (now - mBufferingStart < mBufferingWait &&
|
2009-03-31 17:52:56 -07:00
|
|
|
mStream->GetCachedDataEnd(mPlaybackPosition) < mBufferingEndOffset &&
|
|
|
|
!mStream->IsDataCachedToEndOfStream(mPlaybackPosition) &&
|
|
|
|
!mStream->IsSuspendedByCache()) {
|
2009-02-16 20:56:16 -08:00
|
|
|
LOG(PR_LOG_DEBUG,
|
2009-04-11 02:39:24 -07:00
|
|
|
("In buffering: buffering data until %d bytes available or %f seconds\n",
|
2009-03-31 17:52:56 -07:00
|
|
|
PRUint32(mBufferingEndOffset - mStream->GetCachedDataEnd(mPlaybackPosition)),
|
2009-04-11 02:39:24 -07:00
|
|
|
(mBufferingWait - (now - mBufferingStart)).ToSeconds()));
|
2008-11-06 12:53:20 -08:00
|
|
|
monitor.Wait(PR_MillisecondsToInterval(1000));
|
|
|
|
} else {
|
|
|
|
ChangeState(mNextState);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2009-02-10 17:43:45 -08:00
|
|
|
NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, UpdateReadyStateForData);
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
case STATE_PLAYING: {
|
2008-11-06 12:53:20 -08:00
|
|
|
if (!mAudioStream) {
|
|
|
|
OpenAudioStream();
|
2009-02-16 20:56:16 -08:00
|
|
|
if (!mAudioStream) {
|
2009-03-02 19:18:23 -08:00
|
|
|
ChangeState(STATE_ERROR);
|
2009-02-16 20:56:16 -08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
TimeStamp lastWakeup = now -
|
2009-09-21 17:08:13 -07:00
|
|
|
TimeDuration::FromMilliseconds(AUDIO_BUFFER_LENGTH);
|
2009-02-16 20:56:16 -08:00
|
|
|
|
|
|
|
do {
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeDuration sleepTime = now - lastWakeup;
|
2009-02-16 20:56:16 -08:00
|
|
|
lastWakeup = now;
|
|
|
|
|
|
|
|
// We aim to have AUDIO_BUFFER_LENGTH milliseconds of audio
|
|
|
|
// buffered, but only sleep for AUDIO_BUFFER_WAKEUP milliseconds
|
|
|
|
// (waking early to refill before the backend underruns). Since we
|
|
|
|
// wake early, we only buffer sleepTime milliseconds of audio since
|
|
|
|
// there is still AUDIO_BUFFER_LENGTH - sleepTime milliseconds of
|
|
|
|
// audio buffered.
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeDuration targetTime =
|
|
|
|
TimeDuration::FromMilliseconds(AUDIO_BUFFER_LENGTH);
|
2009-02-16 20:56:16 -08:00
|
|
|
if (sleepTime < targetTime) {
|
|
|
|
targetTime = sleepTime;
|
|
|
|
}
|
2009-02-10 17:43:45 -08:00
|
|
|
|
2009-04-11 02:39:24 -07:00
|
|
|
PRInt64 len = TimeToBytes(targetTime.ToSeconds());
|
2009-02-10 17:43:45 -08:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
PRInt64 leftToPlay =
|
|
|
|
GetDataLength() - (mPlaybackPosition - mWavePCMOffset);
|
2009-02-16 20:56:16 -08:00
|
|
|
if (leftToPlay <= len) {
|
|
|
|
len = leftToPlay;
|
2009-02-05 00:02:21 -08:00
|
|
|
ChangeState(STATE_ENDED);
|
|
|
|
}
|
2009-02-16 20:56:16 -08:00
|
|
|
|
2009-06-01 14:55:58 -07:00
|
|
|
PRInt64 availableOffset = mStream->GetCachedDataEnd(mPlaybackPosition);
|
2009-02-16 20:56:16 -08:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
// don't buffer if we're at the end of the stream, or if the
|
|
|
|
// load has been suspended by the cache (in the latter case
|
|
|
|
// we need to advance playback to free up cache space)
|
2009-06-01 14:55:58 -07:00
|
|
|
if (mState != STATE_ENDED &&
|
|
|
|
availableOffset < mPlaybackPosition + len &&
|
2009-03-31 17:52:56 -07:00
|
|
|
!mStream->IsSuspendedByCache()) {
|
2009-09-21 17:08:13 -07:00
|
|
|
mBufferingStart = now;
|
|
|
|
mBufferingEndOffset = mPlaybackPosition +
|
|
|
|
TimeToBytes(mBufferingWait.ToSeconds());
|
|
|
|
mBufferingEndOffset = PR_MAX(mPlaybackPosition + len, mBufferingEndOffset);
|
|
|
|
mNextState = mState;
|
|
|
|
ChangeState(STATE_BUFFERING);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, UpdateReadyStateForData);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
2009-02-16 20:56:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
nsAutoArrayPtr<char> buf(new char[size_t(len)]);
|
|
|
|
PRInt64 got = 0;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
monitor.Exit();
|
2009-02-16 20:56:16 -08:00
|
|
|
PRBool ok = ReadAll(buf.get(), len, &got);
|
|
|
|
monitor.Enter();
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// Reached EOF.
|
|
|
|
if (!ok) {
|
2008-11-06 12:53:20 -08:00
|
|
|
ChangeState(STATE_ENDED);
|
2009-02-16 20:56:16 -08:00
|
|
|
if (got == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate difference between the current media stream position
|
|
|
|
// and the expected end of the PCM data.
|
2009-03-31 17:52:56 -07:00
|
|
|
PRInt64 endDelta = mWavePCMOffset + mWaveLength - mPlaybackPosition;
|
2008-11-06 12:53:20 -08:00
|
|
|
if (endDelta < 0) {
|
2009-02-16 20:56:16 -08:00
|
|
|
// Read past the end of PCM data. Adjust got to avoid playing
|
2008-11-06 12:53:20 -08:00
|
|
|
// back trailing data.
|
2009-02-16 20:56:16 -08:00
|
|
|
got -= -endDelta;
|
2008-11-06 12:53:20 -08:00
|
|
|
ChangeState(STATE_ENDED);
|
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
if (mState == STATE_ENDED) {
|
|
|
|
got = RoundDownToSample(got);
|
2008-11-10 00:12:13 -08:00
|
|
|
}
|
2009-02-16 20:56:16 -08:00
|
|
|
|
|
|
|
PRUint32 sampleSize = mSampleFormat == nsAudioStream::FORMAT_U8 ? 1 : 2;
|
|
|
|
NS_ABORT_IF_FALSE(got % sampleSize == 0, "Must write complete samples");
|
|
|
|
PRUint32 lengthInSamples = got / sampleSize;
|
|
|
|
|
|
|
|
monitor.Exit();
|
2008-11-10 00:12:13 -08:00
|
|
|
mAudioStream->Write(buf.get(), lengthInSamples);
|
2008-11-06 12:53:20 -08:00
|
|
|
monitor.Enter();
|
2009-02-16 20:56:16 -08:00
|
|
|
|
|
|
|
FirePositionChanged(PR_FALSE);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
if (mState == STATE_PLAYING) {
|
|
|
|
monitor.Wait(PR_MillisecondsToInterval(AUDIO_BUFFER_WAKEUP));
|
2009-04-11 02:39:24 -07:00
|
|
|
now = TimeStamp::Now();
|
2009-02-16 20:56:16 -08:00
|
|
|
}
|
|
|
|
} while (mState == STATE_PLAYING);
|
2008-11-06 12:53:20 -08:00
|
|
|
break;
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
case STATE_SEEKING:
|
|
|
|
{
|
|
|
|
CloseAudioStream();
|
|
|
|
|
2008-12-14 06:54:45 -08:00
|
|
|
mSeekTime = NS_MIN(mSeekTime, GetDuration());
|
|
|
|
float seekTime = mSeekTime;
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
monitor.Exit();
|
|
|
|
nsCOMPtr<nsIRunnable> startEvent =
|
|
|
|
NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, SeekingStarted);
|
|
|
|
NS_DispatchToMainThread(startEvent, NS_DISPATCH_SYNC);
|
|
|
|
monitor.Enter();
|
|
|
|
|
|
|
|
if (mState == STATE_SHUTDOWN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-14 06:54:45 -08:00
|
|
|
// Calculate relative offset within PCM data.
|
|
|
|
PRInt64 position = RoundDownToSample(TimeToBytes(seekTime));
|
2009-02-16 20:56:16 -08:00
|
|
|
NS_ABORT_IF_FALSE(position >= 0 && position <= GetDataLength(), "Invalid seek position");
|
2008-12-14 06:54:45 -08:00
|
|
|
// Convert to absolute offset within stream.
|
|
|
|
position += mWavePCMOffset;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
monitor.Exit();
|
2009-01-16 00:22:08 -08:00
|
|
|
nsresult rv;
|
|
|
|
rv = mStream->Seek(nsISeekableStream::NS_SEEK_SET, position);
|
2008-11-06 12:53:20 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Seek failed");
|
|
|
|
}
|
|
|
|
monitor.Enter();
|
2009-03-31 17:52:56 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mPlaybackPosition = position;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
if (mState == STATE_SHUTDOWN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-14 06:54:45 -08:00
|
|
|
if (mState == STATE_SEEKING && mSeekTime == seekTime) {
|
2009-02-23 18:34:36 -08:00
|
|
|
// Special case #1: if a seek was requested during metadata load,
|
2008-12-14 12:46:28 -08:00
|
|
|
// mNextState will have been clobbered. This can only happen when
|
|
|
|
// we're instantiating a decoder to service a seek request after
|
|
|
|
// playback has ended, so we know that the clobbered mNextState
|
|
|
|
// was PAUSED.
|
2009-02-23 18:34:36 -08:00
|
|
|
// Special case #2: if a seek is requested after the state machine
|
|
|
|
// entered STATE_ENDED but before the user has seen the ended
|
|
|
|
// event, playback has not ended as far as the user's
|
|
|
|
// concerned--the state machine needs to return to the last
|
|
|
|
// playback state.
|
2009-10-19 19:48:03 -07:00
|
|
|
// Special case #3: if seeking to the end of the media, transition
|
|
|
|
// directly into STATE_ENDED.
|
2008-12-14 12:46:28 -08:00
|
|
|
State nextState = mNextState;
|
|
|
|
if (nextState == STATE_SEEKING) {
|
|
|
|
nextState = STATE_PAUSED;
|
2009-02-23 18:34:36 -08:00
|
|
|
} else if (nextState == STATE_ENDED) {
|
|
|
|
nextState = mPaused ? STATE_PAUSED : STATE_PLAYING;
|
2009-10-19 19:48:03 -07:00
|
|
|
} else if (GetDuration() == seekTime) {
|
|
|
|
nextState = STATE_ENDED;
|
2008-12-14 12:46:28 -08:00
|
|
|
}
|
|
|
|
ChangeState(nextState);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
2009-02-16 20:56:16 -08:00
|
|
|
|
|
|
|
FirePositionChanged(PR_TRUE);
|
|
|
|
|
|
|
|
monitor.Exit();
|
|
|
|
nsCOMPtr<nsIRunnable> stopEvent =
|
|
|
|
NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, SeekingStopped);
|
|
|
|
NS_DispatchToMainThread(stopEvent, NS_DISPATCH_SYNC);
|
|
|
|
monitor.Enter();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_PAUSED:
|
|
|
|
monitor.Wait();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ENDED:
|
2009-02-16 20:56:16 -08:00
|
|
|
FirePositionChanged(PR_TRUE);
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mAudioStream) {
|
|
|
|
monitor.Exit();
|
|
|
|
mAudioStream->Drain();
|
|
|
|
monitor.Enter();
|
2009-04-05 17:19:33 -07:00
|
|
|
|
|
|
|
// After the drain call the audio stream is unusable. Close it so that
|
|
|
|
// next time audio is used a new stream is created.
|
|
|
|
CloseAudioStream();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-23 18:34:36 -08:00
|
|
|
if (mState == STATE_ENDED) {
|
2008-11-06 12:53:20 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, PlaybackEnded);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
2009-02-23 18:34:36 -08:00
|
|
|
do {
|
|
|
|
monitor.Wait();
|
|
|
|
} while (mState == STATE_ENDED);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ERROR:
|
2009-09-21 17:08:13 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event = NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, DecodeError);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
monitor.Wait();
|
|
|
|
|
|
|
|
if (mState != STATE_SHUTDOWN) {
|
|
|
|
NS_WARNING("Invalid state transition");
|
|
|
|
ChangeState(STATE_ERROR);
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SHUTDOWN:
|
|
|
|
CloseAudioStream();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
#if defined(DEBUG)
|
|
|
|
static PRBool
|
|
|
|
IsValidStateTransition(State aStartState, State aEndState)
|
|
|
|
{
|
|
|
|
if (aEndState == STATE_SHUTDOWN) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStartState == aEndState) {
|
|
|
|
LOG(PR_LOG_WARNING, ("Transition to current state requested"));
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aStartState) {
|
|
|
|
case STATE_LOADING_METADATA:
|
|
|
|
if (aEndState == STATE_PLAYING || aEndState == STATE_SEEKING ||
|
|
|
|
aEndState == STATE_PAUSED || aEndState == STATE_ERROR)
|
|
|
|
return PR_TRUE;
|
|
|
|
break;
|
|
|
|
case STATE_BUFFERING:
|
|
|
|
if (aEndState == STATE_PLAYING || aEndState == STATE_PAUSED ||
|
|
|
|
aEndState == STATE_SEEKING)
|
|
|
|
return PR_TRUE;
|
|
|
|
break;
|
|
|
|
case STATE_PLAYING:
|
|
|
|
if (aEndState == STATE_BUFFERING || aEndState == STATE_SEEKING ||
|
|
|
|
aEndState == STATE_ENDED || aEndState == STATE_PAUSED)
|
|
|
|
return PR_TRUE;
|
|
|
|
break;
|
|
|
|
case STATE_SEEKING:
|
2009-10-19 19:48:03 -07:00
|
|
|
if (aEndState == STATE_PLAYING || aEndState == STATE_PAUSED ||
|
|
|
|
aEndState == STATE_ENDED)
|
2009-02-16 20:56:16 -08:00
|
|
|
return PR_TRUE;
|
|
|
|
break;
|
|
|
|
case STATE_PAUSED:
|
|
|
|
if (aEndState == STATE_PLAYING || aEndState == STATE_SEEKING)
|
|
|
|
return PR_TRUE;
|
|
|
|
break;
|
|
|
|
case STATE_ENDED:
|
2009-02-23 18:34:36 -08:00
|
|
|
if (aEndState == STATE_SEEKING)
|
|
|
|
return PR_TRUE;
|
|
|
|
/* fallthrough */
|
2009-02-16 20:56:16 -08:00
|
|
|
case STATE_ERROR:
|
|
|
|
case STATE_SHUTDOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(PR_LOG_ERROR, ("Invalid state transition from %d to %d", aStartState, aEndState));
|
|
|
|
return PR_FALSE;
|
2008-12-14 06:54:45 -08:00
|
|
|
}
|
2009-02-16 20:56:16 -08:00
|
|
|
#endif
|
2008-12-14 06:54:45 -08:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
void
|
|
|
|
nsWaveStateMachine::ChangeState(State aState)
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2009-03-02 19:18:23 -08:00
|
|
|
if (mState == STATE_SHUTDOWN) {
|
|
|
|
LOG(PR_LOG_WARNING, ("In shutdown, state transition ignored"));
|
|
|
|
return;
|
|
|
|
}
|
2009-02-16 20:56:16 -08:00
|
|
|
#if defined(DEBUG)
|
|
|
|
NS_ABORT_IF_FALSE(IsValidStateTransition(mState, aState), "Invalid state transition");
|
|
|
|
#endif
|
2008-11-06 12:53:20 -08:00
|
|
|
mState = aState;
|
|
|
|
monitor.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::OpenAudioStream()
|
|
|
|
{
|
|
|
|
mAudioStream = new nsAudioStream();
|
|
|
|
if (!mAudioStream) {
|
|
|
|
LOG(PR_LOG_ERROR, ("Could not create audio stream"));
|
|
|
|
} else {
|
2008-11-10 00:12:13 -08:00
|
|
|
NS_ABORT_IF_FALSE(mMetadataValid,
|
|
|
|
"Attempting to initialize audio stream with invalid metadata");
|
|
|
|
mAudioStream->Init(mChannels, mSampleRate, mSampleFormat);
|
2008-11-06 12:53:20 -08:00
|
|
|
mAudioStream->SetVolume(mInitialVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::CloseAudioStream()
|
|
|
|
{
|
|
|
|
if (mAudioStream) {
|
|
|
|
mAudioStream->Shutdown();
|
|
|
|
mAudioStream = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
nsMediaDecoder::Statistics
|
|
|
|
nsWaveStateMachine::GetStatistics()
|
|
|
|
{
|
|
|
|
nsMediaDecoder::Statistics result;
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
2009-03-31 17:52:56 -07:00
|
|
|
result.mDownloadRate = mStream->GetDownloadRate(&result.mDownloadRateReliable);
|
2009-02-05 00:02:21 -08:00
|
|
|
result.mPlaybackRate = mSampleRate*mChannels*mSampleSize;
|
|
|
|
result.mPlaybackRateReliable = PR_TRUE;
|
2009-03-31 17:52:56 -07:00
|
|
|
result.mTotalBytes = mStream->GetLength();
|
|
|
|
result.mDownloadPosition = mStream->GetCachedDataEnd(mPlaybackPosition);
|
2009-02-05 00:02:21 -08:00
|
|
|
result.mDecoderPosition = mPlaybackPosition;
|
|
|
|
result.mPlaybackPosition = mPlaybackPosition;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::NotifyBytesConsumed(PRInt64 aBytes)
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
mPlaybackPosition += aBytes;
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
static PRUint32
|
|
|
|
ReadUint32BE(const char** aBuffer)
|
|
|
|
{
|
|
|
|
PRUint32 result =
|
|
|
|
PRUint8((*aBuffer)[0]) << 24 |
|
|
|
|
PRUint8((*aBuffer)[1]) << 16 |
|
|
|
|
PRUint8((*aBuffer)[2]) << 8 |
|
|
|
|
PRUint8((*aBuffer)[3]);
|
|
|
|
*aBuffer += sizeof(PRUint32);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRUint32
|
|
|
|
ReadUint32LE(const char** aBuffer)
|
|
|
|
{
|
|
|
|
PRUint32 result =
|
|
|
|
PRUint8((*aBuffer)[3]) << 24 |
|
|
|
|
PRUint8((*aBuffer)[2]) << 16 |
|
|
|
|
PRUint8((*aBuffer)[1]) << 8 |
|
|
|
|
PRUint8((*aBuffer)[0]);
|
|
|
|
*aBuffer += sizeof(PRUint32);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRUint16
|
|
|
|
ReadUint16LE(const char** aBuffer)
|
|
|
|
{
|
|
|
|
PRUint16 result =
|
|
|
|
PRUint8((*aBuffer)[1]) << 8 |
|
|
|
|
PRUint8((*aBuffer)[0]) << 0;
|
|
|
|
*aBuffer += sizeof(PRUint16);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-15 17:05:28 -08:00
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::IsShutdown()
|
|
|
|
{
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
return mState == STATE_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2009-02-16 20:56:16 -08:00
|
|
|
nsWaveStateMachine::ReadAll(char* aBuf, PRInt64 aSize, PRInt64* aBytesRead = nsnull)
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
|
|
|
PRUint32 got = 0;
|
2009-02-16 20:56:16 -08:00
|
|
|
if (aBytesRead) {
|
|
|
|
*aBytesRead = 0;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
do {
|
|
|
|
PRUint32 read = 0;
|
2009-02-15 17:05:28 -08:00
|
|
|
if (NS_FAILED(mStream->Read(aBuf + got, aSize - got, &read))) {
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_WARNING("Stream read failed");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2009-02-16 20:56:16 -08:00
|
|
|
if (IsShutdown() || read == 0) {
|
2009-02-15 17:05:28 -08:00
|
|
|
return PR_FALSE;
|
2009-02-16 20:56:16 -08:00
|
|
|
}
|
2009-03-29 18:19:10 -07:00
|
|
|
NotifyBytesConsumed(read);
|
2008-11-06 12:53:20 -08:00
|
|
|
got += read;
|
2009-02-16 20:56:16 -08:00
|
|
|
if (aBytesRead) {
|
|
|
|
*aBytesRead = got;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
} while (got != aSize);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::LoadRIFFChunk()
|
|
|
|
{
|
|
|
|
char riffHeader[RIFF_INITIAL_SIZE];
|
|
|
|
const char* p = riffHeader;
|
|
|
|
|
|
|
|
NS_ABORT_IF_FALSE(mStream->Tell() == 0,
|
|
|
|
"LoadRIFFChunk called when stream in invalid state");
|
|
|
|
|
2009-02-15 17:05:28 -08:00
|
|
|
if (!ReadAll(riffHeader, sizeof(riffHeader))) {
|
2008-11-06 12:53:20 -08:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadUint32BE(&p) != RIFF_CHUNK_MAGIC) {
|
|
|
|
NS_WARNING("Stream data not in RIFF format");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over RIFF size field.
|
|
|
|
p += 4;
|
|
|
|
|
|
|
|
if (ReadUint32BE(&p) != WAVE_CHUNK_MAGIC) {
|
|
|
|
NS_WARNING("Expected WAVE chunk");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::ScanForwardUntil(PRUint32 aWantedChunk, PRUint32* aChunkSize)
|
|
|
|
{
|
2009-03-15 22:53:48 -07:00
|
|
|
NS_ABORT_IF_FALSE(aChunkSize, "Require aChunkSize argument");
|
2009-02-16 20:56:16 -08:00
|
|
|
*aChunkSize = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char chunkHeader[8];
|
|
|
|
const char* p = chunkHeader;
|
|
|
|
|
|
|
|
if (!ReadAll(chunkHeader, sizeof(chunkHeader))) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 magic = ReadUint32BE(&p);
|
2009-09-06 20:28:26 -07:00
|
|
|
PRUint32 chunkSize = ReadUint32LE(&p);
|
2009-02-16 20:56:16 -08:00
|
|
|
|
|
|
|
if (magic == aWantedChunk) {
|
2009-09-06 20:28:26 -07:00
|
|
|
*aChunkSize = chunkSize;
|
2009-02-16 20:56:16 -08:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RIFF chunks are two-byte aligned, so round up if necessary.
|
2009-09-06 20:28:26 -07:00
|
|
|
chunkSize += chunkSize % 2;
|
2009-02-16 20:56:16 -08:00
|
|
|
|
2009-09-06 20:28:26 -07:00
|
|
|
while (chunkSize > 0) {
|
|
|
|
PRUint32 size = PR_MIN(chunkSize, 1 << 16);
|
|
|
|
nsAutoArrayPtr<char> chunk(new char[size]);
|
|
|
|
if (!ReadAll(chunk.get(), size)) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
chunkSize -= size;
|
2009-02-16 20:56:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::LoadFormatChunk()
|
|
|
|
{
|
2009-02-16 20:56:16 -08:00
|
|
|
PRUint32 fmtSize, rate, channels, sampleSize, sampleFormat;
|
|
|
|
char waveFormat[WAVE_FORMAT_CHUNK_SIZE];
|
2008-11-06 12:53:20 -08:00
|
|
|
const char* p = waveFormat;
|
|
|
|
|
|
|
|
// RIFF chunks are always word (two byte) aligned.
|
|
|
|
NS_ABORT_IF_FALSE(mStream->Tell() % 2 == 0,
|
|
|
|
"LoadFormatChunk called with unaligned stream");
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
// The "format" chunk may not directly follow the "riff" chunk, so skip
|
|
|
|
// over any intermediate chunks.
|
|
|
|
if (!ScanForwardUntil(FRMT_CHUNK_MAGIC, &fmtSize)) {
|
|
|
|
return PR_FALSE;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
if (!ReadAll(waveFormat, sizeof(waveFormat))) {
|
2008-11-06 12:53:20 -08:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadUint16LE(&p) != WAVE_FORMAT_ENCODING_PCM) {
|
|
|
|
NS_WARNING("WAVE is not uncompressed PCM, compressed encodings are not supported");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
channels = ReadUint16LE(&p);
|
|
|
|
rate = ReadUint32LE(&p);
|
|
|
|
|
|
|
|
// Skip over average bytes per second field.
|
|
|
|
p += 4;
|
|
|
|
|
|
|
|
sampleSize = ReadUint16LE(&p);
|
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
sampleFormat = ReadUint16LE(&p);
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
// PCM encoded WAVEs are not expected to have an extended "format" chunk,
|
|
|
|
// but I have found WAVEs that have a extended "format" chunk with an
|
|
|
|
// extension size of 0 bytes. Be polite and handle this rather than
|
|
|
|
// considering the file invalid. This code skips any extension of the
|
|
|
|
// "format" chunk.
|
2009-02-16 20:56:16 -08:00
|
|
|
if (fmtSize > WAVE_FORMAT_CHUNK_SIZE) {
|
2008-11-06 12:53:20 -08:00
|
|
|
char extLength[2];
|
|
|
|
const char* p = extLength;
|
|
|
|
|
2009-02-15 17:05:28 -08:00
|
|
|
if (!ReadAll(extLength, sizeof(extLength))) {
|
2008-11-06 12:53:20 -08:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 extra = ReadUint16LE(&p);
|
2009-02-16 20:56:16 -08:00
|
|
|
if (fmtSize - (WAVE_FORMAT_CHUNK_SIZE + 2) != extra) {
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_WARNING("Invalid extended format chunk size");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
extra += extra % 2;
|
|
|
|
|
|
|
|
if (extra > 0) {
|
|
|
|
nsAutoArrayPtr<char> chunkExtension(new char[extra]);
|
2009-02-15 17:05:28 -08:00
|
|
|
if (!ReadAll(chunkExtension.get(), extra)) {
|
2008-11-06 12:53:20 -08:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RIFF chunks are always word (two byte) aligned.
|
|
|
|
NS_ABORT_IF_FALSE(mStream->Tell() % 2 == 0,
|
|
|
|
"LoadFormatChunk left stream unaligned");
|
|
|
|
|
|
|
|
// Make sure metadata is fairly sane. The rate check is fairly arbitrary,
|
2008-11-10 00:12:13 -08:00
|
|
|
// but the channels check is intentionally limited to mono or stereo
|
|
|
|
// because that's what the audio backend currently supports.
|
2008-11-06 12:53:20 -08:00
|
|
|
if (rate < 100 || rate > 96000 ||
|
|
|
|
channels < 1 || channels > 2 ||
|
2008-11-10 00:12:13 -08:00
|
|
|
(sampleSize != 1 && sampleSize != 2 && sampleSize != 4) ||
|
|
|
|
(sampleFormat != 8 && sampleFormat != 16)) {
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_WARNING("Invalid WAVE metadata");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
mSampleRate = rate;
|
|
|
|
mChannels = channels;
|
|
|
|
mSampleSize = sampleSize;
|
2008-11-10 00:12:13 -08:00
|
|
|
if (sampleFormat == 8) {
|
|
|
|
mSampleFormat = nsAudioStream::FORMAT_U8;
|
|
|
|
} else {
|
|
|
|
mSampleFormat = nsAudioStream::FORMAT_S16_LE;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsWaveStateMachine::FindDataOffset()
|
|
|
|
{
|
|
|
|
// RIFF chunks are always word (two byte) aligned.
|
|
|
|
NS_ABORT_IF_FALSE(mStream->Tell() % 2 == 0,
|
|
|
|
"FindDataOffset called with unaligned stream");
|
|
|
|
|
|
|
|
// The "data" chunk may not directly follow the "format" chunk, so skip
|
|
|
|
// over any intermediate chunks.
|
2009-04-05 17:20:51 -07:00
|
|
|
PRUint32 length;
|
2009-02-16 20:56:16 -08:00
|
|
|
if (!ScanForwardUntil(DATA_CHUNK_MAGIC, &length)) {
|
|
|
|
return PR_FALSE;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-04-05 17:20:51 -07:00
|
|
|
PRInt64 offset = mStream->Tell();
|
|
|
|
if (offset <= 0 || offset > PR_UINT32_MAX) {
|
|
|
|
NS_WARNING("PCM data offset out of range");
|
2008-11-06 12:53:20 -08:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoMonitor monitor(mMonitor);
|
|
|
|
mWaveLength = length;
|
|
|
|
mWavePCMOffset = PRUint32(offset);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
PRInt64
|
|
|
|
nsWaveStateMachine::GetDataLength()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mMetadataValid,
|
|
|
|
"Attempting to initialize audio stream with invalid metadata");
|
|
|
|
|
|
|
|
PRInt64 length = mWaveLength;
|
|
|
|
// If the decoder has a valid content length, and it's shorter than the
|
|
|
|
// expected length of the PCM data, calculate the playback duration from
|
|
|
|
// the content length rather than the expected PCM data length.
|
2009-03-31 17:52:56 -07:00
|
|
|
PRInt64 streamLength = mStream->GetLength();
|
|
|
|
if (streamLength >= 0) {
|
|
|
|
PRInt64 dataLength = PR_MAX(0, streamLength - mWavePCMOffset);
|
|
|
|
length = PR_MIN(dataLength, length);
|
2009-02-16 20:56:16 -08:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveStateMachine::FirePositionChanged(PRBool aCoalesce)
|
|
|
|
{
|
|
|
|
if (aCoalesce && mPositionChangeQueued) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPositionChangeQueued = PR_TRUE;
|
|
|
|
nsCOMPtr<nsIRunnable> event = NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, mDecoder, PlaybackPositionChanged);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsWaveDecoder, nsIObserver)
|
|
|
|
|
|
|
|
nsWaveDecoder::nsWaveDecoder()
|
2009-02-05 00:02:21 -08:00
|
|
|
: mInitialVolume(1.0),
|
2009-02-16 20:56:16 -08:00
|
|
|
mCurrentTime(0.0),
|
2008-11-06 12:53:20 -08:00
|
|
|
mEndedDuration(std::numeric_limits<float>::quiet_NaN()),
|
2008-12-14 10:02:54 -08:00
|
|
|
mEnded(PR_FALSE),
|
2009-01-15 12:26:51 -08:00
|
|
|
mSeekable(PR_TRUE),
|
2009-02-05 00:02:21 -08:00
|
|
|
mResourceLoaded(PR_FALSE),
|
|
|
|
mMetadataLoadedReported(PR_FALSE),
|
|
|
|
mResourceLoadedReported(PR_FALSE)
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsWaveDecoder);
|
2009-09-29 14:32:44 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gWaveDecoderLog) {
|
|
|
|
gWaveDecoderLog = PR_NewLogModule("nsWaveDecoder");
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsWaveDecoder::~nsWaveDecoder()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsWaveDecoder);
|
|
|
|
}
|
|
|
|
|
2009-09-10 18:44:21 -07:00
|
|
|
PRBool
|
|
|
|
nsWaveDecoder::Init(nsHTMLMediaElement* aElement)
|
|
|
|
{
|
|
|
|
nsMediaDecoder::Init(aElement);
|
|
|
|
|
2010-02-24 11:14:14 -08:00
|
|
|
nsContentUtils::RegisterShutdownObserver(this);
|
2009-09-10 18:44:21 -07:00
|
|
|
|
|
|
|
mPlaybackStateMachine = new nsWaveStateMachine(this,
|
|
|
|
TimeDuration::FromMilliseconds(BUFFERING_TIMEOUT),
|
|
|
|
mInitialVolume);
|
|
|
|
NS_ENSURE_TRUE(mPlaybackStateMachine, PR_FALSE);
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-14 19:28:08 -07:00
|
|
|
nsMediaStream*
|
|
|
|
nsWaveDecoder::GetCurrentStream()
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2009-09-14 19:28:08 -07:00
|
|
|
return mStream;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
already_AddRefed<nsIPrincipal>
|
2008-11-06 12:53:20 -08:00
|
|
|
nsWaveDecoder::GetCurrentPrincipal()
|
|
|
|
{
|
|
|
|
if (!mStream) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return mStream->GetCurrentPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
nsWaveDecoder::GetCurrentTime()
|
|
|
|
{
|
2009-02-16 20:56:16 -08:00
|
|
|
return mCurrentTime;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWaveDecoder::Seek(float aTime)
|
|
|
|
{
|
2008-12-02 17:38:15 -08:00
|
|
|
if (mPlaybackStateMachine) {
|
2009-03-31 17:52:56 -07:00
|
|
|
mPlaybackStateMachine->Seek(aTime);
|
2008-11-06 12:53:20 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-12-14 12:46:28 -08:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWaveDecoder::PlaybackRateChanged()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
nsWaveDecoder::GetDuration()
|
|
|
|
{
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
return mPlaybackStateMachine->GetDuration();
|
|
|
|
}
|
|
|
|
return mEndedDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::Pause()
|
|
|
|
{
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
mPlaybackStateMachine->Pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::SetVolume(float aVolume)
|
|
|
|
{
|
|
|
|
mInitialVolume = aVolume;
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
mPlaybackStateMachine->SetVolume(aVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWaveDecoder::Play()
|
|
|
|
{
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
mPlaybackStateMachine->Play();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::Stop()
|
|
|
|
{
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
mPlaybackStateMachine->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStream) {
|
2009-03-31 17:52:56 -07:00
|
|
|
mStream->Close();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPlaybackThread) {
|
2008-12-14 10:02:54 -08:00
|
|
|
mPlaybackThread->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPlaybackStateMachine) {
|
2008-11-06 12:53:20 -08:00
|
|
|
mEndedDuration = mPlaybackStateMachine->GetDuration();
|
2008-12-14 10:02:54 -08:00
|
|
|
mEnded = mPlaybackStateMachine->IsEnded();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mPlaybackThread = nsnull;
|
|
|
|
mPlaybackStateMachine = nsnull;
|
|
|
|
mStream = nsnull;
|
|
|
|
|
2010-02-24 11:14:14 -08:00
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-09-14 19:30:43 -07:00
|
|
|
nsWaveDecoder::Load(nsMediaStream* aStream, nsIStreamListener** aStreamListener)
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2009-09-14 19:30:43 -07:00
|
|
|
NS_ASSERTION(aStream, "A stream should be provided");
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-09-14 19:30:44 -07:00
|
|
|
if (aStreamListener) {
|
|
|
|
*aStreamListener = nsnull;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-09-14 19:30:43 -07:00
|
|
|
mStream = aStream;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-09-14 19:30:43 -07:00
|
|
|
nsresult rv = mStream->Open(aStreamListener);
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-09-14 19:30:43 -07:00
|
|
|
|
2009-03-29 18:19:10 -07:00
|
|
|
mPlaybackStateMachine->SetStream(mStream);
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
rv = NS_NewThread(getter_AddRefs(mPlaybackThread));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mPlaybackThread->Dispatch(mPlaybackStateMachine, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::MetadataLoaded()
|
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement) {
|
|
|
|
mElement->MetadataLoaded();
|
2009-05-18 16:06:10 -07:00
|
|
|
mElement->FirstFrameLoaded(mResourceLoaded);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
2009-01-15 12:26:51 -08:00
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
mMetadataLoadedReported = PR_TRUE;
|
|
|
|
|
|
|
|
if (mResourceLoaded) {
|
|
|
|
ResourceLoaded();
|
|
|
|
} else {
|
2009-01-15 12:26:51 -08:00
|
|
|
StartProgress();
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::PlaybackEnded()
|
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-23 18:34:36 -08:00
|
|
|
if (!mPlaybackStateMachine->IsEnded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-14 19:30:45 -07:00
|
|
|
// Update ready state; now that we've finished playback, we should
|
|
|
|
// switch to HAVE_CURRENT_DATA.
|
|
|
|
UpdateReadyStateForData();
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement) {
|
|
|
|
mElement->PlaybackEnded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::ResourceLoaded()
|
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2009-01-15 12:26:51 -08:00
|
|
|
|
|
|
|
mResourceLoaded = PR_TRUE;
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
if (!mMetadataLoadedReported || mResourceLoadedReported)
|
|
|
|
return;
|
2009-01-15 12:26:51 -08:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
StopProgress();
|
2009-01-15 12:26:51 -08:00
|
|
|
|
|
|
|
if (mElement) {
|
2009-02-05 00:02:21 -08:00
|
|
|
// Ensure the final progress event gets fired
|
2009-01-15 12:26:51 -08:00
|
|
|
mElement->DispatchAsyncProgressEvent(NS_LITERAL_STRING("progress"));
|
2009-02-05 00:02:21 -08:00
|
|
|
mElement->ResourceLoaded();
|
2009-01-15 12:26:51 -08:00
|
|
|
}
|
2009-02-05 00:02:21 -08:00
|
|
|
|
|
|
|
mResourceLoadedReported = PR_TRUE;
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::NetworkError()
|
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement) {
|
|
|
|
mElement->NetworkError();
|
|
|
|
}
|
2009-05-18 16:08:08 -07:00
|
|
|
Shutdown();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsWaveDecoder::IsSeeking() const
|
|
|
|
{
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
return mPlaybackStateMachine->IsSeeking();
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-12-14 10:02:54 -08:00
|
|
|
PRBool
|
|
|
|
nsWaveDecoder::IsEnded() const
|
|
|
|
{
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
return mPlaybackStateMachine->IsEnded();
|
|
|
|
}
|
|
|
|
return mEnded;
|
|
|
|
}
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
nsMediaDecoder::Statistics
|
|
|
|
nsWaveDecoder::GetStatistics()
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2009-02-05 00:02:21 -08:00
|
|
|
if (!mPlaybackStateMachine)
|
|
|
|
return Statistics();
|
|
|
|
return mPlaybackStateMachine->GetStatistics();
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
void
|
2009-03-31 17:52:56 -07:00
|
|
|
nsWaveDecoder::NotifySuspendedStatusChanged()
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
if (mStream->IsSuspendedByCache() && mElement) {
|
|
|
|
// if this is an autoplay element, we need to kick off its autoplaying
|
|
|
|
// now so we consume data and hopefully free up cache space
|
|
|
|
mElement->NotifyAutoplayDataReady();
|
2009-04-05 17:20:51 -07:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-31 17:52:56 -07:00
|
|
|
nsWaveDecoder::NotifyBytesDownloaded()
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2009-03-31 17:52:56 -07:00
|
|
|
UpdateReadyStateForData();
|
2009-09-14 19:30:45 -07:00
|
|
|
Progress(PR_FALSE);
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::NotifyDownloadEnded(nsresult aStatus)
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2009-04-05 17:20:51 -07:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
ResourceLoaded();
|
|
|
|
} else if (aStatus != NS_BASE_STREAM_CLOSED &&
|
|
|
|
aStatus != NS_BINDING_ABORTED) {
|
|
|
|
NetworkError();
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
UpdateReadyStateForData();
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
void
|
|
|
|
nsWaveDecoder::Shutdown()
|
|
|
|
{
|
2009-05-18 16:08:08 -07:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2008-12-08 16:43:56 -08:00
|
|
|
mShuttingDown = PR_TRUE;
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
nsMediaDecoder::Shutdown();
|
|
|
|
|
2009-05-18 16:08:08 -07:00
|
|
|
// An event that gets posted to the main thread, when the media element is
|
|
|
|
// being destroyed, to destroy the decoder. Since the decoder shutdown can
|
|
|
|
// block and post events this cannot be done inside destructor calls. So
|
|
|
|
// this event is posted asynchronously to the main thread to perform the
|
|
|
|
// shutdown.
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NEW_RUNNABLE_METHOD(nsWaveDecoder, this, Stop);
|
2008-11-06 12:53:20 -08:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWaveDecoder::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-05 00:02:21 -08:00
|
|
|
nsWaveDecoder::UpdateReadyStateForData()
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2009-02-05 00:02:21 -08:00
|
|
|
if (!mElement || mShuttingDown || !mPlaybackStateMachine)
|
2008-12-08 16:43:56 -08:00
|
|
|
return;
|
|
|
|
|
2009-02-10 17:43:45 -08:00
|
|
|
nsHTMLMediaElement::NextFrameStatus frameStatus =
|
|
|
|
mPlaybackStateMachine->GetNextFrameStatus();
|
|
|
|
if (frameStatus == nsHTMLMediaElement::NEXT_FRAME_AVAILABLE &&
|
|
|
|
!mMetadataLoadedReported) {
|
|
|
|
frameStatus = nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
mElement->UpdateReadyStateForData(frameStatus);
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::SeekingStarted()
|
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement) {
|
2009-05-18 15:44:17 -07:00
|
|
|
UpdateReadyStateForData();
|
2008-11-06 12:53:20 -08:00
|
|
|
mElement->SeekStarted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::SeekingStopped()
|
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement) {
|
2009-02-05 00:02:21 -08:00
|
|
|
UpdateReadyStateForData();
|
2009-05-18 15:44:17 -07:00
|
|
|
mElement->SeekCompleted();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-09-21 17:08:13 -07:00
|
|
|
nsWaveDecoder::DecodeError()
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement) {
|
2009-09-21 17:08:13 -07:00
|
|
|
mElement->DecodeError();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
2009-09-21 17:08:13 -07:00
|
|
|
Shutdown();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-16 20:56:16 -08:00
|
|
|
void
|
|
|
|
nsWaveDecoder::PlaybackPositionChanged()
|
|
|
|
{
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float lastTime = mCurrentTime;
|
|
|
|
|
|
|
|
if (mPlaybackStateMachine) {
|
|
|
|
mCurrentTime = mPlaybackStateMachine->GetTimeForPositionChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mElement && lastTime != mCurrentTime) {
|
2009-05-18 15:44:17 -07:00
|
|
|
UpdateReadyStateForData();
|
2009-02-16 20:56:16 -08:00
|
|
|
mElement->DispatchSimpleEvent(NS_LITERAL_STRING("timeupdate"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-07 02:10:34 -08:00
|
|
|
void
|
|
|
|
nsWaveDecoder::SetDuration(PRInt64 /* aDuration */)
|
|
|
|
{
|
|
|
|
// Ignored by the wave decoder since we can compute the
|
|
|
|
// duration directly from the wave data itself.
|
|
|
|
}
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
void
|
|
|
|
nsWaveDecoder::SetSeekable(PRBool aSeekable)
|
|
|
|
{
|
|
|
|
mSeekable = aSeekable;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsWaveDecoder::GetSeekable()
|
|
|
|
{
|
|
|
|
return mSeekable;
|
|
|
|
}
|
2009-01-21 15:54:40 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::Suspend()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
2009-05-17 19:02:20 -07:00
|
|
|
mStream->Suspend(PR_TRUE);
|
2009-01-21 15:54:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::Resume()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
mStream->Resume();
|
|
|
|
}
|
|
|
|
}
|
2009-04-09 18:28:24 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsWaveDecoder::MoveLoadsToBackground()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
mStream->MoveLoadsToBackground();
|
|
|
|
}
|
|
|
|
}
|