本文将介绍如何快速完成 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 文件顶部引入头文件:
注意:
在已有头文件的后面引用 "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 类公有继承于 CWinApp 和 ITRTCCloudCallback,并声明回调函数和成员变量。
#include "ITRTCCloud.h"
class CTRTCDemoApp : public CWinApp, public ITRTCCloudCallback
{
public:
CTRTCDemoApp();
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;
DECLARE_MESSAGE_MAP()
private:
ITRTCCloud* trtc_cloud_;
};
2. 在 TRTCDemo.cpp 文件的 CTRTCDemo::InitInstance()
方法中调用初始化接口创建 TRTC 的对象实例。
trtc_cloud_ = getTRTCShareInstance();
trtc_cloud_->addCallback(this);
说明:
将初始化接口代码添加在 SetRegistryKey(_T("Local AppWizard-Generated Applications"))
方法后。
3. 实现声明的回调方法。
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");
}
}
void CTRTCDemoApp::onWarning(TXLiteAVWarning warningCode, const char* warningMsg, void* extrainfo) {
if (warningCode == WARNING_VIDEO_RENDER_FAIL) {
printf("WARNING_VIDEO_RENDER_FAIL");
}
}
void CTRTCDemoApp::onEnterRoom(int result) {
if (result > 0) {
printf("Enter room succeed");
}
else {
printf("Enter room failed");
}
}
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()
{
liteav::TRTCParams trtcParams;
trtcParams.sdkAppId = 1400000123;
trtcParams.userId = "denny";
trtcParams.roomId = 123321;
trtcParams.userSig = "xxx";
trtcParams.role = liteav::TRTCRoleAnchor;
trtc_cloud_->enterRoom(trtcParams, liteav::TRTCAppSceneLIVE);
}
观众角色:
BOOL CTRTCDemo::InitInstance()
{
liteav::TRTCParams trtcParams;
trtcParams.sdkAppId = 1400000123;
trtcParams.userId = "denny";
trtcParams.roomId = 123321;
trtcParams.userSig = "xxx";
trtcParams.role = liteav::TRTCRoleAudience;
trtc_cloud_->enterRoom(trtcParams, liteav::TRTCAppSceneLIVE);
}
注意:
如果以观众角色进入房间,sdkAppId 和 roomId 需要与主播端相同,而userId 和 userSig 需要替换为自己的值。
步骤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) {
liteav::TRTCRenderParams render_params;
render_params.mirrorType = liteav::TRTCVideoMirrorType_Enable;
render_params.fillMode = TRTCVideoFillMode_Fill;
trtc_cloud_->setLocalRenderParams(render_params);
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
参数确定采集模式,建议根据您的需求选择以下其中一个适合您项目的模式。
trtc_cloud_->startLocalAudio(TRTCAudioQualitySpeech);
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) {
CWnd* pVideoView = m_pMainWnd->GetDlgItem(AFX_IDC_PICTURE);
auto video_view = (liteav::TXView)(pVideoView->GetSafeHwnd());
trtc_cloud_->startRemoteView("denny", liteav::TRTCVideoStreamTypeBig, video_view);
printf("Enter room succeed");
}
之后,您可以调用 stopRemoteView
接口停止某个远端用户的视频,也可以通过接口 stopAllRemoteView
停止播放所有远端用户的视频。
trtc_cloud_->stopRemoteView("denny", liteav::TRTCVideoStreamTypeBig);
trtc_cloud_->stopAllRemoteView();
步骤8. 播放/停止音频流
您可以通过调用接口 muteRemoteAudio("denny",true)
来静音远端用户 denny 的声音。
trtc_cloud_->muteRemoteAudio("denny", true);
之后,您可以通过调用接口 muteRemoteAudio("denny",false)
来解除对他的静音。
trtc_cloud_->muteRemoteAudio("denny", false);
步骤9. 退出房间
您可以调用接口 exitRoom
即可退出当前的房间,SDK 会在退房结束后通过 onExitRoom(int reason)
回调事件通知您。
常见问题
联系我们
如果有任何需要或者反馈,您可以联系:info_rtc@tencent.com。
本页内容是否解决了您的问题?