tencent cloud

文档反馈

Windows C++

最后更新时间:2024-07-18 15:23:04
    本文将介绍如何快速完成 Windows RTC Engine 的接入,实现一个基本的音视频通话。

    环境准备

    操作系统:Windows 7及以上版本。
    开发环境:Visual Studio 2010及以上版本,推荐使用 Visual Studio 2015。

    步骤1. 导入 SDK

    1. 打开 Visual Studio,新建一个名为 TRTCDemo 的 MFC 应用程序。在向导的 MFC Application 页面,Application type 选择 Dialog based,其他向导配置选择默认即可,单击 Finish
    
    2. 下载 WIndows SDK,将解压后的 SDK 文件拷贝到TRTCDemo.vcxproj 所在目录下。
    
    
    

    步骤2. 配置项目

    打开 TRTCDemo.sln 属性页,在 Solution Explorer > TRTCDemo 工程的右键菜单 > Properties,进行以下配置:
    1. 添加 Additional Include Directories:
    C/C++ > General > Additional Include Directories,添加 SDK 头文件目录:$(ProjectDir)SDK\\CPlusPlus\\Win64\\include$(ProjectDir)SDK\\CPlusPlus\\Win64\\include\\TRTC
    
    2. 添加 Additional Library Directories:
    Linker > General > Additional Library Directories,添加 SDK 库目录$(ProjectDir)SDK\\CPlusPlus\\Win64\\lib
    
    
    
    3. 添加 Additional Dependencies:
    Linker > Input > Additional Dependencies,添加 SDK 库文件liteav.lib
    
    
    
    4. 添加 Command line:
    Build Events > Post-Build Event > Command line,添加copy /Y $(ProjectDir)SDK\\CPlusPlus\\Win64\\lib\\*.dll $(OutDir)
    
    
    
    5. 打印 SDK 版本号:
    TRTCDemoDlg.cpp 文件顶部引入头文件:
    #include "ITRTCCloud.h"
    注意:
    在已有头文件的后面引用 "ITRTCCloud.h",否则将会出现报错: ITRTCCloud:未定义标识符
    CTRTCDemoDlg::OnInitDialog 函数中,添加以下代码:
    ITRTCCloud * pTRTCCloud = getTRTCShareInstance();
    CString szText;
    szText.Format(L"SDK version: %hs", pTRTCCloud->getSDKVersion());
    
    CWnd *pStatic = GetDlgItem(IDC_STATIC);
    pStatic->SetWindowTextW(szText);
    完成以上步骤,单击运行可以打印 SDK 版本号。
    
    
    
    说明:
    如果出现报错信息:module machine type 'x86' conflicts with target machine type 'x64',在解决方案平台选择 'x64'。

    步骤3. 创建 TRTC 实例

    1. TRTCDemo.h 文件中引用头文件 "ITRTCCloud.h",将 CTRTCDemo 类公有继承于 CWinAppITRTCCloudCallback,并声明回调函数和成员变量。
    #include "ITRTCCloud.h"
    class CTRTCDemoApp : public CWinApp, public ITRTCCloudCallback
    {
    public:
    CTRTCDemoApp();
    
    // Overwrite
    public:
    virtual BOOL InitInstance();
    virtual void onError(TXLiteAVError errCode, const char* errMsg, void* extraInfo) override;
    virtual void onWarning(TXLiteAVWarning warningCode, const char* warningMsg, void* extrainfo) override;
    virtual void onEnterRoom(int result) override;
    virtual void onExitRoom(int reason) override;
    // Emplement
    
    DECLARE_MESSAGE_MAP()
    
    private:
    ITRTCCloud* trtc_cloud_; // Declare the member variable trtc cloud
    };
    2. TRTCDemo.cpp 文件的 CTRTCDemo::InitInstance() 方法中调用初始化接口创建 TRTC 的对象实例。
    // Create trtc instance(singleton) and set up event listeners
    trtc_cloud_ = getTRTCShareInstance();
    trtc_cloud_->addCallback(this);
    说明:
    将初始化接口代码添加在 SetRegistryKey(_T("Local AppWizard-Generated Applications")) 方法后。
    3. 实现声明的回调方法。
    // Listen for SDK events, and print logs for error messages such as "Current application is not authorized to use the camera"
    // Listen to the "onError" event, and print logs for errors such as "Camera is not authorized"
    void CTRTCDemoApp::onError(TXLiteAVError errCode, const char* errMsg, void* extraInfo) {
    if (errCode == ERR_CAMERA_NOT_AUTHORIZED) {
    printf("Current application is not authorized to use the camera");
    }
    }
    
    // Listen for SDK events, and print logs for warning messages
    // Listen to the "onWarning" event, and print logs for errors such as "WARNING_VIDEO_RENDER_FAIL"
    void CTRTCDemoApp::onWarning(TXLiteAVWarning warningCode, const char* warningMsg, void* extrainfo) {
    if (warningCode == WARNING_VIDEO_RENDER_FAIL) {
    printf("WARNING_VIDEO_RENDER_FAIL");
    }
    
    }
    
    // Listen for the `onEnterRoom` event of the SDK to get the room entry result
    // override to the onEnterRoom event of the SDK and learn whether the room is successfully entered
    void CTRTCDemoApp::onEnterRoom(int result) {
    if (result > 0) {
    printf("Enter room succeed");
    }
    else {
    printf("Enter room failed");
    }
    }
    
    // Listen for the `onExitRoom` event of the SDK to get the room exit result
    // override to the onExitRoom event of the SDK and learn whether the room is successfully exited
    void CTRTCDemoApp::onExitRoom(int reason) {
    if (reason == 0) {
    printf("Exit current room by calling the 'exitRoom' api of sdk ...");
    } else if (reason == 1) {
    printf("Kicked out of the current room by server through the restful api...");
    } else if (reason == 2) {
    printf("Current room is dissolved by server through the restful api...");
    }
    }

    步骤4. 进入房间

    1. Tencent RTC 控制台中单击Create Application,在 Application Overview 中获取 SDKAppID
    
    
    
    2. UserSig Tools中下来选择 SDKAppID ,并输入您自己的用户名(UserID),单击Generate即可获取您自己的 UserSig
    
    
    
    3. CTRTCDemo::InitInstance() 方法中设置进房参数 TRTCParams 后,调用 enterRoom 接口函数即可进入房间。
    主播角色:
    BOOL CTRTCDemo::InitInstance()
    {
    // ...other codes
    // Assemble TRTC room entry parameters. Replace the field values in `TRTCParams` with your own parameter values
    // Replace each field in TRTCParams with your own parameters
    liteav::TRTCParams trtcParams;
    trtcParams.sdkAppId = 1400000123; // Replace with your own SDKAppID
    trtcParams.userId = "denny"; // Replace with your own user ID
    trtcParams.roomId = 123321; // Replace with your own room number
    trtcParams.userSig = "xxx"; // Replace with your own userSig
    trtcParams.role = liteav::TRTCRoleAnchor;
    
    // If your scenario is live streaming, set the application scenario to `TRTC_APP_SCENE_LIVE`
    // If your application scenario is a group video call, use "TRTC_APP_SCENE_LIVE"
    trtc_cloud_->enterRoom(trtcParams, liteav::TRTCAppSceneLIVE);
    // ...other codes
    }
    观众角色:
    BOOL CTRTCDemo::InitInstance()
    {
    // ...other codes
    // Assemble TRTC room entry parameters. Replace the field values in `TRTCParams` with your own parameter values
    // Replace each field in TRTCParams with your own parameters
    liteav::TRTCParams trtcParams;
    trtcParams.sdkAppId = 1400000123; // Replace with your own SDKAppID
    trtcParams.userId = "denny"; // Replace with your own user ID
    trtcParams.roomId = 123321; // Replace with your own room number
    trtcParams.userSig = "xxx"; // Replace with your own userSig
    trtcParams.role = liteav::TRTCRoleAudience;
    
    // If your scenario is live streaming, set the application scenario to `TRTC_APP_SCENE_LIVE`
    // If your application scenario is a group video call, use "TRTC_APP_SCENE_LIVE"
    trtc_cloud_->enterRoom(trtcParams, liteav::TRTCAppSceneLIVE);
    // ...other codes
    }
    注意:
    如果以观众角色进入房间,sdkAppIdroomId 需要与主播端相同,而userIduserSig 需要替换为自己的值。

    步骤5. 打开摄像头

    1. 在资源文件 IDD_TRTCDEMO_DIALOG 中,单击左侧边框的 Toolbox,拖动 Picture Control 到对话框中。
    2. Picture Control 控件的右键菜单中选择 Properties,ID 选择 AFX_IDC_PICTURE
    3. CTRTCDemo::onEnterRoom() 方法中调用接口 setLocalRenderParams 来设置本地预览的渲染参数,然后调用接口 startLocalPreview 打开摄像头预览,如以下代码:
    void CTRTCDemo::onEnterRoom(int result) {
    if (result > 0) {
    // Set the preview mode of the local video image: Enable horizontal mirroring and set the fill mode for the video image
    liteav::TRTCRenderParams render_params;
    render_params.mirrorType = liteav::TRTCVideoMirrorType_Enable;
    render_params.fillMode = TRTCVideoFillMode_Fill;
    trtc_cloud_->setLocalRenderParams(render_params);
    
    // Start a preview of the local camera
    CWnd* pLocalVideoView = m_pMainWnd->GetDlgItem(AFX_IDC_PICTURE);
    auto local_view = (liteav::TXView)(pLocalVideoView->GetSafeHwnd());
    trtc_cloud_->startLocalPreview(local_view);
    
    printf("Enter room succeed");
    }
    else {
    printf("Enter room failed");
    }
    }

    步骤6. 打开麦克风

    您可以调用 startLocalAudio 来开启麦克风采集,该接口需要您通过 quality 参数确定采集模式,建议根据您的需求选择以下其中一个适合您项目的模式
    // Enable microphone acquisition and set the current scene to: Voice mode
    // For high noise suppression capability, strong and weak network resistance
    trtc_cloud_->startLocalAudio(TRTCAudioQualitySpeech);
    
    // Enable microphone acquisition, and set the current scene to: Music mode
    // For high fidelity acquisition, low sound quality loss, recommended to use with professional sound cards
    trtc_cloud_->startLocalAudio(TRTCAudioQualityMusic);

    步骤7. 播放/停止视频流

    1. 创建一个新项目,按照步骤1-4观众角色进入 denny 的房间。
    2. 在资源文件 IDD_TRTCDEMO_DIALOG 中添加 Picture Control 控件,并将 ID 选择为 AFX_IDC_PICTURE
    3. CTRTCDemo::onEnterRoom() 方法中调用接口 startRemoteView 来播放远端用户的视频画面。
    if (result > 0) {
    // Start a preview of the local camera
    CWnd* pVideoView = m_pMainWnd->GetDlgItem(AFX_IDC_PICTURE);
    auto video_view = (liteav::TXView)(pVideoView->GetSafeHwnd());
    // Play denny's camera footage
    trtc_cloud_->startRemoteView("denny", liteav::TRTCVideoStreamTypeBig, video_view);
    printf("Enter room succeed");
    }
    之后,您可以调用 stopRemoteView 接口停止某个远端用户的视频,也可以通过接口 stopAllRemoteView 停止播放所有远端用户的视频。
    // Stop denny's camera footage
    trtc_cloud_->stopRemoteView("denny", liteav::TRTCVideoStreamTypeBig);
    
    // Stop all camera footages
    trtc_cloud_->stopAllRemoteView();

    步骤8. 播放/停止音频流

    您可以通过调用接口 muteRemoteAudio("denny",true) 来静音远端用户 denny 的声音。
    // Mute user with id denny
    trtc_cloud_->muteRemoteAudio("denny", true);
    之后,您可以通过调用接口 muteRemoteAudio("denny",false) 来解除对他的静音。
    // Unmute user with id denny
    trtc_cloud_->muteRemoteAudio("denny", false);

    步骤9. 退出房间

    您可以调用接口 exitRoom 即可退出当前的房间,SDK 会在退房结束后通过 onExitRoom(int reason) 回调事件通知您。
    // Exit current room
    trtc_cloud_->exitRoom();

    常见问题

    您可以在API 参考查看所有函数列表及其描述。
    如果您的接入和使用中遇到问题,请参见 常见问题

    联系我们

    如果有任何需要或者反馈,您可以联系:info_rtc@tencent.com。
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持