You need to enable JavaScript to run this app.
导航

场景搭建(iOS)

最近更新时间2024.04.11 14:39:16

首次发布时间2022.05.18 16:36:21

SDK集成

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

alt

整体实现流程

主播与主播连麦pk

alt

主播与观众连麦互动

alt

核心功能实现

主播开启直播和观众进房

时序图

alt

示例代码

/**
 * 加入RTC房间并初始化参数
 * @param token: RTC Token
 * @param roomID: RTC room id
 * @param uid: RTC user id
 * @param isHost: YES 业务上主播 ; NO 业务上观众
 **/
- (void)joinRTCRoomWithToken:(NSString *)token
                      roomID:(NSString *)roomID
                         uid:(NSString *)uid
                      isHost:(BOOL)isHost {
    // 初始化 ByteRTCVideo 对象
    self.rtcEngineKit = [ByteRTCVideo createRTCVideo:APPID
                                        delegate:self
                                      parameters:@{}];
    
    // 初始化 ByteRTCRoom 对象
    self.rtcRoom = [self.rtcEngineKit createRTCRoom:roomID];
    self.rtcRoom.delegate = self;
    
    // 设置主播为可见,观众为隐身
    [self.rtcRoom setUserVisibility:isHost ? YES : NO];
    
    // 加入房间时主播需要开启麦克风、相机,观众需要关闭麦克风、相机
    if (isHost) {
        [self.rtcEngineKit startVideoCapture];
        [self.rtcEngineKit startAudioCapture];
        
        // 设置本地渲染和编码镜像
        [self.rtcEngineKit setLocalVideoMirrorType:ByteRTCMirrorTypeRenderAndEncoder];
    } else {
        [self.rtcEngineKit stopVideoCapture];
        [self.rtcEngineKit stopAudioCapture];
    }
 
    // 设置音频路由模式
    [self.rtcEngineKit setDefaultAudioRoute:ByteRTCAudioRouteSpeakerphone];
 
    // 开启发言者音量监听
    ByteRTCAudioPropertiesConfig *audioPropertiesConfig = [[ByteRTCAudioPropertiesConfig alloc] init];
    audioPropertiesConfig.interval = 300;
    [self.rtcEngineKit enableAudioPropertiesReport:audioPropertiesConfig];
    
    // 加入房间,开始连麦,需要申请AppId和Token
    ByteRTCUserInfo *userInfo = [[ByteRTCUserInfo alloc] init];
    userInfo.userId = uid;
    ByteRTCRoomConfig *config = [[ByteRTCRoomConfig alloc] init];
    config.profile = ByteRTCRoomProfileInteractivePodcast;
    config.isAutoPublish = YES;
    config.isAutoSubscribeAudio = YES;
    config.isAutoSubscribeVideo = YES;
    [self.rtcRoom joinRoom:token userInfo:userInfo roomConfig:config];
}

主播与主播连麦 PK

时序图

alt

示例代码

/**
 * 开启跨房间转推
 * @param roomID 对方房间的 room Id
 * @param token  加入房间所需要的 token, 由业务服务器下发
 **/
- (void)startForwardStream:(NSString *)roomId token:(NSString *)token {
    ForwardStreamConfiguration *configuration = [[ForwardStreamConfiguration alloc] init];
    configuration.roomId = roomId;
    configuration.token = token;
    int res = [self.rtcRoom startForwardStreamToRooms:@[configuration]];
    if (res != 0) {
        NSLog(@"开启跨房转推失败 code:%d", res);
    }
}

/**
 * 房间内新增远端媒体流流的回调
 * @param rtcRoom ByteRTCRoom 的对象
 * @param userId  用户的用户 ID
 * @param type 远端媒体流的类型
 **/
- (void)rtcRoom:(ByteRTCRoom *)rtcRoom onUserPublishStream:(NSString *)userId
           type:(ByteRTCMediaStreamType)type {
    if (type == ByteRTCMediaStreamTypeVideo ||
        type == ByteRTCMediaStreamTypeBoth) {
        // 获取业务层渲染View
        UIView *renderView = [self getRenderView];
        
        ByteRTCVideoCanvas *canvas = [[ByteRTCVideoCanvas alloc] init];
        canvas.renderMode = ByteRTCRenderModeHidden;
        canvas.view.backgroundColor = [UIColor clearColor];
        canvas.view = renderView;
                
        ByteRTCRemoteStreamKey *streamKey = [[ByteRTCRemoteStreamKey alloc] init];
        streamKey.userId = userId;
        streamKey.roomId = self.rtcRoom.getRoomId;
        streamKey.streamIndex = ByteRTCStreamIndexMain;
        [self.rtcEngineKit setRemoteVideoCanvas:streamKey withCanvas:canvas];          
    }
}

/**
 * 结束跨房间转推
 **/
- (void)stopForwardStream {
    [self.rtcRoom stopForwardStreamToRooms];
}

主播与观众连麦互动

时序图

alt

示例代码

/**
* 主播和观众连麦成功
* @param userID 上麦用户的 User ID
**/
- (void)receivedJoinInteractWithUser:(NSString *)userID {
   // 模拟登录用户 User ID
   NSString *LoginUserID = @"";
   // 上麦的用户是登录用户
   if ([userID isEqualToString:LoginUserID]) {
       // 更新 RTC 编码分辨率、帧率、码率,具体数值可以联系技术支持。
       // 参考:https://www.volcengine.com/docs/6348/70122
       ByteRTCVideoEncoderConfig *videoEncoderConfig = [[ByteRTCVideoEncoderConfig alloc] init];
       videoEncoderConfig.width = width;
       videoEncoderConfig.height = height;
       videoEncoderConfig.frameRate = frameRate;
       videoEncoderConfig.maxBitrate = maxBitrate;
       [self.rtcEngineKit setMaxVideoEncoderConfig:videoEncoderConfig];
       
       // 观众开启相机、麦克风采集
       [self.rtcEngineKit startAudioCapture];
       [self.rtcEngineKit startVideoCapture];
       
       // 观众开启音视频推流
       [self.rtcRoom publishStream:ByteRTCMediaStreamTypeBoth];
       
       // 观众设置为可见状态
       [self.rtcRoom setUserVisibility:YES];
   }
}

/**
* 收到观众下麦
* @param userID 下麦用户的 User ID
**/
- (void)receivedLeaveInteractWithUser:(NSString *)userID {
   // 模拟登录用户 User ID
   NSString *LoginUserID = @"";
   // 下麦的观众是登录用户
   if ([userID isEqualToString:LoginUserID]) {
       // 观众开启相机、麦克风采集
       [self.rtcEngineKit stopAudioCapture];
       [self.rtcEngineKit stopVideoCapture];
       
       // 观众开启音视频推流
       [self.rtcRoom unpublishStream:ByteRTCMediaStreamTypeBoth];
       
       // 观众设置为隐身状态
       [self.rtcRoom setUserVisibility:NO];
   }
}

核心功能 API 与回调参考

API

功能点API
创建 ByteRTCVideo 实例createRTCVideo:delegate:parameters:
创建 ByteRTCRoom 实例createRTCRoom:
设置用户可见性setUserVisibility:
开启内部视频采集startVideoCapture
关闭内部视频采集stopVideoCapture
开启内部音频采集startAudioCapture
关闭内部音频采集stopAudioCapture
设置 RTC 编码分辨率等参数SetVideoEncoderConfig:
为采集到的视频流开启镜像setLocalVideoMirrorType:
设置当前音频播放路由setDefaultAudioRoute:
开启音量信息提示enableAudioPropertiesReport:
加入 RTC 房间joinRoom:userInfo:roomConfig:
离开 RTC 房间leaveRoom
销毁房间对象destroy
在当前所在房间内发布媒体流publishStream:
停止媒体流发布到当前所在房间中unpublishStream:
开始跨房间转发媒体流startForwardStreamToRooms:
停止跨房间转发媒体流stopForwardStreamToRooms
设置本地视频渲染setLocalVideoCanvas:withCanvas:
设置远端视频渲染setRemoteVideoCanvas:withCanvas:

回调

功能点回调
本地用户加入 RTC 回调rtcRoom:onRoomStateChanged:uid:state:extraInfo
远端用户加入 RTC 回调rtcRoom:onUserJoined:elapsed:
本地用户音量回调rtcEngine:onLocalAudioPropertiesReport:
远端用户音量回调rtcEngine:onRemoteAudioPropertiesReport:totalRemoteVolume:
房间内新增远端媒体流回调rtcRoom:onUserPublishStream:type:
跨房间媒体流转发状态和错误回调rtcRoom:onForwardStreamStateChanged: