WebRTC之PeerConnection的建立过程
2016-01-05 19:00
423 查看
基于第三方webrtc开源平台开发视频会议难度不是很大,主要是业务方面的问题。但是,一旦涉及核心的底层问题就需要阅读源代码,找出bug了,难度不小。
项目需要,分析了一下peerconnection的创建过程。
假设clientA,clientB分为为offer和answer.
![](http://images2015.cnblogs.com/blog/778894/201601/778894-20160105185733668-1519668935.png)
Offer端
pc =new RTCPeerConnection(null);
pc.onicecandidate=handleIceCandidate;
pc.onaddstream=handleRemoteStreamAdded;
pc.onremovestream=handleRemoteStreamRemoved;
pc.addStream(localStream);
pc.createOffer(setLocalAndSend,handleCreateOfferError);
function
handleIceCandidate(event){
//将本地产生的candidate发送给对方
sendMessage({
type:”candidate”;
candidate:event.candidate.candidate;
……;
});
}
function
setLocalAndSend(sdp){
pc.setLocalDescription(sdp);//设置本地sdp,完成设置后onicecandidate事件会调用。
sendMessage(sdp);//将offer发送给对方
}
当offer提供端接收到来自对方的answer时:
pc.setRemoteDescription(new RTCSessionDescription(message));
当offer端接收到来自对方的candidate时,pc.addIceCandidate(candidate);//将来自对方的candidate设置给本地
2.
Answer端的代码与offer端类似,红色部分代码不同
pc =new
RTCPeerConnection(null);
pc.onicecandidate=handleIceCandidate;
pc.onaddstream=handleRemoteStreamAdded;
pc.onremovestream=handleRemoteStreamRemoved;
pc.addStream(localStream);
注意区别:offer端是主动调用createOffer函数并将offer发送给对方。Answer端接受到offer后,才会创建peerConnection等一系列操作:
pc.setRemoteDescription(sdp);//设置接收到的远端offer
pc.createAnswer(setLocalAndSend,null,sdpConstraints);//创建answer并发送给对方。
setLocalAndSend中会设置本地sdp,完成设置后onicecandidate事件会调用。然后将candidate发送给对方,对方收到candidate后调用addIceCandidate函数完成peerconnection的创建。
对于onaddstream事件的调用时机,对于offer端,在接收到offer之后就可能onaddstream事件就被触发了。
项目需要,分析了一下peerconnection的创建过程。
假设clientA,clientB分为为offer和answer.
![](http://images2015.cnblogs.com/blog/778894/201601/778894-20160105185733668-1519668935.png)
Offer端
pc =new RTCPeerConnection(null);
pc.onicecandidate=handleIceCandidate;
pc.onaddstream=handleRemoteStreamAdded;
pc.onremovestream=handleRemoteStreamRemoved;
pc.addStream(localStream);
pc.createOffer(setLocalAndSend,handleCreateOfferError);
function
handleIceCandidate(event){
//将本地产生的candidate发送给对方
sendMessage({
type:”candidate”;
candidate:event.candidate.candidate;
……;
});
}
function
setLocalAndSend(sdp){
pc.setLocalDescription(sdp);//设置本地sdp,完成设置后onicecandidate事件会调用。
sendMessage(sdp);//将offer发送给对方
}
当offer提供端接收到来自对方的answer时:
pc.setRemoteDescription(new RTCSessionDescription(message));
当offer端接收到来自对方的candidate时,pc.addIceCandidate(candidate);//将来自对方的candidate设置给本地
2.
Answer端的代码与offer端类似,红色部分代码不同
pc =new
RTCPeerConnection(null);
pc.onicecandidate=handleIceCandidate;
pc.onaddstream=handleRemoteStreamAdded;
pc.onremovestream=handleRemoteStreamRemoved;
pc.addStream(localStream);
注意区别:offer端是主动调用createOffer函数并将offer发送给对方。Answer端接受到offer后,才会创建peerConnection等一系列操作:
pc.setRemoteDescription(sdp);//设置接收到的远端offer
pc.createAnswer(setLocalAndSend,null,sdpConstraints);//创建answer并发送给对方。
setLocalAndSend中会设置本地sdp,完成设置后onicecandidate事件会调用。然后将candidate发送给对方,对方收到candidate后调用addIceCandidate函数完成peerconnection的创建。
对于onaddstream事件的调用时机,对于offer端,在接收到offer之后就可能onaddstream事件就被触发了。
相关文章推荐
- JUnit4 中@AfterClass @BeforeClass @after @before的区别对比
- stm32系统在线调试时程序运行正常输出正常,但是一旦把程序烧写进去则输出不正常
- stm32系统在线调试时程序运行正常输出正常,但是一旦把程序烧写进去则输出不正常
- 详解cookie模块
- 湖南集训End
- 修改wamp默认网站根目录
- NVMe SSD minor number 数量 64?
- android 自定义相机
- MySQL与Oracle的区别
- left,right,substring,substring_index MySQL截取字符串函数方法
- iOS设计模式——Category
- 工厂模式(Factory)和抽象工厂模式(Abstract Factory)
- 遮罩层
- LNMP环境搭建
- CreateProcess()用法
- WPS安装后显示系统字体找不到
- 在tomcat中指定jdk,不配置环境变量
- android 系统相机
- 6.0新特性 权限管理方式
- Async/Await 最佳实践