In which we publish a MJPEG stream from the BeagleBone Black.
Continuing with the series of post on OpenCV, webcams, and MJPEG, today we will look at streaming an MJPEG capture from the BBB. Before I get into it though, you should know that I did try FFMPEG/avconv and VLC to stream video from the BBB using rtp, but the several seconds of latency made it unsuitable for my needs. You should also know that I do not claim that this is the one true way to stream video from the BBB.
The libraries used:
- ZeroMQ and CZMQ - used to create pub/sub connections between the BBB and software running on a desktop
- OpenCV - used to display the MJPEG stream
The subscriber was compiled and tested under Windows 7 using Visual Studio 2012; however, the code should compile under Linux with very few, if any, modifications.
I am working on a project that requires a video stream from the BBB be consumed in N places where N is a minimum of 2. The stream will be processed using OpenCV, and because of the nature of this project, I need as little latency in the video stream as possible.
Theory of Operation:
The BBB will capture frames in MJPEG format from a webcam via a modified version of framegrabber. The modified version of framegrabber can run indefinitely and outputs the frames as a series of ZeroMQ messages over a publish socket. The clients will subscribe to the publish socket on the BBB using ZeroMQ and load each frame received into OpenCV.
The ZeroMQ pub/sub configuration allows many clients to connect to the published stream. No synchronization is used between the publisher and subscriber; the the stream is treated as continuous, and the subscribers are free to connect and disconnect at will.
Single subscriber 640x480 - cpu use on the BBB ~4.3% and memory use ~0.8%
Multiple subscribers 640x480 - cpu use on BBB ~6.6% and memory use ~0.8%
Single subscriber 1920x1080 - cpu use on BBB ~23.2% and memory use ~3.5%
Using this setup, I have been able to stream frames with a resolution as high as 1920x1080 with little to no latency, but there is a limitation, the network. When using this over Wifi with high resolutions or several clients running on one machine, I noticed the frame rate would drop the further I went from the router. If you watch the output of the top command on the BBB as you get further from the router, you will see framegrabber's memory use begin to climb. This is due to the publish socket buffering the data. As you walk back towards the router, you will see the memory use drop until it, and the frame rate, stabilizes. During this stabilization period you will probably experience delayed video that is displayed at a higher frame rate than normal as the buffer is flushed.
There are several things you can do reduce or eliminate this latency.
- If possible, use a wired connection
- Use an 802.11 N router and clients
- Make sure your WiFi router is optimally located
- Adjust the QoS settings of your router to give higher or highest priority to the traffic on the port you publish over
To reduce the amount of time it takes for subscribers to catch up once their connection has improved, the high water mark on the socket can be reduced. This has the effect of dropping frames once too many are buffered and essentially reduces the amount of buffered data a subscriber has to process to get in sync.
The reader may find it interesting that it does not matter if the BBB publisher or the subscribers are started up first. The publisher will simply dump data until at least one subscriber connects, and the subscribers will wait on the publisher. In addition, you can kill the publisher while subscribers are connected, restart it with new (or the same) settings, and the subscribers will continue on. The reader should verify this by changing the resolution after the subscriber or subscribers have connected.
framegrabberPub.c (17.96 kb) Publisher - you will need zhelpers.h
gcc framegrabberPub.c -lzmq -o framegrabberPub
framegrabberSub.c (3.79 kb) C client
framegrabberSub.py (2.52 kb) Python client
The Python client will display the stream with little latency until garbage collection occurs. When this happens, the display will freeze and the buffered data on the BBB will increase. Once garbage collection completes, the display will eventually synchronize much like the WiFi issue detailed above.
If your wireless router is capable of broadcasting at both 2.4 and 5 Ghz at the same time, you can improve performance when using a WiFi connection for both the publisher and the subscriber by having one connect at 2.4 Ghz and the other connect at 5 Ghz.
Added a link to zhelpers.h needed to compile the publisher.