webrtc之入门

webrtc developers

The WebRTC APIs

Three main tasks

  • Acquiring audio and video
  • Communicating audio and video
  • Communicating arbitrary data

Three main JavaScript APIs

  • MediaStream(aka getUserMedia)
  • RTCPeerConnection
  • RTCDataChannel

MediaStream

(Acquiring audio and video)

MediaStream

  • Pepresent a stream of audio and/or video
  • Can contain multiple ‘tracks’
  • Obtain a MediaStream with navigator.getUserMedia()

Constraints

  • Controls the contents of the MediaStream
  • Media type, resolution, frame rate

    RTCPeerConnection

    (Audio and video communication between peers)

    RTCPeerConnection does a lot

  • Signal processing
  • Codec handling
  • Peer to peer communication
  • Security
  • Bandwidth management

    WebRTC architecture

    image

RTCDataChannel

(Bidirectional communication of arbitrary data between peers)

RTCDataChannel

  • Same API as WebSockets
  • Ultra-low latency
  • Unreliable or reliable
  • Secure

Servers and Protocols

(Peer to peer — but we need servers :)

Abstract Signaling

  • Need to exchange ‘session description’ objects:
    • What formats I support, what I want to send
    • Network information for peer-to-peer setup
  • Can use any messaging mechanism
  • Can use any messaging protocol image

STUN and TRUN

(P2P in the age of firewalls and NATs)

An ideal world

image

The real world

image

STUN

  • Tell me what my public IP address is
  • Simple server, cheap to run
  • Data flows peer-to-peer image

TURN

  • Provide a cloud fallback if peer-to-peer communication fails
  • Data is sent through server, uses server bandwidth
  • Ensures the call works in almost all environments image

ICE

  • ICE: a framework for connecting peers
  • Tries to find the best path for each call
  • Vast majority of calls can use STUN (webrtcstats.com):

Deploying STUN/TURN

  • stun.l.google.com:19302
  • WebRTC stunserver, turnserver
  • rfc5766-turn-server
  • restund

Security

Security throughout WebRTC

Architectures

Peer to Peer : one-to-one call

clientA <——–> clientB

Mesh: small N-way call

1
2
3
4
5
6
clientA <-------------> clientB
/|\ \ / /|\
| \ / |
| / \ |
\|/ / \|/
clientC <--------------> clientD

Star: medium N-way call

1
2
3
clientA <---------> clientB
clientA <---------> clientC
clientA <---------> clientD

MCU: large N-way call

1
2
3
4
5
6
7
MCU <-------------->clientA
MCU <-------------->clientB
MCU <-------------->clientC
MCU <-------------->clientD
MCU <-------------->clientE
MCU <-------------->clientF
MCU <-------------->clientG
坚持原创技术分享,您的支持将鼓励我继续创作!