Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / content / media / video / src / nsMediaDecoder.cpp
blob7e188e92222c63f64f4896dada263f48c7168be3
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
14 * License.
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.
22 * Contributor(s):
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 ***** */
38 #include "prlog.h"
39 #include "prmem.h"
40 #include "nsIFrame.h"
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"
55 #ifdef PR_LOGGING
56 // Logging object for decoder
57 PRLogModuleInfo* gVideoDecoderLog = nsnull;
58 #endif
60 nsMediaDecoder::nsMediaDecoder() :
61 mElement(0),
62 mRGBWidth(-1),
63 mRGBHeight(-1),
64 mSizeChanged(PR_FALSE),
65 mVideoUpdateLock(nsnull),
66 mFramerate(0.0)
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()
89 StopProgress();
90 ElementUnavailable();
94 nsresult nsMediaDecoder::InitLogger()
96 #ifdef PR_LOGGING
97 gVideoDecoderLog = PR_NewLogModule("nsMediaDecoder");
98 #endif
99 return NS_OK;
102 void nsMediaDecoder::Invalidate()
104 if (!mElement)
105 return;
107 nsIFrame* frame = mElement->GetPrimaryFrame();
108 if (!frame)
109 return;
112 nsAutoLock lock(mVideoUpdateLock);
113 if (mSizeChanged) {
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,
120 NS_FRAME_IS_DIRTY);
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);
130 decoder->Progress();
133 void nsMediaDecoder::Progress()
135 if (!mElement)
136 return;
138 mElement->DispatchProgressEvent(NS_LITERAL_STRING("progress"));
141 nsresult nsMediaDecoder::StartProgress()
143 nsresult rv = NS_OK;
145 if (!mProgressTimer) {
146 mProgressTimer = do_CreateInstance("@mozilla.org/timer;1");
147 rv = mProgressTimer->InitWithFuncCallback(ProgressCallback,
148 this,
149 350, // Number of milliseconds defined in spec
150 nsITimer::TYPE_REPEATING_PRECISE);
152 return rv;
155 nsresult nsMediaDecoder::StopProgress()
157 nsresult rv = NS_OK;
158 if (mProgressTimer) {
159 rv = mProgressTimer->Cancel();
160 mProgressTimer = nsnull;
162 return rv;
165 void nsMediaDecoder::SetRGBData(PRInt32 aWidth, PRInt32 aHeight, float aFramerate, unsigned char* aRGBBuffer)
167 if (mRGBWidth != aWidth || mRGBHeight != aHeight) {
168 mRGBWidth = aWidth;
169 mRGBHeight = aHeight;
170 mSizeChanged = PR_TRUE;
171 // Delete buffer so we'll reallocate it
172 mRGB = nsnull;
174 mFramerate = aFramerate;
176 if (!mRGB)
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);
187 if (!mRGB)
188 return;
190 /* Create a surface backed by the RGB */
191 nsRefPtr<gfxASurface> surface =
192 new gfxImageSurface(mRGB,
193 gfxIntSize(mRGBWidth, mRGBHeight),
194 mRGBWidth * 4,
195 gfxASurface::ImageFormatARGB32);
197 if (!surface)
198 return;
200 nsRefPtr<gfxPattern> pat = new gfxPattern(surface);
201 if (!pat)
202 return;
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 */
208 aContext->NewPath();
209 aContext->PixelSnappedRectangleAndSetPattern(aRect, pat);
210 aContext->Fill();
212 #ifdef DEBUG_FRAME_RATE
214 // Output 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;
218 count++;
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));
221 count = 0;
222 last = double(PR_IntervalToMilliseconds(PR_IntervalNow()))/1000.0;
225 #endif
228 void nsMediaDecoder::ElementAvailable(nsHTMLMediaElement* anElement)
230 mElement = anElement;
233 void nsMediaDecoder::ElementUnavailable()
235 mElement = nsnull;