Windows Form与Unity3D HttpWebRequest性能 [英] Windows Form vs Unity3D HttpWebRequest Performance

查看:165
本文介绍了Windows Form与Unity3D HttpWebRequest性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个简单的程序,该程序可以从远程IP摄像机获取图像.经过几天的研究,我能够使用获得的示例代码从 MJPEG 实时流中提取 JPEG 图像.

I am working on a simple program that grabs image from a remote IP camera. After days of research, I was able to extract JPEG images from MJPEG live stream with sample codes I got.

我使用Windows Form做了一个原型.使用Windows Form,我每10秒钟从IP摄像机适当地接收80张图像.

I did a prototype with using Windows Form. With Windows Form, I receive appropriately 80 images every 10 second from the IP camera.

现在,我将代码移植到 Unity3D 上,每10秒可以得到2帧.

Now I ported the code to Unity3D and I get about 2 frames every 10 seconds.

因此,基本上 78 个图像未收到. 事情看起来像中世纪PowerPoint幻灯片放映.

So basically about 78 Images are not received. The thing looks like medieval PowerPoint slide show.

就像在Windows窗体中一样,我正在新线程中运行该函数.我最初以为Unity中的问题是因为我正在显示图像,但是不是.

I am running the function in new Thread just like I did in the Windows Form. I first thought that the problem in Unity is because I was displaying the image, but it wasn't.

我删除了将图像显示为纹理的代码,并使用整数来计数接收到的图像数量.不过,每隔 10秒,我得到大约 2 4 张图像.在 Windows窗体应用中,每隔 10秒,我得到 80 100 张图像.

I removed the code that displays the Image as a texture and used an integer to count the number of images received. Still, I get about 2 to 4 images every 10 seconds. Meaning in the Windows Form App, I get about 80 to 100 images every 10 seconds.

Unity 10 秒内接收 2 图像对于我正在做的工作不可接受.我写的代码似乎不是问题,因为它在 Windows窗体中可以很好地工作.

Receiving 2 images in 10 seconds in Unity is unacceptable for what I am doing. The code I wrote doesn't seem to be the problem because it works great in Windows Form.

我尝试过的事情:

  1. 虽然问题出在Unity3D编辑器运行时,所以我要求使用Windows 10 64bit并运行它,但这并不能解决问题.

  1. I though the problem is from the Unity3D Editor run-time, so I called for Windows 10 64bit and ran it but that didn't solve the problem.

脚本后端 Mono2x 更改为 IL2CPP ,但问题仍然存在.

Changed the Scripting Backend from Mono2x to IL2CPP but the problem still remains.

Api兼容性级别 .NET 2.0 更改为 .NET 2.0子集,并且没有任何更改.

Changed the Api compatibility Level from .NET 2.0 to .NET 2.0 Subset and nothing changed.

以下是我遇到的一个简单函数.即使我从另一个线程调用它,它在Unity上的运行也.

Below is a simple function I that is having that problem. It runs too slow on Unity even though I called it from another thread.

bool keepRunning = true;
    private void Decode_MJPEG_Images(string streamTestURL = null)
    {
        keepRunning = true;
        streamTestURL = "http://64.122.208.241:8000/axis-cgi/mjpg/video.cgi?resolution=320x240"; //For Testing purposes only

        // create HTTP request
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(streamTestURL);
        // get response
        WebResponse resp = req.GetResponse();

        System.IO.Stream imagestream = resp.GetResponseStream();

        const int BufferSize = 5000000;
        byte[] imagebuffer = new byte[BufferSize];
        int a = 2;
        int framecounter = 0;
        int startreading = 0;
        byte[] start_checker = new byte[2];
        byte[] end_checker = new byte[2];

        while (keepRunning)
        {
            start_checker[1] = (byte)imagestream.ReadByte();
            end_checker[1] = start_checker[1];

            //This if statement searches for the JPEG header, and performs the relevant operations
            if (start_checker[0] == 0xff && start_checker[1] == 0xd8)// && Reset ==0)
            {
                Array.Clear(imagebuffer, 0, imagebuffer.Length);
                //Rebuild jpeg header into imagebuffer
                imagebuffer[0] = 0xff;
                imagebuffer[1] = 0xd8;
                a = 2;
                framecounter++;
                startreading = 1;
            }

            //This if statement searches for the JPEG footer, and performs the relevant operations
            if (end_checker[0] == 0xff && end_checker[1] == 0xd9)
            {
                startreading = 0;
                //Write final part of JPEG header into imagebuffer
                imagebuffer[a] = start_checker[1];
                System.IO.MemoryStream jpegstream = new System.IO.MemoryStream(imagebuffer);

                Debug.Log("Received Full Image");
                Debug.Log(framecounter.ToString());


                //Display Image
            }

            //This if statement fills the imagebuffer, if the relevant flags are set
            if (startreading == 1 && a < BufferSize)
            {
                imagebuffer[a] = start_checker[1];
                a++;
            }

            //Catches error condition where a = buffer size - this should not happen in normal operation
            if (a == BufferSize)
            {
                a = 2;
                startreading = 0;
            }

            start_checker[0] = start_checker[1];
            end_checker[0] = end_checker[1];

        }


        resp.Close();
    }

现在,我将此问题归咎于 HttpWebRequest .也许在Unity中实现不佳.不确定....

Now I am blaming HttpWebRequest for this problem. Maybe it was poorly implemented in Unity. Not sure....

这是怎么回事?为什么会这样呢?我该如何解决?

What's going on? Why is this happening? How can I fix it?

推荐答案

也许是必须使用Read[a lot]而不是Read ??

Is it perhaps the case that one has to use Read[a lot] instead of Read ??

Read[a lot]:

https://msdn.microsoft.com/zh-CN/library/system.io.stream.read(v=vs.110).aspx

返回值类型:System.Int32读入缓冲区的字节总数.如果当前没有足够的字节数,则可以小于请求的字节数

可以想象,ReadAsync可以提供帮助,手册,尽管它会导致完全不同的代码.

Conceivably, ReadAsync could help, manual, although it results in wildly different code.

这篇关于Windows Form与Unity3D HttpWebRequest性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆