野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 67|回复: 0

[求助] Gsteramer使用mppvideodec硬解码依旧延迟过大

[复制链接]
发表于 2024-4-10 09:50:12 | 显示全部楼层 |阅读模式
本帖最后由 orangeccc 于 2024-4-10 09:55 编辑

我希望能够使用RKNN推理无线相机的Rtsp流,格式为H264。由于延迟过大我尝试使用Gsteramer的mppvideodec进行硬解码,针对这部分进行了尝试。
我尝试使用如下程序进行收流:
  1. import gi
  2. gi.require_version('Gst', '1.0')
  3. from gi.repository import Gst, GObject, GLib

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

  7. 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")


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

  10. # 开始主循环
  11. try:
  12.     loop.run()
  13. except KeyboardInterrupt:
  14.     # 捕获键盘中断事件,停止管道并退出
  15.     pipeline.set_state(Gst.State.NULL)
  16.     loop.quit()
复制代码
此时的延迟是比较小的,约为0.2秒,但是我用下面的管道和程序进行测试则有非常大的延迟,最大时延迟会有一分钟左右:
  1. import gi
  2. import cv2
  3. import numpy as np
  4. from gi.repository import Gst
  5. gi.require_version('Gst', '1.0')
  6. # 初始化GStreamer
  7. Gst.init(None)
  8. import time  # 导入time模块

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

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

  32. # 设置GStreamer管道
  33. 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')

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

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

  43. cv2.destroyAllWindows()
  44. pipeline.set_state(Gst.State.NULL)
复制代码
gst_to_opencv以及new_sample打印出的两个耗时均为毫秒级耗时。
我想请问的是两个程序比较,为什么第二个程序的延迟增加的如此严重,如何能够减小延迟?

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-30 10:50 , Processed in 0.172694 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表