2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef HTMLMediaElement_h
27 #define HTMLMediaElement_h
31 #include "HTMLElement.h"
32 #include "MediaPlayer.h"
35 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
36 #include "MediaPlayerProxy.h"
46 class HTMLMediaElement
: public HTMLElement
, public MediaPlayerClient
{
48 HTMLMediaElement(const QualifiedName
&, Document
*);
49 virtual ~HTMLMediaElement();
51 bool checkDTD(const Node
* newChild
);
53 void attributeChanged(Attribute
*, bool preserveDecls
);
54 void parseMappedAttribute(MappedAttribute
*);
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
);
63 MediaPlayer
* player() const { return m_player
.get(); }
65 virtual bool isVideo() const { return false; }
66 virtual bool hasVideo() const { return false; }
70 virtual void defaultEventHandler(Event
*);
72 // Pauses playback without changing any states or generating events
73 void setPausedInternal(bool);
75 bool inActiveDocument() const { return m_inActiveDocument
; }
79 PassRefPtr
<MediaError
> error() const;
83 void setSrc(const String
&);
84 String
currentSrc() const;
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);
91 PassRefPtr
<TimeRanges
> buffered() const;
92 void load(ExceptionCode
&);
93 String
canPlayType(const String
& mimeType
) const;
96 enum ReadyState
{ HAVE_NOTHING
, HAVE_METADATA
, HAVE_CURRENT_DATA
, HAVE_FUTURE_DATA
, HAVE_ENOUGH_DATA
};
97 ReadyState
readyState() const;
101 float currentTime() const;
102 void setCurrentTime(float, ExceptionCode
&);
103 float duration() 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;
112 bool autoplay() const;
113 void setAutoplay(bool b
);
115 void setLoop(bool b
);
120 bool controls() const;
121 void setControls(bool);
122 float volume() const;
123 void setVolume(float, ExceptionCode
&);
126 void togglePlayState();
127 void beginScrubbing();
130 bool canPlay() const;
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
);
137 virtual void finishParsingChildren();
141 float getTimeOffsetAttribute(const QualifiedName
&, float valueOnError
) const;
142 void setTimeOffsetAttribute(const QualifiedName
&, float value
);
144 virtual void documentWillBecomeInactive();
145 virtual void documentDidBecomeActive();
146 virtual void mediaVolumeDidChange();
148 void setReadyState(MediaPlayer::ReadyState
);
149 void setNetworkState(MediaPlayer::NetworkState
);
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
*);
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();
170 void seek(float time
, ExceptionCode
&);
171 void checkIfSeekNeeded();
173 void scheduleTimeupdateEvent(bool periodicEvent
);
174 void scheduleProgressEvent(const AtomicString
& eventName
);
175 void scheduleEvent(const AtomicString
& eventName
);
176 void enqueueEvent(RefPtr
<Event
> event
);
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
);
188 // These "internal" functions do not check user gesture restrictions.
191 void pauseInternal();
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
; }
199 void updatePlayState();
200 bool potentiallyPlaying() const;
201 bool endedPlayback() const;
202 bool stoppedDueToErrors() const;
203 bool pausedForUserInteraction() const;
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
210 RequireUserGestureForLoadRestriction
= 1 << 0,
211 RequireUserGestureForRateChangeRestriction
= 1 << 1,
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
;
221 float m_playbackRate
;
222 float m_defaultPlaybackRate
;
223 NetworkState m_networkState
;
224 ReadyState m_readyState
;
227 RefPtr
<MediaError
> m_error
;
230 float m_currentTimeDuringSeek
;
232 unsigned m_previousProgress
;
233 double m_previousProgressTime
;
235 // the last time a timeupdate event was sent (wall clock)
236 double m_lastTimeUpdateEventWallTime
;
238 // the last time a timeupdate event was sent in movie time
239 float m_lastTimeUpdateEventMovieTime
;
242 enum LoadState
{ WaitingForSource
, LoadingFromSrcAttr
, LoadingFromSourceElement
};
243 LoadState m_loadState
;
244 Node
*m_currentSourceNode
;
246 OwnPtr
<MediaPlayer
> m_player
;
248 BehaviorRestrictions m_restrictions
;
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
;
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;
263 // data has not been loaded since sending a "stalled" event
264 bool m_sentStalledEvent
: 1;
266 // time has not changed since sending an "ended" event
267 bool m_sentEndEvent
: 1;
269 bool m_pausedInternal
: 1;
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;
275 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
276 bool m_needWidgetUpdate
: 1;
279 bool m_inFullScreen
: 1;
280 bool m_requestingPlay
: 1;