快速集成 SDK
本页介绍如何在windows和MAC平台上,设置环境以及集成 CloudHubRTCEngineSDK。
准备环境
下载最新的 CloudHubRTCEngineSDK 软件包。
windows版软件包内容如下:
文件 | 描述 |
---|---|
CloudHubBase.h | 基础结构体和枚举值 |
CloudHubBaseInternal.h | 依赖结构体的定义 |
ICloudHubRtcEngine.h | 音视频引擎的接口类 |
roomsdk.dll | windows版音视频引擎库,分为x86和x64版 |
roomsdk.dll.lib | windows版音视频引擎函数调用库文件,分为x86和x64版 |
MAC版软件包内容如下:
文件 | 描述 |
---|---|
CloudHubBase.h | 基础结构体和枚举值 |
CloudHubBaseInternal.h | 依赖结构体的定义 |
ICloudHubRtcEngine.h | 音视频引擎的接口类 |
roomsdk.dylib | MAC版音视频引擎库 |
集成步骤
1. 初始化 CloudHubRTCEngine
在调用其他 API 前,需要创建并初始化 CloudHubRTCEngine 对象。
在此之前,需要申请 APPID ,请参考 创建 CloudHub 账号并获取 AppId。
初始化 CloudHubRTCEngine 时需要提供一个回调事件处理器,关于事件的具体信息,请参考 概览。
class EventHandler : public IRtcEngineEventHandler
{
public:
virtual ~IRtcEngineEventHandler() {}
virtual void onConnectionStateChanged(CONNECTION_STATE_TYPE state)
{
printf("\n EventHandler::onConnectionStateChanged(%d) \n", state);
}
virtual void onConnectionLost()
{
printf("\n EventHandler::onConnectionLost() \n");
}
virtual void onJoinChannelSuccess(const char* channel,
const char* uid,
int elapsed)
{
printf("\n EventHandler::onJoinChannelSuccess(%s, %s) \n",
channelId, uid);
}
virtual void onRejoinChannelSuccess(const char* channel,
const char* uid,
int elapsed)
{
printf("\n EventHandler::onRejoinChannelSuccess(%s, %s) \n",
channelId, uid);
}
virtual void onUserJoined(const char* uid,
const char* properties,
bool isHistory,
const char* fromChannel)
{
printf("\n EventHandler::onUserJoined(%s) \n", uid);
}
virtual void onUserOffline(const char* uid)
{
printf("\n EventHandler::onUserOffline(%s) \n", uid);
}
virtual void onLeaveChannel(const RtcStats& stats)
{
printf("\n EventHandler::onLeaveChannel() \n");
}
virtual void onLocalVideoStateChanged(
LOCAL_VIDEO_STREAM_STATE localVideoState,
LOCAL_VIDEO_STREAM_ERROR error)
{
printf("\n EventHandler::onLocalVideoStateChanged(%d, %d) \n",
localVideoState, error);
}
virtual void onRemoteVideoStateChanged(const char* uid,
MEDIA_TYPE mediaType,
REMOTE_VIDEO_STATE state,
REMOTE_VIDEO_STATE_REASON reason)
{
printf("\n EventHandler::onRemoteVideoStateChanged(%s, %d, %d, %d) \n",
uid, mediaType, state, reason);
}
virtual void onError(int err, const char* msg)
{
printf("\n EventHandler::onError(%d, %s) \n", err, msg);
}
};
const char* const appId = ""; /* using your appId */
EventHandler* handler = new EventHandler();
RtcEngineContext context;
context.eventHandler = handler;
context.appId = appId;
cloudhub::IRtcEngine* const engine = createRtcEngine();
if (engine == NULL)
{
printf(" Error! Create engine failed. \n");
return (-1);
}
engine->initialize(context);
2. 设置视频相关配置
engine->enableAudio(true);
engine->enableLocalAudio(true);
engine->enableVideo(true);
engine->enableLocalVideo(true);
3. 加入频道
const ERROR_CODE ret = engine->joinChannel(token, channelId, NULL, NULL);
if (ret != ERR_OK)
{
printf(" Error! joinChannel() failed. ret = %d \n", ret);
return (-1);
}
4. 观看视频
观看自己的视频:
VideoCanvas canvas;
canvas.view = hwnd;
engine->setupLocalVideo(canvas);
engine->startPreview();
当其他用户发布视频时,观看其视频;当其他用户停止发布视频时,需要停止观看。可以在 EventHandler 中的 onRemoteVideoStateChanged 回调函数里添加如下代码:
void onRemoteVideoStateChanged(const char* uid,
MEDIA_TYPE mediaType,
REMOTE_VIDEO_STATE state,
REMOTE_VIDEO_STATE_REASON reason)
{
if (state == REMOTE_VIDEO_STATE_STARTING
&& (reason == REMOTE_VIDEO_STATE_REASON_ADD_REMOTESTREAM
|| reason==REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED))
{
HWND hwnd = 0;// get a window and store it in hwnd
VideoCanvas canvas;
strcpy(canvas.uid, uid);
canvas.view = hwnd;
canvas.mediaType = mediaType;
m_pRoomSdk->setupRemoteVideo(canvas);
}
else if (REMOTE_VIDEO_STATE_STOPPED == state
&& (reason == REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED
|| reason == REMOTE_VIDEO_STATE_REASON_REMOVE_REMOTESTREAM))
{
VideoCanvas canvas;
strncpy(canvas.uid, uid, strlen(uid));
canvas.view = 0;// canvas.view = 0 means to remove the view
canvas.mediaType = mediaType;
m_pRoomSdk->setupRemoteVideo(canvas);
}
}
5. 离开频道
根据业务需求,调用 leaveChannel
离开当前频道。
engine->leaveChannel();