I’ve been distracted by other things, but just in case it’s useful to anyone, here’s how to make a HackspaceHat with one-way streaming audio and video (i.e. audio and video streaming from a Pi to a remote server).
We’re now thinking about different ways of doing this, as we have been having lots of problems with ports, and we’re using WebRTC only to stream to a server, not peer-to-peer, and only one-way. But anyway. It’s easy and it mostly works.
This excellent article got me started – I added audio, and the remote server, plus the gstreamer commands have changed slightly.
You will need:
- A Raspberry Pi 2 with a Raspi camera attached and wifi (RT5370 chipset is easiest, especially if want to create an access point) or ethernet, a cheapo USB soundcard and a small cheap 3.5m jack microphone (I have this one from Maplin, which has a pleasingly 70’s feel when you detach it from its holder)
- A server, for example a Linode, running Ubuntu 14. I haven’t been able to make it work on AWS for unidentified reasons.
- A web browser compatible with WebRTC
Set up the Pi
This is how I do it on a Mac. Here are the official instructions.
diskutil list
diskutil unmountDisk /dev/diskn
sudo dd bs=1m if=~/Downloads/2015-05-05-raspbian-wheezy.img of=/dev/rdiskn
(where ‘n’ is the disk number from diskutil list
, on mine, usually, ‘2’).
I’ve not tried this with jessie yet – no reason to think it wouldn’t work.
Then boot up the Pi and ssh in using network sharing and an ethernet cable, or use a keyboard and HDMI screen. Log in.
Finish the config and enable camera
sudo raspi-config
- expand filesystem
- enable camera
Reboot.
On the Pi
Here we set up gstreamer to send the output of the Raspi camera to Janus, a webRTC gateway, which can be on a local or remote server.
Update the Pi
sudo apt-get update -y && sudo apt-get upgrade -y
Install gstreamer
sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad libgstreamer-plugins-base1.0-dev
Install Janus on the pi
For a self-contained, easily testable system we also need Janus and nginx on the Pi. Later we’ll install these two on the server too.
Install prerequisites:
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
Install Janus:
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
make
sudo make install
sudo make configs
Configure Janus
sudo pico /opt/janus/etc/janus/janus.plugin.streaming.cfg
[gst-rpwc]
type = rtp
id = 1
description = RPWC H264 test streaming
audio = yes
audioport = 8005
audiopt = 10
audiortpmap = opus/48000/2
video = yes
videoport = 8004
videopt = 96
videortpmap = H264/90000
videofmtp = profile-level-id=42e028\;packetization-mode=1
Install nginx
sudo aptitude install nginx
Install a modified HTML page
cd /usr/share/nginx/www
sudo cp -r /home/pi/janus-gateway/html/* .
sudo curl -O https://gist.githubusercontent.com/libbymiller/70ad942d070853167659/raw/acc38f4ccefb2c333a4cca460ec52a3151cbeebd/janus-gateway-streamtest.html
sudo service nginx start
Test
in one terminal window:
raspivid --verbose --nopreview -hf -vf --width 640 --height 480 --framerate 15 --bitrate 1000000 --profile baseline --timeout 0 -o - | gst-launch-0.10 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! udpsink host=127.0.0.1 port=8004 alsasrc device=plughw:Set ! audioconvert ! audioresample ! opusenc ! rtpopuspay ! udpsink host=127.0.0.1 port=8005
in another:
/opt/janus/bin/janus -F /opt/janus/etc/janus
Go to http://pi-ip/janus-gateway-streamtest.html in Firefox (Chrome doesn’t support h264)
You should get audio and video streaming from your Pi.
On a server
Next we want to stream that to a remote server. The simplest way of doing this is to tell gstreamer to send the streams to the IP of the remote server. In theory we ought to be able to get WebRTC to handle this via Janus, but I’ve not been able to figure out how, nor whether it’s worth it – for our usecase you’re always going to want to stream via a server, we think.
Deploy a server
I used a Linode – Linode 2048, 48GB DISK 2 CPU Cores 3TB XFER .03/hr to $20/mo with 64 bit Ubuntu 14 LTS.
Install Janus and Nginx on the server
sudo apt-get update -y && sudo apt-get upgrade -y
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
you may need to do
sudo apt-get install gupnp-igd-1.0
Install Janus:
sudo apt-get install git
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
make
sudo make install
sudo make configs
Configure Janus
sudo pico /opt/janus/etc/janus/janus.plugin.streaming.cfg
[gst-rpwc]
type = rtp
id = 1
description = RPWC H264 test streaming
audio = yes
audioport = 8005
audiopt = 10
audiortpmap = opus/48000/2
video = yes
videoport = 8004
videopt = 96
videortpmap = H264/90000
videofmtp = profile-level-id=42e028\;packetization-mode=1
Install nginx
sudo aptitude install nginx
Install a modified HTML page
cd /usr/share/nginx/html/
sudo cp -r /opt/janus/share/janus/demos/* .
sudo curl -O https://gist.githubusercontent.com/libbymiller/70ad942d070853167659/raw/acc38f4ccefb2c333a4cca460ec52a3151cbeebd/janus-gateway-streamtest.html
sudo service nginx start
Test it
On the pi:
raspivid --verbose --nopreview -hf -vf --width 640 --height 480 --framerate 15 --bitrate 1000000 --profile baseline --timeout 0 -o - | gst-launch-0.10 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! udpsink host=ip-of-server port=8004 alsasrc device=plughw:Set ! audioconvert ! audioresample ! opusenc ! rtpopuspay ! udpsink host=ip-of-server port=8005
On the server:
/opt/janus/bin/janus -F /opt/janus/etc/janus
on the server you should see
[gst-rpwc] New video stream! (ssrc=460806300)
[gst-rpwc] New audio stream! (ssrc=3733839967)
go to http://ip-of-server/janus-gateway-streamtest.html in Firefox.
Enabling Wifi on the Pi
Just for reference.
Wpasupplicant is normally installed. If it isn’t, do:
sudo apt-get install -y wpasupplicant
Then edit:
sudo pico /etc/wpa_supplicant.conf
network={
ssid="YourWifiNetworkName"
psk="YourWifiNetworkPassword"
}
A version that works only within 10-30 metres
This version uses the wifi card as an access point, so to view the video / audio you connect to the Pi’s access point and then go to the web page on the Pi. Not sure how useful it is, but it’s quite fun, and easy, as you don’t need a separate server on the internet.
Create access point
This is a script we used for Radiodan, slightly modified to just create an access point.
git clone https://github.com/radiodan/provision.git
cd provision
replace the contents of steps/wpa/install.sh
with
sudo apt-get install -y --force-yes dnsmasq && \
sudo apt-get install -y --force-yes ruby1.9.1-dev hostapd=1:1.0-3+deb7u2 wpasupplicant && \
sudo gem install --no-ri --no-rdoc wpa_cli_web
then
sudo mkdir /var/log/radiodan
sudo LOG_LEVEL=DEBUG ./provision avahi nginx wpa
change name of network
sudo pico /etc/hostapd/hostapd.conf
change ssid to
ssid=hackspacehat
edit /etc/nginx/sites-enabled/wpa_cli_web_redirect
just this:
server {
listen 80;
root /var/www/html;
}
edit /opt/radiodan/adhoc/try_adhoc_network
to remove
# Do 6 scans over 1 min
#for i in {1..6}
#do
# echo "Scan $i of 6"
# /sbin/wpa_cli scan
# /bin/sleep 10
#done
and also
#echo "Starting wpa-cli-web"
#/etc/init.d/wpa-cli-web start
remove wpa server from init.d
sudo update-rc.d wpa_cli_web remove
sudo update-rc.d wpa-conf-copier remove
reboot
unplug ethernet if plugged in
connect to hackspacehat to check it works – you should be able to ssh to 10.0.0.200
.
(this is overkill, but I haven’t had time to investigate a quicker way)
Janus and gstreamer commands
As in the first example, we just run gstreamer sending the streams to localhost, and Janus on the pi.
So two windows on the pi, one sending the streams:
raspivid --verbose --nopreview -hf -vf --width 640 --height 480 --framerate 15 --bitrate 1000000 --profile baseline --timeout 0 -o - | gst-launch-0.10 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! udpsink host=127.0.0.1 port=8004 alsasrc device=plughw:Set ! audioconvert ! audioresample ! opusenc ! rtpopuspay ! udpsink host=127.0.0.1 port=8005
one running Janus:
/opt/janus/bin/janus -F /opt/janus/etc/janus
then connect to AP “hackspacehat” and
go to http://10.0.0.200/janus-gateway-streamtest.html
You should get audio and video from your Pi, which will work while you are in range of the Pi’s access point.