Google developers logo

Real-time communication built into the browser


WebRTC is a new front in the long war for an open and unencumbered web
Brendan Eich
– Mozilla CTO and inventor of JavaScript
RTC via a server
RTC peer to peer

Shopping list

WebRTC across platforms

Qt moving to Chromium

  • Framework for cross-platform/device native and embedded apps
  • Qt WebKit => Qt WebEngine
  • Multimedia and new HTML5 features such as WebRTC working out-of the-box
1,000,000,000+
WebRTC endpoints
Sky TV interview done via WebRTC
Webcam and Yeti mic used for Sky interview

What do we need for RTC?

Four main tasks

  • Acquiring audio and video
  • Establishing a connection between peers
  • Communicating audio and video
  • Communicating arbitrary data

Three main JavaScript APIs

  • MediaStreams (aka getUserMedia)
  • RTCPeerConnection
  • RTCDataChannel

MediaStreams

Acquiring audio and video

MediaStream

  • Represents a stream of synchronised media
  • Can contain multiple audio and/or video MediaStreamTracks
  • Obtain a MediaStream with navigator.getUserMedia()

gUM

It's pretty simple.

var constraints = {video: true};

function successCallback(stream) {
  var video = document.querySelector("video");
  video.src = window.URL.createObjectURL(stream);
}

function errorCallback(error) {
  console.log("navigator.getUserMedia error: ", error);
}

navigator.getUserMedia(constraints, successCallback, errorCallback);

 

var constraints = {video: true};

function successCallback(stream) {
  var video = document.querySelector("video");
  video.src = window.URL.createObjectURL(stream);
}

function errorCallback(error) {
  console.log("navigator.getUserMedia error: ", error);
}

navigator.getUserMedia(constraints, successCallback, errorCallback);

gUM permissions

  • HTTPS only prompts once!
  • Chrome apps: audioCapture and videoCapture permissions
  • UI settings can be changed afterwards.
Don't use file:// URLs

getUserMedia + Web Audio

// Success callback when requesting audio input stream
function gotStream(stream) {
    var audioContext = new webkitAudioContext();

    // Create an AudioNode from the stream
    var mediaStreamSource = audioContext.createMediaStreamSource(stream);

    // Connect it to the destination or any other node for processing!
    mediaStreamSource.connect(audioContext.destination);
}

navigator.webkitGetUserMedia( {audio:true}, gotStream);

Make sure to enable Web Audio Input in about:flags!

Work underway to make this more user-focused

  • User wants to choose "front-facing" or "rear-facing" camera, not a USB ID!
  • Choose source: spec
  • Apply constraints dynamically from JavaScript: spec

gUM screencapture!

Be sure to enable screen capture support in getUserMedia!

var constraints = {
  video: {
    mandatory: {
      chromeMediaSource: 'screen'
    }
  }
};

navigator.webkitGetUserMedia(constraints, gotStream);

RTCPeerConnection

Audio and video communication between peers

Communicate Media Streams

WebRTC video chat: caller

getUserMedia
+
RTCPeerConnection
WebRTC video chat: callee

WebRTC architecture

WebRTC architecture diagram

WebRTC infrastructure

STUN, TURN and signaling

Peer to peer — but we need servers :^\

Signaling - coordinating communication

  • Need to cope with NATs and firewalls: STUN and TURN
  • To do this, we exchange 'session description' objects:
    • What media formats I support, what I want to send
    • Network information for peer-to-peer setup
  • Signaling *can* use any messaging mechanism or protocol

JSEP architecture

JSETP architecture diagram

RTCPeerConnection initialisation

  • Ascertain local media conditions: resolution, codec capabilities...
  • Get potential network addresses for the application's host: candidates
  • Exchange this data via the signaling mechanism.
w3.org/TR/webrtc/#simple-peer-to-peer-example

Signaling with Node and Socket.io

  • Socket.io uses WebSocket with fallbacks
  • Simple to exchange messages
  • Built-in concept of 'rooms'

Codelab

Live example

An RTCSessionDescription

v=0
o=- 7614219274584779017 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE audio video
a=msid-semantic: WMS
m=audio 1 RTP/SAVPF 111 103 104 0 8 107 106 105 13 126
c=IN IP4 0.0.0.0
a=rtcp:1 IN IP4 0.0.0.0
a=ice-ufrag:W2TGCZw2NZHuwlnf
a=ice-pwd:xdQEccP40E+P0L5qTyzDgfmW
...

Want to know what all this SDP gobbledygook actually means?

Take a look at the IETF examples.

Candidate objects

{
  'type':'candidate',
  'label':1,
  'id':'video',
  'candidate':'a=candidate:1274936569 1 udp 1845501695 2.96.35.15 63579
               typ srflx raddr 192.168.0.3 rport 63579 generation 0\r\n'
}
...
{
  'type':'candidate',
  'label':0,
  'id':'audio',
  'candidate':'a=candidate:3802297132 1 udp 2113937151 192.168.0.3 63579
               typ host generation 0\r\n'
}

My head hurts.

RTCDataChannel

Bidirectional communication of arbitrary data between peers

Communicate arbitrary data

Game: caller
onreceivemessage = handle(data); ... var myData = [ { id: "ship1"; x: 24, y: 11, velocity: 7 }, .... ] send(myData);

RTCDataChannel
+
RTCPeerConnection
onreceivemessage = handle(data); ... var myData = [ { id: "ship7"; x: 19, y: 4, velocity: 18 }, .... ] send(myData);
Game: callee

RTCDataChannel

  • Same API as WebSockets
  • Ultra-low latency
  • Optionally unreliable or reliable:
    — Firefox and Chrome 31, Chrome 30 behind a flag
  • Secure

RTCDataChannel API

var pc = new webkitRTCPeerConnection(servers,
  {optional: [{RtpDataChannels: true}]});

pc.ondatachannel = function(event) {
  receiveChannel = event.channel;
  receiveChannel.onmessage = function(event){
    document.querySelector("div#receive").innerHTML = event.data;
  };
};

sendChannel = pc.createDataChannel("sendDataChannel", {reliable: false});

document.querySelector("button#send").onclick = function (){
  var data = document.querySelector("textarea#send").value;
  sendChannel.send(data);
};

Security

Security throughout WebRTC

Secure pathways

Secure pathways between peers

Voice

Voice is just another JS application
Henning Schulzrinne
– CTO, US FCC

Phones and more - beyond browsers

  • Easy to interoperate with non-browser devices
    • sipML5 open source JavaScript SIP client
    • Phono open source JavaScript phone API
    • Zingaya embeddable phone widget

Telephony

Developing with WebRTC

chrome://webrtc-internals

chrome://webrtc-internals screenshot

adapter.js

Lets you use the same code in all browsers:

  • Removes vendor prefixes
  • Abstracts Chrome/Firefox differences
  • Minimizes effects of spec churn
My brain has already exploded.

JavaScript frameworks

More Information

Contact Us

WebRTC and HTML5 could enable the same transformation for real-time communications that the original browser did for information.
Phil Edholm
— NoJitter