Export (0) Print
Expand All

Delivering Assets with the Media Services SDK for .NET

Updated: April 29, 2013

This topic shows three most common options for delivering media content to your clients.

  • Streaming adaptive content in one of the following formats: MPEG DASH, Apple HTTP Live Streaming (HLS), and Smooth Streaming.

    Once you generate the streaming URLs (as shown in this topic) you can test them using the following players:

    To test the Smooth Streaming URL, use the smf.cloudapp.net/healthmonitorplayer.

    To test the MPEG DASH URL, use the dashif.org/reference/players/javascript/player.

    To test the HLS URL, use Safari an iOS device or 3ivx-hls-player

  • Progressively downloading MP4 files.

  • Downloading asset files to your local machine.

For more information and concepts and terminology related to delivering media content, see Delivering Content.

WarningWarning
Media Services uses the value of the IAssetFile.Name property when building URLs for the streaming content (for example, http://{WAMSAccount}.origin.mediaservices.windows.net/{GUID}/{IAssetFile.Name}/streamingParameters.) For this reason, percent-encoding is not allowed. The value of the Name property cannot have any of the following percent-encoding-reserved characters: !*'();:@&=+$,/?%#[]". Also, there can only be one ‘.’ for the file name extension.

Set up your project

The following topics show how to setup for Media Services development.

  1. Setup for Development on the Media Services SDK for .NET

    Note that code in this topic uses Azure Media Services .NET SDK Extensions.  The Media Services .NET SDK Extensions is a set of extension methods and helper functions that simplify your code and make it easier to develop with Media Services.

  2. Connect to Media Services Using the Media Services SDK

Example

You can deliver (download or stream) assets that are already stored in your Media Services account (available through the Assets collection). You can also download or stream assets that result from running an encoding or packaging job (available through the OutputMediaAssets collection). The example in this topic shows how to access and deliver the job’s output media assets.

The code in this section does the following:

  1. Creates a new asset and uploads a mezzanine MP4 file from a local directory.

  2. Prepares a job with a single task to transcode the previous mezzanine asset into a multi-bitrate asset.

  3. Submits the job and waits until it is completed.

  4. Gets the output asset produced the encoding job.

  5. Publishes the output asset by creating an Origin locator.

  6. Generates the Smooth Streaming, HLS and MPEG-DASH URLs for adaptive streaming.

    To stream your content based on the set of adaptive bitrate MP4 files, you must first get at least one On-demand Streaming reserved unit. For more information, see Dynamic Packaging.

  7. Creates a SAS locator that is used for progressive download and to download files to a local directory.

    The content that you want to progressively download cannot be encrypted.

  8. Generates the progressive download URLs for each MP4 file in the asset.

  9. Downloads the output asset to a local folder.

noteNote
This sample uses Azure Media Services .NET SDK Extensions.  The SDK extensions hide lots of details. If you want to see how some methods are implemented (for example, how the following methods build the streaming URLs: GetSmoothStreamingUri(), GetHlsUri(), andGetMpegDashUri()), you can browse through the code located on github: azure-sdk-for-media-services-extensions.

Make sure to update the code to point to the folder where your input MP4 file is located.

private static readonly string _mediaFiles =
Path.GetFullPath(@"../..\Media");

private static readonly string _singleMP4File =
    Path.Combine(_mediaFiles, @"SingleMP4\BigBuckBunny.mp4");

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using Microsoft.WindowsAzure.MediaServices.Client;


namespace MediaServicesDeliveringContent 
{ 
    class Program 
    {
        private static readonly string _mediaFiles =
            Path.GetFullPath(@"../..\Media");

        private static readonly string _singleMP4File =
            Path.Combine(_mediaFiles, @"SingleMP4\BigBuckBunny.mp4");

        private static MediaServicesCredentials _cachedCredentials = null;
        private static CloudMediaContext _context = null;

        // Media Services account information.
        private static readonly string _mediaServicesAccountName =
            ConfigurationManager.AppSettings["MediaServicesAccountName"];
        private static readonly string _mediaServicesAccountKey =
            ConfigurationManager.AppSettings["MediaServicesAccountKey"];
        
        static void Main(string[] args) 
        {
            _cachedCredentials = new MediaServicesCredentials(_mediaServicesAccountName,
                                                                                _mediaServicesAccountKey);
            _context = new CloudMediaContext(_cachedCredentials); 

            Console.WriteLine("Creating new asset from local file..."); 
 
            // Create a new asset and upload a mezzanine file from a local path. 
            IAsset inputAsset = _context.Assets.CreateFromFile(
                _singleMP4File, 
                AssetCreationOptions.None, 
                (af, p) => 
                { 
                    Console.WriteLine("Uploading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress); 
                }); 
 
            Console.WriteLine("Asset created."); 
 
            // Prepare a job with a single task to transcode the previous mezzanine asset 
            // into a multi-bitrate asset. 
            IJob job = _context.Jobs.CreateWithSingleTask( 
                MediaProcessorNames.WindowsAzureMediaEncoder, 
                MediaEncoderTaskPresetStrings.H264AdaptiveBitrateMP4Set720p, 
                inputAsset, 
                "Sample Adaptive Bitrate MP4", 
                AssetCreationOptions.None); 
 
            Console.WriteLine("Submitting transcoding job..."); 
 
            // Submit the job and wait until it is completed. 
            job.Submit(); 
            job = job.StartExecutionProgressTask( 
                j => 
                { 
                    Console.WriteLine("Job state: {0}", j.State); 
                    Console.WriteLine("Job progress: {0:0.##}%", j.GetOverallProgress()); 
                }, 
                CancellationToken.None).Result; 
 
            Console.WriteLine("Transcoding job finished."); 
             
            // The OutputMediaAssets[0] contains the first 
            // (and in this case the only) output asset 
            // produced by the encoding job.
            IAsset outputAsset = job.OutputMediaAssets[0]; 
 
            Console.WriteLine("Publishing output asset..."); 
 
            // Publish the output asset by creating an Origin locator.  
            // Define the Read only access policy and
            // specify that the asset can be accessed for 30 days.  
            _context.Locators.Create( 
                LocatorType.OnDemandOrigin, 
                outputAsset, 
                AccessPermissions.Read, 
                TimeSpan.FromDays(30));

            // Generate the Smooth Streaming, HLS and MPEG-DASH URLs for adaptive streaming.  
            Uri smoothStreamingUri = outputAsset.GetSmoothStreamingUri();
            Uri hlsUri = outputAsset.GetHlsUri();
            Uri mpegDashUri = outputAsset.GetMpegDashUri();

            // To stream your content based on the set of 
            // adaptive bitrate MP4 files, you must first get 
            // at least one On-demand Streaming reserved unit. 
            // For more information, see http://msdn.microsoft.com/en-us/library/jj889436.aspx.
            Console.WriteLine("Output is now available for adaptive streaming:");

            // Show the streaming URLs. 
            Console.WriteLine(smoothStreamingUri);
            Console.WriteLine(hlsUri);
            Console.WriteLine(mpegDashUri);
                
            //
            // Create a SAS locator that is used for progressive download
            // or to download files to a local directory.  
            // The content that you want to progressively download cannot be encrypted.
            _context.Locators.Create( 
                LocatorType.Sas, 
                outputAsset, 
                AccessPermissions.Read, 
                TimeSpan.FromDays(30)); 

            // Get all the MP4 files in the output asset.
            IEnumerable<IAssetFile> mp4AssetFiles = outputAsset
                    .AssetFiles
                    .ToList()
                    .Where(af => af.Name.EndsWith(".mp4", 
                        StringComparison.OrdinalIgnoreCase));

            // Generate the Progressive Download URLs for each MP4. 
            List<Uri> mp4ProgressiveDownloadUris = 
                mp4AssetFiles.Select(af => af.GetSasUri()).ToList();

            Console.WriteLine("You can progressively download the following MP4 files:");
            mp4ProgressiveDownloadUris.ForEach(uri => Console.WriteLine(uri)); 
 
             string outputFolder = Path.Combine(_mediaFiles, @"job-output");

            if (!Directory.Exists(outputFolder)) 
            { 
                Directory.CreateDirectory(outputFolder); 
            } 
 
            Console.WriteLine("Downloading output asset files to local folder..."); 
 
            // Download the output asset to a local folder. 
            outputAsset.DownloadToFolder( 
                outputFolder, 
                (af, p) => 
                { 
                    Console.WriteLine("Downloading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress); 
                }); 
 
            Console.WriteLine("Downloaded files are located in '{0}'.", Path.GetFullPath(outputFolder)); 
        } 
    } 
} 

See Also


Build Date:

2014-07-11

Community Additions

ADD
Show:
© 2014 Microsoft