gecko/content/media/nsMediaDecoder.cpp
Robert O'Callahan b456ec8e36 Bug 499880 - Restructure content/media directory. r+=doublec
--HG--
rename : content/media/video/src/nsAudioStream.cpp => content/media/nsAudioStream.cpp
rename : content/media/video/public/nsAudioStream.h => content/media/nsAudioStream.h
rename : content/media/video/src/nsMediaCache.cpp => content/media/nsMediaCache.cpp
rename : content/media/video/public/nsMediaCache.h => content/media/nsMediaCache.h
rename : content/media/video/src/nsMediaDecoder.cpp => content/media/nsMediaDecoder.cpp
rename : content/media/video/public/nsMediaDecoder.h => content/media/nsMediaDecoder.h
rename : content/media/video/src/nsMediaStream.cpp => content/media/nsMediaStream.cpp
rename : content/media/video/public/nsMediaStream.h => content/media/nsMediaStream.h
rename : content/media/video/src/nsChannelReader.cpp => content/media/ogg/nsChannelReader.cpp
rename : content/media/video/public/nsChannelReader.h => content/media/ogg/nsChannelReader.h
rename : content/media/video/src/nsOggDecoder.cpp => content/media/ogg/nsOggDecoder.cpp
rename : content/media/video/public/nsOggDecoder.h => content/media/ogg/nsOggDecoder.h
rename : content/media/video/test/320x240.allow-origin.ogv => content/media/test/320x240.allow-origin.ogv
rename : content/media/video/test/320x240.allow-origin.ogv^headers^ => content/media/test/320x240.allow-origin.ogv^headers^
rename : content/media/video/test/320x240.ogv => content/media/test/320x240.ogv
rename : content/media/video/test/Makefile.in => content/media/test/Makefile.in
rename : content/media/video/test/big.wav => content/media/test/big.wav
rename : content/media/video/test/bug461281.ogg => content/media/test/bug461281.ogg
rename : content/media/video/test/bug482461.ogv => content/media/test/bug482461.ogv
rename : content/media/video/test/can_play_type_ogg.js => content/media/test/can_play_type_ogg.js
rename : content/media/video/test/can_play_type_wave.js => content/media/test/can_play_type_wave.js
rename : content/media/video/test/contentDuration1.sjs => content/media/test/contentDuration1.sjs
rename : content/media/video/test/contentDuration2.sjs => content/media/test/contentDuration2.sjs
rename : content/media/video/test/contentDuration3.sjs => content/media/test/contentDuration3.sjs
rename : content/media/video/test/contentDuration4.sjs => content/media/test/contentDuration4.sjs
rename : content/media/video/test/contentDuration5.sjs => content/media/test/contentDuration5.sjs
rename : content/media/video/test/contentDuration6.sjs => content/media/test/contentDuration6.sjs
rename : content/media/video/test/crashtests/468763-1.html => content/media/test/crashtests/468763-1.html
rename : content/media/video/test/crashtests/474744-1.html => content/media/test/crashtests/474744-1.html
rename : content/media/video/test/crashtests/crashtests.list => content/media/test/crashtests/crashtests.list
rename : content/media/video/test/dynamic_redirect.sjs => content/media/test/dynamic_redirect.sjs
rename : content/media/video/test/file_access_controls.html => content/media/test/file_access_controls.html
rename : content/media/video/test/r11025_s16_c1.wav => content/media/test/r11025_s16_c1.wav
rename : content/media/video/test/r11025_s16_c1_trailing.wav => content/media/test/r11025_s16_c1_trailing.wav
rename : content/media/video/test/r11025_u8_c1.wav => content/media/test/r11025_u8_c1.wav
rename : content/media/video/test/r11025_u8_c1_trunc.wav => content/media/test/r11025_u8_c1_trunc.wav
rename : content/media/video/test/r16000_u8_c1_list.wav => content/media/test/r16000_u8_c1_list.wav
rename : content/media/video/test/redirect.sjs => content/media/test/redirect.sjs
rename : content/media/video/test/seek.ogv => content/media/test/seek.ogv
rename : content/media/video/test/small-shot.ogg => content/media/test/small-shot.ogg
rename : content/media/video/test/sound.ogg => content/media/test/sound.ogg
rename : content/media/video/test/test_access_control.html => content/media/test/test_access_control.html
rename : content/media/video/test/test_audio1.html => content/media/test/test_audio1.html
rename : content/media/video/test/test_audio2.html => content/media/test/test_audio2.html
rename : content/media/video/test/test_audioDocumentTitle.html => content/media/test/test_audioDocumentTitle.html
rename : content/media/video/test/test_autobuffer.html => content/media/test/test_autobuffer.html
rename : content/media/video/test/test_autobuffer2.html => content/media/test/test_autobuffer2.html
rename : content/media/video/test/test_autoplay.html => content/media/test/test_autoplay.html
rename : content/media/video/test/test_bug448534.html => content/media/test/test_bug448534.html
rename : content/media/video/test/test_bug461281.html => content/media/test/test_bug461281.html
rename : content/media/video/test/test_bug463162.xhtml => content/media/test/test_bug463162.xhtml
rename : content/media/video/test/test_bug465498.html => content/media/test/test_bug465498.html
rename : content/media/video/test/test_bug468190.html => content/media/test/test_bug468190.html
rename : content/media/video/test/test_bug468190_wav.html => content/media/test/test_bug468190_wav.html
rename : content/media/video/test/test_bug476973.html => content/media/test/test_bug476973.html
rename : content/media/video/test/test_bug482461.html => content/media/test/test_bug482461.html
rename : content/media/video/test/test_bug493187.html => content/media/test/test_bug493187.html
rename : content/media/video/test/test_bug495145.html => content/media/test/test_bug495145.html
rename : content/media/video/test/test_bug495145_wav.html => content/media/test/test_bug495145_wav.html
rename : content/media/video/test/test_bug495300.html => content/media/test/test_bug495300.html
rename : content/media/video/test/test_bug495319.html => content/media/test/test_bug495319.html
rename : content/media/video/test/test_can_play_type.html => content/media/test/test_can_play_type.html
rename : content/media/video/test/test_can_play_type_no_ogg.html => content/media/test/test_can_play_type_no_ogg.html
rename : content/media/video/test/test_can_play_type_no_wave.html => content/media/test/test_can_play_type_no_wave.html
rename : content/media/video/test/test_can_play_type_ogg.html => content/media/test/test_can_play_type_ogg.html
rename : content/media/video/test/test_can_play_type_wave.html => content/media/test/test_can_play_type_wave.html
rename : content/media/video/test/test_closing_connections.html => content/media/test/test_closing_connections.html
rename : content/media/video/test/test_constants.html => content/media/test/test_constants.html
rename : content/media/video/test/test_contentDuration1.html => content/media/test/test_contentDuration1.html
rename : content/media/video/test/test_contentDuration2.html => content/media/test/test_contentDuration2.html
rename : content/media/video/test/test_contentDuration3.html => content/media/test/test_contentDuration3.html
rename : content/media/video/test/test_contentDuration4.html => content/media/test/test_contentDuration4.html
rename : content/media/video/test/test_contentDuration5.html => content/media/test/test_contentDuration5.html
rename : content/media/video/test/test_contentDuration6.html => content/media/test/test_contentDuration6.html
rename : content/media/video/test/test_controls.html => content/media/test/test_controls.html
rename : content/media/video/test/test_currentTime.html => content/media/test/test_currentTime.html
rename : content/media/video/test/test_decoder_disable.html => content/media/test/test_decoder_disable.html
rename : content/media/video/test/test_delay_load.html => content/media/test/test_delay_load.html
rename : content/media/video/test/test_duration1.html => content/media/test/test_duration1.html
rename : content/media/video/test/test_ended1.html => content/media/test/test_ended1.html
rename : content/media/video/test/test_ended2.html => content/media/test/test_ended2.html
rename : content/media/video/test/test_error_on_404.html => content/media/test/test_error_on_404.html
rename : content/media/video/test/test_info_leak.html => content/media/test/test_info_leak.html
rename : content/media/video/test/test_load.html => content/media/test/test_load.html
rename : content/media/video/test/test_load_candidates.html => content/media/test/test_load_candidates.html
rename : content/media/video/test/test_media_selection.html => content/media/test/test_media_selection.html
rename : content/media/video/test/test_mixed_principals.html => content/media/test/test_mixed_principals.html
rename : content/media/video/test/test_networkState.html => content/media/test/test_networkState.html
rename : content/media/video/test/test_onloadedmetadata.html => content/media/test/test_onloadedmetadata.html
rename : content/media/video/test/test_paused.html => content/media/test/test_paused.html
rename : content/media/video/test/test_paused_after_ended.html => content/media/test/test_paused_after_ended.html
rename : content/media/video/test/test_play.html => content/media/test/test_play.html
rename : content/media/video/test/test_progress1.html => content/media/test/test_progress1.html
rename : content/media/video/test/test_progress2.html => content/media/test/test_progress2.html
rename : content/media/video/test/test_progress3.html => content/media/test/test_progress3.html
rename : content/media/video/test/test_progress4.html => content/media/test/test_progress4.html
rename : content/media/video/test/test_readyState.html => content/media/test/test_readyState.html
rename : content/media/video/test/test_seek1.html => content/media/test/test_seek1.html
rename : content/media/video/test/test_seek2.html => content/media/test/test_seek2.html
rename : content/media/video/test/test_seek3.html => content/media/test/test_seek3.html
rename : content/media/video/test/test_seek4.html => content/media/test/test_seek4.html
rename : content/media/video/test/test_seek5.html => content/media/test/test_seek5.html
rename : content/media/video/test/test_seek6.html => content/media/test/test_seek6.html
rename : content/media/video/test/test_seek7.html => content/media/test/test_seek7.html
rename : content/media/video/test/test_seek8.html => content/media/test/test_seek8.html
rename : content/media/video/test/test_seek9.html => content/media/test/test_seek9.html
rename : content/media/video/test/test_source.html => content/media/test/test_source.html
rename : content/media/video/test/test_source_write.html => content/media/test/test_source_write.html
rename : content/media/video/test/test_standalone.html => content/media/test/test_standalone.html
rename : content/media/video/test/test_timeupdate1.html => content/media/test/test_timeupdate1.html
rename : content/media/video/test/test_timeupdate2.html => content/media/test/test_timeupdate2.html
rename : content/media/video/test/test_timeupdate3.html => content/media/test/test_timeupdate3.html
rename : content/media/video/test/test_videoDocumentTitle.html => content/media/test/test_videoDocumentTitle.html
rename : content/media/video/test/test_volume.html => content/media/test/test_volume.html
rename : content/media/video/test/test_wav_8bit.html => content/media/test/test_wav_8bit.html
rename : content/media/video/test/test_wav_ended1.html => content/media/test/test_wav_ended1.html
rename : content/media/video/test/test_wav_ended2.html => content/media/test/test_wav_ended2.html
rename : content/media/video/test/test_wav_list.html => content/media/test/test_wav_list.html
rename : content/media/video/test/test_wav_onloadedmetadata.html => content/media/test/test_wav_onloadedmetadata.html
rename : content/media/video/test/test_wav_seek1.html => content/media/test/test_wav_seek1.html
rename : content/media/video/test/test_wav_seek3.html => content/media/test/test_wav_seek3.html
rename : content/media/video/test/test_wav_seek4.html => content/media/test/test_wav_seek4.html
rename : content/media/video/test/test_wav_seek5.html => content/media/test/test_wav_seek5.html
rename : content/media/video/test/test_wav_seek6.html => content/media/test/test_wav_seek6.html
rename : content/media/video/test/test_wav_seek7.html => content/media/test/test_wav_seek7.html
rename : content/media/video/test/test_wav_seek8.html => content/media/test/test_wav_seek8.html
rename : content/media/video/test/test_wav_seek_past_end.html => content/media/test/test_wav_seek_past_end.html
rename : content/media/video/test/test_wav_seek_then_play.html => content/media/test/test_wav_seek_then_play.html
rename : content/media/video/test/test_wav_standalone.html => content/media/test/test_wav_standalone.html
rename : content/media/video/test/test_wav_timeupdate1.html => content/media/test/test_wav_timeupdate1.html
rename : content/media/video/test/test_wav_timeupdate2.html => content/media/test/test_wav_timeupdate2.html
rename : content/media/video/test/test_wav_trailing.html => content/media/test/test_wav_trailing.html
rename : content/media/video/test/test_wav_trunc.html => content/media/test/test_wav_trunc.html
rename : content/media/video/test/test_wav_trunc_seek.html => content/media/test/test_wav_trunc_seek.html
rename : content/media/video/test/use_large_cache.js => content/media/test/use_large_cache.js
rename : content/media/video/src/nsWaveDecoder.cpp => content/media/wave/nsWaveDecoder.cpp
rename : content/media/video/public/nsWaveDecoder.h => content/media/wave/nsWaveDecoder.h
2009-06-26 16:23:15 +12:00

304 lines
8.7 KiB
C++

/* -*- 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 *****
* Version: ML 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is the Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Double <chris.double@double.co.nz>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "prlog.h"
#include "prmem.h"
#include "nsIFrame.h"
#include "nsIDocument.h"
#include "nsThreadUtils.h"
#include "nsIDOMHTMLMediaElement.h"
#include "nsNetUtil.h"
#include "nsHTMLMediaElement.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsAutoLock.h"
#include "nsIRenderingContext.h"
#include "gfxContext.h"
#include "gfxImageSurface.h"
#include "nsPresContext.h"
#include "nsMediaDecoder.h"
#include "nsDOMError.h"
// 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
#ifdef PR_LOGGING
// Logging object for decoder
PRLogModuleInfo* gVideoDecoderLog = nsnull;
#endif
nsMediaDecoder::nsMediaDecoder() :
mElement(0),
mRGBWidth(-1),
mRGBHeight(-1),
mProgressTime(),
mDataTime(),
mVideoUpdateLock(nsnull),
mFramerate(0.0),
mAspectRatio(1.0),
mSizeChanged(PR_FALSE),
mShuttingDown(PR_FALSE)
{
MOZ_COUNT_CTOR(nsMediaDecoder);
}
nsMediaDecoder::~nsMediaDecoder()
{
if (mVideoUpdateLock) {
PR_DestroyLock(mVideoUpdateLock);
mVideoUpdateLock = nsnull;
}
MOZ_COUNT_DTOR(nsMediaDecoder);
}
PRBool nsMediaDecoder::Init(nsHTMLMediaElement* aElement)
{
mElement = aElement;
mVideoUpdateLock = PR_NewLock();
return mVideoUpdateLock != nsnull;
}
void nsMediaDecoder::Shutdown()
{
StopProgress();
mElement = nsnull;
}
nsHTMLMediaElement* nsMediaDecoder::GetMediaElement()
{
return mElement;
}
nsresult nsMediaDecoder::InitLogger()
{
#ifdef PR_LOGGING
gVideoDecoderLog = PR_NewLogModule("nsMediaDecoder");
#endif
return NS_OK;
}
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;
}
void nsMediaDecoder::Invalidate()
{
if (!mElement)
return;
nsIFrame* frame = mElement->GetPrimaryFrame();
{
nsAutoLock lock(mVideoUpdateLock);
if (mSizeChanged) {
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);
mSizeChanged = PR_FALSE;
if (frame) {
nsPresContext* presContext = frame->PresContext();
nsIPresShell *presShell = presContext->PresShell();
presShell->FrameNeedsReflow(frame,
nsIPresShell::eStyleChange,
NS_FRAME_IS_DIRTY);
}
}
}
if (frame) {
nsRect r(nsPoint(0,0), frame->GetSize());
frame->Invalidate(r);
}
}
static void ProgressCallback(nsITimer* aTimer, void* aClosure)
{
nsMediaDecoder* decoder = static_cast<nsMediaDecoder*>(aClosure);
decoder->Progress(PR_TRUE);
}
void nsMediaDecoder::Progress(PRBool aTimer)
{
if (!mElement)
return;
TimeStamp now = TimeStamp::Now();
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.
if ((mProgressTime.IsNull() ||
now - mProgressTime >= TimeDuration::FromMilliseconds(PROGRESS_MS)) &&
!mDataTime.IsNull() &&
now - mDataTime <= TimeDuration::FromMilliseconds(PROGRESS_MS)) {
mElement->DispatchAsyncProgressEvent(NS_LITERAL_STRING("progress"));
mProgressTime = now;
}
if (!mDataTime.IsNull() &&
now - mDataTime >= TimeDuration::FromMilliseconds(STALL_MS)) {
mElement->DownloadStalled();
// Null it out
mDataTime = TimeStamp();
}
}
nsresult nsMediaDecoder::StartProgress()
{
if (mProgressTimer)
return NS_OK;
mProgressTimer = do_CreateInstance("@mozilla.org/timer;1");
return mProgressTimer->InitWithFuncCallback(ProgressCallback,
this,
PROGRESS_MS,
nsITimer::TYPE_REPEATING_SLACK);
}
nsresult nsMediaDecoder::StopProgress()
{
if (!mProgressTimer)
return NS_OK;
nsresult rv = mProgressTimer->Cancel();
mProgressTimer = nsnull;
return rv;
}
void nsMediaDecoder::SetRGBData(PRInt32 aWidth, PRInt32 aHeight, float aFramerate,
float aAspectRatio, unsigned char* aRGBBuffer)
{
nsAutoLock lock(mVideoUpdateLock);
if (mRGBWidth != aWidth || mRGBHeight != aHeight ||
mAspectRatio != aAspectRatio) {
mRGBWidth = aWidth;
mRGBHeight = aHeight;
mAspectRatio = aAspectRatio;
mSizeChanged = PR_TRUE;
}
mFramerate = aFramerate;
mRGB = aRGBBuffer;
}
void nsMediaDecoder::Paint(gfxContext* aContext,
gfxPattern::GraphicsFilter aFilter,
const gfxRect& aRect)
{
nsAutoLock lock(mVideoUpdateLock);
if (!mRGB)
return;
/* Create a surface backed by the RGB */
nsRefPtr<gfxASurface> surface =
new gfxImageSurface(mRGB,
gfxIntSize(mRGBWidth, mRGBHeight),
mRGBWidth * 4,
gfxASurface::ImageFormatRGB24);
if (!surface)
return;
nsRefPtr<gfxPattern> pat = new gfxPattern(surface);
if (!pat)
return;
// Make the source image fill the rectangle completely
pat->SetMatrix(gfxMatrix().Scale(mRGBWidth/aRect.Width(), mRGBHeight/aRect.Height()));
pat->SetFilter(aFilter);
// Set PAD mode so that when the video is being scaled, we do not sample
// outside the bounds of the video image.
gfxPattern::GraphicsExtend extend = gfxPattern::EXTEND_PAD;
// PAD is too slow with X11 surfaces, so prefer speed over correctness and
// use NONE.
nsRefPtr<gfxASurface> target = aContext->CurrentSurface();
gfxASurface::gfxSurfaceType type = target->GetType();
if (type == gfxASurface::SurfaceTypeXlib ||
type == gfxASurface::SurfaceTypeXcb) {
extend = gfxPattern::EXTEND_NONE;
}
pat->SetExtend(extend);
/* Draw RGB surface onto frame */
aContext->NewPath();
aContext->PixelSnappedRectangleAndSetPattern(aRect, pat);
aContext->Fill();
#ifdef DEBUG_FRAME_RATE
{
// Output frame rate
static float last = double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
float now = double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
static int count = 0;
count++;
if (now-last > 10.0) {
LOG(PR_LOG_DEBUG, ("Paint Frame Rate = %f (should be %f)\n", (float)count / (float)(now-last), mFramerate));
count = 0;
last = double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
}
}
#endif
}