1/*
2 * Copyright (C) 2011 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 MediaController_h
27#define MediaController_h
28
29#if ENABLE(VIDEO)
30
31#include "ActiveDOMObject.h"
32#include "Event.h"
33#include "EventListener.h"
34#include "EventTarget.h"
35#include "MediaControllerInterface.h"
36#include "Timer.h"
37#include <wtf/PassRefPtr.h>
38#include <wtf/RefCounted.h>
39#include <wtf/Vector.h>
40
41namespace WebCore {
42
43class Clock;
44class HTMLMediaElement;
45class Event;
46class ScriptExecutionContext;
47
48class MediaController : public RefCounted<MediaController>, public MediaControllerInterface, public EventTarget {
49public:
50    static PassRefPtr<MediaController> create(ScriptExecutionContext*);
51    virtual ~MediaController();
52
53    void addMediaElement(HTMLMediaElement*);
54    void removeMediaElement(HTMLMediaElement*);
55    bool containsMediaElement(HTMLMediaElement*) const;
56
57    const String& mediaGroup() const { return m_mediaGroup; }
58
59    virtual PassRefPtr<TimeRanges> buffered() const;
60    virtual PassRefPtr<TimeRanges> seekable() const;
61    virtual PassRefPtr<TimeRanges> played();
62
63    virtual double duration() const;
64    virtual double currentTime() const;
65    virtual void setCurrentTime(double, ExceptionCode&);
66
67    virtual bool paused() const { return m_paused; }
68    virtual void play();
69    virtual void pause();
70    void unpause();
71
72    virtual double defaultPlaybackRate() const { return m_defaultPlaybackRate; }
73    virtual void setDefaultPlaybackRate(double);
74
75    virtual double playbackRate() const;
76    virtual void setPlaybackRate(double);
77
78    virtual double volume() const { return m_volume; }
79    virtual void setVolume(double, ExceptionCode&);
80
81    virtual bool muted() const { return m_muted; }
82    virtual void setMuted(bool);
83
84    virtual ReadyState readyState() const { return m_readyState; }
85
86    enum PlaybackState { WAITING, PLAYING, ENDED };
87    const AtomicString& playbackState() const;
88
89    virtual bool supportsFullscreen() const { return false; }
90    virtual bool isFullscreen() const { return false; }
91    virtual void enterFullscreen() { }
92
93    virtual bool hasAudio() const;
94    virtual bool hasVideo() const;
95    virtual bool hasClosedCaptions() const;
96    virtual void setClosedCaptionsVisible(bool);
97    virtual bool closedCaptionsVisible() const { return m_closedCaptionsVisible; }
98
99    virtual bool supportsScanning() const;
100
101    virtual void beginScrubbing();
102    virtual void endScrubbing();
103
104    virtual bool canPlay() const;
105
106    virtual bool isLiveStream() const;
107
108    virtual bool hasCurrentSrc() const;
109
110    virtual void returnToRealtime();
111
112    bool isBlocked() const;
113
114    // EventTarget
115    using RefCounted<MediaController>::ref;
116    using RefCounted<MediaController>::deref;
117
118private:
119    MediaController(ScriptExecutionContext*);
120    void reportControllerState();
121    void updateReadyState();
122    void updatePlaybackState();
123    void updateMediaElements();
124    void bringElementUpToSpeed(HTMLMediaElement*);
125    void scheduleEvent(const AtomicString& eventName);
126    void asyncEventTimerFired(Timer<MediaController>*);
127    void clearPositionTimerFired(Timer<MediaController>*);
128    bool hasEnded() const;
129    void scheduleTimeupdateEvent();
130    void timeupdateTimerFired(Timer<MediaController>*);
131    void startTimeupdateTimer();
132
133    // EventTarget
134    virtual void refEventTarget() { ref(); }
135    virtual void derefEventTarget() { deref(); }
136    virtual const AtomicString& interfaceName() const;
137    virtual ScriptExecutionContext* scriptExecutionContext() const { return m_scriptExecutionContext; };
138    virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
139    virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
140    EventTargetData m_eventTargetData;
141
142    friend class HTMLMediaElement;
143    friend class MediaControllerEventListener;
144    Vector<HTMLMediaElement*> m_mediaElements;
145    bool m_paused;
146    double m_defaultPlaybackRate;
147    double m_volume;
148    mutable double m_position;
149    bool m_muted;
150    ReadyState m_readyState;
151    PlaybackState m_playbackState;
152    Vector<RefPtr<Event> > m_pendingEvents;
153    Timer<MediaController> m_asyncEventTimer;
154    mutable Timer<MediaController> m_clearPositionTimer;
155    String m_mediaGroup;
156    bool m_closedCaptionsVisible;
157    PassRefPtr<Clock> m_clock;
158    ScriptExecutionContext* m_scriptExecutionContext;
159    Timer<MediaController> m_timeupdateTimer;
160    double m_previousTimeupdateTime;
161};
162
163} // namespace WebCore
164
165#endif
166#endif
167