0804b02e835a70bd1c6d1c253c434e9be9e7a0f9
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / objc / MediaPlaybackTargetPickerMac.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. 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 "MediaPlaybackTargetPickerMac.h"
28
29 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS)
30
31 #import "Logging.h"
32 #import <WebCore/FloatRect.h>
33 #import <WebCore/MediaPlaybackTargetMac.h>
34 #import <objc/runtime.h>
35 #import <pal/cf/CoreMediaSoftLink.h>
36 #import <pal/spi/cocoa/AVKitSPI.h>
37 #import <pal/spi/mac/AVFoundationSPI.h>
38 #import <wtf/MainThread.h>
39
40 typedef AVOutputContext AVOutputContextType;
41 typedef AVOutputDeviceMenuController AVOutputDeviceMenuControllerType;
42
43 SOFTLINK_AVKIT_FRAMEWORK()
44 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
45
46 SOFT_LINK_CLASS_OPTIONAL(AVFoundation, AVOutputContext)
47 SOFT_LINK_CLASS_OPTIONAL(AVKit, AVOutputDeviceMenuController)
48
49 using namespace WebCore;
50
51 static NSString *externalOutputDeviceAvailableKeyName = @"externalOutputDeviceAvailable";
52 static NSString *externalOutputDevicePickedKeyName = @"externalOutputDevicePicked";
53
54 @interface WebAVOutputDeviceMenuControllerHelper : NSObject {
55     MediaPlaybackTargetPickerMac* m_callback;
56 }
57
58 - (instancetype)initWithCallback:(MediaPlaybackTargetPickerMac*)callback;
59 - (void)clearCallback;
60 - (void)observeValueForKeyPath:(id)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
61 @end
62
63 namespace WebCore {
64
65 MediaPlaybackTargetPickerMac::MediaPlaybackTargetPickerMac(MediaPlaybackTargetPicker::Client& client)
66     : MediaPlaybackTargetPicker(client)
67     , m_outputDeviceMenuControllerDelegate(adoptNS([[WebAVOutputDeviceMenuControllerHelper alloc] initWithCallback:this]))
68 {
69 }
70
71 MediaPlaybackTargetPickerMac::~MediaPlaybackTargetPickerMac()
72 {
73     setClient(nullptr);
74     [m_outputDeviceMenuControllerDelegate clearCallback];
75 }
76
77 bool MediaPlaybackTargetPickerMac::externalOutputDeviceAvailable()
78 {
79     return devicePicker().externalOutputDeviceAvailable;
80 }
81
82 Ref<MediaPlaybackTarget> MediaPlaybackTargetPickerMac::playbackTarget()
83 {
84     AVOutputContext* context = m_outputDeviceMenuController ? [m_outputDeviceMenuController.get() outputContext] : nullptr;
85
86     return WebCore::MediaPlaybackTargetMac::create(context);
87 }
88
89 AVOutputDeviceMenuControllerType *MediaPlaybackTargetPickerMac::devicePicker()
90 {
91     if (!getAVOutputDeviceMenuControllerClass())
92         return nullptr;
93
94     if (!m_outputDeviceMenuController) {
95         LOG(Media, "MediaPlaybackTargetPickerMac::devicePicker - allocating picker");
96
97         RetainPtr<AVOutputContextType> context = adoptNS([allocAVOutputContextInstance() init]);
98         m_outputDeviceMenuController = adoptNS([allocAVOutputDeviceMenuControllerInstance() initWithOutputContext:context.get()]);
99
100         [m_outputDeviceMenuController.get() addObserver:m_outputDeviceMenuControllerDelegate.get() forKeyPath:externalOutputDeviceAvailableKeyName options:NSKeyValueObservingOptionNew context:nullptr];
101         [m_outputDeviceMenuController.get() addObserver:m_outputDeviceMenuControllerDelegate.get() forKeyPath:externalOutputDevicePickedKeyName options:NSKeyValueObservingOptionNew context:nullptr];
102
103         LOG(Media, "MediaPlaybackTargetPickerMac::devicePicker - allocated menu controller %p", m_outputDeviceMenuController.get());
104
105         if (m_outputDeviceMenuController.get().externalOutputDeviceAvailable)
106             availableDevicesDidChange();
107     }
108
109     return m_outputDeviceMenuController.get();
110 }
111
112 void MediaPlaybackTargetPickerMac::showPlaybackTargetPicker(const FloatRect& location, bool checkActiveRoute)
113 {
114     if (!client() || m_showingMenu)
115         return;
116
117     LOG(Media, "MediaPlaybackTargetPickerMac::showPlaybackTargetPicker - checkActiveRoute = %i", (int)checkActiveRoute);
118
119     m_showingMenu = true;
120
121     if ([devicePicker() showMenuForRect:location appearanceName:NSAppearanceNameVibrantLight allowReselectionOfSelectedOutputDevice:!checkActiveRoute]) {
122         if (!checkActiveRoute)
123             currentDeviceDidChange();
124     }
125     m_showingMenu = false;
126 }
127
128 void MediaPlaybackTargetPickerMac::startingMonitoringPlaybackTargets()
129 {
130     LOG(Media, "MediaPlaybackTargetPickerMac::startingMonitoringPlaybackTargets");
131
132     devicePicker();
133 }
134
135 void MediaPlaybackTargetPickerMac::stopMonitoringPlaybackTargets()
136 {
137     LOG(Media, "MediaPlaybackTargetPickerMac::stopMonitoringPlaybackTargets");
138     // Nothing to do, AirPlay takes care of this automatically.
139 }
140
141 void MediaPlaybackTargetPickerMac::invalidatePlaybackTargets()
142 {
143     LOG(Media, "MediaPlaybackTargetPickerMac::invalidatePlaybackTargets");
144
145     if (m_outputDeviceMenuController) {
146         [m_outputDeviceMenuController removeObserver:m_outputDeviceMenuControllerDelegate.get() forKeyPath:externalOutputDeviceAvailableKeyName];
147         [m_outputDeviceMenuController removeObserver:m_outputDeviceMenuControllerDelegate.get() forKeyPath:externalOutputDevicePickedKeyName];
148         m_outputDeviceMenuController = nullptr;
149     }
150     currentDeviceDidChange();
151 }
152
153 } // namespace WebCore
154
155 @implementation WebAVOutputDeviceMenuControllerHelper
156 - (instancetype)initWithCallback:(MediaPlaybackTargetPickerMac*)callback
157 {
158     if (!(self = [super init]))
159         return nil;
160
161     m_callback = callback;
162
163     return self;
164 }
165
166 - (void)clearCallback
167 {
168     m_callback = nil;
169 }
170
171 - (void)observeValueForKeyPath:(id)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
172 {
173     UNUSED_PARAM(object);
174     UNUSED_PARAM(change);
175     UNUSED_PARAM(context);
176
177     if (!m_callback)
178         return;
179
180     LOG(Media, "MediaPlaybackTargetPickerMac::observeValueForKeyPath - key = %s", [keyPath UTF8String]);
181
182     if (![keyPath isEqualToString:externalOutputDeviceAvailableKeyName] && ![keyPath isEqualToString:externalOutputDevicePickedKeyName])
183         return;
184
185     RetainPtr<WebAVOutputDeviceMenuControllerHelper> protectedSelf = self;
186     RetainPtr<NSString> protectedKeyPath = keyPath;
187     callOnMainThread([protectedSelf = WTFMove(protectedSelf), protectedKeyPath = WTFMove(protectedKeyPath)] {
188         MediaPlaybackTargetPickerMac* callback = protectedSelf->m_callback;
189         if (!callback)
190             return;
191
192         if ([protectedKeyPath isEqualToString:externalOutputDeviceAvailableKeyName])
193             callback->availableDevicesDidChange();
194         else if ([protectedKeyPath isEqualToString:externalOutputDevicePickedKeyName])
195             callback->currentDeviceDidChange();
196     });
197 }
198 @end
199
200 #endif // ENABLE(WIRELESS_PLAYBACK_TARGET)