Windows窗体VS Unity3D HttpWebRequest的服务表现 [英] Windows Form vs Unity3D HttpWebRequest Performace

查看:564
本文介绍了Windows窗体VS Unity3D HttpWebRequest的服务表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个简单的程序,从远程IP摄像机抓取的图像。经过研究日子里,我是能够提取的 JPEG MJPEG 实时流与我有示例代码。

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窗体。随着Windows窗体,我得到妥善80图片来自网络摄影机每10秒。

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

现在我移植的代码的 Unity3D 和我得到2帧每10秒。

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没有新的线程功能形成。我首先想到,在团结问题是因为我是显示图像,但它的不是

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.

我删除所显示的图像作为纹理的代码和所使用的整数计数接收到的图像的数量。不过,我得到的 2 4 图片每个<强10秒。。在 Windows窗体含义应用,我得到的 80 100 图片每个<强10秒。。

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.

接收 2 10 在几秒钟内的图像统一接受作为我在做什么。我写的代码似乎并不成为问题,因为它的 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 64并运行它,但没有解决问题。

  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.

下面是有这个问题简单的功能我。它运行过的团结,即使我把它称为从另一个线程

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 这个问题。也许它是在统一执行不力。不知道....

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?

推荐答案

是不是也许,人们必须使用的情况下阅读[一许多] 而不是 ??

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

阅读[很多]

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

返回值类型:System.Int32读入缓冲区的字节总数。这可以是<青霉>小于要求的字节数如果很多字节当前不可

Return Value Type: System.Int32 The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available

可以想象, ReadAsync 能帮上忙,的手动,虽然它会导致完全不同的代码。

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

这篇关于Windows窗体VS Unity3D HttpWebRequest的服务表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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