1

环境

  • Xcode @ OS-X 优胜美地
  • iOS App @ Obj-C

用例

  • 在 iOS 和 OSX 之间设置了 Quicktime 镜像会话(如何在 iOS 8 和 Yosemite 之间设置镜像会话?
  • 第三方 SDK 与 iOS 应用程序集成
  • SDK用于视频播放
  • 当视频正在播放并设置了镜像会话(作为外部显示器)时,没有视频正在播放(只有音频)
  • SDK没有任何API来控制外部视频播放/镜像在镜像时的开/关

我需要能够将视频镜像到我的 OSX 桌面,为此,我尝试通过以下方式从 3rd 方 SDK 隐藏镜像屏幕(但这没有帮助):

namespace NSNotificationCenterS
{
    namespace Original
    {
        IMP addObserver = 0;
    }

    void addObserver(id self, SEL _cmd, id notificationObserver, SEL notificationSelector, NSString* notificationName, id notificationSender){
        if( (YES == [notificationName isEqualToString:UIScreenDidConnectNotification]) || 
            (YES == [notificationName isEqualToString:UIScreenDidDisconnectNotification]) || 
            (YES == [notificationName isEqualToString:UIScreenModeDidChangeNotification ]) ) 
        {
            NSLog(@"NSNotificationCenter addObserver(%@, %@, '%@', %@) SUPRESSED!!!", notificationObserver, NSStringFromSelector(notificationSelector), notificationName, notificationSender);
            return;// Supress notifications of this kind of events 
        }
        // NSLog(@"NSNotificationCenter addObserver(%@, %@, '%@', %@)", notificationObserver, NSStringFromSelector(notificationSelector), notificationName, notificationSender);
        ((void(*)(id, SEL, id, SEL, NSString*,id))Original::addObserver)(self, _cmd, notificationObserver, notificationSelector, notificationName, notificationSender);
    }

    void initHooks() {
        Method method;
        method = class_getInstanceMethod([NSNotificationCenter class], @selector(addObserver:selector:name:object:));
        Original::addObserver = method_getImplementation(method);
        method_setImplementation(method, (IMP)NSNotificationCenterS::addObserver);
    }
}

namespace AVPlayerS
{
    namespace Original
    {
        IMP init = 0;
        IMP setAllowsExternalPlayback = 0;
        IMP setUsesExternalPlaybackWhileExternalScreenIsActive = 0;
        IMP setAllowsAirPlayVideo = 0;
        IMP setUsesAirPlayVideoWhileAirPlayScreenIsActive = 0;
    }

    id init(id self, SEL _cmd) {
        NSLog(@"AVPlayer init, %@\n", self);
        id ret = ((id(*)(id,SEL))Original::init)(self, _cmd);
        if(nil == ret)
            return nil;
        ((void(*)(id, SEL, BOOL))Original::setAllowsExternalPlayback)(ret, @selector(setAllowsExternalPlayback:), YES);
        ((void(*)(id, SEL, BOOL))Original::setUsesExternalPlaybackWhileExternalScreenIsActive)(ret, @selector(setUsesExternalPlaybackWhileExternalScreenIsActive:), YES);
        ((void(*)(id, SEL, BOOL))Original::setAllowsAirPlayVideo)(ret, @selector(setAllowsAirPlayVideo:), YES);
        ((void(*)(id, SEL, BOOL))Original::setUsesAirPlayVideoWhileAirPlayScreenIsActive)(ret, @selector(setUsesAirPlayVideoWhileAirPlayScreenIsActive:), YES);

        NSLog(@"AVPlayer, %d, %d, %d, %d\n", [ret allowsExternalPlayback], [ret usesExternalPlaybackWhileExternalScreenIsActive], [ret allowsAirPlayVideo], [ret usesAirPlayVideoWhileAirPlayScreenIsActive]);
        return ret;
    }

    UIScreen* mirroredScreen(id self, SEL _cmd) {
        return nil;
    }

    NSArray* getScreens(id self, SEL _cmd) {
        return [NSArray arrayWithObject:[UIScreen mainScreen]];
    }

    void flag_stub(id self, SEL _cmd, BOOL bSet){
        NSLog(@"AVPlayer flag_stub(%@, %@, '%s')", self, NSStringFromSelector(_cmd), bSet ? "true" : "false");
    }

    BOOL ret_YES(id self, SEL _cmd) {
        NSLog(@"AVPlayer ret_YES(%@, %@)", self, NSStringFromSelector(_cmd));
        return YES;
    }

    BOOL ret_NO(id self, SEL _cmd) {
        NSLog(@"AVPlayer ret_NO(%@, %@)", self, NSStringFromSelector(_cmd));
        return NO;
    }

    void initHooks() {
        Method method;

        method   = class_getInstanceMethod([UIScreen class], @selector(mirroredScreen));
        method_setImplementation(method, (IMP)AVPlayerS::mirroredScreen);

        method   = class_getClassMethod([UIScreen class], @selector(screens));
        method_setImplementation(method, (IMP)AVPlayerS::getScreens);

        method = class_getInstanceMethod([AVPlayer class], @selector(init));
        AVPlayerS::Original::init  = method_getImplementation(method);
        method_setImplementation(method, (IMP)AVPlayerS::init);

        method = class_getInstanceMethod([AVPlayer class], @selector(setAllowsExternalPlayback:));
        AVPlayerS::Original::setAllowsExternalPlayback  = method_getImplementation(method);
        method_setImplementation(method, (IMP)flag_stub);

        method = class_getInstanceMethod([AVPlayer class], @selector(setUsesExternalPlaybackWhileExternalScreenIsActive:));
        AVPlayerS::Original::setUsesExternalPlaybackWhileExternalScreenIsActive  = method_getImplementation(method);
        method_setImplementation(method, (IMP)flag_stub);

        method = class_getInstanceMethod([AVPlayer class], @selector(setAllowsAirPlayVideo:));
        AVPlayerS::Original::setAllowsAirPlayVideo  = method_getImplementation(method);
        method_setImplementation(method, (IMP)flag_stub);

        method = class_getInstanceMethod([AVPlayer class], @selector(setUsesAirPlayVideoWhileAirPlayScreenIsActive:));
        AVPlayerS::Original::setUsesAirPlayVideoWhileAirPlayScreenIsActive  = method_getImplementation(method);
        method_setImplementation(method, (IMP)flag_stub);

        method = class_getInstanceMethod([AVPlayer class], @selector(isExternalPlaybackActive));
        method_setImplementation(method, (IMP)ret_NO);

        method = class_getInstanceMethod([AVPlayer class], @selector(isAirPlayVideoActive));
        method_setImplementation(method, (IMP)ret_NO);

        method = class_getInstanceMethod([AVPlayer class], @selector(allowsAirPlayVideo));
        //method_setImplementation(method, (IMP)ret_YES);
        method_setImplementation(method, (IMP)ret_NO);
    }
}

如何绕过视频播放镜像保护?我应该拦截任何较低级别的 API 吗?

4

0 回答 0