71858e1ecb5713be058e73121be0b6eea5921990
[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
91     WebPlaybackSessionInterfaceContext(WebPlaybackSessionManager&, uint64_t contextId);
92
93     WebPlaybackSessionManager* m_manager;
94     uint64_t m_contextId;
95 };
96
97 class WebPlaybackSessionManager : public RefCounted<WebPlaybackSessionManager>, private IPC::MessageReceiver {
98 public:
99     static Ref<WebPlaybackSessionManager> create(WebPage&);
100     virtual ~WebPlaybackSessionManager();
101
102     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
103
104     void setUpPlaybackControlsManager(WebCore::HTMLMediaElement&);
105     void clearPlaybackControlsManager();
106     uint64_t contextIdForMediaElement(WebCore::HTMLMediaElement&);
107
108 protected:
109     friend class WebPlaybackSessionInterfaceContext;
110     friend class WebVideoFullscreenManager;
111
112     explicit WebPlaybackSessionManager(WebPage&);
113
114     typedef std::tuple<RefPtr<WebCore::WebPlaybackSessionModelMediaElement>, RefPtr<WebPlaybackSessionInterfaceContext>> ModelInterfaceTuple;
115     ModelInterfaceTuple createModelAndInterface(uint64_t contextId);
116     ModelInterfaceTuple& ensureModelAndInterface(uint64_t contextId);
117     WebCore::WebPlaybackSessionModelMediaElement& ensureModel(uint64_t contextId);
118     WebPlaybackSessionInterfaceContext& ensureInterface(uint64_t contextId);
119     void removeContext(uint64_t contextId);
120     void addClientForContext(uint64_t contextId);
121     void removeClientForContext(uint64_t contextId);
122
123     // Interface to WebPlaybackSessionInterfaceContext
124     void resetMediaState(uint64_t contextId);
125     void durationChanged(uint64_t contextId, double);
126     void currentTimeChanged(uint64_t contextId, double currentTime, double anchorTime);
127     void bufferedTimeChanged(uint64_t contextId, double bufferedTime);
128     void playbackStartedTimeChanged(uint64_t contextId, double playbackStartedTime);
129     void rateChanged(uint64_t contextId, bool isPlaying, float playbackRate);
130     void seekableRangesChanged(uint64_t contextId, const WebCore::TimeRanges&);
131     void canPlayFastReverseChanged(uint64_t contextId, bool value);
132     void audioMediaSelectionOptionsChanged(uint64_t contextId, const Vector<WebCore::MediaSelectionOption>& options, uint64_t selectedIndex);
133     void legibleMediaSelectionOptionsChanged(uint64_t contextId, const Vector<WebCore::MediaSelectionOption>& options, uint64_t selectedIndex);
134     void audioMediaSelectionIndexChanged(uint64_t contextId, uint64_t selectedIndex);
135     void legibleMediaSelectionIndexChanged(uint64_t contextId, uint64_t selectedIndex);
136     void externalPlaybackChanged(uint64_t contextId, bool enabled, WebCore::WebPlaybackSessionModel::ExternalPlaybackTargetType, String localizedDeviceName);
137     void wirelessVideoPlaybackDisabledChanged(uint64_t contextId, bool);
138
139     // Messages from WebPlaybackSessionManagerProxy
140     void play(uint64_t contextId);
141     void pause(uint64_t contextId);
142     void togglePlayState(uint64_t contextId);
143     void beginScrubbing(uint64_t contextId);
144     void endScrubbing(uint64_t contextId);
145     void seekToTime(uint64_t contextId, double time);
146     void fastSeek(uint64_t contextId, double time);
147     void beginScanningForward(uint64_t contextId);
148     void beginScanningBackward(uint64_t contextId);
149     void endScanning(uint64_t contextId);
150     void selectAudioMediaOption(uint64_t contextId, uint64_t index);
151     void selectLegibleMediaOption(uint64_t contextId, uint64_t index);
152     void handleControlledElementIDRequest(uint64_t contextId);
153     void togglePictureInPicture(uint64_t contextId);
154
155     WebPage* m_page;
156     HashMap<WebCore::HTMLMediaElement*, uint64_t> m_mediaElements;
157     HashMap<uint64_t, ModelInterfaceTuple> m_contextMap;
158     uint64_t m_controlsManagerContextId { 0 };
159     HashCountedSet<uint64_t> m_clientCounts;
160 };
161
162 } // namespace WebKit
163
164 #endif // PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))