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