Gstreamer streaming

Hi,
Im using thus code on jetson with zed2i camera, on jetson devic I run the below code and on laptop i open stream using vlc.

Kindly assist me with my concerns :

  1. I have a delay mainly 1 second which is unaccepted at all and can’t be ignored at all.

  2. I’m thinking that, how can open stream on web browser not using vlc?

import sys
import numpy as np
import pyzed.sl as sl
import cv2
import gi
gi.require_version(‘Gst’, ‘1.0’)
gi.require_version(‘GstRtspServer’, ‘1.0’)
from gi.repository import Gst, GstRtspServer, GObject

def main():

# Create a ZED camera object
zed = sl.Camera()

init = sl.InitParameters()
init.camera_resolution = sl.RESOLUTION.HD720
init.camera_fps = 30
init.set_from_serial_number(serial)
#init.coordinate_units = sl.UNIT.METER

# Open the camera
err = zed.open(init)
if err != sl.ERROR_CODE.SUCCESS:
    print(repr(err))
    zed.close()
    exit(1)

# Set runtime parameters after opening the camera
runtime = sl.RuntimeParameters()

# Set image size to HD720 (1280x720)
image_size = sl.Resolution(1280, 720)
image_size_out = sl.Resolution(1280, 720)

# Declare your sl.Mat matrices with the updated size
image_zed = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)
image_zed_out = sl.Mat(image_size_out.width, image_size_out.height, sl.MAT_TYPE.U8_C4)

out_send = cv2.VideoWriter(
    'appsrc is-live=false ! videoconvert  ! video/x-raw,format=YUY2,width=1280,height=720,framerate=30/1 ! nvvidconv ! '
    'nvv4l2h264enc insert-sps-pps=1 bitrate=9000000 ! video/x-h264, '
    'stream-format=byte-stream ! rtph264pay config-interval=1 ! '
    'udpsink host=(ip) port=3000',
    cv2.CAP_GSTREAMER, 0, 30, (1280, 720), True)

if not out_send.isOpened():
    print('VideoWriter not opened')
    exit(0)

rtsp_port_num = 3000

server = GstRtspServer.RTSPServer.new()
server.props.service = "%d" % rtsp_port_num
server.attach(None)
codec = "H264"
updsink_port_num = 3000
factory = GstRtspServer.RTSPMediaFactory.new()
factory.set_launch(
    "(udpsrc name=pay0 port=%d buffer-size=524288 caps=\"application/x-rtp, media=video, clock-rate=90000, encoding-name=(string)%s, payload=96 \" )"
    % (updsink_port_num, codec))

factory.set_shared(True)
server.get_mount_points().add_factory("/ds-test", factory)

# output RTSP info
print("\n *** Launched RTSP Streaming at rtsp://localhost:%d/ds-test ***\n\n" % rtsp_port_num)

key = ' '
while True:
    err = zed.grab(runtime)

    if err == sl.ERROR_CODE.SUCCESS:
        # Retrieve the left image in the fixed resolution
        zed.retrieve_image(image_zed, sl.VIEW.LEFT, sl.MEM.CPU, image_size)
        zed.retrieve_image(image_zed_out, sl.VIEW.LEFT)

        # To recover data from sl.Mat to use it with OpenCV, use the get_data() method
        # It returns a numpy array that can be used as a matrix with OpenCV
        image_ocv = image_zed.get_data()
        image_ocv_out = image_zed_out.get_data()
        # Convert RGBA to RGB for out_send.write
        image_RGB = cv2.cvtColor(image_ocv_out, cv2.COLOR_RGBA2RGB)
        #cv2.imshow('Image', image_ocv)
        # out_send in A RTSP stream
        out_send.write(image_RGB)
        key = cv2.waitKey(1)

cv2.destroyAllWindows()
zed.close()

print("\nFINISH")

if name == “main”:
main()

@DaneLLL
@proventusnova

Hello @ahmadsondos103,

Thanks for reaching out!

What happens if you try with the following simplified pipe?

gst-launch-1.0 zedsrc ! autovideoconvert ! queue ! ximagesink

Do you still see the same delay?

As per your second question.
You could use WebRTC in order to stream it into a web browser.

May we ask what is your use case?

We want to understand better your situation so we can provide better support.

regards,
Andrew

Hi,
Please refer to the examples in FAQ:

Jetson AGX Orin FAQ
Q: Is there an example for running UDP streaming?
Q: How to autorun an app or commands at startup?

Certain latency is from camera source or network. You can compare with videotestsrc plugin to get more information.

For using WebRTC, you can also check

jetson-inference/docs/webrtc-server.md at master · dusty-nv/jetson-inference · GitHub
jetson-inference/docs/webrtc-html.md at master · dusty-nv/jetson-inference · GitHub

By using commqnd line , no I dont have same delay it works perfect

I want to stream from multiple zed2i camera, with encoding h264 because I have limited network bandwidth and receive it in webpage without any delay.
HD720/30

Only this is my concern, kindly assist.
Im trying alot of solution but I have a problem in encoding.

No the delay isnt relate to camera.

I will try html project codes in the link that u attached, but Mr. Danell how to ensure that Gpu accelerated encoding h264 will be applied ? becuse I have limited bandwidth and want to use multiple camera ??

Kindly assist ASAP

@ahmadsondos103,

Thanks for getting back with further details.

Since you are using nvv4l2h264enc you are making sure to use the HW codec since the element is part of the HW accelerated GStreamer elements by NVIDIA.

Just to make sure we are on the same page.

If you run the full encoding pipeline, with encode and rtsp stream from the command line, do you get any latency ?

regards,
Andrew

Firstly, thank Mr . Andrew for ur replying

Im sorry, now Im arriving office and check the command I used

gst-launch-1.0 rtspsrc location=rtsp://Ip:3000/ds-test latency=0 ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! autovideosink sync=false

And got no latency. Is there any info u need ?

Actually Mr. Andrew , Im trying to imclude zed sdk in my code to use its depthmap calculations in my project and at the same time make streaming from zed cam with h264 encoding to ba accessed from web client wothout zed sdk.

(For depthmap calculations can use sdk on client side but for streaming cant use sdk on web client) beacuse of that Im trying to do this in the code I provided.

Hello @ahmadsondos103,

Sorry for answering 10h later.
I believe there might be a great time difference between our time zones.

Ok, that is interesting.
Now I have just a few questions to make sure I understand the situation properly.

  1. The ZED application you appended in the initial post seems to be capturing from appsrc and then creating a UDP stream in port 3000. Is that application running on the Jetson board ?
  2. The last command you send receives a RTSP stream from port 3000, where is that stream coming from? Is that the stream created on the ZED application?
  3. Is the receiving stream being also run from the Jetson machine?
  4. When you capture the stream with VLC and get latency, do you run VLC on the same machine or a separate computer?

Lets try first to figure out the issue with the latency.
Then we can move to capturing using a web browser.

Looking forward to hearing your comments.

regards,
Andrew

Don’t worry Mr. Andrew,

Thank you for your interest in this matter. I appreciate your time and consideration

  1. The stream is created from the ZED camera connected to the same Jetson device that is running the Python script.

  2. the stream created on the ZED application.

  3. My setup consists of a ZED 2i camera connected to a Jetson device, which is then connected to my laptop via a network switch. The Python script running on the Jetson creates a UDP stream and sends it over the network using RTSP. When I access the stream on the same Jetson using the last command I shared, there is no delay.

  4. However, when I access the stream using VLC on my laptop with the RTSP URI, I experience a delay.

Hello @ahmadsondos103,

Thanks for getting back with the extra details, now I have a clearer idea of you set up.

Now, given that you are able to capture the stream and play it with no delay on the same device that is producing the UDP stream (the Jetson), and you experience the delay when you try to play it on your computer using VLC, it seems like the delay is most likely happening on the receiving end.

There are 3 different possible latency sources that I can think of:

  1. The receiving method, in this case VLC.
  2. The network.
  3. Some restriction or limitation on the computer system itself.

Lets try the following so we can take VLC out of the equation and rule option 1 out.

Could you please try capturing the UDP stream on your PC using the following GStreamer pipe?

gst-launch-1.0 udpsrc port=3000 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! queue ! avdec_h264 ! xvimagesink sync=false async=false -e

We will then evaluate if there is still the same latency happening. If there is no latency, it could be VLC that is not working as expected. However, if there is still latency happening, we could move to testing options 2 and 3.

Please let us know how it goes.

regards,
Andrew

Hi again Mr. Andrew

Thanks for ur support

I trued it , still theres a delay .0002 s, and update my code with import sys
import numpy as np
import pyzed.sl as sl
import cv2

def main():

# Create a ZED camera object
zed = sl.Camera()

init = sl.InitParameters()
init.camera_resolution = sl.RESOLUTION.HD720
init.depth_mode = sl.DEPTH_MODE.PERFORMANCE
init.camera_fps = 30
init.set_from_serial_number(serial)

# Open the camera
err = zed.open(init)
if err != sl.ERROR_CODE.SUCCESS:
    print(repr(err))
    zed.close()
    exit(1)

# Set runtime parameters after opening the camera
runtime = sl.RuntimeParameters()

# Set image size to HD720 (1280x720)
image_size = sl.Resolution(1280, 720)

# Declare your sl.Mat matrices with the updated size
image_zed = sl.Mat(image_size.width, image_size.height, sl.MAT_TYPE.U8_C4)

# Optimized GStreamer pipeline using nvmm memory
out_send = cv2.VideoWriter(
    'appsrc ! videoconvert ! nvvidconv ! '
    'nvv4l2h264enc insert-sps-pps=1 bitrate=11000000 ! h264parse ! '
    'rtph264pay config-interval=0 pt=96 ! udpsink host=receiver_ip port=3000 sync=false',
    cv2.CAP_GSTREAMER, 0, 30, (1280, 720), True)

if not out_send.isOpened():
    print('VideoWriter not opened')
    exit(0)

key = ' '
while True:
    err = zed.grab(runtime)

    if err == sl.ERROR_CODE.SUCCESS:
        # Retrieve the left image in the fixed resolution
        zed.retrieve_image(image_zed, sl.VIEW.LEFT, sl.MEM.CPU, image_size)

        # Convert the image from RGBA to RGB (4 channels to 3 channels)
        image_ocv = image_zed.get_data()
        image_RGB = cv2.cvtColor(image_ocv, cv2.COLOR_RGBA2RGB)

        # Stream the image
        out_send.write(image_RGB)
        #print("Frame sent")  # Add this line for debugging
        key = cv2.waitKey(1)

cv2.destroyAllWindows()
zed.close()

print("\nFINISH")

if name == “main”:
main()

and ur command that u shared :
gst-launch-1.0 udpsrc port=3000 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! queue ! avdec_h264 ! xvimagesink sync=false async=false -e

But the result is acceptable, but now I want ur help how to view it in web browser Im interested to transmit stream using h264 because the limitations in my network bandwidth.

Hello @ahmadsondos103,

That is great to hear.
Seems like the issue was probably VLC on your client side.
And it is also good to hear that the latency you are getting is now acceptable.

Now, before we move into the next tasks, which corresponds to streaming into a browser.
We would need to first understand a bit better your use case, so we can provide an answer tailored for your system.

  1. What is your system use case?
  2. Are you planning on accessing the stream from outside your local network?
  3. How many devices are you planning to have as clients of the stream?
  4. Why do you want to access the stream from the browser?

Looking forward to learn more about your project.

regards,
Andrew

  1. As previously mentioned, I have multiple ZED 2i cameras from which I need to stream video. Using the ZED SDK, I aim to obtain the depth map for each streamed frame. This is why the ZED SDK is integrated into my Python code.

  2. I will be accessing the stream over a local network, but due to limited bandwidth, I need to encode the stream using hardware-accelerated H.264 encoding.

  3. The system will support only one client connection at a time.

  4. I plan to develop a web interface to display the stream.

I believe I’ve provided all the necessary information. I would greatly appreciate your assistance as soon as possible. I’ve spent a significant amount of time exploring multiple solutions, but I have not yet achieved my desired outcome.

Note >> only i want to use zed sdk on jetson and on cliennt side I want to receve and display streams without using zed sdk

@ahmadsondos103,

Thanks for the extra details.

There is a couple of way you could pursue that.
For instance, there is this example which uses tcpclientsink in order to allow a react app to consume the stream.

However, in the past, what we have done for other customers is setting up a WebRTC system so that you can more easily and dynamically interact with the streams through a web app. However, this is easier said than done.

GStreamer already offers elements that you can play with in order to use WebRTC in your application, for instance webrtcsink and whipclientsink.

Now, if you want to speed up the development process, we would be glad to give you a hand.

Would you be opposed to jumping into a free call so we can go over the details?

regards,
Anrew

@proventusnova

Thank you for your follow-up, Mr. Andrew. I truly appreciate your support—your assistance has been the most helpful I’ve received so far.

I will give WebRTC a try. Additionally, I’m open to jumping on a call to discuss the details further. Let me know a suitable time!

1 Like

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