18 Mart 2008 Salı

FLV Flash video streaming with ASP.NET 2.0, IIS and HTTP handler

 

Yazının Gerçek Adresi : http://blogs.ugidotnet.org/kfra/archive/2006/10/04/50003.aspx

Using this HTTP handler you can easily FLV streaming downloads just like video.google.com does. All you need is to install on your IIS 5.0/6.0 the following HTTP handler and to get this to work correctly, you will need to make sure that IIS handles request for .flv files. In your site's properties, click the "Home directory tab" and click the "Configuration" button. You'll get a form like this:

iis1

Add the entry for .flv, click edit, and copy the path in the executable field. This is the aspnet_isapi.dll for the current version of the .NET Framework of your virtual site. Cancel out of that dialog and click "add." Paste the path into the executable, use the extension .flv and set your verbs limited to "GET, POST, HEAD, DEBUG" like this:

iis2

Now any request for a .flv file on the site will be handled by ASP.NET. Since the server-wide machine.config file doesn't specify what class should handle the request, a default handler is used unless we add the following lines to the web.config file:

Web.config

   1: <httpHandlers>        


   2:        verb="*" path="*.flv" type="FLVStreaming" />


   3: </httpHandlers> 






FLVStreaming.cs






   1: using System.Web;


   2:  


   3: public class FLVStreaming : IHttpHandler


   4: {


   5:  


   6:     // FLV header


   7:     private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");


   8:  


   9:     public FLVStreaming()


  10:     {


  11:     }


  12:  


  13:     public void ProcessRequest(HttpContext context)


  14:     {


  15:         try


  16:         {


  17:             int pos;


  18:             int length;


  19:  


  20:             // Check start parameter if present


  21:             string filename = Path.GetFileName(context.Request.FilePath);


  22:  


  23:             using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))


  24:             {


  25:                 string qs = context.Request.Params["start"];


  26:  


  27:                 if (string.IsNullOrEmpty(qs))


  28:                 {


  29:                     pos = 0;


  30:                     length = Convert.ToInt32(fs.Length);


  31:                 }


  32:                 else


  33:                 {


  34:                     pos = Convert.ToInt32(qs);


  35:                     length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;


  36:                 }


  37:  


  38:                 // Add HTTP header stuff: cache, content type and length        


  39:                 context.Response.Cache.SetCacheability(HttpCacheability.Public);


  40:                 context.Response.Cache.SetLastModified(DateTime.Now);


  41:  


  42:                 context.Response.AppendHeader("Content-Type", "video/x-flv");


  43:                 context.Response.AppendHeader("Content-Length", length.ToString());


  44:  


  45:                 // Append FLV header when sending partial file


  46:                 if (pos > 0)


  47:                 {


  48:                     context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);


  49:                     fs.Position = pos;


  50:                 }


  51:  


  52:                 // Read buffer and write stream to the response stream


  53:                 const int buffersize = 16384;


  54:                 byte[] buffer = new byte[buffersize];


  55:                 


  56:                 int count = fs.Read(buffer, 0, buffersize);


  57:                 while (count > 0)


  58:                 {


  59:                     if (context.Response.IsClientConnected)


  60:                     {


  61:                         context.Response.OutputStream.Write(buffer, 0, count);


  62:                         count = fs.Read(buffer, 0, buffersize);


  63:                     }


  64:                     else


  65:                     {


  66:                         count = -1;


  67:                     }


  68:                 }


  69:             }


  70:         }


  71:         catch (Exception ex)


  72:         {


  73:             System.Diagnostics.Debug.WriteLine(ex.ToString());


  74:         }


  75:     }


  76:  


  77:     public bool IsReusable


  78:     {


  79:         get { return true; }


  80:     }


  81:  


  82:     private static byte[] HexToByte(string hexString)


  83:     {


  84:         byte[] returnBytes = new byte[hexString.Length / 2];


  85:         for (int i = 0; i < returnBytes.Length; i++)


  86:             returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);


  87:         return returnBytes;


  88:     }


  89:  


  90: }




All you need now to stream your favorite FLV movies is a custom-made player which is fetching the contents passing to the request the ?start= parameter in order to seek the current position inside the video file.



Fabian Topfstedt has one available onto his site (get the player and place it in your site document root)



To use Fabian player you have to embed the following HTML code inside your page:





   1: <object 


   2:   classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 


   3:   codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0


   4:   width="336" 


   5:   height="297">    


   6:   <param name="movie" value="scrubber.swf?file=http://&bufferTime=3&autoStart=false" />    


   7:   <param name="quality" value="high" />    


   8:   <embed src="scrubber.swf?file=http://&bufferTime=3&autoStart=false" 


   9:     quality="high" 


  10:     pluginspage="http://www.macromedia.com/go/getflashplayer" 


  11:     type="application/x-shockwave-flash" 


  12:     width="336" 


  13:     height="297"></embed>  


  14: </object>




The video file was converted into a .flv using of ffmpeg and indexed with flvtool2 in order to add the correct metadata inside the FLV file.





   1: ffmpege.exe -i test.avi test.flv


   2: flvtool2.exe -U test.flv




You can download here solution and project files or if you have the chance check online demo here.



An ASP.NET v1.1 version is available here.



Feel free to contact me for any information.