Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning WebRTC
Learning WebRTC

Learning WebRTC: Develop interactive real-time communication applications with WebRTC

eBook
$17.99 $25.99
Paperback
$32.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning WebRTC

Chapter 2. Getting the User's Media

Obtaining a live video and audio feed from a user's webcam and microphone is the first step to creating a communication platform on WebRTC. This has traditionally been done through browser plugins, but we will use the getUserMedia API to do this all in JavaScript.

In this chapter, we will cover the following topics:

  • Getting access to media devices
  • Constraining the media stream
  • Handling multiple devices
  • Modifying the stream data

Getting access to media devices

There has been a long history behind trying to get media devices to the browser screen. Many have struggled with various Flash-based or other plugin-based solutions that required you to download and install something in your browser to be able to capture the user's camera. This is why the W3C decided to finally create a group to bring this functionality into the browser. The latest browsers now give the JavaScript access to the getUserMedia API, also known as the MediaStream API.

This API has a few key points of functionality:

  • It provides a stream object that represents a real-time media stream, either in the form of audio or video
  • It handles the selection of input devices when there are multiple cameras or microphones connected to the computer
  • It provides security through user preferences and permissions that ask the user before a web page can start fetching a stream from a computer's device

Before we move any further, we should set a few standards...

Constraining the media stream

Now that we know how to get a stream from the browser, we will cover configuring this stream using the first parameter of the getUserMedia API. This parameter expects an object of keys and values telling the browser how to look for and process streams coming from the connected devices. The first options we will cover are simply turning on or off the video or audio streams:

navigator.getUserMedia({ video: false, audio: true }, function (stream) {
  // Now our stream does not contain any video!
});

When you add this stream to the <video> element, it will now not show any video coming from the camera. You can also do the opposite and get just a video feed and no audio. This is great while developing a WebRTC application when you do not want to listen to yourself talk all day!

Tip

Typically, you will be calling yourself a lot while developing the WebRTC applications. This creates the phenomenon known as audio feedback where you are being recorded through the...

Handling multiple devices

In some cases, users may have more than one camera or microphone attached to their device. This is especially the case on mobile devices that often have a front-facing camera and a rear-facing one. In this case, you want to search through the available cameras or microphones and select the appropriate device for your user's needs. Fortunately, to do this, an API called MediaSourceTrack is exposed to the browser.

Note

On the other hand, since many of these APIs are still being created, not everything will be supported by all the browsers. This is especially the case with MediaSourceTrack, which is only supported in the latest version of Chrome at the time of writing this book.

With MediaSourceTrack, we can ask for a list of devices and select the one we need:

MediaStreamTrack.getSources(function(sources) {
    var audioSource = null;
    var videoSource = null;
    for (var i = 0; i < sources.length; ++i) {
      var source = sources[i];
      if(source.kind...

Modifying the media stream

We can take this project even further. Most image sharing applications today have some set of filters that you can apply to your images to make them look even cooler. This is also possible on the Web using CSS filters to provide different effects. We can add some CSS classes that apply different filters to our <canvas> element:

<style>
      .grayscale {
        -webkit-filter: grayscale(1);
        -moz-filter: grayscale(1);
        -ms-filter: grayscale(1);
        -o-filter: grayscale(1);
        filter: grayscale(1);
      }

      .sepia {
        -webkit-filter: sepia(1);
        -moz-filter: sepia(1);
        -ms-filter: sepia(1);
        -o-filter: sepia(1);
        filter: sepia(1);
      }

      .invert {
        -webkit-filter: invert(1);
        -moz-filter: invert(1);
        -ms-filter: invert(1);
        -o-filter: invert(1);
        filter: invert(1);
      }
    </style>

And, also some JavaScript to change the filter on click...

Self-test questions

Q1. The browser will allow camera and microphone access even if the current page is opened as a file and not being served from a web server. True or false?

Q2. Which one of the following is not a correct browser prefix?

  1. webkitGetUserMedia
  2. mozGetUserMedia
  3. blinkGetUserMedia
  4. msGetUserMedia

Q3. The getUserMedia API will call the third argument as a function if an error happens while obtaining the camera or microphone stream. True or false?

Q4. Which one of the following does constraining the video stream not help with?

  1. Securing the video stream
  2. Saving processing power
  3. Providing a good user experience
  4. Saving bandwidth

Q5. The getUserMedia API can be combined with the Canvas API and CSS filters to add even more features to your application. True or false?

Getting access to media devices


There has been a long history behind trying to get media devices to the browser screen. Many have struggled with various Flash-based or other plugin-based solutions that required you to download and install something in your browser to be able to capture the user's camera. This is why the W3C decided to finally create a group to bring this functionality into the browser. The latest browsers now give the JavaScript access to the getUserMedia API, also known as the MediaStream API.

This API has a few key points of functionality:

  • It provides a stream object that represents a real-time media stream, either in the form of audio or video

  • It handles the selection of input devices when there are multiple cameras or microphones connected to the computer

  • It provides security through user preferences and permissions that ask the user before a web page can start fetching a stream from a computer's device

Before we move any further, we should set a few standards about our coding...

Left arrow icon Right arrow icon

Description

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.

Who is this book for?

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.

What you will learn

  • Understand the underlying platform that WebRTC is built upon
  • Create applications that utilize your web camera and microphone
  • Build your very own signaling server from scratch
  • Enable your applications to communicate with multiple users
  • Share data and files across peertopeer connections using WebRTC
  • Implement best practices to secure and make a WebRTC application perform
  • Get to know more about multipeer mesh networks for more than two users
  • Learn the best practices behind traversing networks, signaling, security, and data transport in WebRTC applications
  • Use full mesh networks, partial mesh networks, and multipoint control units to manage your applications

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 30, 2015
Length: 186 pages
Edition : 1st
Language : English
ISBN-13 : 9781783983674
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 30, 2015
Length: 186 pages
Edition : 1st
Language : English
ISBN-13 : 9781783983674
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 133.97
WebRTC Blueprints
$45.99
WebRTC Integrator's Guide
$54.99
Learning WebRTC
$32.99
Total $ 133.97 Stars icon
Banner background image

Table of Contents

10 Chapters
1. Getting Started with WebRTC Chevron down icon Chevron up icon
2. Getting the User's Media Chevron down icon Chevron up icon
3. Creating a Basic WebRTC Application Chevron down icon Chevron up icon
4. Creating a Signaling Server Chevron down icon Chevron up icon
5. Connecting Clients Together Chevron down icon Chevron up icon
6. Sending Data with WebRTC Chevron down icon Chevron up icon
7. File Sharing Chevron down icon Chevron up icon
8. Advanced Security and Large-scale Optimization Chevron down icon Chevron up icon
A. Answers to Self-test Questions Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(5 Ratings)
5 star 40%
4 star 40%
3 star 0%
2 star 20%
1 star 0%
Carlos Camacho Aug 27, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
General pros+ This book is a quite good reference to learn WebRTC, quite basic but it contains a lot of information on how to get it runing using several examples.+ Self tests to check what you have learned.+ Quick explanation.+ Lot of examples.+ To be read in 1 week (1 hour daily).General cons- Not related to the book as this is a basic book, we were trying to reach some scalable broadcasting solution over WebRTC, and there is no solution covered by WebRTC yet.A realy good book.
Amazon Verified review Amazon
Amazon Customer Sep 02, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
great job.Sometimes code quirks especially at dataChannel section.But the code example is well organized and well explained.Thanks
Amazon Verified review Amazon
Sunny Jul 09, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A good book to start.
Amazon Verified review Amazon
Yahamid Jul 24, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is my third reading on the topic of WebRTC. Being a person oriented with the topic, I think it's relatively basic for me but still I found some new information out of it here and there.For beginners, I think this is a good book to start with. It's quite short book (cannot tell the number of pages as I read it online in a few hours) but my perception is that anyone with sufficient of understanding of how Web and the Internet works, it will be a quick read along with testing the sample codes.A good thing is that at the end of chapters there are also certain self-test questions, a cool thing!
Amazon Verified review Amazon
Alexander Pabel Mar 27, 2021
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The book is written good and contains lots of informations what is good but unfortunately the code is not working. Maybe because the code is to old and deprecated. For the price of a new book i would expect at least that most of the code is up to date.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.