The video for this presentation is located at: https://vimeo.com/133079307
Patrick Cason and Kenny House gave a talk to introduce Javascript developers to the basic concepts of WebRTC. In the talk are examples of how to implement WebRTC as well as a high-level overview of basic networking when streaming live audio and video peer-to-peer.
To learn more about how Kenny and Patrick use WebRTC in their work, visit www.octovis.com.
2. WEBRTC IN JAVASCRIPT
IETF (RTCWEB SPECIFICATION)
WebRTC consists of a protocol specification and
a Javascript API specification that, if all are
implemented, will allow communication using
audio, video, and data sent along the most
direct possible path between participants.
2
3. WEBRTC IN JAVASCRIPT
History and Overview
• Been around since 2011, still in draft status, but it’s used
in production
• Google acquired two companies (On2… video codecs
and GIPS… VoIP calling)
• Wrote a Javascript API for submission to W3C and
called it WebRTC (“Web Real-time Communication”)
3
4. WEBRTC IN JAVASCRIPT
Why we chose WebRTC…
• The web is moving towards real-time functionality
• People want something native to their browser
• Video and audio calls on the web !== Flash or ActiveX
4
(AND WHY YOU SHOULD TOO)
5. WEBRTC IN JAVASCRIPT
1. Have media to send (webcam, microphone, etc.)
2. Figure out best way for peers to communicate
3. Determine your media’s format (SDP)
4. Send communication and media information
5. Repeat steps 1 - 4 for other peers
5
6. WEBRTC IN JAVASCRIPT
1. Have media to send (webcam, microphone, etc.)
2. Figure out best way for peers to communicate
3. Determine your media’s format (SDP)
4. Send communication and media information
5. Repeat steps 1 - 4 for other peers
6
7. WEBRTC IN JAVASCRIPT
getUserMedia()
• Handles all transport of
video and audio
information from the
hardware to the web
• Each MediaStream
contains multiple
“tracks”
7
navigator.getUserMedia()
8. WEBRTC IN JAVASCRIPT
getUserMedia()
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);
8
9. WEBRTC IN JAVASCRIPT
getUserMedia() constraints
• Intended to control the media stream via a
configuration object
• Controls media type, resolution, frame rate, etc.
• Able to specify specs as either “mandatory” or
“optional”
• Constraints only affect local view of your stream
9
10. WEBRTC IN JAVASCRIPT
getUserMedia() audio analysis
• Only works for Chrome local audio streams, you can’t
currently analyze remote audio streams
10
// 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);
11. WEBRTC IN JAVASCRIPT
1. Have media to send (webcam, microphone, etc.)
2. Figure out best way for peers to communicate
3. Determine your media’s format (SDP)
4. Send communication and media information
5. Repeat steps 1 - 4 for other peers
11
12. WEBRTC IN JAVASCRIPT
TCP & UDP
• UDP (User Datagram Protocol)
Unreliable… fast, and with a low overhead
• TCP (Transmission Control Protocol)
Reliable… slow, and with a high overhead
• HTTP uses TCP for connections, vast majority of web
services use TCP
12
13. WEBRTC IN JAVASCRIPT
All about dat UDP
• UDP is a better choice for streaming media…
• It’s fast
• You don’t usually need every single packet
• Caveat? You need to know where to send packets.
13
17. WEBRTC IN JAVASCRIPT
ICE, ICE Connections…
• ICE is a priority-based protocol for testing connections
between two computers
• ICE calls these possible connection points “candidates”
• Computers send them to each other, test them out, and
choose the best fit
17
20. WEBRTC IN JAVASCRIPT
ICE Connections
var pc;
pc = new RTCPeerConnection({
'iceServers': [
{ 'url': 'stun:stun.services.mozilla.com' },
{ 'url': 'stun:stun.l.google.com:19302' }
]
});
pc.onicecandidate = function(event) {
if(event.candidate !== null) {
mySocket.send(JSON.stringify({
'ice': event.candidate
}));
}
}
20
21. WEBRTC IN JAVASCRIPT
Gotchas
• In addition to your signaling system server, you’ll also
need to run your own STUN/TURN server
• ICE checks for local network connections first, so it will
almost always work over a local network
• This doesn’t mean it works over the Internet
• Particularly embarrassing when demoing for a
client…
21
23. WEBRTC IN JAVASCRIPT
1. Have media to send (webcam, microphone, etc.)
2. Figure out best way for peers to communicate
3. Determine your media’s format (SDP)
4. Send communication and media information
5. Repeat steps 1 - 4 for other peers
23
24. WEBRTC IN JAVASCRIPT
SDP
• Two peers on ICE communicate via a spec called SDP
(“Session Description Protocol”) which tell what each
peer is interested in
• Do you want audio? Sure.
• Do you want video? Definitely.
• Breakfast in bed? Every Sunday.
24
25. WEBRTC IN JAVASCRIPT
SDP (continued)
• SDP also handles codec specifications
• Generally no need to modify an SDP string
• First peer sends an offer, second peer sends an answer
• Both ICE and SDP require a signaling server to set up
the direct connection between peers
25
28. WEBRTC IN JAVASCRIPT
RTCPeerConnection
pc = new RTCPeerConnection(null);
pc.addStream(localStream);
pc.createOffer(sendingOffer);
function sendingOffer(desc) {
pc.setLocalDescription(desc);
sendAnswer(desc);
}
// When answer received over signaling
function gotAnswer(desc) {
pc.setRemoteDescription(desc);
}
28
30. WEBRTC IN JAVASCRIPT
1. Have media to send (webcam, microphone, etc.)
2. Figure out best way for peers to communicate
3. Determine your media’s format (SDP)
4. Send communication and media information
5. Repeat steps 1 - 4 for other peers
30
32. WEBRTC IN JAVASCRIPT
Platform Support
• Google Chrome (mostly)
• Chrome for Android (mostly)
• Firefox (kinda)
• Opera (not really)
• Safari & IE (srsly bro?)
• Native Java and Objective-C bindings (about the same as
Chrome)
32
33. WEBRTC IN JAVASCRIPT
Nerdy Details
• Video codec support: VP8, H264
• Audio codec support: G.711, iLBC, iSAC, OPUS
• Media transport protocol: RTP/RTCP
• Security protocol: SRTP
• NAT traversal: STUN/TURN/ICE
• Signaling: Peer-to-peer, but requires a signaling component
(agnostic) to notify peers of status changes and new messages
33
34. WEBRTC IN JAVASCRIPT
adapter.js & Temasys
• adapter.js (maintained by Google) is a Javascript shim
made to normalize browser differences
• Minimizes the effects of spec churn from subtleties
• Temasys also makes their own version of adapter.js
which adds a Flash fallback for a poor man’s browsers
34
35. WEBRTC IN JAVASCRIPT
WebRTC Internals
• God’s gift to WebRTC
developers
• Available at:
chrome://webrtc-internals
• Or programmatically:
myPeerConnection.getStats()
35
36. WEBRTC IN JAVASCRIPT
Resources
• Getting Started with WebRTC
• Capturing Audio & Video in HTML5
• WebRTC in the Real World
• Google I/O Presentation Video
• HTML5 WebRTC Explained Video
• WebRTC @ MDN
• WebRTC Book
36