Streaming live video from RaspberryPI to Azure Media Services

Posted on

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.

Hardware

  • Raspberry PI 2 Model B
  • SD Card 16 GB
  • Microsoft LifeCam 6000/ Raspberry PI Camera module
  • USB wifi adapter if you don’t have wired Ethernet connection
  • Keyboard (optional)
  • Mouse(optional)
  • Monitor(optional)

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

Configuring required software

Installing Ubuntu Mate

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:

  • Pentium III 750-megahertz
  • 512 megabytes (MB) of RAM
  • 8 gigabytes (GB) of available space on the hard disk
  • Bootable DVD-ROM drive
  • Keyboard and Mouse (or other pointing device)Video adapter and monitor with 1024 x 768 or higher resolution
  • Sound card
  • Speakers or headphones

 

Raspberry PI Specs:

  • A 900MHz quad-core ARM Cortex-A7 CPU
  • 1GB RAM
  • 4 USB ports
  • 40 GPIO pins
  • Full HDMI port
  • Ethernet port
  • Combined 3.5mm audio jack and composite video
  • Camera interface (CSI)
  • Display interface (DSI)
  • Micro SD card slot
  • VideoCore IV 3D graphics core

 

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.

Enabling RaspberryPi camera module in Ubuntu Mate

modifyfile /boot/firmware/config.txt
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

Installing FFMPEG

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:

Install build tools

sudo apt-get install makeinfo texinfo texi2html automake

Compile amd install H264 libraries:

cd /usr/src
git clone git://git.videolan.org/x264
cd x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
sudo make
sudo make install

Compile amd install FFMPEG:

git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
sudo make
sudo make install

Testing ffmpeg and Camera module:

Once you have all software

 

Configuring Live channel In Azure Media Services

Now you have to provision Azure media services  services and create live channel.

  • Go to https://azure.microsoft.com and create account if you don’t have one. There are free trial offers available to play
  • Provision Azure Media Services account

    Provision Azure Media Services Account

    Provision Azure Media Services Account

  • Create Live Channel

    Create Azure Media Services Channel For RaspberryPI

    Create Azure Media Services Channel For RaspberryPI

  • Specify Channel name and description

    Specify Azure Media Services Channel Proerties

    Specify Azure Media Services Channel Properties

 

  • Select RTMP ingest protocol
    Azure Media Services Live Channel Protocol

 

  • Specify ingest restrictions
    Live channel ingest restrictions

    Live channel ingest restrictions

    Copy Live Stream Ingest Urls

    Copy Live Stream Ingest Urls

Pushing live stream from Raspberry Pi to Azure Media Services 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.


nano ~/azure_ffmpeg

#!/bin/bash
modprobe bcm2835-v4l2
INGESTURI=”Paste live channel ingest url here from Azure Media Services”
while :
do
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
sleep 10
done

chmod u+x ~/azure_ffmpeg
sudo ~/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.

Preview Azure Media Services live stream

Preview Azure Media Services live stream

 

PreviewLiveChannelWithGeorgeInOffice

What next

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.

Tags: ,


  • Hello I have tried to run camera capture from my windows 10 but to be honestly I do not have much experience with ffmpeq so I was wondering if you can help me with a ffmpeq script. I Have tried the script below but is not working

    ffmpeg -f dshow -i video=”HP Webcam-101″ -f flv rtmp://testcast-licentamediaservice.channel.mediaservices.windows.net:1935/live/8640c77496cb40f283f6fb7aa7b29ceb/stream1

  • what if I want to use a webcam? what are the necessary changes?

    • If you are using webcam then linux should detect is /dev/video0.
      You can skip command “modprobe bcm2835-v4l2”

  • Simon

    Hello, thanks a lot for this nice tutorial…

    When i start the script it shows following output every 10 seconds.

    But in Azure Portal it says “A network error caused the video download to fail part-away….” when i start playing published url.

    Do you have any idea wheres the problem?


    ffmpeg version N-73832-g736a386 Copyright (c) 2000-2015 the FFmpeg developers
    built with gcc 4.6 (Debian 4.6.3-14+rpi1)
    configuration: –arch=armel –target-os=linux –enable-gpl –enable-libx264 –enable-nonfree
    libavutil 54. 28.100 / 54. 28.100
    libavcodec 56. 49.101 / 56. 49.101
    libavformat 56. 40.101 / 56. 40.101
    libavdevice 56. 4.100 / 56. 4.100
    libavfilter 5. 25.100 / 5. 25.100
    libswscale 3. 1.101 / 3. 1.101
    libswresample 1. 2.101 / 1. 2.101
    libpostproc 53. 3.100 / 53. 3.100
    [video4linux2,v4l2 @ 0x2011610] fd:3 capabilities:85200005
    Input #0, video4linux2,v4l2, from ‘/dev/video0’:
    Duration: N/A, start: 4119.288737, bitrate: 283115 kb/s
    Stream #0:0: Video: rawvideo, 1 reference frame (I420 / 0x30323449), yuv420p, 1024×768, 283115 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
    [graph 0 input from stream 0:0 @ 0x2010c40] w:1024 h:768 pixfmt:yuv420p tb:1/30 fr:30/1 sar:0/1 sws_param:flags=2
    [graph 0 input from stream 0:0 @ 0x2010c40] TB:0.033333 FRAME_RATE:30.000000 SAMPLE_RATE:nan

    Thank you!

    • Can you provide command line parameters of ffmpeg? What i found that the best way to diagnose is to start with lowest acceptable bitrate: for example 300K for video and something small for audio. If simplest configuration working than increase quality gradually.

  • Shirshank Deepankar

    Hello

    I am trying to implement Live Video streaming from Raspberry PI 3 on Azure, found your blog while searching .

    Tried to follow the steps, receiving Input/Output error, based on recommendation changed the url by adding “/stream” at end.

    ffmpeg -framerate 30 -r 30 -s 320×240 -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 rtmp://xxxxxxxstream-xxxxxstream.channel.mediaservices.windows.net:1936/live/9bxxxf6bf90xxxx919d7xxx31/stream

    After executing above on command line ffmpeg, while previewing was able to see few second of stream but it wasn’t consistent, have tried multiple options still stream doesn’t play at all.

    Do i am missing any configuration.

    Log:

    pi@raspberrypi:/usr/src/x264/FFmpeg $ ffmpeg -framerate 30 -r 30 -s 640×480 -i /dev/video0 -vcodec libx264 -preset ultrafast -acodec libfaac -ab 48k -b:v 300k -maxrate 300k -bufsize 300k -r 30 -g 60 -keyint_min 60 -sc_threshold 0 -f flv rtmp://xxxxxxxstream-xxxxxstream.channel.mediaservices.windows.net:1936/live/9bxxxf6bf90xxxx919d7xxx31
    ffmpeg version N-81647-gd41bfa9 Copyright (c) 2000-2016 the FFmpeg developers
    built with gcc 4.9.2 (Raspbian 4.9.2-10)
    configuration: –arch=armel –target-os=linux –enable-gpl –enable-libx264 –enable-nonfree
    libavutil 55. 29.100 / 55. 29.100
    libavcodec 57. 55.101 / 57. 55.101
    libavformat 57. 48.103 / 57. 48.103
    libavdevice 57. 0.102 / 57. 0.102
    libavfilter 6. 62.100 / 6. 62.100
    libswscale 4. 1.100 / 4. 1.100
    libswresample 2. 1.100 / 2. 1.100
    libpostproc 54. 0.100 / 54. 0.100
    Input #0, video4linux2,v4l2, from ‘/dev/video0’:
    Duration: N/A, start: 3675.486221, bitrate: 147456 kb/s
    Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640×480, 147456 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
    rtmp://xxxxxstream-xxxx.channel.mediaservices.windows.net:1936/live/9b78xxx6bf90414xxxe1a0a31: Input/output error

  • John Deutscher

    Also, note you should use the “make -j4” option to compile faster on a newer Pi. It will use all 4 cores.
    There are also some recent updates to FFMPEG to support hardware encoding.

    https://ubuntu-mate.community/t/hardware-h264-video-encoding-with-libav-openmax-il/4997/16

    To enable hardware encoding:
    1) add “–enable-omx –enable-omx-rpi” to ./configure options
    2) use encoder option “-c:v h264_omx”

    • Thanks John for Pi3 instructions. Need to buy newer version of PI to try it out.