[iOS] do not pause video when entering background while playing to external device
[WebKit-https.git] / Source / WebCore / platform / audio / ios / MediaSessionManagerIOS.mm
1 /*
2  * Copyright (C) 2014 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 "MediaSession.h"
34 #import "NotImplemented.h"
35 #import "SoftLinking.h"
36 #import "WebCoreSystemInterface.h"
37 #import "WebCoreThreadRun.h"
38 #import <AVFoundation/AVAudioSession.h>
39 #import <MediaPlayer/MPAVRoutingController.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 NSString* WebUIApplicationWillResignActiveNotification = @"WebUIApplicationWillResignActiveNotification";
87 NSString* WebUIApplicationWillEnterForegroundNotification = @"WebUIApplicationWillEnterForegroundNotification";
88 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)clearCallback;
100 - (void)interruption:(NSNotification *)notification;
101 - (void)applicationWillEnterForeground:(NSNotification *)notification;
102 - (void)applicationWillResignActive:(NSNotification *)notification;
103 - (BOOL)hasWirelessTargetsAvailable;
104 - (void)startMonitoringAirPlayRoutes;
105 - (void)stopMonitoringAirPlayRoutes;
106 @end
107
108 namespace WebCore {
109
110 MediaSessionManager& MediaSessionManager::sharedManager()
111 {
112     DEPRECATED_DEFINE_STATIC_LOCAL(MediaSessionManageriOS, manager, ());
113     return manager;
114 }
115
116 MediaSessionManageriOS::MediaSessionManageriOS()
117     :MediaSessionManager()
118     , m_objcObserver(adoptNS([[WebMediaSessionHelper alloc] initWithCallback:this]))
119 {
120     resetRestrictions();
121 }
122
123 MediaSessionManageriOS::~MediaSessionManageriOS()
124 {
125     [m_objcObserver clearCallback];
126 }
127
128 void MediaSessionManageriOS::resetRestrictions()
129 {
130     MediaSessionManager::resetRestrictions();
131
132     static wkDeviceClass deviceClass = iosDeviceClass();
133     if (deviceClass == wkDeviceClassiPhone || deviceClass == wkDeviceClassiPod)
134         addRestriction(MediaSession::Video, InlineVideoPlaybackRestricted);
135
136     addRestriction(MediaSession::Video, ConcurrentPlaybackNotPermitted);
137     addRestriction(MediaSession::Video, BackgroundPlaybackNotPermitted);
138
139     removeRestriction(MediaSession::Audio, ConcurrentPlaybackNotPermitted);
140     removeRestriction(MediaSession::Audio, BackgroundPlaybackNotPermitted);
141
142     removeRestriction(MediaSession::WebAudio, ConcurrentPlaybackNotPermitted);
143     removeRestriction(MediaSession::WebAudio, BackgroundPlaybackNotPermitted);
144
145     removeRestriction(MediaSession::Audio, MetadataPreloadingNotPermitted);
146     removeRestriction(MediaSession::Video, MetadataPreloadingNotPermitted);
147
148     addRestriction(MediaSession::Audio, AutoPreloadingNotPermitted);
149     addRestriction(MediaSession::Video, AutoPreloadingNotPermitted);
150 }
151
152 #if ENABLE(IOS_AIRPLAY)
153
154 bool MediaSessionManageriOS::hasWirelessTargetsAvailable()
155 {
156     return [m_objcObserver hasWirelessTargetsAvailable];
157 }
158
159 void MediaSessionManageriOS::startMonitoringAirPlayRoutes()
160 {
161     [m_objcObserver startMonitoringAirPlayRoutes];
162 }
163
164 void MediaSessionManageriOS::stopMonitoringAirPlayRoutes()
165 {
166     [m_objcObserver stopMonitoringAirPlayRoutes];
167 }
168 #endif
169     
170 void MediaSessionManageriOS::sessionWillBeginPlayback(MediaSession& session)
171 {
172     MediaSessionManager::sessionWillBeginPlayback(session);
173     updateNowPlayingInfo();
174 }
175     
176 void MediaSessionManageriOS::sessionWillEndPlayback(MediaSession& session)
177 {
178     MediaSessionManager::sessionWillEndPlayback(session);
179     updateNowPlayingInfo();
180 }
181     
182 void MediaSessionManageriOS::updateNowPlayingInfo()
183 {
184     MPNowPlayingInfoCenter *nowPlaying = (MPNowPlayingInfoCenter *)[getMPNowPlayingInfoCenterClass() defaultCenter];
185     const MediaSession* currentSession = this->currentSession();
186     
187     if (!currentSession) {
188         [nowPlaying setNowPlayingInfo:nil];
189         return;
190     }
191     
192     RetainPtr<NSMutableDictionary> info = adoptNS([[NSMutableDictionary alloc] init]);
193     
194     String title = currentSession->title();
195     if (!title.isEmpty())
196         [info setValue:static_cast<NSString *>(title) forKey:MPMediaItemPropertyTitle];
197     
198     double duration = currentSession->duration();
199     if (std::isfinite(duration) && duration != MediaPlayer::invalidTime())
200         [info setValue:@(duration) forKey:MPMediaItemPropertyPlaybackDuration];
201     
202     double currentTime = currentSession->currentTime();
203     if (std::isfinite(currentTime) && currentTime != MediaPlayer::invalidTime())
204         [info setValue:@(currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
205     
206     [info setValue:(currentSession->state() == MediaSession::Playing ? @YES : @NO) forKey:MPNowPlayingInfoPropertyPlaybackRate];
207     [nowPlaying setNowPlayingInfo:info.get()];
208 }
209
210 } // namespace WebCore
211
212 @implementation WebMediaSessionHelper
213
214 - (id)initWithCallback:(MediaSessionManageriOS*)callback
215 {
216     if (!(self = [super init]))
217         return nil;
218     
219     _callback = callback;
220     _volumeView = adoptNS([[getMPVolumeViewClass() alloc] init]);
221
222     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
223     [center addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
224
225     [center addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
226     [center addObserver:self selector:@selector(applicationWillEnterForeground:) name:WebUIApplicationWillEnterForegroundNotification object:nil];
227     [center addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
228     [center addObserver:self selector:@selector(applicationDidBecomeActive:) name:WebUIApplicationDidBecomeActiveNotification object:nil];
229     [center addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
230     [center addObserver:self selector:@selector(applicationWillResignActive:) name:WebUIApplicationWillResignActiveNotification object:nil];
231     [center addObserver:self selector:@selector(wirelessRoutesAvailableDidChange:) name:MPVolumeViewWirelessRoutesAvailableDidChangeNotification object:_volumeView.get()];
232
233     // Now playing won't work unless we turn on the delivery of remote control events.
234     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
235     
236     return self;
237 }
238
239 - (void)dealloc
240 {
241     [[NSNotificationCenter defaultCenter] removeObserver:self];
242     [super dealloc];
243 }
244
245 - (void)clearCallback
246 {
247     _callback = nil;
248 }
249
250 - (BOOL)hasWirelessTargetsAvailable
251 {
252     return [_volumeView areWirelessRoutesAvailable];
253 }
254
255 - (void)startMonitoringAirPlayRoutes
256 {
257     if (_airPlayPresenceRoutingController)
258         return;
259
260     _airPlayPresenceRoutingController = adoptNS([[getMPAVRoutingControllerClass() alloc] initWithName:@"WebCore - HTML media element checking for AirPlay route presence"]);
261     [_airPlayPresenceRoutingController setDiscoveryMode:MPRouteDiscoveryModePresence];
262 }
263
264 - (void)stopMonitoringAirPlayRoutes
265 {
266     _airPlayPresenceRoutingController = nil;
267 }
268
269 - (void)interruption:(NSNotification *)notification
270 {
271     if (!_callback)
272         return;
273
274     NSUInteger type = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
275     MediaSession::EndInterruptionFlags flags = MediaSession::NoFlags;
276
277     if (type == AVAudioSessionInterruptionTypeEnded && [[[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey] unsignedIntegerValue] == AVAudioSessionInterruptionOptionShouldResume)
278         flags = MediaSession::MayResumePlaying;
279
280     WebThreadRun(^{
281         if (!_callback)
282             return;
283
284         if (type == AVAudioSessionInterruptionTypeBegan)
285             _callback->beginInterruption(MediaSession::SystemInterruption);
286         else
287             _callback->endInterruption(flags);
288
289     });
290 }
291
292 - (void)applicationWillEnterForeground:(NSNotification *)notification
293 {
294     UNUSED_PARAM(notification);
295
296     if (!_callback)
297         return;
298
299     WebThreadRun(^{
300         if (!_callback)
301             return;
302
303         _callback->applicationWillEnterForeground();
304     });
305 }
306
307 - (void)applicationDidBecomeActive:(NSNotification *)notification
308 {
309     UNUSED_PARAM(notification);
310
311     if (!_callback)
312         return;
313
314     WebThreadRun(^{
315         if (!_callback)
316             return;
317
318         _callback->applicationWillEnterForeground();
319     });
320 }
321
322 - (void)applicationWillResignActive:(NSNotification *)notification
323 {
324     UNUSED_PARAM(notification);
325
326     if (!_callback)
327         return;
328     
329     WebThreadRun(^{
330         if (!_callback)
331             return;
332         
333         _callback->applicationWillEnterBackground();
334     });
335 }
336
337 - (void)wirelessRoutesAvailableDidChange:(NSNotification *)notification
338 {
339     UNUSED_PARAM(notification);
340
341     if (!_callback)
342         return;
343
344     WebThreadRun(^{
345         if (!_callback)
346             return;
347
348         _callback->wirelessRoutesAvailableChanged();
349     });
350 }
351 @end
352
353 #endif // PLATFORM(IOS)