[Mac] Update AirPlay handling
[WebKit-https.git] / Source / WebCore / platform / audio / ios / MediaSessionManagerIOS.mm
1 /*
2  * Copyright (C) 2014-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 "MediaSessionManagerIOS.h"
28
29 #if PLATFORM(IOS)
30
31 #import "Logging.h"
32 #import "MediaPlayer.h"
33 #import "MediaPlayerSPI.h"
34 #import "MediaSession.h"
35 #import "SoftLinking.h"
36 #import "SystemMemory.h"
37 #import "WebCoreSystemInterface.h"
38 #import "WebCoreThreadRun.h"
39 #import <AVFoundation/AVAudioSession.h>
40 #import <MediaPlayer/MPMediaItem.h>
41 #import <MediaPlayer/MPNowPlayingInfoCenter.h>
42 #import <MediaPlayer/MPVolumeView.h>
43 #import <UIKit/UIApplication.h>
44 #import <objc/runtime.h>
45 #import <wtf/RetainPtr.h>
46
47 SOFT_LINK_FRAMEWORK(AVFoundation)
48 SOFT_LINK_CLASS(AVFoundation, AVAudioSession)
49 SOFT_LINK_POINTER(AVFoundation, AVAudioSessionInterruptionNotification, NSString *)
50 SOFT_LINK_POINTER(AVFoundation, AVAudioSessionInterruptionTypeKey, NSString *)
51 SOFT_LINK_POINTER(AVFoundation, AVAudioSessionInterruptionOptionKey, NSString *)
52
53 #define AVAudioSession getAVAudioSessionClass()
54 #define AVAudioSessionInterruptionNotification getAVAudioSessionInterruptionNotification()
55 #define AVAudioSessionInterruptionTypeKey getAVAudioSessionInterruptionTypeKey()
56 #define AVAudioSessionInterruptionOptionKey getAVAudioSessionInterruptionOptionKey()
57
58 SOFT_LINK_FRAMEWORK(UIKit)
59 SOFT_LINK_CLASS(UIKit, UIApplication)
60 SOFT_LINK_POINTER(UIKit, UIApplicationWillResignActiveNotification, NSString *)
61 SOFT_LINK_POINTER(UIKit, UIApplicationWillEnterForegroundNotification, NSString *)
62 SOFT_LINK_POINTER(UIKit, UIApplicationDidBecomeActiveNotification, NSString *)
63
64 #define UIApplication getUIApplicationClass()
65 #define UIApplicationWillResignActiveNotification getUIApplicationWillResignActiveNotification()
66 #define UIApplicationWillEnterForegroundNotification getUIApplicationWillEnterForegroundNotification()
67 #define UIApplicationDidBecomeActiveNotification getUIApplicationDidBecomeActiveNotification()
68
69 SOFT_LINK_FRAMEWORK(MediaPlayer)
70 SOFT_LINK_CLASS(MediaPlayer, MPAVRoutingController)
71 SOFT_LINK_CLASS(MediaPlayer, MPNowPlayingInfoCenter)
72 SOFT_LINK_CLASS(MediaPlayer, MPVolumeView)
73 SOFT_LINK_POINTER(MediaPlayer, MPMediaItemPropertyTitle, NSString *)
74 SOFT_LINK_POINTER(MediaPlayer, MPMediaItemPropertyPlaybackDuration, NSString *)
75 SOFT_LINK_POINTER(MediaPlayer, MPNowPlayingInfoPropertyElapsedPlaybackTime, NSString *)
76 SOFT_LINK_POINTER(MediaPlayer, MPNowPlayingInfoPropertyPlaybackRate, NSString *)
77 SOFT_LINK_POINTER(MediaPlayer, MPVolumeViewWirelessRoutesAvailableDidChangeNotification, NSString *)
78
79
80 #define MPMediaItemPropertyTitle getMPMediaItemPropertyTitle()
81 #define MPMediaItemPropertyPlaybackDuration getMPMediaItemPropertyPlaybackDuration()
82 #define MPNowPlayingInfoPropertyElapsedPlaybackTime getMPNowPlayingInfoPropertyElapsedPlaybackTime()
83 #define MPNowPlayingInfoPropertyPlaybackRate getMPNowPlayingInfoPropertyPlaybackRate()
84 #define MPVolumeViewWirelessRoutesAvailableDidChangeNotification getMPVolumeViewWirelessRoutesAvailableDidChangeNotification()
85
86 WEBCORE_EXPORT NSString* WebUIApplicationWillResignActiveNotification = @"WebUIApplicationWillResignActiveNotification";
87 WEBCORE_EXPORT NSString* WebUIApplicationWillEnterForegroundNotification = @"WebUIApplicationWillEnterForegroundNotification";
88 WEBCORE_EXPORT NSString* WebUIApplicationDidBecomeActiveNotification = @"WebUIApplicationDidBecomeActiveNotification";
89
90 using namespace WebCore;
91
92 @interface WebMediaSessionHelper : NSObject {
93     MediaSessionManageriOS* _callback;
94     RetainPtr<MPVolumeView> _volumeView;
95     RetainPtr<MPAVRoutingController> _airPlayPresenceRoutingController;
96 }
97
98 - (id)initWithCallback:(MediaSessionManageriOS*)callback;
99 - (void)allocateVolumeView;
100 - (void)clearCallback;
101 - (void)interruption:(NSNotification *)notification;
102 - (void)applicationWillEnterForeground:(NSNotification *)notification;
103 - (void)applicationWillResignActive:(NSNotification *)notification;
104 - (BOOL)hasWirelessTargetsAvailable;
105 - (void)startMonitoringAirPlayRoutes;
106 - (void)stopMonitoringAirPlayRoutes;
107 @end
108
109 namespace WebCore {
110
111 MediaSessionManager& MediaSessionManager::sharedManager()
112 {
113     DEPRECATED_DEFINE_STATIC_LOCAL(MediaSessionManageriOS, manager, ());
114     return manager;
115 }
116
117 MediaSessionManageriOS::MediaSessionManageriOS()
118     :MediaSessionManager()
119     , m_objcObserver(adoptNS([[WebMediaSessionHelper alloc] initWithCallback:this]))
120 {
121     resetRestrictions();
122 }
123
124 MediaSessionManageriOS::~MediaSessionManageriOS()
125 {
126     [m_objcObserver clearCallback];
127 }
128
129 void MediaSessionManageriOS::resetRestrictions()
130 {
131     static const size_t systemMemoryRequiredForVideoInBackgroundTabs = 1024 * 1024 * 1024;
132
133     LOG(Media, "MediaSessionManageriOS::resetRestrictions");
134
135     MediaSessionManager::resetRestrictions();
136
137     static wkDeviceClass deviceClass = iosDeviceClass();
138     if (deviceClass == wkDeviceClassiPhone || deviceClass == wkDeviceClassiPod)
139         addRestriction(MediaSession::Video, InlineVideoPlaybackRestricted);
140
141     if (systemTotalMemory() < systemMemoryRequiredForVideoInBackgroundTabs) {
142         LOG(Media, "MediaSessionManageriOS::resetRestrictions - restricting video in background tabs because system memory = %zul", systemTotalMemory());
143         addRestriction(MediaSession::Video, BackgroundTabPlaybackRestricted);
144     }
145
146     addRestriction(MediaSession::Video, ConcurrentPlaybackNotPermitted);
147     addRestriction(MediaSession::Video, BackgroundProcessPlaybackRestricted);
148
149     removeRestriction(MediaSession::Audio, ConcurrentPlaybackNotPermitted);
150     removeRestriction(MediaSession::Audio, BackgroundProcessPlaybackRestricted);
151
152     removeRestriction(MediaSession::WebAudio, ConcurrentPlaybackNotPermitted);
153     removeRestriction(MediaSession::WebAudio, BackgroundProcessPlaybackRestricted);
154
155     removeRestriction(MediaSession::Audio, MetadataPreloadingNotPermitted);
156     removeRestriction(MediaSession::Video, MetadataPreloadingNotPermitted);
157
158     addRestriction(MediaSession::Audio, AutoPreloadingNotPermitted);
159     addRestriction(MediaSession::Video, AutoPreloadingNotPermitted);
160 }
161
162 bool MediaSessionManageriOS::hasWirelessTargetsAvailable()
163 {
164     return [m_objcObserver hasWirelessTargetsAvailable];
165 }
166
167 void MediaSessionManageriOS::configureWireLessTargetMonitoring()
168 {
169     Vector<MediaSession*> sessions = this->sessions();
170     bool requiresMonitoring = false;
171
172     for (auto* session : sessions) {
173         if (session->requiresPlaybackTargetRouteMonitoring()) {
174             requiresMonitoring = true;
175             break;
176         }
177     }
178
179     LOG(Media, "MediaSessionManageriOS::configureWireLessTargetMonitoring - requiresMonitoring = %s", requiresMonitoring ? "true" : "false");
180
181     if (requiresMonitoring)
182         [m_objcObserver startMonitoringAirPlayRoutes];
183     else
184         [m_objcObserver stopMonitoringAirPlayRoutes];
185 }
186
187 void MediaSessionManageriOS::sessionWillBeginPlayback(MediaSession& session)
188 {
189     MediaSessionManager::sessionWillBeginPlayback(session);
190     updateNowPlayingInfo();
191 }
192     
193 void MediaSessionManageriOS::sessionWillEndPlayback(MediaSession& session)
194 {
195     MediaSessionManager::sessionWillEndPlayback(session);
196     updateNowPlayingInfo();
197 }
198     
199 void MediaSessionManageriOS::updateNowPlayingInfo()
200 {
201     LOG(Media, "MediaSessionManageriOS::updateNowPlayingInfo");
202
203     MPNowPlayingInfoCenter *nowPlaying = (MPNowPlayingInfoCenter *)[getMPNowPlayingInfoCenterClass() defaultCenter];
204     const MediaSession* currentSession = this->currentSession();
205     
206     if (!currentSession) {
207         [nowPlaying setNowPlayingInfo:nil];
208         return;
209     }
210     
211     RetainPtr<NSMutableDictionary> info = adoptNS([[NSMutableDictionary alloc] init]);
212     
213     String title = currentSession->title();
214     if (!title.isEmpty())
215         [info setValue:static_cast<NSString *>(title) forKey:MPMediaItemPropertyTitle];
216     
217     double duration = currentSession->duration();
218     if (std::isfinite(duration) && duration != MediaPlayer::invalidTime())
219         [info setValue:@(duration) forKey:MPMediaItemPropertyPlaybackDuration];
220     
221     double currentTime = currentSession->currentTime();
222     if (std::isfinite(currentTime) && currentTime != MediaPlayer::invalidTime())
223         [info setValue:@(currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
224     
225     [info setValue:(currentSession->state() == MediaSession::Playing ? @YES : @NO) forKey:MPNowPlayingInfoPropertyPlaybackRate];
226     [nowPlaying setNowPlayingInfo:info.get()];
227 }
228
229 bool MediaSessionManageriOS::sessionCanLoadMedia(const MediaSession& session) const
230 {
231     return session.state() == MediaSession::Playing || !session.isHidden() || session.displayType() == MediaSession::Optimized;
232 }
233
234 void MediaSessionManageriOS::externalOutputDeviceAvailableDidChange()
235 {
236     Vector<MediaSession*> sessionList = sessions();
237     bool haveTargets = [m_objcObserver hasWirelessTargetsAvailable];
238     for (auto* session : sessionList)
239         session->externalOutputDeviceAvailableDidChange(haveTargets);
240 }
241
242 } // namespace WebCore
243
244 @implementation WebMediaSessionHelper
245
246 - (void)allocateVolumeView
247 {
248     if (!isMainThread()) {
249         // Call synchronously to the main thread so that _volumeView will be completely setup before the constructor completes
250         // because hasWirelessTargetsAvailable is synchronous and can be called on the WebThread.
251         RetainPtr<WebMediaSessionHelper> strongSelf = self;
252         dispatch_sync(dispatch_get_main_queue(), [strongSelf]() {
253             [strongSelf allocateVolumeView];
254             return;
255         });
256     }
257
258     _volumeView = adoptNS([allocMPVolumeViewInstance() init]);
259     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wirelessRoutesAvailableDidChange:) name:MPVolumeViewWirelessRoutesAvailableDidChangeNotification object:_volumeView.get()];
260     
261 }
262
263 - (id)initWithCallback:(MediaSessionManageriOS*)callback
264 {
265     LOG(Media, "-[WebMediaSessionHelper initWithCallback]");
266
267     if (!(self = [super init]))
268         return nil;
269     
270     _callback = callback;
271
272     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
273     [center addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
274
275     [center addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
276     [center addObserver:self selector:@selector(applicationWillEnterForeground:) name:WebUIApplicationWillEnterForegroundNotification object:nil];
277     [center addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
278     [center addObserver:self selector:@selector(applicationDidBecomeActive:) name:WebUIApplicationDidBecomeActiveNotification object:nil];
279     [center addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
280     [center addObserver:self selector:@selector(applicationWillResignActive:) name:WebUIApplicationWillResignActiveNotification object:nil];
281
282     [self allocateVolumeView];
283
284     // Now playing won't work unless we turn on the delivery of remote control events.
285     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
286     
287     return self;
288 }
289
290 - (void)dealloc
291 {
292     LOG(Media, "-[WebMediaSessionHelper dealloc]");
293
294     if (!isMainThread()) {
295         auto volumeView = WTF::move(_volumeView);
296         auto routingController = WTF::move(_airPlayPresenceRoutingController);
297
298         callOnMainThread([volumeView, routingController] () mutable {
299             LOG(Media, "-[WebMediaSessionHelper dealloc] - dipatched to MainThread");
300
301             volumeView.clear();
302
303             if (!routingController)
304                 return;
305
306             [routingController setDiscoveryMode:MPRouteDiscoveryModeDisabled];
307             routingController.clear();
308         });
309     }
310
311     [[NSNotificationCenter defaultCenter] removeObserver:self];
312     [super dealloc];
313 }
314
315 - (void)clearCallback
316 {
317     LOG(Media, "-[WebMediaSessionHelper clearCallback]");
318     _callback = nil;
319 }
320
321 - (BOOL)hasWirelessTargetsAvailable
322 {
323     LOG(Media, "-[WebMediaSessionHelper hasWirelessTargetsAvailable]");
324     return [_volumeView areWirelessRoutesAvailable];
325 }
326
327 - (void)startMonitoringAirPlayRoutes
328 {
329     if (_airPlayPresenceRoutingController)
330         return;
331
332     LOG(Media, "-[WebMediaSessionHelper startMonitoringAirPlayRoutes]");
333
334     RetainPtr<WebMediaSessionHelper> strongSelf = self;
335     callOnMainThread([strongSelf] () {
336         LOG(Media, "-[WebMediaSessionHelper startMonitoringAirPlayRoutes] - dipatched to MainThread");
337
338         if (strongSelf->_airPlayPresenceRoutingController)
339             return;
340
341         strongSelf->_airPlayPresenceRoutingController = adoptNS([allocMPAVRoutingControllerInstance() initWithName:@"WebCore - HTML media element checking for AirPlay route presence"]);
342         [strongSelf->_airPlayPresenceRoutingController setDiscoveryMode:MPRouteDiscoveryModePresence];
343     });
344 }
345
346 - (void)stopMonitoringAirPlayRoutes
347 {
348     if (!_airPlayPresenceRoutingController)
349         return;
350
351     LOG(Media, "-[WebMediaSessionHelper stopMonitoringAirPlayRoutes]");
352
353     RetainPtr<WebMediaSessionHelper> strongSelf = self;
354     callOnMainThread([strongSelf] () {
355         LOG(Media, "-[WebMediaSessionHelper stopMonitoringAirPlayRoutes] - dipatched to MainThread");
356
357         if (!strongSelf->_airPlayPresenceRoutingController)
358             return;
359
360         [strongSelf->_airPlayPresenceRoutingController setDiscoveryMode:MPRouteDiscoveryModeDisabled];
361         strongSelf->_airPlayPresenceRoutingController = nil;
362     });
363 }
364
365 - (void)interruption:(NSNotification *)notification
366 {
367     if (!_callback)
368         return;
369
370     NSUInteger type = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
371     MediaSession::EndInterruptionFlags flags = MediaSession::NoFlags;
372
373     LOG(Media, "-[WebMediaSessionHelper interruption] - type = %i", (int)type);
374
375     if (type == AVAudioSessionInterruptionTypeEnded && [[[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey] unsignedIntegerValue] == AVAudioSessionInterruptionOptionShouldResume)
376         flags = MediaSession::MayResumePlaying;
377
378     WebThreadRun(^{
379         if (!_callback)
380             return;
381
382         if (type == AVAudioSessionInterruptionTypeBegan)
383             _callback->beginInterruption(MediaSession::SystemInterruption);
384         else
385             _callback->endInterruption(flags);
386
387     });
388 }
389
390 - (void)applicationWillEnterForeground:(NSNotification *)notification
391 {
392     UNUSED_PARAM(notification);
393
394     if (!_callback)
395         return;
396
397     LOG(Media, "-[WebMediaSessionHelper applicationWillEnterForeground]");
398
399     WebThreadRun(^{
400         if (!_callback)
401             return;
402
403         _callback->applicationWillEnterForeground();
404     });
405 }
406
407 - (void)applicationDidBecomeActive:(NSNotification *)notification
408 {
409     UNUSED_PARAM(notification);
410
411     if (!_callback)
412         return;
413
414     LOG(Media, "-[WebMediaSessionHelper applicationDidBecomeActive]");
415
416     WebThreadRun(^{
417         if (!_callback)
418             return;
419
420         _callback->applicationWillEnterForeground();
421     });
422 }
423
424 - (void)applicationWillResignActive:(NSNotification *)notification
425 {
426     UNUSED_PARAM(notification);
427
428     if (!_callback)
429         return;
430
431     LOG(Media, "-[WebMediaSessionHelper applicationWillResignActive]");
432
433     WebThreadRun(^{
434         if (!_callback)
435             return;
436         
437         _callback->applicationWillEnterBackground();
438     });
439 }
440
441 - (void)wirelessRoutesAvailableDidChange:(NSNotification *)notification
442 {
443     UNUSED_PARAM(notification);
444
445     if (!_callback)
446         return;
447
448     LOG(Media, "-[WebMediaSessionHelper wirelessRoutesAvailableDidChange]");
449
450     WebThreadRun(^{
451         if (!_callback)
452             return;
453
454         _callback->externalOutputDeviceAvailableDidChange();
455     });
456 }
457 @end
458
459 #endif // PLATFORM(IOS)