iOS开发iOS-开发

iOS - 使用 ReplayKit Live 实现第三方直播

2017-01-10  本文已影响1353人  Mitchell

一、基本流程


二、发起端具体实现

//在需要被录制的 App 中添加个入口,例如按钮,点击的时候调用如下方法
#pragma mark action 录制点击
-(IBAction)replayLiveClick:(id)sender {
    //发起请求
    [RPBroadcastActivityViewController loadBroadcastActivityViewControllerWithHandler:^(RPBroadcastActivityViewController * _Nullable broadcastActivityViewController, NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"%@",error);
            return;
        }
        broadcastActivityViewController.delegate = self;
        [self presentViewController:broadcastActivityViewController animated:true completion:^{
        }];
    }];
}
#pragma mark action 暂停直播
-(IBAction)pauseClick:(id)sender {
    [self.broadcastViewController pauseBroadcast];
    NSLog(@"pause");
}
#pragma mark action 继续直播
-(IBAction)resumeClick:(id)sender {
    [self.broadcastViewController resumeBroadcast];
    NSLog(@"resume");
}
#pragma mark action 完成直播
-(IBAction)finishClick:(id)sender {
    [self.broadcastViewController finishBroadcastWithHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@",error);
        }
        NSLog(@"finish");
    }];
}
//需要遵守代理<RPBroadcastActivityViewControllerDelegate>
#pragma mark ------------------ broad代理方法 ------------------
-(void)broadcastActivityViewController:(RPBroadcastActivityViewController *)broadcastActivityViewController didFinishWithBroadcastController:(RPBroadcastController *)broadcastController error:(NSError *)error{
    NSLog(@"%s",__func__);
    if (error) {
        NSLog(@"%@",error);
    }
    [self dismissViewControllerAnimated:true completion:nil];
//使用相机
    [RPScreenRecorder sharedRecorder].cameraEnabled = true;
//使用麦克风
    [RPScreenRecorder sharedRecorder].microphoneEnabled = true;
    self.broadcastViewController = broadcastController;
//开始录制
    [broadcastController startBroadcastWithHandler:^(NSError * _Nullable error) {
        NSLog(@"开始录");
        if (error) {
            NSLog(@"%@",error);
        }
        [[RPScreenRecorder sharedRecorder].cameraPreviewView setFrame:CGRectMake(0, KeyHeight - 100, 100, 100)];
        [self.view addSubview:[RPScreenRecorder sharedRecorder].cameraPreviewView];
    }];
}

当调用 replayLiveClick 方法之后会出现支持 Broadcast 功能的 App 列表,以我的 snake Demo 为例会出现这样的界面:

Snake.PNG
大家可以尝试使用 Mobcrush 来尝试,我使用 Mobcrush 已经测试成功。映客的话总报错。
那么选择 Mobcrush 来尝试一下,会出现这样的界面,这个界面就是接下来支持客户端中 Broadcast UI 的界面。可以通过这个界面去传递输入一些参数,传递给之后的 BroadcastUpload。
-(void)broadcastActivityViewController:(RPBroadcastActivityViewController *)broadcastActivityViewController didFinishWithBroadcastController:(RPBroadcastController *)broadcastController error:(NSError *)error

代理方法,在这个方法内开始实现录屏。


IMG_2243.PNG

三、被调用端实现

//UI 中对应的方法
-(IBAction)cancelClick:(id)sender {
    NSLog(@"%s",__func__);
    [self userDidCancelSetup];
}
-(IBAction)okClick:(id)sender {
    NSLog(@"%s",__func__);
    [self userDidFinishSetup];
}
//系统自带
// Called when the user has finished interacting with the view controller and a broadcast stream can start
-(void)userDidFinishSetup {
    // Broadcast url that will be returned to the application
    NSURL *broadcastURL = [NSURL URLWithString:@"http://broadcastURL_example/stream1"];
    // Service specific broadcast data example which will be supplied to the process extension during broadcast
    NSString *userID = @"user1";
    NSString *endpointURL = @"http://broadcastURL_example/stream1/upload";
    NSDictionary *setupInfo = @{ @"userID" : userID, @"endpointURL" : endpointURL };
    // Set broadcast settings
    RPBroadcastConfiguration *broadcastConfig = [[RPBroadcastConfiguration alloc] init];
    broadcastConfig.clipDuration = 5.0; // deliver movie clips every 5 seconds
    // Tell ReplayKit that the extension is finished setting up and can begin broadcasting
//传递 setupInfo
    [self.extensionContext completeRequestWithBroadcastURL:broadcastURL broadcastConfiguration:broadcastConfig setupInfo:setupInfo];
}
-(void)userDidCancelSetup {
    // Tell ReplayKit that the extension was cancelled by the user
    [self.extensionContext cancelRequestWithError:[NSError errorWithDomain:@"YourAppDomain" code:-1     userInfo:nil]];
}
#import "SampleHandler.h"
#import "ReplayKitUploader.h"
//  To handle samples with a subclass of RPBroadcastSampleHandler set the following in the extension's Info.plist file:
//  - RPBroadcastProcessMode should be set to RPBroadcastProcessModeSampleBuffer
//  - NSExtensionPrincipalClass should be set to this class
@implementation SampleHandler
-(void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
    // User has requested to start the broadcast. Setup info from the UI extension will be supplied.
    NSLog(@"进行视频流的一些初始化工作");
    [[ReplayKitUploader sharedObject] setInfo:setupInfo];
}
-(void)broadcastPaused {
    // User has requested to pause the broadcast. Samples will stop being delivered.
//暂停
    NSLog(@"%s",__func__);
}
-(void)broadcastResumed {
    // User has requested to resume the broadcast. Samples delivery will resume.
//继续
    NSLog(@"%s",__func__);
}
-(void)broadcastFinished {
    // User has requested to finish the broadcast.
//结束
    NSLog(@"%s",__func__);
}
-(void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
    //这里来发送音频视频数据
    NSLog(@"发送音频视频数据");
    switch (sampleBufferType) {
        case RPSampleBufferTypeVideo:
            // Handle audio sample buffer
            break;
        case RPSampleBufferTypeAudioApp:
            // Handle audio sample buffer for app audio
            break;
        case RPSampleBufferTypeAudioMic:
            // Handle audio sample buffer for mic audio
            break;
        default:
            break;
    }
[[ReplayKitUploader sharedObject]handleBuffer:sampleBuffer];  
}
@end

四、运行查看

2017-01-10 23:14:04.776167 ReplayKitUpload[4224:210651] 进行视频流的一些初始化工作
2017-01-10 23:14:04.777215 ReplayKitUpload[4224:210651] info = {
    endpointURL = "http://broadcastURL_example/stream1/upload";
    userID = user1;
}
2017-01-10 23:14:05.271903 ReplayKitUpload[4224:210652] 发送音频视频数据
2017-01-10 23:14:05.272001 ReplayKitUpload[4224:210652] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:14:05.276273 ReplayKitUpload[4224:210648] 发送音频视频数据
2017-01-10 23:14:05.276462 ReplayKitUpload[4224:210648] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:14:05.297799 ReplayKitUpload[4224:210651] 发送音频视频数据
2017-01-10 23:14:05.297895 ReplayKitUpload[4224:210651] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:14:05.321041 ReplayKitUpload[4224:210648] 发送音频视频数据
2017-01-10 23:14:05.321147 ReplayKitUpload[4224:210648] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:14:05.346496 ReplayKitUpload[4224:210650] 发送音频视频数据
2017-01-10 23:14:05.346621 ReplayKitUpload[4224:210650] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:14:05.349972 ReplayKitUpload[4224:210651] 发送音频视频数据
2017-01-10 23:14:05.350073 ReplayKitUpload[4224:210651] -[ReplayKitUploader handleBuffer:]
//点击 pause
2017-01-10 23:15:00.100968 ReplayKitUpload[4224:210651] -[SampleHandler broadcastPaused]
//点击 resume
2017-01-10 23:15:44.427305 ReplayKitUpload[4224:210650] -[SampleHandler broadcastResumed]
2017-01-10 23:15:44.973824 ReplayKitUpload[4224:210650] 发送音频视频数据
2017-01-10 23:15:44.973921 ReplayKitUpload[4224:210650] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:15:44.988761 ReplayKitUpload[4224:212103] 发送音频视频数据
2017-01-10 23:15:44.988865 ReplayKitUpload[4224:212103] -[ReplayKitUploader handleBuffer:]
//点击 finished
handleBuffer:]
2017-01-10 23:16:07.694806 ReplayKitUpload[4224:210650] -[SampleHandler broadcastFinished]
2017-01-10 23:16:07.695986 ReplayKitUpload[4224:212166] 发送音频视频数据
2017-01-10 23:16:07.697114 ReplayKitUpload[4224:212166] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:16:07.697291 ReplayKitUpload[4224:212166] 发送音频视频数据
2017-01-10 23:16:07.697458 ReplayKitUpload[4224:212166] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:16:07.697563 ReplayKitUpload[4224:212166] 发送音频视频数据
2017-01-10 23:16:07.697626 ReplayKitUpload[4224:212166] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:16:07.698168 ReplayKitUpload[4224:212166] 发送音频视频数据
2017-01-10 23:16:07.698234 ReplayKitUpload[4224:212166] -[ReplayKitUploader handleBuffer:]
2017-01-10 23:16:07.698310 ReplayKitUpload[4224:212166] 发送音频视频数据
2017-01-10 23:16:07.698353 ReplayKitUpload[4224:212166] -[ReplayKitUploader handleBuffer:]
Program ended with exit code: 0
上一篇下一篇

猜你喜欢

热点阅读