Saturday, February 9, 2013

Streaming Video From the Rover

While experimenting with the excellent IP Webcam video server for Android, I found that the standard web browser and programs like VLC were intolerant of disconnects of the WiFi. Each time it disconnected, it would require manual intervention to reconnect, so I started looking at what it would take to make a very simple viewer in my Java client program.

It turns out to be relatively easy, though there is plenty of room for improvement. I found that even on my little netbook, I could get smooth video at 320x200 from the Android server just by repeatedly pulling down a static JPG from the IP Webcam and shoving each image into a simple extended JContainer. I expected it to be jerky, but it worked surprisingly well.

Note: I first tried a JLabel, which is the simplest way to load a picture in Java that I'm aware of. It was VERY slow. Not recommended. :-)

While it would likely be a bit faster to read the MJPEG stream it can provide, this was much easier to get working, and should be fine for what I want to do with it.

Once I had it working in a test program, I added a dedicated thread to my client program to handle the video connection and display. So now the client program consists of a GUI thread, a data/command thread, and a video thread.

The video thread launches and goes into a nested loop. The outer loop is keyed to a flag that gets set when the user requests a connection to the robot. The inner loop grabs the images from the Android server. If the connection drops or an image retrieval error occurs, it breaks out of the inner loop, resets and continues retrying until the user requests a disconnect. As a result, like the command/sensor thread, it automatically reconnects after the WiFi connects back up, and resumes the video feed.

The handling of the loss of the network connection has been consistently the hardest part of this so far, but that nested loop approach has worked well in both threads to automatically re-establish the connection.


  1. Next up, recording the video so you can post it to youtube.

  2. Woohoo, a comment! :-)

    Hey, that's a cool idea. I think MJPEG streams are just concatenated JPEGS, which should be pretty easy.. I would just need to check and see if there is header data or such stuff, and figure out how to ensure the frames get written out at the right rate.

    I think next step is to buy a robot base and get it wired up... I've picked it out, just need to order.

  3. It appears the simplest way to do that would just be to add some code to insure the frames come in at a reasonably constant rate, and then dump out the frames to disk. They could be reassembled with a call to ffmpeg.

    Could also work with one of a couple of Java video libraries (which would handle wrapping the JPEG bitstream up as an AVI for you).