Nvidia Jetson Nano, Ubuntu 18.04, Python3.6 GStreamer1.14.5
I need to handle video in two different ways. One is to encode h264 using nvidia hardware and stream to a rtmpsink at 30 fps, two is to capture low fps single frames and hand them over via appsink for later object detection in OpenCV (not yet implemented)
Problem is that the streams runs very smoothly but whenever the second pipeline grabs a frame the stream does hold for a very short moment.
Being totally new to Python and GStreamer I used code from a example that appears to fit my needs and modified mostly the pipeline structure.
import gi
from time import sleep
gi.require_version("Gst", "1.0")
gi.require_version("GstApp", "1.0")
from gi.repository import Gst, GstApp, GLib
Gst.init()
_ = GstApp
from threading import Thread
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target=main_loop.run)
main_loop_thread.start()
pipeRGB = """
v4l2src io-mode=4 device=/dev/video0 ! video/x-raw, width=1920, height=1080, framerate=30/1 ! tee name=t
t. ! queue ! nvvidconv ! nvv4l2h264enc ! h264parse ! flvmux ! rtmpsink location='rtmp://example.com/live/rgb live=1'
t. ! queue ! videorate ! video/x-raw, width=1920, height=1080, framerate=1/5 ! queue ! videoconvert ! video/x-raw,format=BGR ! appsink name=sinkRGB
"""
pipelineRGB = Gst.parse_launch(pipeRGB)
pipelineRGB.set_state(Gst.State.PLAYING)
appsinkRGB = pipelineRGB.get_by_name("sinkRGB")
if appsinkRGB is None:
raise RuntimeError("Failed to retrieve 'sinkRGB' from the pipeline.")
try:
while True:
sleep(0.1)
sample = appsinkRGB.try_pull_sample(Gst.SECOND)
if sample is None:
continue
print("Got RGB Sample!")
except KeyboardInterrupt:
pass
pipelineRGB.set_state(Gst.State.NULL)
pipelineIR.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()
Running the code on the Jetson Nano results in the following output:
nvidia@localhost:~/gst$ python3 gst-rgb.py
Opening in BLOCKING MODE
NvMMLiteOpen : Block : BlockType = 4
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4
H264: Profile = 66, Level = 0
Got RGB Sample!
Got RGB Sample!
Got RGB Sample!
Got RGB Sample!
Every 5 seconds there is a short break in the live stream. This corresponds to the execution of the appsinkRGB pipeline of GStreamer (framerate=1/5
)
I used gst-launch-1.0 on the console with the same pipelines but using fakesink instead of appsink and the result was the same, stream pausing briefly every 5 seconds.
Pete Bo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.