Live video streaming via Wi-Fi

To finish this chapter, we are going to see another cool application that we can make with the Arduino Yún and our USB camera. Remember that the camera is actually a standard webcam, and that it is also made to capture videos. Wouldn't it be cool to automatically stream video on a private video channel on the Web, so you can watch over your home in real time from anywhere just by going into a web browser? That's exactly what we are going to do in this section.

Many commercial IP cameras are actually doing this with proprietary solutions, but I wanted to use commonly available tools; this is why we chose the YouTube live event service to stream the video that can then be accessed from any device.

To make the application work, we first need to install some additional software packages on the Yún, as shown in the following steps:

  1. Connect to the Yún again using SSH with your Arduino Yún name and password, and type the following command to get the correct package for live streaming:
    wget http://www.custommobileapps.com.au/downloads/mjpg-streamer.ipk
    
  2. Note that if the link is not valid anymore and you can't find the files, this package is also available inside the code of this chapter. You can now install it with the following command:
    opkg install mjpg-streamer.ipk
    
  3. You can now start the live streaming software on your Arduino Yún using the following command:
    mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 640x480 -f 25" -o "output_http.so -p 8080 -w /www/webcam" &
    

    Here, the parameter after –h is the resolution and the one after –i is the port on which the stream will be available. We also specified the number of frames per second using the –I command. The other options are less important and you do not have to worry about them.

Note that we didn't stream at HD resolution; it was apparently too much for the Arduino Yún, and the video stream suffered significant lag and also had corrupted images, which is not what we want at all. You can then access your stream by going to your Arduino Yún's address in your web browser followed by 8080 to specify the correct port. For example, in my case, it was http://myarduinoyun.local:8080/stream.html.

This actually gives you direct access to the live stream. You should then see the stream interface with the live stream in the middle of the page, as shown in the following screenshot:

Live video streaming via Wi-Fi

You can also use the different elements of the menu on the left to explore other possibilities of this streaming software. For example, you can get a link for VideoLAN, so you can access your stream right from the VLC player.

Now, this is already great and you could stop here to access your video stream from your local Wi-Fi network. But it would be even better if the stream was available online, so you could access it from anywhere in the world even without being connected to your local Wi-Fi network.

The first step is to go to your YouTube account in VIDEO MANAGER and to the Live Events menu on the left, as shown in the following screenshot:

Live video streaming via Wi-Fi

From this menu, you can create your stream just like you would create a new YouTube video. Make sure that you put the video as unlisted unless you want other people on YouTube to be able to see what's going on in your home. Compared to a private video, you will still be able to share the video with the people you know just by giving them the URL of the stream. Then, on the next page, YouTube will ask you to choose which encoder you want to use.

I chose Wirecast from the list and downloaded it from their website. In the Wirecast interface, you need to set the correct video source (by default, it will stream from your computer's webcam). In the menu where you can select the video source, select Web Stream Source and configure it, as shown in the following screenshot:

Live video streaming via Wi-Fi

Basically, you need to choose HTTP as the protocol, use Motion JPEG as the format, and put the URL from the VideoLAN tab of the streaming interface. For example, for my project, it was myarduinoyun.local:8080/?action=stream.

After a moment, if everything is working fine, you should see the live stream from your USB camera appear directly in the main window of Wirecast. Don't worry if there is some lag at this point; it is only a delay usually; in my case, I had about 1-2 seconds of delay in the Wirecast software. The following is the image I got in the main Wirecast interface after adding the right video stream:

Live video streaming via Wi-Fi

Also, make sure that this stream is the only one that will be sent to YouTube. For this purpose, delete all the other streams from the Wirecast interface. Indeed, by default, Wirecast puts the stream that comes from your webcam on the interface.

The next step is to actually stream data to YouTube. Click on the Stream button at the top of the interface, which should turn red, after which you will be prompted to enter your YouTube credentials. It should then automatically detect your live event video that you just created on YouTube.

Accept the settings, make sure it is streaming from Wirecast, and go back to the YouTube interface. You can now go to the video manager, and go to the Live Control Room tab. This is where you should see that YouTube is actually receiving some data from your Arduino Yún via Wirecast running on your computer. It should indicate that the Stream Status is GOOD, as shown in the following screenshot:

Live video streaming via Wi-Fi

If this is not the case, please go back to the Wirecast application to check that the streaming process is working correctly. At this moment, don't worry; your stream is not working just yet. You should see that the Preview button, as shown in the following screenshot, is now available and can be clicked. Just click on it.

Live video streaming via Wi-Fi

YouTube will then prepare your stream, as shown in the following screenshot, and you will have to wait for a moment (around 30 seconds when I tried it):

Live video streaming via Wi-Fi

After a while, the page will be updated automatically so that you can move to the next step and actually start the streaming, as shown in the following screenshot:

Live video streaming via Wi-Fi

Note that before making the stream live, you can preview it using the options on the preceding page. If what you see is satisfactory, you can now click on Start Streaming to finally finish the process. You will then have access to the stream on this page or directly on the dedicated page of the stream. The following screenshot is the final result on the YouTube interface:

Live video streaming via Wi-Fi

You can see from the red dot below the video that the video is streaming live. Because the video is marked as Unlisted, only people with the URL can access it. You can, for example, mark the video as a favorite in your YouTube account and then access it from anywhere. You can also share it with your family and friends, so they can also watch the stream from their browsers.

Note that because we are using the Wirecast software on our computer to encode the stream for YouTube, we need to have our computer on for this to work. At the time this book was written, no software was available to directly stream the video to YouTube without the help of a computer, but this might change in the future, removing the need for a computer to stream the video.