Webrtc 介绍与Demo环境搭建
一,webrtc的基本介绍
WebRTC是一个开源项目,提供简单的JavaScript接口以实现浏览器的实时通信(RTC)。与普通的客户端与服务器之间的即时通信不同,webrtc通过一系列的信令,能建立起一个浏览器与浏览器之间(peer-to-peer)的信道,这个信道可以发送任何数据,包括音视频数据,而不需要经过服务器。
WebRTC通过实现MediaStream,调用设备的摄像头、话筒,使得浏览器之间可以传递音频和视频
WebRTC applications need to do several things:
* Get streamingaudio, video or other data.
* Get networkinformation such as IP addresses and ports, and exchange this with other WebRTCclients (known as peers) to enable connection, even through NATs and firewalls.
* Coordinatesignaling communication to report errors and initiate or close sessions.
* Exchangeinformation about media and client capability, such as resolution and codecs.
* Communicate streamingaudio, video or data.
To acquire and communicate streaming data, WebRTCimplements the following APIs:
* MediaStream:get access to data streams, such as from the user's camera and microphone.
*RTCPeerConnection: audio or video calling, with facilities for encryption andbandwidth management.
* RTCDataChannel:peer-to-peer communication of generic data.
现在WebRTC已经可以在较新版的Chrome、Opera和Firefox中使用了,著名的浏览器兼容性查询网站caniuse上给出了一份详尽的浏览器兼容情况。国内大部分主流浏览器都支持(如:360极速浏览器,傲游,搜狗等)。
支持的操作系统平台有:ios, android。
从https://webrtc.org/start/开始认识webrtc.
网络拓扑图:
上面是一个简单的webrtc系统网络拓扑图,其中链路1表示终端从服务器上获取前端页面;链路2表示信令交互过程;链路3表示NAT穿透过程;链路4是终端数据交互。
二, webrtc的信令框架
1) Whatis signaling?
WebRTC uses RTCPeerConnection to communicate streamingdata between browsers, but also needs a mechanism to coordinate communicationand to send control messages, a process known as signaling. Signaling methodsand protocols are not specified by WebRTC.
Instead, WebRTC app developers can choose whatevermessaging protocol they prefer, such as SIP or XMPP, and any appropriate duplex(two-way) communication channel. The apprtc.appspot.com example usesXHR and the Channel API as the signaling mechanism. The codelab webuilt uses Socket.io runningon a Node server.
WEBRTC支持点对点数据通讯,但仍然需要服务端来协调双方的信令消息。在Webrtc规范中未定义信令相关的方法和协议,也就是说信令服务器和控制机制是由应用开发者来自已定义和实现。
至于为什么对信令机制不作定义,有以下的解释:
Why is signaling not defined by WebRTC?
To avoidredundancy and to maximize compatibility with established technologies,signaling methods and protocols are not specified by WebRTC standards.
2) What does signaling do?
Signaling is used to exchange three types of information:
* Session control messages: to initialize or closecommunication and report errors.
* Network configuration: to the outside world, what's mycomputer's IP address and port?
* Media capabilities: what codecs and resolutions can behandled by my browser and the browser i