Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question IP camera footage not displaying over mesh renderer

Discussion in 'Multiplayer' started by diogomiguelmm, Sep 18, 2020.

  1. diogomiguelmm

    diogomiguelmm

    Joined:
    Sep 9, 2020
    Posts:
    6
    I'm doing this experiment of mine where the Unity project uses the AR/XR (ARFoundation) Kit. I had no problem setting everything up in Unity itself, as the documentation provided was pretty clear.

    Now to the IP Camera connection part. I've searched through various pages around the internet, from here on Stackoverflow to several Unity forum threads and managed to have the code below that is supposed to fetch the video stream via URL while also processing the image.

    When I start the program, instead of showing the footage, a plain white screen appears, like seen in the image below. No compiling errors are shown up in the console either.

    upload_2020-9-18_10-30-33.png

    I'm using an assisting iSpy software to help me generate a link for the camera. The IP camera I'm using is an AHD x001woxytn.
    https://www.ispyconnect.com/man.aspx?n=Ahd

    I'm just not sure if it's from the URL itself or if it's something wrong with the code, which is down below:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6. using System.Net;
    7. using System.IO;
    8.  
    9. public class IPCamConfig : MonoBehaviour
    10. {
    11.  
    12.     [HideInInspector]
    13.     public Byte[] JpegData;
    14.     [HideInInspector]
    15.     public string resolution = "320x240";
    16.  
    17.     private Texture2D texture;
    18.     private Stream stream;
    19.     private WebResponse resp;
    20.     public MeshRenderer frame;
    21.  
    22.     public void StopStream()
    23.     {
    24.         stream.Close();
    25.         resp.Close();
    26.     }
    27.  
    28.     public void GetVideo(string ip)
    29.     {
    30.         texture = new Texture2D(2, 2);
    31.         // create HTTP request
    32.         resolution = "320x240";
    33.         string url = "http://" + ip + "192.168.1.122/videostream.asf?usr=admin&pwd=" + resolution + "&resolution=320*240";
    34.         HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    35.         req.Credentials = new NetworkCredential("login", "password");
    36.         // get response
    37.         resp = req.GetResponse();
    38.         // get response stream
    39.         stream = resp.GetResponseStream();
    40.         frame.material.color = Color.white;
    41.         StartCoroutine(GetFrame());
    42.     }
    43.  
    44.     public IEnumerator GetFrame()
    45.     {
    46.         while (true)
    47.         {
    48.             int bytesToRead = FindLength(stream);
    49.             if (bytesToRead == -1)
    50.             {
    51.                 //                print("End of stream");
    52.                 yield break;
    53.             }
    54.  
    55.             int leftToRead = bytesToRead;
    56.  
    57.             while (leftToRead > 0)
    58.             {
    59.                 //                print (leftToRead);
    60.                 leftToRead -= stream.Read(JpegData, bytesToRead - leftToRead, leftToRead);
    61.                 yield return null;
    62.             }
    63.  
    64.             MemoryStream ms = new MemoryStream(JpegData, 0, bytesToRead, false, true);
    65.  
    66.             texture.LoadImage(ms.GetBuffer());
    67.             frame.material.mainTexture = texture;
    68.             frame.material.color = Color.white;
    69.             stream.ReadByte(); // CR after bytes
    70.             stream.ReadByte(); // LF after bytes
    71.         }
    72.     }
    73.  
    74.     int FindLength(Stream stream)
    75.     {
    76.         int b;
    77.         string line = "";
    78.         int result = -1;
    79.         bool atEOL = false;
    80.  
    81.         while ((b = stream.ReadByte()) != -1)
    82.         {
    83.             if (b == 10) continue; // ignore LF char
    84.             if (b == 13)
    85.             { // CR
    86.                 if (atEOL)
    87.                 {  // two blank lines means end of header
    88.                     stream.ReadByte(); // eat last LF
    89.                     return result;
    90.                 }
    91.                 if (line.StartsWith("Content-Length:"))
    92.                 {
    93.                     result = Convert.ToInt32(line.Substring("Content-Length:".Length).Trim());
    94.                 }
    95.                 else
    96.                 {
    97.                     line = "";
    98.                 }
    99.                 atEOL = true;
    100.             }
    101.             else
    102.             {
    103.                 atEOL = false;
    104.                 line += (char)b;
    105.             }
    106.         }
    107.         return -1;
    108.     }
    109. }
    Credit goes to @Whlam for the code.