How to live stream from multiple webcam

I have five webcams I want to live to stream their content to m3u8(HLS stream), so I can use an HTML web player to play that file.

My current setup:

I have five systems each has a webcam connected to it, so I am using RTSP to stream data from the system to AWS.

./ffmpeg -f avfoundation -s 640x480 -r 30 -i "0" -f rtsp rtsp://awsurl.com:10000/cam1 ./ffmpeg -f avfoundation -s 640x480 -r 30 -i "0" -f rtsp rtsp://awsurl.com:10000/cam2 

....

./ffmpeg -f avfoundation -s 640x480 -r 30 -i "0" -f rtsp rtsp://awsurl.com:10000/cam5 

On the cloud, I want to set up a server. I Googled and learned about GStreamer, with which I can set up an RTSP server. The command below has an error. (I can't figure out how to set up one server for multiple webcam streams)

gst-launch-1.0 udpsrc port=10000 ! rtph264depay ! h264parse ! video/x-h264,stream-format=avc ! \ mpegtsmux ! hlssink target-duration=2 location="output_%05d.ts"\ playlist-root= playlists-max=3 

I question how I can set up the RTSP to differentiate between multiple webcam streams using one server (or do I have to create a server for each webcam stream)?

3

2 Answers

This might not be a canonical answer, as there are no details about the camera streams, the OS and your programming language, but you may try the following:

1. Install prerequisites

You would need gstrtspserver library (and may be gstreamer dev packages as well if you want to try from C++). Assuming a Linux Ubuntu host, you would use:

sudo apt-get install libgstrtspserver-1.0 libgstreamer1.0-dev 

2. Get information about the received streams

You may use various tools for that, with gstreamer you may use:

gst-discoverer-1.0 rtsp://awsurl.com:10000/cam1 

For example, if you see:

Topology: unknown: application/x-rtp video: H.264 (Constrained Baseline Profile) 

Then it is H264 encoded video sent by RTP so RTPH264. You would get more details adding verbose flag (-v).

If you want your RTSP server to stream with H264 encoding and the incoming stream is also H264, then you would just forward. If the received stream has a different encoding than what you want to encode, then you would have to decode video and re-encode it.

3. Run the server:

This python script would run a RTSP server, streaming 2 cams with H264 encoding (expanding to 5 should be straight forward). Assuming here that the first cam is H264 encoded, it is just forwarding. For the second camera, the stream is decoded and re-encoded into H264 video. In latter case, it is difficult to give a canonical answer, because the decoder and encoder plugins would depend on your platform. Some also use special memory space (NVMM for Nvidia, 3d11 for Windows, ...), in such case you may have to copy to system memory for encoding with x264enc, or better use an other encoder using same memory space as input.

import gi gi.require_version('Gst','1.0') gi.require_version('GstVideo','1.0') gi.require_version('GstRtspServer','1.0') from gi.repository import GObject, GLib, Gst, GstVideo, GstRtspServer Gst.init(None) mainloop = GLib.MainLoop() server = GstRtspServer.RTSPServer() mounts = server.get_mount_points() factory1 = GstRtspServer.RTSPMediaFactory() factory1.set_launch('( rtspsrc location=rtsp://awsurl.com:10000/cam1 latency=500 ! rtph264depay ! h264parse ! rtph264pay name=pay0 pt=96 )') mounts.add_factory("/cam1", factory1) factory2 = GstRtspServer.RTSPMediaFactory() factory2.set_launch('( uridecodebin uri=rtsp://awsurl.com:10000/cam2 source::latency=500 ! queue ! x264enc key-int-max=15 insert-vui=1 ! h264parse ! rtph264pay name=pay0 pt=96 )') mounts.add_factory("/cam2", factory2) server.attach(None) print ("stream ready at rtsp://127.0.0.1:8554/{cam1,cam2,...}") mainloop.run() 

If you want using C++ instead of python, you would checkout sample test-launch for your gstreamer version (you can get it with gst-launch-1.0 --version) that is similar to this script and adapt.

4. Test

Note that it may take a few seconds to start before displaying.

gst-play-1.0 rtsp://[Your AWS IP]:8554/cam1 gst-play-1.0 rtsp://[Your AWS IP]:8554/cam2 

I have no experience with AWS, be sure that no firewall blocks UDP/8554.

1

rtsp-simple-server might be a good choice for presenting and broadcasting live streams through various format/protocols such as HLS over HTTP.

Even on meager and old configuration, it still has provided me a decent latency.

If you look for reduced latency, you might be surprised with cam2ip. Unfortunatly this isn't HLS, it's actually mjpeg, and thus without sound, but with far better latency.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like