Python binding send RTP

setup:
• Hardware Platform:Jetson Xavier NX
• DeepStream Version: 6.1
• JetPack Version: 5.0.1 DP
• TensorRT Version: 8.4.0.11
• Issue Type( questions)

Hi folks,
I run some Python script on my Jetson Xavier that captures the h264 stream and implements YOLO + tracker the display the stream on the screen (this pipeline work fine).

Now I add to the pipeline more elements my goal is to encode the new video with the annotation of Yolo and stream rtp UDP multicast stream.
I don’t get any error but unfortunately, I don’t see RTP packets in the Wireshark I don’t understand what is the problem, In addition, I would be happy to understand what I need to add to my pipeline after I have the stream with the inference I want to resize to 704X576 and then send the video and not stream video size 640X640.
I will be happy to help.
Here is my code:

#!/usr/bin/env python3

################################################################################
# SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################

import sys

sys.path.append('../')
import configparser
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import GLib, Gst
from common.bus_call import bus_call
import pyds

fps_streams = {}

PGIE_CLASS_ID_PERSON = 4
PGIE_CLASS_ID_GAZEBO = 3
PGIE_CLASS_ID_TRUCK = 2
PGIE_CLASS_ID_PICKUP = 1
PGIE_CLASS_ID_CAR = 0
past_tracking_meta = [0]

# UDP Properties
PORT = 46002
MULTI_GROUP = "234.0.0.0"
PORT_SINK = 46003
MULTI_GROUP_SINK = "244.0.0.0"



def osd_sink_pad_buffer_probe(pad, info, u_data):
    frame_number = 0
    # Intiallizing object counter with 0.
    obj_counter = {
        PGIE_CLASS_ID_PERSON:0,
        PGIE_CLASS_ID_GAZEBO:0,
        PGIE_CLASS_ID_TRUCK:0,
        PGIE_CLASS_ID_PICKUP:0,
        PGIE_CLASS_ID_CAR:0
    }

    num_rects = 0
    gst_buffer = info.get_buffer()
    if not gst_buffer:
        print("Unable to get GstBuffer ")
        return

    # Retrieve batch metadata from the gst_buffer
    # Note that pyds.gst_buffer_get_nvds_batch_meta() expects the
    # C address of gst_buffer as input, which is obtained with hash(gst_buffer)
    batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
    l_frame = batch_meta.frame_meta_list
    while l_frame is not None:
        try:
            # Note that l_frame.data needs a cast to pyds.NvDsFrameMeta
            # The casting is done by pyds.NvDsFrameMeta.cast()
            # The casting also keeps ownership of the underlying memory
            # in the C code, so the Python garbage collector will leave
            # it alone.
            frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
        except StopIteration:
            break

        frame_number = frame_meta.frame_num
        num_rects = frame_meta.num_obj_meta
        l_obj = frame_meta.obj_meta_list
        while l_obj is not None:
            try:
                # Casting l_obj.data to pyds.NvDsObjectMeta
                obj_meta = pyds.NvDsObjectMeta.cast(l_obj.data)
            except StopIteration:
                break
            obj_counter[obj_meta.class_id] += 1
            try:
                l_obj = l_obj.next
            except StopIteration:
                break


        try:
            l_frame = l_frame.next
        except StopIteration:
            break
    # past traking meta data
    if (past_tracking_meta[0] == 1):
        l_user = batch_meta.batch_user_meta_list
        while l_user is not None:
            try:
                # Note that l_user.data needs a cast to pyds.NvDsUserMeta
                # The casting is done by pyds.NvDsUserMeta.cast()
                # The casting also keeps ownership of the underlying memory
                # in the C code, so the Python garbage collector will leave
                # it alone
                user_meta = pyds.NvDsUserMeta.cast(l_user.data)
            except StopIteration:
                break
            if (user_meta and user_meta.base_meta.meta_type == pyds.NvDsMetaType.NVDS_TRACKER_PAST_FRAME_META):
                try:
                    # Note that user_meta.user_meta_data needs a cast to pyds.NvDsPastFrameObjBatch
                    # The casting is done by pyds.NvDsPastFrameObjBatch.cast()
                    # The casting also keeps ownership of the underlying memory
                    # in the C code, so the Python garbage collector will leave
                    # it alone
                    pPastFrameObjBatch = pyds.NvDsPastFrameObjBatch.cast(user_meta.user_meta_data)
                except StopIteration:
                    break
                for trackobj in pyds.NvDsPastFrameObjBatch.list(pPastFrameObjBatch):
                    print("streamId=", trackobj.streamID)
                    print("surfaceStreamID=", trackobj.surfaceStreamID)
                    for pastframeobj in pyds.NvDsPastFrameObjStream.list(trackobj):
                        print("numobj=", pastframeobj.numObj)
                        print("uniqueId=", pastframeobj.uniqueId)
                        print("classId=", pastframeobj.classId)
                        print("objLabel=", pastframeobj.objLabel)
                        for objlist in pyds.NvDsPastFrameObjList.list(pastframeobj):
                            print('frameNum:', objlist.frameNum)
                            print('tBbox.left:', objlist.tBbox.left)
                            print('tBbox.width:', objlist.tBbox.width)
                            print('tBbox.top:', objlist.tBbox.top)
                            print('tBbox.right:', objlist.tBbox.height)
                            print('confidence:', objlist.confidence)
                            print('age:', objlist.age)
            try:
                l_user = l_user.next
            except StopIteration:
                break
    return Gst.PadProbeReturn.OK


def main(args=1):
    Gst.init(None)

    # Create gstreamer elements
    # Create Pipeline element that will form a connection of other elements
    print("Creating Pipeline \n ")
    pipeline = Gst.Pipeline()

    if not pipeline:
        sys.stderr.write(" Unable to create Pipeline \n")

    # Source element for reading from the file
    print("Creating Source \n ")
    # source = Gst.ElementFactory.make("filesrc", "file-source")
    source = Gst.ElementFactory.make("udpsrc", "UDP-source")
    source.set_property("port", PORT)
    source.set_property("multicast-group", MULTI_GROUP)

    if not source:
        sys.stderr.write(" Unable to create Source \n")

    # Since the data format in the input file is elementary h264 stream,
    # we need a h264parser
    print("Creating H264Parser \n")
    h264parser = Gst.ElementFactory.make("h264parse", "h264-parser")
    # h264parser = Gst.ElementFactory.make("mpeg4videoparse", "mpeg4-parser")
    if not h264parser:
        sys.stderr.write(" Unable to create h264 parser \n")

    # Use nvdec_h264 for hardware accelerated decode on GPU
    print("Creating Decoder \n")
    decoder = Gst.ElementFactory.make("nvv4l2decoder", "nvv4l2-decoder")
    decoder.set_property('enable-max-performance', 1)
    if not decoder:
        sys.stderr.write(" Unable to create Nvv4l2 Decoder \n")

    # Create nvstreammux instance to form batches from one or more sources.
    streammux = Gst.ElementFactory.make("nvstreammux", "Stream-muxer")
    if not streammux:
        sys.stderr.write(" Unable to create NvStreamMux \n")

    streammux.set_property('width', 640)
    streammux.set_property('height', 640)
    streammux.set_property('batch-size', 1)
    streammux.set_property('batched-push-timeout', 40000)
    streammux.set_property('live-source', 1)
    streammux.set_property('compute-hw', 1)

    # Use nvinfer to run inferencing on decoder's output,
    # behaviour of inferencing is set through config file
    pgie = Gst.ElementFactory.make("nvinfer", "primary-inference")
    if not pgie:
        sys.stderr.write(" Unable to create pgie \n")
    # Set properties of pgie
    pgie.set_property('config-file-path', "config_infer_primary_yoloV5.txt")

    tracker = Gst.ElementFactory.make("nvtracker", "tracker")
    if not tracker:
        sys.stderr.write(" Unable to create tracker \n")

    # Set properties of tracker
    config = configparser.ConfigParser()
    config.read('tracker_config.txt')
    config.sections()

    for key in config['tracker']:
        if key == 'tracker-width':
            tracker_width = config.getint('tracker', key)
            tracker.set_property('tracker-width', tracker_width)
        if key == 'tracker-height':
            tracker_height = config.getint('tracker', key)
            tracker.set_property('tracker-height', tracker_height)
        if key == 'gpu-id':
            tracker_gpu_id = config.getint('tracker', key)
            tracker.set_property('gpu_id', tracker_gpu_id)
        if key == 'll-lib-file':
            tracker_ll_lib_file = config.get('tracker', key)
            tracker.set_property('ll-lib-file', tracker_ll_lib_file)
        if key == 'll-config-file':
            tracker_ll_config_file = config.get('tracker', key)
            tracker.set_property('ll-config-file', tracker_ll_config_file)
        if key == 'enable-batch-process':
            tracker_enable_batch_process = config.getint('tracker', key)
            tracker.set_property('enable_batch_process', tracker_enable_batch_process)
        if key == 'enable-past-frame':
            tracker_enable_past_frame = config.getint('tracker', key)
            tracker.set_property('enable_past_frame', tracker_enable_past_frame)
        if key == 'compute-hw':
            tracker_enable_compute_hw = config.getint('tracker', key)
            tracker.set_property('compute-hw', tracker_enable_compute_hw)

    nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor")
    if not nvvidconv:
        sys.stderr.write(" Unable to create nvvidconv \n")

    # Create OSD to draw on the converted RGBA buffer
    nvosd = Gst.ElementFactory.make("nvdsosd", "onscreendisplay")

    if not nvosd:
        sys.stderr.write(" Unable to create nvosd \n")

    # Add encoding and transmit udp stream
    nvvidconv_postosd = Gst.ElementFactory.make("nvvideoconvert", "convertor_postosd")
    # Convert to OCU format 704X576
    # nvvidconv_postosd.set_property("width", 704)
    # nvvidconv_postosd.set_property("height", 576)
    # for rtp stream
    caps = Gst.ElementFactory.make("capsfilter", "filter")
    caps.set_property("caps", Gst.Caps.from_string(
        "application/x-rtp, media=video, clock-rate=90000, encoding-name=(string)H264, payload=96"))
    encoder = Gst.ElementFactory.make("nvv4l2h264enc", "encoder")
    encoder.set_property('maxperf-enable', 1)
    encoder.set_property('preset-level', 1)
    encoder.set_property('insert-sps-pps', 1)
    encoder.set_property('insert-vui', 1)
    rtppay = Gst.ElementFactory.make("rtph264pay", "rtppay")
    sink = Gst.ElementFactory.make("udpsink", "udpsink")
    sink.set_property('port', PORT_SINK)
    sink.set_property('auto-multicast', True)
    sink.set_property('host',  MULTI_GROUP_SINK)
    # sink.set_property('sync', 1)
    # sink.set_property('async', False)




    print("Adding elements to Pipeline \n")
    pipeline.add(source)
    pipeline.add(h264parser)
    pipeline.add(decoder)
    pipeline.add(streammux)
    pipeline.add(pgie)
    pipeline.add(tracker)
    pipeline.add(nvvidconv)
    pipeline.add(nvosd)
    pipeline.add(nvvidconv_postosd)
    pipeline.add(caps)
    pipeline.add(encoder)
    pipeline.add(rtppay)
    pipeline.add(sink)

    # we link the elements together
    # file-source -> h264-parser -> nvh264-decoder ->
    # nvinfer -> nvvidconv -> nvosd -> video-renderer
    print("Linking elements in the Pipeline \n")
    source.link(h264parser)
    h264parser.link(decoder)

    sinkpad = streammux.get_request_pad("sink_0")
    if not sinkpad:
        sys.stderr.write(" Unable to get the sink pad of streammux \n")
    srcpad = decoder.get_static_pad("src")
    if not srcpad:
        sys.stderr.write(" Unable to get source pad of decoder \n")
    srcpad.link(sinkpad)
    streammux.link(pgie)
    pgie.link(tracker)
    tracker.link(nvvidconv)
    nvvidconv.link(nvosd)
    nvosd.link(nvvidconv_postosd)
    nvvidconv_postosd.link(caps)
    caps.link(encoder)
    encoder.link(rtppay)
    rtppay.link(sink)

    # create and event loop and feed gstreamer bus mesages to it
    loop = GLib.MainLoop()

    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect("message", bus_call, loop)


    # Lets add probe to get informed of the meta data generated, we add probe to
    # the sink pad of the osd element, since by that time, the buffer would have
    # had got all the metadata.
    osdsinkpad = nvosd.get_static_pad("sink")
    if not osdsinkpad:
        sys.stderr.write(" Unable to get sink pad of nvosd \n")
    osdsinkpad.add_probe(Gst.PadProbeType.BUFFER, osd_sink_pad_buffer_probe, 0)

    print("Starting pipeline \n")

    # start play back and listed to event
    pipeline.set_state(Gst.State.PLAYING)
    try:
        loop.run()
    except:
        pass

    # cleanup
    pipeline.set_state(Gst.State.NULL)


if __name__ == '__main__':
    sys.exit(main(sys.argv))

I try to replace caps with:

    caps.set_property(
        "caps", Gst.Caps.from_string("video/x-raw(memory:NVMM), format=I420")
    )```



and I get this Warning

NvMMLiteOpen : Block : BlockType = 261
NVMEDIA: Reading vendor.tegra.display-size : status: 6
NvMMLiteBlockCreate : Block : BlockType = 261
NvMMLiteOpen : Block : BlockType = 4
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4
H264: Profile = 66, Level = 0
NVMEDIA: Need to set EMC bandwidth : 313333
NVMEDIA: Need to set EMC bandwidth : 313333
NVMEDIA_ENC: bBlitMode is set to TRUE

There may be a problem with your port or IP configuration. You can try it out according to the configuration in our demo first. deepstream-test1-rtsp-out

You can use nvvideoconvert plugin to scale the video to any resolution.

about this warning :

NVMEDIA: Need to set EMC bandwidth : 313333
NVMEDIA: Need to set EMC bandwidth : 313333
NVMEDIA_ENC: bBlitMode is set to TRUE

this is not may problem?
In addition before the encoder is correct to connect caps elemnt with this property:

    caps = Gst.ElementFactory.make("capsfilter", "caps")
    caps.set_property(
        "caps", Gst.Caps.from_string("video/x-raw(memory:NVMM), width=(int)704, height=(int)576, \
format=(string)NV12, framerate=(fraction)30/1'")
    )

No. There is no Error log. So did deepstream-test1-rtsp-out run ok in your enviroment?

When I try to run I test1-rtsp-out I get:

Error: gst-resource-error-quark: Could not get/set settings from/on resource. (13): gstmultiudpsink.c(1276): gst_multiudpsink_configure_client (): /GstPipeline:pipeline0/GstUDPSink:udpsink:
Could not join multicast group: Error joining multicast group: No such device

I try to solve this problem by:

sudo route add -net 224.224.255.255 netmask 255.0.0.0 eth0

bt I get this message:

route: netmask doesn't match route address
Usage: inet_route [-vF] del {-host|-net} Target[/prefix] [gw Gw] [metric M] [[dev] If]
       inet_route [-vF] add {-host|-net} Target[/prefix] [gw Gw] [metric M]
                              [netmask N] [mss Mss] [window W] [irtt I]
                              [mod] [dyn] [reinstate] [[dev] If]
       inet_route [-vF] add {-host|-net} Target[/prefix] [metric M] reject
       inet_route [-FC] flush      NOT supported

So I replace the ip host udpsink to my multicast ip 244.0.0.0
and again I don’t see video I only see in the wireshark some arbitrary message on protocol ARP (attached image to this post)

From the log, it should be a network issue in your environment. see if this post works: https://askubuntu.com/questions/1317392/error-netmask-doesnt-match-route-address. You can try to reconfigure your network environment.

I will try but still I not understand why It’s a problem to use multicast IP like 244.0.0.0 Why should I insist on 224.224.255.255?
or it’s just for checking, In addition how I can reconfigure my network environment?

You can check the IP multicast address range.

Ok I fix the problem of ip port ‘224.224.255.255’ by add manual from jetson GUI and reboot know I see UDP packets In wireshark.


thank you for your help we make a progress still I try to understand because I want to run my code, I need to work only this ip: ‘224.224.255.255’ it’s multicast ?
In addition I want send rtp not rtsp so if I understand correctly I dont need use RTSP_Server method.
And about resize the video using nvvconverter I don;t see any property of the element of resize I need use caps?
for example

    nvvidconv_postosd = Gst.ElementFactory.make("nvvideoconvert", "convertor_postosd")
    # Convert to OCU format 704X576
    nvvidconv_postosd.set_property("convertor_postosd", 
                                   Gst.Caps.from_string("video/x-raw(memory:NVMM),width=(int)704, height=(int)576"))

or

    caps = Gst.ElementFactory.make("capsfilter", "caps")
    caps.set_property("caps", Gst.Caps.from_string("video/x-raw(memory:NVMM), width=(int)704, height=(int)576, format=(string)I420"))

unfortunately, In my code, I see UDP packets in Wireshark but I can’t see the video in VLC maybe I missing something like Paylod?

About resize the video, you can refer to our Guide about nvvideoconvert: src-crop dest-crop.
About the video cannot be displayed in VLC. You need to check your port and IP configuration.

My vlc configuration rtp://@224.224.255.255:5400
and still, I don’t see the video I am suspicious that something in my pipeline is not correct although I don’t get error in log commands.

I solve the problem by replace rtph264pay with two difreent elements

  1. mpegtsmux
  2. rtpmp2tpay
    now I can see video in multicast

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.