834673af94f110d254c68554c40e00819a756a4b
[WebKit.git] / Source / WebKit / GPUProcess / media / RemoteMediaPlayerManagerProxy.cpp
1 /*
2  * Copyright (C) 2019 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 #include "config.h"
27 #include "RemoteMediaPlayerManagerProxy.h"
28
29 #if ENABLE(GPU_PROCESS)
30
31 #include "GPUConnectionToWebProcess.h"
32 #include "RemoteMediaPlayerConfiguration.h"
33 #include "RemoteMediaPlayerManagerMessages.h"
34 #include "RemoteMediaPlayerManagerProxyMessages.h"
35 #include "RemoteMediaPlayerProxy.h"
36 #include "WebCoreArgumentCoders.h"
37 #include <WebCore/MediaPlayer.h>
38 #include <WebCore/MediaPlayerPrivate.h>
39 #include <wtf/UniqueRef.h>
40
41 #define MESSAGE_CHECK_CONTEXTID(identifier) MESSAGE_CHECK_BASE(m_proxies.isValidKey(identifier), &m_gpuConnectionToWebProcess.connection())
42
43 namespace WebKit {
44 using namespace WebCore;
45
46 RemoteMediaPlayerManagerProxy::RemoteMediaPlayerManagerProxy(GPUConnectionToWebProcess& connection)
47     : m_gpuConnectionToWebProcess(connection)
48 #if !RELEASE_LOG_DISABLED
49     , m_logIdentifier(WTF::LoggerHelper::uniqueLogIdentifier())
50 #endif
51 {
52 }
53
54 RemoteMediaPlayerManagerProxy::~RemoteMediaPlayerManagerProxy()
55 {
56 }
57
58 void RemoteMediaPlayerManagerProxy::createMediaPlayer(MediaPlayerPrivateRemoteIdentifier id, MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, RemoteMediaPlayerProxyConfiguration&& proxyConfiguration, CompletionHandler<void(RemoteMediaPlayerConfiguration&)>&& completionHandler)
59 {
60     MESSAGE_CHECK_CONTEXTID(id);
61
62     ASSERT(!m_proxies.contains(id));
63
64     RemoteMediaPlayerConfiguration playerConfiguration;
65
66     auto proxy = makeUnique<RemoteMediaPlayerProxy>(*this, id, m_gpuConnectionToWebProcess.connection(), engineIdentifier, WTFMove(proxyConfiguration));
67     proxy->getConfiguration(playerConfiguration);
68     m_proxies.add(id, WTFMove(proxy));
69
70     completionHandler(playerConfiguration);
71 }
72
73 void RemoteMediaPlayerManagerProxy::deleteMediaPlayer(MediaPlayerPrivateRemoteIdentifier id)
74 {
75     if (auto proxy = m_proxies.take(id))
76         proxy->invalidate();
77 }
78
79 void RemoteMediaPlayerManagerProxy::getSupportedTypes(MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, CompletionHandler<void(Vector<String>&&)>&& completionHandler)
80 {
81     auto engine = MediaPlayer::mediaEngine(engineIdentifier);
82     if (!engine) {
83         WTFLogAlways("Failed to find media engine.");
84         completionHandler({ });
85         return;
86     }
87
88     HashSet<String, ASCIICaseInsensitiveHash> engineTypes;
89     engine->getSupportedTypes(engineTypes);
90
91     auto result = WTF::map(engineTypes, [] (auto& type) {
92         return type;
93     });
94
95     completionHandler(WTFMove(result));
96 }
97
98 void RemoteMediaPlayerManagerProxy::supportsType(MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, const WebCore::MediaEngineSupportParameters&& parameters, CompletionHandler<void(MediaPlayer::SupportsType)>&& completionHandler)
99 {
100     auto engine = MediaPlayer::mediaEngine(engineIdentifier);
101     if (!engine) {
102         WTFLogAlways("Failed to find media engine.");
103         completionHandler(MediaPlayer::SupportsType::IsNotSupported);
104         return;
105     }
106
107     auto result = engine->supportsTypeAndCodecs(parameters);
108     completionHandler(result);
109 }
110
111 void RemoteMediaPlayerManagerProxy::originsInMediaCache(MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, const String&& path, CompletionHandler<void(Vector<WebCore::SecurityOriginData>&&)>&& completionHandler)
112 {
113     auto engine = MediaPlayer::mediaEngine(engineIdentifier);
114     if (!engine) {
115         WTFLogAlways("Failed to find media engine.");
116         completionHandler({ });
117         return;
118     }
119
120     auto origins = engine->originsInMediaCache(path);
121
122     Vector<WebCore::SecurityOriginData> result;
123     for (auto& origin : origins)
124         result.append(origin->data());
125
126     completionHandler(WTFMove(result));
127 }
128
129 void RemoteMediaPlayerManagerProxy::clearMediaCache(MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, const String&&path, WallTime modifiedSince)
130 {
131     auto engine = MediaPlayer::mediaEngine(engineIdentifier);
132     if (!engine) {
133         WTFLogAlways("Failed to find media engine.");
134         return;
135     }
136
137     engine->clearMediaCache(path, modifiedSince);
138 }
139
140 void RemoteMediaPlayerManagerProxy::clearMediaCacheForOrigins(MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, const String&& path, Vector<WebCore::SecurityOriginData>&& originData)
141 {
142     auto engine = MediaPlayer::mediaEngine(engineIdentifier);
143     if (!engine) {
144         WTFLogAlways("Failed to find media engine.");
145         return;
146     }
147
148     HashSet<RefPtr<SecurityOrigin>> origins;
149     for (auto& data : originData)
150         origins.add(data.securityOrigin());
151
152     engine->clearMediaCacheForOrigins(path, origins);
153 }
154
155 void RemoteMediaPlayerManagerProxy::supportsKeySystem(MediaPlayerEnums::MediaEngineIdentifier engineIdentifier, const String&& keySystem, const String&& mimeType, CompletionHandler<void(bool)>&& completionHandler)
156 {
157     auto engine = MediaPlayer::mediaEngine(engineIdentifier);
158     if (!engine) {
159         WTFLogAlways("Failed to find media engine.");
160         return;
161     }
162
163     auto result = engine->supportsKeySystem(keySystem, mimeType);
164     completionHandler(result);
165 }
166
167 void RemoteMediaPlayerManagerProxy::prepareForPlayback(MediaPlayerPrivateRemoteIdentifier id, bool privateMode, WebCore::MediaPlayerEnums::Preload preload, bool preservesPitch, bool prepareForRendering)
168 {
169     if (auto player = m_proxies.get(id))
170         player->prepareForPlayback(privateMode, preload, preservesPitch, prepareForRendering);
171 }
172
173 void RemoteMediaPlayerManagerProxy::load(MediaPlayerPrivateRemoteIdentifier id, URL&& url, WebCore::ContentType&& contentType, String&& keySystem)
174 {
175     if (auto player = m_proxies.get(id))
176         player->load(WTFMove(url), WTFMove(contentType), WTFMove(keySystem));
177 }
178
179 void RemoteMediaPlayerManagerProxy::cancelLoad(MediaPlayerPrivateRemoteIdentifier id)
180 {
181     if (auto player = m_proxies.get(id))
182         player->cancelLoad();
183 }
184
185 void RemoteMediaPlayerManagerProxy::prepareToPlay(MediaPlayerPrivateRemoteIdentifier id)
186 {
187     if (auto player = m_proxies.get(id))
188         player->prepareToPlay();
189 }
190
191 void RemoteMediaPlayerManagerProxy::play(MediaPlayerPrivateRemoteIdentifier id)
192 {
193     if (auto player = m_proxies.get(id))
194         player->play();
195 }
196
197 void RemoteMediaPlayerManagerProxy::pause(MediaPlayerPrivateRemoteIdentifier id)
198 {
199     if (auto player = m_proxies.get(id))
200         player->pause();
201 }
202
203 void RemoteMediaPlayerManagerProxy::seek(MediaPlayerPrivateRemoteIdentifier id, MediaTime&& time)
204 {
205     if (auto player = m_proxies.get(id))
206         player->seek(WTFMove(time));
207 }
208
209 void RemoteMediaPlayerManagerProxy::seekWithTolerance(MediaPlayerPrivateRemoteIdentifier id, MediaTime&& time, MediaTime&& negativeTolerance, MediaTime&& positiveTolerance)
210 {
211     if (auto player = m_proxies.get(id))
212         player->seekWithTolerance(WTFMove(time), WTFMove(negativeTolerance), WTFMove(positiveTolerance));
213 }
214
215 void RemoteMediaPlayerManagerProxy::setVolume(MediaPlayerPrivateRemoteIdentifier id, double volume)
216 {
217     if (auto player = m_proxies.get(id))
218         player->setVolume(volume);
219 }
220
221 void RemoteMediaPlayerManagerProxy::setMuted(MediaPlayerPrivateRemoteIdentifier id, bool muted)
222 {
223     if (auto player = m_proxies.get(id))
224         player->setMuted(muted);
225 }
226
227 void RemoteMediaPlayerManagerProxy::setPreload(MediaPlayerPrivateRemoteIdentifier id, WebCore::MediaPlayerEnums::Preload preload)
228 {
229     if (auto player = m_proxies.get(id))
230         player->setPreload(preload);
231 }
232
233 void RemoteMediaPlayerManagerProxy::setPrivateBrowsingMode(MediaPlayerPrivateRemoteIdentifier id, bool privateMode)
234 {
235     if (auto player = m_proxies.get(id))
236         player->setPrivateBrowsingMode(privateMode);
237 }
238
239 void RemoteMediaPlayerManagerProxy::setPreservesPitch(MediaPlayerPrivateRemoteIdentifier id, bool preservesPitch)
240 {
241     if (auto player = m_proxies.get(id))
242         player->setPreservesPitch(preservesPitch);
243 }
244
245 #if !RELEASE_LOG_DISABLED
246 const Logger& RemoteMediaPlayerManagerProxy::logger() const
247 {
248     return m_gpuConnectionToWebProcess.logger();
249 }
250
251 WTFLogChannel& RemoteMediaPlayerManagerProxy::logChannel() const
252 {
253     return WebKit2LogMedia;
254 }
255 #endif
256
257 } // namespace WebKit
258
259 #undef MESSAGE_CHECK_CONTEXTID
260
261 #endif