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"
|
2011-09-26 17:25:41 -07:00
|
|
|
#include "mozilla/Preferences.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
|
|
|
|
|
2011-09-26 17:25:41 -07:00
|
|
|
// Wait this number of milliseconds when buffering, then leave and play
|
2010-04-01 20:03:07 -07:00
|
|
|
// as best as we can if the required amount of data hasn't been
|
|
|
|
// retrieved.
|
2011-09-26 17:25:41 -07:00
|
|
|
static const PRUint32 BUFFERING_WAIT = 30000;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// If audio queue has less than this many usecs of decoded audio, we won't risk
|
2010-04-27 01:53:44 -07:00
|
|
|
// 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.
|
2011-04-13 15:12:23 -07:00
|
|
|
static const PRUint32 LOW_AUDIO_USECS = 300000;
|
2010-04-27 01:53:44 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// If more than this many usecs 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
|
2011-04-13 15:12:23 -07:00
|
|
|
// LOW_AUDIO_USECS above) we'll also increase this value to ensure it's not
|
2010-11-28 12:06:38 -08:00
|
|
|
// less than the low audio threshold.
|
2011-04-13 15:12:23 -07:00
|
|
|
const PRInt64 AMPLE_AUDIO_USECS = 1000000;
|
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
|
2011-09-26 20:31:18 -07:00
|
|
|
// hardware when the audio stream contains missing frames and we're
|
2010-08-12 19:28:15 -07:00
|
|
|
// 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-04-13 15:12:23 -07:00
|
|
|
static const int AUDIO_DURATION_USECS = 40000;
|
2010-05-30 21:02:00 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// If we increase our "low audio threshold" (see LOW_AUDIO_USECS above), we
|
2010-11-28 12:06:38 -08:00
|
|
|
// 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.
|
2011-04-13 15:12:23 -07:00
|
|
|
static const PRInt64 LOW_DATA_THRESHOLD_USECS = 5000000;
|
2010-11-28 12:06:38 -08:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// LOW_DATA_THRESHOLD_USECS needs to be greater than AMPLE_AUDIO_USECS, otherwise
|
2011-03-23 15:28:57 -07:00
|
|
|
// the skip-to-keyframe logic can activate when we're running low on data.
|
2011-04-13 15:12:23 -07:00
|
|
|
PR_STATIC_ASSERT(LOW_DATA_THRESHOLD_USECS > AMPLE_AUDIO_USECS);
|
2010-11-28 12:06:38 -08:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// Amount of excess usecs of data to add in to the "should we buffer" calculation.
|
|
|
|
static const PRUint32 EXHAUSTED_DATA_MARGIN_USECS = 60000;
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// If we enter buffering within QUICK_BUFFER_THRESHOLD_USECS seconds of starting
|
2011-03-23 15:28:57 -07:00
|
|
|
// 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.
|
2011-04-13 15:12:23 -07:00
|
|
|
static const PRUint32 QUICK_BUFFER_THRESHOLD_USECS = 2000000;
|
2011-03-23 15:28:57 -07:00
|
|
|
|
|
|
|
// If we're quick buffering, we'll remain in buffering mode while we have less than
|
2011-04-13 15:12:23 -07:00
|
|
|
// QUICK_BUFFERING_LOW_DATA_USECS of decoded data available.
|
|
|
|
static const PRUint32 QUICK_BUFFERING_LOW_DATA_USECS = 1000000;
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// If QUICK_BUFFERING_LOW_DATA_USECS is > AMPLE_AUDIO_USECS, we won't exit
|
2011-03-23 15:28:57 -07:00
|
|
|
// quick buffering in a timely fashion, as the decode pauses when it
|
2011-04-13 15:12:23 -07:00
|
|
|
// reaches AMPLE_AUDIO_USECS decoded data, and thus we'll never reach
|
|
|
|
// QUICK_BUFFERING_LOW_DATA_USECS.
|
|
|
|
PR_STATIC_ASSERT(QUICK_BUFFERING_LOW_DATA_USECS <= AMPLE_AUDIO_USECS);
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
static TimeDuration UsecsToDuration(PRInt64 aUsecs) {
|
|
|
|
return TimeDuration::FromMilliseconds(static_cast<double>(aUsecs) / USECS_PER_MS);
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
static PRInt64 DurationToUsecs(TimeDuration aDuration) {
|
|
|
|
return static_cast<PRInt64>(aDuration.ToSeconds() * USECS_PER_S);
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
class nsAudioMetadataEventRunner : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsBuiltinDecoder> mDecoder;
|
|
|
|
public:
|
|
|
|
nsAudioMetadataEventRunner(nsBuiltinDecoder* aDecoder, PRUint32 aChannels,
|
2011-04-11 14:15:45 -07:00
|
|
|
PRUint32 aRate) :
|
2010-08-25 06:10:00 -07:00
|
|
|
mDecoder(aDecoder),
|
|
|
|
mChannels(aChannels),
|
2011-04-11 14:15:45 -07:00
|
|
|
mRate(aRate)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2011-04-11 14:15:45 -07:00
|
|
|
mDecoder->MetadataLoaded(mChannels, mRate);
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PRUint32 mChannels;
|
|
|
|
const PRUint32 mRate;
|
|
|
|
};
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
// Shuts down a thread asynchronously.
|
|
|
|
class ShutdownThreadEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {}
|
|
|
|
~ShutdownThreadEvent() {}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mThread->Shutdown();
|
|
|
|
mThread = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2011-11-07 17:38:17 -08:00
|
|
|
// Owns the global state machine thread and counts of
|
|
|
|
// state machine and decoder threads. There should
|
|
|
|
// only be one instance of this class.
|
|
|
|
class StateMachineTracker
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
StateMachineTracker() :
|
|
|
|
mMonitor("media.statemachinetracker"),
|
|
|
|
mStateMachineCount(0),
|
|
|
|
mDecodeThreadCount(0),
|
|
|
|
mStateMachineThread(nsnull)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(StateMachineTracker);
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
}
|
|
|
|
|
|
|
|
~StateMachineTracker()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(StateMachineTracker);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Access singleton instance. This is initially called on the main
|
|
|
|
// thread in the nsBuiltinDecoderStateMachine constructor resulting
|
|
|
|
// in the global object being created lazily. Non-main thread
|
|
|
|
// access always occurs after this and uses the monitor to
|
|
|
|
// safely access the decode thread counts.
|
|
|
|
static StateMachineTracker& Instance();
|
|
|
|
|
|
|
|
// Instantiate the global state machine thread if required.
|
|
|
|
// Call on main thread only.
|
|
|
|
void EnsureGlobalStateMachine();
|
|
|
|
|
|
|
|
// Destroy global state machine thread if required.
|
|
|
|
// Call on main thread only.
|
|
|
|
void CleanupGlobalStateMachine();
|
|
|
|
|
|
|
|
// Return the global state machine thread. Call from any thread.
|
|
|
|
nsIThread* GetGlobalStateMachineThread()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
NS_ASSERTION(mStateMachineThread, "Should have non-null state machine thread!");
|
|
|
|
return mStateMachineThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum number of active decode threads allowed. When more
|
|
|
|
// than this number are active the thread creation will fail.
|
|
|
|
static const PRUint32 MAX_DECODE_THREADS = 25;
|
|
|
|
|
|
|
|
// Returns the number of active decode threads.
|
|
|
|
// Call on any thread. Holds the internal monitor so don't
|
|
|
|
// call with any other monitor held to avoid deadlock.
|
|
|
|
PRUint32 GetDecodeThreadCount();
|
|
|
|
|
|
|
|
// Keep track of the fact that a decode thread was created.
|
|
|
|
// Call on any thread. Holds the internal monitor so don't
|
|
|
|
// call with any other monitor held to avoid deadlock.
|
|
|
|
void NoteDecodeThreadCreated();
|
|
|
|
|
|
|
|
// Keep track of the fact that a decode thread was destroyed.
|
|
|
|
// Call on any thread. Holds the internal monitor so don't
|
|
|
|
// call with any other monitor held to avoid deadlock.
|
|
|
|
void NoteDecodeThreadDestroyed();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Holds global instance of StateMachineTracker.
|
|
|
|
// Writable on main thread only.
|
|
|
|
static StateMachineTracker* mInstance;
|
|
|
|
|
|
|
|
// Reentrant monitor that must be obtained to access
|
|
|
|
// the decode thread count member and methods.
|
|
|
|
ReentrantMonitor mMonitor;
|
|
|
|
|
|
|
|
// Number of instances of nsBuiltinDecoderStateMachine
|
|
|
|
// that are currently instantiated. Access on the
|
|
|
|
// main thread only.
|
|
|
|
PRUint32 mStateMachineCount;
|
|
|
|
|
|
|
|
// Number of instances of decoder threads that are
|
|
|
|
// currently instantiated. Access only with the
|
|
|
|
// mMonitor lock held. Can be used from any thread.
|
|
|
|
PRUint32 mDecodeThreadCount;
|
|
|
|
|
|
|
|
// Global state machine thread. Write on the main thread
|
|
|
|
// only, read from the decoder threads. Synchronized via
|
|
|
|
// the mMonitor.
|
|
|
|
nsIThread* mStateMachineThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
StateMachineTracker* StateMachineTracker::mInstance = nsnull;
|
|
|
|
|
|
|
|
StateMachineTracker& StateMachineTracker::Instance()
|
|
|
|
{
|
|
|
|
if (!mInstance) {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
mInstance = new StateMachineTracker();
|
|
|
|
}
|
|
|
|
return *mInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachineTracker::EnsureGlobalStateMachine()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
if (mStateMachineCount == 0) {
|
|
|
|
NS_ASSERTION(!mStateMachineThread, "Should have null state machine thread!");
|
|
|
|
nsresult res = NS_NewThread(&mStateMachineThread,
|
|
|
|
nsnull);
|
|
|
|
NS_ABORT_IF_FALSE(NS_SUCCEEDED(res), "Can't create media state machine thread");
|
|
|
|
}
|
|
|
|
mStateMachineCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachineTracker::CleanupGlobalStateMachine()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
NS_ABORT_IF_FALSE(mStateMachineCount > 0,
|
|
|
|
"State machine ref count must be > 0");
|
|
|
|
mStateMachineCount--;
|
|
|
|
if (mStateMachineCount == 0) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("Destroying media state machine thread"));
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
nsCOMPtr<nsIRunnable> event = new ShutdownThreadEvent(mStateMachineThread);
|
|
|
|
NS_RELEASE(mStateMachineThread);
|
|
|
|
mStateMachineThread = nsnull;
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
|
|
|
|
NS_ASSERTION(mDecodeThreadCount == 0, "Decode thread count must be zero.");
|
|
|
|
mInstance = nsnull;
|
|
|
|
}
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachineTracker::NoteDecodeThreadCreated()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
++mDecodeThreadCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateMachineTracker::NoteDecodeThreadDestroyed()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
--mDecodeThreadCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 StateMachineTracker::GetDecodeThreadCount()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
return mDecodeThreadCount;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
nsBuiltinDecoderStateMachine::nsBuiltinDecoderStateMachine(nsBuiltinDecoder* aDecoder,
|
2011-09-26 17:25:41 -07:00
|
|
|
nsBuiltinDecoderReader* aReader,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aRealTime) :
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecoder(aDecoder),
|
|
|
|
mState(DECODER_STATE_DECODING_METADATA),
|
|
|
|
mCbCrSize(0),
|
|
|
|
mPlayDuration(0),
|
|
|
|
mStartTime(-1),
|
|
|
|
mEndTime(-1),
|
|
|
|
mSeekTime(0),
|
2011-08-24 16:42:23 -07:00
|
|
|
mFragmentEndTime(-1),
|
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),
|
2011-09-29 16:34:37 -07:00
|
|
|
mSeekable(true),
|
|
|
|
mPositionChangeQueued(false),
|
|
|
|
mAudioCompleted(false),
|
|
|
|
mGotDurationFromMetaData(false),
|
|
|
|
mStopDecodeThread(true),
|
|
|
|
mDecodeThreadIdle(false),
|
|
|
|
mStopAudioThread(true),
|
|
|
|
mQuickBuffering(false),
|
|
|
|
mIsRunning(false),
|
|
|
|
mRunAgain(false),
|
|
|
|
mDispatchedRunEvent(false),
|
|
|
|
mDecodeThreadWaiting(false),
|
|
|
|
mRealTime(aRealTime),
|
|
|
|
mEventManager(aDecoder)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2010-05-05 19:31:02 -07:00
|
|
|
MOZ_COUNT_CTOR(nsBuiltinDecoderStateMachine);
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2011-11-07 17:38:17 -08:00
|
|
|
|
|
|
|
StateMachineTracker::Instance().EnsureGlobalStateMachine();
|
2011-09-26 17:25:41 -07:00
|
|
|
|
|
|
|
// only enable realtime mode when "media.realtime_decoder.enabled" is true.
|
2011-09-28 23:19:26 -07:00
|
|
|
if (Preferences::GetBool("media.realtime_decoder.enabled", false) == false)
|
2011-09-29 16:34:37 -07:00
|
|
|
mRealTime = false;
|
2011-09-26 17:25:41 -07:00
|
|
|
|
|
|
|
mBufferingWait = mRealTime ? 0 : BUFFERING_WAIT;
|
|
|
|
mLowDataThresholdUsecs = mRealTime ? 0 : LOW_DATA_THRESHOLD_USECS;
|
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
|
|
|
{
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2010-05-05 19:31:02 -07:00
|
|
|
MOZ_COUNT_DTOR(nsBuiltinDecoderStateMachine);
|
2011-07-11 20:39:32 -07:00
|
|
|
if (mTimer)
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nsnull;
|
2011-11-07 17:38:17 -08:00
|
|
|
|
|
|
|
StateMachineTracker::Instance().CleanupGlobalStateMachine();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::HasFutureAudio() const {
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().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 &&
|
2011-04-13 15:12:23 -07:00
|
|
|
(AudioDecodedUsecs() > LOW_AUDIO_USECS || mReader->mAudioQueue.IsFinished());
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::HaveNextFrameData() const {
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-09-14 16:24:47 -07:00
|
|
|
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.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-01-12 17:06:15 -08:00
|
|
|
PRInt64 audioDecoded = mReader->mAudioQueue.Duration();
|
|
|
|
if (mAudioEndTime != -1) {
|
|
|
|
audioDecoded += mAudioEndTime - GetMediaTime();
|
|
|
|
}
|
|
|
|
return audioDecoded;
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::DecodeThreadRun()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
if (mState == DECODER_STATE_DECODING_METADATA) {
|
|
|
|
if (NS_FAILED(DecodeMetadata())) {
|
|
|
|
NS_ASSERTION(mState == DECODER_STATE_SHUTDOWN,
|
|
|
|
"Should be in shutdown state if metadata loading fails.");
|
|
|
|
LOG(PR_LOG_DEBUG, ("Decode metadata failed, shutting down decode thread"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:37 -07:00
|
|
|
while (mState != DECODER_STATE_SHUTDOWN &&
|
|
|
|
mState != DECODER_STATE_COMPLETED &&
|
|
|
|
!mStopDecodeThread)
|
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
if (mState == DECODER_STATE_DECODING || mState == DECODER_STATE_BUFFERING) {
|
|
|
|
DecodeLoop();
|
|
|
|
} else if (mState == DECODER_STATE_SEEKING) {
|
|
|
|
DecodeSeek();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecodeThreadIdle = true;
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Decode thread finished", mDecoder.get()));
|
2011-07-11 20:39:25 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::DecodeLoop()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:37 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Start DecodeLoop()", mDecoder.get()));
|
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// We want to "pump" the decode until we've got a few frames decoded
|
2010-04-01 20:03:07 -07:00
|
|
|
// before we consider whether decode is falling behind.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool audioPump = true;
|
|
|
|
bool videoPump = true;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// 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
|
2011-09-29 16:34:37 -07:00
|
|
|
// playback position. skipToNextKeyframe is true if we're currently
|
2010-04-01 20:03:07 -07:00
|
|
|
// skipping up to the next keyframe.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool skipToNextKeyframe = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Once we've decoded more than videoPumpThreshold video frames, we'll
|
|
|
|
// no longer be considered to be "pumping video".
|
2011-09-26 17:25:41 -07:00
|
|
|
const unsigned videoPumpThreshold = mRealTime ? 0 : AMPLE_VIDEO_FRAMES / 2;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
// After the audio decode fills with more than audioPumpThreshold usecs
|
2010-04-01 20:03:07 -07:00
|
|
|
// of decoded audio, we'll start to check whether the audio or video decode
|
|
|
|
// is falling behind.
|
2011-09-26 17:25:41 -07:00
|
|
|
const unsigned audioPumpThreshold = mRealTime ? 0 : LOW_AUDIO_USECS * 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.
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 lowAudioThreshold = LOW_AUDIO_USECS;
|
2010-11-28 12:06:38 -08:00
|
|
|
|
|
|
|
// 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!).
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 ampleAudioThreshold = AMPLE_AUDIO_USECS;
|
2010-11-28 12:06:38 -08:00
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
MediaQueue<VideoData>& videoQueue = mReader->mVideoQueue;
|
2011-08-15 22:19:51 -07:00
|
|
|
MediaQueue<AudioData>& audioQueue = mReader->mAudioQueue;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
// Main decode loop.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool videoPlaying = HasVideo();
|
|
|
|
bool audioPlaying = HasAudio();
|
2011-07-11 20:39:25 -07:00
|
|
|
while ((mState == DECODER_STATE_DECODING || mState == DECODER_STATE_BUFFERING) &&
|
2011-07-11 20:39:10 -07:00
|
|
|
!mStopDecodeThread &&
|
2011-01-12 17:06:15 -08:00
|
|
|
(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)
|
|
|
|
{
|
2011-09-29 16:34:37 -07:00
|
|
|
videoPump = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
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.
|
2011-04-13 15:12:23 -07:00
|
|
|
if (audioPump && GetDecodedAudioDuration() >= audioPumpThreshold) {
|
2011-09-29 16:34:37 -07:00
|
|
|
audioPump = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
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
|
|
|
{
|
2011-09-29 16:34:37 -07:00
|
|
|
skipToNextKeyframe = true;
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Skipping video decode to the next keyframe", mDecoder.get()));
|
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();
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
2011-01-12 17:06:15 -08:00
|
|
|
TimeStamp start = TimeStamp::Now();
|
|
|
|
videoPlaying = mReader->DecodeVideoFrame(skipToNextKeyframe, currentTime);
|
|
|
|
decodeTime = TimeStamp::Now() - start;
|
|
|
|
}
|
2011-04-13 15:12:23 -07:00
|
|
|
if (THRESHOLD_FACTOR * DurationToUsecs(decodeTime) > lowAudioThreshold &&
|
2011-03-23 15:28:57 -07:00
|
|
|
!HasLowUndecodedData())
|
2010-11-28 12:06:38 -08:00
|
|
|
{
|
|
|
|
lowAudioThreshold =
|
2011-04-13 15:12:23 -07:00
|
|
|
NS_MIN(THRESHOLD_FACTOR * DurationToUsecs(decodeTime), AMPLE_AUDIO_USECS);
|
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))
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
2010-05-05 19:31:02 -07:00
|
|
|
audioPlaying = mReader->DecodeAudioData();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-09-26 17:25:41 -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.
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2011-01-12 17:06:15 -08:00
|
|
|
|
2011-06-30 16:00:22 -07:00
|
|
|
// The ready state can change when we've decoded data, so update the
|
|
|
|
// ready state, so that DOM events can fire.
|
|
|
|
UpdateReadyState();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
if ((mState == DECODER_STATE_DECODING || mState == DECODER_STATE_BUFFERING) &&
|
2011-07-11 20:39:10 -07:00
|
|
|
!mStopDecodeThread &&
|
2011-06-30 16:00:22 -07:00
|
|
|
(videoPlaying || audioPlaying) &&
|
2011-01-12 17:06:15 -08:00
|
|
|
(!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
|
2011-08-15 22:19:51 -07:00
|
|
|
// it pops AudioData off the audio queue. So if the audio push thread pops
|
|
|
|
// the last AudioData off the audio queue right after that queue reported
|
2011-01-12 17:06:15 -08:00
|
|
|
// 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.
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecodeThreadWaiting = true;
|
2011-07-11 20:39:37 -07:00
|
|
|
if (mDecoder->GetState() != nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
|
|
|
// We're not playing, and the decode is about to wait. This means
|
|
|
|
// the decode thread may not be needed in future. Signal the state
|
|
|
|
// machine thread to run, so it can decide whether to shutdown the
|
|
|
|
// decode thread.
|
|
|
|
ScheduleStateMachine();
|
|
|
|
}
|
2011-07-11 20:39:25 -07:00
|
|
|
mDecoder->GetReentrantMonitor().Wait();
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecodeThreadWaiting = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:15 -08:00
|
|
|
} // End decode loop.
|
|
|
|
|
2011-07-11 20:39:10 -07:00
|
|
|
if (!mStopDecodeThread &&
|
2011-01-12 17:06:15 -08:00
|
|
|
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;
|
2011-07-11 20:39:32 -07:00
|
|
|
ScheduleStateMachine();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-01-12 17:06:15 -08:00
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Exiting DecodeLoop", mDecoder.get()));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::IsPlaying()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
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.");
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Begun audio thread/loop", mDecoder.get()));
|
2010-12-19 11:05:40 -08:00
|
|
|
PRInt64 audioDuration = 0;
|
2010-08-12 19:28:15 -07:00
|
|
|
PRInt64 audioStartTime = -1;
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 framesWritten = 0;
|
2010-08-12 19:28:15 -07:00
|
|
|
PRUint32 channels, rate;
|
2011-01-16 19:03:00 -08:00
|
|
|
double volume = -1;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool setVolume;
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt32 minWriteFrames = -1;
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-09-29 16:34:37 -07:00
|
|
|
mAudioCompleted = false;
|
2010-08-12 19:28:15 -07:00
|
|
|
audioStartTime = mAudioStartTime;
|
2011-03-23 20:53:03 -07:00
|
|
|
channels = mInfo.mAudioChannels;
|
|
|
|
rate = mInfo.mAudioRate;
|
2010-08-12 19:28:15 -07:00
|
|
|
NS_ASSERTION(audioStartTime != -1, "Should have audio start time by now");
|
2011-07-11 20:40:38 -07:00
|
|
|
}
|
2011-07-11 20:39:30 -07:00
|
|
|
|
2011-07-11 20:40:38 -07:00
|
|
|
// It is unsafe to call some methods of nsAudioStream with the decoder
|
|
|
|
// monitor held, as on Android those methods do a synchronous dispatch to
|
|
|
|
// the main thread. If the audio thread holds the decoder monitor while
|
|
|
|
// it does a synchronous dispatch to the main thread, we can get deadlocks
|
|
|
|
// if the main thread tries to acquire the decoder monitor before the
|
|
|
|
// dispatched event has finished (or even started!) running. Methods which
|
|
|
|
// are unsafe to call with the decoder monitor held are documented as such
|
|
|
|
// in nsAudioStream.h.
|
|
|
|
nsRefPtr<nsAudioStream> audioStream = nsAudioStream::AllocateStream();
|
2011-08-15 22:19:51 -07:00
|
|
|
audioStream->Init(channels, rate, MOZ_AUDIO_DATA_FORMAT);
|
2011-07-11 20:40:38 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
// We must hold the monitor while setting mAudioStream or whenever we query
|
|
|
|
// the playback position off the audio thread. This ensures the audio stream
|
|
|
|
// is always alive when we use it off the audio thread. Note that querying
|
|
|
|
// the playback position does not do a synchronous dispatch to the main
|
|
|
|
// thread, so it's safe to call with the decoder monitor held.
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
mAudioStream = audioStream;
|
2011-07-11 20:39:30 -07:00
|
|
|
volume = mVolume;
|
|
|
|
mAudioStream->SetVolume(volume);
|
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.
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(mState != DECODER_STATE_DECODING_METADATA,
|
|
|
|
"Should have meta data before audio started playing.");
|
|
|
|
while (mState != DECODER_STATE_SHUTDOWN &&
|
2011-07-11 20:39:10 -07:00
|
|
|
!mStopAudioThread &&
|
2010-04-01 20:03:07 -07:00
|
|
|
(!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())))
|
|
|
|
{
|
2011-07-11 20:39:30 -07:00
|
|
|
if (!IsPlaying() && !mAudioStream->IsPaused()) {
|
|
|
|
mAudioStream->Pause();
|
|
|
|
}
|
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 ||
|
2011-07-11 20:39:10 -07:00
|
|
|
mStopAudioThread ||
|
2010-04-01 20:03:07 -07:00
|
|
|
mReader->mAudioQueue.AtEndOfStream())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-09-05 19:14:50 -07:00
|
|
|
|
2011-07-11 20:39:30 -07:00
|
|
|
// We only want to go to the expense of changing the volume if
|
|
|
|
// the volume has changed.
|
2010-09-05 19:14:50 -07:00
|
|
|
setVolume = volume != mVolume;
|
|
|
|
volume = mVolume;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:40:38 -07:00
|
|
|
// Note audio stream IsPaused() does not do synchronous dispatch to the
|
|
|
|
// main thread on Android, so can be called safely with the decoder
|
|
|
|
// monitor held.
|
2011-07-11 20:39:30 -07:00
|
|
|
if (IsPlaying() && mAudioStream->IsPaused()) {
|
|
|
|
mAudioStream->Resume();
|
2010-09-05 19:14:50 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-11 20:39:30 -07:00
|
|
|
|
|
|
|
if (setVolume) {
|
|
|
|
mAudioStream->SetVolume(volume);
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
if (minWriteFrames == -1) {
|
|
|
|
minWriteFrames = mAudioStream->GetMinWriteSize();
|
2011-07-11 20:39:30 -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");
|
2011-09-26 20:31:18 -07:00
|
|
|
// See if there's a gap in the audio. If there is, push silence into the
|
|
|
|
// audio hardware, so we can play across the gap.
|
2011-08-15 22:19:51 -07:00
|
|
|
const AudioData* s = mReader->mAudioQueue.PeekFront();
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Calculate the number of frames that have been pushed onto the audio
|
2010-08-12 19:28:15 -07:00
|
|
|
// hardware.
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 playedFrames = 0;
|
|
|
|
if (!UsecsToFrames(audioStartTime, rate, playedFrames)) {
|
|
|
|
NS_WARNING("Int overflow converting playedFrames");
|
2010-08-12 19:28:15 -07:00
|
|
|
break;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
if (!AddOverflow(playedFrames, audioDuration, playedFrames)) {
|
|
|
|
NS_WARNING("Int overflow adding playedFrames");
|
2010-08-12 19:28:15 -07:00
|
|
|
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;
|
2011-09-26 20:31:18 -07:00
|
|
|
if (!UsecsToFrames(s->mTime, rate, sampleTime)) {
|
2010-08-12 19:28:15 -07:00
|
|
|
NS_WARNING("Int overflow converting sampleTime");
|
|
|
|
break;
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
PRInt64 missingFrames = 0;
|
|
|
|
if (!AddOverflow(sampleTime, -playedFrames, missingFrames)) {
|
|
|
|
NS_WARNING("Int overflow adding missingFrames");
|
2010-08-12 19:28:15 -07:00
|
|
|
break;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
if (missingFrames > 0) {
|
2011-08-15 22:19:51 -07:00
|
|
|
// The next audio chunk begins some time after the end of the last chunk
|
|
|
|
// we pushed to the audio hardware. We must push silence into the audio
|
|
|
|
// hardware so that the next audio chunk begins playback at the correct
|
2010-08-12 19:28:15 -07:00
|
|
|
// time.
|
2011-09-26 20:31:18 -07:00
|
|
|
missingFrames = NS_MIN(static_cast<PRInt64>(PR_UINT32_MAX), missingFrames);
|
|
|
|
framesWritten = PlaySilence(static_cast<PRUint32>(missingFrames),
|
|
|
|
channels, playedFrames);
|
2010-08-12 19:28:15 -07:00
|
|
|
} else {
|
2011-09-26 20:31:18 -07:00
|
|
|
framesWritten = PlayFromAudioQueue(sampleTime, channels);
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
audioDuration += framesWritten;
|
2010-05-12 17:59:42 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 playedUsecs;
|
2011-09-26 20:31:18 -07:00
|
|
|
if (!FramesToUsecs(audioDuration, rate, playedUsecs)) {
|
2011-04-13 15:12:23 -07:00
|
|
|
NS_WARNING("Int overflow calculating playedUsecs");
|
2010-08-12 19:28:15 -07:00
|
|
|
break;
|
|
|
|
}
|
2011-04-13 15:12:23 -07:00
|
|
|
if (!AddOverflow(audioStartTime, playedUsecs, mAudioEndTime)) {
|
2010-08-12 19:28:15 -07:00
|
|
|
NS_WARNING("Int overflow calculating audio end time");
|
|
|
|
break;
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 17:59:42 -07:00
|
|
|
if (mReader->mAudioQueue.AtEndOfStream() &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN &&
|
2011-07-11 20:39:10 -07:00
|
|
|
!mStopAudioThread)
|
2010-05-12 17:59:42 -07:00
|
|
|
{
|
2011-09-26 20:31:18 -07:00
|
|
|
// Last frame pushed to audio hardware, wait for the audio to finish,
|
2010-05-12 17:59:42 -07:00
|
|
|
// before the audio thread terminates.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool seeking = false;
|
2011-07-11 20:39:30 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-09-26 20:31:18 -07:00
|
|
|
if (framesWritten < minWriteFrames) {
|
|
|
|
// We've not written minWriteFrames in the last write, the audio
|
2011-08-15 20:32:10 -07:00
|
|
|
// may not start playing. Write silence to ensure we've got enough
|
2011-09-26 20:31:18 -07:00
|
|
|
// written to start playback.
|
|
|
|
PRInt64 minToWrite = minWriteFrames - framesWritten;
|
|
|
|
if (minToWrite < PR_UINT32_MAX / channels) {
|
2011-08-15 20:32:10 -07:00
|
|
|
// Write silence manually rather than using PlaySilence(), so that
|
2011-09-26 20:31:18 -07:00
|
|
|
// the AudioAPI doesn't get a copy of the audio frames.
|
|
|
|
PRUint32 numSamples = minToWrite * channels;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buf(new AudioDataValue[numSamples]);
|
|
|
|
memset(buf.get(), 0, numSamples * sizeof(AudioDataValue));
|
|
|
|
mAudioStream->Write(buf, minToWrite);
|
2011-08-15 20:32:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-01 17:31:53 -08:00
|
|
|
PRInt64 oldPosition = -1;
|
2011-07-11 20:39:30 -07:00
|
|
|
PRInt64 position = GetMediaTime();
|
|
|
|
while (oldPosition != position &&
|
|
|
|
mAudioEndTime - position > 0 &&
|
|
|
|
mState != DECODER_STATE_SEEKING &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN)
|
2011-02-01 17:31:53 -08:00
|
|
|
{
|
2011-07-11 20:39:30 -07:00
|
|
|
const PRInt64 DRAIN_BLOCK_USECS = 100000;
|
|
|
|
Wait(NS_MIN(mAudioEndTime - position, DRAIN_BLOCK_USECS));
|
|
|
|
oldPosition = position;
|
|
|
|
position = GetMediaTime();
|
2011-02-01 17:31:53 -08:00
|
|
|
}
|
2011-07-11 20:39:30 -07:00
|
|
|
seeking = mState == DECODER_STATE_SEEKING;
|
|
|
|
}
|
2011-02-01 17:31:53 -08:00
|
|
|
|
2011-07-11 20:39:30 -07:00
|
|
|
if (!seeking && !mAudioStream->IsPaused()) {
|
|
|
|
mAudioStream->Drain();
|
2011-09-26 20:31:18 -07:00
|
|
|
// Fire one last event for any extra frames that didn't fill a framebuffer.
|
2011-07-11 20:39:30 -07:00
|
|
|
mEventManager.Drain(mAudioEndTime);
|
2010-05-12 17:59:42 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Reached audio stream end.", mDecoder.get()));
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:40:38 -07:00
|
|
|
// Must hold lock while anulling the audio stream to prevent
|
2011-07-11 20:39:30 -07:00
|
|
|
// state machine thread trying to use it while we're destroying it.
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-07-11 20:39:30 -07:00
|
|
|
mAudioStream = nsnull;
|
|
|
|
mEventManager.Clear();
|
2011-09-29 16:34:37 -07:00
|
|
|
mAudioCompleted = true;
|
2010-05-12 17:59:42 -07:00
|
|
|
UpdateReadyState();
|
2011-07-11 20:39:32 -07:00
|
|
|
// Kick the decode thread; it may be sleeping waiting for this to finish.
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:40:38 -07:00
|
|
|
|
|
|
|
// Must not hold the decoder monitor while we shutdown the audio stream, as
|
|
|
|
// it makes a synchronous dispatch on Android.
|
|
|
|
audioStream->Shutdown();
|
|
|
|
audioStream = nsnull;
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Audio stream finished playing, audio thread exit", mDecoder.get()));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 nsBuiltinDecoderStateMachine::PlaySilence(PRUint32 aFrames,
|
2010-08-25 06:10:00 -07:00
|
|
|
PRUint32 aChannels,
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint64 aFrameOffset)
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2010-08-12 19:28:15 -07:00
|
|
|
{
|
2011-07-11 20:39:30 -07:00
|
|
|
NS_ASSERTION(OnAudioThread(), "Only call on audio thread.");
|
|
|
|
NS_ASSERTION(!mAudioStream->IsPaused(), "Don't play when paused");
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 maxFrames = SILENCE_BYTES_CHUNK / aChannels / sizeof(AudioDataValue);
|
|
|
|
PRUint32 frames = NS_MIN(aFrames, maxFrames);
|
|
|
|
PRUint32 numSamples = frames * aChannels;
|
|
|
|
nsAutoArrayPtr<AudioDataValue> buf(new AudioDataValue[numSamples]);
|
|
|
|
memset(buf.get(), 0, numSamples * sizeof(AudioDataValue));
|
|
|
|
mAudioStream->Write(buf, frames);
|
2010-08-25 06:10:00 -07:00
|
|
|
// Dispatch events to the DOM for the audio just written.
|
2011-09-26 20:31:18 -07:00
|
|
|
mEventManager.QueueWrittenAudioData(buf.get(), frames * aChannels,
|
|
|
|
(aFrameOffset + frames) * aChannels);
|
|
|
|
return frames;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 nsBuiltinDecoderStateMachine::PlayFromAudioQueue(PRUint64 aFrameOffset,
|
2010-08-25 06:10:00 -07:00
|
|
|
PRUint32 aChannels)
|
2010-08-12 19:28:15 -07:00
|
|
|
{
|
2011-07-11 20:39:30 -07:00
|
|
|
NS_ASSERTION(OnAudioThread(), "Only call on audio thread.");
|
|
|
|
NS_ASSERTION(!mAudioStream->IsPaused(), "Don't play when paused");
|
2011-09-26 20:31:18 -07:00
|
|
|
nsAutoPtr<AudioData> audio(mReader->mAudioQueue.PopFront());
|
2010-08-12 19:28:15 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-08-12 19:28:15 -07:00
|
|
|
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.
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
PRInt64 offset = -1;
|
2011-09-26 20:31:18 -07:00
|
|
|
PRUint32 frames = 0;
|
2011-07-11 20:39:30 -07:00
|
|
|
// 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
|
|
|
|
// monitor while paused; we would block, and the state machine won't be
|
|
|
|
// able to acquire the audio monitor in order to resume or destroy the
|
|
|
|
// audio stream.
|
|
|
|
if (!mAudioStream->IsPaused()) {
|
2011-09-26 20:31:18 -07:00
|
|
|
mAudioStream->Write(audio->mAudioData,
|
|
|
|
audio->mFrames);
|
2011-07-11 20:39:30 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
offset = audio->mOffset;
|
|
|
|
frames = audio->mFrames;
|
2011-07-11 20:39:30 -07:00
|
|
|
|
|
|
|
// Dispatch events to the DOM for the audio just written.
|
2011-09-26 20:31:18 -07:00
|
|
|
mEventManager.QueueWrittenAudioData(audio->mAudioData.get(),
|
|
|
|
audio->mFrames * aChannels,
|
|
|
|
(aFrameOffset + frames) * aChannels);
|
2011-07-11 20:39:30 -07:00
|
|
|
} else {
|
2011-09-26 20:31:18 -07:00
|
|
|
mReader->mAudioQueue.PushFront(audio);
|
|
|
|
audio.forget();
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
if (offset != -1) {
|
|
|
|
mDecoder->UpdatePlaybackOffset(offset);
|
|
|
|
}
|
2011-09-26 20:31:18 -07:00
|
|
|
return frames;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:30 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StopPlayback()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:37 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p StopPlayback()", mDecoder.get()));
|
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread() || OnDecodeThread(),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
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()) {
|
2011-04-13 15:12:23 -07:00
|
|
|
mPlayDuration += DurationToUsecs(TimeStamp::Now() - mPlayStartTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
mPlayStartTime = TimeStamp();
|
|
|
|
}
|
2011-07-11 20:39:37 -07:00
|
|
|
// Notify the audio thread, so that it notices that we've stopped playing,
|
|
|
|
// so it can pause audio playback.
|
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2011-07-11 20:39:30 -07:00
|
|
|
NS_ASSERTION(!IsPlaying(), "Should report not playing at end of StopPlayback()");
|
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
|
|
|
{
|
2011-07-11 20:39:37 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p StartPlayback()", mDecoder.get()));
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(!IsPlaying(), "Shouldn't be playing when StartPlayback() is called");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p StartPlayback", mDecoder.get()));
|
2011-01-17 16:53:18 -08:00
|
|
|
mDecoder->mPlaybackStatistics.Start(TimeStamp::Now());
|
2010-04-01 20:03:07 -07:00
|
|
|
mPlayStartTime = TimeStamp::Now();
|
2011-07-11 20:39:37 -07:00
|
|
|
|
2011-07-11 20:39:30 -07:00
|
|
|
NS_ASSERTION(IsPlaying(), "Should report playing by end of StartPlayback()");
|
2011-07-11 20:39:37 -07:00
|
|
|
if (NS_FAILED(StartAudioThread())) {
|
|
|
|
NS_WARNING("Failed to create audio thread");
|
|
|
|
}
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-01-31 18:57:13 -08:00
|
|
|
void nsBuiltinDecoderStateMachine::UpdatePlaybackPositionInternal(PRInt64 aTime)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread() || OnDecodeThread(),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on state machine thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool fragmentEnded = mFragmentEndTime >= 0 && GetMediaTime() >= mFragmentEndTime;
|
2011-08-24 16:42:23 -07:00
|
|
|
if (!mPositionChangeQueued || fragmentEnded) {
|
2011-09-29 16:34:37 -07:00
|
|
|
mPositionChangeQueued = true;
|
2010-04-01 20:03:07 -07:00
|
|
|
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());
|
2011-08-24 16:42:23 -07:00
|
|
|
|
|
|
|
if (fragmentEnded) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
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.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mPositionChangeQueued = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
nsHTMLMediaElement::NextFrameStatus nsBuiltinDecoderStateMachine::GetNextFrameStatus()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-04-01 20:03:07 -07:00
|
|
|
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.");
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-09-05 19:14:50 -07:00
|
|
|
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() ||
|
2011-07-11 20:39:23 -07:00
|
|
|
OnStateMachineThread() ||
|
2011-03-23 15:28:57 -07:00
|
|
|
OnDecodeThread(),
|
|
|
|
"Should be on main, decode, or state machine thread.");
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
return static_cast<double>(mCurrentFrameTime) / static_cast<double>(USECS_PER_S);
|
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
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread() || OnDecodeThread(),
|
|
|
|
"Should be on main or decode thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-05-08 14:10:28 -07:00
|
|
|
if (aDuration == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mStartTime != -1) {
|
|
|
|
mEndTime = mStartTime + aDuration;
|
|
|
|
} else {
|
|
|
|
mStartTime = 0;
|
|
|
|
mEndTime = aDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-08 14:10:28 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::SetEndTime(PRInt64 aEndTime)
|
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread");
|
2011-05-08 14:10:28 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
mEndTime = aEndTime;
|
|
|
|
}
|
|
|
|
|
2011-08-24 16:42:23 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::SetFragmentEndTime(PRInt64 aEndTime)
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
mFragmentEndTime = aEndTime < 0 ? aEndTime : aEndTime + mStartTime;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::SetSeekable(bool aSeekable)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
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.
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
// Change state before issuing shutdown request to threads so those
|
|
|
|
// threads can start exiting cleanly during the Shutdown call.
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state to SHUTDOWN", mDecoder.get()));
|
2011-07-11 20:39:32 -07:00
|
|
|
ScheduleStateMachine();
|
2010-04-01 20:03:07 -07:00
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StartDecoding()
|
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread() || OnDecodeThread(),
|
|
|
|
"Should be on state machine or decode thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2011-03-23 15:28:57 -07:00
|
|
|
if (mState != DECODER_STATE_DECODING) {
|
|
|
|
mDecodeStartTime = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
mState = DECODER_STATE_DECODING;
|
2011-07-11 20:39:32 -07:00
|
|
|
ScheduleStateMachine();
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mState == DECODER_STATE_BUFFERING) {
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from BUFFERING to DECODING", mDecoder.get()));
|
2010-04-01 20:03:07 -07:00
|
|
|
mState = DECODER_STATE_DECODING;
|
2011-03-23 15:28:57 -07:00
|
|
|
mDecodeStartTime = TimeStamp::Now();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
ScheduleStateMachine();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::ResetPlayback()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
2010-05-30 21:02:00 -07:00
|
|
|
mVideoFrameEndTime = -1;
|
2010-04-01 20:03:07 -07:00
|
|
|
mAudioStartTime = -1;
|
|
|
|
mAudioEndTime = -1;
|
2011-09-29 16:34:37 -07:00
|
|
|
mAudioCompleted = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
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.");
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
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");
|
2011-04-13 15:12:23 -07:00
|
|
|
double t = aTime * static_cast<double>(USECS_PER_S);
|
2010-04-01 20:03:07 -07:00
|
|
|
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);
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state to SEEKING (to %f)", mDecoder.get(), aTime));
|
2010-04-01 20:03:07 -07:00
|
|
|
mState = DECODER_STATE_SEEKING;
|
2011-07-11 20:39:32 -07:00
|
|
|
ScheduleStateMachine();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:10 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StopDecodeThread()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-09-29 16:34:37 -07:00
|
|
|
mStopDecodeThread = true;
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mDecodeThread) {
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Shutdown decode thread", mDecoder.get()));
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
2010-04-01 20:03:07 -07:00
|
|
|
mDecodeThread->Shutdown();
|
2011-11-07 17:38:17 -08:00
|
|
|
StateMachineTracker::Instance().NoteDecodeThreadDestroyed();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
mDecodeThread = nsnull;
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecodeThreadIdle = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsBuiltinDecoderStateMachine::StopAudioThread()
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-09-29 16:34:37 -07:00
|
|
|
mStopAudioThread = true;
|
2011-07-11 20:39:10 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mAudioThread) {
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Shutdown audio thread", mDecoder.get()));
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
2010-04-01 20:03:07 -07:00
|
|
|
mAudioThread->Shutdown();
|
|
|
|
}
|
|
|
|
mAudioThread = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-07-11 20:39:10 -07:00
|
|
|
nsBuiltinDecoderStateMachine::StartDecodeThread()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-11-07 17:38:17 -08:00
|
|
|
PRUint32 count = 0;
|
|
|
|
bool created = false;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit mon(mDecoder->GetReentrantMonitor());
|
|
|
|
count = StateMachineTracker::Instance().GetDecodeThreadCount();
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mStopDecodeThread = false;
|
2011-07-11 20:39:25 -07:00
|
|
|
if ((mDecodeThread && !mDecodeThreadIdle) || mState >= DECODER_STATE_COMPLETED)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-11-07 17:38:17 -08:00
|
|
|
if (!mDecodeThread && count > StateMachineTracker::MAX_DECODE_THREADS) {
|
|
|
|
// Have to run one iteration of the state machine loop to ensure the
|
|
|
|
// shutdown state is processed.
|
|
|
|
ScheduleStateMachine();
|
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
if (!mDecodeThread) {
|
2011-07-26 20:28:03 -07:00
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(mDecodeThread),
|
|
|
|
nsnull,
|
|
|
|
MEDIA_THREAD_STACK_SIZE);
|
2010-04-01 20:03:07 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-11-07 17:38:17 -08:00
|
|
|
// Have to run one iteration of the state machine loop to ensure the
|
|
|
|
// shutdown state is processed.
|
|
|
|
ScheduleStateMachine();
|
2010-04-01 20:03:07 -07:00
|
|
|
mState = DECODER_STATE_SHUTDOWN;
|
|
|
|
return rv;
|
|
|
|
}
|
2011-11-07 17:38:17 -08:00
|
|
|
created = true;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:25 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(this, &nsBuiltinDecoderStateMachine::DecodeThreadRun);
|
|
|
|
mDecodeThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2011-09-29 16:34:37 -07:00
|
|
|
mDecodeThreadIdle = false;
|
2011-11-07 17:38:17 -08:00
|
|
|
|
|
|
|
if (created) {
|
|
|
|
ReentrantMonitorAutoExit mon(mDecoder->GetReentrantMonitor());
|
|
|
|
StateMachineTracker::Instance().NoteDecodeThreadCreated();
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:10 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsBuiltinDecoderStateMachine::StartAudioThread()
|
|
|
|
{
|
2011-07-11 20:39:32 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread() || OnDecodeThread(),
|
|
|
|
"Should be on state machine or decode thread.");
|
2011-07-11 20:39:10 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-09-29 16:34:37 -07:00
|
|
|
mStopAudioThread = false;
|
2010-04-01 20:03:07 -07:00
|
|
|
if (HasAudio() && !mAudioThread) {
|
2011-07-26 20:28:03 -07:00
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(mAudioThread),
|
|
|
|
nsnull,
|
|
|
|
MEDIA_THREAD_STACK_SIZE);
|
2010-04-01 20:03:07 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
PRInt64 nsBuiltinDecoderStateMachine::AudioDecodedUsecs() const
|
2010-09-14 16:24:47 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(HasAudio(),
|
2011-04-13 15:12:23 -07:00
|
|
|
"Should only call AudioDecodedUsecs() when we have audio");
|
2010-09-14 16:24:47 -07:00
|
|
|
// 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-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::HasLowDecodedData(PRInt64 aAudioUsecs) const
|
2011-01-17 16:53:18 -08:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-03-23 15:28:57 -07:00
|
|
|
// 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() &&
|
2011-04-13 15:12:23 -07:00
|
|
|
AudioDecodedUsecs() < aAudioUsecs)
|
2011-03-23 15:28:57 -07:00
|
|
|
||
|
|
|
|
(!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-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::HasLowUndecodedData() const
|
2011-03-23 15:28:57 -07:00
|
|
|
{
|
2011-09-26 17:25:41 -07:00
|
|
|
return GetUndecodedData() < mLowDataThresholdUsecs;
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 nsBuiltinDecoderStateMachine::GetUndecodedData() const
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-03-23 15:28:57 -07:00
|
|
|
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) {
|
2011-06-14 19:26:33 -07:00
|
|
|
return static_cast<PRInt64>((end - currentTime) * USECS_PER_S);
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-11 14:15:45 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::SetFrameBufferLength(PRUint32 aLength)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aLength >= 512 && aLength <= 16384,
|
|
|
|
"The length must be between 512 and 16384");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-04-11 14:15:45 -07:00
|
|
|
mEventManager.SetSignalBufferLength(aLength);
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:23 -07:00
|
|
|
nsresult nsBuiltinDecoderStateMachine::DecodeMetadata()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-07-11 20:39:25 -07:00
|
|
|
NS_ASSERTION(mState == DECODER_STATE_DECODING_METADATA,
|
|
|
|
"Only call when in metadata decoding state");
|
2011-07-11 20:39:23 -07:00
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Decoding Media Headers", mDecoder.get()));
|
2011-07-11 20:39:23 -07:00
|
|
|
nsresult res;
|
|
|
|
nsVideoInfo info;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
res = mReader->ReadMetadata(&info);
|
|
|
|
}
|
|
|
|
mInfo = info;
|
|
|
|
|
|
|
|
if (NS_FAILED(res) || (!info.mHasVideo && !info.mHasAudio)) {
|
2011-07-11 20:39:34 -07:00
|
|
|
// Dispatch the event to call DecodeError synchronously. This ensures
|
|
|
|
// we're in shutdown state by the time we exit the decode thread.
|
|
|
|
// If we just moved to shutdown state here on the decode thread, we may
|
|
|
|
// cause the state machine to shutdown/free memory without closing its
|
|
|
|
// media stream properly, and we'll get callbacks from the media stream
|
|
|
|
// causing a crash. Note the state machine shutdown joins this decode
|
|
|
|
// thread during shutdown (and other state machines can run on the state
|
|
|
|
// machine thread while the join is waiting), so it's safe to do this
|
|
|
|
// synchronously.
|
2011-07-11 20:39:23 -07:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::DecodeError);
|
2011-07-11 20:39:34 -07:00
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_SYNC);
|
2011-07-11 20:39:23 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mDecoder->StartProgressUpdates();
|
|
|
|
mGotDurationFromMetaData = (GetDuration() != -1);
|
|
|
|
|
|
|
|
VideoData* videoData = FindStartTime();
|
|
|
|
if (videoData) {
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
RenderVideoFrame(videoData, TimeStamp::Now());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 %lld to %lld (duration %lld) seekable=%d",
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder.get(), mStartTime, mEndTime, GetDuration(), mSeekable));
|
2011-07-11 20:39:23 -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.
|
|
|
|
if (HasAudio()) {
|
|
|
|
mEventManager.Init(mInfo.mAudioChannels, mInfo.mAudioRate);
|
|
|
|
// Set the buffer length at the decoder level to be able, to be able
|
|
|
|
// to retrive the value via media element method. The RequestFrameBufferLength
|
|
|
|
// will call the nsBuiltinDecoderStateMachine::SetFrameBufferLength().
|
|
|
|
PRUint32 frameBufferLength = mInfo.mAudioChannels * FRAMEBUFFER_LENGTH_PER_CHANNEL;
|
|
|
|
mDecoder->RequestFrameBufferLength(frameBufferLength);
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> metadataLoadedEvent =
|
|
|
|
new nsAudioMetadataEventRunner(mDecoder, mInfo.mAudioChannels, mInfo.mAudioRate);
|
|
|
|
NS_DispatchToMainThread(metadataLoadedEvent, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
if (mState == DECODER_STATE_DECODING_METADATA) {
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from DECODING_METADATA to DECODING", mDecoder.get()));
|
2011-07-11 20:39:23 -07:00
|
|
|
StartDecoding();
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
if ((mState == DECODER_STATE_DECODING || mState == DECODER_STATE_COMPLETED) &&
|
|
|
|
mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING &&
|
|
|
|
!IsPlaying())
|
|
|
|
{
|
|
|
|
StartPlayback();
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:23 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::DecodeSeek()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
NS_ASSERTION(mState == DECODER_STATE_SEEKING,
|
|
|
|
"Only call when in seeking state");
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool currentTimeChanged = false;
|
2011-07-11 20:39:25 -07:00
|
|
|
PRInt64 mediaTime = GetMediaTime();
|
|
|
|
if (mediaTime != seekTime) {
|
|
|
|
currentTimeChanged = true;
|
|
|
|
UpdatePlaybackPositionInternal(seekTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeekingStarted will do a UpdateReadyStateForData which will
|
|
|
|
// inform the element and its users that we have no frames
|
|
|
|
// to display
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
nsCOMPtr<nsIRunnable> startEvent =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::SeekingStarted);
|
|
|
|
NS_DispatchToMainThread(startEvent, NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentTimeChanged) {
|
|
|
|
// 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.
|
2011-07-11 20:39:30 -07:00
|
|
|
StopPlayback();
|
2011-07-11 20:39:25 -07:00
|
|
|
StopAudioThread();
|
|
|
|
ResetPlayback();
|
|
|
|
nsresult res;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
// Now perform the seek. We must not hold the state machine monitor
|
2011-07-11 20:39:30 -07:00
|
|
|
// while we seek, since the seek reads, which could block on I/O.
|
2011-07-11 20:39:25 -07:00
|
|
|
res = mReader->Seek(seekTime,
|
|
|
|
mStartTime,
|
|
|
|
mEndTime,
|
|
|
|
mediaTime);
|
|
|
|
}
|
2011-07-11 20:39:30 -07:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
2011-08-15 22:19:51 -07:00
|
|
|
AudioData* audio = HasAudio() ? mReader->mAudioQueue.PeekFront() : nsnull;
|
2011-07-11 20:39:25 -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;
|
|
|
|
mPlayDuration = startTime - mStartTime;
|
|
|
|
if (HasVideo()) {
|
|
|
|
nsAutoPtr<VideoData> video(mReader->mVideoQueue.PeekFront());
|
|
|
|
if (video) {
|
|
|
|
NS_ASSERTION(video->mTime <= seekTime && seekTime <= video->mEndTime,
|
|
|
|
"Seek target should lie inside the first frame after seek");
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
RenderVideoFrame(video, TimeStamp::Now());
|
|
|
|
}
|
|
|
|
mReader->mVideoQueue.PopFront();
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::Invalidate);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDecoder->StartProgressUpdates();
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Try to decode another frame to detect if we're at the end...
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Seek completed, mCurrentFrameTime=%lld\n",
|
|
|
|
mDecoder.get(), mCurrentFrameTime));
|
2011-07-11 20:39:25 -07:00
|
|
|
|
|
|
|
// Change state to DECODING or COMPLETED now. SeekingStopped will
|
|
|
|
// call nsBuiltinDecoderStateMachine::Seek to reset our state to SEEKING
|
|
|
|
// if we need to seek again.
|
2011-07-11 20:39:34 -07:00
|
|
|
|
2011-07-11 20:39:25 -07:00
|
|
|
nsCOMPtr<nsIRunnable> stopEvent;
|
|
|
|
if (GetMediaTime() == mEndTime) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from SEEKING (to %lld) to COMPLETED",
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder.get(), seekTime));
|
2011-07-11 20:39:25 -07:00
|
|
|
stopEvent = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::SeekingStoppedAtEnd);
|
|
|
|
mState = DECODER_STATE_COMPLETED;
|
|
|
|
} else {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from SEEKING (to %lld) to DECODING",
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder.get(), seekTime));
|
2011-07-11 20:39:25 -07:00
|
|
|
stopEvent = NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::SeekingStopped);
|
|
|
|
StartDecoding();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
NS_DispatchToMainThread(stopEvent, NS_DISPATCH_SYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-09-29 16:34:37 -07:00
|
|
|
mQuickBuffering = false;
|
2011-07-11 20:39:32 -07:00
|
|
|
|
|
|
|
ScheduleStateMachine();
|
2011-07-11 20:39:25 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
// Runnable to dispose of the decoder and state machine on the main thread.
|
|
|
|
class nsDecoderDisposeEvent : public nsRunnable {
|
|
|
|
public:
|
2011-09-21 00:01:00 -07:00
|
|
|
nsDecoderDisposeEvent(already_AddRefed<nsBuiltinDecoder> aDecoder,
|
|
|
|
already_AddRefed<nsBuiltinDecoderStateMachine> aStateMachine)
|
|
|
|
: mDecoder(aDecoder), mStateMachine(aStateMachine) {}
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_IMETHOD Run() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be on main thread.");
|
2011-09-21 00:01:00 -07:00
|
|
|
mStateMachine->ReleaseDecoder();
|
|
|
|
mDecoder->ReleaseStateMachine();
|
|
|
|
mStateMachine = nsnull;
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsBuiltinDecoder> mDecoder;
|
2011-09-21 00:01:00 -07:00
|
|
|
nsCOMPtr<nsBuiltinDecoderStateMachine> mStateMachine;
|
2011-07-11 20:39:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Runnable which dispatches an event to the main thread to dispose of the
|
|
|
|
// decoder and state machine. This runs on the state machine thread after
|
|
|
|
// the state machine has shutdown, and all events for that state machine have
|
|
|
|
// finished running.
|
|
|
|
class nsDispatchDisposeEvent : public nsRunnable {
|
|
|
|
public:
|
2011-09-21 00:01:00 -07:00
|
|
|
nsDispatchDisposeEvent(nsBuiltinDecoder* aDecoder,
|
|
|
|
nsBuiltinDecoderStateMachine* aStateMachine)
|
|
|
|
: mDecoder(aDecoder), mStateMachine(aStateMachine) {}
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_IMETHOD Run() {
|
2011-09-21 00:01:00 -07:00
|
|
|
NS_DispatchToMainThread(new nsDecoderDisposeEvent(mDecoder.forget(),
|
|
|
|
mStateMachine.forget()));
|
2011-07-11 20:39:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsBuiltinDecoder> mDecoder;
|
2011-09-21 00:01:00 -07:00
|
|
|
nsCOMPtr<nsBuiltinDecoderStateMachine> mStateMachine;
|
2011-07-11 20:39:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult nsBuiltinDecoderStateMachine::RunStateMachine()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-07-11 20:39:32 -07:00
|
|
|
|
2011-11-24 18:06:20 -08:00
|
|
|
nsMediaStream* stream = mDecoder->GetStream();
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ENSURE_TRUE(stream, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
switch (mState) {
|
|
|
|
case DECODER_STATE_SHUTDOWN: {
|
2010-04-01 20:03:07 -07:00
|
|
|
if (IsPlaying()) {
|
2011-07-11 20:39:30 -07:00
|
|
|
StopPlayback();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:10 -07:00
|
|
|
StopAudioThread();
|
|
|
|
StopDecodeThread();
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(mState == DECODER_STATE_SHUTDOWN,
|
2011-09-21 00:01:00 -07:00
|
|
|
"How did we escape from the shutdown state?");
|
2011-07-11 20:39:34 -07:00
|
|
|
// We must daisy-chain these events to destroy the decoder. We must
|
|
|
|
// destroy the decoder on the main thread, but we can't destroy the
|
|
|
|
// decoder while this thread holds the decoder monitor. We can't
|
|
|
|
// dispatch an event to the main thread to destroy the decoder from
|
|
|
|
// here, as the event may run before the dispatch returns, and we
|
|
|
|
// hold the decoder monitor here. We also want to guarantee that the
|
|
|
|
// state machine is destroyed on the main thread, and so the
|
|
|
|
// event runner running this function (which holds a reference to the
|
|
|
|
// state machine) needs to finish and be released in order to allow
|
|
|
|
// that. So we dispatch an event to run after this event runner has
|
|
|
|
// finished and released its monitor/references. That event then will
|
|
|
|
// dispatch an event to the main thread to release the decoder and
|
|
|
|
// state machine.
|
2011-09-21 00:01:00 -07:00
|
|
|
NS_DispatchToCurrentThread(new nsDispatchDisposeEvent(mDecoder, this));
|
2010-04-01 20:03:07 -07:00
|
|
|
return NS_OK;
|
2011-07-11 20:39:32 -07:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
case DECODER_STATE_DECODING_METADATA: {
|
|
|
|
// Ensure we have a decode thread to decode metadata.
|
|
|
|
return StartDecodeThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
case DECODER_STATE_DECODING: {
|
2011-07-11 20:39:37 -07:00
|
|
|
if (mDecoder->GetState() != nsBuiltinDecoder::PLAY_STATE_PLAYING &&
|
|
|
|
IsPlaying())
|
|
|
|
{
|
|
|
|
// We're playing, but the element/decoder is in paused state. Stop
|
|
|
|
// playing! Note we do this before StopDecodeThread() below because
|
|
|
|
// that blocks this state machine's execution, and can cause a
|
|
|
|
// perceptible delay between the pause command, and playback actually
|
|
|
|
// pausing.
|
|
|
|
StopPlayback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPausedAndDecoderWaiting()) {
|
|
|
|
// The decode buffers are full, and playback is paused. Shutdown the
|
|
|
|
// decode thread.
|
|
|
|
StopDecodeThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're playing and/or our decode buffers aren't full. Ensure we have
|
|
|
|
// an active decode thread.
|
|
|
|
if (NS_FAILED(StartDecodeThread())) {
|
|
|
|
NS_WARNING("Failed to start media decode thread!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
AdvanceFrame();
|
|
|
|
NS_ASSERTION(mDecoder->GetState() != nsBuiltinDecoder::PLAY_STATE_PLAYING ||
|
|
|
|
IsStateMachineScheduled(), "Must have timer scheduled");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
case DECODER_STATE_BUFFERING: {
|
2011-07-11 20:39:37 -07:00
|
|
|
if (IsPausedAndDecoderWaiting()) {
|
|
|
|
// The decode buffers are full, and playback is paused. Shutdown the
|
|
|
|
// decode thread.
|
|
|
|
StopDecodeThread();
|
|
|
|
return NS_OK;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
NS_ASSERTION(!mBufferingStart.IsNull(), "Must know buffering start time.");
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
TimeDuration elapsed = now - mBufferingStart;
|
2011-11-24 18:06:20 -08:00
|
|
|
bool isLiveStream = mDecoder->GetStream()->GetLength() == -1;
|
2011-07-11 20:39:32 -07:00
|
|
|
if ((isLiveStream || !mDecoder->CanPlayThrough()) &&
|
2011-09-26 17:25:41 -07:00
|
|
|
elapsed < TimeDuration::FromSeconds(mBufferingWait) &&
|
2011-07-11 20:39:32 -07:00
|
|
|
(mQuickBuffering ? HasLowDecodedData(QUICK_BUFFERING_LOW_DATA_USECS)
|
2011-09-26 17:25:41 -07:00
|
|
|
: (GetUndecodedData() < mBufferingWait * USECS_PER_S / 1000)) &&
|
2011-07-11 20:39:32 -07:00
|
|
|
!stream->IsDataCachedToEndOfStream(mDecoder->mDecoderPosition) &&
|
|
|
|
!stream->IsSuspended())
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:32 -07:00
|
|
|
LOG(PR_LOG_DEBUG,
|
2011-07-11 20:39:34 -07:00
|
|
|
("%p Buffering: %.3lfs/%ds, timeout in %.3lfs %s",
|
|
|
|
mDecoder.get(),
|
2011-07-11 20:39:32 -07:00
|
|
|
GetUndecodedData() / static_cast<double>(USECS_PER_S),
|
2011-09-26 17:25:41 -07:00
|
|
|
mBufferingWait,
|
|
|
|
mBufferingWait - elapsed.ToSeconds(),
|
2011-07-11 20:39:32 -07:00
|
|
|
(mQuickBuffering ? "(quick exit)" : "")));
|
|
|
|
ScheduleStateMachine(USECS_PER_S);
|
|
|
|
return NS_OK;
|
|
|
|
} else {
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from BUFFERING to DECODING", mDecoder.get()));
|
2011-07-11 20:39:32 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Buffered for %.3lfs",
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder.get(),
|
2011-07-11 20:39:32 -07:00
|
|
|
(now - mBufferingStart).ToSeconds()));
|
|
|
|
StartDecoding();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
// Notify to allow blocked decoder thread to continue
|
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
|
|
|
UpdateReadyState();
|
|
|
|
if (mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING &&
|
|
|
|
!IsPlaying())
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:32 -07:00
|
|
|
StartPlayback();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
NS_ASSERTION(IsStateMachineScheduled(), "Must have timer scheduled");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
case DECODER_STATE_SEEKING: {
|
|
|
|
// Ensure we have a decode thread to perform the seek.
|
|
|
|
return StartDecodeThread();
|
|
|
|
}
|
2011-01-17 16:53:18 -08:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
case DECODER_STATE_COMPLETED: {
|
|
|
|
StopDecodeThread();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
if (mState != DECODER_STATE_COMPLETED) {
|
|
|
|
// While we're waiting for the decode thread to shutdown, we can
|
|
|
|
// change state, for example to seeking or shutdown state.
|
|
|
|
// Whatever changed our state should have scheduled another state
|
|
|
|
// machine run.
|
|
|
|
NS_ASSERTION(IsStateMachineScheduled(), "Must have timer scheduled");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:32 -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.
|
|
|
|
if (mState == DECODER_STATE_COMPLETED &&
|
|
|
|
(mReader->mVideoQueue.GetSize() > 0 ||
|
|
|
|
(HasAudio() && !mAudioCompleted)))
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:32 -07:00
|
|
|
AdvanceFrame();
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(mDecoder->GetState() != nsBuiltinDecoder::PLAY_STATE_PLAYING ||
|
|
|
|
IsStateMachineScheduled(),
|
|
|
|
"Must have timer scheduled");
|
2011-07-11 20:39:32 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
// StopPlayback in order to reset the IsPlaying() state so audio
|
|
|
|
// is restarted correctly.
|
|
|
|
StopPlayback();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
if (mState != DECODER_STATE_COMPLETED) {
|
2011-07-11 20:39:34 -07:00
|
|
|
// While we're presenting a frame we can change state. Whatever changed
|
|
|
|
// our state should have scheduled another state machine run.
|
2011-07-11 20:39:32 -07:00
|
|
|
NS_ASSERTION(IsStateMachineScheduled(), "Must have timer scheduled");
|
|
|
|
return NS_OK;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
|
|
|
|
StopAudioThread();
|
|
|
|
if (mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
|
|
|
PRInt64 videoTime = HasVideo() ? mVideoFrameEndTime : 0;
|
|
|
|
PRInt64 clockTime = NS_MAX(mEndTime, NS_MAX(videoTime, GetAudioClock()));
|
|
|
|
UpdatePlaybackPosition(clockTime);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mDecoder, &nsBuiltinDecoder::PlaybackEnded);
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:28:57 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::RenderVideoFrame(VideoData* aData,
|
2011-06-23 15:08:54 -07:00
|
|
|
TimeStamp aTarget)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread() || OnDecodeThread(),
|
|
|
|
"Should be on state machine or decode thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertNotCurrentThreadIn();
|
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) {
|
2011-06-23 15:08:54 -07:00
|
|
|
mDecoder->SetVideoData(aData->mDisplay, 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
|
|
|
{
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
2011-07-11 20:39:30 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
if (!HasAudio())
|
2010-04-01 20:03:07 -07:00
|
|
|
return -1;
|
2011-07-11 20:40:38 -07:00
|
|
|
// We must hold the decoder monitor while using the audio stream off the
|
|
|
|
// audio thread to ensure that it doesn't get destroyed on the audio thread
|
|
|
|
// while we're using it.
|
2011-07-11 20:39:37 -07:00
|
|
|
if (!mAudioStream) {
|
|
|
|
// Audio thread hasn't played any data yet.
|
|
|
|
return mAudioStartTime;
|
|
|
|
}
|
2011-07-11 20:40:38 -07:00
|
|
|
// Note that querying the playback position does not do a synchronous
|
|
|
|
// dispatch to the main thread on Android, so it's safe to call with
|
|
|
|
// the decoder monitor held here.
|
2011-07-11 20:39:37 -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
|
|
|
{
|
2011-07-11 20:39:34 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-07-11 20:39:37 -07:00
|
|
|
NS_ASSERTION(!HasAudio() || mAudioStartTime != -1,
|
|
|
|
"Should know audio start time if we have audio.");
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
if (mDecoder->GetState() != nsBuiltinDecoder::PLAY_STATE_PLAYING) {
|
|
|
|
return;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
// 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;
|
|
|
|
if (!IsPlaying()) {
|
|
|
|
clock_time = mPlayDuration + mStartTime;
|
|
|
|
} else {
|
|
|
|
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 = clock_time - mStartTime;
|
|
|
|
mPlayStartTime = TimeStamp::Now();
|
|
|
|
} else {
|
2011-09-26 20:31:18 -07:00
|
|
|
// Audio is disabled on this system. Sync to the system clock.
|
2011-07-11 20:39:32 -07:00
|
|
|
clock_time = DurationToUsecs(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;
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2011-07-11 20:39:32 -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.
|
|
|
|
PRInt64 remainingTime = AUDIO_DURATION_USECS;
|
|
|
|
NS_ASSERTION(clock_time >= mStartTime, "Should have positive clock time.");
|
|
|
|
nsAutoPtr<VideoData> currentFrame;
|
|
|
|
if (mReader->mVideoQueue.GetSize() > 0) {
|
|
|
|
VideoData* frame = mReader->mVideoQueue.PeekFront();
|
2011-09-26 17:25:41 -07:00
|
|
|
while (mRealTime || clock_time >= frame->mTime) {
|
2011-07-11 20:39:32 -07:00
|
|
|
mVideoFrameEndTime = frame->mEndTime;
|
|
|
|
currentFrame = frame;
|
|
|
|
mReader->mVideoQueue.PopFront();
|
|
|
|
// Notify the decode thread that the video queue's buffers may have
|
|
|
|
// free'd up space for more frames.
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().NotifyAll();
|
2011-07-11 20:39:32 -07:00
|
|
|
mDecoder->UpdatePlaybackOffset(frame->mOffset);
|
|
|
|
if (mReader->mVideoQueue.GetSize() == 0)
|
|
|
|
break;
|
|
|
|
frame = mReader->mVideoQueue.PeekFront();
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
// Current frame has already been presented, wait until it's time to
|
|
|
|
// present the next frame.
|
|
|
|
if (frame && !currentFrame) {
|
|
|
|
PRInt64 now = IsPlaying()
|
|
|
|
? (DurationToUsecs(TimeStamp::Now() - mPlayStartTime) + mPlayDuration)
|
|
|
|
: mPlayDuration;
|
|
|
|
remainingTime = frame->mTime - mStartTime - now;
|
|
|
|
}
|
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
|
2011-07-11 20:39:32 -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.
|
2011-11-24 18:06:20 -08:00
|
|
|
nsMediaStream* stream = mDecoder->GetStream();
|
2011-07-11 20:39:32 -07:00
|
|
|
if (mState == DECODER_STATE_DECODING &&
|
|
|
|
mDecoder->GetState() == nsBuiltinDecoder::PLAY_STATE_PLAYING &&
|
|
|
|
HasLowDecodedData(remainingTime + EXHAUSTED_DATA_MARGIN_USECS) &&
|
|
|
|
!stream->IsDataCachedToEndOfStream(mDecoder->mDecoderPosition) &&
|
|
|
|
!stream->IsSuspended() &&
|
|
|
|
(JustExitedQuickBuffering() || HasLowUndecodedData()))
|
|
|
|
{
|
2011-02-17 18:30:33 -08:00
|
|
|
if (currentFrame) {
|
2011-07-11 20:39:32 -07:00
|
|
|
mReader->mVideoQueue.PushFront(currentFrame.forget());
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
StartBuffering();
|
|
|
|
ScheduleStateMachine();
|
|
|
|
return;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
// We've got enough data to keep playing until at least the next frame.
|
|
|
|
// Start playing now if need be.
|
2011-08-24 16:42:23 -07:00
|
|
|
if (!IsPlaying() && ((mFragmentEndTime >= 0 && clock_time < mFragmentEndTime) || mFragmentEndTime < 0)) {
|
2011-07-11 20:39:32 -07:00
|
|
|
StartPlayback();
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -07:00
|
|
|
if (currentFrame) {
|
|
|
|
// Decode one frame and display it.
|
|
|
|
TimeStamp presTime = mPlayStartTime - UsecsToDuration(mPlayDuration) +
|
|
|
|
UsecsToDuration(currentFrame->mTime - mStartTime);
|
|
|
|
NS_ASSERTION(currentFrame->mTime >= mStartTime, "Should have positive frame time");
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
|
|
|
// If we have video, we want to increment the clock in steps of the frame
|
|
|
|
// duration.
|
|
|
|
RenderVideoFrame(currentFrame, presTime);
|
|
|
|
}
|
|
|
|
mDecoder->GetFrameStatistics().NotifyPresentedFrame();
|
|
|
|
PRInt64 now = DurationToUsecs(TimeStamp::Now() - mPlayStartTime) + mPlayDuration;
|
|
|
|
remainingTime = currentFrame->mEndTime - mStartTime - now;
|
|
|
|
currentFrame = nsnull;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-07-11 20:39:32 -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.
|
|
|
|
if (mVideoFrameEndTime != -1 || mAudioEndTime != -1) {
|
2011-09-26 20:31:18 -07:00
|
|
|
// These will be non -1 if we've displayed a video frame, or played an audio frame.
|
2011-07-11 20:39:32 -07:00
|
|
|
clock_time = NS_MIN(clock_time, NS_MAX(mVideoFrameEndTime, mAudioEndTime));
|
|
|
|
if (clock_time > GetMediaTime()) {
|
|
|
|
// 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);
|
2011-02-17 18:30:33 -08:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// If the number of audio/video frames queued has changed, either by
|
|
|
|
// this function popping and playing a video frame, or by the audio
|
|
|
|
// thread popping and playing an audio frame, we may need to update our
|
2011-07-11 20:39:32 -07:00
|
|
|
// ready state. Post an update to do so.
|
|
|
|
UpdateReadyState();
|
|
|
|
|
|
|
|
ScheduleStateMachine(remainingTime);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-04-13 15:12:23 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::Wait(PRInt64 aUsecs) {
|
2011-07-11 20:39:32 -07:00
|
|
|
NS_ASSERTION(OnAudioThread(), "Only call on the audio thread");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-06-25 07:06:02 -07:00
|
|
|
TimeStamp end = TimeStamp::Now() + UsecsToDuration(NS_MAX<PRInt64>(USECS_PER_MS, aUsecs));
|
2010-04-01 20:03:07 -07:00
|
|
|
TimeStamp now;
|
|
|
|
while ((now = TimeStamp::Now()) < end &&
|
|
|
|
mState != DECODER_STATE_SHUTDOWN &&
|
2011-07-11 20:39:30 -07:00
|
|
|
mState != DECODER_STATE_SEEKING &&
|
2011-07-11 20:39:32 -07:00
|
|
|
!mStopAudioThread &&
|
|
|
|
IsPlaying())
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
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;
|
|
|
|
}
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().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
|
|
|
{
|
2011-07-11 20:39:23 -07:00
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
PRInt64 startTime = 0;
|
|
|
|
mStartTime = 0;
|
|
|
|
VideoData* v = nsnull;
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit exitMon(mDecoder->GetReentrantMonitor());
|
2011-05-08 14:10:28 -07:00
|
|
|
v = mReader->FindStartTime(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
|
2011-09-26 20:31:18 -07:00
|
|
|
// first actual audio frame we have, we'll inject silence during playback
|
2010-08-12 19:28:15 -07:00
|
|
|
// to ensure the audio starts at the correct time.
|
|
|
|
mAudioStartTime = mStartTime;
|
2011-07-11 20:39:34 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Media start time is %lld", mDecoder.get(), mStartTime));
|
2010-04-01 20:03:07 -07:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::UpdateReadyState() {
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::JustExitedQuickBuffering()
|
2011-03-23 15:28:57 -07:00
|
|
|
{
|
|
|
|
return !mDecodeStartTime.IsNull() &&
|
|
|
|
mQuickBuffering &&
|
2011-04-13 15:12:23 -07:00
|
|
|
(TimeStamp::Now() - mDecodeStartTime) < TimeDuration::FromSeconds(QUICK_BUFFER_THRESHOLD_USECS);
|
2011-03-23 15:28:57 -07:00
|
|
|
}
|
|
|
|
|
2010-07-22 15:48:32 -07:00
|
|
|
void nsBuiltinDecoderStateMachine::StartBuffering()
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2010-07-22 15:48:32 -07:00
|
|
|
|
2011-07-11 20:39:37 -07:00
|
|
|
if (IsPlaying()) {
|
|
|
|
StopPlayback();
|
|
|
|
}
|
|
|
|
|
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() &&
|
2011-04-13 15:12:23 -07:00
|
|
|
decodeDuration < UsecsToDuration(QUICK_BUFFER_THRESHOLD_USECS);
|
2011-03-23 15:28:57 -07:00
|
|
|
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-05-07 23:24:09 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Changed state from DECODING to BUFFERING, decoded for %.3lfs",
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder.get(), decodeDuration.ToSeconds()));
|
2011-03-23 15:28:57 -07:00
|
|
|
nsMediaDecoder::Statistics stats = mDecoder->GetStatistics();
|
2011-05-07 23:24:09 -07:00
|
|
|
LOG(PR_LOG_DEBUG, ("%p Playback rate: %.1lfKB/s%s download rate: %.1lfKB/s%s",
|
2011-07-11 20:39:34 -07:00
|
|
|
mDecoder.get(),
|
2011-03-23 15:28:57 -07:00
|
|
|
stats.mPlaybackRate/1024, stats.mPlaybackRateReliable ? "" : " (unreliable)",
|
|
|
|
stats.mDownloadRate/1024, stats.mDownloadRateReliable ? "" : " (unreliable)"));
|
2010-07-22 15:48:32 -07:00
|
|
|
}
|
2011-03-23 15:28:58 -07:00
|
|
|
|
|
|
|
nsresult nsBuiltinDecoderStateMachine::GetBuffered(nsTimeRanges* aBuffered) {
|
2011-11-24 18:06:20 -08:00
|
|
|
nsMediaStream* stream = mDecoder->GetStream();
|
2011-03-23 15:28:58 -07:00
|
|
|
NS_ENSURE_TRUE(stream, NS_ERROR_FAILURE);
|
|
|
|
stream->Pin();
|
|
|
|
nsresult res = mReader->GetBuffered(aBuffered, mStartTime);
|
|
|
|
stream->Unpin();
|
|
|
|
return res;
|
|
|
|
}
|
2011-07-11 20:39:32 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsBuiltinDecoderStateMachine::IsPausedAndDecoderWaiting() {
|
2011-07-11 20:39:37 -07:00
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
|
|
|
|
|
|
|
return
|
|
|
|
mDecodeThreadWaiting &&
|
|
|
|
mDecoder->GetState() != nsBuiltinDecoder::PLAY_STATE_PLAYING &&
|
|
|
|
(mState == DECODER_STATE_DECODING || mState == DECODER_STATE_BUFFERING);
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
nsresult nsBuiltinDecoderStateMachine::Run()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
|
|
|
|
2011-09-26 17:25:41 -07:00
|
|
|
return CallRunStateMachine();
|
2011-07-11 20:39:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsBuiltinDecoderStateMachine::CallRunStateMachine()
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Should be on state machine thread.");
|
2011-09-29 16:34:37 -07:00
|
|
|
// This will be set to true by ScheduleStateMachine() if it's called
|
2011-07-11 20:39:34 -07:00
|
|
|
// while we're in RunStateMachine().
|
2011-09-29 16:34:37 -07:00
|
|
|
mRunAgain = false;
|
2011-07-11 20:39:34 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Set to true whenever we dispatch an event to run this state machine.
|
2011-07-11 20:39:34 -07:00
|
|
|
// This flag prevents us from dispatching
|
2011-09-29 16:34:37 -07:00
|
|
|
mDispatchedRunEvent = false;
|
2011-07-11 20:39:34 -07:00
|
|
|
|
|
|
|
mTimeout = TimeStamp();
|
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
mIsRunning = true;
|
2011-07-11 20:39:34 -07:00
|
|
|
nsresult res = RunStateMachine();
|
2011-09-29 16:34:37 -07:00
|
|
|
mIsRunning = false;
|
2011-07-11 20:39:34 -07:00
|
|
|
|
|
|
|
if (mRunAgain && !mDispatchedRunEvent) {
|
2011-09-29 16:34:37 -07:00
|
|
|
mDispatchedRunEvent = true;
|
2011-07-11 20:39:34 -07:00
|
|
|
return NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TimeoutExpired(nsITimer *aTimer, void *aClosure) {
|
2011-07-11 20:39:32 -07:00
|
|
|
nsBuiltinDecoderStateMachine *machine =
|
|
|
|
static_cast<nsBuiltinDecoderStateMachine*>(aClosure);
|
|
|
|
NS_ASSERTION(machine, "Must have been passed state machine");
|
2011-07-11 20:39:34 -07:00
|
|
|
machine->TimeoutExpired();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsBuiltinDecoderStateMachine::TimeoutExpired()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
NS_ASSERTION(OnStateMachineThread(), "Must be on state machine thread");
|
|
|
|
if (mIsRunning) {
|
2011-09-29 16:34:37 -07:00
|
|
|
mRunAgain = true;
|
2011-07-11 20:39:34 -07:00
|
|
|
} else if (!mDispatchedRunEvent) {
|
2011-09-26 17:25:41 -07:00
|
|
|
// We don't have an event dispatched to run the state machine, so we
|
|
|
|
// can just run it from here.
|
|
|
|
CallRunStateMachine();
|
2011-07-11 20:39:34 -07:00
|
|
|
}
|
|
|
|
// Otherwise, an event has already been dispatched to run the state machine
|
|
|
|
// as soon as possible. Nothing else needed to do, the state machine is
|
|
|
|
// going to run anyway.
|
2011-07-11 20:39:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsBuiltinDecoderStateMachine::ScheduleStateMachine() {
|
|
|
|
return ScheduleStateMachine(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsBuiltinDecoderStateMachine::ScheduleStateMachine(PRInt64 aUsecs) {
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-11-07 17:38:17 -08:00
|
|
|
NS_ABORT_IF_FALSE(GetStateMachineThread(),
|
2011-07-11 20:39:34 -07:00
|
|
|
"Must have a state machine thread to schedule");
|
2011-07-11 20:39:32 -07:00
|
|
|
|
|
|
|
if (mState == DECODER_STATE_SHUTDOWN) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-12-22 03:07:12 -08:00
|
|
|
aUsecs = PR_MAX(aUsecs, 0);
|
2011-07-11 20:39:32 -07:00
|
|
|
|
|
|
|
TimeStamp timeout = TimeStamp::Now() + UsecsToDuration(aUsecs);
|
|
|
|
if (!mTimeout.IsNull()) {
|
|
|
|
if (timeout >= mTimeout) {
|
|
|
|
// We've already scheduled a timer set to expire at or before this time,
|
|
|
|
// or have an event dispatched to run the state machine.
|
|
|
|
return NS_OK;
|
2011-07-11 20:39:34 -07:00
|
|
|
}
|
|
|
|
if (mTimer) {
|
2011-07-11 20:39:32 -07:00
|
|
|
// We've been asked to schedule a timer to run before an existing timer.
|
|
|
|
// Cancel the existing timer.
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
PRUint32 ms = static_cast<PRUint32>((aUsecs / USECS_PER_MS) & 0xFFFFFFFF);
|
2011-09-26 17:25:41 -07:00
|
|
|
if (mRealTime && ms > 40)
|
|
|
|
ms = 40;
|
2011-07-11 20:39:32 -07:00
|
|
|
if (ms == 0) {
|
2011-07-11 20:39:34 -07:00
|
|
|
if (mIsRunning) {
|
|
|
|
// We're currently running this state machine on the state machine
|
|
|
|
// thread. Signal it to run again once it finishes its current cycle.
|
2011-09-29 16:34:37 -07:00
|
|
|
mRunAgain = true;
|
2011-07-11 20:39:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
} else if (!mDispatchedRunEvent) {
|
|
|
|
// We're not currently running this state machine on the state machine
|
|
|
|
// thread. Dispatch an event to run one cycle of the state machine.
|
2011-09-29 16:34:37 -07:00
|
|
|
mDispatchedRunEvent = true;
|
2011-11-07 17:38:17 -08:00
|
|
|
return GetStateMachineThread()->Dispatch(this, NS_DISPATCH_NORMAL);
|
2011-07-11 20:39:34 -07:00
|
|
|
}
|
|
|
|
// We're not currently running this state machine on the state machine
|
|
|
|
// thread, but something has already dispatched an event to run it again,
|
|
|
|
// so just exit; it's going to run real soon.
|
|
|
|
return NS_OK;
|
2011-07-11 20:39:32 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
mTimeout = timeout;
|
|
|
|
|
|
|
|
nsresult res;
|
2011-07-11 20:39:32 -07:00
|
|
|
if (!mTimer) {
|
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1", &res);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2011-11-07 17:38:17 -08:00
|
|
|
mTimer->SetTarget(GetStateMachineThread());
|
2011-07-11 20:39:32 -07:00
|
|
|
}
|
|
|
|
|
2011-07-11 20:39:34 -07:00
|
|
|
res = mTimer->InitWithFuncCallback(::TimeoutExpired,
|
2011-07-11 20:39:32 -07:00
|
|
|
this,
|
|
|
|
ms,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
return res;
|
|
|
|
}
|
2011-11-07 17:38:17 -08:00
|
|
|
|
|
|
|
bool nsBuiltinDecoderStateMachine::OnStateMachineThread() const
|
|
|
|
{
|
|
|
|
return IsCurrentThread(GetStateMachineThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIThread* nsBuiltinDecoderStateMachine::GetStateMachineThread()
|
|
|
|
{
|
|
|
|
return StateMachineTracker::Instance().GetGlobalStateMachineThread();
|
|
|
|
}
|
|
|
|
|
2011-11-21 16:34:21 -08:00
|
|
|
void nsBuiltinDecoderStateMachine::NotifyAudioAvailableListener()
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
mEventManager.NotifyAudioAvailableListener();
|
|
|
|
}
|