CloudHub Docs
Download Documents

RTC


Fast integration SDK

This page describes how to set up the environment and integrate the CloudHubRTCEngineSDK on windows and MAC platforms.

Prepare the environment

Download the latest CloudHubRTCEngineSDK package.

The windows package is as follows:

FileDescription
CloudHubBase.hStructs and Enums
CloudHubBaseInternal.hThe definition of a dependent structure
ICloudHubRtcEngine.hThe main interface file for CloudHubRTCEngineSDK
roomsdk.dllWindows RTC, divided into x86 and x64
roomsdk.dll.libThe windows version of RTC function calls the library file, including x86 and x64

The Mac package is as follows:

FileDescription
CloudHubBase.hStructs and Enums
CloudHubBaseInternal.hThe definition of a dependent structure
ICloudHubRtcEngine.hThe main interface file for CloudHubRTCEngineSDK
roomsdk.dylibMAC RTC

Integrate the CloudHub SDK

1. Initialize CloudHubRTCEngine

Before you can call another API, you need to create and initialize the CloudHubRTCEngine object.

First, an APPID is required , See Gets an AppId

A callback event processor is required to initialize CloudHubRTCEngine. See Overview

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. Set the audio and video

engine->enableAudio(true);
engine->enableLocalAudio(true);
engine->enableVideo(true);
engine->enableLocalVideo(true);

3. Join channel

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. Video playback

Watch your own video:

VideoCanvas canvas;
canvas.view = hwnd;
engine->setupLocalVideo(canvas);
engine->startPreview();

Watch videos when other users publish them, and stop when other users stop publishing videos.You can add the code in the 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. Leave channel

Call leaveChannel to leave the channel.

engine->leaveChannel();