SDK集成

如下是一个总体接入流程,详细细节请参见 RTC服务开通指南。

alt

整体实现流程
整体业务流程图
核心功能实现
主播开启直播和观众进房
时序图

alt

示例代码
- (void)startMixStreamRetweetWithPushUrl:(NSString *)pushUrl
                                  rtcUid:(NSString *)rtcUid
                               rtcRoomId:(NSString *)rtcRoomId {
    if (NOEmptyStr(pushUrl)) {
        // 初始化 ByteRTCVideo 对象
        // Initialize the ByteRTCVideo object
        self.rtcEngineKit = [ByteRTCVideo createRTCVideo:appID
                                            delegate:self
                                          parameters:@{}];
                                        
        // 初始化 ByteRTCRoom 对象  
        // Initialize the ByteRTCRoom object
        self.rtcRoom = [self.rtcEngineKit createRTCRoom:rtcRoomID];
        self.rtcRoom.delegate = self;
        
        // 加入 RTC 房间
        // join the RTC room
        ByteRTCUserInfo *userInfo = [[ByteRTCUserInfo alloc] init];
        userInfo.userId = userID;
        ByteRTCRoomConfig *config = [[ByteRTCRoomConfig alloc] init];
        config.profile = ByteRTCRoomProfileLiveBroadcasting;
        config.isAutoPublish = YES;
        config.isAutoSubscribeAudio = YES;
        config.isAutoSubscribeVideo = YES;
        [self.rtcRoom joinRoomByToken:token
                             userInfo:userInfo
                           roomConfig:config];
        
        // 设置采集分辨率
        // Set the video capture config
        ByteRTCVideoCaptureConfig *captureConfig = [[ByteRTCVideoCaptureConfig alloc] init];
        captureConfig.videoSize = CGSizeMake(720, 1280);
        captureConfig.frameRate = 15;
        [self.rtcEngineKit setVideoCaptureConfig:captureConfig];
        // 设置推流编码分辨率                // Set the video encoder config
        ByteRTCVideoEncoderConfig *encoderConfig = [[ByteRTCVideoEncoderConfig alloc] init];
        encoderConfig.videoSize = videoSize;
        encoderConfig.frameRate = frameRate;
        encoderConfig.maxKbps = maxKbps;
        [self.rtcEngineKit SetMaxVideoEncoderConfig:encoderConfig];
        
        // 开启相机和麦克风采集
        // Start video and audio capture
        [self.rtcEngineKit startVideoCapture];
        [self.rtcEngineKit startAudioCapture];
    }
}

// 加入 RTC 房间成功
// Join RTC room succeed delegate
- (void)rtcRoom:(ByteRTCRoom *)rtcRoom onRoomStateChanged:(NSString *)roomId withUid:(NSString *)uid state:(NSInteger)state extraInfo:(NSString *)extraInfo {
    if (state == 0) {
        // 收到 RTC 入房成功回调之后,开始合流转推
        // After receiving the successful callback of RTC entry, start to merge and retweet
        [self startLiveTranscoding];
    }
}

- (void)startLiveTranscoding {    
    // 获取合流转推视频流布局
    // Get the layout of the confluence retweet video stream
    NSArray *regions = [self getRegionWithUserId:rtcUid
                                       rtcRoomId:rtcRoomId];
    // 设置合流转推参数
    // Confluence retweet Setting
    self.transcodingSetting = [ByteRTCLiveTranscoding defaultTranscoding];
    self.transcodingSetting.layout.regions = regions;
    self.transcodingSetting.roomId = rtcRoomId;
    self.transcodingSetting.userId = [LocalUserComponents userModel].uid;
    self.transcodingSetting.url = pushUrl;
    self.transcodingSetting.expectedMixingType = ByteRTCStreamMixingTypeByServer;
    self.transcodingSetting.audio.sampleRate = 44100;
    self.transcodingSetting.audio.channels = 2;
    self.transcodingSetting.video.fps = frameRate;
    self.transcodingSetting.video.kBitRate = maxKbps;
    self.transcodingSetting.video.width = videoSize.width;
    self.transcodingSetting.video.height = videoSize.height;
    
    // 开始合流转推
    // start merge retweet
    [self.rtcEngineKit startLiveTranscoding:@""
                                transcoding:self.transcodingSetting
                                   observer:self];
}
- (NSArray *)getRegionWithUserId:(NSString *)userId
                       rtcRoomId:(NSString *)rtcRoomId {
    NSInteger audienceIndex = 0;
    NSMutableArray *list = [[NSMutableArray alloc] init];
    ByteRTCVideoCompositingRegion *region = [[ByteRTCVideoCompositingRegion alloc] init];
    region.uid = userId;
    region.roomId = rtcRoomId;
    region.localUser = [userModel.uid isEqualToString:[LocalUserComponents userModel].uid] ? YES : NO;
    region.renderMode = ByteRTCRenderModeHidden;
    region.x = 0.0;
    region.y = 0.0;
    region.width = 1.0;
    region.height = 1.0;
    region.zOrder = 1;
    region.alpha = 1.0;
    [list addObject:region];
        return [list copy];
}
objectivec
主播与观众连麦互动
时序图

alt

示例代码
- (void)audienceOnMic {
    // 初始化 ByteRTCVideo 对象
    // Initialize the ByteRTCVideo object
    self.rtcEngineKit = [ByteRTCVideo createRTCVideo:appID
                                            delegate:self
                                          parameters:@{}];
                                            // 初始化 ByteRTCRoom 对象      // Initialize the ByteRTCRoom object
    self.rtcRoom = [self.rtcEngineKit createRTCRoom:rtcRoomID];
    self.rtcRoom.delegate = self;
        // 设置推流编码分辨率            // Set the video encoder config
    ByteRTCVideoEncoderConfig *encoderConfig = [[ByteRTCVideoEncoderConfig alloc] init];
    encoderConfig.videoSize = videoSize;
    encoderConfig.frameRate = frameRate;
    encoderConfig.maxKbps = maxKbps;
    [self.rtcEngineKit SetMaxVideoEncoderConfig:encoderConfig];
        // 加入 RTC 房间
    // join the RTC room
    ByteRTCUserInfo *userInfo = [[ByteRTCUserInfo alloc] init];
    userInfo.userId = userID;
    ByteRTCRoomConfig *config = [[ByteRTCRoomConfig alloc] init];
    config.profile = ByteRTCRoomProfileLiveBroadcasting;
    config.isAutoPublish = YES;
    config.isAutoSubscribeAudio = YES;
    config.isAutoSubscribeVideo = YES;
    [self.rtcRoom joinRoomByToken:token
                         userInfo:userInfo
                       roomConfig:config];
    
    // 开启相机和麦克风采集
    // Start video and audio capture
    [self.rtcEngineKit startVideoCapture];
    [self.rtcEngineKit startAudioCapture];
}
objectivec
- (void)audienceLeaveMic {
    // 离开 RTC 房间
    // Leave the RTC room
    [self.rtcRoom leaveRoom];
    
    // 释放 rtcRoom 对象
    // destroy the rtcRoom object
    [self.rtcRoom destroy];
    self.rtcRoom = nil;
    
    // 关闭相机和麦克风采集
    // Stop video and audio capture
    [self.rtcEngineKit stopVideoCapture];
    [self.rtcEngineKit stopAudioCapture];
}
objectivec
主播与主播连麦PK
时序图

alt

示例代码
- (void)startCoHost {
    // 开启跨房转推
    // Enable span the room retweet stream
    ForwardStreamConfiguration *configuration = [[ForwardStreamConfiguration alloc] init];
    configuration.roomId = roomId;
    configuration.token = token;
    [self.rtcEngineKit startForwardStreamToRooms:@[configuration]];
}

- (void)rtcEngine:(ByteRTCEngineKit *)engine onUserPublishStream:(NSString *)userId type:(ByteRTCMediaStreamType)type {
    // 收到 B 主播视频流回调
    // Receive video stream from anchor B
    if (type == ByteRTCMediaStreamTypeBoth ||
        type == ByteRTCMediaStreamTypeVideo) {
        
        // 修改 RTC 编码参数
        // Encoder config
        ByteRTCVideoSolution *pushRTCVideoConfig = [[ByteRTCVideoSolution alloc] init];
        pushRTCVideoConfig.videoSize = videoSize;
        pushRTCVideoConfig.frameRate = frameRate;
        pushRTCVideoConfig.maxKbps = maxKbps;
        [self.rtcEngineKit SetMaxVideoEncoderConfig:@[pushRTCVideoConfig]];
            // 当前主播的合流布局
        // Current streamer's confluence layout
        ByteRTCVideoCompositingRegion *localRegion = [[ByteRTCVideoCompositingRegion alloc] init];
        // 当前登录主播的 user id 
        // The user id of the currently logged in host
        localRegion.uid = localUserId;
        localRegion.roomId = rtcRoomId;
        localRegion.localUser = YES;
        localRegion.renderMode = ByteRTCRenderModeHidden;
        localRegion.x = 0.0;
        localRegion.y = 0.25;
        localRegion.width = 0.5;
        localRegion.height = 0.5;
        localRegion.zOrder = 0;
        localRegion.alpha = 1.0;
            // 远端主播的合流布局
        // Confluence layout of remote anchors
        ByteRTCVideoCompositingRegion *remoteRegion = [[ByteRTCVideoCompositingRegion alloc] init];
        // 远端主播 user id 
        // remote host user id
        remoteRegion.uid = userId;
        remoteRegion.roomId = rtcRoomId;
        remoteRegion.localUser = NO;
        remoteRegion.renderMode = ByteRTCRenderModeHidden;
        remoteRegion.x = 0.5;
        remoteRegion.y = 0.25;
        remoteRegion.width = 0.5;
        remoteRegion.height = 0.5;
        remoteRegion.zOrder = 0;
        remoteRegion.alpha = 1.0;
    
        // 更新合流布局
        // Update Confluence Layout
        self.transcodingSetting.layout.regions = @[localRegion, remoteRegion];
        [self.rtcEngineKit updateLiveTranscoding:@""
                                     transcoding:self.transcodingSetting];
    }
}
objectivec
核心功能 API 与回调参考
API
功能点API
创建 ByteRTCVideo 实例createRTCVideo:delegate:parameters:
创建 ByteRTCRoom 实例createRTCRoom
设置相机采集参数setVideoCaptureConfig:
设置视频发布参数SetMaxVideoEncoderConfig
设置镜像setLocalVideoMirrorType
获取合流转推默认配置参数ByteRTCLiveTranscoding.defaultTranscoding
加入RTC房间joinRoomByToken:userInfo:roomConfig:
离开RTC房间leaveRoom
销毁房间对象destroy
开启合流转推startLiveTranscoding:transcoding:observer:
更新合流转推参数updateLiveTranscoding:transcoding:
关闭合流转推stopLiveTranscoding:
开启跨房间转推startForwardStreamToRooms:
关闭跨房间转推stopForwardStreamToRooms
开启相机采集startVideoCapture
关闭相机采集stopVideoCapture
开启麦克风采集startAudioCapture
关闭麦克风采集stopAudioCapture
切换前置/后置摄像头switchCamera:
暂停接收来自远端的媒体流pauseAllSubscribedStream:
恢复接收来自远端的媒体流resumeAllSubscribedStream:
回调
功能点回调
本地用户加入 RTC 回调rtcRoom:onRoomStateChanged:withUid:state:extraInfo:
远端用户加入 RTC 回调rtcRoom:onUserJoined:elapsed:
收到远端用户音视频流回调rtcRoom:onUserPublishStream:type:
本地流数据统计以及网络质量回调rtcRoom:onLocalStreamStats:
远端流数据统计以及网络质量回调rtcRoom:onRemoteStreamStats: