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 ***** */
38 #if !defined(nsVideoDecoder_h___)
39 #define nsVideoDecoder_h___
41 #include "nsIObserver.h"
44 #include "gfxContext.h"
49 extern PRLogModuleInfo
* gVideoDecoderLog
;
50 #define LOG(type, msg) PR_LOG(gVideoDecoderLog, type, msg)
52 #define LOG(type, msg)
55 class nsHTMLMediaElement
;
57 // All methods of nsVideoDecoder must be called from the main thread only
58 // with the exception of SetRGBData. The latter can be called from any thread.
59 class nsVideoDecoder
: public nsIObserver
63 virtual ~nsVideoDecoder() { }
65 // Initialize the logging object
66 static nsresult
InitLogger();
68 // Perform any initialization required for the decoder.
69 // Return PR_TRUE on successful initialisation, PR_FALSE
71 virtual PRBool
Init();
74 // Return the current URI being played or downloaded.
75 virtual void GetCurrentURI(nsIURI
** aURI
) = 0;
77 // Return the time position in the video stream being
78 // played measured in seconds.
79 virtual float GetCurrentTime() = 0;
81 // Seek to the time position in (seconds) from the start of the video.
82 virtual nsresult
Seek(float time
) = 0;
84 // Called by the element when the playback rate has been changed.
85 // Adjust the speed of the playback, optionally with pitch correction,
86 // when this is called.
87 virtual nsresult
PlaybackRateChanged() = 0;
89 // Return the duration of the video in seconds.
90 virtual float GetDuration() = 0;
92 // Pause video playback.
93 virtual void Pause() = 0;
95 // Return the current audio volume that the video plays at.
96 // This is a value form 0 through to 1.0.
97 virtual float GetVolume() = 0;
99 // Set the audio volume. It should be a value from 0 to 1.0.
100 virtual void SetVolume(float volume
) = 0;
102 // Returns the current video frame width and height.
103 // If there is no video frame, returns the given default size.
104 virtual nsIntSize
GetVideoSize(nsIntSize defaultSize
) = 0;
106 // Return the current framerate of the video, in frames per second.
107 virtual double GetVideoFramerate() = 0;
109 // Start playback of a video. 'Load' must have previously been
111 virtual nsresult
Play() = 0;
113 // Stop playback of a video, and stop download of video stream.
114 virtual void Stop() = 0;
116 // Start downloading the video at the given URI. Decode
117 // the downloaded data up to the point of the first frame
119 virtual nsresult
Load(nsIURI
* aURI
) = 0;
121 // Draw the latest video data. This is done
122 // here instead of in nsVideoFrame so that the lock around the
123 // RGB buffer doesn't have to be exposed publically.
124 // The current video frame is drawn to fill aRect.
125 // Called in the main thread only.
126 virtual void Paint(gfxContext
* aContext
, const gfxRect
& aRect
);
128 // Called when the video file has completed downloading.
129 virtual void ResourceLoaded() = 0;
131 // Return the current number of bytes loaded from the video file.
132 // This is used for progress events.
133 virtual PRUint32
GetBytesLoaded() = 0;
135 // Return the size of the video file in bytes.
136 // This is used for progress events.
137 virtual PRUint32
GetTotalBytes() = 0;
139 // Called when the HTML DOM element is bound.
140 virtual void ElementAvailable(nsHTMLMediaElement
* anElement
);
142 // Called when the HTML DOM element is unbound.
143 virtual void ElementUnavailable();
145 // Invalidate the frame.
146 virtual void Invalidate();
148 // Update progress information.
149 virtual void Progress();
152 // Cleanup internal data structures
153 virtual void Shutdown();
155 // Start invalidating the video frame at the interval required
156 // by the specificed framerate (in frames per second).
157 nsresult
StartInvalidating(double aFramerate
);
159 // Stop invalidating the video frame
160 void StopInvalidating();
162 // Start timer to update download progress information.
163 nsresult
StartProgress();
165 // Stop progress information timer.
166 nsresult
StopProgress();
168 // Set the RGB width, height and framerate. The passed RGB buffer is
169 // copied to the mRGB buffer. This also allocates the mRGB buffer if
171 // This is the only nsVideoDecoder method that may be called
172 // from threads other than the main thread.
173 void SetRGBData(PRInt32 aWidth
,
176 unsigned char* aRGBBuffer
);
179 // Timer used for invalidating the video
180 nsCOMPtr
<nsITimer
> mInvalidateTimer
;
182 // Timer used for updating progress events
183 nsCOMPtr
<nsITimer
> mProgressTimer
;
185 // The element is not reference counted. Instead the decoder is
186 // notified when it is able to be used. It should only ever be
187 // accessed from the main thread.
188 nsHTMLMediaElement
* mElement
;
190 // RGB data for last decoded frame of video data.
191 // The size of the buffer is mRGBWidth*mRGBHeight*4 bytes and
192 // contains bytes in RGBA format.
193 nsAutoArrayPtr
<unsigned char> mRGB
;
198 // Has our size changed since the last repaint?
199 PRPackedBool mSizeChanged
;
201 // Lock around the video RGB, width and size data. This
202 // is used in the decoder backend threads and the main thread
203 // to ensure that repainting the video does not use these
204 // values while they are out of sync (width changed but
205 // not height yet, etc).
206 // Backends that are updating the height, width or writing
207 // to the RGB buffer must obtain this lock first to ensure that
208 // the video element does not use video data or sizes that are
209 // in the midst of being changed.
210 PRLock
* mVideoUpdateLock
;
212 // Framerate of video being displayed in the element
213 // expressed in numbers of frames per second.