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