项目准备
- 找到腾讯云TRTC官方文档,下载SDK 官方文档地址:https://cloud.tencent.com/document/product/647
WindowsC++官方下载地址:https://liteav.sdk.qcloud.com/download/latest/TXLiteAVSDK_TRTC_Win_latest.zip - 解压文件夹,找到SDK>CPlusPlus目录
- 根据你的需要选择32位(win32文件夹内)还是64位(win64文件夹内)版本。我这里以win32 版本为主。
- 使用VS2019 建一个控制台应用程序,使用WindowsSDK基本包即可。调整项目目录,如下:
- 修改项目配置,将"项目目录>include"添加到include搜索路径,具体修改方法" 右键项目>属性>vc++目录>常规>包含目录 添加include文件夹路径"
书写代码
def.h
根据实际需求修改
#pragma once
#define WIN_CLASSNAME TEXT("My_Widget")
#define WIN_WINDNAME TEXT("Worker")
#define GET_PARAMS(params,type,index) *((type *)((void ** )params)[index])
#define THREAD_PARAMS(params,type,index) GET_PARAMS(params,type,index)
#include <iostream>
#define COUT() std::cout
#define COUT_ERROR(code,msg) std::cout<<"Error("<<code<<"): at "<<__FILE__<<":"<<__LINE__<<" what:"<<msg
#define COUT_ERRORLN(code,msg) COUT_ERROR(code,msg)<<std::endl
#define COUT_MESSAGE(msg) std::cout<<"Message: at "<<__FILE__<<":"<<__LINE__<<" message:"<<msg
#define COUT_MESSAGELN(msg) COUT_MESSAGE(msg)<<std::endl
#define COUT_LN() std::endl
mian.cpp
。
#include <Windows.h>
#include <time.h>
#include <iostream>
#include <vector>
//TX TRTC
#include <TRTC/TXLiteAVBase.h>
#include <TRTC/ITRTCCloud.h>
#include <TRTC/ITXLiteAVLocalRecord.h>
#pragma comment(lib, "liteav.lib")
#include <math.h>
#include "def.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int main(int argc, const char** argv) {
COUT_MESSAGELN(getLiteAvSDKVersion());
HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = WIN_CLASSNAME;
ATOM atRet = RegisterClass(&wndclass);
HWND hWndMian = CreateWindow(WIN_CLASSNAME, L"显示远端的窗口", WS_OVERLAPPEDWINDOW | WS_POPUP,
10, 10, 800, 600,
NULL, NULL, hInstance, NULL);
ShowWindow(hWndMian, SW_SHOW);
UpdateWindow(hWndMian);
HWND hwndChildMy = CreateWindow(WIN_CLASSNAME, L"显示自己的窗口", WS_VISIBLE | WS_CHILD,
30, 30, 200, 180,
hWndMian, NULL, hInstance, NULL);
int iAppid = 0; //这里填充在腾讯云控制台创建的应用ID
std::string strUserId = "";
std::cout << "请输入UserId:" << std::endl;
std::cin >> strUserId;
/*{//向服务器获取签名
HttpClient stHttpClient(L"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0");
WCHAR wcstrBuf[1024];
wsprintf(wcstrBuf, L"http://127.0.0.1:8081/trtc/userssig?uid=%s", strUserId.c_str());
std::string strResText = "";
std::vector<std::wstring> headers;
headers.push_back(L"Host:127.0.0.1");
stHttpClient.http_get(wcstrBuf, headers, strResText);
}*/
std::cout << "输入签名:" << std::endl;
std::cin >> strResText;
//打印签名
std::cout << strResText << std::endl;
MyColudCallback stCallBack;
ITRTCCloud* pstCould = getTRTCShareInstance();
void* callbackParams[3] = { &pstCould,&hwndChildMy,&hWndMian};
stCallBack._params = callbackParams;
pstCould->addCallback(&stCallBack);
pstCould->startLocalPreview(hwndChildMy);
TRTCParams trtcParams;
trtcParams.sdkAppId = iAppid;
trtcParams.userId = strUserId.c_str();
trtcParams.userSig = strResText.c_str();
trtcParams.roomId = 1001;
pstCould->enterRoom(trtcParams, trtc::TRTCAppScene::TRTCAppSceneVideoCall);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
pstCould->exitRoom();
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_CREATE:
{
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
MyColudCallback .h
TRTCCould回调,在此类中重写ITRTCCloudCallback的虚函数
#pragma once
#include <TRTC/ITRTCCloud.h>
class MyColudCallback : public trtc::ITRTCCloudCallback
{
public:
MyColudCallback(void* params = NULL);
public:
virtual void onError(TXLiteAVError errCode, const char* errMsg, void* extraInfo) override;
virtual void onWarning(TXLiteAVWarning warningCode, const char* warningMsg, void* extraInfo) override;
public:
virtual void onEnterRoom(int result) override;
virtual void onExitRoom(int reason) override;
virtual void onRemoteUserEnterRoom(const char* userId) override;
virtual void onFirstVideoFrame(const char* cstrUserId, const TRTCVideoStreamType streamType, const int width, const int height) override;
public:
void* _params;
};
MyColudCallback.cpp
#include "MyColudCallback.h"
#include "def.h"
MyColudCallback::MyColudCallback(void * params) {
_params = params;
}
void MyColudCallback::onError(TXLiteAVError errCode, const char* errMsg, void* extraInfo)
{
COUT_ERRORLN(errCode, errMsg);
}
void MyColudCallback::onWarning(TXLiteAVWarning warningCode, const char* warningMsg, void* extraInfo)
{
COUT_MESSAGELN(warningMsg);
}
void MyColudCallback::onEnterRoom(int result)
{
if(reuslt < 0 ){
//出错误了
}
COUT_MESSAGE("加入房间:Result = ") << result << COUT_LN();
}
void MyColudCallback::onExitRoom(int reason)
{
COUT_MESSAGE("退出房间:reason = ") << reason << COUT_LN();
}
void MyColudCallback::onRemoteUserEnterRoom(const char* userId)
{
ITRTCCloud* pstCould = GET_PARAMS(_params, ITRTCCloud*,0);
HWND hwndChildMy = GET_PARAMS(_params, HWND, 1);
HWND hwndMain = GET_PARAMS(_params, HWND, 2);
//拉取房间内远端数据,渲染到主要窗口中
pstCould->startRemoteView(userId, TRTCVideoStreamTypeBig, hwndMain);
}
void MyColudCallback::onFirstVideoFrame(const char* cstrUserId, const TRTCVideoStreamType streamType, const int width, const int height)
{
COUT() << "\n开始渲染视频:\t " << width << " x " << height << COUT_LN();
}