Linux – How to stream h264 video from web-cam to network

linuxstreamingvideo

I have a web-cam which I want to use to stream h264 video. Now I'm streaming mjpg video with mjpg-streamer. And now I'd like to stream h264. I've found that it could be done with gstreamer, but I'm totally new to it. Also after reading some blogs/articles/etc it seems to me that there were some issues with h264 and gstreamer. Is it still true?

May be there are some other methods to do this besides gstreamer?

Any info would be helpful.
Thanks in advance.

Best Answer

I will explain how to stream h264 using gstreamer.

First, you need Linux kernel 3.2 or later to have "H264 pixel format" supported in the v4l2 drivers.

Use v4l2-ctl to check that you have proper H.264 support for the camera:

# v4l2-ctl --list-formats
# v4l2-ctl --list-formats-ext

Next, you will need video4linux libraries and utilities. These include:

libv4l and libv4l-dev
v4l-utils
qv4l2 (may be part of v4l-utils)
v4l2ucp

Setting up the frame rate:

# v4l2-ctl --set-fmt-video=width=800,height=448,pixelformat=1
# v4l2-ctl --set-parm=30

Gstreamer has a v4l2src input element, it does not yet support the video/x-264 format.

But, we can capture H.264 stream and send to a file or even standard out, by simply piping it to the standard input of a gstreamer pipeline using a file source:

#  ./capture -c 100000 -o | gst-launch -v -e filesrc location=/dev/fd/0 ! h264parse ! decodebin2 ! xvimagesink sync=false

The encoded video stream can be sent over the network using Gstreamer:

#  gst-launch -v udpsrc port=4000 caps='application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264' ! \
          rtph264depay ! ffdec_h264 ! xvimagesink sync=false

 # ./capture -c 10000 -o | gst-launch -v -e filesrc location=/dev/fd/0 ! h264parse ! rtph264pay ! udpsink  host=host_IP port=4000

I hope you will find this useful.

Related Question