Rewind and Fast Forward using the Smooth Streaming Media Element (SSME) Beta 2

Hey Folks! Time ago I blogged about how to add the Slow Motion capability to your Smooth Streaming player by using the Smooth Streaming Media Element published by the IIS team.

Now it’s time to share with you how to provide the rewind and fast forward options to enable a real rich video experience.

Again, adding support for these features is easy, and if you understood what we did for providing the Slow Motion experience, then the following will be a piece of cake.

For rewind, the trick is getting all the playback rates under 0.0 and for fast forward, all the playback rates above 1.0.

Show me the code!

Rewind
/// <summary>
/// Toogles the PlaybackRate of the Smooth Streaming Media Element.
/// Uses the PlaybackRates under 0.0 to support Rewind.
/// </summary>
public void OnRewind()
{
    // PlaybackRate index to set on the SSME.
    int newPlaybackRateIndex = 0;
    // Verify the state of the SmoothPlayer
    if (this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Paused
        || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Playing
        || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Buffering)
    {
        // Get the current PlaybackRate of the SSME
        double playbackRate = this.PlaybackRate;
        // The first time we get the Rewind PlaybackRates from
        // the SSME supported PlayBackRates. Rewind PlaybackRates 
        // are under 0.0
        if (this.rewindPlaybackRates == null)
        {
            this.rewindPlaybackRates = new List<double>();
            IList<double> supportedPlaybackRates = this.SmoothPlayer.SupportedPlaybackRates;
            for (int i = 0; i < supportedPlaybackRates.Count - 1; i++)
            {
                if (supportedPlaybackRates[i] < 0.0)
                {
                    this.rewindPlaybackRates.Add(supportedPlaybackRates[i]);
                }
            }
            this.rewindPlaybackRates.Add(1.0);
        }
        // Verify if the current PlaybackRate is within the 
        // allowables values. Get the index of the new 
        // PlaybackRate to set.
        if (playbackRate >= 0.0)
        {
            newPlaybackRateIndex = 0;
        }
        else
        {
            for (int i = 0; i < this.rewindPlaybackRates.Count - 1; i++)
            {
                if (this.rewindPlaybackRates[i] == playbackRate)
                {
                    newPlaybackRateIndex = (i + 1) % this.rewindPlaybackRates.Count;
                    break;
                }
            }
        }
        this.SmoothPlayer.SetPlaybackRate(this.rewindPlaybackRates[newPlaybackRateIndex]);
    }
}
Fast Forward
/// <summary>
/// Toogles the PlaybackRate of the Smooth Streaming Media Element.
/// Uses the PlaybackRates greather than 1.0 to support Fast forward.
/// </summary>
public void OnFastForward()
{
    // PlaybackRate index to set on the SSME.
    int newPlaybackRateIndex = 0;
    // Verify the state of the SmoothPlayer
    if (this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Paused
        || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Playing
        || this.SmoothPlayer.CurrentState == SmoothStreamingMediaElementState.Buffering)
    {
        // Get the current PlaybackRate of the SSME
        double playbackRate = this.PlaybackRate;
        // The first time we get the Fast Forward PlaybackRates from
        // the SSME supported PlayBackRates. Fast Forward PlaybackRates 
        // are above 1.0
        if (this.fastForwardPlaybackRates == null)
        {
            this.fastForwardPlaybackRates = new List<double>();
            IList<double> supportedPlaybackRates = this.SmoothPlayer.SupportedPlaybackRates;
            for (int i = 0; i < supportedPlaybackRates.Count - 1; i++)
            {
                if (supportedPlaybackRates[i] > 1.0)
                {
                    this.fastForwardPlaybackRates.Add(supportedPlaybackRates[i]);
                }
            }
            this.fastForwardPlaybackRates.Add(1.0);
        }
        // Verify if the current PlaybackRate is within the 
        // allowables values. Get the index of the new 
        // PlaybackRate to set.
        if (playbackRate <= 1.0)
        {
            newPlaybackRateIndex = 0;
        }
        else
        {
            for (int i = 0; i < this.fastForwardPlaybackRates.Count - 1; i++)
            {
                if (this.fastForwardPlaybackRates[i] == playbackRate)
                {
                    newPlaybackRateIndex = (i + 1) % this.fastForwardPlaybackRates.Count;
                    break;
                }
            }
        }
        this.SmoothPlayer.SetPlaybackRate(this.fastForwardPlaybackRates[newPlaybackRateIndex]);
    }
}

For more information about the SSME Beta 2 release, check Vishal’s post : IIS Smooth Streaming Player Development Kit Beta 2 released

Enjoy it!
kick it on DotNetKicks.com
Shout it

Slow Motion using the Smooth Streaming Media Element (SSME)

The week started with really great news from the IIS Team. The IIS Media Services 3.0 streaming was released. Among others features the release includes the RTW bits of Live Smooth Streaming.

Also, in the efforts to bring the Smooth Streaming experience to everyone the IIS team published the IIS Smooth Streaming Player Development Kit 1.0 – Beta 1 which is intended to aid the development of rich Smooth Streaming experiences.

Bunch of posts to review if you are not aware of the new releases:

I’m very excited with this release, as we have been using the Smooth Streaming Media Element almost from its beginning mainly for the work we did for the NBC Sunday Night Football event. (see here & here). I will start sharing with you how to take advantage of the SSME.

In this post I will show you how easy is to bring an Slow Motion experience using the SSME. Vishal blogged a great SSME getting started guide that I encourage you  to review before digging into the details of this post.

Adding support for Slow Motion to your Smooth Player is really easy, and basically what you have to do is getting the playback rates between 0.0 and 1.0, and move across them every time you click your Slow Motion button on the player.

Below you will find a code snippet ready to be used that will help you to implement the Slow Motion logic.

private List<double> slowMotionPlaybackRates;
/// <summary>
/// Toogles the PlaybackRate of the Smooth Streaming Media Element.
/// Uses the PlaybackRates between 0.0 and 1.0 to support Slow Motion.
/// </summary>
public void OnSlowMotion()
{
    // PlaybackRate index to set on the SSME.
    int newPlaybackRateIndex = 0;
    // Verify the state of the SmoothPlayer
    if (this.SmoothPlayer.CurrentState == MediaElementState.Paused
        || this.SmoothPlayer.CurrentState == MediaElementState.Playing
        || this.SmoothPlayer.CurrentState == MediaElementState.Buffering)
    {
        // Get the current PlaybackRate of the SSME
        double playbackRate = this.SmoothPlayer.PlaybackRate;
        // The first time we get the SlowMotion PlaybackRates from
        // the SSME supported PlayBackRates. SlowMotion PlaybackRates 
        // are between 0.0 and 1.0.
        if (this.slowMotionPlaybackRates == null)
        {
            this.slowMotionPlaybackRates = new List<double>();
            IList<double> supportedPlaybackRates = this.SmoothPlayer.SupportedPlaybackRates;
            for (int i = 0; i < supportedPlaybackRates.Count; i++)
            {
                if (supportedPlaybackRates[i] > 0.0 && supportedPlaybackRates[i] < 1.0)
                {
                    this.slowMotionPlaybackRates.Add(supportedPlaybackRates[i]);
                }
            }
            this.slowMotionPlaybackRates.Add(1.0);
        }
        // Verify if the current PlaybackRate is within the 
        // allowables values. Get the index of the new 
        // PlaybackRate to set.
        if (playbackRate <= 0.0 || playbackRate >= 1.0)
        {
            newPlaybackRateIndex = 0;
        }
        else
        {
            for (int i = 0; i < this.slowMotionPlaybackRates.Count; i++)
            {
                if (this.slowMotionPlaybackRates[i] == playbackRate)
                {
                    newPlaybackRateIndex = (i + 1) % this.slowMotionPlaybackRates.Count;
                    break;
                }
            }
        }
        this.SmoothPlayer.SetPlaybackRate(this.slowMotionPlaybackRates[newPlaybackRateIndex]);
    }
}

Hope you find the code snippet useful. Stay tuned!, new posts are coming.

PS: If you face any issue with the Smooth Streaming Development Kit or if you just want to provide feedback to the IIS team, please use this thread.

PRISM @ CodeCamp Buenos Aires 2009

This post announces an event being held at Buenos Aires, Argentina.

CodeCamp Buenos Aires 2009

 

On September 26th, a new CodeCamp will take place at Buenos Aires. This year, I will join my teammate Diego Poza and together we will talk about how to develop composite applications for WPF & Silverlight using PRISM. Our presentation starts at 4:15 PM and will last 1 hour.

 

During the presentation we will go over PRISM Core Concepts and show a real-world implementation. Don’t miss the surprises at the end of the session.

Desarrollando aplicaciones modulares en WPF y Silverlight con Prism

On a related topic, Southworks is one of the sponsors of the event and several other southies will be presenting as well:

  • Matias and Johnny will be presenting about Mega Datacenters with Windows Azure. Johnny will also join Zaiden and together they will present about VS 2010 and C# 4.0
  • Martin Salias will be in functional mode explaining the main advantages of F#.
  • Nico Paez will be talking about open source tools for .NET
  • Beto, our IT Pro, will be off the charts. He will present in four sessions talking about Hyper-V, Windows Server 2008 R2 and SSME (SCOM, SCVMM, SCDPM).

Are you going to miss this amazing event? Register Now!

Where: Universidad de Palermo, Mario Bravo 1050, Buenos Aires

Time: From 9:30 AM to 19:00 PM

See you in the event and in the Southworks booth.

Live Smooth Streaming: How-to: Start, Stop & Shutdown a Publishing Point Programmatically

During the last months I have been working in a project highly related to multimedia and iis-smoothclient development technologies such as Silverlight. And in the last few weeks, I started to look to some of the new IIS Media services such as Smooth Streaming and Live Smooth Streaming in order to gather as much knowledge about them to perform some spikes for the project.

One of the things that came up while spiking was finding a way to start, stop & shutdown a publishing point via code.

Microsoft.Web.Administration to the Rescue

ServerManagerWith IIS7 a new API to administer IIS from managed code was introduced. This API is really simple to use and with a few lines of code you can manipulate the server configuration as any other information available in IIS7. (if you want to taste the power of this API, I recommend you to read this post from Carlos Aguilar Mares).

So, I used the Microsoft.Web.Administration.dll assembly, that can be found at IIS Directory (%windir%\System32\inetSrv), to perform the operations over the publishing point. But it was not so easy to do it, as there is no information available yet about what RSCA function has to be called  and which parameters need to be used in order get the desired results.

Then, I started to look at the different IIS configuration files (%windir%\System32\inetsrv\config) searching for clues and I came up with the Microsoft.Web.Management.Media.LiveStreaming.dll assembly (this assembly can be found in the GAC after installing the Live Smooth Streaming bits).

In that moment I summoned my best friend Reflector and together went to the depths of this assembly until we found some code that might be useful for our objective.

After doing some tests, I ended up with the following method:

private static void ExecuteRscaFunction(ConfigurationElement workerProcess,
                                        string siteName, string applicationPath,
                                        string fileName, string functionName)
{
    ConfigurationMethod configurationMethod = workerProcess.Methods["GetCustomData"];

    ConfigurationMethodInstance instance = configurationMethod.CreateInstance();
    instance.Input["guidIdOfFunctionCall"] = “Media_LiveStreaming_Control”;

    string currentLogicalPath = applicationPath;

    if (!currentLogicalPath.EndsWith(“/”, StringComparison.OrdinalIgnoreCase))
    {
        currentLogicalPath = currentLogicalPath + “/”;
    }

    string fullPath = string.Concat(currentLogicalPath, fileName);
    string parameters = string.Format(CultureInfo.InvariantCulture, “{0};{1};{2}”,
                                      functionName, siteName, fullPath);
    instance.Input["parametersOfFunctionCall"] = parameters;
    instance.Execute();
}

The most important thing of the method above is the functionName parameter. This parameter can be one the following values depending on what you want to accomplish:

  • StartPublishingPoint. This is used to start a publishing point.
  • StopPublishingPoint. This is used to stop the live source of the publishing point.
  • ShutdownPublishingPoint. This is used to shutdown a publishing point.

To understand where the others parameters should come from, let’s take a look at the following example: image So, if we want to the start the LiveSmoothStream publishing point associated to the SmoothStreaming application from the Default Web Site, we should call the ExecuteRscaFunction method in this way:

ExecuteRscaFunction(workerProcess,
                    “Default Web Site”, “/SmoothStreaming”,
                    “LiveSmoothStream.isml”, “StartPublishingPoint”);

The remaining parameter (workerProcess) must be retrieved from the application pool associated to the application.

Note: You can use the Microsoft.Web.Administration API to get all the values previously mentioned.

To see if the function is working you can add the following lines of code at the end of the method to parse the output and print it on the console.

string rawOutput = instance.Output["data"] as string;

byte[] bytes = Convert.FromBase64String(rawOutput);
ASCIIEncoding encoding = new ASCIIEncoding();

Console.WriteLine(encoding.GetString(bytes));

The output after executing the Stop, Shutdown and Start functions:

image

Hope this helps!. If you want to learn more about Smooth Streaming you might find useful the following links:

Happy streaming!.

kick it on DotNetKicks.com