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(nsMediaDecoder_h_)
39 #define nsMediaDecoder_h_
41 #include "nsIObserver.h"
42 #include "nsIPrincipal.h"
45 #include "gfxContext.h"
50 extern PRLogModuleInfo
* gVideoDecoderLog
;
51 #define LOG(type, msg) PR_LOG(gVideoDecoderLog, type, msg)
53 #define LOG(type, msg)
56 class nsHTMLMediaElement
;
58 // All methods of nsMediaDecoder must be called from the main thread only
59 // with the exception of SetRGBData. The latter can be called from any thread.
60 class nsMediaDecoder
: public nsIObserver
64 virtual ~nsMediaDecoder();
66 // Initialize the logging object
67 static nsresult
InitLogger();
69 // Perform any initialization required for the decoder.
70 // Return PR_TRUE on successful initialisation, PR_FALSE
72 virtual PRBool
Init();
74 // Return the current URI being played or downloaded.
75 virtual void GetCurrentURI(nsIURI
** aURI
) = 0;
77 // Return the principal of the current URI being played or downloaded.
78 virtual nsIPrincipal
* GetCurrentPrincipal() = 0;
80 // Return the time position in the video stream being
81 // played measured in seconds.
82 virtual float GetCurrentTime() = 0;
84 // Seek to the time position in (seconds) from the start of the video.
85 virtual nsresult
Seek(float time
) = 0;
87 // Called by the element when the playback rate has been changed.
88 // Adjust the speed of the playback, optionally with pitch correction,
89 // when this is called.
90 virtual nsresult
PlaybackRateChanged() = 0;
92 // Return the duration of the video in seconds.
93 virtual float GetDuration() = 0;
95 // Pause video playback.
96 virtual void Pause() = 0;
98 // Return the current audio volume that the video plays at.
99 // This is a value form 0 through to 1.0.
100 virtual float GetVolume() = 0;
102 // Set the audio volume. It should be a value from 0 to 1.0.
103 virtual void SetVolume(float volume
) = 0;
105 // Start playback of a video. 'Load' must have previously been
107 virtual nsresult
Play() = 0;
109 // Stop playback of a video, and stop download of video stream.
110 virtual void Stop() = 0;
112 // Start downloading the video. Decode the downloaded data up to the
113 // point of the first frame of data.
114 // Exactly one of aURI and aChannel must be null. aListener must be
115 // null if and only if aChannel is.
116 virtual nsresult
Load(nsIURI
* aURI
,
117 nsIChannel
* aChannel
,
118 nsIStreamListener
**aListener
) = 0;
120 // Draw the latest video data. This is done
121 // here instead of in nsVideoFrame so that the lock around the
122 // RGB buffer doesn't have to be exposed publically.
123 // The current video frame is drawn to fill aRect.
124 // Called in the main thread only.
125 virtual void Paint(gfxContext
* aContext
, const gfxRect
& aRect
);
127 // Called when the video file has completed downloading.
128 virtual void ResourceLoaded() = 0;
130 // Called if the media file encounters a network error.
131 virtual void NetworkError() = 0;
133 // Call from any thread safely. Return PR_TRUE if we are currently
134 // seeking in the media resource.
135 virtual PRBool
IsSeeking() const = 0;
137 // Return PR_TRUE if the decoder has reached the end of playback.
138 // Call in the main thread only.
139 virtual PRBool
IsEnded() const = 0;
141 // Return the current number of bytes loaded from the video file.
142 // This is used for progress events.
143 virtual PRUint64
GetBytesLoaded() = 0;
145 // Return the size of the video file in bytes. Return 0 if the
146 // size is unknown or the stream is infinite.
147 virtual PRInt64
GetTotalBytes() = 0;
149 // Set the size of the video file in bytes.
150 virtual void SetTotalBytes(PRInt64 aBytes
) = 0;
152 // Set a flag indicating whether seeking is supported
153 virtual void SetSeekable(PRBool aSeekable
) = 0;
155 // Return PR_TRUE if seeking is supported.
156 virtual PRBool
GetSeekable() = 0;
158 // Called when the HTML DOM element is bound.
159 virtual void ElementAvailable(nsHTMLMediaElement
* anElement
);
161 // Called when the HTML DOM element is unbound.
162 virtual void ElementUnavailable();
164 // Invalidate the frame.
165 virtual void Invalidate();
167 // Update progress information.
168 virtual void Progress();
170 // Keep track of the number of bytes downloaded
171 virtual void UpdateBytesDownloaded(PRUint64 aBytes
) = 0;
173 // Cleanup internal data structures. Must be called on the main
174 // thread by the owning object before that object disposes of this object.
175 virtual void Shutdown();
179 // Start timer to update download progress information.
180 nsresult
StartProgress();
182 // Stop progress information timer.
183 nsresult
StopProgress();
185 // Set the RGB width, height and framerate. The passed RGB buffer is
186 // copied to the mRGB buffer. This also allocates the mRGB buffer if
188 // This is the only nsMediaDecoder method that may be called
189 // from threads other than the main thread.
190 // It must be called with the mVideoUpdateLock held.
191 void SetRGBData(PRInt32 aWidth
,
194 unsigned char* aRGBBuffer
);
197 // Timer used for updating progress events
198 nsCOMPtr
<nsITimer
> mProgressTimer
;
200 // The element is not reference counted. Instead the decoder is
201 // notified when it is able to be used. It should only ever be
202 // accessed from the main thread.
203 nsHTMLMediaElement
* mElement
;
205 // RGB data for last decoded frame of video data.
206 // The size of the buffer is mRGBWidth*mRGBHeight*4 bytes and
207 // contains bytes in RGBA format.
208 nsAutoArrayPtr
<unsigned char> mRGB
;
213 // Has our size changed since the last repaint?
214 PRPackedBool mSizeChanged
;
216 // Lock around the video RGB, width and size data. This
217 // is used in the decoder backend threads and the main thread
218 // to ensure that repainting the video does not use these
219 // values while they are out of sync (width changed but
220 // not height yet, etc).
221 // Backends that are updating the height, width or writing
222 // to the RGB buffer must obtain this lock first to ensure that
223 // the video element does not use video data or sizes that are
224 // in the midst of being changed.
225 PRLock
* mVideoUpdateLock
;
227 // Framerate of video being displayed in the element
228 // expressed in numbers of frames per second.
231 // True if the decoder is being shutdown. At this point all events that
232 // are currently queued need to return immediately to prevent javascript
233 // being run that operates on the element and decoder during shutdown.
234 // Read/Write from the main thread only.
235 PRPackedBool mShuttingDown
;
237 // True if the decoder is currently in the Stop() method. This is used to
238 // prevent recursive calls into Stop while it is spinning the event loop
239 // waiting for the playback event loop to shutdown. Read/Write from the
241 PRPackedBool mStopping
;