上篇介绍了一些基本的信息:域网webrtc 一对一音视频通讯(一)
直接上手demo,通讯有两个点一个是SDP的交换
1.前端的一个数据类型一个offer、一个answer
const offer_desc1={
"type": "offer",
"sdp": desc_pc1.sdp
};
var test_desc2={
"type": "answer",
"sdp": desc_pc2.sdp
};
2.SDP信息使用websocket进行数数据转发
首先channel和用户绑定;
然后通过channel进行转发;
简单的消息类型;
private String cmd;//指令:1-绑定
private String fromUser;
private String acceptUsr;
private String msg;//信息
//只做消息转发
if("offer".equals(message.getCmd())||
"answer".equals(message.getCmd())||
"candidate".equals(message.getCmd())){
Channel channel=ChannelManager.getChannelByCode(message.getAcceptUsr());
if(null!=channel) {
channel.writeAndFlush(new TextWebSocketFrame(mapper.writeValueAsString(message)));
}else {
logger.info("------对方用户:{}未登录",message.getAcceptUsr());
}
}
ICE Candidate的交互
网络信息交互的。看是否是P2P还是需要中继服务器转发信息。
传递的交互地址通过本地网卡和turn/stun服务器解决
let configuration = {};
configuration = { iceServers: [{
urls:['stun:stun.minisipserver.com',
'stun:stun.zoiper.com',
'stun:stun.voipbuster.com']}]};
//源连接,
pc1 = new RTCPeerConnection(configuration);
信息格式,信息传递可以通过websocket转发
{
"candidate": "candidate:foundation icegroupid type priority ip port typ host generation x ufrag xxxx network-cost 999",
"sdpMid": "0",
"sdpMLineIndex": 0
}
{
"candidate": "candidate:545662605 1 udp 2113937151 a8e7c788-6636-4392-b5af-08253ad10ee5.local 51579 typ host generation 0 ufrag 0XDd network-cost 999",
"sdpMid": "0",
"sdpMLineIndex": 0
}
- candidate:ICE候选
- - foundation:用于标志和区分来自同一个stun的不同的候选者,ID标识
- icegroupid:ICE的组ID
- type:协议类型
- priority:优先级
- ip:ip地址
- port:端口
- typ:标识后面字段的属性类型是候选类型
- - host:本地接口获取到的candidate
- srflx:NAT网关在公网侧的IP地址,通过STUN或者TURN收集(server reflexive candidate)
- prflx:可以在ICE的后续阶段中获取到(peer reflexive candidate)
- relay:TURN服务器的公网转发地址,通过TURN收集
- generation x:表明当前是第几代的候选
- ufrag xxxx:分配的用户名标识
- network-cost xxx:网卡标识
国内一些能用免费的 stun服务器
或者自己搭建个coturn服务
stun.minisipserver.com
stun.zoiper.com
stun.voipbuster.com
stun.sipgate.net
stun.schlund.de
stun.voipstunt.com
stun.1und1.de
stun.gmx.net
stun.callwithus.com
stun.internetcalls.com
stun.voip.aebc.com
stun.internetcalls.com
stun.callwithus.com
stun.gmx.net
stun.1und1.de
stun.voxgratia.org
浏览器查看webrtc的信息: chrome://webrtc-internals/
效果
转发
发起
观看