Saturday, October 4, 2014

Video record from Foscam IP camera with GStreamer-1.0 in C++

I posted a similar article with libvlc. This is to do the same but with GStreamer-1.0. After I learn about GStreamer, I found it very attractive and useful. Although the library wasn't easy to understand at first, it is well documented and once I understood the library design, it was very easy to use. I found this PDF document very useful.

Unfortunately I need to skip how to install GStreamer library in this article, which is the most tricky step. It is because GStreamer-1.0 is not yet included in the Rasbian package system. I wrote another article about how to install GStreamer-1.0 manually on Raspberry Pi but your mileage may vary.

The program above is modified from an example in the PDF document; Chapter 20 has an example program related to "decodebin". This program does the following:
  1. initialize GStreamer with gst_init().
  2. check the version of GStreamer; the version has to be 1.0 or above.
  3. creates loop and pipeline.
  4. assign a call back function, my_bus_callback, to the bus that came from the pipeline.
  5. create bunch of elements: uridecodebin, omxh264enc, h264parse, lamemp3enc, matroskamux, filesink, videoconvert and audioconvert.
  6. set the source location and destination location; source in this case is the foscam camera URL and the destination is a MKV file name.
  7. set an call back function, cb_newpad, to the uridecodebin; this step has to be dynamic because uridecodebin will not know what file type it is going to take from the URL at compile time.
  8. link the rest of the elements; there is no dynamic linking needed for those.
  9. let GStreamer running in loop.
  10. clean up once the program is done running, which wouldn't happen in this case because the camera will never have end of stream.
For more details, please check out the GStreamer manual.

One thing that I learned is that I couldn't change the target-bitrate or anything about omxh264enc. I complains that the version is mismatching. I think it is a kind of bug in the encoder; of course your mileage may vary on this as well.

The callback function, cb_newpad, is also modified from an example from the PDF document. The original code was implemented only for audio but I added video pad linking as well. I am not sure if it has to un-reference GstStructure but the original code didn't.

The callback function, my_bus_callback, is also from the PDF document. It is to interact with GStreamer on various events.

You will need to change the URL as your setting; IP address, user id and password.
Note that the Makefile now has CFLAGS with GStreamer compile flags. It comes with an external utility, pkg-config. pkg-config will return the compile flag for GStreamer-1.0.

The screenshot above shows how to compile and execute. It generates the movie file. I had to ^C to stop it because the program is not ready for a proper shutdown. lol. I think I can shutdown properly with the "loop" but it will be for another article.

To make sure it is H.264 encoded, I opened it from Windows version of VLC.

No comments:

Post a Comment

About Me

My photo
Tomorrow may not come, so I want to do my best now.