You will recieve your password to this address. Address is not made public.

Your preferred username that is used when logging in.

How To: Pseudostreaming with IIS and ASP.NET Created Feb 4, 2009

This thread is solved

Views: 16226     Replies: 31     Last reply Jul 26, 2010  
You must login first before you can use this feature

killebrewj

Posts: 80

Registered:
May 26, 2008

How To: Pseudostreaming with IIS and ASP.NET

Posted: Feb 4, 2009

For almost a year, Ive been using flowplayer's pseudostreaming capabilities with IIS6 and ASP.NET 2.0. I thought I'd share this because it does not require PHP so you can use this on a very basic IIS setup.
  • Open up IIS Manager. In the site properties, click the "Home directory" tab then click the "Configuration" button.
  • Add an entry for .flv. The Executable path is whatever the aspnet_isapi.dll path is for your version of .NET Framework. Check some of the other file types like .config to be sure. Set Verbs to "Limit to:" with the value GET,HEAD,POST,DEBUG. Click OK get back to the site properties.
  • If you have not done so already, you'll need to make sure you have a mime type for flv files, so go to the HTTP Headers tab and click the MIME Types... button. If .flv does not appear, click new. Extention is .flv and MIME type is flv-application/octet-stream. You may have something else here and I'm not sure if it matters if you have this exact value but this works for me. Click OK and get back to the site properties.
  • One thing to consider is the size of the flv files and how long they will take for your visitors to download. In my case, I have some flv files that are 150MB, so it can take some slow DSL users maybe 30 min or more to download as they watch. Because of this, the script would time out before the download is done, cutting the video off. By default the script will stop in 110 seconds, so if that is not enough time for some slower connections, go to the ASP.NET tab and click Edit Configuration. Now go to the application tab and change the Request execution timeout to a higher value. I chose 1800 seconds. We are done with the site properties for now.
  • In order to make sure any request for a .flv file is handled by ASP.NET, edit the web.config file (should be in the root of your website) and add the following lines just before </system.web> near the end of the file:
  • 
        <httpHandlers>        
               verb="*" path="*.flv" type="FLVStreaming" />
        </httpHandlers> 
    
  • In the root of the website, create an App_Code folder if you dont already have one. Place a file called FLVStreaming.cs there with the following inside: ( This is for ASP 2 ONLY, not ASP 1.1 )
  • 
    using System;
    using System.IO;
    using System.Web;
    
    public class FLVStreaming : IHttpHandler
    {
        private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");
    
        public FLVStreaming()
        {
        }
    
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                int pos;
                int length;
    
                // Check start parameter if present
                string filename = Path.GetFileName(context.Request.FilePath);
    
                using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    string qs = context.Request.Params["start"];
    
                    if (string.IsNullOrEmpty(qs))
                    {
                        pos = 0;
                        length = Convert.ToInt32(fs.Length);
                    }
                    else
                    {
                        pos = Convert.ToInt32(qs);
                        length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
                    }
    
                    // Add HTTP header stuff: cache, content type and length        
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);
                    context.Response.Cache.SetLastModified(DateTime.Now);
    
                    context.Response.AppendHeader("Content-Type", "video/x-flv");
                    context.Response.AppendHeader("Content-Length", length.ToString());
    
                    // Append FLV header when sending partial file
                    if (pos > 0)
                    {
                        context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
                        fs.Position = pos;
                    }
    
                    // Read buffer and write stream to the response stream
                    const int buffersize = 16384;
                    byte[] buffer = new byte[buffersize];
                    
                    int count = fs.Read(buffer, 0, buffersize);
                    while (count > 0)
                    {
                        if (context.Response.IsClientConnected)
                        {
                            context.Response.OutputStream.Write(buffer, 0, count);
    						
    						//Starts sending to client right away
    						context.Response.Flush();
    			
                            count = fs.Read(buffer, 0, buffersize);
                        }
                        else
                        {
                            count = -1;
                        }a
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
    
        public bool IsReusable
        {
            get { return true; }
        }
    
        private static byte[] HexToByte(string hexString)
        {
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    
    }
    
  • Now go back to IIS Manager and find that App_Code folder under your site. Bring up the properties of the App_Code folder, and on the Directory tab, Click the Create button to create an application. The application name field should then show App_Code. I'm also not 100% sure you need this step in every case, but I did.
  • Now the server is ready to go. For flowplayer, use the pseudostreaming plugin as demonstrated on the plugin page:http://www.flowplayer.org/plugins/streaming/pseudostreaming.html
As for flash video, I've been using the Adobe flash video encoder to create the flv files since I created the videos for my site in Adobe Premiere. After creating the flv, I used flvmdi for metadata injections. http://www.buraks.com/flvmdi/

This is mostly off the top of my head and I set this up a year ago so I'm sorry if I missed something.

Anssi
Flowplayer Flash & video streaming developer

Posts: 1076

Registered:
Jul 24, 2007

» How To: Pseudostreaming with IIS and ASP.NET

Posted: Feb 4, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Thanks for sharing this!

Lappen

Posts: 1

Registered:
Mar 31, 2009

» How To: Pseudostreaming with IIS and ASP.NET

Posted: Mar 31, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Hi, I have done all the above, but I get error 200 stream not found.
This is when I try to play a .flv from my own server, configured as above. I have tried with the relative filename and the http:// sitename.
When I try to access the flv-file directly from IE i get "HTTP 403.1 Forbidden: Execute Access Forbidden"

When I use the url:'http://examples-s3.simplecdn.net/videos/Extremists.flv' as in the demo it works fine.

When I put the flv-file on another server (at work) it streams, but not pseudostreaming (not configured).

It feels like an IIS problem, but do anyone know how to correct the problem?

nrossello

Posts: 1

Registered:
Sep 8, 2009

How To: Pseudostreaming with IIS and ASP.NET

Posted: Sep 9, 2009

Reply to: » How To: Pseudostreaming with IIS and ASP.NET, from Lappen
I am having the same problem.... 200 Stream not found...
btw
<httpHandlers>
<add verb="*" path="*.flv" type="FLVStreaming" />
</httpHandlers>

i can't get it to work fine

oncallken
Ken Ballard Oncall Interactive http://www.oncallinteractive.com

Posts: 1

Registered:
Apr 1, 2009

Purpose of _flvheader?

Posted: Apr 1, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Hey all...I'm trying to refactor this process and code to pseudostream mp3's. The thing that I'm getting hung up on is I'm not sure what the _flvheader is doing. I see that it's getting appended onto the beginning of the stream, but I'm afraid this is way outside of my knowledge. Can anyone enlighten me on this? I'm assuming this value will have to be changed for mp3's, but I'm not sure where to begin on that.


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

Thanks in advance for your help!

chanduu4u

Posts: 3

Registered:
Apr 3, 2009

how to use the control

Posted: Apr 3, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Hello, every this fine, thanks for sharing.
Can you please provide the code for aspx and aspx.cs?

thanks in advance

Chanduu4u

killebrewj

Posts: 80

Registered:
May 26, 2008

» how to use the control

Posted: Apr 15, 2009

Reply to: how to use the control, from chanduu4u
everything you need is provided. This will work with any pseudostreaming enabled flowplayer setup and flv videos.

chanduu4u

Posts: 3

Registered:
Apr 3, 2009

code for asp.net 2.0

Posted: Apr 24, 2009

Reply to: » how to use the control, from killebrewj
oops! where is the code?

rdevalco

Posts: 1

Registered:
Feb 15, 2010

» » how to use the control

Posted: Feb 15, 2010

Reply to: » how to use the control, from killebrewj
i think he wants an actual example, as would I.

i'm in IIS

followed the code but can never get my h.264 videos to pseudo stream...

so I'm asking you please put a sample aspx, aspx.cs here that we can see to ensure we're doing things right...

tuanb

Posts: 1

Registered:
Apr 17, 2009

» How To: Pseudostreaming with IIS and ASP.NET

Posted: Apr 17, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Hi,
I got an error in FLVStreaming.cs file at
line 71 }a

What is the correct syntax for this line?
Thanks,

arjen

Posts: 21

Registered:
Feb 4, 2009

» » How To: Pseudostreaming with IIS and ASP.NET

Posted: Apr 17, 2009

Reply to: » How To: Pseudostreaming with IIS and ASP.NET, from tuanb
Hi Tuanb,

Just remove the 'a' from that line and leave the '}'.

Regards,

crimsondeath

Posts: 1

Registered:
May 11, 2009

One more thing

Posted: May 11, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
First of all, thanks for posting this. It works great.
However, I did have to add one extra line of code.
At the end of the while(){} statement that writes the bytes I had to add a:
context.Response.Close();

Or else after a few times of refreshing the page IIS would no longer respond, or take a very long time to do so.
So if anyone has this same problem I believe this might help.

Thanks,

nhardel

Posts: 1

Registered:
Jul 10, 2009

Setup of web.config file

Posted: Jul 10, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
This doesnt look right


<httpHandlers>
    verb="*" path="*.flv" type="FLVStreaming" />
</httpHandlers>

Shouldn't it look more like this?


<httpHandlers>
  <add verb="*" path="*.flv" type="FLVStreaming" />
</httpHandlers>

and my web.config file has multiple </system.web>
so which one should I be looking for?

omar

Posts: 9

Registered:
Jan 26, 2009

mp4?

Posted: Oct 25, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Can this be setup for mp4?

napalme

Posts: 8

Registered:
Dec 8, 2009

x64 version of Windows 2003

Posted: Dec 20, 2009

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
In my case with 64-bit Windows 2003 i can't make it work:
server respond with
"%1 is not a Win32 application."

of course I turned on 32-bit support with
"script.exe adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 ?1?"

What can i do with that?

reg.required.2008

Posts: 2

Registered:
Feb 26, 2010

Debugging Website, not hitting FLVStreaming httphandler

Posted: Feb 28, 2010

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Thanks for sharing really valuable code. I followed same as explained. While attaching debugger, it is not hitting ProcessRequest() method. So does it mean, it is not getting executed? or am i missing something ?

Any input by anyone is most welcome.. :)

smcbay

Posts: 20

Registered:
Aug 2, 2009

Here are the FIXED VERSIONS of this post...

Posted: May 7, 2010

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj

<httpHandlers>        
          <add verb="*" path="*.flv" type="FLVStreaming" ></add>
    </httpHandlers>

-----------------------------


using System;
using System.IO;
using System.Web;

public class FLVStreaming : IHttpHandler
{
    private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");

    public FLVStreaming()
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            int pos;
            int length;

            // Check start parameter if present
            string filename = Path.GetFileName(context.Request.FilePath);

            using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string qs = context.Request.Params["start"];

                if (string.IsNullOrEmpty(qs))
                {
                    pos = 0;
                    length = Convert.ToInt32(fs.Length);
                }
                else
                {
                    pos = Convert.ToInt32(qs);
                    length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;
                }

                // Add HTTP header stuff: cache, content type and length        
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetLastModified(DateTime.Now);

                context.Response.AppendHeader("Content-Type", "video/x-flv");
                context.Response.AppendHeader("Content-Length", length.ToString());

                // Append FLV header when sending partial file
                if (pos > 0)
                {
                    context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);
                    fs.Position = pos;
                }

                // Read buffer and write stream to the response stream
                const int buffersize = 16384;
                byte[] buffer = new byte[buffersize];
                
                int count = fs.Read(buffer, 0, buffersize);
                while (count > 0)
                {
                    if (context.Response.IsClientConnected)
                    {
                        context.Response.OutputStream.Write(buffer, 0, count);
						
						//Starts sending to client right away
						context.Response.Flush();
			
                        count = fs.Read(buffer, 0, buffersize);
                    }
                    else
                    {
                        count = -1;
                    }
                } context.Response.Close();
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }

    private static byte[] HexToByte(string hexString)
    {
        byte[] returnBytes = new byte[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        return returnBytes;
    }

}

FISUser

Posts: 1

Registered:
Apr 19, 2010

ASP.Net forKillebrewj

Posted: May 13, 2010

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Hi KillerBrewj

I am currently attempting to use Flowplayer on a HTTPS server using asp.net (VB.net) Can I lean on you with questions as I do not have access to the IIS server that this will run under and must rely on another person to do the trick. I think I understand your strategy and I am New to Flowplayer. I did get it running under https, however the video chugs.. so I am going to attempt this pseudostreaming to see if it improves. I see this posting is from last year. however, you seem to be a .Net programmer. thanks

killebrewj

Posts: 80

Registered:
May 26, 2008

» ASP.Net forKillebrewj

Posted: May 24, 2010

Reply to: ASP.Net forKillebrewj, from FISUser
I dont normally check on these forums but you can ask here if you want and someone will probably reply.

You should rely on smcbay's version because something went wrong when I created the original post since something is wrong with this site and the way brackets are handled. The script wont display correctly when I try to post it, nor can I go back and edit my original post (shame on your flowplayer site admins).

Also, HTTPS may not be the best way to deliver the video. You should probably sent it over http even if the site is otherwise secured. Otherwise all the stream data would need to be encrypted by the server (i think) and this could slow things down if a lot of streams are accessed from one server at the same time.

blckhm

Posts: 4

Registered:
Dec 15, 2008

» How To: Pseudostreaming with IIS and ASP.NET

Posted: Jul 6, 2010

Reply to: How To: Pseudostreaming with IIS and ASP.NET, from killebrewj
Hi,

The application did not run on server 2008 R2 (x64) with IIS7.

When I seek to video, everytime video starts from begining.

I also tried server 2003 R2 st.edt. (x86) and runs perfect. But in IIS7 x64 did not run...

What should I do to run this application ?