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