Gsteramer使用mppvideodec硬解码依旧延迟过大

我希望能够使用RKNN推理无线相机的Rtsp流,格式为H264。由于延迟过大我尝试使用Gsteramer的mppvideodec进行硬解码,针对这部分进行了尝试。
我尝试使用如下程序进行收流:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib
Gst.init(None)
loop = GLib.MainLoop()

pipeline2 = Gst.parse_launch("rtspsrc location=rtsp://192.168.0.133:554/test ! rtph264depay ! h264parse ! mppvideodec ! videorate ! video/x-raw,framerate=60/1 ! videoscale ! videoconvert ! video/x-raw,width=1280,height=720 ! autovideosink sync=false")

pipeline2.set_state(Gst.State.PLAYING)

try:
    loop.run()
except KeyboardInterrupt:
    
    pipeline.set_state(Gst.State.NULL)
    loop.quit()

此时的延迟是比较小的,约为0.2秒,但是我用下面的管道和程序进行测试则有非常大的延迟,最大时延迟会有一分钟左右:

import gi
import cv2
import numpy as np
from gi.repository import Gst
gi.require_version('Gst', '1.0')
# 初始化GStreamer
Gst.init(None)
import time  # 导入time模块

def gst_to_opencv(sample):
    start_time = time.time()  # 获取函数开始的时间
    buf = sample.get_buffer()
    caps = sample.get_caps()
    print("图像数据格式:", caps.to_string())
    array = np.ndarray(
        (caps.get_structure(0).get_value('height'),
         caps.get_structure(0).get_value('width'),
         3),
        buffer=buf.extract_dup(0, buf.get_size()),
        dtype=np.uint8)
    end_time = time.time()  # 获取函数结束的时间
    print("gst_to_opencv耗时: {:.5f}秒".format(end_time - start_time))  # 打印函数耗时
    return array

def new_sample(sink, data):
    sample = sink.emit('pull-sample')
    start_time = time.time()  # 获取OpenCV处理开始的时间
    frame = gst_to_opencv(sample)
    cv2.imshow('Live Video', frame)
    cv2.waitKey(1)
    end_time = time.time()  # 获取显示结束的时间
    print("OpenCV显示耗时: {:.5f}秒".format(end_time - start_time))  # 打印OpenCV处理+显示耗时
    return Gst.FlowReturn.OK

# 设置GStreamer管道 
pipeline = Gst.parse_launch('rtspsrc location=rtsp://192.168.0.133:554/test ! rtph264depay ! h264parse ! mppvideodec ! videorate ! video/x-raw,framerate=60/1 ! videoscale ! videoconvert ! video/x-raw,format=BGR,width=1280,height=720 ! appsink emit-signals=True name=mysink')

sink = pipeline.get_by_name('mysink')
sink.set_property('sync', False)
sink.connect('new-sample', new_sample, None)
pipeline.set_state(Gst.State.PLAYING)

try:
    while True:
        pass  # 循环运行,直到用户关闭窗口
except KeyboardInterrupt:
    print("Interrupted by user")

cv2.destroyAllWindows()
pipeline.set_state(Gst.State.NULL)

gst_to_opencv以及new_sample打印出的两个耗时均为毫秒级耗时。
我想请问的是两个程序比较,为什么第二个程序的延迟增加的如此严重,如何能够减小延迟?

Hello @range

你可以加一下打印跟一下你的代码,看看具体是在哪里耗时较长。