Low Latency USB Webcam Streaming from a Raspberry Pi to an Arduino

For a robotics project that I am doing I need to create a low latency webcam stream from our robot. I wanted to use a Raspberry Pi and a USB webcam (a Microsoft LiveCam). I googled around for a while and found lots of tutorials to stream with the Raspberry Pi Camera but only a few that were to stream a USB webcam.  Finally I found this tutorial which worked once I adapted it for my webcam and home network. The latency barley noticeable! See this video to see the latency. I will summarize the tutorial with some modification for using a USB webcam instead of a screen capture. Note that you will have to replace the IP address with the IP address of you client computer (the computer which is viewing the stream).

First install gstreamer on the PI
 sudo apt-get install gstreamer1.0-tools  

Then install gstreamer on the client computer. Go to the gstreamer website to find out how to download it for your OS: https://gstreamer.freedesktop.org/download/

Once gstreamer is installed run this command on the client computer:
 gst-launch-1.0 udpsrc port=5000 ! application/x-rtp,encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! autovideosink  
If the command is not recognized and you are using a windows client use the following command to change your current directory to the default gstreamer directory. Then re-run the original command.
 cd C:\gstreamer\1.0\x86_64\bin  

Then run this command on the pi. Remember to replace the IP address with your client's IP.
 gst-launch-1.0 v4l2src device="/dev/video0" ! video/x-raw,format=I420,width=640,height=480,framerate=15/1 ! jpegenc ! rtpjpegpay ! udpsink host=<IP Address> port=5000  

You are done! A window with the stream should appear on your client computer. To change the resolution and frame rate adjust the width, height, and frame rate parameters in the command run on the pi.
Again special thanks to this tutorial for showing me these commands. I have tested these commands on both a Raspberry Pi 1 model B and a Raspberry Pi 2 as hosts. I have only tested it on windows clients but gstreamer is cross platform so it should be similar.


Comments