2008-07-29 23:50:14 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2010-03-14 18:44:37 -07:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2008-07-29 23:50:14 -07:00
|
|
|
*
|
|
|
|
* 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>
|
2010-04-01 20:03:07 -07:00
|
|
|
* Chris Pearce <chris@pearce.org.nz>
|
2008-07-29 23:50:14 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2009-09-29 14:32:44 -07:00
|
|
|
|
2008-11-09 17:38:02 -08:00
|
|
|
#include <limits>
|
2008-07-29 23:50:14 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsAudioStream.h"
|
|
|
|
#include "nsHTMLVideoElement.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2008-10-19 00:39:21 -07:00
|
|
|
#include "nsTArray.h"
|
2010-04-27 01:53:44 -07:00
|
|
|
#include "VideoUtils.h"
|
|
|
|
#include "nsBuiltinDecoder.h"
|
|
|
|
|
2010-05-05 19:31:02 -07:00
|
|
|
using namespace mozilla;
|
2009-04-11 02:39:24 -07:00
|
|
|
|
2009-09-29 14:32:44 -07:00
|
|
|
#ifdef PR_LOGGING
|
2010-04-27 01:53:44 -07:00
|
|
|
PRLogModuleInfo* gBuiltinDecoderLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gBuiltinDecoderLog, type, msg)
|
2009-09-29 14:32:44 -07:00
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsBuiltinDecoder, nsIObserver)
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::Pause()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2009-05-27 20:49:22 -07:00
|
|
|
if (mPlayState == PLAY_STATE_SEEKING || mPlayState == PLAY_STATE_ENDED) {
|
2008-10-19 00:39:21 -07:00
|
|
|
mNextState = PLAY_STATE_PAUSED;
|
|
|
|
return;
|
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
ChangeState(PLAY_STATE_PAUSED);
|
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::SetVolume(float volume)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-10-19 00:39:21 -07:00
|
|
|
mInitialVolume = volume;
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetVolume(volume);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
float nsBuiltinDecoder::GetDuration()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-11-09 17:38:02 -08:00
|
|
|
if (mDuration >= 0) {
|
|
|
|
return static_cast<float>(mDuration) / 1000.0;
|
|
|
|
}
|
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsBuiltinDecoder::nsBuiltinDecoder() :
|
2009-02-05 00:02:21 -08:00
|
|
|
mDecoderPosition(0),
|
|
|
|
mPlaybackPosition(0),
|
2008-10-23 01:02:18 -07:00
|
|
|
mCurrentTime(0.0),
|
2008-10-19 00:39:21 -07:00
|
|
|
mInitialVolume(0.0),
|
|
|
|
mRequestedSeekTime(-1.0),
|
2009-02-22 16:51:06 -08:00
|
|
|
mDuration(-1),
|
2008-11-09 17:38:02 -08:00
|
|
|
mSeekable(PR_TRUE),
|
2010-04-02 16:47:15 -07:00
|
|
|
mMonitor("media.decoder"),
|
2008-10-19 00:39:21 -07:00
|
|
|
mPlayState(PLAY_STATE_PAUSED),
|
2009-01-15 12:26:51 -08:00
|
|
|
mNextState(PLAY_STATE_PAUSED),
|
|
|
|
mResourceLoaded(PR_FALSE),
|
|
|
|
mIgnoreProgressData(PR_FALSE)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
MOZ_COUNT_CTOR(nsBuiltinDecoder);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-09-29 14:32:44 -07:00
|
|
|
#ifdef PR_LOGGING
|
2010-04-27 01:53:44 -07:00
|
|
|
if (!gBuiltinDecoderLog) {
|
|
|
|
gBuiltinDecoderLog = PR_NewLogModule("nsBuiltinDecoder");
|
2009-09-29 14:32:44 -07:00
|
|
|
}
|
|
|
|
#endif
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
PRBool nsBuiltinDecoder::Init(nsHTMLMediaElement* aElement)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-09-10 18:44:21 -07:00
|
|
|
if (!nsMediaDecoder::Init(aElement))
|
|
|
|
return PR_FALSE;
|
|
|
|
|
2010-02-24 11:14:14 -08:00
|
|
|
nsContentUtils::RegisterShutdownObserver(this);
|
2010-03-01 15:41:49 -08:00
|
|
|
mImageContainer = aElement->GetImageContainer();
|
2009-09-10 18:44:21 -07:00
|
|
|
return PR_TRUE;
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::Stop()
|
2009-05-18 16:08:08 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread");
|
2009-05-18 16:08:08 -07:00
|
|
|
|
|
|
|
// The decode thread must die before the state machine can die.
|
|
|
|
// The state machine must die before the reader.
|
|
|
|
// The state machine must die before the decoder.
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mStateMachineThread)
|
|
|
|
mStateMachineThread->Shutdown();
|
2009-05-18 16:08:08 -07:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
mStateMachineThread = nsnull;
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine = nsnull;
|
2009-05-18 16:08:08 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::Shutdown()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-05-18 16:08:08 -07:00
|
|
|
|
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2008-12-08 16:43:56 -08:00
|
|
|
mShuttingDown = PR_TRUE;
|
|
|
|
|
2009-05-18 16:08:08 -07:00
|
|
|
// This changes the decoder state to SHUTDOWN and does other things
|
|
|
|
// necessary to unblock the state machine thread if it's blocked, so
|
|
|
|
// the asynchronous shutdown in nsDestroyStateMachine won't deadlock.
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->Shutdown();
|
2009-05-18 16:08:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force any outstanding seek and byterange requests to complete
|
|
|
|
// to prevent shutdown from deadlocking.
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mStream) {
|
|
|
|
mStream->Close();
|
2009-09-21 17:03:42 -07:00
|
|
|
}
|
2009-05-18 16:08:08 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
ChangeState(PLAY_STATE_SHUTDOWN);
|
|
|
|
nsMediaDecoder::Shutdown();
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
// We can't destroy mDecoderStateMachine until mStateMachineThread is shut down.
|
2009-05-18 16:08:08 -07:00
|
|
|
// It's unsafe to Shutdown() the decode thread here, as
|
|
|
|
// nsIThread::Shutdown() may run events, such as JS event handlers,
|
|
|
|
// and we could be running at an unsafe time such as during element
|
|
|
|
// destruction.
|
|
|
|
// So we destroy the decoder on the main thread in an asynchronous event.
|
|
|
|
// See bug 468721.
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_NewRunnableMethod(this, &nsBuiltinDecoder::Stop);
|
2009-05-18 16:08:08 -07:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
2010-02-24 11:14:14 -08:00
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsBuiltinDecoder::~nsBuiltinDecoder()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2010-08-05 00:40:35 -07:00
|
|
|
UnpinForSeek();
|
2010-04-27 01:53:44 -07:00
|
|
|
MOZ_COUNT_DTOR(nsBuiltinDecoder);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsresult nsBuiltinDecoder::Load(nsMediaStream* aStream,
|
2010-09-20 17:49:50 -07:00
|
|
|
nsIStreamListener** aStreamListener,
|
|
|
|
nsMediaDecoder* aCloneDonor)
|
2008-09-06 16:47:28 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-09-14 19:30:44 -07:00
|
|
|
if (aStreamListener) {
|
|
|
|
*aStreamListener = nsnull;
|
|
|
|
}
|
2009-09-14 19:28:08 -07:00
|
|
|
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
|
|
|
// Hold the lock while we do this to set proper lock ordering
|
|
|
|
// expectations for dynamic deadlock detectors: decoder lock(s)
|
|
|
|
// should be grabbed before the cache lock
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2009-09-10 18:44:21 -07:00
|
|
|
|
2009-09-14 19:30:43 -07:00
|
|
|
nsresult rv = aStream->Open(aStreamListener);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete aStream;
|
2009-03-31 17:52:56 -07:00
|
|
|
return rv;
|
2009-09-14 19:30:43 -07:00
|
|
|
}
|
2009-09-14 19:30:43 -07:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
mStream = aStream;
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine = CreateStateMachine();
|
|
|
|
if (!mDecoderStateMachine) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-09-20 17:49:50 -07:00
|
|
|
nsBuiltinDecoder* cloneDonor = static_cast<nsBuiltinDecoder*>(aCloneDonor);
|
|
|
|
if (NS_FAILED(mDecoderStateMachine->Init(cloneDonor ?
|
|
|
|
cloneDonor->mDecoderStateMachine : nsnull))) {
|
2010-04-01 20:03:07 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-11-09 17:38:02 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine->SetSeekable(mSeekable);
|
|
|
|
mDecoderStateMachine->SetDuration(mDuration);
|
2008-11-09 17:38:02 -08:00
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
ChangeState(PLAY_STATE_LOADING);
|
|
|
|
|
2010-08-25 01:45:45 -07:00
|
|
|
return StartStateMachineThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsBuiltinDecoder::StartStateMachineThread()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mDecoderStateMachine,
|
|
|
|
"Must have state machine to start state machine thread");
|
|
|
|
if (mStateMachineThread) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(mStateMachineThread));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-04-27 01:53:44 -07:00
|
|
|
return mStateMachineThread->Dispatch(mDecoderStateMachine, NS_DISPATCH_NORMAL);
|
2008-09-06 16:47:28 -07:00
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsresult nsBuiltinDecoder::Play()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-08-25 01:45:45 -07:00
|
|
|
nsresult res = StartStateMachineThread();
|
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2008-10-19 00:39:21 -07:00
|
|
|
if (mPlayState == PLAY_STATE_SEEKING) {
|
|
|
|
mNextState = PLAY_STATE_PLAYING;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-03-31 18:07:10 -07:00
|
|
|
if (mPlayState == PLAY_STATE_ENDED)
|
|
|
|
return Seek(0);
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
ChangeState(PLAY_STATE_PLAYING);
|
|
|
|
return NS_OK;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsresult nsBuiltinDecoder::Seek(float aTime)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
if (aTime < 0.0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mRequestedSeekTime = aTime;
|
|
|
|
|
|
|
|
// If we are already in the seeking state, then setting mRequestedSeekTime
|
|
|
|
// above will result in the new seek occurring when the current seek
|
|
|
|
// completes.
|
|
|
|
if (mPlayState != PLAY_STATE_SEEKING) {
|
2009-03-31 18:07:10 -07:00
|
|
|
if (mPlayState == PLAY_STATE_ENDED) {
|
|
|
|
mNextState = PLAY_STATE_PLAYING;
|
2010-04-27 01:53:44 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-03-31 18:07:10 -07:00
|
|
|
mNextState = mPlayState;
|
|
|
|
}
|
2010-08-05 00:40:35 -07:00
|
|
|
PinForSeek();
|
2008-10-19 00:39:21 -07:00
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:45:45 -07:00
|
|
|
return StartStateMachineThread();
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsresult nsBuiltinDecoder::PlaybackRateChanged()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-10-19 00:39:21 -07:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
float nsBuiltinDecoder::GetCurrentTime()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-10-23 01:02:18 -07:00
|
|
|
return mCurrentTime;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
nsMediaStream* nsBuiltinDecoder::GetCurrentStream()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
return mStream;
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
already_AddRefed<nsIPrincipal> nsBuiltinDecoder::GetCurrentPrincipal()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
return mStream ? mStream->GetCurrentPrincipal() : nsnull;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
void nsBuiltinDecoder::AudioAvailable(float* aFrameBuffer,
|
|
|
|
PRUint32 aFrameBufferLength,
|
2010-10-06 15:58:36 -07:00
|
|
|
float aTime)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-09-05 19:14:50 -07:00
|
|
|
// Auto manage the frame buffer's memory. If we return due to an error
|
|
|
|
// here, this ensures we free the memory. Otherwise, we pass off ownership
|
|
|
|
// to HTMLMediaElement::NotifyAudioAvailable().
|
|
|
|
nsAutoArrayPtr<float> frameBuffer(aFrameBuffer);
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2010-08-25 06:10:00 -07:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mElement->MayHaveAudioAvailableEventListener()) {
|
2010-08-17 06:40:00 -07:00
|
|
|
return;
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-05 19:14:50 -07:00
|
|
|
mElement->NotifyAudioAvailable(frameBuffer.forget(), aFrameBufferLength, aTime);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsBuiltinDecoder::MetadataLoaded(PRUint32 aChannels,
|
|
|
|
PRUint32 aRate,
|
|
|
|
PRUint32 aFrameBufferLength)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameBufferLength = aFrameBufferLength;
|
2008-12-08 16:43:56 -08:00
|
|
|
|
2008-12-10 06:23:23 -08:00
|
|
|
// Only inform the element of MetadataLoaded if not doing a load() in order
|
|
|
|
// to fulfill a seek, otherwise we'll get multiple metadataloaded events.
|
|
|
|
PRBool notifyElement = PR_TRUE;
|
2008-11-09 17:38:02 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-27 01:53:44 -07:00
|
|
|
mDuration = mDecoderStateMachine ? mDecoderStateMachine->GetDuration() : -1;
|
2010-07-19 18:29:27 -07:00
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
|
2008-12-10 06:23:23 -08:00
|
|
|
notifyElement = mNextState != PLAY_STATE_SEEKING;
|
2008-11-09 17:38:02 -08:00
|
|
|
}
|
|
|
|
|
2008-12-10 06:23:23 -08:00
|
|
|
if (mElement && notifyElement) {
|
2009-01-28 01:33:37 -08:00
|
|
|
// Make sure the element and the frame (if any) are told about
|
|
|
|
// our new size.
|
|
|
|
Invalidate();
|
2010-08-25 06:10:00 -07:00
|
|
|
mElement->MetadataLoaded(aChannels, aRate);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2009-01-15 12:26:51 -08:00
|
|
|
|
|
|
|
if (!mResourceLoaded) {
|
|
|
|
StartProgress();
|
2010-04-27 01:53:44 -07:00
|
|
|
}
|
|
|
|
else if (mElement) {
|
2009-01-15 12:26:51 -08:00
|
|
|
// Resource was loaded during metadata loading, when progress
|
|
|
|
// events are being ignored. Fire the final progress event.
|
2010-09-09 20:29:06 -07:00
|
|
|
mElement->DispatchAsyncEvent(NS_LITERAL_STRING("progress"));
|
2009-01-15 12:26:51 -08:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2008-12-10 06:23:23 -08:00
|
|
|
// Only inform the element of FirstFrameLoaded if not doing a load() in order
|
|
|
|
// to fulfill a seek, otherwise we'll get multiple loadedfirstframe events.
|
2010-08-22 19:31:14 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-01 20:03:07 -07:00
|
|
|
PRBool resourceIsLoaded = !mResourceLoaded && mStream &&
|
|
|
|
mStream->IsDataCachedToEndOfStream(mDecoderPosition);
|
2008-12-10 06:23:23 -08:00
|
|
|
if (mElement && notifyElement) {
|
2009-05-18 16:06:10 -07:00
|
|
|
mElement->FirstFrameLoaded(resourceIsLoaded);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
// The element can run javascript via events
|
2010-04-01 20:03:07 -07:00
|
|
|
// before reaching here, so only change the
|
2008-10-19 00:39:21 -07:00
|
|
|
// state if we're still set to the original
|
|
|
|
// loading state.
|
|
|
|
if (mPlayState == PLAY_STATE_LOADING) {
|
|
|
|
if (mRequestedSeekTime >= 0.0) {
|
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
2010-04-27 01:53:44 -07:00
|
|
|
}
|
|
|
|
else {
|
2008-10-19 00:39:21 -07:00
|
|
|
ChangeState(mNextState);
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 12:26:51 -08:00
|
|
|
|
2009-05-18 16:06:10 -07:00
|
|
|
if (resourceIsLoaded) {
|
2009-01-15 12:26:51 -08:00
|
|
|
ResourceLoaded();
|
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::ResourceLoaded()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
// Don't handle ResourceLoaded if we are shutting down, or if
|
|
|
|
// we need to ignore progress data due to seeking (in the case
|
|
|
|
// that the seek results in reaching end of file, we get a bogus call
|
|
|
|
// to ResourceLoaded).
|
2009-01-06 19:33:42 -08:00
|
|
|
if (mShuttingDown)
|
2008-12-08 16:43:56 -08:00
|
|
|
return;
|
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
{
|
|
|
|
// If we are seeking or loading then the resource loaded notification we get
|
|
|
|
// should be ignored, since it represents the end of the seek request.
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2009-02-05 00:02:21 -08:00
|
|
|
if (mIgnoreProgressData || mResourceLoaded || mPlayState == PLAY_STATE_LOADING)
|
2009-01-15 12:26:51 -08:00
|
|
|
return;
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
Progress(PR_FALSE);
|
2009-01-15 12:26:51 -08:00
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
mResourceLoaded = PR_TRUE;
|
|
|
|
StopProgress();
|
|
|
|
}
|
2009-02-05 02:51:24 -08:00
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
// Ensure the final progress event gets fired
|
2008-07-29 23:50:14 -07:00
|
|
|
if (mElement) {
|
|
|
|
mElement->ResourceLoaded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NetworkError()
|
2008-11-06 12:53:20 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-05-18 16:08:08 -07:00
|
|
|
if (mShuttingDown)
|
2008-12-08 16:43:56 -08:00
|
|
|
return;
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
if (mElement)
|
|
|
|
mElement->NetworkError();
|
2009-05-18 16:08:08 -07:00
|
|
|
|
|
|
|
Shutdown();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::DecodeError()
|
2009-09-21 17:08:13 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-09-21 17:08:13 -07:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mElement)
|
|
|
|
mElement->DecodeError();
|
|
|
|
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
PRBool nsBuiltinDecoder::IsSeeking() const
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2008-12-14 10:02:54 -08:00
|
|
|
return mPlayState == PLAY_STATE_SEEKING || mNextState == PLAY_STATE_SEEKING;
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
PRBool nsBuiltinDecoder::IsEnded() const
|
2008-12-14 10:02:54 -08:00
|
|
|
{
|
|
|
|
return mPlayState == PLAY_STATE_ENDED || mPlayState == PLAY_STATE_SHUTDOWN;
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::PlaybackEnded()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mShuttingDown || mPlayState == nsBuiltinDecoder::PLAY_STATE_SEEKING)
|
2008-12-08 16:43:56 -08:00
|
|
|
return;
|
|
|
|
|
2009-06-01 14:54:16 -07:00
|
|
|
PlaybackPositionChanged();
|
2009-03-31 18:07:10 -07:00
|
|
|
ChangeState(PLAY_STATE_ENDED);
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
if (mElement) {
|
2009-05-18 15:44:17 -07:00
|
|
|
UpdateReadyStateForData();
|
2008-10-19 00:39:21 -07:00
|
|
|
mElement->PlaybackEnded();
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_IMETHODIMP nsBuiltinDecoder::Observe(nsISupports *aSubjet,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *someData)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-07-29 23:50:14 -07:00
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
nsMediaDecoder::Statistics
|
2010-04-27 01:53:44 -07:00
|
|
|
nsBuiltinDecoder::GetStatistics()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-07-22 15:48:32 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread() || OnStateMachineThread(),
|
|
|
|
"Should be on main or state machine thread.");
|
2009-02-05 00:02:21 -08:00
|
|
|
Statistics result;
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
if (mStream) {
|
2009-03-31 17:52:56 -07:00
|
|
|
result.mDownloadRate =
|
2010-04-01 20:03:07 -07:00
|
|
|
mStream->GetDownloadRate(&result.mDownloadRateReliable);
|
2009-03-31 17:52:56 -07:00
|
|
|
result.mDownloadPosition =
|
2010-04-01 20:03:07 -07:00
|
|
|
mStream->GetCachedDataEnd(mDecoderPosition);
|
|
|
|
result.mTotalBytes = mStream->GetLength();
|
2009-03-31 17:52:56 -07:00
|
|
|
result.mPlaybackRate = ComputePlaybackRate(&result.mPlaybackRateReliable);
|
|
|
|
result.mDecoderPosition = mDecoderPosition;
|
|
|
|
result.mPlaybackPosition = mPlaybackPosition;
|
2010-04-27 01:53:44 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-03-31 17:52:56 -07:00
|
|
|
result.mDownloadRate = 0;
|
|
|
|
result.mDownloadRateReliable = PR_TRUE;
|
|
|
|
result.mPlaybackRate = 0;
|
|
|
|
result.mPlaybackRateReliable = PR_TRUE;
|
|
|
|
result.mDecoderPosition = 0;
|
|
|
|
result.mPlaybackPosition = 0;
|
|
|
|
result.mDownloadPosition = 0;
|
|
|
|
result.mTotalBytes = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
return result;
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
double nsBuiltinDecoder::ComputePlaybackRate(PRPackedBool* aReliable)
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
GetMonitor().AssertCurrentThreadIn();
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread() || IsCurrentThread(mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on main or state machine thread.");
|
|
|
|
|
|
|
|
PRInt64 length = mStream ? mStream->GetLength() : -1;
|
2009-03-31 17:52:56 -07:00
|
|
|
if (mDuration >= 0 && length >= 0) {
|
|
|
|
*aReliable = PR_TRUE;
|
|
|
|
return double(length)*1000.0/mDuration;
|
|
|
|
}
|
|
|
|
return mPlaybackStatistics.GetRateAtLastStop(aReliable);
|
|
|
|
}
|
2009-02-05 00:02:21 -08:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::UpdatePlaybackRate()
|
2009-03-31 17:52:56 -07:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread() || IsCurrentThread(mStateMachineThread),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on main or state machine thread.");
|
|
|
|
GetMonitor().AssertCurrentThreadIn();
|
|
|
|
if (!mStream)
|
2009-03-31 17:52:56 -07:00
|
|
|
return;
|
|
|
|
PRPackedBool reliable;
|
|
|
|
PRUint32 rate = PRUint32(ComputePlaybackRate(&reliable));
|
2009-05-18 16:05:13 -07:00
|
|
|
if (reliable) {
|
|
|
|
// Avoid passing a zero rate
|
2010-04-01 20:03:07 -07:00
|
|
|
rate = NS_MAX(rate, 1u);
|
2010-04-27 01:53:44 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-03-31 17:52:56 -07:00
|
|
|
// Set a minimum rate of 10,000 bytes per second ... sometimes we just
|
|
|
|
// don't have good data
|
2010-04-01 20:03:07 -07:00
|
|
|
rate = NS_MAX(rate, 10000u);
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
mStream->SetPlaybackRate(rate);
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NotifySuspendedStatusChanged()
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
if (!mStream)
|
2009-03-31 17:52:56 -07:00
|
|
|
return;
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mStream->IsSuspendedByCache() && mElement) {
|
2009-03-31 17:52:56 -07:00
|
|
|
// if this is an autoplay element, we need to kick off its autoplaying
|
|
|
|
// now so we consume data and hopefully free up cache space
|
|
|
|
mElement->NotifyAutoplayDataReady();
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NotifyBytesDownloaded()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-03-31 17:52:56 -07:00
|
|
|
UpdateReadyStateForData();
|
2009-09-14 19:30:45 -07:00
|
|
|
Progress(PR_FALSE);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NotifyDownloadEnded(nsresult aStatus)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
2010-09-02 17:03:03 -07:00
|
|
|
if (aStatus == NS_BINDING_ABORTED) {
|
|
|
|
// Download has been cancelled by user.
|
|
|
|
mElement->LoadAborted();
|
2008-12-08 16:43:56 -08:00
|
|
|
return;
|
2010-09-02 17:03:03 -07:00
|
|
|
}
|
2008-12-08 16:43:56 -08:00
|
|
|
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2009-03-31 17:52:56 -07:00
|
|
|
UpdatePlaybackRate();
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
2009-02-05 00:02:21 -08:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
ResourceLoaded();
|
2010-04-27 01:53:44 -07:00
|
|
|
}
|
|
|
|
else if (aStatus != NS_BASE_STREAM_CLOSED) {
|
2009-02-05 00:02:21 -08:00
|
|
|
NetworkError();
|
|
|
|
}
|
|
|
|
UpdateReadyStateForData();
|
2009-02-05 00:02:21 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NotifyBytesConsumed(PRInt64 aBytes)
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(OnStateMachineThread() || mDecoderStateMachine->OnDecodeThread(),
|
2010-04-01 20:03:07 -07:00
|
|
|
"Should be on play state machine or decode thread.");
|
2009-02-05 00:02:21 -08:00
|
|
|
if (!mIgnoreProgressData) {
|
|
|
|
mDecoderPosition += aBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NextFrameUnavailableBuffering()
|
2010-03-14 16:46:38 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be called on main thread");
|
2010-04-27 01:53:44 -07:00
|
|
|
if (!mElement || mShuttingDown || !mDecoderStateMachine)
|
2010-03-14 16:46:38 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
mElement->UpdateReadyStateForData(nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE_BUFFERING);
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NextFrameAvailable()
|
2010-03-14 16:46:38 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be called on main thread");
|
2010-04-27 01:53:44 -07:00
|
|
|
if (!mElement || mShuttingDown || !mDecoderStateMachine)
|
2010-03-14 16:46:38 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
mElement->UpdateReadyStateForData(nsHTMLMediaElement::NEXT_FRAME_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::NextFrameUnavailable()
|
2010-03-14 16:46:38 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be called on main thread");
|
2010-04-27 01:53:44 -07:00
|
|
|
if (!mElement || mShuttingDown || !mDecoderStateMachine)
|
2010-03-14 16:46:38 -07:00
|
|
|
return;
|
|
|
|
mElement->UpdateReadyStateForData(nsHTMLMediaElement::NEXT_FRAME_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::UpdateReadyStateForData()
|
2009-02-05 00:02:21 -08:00
|
|
|
{
|
2010-03-14 16:46:38 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be called on main thread");
|
2010-04-27 01:53:44 -07:00
|
|
|
if (!mElement || mShuttingDown || !mDecoderStateMachine)
|
2008-12-08 16:43:56 -08:00
|
|
|
return;
|
2010-03-14 16:46:38 -07:00
|
|
|
nsHTMLMediaElement::NextFrameStatus frameStatus =
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine->GetNextFrameStatus();
|
2009-02-10 17:43:45 -08:00
|
|
|
mElement->UpdateReadyStateForData(frameStatus);
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::SeekingStopped()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
// An additional seek was requested while the current seek was
|
|
|
|
// in operation.
|
2010-08-05 00:40:35 -07:00
|
|
|
if (mRequestedSeekTime >= 0.0) {
|
2008-10-19 00:39:21 -07:00
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
2010-08-05 00:40:35 -07:00
|
|
|
} else {
|
|
|
|
UnpinForSeek();
|
2008-10-19 00:39:21 -07:00
|
|
|
ChangeState(mNextState);
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mElement) {
|
2009-02-05 00:02:21 -08:00
|
|
|
UpdateReadyStateForData();
|
2009-05-18 15:44:17 -07:00
|
|
|
mElement->SeekCompleted();
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-31 03:02:17 -07:00
|
|
|
// This is called when seeking stopped *and* we're at the end of the
|
|
|
|
// media.
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::SeekingStoppedAtEnd()
|
2009-05-31 03:02:17 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
|
2009-05-31 03:02:17 -07:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PRBool fireEnded = PR_FALSE;
|
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2009-05-31 03:02:17 -07:00
|
|
|
|
|
|
|
// An additional seek was requested while the current seek was
|
|
|
|
// in operation.
|
|
|
|
if (mRequestedSeekTime >= 0.0) {
|
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
2010-08-05 00:40:35 -07:00
|
|
|
} else {
|
|
|
|
UnpinForSeek();
|
2009-06-11 19:14:28 -07:00
|
|
|
fireEnded = mNextState != PLAY_STATE_PLAYING;
|
|
|
|
ChangeState(fireEnded ? PLAY_STATE_ENDED : mNextState);
|
2009-05-31 03:02:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mElement) {
|
|
|
|
UpdateReadyStateForData();
|
|
|
|
mElement->SeekCompleted();
|
|
|
|
if (fireEnded) {
|
|
|
|
mElement->PlaybackEnded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::SeekingStarted()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
if (mElement) {
|
2009-05-18 15:44:17 -07:00
|
|
|
UpdateReadyStateForData();
|
2008-10-19 00:39:21 -07:00
|
|
|
mElement->SeekStarted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::ChangeState(PlayState aState)
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
if (mNextState == aState) {
|
|
|
|
mNextState = PLAY_STATE_PAUSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPlayState == PLAY_STATE_SHUTDOWN) {
|
2010-04-01 20:03:07 -07:00
|
|
|
mMonitor.NotifyAll();
|
2008-10-19 00:39:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPlayState = aState;
|
|
|
|
switch (aState) {
|
|
|
|
case PLAY_STATE_PAUSED:
|
|
|
|
/* No action needed */
|
|
|
|
break;
|
|
|
|
case PLAY_STATE_PLAYING:
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine->Decode();
|
2008-10-19 00:39:21 -07:00
|
|
|
break;
|
|
|
|
case PLAY_STATE_SEEKING:
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine->Seek(mRequestedSeekTime);
|
2008-10-19 00:39:21 -07:00
|
|
|
mRequestedSeekTime = -1.0;
|
|
|
|
break;
|
|
|
|
case PLAY_STATE_LOADING:
|
|
|
|
/* No action needed */
|
|
|
|
break;
|
|
|
|
case PLAY_STATE_START:
|
|
|
|
/* No action needed */
|
|
|
|
break;
|
|
|
|
case PLAY_STATE_ENDED:
|
|
|
|
/* No action needed */
|
|
|
|
break;
|
|
|
|
case PLAY_STATE_SHUTDOWN:
|
|
|
|
/* No action needed */
|
|
|
|
break;
|
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
mMonitor.NotifyAll();
|
2008-10-19 00:39:21 -07:00
|
|
|
}
|
2008-10-23 01:02:18 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::PlaybackPositionChanged()
|
2008-10-23 01:02:18 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-12-08 16:43:56 -08:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2008-10-23 01:02:18 -07:00
|
|
|
float lastTime = mCurrentTime;
|
|
|
|
|
|
|
|
// Control the scope of the monitor so it is not
|
|
|
|
// held while the timeupdate and the invalidate is run.
|
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mCurrentTime = mDecoderStateMachine->GetCurrentTime();
|
|
|
|
mDecoderStateMachine->ClearPositionChangeFlag();
|
2008-10-23 01:02:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate the frame so any video data is displayed.
|
|
|
|
// Do this before the timeupdate event so that if that
|
|
|
|
// event runs JavaScript that queries the media size, the
|
|
|
|
// frame has reflowed and the size updated beforehand.
|
|
|
|
Invalidate();
|
|
|
|
|
|
|
|
if (mElement && lastTime != mCurrentTime) {
|
2010-09-09 22:49:26 -07:00
|
|
|
FireTimeUpdate();
|
2008-10-23 01:02:18 -07:00
|
|
|
}
|
|
|
|
}
|
2008-11-09 17:38:02 -08:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::DurationChanged()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
PRInt64 oldDuration = mDuration;
|
2010-04-27 01:53:44 -07:00
|
|
|
mDuration = mDecoderStateMachine ? mDecoderStateMachine->GetDuration() : -1;
|
2010-07-19 18:29:27 -07:00
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mElement && oldDuration != mDuration) {
|
|
|
|
LOG(PR_LOG_DEBUG, ("%p duration changed to %lldms", this, mDuration));
|
2010-09-09 20:29:06 -07:00
|
|
|
mElement->DispatchEvent(NS_LITERAL_STRING("durationchange"));
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::SetDuration(PRInt64 aDuration)
|
2009-02-07 02:10:34 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2009-02-07 02:10:34 -08:00
|
|
|
mDuration = aDuration;
|
2010-07-19 18:29:27 -07:00
|
|
|
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetDuration(mDuration);
|
2009-02-07 02:10:34 -08:00
|
|
|
}
|
2010-07-19 18:29:27 -07:00
|
|
|
|
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
2009-02-07 02:10:34 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::SetSeekable(PRBool aSeekable)
|
2008-11-09 17:38:02 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-11-09 17:38:02 -08:00
|
|
|
mSeekable = aSeekable;
|
2010-04-27 01:53:44 -07:00
|
|
|
if (mDecoderStateMachine) {
|
2010-04-01 20:03:07 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-04-27 01:53:44 -07:00
|
|
|
mDecoderStateMachine->SetSeekable(aSeekable);
|
2008-11-09 17:38:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
PRBool nsBuiltinDecoder::GetSeekable()
|
2008-11-09 17:38:02 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
2008-11-09 17:38:02 -08:00
|
|
|
return mSeekable;
|
|
|
|
}
|
2009-01-06 19:33:42 -08:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::Suspend()
|
2009-01-21 15:54:40 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
if (mStream) {
|
|
|
|
mStream->Suspend(PR_TRUE);
|
2009-01-21 15:54:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-22 15:48:32 -07:00
|
|
|
void nsBuiltinDecoder::Resume(PRBool aForceBuffering)
|
2009-01-21 15:54:40 -08:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
if (mStream) {
|
|
|
|
mStream->Resume();
|
2009-01-21 15:54:40 -08:00
|
|
|
}
|
2010-07-22 15:48:32 -07:00
|
|
|
if (aForceBuffering) {
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
mDecoderStateMachine->StartBuffering();
|
|
|
|
}
|
2009-01-21 15:54:40 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::StopProgressUpdates()
|
2009-01-15 12:26:51 -08:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mStateMachineThread), "Should be on state machine thread.");
|
2009-01-15 12:26:51 -08:00
|
|
|
mIgnoreProgressData = PR_TRUE;
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mStream) {
|
|
|
|
mStream->SetReadMode(nsMediaCacheStream::MODE_METADATA);
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2009-01-15 12:26:51 -08:00
|
|
|
}
|
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::StartProgressUpdates()
|
2009-01-15 12:26:51 -08:00
|
|
|
{
|
2010-04-27 01:53:44 -07:00
|
|
|
NS_ASSERTION(IsCurrentThread(mStateMachineThread), "Should be on state machine thread.");
|
2009-01-15 12:26:51 -08:00
|
|
|
mIgnoreProgressData = PR_FALSE;
|
2010-04-01 20:03:07 -07:00
|
|
|
if (mStream) {
|
|
|
|
mStream->SetReadMode(nsMediaCacheStream::MODE_PLAYBACK);
|
|
|
|
mDecoderPosition = mPlaybackPosition = mStream->Tell();
|
2009-03-31 17:52:56 -07:00
|
|
|
}
|
2009-01-15 12:26:51 -08:00
|
|
|
}
|
2009-04-09 18:28:24 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
void nsBuiltinDecoder::MoveLoadsToBackground()
|
2009-04-09 18:28:24 -07:00
|
|
|
{
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
|
|
|
|
if (mStream) {
|
|
|
|
mStream->MoveLoadsToBackground();
|
2009-04-09 18:28:24 -07:00
|
|
|
}
|
|
|
|
}
|
2010-04-27 01:53:45 -07:00
|
|
|
|
|
|
|
void nsBuiltinDecoder::UpdatePlaybackOffset(PRInt64 aOffset)
|
|
|
|
{
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
mPlaybackPosition = NS_MAX(aOffset, mPlaybackPosition);
|
|
|
|
}
|