如何基于ReplayKit實現低延遲rtmp推屏

目錄

1背景說明

2 iOS錄制屏幕三要素

3集成說明

4 SDK對接

5 SDK運行

6如何在移動端測試

7 Demo下載

1背景說明

在ReplayKit live未出來之前,iOS推流屏幕信息,必須使用實際或虛擬的AirPlay模式,使用起來非常不便,自iOS 10開始,蘋果在 iOS 9 的 ReplayKit 保存錄屏視頻的基礎上,增加了視頻流實時直播功能,iOS 11新增的ReplayKit2,進一步提升了Replaykit的易用性,可以對整個手機實現屏幕錄制,不再局限于特定App。ReplayKit的出現,讓直播更穩定、對設備性能要求低、并可直接嵌入主 app。

2?iOS錄制屏幕三要素

宿主App:被錄屏的應用(對應SmartiOSScreenPublisherV2)

容器App:本身與錄屏直播沒有直接關系, 僅僅提供錄屏App擴展的發布渠道(對應DaniuliveExtSetupUI)

錄屏App擴展:實現錄屏和直播的主要功能(對應DaniuliveExt)

3集成說明

添加擴展: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

4 SDK對接

導入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工作人員。

5 SDK運行

1. 先運行SmartiOSScreenPublisherV2,在移動端生成宿主app;

2. 再運行DaniuliveExtSetupUI,加載到宿主app;

3. 最后運行DaniuliveExt,加載到宿主app。

6如何在移動端測試

前臺啟動,設置推送的url,點擊“開始推屏”:

后臺啟動(推屏rtmp url可在前臺推送url頁面設置):

 

7 Demo下載

測試Demo可以到Github或者QQ群共享下載

Github:https://github.com/daniulive/SmarterStreaming/

CSDN Demo下載鏈接:?https://download.csdn.net/download/renhui1112/10313775

QQ群:

大牛直播精英群: 294891451

大牛直播技術交流群: 499687479

污污直播app-污污直播破解版永久免费版