2 * Copyright (C) 2014 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 "MediaSessionManagerIOS.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>
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 *)
53 #define AVAudioSession getAVAudioSessionClass()
54 #define AVAudioSessionInterruptionNotification getAVAudioSessionInterruptionNotification()
55 #define AVAudioSessionInterruptionTypeKey getAVAudioSessionInterruptionTypeKey()
56 #define AVAudioSessionInterruptionOptionKey getAVAudioSessionInterruptionOptionKey()
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 *)
64 #define UIApplication getUIApplicationClass()
65 #define UIApplicationWillResignActiveNotification getUIApplicationWillResignActiveNotification()
66 #define UIApplicationWillEnterForegroundNotification getUIApplicationWillEnterForegroundNotification()
67 #define UIApplicationDidBecomeActiveNotification getUIApplicationDidBecomeActiveNotification()
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 *)
80 #define MPMediaItemPropertyTitle getMPMediaItemPropertyTitle()
81 #define MPMediaItemPropertyPlaybackDuration getMPMediaItemPropertyPlaybackDuration()
82 #define MPNowPlayingInfoPropertyElapsedPlaybackTime getMPNowPlayingInfoPropertyElapsedPlaybackTime()
83 #define MPNowPlayingInfoPropertyPlaybackRate getMPNowPlayingInfoPropertyPlaybackRate()
84 #define MPVolumeViewWirelessRoutesAvailableDidChangeNotification getMPVolumeViewWirelessRoutesAvailableDidChangeNotification()
86 NSString* WebUIApplicationWillResignActiveNotification = @"WebUIApplicationWillResignActiveNotification";
87 NSString* WebUIApplicationWillEnterForegroundNotification = @"WebUIApplicationWillEnterForegroundNotification";
88 NSString* WebUIApplicationDidBecomeActiveNotification = @"WebUIApplicationDidBecomeActiveNotification";
90 using namespace WebCore;
92 @interface WebMediaSessionHelper : NSObject {
93 MediaSessionManageriOS* _callback;
94 RetainPtr<MPVolumeView> _volumeView;
95 RetainPtr<MPAVRoutingController> _airPlayPresenceRoutingController;
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;
110 MediaSessionManager& MediaSessionManager::sharedManager()
112 DEPRECATED_DEFINE_STATIC_LOCAL(MediaSessionManageriOS, manager, ());
116 MediaSessionManageriOS::MediaSessionManageriOS()
117 :MediaSessionManager()
118 , m_objcObserver(adoptNS([[WebMediaSessionHelper alloc] initWithCallback:this]))
123 MediaSessionManageriOS::~MediaSessionManageriOS()
125 [m_objcObserver clearCallback];
128 void MediaSessionManageriOS::resetRestrictions()
130 MediaSessionManager::resetRestrictions();
132 static wkDeviceClass deviceClass = iosDeviceClass();
133 if (deviceClass == wkDeviceClassiPhone || deviceClass == wkDeviceClassiPod)
134 addRestriction(MediaSession::Video, InlineVideoPlaybackRestricted);
136 addRestriction(MediaSession::Video, ConcurrentPlaybackNotPermitted);
137 addRestriction(MediaSession::Video, BackgroundPlaybackNotPermitted);
139 removeRestriction(MediaSession::Audio, ConcurrentPlaybackNotPermitted);
140 removeRestriction(MediaSession::Audio, BackgroundPlaybackNotPermitted);
142 removeRestriction(MediaSession::WebAudio, ConcurrentPlaybackNotPermitted);
143 removeRestriction(MediaSession::WebAudio, BackgroundPlaybackNotPermitted);
145 removeRestriction(MediaSession::Audio, MetadataPreloadingNotPermitted);
146 removeRestriction(MediaSession::Video, MetadataPreloadingNotPermitted);
148 addRestriction(MediaSession::Audio, AutoPreloadingNotPermitted);
149 addRestriction(MediaSession::Video, AutoPreloadingNotPermitted);
152 #if ENABLE(IOS_AIRPLAY)
154 bool MediaSessionManageriOS::hasWirelessTargetsAvailable()
156 return [m_objcObserver hasWirelessTargetsAvailable];
159 void MediaSessionManageriOS::startMonitoringAirPlayRoutes()
161 [m_objcObserver startMonitoringAirPlayRoutes];
164 void MediaSessionManageriOS::stopMonitoringAirPlayRoutes()
166 [m_objcObserver stopMonitoringAirPlayRoutes];
170 void MediaSessionManageriOS::sessionWillBeginPlayback(MediaSession& session)
172 MediaSessionManager::sessionWillBeginPlayback(session);
173 updateNowPlayingInfo();
176 void MediaSessionManageriOS::sessionWillEndPlayback(MediaSession& session)
178 MediaSessionManager::sessionWillEndPlayback(session);
179 updateNowPlayingInfo();
182 void MediaSessionManageriOS::updateNowPlayingInfo()
184 MPNowPlayingInfoCenter *nowPlaying = (MPNowPlayingInfoCenter *)[getMPNowPlayingInfoCenterClass() defaultCenter];
185 const MediaSession* currentSession = this->currentSession();
187 if (!currentSession) {
188 [nowPlaying setNowPlayingInfo:nil];
192 RetainPtr<NSMutableDictionary> info = adoptNS([[NSMutableDictionary alloc] init]);
194 String title = currentSession->title();
195 if (!title.isEmpty())
196 [info setValue:static_cast<NSString *>(title) forKey:MPMediaItemPropertyTitle];
198 double duration = currentSession->duration();
199 if (std::isfinite(duration) && duration != MediaPlayer::invalidTime())
200 [info setValue:@(duration) forKey:MPMediaItemPropertyPlaybackDuration];
202 double currentTime = currentSession->currentTime();
203 if (std::isfinite(currentTime) && currentTime != MediaPlayer::invalidTime())
204 [info setValue:@(currentTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
206 [info setValue:(currentSession->state() == MediaSession::Playing ? @YES : @NO) forKey:MPNowPlayingInfoPropertyPlaybackRate];
207 [nowPlaying setNowPlayingInfo:info.get()];
210 } // namespace WebCore
212 @implementation WebMediaSessionHelper
214 - (id)initWithCallback:(MediaSessionManageriOS*)callback
216 if (!(self = [super init]))
219 _callback = callback;
220 _volumeView = adoptNS([[getMPVolumeViewClass() alloc] init]);
222 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
223 [center addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
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()];
233 // Now playing won't work unless we turn on the delivery of remote control events.
234 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
241 [[NSNotificationCenter defaultCenter] removeObserver:self];
245 - (void)clearCallback
250 - (BOOL)hasWirelessTargetsAvailable
252 return [_volumeView areWirelessRoutesAvailable];
255 - (void)startMonitoringAirPlayRoutes
257 if (_airPlayPresenceRoutingController)
260 _airPlayPresenceRoutingController = adoptNS([[getMPAVRoutingControllerClass() alloc] initWithName:@"WebCore - HTML media element checking for AirPlay route presence"]);
261 [_airPlayPresenceRoutingController setDiscoveryMode:MPRouteDiscoveryModePresence];
264 - (void)stopMonitoringAirPlayRoutes
266 _airPlayPresenceRoutingController = nil;
269 - (void)interruption:(NSNotification *)notification
274 NSUInteger type = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
275 MediaSession::EndInterruptionFlags flags = MediaSession::NoFlags;
277 if (type == AVAudioSessionInterruptionTypeEnded && [[[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey] unsignedIntegerValue] == AVAudioSessionInterruptionOptionShouldResume)
278 flags = MediaSession::MayResumePlaying;
284 if (type == AVAudioSessionInterruptionTypeBegan)
285 _callback->beginInterruption(MediaSession::SystemInterruption);
287 _callback->endInterruption(flags);
292 - (void)applicationWillEnterForeground:(NSNotification *)notification
294 UNUSED_PARAM(notification);
303 _callback->applicationWillEnterForeground();
307 - (void)applicationDidBecomeActive:(NSNotification *)notification
309 UNUSED_PARAM(notification);
318 _callback->applicationWillEnterForeground();
322 - (void)applicationWillResignActive:(NSNotification *)notification
324 UNUSED_PARAM(notification);
333 _callback->applicationWillEnterBackground();
337 - (void)wirelessRoutesAvailableDidChange:(NSNotification *)notification
339 UNUSED_PARAM(notification);
348 _callback->wirelessRoutesAvailableChanged();
353 #endif // PLATFORM(IOS)