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 the current number of bytes loaded from the video file.
138 // This is used for progress events.
139 virtual PRUint64
GetBytesLoaded() = 0;
141 // Return the size of the video file in bytes. Return 0 if the
142 // size is unknown or the stream is infinite.
143 virtual PRInt64
GetTotalBytes() = 0;
145 // Set the size of the video file in bytes.
146 virtual void SetTotalBytes(PRInt64 aBytes
) = 0;
148 // Set a flag indicating whether seeking is supported
149 virtual void SetSeekable(PRBool aSeekable
) = 0;
151 // Return PR_TRUE if seeking is supported.
152 virtual PRBool
GetSeekable() = 0;
154 // Called when the HTML DOM element is bound.
155 virtual void ElementAvailable(nsHTMLMediaElement
* anElement
);
157 // Called when the HTML DOM element is unbound.
158 virtual void ElementUnavailable();
160 // Invalidate the frame.
161 virtual void Invalidate();
163 // Update progress information.
164 virtual void Progress();
166 // Keep track of the number of bytes downloaded
167 virtual void UpdateBytesDownloaded(PRUint64 aBytes
) = 0;
169 // Cleanup internal data structures. Must be called on the main
170 // thread by the owning object before that object disposes of this object.
171 virtual void Shutdown();
175 // Start timer to update download progress information.
176 nsresult
StartProgress();
178 // Stop progress information timer.
179 nsresult
StopProgress();
181 // Set the RGB width, height and framerate. The passed RGB buffer is
182 // copied to the mRGB buffer. This also allocates the mRGB buffer if
184 // This is the only nsMediaDecoder method that may be called
185 // from threads other than the main thread.
186 // It must be called with the mVideoUpdateLock held.
187 void SetRGBData(PRInt32 aWidth
,
190 unsigned char* aRGBBuffer
);
193 // Timer used for updating progress events
194 nsCOMPtr
<nsITimer
> mProgressTimer
;
196 // The element is not reference counted. Instead the decoder is
197 // notified when it is able to be used. It should only ever be
198 // accessed from the main thread.
199 nsHTMLMediaElement
* mElement
;
201 // RGB data for last decoded frame of video data.
202 // The size of the buffer is mRGBWidth*mRGBHeight*4 bytes and
203 // contains bytes in RGBA format.
204 nsAutoArrayPtr
<unsigned char> mRGB
;
209 // Has our size changed since the last repaint?
210 PRPackedBool mSizeChanged
;
212 // Lock around the video RGB, width and size data. This
213 // is used in the decoder backend threads and the main thread
214 // to ensure that repainting the video does not use these
215 // values while they are out of sync (width changed but
216 // not height yet, etc).
217 // Backends that are updating the height, width or writing
218 // to the RGB buffer must obtain this lock first to ensure that
219 // the video element does not use video data or sizes that are
220 // in the midst of being changed.
221 PRLock
* mVideoUpdateLock
;
223 // Framerate of video being displayed in the element
224 // expressed in numbers of frames per second.