Implement the Remote Playback API.
[WebKit-https.git] / Source / WebKitLegacy / mac / WebView / WebMediaPlaybackTargetPicker.mm
1 /*
2  * Copyright (C) 2015 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 #import "WebMediaPlaybackTargetPicker.h"
27
28 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS_FAMILY)
29
30 #import <WebCore/MediaPlaybackTarget.h>
31 #import <WebCore/Page.h>
32 #import <WebCore/WebMediaSessionManager.h>
33
34 std::unique_ptr<WebMediaPlaybackTargetPicker> WebMediaPlaybackTargetPicker::create(WebCore::Page& page)
35 {
36     return makeUnique<WebMediaPlaybackTargetPicker>(page);
37 }
38
39 WebMediaPlaybackTargetPicker::WebMediaPlaybackTargetPicker(WebCore::Page& page)
40     : m_page(&page)
41 {
42 }
43
44 void WebMediaPlaybackTargetPicker::addPlaybackTargetPickerClient(uint64_t contextId)
45 {
46     WebCore::WebMediaSessionManager::shared().addPlaybackTargetPickerClient(*this, contextId);
47 }
48
49 void WebMediaPlaybackTargetPicker::removePlaybackTargetPickerClient(uint64_t contextId)
50 {
51     WebCore::WebMediaSessionManager::shared().removePlaybackTargetPickerClient(*this, contextId);
52 }
53
54 void WebMediaPlaybackTargetPicker::showPlaybackTargetPicker(uint64_t contextId, const WebCore::FloatRect& rect, bool hasVideo)
55 {
56     WebCore::WebMediaSessionManager::shared().showPlaybackTargetPicker(*this, contextId, WebCore::IntRect(rect), hasVideo, m_page ? m_page->useDarkAppearance() : false);
57 }
58
59 void WebMediaPlaybackTargetPicker::playbackTargetPickerClientStateDidChange(uint64_t contextId, WebCore::MediaProducer::MediaStateFlags state)
60 {
61     WebCore::WebMediaSessionManager::shared().clientStateDidChange(*this, contextId, state);
62 }
63
64 void WebMediaPlaybackTargetPicker::setMockMediaPlaybackTargetPickerEnabled(bool enabled)
65 {
66     WebCore::WebMediaSessionManager::shared().setMockMediaPlaybackTargetPickerEnabled(enabled);
67 }
68
69 void WebMediaPlaybackTargetPicker::setMockMediaPlaybackTargetPickerState(const String& name, WebCore::MediaPlaybackTargetContext::State state)
70 {
71     WebCore::WebMediaSessionManager::shared().setMockMediaPlaybackTargetPickerState(name, state);
72 }
73
74 void WebMediaPlaybackTargetPicker::mockMediaPlaybackTargetPickerDismissPopup()
75 {
76     WebCore::WebMediaSessionManager::shared().mockMediaPlaybackTargetPickerDismissPopup();
77 }
78
79 void WebMediaPlaybackTargetPicker::setPlaybackTarget(uint64_t contextId, Ref<WebCore::MediaPlaybackTarget>&& target)
80 {
81     if (!m_page)
82         return;
83
84     m_page->setPlaybackTarget(contextId, WTFMove(target));
85 }
86
87 void WebMediaPlaybackTargetPicker::externalOutputDeviceAvailableDidChange(uint64_t contextId, bool available)
88 {
89     if (!m_page)
90         return;
91
92     m_page->playbackTargetAvailabilityDidChange(contextId, available);
93 }
94
95 void WebMediaPlaybackTargetPicker::setShouldPlayToPlaybackTarget(uint64_t contextId, bool shouldPlay)
96 {
97     if (!m_page)
98         return;
99
100     m_page->setShouldPlayToPlaybackTarget(contextId, shouldPlay);
101 }
102
103 void WebMediaPlaybackTargetPicker::playbackTargetPickerWasDismissed(uint64_t contextId)
104 {
105     if (m_page)
106         m_page->playbackTargetPickerWasDismissed(contextId);
107 }
108
109 void WebMediaPlaybackTargetPicker::invalidate()
110 {
111     m_page = nullptr;
112     WebCore::WebMediaSessionManager::shared().removeAllPlaybackTargetPickerClients(*this);
113 }
114
115 #endif