跳到主要内容

WinSDK初始化

更新时间:2025-03-06 19:07:10

SDK初始化

使用RTCX-Windows-PCSDK前必要操作,请在用户授权后再进行SDK初始化操作

初始化示例

using namespace rtx::pcsdk;

// 初始换SDK,appKey和appSecret是平台分配给PC SDK,在系统中的唯一标识及对应的密钥
InitParam param;
param.appKey = "aaaaaaaa";
param.appSecret = "bbbbbbbbbbbbbbb";
IRtcxPcApi::Initialize(param);
std::shared_ptr<TestPcEvtHandler> handler = std::make_shared<TestPcEvtHandler>();
IRtcxPcApi::Ptr pcapi = IRtcxPcApi::CreateRtcxPcApi(handler);

完整demo示例

#include <iostream>
#include <sstream>
#include <thread>
#include <chrono>
#include <atomic>
#include <memory>
#include <windows.h>
#include <tchar.h>
#include "IRtcxPcApi.h"


// 全局变量:窗口句柄和线程控制标志
std::atomic<HWND> g_hwnd{ nullptr };
std::atomic<bool> g_msgLoopRunning{ false };
std::shared_ptr<std::thread> g_windowThread = nullptr;

// 窗口消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd); // 关闭对话框
break;
case WM_DESTROY:
PostQuitMessage(0); // 终止消息循环
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

// 异步线程函数:创建窗口并运行消息循环
void AsyncWindowThread() {
if (!g_hwnd.load()) {
// 注册窗口类
WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = _T("VideoRendererClass");
if (!RegisterClassEx(&wc)) {
std::cerr << "注册窗口类失败\n";
return;
}

// 创建对话框风格窗口(无最小化/最大化按钮)
HWND hwnd = CreateWindowEx(
0, wc.lpszClassName, _T("video player"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
nullptr, nullptr, wc.hInstance, nullptr
);

if (!hwnd) {
std::cerr << "窗口创建失败\n";
return;
}

g_hwnd.store(hwnd); // 存储全局句柄
}

ShowWindow(g_hwnd.load(), SW_SHOW);
UpdateWindow(g_hwnd.load());

// 启动消息循环
g_msgLoopRunning.store(true);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0) && g_msgLoopRunning) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

g_msgLoopRunning.store(false);
}

using namespace rtx::pcsdk;

class TestPcEvtHandler : public IRtcxPcEvtHandler
{
public:
TestPcEvtHandler() {}

virtual ~TestPcEvtHandler() {}

void OnConnection(int errCode, const std::string &message) override {}
};

void startPlay(IRtcxPcApi::Ptr pcapi, std::unordered_map<std::string, rtx::account::DeviceInfo> &iotIdDeviceMap) {
std::string iotId;
// 提示用户输入
std::cout << "input iotId for play: ";
std::cin >> iotId;
auto it = iotIdDeviceMap.find(iotId);
const rtx::account::DeviceInfo *deviceInfoPtr = it != iotIdDeviceMap.end() ? &it->second : nullptr;
if (deviceInfoPtr) {
if (deviceInfoPtr->status != rtx::account::DeviceStatus::ONLINE) {
std::cout << "device not online!!!" << std::endl;
return;
}
if (!g_windowThread) {
// 启动异步线程处理窗口和消息循环
g_windowThread = std::make_shared<std::thread>(AsyncWindowThread);
g_windowThread->detach();
}


// 等待窗口句柄初始化
while (!g_hwnd.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

ShowWindow(g_hwnd.load(), SW_SHOW);
UpdateWindow(g_hwnd.load());
// 获取窗口句柄并传递给渲染器
HWND renderHwnd = g_hwnd.load();
// 开始播放
pcapi->StartPlay(deviceInfoPtr->deviceName, deviceInfoPtr->productKey, renderHwnd);

}
}

int main()
{
// 初始换SDK,appKey和appSecret是平台分配给PC SDK,在系统中的唯一标识及对应的密钥
InitParam param;
param.appKey = "aaaaaaaa";
param.appSecret = "bbbbbbbbbbbbbbb";
IRtcxPcApi::Initialize(param);
std::shared_ptr<TestPcEvtHandler> handler = std::make_shared<TestPcEvtHandler>();
IRtcxPcApi::Ptr pcapi = IRtcxPcApi::CreateRtcxPcApi(handler);
// 用户登录
pcapi->Login("xxxx", "yyyyy");
std::unordered_map<std::string, rtx::account::DeviceInfo> iotIdDeviceMap;
while(1)
{
std::cout << "please select:" << std::endl
<< "1: get device list" << std::endl
<< "2: start play" << std::endl
<< "3: stop play" << std::endl
<< "100: quit" << std::endl;

int cmd = 0;
int quit = 0;
scanf("%d",&cmd);
switch(cmd) {
case 1:
{
// 获取设备列表,默认只获取第一页,所有列表需要自行遍历获取
rtx::account::DeviceListReq req;
std::shared_ptr<rtx::account::DeviceList> resp = pcapi->GetDeviceList(req);
if (resp && resp->total > 0)
{
std::cout << "total:" << resp->total << ", pageNo:" << resp->pageNo << ", pageSize:" << resp->pageSize << std::endl;
for (size_t i = 0; i < resp->data.size(); i++)
{
const rtx::account::DeviceInfo &deviceInfo = resp->data[i];
iotIdDeviceMap[deviceInfo.iotId] = deviceInfo;
std::cout << deviceInfo.toJsonString() << std::endl;
}
}
break;
}
case 2:
{
// 开始播放
startPlay(pcapi, iotIdDeviceMap);
break;
}
case 3:
{
// 停止播放
pcapi->StopPlay();
ShowWindow(g_hwnd.load(), SW_HIDE);

break;
}
case 100:
{
quit = 1;
break;
}
}
if (quit) {
break;
}
}
// 用户登出
pcapi->Logout();
pcapi = nullptr;
handler = nullptr;
IRtcxPcApi::Uninitialize();
g_msgLoopRunning.store(false);
DestroyWindow(g_hwnd.load());
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "leave pcsdk test" << std::endl;
return 0;
}