Multimedia (RSS)

All things related to Multimedia

Simple video playback with Silverlight

The main feature of Silverlight that I am really excited about is the capability playing back videos. It does mean that we do not have to embed WMP controls on the HTML page anymore, but it will be possible to playback Windows Media Videos with Silverlight only, that means that Silverlight does not rely on WMP for video playback and it will be possible to playback Windows Media videos on other platforms as well. I have not tried this capability on the Mac yet, so I do not know how well the video playback works or does not work. But here is a quick way how to do this:

I am reusing the files from my Silverlight scratch project:

Quick sample for starting with Silverlight

The first step is to get a video file and copy to folder, where the .html and .xaml file will be. Right now I am trying to play video only locally. Then you will need to create a new .xaml file (player.xaml), which looks like this:

<Canvas Width="800" Height="800" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Canvas x:Name="VideoLayer">
  <MediaElement x:Name="VideoElement" Canvas.Top="0" Canvas.Left="0" Stretch="Uniform" AutoPlay="True" Source="butterfly.wmv" />
  </Canvas>
 </Canvas>

In the .xaml snippet we have a Canvas and a MediaElement inside the Canvas. The source attribute is set to butterfly.wmv - this is the name of the video I copied to the same folder with the .html and .xaml files - and the autoplay property set to true, so it will start playing right away.

The next step is to modify the .html file, so it loads the right .xaml file (player.xaml) and change the size, so it can display the whole video.

So it will look like this:

 <script type="text/javascript">
  function createMySilverlightControl()
  { 
      Sys.Silverlight.createObject(
          "player.xaml",                  // Source property value.
          parentElement,                  // DOM reference to hosting DIV tag.
          "mySilverlightControl",         // Unique control ID value.
          {                               // Control properties.
              width:'720',                // Width of rectangular region of
                                          // control in pixels.
              height:'480',               // Height of rectangular region of
                                          // control in pixels.
              inplaceInstallPrompt:false, // Determines whether to display
                                          // in-place install prompt if
                                          // invalid version detected.
              background:'#D6D6D6',       // Background color of control.
              isWindowless:'false',       // Determines whether to display control
                                          // in Windowless mode.
              framerate:'24',             // MaxFrameRate property value.
              version:'0.9'               // Control version to use.
          },
          {
              onError:null,               // OnError property value --
                                          // event handler function name.
              onLoad:null                 // OnLoad property value --
                                          // event handler function name.
          },
          null);                          // Context value -- event handler function name.
  }
       
       
        // Retrieve the div element you created in the previous step.
        var parentElement =
            document.getElementById("mySilverlightControlHost");
       
        // This function creates the Silverlight control.
        createMySilverlightControl();
       
 </script>

 

Now the only thing is left to launch the html page and enjoy the video.

Displaying the Property pages for an Encoder Device

Having some problems with presenting the available devices for a Windows Media Encoder session and showing the properties for a specific device. In the Windows Media Encoder SDK you will find the following code on how to display property pages for plug-ins:

    if ( SUCCEEDED( hr ) )
    {
        hr = pPlugin->QueryInterface(IID_ISpecifyPropertyPages,
                                (LPVOID*)&pPages);
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pPages->GetPages( &uuid );
    }

    // Invoke a dialog box to display.
    if ( SUCCEEDED( hr ) )
    {
        hr = OleCreatePropertyFrame
         (
            m_hWnd,             // You must create the parent window.
          200,                    // Horizontal position for the dialog box.
            200,                // Vertical position for the dialog box.
          CComBSTR("name"),        // String used for the dialog box caption.
          1,                    // Number of pointers passed in pPlugin.
          &pPlugin,                // Pointer to the plug-in.
          uuid.cElems,            // Number of property pages.
          uuid.pElems,            // Array of property page CLSIDs.
          LOCALE_USER_DEFAULT,    // Locale ID for the dialog box.
          0,                    // Reserved.
          NULL                    // Reserved.
          );
    }

 

When you first run this code you will quickly find out that this is not really working. To make it work you need to change this a little bit:

 
    if ( SUCCEEDED( hr ) )
    {
        hr = pPlugin->QueryInterface(IID_ISpecifyPropertyPages,
                                (LPVOID*)&pPages);
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pPages->GetPages( &uuid );
    }

    IUnknown* pPPObjectUnk = NULL; 

    if(SUCCEEDED(hr))
    {
        hr = pPages->QueryInterface(IID_IUnknown, (LPVOID*)&pPPObjectUnk);
    }

    // Invoke a dialog box to display.
    if ( SUCCEEDED( hr ) )
    {
        hr = OleCreatePropertyFrame
         (
            m_hWnd,             // You must create the parent window.
          200,                    // Horizontal position for the dialog box.
            200,                // Vertical position for the dialog box.
          CComBSTR("name"),        // String used for the dialog box caption.
          1,                    // Number of pointers passed in pPlugin.
          &pPPObjectUnk,                // Pointer to the plug-in.
          uuid.cElems,            // Number of property pages.
          uuid.pElems,            // Array of property page CLSIDs.
          LOCALE_USER_DEFAULT,    // Locale ID for the dialog box.
          0,                    // Reserved.
          NULL                    // Reserved.
          );
    }

    if(pPPObjectUnk)
    {
        pPPObjectUnk->Release();
        pPPObjectUnk = NULL; 
    }

Media Transfer Protocol sample code.

Media Transfer Protocol a.k.a MTP is a Microsoft technology for transferring media files from your PC to your portable media player. I have a Zen Vision:M and I am very interested learning what this SDK can do for me :) There is a site called opensource.creative.com and it has a series of articles with very useful MTP related articles:

Getting the Microsoft Windows Media Format 9.5 SDK Working
MTP Device Enumeration in Windows
MTP File Transfer in Windows
Obtaining DRM Transfer Certificates

WMP11 Beta for XP

Available now! Download it from here. And some review as well "Ten reasons to love WMP11". They seem to like it... I am installing it now!

And they are right. WMP11 is really, really, totally cool! I thought I just double the risk and installed IE7 Beta2 as well, oh what is the worse can happen other than loosing all my data and reinstall my machine! And both of the applications are working like a charm. Actually, the Community Server editor is not working anymore, possibly because of IE7. But still happy...

Stupid Error : RunTime Check Failure #0 - The value of ESP

I just spent the last hour with this and I have no clue how would I found out this alone. I am trying to figure out the maximum keyframe space of a wmv file and for this I am using the Windows Media Format SDK and the wmvprop sample. At some point of the execution I would make a call for each video stream to find out this property by calling:

hr = pVideoMediaProps->GetMaxKeyFrameSpacing(&pllKeyFrameTime);

When I was stepping through my code and arrived to the call above I received the following error:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

I must tell you this error message is dead scary, because I am not a COM guru, nor a C++ one. So I spent an hour trying to figure it out and changed all the calling convetions in my call, but no joy! Then I called for help. Another 20 minutes 2 people staring at the screen then my collegue Kalman suggested to comment out my code and see if that works. It worked! Then we know that I am crappy coder :) Another session of blank staring for 2 mins then I saw this piece: like couple of lines earlier:

hr = pConfig->QueryInterface(IID_IWMStreamConfig, (void **) &pVideoMediaProps);

and the definition of my variable like 30 lines earlier:

IWMVideoMediaProps *pVideoMediaProps = NULL;

so that tells me basically that I screwed this up by querying for one interface and supplying another interface to QueryInterface. So tho correct the error it should be queried like this:

hr = pConfig->QueryInterface(IID_IWMVideoMediaProps, (void **) &pVideoMediaProps);

Honestly, I do not understand what happens under the hood - I really would like too, so someone smart please explain it to me - and why this error message shown, but I thought that I would try to save some time to other people with this. I really hope that this will be handy for someone sometimes :)

Another interesting twist was that after looking at the documentation I learned that:  

Remarks

This method retrieves the value set by SetMaxKeyFrameSpacing, or the default value for the key frame spacing, during the encoding process only. If called for a file that is open in the reader, the method always returns zero.

For more information, see the Remarks for SetMaxKeyFrameSpacing.

And I had a reader open and I received 0 as a result when I ignored this error! The moral of the story? COM rulez! Pay respect to it and documentation is your friend!

Protection Error while debugging DShow app

I recently had a small problem with debugging an MFC application, when I tried to run the application under debugger I received the following error mesage:

Protection Error - Debugger Detected! Please, close it down and restart.

more...