Friday, May 30, 2008

Iterate through Enumeration C#

Need to iterate through an Enumeration in C#?

First create the array using following code:

Array lstProvider = System.Enum.GetValues( typeof(CommonShared.EnumProvider));

Then you can directly call the following foreach loop:

foreach (CommonShared.EnumProvider enProvider in lstProvider)
{
// do something...
}

Monday, May 5, 2008

Pseudo streaming of H.264

Recently I have worked with progressive download of H.264 content. Progressive download has it's drawbacks, so I desided to look into pseudo streaming og H.264. Pseudo streaming is progressive download, but has some of the advantages of real streaming, meaning that the user can skip in the file before the full video is downloaded.

A search on Google gave me a few hits, where the most interesting one was: http://h264.code-shop.com/trac. This article states that you have to use a special web server and a plugin on this server. Since we already run IIS, I wanted to find out if IIS could also handle this kind of requests.

I have done a similar thing before with Flash-video, and the code back then was pretty straight forward:

context.Response.ContentType = "video/H264";
string filename = context.Request.QueryString["fil"];

byte[] buffer = new byte[4096];
using (FileStream stream = File.OpenRead(filename)) {
  using (MemoryStream memoryStream = new MemoryStream()) {
    int count = 0;
    do {
      count = stream.Read(buffer, 0, buffer.Length);
      memoryStream.Write(buffer, 0, count);
    } while (count != 0);

    memoryStream.WriteTo(context.Response.OutputStream);

  }
}

My first attemt using this code was not a success! To find out why, I tried to download the mp4-file from the above URL. I then modified my Flash player to play this mp4-file. Cliked "Play" and ... Yes, it played!!

Why? The difference between my mp4 test file and the one I downloaded from http://h264.code-shop.com/trac was the encoding - not the code or the web server. Take a look at: http://h264.code-shop.com/trac/wiki/Encoding to find out how you should encode the mp4 video, and you will be able to pseudo stream H.264 with standard IIS and a few lines of C# code.