metaRTC5.0 API编程指南(一)

概述

metaRTC5.0版本 API进行了重构,本篇文章将介绍webrtc传输调用流程和例子。

metaRTC5.0版本提供了C++和纯C两种接口。

纯C接口YangPeerConnection

头文件:include/yangrtc/YangPeerConnection.h

typedef struct{
	void* conn;
	YangAVInfo* avinfo;
	YangStreamConfig streamconfig;
}YangPeer;

typedef struct {
	YangPeer peer;
//初始化
	void (*init)(YangPeer* peer);

//生成本端sdp
	int32_t (*createOffer)(YangPeer* peer, char **psdp);
//交换sdp时,获取对端sdp后,生成本地sdp以回传回对端
	int32_t (*createAnswer)(YangPeer* peer,char* answer);
//http侦听信令侦听到请求后,生成http的answer sdp
	int32_t (*createHttpAnswer)(YangPeer* peer,char* answer);
//启动metaRTC开始webrtc
	int32_t (*setRemoteDescription)(YangPeer* peer,char* sdp);
//封装了srs/zlm的信令交换并启动metaRTC
	int32_t (*connectSfuServer)(YangPeer* peer);
//关闭metartc
	int32_t (*close)(YangPeer* peer);
//是否活连接
	int32_t (*isAlive)(YangPeer* peer);
//是否已经连接
	int32_t (*isConnected)(YangPeer* peer);
//推音频流
	int32_t (*on_audio)(YangPeer* peer,YangFrame* audioFrame);
//推视频流
	int32_t (*on_video)(YangPeer* peer,YangFrame* videoFrame);
//推送datachannel消息
	int32_t (*on_message)(YangPeer* peer,YangFrame* msgFrame);
//发送rtc系统消息
	int32_t (*sendRtcMessage)(YangPeer* peer, YangRtcMessageType mess);
}YangPeerConnection;

C++接口YangPeerConnection2

头文件:include/yangrtc/YangPeerConnection2.h

class YangPeerConnection2 {
public:
	YangPeerConnection2(YangAVInfo* avinfo,YangStreamConfig* streamConfig);
	virtual ~YangPeerConnection2();
	YangStreamConfig* streamConfig;
public:
//初始化
	void init();

//生成本端sdp
	int32_t createOffer( char **psdp);
//交换sdp时,获取对端sdp后,生成本地sdp以回传回对端
	int32_t createAnswer(char* answer);
//http侦听信令侦听到请求后,生成http的answer sdp
	int32_t createHttpAnswer(char* answer);
//启动metaRTC开始webrtc
	int32_t setRemoteDescription(char* sdp);
//封装了srs/zlm的信令交换并启动metaRTC
	int32_t connectSfuServer();
//关闭metartc
	int32_t close();
//是否活连接
	int32_t isAlive();
//是否已经连接
	int32_t isConnected();
//推音频流
	int32_t on_audio(YangFrame* audioFrame);
//推视频流
	int32_t on_video(YangFrame* videoFrame);
//推送datachannel消息
	int32_t on_message(YangFrame* msgFrame);
//发送rtc系统消息
	int32_t sendRtcMessage( YangRtcMessageType mess);

private:
	YangPeerConnection m_conn;
};

接口调用流程及例子

配置参数

参数在YangStreamConfig和YangAVInfo两个struct中
libmetartc5/src/yangp2p/YangP2pRtc.cpp中代码样例

void g_p2p_rtcrecv_sendData(void* context,YangFrame* frame){
	YangP2pRtc* rtcHandle=(YangP2pRtc*)context;
    rtcHandle->publishMsg(frame);
}
void g_p2p_rtcrecv_sslAlert(void* context,int32_t uid,char* type,char* desc){
	if(context==NULL||type==NULL||desc==NULL) return;
	YangP2pRtc* rtc=(YangP2pRtc*)context;

	if(strcmp(type,"warning")==0&&strcmp(desc,"CN")==0){
		rtc->removePeer(uid);
	}
}
void g_p2p_rtcrecv_receiveAudio(void* user,YangFrame *audioFrame){
	if(user==NULL || audioFrame==NULL) return;
	YangP2pRtc* rtcHandle=(YangP2pRtc*)user;
	rtcHandle->receiveAudio(audioFrame);
}
void g_p2p_rtcrecv_receiveVideo(void* user,YangFrame *videoFrame){
	if(user==NULL || videoFrame==NULL) return;
    YangP2pRtc* rtcHandle=(YangP2pRtc*)user;
	rtcHandle->receiveVideo(videoFrame);
}

void g_p2p_rtcrecv_receiveMsg(void* user,YangFrame *msgFrame){
	if(user==NULL) return;

	YangP2pRtc* rtcHandle=(YangP2pRtc*)user;
	rtcHandle->receiveMsg(msgFrame);
}

int32_t YangP2pRtc::connectPeer(int32_t nettype, string server,int32_t localPort,int32_t pport,string app,string stream) {
	int32_t ret = 0;
	YangPeerConnection* sh=(YangPeerConnection*)calloc(sizeof(YangPeerConnection),1);

	strcpy(sh->peer.streamconfig.app,app.c_str());
	sh->peer.streamconfig.streamOptType=Yang_Stream_Both;

	strcpy(sh->peer.streamconfig.remoteIp,server.c_str());
	sh->peer.streamconfig.remotePort=pport;

	m_clientUid=m_uidSeq++;
	strcpy(sh->peer.streamconfig.stream,stream.c_str());
	sh->peer.streamconfig.uid=m_clientUid;

	sh->peer.streamconfig.isServer=0;
	sh->peer.streamconfig.localPort=localPort;

	sh->peer.streamconfig.recvCallback.context=this;
	sh->peer.streamconfig.recvCallback.receiveAudio=g_p2p_rtcrecv_receiveAudio;
	sh->peer.streamconfig.recvCallback.receiveVideo=g_p2p_rtcrecv_receiveVideo;
	sh->peer.streamconfig.recvCallback.receiveMsg=g_p2p_rtcrecv_receiveMsg;
	
}

纯C接口调用例子

YangPeerConnection* sh=(YangPeerConnection*)calloc(sizeof(YangPeerConnection),1);
....配置参数..
//将参数传入YangPeerConnection	memcpy(&sh->peer.streamconfig.rtcCallback,&m_context->rtcCallback,sizeof(YangRtcCallback));
	sh->peer.avinfo=&m_context->avinfo;

//1.初始化
	yang_create_peerConnection(sh);
    sh->init(&sh->peer);
//2.生成本端sdp,srs和zlm调用不需要这一步,p2p需要
	char* localSdp;
	char* remoteSdp=(char*)calloc(12*1000,1);

	sh->createOffer(&sh->peer, &localSdp);

//3.取得对端或者sfu服务器的sdp后,启动metartc
   //点对点调用
    ret=sh->setRemoteDescription(&sh->peer,remoteSdp);
   //srs/zlm调用,里面已经封装了sdp交换和metartc启动
    ret=sh->connectSfuServer(&sh->peer)

//4.执行程序后,销毁对象
		sh->close(&sh->peer);
		yang_destroy_peerConnection(sh);
		yang_free(sh);

C++接口调用例子

....参数配置..
//1.初始化
YangPeerConnection2* sh=new YangPeerConnection2(&m_context->avinfo,&streamconfig);
sh->init();

//2.生成本端sdp,注意:srs和zlm调用不需要这一步   
	char* localSdp;
	char* remoteSdp=(char*)calloc(12*1000,1);
	sh->createOffer(&localSdp);

//3.取得对端或者sfu服务器的sdp后,启动metartc
//srs/zlm调用
    ret = sh->connectSfuServer();
//p2p调用
    ret=sh->setRemoteDescription(remoteSdp);

//4.执行程序后,销毁对象
        sh->close();
		yang_delete(sh);

ICE设置

iceCandidateType参数可以在配置文件中配置,也可以在程序中赋值。

typedef enum{

        YangIceHost,

        YangIceStun,

        YangIceTurn

}YangIceCandidateType;

[rtc]
usingDatachannel=0
iceServerIP=10.42.0.1
iceUsingLocalIp=0
iceLocalIP=192.168.1.102
iceCandidateType=0  //0:host 1:stun 2:turn
iceStunPort=3478
iceServerIP=10.42.0.171
iceUserName=metartc
icePassword=metartc
audioQueueCount=5
vidoeQueueCount=5

你可能感兴趣的:(metaRTC,c++,c语言,webrtc)