Search Unity

Video C# Unity - Stream from IP camera over 3D Plane object

Discussion in 'Audio & Video' started by aegis80, Apr 1, 2019.

  1. aegis80

    aegis80

    Joined:
    Jun 6, 2018
    Posts:
    5
    Hello i made a program with Unity3D that "project" the image of an ip camera over a 3D object, a plane..

    The script is working well with streams i got from the internet but i have problems with my Panasonic WV-SW458.

    Do not know why because i get the right stream on the internet and in the VLC with both the internet camera and my Panasonic.

    I'll attach my code , maybe i'm missing something that, at the moment, i cannot see...

    Code (CSharp):
    1.  using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5. using System.Net;
    6. using System.IO;
    7.  
    8. public class WebStream : MonoBehaviour {
    9.  
    10.      [HideInInspector]
    11.      public Byte [] JpegData;
    12.      [HideInInspector]
    13.      public string resolution = "480x360";
    14.  
    15.      private Texture2D texture;
    16.      private Stream stream;
    17.      private WebResponse resp;
    18.      public MeshRenderer frame;
    19.  
    20.      public void Start() {
    21.        GetVideo();
    22.      }
    23.  
    24.      public void StopStream(){
    25.          stream.Close ();
    26.          resp.Close ();
    27.      }
    28.  
    29.      public void GetVideo(){
    30.          texture = new Texture2D(2, 2);
    31.  
    32.          //Working
    33.          //string url = "http://24.172.4.142/mjpg/video.mjpg?COUNTER";
    34.          //Working
    35.          string url = "http://200.36.58.250/mjpg/video.mjpg?resolution=640x480";
    36.          //NotWorking
    37.          //rtsp://192.168.140.120/MediaInput/h264
    38.          //NotWorking
    39.          //string url = "http://192.168.140.120/cgi-bin/mjpeg";
    40.  
    41.          HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
    42.          //For testing
    43.         // req.ProtocolVersion = HttpVersion.Version10;
    44.  
    45.          // get response
    46.          resp = req.GetResponse();
    47.          // get response stream
    48.          stream = resp.GetResponseStream();
    49.          frame.material.color = Color.white;
    50.          StartCoroutine (GetFrame ());
    51.      }
    52.  
    53.      public IEnumerator GetFrame (){
    54.         // Byte [] JpegData = new Byte[105536];
    55.        //   Byte [] JpegData = new Byte[205536];
    56.          Byte [] JpegData = new Byte[505536];
    57.          while(true) {
    58.              int bytesToRead = FindLength(stream);
    59.              if (bytesToRead == -1) {
    60.                  yield break;
    61.              }
    62.  
    63.              int leftToRead=bytesToRead;
    64.  
    65.              while (leftToRead > 0) {
    66.                 Debug.Log("Left To Read" + leftToRead);
    67.                  leftToRead -= stream.Read (JpegData, bytesToRead - leftToRead, leftToRead);
    68.                  yield return null;
    69.              }
    70.  
    71.              MemoryStream ms = new MemoryStream(JpegData, 0, bytesToRead, false, true);
    72.  
    73.              texture.LoadImage (ms.GetBuffer ());
    74.              frame.material.mainTexture = texture;
    75.              frame.material.color = Color.white;
    76.              stream.ReadByte(); // CR after bytes
    77.              stream.ReadByte(); // LF after bytes
    78.          }
    79.      }
    80.  
    81.      int FindLength(Stream stream)  {
    82.          int b;
    83.          string line="";
    84.          int result=-1;
    85.          bool atEOL=false;
    86.          while ((b=stream.ReadByte())!=-1) {
    87.              if (b==10) continue; // ignore LF char
    88.              if (b==13) { // CR
    89.                  if (atEOL) {  // two blank lines means end of header
    90.                      stream.ReadByte(); // eat last LF
    91.                      return result;
    92.                  }
    93.                  if (line.StartsWith("Content-Length:")) {
    94.                      result=Convert.ToInt32(line.Substring("Content-Length:".Length).Trim());
    95.                  } else {
    96.                      line="";
    97.                  }
    98.                  atEOL=true;
    99.              } else {
    100.                  atEOL=false;
    101.                  line+=(char)b;
    102.              }
    103.          }
    104.          return -1;
    105.      }
    106. }
     
    AdamsCheng likes this.
  2. ontreus

    ontreus

    Joined:
    May 18, 2015
    Posts:
    3
    your code works well, it worked for me
    Congratulations