如何实现发送方的TCP流量限制?
我即将用 C++ (windows/linux) 为多个用户实现一个网络摄像头视频聊天系统。由于“普通”用户通常通过 DSL/电缆进行连接,因此我的(首选)TCP/IP 连接存在很大的带宽限制。
基本思想是在给定发送方带宽限制的情况下传输尽可能高的帧速率。 (其他应用程序可能仍然需要在后台使用互联网带宽。)第二步,相机捕获速率应根据网络限制自动调整,以避免不必要的 CPU 开销。
我拥有的是必须传输到远程端的恒定的压缩图像流(缓冲区大小变化很大)。假设有 20kb/s 的限制,我如何最好地实现该限制? (请注意,用户应定义此限制!)
提前谢谢, Mayday
编辑:问题澄清(抱歉!)
- 这是关于如何对任意 TCP/IP 连接进行流量整形。
- 这不是我的用例所建议的如何实现图像速率/质量降低。 (尽管我还没有考虑自动调整图像压缩。(Thx Jon))
I'm about to implement a webcam video chat system for multiple users in C++ (windows/linux). As the 'normal' user is usually connected via DSL/cable, there is a strong bandwidth limitation for my (prefered) TCP/IP connections.
The basic idea is to transmit the highest possible framerate given a bandwidth limitation for the sender side. (Other applications may still require internet bandwidth in the background.) In a second step, the camera-capture-rate shall be automatically adjusted to the network limitations to avoid unncessary CPU overhead.
What I have is a constant stream of compressed images (with strongly variing buffer sizes) that have to be transmitted to the remote side. Given a limitation of let's say 20kb/s, how do I best implement that limitation? (Note that the user shall define this limit!)
Thx in advance,
Mayday
Edit: Question clearifications (sry!)
- It's about how to traffic-shape an arbitrary TCP/IP connection.
- It's not how to implement image rate/quality reduction as my use-case suggests. (Altough I didn't consider to automatically adjust image compression, yet. (Thx Jon))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以采取两件事来减少带宽:
在实现选择图像大小和数量以遵守用户选择的限制的算法时,您必须在简单/鲁棒算法之间进行平衡以及一种高性能算法(能够最大限度地利用限制的算法)。
我尝试的第一种方法是使用您在任何时间点使用的带宽的滚动平均值来“播种”您的算法。每隔一段时间检查一次平均值。如果它超过了您的限制,请指示算法减少使用(与您超出限制的程度成比例)。如果它明显低于您的限制,例如低于 90%,请指示算法使用更多。
less/more 指令可能是一个变量(可能是 int 或 float,这里确实有很大的创造性空间),供您的算法用来决定:
There are two things you can do to reduce your bandwidth:
When implementing an algorithm that picks image size and quantity to honor the user-selected limit, you have to balance between a simple/robust algorithm and a performant algorithm (one that makes maximum use out of the limit).
The first approach I would try is to use a rolling average of the bandwidth you are using at any point in time to "seed" your algorithm. Every once in a while, check the average. If it becomes more than your limit, instruct the algorithm to use less (in proportion to how much you overstepped the limit). If it becomes significantly lower than your limit, say less than 90%, instruct the algorithm to use more.
The less/more instruction might be a variable (maybe
int
orfloat
, really there is much scope for inventiveness here) used by your algorithm to decide:您需要至少 3 个帧的缓冲区/队列:
当网络发送方完成发送帧时,它将“下一个要发送的”帧复制到“当前发送”的槽中。当相机读取器完成从相机复制帧时,它会用复制的帧替换“下一个要发送的”帧。 (显然,需要围绕“下一个要发送的”帧进行同步)。
然后,发送方可以根据需要调整其发送速率。如果它的运行速度比相机慢,它就会丢帧。
You need a buffer / queue of at least 3 frames:
When the network sender finishes sending a frame, it copies the "to be sent next" frame to the "currently sending" slot. When the camera reader finishes copying a frame from the camera, it replaces the "to be sent next" frame with the copied frame. (Obviously, synchronisation is required around the "to be sent next" frame).
The sender can then modulate its sending rate as it sees fit. If it's running slower than the camera, it will simply drop frames.