pythonvideo-streaminggstreamerrtsprtsp-server

Gstreamer Python correcting launch string/cap for rtsp-server


I have a task to receive RTSP, make a frame processing in OpenCV and give RTSP with processed video. I found that RTSP-server example and some internet examples can give me working RTSP-server that can play saved video. But I want to give RTSP not a video. So I rewrite def init this way:

*def __init__(self, **properties):
        super(SensorFactory, self).__init__(**properties)
        self.cap = cv2.VideoCapture("myrtsp_src")
        self.number_frames = 0
        self.fps = 20
        self.duration = 1 / self.fps * Gst.SECOND  # duration of a frame in nanoseconds
        self.launch_string = 'appsrc name=source block=true format=GST_FORMAT_TIME ' \
                             'caps=video/x-raw,format=BGR,width=1280,height=960,framerate={}/1 ' \
                             '! videoconvert ' \
                             '! x264enc speed-preset=ultrafast tune=zerolatency ! queue ' \
                             '! rtph264pay config-interval=1 name=pay0 pt=96 '.format(self.fps)*

This one is working but with problems. What I want is to rewrite self.launch_string to my correctly working pipeline:

"rtspsrc location=myrtsp_src latency = 400 protocols=tcp\n"
                             "! rtph264depay ! h264parse ! decodebin ! videoscale ! video/x-raw,width=1280,height=960\n" 
                             "! videoconvert ! autovideosink sync=false")

The problem is I cant completely or in parts replace it - nothing is working.

I believe there is a key in appsrc and x264enc but I dont know what exactly is not working. I tried to debug and there is something what I receive:

0:00:29.691611082 7486 0x55ea7afe0240 ERROR rtspclient rtsp-client.c:1087:find_media: client 0x55ea7aff1170: can't prepare media 0:00:29.691676543 7486 0x7f1250003de0 DEBUG default rtsp-thread-pool.c:159:do_quit: stop mainloop of thread 0x7f1250039040 Also had a mistakes after trying to use without appsrc:

File "/home/alex/gstreamer/working_on/trial_rtsp.py", line 47, in do_configure
    appsrc.connect('need-data', self.on_need_data)
AttributeError: 'NoneType' object has no attribute 'connect'

Solution

  • I was trying to check different troubles I could have.

    The problem was:

    Firstly, to use cv2.CAP_GSTREAMER. Then if have a trouble with code, check it by:

    print(cv2.getBuildInformation())
    

    If you receive Video I/O:

    DC1394:                      YES (2.2.6)
    FFMPEG:                      YES
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                NO
    GStreamer:                   NO
    PvAPI:                       NO
    v4l/v4l2:                    YES (linux/videodev2.h)
    gPhoto2:                     YES
    

    Then check that OpenCV installed with GSTREAMER_ON. I had a problem with installation both from sudo and pip, so my program used pip-version without GSTREAMER. I uninstalled it, and here is part of my working code:

        self.cap = cv2.VideoCapture("rtspsrc location=my_rtspsrc latency = 100 protocols=tcp\n"
                                 "! rtph264depay ! h264parse ! decodebin ! videoscale ! video/x-raw,width=1280,height=960\n" 
                                 "! videoconvert ! appsink sync=false", cv2.CAP_GSTREAMER)
        self.number_frames = 0
        self.fps = 20
        self.duration = 1 / self.fps * Gst.SECOND  
        self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME ' \
                                 'caps=video/x-raw,format=BGR,width=1280,height=960,framerate={}/1 ' \
                                 '! videoconvert ! video/x-raw,format=I420 ' \
                                 '! x264enc speed-preset=ultrafast tune=zerolatency ' \
                                 '! rtph264pay config-interval=1 name=pay0 pt=96'.format(self.fps)