23,99 €
If you are a web developer who wants to create well designed WebRTC applications for your users, this is the book for you. Even if you are early in your web development career, this book aims to provide a complete understanding of the WebRTC API. It is assumed that you have previous exposure to web development using HTML5 and JavaScript technologies.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 214
Veröffentlichungsjahr: 2015
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2015
Production reference: 1250615
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-366-7
www.packtpub.com
Author
Dan Ristic
Reviewers
Roy Binux
Tsahi Levent-Levi
Andrii Sergiienko
Commissioning Editor
Andrew Duckworth
Acquisition Editor
Nikhil Karkal
Content Development Editor
Manasi Pandire
Technical Editor
Rahul C. Shah
Copy Editors
Sonia Michelle Cheema
Swati Priya
Neha Vyas
Project Coordinator
Bijal Patel
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Graphics
Disha Haria
Production Coordinator
Conidon Miranda
Cover Work
Conidon Miranda
Dan Ristic is a frontend engineer and evangelist for Open Web. He strives to push the Web forward with new and creative ideas using the latest technologies. He attended the University of Advancing Technology, Arizona, where he furthered his engineering knowledge and fueled his passion for the Web. He has been writing applications on the Web ever since.
He currently lives and works in San Francisco as a senior software engineer at Sony Network Entertainment International. At Sony, he is responsible for managing the frontend application architecture and delivering the PlayStation Store to millions of users. In his free time, he can be found hiking, exploring, working on projects, and attending events.
I would like to thank my family for their never-ending support, motivation, and encouragement in everything I do.
I would also like to thank my friends for the innumerable cups of coffee and their help to keep me on track.
Roy Binux is a software engineer and an open source developer. He focuses on information extraction and the Spider System at work and builds interesting things online for fun. He is open to new technologies and tries to make the process of getting knowledge from the Web easier and build something useful with the power of the Web. Most of his work is open source, and you can find him on GitHub at http://github.com/binux.
Tsahi Levent-Levi is an independent analyst and consultant for WebRTC.
He has 15 years of experience in telecommunications, VoIP, and the 3G industry as an engineer, manager, marketer, and CTO. He is an entrepreneur, independent analyst, and consultant and assists companies to form a bridge between technologies and business strategies in the domain of telecommunications.
He has an MSc degree in computer science and an MBA degree, with specialization in entrepreneurship and strategy. He has been granted three patents related to 3G-324M and VoIP. He was the chairman of various activity groups within IMTC, an organization focusing on the interoperability of multimedia communications.
He is also the author and editor of his blog https://bloggeek.me, which focuses on the ecosystem and business opportunities around WebRTC.
Andrii Sergiienko is an entrepreneur who is passionate about information technology and travelling. He has lived in different places, such as Ukraine, Russia, Belarus, Mongolia, Buryatia, and Siberia—and has spent many years in every place. He also likes to travel by auto.
From his early childhood, he was interested in computer programming and hardware. He took his first step in these fields more than 20 years ago. He has experience working with a wide set of languages and technologies, including C, C++, Java, Assembler, Erlang, JavaScript, PHP, Riak, shell scripting, computer networks, security, and so on.
During his career, he has worked for both small local companies, such as domestic ISP, and large world corporations, such as Hewlett Packard. He has also started his own companies—some of them were relatively successful; others totally failed.
Today, he is working on the growth aspect of Oslikas—his new company that has its headquarters in Estonia. The company is focused on modern IT technologies and solutions. They also develop a full-stack framework to create rich media WebRTC applications and services. For more information on Oslikas, you can visit http://www.oslikas.com.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
When I first started writing HTML code, I was excited. Here I was typing letters into the keyboard, each one giving the computer some instructions that it knew what to do with. I was excited that I was creating something and that these instructions allowed me to express my creativity. When I finally saved my work, fired up my browser, and loaded my page, I was in awe. I saw my name in big bold letters with a moving picture, called a GIF, of an animated fire.
The Web has certainly come a long way since then. This is largely due to the fact that it is not just a place to create something new, but also a platform to share and show this creativity to others. This powerful platform for creative expression is what powers the Web and keeps it growing faster than ever. It has become so popular that we are moving our entire lives onto it. Websites power your e-mail, entertainment, bank accounts, legal documents, taxes, and even parts of this book were written using web tools. It is the want to move our lives to the Web that drives the development of powerful, easy-to-use APIs, such as WebRTC.
WebRTC is one of the most substantial additions to the Web platform. It brings about an entire suite of new technologies, such as cameras, streaming data, and even an entirely new network protocol stack. It is amazing to not only see the amount of work going into the WebRTC API, but also to know that this is all free for use by any application developer out there.
The aim of WebRTC is to democratize real-time communication. Earlier, building even a smaller video communication application used to take months and involved custom engineering to make even the smallest of applications. However, now we can do it in half the time or even less. This also brings the open source community into real-time communication. You can find other examples of WebRTC in the world and look at how these applications are built by searching through the source code.
It is this creative expression and freedom provided by the Web that drives the motivations for this book. I am glad to have the chance to bring it to more people through my writing, and I hope to inspire others just as I was inspired the first time I built a web page. Writing this book is one of the largest and toughest things I have done in my career, but I am grateful for all the help I have had along the way.
If you are looking for the easiest way to create a new real-time experience and share this with others, then read this book. This book, along with all technical books, is just a way of continuing to drive people to create something even better on the Web. You will learn not just about how to use WebRTC, but also what powers it under the hood. This book serves as not just a learning tool, but also as an inspiration for creating something truly amazing.
Chapter 1, Getting Started with WebRTC, covers how WebRTC enables audio and video communication for web-based applications. You will also begin by running an example of a WebRTC application inside your browser.
Chapter 2, Getting the User's Media, covers the first step when creating a communication application to get webcam and microphone input. This chapter also covers how to use the Media Capture and Streams API to capture this information from you. We also begin development by building the foundation of our communication example.
Chapter 3, Creating a Basic WebRTC Application, covers an introduction to the first WebRTC API—the RTCPeerConnection. This chapter also lays the groundwork for creating a WebRTC application by peeking inside the complex structure of WebRTC and what we can expect when we begin working with the API.
Chapter 4, Creating a Signaling Server, covers the steps in creating our very own signaling server to help our clients find each other on the Internet. This includes in-depth information on how signaling works in WebRTC and how we will utilize it in our example application.
Chapter 5, Connecting Clients Together, covers the actual usage of our signaling server. It also covers connecting two users successfully using the WebRTC API, Media Capture, and the signaling server that we created in the previous chapter to build our working example.
Chapter 6, Sending Data with WebRTC, covers an introduction to the RTCDataChannel and how it is used to send raw data between two peers. This chapter elaborates on our example by adding a text-based chat for our clients.
Chapter 7, File Sharing, elaborates on the concept of sending raw data by looking at how we can share files between two peers. This will demonstrate the many uses of WebRTC outside of audio and video sharing.
Chapter 8, Advanced Security and Large-scale Optimization, covers advanced topics when delivering a large-scale WebRTC application. We look at theoretical security and performance optimizations used by other companies in the industry.
Appendix, Answers to Self-test Questions, covers the answers to all the self-test questions that appear at the end of every chapter.
All the examples in this book are built on the Web using web standards. Since the WebRTC specification is fairly new, it is recommended to run the examples inside an updated browser. It is preferred to use the latest Firefox or Chrome web browser for these.
All of the server code is written using Node.js. The Node.js framework runs inside most Windows, Linux, and Mac OSX machines.
You can use any text editor that supports the writing of JavaScript and HTML code.
You should have some experience building web applications using HTML and JavaScript. Maybe, you are building an application right now, or have an idea of a new application that utilizes the power of audio and video communication between users. You might also need to deliver an application with high-performance data transfer between users.
You should have a firm grasp of programming concepts and web development, but the book is written for even a novice web engineer. The concepts are covered in-depth and taken one bit at a time, rather than charging ahead into the advanced topics. You may not know anything about WebRTC or have only heard a bit about it, and want to learn the inner workings of real-time communication.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
The Internet is no stranger to audio and video. Everyday web applications, such as Netflix and Pandora, stream audio and video content to millions of people. On the other hand, the Web is a stranger to real-time communication. Websites, such as Facebook, are only just starting to enable video-based communication in a browser, and they typically use a plugin that users have to install. This is where Web Real-Time Communication (WebRTC) comes into play.
In this chapter, we are going to cover the basics of WebRTC:
Communicating with audio and video is a fairly common task with a history of technologies and tools. For a good example of audio communication, just take a look at a cell phone carrier. Large phone companies have established large networks of audio communication technology to bring audio communication to millions of people across the globe. These networks are a great example when it comes to showing widespread audio communication at its finest.
Video communication is also becoming just as prevalent as audio communication. With technologies such as Apple's FaceTime, Google Hangouts, and Skype video calling, speaking to someone over a video stream is a simple task for an everyday user. A wide range of techniques have been developed in these applications to ensure that the quality of the video is an excellent experience for the user. There have been engineering solutions to problems, such as losing data packets, recovering from disconnections, and reacting to changes in a user's network.
The aim of WebRTC is to bring all of this technology into the browser. Many of these solutions require users to install plugins or applications on their PCs and mobile devices. They also require developers to pay for licensing, creating a huge barrier and deterring new companies to join this space. With WebRTC, the focus is on enabling this technology for every browser user without the need for plugins or hefty technology license fees for developers. The idea is to be able to simply open up a website and connect with another user right then and there.
The biggest accomplishment of WebRTC is bringing high-quality audio and video to the open the Web without the need for third-party software or plugins. Currently, there are no high-quality, well-built, freely available solutions that enable real-time communication in the browser. The success of the Internet is largely due to the high availability and open use of technologies, such as HTML, HTTP, and TCP/IP. To move the Internet forward, we want to continue building on top of these technologies. This is where WebRTC comes into play.
To build a real-time communication application from scratch, we would need to bring in a wealth of libraries and frameworks to deal with the many issues faced when developing these types of applications. These typically include software to handle connection dropping, data loss, and NAT traversal. The great thing about WebRTC is that all of this comes built-in to the browser API. Google has open sourced much of the technology involved in accomplishing this communication in a high-quality and complete manner.
Most of the information about WebRTC, including the source code of its implementation, can be found freely available at http://www.webrtc.org/.
With WebRTC, the heavy lifting is all done for you. The API brings a host of technologies into the browser to make implementation details easy. This includes camera and microphone capture, video and audio encoding and decoding, transportation layers, and session management.
The first step to using any communication platform is to gain access to the camera and microphone on the device that the user is using. This means detecting the types of devices available, getting permission from the user to access them, and obtaining a stream of data from the device itself. This is where we will begin implementing our first application.
Unfortunately, even with the improvements made in network speed, sending a stream of audio and video data over the Internet is too much to handle. This is where encoding and decoding comes in. This is the process of breaking down video frames or audio waves into smaller chunks and compressing them into a smaller size. The smaller size then makes it faster to send them across a network and decompress them on the other side. The algorithm behind this technique is typically called a codec.
If you have ever had trouble playing a video file on your computer, then you have some insight into the complex world of video and audio codecs. There are several different ways to encode audio and video streams, each with their different benefits. To add to this, there are many different companies that have different business goals behind creating and maintaining a codec. This means not all of the codecs are free for everyone to use.
There are many codecs in use inside WebRTC. These include H.264, Opus, iSAC, and VP8. When two browsers speak to each other, they pick the most optimal supported codec between the two users. The browser vendors also meet regularly to decide which codecs should be supported in order for the technology to work. You can read more about the support for various codecs at http://www.webrtc.org/faq.
You could easily write several books on the subject of codecs. In fact, there are many books already written on the subject. Fortunately for us, WebRTC does most of the encoding in the browser layer. We will not worry about it over the course of this book but, once you start venturing past basic video and audio communication, you will more than likely bump heads with codec support.
The transportation layer is the topic of several other books as well. This layer deals with