Jump to content
Sign in to follow this  
Mr.RC-Cam

YouTube Live Stream :: Turtle Camera

Recommended Posts

Perhaps you've noticed the growing popularity of personal live streaming video channels. Thanks to YouTube and some connected webcams there's plenty of romping kittens, wilderness animals, and vacation beaches to watch in real-time.

My interest in video camera projects isn't just FPV/drone related and it was only natural for me to get on the live stream bandwagon. That is to say, now our family pet turtle (named Polly) has the starring role on a reptile's version of The Truman Show. Family and friends can now spy on Polly-the-turtle whenever they want.

pond1b_1000.jpg

Here's a YouTube video from the PollyCam webcam: (Click Image Below)

Live Stream Video


Being a DiY guy, I rolled up my sleeves and built my own video streaming system called PollyCam. The project's hardware (WiFi IP camera & video encoder) is under $150 US and the software is free. A reliable broadband internet connection with a generous monthly data usage plan is needed too.

In case any of you are interested in the technical aspects of this project then you're in luck. Stick around and I'll do my best to explain how to build it.

Share this post


Link to post
Share on other sites

There's a number of ways to connect your personal camera to YouTube Live. A typical installation uses a USB webcam attached to a computer running some encoding software. But my camera is installed outdoors in a weather exposed area near the pond. So I needed a weatherproof WiFi IP security camera that would communicate to a remotely mounted computer installed inside my house.

There are plenty of outdoor security cameras to choose from which can make selecting one a daunting task. I took a chance on a low cost ($80 US) Chinese brand. The camera is a Foscam FI9900P that is rated for 1080P resolution. I'm using its 720p resolution mode and the image quality is nice; The 720p bandwidth requirements are an acceptable match for my encoding hardware and broadband connection.

foscam_stock_photo.jpg

The Foscam camera supports RTSP (Real Time Streaming Protocol) over its WiFi connection. But YouTube Live needs RTMP encoding (Real-Time Messaging Protocol). So this created a square peg and round hole conundrum. Fortunately a $40 US computer module and some free software provides the necessary transcoding that converts RTSP to RTMP.

The computer I'm using is a RaspBerry PI 3 (RPI3) Model B. I already have two of these handing my VoIP phone system (freepbx) and home automation (openhabian).

rpi3_b.jpg

Rather than add another RPI3 dedicated for the video encoding task, I installed the encoding software on my home automation RPI3. It was a risky decision because the video encoding task consumes a lot of CPU resources.

With video encoding and home automation running the CPU loading can vary (depending on amount of motion in the video image) from 50% to 90% utilization. Because of the demands on CPU processing the RPI3's temperature can get quite hot, so a heatsink/fan kit is necessary. Plus operating current is greater than a typical RPI3 installation, so a high current power supply is necessary. I purchased a $17 case / accessory kit that had all these items in it.

case_kit1_1000.jpg

I should mention that a dedicated RPI3 for YouTube streaming is perhaps a better way to go. I have plans to do that when time permits. But so far running the video encoder and home automation on the same RPI3 is working OK, so I'm in no hurry to upgrade it.

I appreciate the RPI3's low cost and tiny size. I use them in headless mode, which means I don't install a keyboard or monitor. So the overall footprint is not much larger than a deck of cards. It has built-in Wi-Fi too, but to ensure reliable connectivity it is wired with a CAT5 cable to a Ethernet hub that is plugged into a 2.4GHz WiFi router.

The router, hub, and RPI3 are shelf mounted in a coat closet inside the house. It's about 150 feet to the wireless IP camera that is hiding in a "birdhouse" above our backyard pond.

birdhouse1_1200.jpg

Share this post


Link to post
Share on other sites

A micro SD memory card is needed for the RPI3's operating system. My experience with running RPI (Raspberry Pi) based systems has taught me to avoid regular SD cards. For reliability I've switched to "high endurance" rated cards. My go-to choice at the moment is the Transcend 16GB TS16GUSDHC10V.

The SD Card needs the Raspian operating system. As I mentioned earlier, I am using an existing RPI3 that has been running my home automation system. It was built a couple years ago and has a Raspian OS that is based on Debian 8 ("Jessie"). There are newer OS versions available for download (all at no cost) that are slightly different. For example, the latest version is Debian 9  ("Stretch").

The Raspian OS version will make a difference. For example, RTSP to RTMP encoding is usually done with a program called ffmpeg. It's available on some Raspian releases. But my Debian 8 OS was released with an alternate encoder called avconv, so that is what I used in this project.

The two programs (ffmpeg & avconv) are said to be identical but I found that there are some nuances that can get in the way. If you are attempting to duplicate this project, and want to avoid unexpected frustrations, then install Raspbian Debian 8 (because it supports the avconv encoder that is used by PollyCam).

There's no need for me to explain how to install Raspian on the SD card. It's well documented online, plus YouTube is full of helpful videos.

After you have installed the OS, and setup the wired Ethernet connection, you can go "headless" by removing the keyboard and monitor. In their place I use two programs (to remotely access the RPI3) called WinSCP and PuTTY on a Windows PC. It's a very clever way to reduce the hardware clutter.

Share this post


Link to post
Share on other sites

Now that the RPI3 has an operating system it's time to install the video encoder software. As mentioned earlier, my RPI3 OS is Debian 8 (Jessie). It directly supports the encoder called "avconv" that is distributed in the apt-get package called libav-tools (libav is a fork of FFmpeg).

The avconv (libav-tools) encoder must be downloaded and installed.  My RPI3 is headless (without keyboard & monitor) so I use the PuTTY program on my WIN10 desktop PC to do this. But if you're not interested in trying out PuTTY then attach a keyboard and HDMI monitor and use them to access the RPI's command line (built-in console terminal).

To install libav-tools, enter the following text on the RPI3's command line:

apt-get install libav-tools 

Then hit Enter and wait a few minutes for the entire file package to download, unpack, and install.

All the hard stuff is out of the way. We're not done yet, but congratulations on getting this far.

 

Share this post


Link to post
Share on other sites

Before we can finish up the RPI3 installation the WiFi camera has to be configured. As mentioned, I am using a Foscam FI9900P. So all my camera setup instructions will be specific to it.

Because the camera will be connected by WiFi I installed its smart phone app (free download from Foscam/Google Play). The nice thing about this app is that it uses a P2P function so that you can scan the QR code (on the camera's printed label) to setup the WiFi connection.  You will be prompted to assign a user name and password. The built-in WiFi scanner function will find your router so your can enter its SSID password. Complete the camera's WiFi setup before proceeding.

At this point your camera is connected to your local network via a WiFi router.

Using your PC's browser (Windows or MAC), go to the Foscam site and download the Equipment Search Tool program. Run the tool and record the camera's IP address:port and 12-digit MAC ID. As an alternative to the Foscam tool you can also find the IP/MAC by accessing your router's DHCP Client table. Or use your favorite method to determine the IP and MAC of a new WiFi device. BTW, the Foscam camera's default port number is 88.

The camera supports direct access from a web browser. Internet Explorer is best, but you can use others. So launch your favorite browser and enter the camera's IP address and port number (IP:port) as the URL. The URL will look something like this (but revise it to match your values):
http://192.168.1.55:88

Install the Web Component add-on when prompted by your browser; Your browser needs it to access the camera. At this point your browser should be able to access your camera's live view and its settings page.

Click the Settings Page tab and choose the Video->Video Settings configuration screen. Use the Stream Type pulldown and choose the User-Defined entry. Change it to 720P resolution, 2M bit rate, 10 Frames, 30 Key Frames, VBR rate control. Click Save.

See screenshot below:

Foscam_screen2_1200.jpg

 

Now click the Live Video tab and choose Outdoor Mode, User-defined Stream, WDR On, NAA Off. The live video image is now configured for 720P video that will be used by your YouTube Live channel. See Screenshot below:

Foscam_screen1_1400.jpg


Now is a good time to access your router's administration function and reserve the DHCP IP address (create static IP) for the camera. Why do this? Configuring your router so that the camera uses a static local IP will ensure that the IP assignment does not change if the router is rebooted. It's not mandatory, but no doubt will reduce future frustrations.

There's a bit more to do, so don't relax yet. 

Share this post


Link to post
Share on other sites

It's time to setup your YouTube live streaming channel. If you're not currently a registered YouTube user then don't worry. All you need is a Google or Gmail account to access YouTube's free video hosting.

Instructions for setting up your YouTube Live Stream are posted here:
https://support.google.com/youtube/answer/2474026?hl=en

In case it helps, here's a random video I found on YouTube that explains how to set it up.

Keep in mind that I don't use the Live Stream Events feature that is discussed in the video. For PollyCam the Stream Now section is all that needs to be configured.

When you have completed setting it up you'll be provided a Server URL and Stream Name/Key (found in the Encoder Setup area of your Stream Now page). These are private text strings (do NOT share them). They are used to configure the RPI3's avcon program, so feel free to copy them into a text file for easy retrieval.

Share this post


Link to post
Share on other sites

All the necessary pieces to creating your YouTube Live Stream are now in place. It's time to stream!

Here's the avconv command for running the live stream video encoder on your Raspberry Pi3 (RPI3):

avconv -rtsp_transport tcp -y -i rtsp://FOSCAM_USERNAME:FOSCAM_PASSWORD@FOSCAM_IP:FOSCAM_PORT/videoMain -vcodec libx264 -preset veryfast -maxrate 2000k -bufsize 4000k -vf "format=yuv420p" -g 10 -acodec libmp3lame -b:a 128k -ar 44100 -f flv -s 1280x720 YOUTUBE_SERVER_URL/YOUTUBE_STREAM_NAMEKEY


But before you use the avconv command you must substitute the values for the following parameters:

FOSCAM_USERNAME: The admin login name for your Foscam camera.
FOSCAM_PASSWORD: The admin password.
FOSCAM_IP: The local IP to your camera.
FOSCAM_PORT: The port number to your camera (default is 88).
YOUTUBE_SERVER_URL: The server URL value provided by your Live Stream account.
YOUTUBE_STREAM_NAMEKEY: The stream name/key value provided by your Live Stream account.

Type the avconv command (with your specific camera and YouTube parameters) into your RPI3 and press Enter. Within 30 seconds the camera's real-time video should appear on your YouTube Live Stream web page. Congratulations, you're now broadcasting live video on your YouTube channel!

Since your live stream system is now up & running this could be the place where we go our separate ways. However, I encountered some unexpected streaming problems that will probably haunt you too. 

For example, broadband slowdowns (data throttling) may cause YouTube to terminate the current live stream connection and require you to restart it. Or maybe your broadband internet account has limited data usage allocations and live streaming is quickly eating it up. Or perhaps you're running your RPI3 in headless mode and you don't want a full time ftp connection to the RPI3.

These situations can be solved with some custom bash script files. I'll share the scripts I wrote in case they help you too.

 

Share this post


Link to post
Share on other sites

Several bash script files have been created to assist PollyCam's video stream process. They will be stored in the default pi user home directory.
The full path we will use is /home/pi/avconv.

Note: If you operate under a different user ID then please revise the home directory path (to suit your user name) and edit the bash files so that the paths referenced in them are accurate. Please be aware that the bash files are unix text formatted (linefeeds only, without carriage returns). Any future edits to them MUST use Unix style formatting. The Raspian "nano" editor is a typical choice for this. I use notepad++ with Edit->EOL Conversion set to Unix(LF).

Begin by creating the avconv directory:

mkdir /home/pi/avconv


Using your preferred method, copy the following bash script files to the /home/avconv directory:

  cpu_status: cpu_status
  cpu_tempcpu_temp
  pollycam_launch: pollycam_launch
  pollycam_run: pollycam_run
  pollycam_start: pollycam_start
  pollycam_stop: pollycam_stop

Use the chmod command to allow command execution of the bash files:

chmod 755 /home/pi/avconv/*

The bash files are now ready for use. As a test, run the cpu_temp program (display CPU temperature) and confirm it works. You must include the full path when you run it, as follows:

/home/pi/avconv/cpu_temp

After you see it working you can end the program with CTRL-Z.

 

Share this post


Link to post
Share on other sites

The two main Live Stream scripts to use are:

pollycam_stop: Terminates ALL PollyCam Youtube Live Stream processes that may be running. Run this script to turn off live streaming whenever you are done broadcasting. Also, always run this script BEFORE making any changes to the other PollyCam scripts.

pollycam_launch: This is the main script that launches PollyCam streaming for unattended operation. It runs as a background process. You may exit the terminal command shell window (live streaming processes will continue to run without the terminal window).


The sub-scripts used by the main pollycam_launch processes are:

pollycam_run: This is a sub-script that is called by pollycam_launch. Its purpose is to restart the avconv process whenever the RTSP connection is lost due to random broadband data throttling, camera WiFi signal loss, or just about anything else that interferes with the YouTube video stream connection.  For useful debugging messages this script can be executed on its own. Note: run pollycam_stop BEFORE using pollycam_run.

pollycam_start: This is a sub-script that is called by pollycam_run. It starts the PollyCam Youtube Live Service using avconv. Runs as background process. BE SURE TO EDIT THE FILE AND ENTER YOUR FOSCAM CAMERA AND YOUTUBE LIVESTREAM PARAMETERS.

 

The utility scripts are:

cpu_status: Displays CPU temperature and CPU throttle state that is reported by raspian's vcgencmd function. Useful for determining if the RPI3's power supply has intermittent voltage sag problems or if the CPU cooling is adequate. This is especially critical when streaming due to the video encoder's high process loading. I suggest that you periodically run the cpu_status script while pollycam_launch is running to monitor your CPU's health.

Note: CPU Throttle status data should always be 0x0, otherwise you have under-voltage or over-heating problem (and video streaming will not be reliable). Details to the vcgencmd throttle value is found here: https://goo.gl/Dg1U2V

cpu_temp: Displays CPU temperature only.

 

Share this post


Link to post
Share on other sites

The details to the PollyCam project are now in your hands. Hopefully the information has been helpful.

I'll post some final comments in a few days. For extra credit I'll even provide details on automating the live stream broadcast times using cron jobs. Plus how to avoid broadband bandwidth penalty fees. See you soon!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×