CloudHub 文档中心
下载中心 文档中心

RTS 实时流直播


快速集成 SDK

本页介绍如何在windows和MAC平台上,设置环境以及集成 CloudHubRTSSDK。

准备环境

下载最新的 CloudHubRTSSDK 软件包。

windows版软件包内容如下:

文件描述
CloudHubBase.h基础结构体和枚举值
CloudHubBaseInternal.h依赖结构体的定义
CloudhubRts.hCloudHubRTSSDK的主要接口文件
rtssdk.dllwindows版快直播库,分为x86和x64版
rtssdk.dll.libwindows版快直播函数调用库文件,分为x86和x64版

MAC版软件包内容如下:

文件描述
CloudHubBase.h基础结构体和枚举值
CloudHubBaseInternal.h依赖结构体的定义
CloudhubRts.hCloudHubRTSSDK的主要接口文件
rtssdk.dylibMAC版快直播库

集成步骤——推流

1. 创建上行流

在调用其他 API 前,需要创建并初始化 IUpStream 对象。

在此之前,需要申请 APPID ,请参考 创建 CloudHub 账号并获取 AppId

初始化 IUpStream 时需要提供一个回调事件处理器,关于事件的具体信息,请参考 概览

class UpStreamEventHandler: public IUpStreamEventHandler {
 public:
  virtual void onError(cloudhub::IUpStream* stream, int code, const char* msg) {
    printf("\n UpStream onError(%d) \n", code);
  }
  
  virtual void onCaptureFirstAudioFrame(cloudhub::IUpStream* stream) {
    printf("\n UpStream onCaptureFirstAudioFrame \n");
  }

  virtual void onCaptureFirstVideoFrame(cloudhub::IUpStream* stream,
                                        int width,
                                        int height,
                                        const char* sourceID="") {
    printf("\n UpStream onCaptureFirstVideoFrame(%d, %d, %s) \n", width, height, sourceID);
  }

  virtual void onPushStatusUpdate(cloudhub::IUpStream* stream,
                                  CONNECTION_STATE_TYPE type) {
    printf("\n UpStream onPushStatusUpdate(%d) \n", (int)type);
  }

  virtual void onLocalVideoStateChanged(
      cloudhub::IUpStream* stream,
      cloudhub::LOCAL_VIDEO_STREAM_STATE localVideoState,
      cloudhub::LOCAL_VIDEO_STREAM_ERROR error){
    printf("\n UpStream onLocalVideoStateChanged(%d, %d) \n", (int)localVideoState, (int)error);
  }

  virtual void onLocalAudioStateChanged(
      cloudhub::IUpStream* stream,
      cloudhub::LOCAL_AUDIO_STREAM_STATE state,
      cloudhub::LOCAL_AUDIO_STREAM_ERROR error) {
    printf("\n UpStream onLocalVideoStateChanged(%d, %d) \n", (int)localVideoState, (int)error);
  }

  virtual void onStatisticsUpdate(cloudhub::IUpStream* stream,
                                  cloudhub::RtcStats& stats) {

  }

  virtual void onAudioVolumeIndication(cloudhub::IUpStream* stream,
                                       int volume) {

  }

  virtual void onVideoDeviceStateChanged(
      const char* deviceId,
      cloudhub::MEDIA_DEVICE_STATE_TYPE deviceState) {
    printf("\n UpStream onVideoDeviceStateChanged(%s, %d) \n", deviceId, (int)deviceState);
  }

  virtual void onAudioDeviceStateChanged(
      const char* deviceId,
      cloudhub::MEDIA_DEVICE_TYPE deviceType,
      cloudhub::MEDIA_DEVICE_STATE_TYPE deviceState) {
    printf("\n UpStream onAudioDeviceStateChanged(%s, %d, %d) \n", deviceId, (int)deviceType, (int)deviceState);
  }

  virtual ~UpStreamEventHandler() {}
};
const char* const appId = ""; /* using your appId */
UpStreamEventHandler handler;

cloudhub::IUpStream* const upStream = createUpStream(appId, &handler);
if (upStream == NULL)
{
    printf(" Error! Create UpStream failed. \n");
    return;
}

2. 设置音视频设备

upStream->startCamera();
upStream->startMicrophone();

3. 本地预览

观看自己的视频:

engine->setRenderView(hwnd);

4. 开始推流

const char* url = ""; /* using your url */
const int ret = upStream->startPush(url);
if (ret != ERR_OK)
{
    printf(" Error! startPush() failed. ret = %d \n", ret);
}

5. 结束推流

根据业务需求,调用 stopPush 结束推流,然后释放上行流。

upStream->stopPush();
deleteUpStream(upStream);

集成步骤——拉流

1. 创建下行流

在调用其他 API 前,需要创建 IDownStream 对象。

在此之前,需要申请 APPID ,请参考 创建 CloudHub 账号并获取 AppId

初始化 IDownStream 时需要提供一个回调事件处理器,关于事件的具体信息,请参考 概览

class DownStreamEventHandler: public IDownStreamEventHandler {
 public:
  virtual void onError(cloudhub::IDownStream* stream,
                       int code,
                       const char* msg) {
    printf("\n DownStream onError(%d) \n", code);
  }
  
  virtual void onFirstRemoteAudioFrame(cloudhub::IDownStream* stream) {
    printf("\n DownStream onFirstRemoteAudioFrame \n");
  }

  virtual void onFirstRemoteVideoFrame(cloudhub::IDownStream* stream,
                                       int width,
                                       int height){
    printf("\n DownStream onFirstRemoteVideoFrame(%d, %d) \n", width, height);
  }

  virtual void onRemoteVideoSizeChange(cloudhub::IDownStream* stream,int width,int height) {
    printf("\n DownStream onRemoteVideoSizeChange(%d, %d) \n", width, height);
  }

  virtual void onPullStatusUpdate(cloudhub::IDownStream* stream,
                                  CONNECTION_STATE_TYPE type) {
    printf("\n DownStream onPullStatusUpdate(%d) \n",(int)type);
  }

  virtual void onRemoteVideoStateChanged(cloudhub::IDownStream* stream,
                                         RTS_VIDEO_STATE state) {
    printf("\n DownStream onRemoteVideoStateChanged(%d) \n",(int)state);
  }

  virtual void onRemoteAudioStateChanged(cloudhub::IDownStream* stream,
                                         RTS_AUDIO_STATE state) {
    printf("\n DownStream onRemoteAudioStateChanged(%d) \n",(int)state);
  }

  virtual void onStatisticsUpdate(cloudhub::IDownStream* stream,
                                  cloudhub::RtcStats& stats) {

  }

  virtual void onAudioVolumeIndication(cloudhub::IDownStream* stream,
                                       int volume) {

  }

  virtual ~DownStreamEventHandler() {}
};
const char* const appId = ""; /* using your appId */
DownStreamEventHandler handler;

cloudhub::IDownStream* const downStream = createDownStream(appId, &handler);
if (downStream == NULL)
{
    printf(" Error! Create downStream failed. \n");
    return;
}

2. 视频播放

观看自己的视频:

downStream->setRenderView(hwnd);

3. 开始拉流

const char* url = ""; /* using your url */
const int ret = downStream->startPull(url);
if (ret != ERR_OK)
{
    printf(" Error! startPull() failed. ret = %d \n", ret);
}

4. 结束拉流

根据业务需求,调用 stopPull 结束拉流,然后释放下行流。

downStream->stopPull();
deleteDownStream(upStream);