You need to enable JavaScript to run this app.
导航
场景搭建(Windows)
最近更新时间:2023.12.28 20:05:12首次发布时间:2022.11.15 18:43:12

SDK集成

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

alt

整体实现流程

整体业务流程图

alt

核心功能实现

进入/退出房间

时序图

alt

示例代码

int VideoCallRtcEngineWrap::initAndJoinRTCRoom() {
    // 初始化 RTC Video 对象
    // Initialize the RTC Video object
    video_engine_(nullptr,[](bytertc::IRTCVideo* self) {
                            bytertc::destroyRTCVideo();}),
    video_engine_.reset(bytertc::createRTCVideo(app_id.c_str(), this, ""));
    
    // 初始化 RTC Room 对象
    // Initialize the RTC Room object
     rtcRoom_ = std::shared_ptr<bytertc::IRTCRoom>(
            video_engine_->createRTCRoom(room_id.c_str()),
                [=](bytertc::IRTCRoom* room) { room->destroy(); });
                
     // 根据预览页配置开启/关闭相机采集和视频流推送
     //Enable/disable video capture and push stream according to the preview page configuration
    if (DataMgr::instance().camera_on) {
        video_engine_->startVideoCapture();
        rtcRoom->publishStream(bytertc::MediaStreamType::kMediaStreamTypeVideo);
    }
    else {
        video_engine_->stopVideoCapture();
        rtcRoom_ ->unpublishStream(bytertc::MediaStreamType::kMediaStreamTypeVideo);
    }
   
    // 根据预览页配置开启/关闭音频采集和流推送
    //Enable/disable audio capture and push stream according to the preview page configuration
    if (DataMgr::instance().mic_on) {
        video_engine_->startAudioCapture();
        rtcRoom->publishStream(bytertc::MediaStreamType::kMediaStreamTypeAudio);
    }
    else {
        video_engine_->stopAudioCapture();
        rtcRoom_ ->unpublishStream(bytertc::MediaStreamType::kMediaStreamTypeAudio);
    }
    
    //开启/关闭发言者音量键控
    //Turn on/off speaker volume keying
    bytertc::AudioPropertiesConfig config;
    config.interval = 1000;
    video_engine_->enableAudioPropertiesReport(config);
    
    //加入房间,开始连麦,需要申请AppId和Token
    //Join the room, start connecting the microphone, you need to apply for AppId and Token
    auto userName = videocall::DataMgr::instance().user_name();
    auto userId = videocall::DataMgr::instance().user_id()
    QJsonObject extra_info;
    extra_info["user_id"] = QString::fromStdString(userId);
    extra_info["user_name"] = QString::fromStdString(userName);
      
    auto infoStr = QString(QJsonDocument(extra_info).toJson());
    auto infoStdString = std::string(infoStr.toUtf8());
    
    bytertc::UserInfo user = {uid.c_str(), infoStdString.c_str()};
    
    bytertc::RTCRoomConfig config;
    config.room_profile_type = bytertc::RoomProfileType::kRoomProfileTypeCommunication;
   
    // 开启自动推送(默认开启)
    // enable automatic push(defult enabled)
    config.is_auto_publish= true;
    // 开启自动订阅音视频(默认开启)
    // Enable automatic subscription of audio and video(defult enabled)
    config.is_auto_subscribe_audio= true;
    config.is_auto_subscribe_video= true;
    rtcRoom_.joinRoom(token, user, config);
}
void leaveRTCRoom(){
    VideoCallNotify::offAll();
    if (screen_shared_uid == self.user_id()) {
        //停止屏幕音频采集和屏幕流采集
        VideoCallRtcVideoWrap::stopScreenAudioCapture();
        VideoCallRtcVideoWrap::stopScreenCapture();
    }
    //离开房间
    rtcRoom_->leaveRoom();
    rtcRoom_->destroy();
}

断线重连

  • 短时间断网
    无需处理。RTC SDK 会补发断网期间丢失的消息。
    例如:本地用户网络从 WIFI 切换到 5G,在网络切换中有远端用户加入房间。当本地用户网络切换成功后,就会收到 RTC SDK -onUserJoined 回调。
  • 长时间断网,时序图和关键代码如下:

时序图

alt

示例代码

void IRTCRoomEventHandler::onRoomStateChanged(const char* room_id, const char* uid,
                                       int state, const char* extra_info) {
    auto infoArray = QByteArray(extra_info.data(), static_cast<int>(extra_info.size()));
    auto infoJsonObj = QJsonDocument::fromJson(infoArray).object();
    auto joinType = infoJsonObj["join_type"].toInt();
    // joinType '0'为首次进房,'1'为重连进房 ;state '0'为加入房间成功
    // joinType '0' is the first time entering the room, '1' is the reconnection entering the room; state '0' means join the room successfully
    if (state == 0 && joinType == 1) {
    // 执行业务重连API,更新业务状态
        userReconnect([=](int code) {
            //do something
        });
   }
}

屏幕共享

屏幕共享详见PC 端屏幕共享

核心功能 API 与回调参考

API

功能点API
创建 ByteRTCVideo 实例createRTCVideo
设置视频发布参数SetVideoEncoderConfig
开启本地音频采集startAudioCapture
开启本地视频采集startVideoCapture
设置本地视频渲染setLocalVideoCanvas
加入 RTC 房间joinRoom
设置视频渲染视图setRemoteVideoCanvas
离开房间leaveRoom
关闭内部音频采集stopAudioCapture
关闭内部视频采集stopVideoCapture
销毁引擎实例对象destroy
发布本地通过摄像头/麦克风采集的媒体流publishStream
取消发布本地通过摄像头/麦克风采集的媒体流unpublishStream
设置音频播放设备为扬声器或者听筒setAudioPlaybackDevice
开启音量提示enableAudioPropertiesReport
开启镜像setLocalVideoMirrorType
设置音质档位setAudioProfile

回调

功能点回调
本地进房成功回调onRoomStateChanged
远端可见用户加入房间onUserJoined