Exemplo n.º 1
0
    public void GetVideo()
    {
        streamState = WebStreamState.Streaming;

        texture = new Texture2D(2, 2, TextureFormat.RGB24, false);

        // create HTTP request
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL);
        //Optional (if authorization is Digest)
        //req.Credentials = new NetworkCredential("username", "password");
        // get response
        WebResponse resp = req.GetResponse();

        // get response stream
        stream = resp.GetResponseStream();
        StartCoroutine(GetFrame());
    }
Exemplo n.º 2
0
    IEnumerator GetFrame()
    {
        Byte[] JpegData = new Byte[1000000];

        while (true)
        {
            int bytesToRead = FindLength(stream);
            //print(bytesToRead);
            if (bytesToRead == -1)
            {
                print("End of stream");
                streamState = WebStreamState.EOS;
                yield break;
            }

            int leftToRead = bytesToRead;

            while (leftToRead > 0)
            {
                //print(leftToRead);

                try
                {
                    leftToRead -= stream.Read(JpegData, bytesToRead - leftToRead, leftToRead);
                } catch (ObjectDisposedException e)
                {
                    print(e.Message);
                    leftToRead  = 0;
                    streamState = WebStreamState.EOS;
                    yield break;
                }

                yield return(null);
            }

            MemoryStream ms = new MemoryStream(JpegData, 0, bytesToRead, false, true);

            //texture.format = TextureFormat.YUY2;
            texture.LoadImage(ms.GetBuffer());
            frame.material.mainTexture = texture;
            stream.ReadByte();             // CR after bytes
            stream.ReadByte();             // LF after bytes
        }
    }