Filetransfer via WebRTC Data Channels!

In this example we will see how to run UV4L to serve a simple custom web application which allows to securely and efficiently transfer a file from the browser to the server (the host where UV4L itself is running) via a WebRTC data channel. Any browser on any device (PC, Android, etc..) should be supported and no third-party plug-in needs to be installed, as the web application is entirely based on web standards.

For the sake of simplicity, we will deploy this web application on a Raspberry Pi, although the procedure is the same for all other supported architectures.

First of all, install all the packages required according to these instructions: uv4l, uv4l-dummy, uv4l-server, either uv4l-webrtc or the one specific for your architecture (e.g. uv4l-webrtc-armv6) and uv4l-demos.

Secondly, as we will enable HTTPS on the Streaming Server to serve the web application, we need a valid SSL key and SSL certificate. A way to generate them, if you don’t have them already, is mentioned in the same instructions as above.

Now let’s log into the Raspberry Pi and run one instance of UV4L. Note that in this example we are only interested in showing a possible use of data channels, therefore we can ignore and disable any audio video and audio streams:

$ uv4l --auto-video_nr --driver dummy --server-option=--enable-builtin-ui=no --server-option=--enable-www-server=yes --server-option=--www-root-path=/usr/share/uv4l/demos/filetransfer/ --server-option=--www-index-file=index.html --server-option=--www-port=4443 --server-option=--www-webrtc-signaling-path=/ --server-option=--www-use-ssl=yes --server-option=--www-ssl-private-key-file=/etc/ssl/private/selfsign.key --server-option=--www-ssl-certificate-file=/etc/ssl/private/selfsign.crt --server-option=--enable-webrtc-video=no --server-option=--enable-webrtc-audio=no --server-option=--enable-webrtc-datachannels=yes --server-option=--webrtc-datachannel-socket=/tmp/ftp.socket

To know what most of those options mean please refer to the server manual. Yes, the command is rather long, but all those options could be put and read from a configuration file instead. This is left as an exercise to the reader.

Now we can run the previously installed file_server program which just opens the local /tmp/ftp.socket and listens to any incoming data that is forwarded by uv4l whenever it in turn receives some data from the data channel:

$ file_server /tmp/ftp.socket ~/

The second argument in the command line, if specified, represents the directory where the received files will be written to. If no directory is specified, the program simply writes the file content to standard output. This is useful if you want to pipe the data being received to another command. For example, to create a simple mp3 music player you can run the following:

$ file_server /tmp/ftp.socket | mpg123 -

The source code of the file_transfer program used in this demo can be found here, while the source code of the web application can be found under /usr/share/uv4l/demos/filetransfer/.

Now let’s try all this out! You can connect to the UV4L Streaming Server with the browser. The URL should be something like https://<rpi_address>:4443, where <rpi_address> has, of course, to be replaced with the real hostname or IP address of the Raspberry Pi in your network. A page like the following should appear in the browser:

filetransfer

Choose a file and click on the Send File button to start the transfer. When it’s completed a pop-up should come up confirming that the transfer went well. You can open the Javascript console on the web page (press F12) to see some debugging messages about what’s going on under the hood during the transfer.

Both the web application and the file_transfer are deliberately simple. The protocol is rather simple and is not resilient to every possible error in the communication (should something go wrong, click on Abort and retry). However, you are encouraged to look at the source code and to improve it to best fit your needs.