2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "MediaPlaybackTargetPickerMac.h"
29 #if ENABLE(WIRELESS_PLAYBACK_TARGET) && !PLATFORM(IOS)
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>
40 typedef AVOutputContext AVOutputContextType;
41 typedef AVOutputDeviceMenuController AVOutputDeviceMenuControllerType;
43 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
44 SOFT_LINK_FRAMEWORK_OPTIONAL(AVKit)
46 SOFT_LINK_CLASS(AVFoundation, AVOutputContext)
47 SOFT_LINK_CLASS(AVKit, AVOutputDeviceMenuController)
49 using namespace WebCore;
51 static NSString *externalOutputDeviceAvailableKeyName = @"externalOutputDeviceAvailable";
52 static NSString *externalOutputDevicePickedKeyName = @"externalOutputDevicePicked";
54 // FIXME: remove this once the headers are available.
55 @interface AVOutputDeviceMenuController (ForwardDeclaration)
56 - (BOOL)showMenuForRect:(NSRect)screenRect appearanceName:(NSString *)appearanceName allowReselectionOfSelectedOutputDevice:(BOOL)allowReselectionOfSelectedOutputDevice;
59 @interface WebAVOutputDeviceMenuControllerHelper : NSObject {
60 MediaPlaybackTargetPickerMac* m_callback;
63 - (instancetype)initWithCallback:(MediaPlaybackTargetPickerMac*)callback;
64 - (void)clearCallback;
65 - (void)observeValueForKeyPath:(id)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
70 std::unique_ptr<MediaPlaybackTargetPickerMac> MediaPlaybackTargetPickerMac::create(MediaPlaybackTargetPicker::Client& client)
72 return std::unique_ptr<MediaPlaybackTargetPickerMac>(new MediaPlaybackTargetPickerMac(client));
75 MediaPlaybackTargetPickerMac::MediaPlaybackTargetPickerMac(MediaPlaybackTargetPicker::Client& client)
76 : MediaPlaybackTargetPicker(client)
77 , m_outputDeviceMenuControllerDelegate(adoptNS([[WebAVOutputDeviceMenuControllerHelper alloc] initWithCallback:this]))
81 MediaPlaybackTargetPickerMac::~MediaPlaybackTargetPickerMac()
84 [m_outputDeviceMenuControllerDelegate clearCallback];
87 bool MediaPlaybackTargetPickerMac::externalOutputDeviceAvailable()
89 return devicePicker().externalOutputDeviceAvailable;
92 Ref<MediaPlaybackTarget> MediaPlaybackTargetPickerMac::playbackTarget()
94 AVOutputContext* context = m_outputDeviceMenuController ? [m_outputDeviceMenuController.get() outputContext] : nullptr;
96 return WebCore::MediaPlaybackTargetMac::create(context);
99 AVOutputDeviceMenuControllerType *MediaPlaybackTargetPickerMac::devicePicker()
101 if (!getAVOutputDeviceMenuControllerClass())
104 if (!m_outputDeviceMenuController) {
105 LOG(Media, "MediaPlaybackTargetPickerMac::devicePicker - allocating picker");
107 RetainPtr<AVOutputContextType> context = adoptNS([[getAVOutputContextClass() alloc] init]);
108 m_outputDeviceMenuController = adoptNS([[getAVOutputDeviceMenuControllerClass() alloc] initWithOutputContext:context.get()]);
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];
113 LOG(Media, "MediaPlaybackTargetPickerMac::devicePicker - allocated menu controller %p", m_outputDeviceMenuController.get());
115 if (m_outputDeviceMenuController.get().externalOutputDeviceAvailable)
116 availableDevicesDidChange();
119 return m_outputDeviceMenuController.get();
122 void MediaPlaybackTargetPickerMac::showPlaybackTargetPicker(const FloatRect& location, bool checkActiveRoute)
124 if (!client() || m_showingMenu)
127 LOG(Media, "MediaPlaybackTargetPickerMac::showPlaybackTargetPicker - checkActiveRoute = %i", (int)checkActiveRoute);
129 AVOutputDeviceMenuControllerType *picker = devicePicker();
130 if (![picker respondsToSelector:@selector(showMenuForRect:appearanceName:allowReselectionOfSelectedOutputDevice:)])
133 m_showingMenu = true;
134 if ([picker showMenuForRect:location appearanceName:NSAppearanceNameVibrantLight allowReselectionOfSelectedOutputDevice:!checkActiveRoute]) {
135 if (!checkActiveRoute)
136 currentDeviceDidChange();
138 m_showingMenu = false;
141 void MediaPlaybackTargetPickerMac::startingMonitoringPlaybackTargets()
143 LOG(Media, "MediaPlaybackTargetPickerMac::startingMonitoringPlaybackTargets");
148 void MediaPlaybackTargetPickerMac::stopMonitoringPlaybackTargets()
150 LOG(Media, "MediaPlaybackTargetPickerMac::stopMonitoringPlaybackTargets");
151 // Nothing to do, AirPlay takes care of this automatically.
154 void MediaPlaybackTargetPickerMac::invalidatePlaybackTargets()
156 LOG(Media, "MediaPlaybackTargetPickerMac::invalidatePlaybackTargets");
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;
163 currentDeviceDidChange();
166 } // namespace WebCore
168 @implementation WebAVOutputDeviceMenuControllerHelper
169 - (instancetype)initWithCallback:(MediaPlaybackTargetPickerMac*)callback
171 if (!(self = [super init]))
174 m_callback = callback;
179 - (void)clearCallback
184 - (void)observeValueForKeyPath:(id)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
186 UNUSED_PARAM(object);
187 UNUSED_PARAM(change);
188 UNUSED_PARAM(context);
193 LOG(Media, "MediaPlaybackTargetPickerMac::observeValueForKeyPath - key = %s", [keyPath UTF8String]);
195 if (![keyPath isEqualToString:externalOutputDeviceAvailableKeyName] && ![keyPath isEqualToString:externalOutputDevicePickedKeyName])
198 RetainPtr<WebAVOutputDeviceMenuControllerHelper> strongSelf = self;
199 RetainPtr<NSString> strongKeyPath = keyPath;
200 callOnMainThread([strongSelf, strongKeyPath] {
201 MediaPlaybackTargetPickerMac* callback = strongSelf->m_callback;
205 if ([strongKeyPath isEqualToString:externalOutputDeviceAvailableKeyName])
206 callback->availableDevicesDidChange();
207 else if ([strongKeyPath isEqualToString:externalOutputDevicePickedKeyName])
208 callback->currentDeviceDidChange();
213 #endif // ENABLE(WIRELESS_PLAYBACK_TARGET)