]> git.saurik.com Git - iphone-api.git/blob - WebCore/HTMLMediaElement.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / HTMLMediaElement.h
1 /*
2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef HTMLMediaElement_h
27 #define HTMLMediaElement_h
28
29 #if ENABLE(VIDEO)
30
31 #include "HTMLElement.h"
32 #include "MediaPlayer.h"
33 #include "Timer.h"
34
35 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
36 #include "MediaPlayerProxy.h"
37 #endif
38
39 namespace WebCore {
40
41 class Event;
42 class MediaError;
43 class KURL;
44 class TimeRanges;
45
46 class HTMLMediaElement : public HTMLElement, public MediaPlayerClient {
47 public:
48 HTMLMediaElement(const QualifiedName&, Document*);
49 virtual ~HTMLMediaElement();
50
51 bool checkDTD(const Node* newChild);
52
53 void attributeChanged(Attribute*, bool preserveDecls);
54 void parseMappedAttribute(MappedAttribute *);
55
56 virtual bool rendererIsNeeded(RenderStyle*);
57 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
58 virtual void insertedIntoDocument();
59 virtual void removedFromDocument();
60 virtual void attach();
61 virtual void recalcStyle(StyleChange);
62
63 MediaPlayer* player() const { return m_player.get(); }
64
65 virtual bool isVideo() const { return false; }
66 virtual bool hasVideo() const { return false; }
67
68 void scheduleLoad();
69
70 virtual void defaultEventHandler(Event*);
71
72 // Pauses playback without changing any states or generating events
73 void setPausedInternal(bool);
74
75 bool inActiveDocument() const { return m_inActiveDocument; }
76
77 // DOM API
78 // error state
79 PassRefPtr<MediaError> error() const;
80
81 // network state
82 KURL src() const;
83 void setSrc(const String&);
84 String currentSrc() const;
85
86 enum NetworkState { NETWORK_EMPTY, NETWORK_IDLE, NETWORK_LOADING, NETWORK_LOADED, NETWORK_NO_SOURCE };
87 NetworkState networkState() const;
88 bool autobuffer() const;
89 void setAutobuffer(bool);
90
91 PassRefPtr<TimeRanges> buffered() const;
92 void load(ExceptionCode&);
93 String canPlayType(const String& mimeType) const;
94
95 // ready state
96 enum ReadyState { HAVE_NOTHING, HAVE_METADATA, HAVE_CURRENT_DATA, HAVE_FUTURE_DATA, HAVE_ENOUGH_DATA };
97 ReadyState readyState() const;
98 bool seeking() const;
99
100 // playback state
101 float currentTime() const;
102 void setCurrentTime(float, ExceptionCode&);
103 float duration() const;
104 bool paused() const;
105 float defaultPlaybackRate() const;
106 void setDefaultPlaybackRate(float);
107 float playbackRate() const;
108 void setPlaybackRate(float);
109 PassRefPtr<TimeRanges> played() const;
110 PassRefPtr<TimeRanges> seekable() const;
111 bool ended() const;
112 bool autoplay() const;
113 void setAutoplay(bool b);
114 bool loop() const;
115 void setLoop(bool b);
116 void play();
117 void pause();
118
119 // controls
120 bool controls() const;
121 void setControls(bool);
122 float volume() const;
123 void setVolume(float, ExceptionCode&);
124 bool muted() const;
125 void setMuted(bool);
126 void togglePlayState();
127 void beginScrubbing();
128 void endScrubbing();
129
130 bool canPlay() const;
131
132 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
133 void setNeedWidgetUpdate(bool needWidgetUpdate) { m_needWidgetUpdate = needWidgetUpdate; }
134 void deliverNotification(MediaPlayerProxyNotificationType notification);
135 void setMediaPlayerProxy(WebMediaPlayerProxy* proxy);
136 String initialURL();
137 virtual void finishParsingChildren();
138 #endif
139
140 protected:
141 float getTimeOffsetAttribute(const QualifiedName&, float valueOnError) const;
142 void setTimeOffsetAttribute(const QualifiedName&, float value);
143
144 virtual void documentWillBecomeInactive();
145 virtual void documentDidBecomeActive();
146 virtual void mediaVolumeDidChange();
147
148 void setReadyState(MediaPlayer::ReadyState);
149 void setNetworkState(MediaPlayer::NetworkState);
150
151 private: // MediaPlayerObserver
152 virtual void mediaPlayerNetworkStateChanged(MediaPlayer*);
153 virtual void mediaPlayerReadyStateChanged(MediaPlayer*);
154 virtual void mediaPlayerTimeChanged(MediaPlayer*);
155 virtual void mediaPlayerRepaint(MediaPlayer*);
156 virtual void mediaPlayerVolumeChanged(MediaPlayer*);
157 virtual void mediaPlayerDurationChanged(MediaPlayer*);
158 virtual void mediaPlayerRateChanged(MediaPlayer*);
159 virtual void mediaPlayerSizeChanged(MediaPlayer*);
160
161 private:
162 void loadTimerFired(Timer<HTMLMediaElement>*);
163 void asyncEventTimerFired(Timer<HTMLMediaElement>*);
164 void progressEventTimerFired(Timer<HTMLMediaElement>*);
165 void playbackProgressTimerFired(Timer<HTMLMediaElement>*);
166 void startPlaybackProgressTimer();
167 void startProgressEventTimer();
168 void stopPeriodicTimers();
169
170 void seek(float time, ExceptionCode&);
171 void checkIfSeekNeeded();
172
173 void scheduleTimeupdateEvent(bool periodicEvent);
174 void scheduleProgressEvent(const AtomicString& eventName);
175 void scheduleEvent(const AtomicString& eventName);
176 void enqueueEvent(RefPtr<Event> event);
177
178 // loading
179 void selectMediaResource();
180 void loadResource(String url, ContentType& contentType);
181 void loadNextSourceChild();
182 void userCancelledLoad();
183 String nextSourceChild(ContentType* contentType = 0);
184 bool havePotentialSourceChild();
185 void noneSupported();
186 void mediaEngineError(PassRefPtr<MediaError> err);
187
188 // These "internal" functions do not check user gesture restrictions.
189 void loadInternal();
190 void playInternal();
191 void pauseInternal();
192
193 bool processingUserGesture() const;
194 bool processingMediaPlayerCallback() const { return m_processingMediaPlayerCallback > 0; }
195 void beginProcessingMediaPlayerCallback() { ++m_processingMediaPlayerCallback; }
196 void endProcessingMediaPlayerCallback() { ASSERT(m_processingMediaPlayerCallback); --m_processingMediaPlayerCallback; }
197
198 void updateVolume();
199 void updatePlayState();
200 bool potentiallyPlaying() const;
201 bool endedPlayback() const;
202 bool stoppedDueToErrors() const;
203 bool pausedForUserInteraction() const;
204
205 // Restrictions to change default behaviors. This is a effectively a compile time choice at the moment
206 // because there are no accessor methods.
207 enum BehaviorRestrictions
208 {
209 NoRestrictions = 0,
210 RequireUserGestureForLoadRestriction = 1 << 0,
211 RequireUserGestureForRateChangeRestriction = 1 << 1,
212 };
213
214 protected:
215 Timer<HTMLMediaElement> m_loadTimer;
216 Timer<HTMLMediaElement> m_asyncEventTimer;
217 Timer<HTMLMediaElement> m_progressEventTimer;
218 Timer<HTMLMediaElement> m_playbackProgressTimer;
219 Vector<RefPtr<Event> > m_pendingEvents;
220
221 float m_playbackRate;
222 float m_defaultPlaybackRate;
223 NetworkState m_networkState;
224 ReadyState m_readyState;
225 String m_currentSrc;
226
227 RefPtr<MediaError> m_error;
228
229 float m_volume;
230 float m_currentTimeDuringSeek;
231
232 unsigned m_previousProgress;
233 double m_previousProgressTime;
234
235 // the last time a timeupdate event was sent (wall clock)
236 double m_lastTimeUpdateEventWallTime;
237
238 // the last time a timeupdate event was sent in movie time
239 float m_lastTimeUpdateEventMovieTime;
240
241 // loading state
242 enum LoadState { WaitingForSource, LoadingFromSrcAttr, LoadingFromSourceElement };
243 LoadState m_loadState;
244 Node *m_currentSourceNode;
245
246 OwnPtr<MediaPlayer> m_player;
247
248 BehaviorRestrictions m_restrictions;
249
250 // counter incremented while processing a callback from the media player, so we can avoid
251 // calling the media engine recursively
252 int m_processingMediaPlayerCallback;
253
254 bool m_processingLoad : 1;
255 bool m_delayingTheLoadEvent : 1;
256 bool m_haveFiredLoadedData : 1;
257 bool m_inActiveDocument : 1;
258 bool m_autoplaying : 1;
259 bool m_muted : 1;
260 bool m_paused : 1;
261 bool m_seeking : 1;
262
263 // data has not been loaded since sending a "stalled" event
264 bool m_sentStalledEvent : 1;
265
266 // time has not changed since sending an "ended" event
267 bool m_sentEndEvent : 1;
268
269 bool m_pausedInternal : 1;
270
271 // Not all media engines provide enough information about a file to be able to
272 // support progress events so setting m_sendProgressEvents disables them
273 bool m_sendProgressEvents : 1;
274
275 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
276 bool m_needWidgetUpdate : 1;
277 #endif
278
279 bool m_inFullScreen : 1;
280 bool m_requestingPlay : 1;
281 };
282
283 } //namespace
284
285 #endif
286 #endif