目录
在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:
![](https://i-blog.csdnimg.cn/blog_migrate/8727ff87753db75aa4a1a699e316e68b.webp?x-image-process=image/format,png)
填写Product Name:
![](https://i-blog.csdnimg.cn/blog_migrate/74d113c61e326c828c22333365cef727.webp?x-image-process=image/format,png)
配置好Product Name(记得勾选”Include UI Extension”)。点”Finish”后可以看到,工程多了两个目录,target也多了两个,对应直播扩展和UI扩展:
![](https://i-blog.csdnimg.cn/blog_migrate/3a7aa02aff2762d3997365624adbea30.webp?x-image-process=image/format,png)
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。
![](https://i-blog.csdnimg.cn/blog_migrate/87390f370d05a81947a9b483f1ea127c.webp?x-image-process=image/format,png)
添加依赖库:
![](https://i-blog.csdnimg.cn/blog_migrate/a5d95205cec1998441caa8a7dc34c2a1.webp?x-image-process=image/format,png)
编写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,点击“开始推屏”:
![](https://i-blog.csdnimg.cn/blog_migrate/8d4910888871877dd54ca872121751ea.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/b3368c0a3347d63d9ed207cf33eeb4c4.webp?x-image-process=image/format,png)
后台启动(推屏rtmp url可在前台推送url页面设置):
![](https://i-blog.csdnimg.cn/blog_migrate/36fd6d2b2d2aefbf673cdf86aad0260f.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/0cd1e7f2b2becdbd67a8657f78e08c69.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/a899fec154562062dbd620357f6c8fc2.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/54840f902002ddc44800e77ef55c2712.webp?x-image-process=image/format,png)
测试Demo可以到Github或者QQ群共享下载
Github:https://github.com/daniulive/SmarterStreaming/
CSDN Demo下载链接: https://download.csdn.net/download/renhui1112/10313775
QQ群:
大牛直播精英群: 294891451
大牛直播技术交流群: 499687479