问题
I'm trying to stream my laptop webcam frames with rtsp with python. To do this I'm using GStreamer. Browsing some examples on the web, I get the following code and I tested it with a local video in .mp4 format
#!/usr/bin/env python
import sysimport gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib
loop = GLib.MainLoop()
Gst.init(None)
class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self):
GstRtspServer.RTSPMediaFactory.__init__(self)
def do_create_element(self, url):
#set mp4 file path to filesrc's location property
src_demux = "filesrc location=/home/user1/Videos/720.mp4 ! qtdemux name=demux"
h264_transcode = "demux.video_0"
#uncomment following line if video transcoding is necessary
#h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
print ("Element created: " + pipeline)
return Gst.parse_launch(pipeline)
class GstreamerRtspServer():
def __init__(self):
self.rtspServer = GstRtspServer.RTSPServer()
self.rtspServer.set_service("7000")
factory = TestRtspMediaFactory()
factory.set_shared(True)
mountPoints = self.rtspServer.get_mount_points()
mountPoints.add_factory("/stream1", factory)
self.rtspServer.attach(None)
if __name__ == '__main__':
s = GstreamerRtspServer()
loop.run()
It expose the stream on the address 127.0.0.1:7000/stream1. So, if I try with the following command from the terminal, I can see the video in a window
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:7000/stream1 ! queue ! rtph264depay ! avdec_h264 ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! autovideosink
Now, I want to change the pipeline to change the source from the mp4 local video to my laptop webcam. I tried with the following:
vl42src device=/dev/video0 ! decodebin ! x264enc ! rtph264pay name=pay0 config-interval=1 pt=96
And, by terminal side, I try to connect with the same pipeline. It don't works. This is the error I get:
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:7000/stream1 ! queue ! rtph264depay ! avdec_h264 ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! autovideosink --gst-debug=3 -v
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Got context from element 'autovideosink0-actual-sink-vaapi': gst.vaapi.Display=context, gst.vaapi.Display=(GstVaapiDisplay)"\(GstVaapiDisplayGLX\)\ vaapidisplayglx1";
Progress: (open) Opening Stream
Progress: (connect) Connecting to rtsp://127.0.0.1:7000/stream1
Progress: (open) Retrieving server options
Progress: (open) Retrieving media info
0:00:00.113988576 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:6563:gst_rtspsrc_send:<rtspsrc0> error: Unhandled error
0:00:00.114040409 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:6563:gst_rtspsrc_send:<rtspsrc0> error: Service Unavailable (503)
ERROR: from element /GstPipeline:pipeline0/GstRTSPSrc:rtspsrc0: Unhandled error
0:00:00.114134907 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:7951:gst_rtspsrc_open:<rtspsrc0> can't get sdp
0:00:00.114188780 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:6031:gst_rtspsrc_loop:<rtspsrc0> we are not connected
Additional debug info:
gstrtspsrc.c(6563): gst_rtspsrc_send (): /GstPipeline:pipeline0/GstRTSPSrc:rtspsrc0:
Service Unavailable (503)
ERROR: pipeline doesn't want to preroll.
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...
What I'm wrong with this? Did anyone know a pipeline that can be used to make things working from my webcam?
Thanks in advance
回答1:
At the end, I found this example and I changed my code accordingly
https://github.com/superdump/pyrtsp
Here the pipeline used instead of those described in the code
v4l2src device=/dev/video0 ! videoconvert ! x264enc speed-preset=ultrafast tune=zerolatency ! rtph264pay name=pay0 pt=96
Here the command from terminal to connect to the stream
gst-launch-1.0 rtsprc location=rtsp://127.0.0.1:8554/test is-live=true ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! videoscale ! autovideosink
However the only remaining issue is slowness in showing images, but it should be optimizable with a bit of tuning of the pipeline
来源:https://stackoverflow.com/questions/61062288/how-to-stream-pc-webcam-with-rtsp-and-gstreamer-on-python