WebRTC data channels

UV4L for the Internet of Things with WebRTC Data Channels

For the impatient reader, this is a short demo of the kind of things that is possible to do with WebRTC Data Channels:

Content not available.
Please allow cookies by clicking Accept on the banner

The WebRTC extension for UV4L supports WebRTC data channels. To know how to install or update the UV4L components, please read these instructions.

By means of UV4L, WebRTC data channels allow real-time, secure and full-duplex data exchange between a remote WebRTC-complaint peer (e.g. a browser) and a third-party application running on localhost. By default the data channel is bundled onto the same transport as audio and video tracks (if present) in one peer connection.

In other words, the data flow that is made possible by UV4L can be represented by the following pipeline:

your app <–(unix domain socket)–> UV4L <–(WebRTC data channel)–> remote peer

UV4L acts as bridge connecting two parties of a communication channel (your application and the remote peer). It instantly relays any incoming or outgoing messages from one endpoint to the other.

More in detail, as both UV4L and the application endpoint run on the same local host as two distinct system processes, the inter-process communication between them is best accomplished with a Unix Domain Socket of type SOCK_SEQPACKET (this type guarantees a reliable connection-oriented socket that preserves message boundaries and delivers messages in the order that they were sent).

In order to communicate with UV4L, the only requirement on the local application is that it has to create the Unix Domain Socket, listen and accept incoming connections (from UV4L). In facts, after having successfully created, offered and established a WebRTC data channel with the remote peer (on the left side of the pipeline), UV4L attempts to open the local socket on the other side. One attempt every second is automatically made until it gets connected or on possible connection failures. Once the local connection is ready, the local application can start to send or receive any messages to or from the remote peer transparently by simply writing or reading to or from the socket respectively. When the WebRTC connection with the remote peer terminates, UV4L closes the socket in turn, so to terminate the connection with the application.

For instance, all this can be extremely useful for remotely controlling the servo motors of a robot from a Web application with a few lines of Javascript code based the standard WebRTC API, which is now supported by any modern browser; e.g. commands may be sent in form of messages from the user interface to the device, where the messages could be actuated by translating them into real OS commands. The opposite direction is also possible, e.g. the application could push messages representing data coming from various sensors mounted on the robot into the pipe, while the Web application, on the other end, can consume them as soon as they are received.

The uv4l-webrtc package comes with a simple echo server demonstrating the overall concept. Every time the server receives a message, it sends the same message back to the sender. The source code in C++ is here. It can be invoked at any time as follows:

pi@raspberrypi:~ $ datachannel_server /tmp/uv4l.socket

To quickly test the bidirectional data channels through the Web interface, open the WebRTC Web page at the UV4L Streaming Server. There you will see a “Data Channel” section with an input and an output text fields inside. Start an audio/video call as usual. At this point the data channel should be opened too, fill the input field with any message and click on the send button to send it to the echo server. You will immediately receive the same message back and have it displayed on the output field.

A simple example of unidirectional data channel that has been used to control servos on a drone can be found here in python. Another example of use of data channels in a computer vision project can be found in the following article (part 1, part 2).

WebRTC data channels are enabled by default with /tmp/uv4l.socket as default path for the socket. The default values can be changed through the relevant options via command line or changed in the configuration file. Please refer to the uv4l-server manual for more details.

IMPORTANT: If you want to use WebRTC data channels with the Janus Gateway, make sure it is built with the support for WebRTC data channels turned on, otherwise the WebRTC connection will be rejected by Janus and fail miserably.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.