如何基于ReplayKit实现低延迟rtmp推屏
目录
在ReplayKit live未出来之前,iOS推流屏幕信息,必须使用实际或虚拟的AirPlay模式,使用起来非常不便,自iOS 10开始,苹果在 iOS 9 的 ReplayKit 保存录屏视频的基础上,增加了视频流实时直播功能,iOS 11新增的ReplayKit2,进一步提升了Replaykit的易用性,可以对整个手机实现屏幕录制,不再局限于特定App。ReplayKit的出现,让直播更稳定、对设备性能要求低、并可直接嵌入主 app。
宿主App:被录屏的应用(对应SmartiOSScreenPublisherV2)
容器App:本身与录屏直播没有直接关系, 仅仅提供录屏App扩展的发布渠道(对应DaniuliveExtSetupUI)
录屏App扩展:实现录屏和直播的主要功能(对应DaniuliveExt)
添加扩展:File->New Target:

填写Product Name:

配置好Product Name(记得勾选”Include UI Extension”)。点”Finish”后可以看到,工程多了两个目录,target也多了两个,对应直播扩展和UI扩展:

APP内需要处理的两个扩展:
Broadcast UI Extension: 提供类似用户登录、URL设置等配置选项的界面,本实例,由于我们在DaniuliveExtSetupUI中新创建了MainInterface.storyboard,将info.plist中的:
NSExtensionPrincipalClass
BroadcastSetupViewController
换成
NSExtensionMainStoryboard
MainInterface
Broadcast Upload Extension:接收图像和音频数据, 进行直播,集成daniulive推送SDK的工作主要在Upload扩展的代码中进行,info.plist中添加音频权限:
NSMicrophoneUsageDescription
请允许使用麦克风
如需生成后台可直接启用的录制程序,info.plist中NSExtensionPointIdentifier改为:
NSExtensionPointIdentifier
com.apple.broadcast-services-upload
如需APP内部debug,查看SampleHandler里面的log,请设置为
NSExtensionPointIdentifier
com.apple.broadcast-services
导入DaniuliveSDK,对应“DaniuliveExt\daniuliveSDK”下的include和libs。

添加依赖库:

编写UI扩展(对应DaniuliveExtSetupUI):
//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: _rtmpUrl.text];
// Service specific broadcast data example which will be supplied to the process extension during broadcast
NSString *endpointURL = _rtmpUrl.text;
NSDictionary *setupInfo = @{@"endpointURL" : endpointURL};
// Set broadcast settings
RPBroadcastConfiguration *broadcastConfig = [[RPBroadcastConfiguration alloc] init];
// Tell ReplayKit that the extension is finished setting up and can begin broadcasting
[self.extensionContext completeRequestWithBroadcastURL:broadcastURL broadcastConfiguration:broadcastConfig setupInfo:setupInfo];
}
- (void)userDidCancelSetup {
// Tell ReplayKit that the extension was
// cancelled by the user
NSError * err = [NSError errorWithDomain:@"com.daniulive.ios"
code:-1
userInfo:nil];
[self.extensionContext cancelRequestWithError:err];
}
SampleHandler.m调用SDK即可:
[if !supportLists]1. [endif]初始化并推送:
[self InitPublisher];
[self StartPublisher];
[if !supportLists]2. [endif]实时数据回调处理:
- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer
withType:(RPSampleBufferType)sampleBufferType {
switch (sampleBufferType) {
case RPSampleBufferTypeVideo:
{
NSLog(@"RPSampleBufferTypeVideo");
if(_smart_publisher_sdk)
{
[_smart_publisher_sdk SmartPublisherPostVideoSampleBuffer:sampleBuffer];
}
}
break;
case RPSampleBufferTypeAudioApp:
NSLog(@"RPSampleBufferTypeAudioApp");
if (s_headPhoneIn || s_isMicEnable == Mic_Disable)
{
if (CMSampleBufferDataIsReady(sampleBuffer) != NO)
{
if(_smart_publisher_sdk)
{
NSInteger type = 2;
[_smart_publisher_sdk SmartPublisherPostAudioSampleBuffer:sampleBuffer inputType:type];
}
}
}
break;
case RPSampleBufferTypeAudioMic:
NSLog(@"RPSampleBufferTypeAudioMic");
if(_smart_publisher_sdk)
{
NSInteger type = 1;
[_smart_publisher_sdk SmartPublisherPostAudioSampleBuffer:sampleBuffer inputType:type];
}
break;
default:
break;
}
}
[if !supportLists]3. [endif]退出:
[self StopPublisher];
[self UnInitPublisher];
NOTE:集成时DaniuliveExt请使用daniulive demo里面的app-name测试,如需授权,请联系大牛直播SDK工作人员。
1. 先运行SmartiOSScreenPublisherV2,在移动端生成宿主app;
2. 再运行DaniuliveExtSetupUI,加载到宿主app;
3. 最后运行DaniuliveExt,加载到宿主app。
前台启动,设置推送的url,点击“开始推屏”:


后台启动(推屏rtmp url可在前台推送url页面设置):




测试Demo可以到Github或者QQ群共享下载
Github:https://github.com/daniulive/SmarterStreaming/
csdn Demo下载:https://download.csdn.net/download/renhui1112/10313775
QQ群:
大牛直播精英群: 294891451
大牛直播技术交流群: 499687479