概述
音视频通信时为了节省服务器带宽,通信效率最好的就是p2p,但是在现实的应用场景中,我们的客户端都是在大局域网内部的,stun协议就是要解决如何穿透这些局域网实现p2p。
metaRTC5.0最新版本支持stun,使p2p在互联网上利用网络穿透技术实现通信。
下载源码
https://github.com/metartc/metaRTC/releases/tag/5.0.022https://github.com/metartc/metaRTC/releases/tag/5.0.022https://gitee.com/metartc/metaRTC/releases/5.0.022https://gitee.com/metartc/metaRTC/releases/5.0.022
?metaRTC5.0编程
metaRTC5.0 p2p stun编程有两种模式:
- websockt+stun
- http+frpc(ngrok)+stun
配置参数
代码配置参数
strcpy(m_context->avinfo.rtc.iceServerIP,"182.92.163.143"); //stun服务器地址
m_context->avinfo.rtc.iceStunPort=3478; //stun服务器端口
m_context->avinfo.rtc.hasIceServer=1; //1:启用stun 0:关闭stun
或者在配置文件yang_config.ini中配置
[rtc]
hasIceServer=1
usingDatachannel=1
iceStunPort=3478
iceServerIP=182.92.163.143
audioQueueCount=5
vidoeQueueCount=5
模式一(websockt+stun)
代码在libmetartc5/src/yangp2p/YangP2pRtcIce.cpp中
//发起连接到对端
int32_t YangP2pRtcIce::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);
....配置参数.....
//初始化YangPeerConnection
yang_create_peerConnection(sh);
sh->init(&sh->peer);
char* localSdp; //本端sdp
char* remoteSdp=(char*)calloc(12*1000,1); //对端sdp
//向stun服务器发送stun request取得外网地址和端口
if(m_context->avinfo.rtc.hasIceServer){
if(sh->requestStunServer(&sh->peer)!=Yang_Ok) yang_error("request stun server fail!");
}
//取得本端sdp
sh->createOffer(&sh->peer, &localSdp);
将sdp通过信令服务传输到对端
//取得对端sdp后,启动metartc
ret=sh->setRemoteDescription(&sh->peer,remoteSdp);
}
//取得对端sdp后, 调用sh->createAnswer(&sh->peer,answer);
//取得answer后,通过信令服务传会对端
int32_t YangP2pRtcIce::addPeer(char* remotesdp,char* answer,char* remoteIp,int32_t localPort,int* phasplay) {
配置参数...
//初始化YangPeerConnection
yang_create_peerConnection(sh);
sh->init(&sh->peer);
//向stun服务器发送stun request取得外网地址和端口
if(m_context->avinfo.rtc.hasIceServer){
if(sh->requestStunServer(&sh->peer)!=Yang_Ok) yang_error("request stun server fail!");
}
//取得answer Sdp
ret = sh->createAnswer(&sh->peer,answer);
....取得answer后,通过信令服务传会对端
//取得对端sdp后,启动metartc
ret = sh->setRemoteDescription(&sh->peer,remotesdp);
.....
}
模式二(http+frpc(ngrok)+stun)
在原来metap2p5的demo代码中修改,修改文件libmetartc5/src/yangp2p/YangP2pRtc.cpp
通过frpc(ngrok)等映射http,用浏览器或者客户端直接访问外网映射地址即可,操作模式和原来模式不变。
|