What is webrtc-adapter?
The webrtc-adapter npm package is a shim to insulate apps from spec changes and prefix differences in WebRTC APIs. It helps developers write code that works across different browsers by providing a consistent API.
What are webrtc-adapter's main functionalities?
Browser Compatibility
The webrtc-adapter package helps ensure that WebRTC code is compatible across different browsers by normalizing the API differences. This code sample demonstrates how to use the adapter to get browser details.
const adapter = require('webrtc-adapter');
console.log(adapter.browserDetails.browser); // Outputs the browser name
console.log(adapter.browserDetails.version); // Outputs the browser version
Unified API
The webrtc-adapter package provides a unified API for accessing media devices, making it easier to write cross-browser WebRTC applications. This code sample shows how to use the adapter to access the user's media devices.
const adapter = require('webrtc-adapter');
const getUserMedia = navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
getUserMedia({ video: true, audio: true })
.then(stream => {
console.log('Got MediaStream:', stream);
})
.catch(error => {
console.error('Error accessing media devices.', error);
});
Peer Connection
The webrtc-adapter package helps manage peer connections by normalizing the API across different browsers. This code sample demonstrates how to create a peer connection and handle ICE candidates.
const adapter = require('webrtc-adapter');
const pc = new RTCPeerConnection();
pc.onicecandidate = event => {
if (event.candidate) {
console.log('New ICE candidate:', event.candidate);
}
};
pc.createOffer()
.then(offer => pc.setLocalDescription(offer))
.then(() => console.log('Offer created and set as local description'))
.catch(error => console.error('Error creating offer:', error));
Other packages similar to webrtc-adapter
simple-peer
The simple-peer package is a lightweight wrapper around the WebRTC API that simplifies peer-to-peer data, video, and audio connections. Unlike webrtc-adapter, which focuses on normalizing the WebRTC API across browsers, simple-peer provides a higher-level abstraction for creating and managing peer connections.
peerjs
The peerjs package is a library that simplifies WebRTC peer-to-peer data, video, and audio connections. It provides a server component for managing peer connections, which is different from webrtc-adapter's focus on API normalization. PeerJS is more focused on providing an easy-to-use API for creating peer connections.
rtcpeerconnection
The rtcpeerconnection package is a simple wrapper around the RTCPeerConnection API, providing a more straightforward interface for creating and managing WebRTC connections. Unlike webrtc-adapter, which aims to normalize the WebRTC API across different browsers, rtcpeerconnection focuses on simplifying the use of the RTCPeerConnection API.
webrtc-adapter
Commonjs adapter.js browser compatibility shim for webRTC
About
WebRTC Adapter provides a more standards-compliant version of
browser RTC objects for use in browser projects using WebRTC.
It is meant for requireJS or browserify'ed
projects, which use a node-style require
syntax, while still running in
a chrome or firefox (at the moment) browser.
In particular, the interface exported by this module attempts to closely mirror
the standard documented by the w3c. In practice,
both the Chrome and Firefox implementations diverge from this standard. Previously,
examples have been commonly built around adapter.js,
a shim that attempts to sandardize many of these differences between browsers.
However, this adapter lives canonically deep within a samples directory, and is not
well suited towards inclusion as a dependency within a larger projects.
This implementation is different in 3 regards.
- The underlying implemenation is discovered via the existance of prefixed properties,
rather than explicit probing of the
navigator
object. This allows the code to run
in firefox extensions while the original adapter could not. - This implementation uses node style exports, rather than attempting to export a fixed
global object.
- This implementation is released un an Apache 2.0, rather than BSD license.
Usage
var MyPeerConnection = require('webrtc-adapter').RTCPeerConnection;
...