Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream AVI file from memory using IIS/Asp.Net

Tags:

asp.net

iis

I have some AVI files on disk but they are encrypted. I'm wondering if there is a way I can decrypt them and stream them to the browser (using MemoryStream or something similar) without having to write any files?

I know there is Windows Media Services but I'm using a Vista machine and Windows Media Services will only install in Windows Server 2003 and 2008.

Is there a way to accomplish this without too much trouble or is Media Services/Windows Server the only way to go? And if there is, would I use something like a custom IHttpHandler (.ashx file)?


Edit:

I have decided to use a custom IHttpHandler. What basic code would I need to have the video play?

like image 820
mcjabberz Avatar asked Jun 12 '26 03:06

mcjabberz


1 Answers

I wouldn't want to use a MemoryStream for video. Assuming you can create a CryptoStream (found in the System.Security.Cryptography namespace) over the encrypted AVI file you should be able to just pump a Read from that to a Write on the Response.OutputStream in a IHttpHandler. Something like:-

 byte[] buffer = new byte[65556];  // adjust the buffer size as yo prefer.
 CryptoStream inStream = YourFunctionToDecryptAVI(aviFilePath);
 int bytesRead = inStream.Read(buffer, 0, 65556); 
 while (bytesRead != 0)
 {
    context.Response.OutputStream.Write(buffer, 0, bytesRead); 
    bytesRead = inStream.Read(buffer, 0, 65556);
    if (!context.Response.IsClientConnected) break;
 }
 Response.Close();  //see edit note.

Make sure you turn off response buffer and specify a content type.

Edit:

Ordinarily I hate calling close, it seems so draconian. However whilst chunked encoding shouldn't require it, in the case of streamed video it may be that the client doesn't like it. Also with large data transfers closing the connection is not really big deal.

like image 120
AnthonyWJones Avatar answered Jun 14 '26 19:06

AnthonyWJones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!