tags:

views:

463

answers:

1

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?

+1  A: 

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.

AnthonyWJones
When I tried it, Media Player fails with a "could not connect to server" error
mcjabberz
If you write the data that way, be sure to Clear() the Response first, and then set its BufferOutput property to False. That will cause ASP.NET to output a "Transfer-Encoding: chunked" HTTP header, so the data is truely streaming.
Remy Lebeau - TeamB
I cleared the Response and set BufferOutput to false. Windows Media Player stays stuck with an "Opening media..." status
mcjabberz
@Remy: I tend not to use .Clear in a handler, its belts and braces but if it really did anything then something is wrong already.
AnthonyWJones
@mcjabberz: Potentially you need to include a Response.Close() at the end of streaming. Without any Content-Length (and with buffering off) it may be that the client will indefinitely wait for content.
AnthonyWJones
Here is <a href="http://pastebin.com/m16846c15">my exact code</a>. I'm still running into the same issue with Media Player getting stuck PS: Thank you all for the help!!!
mcjabberz
Sorry here is the correct link. http://pastebin.com/m16846c15
mcjabberz
You don't appear to be decrypting in that chunk of code? How large is the AVI? Try using a small avi restire buffering to true and remove the close. This checks the basic functionality.
AnthonyWJones