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

主播与主播进行 PK
主播与观众进行连麦
)

- (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 joinRoom: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];
}
Objective-C
- (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 joinRoom:token
userInfo:userInfo
roomConfig:config];
// 开启相机和麦克风采集
// Start video and audio capture
[self.rtcEngineKit startVideoCapture];
[self.rtcEngineKit startAudioCapture];
}
Objective-C-- (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];
}
Objective-C
- (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];
}
}
Objective-C| 功能点 | API |
|---|---|
| 创建 ByteRTCVideo 实例 | createRTCVideo:delegate:parameters: |
| 创建 ByteRTCRoom 实例 | createRTCRoom |
| 设置相机采集参数 | setVideoCaptureConfig: |
| 设置视频发布参数 | setMaxVideoEncoderConfig: |
| 设置镜像 | setLocalVideoMirrorType |
| 获取合流转推默认配置参数 | ByteRTCLiveTranscoding.defaultTranscoding |
| 加入RTC房间 | joinRoom: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: |