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.
Monday, May 5, 2008
Subscribe to:
Post Comments (Atom)

11 comments:
Hi,
Nice code... but how actually to seek video position (what is best benefit of pseudo-streaming) ?
The benefit of pseudo-streaming is to control the bandwidth usage... Say you have a video that is 5 minutes long and a user with high bandwidth. If this user stops the video after, say 30 sec, he has probably downloaded the whole video-file already. If you use pseudo-streaming, the user will only download the bits that is going to be viewed...
Hi,
I am using the same code for displaying video on my VLC player. mpg , dat, wmv works absolutely fine but i am not able to run mp4 videos.
h264 files have to be encoded with the header information in the front of the file... I guess that's the problem here. Do a search for freeware encoding tools that can do this for you.
Hi thanks for posting this. I am developing an ASP.net website and playing back MP4 using flash. I would love to support pseudo streaming and seek anywhere while buffering. Looking at your code it looks like it downloads the entire thing chunk by chunk so this would not allow for seeking right? Is this an IHTTPHandler? if so would you be able to send me the source code for this handler.
It also look like you are getting the filepath from the fil param of the request. Is this the correct way to do it?
TIA
Tuviah
Hi,
Yes, you are correct. To jump in the file you have to do some more work. The flash-player has to make a call to your http-handler (that serves the h264-file) with a parameter telling where in the file you would like to jump. Your http-handler then have to get this parameter, find the correct point in the file, and then start playing from that location. It's not strait forward and I don't have finished code for it... But you can find complete solutions for this out there... Take a look at JWplayer and LightTPD. They do exactly wath you describe in your post. LightTPD is by the way used by YouTube, so it works well... ;)
cannot find LightTPD when searching with google. Is this the correct spelling?
OK I see its another web server. Unfortunately we are married to ASP.NET AJAX so we have to use IIS.
Hi again...
Have you had a look at IIS7 and MediaPack? I belive that they are going to have the same functionality (and more) when they release the MediaPack later this year. I think it's in beta now. You have to use a Silverlight player to get advantage of all the cool features...
Hi Saashem,
It would be nice we can get H.264 pseudo streaming working @ IIS.
hi
i am new to video streaming
my requirement is playing flv file
in web player like youtube
can u please guide me
thanks
srinivas rao.k
Post a Comment