1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: ML 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Chris Double <chris.double@double.co.nz>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 #include "nsIDocument.h"
42 #include "nsThreadUtils.h"
43 #include "nsIDOMHTMLMediaElement.h"
44 #include "nsNetUtil.h"
45 #include "nsHTMLMediaElement.h"
46 #include "nsIObserver.h"
47 #include "nsIObserverService.h"
48 #include "nsAutoLock.h"
49 #include "nsIRenderingContext.h"
50 #include "gfxContext.h"
51 #include "gfxImageSurface.h"
52 #include "nsPresContext.h"
53 #include "nsMediaDecoder.h"
56 // Logging object for decoder
57 PRLogModuleInfo
* gVideoDecoderLog
= nsnull
;
60 nsMediaDecoder::nsMediaDecoder() :
64 mSizeChanged(PR_FALSE
),
65 mVideoUpdateLock(nsnull
),
68 MOZ_COUNT_CTOR(nsMediaDecoder
);
71 nsMediaDecoder::~nsMediaDecoder()
73 if (mVideoUpdateLock
) {
74 PR_DestroyLock(mVideoUpdateLock
);
75 mVideoUpdateLock
= nsnull
;
77 MOZ_COUNT_DTOR(nsMediaDecoder
);
80 PRBool
nsMediaDecoder::Init()
82 mVideoUpdateLock
= PR_NewLock();
84 return mVideoUpdateLock
!= nsnull
;
87 void nsMediaDecoder::Shutdown()
94 nsresult
nsMediaDecoder::InitLogger()
97 gVideoDecoderLog
= PR_NewLogModule("nsMediaDecoder");
102 void nsMediaDecoder::Invalidate()
107 nsIFrame
* frame
= mElement
->GetPrimaryFrame();
112 nsAutoLock
lock(mVideoUpdateLock
);
114 mElement
->UpdateMediaSize(nsIntSize(mRGBWidth
, mRGBHeight
));
115 mSizeChanged
= PR_FALSE
;
116 nsPresContext
* presContext
= frame
->PresContext();
117 nsIPresShell
*presShell
= presContext
->PresShell();
118 presShell
->FrameNeedsReflow(frame
,
119 nsIPresShell::eStyleChange
,
123 nsRect
r(nsPoint(0,0), frame
->GetSize());
124 frame
->Invalidate(r
);
127 static void ProgressCallback(nsITimer
* aTimer
, void* aClosure
)
129 nsMediaDecoder
* decoder
= static_cast<nsMediaDecoder
*>(aClosure
);
133 void nsMediaDecoder::Progress()
138 mElement
->DispatchProgressEvent(NS_LITERAL_STRING("progress"));
141 nsresult
nsMediaDecoder::StartProgress()
145 if (!mProgressTimer
) {
146 mProgressTimer
= do_CreateInstance("@mozilla.org/timer;1");
147 rv
= mProgressTimer
->InitWithFuncCallback(ProgressCallback
,
149 350, // Number of milliseconds defined in spec
150 nsITimer::TYPE_REPEATING_PRECISE
);
155 nsresult
nsMediaDecoder::StopProgress()
158 if (mProgressTimer
) {
159 rv
= mProgressTimer
->Cancel();
160 mProgressTimer
= nsnull
;
165 void nsMediaDecoder::SetRGBData(PRInt32 aWidth
, PRInt32 aHeight
, float aFramerate
, unsigned char* aRGBBuffer
)
167 if (mRGBWidth
!= aWidth
|| mRGBHeight
!= aHeight
) {
169 mRGBHeight
= aHeight
;
170 mSizeChanged
= PR_TRUE
;
171 // Delete buffer so we'll reallocate it
174 mFramerate
= aFramerate
;
177 mRGB
= new unsigned char[aWidth
* aHeight
* 4];
178 if (mRGB
&& aRGBBuffer
) {
179 memcpy(mRGB
.get(), aRGBBuffer
, aWidth
*aHeight
*4);
183 void nsMediaDecoder::Paint(gfxContext
* aContext
, const gfxRect
& aRect
)
185 nsAutoLock
lock(mVideoUpdateLock
);
190 /* Create a surface backed by the RGB */
191 nsRefPtr
<gfxASurface
> surface
=
192 new gfxImageSurface(mRGB
,
193 gfxIntSize(mRGBWidth
, mRGBHeight
),
195 gfxASurface::ImageFormatARGB32
);
200 nsRefPtr
<gfxPattern
> pat
= new gfxPattern(surface
);
204 // Make the source image fill the rectangle completely
205 pat
->SetMatrix(gfxMatrix().Scale(mRGBWidth
/aRect
.Width(), mRGBHeight
/aRect
.Height()));
207 /* Draw RGB surface onto frame */
209 aContext
->PixelSnappedRectangleAndSetPattern(aRect
, pat
);
212 #ifdef DEBUG_FRAME_RATE
215 static float last
= double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
216 float now
= double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
217 static int count
= 0;
219 if (now
-last
> 10.0) {
220 LOG(PR_LOG_DEBUG
, ("Paint Frame Rate = %f (should be %f)\n", (float)count
/ (float)(now
-last
), mFramerate
));
222 last
= double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
228 void nsMediaDecoder::ElementAvailable(nsHTMLMediaElement
* anElement
)
230 mElement
= anElement
;
233 void nsMediaDecoder::ElementUnavailable()