I am building a pyside application on windows and I am trying to receive an rtsp stream carrying data encoded in a custom codec. I’ve tried a few things that didn’t work, so I need some help.
I first tried using opencv with a gstreamer backend. I’m already doing this to receive a video stream, but when I try to receive my custom codec, opencv fails to even open the stream. Here’s the code that just tries to open the stream:
gst = f"rtspsrc location={rtsp_url} ! appsink"
os.environ["GST_DEBUG"] = "3"
cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER)
if key & 0xFF == ord('q'):
print("failed to open stream")
<code>import os
import cv2
gst = f"rtspsrc location={rtsp_url} ! appsink"
os.environ["GST_DEBUG"] = "3"
cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER)
if cap.isOpened():
while True:
ret, frame = cap.read()
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
print(frame)
else:
print("failed to open stream")
</code>
import os
import cv2
gst = f"rtspsrc location={rtsp_url} ! appsink"
os.environ["GST_DEBUG"] = "3"
cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER)
if cap.isOpened():
while True:
ret, frame = cap.read()
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
print(frame)
else:
print("failed to open stream")
which produces this output:
<code>0:00:00.156754700 25048 0000020BB33DCBC0 FIXME default gstutils.c:4089:gst_element_decorate_stream_id_internal:<fakesrc0> Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:03.694289100 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: Delayed linking failed.
0:00:03.698133300 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: failed delayed linking some pad of GstRTSPSrc named rtspsrc0 to some pad of GstAppSink named appsink0
0:00:03.705085500 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: Internal data stream error.
0:00:03.708917300 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: streaming stopped, reason not-linked (-1)
[ WARN:[email protected]] global cap_gstreamer.cpp:2839 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module udpsrc1 reported: Internal data stream error.
[ WARN:[email protected]] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:[email protected]] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name
<code>0:00:00.156754700 25048 0000020BB33DCBC0 FIXME default gstutils.c:4089:gst_element_decorate_stream_id_internal:<fakesrc0> Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:03.694289100 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: Delayed linking failed.
0:00:03.698133300 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: failed delayed linking some pad of GstRTSPSrc named rtspsrc0 to some pad of GstAppSink named appsink0
0:00:03.705085500 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: Internal data stream error.
0:00:03.708917300 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: streaming stopped, reason not-linked (-1)
[ WARN:[email protected]] global cap_gstreamer.cpp:2839 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module udpsrc1 reported: Internal data stream error.
[ WARN:[email protected]] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:[email protected]] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name
failed to open stream
</code>
0:00:00.156754700 25048 0000020BB33DCBC0 FIXME default gstutils.c:4089:gst_element_decorate_stream_id_internal:<fakesrc0> Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:03.694289100 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: Delayed linking failed.
0:00:03.698133300 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: failed delayed linking some pad of GstRTSPSrc named rtspsrc0 to some pad of GstAppSink named appsink0
0:00:03.705085500 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: Internal data stream error.
0:00:03.708917300 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: streaming stopped, reason not-linked (-1)
[ WARN:[email protected]] global cap_gstreamer.cpp:2839 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module udpsrc1 reported: Internal data stream error.
[ WARN:[email protected]] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:[email protected]] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name
failed to open stream
The thing is, the pipeline runs fine without errors in the command line using gst-launch-1.0. I assume cv2.videoCapture can only be given video frame formats? I really don’t know.
I then tried using python bindings for gstreamer, but there doesn’t seem to be any such bindings that work on Windows. I know I can use WSL, but I’m too deep into my development to now set everything up again in WSL. That said, I did a short test in WSL just to see if it would work, and it does. Here’s the code I used:
from threading import Thread
gi.require_version('Gst', '1.0')
gi.require_version("GstApp", "1.0")
from gi.repository import Gst, GstApp, GLib
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target=main_loop.run)
pipeline = Gst.parse_launch("rtspsrc location=rtsp_url ! appsink name=sink")
appsink = pipeline.get_by_name("sink")
pipeline.set_state(Gst.State.PLAYING)
sample = appsink.try_pull_sample(Gst.SECOND)
buf = sample.get_buffer()
result, mapinfo = buf.map(Gst.MapFlags.READ)
x, y, worn = struct.unpack("!ffB", datum)
except KeyboardInterrupt:
pipeline.set_state(Gst.State.NULL)
<code>import gi
from threading import Thread
import struct
gi.require_version('Gst', '1.0')
gi.require_version("GstApp", "1.0")
from gi.repository import Gst, GstApp, GLib
_ = GstApp
Gst.init()
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target=main_loop.run)
main_loop_thread.start()
pipeline = Gst.parse_launch("rtspsrc location=rtsp_url ! appsink name=sink")
appsink = pipeline.get_by_name("sink")
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
sample = appsink.try_pull_sample(Gst.SECOND)
if sample is None:
print("no sample")
continue
buf = sample.get_buffer()
result, mapinfo = buf.map(Gst.MapFlags.READ)
dat = mapinfo.data
datum = dat[12:]
x, y, worn = struct.unpack("!ffB", datum)
print(x, y)
except KeyboardInterrupt:
print("stopping")
pass
pipeline.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()
</code>
import gi
from threading import Thread
import struct
gi.require_version('Gst', '1.0')
gi.require_version("GstApp", "1.0")
from gi.repository import Gst, GstApp, GLib
_ = GstApp
Gst.init()
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target=main_loop.run)
main_loop_thread.start()
pipeline = Gst.parse_launch("rtspsrc location=rtsp_url ! appsink name=sink")
appsink = pipeline.get_by_name("sink")
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
sample = appsink.try_pull_sample(Gst.SECOND)
if sample is None:
print("no sample")
continue
buf = sample.get_buffer()
result, mapinfo = buf.map(Gst.MapFlags.READ)
dat = mapinfo.data
datum = dat[12:]
x, y, worn = struct.unpack("!ffB", datum)
print(x, y)
except KeyboardInterrupt:
print("stopping")
pass
pipeline.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()
Does anyone know if I can get this working with opencv? Or if there is any way to run gstreamer code in python on windows? I’d appreciate any help I can get, I’ve been pulling my hair out for days. Thanks.