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 "core/events/EventTarget.h"
30#include "core/html/HTMLMediaElement.h"
31#include "wtf/LinkedHashSet.h"
32#include "wtf/PassRefPtr.h"
33#include "wtf/RefCounted.h"
34
35namespace blink {
36
37class Clock;
38class ExceptionState;
39class ExecutionContext;
40class GenericEventQueue;
41
42class MediaController FINAL : public RefCountedWillBeGarbageCollectedFinalized<MediaController>, public EventTargetWithInlineData {
43    DEFINE_WRAPPERTYPEINFO();
44    REFCOUNTED_EVENT_TARGET(MediaController);
45    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MediaController);
46public:
47    static PassRefPtrWillBeRawPtr<MediaController> create(ExecutionContext*);
48    virtual ~MediaController();
49
50    void addMediaElement(HTMLMediaElement*);
51    void removeMediaElement(HTMLMediaElement*);
52
53    PassRefPtrWillBeRawPtr<TimeRanges> buffered() const;
54    PassRefPtrWillBeRawPtr<TimeRanges> seekable() const;
55    PassRefPtrWillBeRawPtr<TimeRanges> played();
56
57    double duration() const;
58    double currentTime() const;
59    void setCurrentTime(double);
60
61    bool paused() const { return m_paused; }
62    void play();
63    void pause();
64    void unpause();
65
66    double defaultPlaybackRate() const { return m_defaultPlaybackRate; }
67    void setDefaultPlaybackRate(double);
68
69    double playbackRate() const;
70    void setPlaybackRate(double);
71
72    double volume() const { return m_volume; }
73    void setVolume(double, ExceptionState&);
74
75    bool muted() const { return m_muted; }
76    void setMuted(bool);
77
78    typedef HTMLMediaElement::ReadyState ReadyState;
79    ReadyState readyState() const { return m_readyState; }
80
81    enum PlaybackState { WAITING, PLAYING, ENDED };
82    const AtomicString& playbackState() const;
83
84    bool isRestrained() const;
85    bool isBlocked() const;
86
87#if !ENABLE(OILPAN)
88    void clearExecutionContext() { m_executionContext = nullptr; }
89#endif
90
91    virtual void trace(Visitor*) OVERRIDE;
92
93private:
94    MediaController(ExecutionContext*);
95    void reportControllerState();
96    void updateReadyState();
97    void updatePlaybackState();
98    void updateMediaElements();
99    void bringElementUpToSpeed(HTMLMediaElement*);
100    void scheduleEvent(const AtomicString& eventName);
101    void clearPositionTimerFired(Timer<MediaController>*);
102    bool hasEnded() const;
103    void scheduleTimeupdateEvent();
104    void timeupdateTimerFired(Timer<MediaController>*);
105    void startTimeupdateTimer();
106
107    // EventTarget
108    virtual const AtomicString& interfaceName() const OVERRIDE;
109    virtual ExecutionContext* executionContext() const OVERRIDE { return m_executionContext; }
110
111    friend class HTMLMediaElement;
112    friend class MediaControllerEventListener;
113    // FIXME: A MediaController should ideally keep an otherwise
114    // unreferenced slaved media element alive. When Oilpan is
115    // enabled by default, consider making the hash set references
116    // strong to accomplish that. crbug.com/383072
117    typedef WillBeHeapLinkedHashSet<RawPtrWillBeWeakMember<HTMLMediaElement> > MediaElementSequence;
118    MediaElementSequence m_mediaElements;
119    bool m_paused;
120    double m_defaultPlaybackRate;
121    double m_volume;
122    mutable double m_position;
123    bool m_muted;
124    ReadyState m_readyState;
125    PlaybackState m_playbackState;
126    OwnPtrWillBeMember<GenericEventQueue> m_pendingEventsQueue;
127    mutable Timer<MediaController> m_clearPositionTimer;
128    OwnPtr<Clock> m_clock;
129    RawPtrWillBeWeakMember<ExecutionContext> m_executionContext;
130    Timer<MediaController> m_timeupdateTimer;
131    double m_previousTimeupdateTime;
132};
133
134} // namespace blink
135
136#endif // MediaController_h
137