目录

实时流

设置实时流参数

初始化音频流配置

注意:如不指定,按系统默认配置;如开发者指定,则务必在发起会话前设置!

/**
 * @param[audioStreamConfig]: 音频流配置
 *     audioStreamConfig._type: 流类型,分为FEC_TYPE和ACK_TYPE(默认)
 *     audioStreamConfig._ackStreamWaitTimeMs: 流类型为ACK_TYPE时有效,表示发送数据后直到收到对方接收成功确认的等待时间,单位毫秒
 *     audioStreamConfig._isEncrypt: 是否加密
 */
 void initAudioStreamConfig(const RtsStreamConfig& audioStreamConfig);

 user->initAudioStreamConfig(RtsStreamConfig(ACK_TYPE, 50, false));
 ···
 user->dialCall(appAccount2);
初始化视频流配置

注意:如不指定,按系统默认配置;如开发者指定,则务必在发起会话前设置!

/**
 * @param[videoStreamConfig]: 视频流配置
 *     videoStreamConfig._type: 流类型,分为FEC_TYPE(默认)和ACK_TYPE
 *     videoStreamConfig._ackStreamWaitTimeMs: 流类型为ACK_TYPE时有效,表示发送数据后直到收到对方接收成功确认的等待时间,单位毫秒
 *     videoStreamConfig._isEncrypt: 是否加密
 */
 void initVideoStreamConfig(const RtsStreamConfig& videoStreamConfig);

 user->initVideoStreamConfig(RtsStreamConfig(FEC_TYPE, 50, false));
 ···
 user->dialCall(appAccount2);
设置发送缓存大小
/**
 * @param[size]: 实时流数据发送缓存大小,以消息个数为单位
 */
 void setSendBufferSize(int size);

 user->setSendBufferSize(10000);
获取发送缓存大小
/**
 * @return: 返回实时流数据发送缓存大小
 */
 int getSendBufferSize();

 int size = user->getSendBufferSize();
设置接收缓存大小
/**
 * @param[size]: 实时流数据接收缓存大小,以消息个数为单位
 */
 void setRecvBufferSize(int size);

 user->setRecvBufferSize(10000);
获取接收缓存大小
/**
 * @return: 返回实时流数据接收缓存大小
 */
 int getRecvBufferSize();

 int size = user->getRecvBufferSize();
获取发送缓存使用率
/**
 * @return: 返回实时流数据发送缓存使用率
 */
 float getSendBufferUsageRate();

 float usageRate = user->getSendBufferUsageRate();
获取接收缓存使用率
/**
 * @return: 返回实时流数据接收缓存使用率
 */
 float getRecvBufferUsageRate();

 float usageRate = user->getRecvBufferUsageRate();
清空发送缓存
/**
 * 清空发送缓存中所有待发送数据
 */
 void clearSendBuffer();

 user->clearSendBuffer();
清空接收缓存
/**
 * 清空接收缓存中所有已接收数据
 */
 void clearRecvBuffer();

 user->clearRecvBuffer();

发起会话

/**
 * @param[toAppAccount]: 接收方账号
 * @param[appContent]: 给接收方携带的业务自定义数据,默认为空
 * @param[toResource]: 用户设备的标识,默认不指定
 * @return: 通话ID,唯一标识当前流会话
 */
 uint64_t dialCall(const std::string& toAppAccount, const std::string& appContent = "", const std::string& toResource = "");

发送数据

/**
 * @param[callId]: 通话ID
 * @param[data]: 实时流数据,最大不超过512KB
 * @param[dataType]: 数据类型,音频:AUDIO,视频:VIDEO
 * @param[channelType]: 通道类型,RELAY、P2P_INTERNET、P2P_INTRANET,默认为RELAY
 * @param[ctx]:开发者自定义内容,默认为空
 * @param[canBeDropped]: 实时流数据是否允许丢失,true 允许,false 不允许,默认为false
 * @param[priority]: 实时流数据发送优先级,P0>P1>P2,默认为P1
 * @param[resendCount]: 实时流数据重发次数,默认为2
 * @return: 实时流数据ID,失败为-1
 */
 int sendRtsData(uint64_t callId, const std::string& data, const RtsDataType dataType, const RtsChannelType channelType = RELAY, const std::string& ctx = "", const bool canBeDropped = false, const DataPriority priority = P1, const unsigned int resendCount = 2);

关闭会话

/**
 * @param[callId]: 通话ID
 * @param[byeReason]: 描述信息
 */
 void closeCall(uint64_t callId, std::string byeReason = "");

实时流回调

class RTSCallEventHandler {
public:
   /**
    * 新会话接入回调
    * @param[callId]: 通话ID
    * @param[fromAccount]: 发起方账号
    * @param[appContent]: 发起方携带的业务自定义数据
    * @param[fromResource]: 用户设备的标识
    * @return: LaunchedResponse,成员accepted表示是否接受会话,成员desc携带描述信息
    */
    virtual LaunchedResponse onLaunched(uint64_t callId, const std::string fromAccount, const std::string appContent, const std::string fromResource) = 0;

   /**
    * 会话被接通回调
    * @param[callId]: 通话ID
    * @param[accepted]: 接收方同意ture,拒绝false
    * @param[desc]: 描述信息
    */
    virtual void onAnswered(uint64_t callId, bool accepted, const std::string desc) = 0;

   /**
    * 会话被关闭回调
    * @param[callId]: 通话ID
    * @param[desc]: 描述信息
    */
    virtual void onClosed(uint64_t callId, const std::string desc) = 0;

   /**
    * 接收到数据的回调
    * @param[callId]: 通话ID
    * @param[fromAccount]: 发送方账号
    * @param[resource]: 用户设备的标识
    * @param[data]: 实时流数据
    * @param[dataType]: 数据类型,AUDIO,VIDEO
    * @param[channelType]: 通道类型,RELAY、P2P_INTERNET、P2P_INTRANET
    */
    virtual void onData(uint64_t callId, const std::string fromAccount, const std::string resource, const std::string data, RtsDataType dataType, RtsChannelType channelType) = 0;

    /**
     * 发送数据成功的回调(仅当RtsStreamConfig._type为ACK_TYPE时回调)
     * @param[callId]: 通话ID
     * @param[dataId]: 实时流数据ID,和sendRtsData()返回值对应
     * @param[ctx]: 开发者自定义内容,和sendRtsData()传入的ctx对应
     */
    virtual void onSendDataSuccess(uint64_t callId, int dataId, const std::string ctx) = 0;

    /**
     * 发送数据失败的回调(仅当RtsStreamConfig._type为ACK_TYPE时回调)
     * @param[callId]: 通话ID
     * @param[dataId]: 实时流数据ID,和sendRtsData()返回值对应
     * @param[ctx]: 开发者自定义内容,和sendRtsData()传入的ctx对应
     */
    virtual void onSendDataFailure(uint64_t callId, int dataId, const std::string ctx) = 0;
};

#include <mimc/rts_callevent_handler.h>
#include <mimc/threadsafe_queue.h>
#include <XMDLoggerWrapper.h>
#include <test/rts_message_data.h>

class TestRTSCallEventHandler : public RTSCallEventHandler {
public:
    LaunchedResponse onLaunched(uint64_t callId, const std::string fromAccount, const std::string appContent, const std::string fromResource) {
        XMDLoggerWrapper::instance()->info("In onLaunched, callId is %llu, fromAccount is %s, appContent is %s, fromResource is %s", callId, fromAccount.c_str(), appContent.c_str(), fromResource.c_str());
        inviteRequests.push(RtsMessageData(callId, fromAccount, appContent, fromResource));
        if (this->appContent != "" && appContent != this->appContent) {
            return LaunchedResponse(false, LAUNCH_ERR_ILLEGALAPPCONTENT);
        }
        XMDLoggerWrapper::instance()->info("In onLaunched, appContent is equal to this->appContent");
        return LaunchedResponse(true, LAUNCH_OK);
    }

    void onAnswered(uint64_t callId, bool accepted, const std::string desc) {
        XMDLoggerWrapper::instance()->info("In onAnswered, callId is %llu, accepted is %d, desc is %s", callId, accepted, desc.c_str());
        createResponses.push(RtsMessageData(callId, desc, accepted));
    }

    void onClosed(uint64_t callId, const std::string desc) {
        XMDLoggerWrapper::instance()->info("In onClosed, callId is %llu, desc is %s", callId, desc.c_str());
        byes.push(RtsMessageData(callId, desc));
    }

    void onData(uint64_t callId, const std::string fromAccount, const std::string resource, const std::string data, RtsDataType dataType, RtsChannelType channelType) {
        XMDLoggerWrapper::instance()->info("In handleData, callId is %llu, fromAccount is %s, resource is %s, dataLen is %d, data is %s, dataType is %d", callId, fromAccount.c_str(), resource.c_str(), data.length(), data.c_str(), dataType);
        recvDatas.push(RtsMessageData(callId, data, dataType, channelType));
    }

    void onSendDataSuccess(uint64_t callId, int dataId, const std::string ctx) {
        XMDLoggerWrapper::instance()->info("In handleSendDataSucc, callId is %llu, dataId is %d, ctx is %s", callId, dataId, ctx.c_str());
    }

    void onSendDataFailure(uint64_t callId, int dataId, const std::string ctx) {
        XMDLoggerWrapper::instance()->warn("In handleSendDataFail, callId is %llu, dataId is %d, ctx is %s", callId, dataId, ctx.c_str());
    }

    const std::string& getAppContent() {return this->appContent;}

    RtsMessageData* pollInviteRequest(long timeout_s) {
        RtsMessageData* inviteRequestPtr;
        inviteRequests.pop(timeout_s, &inviteRequestPtr);
        return inviteRequestPtr;
    }

    RtsMessageData* pollCreateResponse(long timeout_s) {
        RtsMessageData* createResponsePtr;
        createResponses.pop(timeout_s, &createResponsePtr);
        return createResponsePtr;
    }

    RtsMessageData* pollBye(long timeout_s) {
        RtsMessageData* byePtr;
        byes.pop(timeout_s, &byePtr);
        return byePtr;
    }

    RtsMessageData* pollData(long timeout_s) {
        RtsMessageData* recvDataPtr;
        recvDatas.pop(timeout_s, &recvDataPtr);
        return recvDataPtr;
    }

    int getDataSize() {
        return recvDatas.size();
    }

    void clear() {
        inviteRequests.clear();
        createResponses.clear();
        byes.clear();
        recvDatas.clear();
    }

    TestRTSCallEventHandler(std::string appContent) {
        this->appContent = appContent;
    }
    TestRTSCallEventHandler() {}
public:
    const std::string LAUNCH_OK = "OK";
    const std::string LAUNCH_ERR_ILLEGALAPPCONTENT = "ILLEGALAPPCONTENT";
private:
    std::string appContent;
    ThreadSafeQueue<RtsMessageData> inviteRequests;
    ThreadSafeQueue<RtsMessageData> createResponses;
    ThreadSafeQueue<RtsMessageData> byes;
    ThreadSafeQueue<RtsMessageData> recvDatas;
};

TestRTSCallEventHandler* callEventHandler = new TestRTSCallEventHandler();
user->registerRTSCallEventHandler(callEventHandler);

回到顶部

results matching ""

    No results matching ""