From 2e4a36301763e517e8979162e90a47c5ca18588e Mon Sep 17 00:00:00 2001 From: "Nicolas Schoemaeker (nschoe)" Date: Sun, 15 Feb 2015 13:29:33 +0100 Subject: [PATCH] Proof-read article about WebRTC --- ...-02-14-Lets-Have-Fun-With-WebRTC-Part-1.md | 1254 +++++++++++++++++ css/default.css | 44 +- css/syntax_highlighting.css | 14 +- site.hs | 12 +- templates/default.html | 2 +- 5 files changed, 1309 insertions(+), 17 deletions(-) create mode 100644 articles/2015-02-14-Lets-Have-Fun-With-WebRTC-Part-1.md diff --git a/articles/2015-02-14-Lets-Have-Fun-With-WebRTC-Part-1.md b/articles/2015-02-14-Lets-Have-Fun-With-WebRTC-Part-1.md new file mode 100644 index 0000000..fa9c03b --- /dev/null +++ b/articles/2015-02-14-Lets-Have-Fun-With-WebRTC-Part-1.md @@ -0,0 +1,1254 @@ +--- +title: Let's Have Fun With WebRTC! - Part 1 +description: WebRTC is a new technology that allows peer-to-peer media communication. It is fast (it relies on UDP), it is secure (all payload is encrypted -mandatory), and the best, simplest APIs have been implemented in web browsers, so all of this is available right now, without the need of any external softwares or plugins! In this series of articles, we will build some cool applications together to demonstrate the power of WebRTC! +--- + +## Introduction + +### So, what's the deal? +Today we are going to build an audio/video communication app with WebRTC, and the best part is that it will be used from the browser: no additional softwares, no additional plugins. +WebRTC is a new technology that allows peer-to-peer (multimedia) communications. It is not intended for browsers in particular, but it is true that the simplest (_read "easiest to use"_) APIs are implemented in Javascript and thus, we will use these. + +### What does WebRTC bring to the table, and what can we do with it? +In this world today, all we can hear is about "[The Cloud](http://betabeat.com/2013/03/chrome-extension-replaces-every-instance-of-the-cloud-with-the-far-superior-my-butt/)". While it has some advantages (data is synchronized across all our devices, it is backed up in case of failure, etc) it does have some drawbacks (data is hosted on private companies that can analyze and sell it, these big servers are the target of some criminal attacks, etc). +WebRTC is **not** a replacement for the Cloud, it has nothing to do with it, but it brings some interesting features: + +- **Fast**: WebRTC uses [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) as the [transport protocol](https://en.wikipedia.org/wiki/Transport_layer), so it is intended to be quick. Besides, it is implemented using only native Javascript (and HTML5) APIs, so forget the load of Flash Player... +- **Peer-to-peer**: WebRTC is designed to be peer-to-peer, so data transit from your computer to your peer's. That's it. There is no central (and privately-owned) server that can intercept your data. +- **Secured**: WebRTC _makes it mandatory_ for **all** payload to be encrypted. You simply cannot initiate a WebRTC call without your data to be encrypted. That is an important aspect that I really like. +- **Media and real-time**-enforced: WebRTC is designed to be a real time protocol (uses [RTP](https://en.wikipedia.org/wiki/Real-time_Transport_Protocol) on top of UDP) and has been designed to handle audio/video streams of data. For instance, the browser's implementations have built-in [adaptive bitrate streaming](https://en.wikipedia.org/wiki/Adaptive_bitrate_streaming): the quality and the compression of audio and video is altered on-the-run to compensate variation in communication's strength (rather than having your connection hanged up, you simply have a decrease in quality until your communication gets back to its top quality). + +#### What are we going to do anyway? +Together, we will build some interesting stuffs. I'll explain how to do it along the way and I'll introduce the underlying concepts as we need/meet them. + +- For this article, we will build a **Skype-like** application, right into the browser: no more ads, and no more spying. This is gonna be fun! +- For the next article, we will build a **file-sharing** application: this will allow you to share any file with anyone of your friends without uploading your file to a server. Pretty handy: you don't want the photos from your last night out to end up on 9gag, do you? + +### Are we good to go? +Well... ready when you are! +Just go grab a bottle of Coke, go buy some candies (_sugar my friend, sugar..._) and listen to some good music (_may I suggest Led Zeppelin, Kashmir?_) and then we are good to go! + +## Let's do This: Skype-like Application in Browser! + +### A Little Word on the Workflow +You can find the code for that article on my [github repo](https://github.com/nschoe/webrtc-fun-part-1). +Here is what we are going to need to do to build our application: + +- **Acquire audio and/or video media stream**: if we want to transmit video and audio, we first need to acquire it, and remember: we are in the web browser, and we don't want things like Flash or Java plugins. +- **Set up a _signaling channel_**: I will describe what it is, what it means and why we needs this. Remember now that it allows the peers to negociate the parameters of the connection. +- **Peer discovery**: We will use ICE framework with a STUN server to discover our public IP and gather candidates. Same thing: I will explain what that means, but basically, it has to do with the fact that you don't know where you peer is on the Internet. +- **Create and send an offer**: WebRTC jargon here. In an _offer_, the _caller_ lists a number of parameters for the connection. +- **Receive the offer and create & send the answer**: This is the previously created offer, the other peer does the same (except this is called an answer this time) and sends it. +- **Receive answer and start transmitting audio/video/raw data**: This is where the real peer-to-peer starts. Previously, the answer and the offer were transmited using the _signalling channel_. + +This is basically the steps we will follow. We will learn together the underlying technologies used by WebRTC (not everything is new in WebRTC) and detail some aspects of the protocols. + +**Bonus point**: writing a project-based paper would not be the same without some code example. So along the articles I will post & comment samples of code, but you will find the whole code [here.](https://github.com/nschoe/fun-with-WebRTC-part-1) + +### Acquire Audio and Video Media Streams +Remember our workflow from earlier? The very first thing we need to do to transmit our pretty face & voice is to acquire the streams. Sounds easy, but until recently, you had to rely on either Adobe Flash, Java (or Microsoft Silverlight?). +A new, neat Javascript API, that integrates well with HTML5 and WebRTC helps us now : +[The MediaStream and MediaCapture API](http://www.w3.org/TR/mediacapture-streams/). + +Let's describe this API briefly first, so that we know what we are dealing with. + +#### Description of the MediaStream API +This API describes a stream of video or audio data. It contains methods and callback to create the streams, manipulate them and use them with other APIs (including the WebRTC API of course). +A `MediaStream` object can be empty or contain several `MediaStreamTrack`s. +A `MediaStreamTrack` is what you can expect from the name: a track, like an audio track from a CD; except that it can be either a video or audio track (described by the `kind` attribute). What is really neat is that all `MediaStreamTrack`s inside a `MediaStream` are synchronized: this proves very useful to keep video and voice synced. Quite naturally, a `MediaStreamTrack` can be manipulated and queried. We denote a number of interesting attributes: + +- `muted`: self-explanatory. Unless there is some weirdness with WebRTC: a muted `MediaStreamTrack` doesn't stop transmitting data, it "just" transmits _meaningless_ data; we'll come back on this later. +- `remote`: says if the track comes from (one of) our peer(s) or from us. + +Each `MediaSTreamTrack` contains one or more `channels` (for instance, an audio track might contain a channel for the left speaker, one channel for the right speaker, etc). This is all just for documentation, because in our use case (web browser), we'll get video from the webcam and audio from the microphone. We won't be dealing with several microphones, so it will all be transparent for us and handled by WebRTC. + +A `MediaStream` has an input and an output. The input depends how you got the stream (local file, webcam, microphone, ...) and the output will typically be a HTML5 `