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

固件类型:官方提供的固件
固件文件名称:ROC-RK3588S-PC_Ubuntu20.04-Xfce-r3115_v1.3.0c_2401
固件下载地址:官网下载
Log日志:code.txt

本帖最后由 oranger 于 2024-4-10 09:55 编辑

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

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib

# 初始化GStreamer
Gst.init(None)
loop = GLib.MainLoop()

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,width=1280,height=720 ! autovideosink sync=false")

# 设置管道状态为播放
pipeline.set_state(Gst.State.PLAYING)

# 开始主循环
try:
    loop.run()
except KeyboardInterrupt:
    # 捕获键盘中断事件,停止管道并退出
    pipeline.set_state(Gst.State.NULL)
    loop.quit()

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

import heartrate; heartrate.trace(browser=True)

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打印出的两个耗时均为毫秒级耗时。
我想请问的是两个程序比较,为什么第二个程序的延迟增加的如此严重,如何能够减小延迟?
tchip_askquestions
code.txt (2.39 KB)

用RGA不要用opencv或者直接用我们的ffmedia
https://wiki.t-firefly.com/zh_CN/Firefly-Linux-Guide/manual_ubuntu.html#ffmedia

zhangzhenzhao 发表于 2024-4-11 09:04
用RGA不要用opencv或者直接用我们的ffmedia
https://wiki.t-firefly.com/zh_CN/Firefly-Linux-Guide/manua

您好,我按照wiki文档里操作后执行demo_opencv.py报错:
Firefly FFMedia: v2.2.4
input source is a rtsp url
INFO: ff_media: connectToServer: connected to server 192.168.0.131:554
INFO: ff_media: RTPSource: RTPSock current buffer size is(425984)
INFO: 16ModuleRtspClient: init: rtsp para w h: 1280 720, ws hs: 1280 720, format: H264
wait…INFO: ff_media: parseRTSPMessage: RTSP message was fragmented
INFO: 10MppDecoder: getTimeoutSample: 0x2d66dd80 frame info changed 1 error 0 discard 0
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 12ModuleMppDec: work: wait for productor ModuleRtspClient timeout
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
INFO: 10RTSPClient: teardownMediaSession: teardown session success
INFO: ff_media: connectToServer: connected to server 192.168.0.131:554
INFO: ff_media: RTPSource: RTPSock current buffer size is(425984)
INFO: ff_media: parseRTSPMessage: RTSP message was fragmented
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 12ModuleMppDec: work: wait for productor ModuleRtspClient timeout
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
INFO: 10RTSPClient: teardownMediaSession: teardown session success
INFO: ff_media: connectToServer: connected to server 192.168.0.131:554
INFO: ff_media: RTPSource: RTPSock current buffer size is(425984)
INFO: ff_media: parseRTSPMessage: RTSP message was fragmented
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 12ModuleMppDec: work: wait for productor ModuleRtspClient timeout
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
ERROR: 10RTSPClient: getOutputDataTimeOut: wait for session to get buffer time out
INFO: 10RTSPClient: teardownMediaSession: teardown session success
INFO: ff_media: connectToServer: connected to server 192.168.0.131:554
INFO: ff_media: RTPSource: RTPSock current buffer size is(425984)
INFO: ff_media: parseRTSPMessage: RTSP message was fragmented