The OpenCV VideoCapture hangs when the rtspsrc unreachable after many retries

Jetpack 4.5 && OpenCV 4.5.5(compiled with cuda)

I want to read rtsp stream from one IP camera, but you know the network is not reliable sometimes, so I add some error handing in the code as bellow and test it use the Camera IP which is unreachable in the code on purpose to test this situation.

But I find when the rtsp ip address is unreachable, the VideoCapture maybe hang after it retries many times. Somtime it is 10 times,sometime it is 900/2000 times.We can find Before Capture Current was outputed but had no corresponding *After Capture Current *.

import json
import cv2
import time

print(cv2.__version__)

cap_url = "rtspsrc location = rtsp://admin:vPn**1234@192.168.254.9:554/cam/realmonitor?channel=1&subtype=0 tcp-timeout=1000000 ! application/x-rtp,media=video,encoding-name=H264 ! rtpjitterbuffer latency=1000 ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR,width=640,height=360 ! appsink drop=1 "
print("Camera Receive Read Net Camera For {} Start Captured.".format(cap_url))
cap = cv2.VideoCapture(cap_url, cv2.CAP_GSTREAMER)

count = 0
capture_count=0
while True:
    if cap and cap.isOpened():
        cap.release()
        cap = None
    if cap and (not cap.isOpened()):
        cap = None
    print("Before Capture Current ",capture_count)
    cap =  cv2.VideoCapture(cap_url, cv2.CAP_GSTREAMER)
    capture_count = capture_count + 1
    print("After Capture Current ",capture_count)
    #time.sleep(0.2)
    if (not cap or (cap and (not cap.isOpened()))):
        print("Capture Error Current ",capture_count)
        continue
    ret = True
    while ret:
        ret, frame_read = cap.read()
        count = count + 1
        if ret:
            print(frame_read.shape,count)
        else:
            print("return false",count)
            continue
    print("Recapture Happen...")
    continue

One hang happens after 267 times:

After Capture Current 267

Capture Error Current 267

Before Capture Current 267

Opening in BLOCKING MODE

Opening in BLOCKING MODE

[ WARN:0@284.899] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module rtspsrc267 reported: Could not open resource for reading and writing.

(python3:8494): GStreamer-**CRITICAL** **: 08:01:24.096:

Trying to dispose element rtspsrc267, but it is in PLAYING instead of the NULL state.

You need to explicitly set elements to the NULL state before

dropping the final reference, to allow them to clean up.

This problem may also be caused by a refcounting bug in the

application or some element.

[ WARN:0@284.903] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline

[ WARN:0@284.903] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

After Capture Current 268

Capture Error Current 268

Before Capture Current 268

Opening in BLOCKING MODE

Opening in BLOCKING MODE

[ WARN:0@285.907] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module rtspsrc269 reported: Could not open resource for reading and writing.

[ WARN:0@285.912] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline

[ WARN:0@285.912] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

After Capture Current 269

Capture Error Current 269

Before Capture Current 269

Opening in BLOCKING MODE

Opening in BLOCKING MODE

[ WARN:0@286.983] global /usr/local/robu/source/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module rtspsrc270 reported: Could not open resource for reading and writing.

Hi,
It should be required to terminate the current pipeline and re-initialize a new one when network is unstable and the source cannot generate RTSP stream. Please refer to this sample and try to terminate and re-launch cv2.VideoCapture():
Nvidia-desktop kernel: [407343.357549] (NULL device *): nvhost_channelctl: invalid cmd 0x80685600 - #5 by DawnMaples

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.