Search Unity

serve media content using HTTPListener in unity3d

Discussion in 'Scripting' started by UnityCoder, Dec 6, 2016.

  1. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    Hello,

    In our unity application we are using HTTPListener to serve encrypted video file. If the video file size less then 200mb then its work without any problem but if its more then 400mb, its get stuck and giving outofmemory exception. Below is the code which i m using to server encrypted video :

    Code (CSharp):
    1. private void ListenerCallback(IAsyncResult result)
    2. {
    3.         HttpListener listener = (HttpListener) result.AsyncState;      
    4.         HttpListenerContext context = listener.EndGetContext(result);
    5.         HttpListenerRequest request = context.Request;
    6.         HttpListenerResponse response = context.Response;      
    7.      
    8.         string filePath = @"C:\temp.mp4";
    9.      
    10.         using (FileStream fs = new FileStream(filePath, FileMode.Open)) {
    11.             int startByte = -1;
    12.             int endByte = -1;
    13.             int byteRange = -1;
    14.             if (request.Headers.GetValues ("Range") != null) {
    15.                 string rangeHeader = request.Headers.GetValues ("Range") [0].Replace ("bytes=", "");
    16.                 string[] range = rangeHeader.Split ('-');
    17.                 startByte = int.Parse (range [0]);
    18.                 if (range [1].Trim ().Length > 0)
    19.                     int.TryParse (range [1], out endByte);                              
    20.                 if (endByte == -1)
    21.                     endByte = (int)fs.Length;
    22.             } else {
    23.                 startByte = 0;
    24.                 endByte = (int)fs.Length;
    25.             }
    26.          
    27.          
    28.             byte[] buffer = new byte[endByte - startByte];
    29.             fs.Position = startByte;
    30.             int read = fs.Read (buffer, 0, endByte - startByte);
    31.             fs.Flush ();
    32.             fs.Close ();
    33.          
    34.          
    35.             response.StatusCode = (int)HttpStatusCode.PartialContent;
    36.             response.StatusDescription="Partial Content";
    37.             response.Headers.Add("Content-Type", "video/mp4");
    38.             response.Headers.Add("Accept-Ranges", "bytes");
    39.             int totalCount = startByte + buffer.Length;
    40.             response.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startByte, totalCount - 1, totalCount));
    41.             response.Headers.Add("Content-Length", buffer.Length.ToString());
    42.             response.Headers.Add("Connection", "keep-alive");
    43.             response.OutputStream.Write(DecryptBytes(buffer), 0, buffer.Length);      
    44.             response.OutputStream.Flush();
    45. }
    Below is my Decrypt function which i used in above code:
    Code (CSharp):
    1.  
    2. private static byte[] DecryptBytes(byte[] message)
    3. {
    4.         var rijndael = new RijndaelManaged();
    5.         UnicodeEncoding UE = new UnicodeEncoding();
    6.         byte[] key = UE.GetBytes(keyGenerator("temp", "temp"));
    7.         rijndael.Key = key;
    8.         rijndael.IV = key;
    9.         //byte[] decMessage = DecryptBytes(rijndael, message);
    10.         rijndael.Padding = PaddingMode.None;
    11.         using (var stream = new MemoryStream())
    12.             using (var decryptor = rijndael.CreateDecryptor())
    13.                 using (var encrypt = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
    14.         {
    15.             encrypt.Write(message, 0, message.Length);
    16.             encrypt.FlushFinalBlock();
    17.             return stream.ToArray();
    18.         }
    19.     }
    I have strong doubt that its Decrypt function is main culprit for large file. I m trying to solve this problem since last week and still no luck. It would be a great help if someone can look into my code and point out what is the exact problem.
     
    Last edited: Dec 6, 2016
  2. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    Can anyone?? Please...
     
  3. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    Maybe encrypt and send it in separate blocks of a maximum of 200mb