iOS - 使用 ReplayKit Live 实现第三方直播
2017-01-10 本文已影响1353人
Mitchell
一、基本流程
- 被直播端接入
#import <ReplayKit/ReplayKit.h>
框架。添加用于发起广播请求的入口。 -
接入的第三方 app 需要接入Broadcast UI 和 Broadcast Upload 两个拓展。这样当直播端发起广播请求的时候,会在列表中找到支持的 App。
ReplayKit Live 流程图.png
二、发起端具体实现
- 首先看一下调用 App 端需要实现的代码:
//在需要被录制的 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 为例会出现这样的界面:
大家可以尝试使用 Mobcrush 来尝试,我使用 Mobcrush 已经测试成功。映客的话总报错。
那么选择 Mobcrush 来尝试一下,会出现这样的界面,这个界面就是接下来支持客户端中 Broadcast UI 的界面。可以通过这个界面去传递输入一些参数,传递给之后的 BroadcastUpload。
-
这里因为我的 Mobcrush 已经登录了,所以直接显示的是让我创建一个直播的界面,如果没有登录界面可能会是让你登录的样子,具体自己可以尝试一下。
IMG_2242.PNG - 接着随便输入一个 title 然后点击 START BROADCAST,当点击完成之后,会调用
-(void)broadcastActivityViewController:(RPBroadcastActivityViewController *)broadcastActivityViewController didFinishWithBroadcastController:(RPBroadcastController *)broadcastController error:(NSError *)error
代理方法,在这个方法内开始实现录屏。
IMG_2243.PNG
三、被调用端实现
-
首先添加 BroadcastUI 和 BroadcastUpload 拓展
BroadcastUI 和 BroadcastUpload.png -
自定制 BroadcastUI 界面,对应着上面 Mobcrush 中点击的时候调起的界面。
自定制.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]];
}
- 最后来看 BroadcastUpload 中,默认是会创建 2 个类,MovieClipHandler 和 SampleHandler,也对应着两种直播方式:
- 将视频和音频编码为一小段mp4文件,交给直播扩展
-
将屏幕和声音的原始数据交给扩展
方式1延迟高,不灵活,优点是扩展app无须关心编码问题;方式2可以自定义发送的内容,可配置性高。目前SDK仅支持第二种方式。由于Xcode默认使用了方式1,因此需要修改直播扩展Info.plist到如图所示
屏幕快照 2017-01-10 下午11.10.13.png
#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
四、运行查看
-
查看Broadcast UI
IMG_2246.PNG -
查看 Broadcast Upload 运行中的 log
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