通过命名管道发送的视频帧非常慢 [英] Video frames send through Named Pipe very slow
问题描述
我有一个可以处理带有其他图形的视频并在Windows窗体中显示输出的应用程序.我不得不将视频处理分为后台进程/服务,然后将视频/帧显示到前端UI.准备好后,图像就会通过命名管道发送.
I have an application that processes video with additional graphics and displays the output in a Windows Form. I had to separate the video processing into a background process/service and displaying the video/frames to a frontend UI. The images are sent via named pipes as soon as they are ready.
一切正常,但是问题在于通过命名管道发送视频非常慢.这是相关的代码:
Everything works, but the issue is that sending the video through the named pipe is very slow. Here's the relevant code:
背景流程:
if (serverPipe.isConnected()) {
ImageConverter converter = new ImageConverter();
erverPipe.WriteBytes((byte[])converter.ConvertTo(_currentImage.ToBitmap(), typeof(byte[])));
serverPipe.Flush();
}
前景UI应用程序:
clientPipe.DataReceived += (sndr, args) =>
{
form.updatePictureBox(args.Data);
};
命名的管道代码来自于此帖子: c#全双工异步命名管道.NET
The named pipe code is from this post: c# Full Duplex Asynchronous Named Pipes .NET
Github链接: https://github. com/cliftonm/clifton/tree/master/Clifton.Core/Clifton.Core.Pipes
Github link: https://github.com/cliftonm/clifton/tree/master/Clifton.Core/Clifton.Core.Pipes
我通过命名管道获得的FPS接近每秒1或2帧.我不确定是否要处理命名管道的工作方式限制或代码效率低下.
The FPS that I get through the named pipe is close to 1 or 2 frames per second. I'm not sure if I'm dealing with a limitation in the way named pipes work or an inefficiency in my code.
推荐答案
我使用您提到的库编写了一个客户端和服务器控制台应用程序,并且在我的机器上,这两者之间需要大约50-60ms的时间来发送4 MB的数据流程.因此,我认为您不应对命名管道限制.我建议您尝试衡量代码的其他部分,并缩小导致FPS降低的原因.
I wrote a client and a server console app using the library you mentioned and on my machine it takes about 50-60ms to send 4 MB of data between the two processes. Therefore I think that you do not deal with a named pipe limitation. I suggest that you try to measure the other parts of your code and try to narrow down what the cause for the low FPS is.
服务器
using Clifton.Core.Pipes;
using System;
using System.Diagnostics;
namespace NamedPipeTest
{
class Program
{
static void Main(string[] args)
{
StartServer();
Console.ReadLine();
}
private static void StartServer()
{
ServerPipe serverPipe = new ServerPipe("Test", p => p.StartByteReaderAsync());
var sw = new Stopwatch();
serverPipe.Connected += (s, ea) => {
Console.WriteLine("Server connected");
sw.Start();
};
serverPipe.DataReceived += (s, ea) => {
Console.WriteLine($"{DateTime.Now.ToString("hh:mm:ss.fff")}\t {sw.ElapsedMilliseconds}\t Data received. Length: {ea.Len}");
sw.Restart();
};
}
}
客户
using Clifton.Core.Pipes;
using System;
namespace NamedPipeTest
{
class Program
{
static void Main(string[] args)
{
StartClient(iterations: 100);
Console.ReadLine();
}
private static void StartClient(int iterations)
{
ClientPipe clientPipe = new ClientPipe(".", "Test", p => p.StartByteReaderAsync());
clientPipe.Connect();
byte[] data = CreateRandomData(sizeMb: 4);
for (int i = 0; i < iterations; i++)
{
clientPipe.WriteBytes(data);
Console.WriteLine($"{DateTime.Now.ToString("hh:mm:ss.fff")}\t Wrote data {i}");
}
}
private static byte[] CreateRandomData(int sizeMb)
{
var data = new byte[1024 * 1024 * sizeMb];
new Random().NextBytes(data);
return data;
}
}
}
这篇关于通过命名管道发送的视频帧非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!