Can not reconnect with cv2.VideoCapture using cv2.CAP_GSTREAMER

Hello guys,

I have an issue with trying reconnect stream.

    def open_cam_rtsp(uri, width, height, latency):
        """Open an RTSP URI (IP CAM)."""
        gst_str = ('rtspsrc location={} latency={} ! '
               'rtph264depay ! h264parse ! omxh264dec ! '
               'nvvidconv ! '
               'video/x-raw, width=(int){}, height=(int){}, '
               'format=(string)BGRx ! videoconvert ! '
               'appsink').format(uri, latency, width, height)
        return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 

This is my format rtsp stream , i’m using HKVion camera. Everything work fine. I try to control the case when camera dont have internet connection (network not stable, camera turn off, …), so i want to call reconnect function after 5 second to make sure when the camera is activated, i can get the stream from it.

def show_camera():
  while(isOpened):
    cap = cv2.VideoCapture(open_cam_rtsp(url), cv2.CAP_GSTREAMER)
    if cap.isOpened():
	    print('Connected to camera')
        isOpened = True
    else:
        print('Unable to open camera')
	    isOpened = False

It run fine after hours bebore this error show , this code will stuck and dont run anymore.

(python:2946615): GStreamer-CRITICAL **: 19:24:31.287:
Trying to dispose element rtph264depay, 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.


(python:2946615): GStreamer-CRITICAL **: 19:24:31.287:
Trying to dispose element h264parse, 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.


(python:2946615): GStreamer-CRITICAL **: 19:24:31.287:
Trying to dispose element udpsrc0, 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.


(python:2946615): GStreamer-CRITICAL **: 19:24:31.287: gst_mini_object_unref: assertion '(g_atomic_int_get (&mini_object->lockstate) & LOCK_MASK) < 4' failed

(python:2946615): GStreamer-CRITICAL **: 19:24:31.287:
Trying to dispose element omxh264dec, but it is in PAUSED 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] global /home/Users/dqy/myLibs/opencv/modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline

(python:2946615): GStreamer-CRITICAL **: 19:24:31.287:
Trying to dispose element videoconvert, 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] global /home/Users/dqy/myLibs/opencv/modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

(python:2946615): GStreamer-CRITICAL **: 19:24:31.287:
Trying to dispose element appsink0, but it is in READY 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.

Not sure for your case, but you may try adding drop=1 to appsink properties.

Hi,
Please try Honey Patouceul’s suggestion and replace onxh264dec with nvv4l2decoder.

And please share your release version($ head -1 /etc/nv_tegra_release) for reference.

I try to apply both solution you guys suggest. I will have a result soon because it take a lot of hours to reproduce ( it just happened for hours running )

Still getting this error:

(python:20746): GStreamer-CRITICAL **: 09:53:14.593: 
    Trying to dispose element capsfilter303, 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.


    (python:20746): GStreamer-CRITICAL **: 09:53:14.593: 
    Trying to dispose element queue303, 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.


    (python:20746): GStreamer-CRITICAL **: 09:53:14.594: 
    Trying to dispose element nvvconv303, 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.


    (python:20746): GStreamer-CRITICAL **: 09:53:14.594: 
    Trying to dispose element h264parse303, 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.


    (python:20746): GStreamer-CRITICAL **: 09:53:14.594: 
    Trying to dispose element rtph264depay303, 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.


    (python:20746): GStreamer-CRITICAL **: 09:53:14.595: 
    Trying to dispose element pipeline303, but it is in PAUSED 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] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline

    (python:20746): GStreamer-CRITICAL **: 09:53:14.595: 
    Trying to dispose element videoconvert303, 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] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

    (python:20746): GStreamer-CRITICAL **: 09:53:14.596: 
    Trying to dispose element appsink303, but it is in READY 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.

This is new log when i try using Honey Patouceul and DaneLLL’s suggestion.

Here is the code i use to connect stream:

Hi,
Please check if you can run a gst-launch-1.0 command:

gst-launch-1.0 uridecodebin uri='rtsp://_RTSP_IP_ADDRESS_' ! nvoverlaysink

To make sure the URI is valid.

Yes sure , everything will be fine , the uri is correct ( i can connect to camera to get frame ). In my case, i met the error when trying to reconnect. I want to control case camera is disconnect + network stable + not network available, so the system will call the connect function with while true condition (just stop reconnect when capture.isOpened = true). When i try to reconnect many times for ( maybe 1 2 hours , maybe longer , it happened randomly ).

This is my test if you need to know

You may try:

cv2.VideoCapture("uridecodebin uri=rtsp://127.0.0.1:8554/test ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1 ", cv2.CAP_GSTREAMER)

# For a H264 encoded source, you may use:
cv2.VideoCapture("rtspsrc location=rtsp://127.0.0.1:8554/test ! 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 ! appsink drop=1 ", cv2.CAP_GSTREAMER)

# If your RTSP server supports RTSP2:
cv2.VideoCapture("rtspsrc location=rtsp://127.0.0.1:8554/test default-rtsp-version=32 ! 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 ! appsink drop=1 ", cv2.CAP_GSTREAMER)


I will try and will reply you as soon possible.

1 Like