Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read RTSP Stream from UDP sink using Python OpenCV and GStreamer

We're developing a software to stream videos from two different cameras with RTSP using GStreamer. To simplify the acquisition process, we're using OpenCV with Python 3.

The problem is: we wanna push the stream to UDP sink, republish it over our LAN as a RTSP Stream and than read it in another PC. But we can't get this to work.

This is the Python code that gets the camera images and starts the streaming with udpsink. In this case we're accessing our local webcam so that the code may be straightforwardly tested by anyone.

import cv2
import time
from multiprocessing import Process

def send():
    video_writer = cv2.VideoWriter(
        'appsrc ! '
        'videoconvert ! '
        'x264enc tune=zerolatency speed-preset=superfast ! '
        'rtph264pay ! '
        'udpsink host=127.0.0.1 port=5000',
        cv2.CAP_GSTREAMER, 0, 1, (640, 480), True)

    video_getter = cv2.VideoCapture(0)

    while True:

        if video_getter.isOpened():
            ret, frame = video_getter.read()
            video_writer.write(frame)
            # cv2.imshow('send', data_to_stream)
            time.sleep(0.1)   

if __name__ == '__main__':
    s = Process(target=send)
    s.start()
    s.join()
    cv2.destroyAllWindows()

When running this, we only get one warning:

[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

Then we try to republish the stream over our LAN as RTSP using the examples/test-launch from GStreamer

./test-launch " udpsrc port=5000 ! h264parse ! rtph264pay name=pay0 pt=96"

This gives us no errors but the default message

stream ready at rtsp://127.0.0.1:8554/test

And then VLC fails to open the stream at this address.

                     ~$ vlc -v rtsp://127.0.0.1:8554/test
VLC media player 3.0.11 Vetinari (revision 3.0.11-0-gdc0c5ced72)
[000055f5dacf3b10] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
Qt: Session management error: None of the authentication protocols specified are supported
[00007f5ea40010f0] live555 demux error: Failed to connect with rtsp://127.0.0.1:8554/test
[00007f5ea4003120] satip stream error: Failed to setup RTSP session

I guess it's something with our pipelines, but I really don't get what it could be. Any help will be appreciated. Thanks in advance.

like image 812
Ralubrusto Avatar asked Oct 31 '20 00:10

Ralubrusto


1 Answers

you can remove the rtph264pay in VideoWriter, then the python script send h264 data, and test-launch receive h264 data, and do rtp packetize, and then rtsp.

so the VideoWriter should be:

video_writer = cv2.VideoWriter(
    'appsrc ! '
    'videoconvert ! '
    'x264enc tune=zerolatency speed-preset=superfast ! '
    'udpsink host=127.0.0.1 port=5000',
    cv2.CAP_GSTREAMER, 0, 1, (640, 480), True)
like image 135
kqmh00 Avatar answered Sep 28 '22 05:09

kqmh00