概述
音视频通信时为了节省服务器带宽,通信效率最好的就是p2p,但是在现实的应用场景中,我们的客户端都是在大局域网内部的,stun协议就是要解决如何穿透这些局域网实现p2p。但在有些网络中不支持穿透就需要使用TURN或者SFU进行转发。
metaRTC5.0新版实现对TURN支持。metaRTC5.0提供有metaRTC客户端和浏览器TURN的demo。
源码下载
GitHub - metartc/metaRTC: A cross-platform WebRTC SDKA cross-platform WebRTC SDK. Contribute to metartc/metaRTC development by creating an account on GitHub.https://github.com/metartc/metaRTCmetaRTC: metaRTC为纯C版本的webrtc 可与谷歌webrtc类库互通,支持Webrtc/SRT/RTMPhttps://gitee.com/metartc/metaRTC
浏览器TURN
如果浏览器使用turn,如ipc等客户端则不需要配置
在metaipc5_html中js/srs.sdk.p2p.js文件中,用下面代码替换280行代码
const config = {
bundlePolicy: 'balanced',
// certificates?: RTCCertificate[];
// iceCandidatePoolSize?: number;
iceTransportPolicy: "relay",// all
rtcpMuxPolicy : 'negotiate',
iceServers: [
{
urls: "turn:192.168.1.102:3478",
username: "metartc",
credential: "metartc"
}
]
};
self.pc = new RTCPeerConnection(config);
metaRTC Turn配置
配置文件yang_config.ini中
iceCandidateType参数可以在配置文件中配置,也可以在程序中赋值。
typedef enum{
YangIceHost,
YangIceStun,
YangIceTurn
}YangIceCandidateType;
配置参数后metaRTC会根据参数进行ice处理。
coturn配置
编辑配置文件/etc/turnserver.conf
# Uncomment if extra security is desired,
# with nonce value having a limited lifetime.
# The nonce value is unique for a session.
# Set this option to limit the nonce lifetime.
# Set it to 0 for unlimited lifetime.
# It defaults to 600 secs (10 min) if no value is provided. After that delay,
# the client will get 438 error and will have to re-authenticate itself.
去掉#stale-nonce=600注释
改为 stale-nonce = 0
#设置用户名密码 user=username2:password2
user=metartc:metartc
#设置realm
realm=metartc.cn
#设置侦听端口,默认为3478
listening-port=3478