2010-04-01 20:03:07 -07:00
|
|
|
/* 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>
|
|
|
|
* Chris Pearce <chris@pearce.org.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 ***** */
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include "nsAudioStream.h"
|
|
|
|
#include "nsTArray.h"
|
2010-04-27 01:53:44 -07:00
|
|
|
#include "nsBuiltinDecoder.h"
|
2010-05-05 19:31:02 -07:00
|
|
|
#include "nsBuiltinDecoderReader.h"
|
|
|
|
#include "nsBuiltinDecoderStateMachine.h"
|
2010-04-01 20:03:07 -07:00
|
|
|
#include "mozilla/mozalloc.h"
|
2010-04-27 01:53:44 -07:00
|
|
|
#include "VideoUtils.h"
|
2011-03-23 15:28:57 -07:00
|
|
|
#include "nsTimeRanges.h"
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
using namespace mozilla;
|
2010-04-01 20:03:07 -07:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2010-04-27 01:53:44 -07:00
|
|
|
extern PRLogModuleInfo* gBuiltinDecoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gBuiltinDecoderLog, type, msg)
|
2010-04-01 20:03:07 -07:00
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Wait this number of seconds when buffering, then leave and play
|
|
|
|
// as best as we can if the required amount of data hasn't been
|
|
|
|
// retrieved.
|
|
|
|
#define BUFFERING_WAIT 30
|
|
|
|
|
|
|
|
// The amount of data to retrieve during buffering is computed based
|
|
|
|
// on the download rate. BUFFERING_MIN_RATE is the minimum download
|
|
|
|
// rate to be used in that calculation to help avoid constant buffering
|
|
|
|
// attempts at a time when the average download rate has not stabilised.
|
|
|
|
#define BUFFERING_MIN_RATE 50000
|
|
|
|
#define BUFFERING_RATE(x) ((x)< BUFFERING_MIN_RATE ? BUFFERING_MIN_RATE : (x))
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
// If audio queue has less than this many ms of decoded audio, we won't risk
|
|
|
|
// trying to decode the video, we'll skip decoding video up to the next
|
2010-11-28 12:06:38 -08:00
|
|
|
// keyframe. We may increase this value for an individual decoder if we
|
|
|
|
// encounter video frames which take a long time to decode.
|
2010-06-08 16:31:27 -07:00
|
|
|
static const PRUint32 LOW_AUDIO_MS = 300;
|
2010-04-27 01:53:44 -07:00
|
|
|
|
2010-05-12 17:59:42 -07:00
|
|
|
// If more than this many ms of decoded audio is queued, we'll hold off
|
2010-11-28 12:06:38 -08:00
|
|
|
// decoding more audio. If we increase the low audio threshold (see
|
|
|
|
// LOW_AUDIO_MS above) we'll also increase this value to ensure it's not
|
|
|
|
// less than the low audio threshold.
|
2011-03-23 15:28:58 -07:00
|
|
|
const PRInt64 AMPLE_AUDIO_MS = 1000;
|
2010-05-12 17:59:42 -07:00
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
// Maximum number of bytes we'll allocate and write at once to the audio
|
|
|
|
// hardware when the audio stream contains missing samples and we're
|
|
|
|
// writing silence in order to fill the gap. We limit our silence-writes
|
|
|
|
// to 32KB in order to avoid allocating an impossibly large chunk of
|
|
|
|
// memory if we encounter a large chunk of silence.
|
|
|
|
const PRUint32 SILENCE_BYTES_CHUNK = 32 * 1024;
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
// If we have fewer than LOW_VIDEO_FRAMES decoded frames, and
|
|
|
|
// we're not "pumping video", we'll skip the video up to the next keyframe
|
|
|
|
// which is at or after the current playback position.
|
|
|
|
static const PRUint32 LOW_VIDEO_FRAMES = 1;
|
|
|
|
|
2010-09-14 16:24:47 -07:00
|
|
|
// If we've got more than AMPLE_VIDEO_FRAMES decoded video frames waiting in
|
|
|
|
// the video queue, we will not decode any more video frames until some have
|
|
|
|
// been consumed by the play state machine thread.
|
|
|
|
static const PRUint32 AMPLE_VIDEO_FRAMES = 10;
|
|
|
|
|
2010-05-30 21:02:00 -07:00
|
|
|
// Arbitrary "frame duration" when playing only audio.
|
2011-03-01 17:40:44 -08:00
|
|
|
static const int AUDIO_DURATION_MS = 40;
|
2010-05-30 21:02:00 -07:00
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
// If we increase our "low audio threshold" (see LOW_AUDIO_MS above), we
|
|
|
|
// use this as a factor in all our calculations. Increasing this will cause
|
|
|
|
// us to be more likely to increase our low audio threshold, and to
|
|
|
|
// increase it by more.
|
|
|
|
static const int THRESHOLD_FACTOR = 2;
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// If we have less than this much undecoded data available, we'll consider
|
|
|
|
// ourselves to be running low on undecoded data. We determine how much
|
|
|
|
// undecoded data we have remaining using the reader's GetBuffered()
|
|
|
|
// implementation.
|
|
|
|
static const PRInt64 LOW_DATA_THRESHOLD_MS = 5000;
|
2010-11-28 12:06:38 -08:00
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// LOW_DATA_THRESHOLD_MS needs to be greater than AMPLE_AUDIO_MS, otherwise
|
|
|
|
// the skip-to-keyframe logic can activate when we're running low on data.
|
|
|
|
PR_STATIC_ASSERT(LOW_DATA_THRESHOLD_MS > AMPLE_AUDIO_MS);
|
2010-11-28 12:06:38 -08:00
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// Amount of excess ms of data to add in to the "should we buffer" calculation.
|
|
|
|
static const PRUint32 EXHAUSTED_DATA_MARGIN_MS = 60;
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// If we enter buffering within QUICK_BUFFER_THRESHOLD_MS seconds of starting
|
|
|
|
// decoding, we'll enter "quick buffering" mode, which exits a lot sooner than
|
|
|
|
// normal buffering mode. This exists so that if the decode-ahead exhausts the
|
|
|
|
// downloaded data while decode/playback is just starting up (for example
|
|
|
|
// after a seek while the media is still playing, or when playing a media
|
|
|
|
// as soon as it's load started), we won't necessarily stop for 30s and wait
|
|
|
|
// for buffering. We may actually be able to playback in this case, so exit
|
|
|
|
// buffering early and try to play. If it turns out we can't play, we'll fall
|
|
|
|
// back to buffering normally.
|
|
|
|
static const PRUint32 QUICK_BUFFER_THRESHOLD_MS = 2000;
|
|
|
|
|
|
|
|
// If we're quick buffering, we'll remain in buffering mode while we have less than
|
|
|
|
// QUICK_BUFFERING_LOW_DATA_MS of decoded data available.
|
|
|
|
static const PRUint32 QUICK_BUFFERING_LOW_DATA_MS = 1000;
|
|
|
|
|
|
|
|
// If QUICK_BUFFERING_LOW_DATA_MS is > AMPLE_AUDIO_MS, we won't exit
|
|
|
|
// quick buffering in a timely fashion, as the decode pauses when it
|
|
|
|
// reaches AMPLE_AUDIO_MS decoded data, and thus we'll never reach
|
|
|
|
// QUICK_BUFFERING_LOW_DATA_MS.
|
|
|
|
PR_STATIC_ASSERT(QUICK_BUFFERING_LOW_DATA_MS <= AMPLE_AUDIO_MS);
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
static TimeDuration MsToDuration(PRInt64 aMs) {
|
|
|
|
return TimeDuration::FromMilliseconds(static_cast<double>(aMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRInt64 DurationToMs(TimeDuration aDuration) {
|
|
|
|
return static_cast<PRInt64>(aDuration.ToSeconds() * 1000);
|
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
class nsAudioMetadataEventRunner : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsBuiltinDecoder> mDecoder;
|
|
|
|
public:
|
|
|
|
nsAudioMetadataEventRunner(nsBuiltinDecoder* aDecoder, PRUint32 aChannels,
|
|
|
|
PRUint32 aRate, PRUint32 aFrameBufferLength) :
|
|
|
|
mDecoder(aDecoder),
|
|
|
|
mChannels(aChannels),
|
|
|
|
mRate(aRate),
|
|
|
|
mFrameBufferLength(aFrameBufferLength)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mDecoder->MetadataLoaded(mChannels, mRate, mFrameBufferLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PRUint32 mChannels;
|
|
|
|
const PRUint32 mRate;
|
|
|
|
const PRUint32 mFrameBufferLength;
|
|
|
|
};
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
nsBuiltinDecoderStateMachine::nsBuiltinDecoderStateMachine(nsBuiltinDecoder* aDecoder,
|
|
|
|
nsBuiltinDecoderReader* aReader) :
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder(aDecoder),
|
|
|
|
mState(DECODER_STATE_DECODING_METADATA),
|
|
|
|
mAudioMonitor("media.audiostream"),
|
|
|
|
mCbCrSize(0),
|
|
|
|
mPlayDuration(0),
|
|
|
|
mStartTime(-1),
|
|
|
|
mEndTime(-1),
|
|
|
|
mSeekTime(0),
|
2010-05-05 19:31:02 -07:00
|
|
|
mReader(aReader),
|
2010-04-01 20:03:07 -07:00
|
|
|
mCurrentFrameTime(0),
|
|
|
|
mAudioStartTime(-1),
|
|
|
|
mAudioEndTime(-1),
|
2010-05-30 21:02:00 -07:00
|
|
|
mVideoFrameEndTime(-1),
|
2010-04-01 20:03:07 -07:00
|
|
|
mVolume(1.0),
|
|
|
|
mSeekable(PR_TRUE),
|
|
|
|
mPositionChangeQueued(PR_FALSE),
|
|
|
|
mAudioCompleted(PR_FALSE),
|
2010-10-06 15:58:36 -07:00
|
|
|
mGotDurationFromMetaData(PR_FALSE),
|
2010-08-25 06:10:00 -07:00
|
|
|
mStopDecodeThreads(PR_TRUE),
|
2011-03-23 15:28:57 -07:00
|
|
|
mQuickBuffering(PR_FALSE),
|
2010-08-25 06:10:00 -07:00
|
|
|
mEventManager(aDecoder)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-05-05 19:31:02 -07:00
|
|
|
MOZ_COUNT_CTOR(nsBuiltinDecoderStateMachine);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
nsBuiltinDecoderStateMachine::~nsBuiltinDecoderStateMachine()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-05-05 19:31:02 -07:00
|
|
|
MOZ_COUNT_DTOR(nsBuiltinDecoderStateMachine);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-12 17:59:42 -07:00
|
|
|
PRBool nsBuiltinDecoderStateMachine::HasFutureAudio() const {
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
2010-09-14 16:24:47 -07:00
|
|
|
NS_ASSERTION(HasAudio(), "Should only call HasFutureAudio() when we have audio");
|
|
|
|
// We've got audio ready to play if:
|
|
|
|
// 1. We've not completed playback of audio, and
|
|
|
|
// 2. we either have more than the threshold of decoded audio available, or
|
|
|
|
// we've completely decoded all audio (but not finished playing it yet
|
|
|
|
// as per 1).
|
|
|
|
return !mAudioCompleted &&
|
|
|
|
(AudioDecodedMs() > LOW_AUDIO_MS || mReader->mAudioQueue.IsFinished());
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsBuiltinDecoderStateMachine::HaveNextFrameData() const {
|
2010-09-14 16:24:47 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
return (!HasAudio() || HasFutureAudio()) &&
|
|
|
|
(!HasVideo() || mReader->mVideoQueue.GetSize() > 0);
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
PRInt64 nsBuiltinDecoderStateMachine::GetDecodedAudioDuration() {
|
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
PRInt64 audioDecoded = mReader->mAudioQueue.Duration();
|
|
|
|
if (mAudioEndTime != -1) {
|
|
|
|
audioDecoded += mAudioEndTime - GetMediaTime();
|
|
|
|
}
|
|
|
|
return audioDecoded;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::DecodeLoop()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
|
|
|
// We want to "pump" the decode until we've got a few frames/samples decoded
|
|
|
|
// before we consider whether decode is falling behind.
|
|
|
|
PRBool audioPump = PR_TRUE;
|
|
|
|
PRBool videoPump = PR_TRUE;
|
|
|
|
|
|
|
|
// If the video decode is falling behind the audio, we'll start dropping the
|
|
|
|
// inter-frames up until the next keyframe which is at or before the current
|
|
|
|
// playback position. skipToNextKeyframe is PR_TRUE if we're currently
|
|
|
|
// skipping up to the next keyframe.
|
|
|
|
PRBool skipToNextKeyframe = PR_FALSE;
|
|
|
|
|
|
|
|
// Once we've decoded more than videoPumpThreshold video frames, we'll
|
|
|
|
// no longer be considered to be "pumping video".
|
2010-09-14 16:24:47 -07:00
|
|
|
const unsigned videoPumpThreshold = AMPLE_VIDEO_FRAMES / 2;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// After the audio decode fills with more than audioPumpThresholdMs ms
|
|
|
|
// of decoded audio, we'll start to check whether the audio or video decode
|
|
|
|
// is falling behind.
|
2010-06-08 16:31:27 -07:00
|
|
|
const unsigned audioPumpThresholdMs = LOW_AUDIO_MS * 2;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
// Our local low audio threshold. We may increase this if we're slow to
|
|
|
|
// decode video frames, in order to reduce the chance of audio underruns.
|
|
|
|
PRInt64 lowAudioThreshold = LOW_AUDIO_MS;
|
|
|
|
|
|
|
|
// Our local ample audio threshold. If we increase lowAudioThreshold, we'll
|
2011-01-12 17:06:15 -08:00
|
|
|
// also increase this too appropriately (we don't want lowAudioThreshold to
|
2010-11-28 12:06:38 -08:00
|
|
|
// be greater than ampleAudioThreshold, else we'd stop decoding!).
|
|
|
|
PRInt64 ampleAudioThreshold = AMPLE_AUDIO_MS;
|
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
MediaQueue<VideoData>& videoQueue = mReader->mVideoQueue;
|
|
|
|
MediaQueue<SoundData>& audioQueue = mReader->mAudioQueue;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
|
|
|
|
PRBool videoPlaying = HasVideo();
|
|
|
|
PRBool audioPlaying = HasAudio();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
// Main decode loop.
|
|
|
|
while (mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
!mStopDecodeThreads &&
|
|
|
|
(videoPlaying || audioPlaying))
|
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
// We don't want to consider skipping to the next keyframe if we've
|
|
|
|
// only just started up the decode loop, so wait until we've decoded
|
2011-01-12 17:06:15 -08:00
|
|
|
// some frames before enabling the keyframe skip logic on video.
|
2011-02-01 17:35:47 -08:00
|
|
|
if (videoPump &&
|
|
|
|
static_cast<PRUint32>(videoQueue.GetSize()) >= videoPumpThreshold)
|
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
videoPump = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
// We don't want to consider skipping to the next keyframe if we've
|
|
|
|
// only just started up the decode loop, so wait until we've decoded
|
|
|
|
// some audio data before enabling the keyframe skip logic on audio.
|
|
|
|
if (audioPump && GetDecodedAudioDuration() >= audioPumpThresholdMs) {
|
2010-04-01 20:03:07 -07:00
|
|
|
audioPump = PR_FALSE;
|
|
|
|
}
|
2011-01-12 17:06:15 -08:00
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
// We'll skip the video decode to the nearest keyframe if we're low on
|
|
|
|
// audio, or if we're low on video, provided we're not running low on
|
|
|
|
// data to decode. If we're running low on downloaded data to decode,
|
|
|
|
// we won't start keyframe skipping, as we'll be pausing playback to buffer
|
|
|
|
// soon anyway and we'll want to be able to display frames immediately
|
|
|
|
// after buffering finishes.
|
2011-03-23 15:28:57 -07:00
|
|
|
if (mState == DECODER_STATE_DECODING &&
|
|
|
|
!skipToNextKeyframe &&
|
2010-11-28 12:06:38 -08:00
|
|
|
videoPlaying &&
|
2011-01-12 17:06:15 -08:00
|
|
|
((!audioPump && audioPlaying && GetDecodedAudioDuration() < lowAudioThreshold) ||
|
2011-02-01 17:35:47 -08:00
|
|
|
(!videoPump &&
|
|
|
|
videoPlaying &&
|
2011-03-23 15:28:57 -07:00
|
|
|
static_cast<PRUint32>(videoQueue.GetSize()) < LOW_VIDEO_FRAMES)) &&
|
|
|
|
!HasLowUndecodedData())
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
skipToNextKeyframe = PR_TRUE;
|
2010-11-28 12:06:38 -08:00
|
|
|
LOG(PR_LOG_DEBUG, ("Skipping video decode to the next keyframe"));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
// Video decode.
|
2011-02-01 17:35:47 -08:00
|
|
|
if (videoPlaying &&
|
|
|
|
static_cast<PRUint32>(videoQueue.GetSize()) < AMPLE_VIDEO_FRAMES)
|
|
|
|
{
|
2010-11-28 12:06:38 -08:00
|
|
|
// Time the video decode, so that if it's slow, we can increase our low
|
|
|
|
// audio threshold to reduce the chance of an audio underrun while we're
|
|
|
|
// waiting for a video decode to complete.
|
2011-01-12 17:06:15 -08:00
|
|
|
TimeDuration decodeTime;
|
|
|
|
{
|
|
|
|
PRInt64 currentTime = GetMediaTime();
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
TimeStamp start = TimeStamp::Now();
|
|
|
|
videoPlaying = mReader->DecodeVideoFrame(skipToNextKeyframe, currentTime);
|
|
|
|
decodeTime = TimeStamp::Now() - start;
|
|
|
|
}
|
2011-03-23 15:28:58 -07:00
|
|
|
if (THRESHOLD_FACTOR * DurationToMs(decodeTime) > lowAudioThreshold &&
|
2011-03-23 15:28:57 -07:00
|
|
|
!HasLowUndecodedData())
|
2010-11-28 12:06:38 -08:00
|
|
|
{
|
|
|
|
lowAudioThreshold =
|
2011-03-23 15:28:58 -07:00
|
|
|
NS_MIN(THRESHOLD_FACTOR * DurationToMs(decodeTime), AMPLE_AUDIO_MS);
|
2010-11-28 12:06:38 -08:00
|
|
|
ampleAudioThreshold = NS_MAX(THRESHOLD_FACTOR * lowAudioThreshold,
|
|
|
|
ampleAudioThreshold);
|
|
|
|
LOG(PR_LOG_DEBUG,
|
|
|
|
("Slow video decode, set lowAudioThreshold=%lld ampleAudioThreshold=%lld",
|
|
|
|
lowAudioThreshold, ampleAudioThreshold));
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-11-28 12:06:38 -08:00
|
|
|
// Audio decode.
|
2011-01-12 17:06:15 -08:00
|
|
|
if (audioPlaying &&
|
|
|
|
(GetDecodedAudioDuration() < ampleAudioThreshold || audioQueue.GetSize() == 0))
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
2010-05-05 19:31:02 -07:00
|
|
|
audioPlaying = mReader->DecodeAudioData();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-01-12 17:06:15 -08:00
|
|
|
|
|
|
|
// Notify to ensure that the AudioLoop() is not waiting, in case it was
|
|
|
|
// waiting for more audio to be decoded.
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
|
|
|
|
if (!IsPlaying()) {
|
|
|
|
// Update the ready state, so that the play DOM events fire. We only
|
|
|
|
// need to do this if we're not playing; if we're playing the playback
|
|
|
|
// code will do an update whenever it advances a frame.
|
|
|
|
UpdateReadyState();
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
if (mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
!mStopDecodeThreads &&
|
|
|
|
(!audioPlaying || (GetDecodedAudioDuration() >= ampleAudioThreshold &&
|
|
|
|
audioQueue.GetSize() > 0))
|
|
|
|
&&
|
2011-02-01 17:35:47 -08:00
|
|
|
(!videoPlaying ||
|
|
|
|
static_cast<PRUint32>(videoQueue.GetSize()) >= AMPLE_VIDEO_FRAMES))
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-01-12 17:06:15 -08:00
|
|
|
// All active bitstreams' decode is well ahead of the playback
|
|
|
|
// position, we may as well wait for the playback to catch up. Note the
|
|
|
|
// audio push thread acquires and notifies the decoder monitor every time
|
|
|
|
// it pops SoundData off the audio queue. So if the audio push thread pops
|
|
|
|
// the last SoundData off the audio queue right after that queue reported
|
|
|
|
// it was non-empty here, we'll receive a notification on the decoder
|
|
|
|
// monitor which will wake us up shortly after we sleep, thus preventing
|
|
|
|
// both the decode and audio push threads waiting at the same time.
|
|
|
|
// See bug 620326.
|
|
|
|
mon.Wait();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
} // End decode loop.
|
|
|
|
|
|
|
|
if (!mStopDecodeThreads &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
mState != DECODER_STATE_SEEKING)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-01-12 17:06:15 -08:00
|
|
|
mState = DECODER_STATE_COMPLETED;
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-01-12 17:06:15 -08:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Shutting down DecodeLoop this=%p", this));
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
PRBool nsBuiltinDecoderStateMachine::IsPlaying()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
return !mPlayStartTime.IsNull();
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::AudioLoop()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(OnAudioThread(), "Should be on audio thread.");
|
|
|
|
LOG(PR_LOG_DEBUG, ("Begun audio thread/loop"));
|
2010-12-19 11:05:40 -08:00
|
|
|
PRInt64 audioDuration = 0;
|
2010-08-12 19:28:15 -07:00
|
|
|
PRInt64 audioStartTime = -1;
|
|
|
|
PRUint32 channels, rate;
|
2011-01-16 19:03:00 -08:00
|
|
|
double volume = -1;
|
2010-09-05 19:14:50 -07:00
|
|
|
PRBool setVolume;
|
2010-12-19 11:05:40 -08:00
|
|
|
PRInt32 minWriteSamples = -1;
|
|
|
|
PRInt64 samplesAtLastSleep = 0;
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
mAudioCompleted = PR_FALSE;
|
2010-08-12 19:28:15 -07:00
|
|
|
audioStartTime = mAudioStartTime;
|
|
|
|
channels = mReader->GetInfo().mAudioChannels;
|
|
|
|
rate = mReader->GetInfo().mAudioRate;
|
|
|
|
NS_ASSERTION(audioStartTime != -1, "Should have audio start time by now");
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
|
2010-12-19 11:05:40 -08:00
|
|
|
// Wait while we're not playing, and we're not shutting down, or we're
|
2010-04-01 20:03:07 -07:00
|
|
|
// playing and we've got no audio to play.
|
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
NS_ASSERTION(mState != DECODER_STATE_DECODING_METADATA,
|
|
|
|
"Should have meta data before audio started playing.");
|
|
|
|
while (mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
!mStopDecodeThreads &&
|
|
|
|
(!IsPlaying() ||
|
|
|
|
mState == DECODER_STATE_BUFFERING ||
|
2010-04-01 22:19:35 -07:00
|
|
|
(mReader->mAudioQueue.GetSize() == 0 &&
|
2010-04-01 20:03:07 -07:00
|
|
|
!mReader->mAudioQueue.AtEndOfStream())))
|
|
|
|
{
|
2010-12-19 11:05:40 -08:00
|
|
|
samplesAtLastSleep = audioDuration;
|
2010-04-01 20:03:07 -07:00
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're shutting down, break out and exit the audio thread.
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN ||
|
|
|
|
mStopDecodeThreads ||
|
|
|
|
mReader->mAudioQueue.AtEndOfStream())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-09-05 19:14:50 -07:00
|
|
|
|
|
|
|
// We only want to go to the expense of taking the audio monitor and
|
|
|
|
// changing the volume if it's the first time we've entered the loop
|
|
|
|
// (as we must sync the volume in case it's changed since the
|
|
|
|
// nsAudioStream was created) or if the volume has changed.
|
|
|
|
setVolume = volume != mVolume;
|
|
|
|
volume = mVolume;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-12-19 11:05:40 -08:00
|
|
|
if (setVolume || minWriteSamples == -1) {
|
2010-09-05 19:14:50 -07:00
|
|
|
MonitorAutoEnter audioMon(mAudioMonitor);
|
|
|
|
if (mAudioStream) {
|
2010-12-19 11:05:40 -08:00
|
|
|
if (setVolume) {
|
|
|
|
mAudioStream->SetVolume(volume);
|
|
|
|
}
|
|
|
|
if (minWriteSamples == -1) {
|
|
|
|
minWriteSamples = mAudioStream->GetMinWriteSamples();
|
|
|
|
}
|
2010-09-05 19:14:50 -07:00
|
|
|
}
|
|
|
|
}
|
2010-04-01 22:19:35 -07:00
|
|
|
NS_ASSERTION(mReader->mAudioQueue.GetSize() > 0,
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should have data to play");
|
2010-08-12 19:28:15 -07:00
|
|
|
// See if there's missing samples in the audio stream. If there is, push
|
|
|
|
// silence into the audio hardware, so we can play across the gap.
|
|
|
|
const SoundData* s = mReader->mAudioQueue.PeekFront();
|
|
|
|
|
|
|
|
// Calculate the number of samples that have been pushed onto the audio
|
|
|
|
// hardware.
|
|
|
|
PRInt64 playedSamples = 0;
|
|
|
|
if (!MsToSamples(audioStartTime, rate, playedSamples)) {
|
|
|
|
NS_WARNING("Int overflow converting playedSamples");
|
|
|
|
break;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
if (!AddOverflow(playedSamples, audioDuration, playedSamples)) {
|
|
|
|
NS_WARNING("Int overflow adding playedSamples");
|
|
|
|
break;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
// Calculate the timestamp of the next chunk of audio in numbers of
|
|
|
|
// samples.
|
|
|
|
PRInt64 sampleTime = 0;
|
|
|
|
if (!MsToSamples(s->mTime, rate, sampleTime)) {
|
|
|
|
NS_WARNING("Int overflow converting sampleTime");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PRInt64 missingSamples = 0;
|
|
|
|
if (!AddOverflow(sampleTime, -playedSamples, missingSamples)) {
|
|
|
|
NS_WARNING("Int overflow adding missingSamples");
|
|
|
|
break;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
if (missingSamples > 0) {
|
|
|
|
// The next sound chunk begins some time after the end of the last chunk
|
|
|
|
// we pushed to the sound hardware. We must push silence into the audio
|
|
|
|
// hardware so that the next sound chunk begins playback at the correct
|
|
|
|
// time.
|
|
|
|
missingSamples = NS_MIN(static_cast<PRInt64>(PR_UINT32_MAX), missingSamples);
|
2010-08-25 06:10:00 -07:00
|
|
|
audioDuration += PlaySilence(static_cast<PRUint32>(missingSamples),
|
2010-10-06 15:58:36 -07:00
|
|
|
channels, playedSamples);
|
2010-08-12 19:28:15 -07:00
|
|
|
} else {
|
2010-08-25 06:10:00 -07:00
|
|
|
audioDuration += PlayFromAudioQueue(sampleTime, channels);
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2010-05-12 17:59:42 -07:00
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
2010-08-12 19:28:15 -07:00
|
|
|
PRInt64 playedMs;
|
|
|
|
if (!SamplesToMs(audioDuration, rate, playedMs)) {
|
|
|
|
NS_WARNING("Int overflow calculating playedMs");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!AddOverflow(audioStartTime, playedMs, mAudioEndTime)) {
|
|
|
|
NS_WARNING("Int overflow calculating audio end time");
|
|
|
|
break;
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2010-09-14 16:24:47 -07:00
|
|
|
PRInt64 audioAhead = mAudioEndTime - GetMediaTime();
|
2010-12-19 11:05:40 -08:00
|
|
|
if (audioAhead > AMPLE_AUDIO_MS &&
|
|
|
|
audioDuration - samplesAtLastSleep > minWriteSamples)
|
|
|
|
{
|
|
|
|
samplesAtLastSleep = audioDuration;
|
2010-05-12 17:59:42 -07:00
|
|
|
// We've pushed enough audio onto the hardware that we've queued up a
|
|
|
|
// significant amount ahead of the playback position. The decode
|
|
|
|
// thread will be going to sleep, so we won't get any new samples
|
|
|
|
// anyway, so sleep until we need to push to the hardware again.
|
|
|
|
Wait(AMPLE_AUDIO_MS / 2);
|
|
|
|
// Kick the decode thread; since above we only do a NotifyAll when
|
|
|
|
// we pop an audio chunk of the queue, the decoder won't wake up if
|
|
|
|
// we've got no more decoded chunks to push to the hardware. We can
|
|
|
|
// hit this condition if the last sample in the stream doesn't have
|
|
|
|
// it's EOS flag set, and the decode thread sleeps just after decoding
|
|
|
|
// that packet, but before realising there's no more packets.
|
|
|
|
mon.NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 17:59:42 -07:00
|
|
|
if (mReader->mAudioQueue.AtEndOfStream() &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
!mStopDecodeThreads)
|
|
|
|
{
|
|
|
|
// Last sample pushed to audio hardware, wait for the audio to finish,
|
|
|
|
// before the audio thread terminates.
|
|
|
|
MonitorAutoEnter audioMon(mAudioMonitor);
|
|
|
|
if (mAudioStream) {
|
2011-02-01 17:31:53 -08:00
|
|
|
PRBool seeking = PR_FALSE;
|
|
|
|
PRInt64 oldPosition = -1;
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoExit audioExit(mAudioMonitor);
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
PRInt64 position = GetMediaTime();
|
|
|
|
while (oldPosition != position &&
|
|
|
|
mAudioEndTime - position > 0 &&
|
|
|
|
mState != DECODER_STATE_SEEKING &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN)
|
|
|
|
{
|
|
|
|
const PRInt64 DRAIN_BLOCK_MS = 100;
|
|
|
|
Wait(NS_MIN(mAudioEndTime - position, DRAIN_BLOCK_MS));
|
|
|
|
oldPosition = position;
|
|
|
|
position = GetMediaTime();
|
|
|
|
}
|
|
|
|
if (mState == DECODER_STATE_SEEKING) {
|
|
|
|
seeking = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-02 15:44:40 -08:00
|
|
|
if (!seeking && mAudioStream && !mAudioStream->IsPaused()) {
|
2011-02-01 17:31:53 -08:00
|
|
|
mAudioStream->Drain();
|
|
|
|
|
|
|
|
// Fire one last event for any extra samples that didn't fill a framebuffer.
|
|
|
|
mEventManager.Drain(mAudioEndTime);
|
|
|
|
}
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Reached audio stream end.", mDecoder));
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
mAudioCompleted = PR_TRUE;
|
2010-05-12 17:59:42 -07:00
|
|
|
UpdateReadyState();
|
|
|
|
// Kick the decode and state machine threads; they may be sleeping waiting
|
|
|
|
// for this to finish.
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
LOG(PR_LOG_DEBUG, ("Audio stream finished playing, audio thread exit"));
|
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
PRUint32 nsBuiltinDecoderStateMachine::PlaySilence(PRUint32 aSamples,
|
|
|
|
PRUint32 aChannels,
|
|
|
|
PRUint64 aSampleOffset)
|
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
{
|
|
|
|
MonitorAutoEnter audioMon(mAudioMonitor);
|
|
|
|
if (mAudioStream->IsPaused()) {
|
|
|
|
// The state machine has paused since we've released the decoder
|
|
|
|
// monitor and acquired the audio monitor. Don't write any audio.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PRUint32 maxSamples = SILENCE_BYTES_CHUNK / aChannels;
|
|
|
|
PRUint32 samples = NS_MIN(aSamples, maxSamples);
|
2010-10-18 19:55:45 -07:00
|
|
|
PRUint32 numValues = samples * aChannels;
|
|
|
|
nsAutoArrayPtr<SoundDataValue> buf(new SoundDataValue[numValues]);
|
|
|
|
memset(buf.get(), 0, sizeof(SoundDataValue) * numValues);
|
|
|
|
mAudioStream->Write(buf, numValues, PR_TRUE);
|
2010-08-25 06:10:00 -07:00
|
|
|
// Dispatch events to the DOM for the audio just written.
|
2010-10-18 19:55:45 -07:00
|
|
|
mEventManager.QueueWrittenAudioData(buf.get(), numValues,
|
2010-08-25 06:10:00 -07:00
|
|
|
(aSampleOffset + samples) * aChannels);
|
2010-08-12 19:28:15 -07:00
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
PRUint32 nsBuiltinDecoderStateMachine::PlayFromAudioQueue(PRUint64 aSampleOffset,
|
|
|
|
PRUint32 aChannels)
|
2010-08-12 19:28:15 -07:00
|
|
|
{
|
|
|
|
nsAutoPtr<SoundData> sound(mReader->mAudioQueue.PopFront());
|
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
NS_WARN_IF_FALSE(IsPlaying(), "Should be playing");
|
|
|
|
// Awaken the decode loop if it's waiting for space to free up in the
|
|
|
|
// audio queue.
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
PRInt64 offset = -1;
|
|
|
|
PRUint32 samples = 0;
|
|
|
|
{
|
|
|
|
MonitorAutoEnter audioMon(mAudioMonitor);
|
|
|
|
if (!mAudioStream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// The state machine could have paused since we've released the decoder
|
|
|
|
// monitor and acquired the audio monitor. Rather than acquire both
|
|
|
|
// monitors, the audio stream also maintains whether its paused or not.
|
|
|
|
// This prevents us from doing a blocking write while holding the audio
|
2010-08-25 06:10:00 -07:00
|
|
|
// monitor while paused; we would block, and the state machine won't be
|
2010-08-12 19:28:15 -07:00
|
|
|
// able to acquire the audio monitor in order to resume or destroy the
|
|
|
|
// audio stream.
|
|
|
|
if (!mAudioStream->IsPaused()) {
|
|
|
|
mAudioStream->Write(sound->mAudioData,
|
|
|
|
sound->AudioDataLength(),
|
|
|
|
PR_TRUE);
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
offset = sound->mOffset;
|
|
|
|
samples = sound->mSamples;
|
2010-08-25 06:10:00 -07:00
|
|
|
|
|
|
|
// Dispatch events to the DOM for the audio just written.
|
|
|
|
mEventManager.QueueWrittenAudioData(sound->mAudioData.get(),
|
|
|
|
sound->AudioDataLength(),
|
|
|
|
(aSampleOffset + samples) * aChannels);
|
2010-08-12 19:28:15 -07:00
|
|
|
} else {
|
|
|
|
mReader->mAudioQueue.PushFront(sound);
|
|
|
|
sound.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (offset != -1) {
|
|
|
|
mDecoder->UpdatePlaybackOffset(offset);
|
|
|
|
}
|
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
nsresult nsBuiltinDecoderStateMachine::Init(nsDecoderStateMachine* aCloneDonor)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-09-20 17:49:50 -07:00
|
|
|
nsBuiltinDecoderReader* cloneReader = nsnull;
|
|
|
|
if (aCloneDonor) {
|
|
|
|
cloneReader = static_cast<nsBuiltinDecoderStateMachine*>(aCloneDonor)->mReader;
|
|
|
|
}
|
|
|
|
return mReader->Init(cloneReader);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StopPlayback(eStopMode aMode)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
2011-01-17 16:53:18 -08:00
|
|
|
mDecoder->mPlaybackStatistics.Stop(TimeStamp::Now());
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// Reset mPlayStartTime before we pause/shutdown the nsAudioStream. This is
|
|
|
|
// so that if the audio loop is about to write audio, it will have the chance
|
|
|
|
// to check to see if we're paused and not write the audio. If not, the
|
|
|
|
// audio thread can block in the write, and we deadlock trying to acquire
|
|
|
|
// the audio monitor upon resume playback.
|
|
|
|
if (IsPlaying()) {
|
|
|
|
mPlayDuration += TimeStamp::Now() - mPlayStartTime;
|
|
|
|
mPlayStartTime = TimeStamp();
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
MonitorAutoEnter audioMon(mAudioMonitor);
|
|
|
|
if (mAudioStream) {
|
|
|
|
if (aMode == AUDIO_PAUSE) {
|
|
|
|
mAudioStream->Pause();
|
|
|
|
} else if (aMode == AUDIO_SHUTDOWN) {
|
|
|
|
mAudioStream->Shutdown();
|
|
|
|
mAudioStream = nsnull;
|
2010-08-25 06:10:00 -07:00
|
|
|
mEventManager.Clear();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StartPlayback()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
|
|
|
NS_ASSERTION(!IsPlaying(), "Shouldn't be playing when StartPlayback() is called");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p StartPlayback", mDecoder));
|
2011-01-17 16:53:18 -08:00
|
|
|
mDecoder->mPlaybackStatistics.Start(TimeStamp::Now());
|
2010-04-01 20:03:07 -07:00
|
|
|
if (HasAudio()) {
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
MonitorAutoEnter audioMon(mAudioMonitor);
|
|
|
|
if (mAudioStream) {
|
|
|
|
// We have an audiostream, so it must have been paused the last time
|
|
|
|
// StopPlayback() was called.
|
|
|
|
mAudioStream->Resume();
|
|
|
|
} else {
|
|
|
|
// No audiostream, create one.
|
2010-05-18 20:04:33 -07:00
|
|
|
const nsVideoInfo& info = mReader->GetInfo();
|
2010-11-16 20:14:19 -08:00
|
|
|
mAudioStream = nsAudioStream::AllocateStream();
|
2010-05-18 20:04:33 -07:00
|
|
|
mAudioStream->Init(info.mAudioChannels,
|
|
|
|
info.mAudioRate,
|
2010-10-18 19:55:45 -07:00
|
|
|
MOZ_SOUND_DATA_FORMAT);
|
2010-04-01 20:03:07 -07:00
|
|
|
mAudioStream->SetVolume(mVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mPlayStartTime = TimeStamp::Now();
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
|
2011-01-31 18:57:13 -08:00
|
|
|
void nsBuiltinDecoderStateMachine::UpdatePlaybackPositionInternal(PRInt64 aTime)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
NS_ASSERTION(mStartTime >= 0, "Should have positive mStartTime");
|
|
|
|
mCurrentFrameTime = aTime - mStartTime;
|
|
|
|
NS_ASSERTION(mCurrentFrameTime >= 0, "CurrentTime should be positive!");
|
|
|
|
if (aTime > mEndTime) {
|
|
|
|
NS_ASSERTION(mCurrentFrameTime > GetDuration(),
|
|
|
|
"CurrentTime must be after duration if aTime > endTime!");
|
|
|
|
mEndTime = aTime;
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::DurationChanged);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2011-01-31 18:57:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsBuiltinDecoderStateMachine::UpdatePlaybackPosition(PRInt64 aTime)
|
|
|
|
{
|
|
|
|
UpdatePlaybackPositionInternal(aTime);
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
if (!mPositionChangeQueued) {
|
|
|
|
mPositionChangeQueued = PR_TRUE;
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::PlaybackPositionChanged);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2010-08-25 06:10:00 -07:00
|
|
|
|
|
|
|
// Notify DOM of any queued up audioavailable events
|
2010-09-14 16:24:47 -07:00
|
|
|
mEventManager.DispatchPendingEvents(GetMediaTime());
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::ClearPositionChangeFlag()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
mPositionChangeQueued = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
nsHTMLMediaElement::NextFrameStatus nsBuiltinDecoderStateMachine::GetNextFrameStatus()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
if (IsBuffering() || IsSeeking()) {
|
|
|
|
return nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE_BUFFERING;
|
|
|
|
} else if (HaveNextFrameData()) {
|
|
|
|
return nsHTMLMediaElement::NEXT_FRAME_AVAILABLE;
|
|
|
|
}
|
|
|
|
return nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2011-01-16 19:03:00 -08:00
|
|
|
void nsBuiltinDecoderStateMachine::SetVolume(double volume)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2010-09-05 19:14:50 -07:00
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
mVolume = volume;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
double nsBuiltinDecoderStateMachine::GetCurrentTime() const
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-03-23 15:28:57 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread() ||
|
|
|
|
mDecoder->OnStateMachineThread() ||
|
|
|
|
OnDecodeThread(),
|
|
|
|
"Should be on main, decode, or state machine thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-01-16 19:03:00 -08:00
|
|
|
return static_cast<double>(mCurrentFrameTime) / 1000.0;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
PRInt64 nsBuiltinDecoderStateMachine::GetDuration()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
if (mEndTime == -1 || mStartTime == -1)
|
|
|
|
return -1;
|
|
|
|
return mEndTime - mStartTime;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::SetDuration(PRInt64 aDuration)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-06-08 16:31:27 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread() || mDecoder->OnStateMachineThread(),
|
|
|
|
"Should be on main or state machine thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
if (mStartTime != -1) {
|
|
|
|
mEndTime = mStartTime + aDuration;
|
|
|
|
} else {
|
|
|
|
mStartTime = 0;
|
|
|
|
mEndTime = aDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::SetSeekable(PRBool aSeekable)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
mSeekable = aSeekable;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::Shutdown()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
|
|
|
// Once we've entered the shutdown state here there's no going back.
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
|
|
|
|
// Change state before issuing shutdown request to threads so those
|
|
|
|
// threads can start exiting cleanly during the Shutdown call.
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state to SHUTDOWN", mDecoder));
|
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StartDecoding()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
|
|
|
"Should be on state machine thread.");
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
if (mState != DECODER_STATE_DECODING) {
|
|
|
|
mDecodeStartTime = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
mState = DECODER_STATE_DECODING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsBuiltinDecoderStateMachine::Play()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2011-03-23 15:28:57 -07:00
|
|
|
// When asked to play, switch to decoding state only if
|
|
|
|
// we are currently buffering. In other cases, we'll start playing anyway
|
|
|
|
// when the state machine notices the decoder's state change to PLAYING.
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
if (mState == DECODER_STATE_BUFFERING) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from BUFFERING to DECODING", mDecoder));
|
|
|
|
mState = DECODER_STATE_DECODING;
|
2011-03-23 15:28:57 -07:00
|
|
|
mDecodeStartTime = TimeStamp::Now();
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::ResetPlayback()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
2010-05-30 21:02:00 -07:00
|
|
|
mVideoFrameEndTime = -1;
|
2010-04-01 20:03:07 -07:00
|
|
|
mAudioStartTime = -1;
|
|
|
|
mAudioEndTime = -1;
|
|
|
|
mAudioCompleted = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-01-16 19:03:00 -08:00
|
|
|
void nsBuiltinDecoderStateMachine::Seek(double aTime)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
2010-04-27 01:53:44 -07:00
|
|
|
// nsBuiltinDecoder::mPlayState should be SEEKING while we seek, and
|
|
|
|
// in that case nsBuiltinDecoder shouldn't be calling us.
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(mState != DECODER_STATE_SEEKING,
|
|
|
|
"We shouldn't already be seeking");
|
|
|
|
NS_ASSERTION(mState >= DECODER_STATE_DECODING,
|
|
|
|
"We should have loaded metadata");
|
|
|
|
double t = aTime * 1000.0;
|
|
|
|
if (t > PR_INT64_MAX) {
|
|
|
|
// Prevent integer overflow.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSeekTime = static_cast<PRInt64>(t) + mStartTime;
|
|
|
|
NS_ASSERTION(mSeekTime >= mStartTime && mSeekTime <= mEndTime,
|
|
|
|
"Can only seek in range [0,duration]");
|
|
|
|
|
|
|
|
// Bound the seek time to be inside the media range.
|
|
|
|
NS_ASSERTION(mStartTime != -1, "Should know start time by now");
|
|
|
|
NS_ASSERTION(mEndTime != -1, "Should know end time by now");
|
|
|
|
mSeekTime = NS_MIN(mSeekTime, mEndTime);
|
|
|
|
mSeekTime = NS_MAX(mStartTime, mSeekTime);
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state to SEEKING (to %f)", mDecoder, aTime));
|
|
|
|
mState = DECODER_STATE_SEEKING;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StopDecodeThreads()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
mStopDecodeThreads = PR_TRUE;
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
if (mDecodeThread) {
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
mDecodeThread->Shutdown();
|
|
|
|
}
|
|
|
|
mDecodeThread = nsnull;
|
|
|
|
}
|
|
|
|
if (mAudioThread) {
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
mAudioThread->Shutdown();
|
|
|
|
}
|
|
|
|
mAudioThread = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-05-05 19:31:02 -07:00
|
|
|
nsBuiltinDecoderStateMachine::StartDecodeThreads()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
mStopDecodeThreads = PR_FALSE;
|
|
|
|
if (!mDecodeThread && mState < DECODER_STATE_COMPLETED) {
|
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(mDecodeThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_NewRunnableMethod(this, &nsBuiltinDecoderStateMachine::DecodeLoop);
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecodeThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
if (HasAudio() && !mAudioThread) {
|
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(mAudioThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-05-05 19:31:02 -07:00
|
|
|
NS_NewRunnableMethod(this, &nsBuiltinDecoderStateMachine::AudioLoop);
|
2010-04-01 20:03:07 -07:00
|
|
|
mAudioThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-14 16:24:47 -07:00
|
|
|
PRInt64 nsBuiltinDecoderStateMachine::AudioDecodedMs() const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasAudio(),
|
|
|
|
"Should only call AudioDecodedMs() when we have audio");
|
|
|
|
// The amount of audio we have decoded is the amount of audio data we've
|
|
|
|
// already decoded and pushed to the hardware, plus the amount of audio
|
|
|
|
// data waiting to be pushed to the hardware.
|
|
|
|
PRInt64 pushed = (mAudioEndTime != -1) ? (mAudioEndTime - GetMediaTime()) : 0;
|
|
|
|
return pushed + mReader->mAudioQueue.Duration();
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
PRBool nsBuiltinDecoderStateMachine::HasLowDecodedData(PRInt64 aAudioMs) const
|
2011-01-17 16:53:18 -08:00
|
|
|
{
|
2011-03-23 15:28:57 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
// We consider ourselves low on decoded data if we're low on audio,
|
|
|
|
// provided we've not decoded to the end of the audio stream, or
|
|
|
|
// if we're only playing video and we're low on video frames, provided
|
|
|
|
// we've not decoded to the end of the video stream.
|
|
|
|
return ((HasAudio() &&
|
|
|
|
!mReader->mAudioQueue.IsFinished() &&
|
|
|
|
AudioDecodedMs() < aAudioMs)
|
|
|
|
||
|
|
|
|
(!HasAudio() &&
|
|
|
|
HasVideo() &&
|
|
|
|
!mReader->mVideoQueue.IsFinished() &&
|
|
|
|
static_cast<PRUint32>(mReader->mVideoQueue.GetSize()) < LOW_VIDEO_FRAMES));
|
2011-01-17 16:53:18 -08:00
|
|
|
}
|
2010-09-14 16:24:47 -07:00
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
PRBool nsBuiltinDecoderStateMachine::HasLowUndecodedData() const
|
|
|
|
{
|
|
|
|
return GetUndecodedData() < LOW_DATA_THRESHOLD_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 nsBuiltinDecoderStateMachine::GetUndecodedData() const
|
|
|
|
{
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
NS_ASSERTION(mState > DECODER_STATE_DECODING_METADATA,
|
|
|
|
"Must have loaded metadata for GetBuffered() to work");
|
|
|
|
nsTimeRanges buffered;
|
|
|
|
|
|
|
|
nsresult res = mDecoder->GetBuffered(&buffered);
|
|
|
|
NS_ENSURE_SUCCESS(res, 0);
|
|
|
|
double currentTime = GetCurrentTime();
|
|
|
|
|
|
|
|
nsIDOMTimeRanges* r = static_cast<nsIDOMTimeRanges*>(&buffered);
|
|
|
|
PRUint32 length = 0;
|
|
|
|
res = r->GetLength(&length);
|
|
|
|
NS_ENSURE_SUCCESS(res, 0);
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < length; ++index) {
|
|
|
|
double start, end;
|
|
|
|
res = r->Start(index, &start);
|
|
|
|
NS_ENSURE_SUCCESS(res, 0);
|
|
|
|
|
|
|
|
res = r->End(index, &end);
|
|
|
|
NS_ENSURE_SUCCESS(res, 0);
|
|
|
|
|
|
|
|
if (start <= currentTime && end >= currentTime) {
|
|
|
|
return static_cast<PRInt64>((end - currentTime) * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
nsresult nsBuiltinDecoderStateMachine::Run()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
2010-04-27 01:53:44 -07:00
|
|
|
nsMediaStream* stream = mDecoder->GetCurrentStream();
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ENSURE_TRUE(stream, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
while (PR_TRUE) {
|
|
|
|
MonitorAutoEnter mon(mDecoder->GetMonitor());
|
|
|
|
switch (mState) {
|
|
|
|
case DECODER_STATE_SHUTDOWN:
|
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback(AUDIO_SHUTDOWN);
|
|
|
|
}
|
|
|
|
StopDecodeThreads();
|
|
|
|
NS_ASSERTION(mState == DECODER_STATE_SHUTDOWN,
|
|
|
|
"How did we escape from the shutdown state???");
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
case DECODER_STATE_DECODING_METADATA:
|
|
|
|
{
|
2010-05-05 19:31:02 -07:00
|
|
|
LoadMetadata();
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mState == DECODER_STATE_SHUTDOWN) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoData* videoData = FindStartTime();
|
|
|
|
if (videoData) {
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
2011-03-23 15:28:57 -07:00
|
|
|
RenderVideoFrame(videoData, TimeStamp::Now());
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the decode threads, so that we can pre buffer the streams.
|
|
|
|
// and calculate the start time in order to determine the duration.
|
|
|
|
if (NS_FAILED(StartDecodeThreads())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mStartTime != -1, "Must have start time");
|
|
|
|
NS_ASSERTION((!HasVideo() && !HasAudio()) ||
|
|
|
|
!mSeekable || mEndTime != -1,
|
|
|
|
"Active seekable media should have end time");
|
|
|
|
NS_ASSERTION(!mSeekable || GetDuration() != -1, "Seekable media should have duration");
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Media goes from %lldms to %lldms (duration %lldms) seekable=%d",
|
|
|
|
mDecoder, mStartTime, mEndTime, GetDuration(), mSeekable));
|
|
|
|
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN)
|
|
|
|
continue;
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
// Inform the element that we've loaded the metadata and the first frame,
|
|
|
|
// setting the default framebuffer size for audioavailable events. Also,
|
|
|
|
// if there is audio, let the MozAudioAvailable event manager know about
|
|
|
|
// the metadata.
|
|
|
|
const nsVideoInfo& info = mReader->GetInfo();
|
|
|
|
PRUint32 frameBufferLength = info.mAudioChannels * FRAMEBUFFER_LENGTH_PER_CHANNEL;
|
2010-04-01 20:03:07 -07:00
|
|
|
nsCOMPtr<nsIRunnable> metadataLoadedEvent =
|
2010-08-25 06:10:00 -07:00
|
|
|
new nsAudioMetadataEventRunner(mDecoder, info.mAudioChannels,
|
|
|
|
info.mAudioRate, frameBufferLength);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_DispatchToMainThread(metadataLoadedEvent, NS_DISPATCH_NORMAL);
|
2010-08-25 06:10:00 -07:00
|
|
|
if (HasAudio()) {
|
|
|
|
mEventManager.Init(info.mAudioChannels, info.mAudioRate);
|
|
|
|
mDecoder->RequestFrameBufferLength(frameBufferLength);
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
if (mState == DECODER_STATE_DECODING_METADATA) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from DECODING_METADATA to DECODING", mDecoder));
|
2011-03-23 15:28:57 -07:00
|
|
|
StartDecoding();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start playback.
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
2010-04-01 20:03:07 -07:00
|
|
|
if (!IsPlaying()) {
|
|
|
|
StartPlayback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODER_STATE_DECODING:
|
|
|
|
{
|
|
|
|
if (NS_FAILED(StartDecodeThreads())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AdvanceFrame();
|
|
|
|
|
|
|
|
if (mState != DECODER_STATE_DECODING)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODER_STATE_SEEKING:
|
|
|
|
{
|
|
|
|
// During the seek, don't have a lock on the decoder state,
|
|
|
|
// otherwise long seek operations can block the main thread.
|
|
|
|
// The events dispatched to the main thread are SYNC calls.
|
|
|
|
// These calls are made outside of the decode monitor lock so
|
|
|
|
// it is safe for the main thread to makes calls that acquire
|
|
|
|
// the lock since it won't deadlock. We check the state when
|
|
|
|
// acquiring the lock again in case shutdown has occurred
|
|
|
|
// during the time when we didn't have the lock.
|
|
|
|
PRInt64 seekTime = mSeekTime;
|
|
|
|
mDecoder->StopProgressUpdates();
|
|
|
|
|
2010-09-09 19:48:36 -07:00
|
|
|
PRBool currentTimeChanged = false;
|
2010-09-14 16:24:47 -07:00
|
|
|
PRInt64 mediaTime = GetMediaTime();
|
|
|
|
if (mediaTime != seekTime) {
|
2010-09-09 19:48:36 -07:00
|
|
|
currentTimeChanged = true;
|
2011-01-31 18:57:13 -08:00
|
|
|
UpdatePlaybackPositionInternal(seekTime);
|
2010-09-09 19:48:36 -07:00
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// SeekingStarted will do a UpdateReadyStateForData which will
|
|
|
|
// inform the element and its users that we have no frames
|
|
|
|
// to display
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
nsCOMPtr<nsIRunnable> startEvent =
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::SeekingStarted);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_DispatchToMainThread(startEvent, NS_DISPATCH_SYNC);
|
|
|
|
}
|
2011-01-31 18:57:13 -08:00
|
|
|
|
2010-09-09 19:48:36 -07:00
|
|
|
if (currentTimeChanged) {
|
2010-08-12 19:28:15 -07:00
|
|
|
// The seek target is different than the current playback position,
|
|
|
|
// we'll need to seek the playback position, so shutdown our decode
|
|
|
|
// and audio threads.
|
|
|
|
StopPlayback(AUDIO_SHUTDOWN);
|
|
|
|
StopDecodeThreads();
|
|
|
|
ResetPlayback();
|
2010-04-01 20:03:07 -07:00
|
|
|
nsresult res;
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
// Now perform the seek. We must not hold the state machine monitor
|
|
|
|
// while we seek, since the seek decodes.
|
2010-08-19 15:50:37 -07:00
|
|
|
res = mReader->Seek(seekTime,
|
|
|
|
mStartTime,
|
|
|
|
mEndTime,
|
2010-09-14 16:24:47 -07:00
|
|
|
mediaTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(res)){
|
|
|
|
SoundData* audio = HasAudio() ? mReader->mAudioQueue.PeekFront() : nsnull;
|
2010-10-06 15:58:36 -07:00
|
|
|
NS_ASSERTION(!audio || (audio->mTime <= seekTime &&
|
|
|
|
seekTime <= audio->mTime + audio->mDuration),
|
|
|
|
"Seek target should lie inside the first audio block after seek");
|
|
|
|
PRInt64 startTime = (audio && audio->mTime < seekTime) ? audio->mTime : seekTime;
|
|
|
|
mAudioStartTime = startTime;
|
2011-03-23 15:28:57 -07:00
|
|
|
mPlayDuration = MsToDuration(startTime - mStartTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
if (HasVideo()) {
|
|
|
|
nsAutoPtr<VideoData> video(mReader->mVideoQueue.PeekFront());
|
|
|
|
if (video) {
|
2010-10-06 15:58:36 -07:00
|
|
|
NS_ASSERTION(video->mTime <= seekTime && seekTime <= video->mEndTime,
|
|
|
|
"Seek target should lie inside the first frame after seek");
|
2011-03-23 15:28:57 -07:00
|
|
|
RenderVideoFrame(video, TimeStamp::Now());
|
2010-10-06 15:58:36 -07:00
|
|
|
mReader->mVideoQueue.PopFront();
|
2011-01-31 18:57:13 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::Invalidate);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDecoder->StartProgressUpdates();
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Try to decode another frame to detect if we're at the end...
|
|
|
|
LOG(PR_LOG_DEBUG, ("Seek completed, mCurrentFrameTime=%lld\n", mCurrentFrameTime));
|
|
|
|
|
|
|
|
// Change state to DECODING or COMPLETED now. SeekingStopped will
|
2010-05-05 19:31:02 -07:00
|
|
|
// call nsBuiltinDecoderStateMachine::Seek to reset our state to SEEKING
|
2010-04-01 20:03:07 -07:00
|
|
|
// if we need to seek again.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> stopEvent;
|
2010-09-14 16:24:47 -07:00
|
|
|
if (GetMediaTime() == mEndTime) {
|
2010-04-01 20:03:07 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from SEEKING (to %lldms) to COMPLETED",
|
|
|
|
mDecoder, seekTime));
|
2010-04-27 01:53:44 -07:00
|
|
|
stopEvent = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::SeekingStoppedAtEnd);
|
2010-04-01 20:03:07 -07:00
|
|
|
mState = DECODER_STATE_COMPLETED;
|
|
|
|
} else {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from SEEKING (to %lldms) to DECODING",
|
|
|
|
mDecoder, seekTime));
|
2010-04-27 01:53:44 -07:00
|
|
|
stopEvent = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::SeekingStopped);
|
2011-03-23 15:28:57 -07:00
|
|
|
StartDecoding();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
NS_DispatchToMainThread(stopEvent, NS_DISPATCH_SYNC);
|
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
|
|
|
|
// Reset quick buffering status. This ensures that if we began the
|
|
|
|
// seek while quick-buffering, we won't bypass quick buffering mode
|
|
|
|
// if we need to buffer after the seek.
|
|
|
|
mQuickBuffering = PR_FALSE;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECODER_STATE_BUFFERING:
|
|
|
|
{
|
2011-01-17 16:53:18 -08:00
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback(AUDIO_PAUSE);
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
|
2011-01-17 16:53:18 -08:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2011-03-23 15:28:57 -07:00
|
|
|
NS_ASSERTION(!mBufferingStart.IsNull(), "Must know buffering start time.");
|
2011-01-17 16:53:18 -08:00
|
|
|
|
2010-09-14 16:24:47 -07:00
|
|
|
// We will remain in the buffering state if we've not decoded enough
|
|
|
|
// data to begin playback, or if we've not downloaded a reasonable
|
|
|
|
// amount of data inside our buffering time.
|
2011-01-17 16:53:18 -08:00
|
|
|
TimeDuration elapsed = now - mBufferingStart;
|
2010-10-06 15:58:36 -07:00
|
|
|
PRBool isLiveStream = mDecoder->GetCurrentStream()->GetLength() == -1;
|
2010-11-28 12:06:38 -08:00
|
|
|
if ((isLiveStream || !mDecoder->CanPlayThrough()) &&
|
2010-09-14 16:24:47 -07:00
|
|
|
elapsed < TimeDuration::FromSeconds(BUFFERING_WAIT) &&
|
2011-03-23 15:28:57 -07:00
|
|
|
(mQuickBuffering ? HasLowDecodedData(QUICK_BUFFERING_LOW_DATA_MS)
|
|
|
|
: (GetUndecodedData() < BUFFERING_WAIT * 1000)) &&
|
2010-09-14 16:24:47 -07:00
|
|
|
!stream->IsDataCachedToEndOfStream(mDecoder->mDecoderPosition) &&
|
|
|
|
!stream->IsSuspended())
|
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
LOG(PR_LOG_DEBUG,
|
2011-03-23 15:28:57 -07:00
|
|
|
("Buffering: %.3lfs/%ds, timeout in %.3lfs %s",
|
|
|
|
GetUndecodedData() / 1000.0,
|
|
|
|
BUFFERING_WAIT,
|
|
|
|
BUFFERING_WAIT - elapsed.ToSeconds(),
|
|
|
|
(mQuickBuffering ? "(quick exit)" : "")));
|
2010-04-01 20:03:07 -07:00
|
|
|
Wait(1000);
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN)
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from BUFFERING to DECODING", mDecoder));
|
2011-03-23 15:28:57 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Buffered for %.3lfs",
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder,
|
2011-01-17 16:53:18 -08:00
|
|
|
(now - mBufferingStart).ToSeconds()));
|
2011-03-23 15:28:57 -07:00
|
|
|
StartDecoding();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mState != DECODER_STATE_BUFFERING) {
|
|
|
|
// Notify to allow blocked decoder thread to continue
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
UpdateReadyState();
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
2010-04-01 20:03:07 -07:00
|
|
|
if (!IsPlaying()) {
|
|
|
|
StartPlayback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DECODER_STATE_COMPLETED:
|
|
|
|
{
|
|
|
|
if (NS_FAILED(StartDecodeThreads())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-12 17:59:42 -07:00
|
|
|
// Play the remaining media. We want to run AdvanceFrame() at least
|
|
|
|
// once to ensure the current playback position is advanced to the
|
|
|
|
// end of the media, and so that we update the readyState.
|
|
|
|
do {
|
2010-04-01 20:03:07 -07:00
|
|
|
AdvanceFrame();
|
2010-05-12 17:59:42 -07:00
|
|
|
} while (mState == DECODER_STATE_COMPLETED &&
|
|
|
|
(mReader->mVideoQueue.GetSize() > 0 ||
|
|
|
|
(HasAudio() && !mAudioCompleted)));
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-04-08 21:33:07 -07:00
|
|
|
if (mAudioStream) {
|
2010-05-30 21:02:00 -07:00
|
|
|
// Close the audio stream so that next time audio is used a new stream
|
2010-04-01 20:03:07 -07:00
|
|
|
// is created. The StopPlayback call also resets the IsPlaying() state
|
|
|
|
// so audio is restarted correctly.
|
|
|
|
StopPlayback(AUDIO_SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState != DECODER_STATE_COMPLETED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("Shutting down the state machine thread"));
|
|
|
|
StopDecodeThreads();
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
2010-05-30 21:02:00 -07:00
|
|
|
PRInt64 videoTime = HasVideo() ? mVideoFrameEndTime : 0;
|
2010-04-01 20:03:07 -07:00
|
|
|
PRInt64 clockTime = NS_MAX(mEndTime, NS_MAX(videoTime, GetAudioClock()));
|
|
|
|
UpdatePlaybackPosition(clockTime);
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::PlaybackEnded);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:45:45 -07:00
|
|
|
if (mState == DECODER_STATE_COMPLETED) {
|
|
|
|
// We've finished playback. Shutdown the state machine thread,
|
|
|
|
// in order to save memory on thread stacks, particuarly on Linux.
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new ShutdownThreadEvent(mDecoder->mStateMachineThread);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
mDecoder->mStateMachineThread = nsnull;
|
|
|
|
return NS_OK;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::RenderVideoFrame(VideoData* aData,
|
|
|
|
TimeStamp aTarget)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread), "Should be on state machine thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
if (aData->mDuplicate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-18 20:04:33 -07:00
|
|
|
nsRefPtr<Image> image = aData->mImage;
|
2010-04-01 20:03:07 -07:00
|
|
|
if (image) {
|
2010-05-18 20:04:33 -07:00
|
|
|
const nsVideoInfo& info = mReader->GetInfo();
|
2011-03-23 15:28:57 -07:00
|
|
|
mDecoder->SetVideoData(gfxIntSize(info.mDisplay.width, info.mDisplay.height),
|
|
|
|
info.mPixelAspectRatio,
|
|
|
|
image,
|
|
|
|
aTarget);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64
|
2010-05-05 19:31:02 -07:00
|
|
|
nsBuiltinDecoderStateMachine::GetAudioClock()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread), "Should be on state machine thread.");
|
2010-04-08 21:33:07 -07:00
|
|
|
if (!mAudioStream || !HasAudio())
|
2010-04-01 20:03:07 -07:00
|
|
|
return -1;
|
2010-04-08 21:33:07 -07:00
|
|
|
PRInt64 t = mAudioStream->GetPosition();
|
2010-04-01 20:03:07 -07:00
|
|
|
return (t == -1) ? -1 : t + mAudioStartTime;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::AdvanceFrame()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread), "Should be on state machine thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
// When it's time to display a frame, decode the frame and display it.
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
2010-04-01 20:03:07 -07:00
|
|
|
if (HasAudio() && mAudioStartTime == -1 && !mAudioCompleted) {
|
2010-05-30 21:02:00 -07:00
|
|
|
// We've got audio (so we should sync off the audio clock), but we've not
|
|
|
|
// played a sample on the audio thread, so we can't get a time from the
|
|
|
|
// audio clock. Just wait and then return, to give the audio clock time
|
|
|
|
// to tick. This should really wait for a specific signal from the audio
|
|
|
|
// thread rather than polling after a sleep. See bug 568431 comment 4.
|
|
|
|
Wait(AUDIO_DURATION_MS);
|
2010-04-01 20:03:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the clock time. If we've got audio, and we've not reached
|
|
|
|
// the end of the audio, use the audio clock. However if we've finished
|
|
|
|
// audio, or don't have audio, use the system clock.
|
|
|
|
PRInt64 clock_time = -1;
|
2011-03-23 15:28:57 -07:00
|
|
|
if (!IsPlaying()) {
|
|
|
|
clock_time = DurationToMs(mPlayDuration) + mStartTime;
|
2010-04-01 20:03:07 -07:00
|
|
|
} else {
|
2011-03-23 15:28:57 -07:00
|
|
|
PRInt64 audio_time = GetAudioClock();
|
|
|
|
if (HasAudio() && !mAudioCompleted && audio_time != -1) {
|
|
|
|
clock_time = audio_time;
|
|
|
|
// Resync against the audio clock, while we're trusting the
|
|
|
|
// audio clock. This ensures no "drift", particularly on Linux.
|
|
|
|
mPlayDuration = MsToDuration(clock_time - mStartTime);
|
|
|
|
mPlayStartTime = TimeStamp::Now();
|
|
|
|
} else {
|
|
|
|
// Sound is disabled on this system. Sync to the system clock.
|
|
|
|
clock_time = DurationToMs(TimeStamp::Now() - mPlayStartTime + mPlayDuration);
|
|
|
|
// Ensure the clock can never go backwards.
|
|
|
|
NS_ASSERTION(mCurrentFrameTime <= clock_time, "Clock should go forwards");
|
|
|
|
clock_time = NS_MAX(mCurrentFrameTime, clock_time) + mStartTime;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// Skip frames up to the frame at the playback position, and figure out
|
|
|
|
// the time remaining until it's time to display the next frame.
|
2011-02-17 18:30:33 -08:00
|
|
|
PRInt64 remainingTime = AUDIO_DURATION_MS;
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(clock_time >= mStartTime, "Should have positive clock time.");
|
2011-02-17 18:30:33 -08:00
|
|
|
nsAutoPtr<VideoData> currentFrame;
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mReader->mVideoQueue.GetSize() > 0) {
|
2011-02-17 18:30:33 -08:00
|
|
|
VideoData* frame = mReader->mVideoQueue.PeekFront();
|
|
|
|
while (clock_time >= frame->mTime) {
|
|
|
|
mVideoFrameEndTime = frame->mEndTime;
|
|
|
|
currentFrame = frame;
|
2010-04-01 20:03:07 -07:00
|
|
|
mReader->mVideoQueue.PopFront();
|
2011-02-17 18:30:33 -08:00
|
|
|
mDecoder->UpdatePlaybackOffset(frame->mOffset);
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mReader->mVideoQueue.GetSize() == 0)
|
|
|
|
break;
|
2011-02-17 18:30:33 -08:00
|
|
|
frame = mReader->mVideoQueue.PeekFront();
|
|
|
|
}
|
|
|
|
// Current frame has already been presented, wait until it's time to
|
|
|
|
// present the next frame.
|
|
|
|
if (frame && !currentFrame) {
|
2011-03-23 15:28:57 -07:00
|
|
|
PRInt64 now = IsPlaying()
|
|
|
|
? DurationToMs(TimeStamp::Now() - mPlayStartTime + mPlayDuration)
|
|
|
|
: DurationToMs(mPlayDuration);
|
2011-02-17 18:30:33 -08:00
|
|
|
remainingTime = frame->mTime - mStartTime - now;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
// Check to see if we don't have enough data to play up to the next frame.
|
|
|
|
// If we don't, switch to buffering mode.
|
|
|
|
nsMediaStream* stream = mDecoder->GetCurrentStream();
|
|
|
|
if (mState == DECODER_STATE_DECODING &&
|
|
|
|
mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING &&
|
|
|
|
HasLowDecodedData(remainingTime + EXHAUSTED_DATA_MARGIN_MS) &&
|
|
|
|
!stream->IsDataCachedToEndOfStream(mDecoder->mDecoderPosition) &&
|
2011-03-23 15:28:57 -07:00
|
|
|
!stream->IsSuspended() &&
|
2011-03-23 15:28:57 -07:00
|
|
|
(JustExitedQuickBuffering() || HasLowUndecodedData()))
|
2011-03-23 15:28:57 -07:00
|
|
|
{
|
|
|
|
if (currentFrame) {
|
|
|
|
mReader->mVideoQueue.PushFront(currentFrame.forget());
|
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
StartBuffering();
|
2011-03-23 15:28:57 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've got enough data to keep playing until at least the next frame.
|
|
|
|
// Start playing now if need be.
|
|
|
|
if (!IsPlaying()) {
|
|
|
|
StartPlayback();
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
|
2011-02-17 18:30:33 -08:00
|
|
|
if (currentFrame) {
|
2011-03-23 15:28:57 -07:00
|
|
|
// Decode one frame and display it.
|
2011-03-23 15:28:57 -07:00
|
|
|
TimeStamp presTime = mPlayStartTime - mPlayDuration +
|
2011-03-23 15:28:58 -07:00
|
|
|
MsToDuration(currentFrame->mTime - mStartTime);
|
2011-02-17 18:30:33 -08:00
|
|
|
NS_ASSERTION(currentFrame->mTime >= mStartTime, "Should have positive frame time");
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
// If we have video, we want to increment the clock in steps of the frame
|
|
|
|
// duration.
|
2011-03-23 15:28:57 -07:00
|
|
|
RenderVideoFrame(currentFrame, presTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
mDecoder->GetFrameStatistics().NotifyPresentedFrame();
|
2011-03-23 15:28:57 -07:00
|
|
|
PRInt64 now = DurationToMs(TimeStamp::Now() - mPlayStartTime + mPlayDuration);
|
2011-02-17 18:30:33 -08:00
|
|
|
remainingTime = currentFrame->mEndTime - mStartTime - now;
|
|
|
|
currentFrame = nsnull;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-11-04 17:44:33 -07:00
|
|
|
// Kick the decode thread in case it filled its buffers and put itself
|
|
|
|
// to sleep.
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// Cap the current time to the larger of the audio and video end time.
|
|
|
|
// This ensures that if we're running off the system clock, we don't
|
|
|
|
// advance the clock to after the media end time.
|
2010-05-30 21:02:00 -07:00
|
|
|
if (mVideoFrameEndTime != -1 || mAudioEndTime != -1) {
|
2010-04-01 20:03:07 -07:00
|
|
|
// These will be non -1 if we've displayed a video frame, or played an audio sample.
|
2010-05-30 21:02:00 -07:00
|
|
|
clock_time = NS_MIN(clock_time, NS_MAX(mVideoFrameEndTime, mAudioEndTime));
|
2010-10-06 15:58:36 -07:00
|
|
|
if (clock_time > GetMediaTime()) {
|
2010-04-01 20:03:07 -07:00
|
|
|
// Only update the playback position if the clock time is greater
|
|
|
|
// than the previous playback position. The audio clock can
|
|
|
|
// sometimes report a time less than its previously reported in
|
|
|
|
// some situations, and we need to gracefully handle that.
|
|
|
|
UpdatePlaybackPosition(clock_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the number of audio/video samples queued has changed, either by
|
|
|
|
// this function popping and playing a video sample, or by the audio
|
|
|
|
// thread popping and playing an audio sample, we may need to update our
|
|
|
|
// ready state. Post an update to do so.
|
|
|
|
UpdateReadyState();
|
|
|
|
|
2011-02-17 18:30:33 -08:00
|
|
|
if (remainingTime > 0) {
|
|
|
|
Wait(remainingTime);
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
} else {
|
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback(AUDIO_PAUSE);
|
|
|
|
mDecoder->GetMonitor().NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == DECODER_STATE_DECODING ||
|
|
|
|
mState == DECODER_STATE_COMPLETED) {
|
|
|
|
mDecoder->GetMonitor().Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:58 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::Wait(PRInt64 aMs) {
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
2011-03-23 15:28:58 -07:00
|
|
|
TimeStamp end = TimeStamp::Now() + MsToDuration(aMs);
|
2010-04-01 20:03:07 -07:00
|
|
|
TimeStamp now;
|
|
|
|
while ((now = TimeStamp::Now()) < end &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
mState != DECODER_STATE_SEEKING)
|
|
|
|
{
|
2011-03-23 15:28:58 -07:00
|
|
|
PRInt64 ms = static_cast<PRInt64>(NS_round((end - now).ToSeconds() * 1000));
|
|
|
|
if (ms == 0 || ms > PR_UINT32_MAX) {
|
2010-04-01 20:03:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(ms <= aMs && ms > 0,
|
2010-05-05 19:31:02 -07:00
|
|
|
"nsBuiltinDecoderStateMachine::Wait interval very wrong!");
|
2011-03-23 15:28:58 -07:00
|
|
|
mDecoder->GetMonitor().Wait(PR_MillisecondsToInterval(static_cast<PRUint32>(ms)));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
VideoData* nsBuiltinDecoderStateMachine::FindStartTime()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread), "Should be on state machine thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
PRInt64 startTime = 0;
|
|
|
|
mStartTime = 0;
|
|
|
|
VideoData* v = nsnull;
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
2010-05-18 20:04:33 -07:00
|
|
|
v = mReader->FindStartTime(mReader->GetInfo().mDataOffset, startTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
if (startTime != 0) {
|
|
|
|
mStartTime = startTime;
|
2010-10-06 15:58:36 -07:00
|
|
|
if (mGotDurationFromMetaData) {
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(mEndTime != -1,
|
|
|
|
"We should have mEndTime as supplied duration here");
|
|
|
|
// We were specified a duration from a Content-Duration HTTP header.
|
|
|
|
// Adjust mEndTime so that mEndTime-mStartTime matches the specified
|
|
|
|
// duration.
|
|
|
|
mEndTime = mStartTime + mEndTime;
|
|
|
|
}
|
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
// Set the audio start time to be start of media. If this lies before the
|
|
|
|
// first acutal audio sample we have, we'll inject silence during playback
|
|
|
|
// to ensure the audio starts at the correct time.
|
|
|
|
mAudioStartTime = mStartTime;
|
2010-04-01 20:03:07 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Media start time is %lldms", mDecoder, mStartTime));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::FindEndTime()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsMediaStream* stream = mDecoder->GetCurrentStream();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Seek to the end of file to find the length and duration.
|
|
|
|
PRInt64 length = stream->GetLength();
|
|
|
|
NS_ASSERTION(length > 0, "Must have a content length to get end time");
|
|
|
|
|
|
|
|
mEndTime = 0;
|
|
|
|
PRInt64 endTime = 0;
|
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
|
|
|
endTime = mReader->FindEndTime(length);
|
|
|
|
}
|
|
|
|
if (endTime != -1) {
|
|
|
|
mEndTime = endTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Media end time is %lldms", mDecoder, mEndTime));
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::UpdateReadyState() {
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
switch (GetNextFrameStatus()) {
|
|
|
|
case nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE_BUFFERING:
|
2010-04-27 01:53:44 -07:00
|
|
|
event = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::NextFrameUnavailableBuffering);
|
2010-04-01 20:03:07 -07:00
|
|
|
break;
|
|
|
|
case nsHTMLMediaElement::NEXT_FRAME_AVAILABLE:
|
2010-04-27 01:53:44 -07:00
|
|
|
event = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::NextFrameAvailable);
|
2010-04-01 20:03:07 -07:00
|
|
|
break;
|
|
|
|
case nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE:
|
2010-04-27 01:53:44 -07:00
|
|
|
event = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::NextFrameUnavailable);
|
2010-04-01 20:03:07 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PR_NOT_REACHED("unhandled frame state");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::LoadMetadata()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsCurrentThread(mDecoder->mStateMachineThread),
|
|
|
|
"Should be on state machine thread.");
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("Loading Media Headers"));
|
2010-10-16 12:57:45 -07:00
|
|
|
nsresult res;
|
2010-05-05 19:31:02 -07:00
|
|
|
{
|
|
|
|
MonitorAutoExit exitMon(mDecoder->GetMonitor());
|
2010-10-16 12:57:45 -07:00
|
|
|
res = mReader->ReadMetadata();
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2010-05-18 20:04:33 -07:00
|
|
|
const nsVideoInfo& info = mReader->GetInfo();
|
2010-05-05 19:31:02 -07:00
|
|
|
|
2010-10-16 12:57:45 -07:00
|
|
|
if (NS_FAILED(res) || (!info.mHasVideo && !info.mHasAudio)) {
|
2010-05-05 19:31:02 -07:00
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::DecodeError);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
return;
|
|
|
|
}
|
2010-10-16 12:57:45 -07:00
|
|
|
mDecoder->StartProgressUpdates();
|
|
|
|
mGotDurationFromMetaData = (GetDuration() != -1);
|
2010-05-05 19:31:02 -07:00
|
|
|
}
|
2010-07-22 15:48:32 -07:00
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
PRBool nsBuiltinDecoderStateMachine::JustExitedQuickBuffering()
|
|
|
|
{
|
|
|
|
return !mDecodeStartTime.IsNull() &&
|
|
|
|
mQuickBuffering &&
|
|
|
|
(TimeStamp::Now() - mDecodeStartTime) < TimeDuration::FromSeconds(QUICK_BUFFER_THRESHOLD_MS);
|
|
|
|
}
|
|
|
|
|
2010-07-22 15:48:32 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StartBuffering()
|
|
|
|
{
|
|
|
|
mDecoder->GetMonitor().AssertCurrentThreadIn();
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
TimeDuration decodeDuration = TimeStamp::Now() - mDecodeStartTime;
|
|
|
|
// Go into quick buffering mode provided we've not just left buffering using
|
|
|
|
// a "quick exit". This stops us flip-flopping between playing and buffering
|
|
|
|
// when the download speed is similar to the decode speed.
|
|
|
|
mQuickBuffering =
|
|
|
|
!JustExitedQuickBuffering() &&
|
|
|
|
decodeDuration < TimeDuration::FromMilliseconds(QUICK_BUFFER_THRESHOLD_MS);
|
|
|
|
mBufferingStart = TimeStamp::Now();
|
|
|
|
|
2010-07-22 15:48:32 -07:00
|
|
|
// We need to tell the element that buffering has started.
|
|
|
|
// We can't just directly send an asynchronous runnable that
|
|
|
|
// eventually fires the "waiting" event. The problem is that
|
|
|
|
// there might be pending main-thread events, such as "data
|
|
|
|
// received" notifications, that mean we're not actually still
|
|
|
|
// buffering by the time this runnable executes. So instead
|
|
|
|
// we just trigger UpdateReadyStateForData; when it runs, it
|
|
|
|
// will check the current state and decide whether to tell
|
|
|
|
// the element we're buffering or not.
|
|
|
|
UpdateReadyState();
|
|
|
|
mState = DECODER_STATE_BUFFERING;
|
2011-03-23 15:28:57 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("Changed state from DECODING to BUFFERING, decoded for %.3lfs",
|
|
|
|
decodeDuration.ToSeconds()));
|
|
|
|
nsMediaDecoder::Statistics stats = mDecoder->GetStatistics();
|
|
|
|
LOG(PR_LOG_DEBUG, ("Playback rate: %.1lfKB/s%s download rate: %.1lfKB/s%s",
|
|
|
|
stats.mPlaybackRate/1024, stats.mPlaybackRateReliable ? "" : " (unreliable)",
|
|
|
|
stats.mDownloadRate/1024, stats.mDownloadRateReliable ? "" : " (unreliable)"));
|
2010-07-22 15:48:32 -07:00
|
|
|
}
|