The player framework's adaptive plugin automatically lights up smooth streaming specific features in the player framework and handles common tasks such as restricting bitrates depending on the size of the player so bandwidth isn't wasted. However, sometimes you need more control over the smooth streaming SDK and have the need to call it directly. The player framework exposes all smooth streaming APIs to your app directly so you still have flexibility if needed.

Properties and methods

In Xaml and JS, it is easy to call methods and access properties on the smooth streaming SDK. Simply access the instance of the IAdaptiveSourceManager instance associated with the player and call away.

For example in JS:
var manager = mediaPlayer.adaptivePlugin.manager.adaptiveSrcManager;
manager.setBufferDelaySec(10);

or from C#:
var adaptivePlugin = new Microsoft.PlayerFramework.Adaptive.AdaptivePlugin();
player.Plugins.Add(adaptivePlugin);
adaptivePlugin.Manager.AdaptiveSrcManager.SetBufferDelaySec(10);

Events

The smooth streaming SDK is a high performance media extension SDK and therefore does the majority of its work on a background thread. But this also means that events are raised on a background thread.

Handling events in C#
When using Xaml/C#/VB, this only presents a problem if you need to modify the UI from such event. In this case, to push control onto the UI thread, simply save a dispatcher instance ahead of time. For example:
private CoreDispatcher dispatcher;

// save dispatcher
dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

// wire up event
adaptiveSourceManager.ManifestReadyEvent += adaptiveSourceManager_ManifestReadyEvent;

then, in your event handler, use the dispatcher...
async void adaptiveSourceManager_ManifestReadyEvent(AdaptiveSource sender, ManifestReadyEventArgs args)
{
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        // do something on the UI thread
    });
}

Handling events in JS
However, handling WinRT events raised on a background thread is not supported in JavaScript. Fear not: you can easily build a light weight WinRT bridge class to do the dispatcher trick above and raise it's own event on the UI thread.

Here's how:
  1. Create a new C# Windows Runtime Component project and add it to your solution and name the project AdaptiveBridge
  2. Download and add this class to that project.
  3. Add a reference to the Smooth Streaming SDK to your new WinRT project.
  4. Make sure it compiles (note, a warning will appear but you can ignore it).
  5. Add a reference to your new WinRT project from your main JS project that uses the player framework.
  6. In your JS file that uses the player framework, create an instance of your bridge class and pass it the adaptiveSrcManager provided to you by the plugin. Once you've done this you can wire up events to it just as you would if the AdaptiveSourceManager raised its events on the UI thread.
var mediaPlayerElement = element.querySelector("[data-win-control='PlayerFramework.MediaPlayer']");
mediaPlayer = mediaPlayerElement.winControl;

var manager = mediaPlayer.adaptivePlugin.manager.adaptiveSrcManager;
//this won't work because adaptiveSrcManager events are raised from a background thread
//manager.addEventListener("adaptivesourcestatusupdatedevent", _onAdaptiveEvent, false);
new AdaptiveBridge.AdaptiveSourceManagerBridge(manager);
bridge.addEventListener("adaptivesourcestatusupdatedevent", _onAdaptiveEvent, false);
...
function _onAdaptiveEvent(e) {
    switch (e.updateType)
    {
        case 4:
            console.log("bitratechanged: " + e.additionalInfo);
            break;
    }
}

Notes:
  • For JS apps: Be aware that any code you run will not be executing on the same thread that the event was raised from. This will limit your ability to do things like call RestrictTracks from JS because that call must be made within the real ManifestReady event handler (not the one bubbled from the dispatcher). If this presents a problem, you should modify the WinRT class to do the work instead of trying to do it from JS.
  • Please consult the smooth streaming SDK API documentation for more info on what APIs are available on how to use them.
  • You can also use the previously mentioned bridge class in a non player framework app that uses the smooth streaming SDK directly. It has no dependencies on the player framework itself, only the Smooth Streaming SDK.

Last edited Jan 23, 2013 at 5:04 PM by timgreenfield, version 5