#if USE(APPLE_INTERNAL_SDK)
#import <MediaPlayer/MPAVRoutingController.h>
-#import <MediaPlayer/MPMediaControlsViewController.h>
+#import <MediaPlayer/MPAVRoutingSheet.h>
+#import <MediaPlayer/MPAudioVideoRoutingPopoverController.h>
#else
-NS_ASSUME_NONNULL_BEGIN
+enum {
+ MPAVItemTypeUnknown = 0,
+ MPAVItemTypeAudio = 1,
+ MPAVItemTypeVideo = 2,
+};
+typedef NSUInteger MPAVItemType;
enum {
MPRouteDiscoveryModeDisabled = 0,
@property (nonatomic, assign) MPRouteDiscoveryMode discoveryMode;
@end
-@interface MPMediaControlsViewController : UIViewController
-@property (nonatomic, copy, nullable) void (^didDismissHandler)(void);
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+@interface MPAudioVideoRoutingPopoverController : UIPopoverController
+@end
+#pragma clang diagnostic pop
+
+@interface MPAudioVideoRoutingPopoverController ()
+- (id)initWithType:(MPAVItemType)avItemType;
+@end
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+@interface MPAVRoutingSheet : UIView
@end
+#pragma clang diagnostic pop
-NS_ASSUME_NONNULL_END
+@interface MPAVRoutingSheet ()
+@property (nonatomic, assign, setter=setAVItemType:) MPAVItemType avItemType;
+
+@property (nonatomic, assign) BOOL mirroringOnly;
+
+- (id)initWithAVItemType:(MPAVItemType)avItemType;
+
+- (void)showInView:(UIView *)view withCompletionHandler:(void (^)(void))completionHandler;
+
+- (void)dismiss;
+@end
#endif
#import "UIKitSPI.h"
#import "WKContentView.h"
+#import "WKContentViewInteraction.h"
+#import "WebPageProxy.h"
#import <WebCore/MediaPlayerSPI.h>
#import <wtf/RetainPtr.h>
#import <wtf/SoftLinking.h>
-#if !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+
SOFT_LINK_FRAMEWORK(MediaPlayer)
SOFT_LINK_CLASS(MediaPlayer, MPAVRoutingController)
-SOFT_LINK_CLASS(MediaPlayer, MPMediaControlsViewController)
-#endif
+SOFT_LINK_CLASS(MediaPlayer, MPAudioVideoRoutingPopoverController)
+SOFT_LINK_CLASS(MediaPlayer, MPAVRoutingSheet)
using namespace WebKit;
@implementation WKAirPlayRoutePicker {
-#if !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
- RetainPtr<MPMediaControlsViewController> _actionSheet;
-#endif
+ RetainPtr<MPAVRoutingController> _routingController;
+ RetainPtr<MPAudioVideoRoutingPopoverController> _popoverController; // iPad
+ RetainPtr<MPAVRoutingSheet> _actionSheet; // iPhone
+ WKContentView* _view; // Weak reference.
+}
+
+- (instancetype)initWithView:(WKContentView *)view
+{
+ if (!(self = [super init]))
+ return nil;
+
+ _view = view;
+ return self;
}
- (void)dealloc
{
-#if !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
- [_actionSheet dismissViewControllerAnimated:0 completion:nil];
-#endif
+ // The ActionSheet's completion handler will release and clear the ActionSheet.
+ [_actionSheet dismiss];
+ [self _dismissAirPlayRoutePickerIPad];
+
[super dealloc];
}
-- (void)showFromView:(WKContentView *)view
+- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
+{
+ if (popoverController != _popoverController)
+ return;
+
+ [self _dismissAirPlayRoutePickerIPad];
+}
+
+- (void)_presentAirPlayPopoverAnimated:(BOOL)animated fromRect:(CGRect)elementRect
+{
+ [_popoverController presentPopoverFromRect:elementRect inView:_view permittedArrowDirections:UIPopoverArrowDirectionAny animated:animated];
+}
+
+- (void)_windowWillRotate:(NSNotification *)notification
+{
+ [_popoverController dismissPopoverAnimated:NO];
+}
+
+- (void)_windowDidRotate:(NSNotification *)notification
+{
+ [self _dismissAirPlayRoutePickerIPad];
+}
+
+- (void)_dismissAirPlayRoutePickerIPad
+{
+ if (!_routingController)
+ return;
+
+ [_routingController setDiscoveryMode:MPRouteDiscoveryModeDisabled];
+ _routingController = nil;
+
+ if (!_popoverController)
+ return;
+
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ [center removeObserver:self name:UIWindowWillRotateNotification object:nil];
+ [center removeObserver:self name:UIWindowDidRotateNotification object:nil];
+
+ [_popoverController dismissPopoverAnimated:NO];
+ [_popoverController setDelegate:nil];
+ _popoverController = nil;
+}
+
+- (void)showAirPlayPickerIPad:(MPAVItemType)itemType fromRect:(CGRect)elementRect
+{
+ if (_popoverController)
+ return;
+
+ _popoverController = adoptNS([allocMPAudioVideoRoutingPopoverControllerInstance() initWithType:itemType]);
+ [_popoverController setDelegate:self];
+
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ [center addObserver:self selector:@selector(_windowWillRotate:) name:UIWindowWillRotateNotification object:nil];
+ [center addObserver:self selector:@selector(_windowDidRotate:) name:UIWindowDidRotateNotification object:nil];
+
+ [self _presentAirPlayPopoverAnimated:YES fromRect:elementRect];
+}
+
+- (void)showAirPlayPickerIPhone:(MPAVItemType)itemType
{
-#if !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
if (_actionSheet)
return;
- __block RetainPtr<MPAVRoutingController> routingController = adoptNS([allocMPAVRoutingControllerInstance() initWithName:@"WebKit - HTML media element showing AirPlay route picker"]);
- [routingController setDiscoveryMode:MPRouteDiscoveryModeDetailed];
+ _actionSheet = adoptNS([allocMPAVRoutingSheetInstance() initWithAVItemType:itemType]);
- _actionSheet = adoptNS([allocMPMediaControlsViewControllerInstance() init]);
- _actionSheet.get().didDismissHandler = ^ {
- [routingController setDiscoveryMode:MPRouteDiscoveryModeDisabled];
- routingController = nil;
+ [_actionSheet showInView:_view withCompletionHandler:^{
+ [_routingController setDiscoveryMode:MPRouteDiscoveryModeDisabled];
+ _routingController = nil;
_actionSheet = nil;
- };
+ }];
+}
- UIViewController *viewControllerForPresentation = [UIViewController _viewControllerForFullScreenPresentationFromView:view];
- [viewControllerForPresentation presentViewController:_actionSheet.get() animated:YES completion:nil];
-#else
- UNUSED_PARAM(view);
-#endif
+- (void)show:(BOOL)hasVideo fromRect:(CGRect)elementRect
+{
+ _routingController = adoptNS([allocMPAVRoutingControllerInstance() initWithName:@"WebKit2 - HTML media element showing AirPlay route picker"]);
+ [_routingController setDiscoveryMode:MPRouteDiscoveryModeDetailed];
+
+ MPAVItemType itemType = hasVideo ? MPAVItemTypeVideo : MPAVItemTypeAudio;
+ if (UICurrentUserInterfaceIdiomIsPad())
+ [self showAirPlayPickerIPad:itemType fromRect:elementRect];
+ else
+ [self showAirPlayPickerIPhone:itemType];
}
@end
+#pragma clang diagnostic pop
+
#endif // PLATFORM(IOS)