2013-01-18 05:43:20 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-09-29 21:32:44 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaDecoder.h"
|
2013-02-16 03:55:36 +00:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2008-11-10 01:38:02 +00:00
|
|
|
#include <limits>
|
2008-07-30 06:50:14 +00:00
|
|
|
#include "nsIObserver.h"
|
2008-10-19 07:39:21 +00:00
|
|
|
#include "nsTArray.h"
|
2010-04-27 08:53:44 +00:00
|
|
|
#include "VideoUtils.h"
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaDecoderStateMachine.h"
|
2013-03-02 19:14:44 +00:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2012-08-21 04:06:46 +00:00
|
|
|
#include "ImageContainer.h"
|
2012-11-14 19:45:13 +00:00
|
|
|
#include "MediaResource.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2013-11-26 00:01:31 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-09-05 20:25:17 +00:00
|
|
|
#include "nsIMemoryReporter.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2013-12-18 03:59:11 +00:00
|
|
|
#include "MediaShutdownManager.h"
|
2014-04-10 17:39:20 +00:00
|
|
|
#include "AudioChannelService.h"
|
2014-07-04 03:55:06 +00:00
|
|
|
#include "mozilla/dom/AudioTrack.h"
|
|
|
|
#include "mozilla/dom/AudioTrackList.h"
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
|
|
|
#include "mozilla/dom/VideoTrack.h"
|
|
|
|
#include "mozilla/dom/VideoTrackList.h"
|
2012-11-14 19:45:13 +00:00
|
|
|
|
2012-12-18 08:49:58 +00:00
|
|
|
#ifdef MOZ_WMF
|
|
|
|
#include "WMFDecoder.h"
|
|
|
|
#endif
|
|
|
|
|
2012-11-14 19:45:13 +00:00
|
|
|
using namespace mozilla::layers;
|
2012-11-16 03:25:26 +00:00
|
|
|
using namespace mozilla::dom;
|
2012-11-14 19:45:13 +00:00
|
|
|
|
2015-01-28 14:31:31 +00:00
|
|
|
// Default timeout msecs until try to enter dormant state by heuristic.
|
|
|
|
static const int DEFAULT_HEURISTIC_DORMANT_TIMEOUT_MSECS = 60000;
|
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-11-14 19:45:13 +00:00
|
|
|
// Number of estimated seconds worth of data we need to have buffered
|
|
|
|
// ahead of the current playback position before we allow the media decoder
|
|
|
|
// to report that it can play through the entire media without the decode
|
|
|
|
// catching up with the download. Having this margin make the
|
2012-11-14 19:46:40 +00:00
|
|
|
// MediaDecoder::CanPlayThrough() calculation more stable in the case of
|
2012-11-14 19:45:13 +00:00
|
|
|
// fluctuating bitrates.
|
2013-05-05 23:59:05 +00:00
|
|
|
static const int64_t CAN_PLAY_THROUGH_MARGIN = 1;
|
2012-11-14 19:45:13 +00:00
|
|
|
|
2014-04-23 09:29:04 +00:00
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef DECODER_LOG
|
|
|
|
|
2009-09-29 21:32:44 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 19:46:40 +00:00
|
|
|
PRLogModuleInfo* gMediaDecoderLog;
|
2014-08-21 00:13:00 +00:00
|
|
|
#define DECODER_LOG(x, ...) \
|
|
|
|
PR_LOG(gMediaDecoderLog, PR_LOG_DEBUG, ("Decoder=%p " x, this, ##__VA_ARGS__))
|
2009-09-29 21:32:44 +00:00
|
|
|
#else
|
2014-08-21 00:13:00 +00:00
|
|
|
#define DECODER_LOG(x, ...)
|
2009-09-29 21:32:44 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-21 00:13:00 +00:00
|
|
|
static const char* const gPlayStateStr[] = {
|
|
|
|
"START",
|
|
|
|
"LOADING",
|
|
|
|
"PAUSED",
|
|
|
|
"PLAYING",
|
|
|
|
"SEEKING",
|
|
|
|
"ENDED",
|
|
|
|
"SHUTDOWN"
|
|
|
|
};
|
|
|
|
|
2013-12-08 05:39:47 +00:00
|
|
|
class MediaMemoryTracker : public nsIMemoryReporter
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
2014-06-24 16:36:43 +00:00
|
|
|
virtual ~MediaMemoryTracker();
|
|
|
|
|
2013-12-08 05:39:47 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2013-11-26 00:01:31 +00:00
|
|
|
|
2014-03-05 21:31:04 +00:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf);
|
|
|
|
|
2013-01-18 05:43:20 +00:00
|
|
|
MediaMemoryTracker();
|
2013-11-07 05:35:30 +00:00
|
|
|
void InitMemoryReporter();
|
|
|
|
|
2013-11-26 00:01:31 +00:00
|
|
|
static StaticRefPtr<MediaMemoryTracker> sUniqueInstance;
|
2012-11-14 19:45:13 +00:00
|
|
|
|
2013-01-18 05:43:20 +00:00
|
|
|
static MediaMemoryTracker* UniqueInstance() {
|
2012-11-14 19:45:13 +00:00
|
|
|
if (!sUniqueInstance) {
|
2013-01-18 05:43:20 +00:00
|
|
|
sUniqueInstance = new MediaMemoryTracker();
|
2013-11-07 05:35:30 +00:00
|
|
|
sUniqueInstance->InitMemoryReporter();
|
2012-11-14 19:45:13 +00:00
|
|
|
}
|
|
|
|
return sUniqueInstance;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
typedef nsTArray<MediaDecoder*> DecodersArray;
|
2012-11-14 19:45:13 +00:00
|
|
|
static DecodersArray& Decoders() {
|
|
|
|
return UniqueInstance()->mDecoders;
|
|
|
|
}
|
|
|
|
|
|
|
|
DecodersArray mDecoders;
|
|
|
|
|
|
|
|
public:
|
2013-01-18 05:43:20 +00:00
|
|
|
static void AddMediaDecoder(MediaDecoder* aDecoder)
|
|
|
|
{
|
2012-11-14 19:45:13 +00:00
|
|
|
Decoders().AppendElement(aDecoder);
|
|
|
|
}
|
|
|
|
|
2013-01-18 05:43:20 +00:00
|
|
|
static void RemoveMediaDecoder(MediaDecoder* aDecoder)
|
|
|
|
{
|
2012-11-14 19:45:13 +00:00
|
|
|
DecodersArray& decoders = Decoders();
|
|
|
|
decoders.RemoveElement(aDecoder);
|
|
|
|
if (decoders.IsEmpty()) {
|
|
|
|
sUniqueInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-26 00:01:31 +00:00
|
|
|
StaticRefPtr<MediaMemoryTracker> MediaMemoryTracker::sUniqueInstance;
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(MediaMemoryTracker, nsIMemoryReporter)
|
2013-01-18 05:43:20 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(MediaDecoder, nsIObserver)
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2015-01-22 04:01:12 +00:00
|
|
|
void MediaDecoder::NotifyOwnerActivityChanged()
|
2013-06-10 12:22:05 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
|
2015-01-28 14:31:31 +00:00
|
|
|
if (!mOwner) {
|
|
|
|
NS_WARNING("MediaDecoder without a decoder owner, can't update dormant");
|
2015-01-27 08:39:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-28 14:31:31 +00:00
|
|
|
UpdateDormantState(false /* aDormantTimeout */, false /* aActivity */);
|
|
|
|
// Start dormant timer if necessary
|
|
|
|
StartDormantTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaDecoder::UpdateDormantState(bool aDormantTimeout, bool aActivity)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
if (!mDecoderStateMachine ||
|
|
|
|
mPlayState == PLAY_STATE_SHUTDOWN ||
|
|
|
|
!mOwner->GetVideoFrameContainer() ||
|
|
|
|
!mDecoderStateMachine->IsDormantNeeded())
|
|
|
|
{
|
2015-01-28 03:00:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-22 04:01:12 +00:00
|
|
|
bool prevDormant = mIsDormant;
|
|
|
|
mIsDormant = false;
|
2015-01-28 14:31:31 +00:00
|
|
|
if (!mOwner->IsActive()) {
|
2015-01-22 04:01:12 +00:00
|
|
|
mIsDormant = true;
|
|
|
|
}
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2015-01-28 14:31:31 +00:00
|
|
|
if (mOwner->IsHidden()) {
|
2015-01-22 04:01:12 +00:00
|
|
|
mIsDormant = true;
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-28 14:31:31 +00:00
|
|
|
// Try to enable dormant by idle heuristic, when the owner is hidden.
|
|
|
|
bool prevHeuristicDormant = mIsHeuristicDormant;
|
|
|
|
mIsHeuristicDormant = false;
|
|
|
|
if (mIsHeuristicDormantSupported && mOwner->IsHidden()) {
|
|
|
|
if (aDormantTimeout && !aActivity &&
|
|
|
|
(mPlayState == PLAY_STATE_PAUSED || mPlayState == PLAY_STATE_ENDED)) {
|
|
|
|
// Enable heuristic dormant
|
|
|
|
mIsHeuristicDormant = true;
|
|
|
|
} else if(prevHeuristicDormant && !aActivity) {
|
|
|
|
// Continue heuristic dormant
|
|
|
|
mIsHeuristicDormant = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsHeuristicDormant) {
|
|
|
|
mIsDormant = true;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 04:01:12 +00:00
|
|
|
|
|
|
|
if (prevDormant == mIsDormant) {
|
|
|
|
// No update to dormant state
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsDormant) {
|
2013-06-10 12:22:05 +00:00
|
|
|
// enter dormant state
|
|
|
|
mDecoderStateMachine->SetDormant(true);
|
|
|
|
|
2014-07-22 10:54:32 +00:00
|
|
|
int64_t timeUsecs = 0;
|
|
|
|
SecondsToUsecs(mCurrentTime, timeUsecs);
|
|
|
|
mRequestedSeekTarget = SeekTarget(timeUsecs, SeekTarget::Accurate);
|
|
|
|
|
2013-06-10 12:22:05 +00:00
|
|
|
mNextState = mPlayState;
|
|
|
|
ChangeState(PLAY_STATE_LOADING);
|
2014-12-13 01:29:24 +00:00
|
|
|
} else {
|
2013-06-10 12:22:05 +00:00
|
|
|
// exit dormant state
|
2013-08-26 16:11:58 +00:00
|
|
|
// trigger to state machine.
|
2013-06-10 12:22:05 +00:00
|
|
|
mDecoderStateMachine->SetDormant(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 14:31:31 +00:00
|
|
|
void MediaDecoder::DormantTimerExpired(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
MediaDecoder* decoder = static_cast<MediaDecoder*>(aClosure);
|
|
|
|
ReentrantMonitorAutoEnter mon(decoder->GetReentrantMonitor());
|
|
|
|
decoder->UpdateDormantState(true /* aDormantTimeout */,
|
|
|
|
false /* aActivity */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaDecoder::StartDormantTimer()
|
|
|
|
{
|
|
|
|
if (!mIsHeuristicDormantSupported) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsHeuristicDormant ||
|
|
|
|
mShuttingDown ||
|
|
|
|
!mOwner ||
|
|
|
|
!mOwner->IsHidden() ||
|
|
|
|
(mPlayState != PLAY_STATE_PAUSED &&
|
|
|
|
mPlayState != PLAY_STATE_ENDED))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDormantTimer) {
|
|
|
|
mDormantTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
}
|
|
|
|
mDormantTimer->InitWithFuncCallback(&MediaDecoder::DormantTimerExpired,
|
|
|
|
this,
|
|
|
|
mHeuristicDormantTimeout,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaDecoder::CancelDormantTimer()
|
|
|
|
{
|
|
|
|
if (mDormantTimer) {
|
|
|
|
mDormantTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::Pause()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2014-12-13 01:29:24 +00:00
|
|
|
if (mPlayState == PLAY_STATE_LOADING ||
|
2014-09-02 18:07:00 +00:00
|
|
|
mPlayState == PLAY_STATE_SEEKING ||
|
|
|
|
mPlayState == PLAY_STATE_ENDED) {
|
2008-10-19 07:39:21 +00:00
|
|
|
mNextState = PLAY_STATE_PAUSED;
|
|
|
|
return;
|
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(PLAY_STATE_PAUSED);
|
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::SetVolume(double aVolume)
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-01-17 03:03:00 +00:00
|
|
|
mInitialVolume = aVolume;
|
2010-04-27 08:53:44 +00:00
|
|
|
if (mDecoderStateMachine) {
|
2011-01-17 03:03:00 +00:00
|
|
|
mDecoderStateMachine->SetVolume(aVolume);
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::ConnectDecodedStreamToOutputStream(OutputStreamData* aStream)
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aStream->mPort, "Already connected?");
|
|
|
|
|
|
|
|
// The output stream must stay in sync with the decoded stream, so if
|
|
|
|
// either stream is blocked, we block the other.
|
|
|
|
aStream->mPort = aStream->mStream->AllocateInputPort(mDecodedStream->mStream,
|
|
|
|
MediaInputPort::FLAG_BLOCK_INPUT | MediaInputPort::FLAG_BLOCK_OUTPUT);
|
|
|
|
// Unblock the output stream now. While it's connected to mDecodedStream,
|
|
|
|
// mDecodedStream is responsible for controlling blocking.
|
|
|
|
aStream->mStream->ChangeExplicitBlockerCount(-1);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::DecodedStreamData::DecodedStreamData(MediaDecoder* aDecoder,
|
2013-02-04 10:04:24 +00:00
|
|
|
int64_t aInitialTime,
|
|
|
|
SourceMediaStream* aStream)
|
2012-07-31 12:17:22 +00:00
|
|
|
: mLastAudioPacketTime(-1),
|
|
|
|
mLastAudioPacketEndTime(-1),
|
|
|
|
mAudioFramesWritten(0),
|
|
|
|
mInitialTime(aInitialTime),
|
|
|
|
mNextVideoTime(aInitialTime),
|
2013-02-04 10:04:24 +00:00
|
|
|
mDecoder(aDecoder),
|
2012-07-31 12:17:22 +00:00
|
|
|
mStreamInitialized(false),
|
|
|
|
mHaveSentFinish(false),
|
|
|
|
mHaveSentFinishAudio(false),
|
|
|
|
mHaveSentFinishVideo(false),
|
|
|
|
mStream(aStream),
|
2013-11-23 09:48:24 +00:00
|
|
|
mHaveBlockedForPlayState(false),
|
|
|
|
mHaveBlockedForStateMachineNotPlaying(false)
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
2013-12-06 12:01:33 +00:00
|
|
|
mListener = new DecodedStreamGraphListener(mStream, this);
|
2013-12-01 21:09:06 +00:00
|
|
|
mStream->AddListener(mListener);
|
2012-07-31 12:17:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::DecodedStreamData::~DecodedStreamData()
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
2013-12-01 21:09:06 +00:00
|
|
|
mListener->Forget();
|
2012-07-31 12:17:22 +00:00
|
|
|
mStream->Destroy();
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:01:33 +00:00
|
|
|
MediaDecoder::DecodedStreamGraphListener::DecodedStreamGraphListener(MediaStream* aStream,
|
|
|
|
DecodedStreamData* aData)
|
|
|
|
: mData(aData),
|
|
|
|
mMutex("MediaDecoder::DecodedStreamData::mMutex"),
|
2013-12-01 21:09:06 +00:00
|
|
|
mStream(aStream),
|
2014-06-12 04:44:56 +00:00
|
|
|
mLastOutputTime(aStream->
|
|
|
|
StreamTimeToMicroseconds(aStream->GetCurrentTime())),
|
2013-12-01 21:09:06 +00:00
|
|
|
mStreamFinishedOnMainThread(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoder::DecodedStreamGraphListener::NotifyOutput(MediaStreamGraph* aGraph,
|
|
|
|
GraphTime aCurrentTime)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mStream) {
|
2014-06-12 04:44:56 +00:00
|
|
|
mLastOutputTime = mStream->
|
|
|
|
StreamTimeToMicroseconds(mStream->GraphTimeToStreamTime(aCurrentTime));
|
2013-12-01 21:09:06 +00:00
|
|
|
}
|
2013-02-04 10:04:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:01:33 +00:00
|
|
|
void
|
|
|
|
MediaDecoder::DecodedStreamGraphListener::DoNotifyFinished()
|
|
|
|
{
|
|
|
|
if (mData && mData->mDecoder) {
|
|
|
|
if (mData->mDecoder->GetState() == PLAY_STATE_PLAYING) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(mData->mDecoder, &MediaDecoder::PlaybackEnded);
|
|
|
|
NS_DispatchToCurrentThread(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mStreamFinishedOnMainThread = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-14 05:47:56 +00:00
|
|
|
MediaDecoder::DecodedStreamGraphListener::NotifyEvent(MediaStreamGraph* aGraph,
|
|
|
|
MediaStreamListener::MediaStreamGraphEvent event)
|
2013-12-06 12:01:33 +00:00
|
|
|
{
|
2014-07-14 05:47:56 +00:00
|
|
|
if (event == EVENT_FINISHED) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(this, &DecodedStreamGraphListener::DoNotifyFinished);
|
|
|
|
aGraph->DispatchToMainThreadAfterStreamStateUpdate(event.forget());
|
|
|
|
}
|
2013-12-06 12:01:33 +00:00
|
|
|
}
|
2014-12-15 01:22:15 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::DestroyDecodedStream()
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2012-07-31 12:17:22 +00:00
|
|
|
|
2014-12-04 18:29:00 +00:00
|
|
|
if (GetDecodedStream()) {
|
|
|
|
GetStateMachine()->ResyncMediaStreamClock();
|
2014-12-23 07:04:20 +00:00
|
|
|
} else {
|
|
|
|
// Avoid the redundant blocking to output stream.
|
|
|
|
return;
|
2014-12-04 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 12:17:22 +00:00
|
|
|
// All streams are having their SourceMediaStream disconnected, so they
|
|
|
|
// need to be explicitly blocked again.
|
2013-01-30 04:19:03 +00:00
|
|
|
for (int32_t i = mOutputStreams.Length() - 1; i >= 0; --i) {
|
2012-07-31 12:17:22 +00:00
|
|
|
OutputStreamData& os = mOutputStreams[i];
|
|
|
|
// During cycle collection, nsDOMMediaStream can be destroyed and send
|
|
|
|
// its Destroy message before this decoder is destroyed. So we have to
|
|
|
|
// be careful not to send any messages after the Destroy().
|
2013-01-30 04:19:03 +00:00
|
|
|
if (os.mStream->IsDestroyed()) {
|
|
|
|
// Probably the DOM MediaStream was GCed. Clean up.
|
2014-12-23 07:03:15 +00:00
|
|
|
MOZ_ASSERT(os.mPort, "Double-delete of the ports!");
|
|
|
|
os.mPort->Destroy();
|
2013-01-30 04:19:03 +00:00
|
|
|
mOutputStreams.RemoveElementAt(i);
|
|
|
|
continue;
|
2012-07-31 12:17:22 +00:00
|
|
|
}
|
2013-01-30 04:19:03 +00:00
|
|
|
os.mStream->ChangeExplicitBlockerCount(1);
|
2012-07-31 12:17:22 +00:00
|
|
|
// Explicitly remove all existing ports. This is not strictly necessary but it's
|
|
|
|
// good form.
|
2014-12-23 07:03:15 +00:00
|
|
|
MOZ_ASSERT(os.mPort, "Double-delete of the ports!");
|
|
|
|
os.mPort->Destroy();
|
|
|
|
os.mPort = nullptr;
|
2012-07-31 12:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mDecodedStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-23 09:48:24 +00:00
|
|
|
void MediaDecoder::UpdateStreamBlockingForStateMachinePlaying()
|
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
if (!mDecodedStream) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-01 21:09:06 +00:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetSyncPointForMediaStream();
|
|
|
|
}
|
2013-11-23 09:48:24 +00:00
|
|
|
bool blockForStateMachineNotPlaying =
|
|
|
|
mDecoderStateMachine && !mDecoderStateMachine->IsPlaying() &&
|
|
|
|
mDecoderStateMachine->GetState() != MediaDecoderStateMachine::DECODER_STATE_COMPLETED;
|
|
|
|
if (blockForStateMachineNotPlaying != mDecodedStream->mHaveBlockedForStateMachineNotPlaying) {
|
|
|
|
mDecodedStream->mHaveBlockedForStateMachineNotPlaying = blockForStateMachineNotPlaying;
|
|
|
|
int32_t delta = blockForStateMachineNotPlaying ? 1 : -1;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mDecodedStream->mStream->ChangeExplicitBlockerCount(delta);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethodWithArg<int32_t>(mDecodedStream->mStream.get(),
|
|
|
|
&MediaStream::ChangeExplicitBlockerCount, delta);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::RecreateDecodedStream(int64_t aStartTimeUSecs)
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2014-08-21 00:13:00 +00:00
|
|
|
DECODER_LOG("RecreateDecodedStream aStartTimeUSecs=%lld!", aStartTimeUSecs);
|
2012-07-31 12:17:22 +00:00
|
|
|
|
|
|
|
DestroyDecodedStream();
|
|
|
|
|
|
|
|
mDecodedStream = new DecodedStreamData(this, aStartTimeUSecs,
|
2012-10-29 04:36:31 +00:00
|
|
|
MediaStreamGraph::GetInstance()->CreateSourceStream(nullptr));
|
2012-07-31 12:17:22 +00:00
|
|
|
|
|
|
|
// Note that the delay between removing ports in DestroyDecodedStream
|
|
|
|
// and adding new ones won't cause a glitch since all graph operations
|
|
|
|
// between main-thread stable states take effect atomically.
|
2013-01-30 04:19:03 +00:00
|
|
|
for (int32_t i = mOutputStreams.Length() - 1; i >= 0; --i) {
|
|
|
|
OutputStreamData& os = mOutputStreams[i];
|
|
|
|
if (os.mStream->IsDestroyed()) {
|
|
|
|
// Probably the DOM MediaStream was GCed. Clean up.
|
|
|
|
// No need to destroy the port; all ports have been destroyed here.
|
|
|
|
mOutputStreams.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ConnectDecodedStreamToOutputStream(&os);
|
2012-07-31 12:17:22 +00:00
|
|
|
}
|
2013-11-23 09:48:24 +00:00
|
|
|
UpdateStreamBlockingForStateMachinePlaying();
|
2012-07-31 12:17:22 +00:00
|
|
|
|
|
|
|
mDecodedStream->mHaveBlockedForPlayState = mPlayState != PLAY_STATE_PLAYING;
|
|
|
|
if (mDecodedStream->mHaveBlockedForPlayState) {
|
|
|
|
mDecodedStream->mStream->ChangeExplicitBlockerCount(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::AddOutputStream(ProcessedMediaStream* aStream,
|
2013-01-30 04:19:03 +00:00
|
|
|
bool aFinishWhenEnded)
|
2012-04-30 03:12:42 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-21 00:13:00 +00:00
|
|
|
DECODER_LOG("AddOutputStream aStream=%p!", aStream);
|
2012-04-30 03:12:42 +00:00
|
|
|
|
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-01-28 23:57:00 +00:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetAudioCaptured();
|
|
|
|
}
|
|
|
|
if (!GetDecodedStream()) {
|
|
|
|
int64_t t = mDecoderStateMachine ?
|
|
|
|
mDecoderStateMachine->GetCurrentTimeUs() : 0;
|
|
|
|
RecreateDecodedStream(t);
|
2012-07-31 12:17:22 +00:00
|
|
|
}
|
|
|
|
OutputStreamData* os = mOutputStreams.AppendElement();
|
|
|
|
os->Init(aStream, aFinishWhenEnded);
|
|
|
|
ConnectDecodedStreamToOutputStream(os);
|
|
|
|
if (aFinishWhenEnded) {
|
|
|
|
// Ensure that aStream finishes the moment mDecodedStream does.
|
|
|
|
aStream->SetAutofinish(true);
|
|
|
|
}
|
2012-04-30 03:12:42 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 03:13:09 +00:00
|
|
|
// This can be called before Load(), in which case our mDecoderStateMachine
|
|
|
|
// won't have been created yet and we can rely on Load() to schedule it
|
|
|
|
// once it is created.
|
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
// Make sure the state machine thread runs so that any buffered data
|
|
|
|
// is fed into our stream.
|
|
|
|
ScheduleStateMachineThread();
|
|
|
|
}
|
2012-04-30 03:12:42 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
double MediaDecoder::GetDuration()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-08-01 18:11:20 +00:00
|
|
|
if (mInfiniteStream) {
|
|
|
|
return std::numeric_limits<double>::infinity();
|
|
|
|
}
|
2008-11-10 01:38:02 +00:00
|
|
|
if (mDuration >= 0) {
|
2011-04-13 22:12:23 +00:00
|
|
|
return static_cast<double>(mDuration) / static_cast<double>(USECS_PER_S);
|
2008-11-10 01:38:02 +00:00
|
|
|
}
|
2011-01-17 03:03:00 +00:00
|
|
|
return std::numeric_limits<double>::quiet_NaN();
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
int64_t MediaDecoder::GetMediaDuration()
|
|
|
|
{
|
2012-12-13 19:42:45 +00:00
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), -1);
|
2012-11-19 15:11:21 +00:00
|
|
|
return GetStateMachine()->GetDuration();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::SetInfinite(bool aInfinite)
|
2011-08-01 18:11:20 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-08-01 18:11:20 +00:00
|
|
|
mInfiniteStream = aInfinite;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::IsInfinite()
|
2011-08-01 18:11:20 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-08-01 18:11:20 +00:00
|
|
|
return mInfiniteStream;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::MediaDecoder() :
|
2009-02-05 08:02:21 +00:00
|
|
|
mDecoderPosition(0),
|
|
|
|
mPlaybackPosition(0),
|
2008-10-23 08:02:18 +00:00
|
|
|
mCurrentTime(0.0),
|
2008-10-19 07:39:21 +00:00
|
|
|
mInitialVolume(0.0),
|
2013-02-26 08:52:23 +00:00
|
|
|
mInitialPlaybackRate(1.0),
|
|
|
|
mInitialPreservesPitch(true),
|
2009-02-23 00:51:06 +00:00
|
|
|
mDuration(-1),
|
2012-11-30 13:17:54 +00:00
|
|
|
mMediaSeekable(true),
|
2013-07-24 09:55:23 +00:00
|
|
|
mSameOriginMedia(false),
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor("media.decoder"),
|
2014-10-28 09:10:08 +00:00
|
|
|
mPlayState(PLAY_STATE_LOADING),
|
2009-01-15 20:26:51 +00:00
|
|
|
mNextState(PLAY_STATE_PAUSED),
|
2011-09-29 23:34:37 +00:00
|
|
|
mIgnoreProgressData(false),
|
2012-07-31 12:17:22 +00:00
|
|
|
mInfiniteStream(false),
|
2012-11-14 19:45:13 +00:00
|
|
|
mOwner(nullptr),
|
2014-06-22 04:43:00 +00:00
|
|
|
mPlaybackStatistics(new MediaChannelStatistics()),
|
2012-11-14 19:45:13 +00:00
|
|
|
mPinnedForSeek(false),
|
2012-11-16 03:25:26 +00:00
|
|
|
mShuttingDown(false),
|
2013-02-27 19:38:11 +00:00
|
|
|
mPausedForPlaybackRateNull(false),
|
2014-07-04 03:55:06 +00:00
|
|
|
mMinimizePreroll(false),
|
2015-01-22 04:01:12 +00:00
|
|
|
mMediaTracksConstructed(false),
|
2015-01-28 14:31:31 +00:00
|
|
|
mIsDormant(false),
|
|
|
|
mIsHeuristicDormantSupported(
|
|
|
|
Preferences::GetBool("media.decoder.heuristic.dormant.enabled", false)),
|
|
|
|
mHeuristicDormantTimeout(
|
|
|
|
Preferences::GetInt("media.decoder.heuristic.dormant.timeout",
|
|
|
|
DEFAULT_HEURISTIC_DORMANT_TIMEOUT_MSECS)),
|
|
|
|
mIsHeuristicDormant(false)
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-14 19:46:40 +00:00
|
|
|
MOZ_COUNT_CTOR(MediaDecoder);
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-01-18 05:43:20 +00:00
|
|
|
MediaMemoryTracker::AddMediaDecoder(this);
|
2009-09-29 21:32:44 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 19:46:40 +00:00
|
|
|
if (!gMediaDecoderLog) {
|
|
|
|
gMediaDecoderLog = PR_NewLogModule("MediaDecoder");
|
2009-09-29 21:32:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-04-10 17:39:20 +00:00
|
|
|
mAudioChannel = AudioChannelService::GetDefaultAudioChannel();
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::Init(MediaDecoderOwner* aOwner)
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-11-14 19:45:13 +00:00
|
|
|
mOwner = aOwner;
|
|
|
|
mVideoFrameContainer = aOwner->GetVideoFrameContainer();
|
2013-12-18 03:59:11 +00:00
|
|
|
MediaShutdownManager::Instance().Register(this);
|
2011-09-29 23:34:37 +00:00
|
|
|
return true;
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::Shutdown()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-11-06 22:33:02 +00:00
|
|
|
|
2009-05-18 23:08:08 +00:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2011-09-29 23:34:37 +00:00
|
|
|
mShuttingDown = true;
|
2008-12-09 00:43:56 +00:00
|
|
|
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
DestroyDecodedStream();
|
|
|
|
}
|
|
|
|
|
2009-05-18 23:08:08 +00: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 08:53:44 +00:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->Shutdown();
|
2009-05-18 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force any outstanding seek and byterange requests to complete
|
|
|
|
// to prevent shutdown from deadlocking.
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
|
|
|
mResource->Close();
|
2009-09-22 00:03:42 +00:00
|
|
|
}
|
2009-05-18 23:08:08 +00:00
|
|
|
|
2015-01-28 14:31:31 +00:00
|
|
|
CancelDormantTimer();
|
|
|
|
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(PLAY_STATE_SHUTDOWN);
|
2012-11-14 19:45:13 +00:00
|
|
|
|
|
|
|
mOwner = nullptr;
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2013-12-18 03:59:11 +00:00
|
|
|
MediaShutdownManager::Instance().Unregister(this);
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::~MediaDecoder()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-01-18 05:43:20 +00:00
|
|
|
MediaMemoryTracker::RemoveMediaDecoder(this);
|
2010-08-05 07:40:35 +00:00
|
|
|
UnpinForSeek();
|
2012-11-14 19:46:40 +00:00
|
|
|
MOZ_COUNT_DTOR(MediaDecoder);
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 09:55:23 +00:00
|
|
|
nsresult MediaDecoder::OpenResource(nsIStreamListener** aStreamListener)
|
2008-09-06 23:47:28 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2009-09-15 02:30:44 +00:00
|
|
|
if (aStreamListener) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*aStreamListener = nullptr;
|
2009-09-15 02:30:44 +00:00
|
|
|
}
|
2009-09-15 02:28:08 +00:00
|
|
|
|
2009-04-01 00:52:56 +00: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
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2009-09-11 01:44:21 +00:00
|
|
|
|
2013-07-24 09:55:23 +00:00
|
|
|
nsresult rv = mResource->Open(aStreamListener);
|
2014-08-21 00:13:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2012-09-17 20:45:38 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-24 09:55:23 +00:00
|
|
|
nsresult MediaDecoder::Load(nsIStreamListener** aStreamListener,
|
|
|
|
MediaDecoder* aCloneDonor)
|
2012-09-17 20:45:38 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
|
2013-07-24 09:55:23 +00:00
|
|
|
nsresult rv = OpenResource(aStreamListener);
|
2012-09-17 20:45:38 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2010-04-27 08:53:44 +00:00
|
|
|
mDecoderStateMachine = CreateStateMachine();
|
2014-08-21 00:13:00 +00:00
|
|
|
NS_ENSURE_TRUE(mDecoderStateMachine, NS_ERROR_FAILURE);
|
2010-04-27 08:53:44 +00:00
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
return InitializeStateMachine(aCloneDonor);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaDecoder::InitializeStateMachine(MediaDecoder* aCloneDonor)
|
2012-09-17 20:45:38 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-12-13 19:42:45 +00:00
|
|
|
NS_ASSERTION(mDecoderStateMachine, "Cannot initialize null state machine!");
|
2012-09-17 20:45:38 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder* cloneDonor = static_cast<MediaDecoder*>(aCloneDonor);
|
2014-08-21 00:13:00 +00:00
|
|
|
nsresult rv = mDecoderStateMachine->Init(
|
|
|
|
cloneDonor ? cloneDonor->mDecoderStateMachine : nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-08-05 14:55:02 +00:00
|
|
|
|
|
|
|
// If some parameters got set before the state machine got created,
|
|
|
|
// set them now
|
|
|
|
SetStateMachineParameters();
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2011-07-12 03:39:34 +00:00
|
|
|
return ScheduleStateMachineThread();
|
2010-08-25 08:45:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 14:55:02 +00:00
|
|
|
void MediaDecoder::SetStateMachineParameters()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mDecoderStateMachine->SetDuration(mDuration);
|
|
|
|
mDecoderStateMachine->SetVolume(mInitialVolume);
|
2015-01-28 23:57:00 +00:00
|
|
|
if (GetDecodedStream()) {
|
|
|
|
mDecoderStateMachine->SetAudioCaptured();
|
|
|
|
}
|
2014-08-05 14:55:02 +00:00
|
|
|
SetPlaybackRate(mInitialPlaybackRate);
|
|
|
|
mDecoderStateMachine->SetPreservesPitch(mInitialPreservesPitch);
|
|
|
|
if (mMinimizePreroll) {
|
|
|
|
mDecoderStateMachine->SetMinimizePrerollUntilPlaybackStarts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 03:43:57 +00:00
|
|
|
void MediaDecoder::SetMinimizePrerollUntilPlaybackStarts()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mMinimizePreroll = true;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaDecoder::ScheduleStateMachineThread()
|
2010-08-25 08:45:45 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-08-25 08:45:45 +00:00
|
|
|
NS_ASSERTION(mDecoderStateMachine,
|
|
|
|
"Must have state machine to start state machine thread");
|
2012-01-19 18:29:12 +00:00
|
|
|
NS_ENSURE_STATE(mDecoderStateMachine);
|
2011-07-12 03:39:32 +00:00
|
|
|
|
|
|
|
if (mShuttingDown)
|
|
|
|
return NS_OK;
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2014-04-22 11:58:00 +00:00
|
|
|
return mDecoderStateMachine->ScheduleStateMachine();
|
2008-09-06 23:47:28 +00:00
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaDecoder::Play()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-01-28 14:31:31 +00:00
|
|
|
UpdateDormantState(false /* aDormantTimeout */, true /* aActivity */);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(mDecoderStateMachine != nullptr, "Should have state machine.");
|
2014-09-01 04:52:39 +00:00
|
|
|
if (mPausedForPlaybackRateNull) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-12 03:39:34 +00:00
|
|
|
nsresult res = ScheduleStateMachineThread();
|
2010-08-25 08:45:45 +00:00
|
|
|
NS_ENSURE_SUCCESS(res,res);
|
2014-12-13 01:29:24 +00:00
|
|
|
if (mPlayState == PLAY_STATE_LOADING || mPlayState == PLAY_STATE_SEEKING) {
|
2008-10-19 07:39:21 +00:00
|
|
|
mNextState = PLAY_STATE_PLAYING;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-12-13 01:29:24 +00:00
|
|
|
if (mPlayState == PLAY_STATE_ENDED) {
|
2014-04-01 03:39:04 +00:00
|
|
|
return Seek(0, SeekTarget::PrevSyncPoint);
|
2014-12-13 01:29:24 +00:00
|
|
|
}
|
2008-10-19 07:39:21 +00:00
|
|
|
|
|
|
|
ChangeState(PLAY_STATE_PLAYING);
|
|
|
|
return NS_OK;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 03:39:04 +00:00
|
|
|
nsresult MediaDecoder::Seek(double aTime, SeekTarget::Type aSeekType)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2015-01-28 14:31:31 +00:00
|
|
|
UpdateDormantState(false /* aDormantTimeout */, true /* aActivity */);
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2011-08-09 10:10:48 +00:00
|
|
|
NS_ABORT_IF_FALSE(aTime >= 0.0, "Cannot seek to a negative value.");
|
|
|
|
|
2014-04-01 03:39:04 +00:00
|
|
|
int64_t timeUsecs = 0;
|
|
|
|
nsresult rv = SecondsToUsecs(aTime, timeUsecs);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-03-28 12:31:29 +00:00
|
|
|
|
2014-04-01 03:39:04 +00:00
|
|
|
mRequestedSeekTarget = SeekTarget(timeUsecs, aSeekType);
|
2011-02-01 02:57:13 +00:00
|
|
|
mCurrentTime = aTime;
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2015-01-16 18:58:00 +00:00
|
|
|
// If we are already in the seeking state, the new seek overrides the old one.
|
|
|
|
if (mPlayState != PLAY_STATE_LOADING) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool paused = false;
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
|
|
|
paused = mOwner->GetPaused();
|
2009-04-01 01:07:10 +00:00
|
|
|
}
|
2011-09-27 02:22:09 +00:00
|
|
|
mNextState = paused ? PLAY_STATE_PAUSED : PLAY_STATE_PLAYING;
|
2010-08-05 07:40:35 +00:00
|
|
|
PinForSeek();
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
|
|
|
}
|
|
|
|
|
2011-07-12 03:39:34 +00:00
|
|
|
return ScheduleStateMachineThread();
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 03:43:57 +00:00
|
|
|
bool MediaDecoder::IsLogicallyPlaying()
|
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
return mPlayState == PLAY_STATE_PLAYING ||
|
|
|
|
mNextState == PLAY_STATE_PLAYING;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
double MediaDecoder::GetCurrentTime()
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-10-23 08:02:18 +00:00
|
|
|
return mCurrentTime;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
already_AddRefed<nsIPrincipal> MediaDecoder::GetCurrentPrincipal()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-07-30 14:20:58 +00:00
|
|
|
return mResource ? mResource->GetCurrentPrincipal() : nullptr;
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 13:17:54 +00:00
|
|
|
void MediaDecoder::QueueMetadata(int64_t aPublishTime,
|
2014-11-06 08:17:05 +00:00
|
|
|
nsAutoPtr<MediaInfo> aInfo,
|
|
|
|
nsAutoPtr<MetadataTags> aTags)
|
2012-11-30 13:17:54 +00:00
|
|
|
{
|
2013-01-30 20:14:43 +00:00
|
|
|
NS_ASSERTION(OnDecodeThread(), "Should be on decode thread.");
|
2012-11-30 13:17:54 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2014-07-04 03:54:54 +00:00
|
|
|
mDecoderStateMachine->QueueMetadata(aPublishTime, aInfo, aTags);
|
2012-11-30 13:17:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 02:34:28 +00:00
|
|
|
bool
|
2014-11-12 04:50:20 +00:00
|
|
|
MediaDecoder::IsExpectingMoreData()
|
2013-01-29 02:34:28 +00:00
|
|
|
{
|
2013-09-04 11:13:06 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2014-11-12 04:50:20 +00:00
|
|
|
|
|
|
|
// If there's no resource, we're probably just getting set up.
|
|
|
|
if (!mResource) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've downloaded anything, we're not waiting for anything.
|
|
|
|
if (mResource->IsDataCachedToEndOfResource(mDecoderPosition)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we should be getting data unless the stream is suspended.
|
|
|
|
return !mResource->IsSuspended();
|
2013-01-29 02:34:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 08:17:05 +00:00
|
|
|
void MediaDecoder::MetadataLoaded(nsAutoPtr<MediaInfo> aInfo,
|
2015-01-16 15:56:19 +00:00
|
|
|
nsAutoPtr<MetadataTags> aTags,
|
2015-01-23 15:00:39 +00:00
|
|
|
bool aRestoredFromDormant)
|
2010-08-25 13:10:00 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-06 08:17:05 +00:00
|
|
|
|
2010-08-25 13:10:00 +00:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-21 00:13:00 +00:00
|
|
|
DECODER_LOG("MetadataLoaded, channels=%u rate=%u hasAudio=%d hasVideo=%d",
|
|
|
|
aInfo->mAudio.mChannels, aInfo->mAudio.mRate,
|
|
|
|
aInfo->HasAudio(), aInfo->HasVideo());
|
|
|
|
|
2008-11-10 01:38:02 +00:00
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2010-04-27 08:53:44 +00:00
|
|
|
mDuration = mDecoderStateMachine ? mDecoderStateMachine->GetDuration() : -1;
|
2010-07-20 01:29:27 +00:00
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
2008-11-10 01:38:02 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 18:11:20 +00:00
|
|
|
if (mDuration == -1) {
|
2011-09-29 23:34:37 +00:00
|
|
|
SetInfinite(true);
|
2011-08-01 18:11:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 08:17:05 +00:00
|
|
|
mInfo = aInfo.forget();
|
2014-07-04 03:55:06 +00:00
|
|
|
ConstructMediaTracks();
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
2009-01-28 09:33:37 +00:00
|
|
|
// Make sure the element and the frame (if any) are told about
|
|
|
|
// our new size.
|
|
|
|
Invalidate();
|
2015-01-23 15:00:39 +00:00
|
|
|
if (!aRestoredFromDormant) {
|
2015-01-16 15:56:19 +00:00
|
|
|
mOwner->MetadataLoaded(mInfo, nsAutoPtr<const MetadataTags>(aTags.forget()));
|
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2014-11-06 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 15:56:19 +00:00
|
|
|
void MediaDecoder::FirstFrameLoaded(nsAutoPtr<MediaInfo> aInfo,
|
2015-01-23 15:00:39 +00:00
|
|
|
bool aRestoredFromDormant)
|
2014-11-06 09:52:44 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-06 08:17:05 +00:00
|
|
|
|
2014-11-06 09:52:44 +00:00
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DECODER_LOG("FirstFrameLoaded, channels=%u rate=%u hasAudio=%d hasVideo=%d",
|
|
|
|
aInfo->mAudio.mChannels, aInfo->mAudio.mRate,
|
|
|
|
aInfo->HasAudio(), aInfo->HasVideo());
|
|
|
|
|
2014-11-06 08:17:05 +00:00
|
|
|
mInfo = aInfo.forget();
|
2009-01-15 20:26:51 +00:00
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
2014-11-06 09:56:56 +00:00
|
|
|
Invalidate();
|
2015-01-23 15:00:39 +00:00
|
|
|
if (!aRestoredFromDormant) {
|
2015-01-16 15:56:19 +00:00
|
|
|
mOwner->FirstFrameLoaded();
|
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2011-11-30 05:05:49 +00:00
|
|
|
// This can run cache callbacks.
|
2012-02-15 04:35:01 +00:00
|
|
|
mResource->EnsureCacheUpToDate();
|
2011-11-30 05:05:49 +00:00
|
|
|
|
2008-10-19 07:39:21 +00:00
|
|
|
// The element can run javascript via events
|
2010-04-02 03:03:07 +00:00
|
|
|
// before reaching here, so only change the
|
2008-10-19 07:39:21 +00:00
|
|
|
// state if we're still set to the original
|
|
|
|
// loading state.
|
|
|
|
if (mPlayState == PLAY_STATE_LOADING) {
|
2014-04-01 03:39:04 +00:00
|
|
|
if (mRequestedSeekTarget.IsValid()) {
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
2010-04-27 08:53:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(mNextState);
|
|
|
|
}
|
|
|
|
}
|
2009-01-15 20:26:51 +00:00
|
|
|
|
2011-11-30 05:05:49 +00:00
|
|
|
// Run NotifySuspendedStatusChanged now to give us a chance to notice
|
|
|
|
// that autoplay should run.
|
|
|
|
NotifySuspendedStatusChanged();
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 03:08:10 +00:00
|
|
|
void MediaDecoder::ResetConnectionState()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mOwner) {
|
|
|
|
// Notify the media element that connection gets lost.
|
|
|
|
mOwner->ResetConnectionState();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we have notified the media element the connection
|
|
|
|
// lost event, the decoder will be reloaded when user tries
|
|
|
|
// to play the Rtsp streaming next time.
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::NetworkError()
|
2008-11-06 20:53:20 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2009-05-18 23:08:08 +00:00
|
|
|
if (mShuttingDown)
|
2008-12-09 00:43:56 +00:00
|
|
|
return;
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner)
|
|
|
|
mOwner->NetworkError();
|
2009-05-18 23:08:08 +00:00
|
|
|
|
|
|
|
Shutdown();
|
2008-11-06 20:53:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::DecodeError()
|
2009-09-22 00:08:13 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2009-09-22 00:08:13 +00:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner)
|
|
|
|
mOwner->DecodeError();
|
2009-09-22 00:08:13 +00:00
|
|
|
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2013-07-24 09:55:23 +00:00
|
|
|
void MediaDecoder::UpdateSameOriginStatus(bool aSameOrigin)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
mSameOriginMedia = aSameOrigin;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaDecoder::IsSameOriginMedia()
|
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
return mSameOriginMedia;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::IsSeeking() const
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-12-13 01:29:24 +00:00
|
|
|
return mPlayState == PLAY_STATE_SEEKING ||
|
|
|
|
(mPlayState == PLAY_STATE_LOADING && mRequestedSeekTarget.IsValid());
|
2008-12-14 18:02:54 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::IsEnded() const
|
2008-12-14 18:02:54 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-12-14 18:02:54 +00:00
|
|
|
return mPlayState == PLAY_STATE_ENDED || mPlayState == PLAY_STATE_SHUTDOWN;
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::PlaybackEnded()
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-07-31 12:17:22 +00:00
|
|
|
|
2014-09-02 18:07:00 +00:00
|
|
|
if (mShuttingDown ||
|
|
|
|
mPlayState == PLAY_STATE_SEEKING ||
|
2014-12-23 07:04:20 +00:00
|
|
|
mPlayState == PLAY_STATE_LOADING) {
|
2008-12-09 00:43:56 +00:00
|
|
|
return;
|
2014-09-02 18:07:00 +00:00
|
|
|
}
|
2008-12-09 00:43:56 +00:00
|
|
|
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2012-07-31 12:17:22 +00:00
|
|
|
|
|
|
|
for (int32_t i = mOutputStreams.Length() - 1; i >= 0; --i) {
|
|
|
|
OutputStreamData& os = mOutputStreams[i];
|
2013-01-30 04:19:03 +00:00
|
|
|
if (os.mStream->IsDestroyed()) {
|
|
|
|
// Probably the DOM MediaStream was GCed. Clean up.
|
2014-12-23 07:03:15 +00:00
|
|
|
MOZ_ASSERT(os.mPort, "Double-delete of the ports!");
|
|
|
|
os.mPort->Destroy();
|
2013-01-30 04:19:03 +00:00
|
|
|
mOutputStreams.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-31 12:17:22 +00:00
|
|
|
if (os.mFinishWhenEnded) {
|
|
|
|
// Shouldn't really be needed since mDecodedStream should already have
|
|
|
|
// finished, but doesn't hurt.
|
|
|
|
os.mStream->Finish();
|
2014-12-23 07:03:15 +00:00
|
|
|
MOZ_ASSERT(os.mPort, "Double-delete of the ports!");
|
|
|
|
os.mPort->Destroy();
|
2012-07-31 12:17:22 +00:00
|
|
|
// Not really needed but it keeps the invariant that a stream not
|
|
|
|
// connected to mDecodedStream is explicity blocked.
|
|
|
|
os.mStream->ChangeExplicitBlockerCount(1);
|
|
|
|
mOutputStreams.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 21:54:16 +00:00
|
|
|
PlaybackPositionChanged();
|
2009-04-01 01:07:10 +00:00
|
|
|
ChangeState(PLAY_STATE_ENDED);
|
2013-10-02 03:05:34 +00:00
|
|
|
InvalidateWithFlags(VideoFrameContainer::INVALIDATE_FORCE);
|
2009-04-01 01:07:10 +00:00
|
|
|
|
2013-01-05 04:00:42 +00:00
|
|
|
UpdateReadyStateForData();
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
|
|
|
mOwner->PlaybackEnded();
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2011-08-01 18:11:20 +00:00
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
// This must be called after |mOwner->PlaybackEnded()| call above, in order
|
2011-08-01 18:11:20 +00:00
|
|
|
// to fire the required durationchange.
|
|
|
|
if (IsInfinite()) {
|
2011-09-29 23:34:37 +00:00
|
|
|
SetInfinite(false);
|
2011-08-01 18:11:20 +00:00
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
NS_IMETHODIMP MediaDecoder::Observe(nsISupports *aSubjet,
|
2010-04-27 08:53:44 +00:00
|
|
|
const char *aTopic,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *someData)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-07-30 06:50:14 +00:00
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::Statistics
|
|
|
|
MediaDecoder::GetStatistics()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2009-02-05 08:02:21 +00:00
|
|
|
Statistics result;
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
2012-11-06 22:33:02 +00:00
|
|
|
result.mDownloadRate =
|
2012-02-15 04:35:01 +00:00
|
|
|
mResource->GetDownloadRate(&result.mDownloadRateReliable);
|
2009-04-01 00:52:56 +00:00
|
|
|
result.mDownloadPosition =
|
2012-02-15 04:35:01 +00:00
|
|
|
mResource->GetCachedDataEnd(mDecoderPosition);
|
|
|
|
result.mTotalBytes = mResource->GetLength();
|
2009-04-01 00:52:56 +00:00
|
|
|
result.mPlaybackRate = ComputePlaybackRate(&result.mPlaybackRateReliable);
|
|
|
|
result.mDecoderPosition = mDecoderPosition;
|
|
|
|
result.mPlaybackPosition = mPlaybackPosition;
|
2010-04-27 08:53:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-04-01 00:52:56 +00:00
|
|
|
result.mDownloadRate = 0;
|
2011-09-29 23:34:37 +00:00
|
|
|
result.mDownloadRateReliable = true;
|
2009-04-01 00:52:56 +00:00
|
|
|
result.mPlaybackRate = 0;
|
2011-09-29 23:34:37 +00:00
|
|
|
result.mPlaybackRateReliable = true;
|
2009-04-01 00:52:56 +00:00
|
|
|
result.mDecoderPosition = 0;
|
|
|
|
result.mPlaybackPosition = 0;
|
|
|
|
result.mDownloadPosition = 0;
|
|
|
|
result.mTotalBytes = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-05 08:02:21 +00:00
|
|
|
return result;
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
double MediaDecoder::ComputePlaybackRate(bool* aReliable)
|
2009-02-05 08:02:21 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2013-11-19 14:01:14 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() || OnStateMachineThread() || OnDecodeThread());
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t length = mResource ? mResource->GetLength() : -1;
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mDuration >= 0 && length >= 0) {
|
2011-09-29 23:34:37 +00:00
|
|
|
*aReliable = true;
|
2011-04-13 22:12:23 +00:00
|
|
|
return length * static_cast<double>(USECS_PER_S) / mDuration;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2014-06-22 04:43:00 +00:00
|
|
|
return mPlaybackStatistics->GetRateAtLastStop(aReliable);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-02-05 08:02:21 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::UpdatePlaybackRate()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() || OnStateMachineThread());
|
2011-04-29 19:21:57 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2012-02-15 04:35:01 +00:00
|
|
|
if (!mResource)
|
2009-04-01 00:52:56 +00:00
|
|
|
return;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool reliable;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t rate = uint32_t(ComputePlaybackRate(&reliable));
|
2009-05-18 23:05:13 +00:00
|
|
|
if (reliable) {
|
|
|
|
// Avoid passing a zero rate
|
2013-01-15 12:22:03 +00:00
|
|
|
rate = std::max(rate, 1u);
|
2010-04-27 08:53:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-04-01 00:52:56 +00:00
|
|
|
// Set a minimum rate of 10,000 bytes per second ... sometimes we just
|
|
|
|
// don't have good data
|
2013-01-15 12:22:03 +00:00
|
|
|
rate = std::max(rate, 10000u);
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
2012-02-15 04:35:01 +00:00
|
|
|
mResource->SetPlaybackRate(rate);
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::NotifySuspendedStatusChanged()
|
2009-02-05 08:02:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-24 07:25:00 +00:00
|
|
|
if (mResource && mOwner) {
|
|
|
|
bool suspended = mResource->IsSuspendedByCache();
|
2012-11-09 05:52:53 +00:00
|
|
|
mOwner->NotifySuspendedByCache(suspended);
|
2012-05-27 23:08:59 +00:00
|
|
|
UpdateReadyStateForData();
|
2012-11-06 22:33:02 +00:00
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::NotifyBytesDownloaded()
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-12-13 19:42:45 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
}
|
2014-11-27 23:17:18 +00:00
|
|
|
if (mOwner) {
|
|
|
|
mOwner->DownloadProgressed();
|
|
|
|
}
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::NotifyDownloadEnded(nsresult aStatus)
|
2008-07-30 06:50:14 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2014-08-21 00:13:00 +00:00
|
|
|
DECODER_LOG("NotifyDownloadEnded, status=%x", aStatus);
|
|
|
|
|
2010-09-03 00:03:03 +00:00
|
|
|
if (aStatus == NS_BINDING_ABORTED) {
|
|
|
|
// Download has been cancelled by user.
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
|
|
|
mOwner->LoadAborted();
|
2011-09-23 04:27:18 +00:00
|
|
|
}
|
2008-12-09 00:43:56 +00:00
|
|
|
return;
|
2010-09-03 00:03:03 +00:00
|
|
|
}
|
2008-12-09 00:43:56 +00:00
|
|
|
|
2009-02-05 08:02:21 +00:00
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
UpdatePlaybackRate();
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
2009-02-05 08:02:21 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2014-09-24 07:25:00 +00:00
|
|
|
// A final progress event will be fired by the MediaResource calling
|
|
|
|
// DownloadSuspended on the element.
|
2014-09-24 07:25:00 +00:00
|
|
|
// Also NotifySuspendedStatusChanged() will be called to update readyState
|
|
|
|
// if download ended with success.
|
2014-09-24 07:25:00 +00:00
|
|
|
} else if (aStatus != NS_BASE_STREAM_CLOSED) {
|
2009-02-05 08:02:21 +00:00
|
|
|
NetworkError();
|
|
|
|
}
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::NotifyPrincipalChanged()
|
2012-04-30 03:12:42 +00:00
|
|
|
{
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
|
|
|
mOwner->NotifyDecoderPrincipalChanged();
|
2012-04-30 03:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 03:37:23 +00:00
|
|
|
void MediaDecoder::NotifyBytesConsumed(int64_t aBytes, int64_t aOffset)
|
2009-02-05 08:02:21 +00:00
|
|
|
{
|
2014-05-11 23:20:00 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2014-05-11 23:20:00 +00:00
|
|
|
MOZ_ASSERT(mDecoderStateMachine);
|
2013-09-18 03:37:23 +00:00
|
|
|
if (mIgnoreProgressData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aOffset >= mDecoderPosition) {
|
2014-06-22 04:43:00 +00:00
|
|
|
mPlaybackStatistics->AddBytes(aBytes);
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
2013-09-18 03:37:23 +00:00
|
|
|
mDecoderPosition = aOffset + aBytes;
|
2009-02-05 08:02:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::UpdateReadyStateForData()
|
2009-02-05 08:02:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-11-09 05:52:53 +00:00
|
|
|
if (!mOwner || mShuttingDown || !mDecoderStateMachine)
|
2008-12-09 00:43:56 +00:00
|
|
|
return;
|
2012-11-14 19:45:31 +00:00
|
|
|
MediaDecoderOwner::NextFrameStatus frameStatus =
|
2010-04-27 08:53:44 +00:00
|
|
|
mDecoderStateMachine->GetNextFrameStatus();
|
2012-11-09 05:52:53 +00:00
|
|
|
mOwner->UpdateReadyStateForData(frameStatus);
|
2008-07-30 06:50:14 +00:00
|
|
|
}
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::SeekingStopped()
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2008-12-09 00:43:56 +00:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool seekWasAborted = false;
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2008-10-19 07:39:21 +00:00
|
|
|
|
|
|
|
// An additional seek was requested while the current seek was
|
|
|
|
// in operation.
|
2014-04-01 03:39:04 +00:00
|
|
|
if (mRequestedSeekTarget.IsValid()) {
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
2011-09-29 23:34:37 +00:00
|
|
|
seekWasAborted = true;
|
2010-08-05 07:40:35 +00:00
|
|
|
} else {
|
|
|
|
UnpinForSeek();
|
2008-10-19 07:39:21 +00:00
|
|
|
ChangeState(mNextState);
|
2010-08-05 07:40:35 +00:00
|
|
|
}
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 03:39:04 +00:00
|
|
|
PlaybackPositionChanged();
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
2009-02-05 08:02:21 +00:00
|
|
|
UpdateReadyStateForData();
|
2011-02-01 02:57:13 +00:00
|
|
|
if (!seekWasAborted) {
|
2012-11-09 05:52:53 +00:00
|
|
|
mOwner->SeekCompleted();
|
2011-02-01 02:57:13 +00:00
|
|
|
}
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-31 10:02:17 +00:00
|
|
|
// This is called when seeking stopped *and* we're at the end of the
|
|
|
|
// media.
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::SeekingStoppedAtEnd()
|
2009-05-31 10:02:17 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-04-02 03:03:07 +00:00
|
|
|
|
2009-05-31 10:02:17 +00:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool fireEnded = false;
|
|
|
|
bool seekWasAborted = false;
|
2009-05-31 10:02:17 +00:00
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2009-05-31 10:02:17 +00:00
|
|
|
|
|
|
|
// An additional seek was requested while the current seek was
|
|
|
|
// in operation.
|
2014-04-01 03:39:04 +00:00
|
|
|
if (mRequestedSeekTarget.IsValid()) {
|
2009-05-31 10:02:17 +00:00
|
|
|
ChangeState(PLAY_STATE_SEEKING);
|
2011-09-29 23:34:37 +00:00
|
|
|
seekWasAborted = true;
|
2010-08-05 07:40:35 +00:00
|
|
|
} else {
|
|
|
|
UnpinForSeek();
|
2011-09-29 23:34:37 +00:00
|
|
|
fireEnded = true;
|
2011-08-29 01:35:52 +00:00
|
|
|
ChangeState(PLAY_STATE_ENDED);
|
2009-05-31 10:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 03:39:04 +00:00
|
|
|
PlaybackPositionChanged();
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
2009-05-31 10:02:17 +00:00
|
|
|
UpdateReadyStateForData();
|
2011-02-01 02:57:13 +00:00
|
|
|
if (!seekWasAborted) {
|
2012-11-09 05:52:53 +00:00
|
|
|
mOwner->SeekCompleted();
|
2011-02-01 02:57:13 +00:00
|
|
|
if (fireEnded) {
|
2012-11-09 05:52:53 +00:00
|
|
|
mOwner->PlaybackEnded();
|
2011-02-01 02:57:13 +00:00
|
|
|
}
|
2009-05-31 10:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::SeekingStarted()
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-12-09 00:43:56 +00:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
2008-10-19 07:39:21 +00:00
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner) {
|
2009-05-18 22:44:17 +00:00
|
|
|
UpdateReadyStateForData();
|
2012-11-09 05:52:53 +00:00
|
|
|
mOwner->SeekStarted();
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::ChangeState(PlayState aState)
|
2008-10-19 07:39:21 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2008-10-19 07:39:21 +00:00
|
|
|
|
|
|
|
if (mNextState == aState) {
|
|
|
|
mNextState = PLAY_STATE_PAUSED;
|
|
|
|
}
|
|
|
|
|
2014-12-13 01:29:24 +00:00
|
|
|
if (mPlayState == PLAY_STATE_SHUTDOWN) {
|
2012-09-17 20:45:38 +00:00
|
|
|
GetReentrantMonitor().NotifyAll();
|
2008-10-19 07:39:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-31 12:17:22 +00:00
|
|
|
if (mDecodedStream) {
|
|
|
|
bool blockForPlayState = aState != PLAY_STATE_PLAYING;
|
|
|
|
if (mDecodedStream->mHaveBlockedForPlayState != blockForPlayState) {
|
|
|
|
mDecodedStream->mStream->ChangeExplicitBlockerCount(blockForPlayState ? 1 : -1);
|
|
|
|
mDecodedStream->mHaveBlockedForPlayState = blockForPlayState;
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 00:13:00 +00:00
|
|
|
|
|
|
|
DECODER_LOG("ChangeState %s => %s",
|
|
|
|
gPlayStateStr[mPlayState], gPlayStateStr[aState]);
|
2008-10-19 07:39:21 +00:00
|
|
|
mPlayState = aState;
|
2013-09-23 09:53:36 +00:00
|
|
|
|
2014-07-04 03:55:06 +00:00
|
|
|
if (mPlayState == PLAY_STATE_PLAYING) {
|
|
|
|
ConstructMediaTracks();
|
|
|
|
} else if (mPlayState == PLAY_STATE_ENDED) {
|
|
|
|
RemoveMediaTracks();
|
|
|
|
}
|
|
|
|
|
2013-09-23 09:53:36 +00:00
|
|
|
ApplyStateToStateMachine(mPlayState);
|
2013-06-10 12:22:05 +00:00
|
|
|
|
2015-01-28 14:31:31 +00:00
|
|
|
CancelDormantTimer();
|
|
|
|
// Start dormant timer if necessary
|
|
|
|
StartDormantTimer();
|
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
GetReentrantMonitor().NotifyAll();
|
2008-10-19 07:39:21 +00:00
|
|
|
}
|
2008-10-23 08:02:18 +00:00
|
|
|
|
2013-09-23 09:53:36 +00:00
|
|
|
void MediaDecoder::ApplyStateToStateMachine(PlayState aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
switch (aState) {
|
|
|
|
case PLAY_STATE_PLAYING:
|
|
|
|
mDecoderStateMachine->Play();
|
|
|
|
break;
|
|
|
|
case PLAY_STATE_SEEKING:
|
2014-04-01 03:39:04 +00:00
|
|
|
mDecoderStateMachine->Seek(mRequestedSeekTarget);
|
|
|
|
mRequestedSeekTarget.Reset();
|
2013-09-23 09:53:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-01-30 06:16:28 +00:00
|
|
|
// The state machine checks for things like PAUSED in RunStateMachine.
|
|
|
|
// Make sure to keep it in the loop.
|
|
|
|
ScheduleStateMachineThread();
|
2013-09-23 09:53:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::PlaybackPositionChanged()
|
2008-10-23 08:02:18 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-12-09 00:43:56 +00:00
|
|
|
if (mShuttingDown)
|
|
|
|
return;
|
|
|
|
|
2011-01-17 03:03:00 +00:00
|
|
|
double lastTime = mCurrentTime;
|
2008-10-23 08:02:18 +00:00
|
|
|
|
|
|
|
// Control the scope of the monitor so it is not
|
|
|
|
// held while the timeupdate and the invalidate is run.
|
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2010-04-27 08:53:44 +00:00
|
|
|
if (mDecoderStateMachine) {
|
2014-07-21 21:34:00 +00:00
|
|
|
// Don't update the official playback position when paused which is
|
|
|
|
// expected by the script. (The current playback position might be still
|
|
|
|
// advancing for a while after paused.)
|
|
|
|
if (!IsSeeking() && mPlayState != PLAY_STATE_PAUSED) {
|
2012-02-14 08:48:16 +00:00
|
|
|
// Only update the current playback position if we're not seeking.
|
|
|
|
// If we are seeking, the update could have been scheduled on the
|
|
|
|
// state machine thread while we were playing but after the seek
|
|
|
|
// algorithm set the current playback position on the main thread,
|
|
|
|
// and we don't want to override the seek algorithm and change the
|
|
|
|
// current time after the seek has started but before it has
|
|
|
|
// completed.
|
2013-12-12 12:33:00 +00:00
|
|
|
if (GetDecodedStream()) {
|
|
|
|
mCurrentTime = mDecoderStateMachine->GetCurrentTimeViaMediaStreamSync()/
|
|
|
|
static_cast<double>(USECS_PER_S);
|
|
|
|
} else {
|
|
|
|
mCurrentTime = mDecoderStateMachine->GetCurrentTime();
|
|
|
|
}
|
2012-02-14 08:48:16 +00:00
|
|
|
}
|
2010-04-27 08:53:44 +00:00
|
|
|
mDecoderStateMachine->ClearPositionChangeFlag();
|
2008-10-23 08:02:18 +00: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();
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner && lastTime != mCurrentTime) {
|
2010-09-10 05:49:26 +00:00
|
|
|
FireTimeUpdate();
|
2008-10-23 08:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-10 01:38:02 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::DurationChanged()
|
2010-04-02 03:03:07 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t oldDuration = mDuration;
|
2010-04-27 08:53:44 +00:00
|
|
|
mDuration = mDecoderStateMachine ? mDecoderStateMachine->GetDuration() : -1;
|
2010-07-20 01:29:27 +00:00
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
|
2014-03-28 15:41:59 +00:00
|
|
|
SetInfinite(mDuration == -1);
|
|
|
|
|
2012-11-09 05:52:53 +00:00
|
|
|
if (mOwner && oldDuration != mDuration && !IsInfinite()) {
|
2014-08-21 00:13:00 +00:00
|
|
|
DECODER_LOG("Duration changed to %lld", mDuration);
|
2015-01-19 04:11:43 +00:00
|
|
|
mOwner->DispatchAsyncEvent(NS_LITERAL_STRING("durationchange"));
|
2010-04-02 03:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::SetDuration(double aDuration)
|
2009-02-07 10:10:34 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-05-01 20:55:13 +00:00
|
|
|
if (mozilla::IsInfinite(aDuration)) {
|
2013-01-24 19:28:48 +00:00
|
|
|
SetInfinite(true);
|
2013-05-01 20:55:13 +00:00
|
|
|
} else if (IsNaN(aDuration)) {
|
2013-01-24 19:28:48 +00:00
|
|
|
mDuration = -1;
|
|
|
|
SetInfinite(true);
|
|
|
|
} else {
|
|
|
|
mDuration = static_cast<int64_t>(NS_round(aDuration * static_cast<double>(USECS_PER_S)));
|
|
|
|
}
|
2010-07-20 01:29:27 +00:00
|
|
|
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2010-04-27 08:53:44 +00:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetDuration(mDuration);
|
2009-02-07 10:10:34 +00:00
|
|
|
}
|
2010-07-20 01:29:27 +00:00
|
|
|
|
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
2009-02-07 10:10:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
void MediaDecoder::SetMediaDuration(int64_t aDuration)
|
|
|
|
{
|
2012-12-24 04:45:57 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(GetStateMachine());
|
2012-11-19 15:11:21 +00:00
|
|
|
GetStateMachine()->SetDuration(aDuration);
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:45:33 +00:00
|
|
|
void MediaDecoder::UpdateEstimatedMediaDuration(int64_t aDuration)
|
2013-05-03 07:48:37 +00:00
|
|
|
{
|
2013-09-10 00:45:33 +00:00
|
|
|
if (mPlayState <= PLAY_STATE_LOADING) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-03 07:48:37 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(GetStateMachine());
|
2013-09-10 00:45:33 +00:00
|
|
|
GetStateMachine()->UpdateEstimatedDuration(aDuration);
|
2013-05-03 07:48:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 13:17:54 +00:00
|
|
|
void MediaDecoder::SetMediaSeekable(bool aMediaSeekable) {
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread() || OnDecodeThread());
|
|
|
|
mMediaSeekable = aMediaSeekable;
|
|
|
|
}
|
|
|
|
|
2014-06-23 10:08:34 +00:00
|
|
|
bool
|
|
|
|
MediaDecoder::IsTransportSeekable()
|
2008-11-10 01:38:02 +00:00
|
|
|
{
|
2013-03-01 18:55:01 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2014-06-23 10:08:34 +00:00
|
|
|
MOZ_ASSERT(OnDecodeThread() || NS_IsMainThread());
|
|
|
|
return GetResource()->IsTransportSeekable();
|
2008-11-10 01:38:02 +00:00
|
|
|
}
|
2009-01-07 03:33:42 +00:00
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
bool MediaDecoder::IsMediaSeekable()
|
|
|
|
{
|
2012-12-13 19:42:45 +00:00
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), false);
|
2012-11-30 13:17:54 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
MOZ_ASSERT(OnDecodeThread() || NS_IsMainThread());
|
|
|
|
return mMediaSeekable;
|
2012-11-19 15:11:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 04:22:47 +00:00
|
|
|
nsresult MediaDecoder::GetSeekable(dom::TimeRanges* aSeekable)
|
2011-08-09 10:10:48 +00:00
|
|
|
{
|
|
|
|
double initialTime = 0.0;
|
|
|
|
|
2012-11-30 13:17:54 +00:00
|
|
|
// We can seek in buffered range if the media is seekable. Also, we can seek
|
|
|
|
// in unbuffered ranges if the transport level is seekable (local file or the
|
|
|
|
// server supports range requests, etc.)
|
|
|
|
if (!IsMediaSeekable()) {
|
|
|
|
return NS_OK;
|
2012-12-07 13:30:03 +00:00
|
|
|
} else if (!IsTransportSeekable()) {
|
|
|
|
return GetBuffered(aSeekable);
|
2012-11-30 13:17:54 +00:00
|
|
|
} else {
|
2011-08-09 10:10:48 +00:00
|
|
|
double end = IsInfinite() ? std::numeric_limits<double>::infinity()
|
|
|
|
: initialTime + GetDuration();
|
|
|
|
aSeekable->Add(initialTime, end);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
void MediaDecoder::SetFragmentEndTime(double aTime)
|
2011-08-24 23:42:23 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-08-24 23:42:23 +00:00
|
|
|
if (mDecoderStateMachine) {
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2012-08-22 15:56:38 +00:00
|
|
|
mDecoderStateMachine->SetFragmentEndTime(static_cast<int64_t>(aTime * USECS_PER_S));
|
2011-08-24 23:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
void MediaDecoder::SetMediaEndTime(int64_t aTime)
|
|
|
|
{
|
2012-12-24 04:45:57 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(GetStateMachine());
|
2012-11-19 15:11:21 +00:00
|
|
|
GetStateMachine()->SetMediaEndTime(aTime);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::Suspend()
|
2009-01-21 23:54:40 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
|
|
|
mResource->Suspend(true);
|
2009-01-21 23:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::Resume(bool aForceBuffering)
|
2009-01-21 23:54:40 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
|
|
|
mResource->Resume();
|
2009-01-21 23:54:40 +00:00
|
|
|
}
|
2010-07-22 22:48:32 +00:00
|
|
|
if (aForceBuffering) {
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2012-01-19 18:29:12 +00:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->StartBuffering();
|
|
|
|
}
|
2010-07-22 22:48:32 +00:00
|
|
|
}
|
2009-01-21 23:54:40 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::StopProgressUpdates()
|
2009-01-15 20:26:51 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(OnStateMachineThread() || OnDecodeThread());
|
2011-07-12 03:39:23 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-09-29 23:34:37 +00:00
|
|
|
mIgnoreProgressData = true;
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
2012-11-14 19:46:40 +00:00
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_METADATA);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-01-15 20:26:51 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::StartProgressUpdates()
|
2009-01-15 20:26:51 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(OnStateMachineThread() || OnDecodeThread());
|
2011-07-12 03:39:23 +00:00
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
2011-09-29 23:34:37 +00:00
|
|
|
mIgnoreProgressData = false;
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
2012-11-14 19:46:40 +00:00
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_PLAYBACK);
|
2012-02-15 04:35:01 +00:00
|
|
|
mDecoderPosition = mPlaybackPosition = mResource->Tell();
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-01-15 20:26:51 +00:00
|
|
|
}
|
2009-04-10 01:28:24 +00:00
|
|
|
|
2014-12-23 01:16:05 +00:00
|
|
|
void MediaDecoder::SetLoadInBackground(bool aLoadInBackground)
|
2009-04-10 01:28:24 +00:00
|
|
|
{
|
2012-11-17 01:37:46 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-15 04:35:01 +00:00
|
|
|
if (mResource) {
|
2014-12-23 01:16:05 +00:00
|
|
|
mResource->SetLoadInBackground(aLoadInBackground);
|
2009-04-10 01:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-27 08:53:45 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::UpdatePlaybackOffset(int64_t aOffset)
|
2010-04-27 08:53:45 +00:00
|
|
|
{
|
2012-09-17 20:45:38 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
2013-01-15 12:22:03 +00:00
|
|
|
mPlaybackPosition = std::max(aOffset, mPlaybackPosition);
|
2010-04-27 08:53:45 +00:00
|
|
|
}
|
2011-07-12 03:39:34 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::OnStateMachineThread() const
|
2011-11-22 00:34:21 +00:00
|
|
|
{
|
2014-02-17 22:53:52 +00:00
|
|
|
return mDecoderStateMachine->OnStateMachineThread();
|
2011-07-12 03:39:34 +00:00
|
|
|
}
|
2011-11-22 00:34:21 +00:00
|
|
|
|
2012-11-22 10:38:28 +00:00
|
|
|
void MediaDecoder::SetPlaybackRate(double aPlaybackRate)
|
|
|
|
{
|
2014-09-01 04:52:39 +00:00
|
|
|
if (aPlaybackRate == 0.0) {
|
2012-11-22 10:38:28 +00:00
|
|
|
mPausedForPlaybackRateNull = true;
|
2014-09-01 04:52:39 +00:00
|
|
|
mInitialPlaybackRate = aPlaybackRate;
|
2012-11-22 10:38:28 +00:00
|
|
|
Pause();
|
|
|
|
return;
|
|
|
|
} else if (mPausedForPlaybackRateNull) {
|
2014-09-01 04:52:39 +00:00
|
|
|
// Play() uses mPausedForPlaybackRateNull value, so must reset it first
|
|
|
|
mPausedForPlaybackRateNull = false;
|
2012-11-22 10:38:28 +00:00
|
|
|
// If the playbackRate is no longer null, restart the playback, iff the
|
|
|
|
// media was playing.
|
|
|
|
if (mOwner && !mOwner->GetPaused()) {
|
|
|
|
Play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetPlaybackRate(aPlaybackRate);
|
2013-02-26 08:52:23 +00:00
|
|
|
} else {
|
|
|
|
mInitialPlaybackRate = aPlaybackRate;
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaDecoder::SetPreservesPitch(bool aPreservesPitch)
|
|
|
|
{
|
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->SetPreservesPitch(aPreservesPitch);
|
2013-02-26 08:52:23 +00:00
|
|
|
} else {
|
|
|
|
mInitialPreservesPitch = aPreservesPitch;
|
2012-11-22 10:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::OnDecodeThread() const {
|
2013-01-30 20:14:43 +00:00
|
|
|
NS_WARN_IF_FALSE(mDecoderStateMachine, "mDecoderStateMachine is null");
|
2013-01-23 19:24:41 +00:00
|
|
|
return mDecoderStateMachine ? mDecoderStateMachine->OnDecodeThread() : false;
|
2012-11-06 22:33:02 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
ReentrantMonitor& MediaDecoder::GetReentrantMonitor() {
|
2012-11-06 22:33:02 +00:00
|
|
|
return mReentrantMonitor.GetReentrantMonitor();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
ImageContainer* MediaDecoder::GetImageContainer()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return mVideoFrameContainer ? mVideoFrameContainer->GetImageContainer() : nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-02 03:05:34 +00:00
|
|
|
void MediaDecoder::InvalidateWithFlags(uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (mVideoFrameContainer) {
|
|
|
|
mVideoFrameContainer->InvalidateWithFlags(aFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::Invalidate()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
if (mVideoFrameContainer) {
|
|
|
|
mVideoFrameContainer->Invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:33:02 +00:00
|
|
|
// Constructs the time ranges representing what segments of the media
|
|
|
|
// are buffered and playable.
|
2013-11-18 04:22:47 +00:00
|
|
|
nsresult MediaDecoder::GetBuffered(dom::TimeRanges* aBuffered) {
|
2014-08-21 00:13:00 +00:00
|
|
|
NS_ENSURE_TRUE(mDecoderStateMachine, NS_ERROR_FAILURE);
|
|
|
|
return mDecoderStateMachine->GetBuffered(aBuffered);
|
2012-11-06 22:33:02 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:33:12 +00:00
|
|
|
size_t MediaDecoder::SizeOfVideoQueue() {
|
2012-11-06 22:33:02 +00:00
|
|
|
if (mDecoderStateMachine) {
|
2014-03-19 21:33:12 +00:00
|
|
|
return mDecoderStateMachine->SizeOfVideoQueue();
|
2012-11-06 22:33:02 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-23 03:14:45 +00:00
|
|
|
size_t MediaDecoder::SizeOfAudioQueue() {
|
2012-11-06 22:33:02 +00:00
|
|
|
if (mDecoderStateMachine) {
|
2014-01-23 03:14:45 +00:00
|
|
|
return mDecoderStateMachine->SizeOfAudioQueue();
|
2012-11-06 22:33:02 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::NotifyDataArrived(const char* aBuffer, uint32_t aLength, int64_t aOffset) {
|
2012-11-06 22:33:02 +00:00
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->NotifyDataArrived(aBuffer, aLength, aOffset);
|
|
|
|
}
|
2014-11-04 01:19:14 +00:00
|
|
|
UpdateReadyStateForData();
|
2012-11-06 22:33:02 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::UpdatePlaybackPosition(int64_t aTime)
|
2012-11-06 22:33:02 +00:00
|
|
|
{
|
|
|
|
mDecoderStateMachine->UpdatePlaybackPosition(aTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide access to the state machine object
|
2012-11-19 15:11:21 +00:00
|
|
|
MediaDecoderStateMachine* MediaDecoder::GetStateMachine() const {
|
2012-11-06 22:33:02 +00:00
|
|
|
return mDecoderStateMachine;
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:44:09 +00:00
|
|
|
void
|
|
|
|
MediaDecoder::NotifyWaitingForResourcesStatusChanged()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
if (mDecoderStateMachine) {
|
|
|
|
mDecoderStateMachine->NotifyWaitingForResourcesStatusChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 15:11:21 +00:00
|
|
|
bool MediaDecoder::IsShutdown() const {
|
2012-12-13 19:42:45 +00:00
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), true);
|
2012-11-19 15:11:21 +00:00
|
|
|
return GetStateMachine()->IsShutdown();
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:33:02 +00:00
|
|
|
// Drop reference to state machine. Only called during shutdown dance.
|
2014-06-18 05:07:02 +00:00
|
|
|
void MediaDecoder::BreakCycles() {
|
2012-11-06 22:33:02 +00:00
|
|
|
mDecoderStateMachine = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoderOwner* MediaDecoder::GetMediaOwner() const
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return mOwner;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::FireTimeUpdate()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
if (!mOwner)
|
|
|
|
return;
|
|
|
|
mOwner->FireTimeUpdate(true);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::PinForSeek()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
MediaResource* resource = GetResource();
|
|
|
|
if (!resource || mPinnedForSeek) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPinnedForSeek = true;
|
|
|
|
resource->Pin();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaDecoder::UnpinForSeek()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
MediaResource* resource = GetResource();
|
|
|
|
if (!resource || !mPinnedForSeek) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPinnedForSeek = false;
|
|
|
|
resource->Unpin();
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
bool MediaDecoder::CanPlayThrough()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
Statistics stats = GetStatistics();
|
2015-01-06 01:58:04 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(mDecoderStateMachine, "CanPlayThrough should have state machine!");
|
|
|
|
if (mDecoderStateMachine->IsRealTime() ||
|
|
|
|
(stats.mTotalBytes < 0 && stats.mDownloadRateReliable) ||
|
2014-12-17 07:05:40 +00:00
|
|
|
(stats.mTotalBytes >= 0 && stats.mTotalBytes == stats.mDownloadPosition)) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-14 19:45:13 +00:00
|
|
|
if (!stats.mDownloadRateReliable || !stats.mPlaybackRateReliable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int64_t bytesToDownload = stats.mTotalBytes - stats.mDownloadPosition;
|
|
|
|
int64_t bytesToPlayback = stats.mTotalBytes - stats.mPlaybackPosition;
|
|
|
|
double timeToDownload = bytesToDownload / stats.mDownloadRate;
|
|
|
|
double timeToPlay = bytesToPlayback / stats.mPlaybackRate;
|
|
|
|
|
|
|
|
if (timeToDownload > timeToPlay) {
|
|
|
|
// Estimated time to download is greater than the estimated time to play.
|
|
|
|
// We probably can't play through without having to stop to buffer.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Estimated time to download is less than the estimated time to play.
|
|
|
|
// We can probably play through without having to buffer, but ensure that
|
|
|
|
// we've got a reasonable amount of data buffered after the current
|
|
|
|
// playback position, so that if the bitrate of the media fluctuates, or if
|
|
|
|
// our download rate or decode rate estimation is otherwise inaccurate,
|
|
|
|
// we don't suddenly discover that we need to buffer. This is particularly
|
|
|
|
// required near the start of the media, when not much data is downloaded.
|
|
|
|
int64_t readAheadMargin =
|
|
|
|
static_cast<int64_t>(stats.mPlaybackRate * CAN_PLAY_THROUGH_MARGIN);
|
2014-12-17 07:05:40 +00:00
|
|
|
return stats.mDownloadPosition > stats.mPlaybackPosition + readAheadMargin;
|
2012-11-14 19:45:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 02:44:04 +00:00
|
|
|
#ifdef MOZ_EME
|
2014-07-30 06:53:34 +00:00
|
|
|
nsresult
|
|
|
|
MediaDecoder::SetCDMProxy(CDMProxy* aProxy)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mProxy = aProxy;
|
|
|
|
// Awaken any readers waiting for the proxy.
|
|
|
|
NotifyWaitingForResourcesStatusChanged();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDMProxy*
|
|
|
|
MediaDecoder::GetCDMProxy()
|
|
|
|
{
|
|
|
|
GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
MOZ_ASSERT(OnDecodeThread() || NS_IsMainThread());
|
|
|
|
return mProxy;
|
|
|
|
}
|
2014-08-08 02:44:04 +00:00
|
|
|
#endif
|
2014-07-30 06:53:34 +00:00
|
|
|
|
2012-11-14 19:45:13 +00:00
|
|
|
#ifdef MOZ_RAW
|
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsRawEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.raw.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsOpusEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.opus.enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsOggEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.ogg.enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WAVE
|
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsWaveEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.wave.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_WEBM
|
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsWebMEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.webm.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-11 22:52:33 +00:00
|
|
|
#ifdef NECKO_PROTOCOL_rtsp
|
2013-09-23 09:53:36 +00:00
|
|
|
bool
|
|
|
|
MediaDecoder::IsRtspEnabled()
|
|
|
|
{
|
|
|
|
//Currently the Rtsp decoded by omx.
|
|
|
|
return (Preferences::GetBool("media.rtsp.enabled", false) && IsOmxEnabled());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-14 19:45:13 +00:00
|
|
|
#ifdef MOZ_GSTREAMER
|
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsGStreamerEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.gstreamer.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-02 21:21:22 +00:00
|
|
|
#ifdef MOZ_OMX_DECODER
|
2012-11-14 19:45:13 +00:00
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaDecoder::IsOmxEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.omx.enabled", false);
|
|
|
|
}
|
2014-07-16 07:40:05 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
MediaDecoder::IsOmxAsyncEnabled()
|
|
|
|
{
|
|
|
|
#if ANDROID_VERSION >= 16
|
|
|
|
return Preferences::GetBool("media.omx.async.enabled", false);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2012-11-14 19:45:13 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-17 01:32:56 +00:00
|
|
|
#ifdef MOZ_ANDROID_OMX
|
2012-11-14 19:45:13 +00:00
|
|
|
bool
|
2014-07-17 01:32:56 +00:00
|
|
|
MediaDecoder::IsAndroidMediaEnabled()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.plugins.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-18 08:49:58 +00:00
|
|
|
#ifdef MOZ_WMF
|
|
|
|
bool
|
|
|
|
MediaDecoder::IsWMFEnabled()
|
|
|
|
{
|
|
|
|
return WMFDecoder::IsEnabled();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-14 01:14:42 +00:00
|
|
|
#ifdef MOZ_APPLEMEDIA
|
|
|
|
bool
|
|
|
|
MediaDecoder::IsAppleMP3Enabled()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.apple.mp3.enabled");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-07 05:35:30 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaMemoryTracker::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-21 06:06:54 +00:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2013-01-18 05:43:20 +00:00
|
|
|
{
|
2013-11-07 05:35:30 +00:00
|
|
|
int64_t video = 0, audio = 0;
|
2014-03-05 21:31:04 +00:00
|
|
|
size_t resources = 0;
|
2013-11-07 05:35:30 +00:00
|
|
|
DecodersArray& decoders = Decoders();
|
|
|
|
for (size_t i = 0; i < decoders.Length(); ++i) {
|
2014-03-05 21:31:04 +00:00
|
|
|
MediaDecoder* decoder = decoders[i];
|
2014-03-19 21:33:12 +00:00
|
|
|
video += decoder->SizeOfVideoQueue();
|
2014-03-05 21:31:04 +00:00
|
|
|
audio += decoder->SizeOfAudioQueue();
|
|
|
|
|
|
|
|
if (decoder->GetResource()) {
|
|
|
|
resources += decoder->GetResource()->SizeOfIncludingThis(MallocSizeOf);
|
|
|
|
}
|
2013-11-07 05:35:30 +00:00
|
|
|
}
|
2013-01-18 05:43:20 +00:00
|
|
|
|
2013-11-07 05:35:30 +00:00
|
|
|
#define REPORT(_path, _amount, _desc) \
|
|
|
|
do { \
|
|
|
|
nsresult rv; \
|
|
|
|
rv = aHandleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
|
|
|
|
KIND_HEAP, UNITS_BYTES, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aData); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
} while (0)
|
2013-01-18 05:43:20 +00:00
|
|
|
|
2014-03-05 21:31:04 +00:00
|
|
|
REPORT("explicit/media/decoded/video", video,
|
2013-11-07 05:35:30 +00:00
|
|
|
"Memory used by decoded video frames.");
|
2013-01-18 05:43:20 +00:00
|
|
|
|
2014-03-05 21:31:04 +00:00
|
|
|
REPORT("explicit/media/decoded/audio", audio,
|
2013-11-07 05:35:30 +00:00
|
|
|
"Memory used by decoded audio chunks.");
|
2013-11-28 01:05:00 +00:00
|
|
|
|
2014-03-05 21:31:04 +00:00
|
|
|
REPORT("explicit/media/resources", resources,
|
|
|
|
"Memory used by media resources including streaming buffers, caches, "
|
|
|
|
"etc.");
|
|
|
|
|
2014-04-13 18:08:10 +00:00
|
|
|
#undef REPORT
|
|
|
|
|
2013-11-07 05:35:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-18 05:43:20 +00:00
|
|
|
|
2013-05-04 10:12:41 +00:00
|
|
|
MediaDecoderOwner*
|
|
|
|
MediaDecoder::GetOwner()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mOwner;
|
|
|
|
}
|
|
|
|
|
2014-07-04 03:55:06 +00:00
|
|
|
void
|
|
|
|
MediaDecoder::ConstructMediaTracks()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mMediaTracksConstructed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mOwner || !mInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLMediaElement* element = mOwner->GetMediaElement();
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMediaTracksConstructed = true;
|
|
|
|
|
|
|
|
AudioTrackList* audioList = element->AudioTracks();
|
|
|
|
if (audioList && mInfo->HasAudio()) {
|
|
|
|
TrackInfo info = mInfo->mAudio.mTrackInfo;
|
|
|
|
nsRefPtr<AudioTrack> track = MediaTrackList::CreateAudioTrack(
|
|
|
|
info.mId, info.mKind, info.mLabel, info.mLanguage, info.mEnabled);
|
|
|
|
|
|
|
|
audioList->AddTrack(track);
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoTrackList* videoList = element->VideoTracks();
|
|
|
|
if (videoList && mInfo->HasVideo()) {
|
|
|
|
TrackInfo info = mInfo->mVideo.mTrackInfo;
|
|
|
|
nsRefPtr<VideoTrack> track = MediaTrackList::CreateVideoTrack(
|
|
|
|
info.mId, info.mKind, info.mLabel, info.mLanguage);
|
|
|
|
|
|
|
|
videoList->AddTrack(track);
|
|
|
|
track->SetEnabledInternal(info.mEnabled, MediaTrack::FIRE_NO_EVENTS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoder::RemoveMediaTracks()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mOwner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLMediaElement* element = mOwner->GetMediaElement();
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioTrackList* audioList = element->AudioTracks();
|
|
|
|
if (audioList) {
|
|
|
|
audioList->RemoveTracks();
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoTrackList* videoList = element->VideoTracks();
|
|
|
|
if (videoList) {
|
|
|
|
videoList->RemoveTracks();
|
|
|
|
}
|
|
|
|
|
|
|
|
mMediaTracksConstructed = false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 05:43:20 +00:00
|
|
|
MediaMemoryTracker::MediaMemoryTracker()
|
2013-11-07 05:35:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaMemoryTracker::InitMemoryReporter()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
2013-11-07 05:35:30 +00:00
|
|
|
RegisterWeakMemoryReporter(this);
|
2012-11-14 19:45:13 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 05:43:20 +00:00
|
|
|
MediaMemoryTracker::~MediaMemoryTracker()
|
2012-11-14 19:45:13 +00:00
|
|
|
{
|
2013-11-07 05:35:30 +00:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2012-11-14 19:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2012-11-14 19:45:33 +00:00
|
|
|
|
2014-04-23 09:29:04 +00:00
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef DECODER_LOG
|