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