2008-07-09 01:22:20 -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: */
|
2012-05-21 04:12:37 -07: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 14:32:44 -07:00
|
|
|
|
|
|
|
#include "nsMediaDecoder.h"
|
2012-02-14 20:35:01 -08:00
|
|
|
#include "MediaResource.h"
|
2009-09-29 14:32:44 -07:00
|
|
|
|
2008-07-09 01:22:20 -07:00
|
|
|
#include "nsHTMLMediaElement.h"
|
2009-01-24 03:00:17 -08:00
|
|
|
#include "nsDOMError.h"
|
2008-07-09 01:22:20 -07:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
// Number of milliseconds between progress events as defined by spec
|
2011-09-14 12:39:50 -07:00
|
|
|
static const PRUint32 PROGRESS_MS = 350;
|
2009-01-15 12:26:51 -08:00
|
|
|
|
|
|
|
// Number of milliseconds of no data before a stall event is fired as defined by spec
|
2011-09-14 12:39:50 -07:00
|
|
|
static const PRUint32 STALL_MS = 3000;
|
2009-01-15 12:26:51 -08:00
|
|
|
|
2010-09-14 16:24:47 -07: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
|
|
|
|
// nsMediaDecoder::CanPlayThrough() calculation more stable in the case of
|
|
|
|
// fluctuating bitrates.
|
2011-09-14 12:39:50 -07:00
|
|
|
static const PRInt64 CAN_PLAY_THROUGH_MARGIN = 10;
|
2010-09-14 16:24:47 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
nsMediaDecoder::nsMediaDecoder() :
|
2011-09-22 21:27:18 -07:00
|
|
|
mElement(nsnull),
|
2010-08-25 06:10:00 -07:00
|
|
|
mFrameBufferLength(0),
|
2011-09-29 16:34:37 -07:00
|
|
|
mPinnedForSeek(false),
|
|
|
|
mShuttingDown(false)
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2008-10-19 00:39:21 -07:00
|
|
|
MOZ_COUNT_CTOR(nsMediaDecoder);
|
2011-07-21 20:17:23 -07:00
|
|
|
MediaMemoryReporter::AddMediaDecoder(this);
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
nsMediaDecoder::~nsMediaDecoder()
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2008-10-19 00:39:21 -07:00
|
|
|
MOZ_COUNT_DTOR(nsMediaDecoder);
|
2011-07-21 20:17:23 -07:00
|
|
|
MediaMemoryReporter::RemoveMediaDecoder(this);
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsMediaDecoder::Init(nsHTMLMediaElement* aElement)
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2009-01-15 23:57:37 -08:00
|
|
|
mElement = aElement;
|
2012-02-14 20:35:01 -08:00
|
|
|
mVideoFrameContainer = aElement->GetVideoFrameContainer();
|
2011-09-29 16:34:37 -07:00
|
|
|
return true;
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
void nsMediaDecoder::Shutdown()
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2008-10-19 00:39:21 -07:00
|
|
|
StopProgress();
|
2009-01-15 23:57:37 -08:00
|
|
|
mElement = nsnull;
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2009-01-24 03:00:17 -08:00
|
|
|
nsHTMLMediaElement* nsMediaDecoder::GetMediaElement()
|
|
|
|
{
|
|
|
|
return mElement;
|
|
|
|
}
|
2008-07-29 21:55:27 -07:00
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
nsresult nsMediaDecoder::RequestFrameBufferLength(PRUint32 aLength)
|
|
|
|
{
|
|
|
|
if (aLength < FRAMEBUFFER_LENGTH_MIN || aLength > FRAMEBUFFER_LENGTH_MAX) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameBufferLength = aLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-09 01:22:20 -07:00
|
|
|
static void ProgressCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
2008-10-19 00:39:21 -07:00
|
|
|
nsMediaDecoder* decoder = static_cast<nsMediaDecoder*>(aClosure);
|
2011-09-29 16:34:37 -07:00
|
|
|
decoder->Progress(true);
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void nsMediaDecoder::Progress(bool aTimer)
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2009-01-06 19:33:42 -08:00
|
|
|
if (!mElement)
|
2008-07-09 01:22:20 -07:00
|
|
|
return;
|
|
|
|
|
2009-04-11 02:39:24 -07:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2009-03-01 22:16:14 -08:00
|
|
|
|
|
|
|
if (!aTimer) {
|
|
|
|
mDataTime = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If PROGRESS_MS has passed since the last progress event fired and more
|
|
|
|
// data has arrived since then, fire another progress event.
|
2009-04-11 02:39:24 -07:00
|
|
|
if ((mProgressTime.IsNull() ||
|
|
|
|
now - mProgressTime >= TimeDuration::FromMilliseconds(PROGRESS_MS)) &&
|
|
|
|
!mDataTime.IsNull() &&
|
|
|
|
now - mDataTime <= TimeDuration::FromMilliseconds(PROGRESS_MS)) {
|
2010-09-09 20:29:06 -07:00
|
|
|
mElement->DispatchAsyncEvent(NS_LITERAL_STRING("progress"));
|
2009-01-15 12:26:51 -08:00
|
|
|
mProgressTime = now;
|
|
|
|
}
|
|
|
|
|
2009-04-11 02:39:24 -07:00
|
|
|
if (!mDataTime.IsNull() &&
|
|
|
|
now - mDataTime >= TimeDuration::FromMilliseconds(STALL_MS)) {
|
2009-05-17 19:03:37 -07:00
|
|
|
mElement->DownloadStalled();
|
2009-04-11 02:39:24 -07:00
|
|
|
// Null it out
|
|
|
|
mDataTime = TimeStamp();
|
2009-01-15 12:26:51 -08:00
|
|
|
}
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
nsresult nsMediaDecoder::StartProgress()
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2009-01-15 12:26:51 -08:00
|
|
|
if (mProgressTimer)
|
|
|
|
return NS_OK;
|
2008-12-05 22:53:23 -08:00
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
mProgressTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
return mProgressTimer->InitWithFuncCallback(ProgressCallback,
|
2009-11-05 23:33:27 -08:00
|
|
|
this,
|
2009-01-15 12:26:51 -08:00
|
|
|
PROGRESS_MS,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
nsresult nsMediaDecoder::StopProgress()
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2009-01-15 12:26:51 -08:00
|
|
|
if (!mProgressTimer)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv = mProgressTimer->Cancel();
|
|
|
|
mProgressTimer = nsnull;
|
|
|
|
|
2008-07-09 01:22:20 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-09-09 22:49:26 -07:00
|
|
|
void nsMediaDecoder::FireTimeUpdate()
|
|
|
|
{
|
|
|
|
if (!mElement)
|
|
|
|
return;
|
2011-09-29 16:34:37 -07:00
|
|
|
mElement->FireTimeUpdate(true);
|
2010-09-09 22:49:26 -07:00
|
|
|
}
|
|
|
|
|
2010-08-05 00:40:35 -07:00
|
|
|
void nsMediaDecoder::PinForSeek()
|
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = GetResource();
|
|
|
|
if (!resource || mPinnedForSeek) {
|
2010-08-05 00:40:35 -07:00
|
|
|
return;
|
|
|
|
}
|
2011-09-29 16:34:37 -07:00
|
|
|
mPinnedForSeek = true;
|
2012-02-14 20:35:01 -08:00
|
|
|
resource->Pin();
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsMediaDecoder::UnpinForSeek()
|
|
|
|
{
|
2012-02-14 20:35:01 -08:00
|
|
|
MediaResource* resource = GetResource();
|
|
|
|
if (!resource || !mPinnedForSeek) {
|
2010-08-05 00:40:35 -07:00
|
|
|
return;
|
|
|
|
}
|
2011-09-29 16:34:37 -07:00
|
|
|
mPinnedForSeek = false;
|
2012-02-14 20:35:01 -08:00
|
|
|
resource->Unpin();
|
2010-08-05 00:40:35 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsMediaDecoder::CanPlayThrough()
|
2010-07-22 15:48:32 -07:00
|
|
|
{
|
|
|
|
Statistics stats = GetStatistics();
|
|
|
|
if (!stats.mDownloadRateReliable || !stats.mPlaybackRateReliable) {
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-07-22 15:48:32 -07:00
|
|
|
}
|
|
|
|
PRInt64 bytesToDownload = stats.mTotalBytes - stats.mDownloadPosition;
|
|
|
|
PRInt64 bytesToPlayback = stats.mTotalBytes - stats.mPlaybackPosition;
|
2011-02-20 13:21:40 -08:00
|
|
|
double timeToDownload = bytesToDownload / stats.mDownloadRate;
|
|
|
|
double timeToPlay = bytesToPlayback / stats.mPlaybackRate;
|
2010-09-14 16:24:47 -07:00
|
|
|
|
|
|
|
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.
|
2011-09-29 16:34:37 -07:00
|
|
|
return false;
|
2010-09-14 16:24:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-03-23 15:28:58 -07:00
|
|
|
PRInt64 readAheadMargin =
|
|
|
|
static_cast<PRInt64>(stats.mPlaybackRate * CAN_PLAY_THROUGH_MARGIN);
|
2010-09-14 16:24:47 -07:00
|
|
|
return stats.mTotalBytes == stats.mDownloadPosition ||
|
|
|
|
stats.mDownloadPosition > stats.mPlaybackPosition + readAheadMargin;
|
2010-07-22 15:48:32 -07:00
|
|
|
}
|
2011-07-21 20:17:23 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
MediaMemoryReporter* MediaMemoryReporter::sUniqueInstance;
|
|
|
|
|
|
|
|
NS_MEMORY_REPORTER_IMPLEMENT(MediaDecodedVideoMemory,
|
2012-04-09 22:52:33 -07:00
|
|
|
"explicit/media/decoded-video",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
MediaMemoryReporter::GetDecodedVideoMemory,
|
|
|
|
"Memory used by decoded video frames.")
|
2011-07-21 20:17:23 -07:00
|
|
|
|
|
|
|
NS_MEMORY_REPORTER_IMPLEMENT(MediaDecodedAudioMemory,
|
2012-04-09 22:52:33 -07:00
|
|
|
"explicit/media/decoded-audio",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
MediaMemoryReporter::GetDecodedAudioMemory,
|
|
|
|
"Memory used by decoded audio chunks.")
|
2011-07-21 20:17:23 -07:00
|
|
|
|
|
|
|
MediaMemoryReporter::MediaMemoryReporter()
|
|
|
|
: mMediaDecodedVideoMemory(new NS_MEMORY_REPORTER_NAME(MediaDecodedVideoMemory))
|
|
|
|
, mMediaDecodedAudioMemory(new NS_MEMORY_REPORTER_NAME(MediaDecodedAudioMemory))
|
|
|
|
{
|
|
|
|
NS_RegisterMemoryReporter(mMediaDecodedVideoMemory);
|
|
|
|
NS_RegisterMemoryReporter(mMediaDecodedAudioMemory);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaMemoryReporter::~MediaMemoryReporter()
|
|
|
|
{
|
|
|
|
NS_UnregisterMemoryReporter(mMediaDecodedVideoMemory);
|
|
|
|
NS_UnregisterMemoryReporter(mMediaDecodedAudioMemory);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|