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#include "bindings/v8/ScriptWrappable.h"
30#include "core/events/Event.h"
31#include "core/events/EventTarget.h"
32#include "core/html/MediaControllerInterface.h"
33#include "platform/Timer.h"
34#include "wtf/PassRefPtr.h"
35#include "wtf/RefCounted.h"
36#include "wtf/Vector.h"
37
38namespace WebCore {
39
40class Clock;
41class Event;
42class ExceptionState;
43class HTMLMediaElement;
44class ExecutionContext;
45
46class MediaController : public RefCounted<MediaController>, public ScriptWrappable, public MediaControllerInterface, public EventTargetWithInlineData {
47    REFCOUNTED_EVENT_TARGET(MediaController);
48public:
49    static PassRefPtr<MediaController> create(ExecutionContext*);
50    virtual ~MediaController();
51
52    void addMediaElement(HTMLMediaElement*);
53    void removeMediaElement(HTMLMediaElement*);
54    bool containsMediaElement(HTMLMediaElement*) const;
55
56    const String& mediaGroup() const { return m_mediaGroup; }
57
58    virtual PassRefPtr<TimeRanges> buffered() const;
59    virtual PassRefPtr<TimeRanges> seekable() const;
60    virtual PassRefPtr<TimeRanges> played();
61
62    virtual double duration() const;
63    virtual double currentTime() const;
64    virtual void setCurrentTime(double, ExceptionState&);
65
66    virtual bool paused() const { return m_paused; }
67    virtual void play();
68    virtual void pause();
69    void unpause();
70
71    virtual double defaultPlaybackRate() const { return m_defaultPlaybackRate; }
72    virtual void setDefaultPlaybackRate(double);
73
74    virtual double playbackRate() const;
75    virtual void setPlaybackRate(double);
76
77    virtual double volume() const { return m_volume; }
78    virtual void setVolume(double, ExceptionState&);
79
80    virtual bool muted() const { return m_muted; }
81    virtual void setMuted(bool);
82
83    virtual ReadyState readyState() const { return m_readyState; }
84
85    enum PlaybackState { WAITING, PLAYING, ENDED };
86    const AtomicString& playbackState() const;
87
88    virtual bool supportsFullscreen() const { return false; }
89    virtual bool isFullscreen() const { return false; }
90    virtual void enterFullscreen() { }
91
92    virtual bool hasAudio() const;
93    virtual bool hasVideo() const;
94    virtual bool hasClosedCaptions() const;
95    virtual void setClosedCaptionsVisible(bool);
96    virtual bool closedCaptionsVisible() const { return m_closedCaptionsVisible; }
97
98    virtual void beginScrubbing();
99    virtual void endScrubbing();
100
101    virtual bool canPlay() const;
102
103    virtual bool hasCurrentSrc() const;
104
105    bool isBlocked() const;
106
107    void clearExecutionContext() { m_executionContext = 0; }
108
109private:
110    MediaController(ExecutionContext*);
111    void reportControllerState();
112    void updateReadyState();
113    void updatePlaybackState();
114    void updateMediaElements();
115    void bringElementUpToSpeed(HTMLMediaElement*);
116    void scheduleEvent(const AtomicString& eventName);
117    void asyncEventTimerFired(Timer<MediaController>*);
118    void clearPositionTimerFired(Timer<MediaController>*);
119    bool hasEnded() const;
120    void scheduleTimeupdateEvent();
121    void timeupdateTimerFired(Timer<MediaController>*);
122    void startTimeupdateTimer();
123
124    // EventTarget
125    virtual const AtomicString& interfaceName() const OVERRIDE;
126    virtual ExecutionContext* executionContext() const OVERRIDE { return m_executionContext; }
127
128    friend class HTMLMediaElement;
129    friend class MediaControllerEventListener;
130    Vector<HTMLMediaElement*> m_mediaElements;
131    bool m_paused;
132    double m_defaultPlaybackRate;
133    double m_volume;
134    mutable double m_position;
135    bool m_muted;
136    ReadyState m_readyState;
137    PlaybackState m_playbackState;
138    Vector<RefPtr<Event> > m_pendingEvents;
139    Timer<MediaController> m_asyncEventTimer;
140    mutable Timer<MediaController> m_clearPositionTimer;
141    String m_mediaGroup;
142    bool m_closedCaptionsVisible;
143    OwnPtr<Clock> m_clock;
144    ExecutionContext* m_executionContext;
145    Timer<MediaController> m_timeupdateTimer;
146    double m_previousTimeupdateTime;
147};
148
149} // namespace WebCore
150
151#endif
152