• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 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 #include "VoidCallback.h"
35 
36 namespace WebCore {
37 
38 class MediaError;
39 class TimeRanges;
40 class KURL;
41 
42 class HTMLMediaElement : public HTMLElement, public MediaPlayerClient {
43 public:
44     HTMLMediaElement(const QualifiedName&, Document*);
45     virtual ~HTMLMediaElement();
46 
47     bool checkDTD(const Node* newChild);
48 
49     void attributeChanged(Attribute*, bool preserveDecls);
50 
51     virtual bool rendererIsNeeded(RenderStyle*);
52     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
53     virtual void insertedIntoDocument();
54     virtual void removedFromDocument();
55     virtual void attach();
56     virtual void recalcStyle(StyleChange);
57 
player()58     MediaPlayer* player() const { return m_player.get(); }
59 
isVideo()60     virtual bool isVideo() const { return false; }
61 
62     void scheduleLoad();
63 
64     virtual void defaultEventHandler(Event*);
65 
66     // Pauses playback without changing any states or generating events
67     void setPausedInternal(bool);
68 
inActiveDocument()69     bool inActiveDocument() const { return m_inActiveDocument; }
70 
71 // DOM API
72 // error state
73     PassRefPtr<MediaError> error() const;
74 
75 // network state
76     KURL src() const;
77     void setSrc(const String&);
78     String currentSrc() const;
79 
80     enum NetworkState { EMPTY, LOADING, LOADED_METADATA, LOADED_FIRST_FRAME, LOADED };
81     NetworkState networkState() const;
82     float bufferingRate();
83     PassRefPtr<TimeRanges> buffered() const;
84     void load(ExceptionCode&);
85 
86 // ready state
87     enum ReadyState { DATA_UNAVAILABLE, CAN_SHOW_CURRENT_FRAME, CAN_PLAY, CAN_PLAY_THROUGH };
88     ReadyState readyState() const;
89     bool seeking() const;
90 
91 // playback state
92     float currentTime() const;
93     void setCurrentTime(float, ExceptionCode&);
94     float duration() const;
95     bool paused() const;
96     float defaultPlaybackRate() const;
97     void setDefaultPlaybackRate(float, ExceptionCode&);
98     float playbackRate() const;
99     void setPlaybackRate(float, ExceptionCode&);
100     PassRefPtr<TimeRanges> played() const;
101     PassRefPtr<TimeRanges> seekable() const;
102     bool ended() const;
103     bool autoplay() const;
104     void setAutoplay(bool b);
105     void play(ExceptionCode&);
106     void pause(ExceptionCode&);
107 
108 // looping
109     float start() const;
110     void setStart(float time);
111     float end() const;
112     void setEnd(float time);
113     float loopStart() const;
114     void setLoopStart(float time);
115     float loopEnd() const;
116     void setLoopEnd(float time);
117     unsigned playCount() const;
118     void setPlayCount(unsigned, ExceptionCode&);
119     unsigned currentLoop() const;
120     void setCurrentLoop(unsigned);
121 
122 // controls
123     bool controls() const;
124     void setControls(bool);
125     float volume() const;
126     void setVolume(float, ExceptionCode&);
127     bool muted() const;
128     void setMuted(bool);
129 
130     bool canPlay() const;
131 
132 protected:
133     float getTimeOffsetAttribute(const QualifiedName&, float valueOnError) const;
134     void setTimeOffsetAttribute(const QualifiedName&, float value);
135 
136     virtual void documentWillBecomeInactive();
137     virtual void documentDidBecomeActive();
138     virtual void mediaVolumeDidChange();
139 
140     void initAndDispatchProgressEvent(const AtomicString& eventName);
141     void dispatchEventAsync(const AtomicString& eventName);
142 
143     void setReadyState(ReadyState);
144 
145 private: // MediaPlayerObserver
146     virtual void mediaPlayerNetworkStateChanged(MediaPlayer*);
147     virtual void mediaPlayerReadyStateChanged(MediaPlayer*);
148     virtual void mediaPlayerTimeChanged(MediaPlayer*);
149     virtual void mediaPlayerRepaint(MediaPlayer*);
150 
151 private:
152     void loadTimerFired(Timer<HTMLMediaElement>*);
153     void asyncEventTimerFired(Timer<HTMLMediaElement>*);
154     void progressEventTimerFired(Timer<HTMLMediaElement>*);
155     void seek(float time, ExceptionCode& ec);
156     void checkIfSeekNeeded();
157 
158     String pickMedia();
159     void updateVolume();
160     void updatePlayState();
161     float effectiveStart() const;
162     float effectiveEnd() const;
163     float effectiveLoopStart() const;
164     float effectiveLoopEnd() const;
165     bool activelyPlaying() const;
166     bool endedPlayback() const;
167 
168 protected:
169     Timer<HTMLMediaElement> m_loadTimer;
170     Timer<HTMLMediaElement> m_asyncEventTimer;
171     Timer<HTMLMediaElement> m_progressEventTimer;
172     Vector<AtomicString> m_asyncEventsToDispatch;
173 
174     float m_defaultPlaybackRate;
175     NetworkState m_networkState;
176     ReadyState m_readyState;
177     String m_currentSrc;
178 
179     RefPtr<MediaError> m_error;
180 
181     bool m_begun;
182     bool m_loadedFirstFrame;
183     bool m_autoplaying;
184 
185     unsigned m_currentLoop;
186     float m_volume;
187     bool m_muted;
188 
189     bool m_paused;
190     bool m_seeking;
191 
192     float m_currentTimeDuringSeek;
193 
194     unsigned m_previousProgress;
195     double m_previousProgressTime;
196     bool m_sentStalledEvent;
197 
198     float m_bufferingRate;
199 
200     unsigned m_loadNestingLevel;
201     unsigned m_terminateLoadBelowNestingLevel;
202 
203     bool m_pausedInternal;
204     bool m_inActiveDocument;
205 
206     OwnPtr<MediaPlayer> m_player;
207 };
208 
209 } //namespace
210 
211 #endif
212 #endif
213