Silverlight video doesn't work when file is streamed from handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Um9nZXIgTWFydGlu?=

    Silverlight video doesn't work when file is streamed from handler

    Note: My apologies for repeating this post from last week, but my nospam
    alias and profile account were incorrect. I think I have fixed this, so
    hopefully this post will trigger MS into a response per their MSDN policy.
    --------------------

    I have a web site under .NET 2.0 that renders videos using the Silverlight
    media player.

    The web page looks like this:
    <%@ Page Language="C#" AutoEventWireup ="true" CodeBehind="vid eo2.aspx.cs"
    Inherits="WebAp plication2.vide o2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:ScriptMana ger ID="ScriptManag er1" runat="server">
    <Scripts>
    <asp:ScriptRefe rence Path="~/script/SilverlightCont rol.js" />
    <asp:ScriptRefe rence Path="~/script/SilverlightMedi a.js" />
    </Scripts>
    </asp:ScriptManag er>
    <div id="mp1p" />

    <script type="text/javascript">
    //<![CDATA[
    Sys.UI.Silverli ght.Control.cre ateObject('mp1p ', '\u003cobject
    type="applicati on/x-silverlight" id="MediaPlayer 1"
    style="height:2 40px;width:320p x;">\r\n\t\u003 ca
    href="http://go2.microsoft.c om/fwlink/?LinkID=114576& v=1.0">\u003cim g
    src="http://go2.microsoft.c om/fwlink/?LinkID=108181" alt="Get Microsoft
    Silverlight" style="border-width:0;" />\u003c/a>\r\n\u003c/object>');
    Sys.Application .initialize();
    Sys.Application .add_init(funct ion()
    {
    // This works:
    $create(Sys.UI. Silverlight.Med iaPlayer, { "mediaSourc e":
    "video/3StrikesChipmun k_56.wmv", "scaleMode" : 1, "source":
    "skins/mediaplayer/Professional.xa ml" }, null, null, $get("mp1p"));

    // This does not work (only difference is that it gets the video from the
    handler)
    //$create(Sys.UI. Silverlight.Med iaPlayer, { "mediaSourc e":
    "handler/getmediaobject. ashx", "scaleMode" : 1, "source":
    "skins/mediaplayer/Professional.xa ml" }, null, null, $get("mp1p"));
    });
    //]]>
    </script>
    </form>
    </body>
    </html>

    ----------------------------------------
    This page works great when I directly reference the video file. But when I
    try to use an HTTP handler instead, all I get is an empty Silverlight control
    with the Play button disabled and no video. You can repro it by commenting
    out the $create line above that references the WMV file and uncomment the
    line below it that uses the getmediaobject. ashx handler.

    The getmediaobject. ashx handler is straight forward:

    using System.IO;
    using System.Web;
    using System.Web.Sess ionState;

    namespace WebApplication2 .handler
    {
    [System.Web.Serv ices.WebService (Namespace = "http://tempuri.org/")]
    [System.Web.Serv ices.WebService Binding(Conform sTo =
    System.Web.Serv ices.WsiProfile s.BasicProfile1 _1)]
    public class getmediaobject : IHttpHandler
    {
    #region IHttpHandler Members

    public bool IsReusable
    {
    get { return true; }
    }

    public void ProcessRequest( HttpContext context)
    {
    ProcessMediaObj ect(context,
    context.Server. MapPath("~/video/3StrikesChipmun k_56.wmv"));
    }

    #endregion

    private void ProcessMediaObj ect(HttpContext context, string filePath)
    {
    FileStream fileStream = null;
    try
    {
    context.Respons e.Clear();
    context.Respons e.ContentType = "video/x-ms-wmv";
    context.Respons e.Buffer = false;

    HttpCachePolicy cachePolicy = context.Respons e.Cache;
    cachePolicy.Set Expires(System. DateTime.Now.Ad dSeconds(259200 0)); // 30
    days
    cachePolicy.Set Cacheability(Ht tpCacheability. Public);
    cachePolicy.Set ValidUntilExpir es(true);

    const int bufferSize = 32768;
    byte[] buffer = new byte[bufferSize];
    long byteCount;
    fileStream = File.OpenRead(f ilePath);
    while ((byteCount = fileStream.Read (buffer, 0, buffer.Length)) 0)
    {
    if (context.Respon se.IsClientConn ected)
    {
    context.Respons e.OutputStream. Write(buffer, 0, buffer.Length);
    context.Respons e.Flush();
    }
    else
    {
    return;
    }
    }
    }
    finally
    {
    if (fileStream != null)
    fileStream.Clos e();

    context.Respons e.End();
    }
    }
    }
    }

    The really odd part is that the handler *does* work on my local network - it
    only
    fails when I publish the code to a web server and then access the page
    across the internet. It happens on both IIS 6 and IIS 7, and in IE and
    Firefox.

    I published the above code at
    http://www.galleryserverpro.com/dev/webapp2/video2.aspx. The page is
    currently configured to use the handler so that you can see it failing for
    yourself.

    Additional info:
    1. I am using the final release of 2.0 Silverlight, including the two script
    files SilverlightCont rol.js and SilverlightMedi a.js.
    2. I have confirmed that no exception is occuring in the handler.
    3. Using Firebug I can confirm that the handler is being called and that it
    is returning data to the browser.

    All evidence points to Silverlight not being able to handle the data
    streamed from Silverlight. Perhaps there is a change I can make to the
    handler to get it to work, or maybe there is an issue within Silverlight.
    Please advise.

    Thanks!
    Roger Martin

  • Allen Chen [MSFT]

    #2
    RE: Silverlight video doesn't work when file is streamed from handler

    Hi Roger,

    I tested your page and it works fine on my side:


    Have you solved this issue? After the site has been published can you watch
    the video when using Windows Media Player to access the ashx? If it cannot
    work for Windows Media Player either I think probably it's not an
    Silverlight issue. We can then move on to diagnose the IIS and the
    HttpHandler.

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subs...#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at

    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    | Thread-Topic: Silverlight video doesn't work when file is streamed from
    handler
    | thread-index: AclDPnB+5p6h0Jh HTZ6iqWci7rNrCg ==
    | From: =?Utf-8?B?Um9nZXIgTWF ydGlu?= <rdmartin@commu nity.nospam>
    | Subject: Silverlight video doesn't work when file is streamed from handler
    | Date: Mon, 10 Nov 2008 06:13:01 -0800
    | Lines: 160
    | Message-ID: <46509CCB-78F1-440F-9B68-1124DBBE0D25@mi crosoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3168
    | Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
    | Path: TK2MSFTNGHUB02. phx.gbl
    | Xref: TK2MSFTNGHUB02. phx.gbl
    microsoft.publi c.dotnet.framew ork.aspnet:7956 0
    | NNTP-Posting-Host: tk2msftibfm01.p hx.gbl 10.40.244.149
    | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
    |
    | Note: My apologies for repeating this post from last week, but my nospam
    | alias and profile account were incorrect. I think I have fixed this, so
    | hopefully this post will trigger MS into a response per their MSDN policy.
    | --------------------
    |
    | I have a web site under .NET 2.0 that renders videos using the
    Silverlight
    | media player.
    |
    | The web page looks like this:
    | <%@ Page Language="C#" AutoEventWireup ="true" CodeBehind="vid eo2.aspx.cs"
    | Inherits="WebAp plication2.vide o2" %>
    |
    | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    | <html xmlns="http://www.w3.org/1999/xhtml">
    | <head runat="server">
    | <title></title>
    | </head>
    | <body>
    | <form id="form1" runat="server">
    | <asp:ScriptMana ger ID="ScriptManag er1" runat="server">
    | <Scripts>
    | <asp:ScriptRefe rence Path="~/script/SilverlightCont rol.js" />
    | <asp:ScriptRefe rence Path="~/script/SilverlightMedi a.js" />
    | </Scripts>
    | </asp:ScriptManag er>
    | <div id="mp1p" />
    |
    | <script type="text/javascript">
    | //<![CDATA[
    | Sys.UI.Silverli ght.Control.cre ateObject('mp1p ', '\u003cobject
    | type="applicati on/x-silverlight" id="MediaPlayer 1"
    | style="height:2 40px;width:320p x;">\r\n\t\u003 ca
    | href="http://go2.microsoft.c om/fwlink/?LinkID=114576& v=1.0">\u003cim g
    | src="http://go2.microsoft.c om/fwlink/?LinkID=108181" alt="Get Microsoft
    | Silverlight" style="border-width:0;" />\u003c/a>\r\n\u003c/object>');
    | Sys.Application .initialize();
    | Sys.Application .add_init(funct ion()
    | {
    | // This works:
    | $create(Sys.UI. Silverlight.Med iaPlayer, { "mediaSourc e":
    | "video/3StrikesChipmun k_56.wmv", "scaleMode" : 1, "source":
    | "skins/mediaplayer/Professional.xa ml" }, null, null, $get("mp1p"));
    |
    | // This does not work (only difference is that it gets the video from
    the
    | handler)
    | //$create(Sys.UI. Silverlight.Med iaPlayer, { "mediaSourc e":
    | "handler/getmediaobject. ashx", "scaleMode" : 1, "source":
    | "skins/mediaplayer/Professional.xa ml" }, null, null, $get("mp1p"));
    | });
    | //]]>
    | </script>
    | </form>
    | </body>
    | </html>
    |
    | ----------------------------------------
    | This page works great when I directly reference the video file. But when
    I
    | try to use an HTTP handler instead, all I get is an empty Silverlight
    control
    | with the Play button disabled and no video. You can repro it by
    commenting
    | out the $create line above that references the WMV file and uncomment the
    | line below it that uses the getmediaobject. ashx handler.
    |
    | The getmediaobject. ashx handler is straight forward:
    |
    | using System.IO;
    | using System.Web;
    | using System.Web.Sess ionState;
    |
    | namespace WebApplication2 .handler
    | {
    | [System.Web.Serv ices.WebService (Namespace = "http://tempuri.org/")]
    | [System.Web.Serv ices.WebService Binding(Conform sTo =
    | System.Web.Serv ices.WsiProfile s.BasicProfile1 _1)]
    | public class getmediaobject : IHttpHandler
    | {
    | #region IHttpHandler Members
    |
    | public bool IsReusable
    | {
    | get { return true; }
    | }
    |
    | public void ProcessRequest( HttpContext context)
    | {
    | ProcessMediaObj ect(context,
    | context.Server. MapPath("~/video/3StrikesChipmun k_56.wmv"));
    | }
    |
    | #endregion
    |
    | private void ProcessMediaObj ect(HttpContext context, string filePath)
    | {
    | FileStream fileStream = null;
    | try
    | {
    | context.Respons e.Clear();
    | context.Respons e.ContentType = "video/x-ms-wmv";
    | context.Respons e.Buffer = false;
    |
    | HttpCachePolicy cachePolicy = context.Respons e.Cache;
    | cachePolicy.Set Expires(System. DateTime.Now.Ad dSeconds(259200 0)); //
    30
    | days
    | cachePolicy.Set Cacheability(Ht tpCacheability. Public);
    | cachePolicy.Set ValidUntilExpir es(true);
    |
    | const int bufferSize = 32768;
    | byte[] buffer = new byte[bufferSize];
    | long byteCount;
    | fileStream = File.OpenRead(f ilePath);
    | while ((byteCount = fileStream.Read (buffer, 0, buffer.Length)) 0)
    | {
    | if (context.Respon se.IsClientConn ected)
    | {
    | context.Respons e.OutputStream. Write(buffer, 0, buffer.Length);
    | context.Respons e.Flush();
    | }
    | else
    | {
    | return;
    | }
    | }
    | }
    | finally
    | {
    | if (fileStream != null)
    | fileStream.Clos e();
    |
    | context.Respons e.End();
    | }
    | }
    | }
    | }
    |
    | The really odd part is that the handler *does* work on my local network -
    it
    | only
    | fails when I publish the code to a web server and then access the page
    | across the internet. It happens on both IIS 6 and IIS 7, and in IE and
    | Firefox.
    |
    | I published the above code at
    | http://www.galleryserverpro.com/dev/webapp2/video2.aspx. The page is
    | currently configured to use the handler so that you can see it failing
    for
    | yourself.
    |
    | Additional info:
    | 1. I am using the final release of 2.0 Silverlight, including the two
    script
    | files SilverlightCont rol.js and SilverlightMedi a.js.
    | 2. I have confirmed that no exception is occuring in the handler.
    | 3. Using Firebug I can confirm that the handler is being called and that
    it
    | is returning data to the browser.
    |
    | All evidence points to Silverlight not being able to handle the data
    | streamed from Silverlight. Perhaps there is a change I can make to the
    | handler to get it to work, or maybe there is an issue within Silverlight.
    | Please advise.
    |
    | Thanks!
    | Roger Martin
    |
    |

    Comment

    • =?Utf-8?B?Um9nZXIgTWFydGlu?=

      #3
      RE: Silverlight video doesn't work when file is streamed from hand

      I got it! After comparing the Response headers of the handler versus the
      hard-coded link, I noticed the handler version did not have a Content-Length.
      So I added this line to the handler:

      context.Respons e.AddHeader("Co ntent-Length", fileStream.Leng th.ToString());

      Voila! The video started working in every OS/browser after that.

      There is a new problem where I can't jump around to different sections of
      the video when using the handler, but I'll create a new post for that.

      Thanks! Roger

      Comment

      • Allen Chen [MSFT]

        #4
        RE: Silverlight video doesn't work when file is streamed from hand

        Hi Roger,

        Good job! I have to say you're an experienced developer. Before you sending
        this post I thought we need do more complex steps to diagnose this issue.
        Anyway I'm glad to know you've solved this problem.

        Thank you for using our Newsgroup Support Service!

        Regards,
        Allen Chen
        Microsoft Online Community Support


        =============== =============== =============== ====
        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.
        =============== =============== =============== ====

        --------------------
        | Thread-Topic: Silverlight video doesn't work when file is streamed from
        hand
        | thread-index: AclFO+spmJsgQMm cTnKfyTgMonN+Ow ==
        | X-WBNR-Posting-Host: 207.46.192.207
        | From: =?Utf-8?B?Um9nZXIgTWF ydGlu?= <rdmartin@commu nity.nospam>
        | References: <46509CCB-78F1-440F-9B68-1124DBBE0D25@mi crosoft.com>
        <oQqYVw6QJHA.16 72@TK2MSFTNGHUB 02.phx.gbl>
        <E9A38D03-83A8-41FC-8950-5EE60D2A18A5@mi crosoft.com>
        <L0ZAkuIRJHA.16 72@TK2MSFTNGHUB 02.phx.gbl>
        | Subject: RE: Silverlight video doesn't work when file is streamed from
        hand
        | Date: Wed, 12 Nov 2008 19:00:00 -0800
        | Lines: 12
        | Message-ID: <93CF5BB2-6315-4F27-8DD8-50D897125DEA@mi crosoft.com>
        | MIME-Version: 1.0
        | Content-Type: text/plain;
        | charset="Utf-8"
        | Content-Transfer-Encoding: 7bit
        | X-Newsreader: Microsoft CDO for Windows 2000
        | Content-Class: urn:content-classes:message
        | Importance: normal
        | Priority: normal
        | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3168
        | Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
        | Path: TK2MSFTNGHUB02. phx.gbl
        | Xref: TK2MSFTNGHUB02. phx.gbl
        microsoft.publi c.dotnet.framew ork.aspnet:7977 3
        | NNTP-Posting-Host: tk2msftibfm01.p hx.gbl 10.40.244.149
        | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
        |
        | I got it! After comparing the Response headers of the handler versus the
        | hard-coded link, I noticed the handler version did not have a
        Content-Length.
        | So I added this line to the handler:
        |
        | context.Respons e.AddHeader("Co ntent-Length",
        fileStream.Leng th.ToString());
        |
        | Voila! The video started working in every OS/browser after that.
        |
        | There is a new problem where I can't jump around to different sections of
        | the video when using the handler, but I'll create a new post for that.
        |
        | Thanks! Roger
        |

        Comment

        Working...