Few weeks ago I bought raspberry PI 2 Model B to my elder kid to encourage him to program something and start hacking. And as it often happens I also started to explore new toy and its capabilities. Since I am working in Azure Media services as developer, first project I actually started to implement myself is building security camera which will stream live video through Azure Media Services. I can look what’s happening around my house from work or from any place where I have access to internet. Looking into device specs and browsing similar projects I concluded that I should be able to archive my goals relatively fast and I started with hacking.
You don’t actually need to plugin monitor, keyboard , mouse if you planning to access your PI from your computer via SSH.
You can use either a usb camera or Raspberry PI Camera module. You should get better results with Raspberry PI Camera module since it has a dedicated bus with better throughput compare to usb connection.
I decided to install Ubuntu Mate instead of Raspbian based on minimal requirements of Mate in order to have full desktop experience for my kids and potentially wider variety of GUI tools. Ubuntu Mate has image optimized for PI.Since I used Ubuntu once a while,I decide to make it my default OS for Raspberry PI .
Minimum requirements from Ubuntu Mate web site:
Raspberry PI Specs:
To write iso image to sd card in Windows simply follow instructions from https://www.raspberrypi.org/documentation/installation/installing-images/windows.md.
Once OS installed you need to configure SSH for remote access, Wifi and make sure that you able to browse internet.
sudo nano /boot/firmware/config.txt
Just add a line “start_x=1” at the bottom of the file config.txt, save it, and reboot the system.
Try command “sudo raspistill -o test.jpg” to see if it is working
FFMPEG is powerful tool to work with video and able to encode and push encoded video to live stream channels.
I found and follow instructions from Jeff Thomson blog post which explains how to build ffmpeg for ARM processor and have hardware acceleration turned on.
By defaults you will not have hardware acceleration if you will download precompiled binaries from ffmpeg site. So you need to get source code and compile it for Raspberry PI.
Here is a summary of steps you need to perform:
sudo apt-get install makeinfo texinfo texi2html automake
git clone git://git.videolan.org/x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
sudo make install
git clone git://source.ffmpeg.org/ffmpeg.git
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
sudo make install
Testing ffmpeg and Camera module:
Once you have all software
Now you have to provision Azure media services services and create live channel.
At this point you have all your hardware,software configured and prepared, live channel is ready to get a live stream from your Raspberry PI device.
It is time to start streaming. I created a simple bash script which using ffmpeg to stream from camera.
INGESTURI=”Paste live channel ingest url here from Azure Media Services”
ffmpeg -framerate 30 -r 30 -s 640×480 -i /dev/video0 -vcodec libx264 -preset ultrafast -acodec libfaac -ab 48k -b:v 500k -maxrate 500k -bufsize 500k -r 30 -g 60 -keyint_min 60 -sc_threshold 0 -f flv $INGESTURI
chmod u+x ~/azure_ffmpeg
We created a script ~/azure_ffmpeg. modprobe bcm2835-v4l2 maps PI camera module as /dev/video0 device. if you are using usb camera then you don’t need this line.
Then we are launching ffmpeg and telling it to stream video from /dev/video0 device to our channel with 500k bit rate using ultrafast preset of libx264 codec. We are also instructing to use audio codec since as of now Azure Media Services requires to have both video and audio to be streamed to channel.
Once script is created and saved chmod command is used to grant script execution permissions. Finally sudo ~/azure_ffmpeg launching stream processing.
While script is running you can preview it and publish through portal. Publisher url is url you can share with world to watch your Raspberry PI live stream.
In this article i showed you how you can use portal to configure and start live channel. My next steps will be create scripts, so i can manage live channels though raspberry PI itself. So stay tuned.