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:
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.
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.