Gstreamer pipeline breaks while passing h264
Using OpenCV with GStreamer pipeline I wrote a code to read the rtsp stream. I am using jetson nano 2GB and my gstreamer version is 1.14 and opencv version is 4.1.1
To ready the rtsp stream, I am using rtph264depay and h264parse. For some reason, my gstreamer pipeline breaks and stops reading the stream.
cap_send = cv2.VideoCapture('rtspsrc location=rtsp://10.0.1.11:554/live0 ! queue max-size-bytes=65536 max-size-buffers=0 max-size-time=0 leaky=downstream ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink sync=false', cv2.CAP_GSTREAMER)
Pipelines breaks if I place an object in front of the camera. I suspect the camera is sending empty frames which causes this issue.
Is there a way to handle this expectation and keep the pipeline running. I am also attaching the python code which I am using.
import cv2
import json
import time, os
from datetime import datetime
import subprocess
width = int(1280)
height = int(720)
fps = float(12.0)
cap_send = cv2.VideoCapture('rtspsrc location=rtsp://10.0.1.11:554/live0 ! queue max-size-bytes=65536 max-size-buffers=0 max-size-time=0 leaky=downstream ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink sync=false', cv2.CAP_GSTREAMER)
out_send = cv2.VideoWriter("appsrc ! videoconvert ! nvvidconv ! nvv4l2h264enc ! video/x-h264, stream-format=byte-stream ! h264parse ! flvmux ! rtmpsink location=rtmp://192.168.2.22:1935/live/test", cv2.CAP_GSTREAMER, 0, 13.0, (1280,720))
scale = 1
font = cv2.FONT_HERSHEY_PLAIN
xMargin = 10
yMarginBottom = 10
yMarginTop = 20
if not cap_send.isOpened():
print('VideoCapture or VideoWriter not opened')
exit(0)
while True:
try:
time.sleep(0.02)
ret,frame = cap_send.read()
if not ret:
print('empty frame')
break
cv2.putText(frame, 'data', (xMargin, yMarginTop), font, scale, (255,255,255), 1, cv2.LINE_4)
out_send.write(frame)
print ('tes')
if cv2.waitKey(1)&0xFF == ord('q'):
break
except Exception as e:
print (e)
cap_send.release()
out_send.release()