af3ca72970e59d1d3ab9e7b7c615225b468111a5
[WebKit-https.git] / Source / WebKit2 / WebProcess / cocoa / WebPlaybackSessionManager.h
1 /*
2  * Copyright (C) 2016-2017 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))
29
30 #include "MessageReceiver.h"
31 #include <WebCore/EventListener.h>
32 #include <WebCore/HTMLMediaElementEnums.h>
33 #include <WebCore/PlatformCALayer.h>
34 #include <WebCore/WebPlaybackSessionInterface.h>
35 #include <WebCore/WebPlaybackSessionModelMediaElement.h>
36 #include <wtf/HashCountedSet.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/RefCounted.h>
39 #include <wtf/RefPtr.h>
40
41 namespace IPC {
42 class Attachment;
43 class Connection;
44 class Decoder;
45 class MessageReceiver;
46 }
47
48 namespace WebCore {
49 class Node;
50 }
51
52 namespace WebKit {
53
54 class WebPage;
55 class WebPlaybackSessionManager;
56
57 class WebPlaybackSessionInterfaceContext final
58     : public RefCounted<WebPlaybackSessionInterfaceContext>
59     , public WebCore::WebPlaybackSessionInterface
60     , public WebCore::WebPlaybackSessionModelClient {
61 public:
62     static Ref<WebPlaybackSessionInterfaceContext> create(WebPlaybackSessionManager& manager, uint64_t contextId)
63     {
64         return adoptRef(*new WebPlaybackSessionInterfaceContext(manager, contextId));
65     }
66     virtual ~WebPlaybackSessionInterfaceContext();
67
68     void invalidate() { m_manager = nullptr; }
69
70 private:
71     friend class WebVideoFullscreenInterfaceContext;
72
73     // WebPlaybackSessionInterface
74     void resetMediaState() final;
75
76     // WebPlaybackModelClient
77     void durationChanged(double) final;
78     void currentTimeChanged(double currentTime, double anchorTime) final;
79     void bufferedTimeChanged(double) final;
80     void playbackStartedTimeChanged(double playbackStartedTime) final;
81     void rateChanged(bool isPlaying, float playbackRate) final;
82     void seekableRangesChanged(const WebCore::TimeRanges&) final;
83     void canPlayFastReverseChanged(bool value) final;
84     void audioMediaSelectionOptionsChanged(const Vector<WebCore::MediaSelectionOption>& options, uint64_t selectedIndex) final;
85     void legibleMediaSelectionOptionsChanged(const Vector<WebCore::MediaSelectionOption>& options, uint64_t selectedIndex) final;
86     void audioMediaSelectionIndexChanged(uint64_t) final;
87     void legibleMediaSelectionIndexChanged(uint64_t) final;
88     void externalPlaybackChanged(bool enabled, WebCore::WebPlaybackSessionModel::ExternalPlaybackTargetType, const String& localizedDeviceName) final;
89     void wirelessVideoPlaybackDisabledChanged(bool) final;
90     void mutedChanged(bool) final;
91
92     WebPlaybackSessionInterfaceContext(WebPlaybackSessionManager&, uint64_t contextId);
93
94     WebPlaybackSessionManager* m_manager;
95     uint64_t m_contextId;
96 };
97
98 class WebPlaybackSessionManager : public RefCounted<WebPlaybackSessionManager>, private IPC::MessageReceiver {
99 public:
100     static Ref<WebPlaybackSessionManager> create(WebPage&);
101     virtual ~WebPlaybackSessionManager();
102
103     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
104
105     void setUpPlaybackControlsManager(WebCore::HTMLMediaElement&);
106     void clearPlaybackControlsManager();
107     uint64_t contextIdForMediaElement(WebCore::HTMLMediaElement&);
108
109 protected:
110     friend class WebPlaybackSessionInterfaceContext;
111     friend class WebVideoFullscreenManager;
112
113     explicit WebPlaybackSessionManager(WebPage&);
114
115     typedef std::tuple<RefPtr<WebCore::WebPlaybackSessionModelMediaElement>, RefPtr<WebPlaybackSessionInterfaceContext>> ModelInterfaceTuple;
116     ModelInterfaceTuple createModelAndInterface(uint64_t contextId);
117     ModelInterfaceTuple& ensureModelAndInterface(uint64_t contextId);
118     WebCore::WebPlaybackSessionModelMediaElement& ensureModel(uint64_t contextId);
119     WebPlaybackSessionInterfaceContext& ensureInterface(uint64_t contextId);
120     void removeContext(uint64_t contextId);
121     void addClientForContext(uint64_t contextId);
122     void removeClientForContext(uint64_t contextId);
123
124     // Interface to WebPlaybackSessionInterfaceContext
125     void resetMediaState(uint64_t contextId);
126     void durationChanged(uint64_t contextId, double);
127     void currentTimeChanged(uint64_t contextId, double currentTime, double anchorTime);
128     void bufferedTimeChanged(uint64_t contextId, double bufferedTime);
129     void playbackStartedTimeChanged(uint64_t contextId, double playbackStartedTime);
130     void rateChanged(uint64_t contextId, bool isPlaying, float playbackRate);
131     void seekableRangesChanged(uint64_t contextId, const WebCore::TimeRanges&);
132     void canPlayFastReverseChanged(uint64_t contextId, bool value);
133     void audioMediaSelectionOptionsChanged(uint64_t contextId, const Vector<WebCore::MediaSelectionOption>& options, uint64_t selectedIndex);
134     void legibleMediaSelectionOptionsChanged(uint64_t contextId, const Vector<WebCore::MediaSelectionOption>& options, uint64_t selectedIndex);
135     void audioMediaSelectionIndexChanged(uint64_t contextId, uint64_t selectedIndex);
136     void legibleMediaSelectionIndexChanged(uint64_t contextId, uint64_t selectedIndex);
137     void externalPlaybackChanged(uint64_t contextId, bool enabled, WebCore::WebPlaybackSessionModel::ExternalPlaybackTargetType, String localizedDeviceName);
138     void wirelessVideoPlaybackDisabledChanged(uint64_t contextId, bool);
139     void mutedChanged(uint64_t contextId, bool);
140
141     // Messages from WebPlaybackSessionManagerProxy
142     void play(uint64_t contextId);
143     void pause(uint64_t contextId);
144     void togglePlayState(uint64_t contextId);
145     void beginScrubbing(uint64_t contextId);
146     void endScrubbing(uint64_t contextId);
147     void seekToTime(uint64_t contextId, double time);
148     void fastSeek(uint64_t contextId, double time);
149     void beginScanningForward(uint64_t contextId);
150     void beginScanningBackward(uint64_t contextId);
151     void endScanning(uint64_t contextId);
152     void selectAudioMediaOption(uint64_t contextId, uint64_t index);
153     void selectLegibleMediaOption(uint64_t contextId, uint64_t index);
154     void handleControlledElementIDRequest(uint64_t contextId);
155     void togglePictureInPicture(uint64_t contextId);
156     void toggleMuted(uint64_t contextId);
157
158     WebPage* m_page;
159     HashMap<WebCore::HTMLMediaElement*, uint64_t> m_mediaElements;
160     HashMap<uint64_t, ModelInterfaceTuple> m_contextMap;
161     uint64_t m_controlsManagerContextId { 0 };
162     HashCountedSet<uint64_t> m_clientCounts;
163 };
164
165 } // namespace WebKit
166
167 #endif // PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))