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: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2010-03-14 18:44:37 -07:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2008-07-09 01:22:20 -07:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2009-09-29 14:32:44 -07:00
|
|
|
|
|
|
|
#include "nsMediaDecoder.h"
|
|
|
|
|
2008-07-09 01:22:20 -07:00
|
|
|
#include "prlog.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIDOMHTMLMediaElement.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsHTMLMediaElement.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsIRenderingContext.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "nsPresContext.h"
|
2009-01-24 03:00:17 -08:00
|
|
|
#include "nsDOMError.h"
|
2008-07-09 01:22:20 -07:00
|
|
|
|
2009-09-21 17:12:06 -07:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
#include "gfxQuartzImageSurface.h"
|
|
|
|
#endif
|
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
// Number of milliseconds between progress events as defined by spec
|
|
|
|
#define PROGRESS_MS 350
|
|
|
|
|
|
|
|
// Number of milliseconds of no data before a stall event is fired as defined by spec
|
|
|
|
#define STALL_MS 3000
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
nsMediaDecoder::nsMediaDecoder() :
|
2008-07-09 01:22:20 -07:00
|
|
|
mElement(0),
|
|
|
|
mRGBWidth(-1),
|
|
|
|
mRGBHeight(-1),
|
2009-04-11 02:39:24 -07:00
|
|
|
mProgressTime(),
|
|
|
|
mDataTime(),
|
2008-07-09 01:22:20 -07:00
|
|
|
mVideoUpdateLock(nsnull),
|
2009-05-18 16:07:19 -07:00
|
|
|
mAspectRatio(1.0),
|
2009-04-05 17:20:51 -07:00
|
|
|
mSizeChanged(PR_FALSE),
|
2009-05-18 16:08:08 -07:00
|
|
|
mShuttingDown(PR_FALSE)
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2008-10-19 00:39:21 -07:00
|
|
|
MOZ_COUNT_CTOR(nsMediaDecoder);
|
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
|
|
|
{
|
|
|
|
if (mVideoUpdateLock) {
|
|
|
|
PR_DestroyLock(mVideoUpdateLock);
|
|
|
|
mVideoUpdateLock = nsnull;
|
|
|
|
}
|
2008-10-19 00:39:21 -07:00
|
|
|
MOZ_COUNT_DTOR(nsMediaDecoder);
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 23:57:37 -08:00
|
|
|
PRBool nsMediaDecoder::Init(nsHTMLMediaElement* aElement)
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2009-01-15 23:57:37 -08:00
|
|
|
mElement = aElement;
|
2008-10-19 00:39:21 -07:00
|
|
|
mVideoUpdateLock = PR_NewLock();
|
2008-07-09 01:22:20 -07:00
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
return mVideoUpdateLock != nsnull;
|
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
|
|
|
|
2009-05-18 16:07:19 -07:00
|
|
|
static PRInt32 ConditionDimension(float aValue, PRInt32 aDefault)
|
|
|
|
{
|
|
|
|
// This will exclude NaNs and infinities
|
|
|
|
if (aValue >= 1.0 && aValue <= 10000.0)
|
|
|
|
return PRInt32(NS_round(aValue));
|
|
|
|
return aDefault;
|
|
|
|
}
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
void nsMediaDecoder::Invalidate()
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
|
|
|
if (!mElement)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsIFrame* frame = mElement->GetPrimaryFrame();
|
2009-11-05 23:33:27 -08:00
|
|
|
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mVideoUpdateLock);
|
|
|
|
if (mSizeChanged) {
|
2009-05-18 16:07:19 -07:00
|
|
|
nsIntSize scaledSize(mRGBWidth, mRGBHeight);
|
|
|
|
// Apply the aspect ratio to produce the intrinsic size we report
|
|
|
|
// to the element.
|
|
|
|
if (mAspectRatio > 1.0) {
|
|
|
|
// Increase the intrinsic width
|
|
|
|
scaledSize.width =
|
|
|
|
ConditionDimension(mAspectRatio*scaledSize.width, scaledSize.width);
|
|
|
|
} else {
|
|
|
|
// Increase the intrinsic height
|
|
|
|
scaledSize.height =
|
|
|
|
ConditionDimension(scaledSize.height/mAspectRatio, scaledSize.height);
|
|
|
|
}
|
|
|
|
mElement->UpdateMediaSize(scaledSize);
|
|
|
|
|
2008-07-09 01:22:20 -07:00
|
|
|
mSizeChanged = PR_FALSE;
|
2009-01-15 23:57:37 -08:00
|
|
|
if (frame) {
|
2009-11-05 23:33:27 -08:00
|
|
|
nsPresContext* presContext = frame->PresContext();
|
2009-01-15 23:57:37 -08:00
|
|
|
nsIPresShell *presShell = presContext->PresShell();
|
2009-11-05 23:33:27 -08:00
|
|
|
presShell->FrameNeedsReflow(frame,
|
2009-01-15 23:57:37 -08:00
|
|
|
nsIPresShell::eStyleChange,
|
|
|
|
NS_FRAME_IS_DIRTY);
|
|
|
|
}
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
}
|
2009-01-15 23:57:37 -08:00
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
nsRect r(nsPoint(0,0), frame->GetSize());
|
|
|
|
frame->Invalidate(r);
|
|
|
|
}
|
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);
|
2009-01-15 12:26:51 -08:00
|
|
|
decoder->Progress(PR_TRUE);
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 12:26:51 -08:00
|
|
|
void nsMediaDecoder::Progress(PRBool 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)) {
|
2009-01-15 12:26:51 -08:00
|
|
|
mElement->DispatchAsyncProgressEvent(NS_LITERAL_STRING("progress"));
|
|
|
|
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-03-01 15:41:49 -08:00
|
|
|
void nsMediaDecoder::SetVideoData(const gfxIntSize& aSize,
|
|
|
|
float aAspectRatio,
|
|
|
|
Image* aImage)
|
2008-07-09 01:22:20 -07:00
|
|
|
{
|
2009-03-16 20:45:00 -07:00
|
|
|
nsAutoLock lock(mVideoUpdateLock);
|
|
|
|
|
2010-03-01 15:41:49 -08:00
|
|
|
if (mRGBWidth != aSize.width || mRGBHeight != aSize.height ||
|
2009-05-18 16:07:19 -07:00
|
|
|
mAspectRatio != aAspectRatio) {
|
2010-03-01 15:41:49 -08:00
|
|
|
mRGBWidth = aSize.width;
|
|
|
|
mRGBHeight = aSize.height;
|
2009-05-18 16:07:19 -07:00
|
|
|
mAspectRatio = aAspectRatio;
|
2008-07-09 01:22:20 -07:00
|
|
|
mSizeChanged = PR_TRUE;
|
|
|
|
}
|
2010-03-01 15:41:49 -08:00
|
|
|
if (mImageContainer && aImage) {
|
|
|
|
mImageContainer->SetCurrentImage(aImage);
|
2009-03-16 15:12:13 -07:00
|
|
|
}
|
2008-07-09 01:22:20 -07:00
|
|
|
}
|