d0aec25e92e97c2852b96dd35fc41671662d8d89
[WebKit-https.git] / Source / WebKit / WebProcess / cocoa / PlaybackSessionManager.mm
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 #import "config.h"
27 #import "PlaybackSessionManager.h"
28
29 #if PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))
30
31 #import "Attachment.h"
32 #import "PlaybackSessionManagerMessages.h"
33 #import "PlaybackSessionManagerProxyMessages.h"
34 #import "WebCoreArgumentCoders.h"
35 #import "WebPage.h"
36 #import "WebProcess.h"
37 #import <WebCore/Color.h>
38 #import <WebCore/Event.h>
39 #import <WebCore/EventNames.h>
40 #import <WebCore/HTMLMediaElement.h>
41 #import <WebCore/Settings.h>
42 #import <WebCore/TimeRanges.h>
43 #import <WebCore/UserGestureIndicator.h>
44 #import <mach/mach_port.h>
45
46 namespace WebKit {
47 using namespace WebCore;
48
49 static uint64_t nextContextId()
50 {
51     static uint64_t contextId = 0;
52     return ++contextId;
53 }
54
55 #pragma mark - PlaybackSessionInterfaceContext
56
57 PlaybackSessionInterfaceContext::PlaybackSessionInterfaceContext(PlaybackSessionManager& manager, uint64_t contextId)
58     : m_manager(&manager)
59     , m_contextId(contextId)
60 {
61 }
62
63 PlaybackSessionInterfaceContext::~PlaybackSessionInterfaceContext()
64 {
65 }
66
67 void PlaybackSessionInterfaceContext::resetMediaState()
68 {
69     if (m_manager)
70         m_manager->resetMediaState(m_contextId);
71 }
72
73 void PlaybackSessionInterfaceContext::durationChanged(double duration)
74 {
75     if (m_manager)
76         m_manager->durationChanged(m_contextId, duration);
77 }
78
79 void PlaybackSessionInterfaceContext::currentTimeChanged(double currentTime, double anchorTime)
80 {
81     if (m_manager)
82         m_manager->currentTimeChanged(m_contextId, currentTime, anchorTime);
83 }
84
85 void PlaybackSessionInterfaceContext::bufferedTimeChanged(double bufferedTime)
86 {
87     if (m_manager)
88         m_manager->bufferedTimeChanged(m_contextId, bufferedTime);
89 }
90
91 void PlaybackSessionInterfaceContext::rateChanged(bool isPlaying, float playbackRate)
92 {
93     if (m_manager)
94         m_manager->rateChanged(m_contextId, isPlaying, playbackRate);
95 }
96
97 void PlaybackSessionInterfaceContext::playbackStartedTimeChanged(double playbackStartedTime)
98 {
99     if (m_manager)
100         m_manager->playbackStartedTimeChanged(m_contextId, playbackStartedTime);
101 }
102
103 void PlaybackSessionInterfaceContext::seekableRangesChanged(const WebCore::TimeRanges& ranges, double lastModifiedTime, double liveUpdateInterval)
104 {
105     if (m_manager)
106         m_manager->seekableRangesChanged(m_contextId, ranges, lastModifiedTime, liveUpdateInterval);
107 }
108
109 void PlaybackSessionInterfaceContext::canPlayFastReverseChanged(bool value)
110 {
111     if (m_manager)
112         m_manager->canPlayFastReverseChanged(m_contextId, value);
113 }
114
115 void PlaybackSessionInterfaceContext::audioMediaSelectionOptionsChanged(const Vector<MediaSelectionOption>& options, uint64_t selectedIndex)
116 {
117     if (m_manager)
118         m_manager->audioMediaSelectionOptionsChanged(m_contextId, options, selectedIndex);
119 }
120
121 void PlaybackSessionInterfaceContext::legibleMediaSelectionOptionsChanged(const Vector<MediaSelectionOption>& options, uint64_t selectedIndex)
122 {
123     if (m_manager)
124         m_manager->legibleMediaSelectionOptionsChanged(m_contextId, options, selectedIndex);
125 }
126
127 void PlaybackSessionInterfaceContext::audioMediaSelectionIndexChanged(uint64_t selectedIndex)
128 {
129     if (m_manager)
130         m_manager->audioMediaSelectionIndexChanged(m_contextId, selectedIndex);
131 }
132
133 void PlaybackSessionInterfaceContext::legibleMediaSelectionIndexChanged(uint64_t selectedIndex)
134 {
135     if (m_manager)
136         m_manager->legibleMediaSelectionIndexChanged(m_contextId, selectedIndex);
137 }
138
139 void PlaybackSessionInterfaceContext::externalPlaybackChanged(bool enabled, PlaybackSessionModel::ExternalPlaybackTargetType type, const String& localizedDeviceName)
140 {
141     if (m_manager)
142         m_manager->externalPlaybackChanged(m_contextId, enabled, type, localizedDeviceName);
143 }
144
145 void PlaybackSessionInterfaceContext::wirelessVideoPlaybackDisabledChanged(bool disabled)
146 {
147     if (m_manager)
148         m_manager->wirelessVideoPlaybackDisabledChanged(m_contextId, disabled);
149 }
150
151 void PlaybackSessionInterfaceContext::mutedChanged(bool muted)
152 {
153     if (m_manager)
154         m_manager->mutedChanged(m_contextId, muted);
155 }
156
157 void PlaybackSessionInterfaceContext::isPictureInPictureSupportedChanged(bool supported)
158 {
159     if (m_manager)
160         m_manager->isPictureInPictureSupportedChanged(m_contextId, supported);
161 }
162
163 void PlaybackSessionInterfaceContext::volumeChanged(double volume)
164 {
165     if (m_manager)
166         m_manager->volumeChanged(m_contextId, volume);
167 }
168
169 #pragma mark - PlaybackSessionManager
170
171 Ref<PlaybackSessionManager> PlaybackSessionManager::create(WebPage& page)
172 {
173     return adoptRef(*new PlaybackSessionManager(page));
174 }
175
176 PlaybackSessionManager::PlaybackSessionManager(WebPage& page)
177     : m_page(&page)
178 {
179     WebProcess::singleton().addMessageReceiver(Messages::PlaybackSessionManager::messageReceiverName(), page.pageID(), *this);
180 }
181
182 PlaybackSessionManager::~PlaybackSessionManager()
183 {
184     for (auto& tuple : m_contextMap.values()) {
185         RefPtr<PlaybackSessionModelMediaElement> model;
186         RefPtr<PlaybackSessionInterfaceContext> interface;
187         std::tie(model, interface) = tuple;
188         model->removeClient(*interface);
189         model->setMediaElement(nullptr);
190
191         interface->invalidate();
192     }
193
194     m_contextMap.clear();
195     m_mediaElements.clear();
196     m_clientCounts.clear();
197
198     if (m_page)
199         WebProcess::singleton().removeMessageReceiver(Messages::PlaybackSessionManager::messageReceiverName(), m_page->pageID());
200 }
201
202 void PlaybackSessionManager::invalidate()
203 {
204     ASSERT(m_page);
205     WebProcess::singleton().removeMessageReceiver(Messages::PlaybackSessionManager::messageReceiverName(), m_page->pageID());
206     m_page = nullptr;
207 }
208
209 PlaybackSessionManager::ModelInterfaceTuple PlaybackSessionManager::createModelAndInterface(uint64_t contextId)
210 {
211     RefPtr<PlaybackSessionModelMediaElement> model = PlaybackSessionModelMediaElement::create();
212     RefPtr<PlaybackSessionInterfaceContext> interface = PlaybackSessionInterfaceContext::create(*this, contextId);
213     model->addClient(*interface);
214
215     return std::make_tuple(WTFMove(model), WTFMove(interface));
216 }
217
218 PlaybackSessionManager::ModelInterfaceTuple& PlaybackSessionManager::ensureModelAndInterface(uint64_t contextId)
219 {
220     auto addResult = m_contextMap.add(contextId, ModelInterfaceTuple());
221     if (addResult.isNewEntry)
222         addResult.iterator->value = createModelAndInterface(contextId);
223     return addResult.iterator->value;
224 }
225
226 WebCore::PlaybackSessionModelMediaElement& PlaybackSessionManager::ensureModel(uint64_t contextId)
227 {
228     return *std::get<0>(ensureModelAndInterface(contextId));
229 }
230
231 PlaybackSessionInterfaceContext& PlaybackSessionManager::ensureInterface(uint64_t contextId)
232 {
233     return *std::get<1>(ensureModelAndInterface(contextId));
234 }
235
236 void PlaybackSessionManager::removeContext(uint64_t contextId)
237 {
238     RefPtr<PlaybackSessionModelMediaElement> model;
239     RefPtr<PlaybackSessionInterfaceContext> interface;
240     std::tie(model, interface) = ensureModelAndInterface(contextId);
241
242     RefPtr<HTMLMediaElement> mediaElement = model->mediaElement();
243     model->setMediaElement(nullptr);
244     model->removeClient(*interface);
245     interface->invalidate();
246     m_mediaElements.remove(mediaElement.get());
247     m_contextMap.remove(contextId);
248 }
249
250 void PlaybackSessionManager::addClientForContext(uint64_t contextId)
251 {
252     m_clientCounts.add(contextId);
253 }
254
255 void PlaybackSessionManager::removeClientForContext(uint64_t contextId)
256 {
257     ASSERT(m_clientCounts.contains(contextId));
258     if (m_clientCounts.remove(contextId))
259         removeContext(contextId);
260 }
261
262 void PlaybackSessionManager::setUpPlaybackControlsManager(WebCore::HTMLMediaElement& mediaElement)
263 {
264     auto foundIterator = m_mediaElements.find(&mediaElement);
265     if (foundIterator != m_mediaElements.end()) {
266         uint64_t contextId = foundIterator->value;
267         if (m_controlsManagerContextId == contextId)
268             return;
269
270         auto previousContextId = m_controlsManagerContextId;
271         m_controlsManagerContextId = contextId;
272         if (previousContextId)
273             removeClientForContext(previousContextId);
274     } else {
275         auto contextId = m_mediaElements.ensure(&mediaElement, [&] { return nextContextId(); }).iterator->value;
276
277         auto previousContextId = m_controlsManagerContextId;
278         m_controlsManagerContextId = contextId;
279         if (previousContextId)
280             removeClientForContext(previousContextId);
281
282         ensureModel(contextId).setMediaElement(&mediaElement);
283     }
284
285     addClientForContext(m_controlsManagerContextId);
286
287     m_page->videoControlsManagerDidChange();
288     m_page->send(Messages::PlaybackSessionManagerProxy::SetUpPlaybackControlsManagerWithID(m_controlsManagerContextId), m_page->pageID());
289 }
290
291 void PlaybackSessionManager::clearPlaybackControlsManager()
292 {
293     if (!m_controlsManagerContextId)
294         return;
295
296     removeClientForContext(m_controlsManagerContextId);
297     m_controlsManagerContextId = 0;
298
299     m_page->videoControlsManagerDidChange();
300     m_page->send(Messages::PlaybackSessionManagerProxy::ClearPlaybackControlsManager(), m_page->pageID());
301 }
302
303 uint64_t PlaybackSessionManager::contextIdForMediaElement(WebCore::HTMLMediaElement& mediaElement)
304 {
305     auto addResult = m_mediaElements.ensure(&mediaElement, [&] { return nextContextId(); });
306     uint64_t contextId = addResult.iterator->value;
307     ensureModel(contextId).setMediaElement(&mediaElement);
308     return contextId;
309 }
310
311 WebCore::HTMLMediaElement* PlaybackSessionManager::currentPlaybackControlsElement() const
312 {
313     if (!m_controlsManagerContextId)
314         return nullptr;
315
316     auto iter = m_contextMap.find(m_controlsManagerContextId);
317     if (iter == m_contextMap.end())
318         return nullptr;
319
320     return std::get<0>(iter->value)->mediaElement();
321 }
322
323 #pragma mark Interface to PlaybackSessionInterfaceContext:
324
325 void PlaybackSessionManager::resetMediaState(uint64_t contextId)
326 {
327     m_page->send(Messages::PlaybackSessionManagerProxy::ResetMediaState(contextId), m_page->pageID());
328 }
329
330 void PlaybackSessionManager::durationChanged(uint64_t contextId, double duration)
331 {
332     m_page->send(Messages::PlaybackSessionManagerProxy::DurationChanged(contextId, duration), m_page->pageID());
333 }
334
335 void PlaybackSessionManager::currentTimeChanged(uint64_t contextId, double currentTime, double anchorTime)
336 {
337     m_page->send(Messages::PlaybackSessionManagerProxy::CurrentTimeChanged(contextId, currentTime, anchorTime), m_page->pageID());
338 }
339
340 void PlaybackSessionManager::bufferedTimeChanged(uint64_t contextId, double bufferedTime)
341 {
342     m_page->send(Messages::PlaybackSessionManagerProxy::BufferedTimeChanged(contextId, bufferedTime), m_page->pageID());
343 }
344
345 void PlaybackSessionManager::playbackStartedTimeChanged(uint64_t contextId, double playbackStartedTime)
346 {
347     m_page->send(Messages::PlaybackSessionManagerProxy::PlaybackStartedTimeChanged(contextId, playbackStartedTime), m_page->pageID());
348 }
349
350 void PlaybackSessionManager::rateChanged(uint64_t contextId, bool isPlaying, float playbackRate)
351 {
352     m_page->send(Messages::PlaybackSessionManagerProxy::RateChanged(contextId, isPlaying, playbackRate), m_page->pageID());
353 }
354
355 void PlaybackSessionManager::seekableRangesChanged(uint64_t contextId, const WebCore::TimeRanges& timeRanges, double lastModifiedTime, double liveUpdateInterval)
356 {
357     Vector<std::pair<double, double>> rangesVector;
358     for (unsigned i = 0; i < timeRanges.length(); i++) {
359         double start = timeRanges.ranges().start(i).toDouble();
360         double end = timeRanges.ranges().end(i).toDouble();
361         rangesVector.append({ start, end });
362     }
363     m_page->send(Messages::PlaybackSessionManagerProxy::SeekableRangesVectorChanged(contextId, WTFMove(rangesVector), lastModifiedTime, liveUpdateInterval), m_page->pageID());
364 }
365
366 void PlaybackSessionManager::canPlayFastReverseChanged(uint64_t contextId, bool value)
367 {
368     m_page->send(Messages::PlaybackSessionManagerProxy::CanPlayFastReverseChanged(contextId, value), m_page->pageID());
369 }
370
371 void PlaybackSessionManager::audioMediaSelectionOptionsChanged(uint64_t contextId, const Vector<MediaSelectionOption>& options, uint64_t selectedIndex)
372 {
373     m_page->send(Messages::PlaybackSessionManagerProxy::AudioMediaSelectionOptionsChanged(contextId, options, selectedIndex), m_page->pageID());
374 }
375
376 void PlaybackSessionManager::legibleMediaSelectionOptionsChanged(uint64_t contextId, const Vector<MediaSelectionOption>& options, uint64_t selectedIndex)
377 {
378     m_page->send(Messages::PlaybackSessionManagerProxy::LegibleMediaSelectionOptionsChanged(contextId, options, selectedIndex), m_page->pageID());
379 }
380
381 void PlaybackSessionManager::externalPlaybackChanged(uint64_t contextId, bool enabled, PlaybackSessionModel::ExternalPlaybackTargetType targetType, String localizedDeviceName)
382 {
383     m_page->send(Messages::PlaybackSessionManagerProxy::ExternalPlaybackPropertiesChanged(contextId, enabled, static_cast<uint32_t>(targetType), localizedDeviceName), m_page->pageID());
384 }
385
386 void PlaybackSessionManager::audioMediaSelectionIndexChanged(uint64_t contextId, uint64_t selectedIndex)
387 {
388     m_page->send(Messages::PlaybackSessionManagerProxy::AudioMediaSelectionIndexChanged(contextId, selectedIndex), m_page->pageID());
389 }
390
391 void PlaybackSessionManager::legibleMediaSelectionIndexChanged(uint64_t contextId, uint64_t selectedIndex)
392 {
393     m_page->send(Messages::PlaybackSessionManagerProxy::LegibleMediaSelectionIndexChanged(contextId, selectedIndex), m_page->pageID());
394 }
395
396 void PlaybackSessionManager::wirelessVideoPlaybackDisabledChanged(uint64_t contextId, bool disabled)
397 {
398     m_page->send(Messages::PlaybackSessionManagerProxy::WirelessVideoPlaybackDisabledChanged(contextId, disabled));
399 }
400
401 void PlaybackSessionManager::mutedChanged(uint64_t contextId, bool muted)
402 {
403     m_page->send(Messages::PlaybackSessionManagerProxy::MutedChanged(contextId, muted));
404 }
405
406 void PlaybackSessionManager::volumeChanged(uint64_t contextId, double volume)
407 {
408     m_page->send(Messages::PlaybackSessionManagerProxy::VolumeChanged(contextId, volume));
409 }
410
411 void PlaybackSessionManager::isPictureInPictureSupportedChanged(uint64_t contextId, bool supported)
412 {
413     m_page->send(Messages::PlaybackSessionManagerProxy::PictureInPictureSupportedChanged(contextId, supported));
414 }
415
416 #pragma mark Messages from PlaybackSessionManagerProxy:
417
418 void PlaybackSessionManager::play(uint64_t contextId)
419 {
420     UserGestureIndicator indicator(ProcessingUserGesture);
421     ensureModel(contextId).play();
422 }
423
424 void PlaybackSessionManager::pause(uint64_t contextId)
425 {
426     UserGestureIndicator indicator(ProcessingUserGesture);
427     ensureModel(contextId).pause();
428 }
429
430 void PlaybackSessionManager::togglePlayState(uint64_t contextId)
431 {
432     UserGestureIndicator indicator(ProcessingUserGesture);
433     ensureModel(contextId).togglePlayState();
434 }
435
436 void PlaybackSessionManager::beginScrubbing(uint64_t contextId)
437 {
438     UserGestureIndicator indicator(ProcessingUserGesture);
439     ensureModel(contextId).beginScrubbing();
440 }
441
442 void PlaybackSessionManager::endScrubbing(uint64_t contextId)
443 {
444     UserGestureIndicator indicator(ProcessingUserGesture);
445     ensureModel(contextId).endScrubbing();
446 }
447
448 void PlaybackSessionManager::seekToTime(uint64_t contextId, double time, double toleranceBefore, double toleranceAfter)
449 {
450     UserGestureIndicator indicator(ProcessingUserGesture);
451     ensureModel(contextId).seekToTime(time, toleranceBefore, toleranceAfter);
452 }
453
454 void PlaybackSessionManager::fastSeek(uint64_t contextId, double time)
455 {
456     UserGestureIndicator indicator(ProcessingUserGesture);
457     ensureModel(contextId).fastSeek(time);
458 }
459
460 void PlaybackSessionManager::beginScanningForward(uint64_t contextId)
461 {
462     UserGestureIndicator indicator(ProcessingUserGesture);
463     ensureModel(contextId).beginScanningForward();
464 }
465
466 void PlaybackSessionManager::beginScanningBackward(uint64_t contextId)
467 {
468     UserGestureIndicator indicator(ProcessingUserGesture);
469     ensureModel(contextId).beginScanningBackward();
470 }
471
472 void PlaybackSessionManager::endScanning(uint64_t contextId)
473 {
474     UserGestureIndicator indicator(ProcessingUserGesture);
475     ensureModel(contextId).endScanning();
476 }
477
478 void PlaybackSessionManager::selectAudioMediaOption(uint64_t contextId, uint64_t index)
479 {
480     UserGestureIndicator indicator(ProcessingUserGesture);
481     ensureModel(contextId).selectAudioMediaOption(index);
482 }
483
484 void PlaybackSessionManager::selectLegibleMediaOption(uint64_t contextId, uint64_t index)
485 {
486     UserGestureIndicator indicator(ProcessingUserGesture);
487     ensureModel(contextId).selectLegibleMediaOption(index);
488 }
489
490 void PlaybackSessionManager::handleControlledElementIDRequest(uint64_t contextId)
491 {
492     auto element = ensureModel(contextId).mediaElement();
493     if (element)
494         m_page->send(Messages::PlaybackSessionManagerProxy::HandleControlledElementIDResponse(contextId, element->getIdAttribute()));
495 }
496
497 void PlaybackSessionManager::togglePictureInPicture(uint64_t contextId)
498 {
499     UserGestureIndicator indicator(ProcessingUserGesture);
500     ensureModel(contextId).togglePictureInPicture();
501 }
502
503 void PlaybackSessionManager::toggleMuted(uint64_t contextId)
504 {
505     UserGestureIndicator indicator(ProcessingUserGesture);
506     ensureModel(contextId).toggleMuted();
507 }
508
509 void PlaybackSessionManager::setMuted(uint64_t contextId, bool muted)
510 {
511     UserGestureIndicator indicator(ProcessingUserGesture);
512     ensureModel(contextId).setMuted(muted);
513 }
514
515 void PlaybackSessionManager::setVolume(uint64_t contextId, double volume)
516 {
517     UserGestureIndicator indicator(ProcessingUserGesture);
518     ensureModel(contextId).setVolume(volume);
519 }
520
521 void PlaybackSessionManager::setPlayingOnSecondScreen(uint64_t contextId, bool value)
522 {
523     UserGestureIndicator indicator(ProcessingUserGesture);
524     ensureModel(contextId).setPlayingOnSecondScreen(value);
525 }
526
527 } // namespace WebKit
528
529 #endif // PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))