Building a Raspberry Pi 2 WebRTC cameraFollow article
Using Janus and gStreamer to feed video straight into the browser.
WebRTC enables browser-based Real Time Communications (RTC) via simple APIs. It is royalty free and powerful. We can use Janus, a general purpose WebRTC gateway, to stream video from a Raspberry Pi directly to browsers, without having to install any extra software on client machines.
We will use a gStreamer pipeline to take the video output from a Raspberry Pi camera module and encode the video in H.264 format before passing it on to Janus.
- Raspberry Pi 2
- USB Wifi adaptor
- 8GB micro SD card
- Raspberry Pi Camera
Initial Raspberry Pi setup
To begin with we will assume that:
- you are running the latest version of Raspbian (version 'January 2015' at time of writing).
- you have a Raspberry Pi camera module attached.
- the network is configured and you can SSH into the Pi.
First we will enable the Pi camera module:
- $ sudo raspi-config
- Choose 'Enable Camera' and press return
- Save and quit raspi-config
Testing the camera
Now that we have enabled the camera module we can test that it is working correctly using the command line tool raspistill.
$ raspistill -o test.jpg
This should have taken a photo. Type ls to see if test.jpg has appeared in your directory.
Copy this photo over to your local machine and open it with an image viewer. If it needs rotating you can use the options -vf and -hf for vertical and horizontal flipping when taking the photo. E.g:
$ raspistill -vf -hf -o test2.jpg
Once you are happy the camera is working and you know which extra options to use we can move on to getting the camera streaming video over the network.
Setting up Janus
It is worth taking a look at the Janus GitHub repository for some background information and to assist with installation.
First make sure your Apt cache is up to date:
$ sudo aptitude update
Next we need to install build dependencies as follows:
$ sudo aptitude install libmicrohttpd-dev libjansson-dev libnice-dev libssl-dev libsrtp-dev libsofia-sip-ua-dev libglib2.0-dev libopus-dev libogg-dev libini-config-dev libcollection-dev pkg-config gengetopt libtool automake dh-autoreconf
We will not be making use of Data Channels, WebSockets or RabbitMQ so we don't need to install the optional dependencies.
Next we can clone the repository onto the Pi, build and install:
$ git clone https://github.com/meetecho/janus-gateway.git
$ cd janus-gateway
$ sh autogen.sh
$ ./configure --disable-websockets --disable-data-channels --disable-rabbitmq --disable-docs --prefix=/opt/janus
$ sudo make install
Now run a comand to install default configuration files:
$ sudo make configs
Next modify /opt/janus/etc/janus/janus.plugin.streaming.cfg (we will use nano but you may use an editor of your choice):
$ sudo nano janus.plugin.streaming.cfg
Add the following lines (and comment out the other example streams by adding a semicolon before each line)
type = rtp
id = 1
description = RPWC H264 test streaming
audio = no
video = yes
videoport = 8004
videopt = 96
videortpmap = H264/90000
videofmtp = profile-level-id=42e028\;packetization-mode=1
Save and exit the editor.
Installing gStreamer and Nginx
gStreamer is a pipeline-based multimedia framework that we will use to encode the video for streaming. To install gstreamer:
$ sudo aptitude install gstreamer1.0
Nginx is a lightweight web server that we will use to serve the Janus demos, specifically the streaming example. To install this:
$ sudo aptitude install nginx
Now copy the Janus HTML content to the Nginx server root:
$ sudo cp -r /opt/janus/share/janus/demos/ /usr/share/nginx/www/
Now start Nginx:
$ sudo service nginx start
Now open two SSH shell sessions logged into the Pi. We will run one command in each shell.
In the first shell execute the following:
$ raspivid --verbose --nopreview -hf -vf --width 640 --height 480 --framerate 15 --bitrate 1000000 --profile baseline --timeout 0 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! udpsink host=127.0.0.1 port=8004
This will take video from the camera, format it and pipe it into gStreamer to be H.264 encoded, before sending it on to Janus.
In the second shell navigate to /opt/janus/bin/ and execute:
$ ./janus -F /opt/janus/etc/janus/
This starts the Janus WebRTC gateway.
On a different computer connected to the same network, ensure you running a recent version of Firefox (we are using build 37). Enter the following into the address bar:
This will load the streaming demo that is bundled with Janus. Click the 'Start' button next to the heading, choose your stream and click 'Watch or Listen' to display the video stream from the Pi directly into your browser!
To complete the hardware side of the project we made a simple enclosure for the Pi and Pi camera module that can be mounted on a tripod or CCTV camera mount.
See attached for more details, including the design files for laser cutting.
This project is shared under - Creative Commons Attribution 3.0 Unported (CC BY 3.0)