Skip to content

metaRTC5.0 API Sample

yangrtc edited this page Aug 10, 2022 · 12 revisions

接口调用流程及例子

配置参数

参数在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接口调用例子(pure C sample)

YangPeerConnection* sh=(YangPeerConnection*)calloc(sizeof(YangPeerConnection),1);  
....配置参数..  
//将参数传入  
	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++接口调用例子(C++ sample)

....参数配置..
//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);