天天看點

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

目錄

1背景說明 2 iOS錄制螢幕三要素 3內建說明 4 SDK對接 5 SDK運作 6如何在移動端測試 7 Demo下載下傳

在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)

添加擴充: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,請設定為

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];

                    }

                }

        case RPSampleBufferTypeAudioMic:

            NSLog(@"RPSampleBufferTypeAudioMic");

                NSInteger type = 1;

                [_smart_publisher_sdk SmartPublisherPostAudioSampleBuffer:sampleBuffer inputType:type];

        default:

    }

[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/10313367 https://download.csdn.net/download/renhui1112/10313775

QQ群:

大牛直播精英群: 294891451

大牛直播技術交流群: 499687479