Gst-python rtsp streaming

Currently, I’m trying to perform streaming using gst-python because I want to do some work before sending frame out. Here’s my using scripts:

import cv2
import gi

gi.require_version(‘Gst’, ‘1.0’)
gi.require_version(‘GstRtspServer’, ‘1.0’)
from gi.repository import Gst, GstRtspServer, GObject

class SensorFactory(GstRtspServer.RTSPMediaFactory):
def init(self, **properties):
super(SensorFactory, self).init(**properties)
self.cap = cv2.VideoCapture(0)
self.width = int(self.cap.get(3))
self.height = int(self.cap.get(4))
self.fps = int(self.cap.get(cv2.CAP_PROP_FPS))
self.number_frames = 0
self.duration = 1 / self.fps * Gst.SECOND # duration of a frame in nanoseconds
self.launch_string = 'appsrc name=source is-live=true block=true >format=GST_FORMAT_TIME ’
'caps=video/x-raw,format=BGR,width=640,height=480,framerate=
20/1 ’
'! videoconvert ! video/x-raw,format=I420 ’
'! x264enc speed-preset=ultrafast tune=zerolatency ! queue ’
'! rtph264pay config-interval=1 name=pay0 pt=96 ’
# streams to gst-launch-1.0 rtspsrc location=rtsp://localhost:8554/test latency=50 ! >decodebin ! autovideosink

def on_need_data(self, src, lenght):
if self.cap.isOpened():
ret, frame = self.cap.read()
if ret:
frame = cv2.resize(frame, (640, 480), interpolation=cv2.INTER_AREA)
data = frame.tostring()
buf = Gst.Buffer.new_allocate(None, len(data), None)
buf.fill(0, data)
buf.duration = self.duration
timestamp = self.number_frames * self.duration
buf.pts = buf.dts = int(timestamp)
buf.offset = timestamp
self.number_frames += 1
retval = src.emit(‘push-buffer’, buf)
if retval != Gst.FlowReturn.OK:
print(retval)

def do_create_element(self, url):
return Gst.parse_launch(self.launch_string)

def do_configure(self, rtsp_media):
self.number_frames = 0
appsrc = rtsp_media.get_element().get_child_by_name(‘source’)
appsrc.connect(‘need-data’, self.on_need_data)

class GstServer(GstRtspServer.RTSPServer):
def init(self, **properties):
super(GstServer, self).init(**properties)
self.factory = SensorFactory()
self.factory.set_shared(True)
self.get_mount_points().add_factory("/test", self.factory)
self.attach(None)

GObject.threads_init()
Gst.init(None)

server = GstServer()

loop = GObject.MainLoop()
loop.run()
This code work well for webcame but when I change to use rtsp camera, it’s latency becomes higher (up to 10sec). What I did to change to use rtsp as input stream:
change:
self.cap = cv2.VideoCapture(0)
to
self.gst_in = rtspsrc location=rtsp://admin:vnnet123456@192.168.1.201/1 latency=0 !
rtph264depay ! h264parse ! nvv4l2decoder enable-max-performance=1 ! nvvidconv !
video/x-raw, format=(string)BGRx ! videoconvert ! appsink sync=false
self.cap = cv2.VideoCapture(gst_in, cv2.CAP_GSTREAMER)

What I did wrong? How can I reduce latency on streaming?

Hi,
Looks like decoder receives complete SPS/PPS very late. Please configure the property in h264parse:

  config-interval     : Send SPS and PPS Insertion Interval in seconds (sprop parameter sets will be multiplexed in the data stream when detected.) (0 = disabled, -1 = send with every IDR frame)
                        flags: readable, writable
                        Integer. Range: -1 - 3600 Default: 0

See if it helps.