- WebRTC Cookbook
- Andrii Sergiienko
- 447字
- 2021-07-23 20:27:07
Detecting WebRTC functions supported by a browser
WebRTC is not fully supported by all available web browsers at this time. Moreover, there is a chance that your application will be running under some kind of exotic environment or web browser that does not support WebRTC. So you need to have some mechanism that would enable you to detect whether the environment in which your web application is running supports the necessary WebRTC features the application is going to use. In this recipe, we will cover the basic method of doing that.
Getting ready
This task is relevant for the client side only, so all the code will be written in JavaScript. Thus, no specific preparation is needed.
How to do it…
You can write a JavaScript library that can be used to detect which WebRTC methods are available under the environment and by what names they are known for your application.
The following code represents a basic but productive example of such a kind of library:
var webrtcDetectedVersion = null; var webrtcDetectedBrowser = null; window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; function initWebRTCAdapter() { if (navigator.mozGetUserMedia) { webrtcDetectedBrowser = "firefox"; webrtcDetectedVersion = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10); RTCPeerConnection = mozRTCPeerConnection; RTCSessionDescription = mozRTCSessionDescription; RTCIceCandidate = mozRTCIceCandidate; getUserMedia = navigator.mozGetUserMedia.bind(navigator); attachMediaStream = function(element, stream) { element.mozSrcObject = stream; element.play(); }; reattachMediaStream = function(to, from) { to.mozSrcObject = from.mozSrcObject; to.play(); }; MediaStream.prototype.getVideoTracks = function() { return []; }; MediaStream.prototype.getAudioTracks = function() { return []; }; return true; } else if (navigator.webkitGetUserMedia) { webrtcDetectedBrowser = "chrome"; webrtcDetectedVersion = parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10); RTCPeerConnection = webkitRTCPeerConnection; getUserMedia = navigator.webkitGetUserMedia.bind(navigator); attachMediaStream = function(element, stream) { element.src = webkitURL.createObjectURL(stream); }; reattachMediaStream = function(to, from) { to.src = from.src; }; if (!webkitMediaStream.prototype.getVideoTracks) { webkitMediaStream.prototype.getVideoTracks = function() { return this.videoTracks; }; webkitMediaStream.prototype.getAudioTracks = function() { return this.audioTracks; }; } if (!webkitRTCPeerConnection.prototype.getLocalStreams) { webkitRTCPeerConnection.prototype.getLocalStreams = function() { return this.localStreams; }; webkitRTCPeerConnection.prototype.getRemoteStreams = function() { return this.remoteStreams; }; } return true; } else return false; };
How it works…
This solution tests which WebRTC API methods are available in the environment and how they are named. So your application can use certain API function names that will be relevant for any web browser, without using browser-specific function names.
There's more…
There is another way to solve this task. You don't necessary have to write your own adapter. You can take the adapter prepared by Google. It can be found at http://apprtc.webrtc.org/js/adapter.js. You just need to include it in your JavaScript code.
You can also consider using a browser's plugin that enables the use of WebRTC in Safari and Internet Explorer. You can get these at https://temasys.atlassian.net/wiki/display/TWPP/How+to+integrate+the+plugin+with+your+website.
See also
You can find more information on the adapter at the web page http://www.webrtc.org/web-apis/interop.
- Learning Python Web Penetration Testing
- Embedded Linux Projects Using Yocto Project Cookbook
- C語言程序設(shè)計教程(第2版)
- Mastering Python High Performance
- Building Mapping Applications with QGIS
- OpenShift在企業(yè)中的實踐:PaaS DevOps微服務(wù)(第2版)
- R Deep Learning Cookbook
- Haskell Data Analysis Cookbook
- 大數(shù)據(jù)分析與應(yīng)用實戰(zhàn):統(tǒng)計機器學(xué)習(xí)之?dāng)?shù)據(jù)導(dǎo)向編程
- Kivy Cookbook
- PHP+MySQL動態(tài)網(wǎng)站開發(fā)從入門到精通(視頻教學(xué)版)
- Getting Started with Electronic Projects
- jQuery Mobile Web Development Essentials(Second Edition)
- Arduino Electronics Blueprints
- Microsoft Dynamics GP 2013 Cookbook