如何通过UDP发送实时数据?

时间:2022-01-04 03:54:54

I have to send a sequence of video frames over UDP as fast and real-time as possible and while I got the basics working, I am running into all sorts of difficulties. Some of my goals:

我必须在UDP上尽可能快和实时地发送一系列视频帧,在基本工作的同时,我遇到了各种各样的困难。我的一些目标:

  1. Data will normally be sent over dial-up (hence UDP instead of TCP), but also needs to support fast Ethernet.

    数据通常通过拨号(因此UDP而不是TCP)发送,但也需要支持快速以太网。

  2. It's OK to occasionally drop frames (hence UDP instead of TCP).

    偶尔删除帧是可以的(因此UDP而不是TCP)。

  3. Need low latency. The frame the remote receives should be one that was recently sent (no more than a few frames waiting in buffers).

    需要低延迟。远程接收的帧应该是最近发送的(不超过几个帧在缓冲区中等待)。

  4. I need to be able to detect the effective bandwidth so that I can compress the frames more or less to keep frame rate up.

    我需要能够检测有效的带宽,这样我就可以或多或少地压缩帧来保持帧速率。

I have managed to implement most of the pieces:

我成功地实现了大部分的部分:

  1. I break up frame data into one or more datagrams of about 500 bytes and each has a sequence number and other info. The receiver reassembles the entire frame and detects if any datagrams are missing.

    我将帧数据分解为一个或多个数据报,大约500字节,每个数据报都有序列号和其他信息。接收器重新组装整个帧并检测是否有任何数据报丢失。

  2. If the receiver detects more than a certain percentage of dropped frames (e.g. 50% over the last 10 frames), I send a TCP message to the sender to slow down by 50%. Sender than slowly increases speed by 5% each subsequent frame.

    如果接收方检测到超过一定百分比的丢失帧(例如,在最后10帧中50%),我将向发送方发送TCP消息,以降低50%的速度。发送者比缓慢增加速度5%每后续帧。

  3. Using System.Net.Sockets.UdpClient to send and receive the data.

    使用System.Net.Sockets。UdpClient用于发送和接收数据。

  4. I have a separate TCP channel used for control messages back to sender.

    我有一个单独的TCP通道,用于控制返回给发送者的消息。

My main difficulty right now is detecting the effective bandwidth and dealing with latency, especially over dial-up (max ~4,000 bytes/sec). For example, if I try to send 100,000 bytes/second using TcpClient.Send() they ALL seem to arrive (no dropped datagrams) but with large latency by the time last datagram arrives. I think the TcpClient.Send() function is blocking until the buffer is able to send which messes up my current algorithm.

我现在的主要困难是检测有效的带宽和处理延迟,特别是拨号上网(最大4000字节/秒)。例如,如果我尝试使用TcpClient.Send()发送100,000字节/秒,它们似乎都到达了(没有丢失的数据报),但是在最后一个数据报到达时,延迟会很大。我认为TcpClient.Send()函数正在阻塞,直到缓冲区能够发送,这就打乱了我当前的算法。

Can anybody point me to any sources of information for how to:

谁能告诉我如何:

  1. Detect actual bandwidth over UDP.

    检测UDP的实际带宽。

  2. A better algorithm for dynamically adjusting bandwidth to suit the available pipe.

    一种更好的动态调整带宽以适应可用管道的算法。

  3. Send data smoothly at the desired bandwidth.

    在所需的带宽上平稳地发送数据。

  4. A way to detect and keep latency down to a minimum.

    一种将延迟降低到最小的方法。

I have been spinning my wheels over the last week and every time I solve one problem it seems another rears up is head.

在过去的一周里,我一直在不停地思考,每次我解决了一个问题,就好像另一个问题又出现了。

1 个解决方案

#1


2  

You can also to add a timestamp to every packet. Then you can detect if the delay increase. In this case you send back a message to reduce the bandwidth.

您还可以向每个包添加时间戳。然后你可以检测延迟是否增加。在这种情况下,您发送一个消息以减少带宽。

On creating the connection you detect the latency with very few packets. This value should not change at running.

在创建连接时,您可以用非常少的数据包来检测延迟。此值在运行时不应更改。

#1


2  

You can also to add a timestamp to every packet. Then you can detect if the delay increase. In this case you send back a message to reduce the bandwidth.

您还可以向每个包添加时间戳。然后你可以检测延迟是否增加。在这种情况下,您发送一个消息以减少带宽。

On creating the connection you detect the latency with very few packets. This value should not change at running.

在创建连接时,您可以用非常少的数据包来检测延迟。此值在运行时不应更改。