CloudUsing the Media Services SDK in Real-World Applications

Using the Media Services SDK in Real-World Applications

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

In an earlier article, we learned about Azure Media Services, the cloud offering for media-related tasks from Microsoft. In this article, we will learn how to get started building real-world applications that use the Media Services.

Media Service Account

If you do not have a media service account, you will need to navigate to https://manage.windowsazure.com and create a new media service account.

The Media Services home screen
Figure 1: The Media Services home screen

Once the account is created, you can click on account details and search for a media access key.

Confirming that the account has been created
Figure 2: Confirming that the account has been created

You can download the sample project that will contain the media access key.

Tools/Prerequisites

To start building Azure Media Services applications, you will need the Media Services libraries. These libraries are available for .NET and Java. For Java, it has three difference flavors (Windows, Mac and, Linux).

Create a new Windows Desktop Console Application titled AzureMediaServicesDemo.

Creating a new project
Figure 3: Creating a new project

Next, we will get the Azure Media Services .NET SDK. For this, we need to run the following command in the Package Manager Consoler of Visual Studio.

Opening Package Manager
Figure 4: Opening Package Manager

Install-Package windowsazure.mediaservices

When you execute the above command, you should see output as follows:

PM> Install-Package windowsazure.mediaservices
Attempting to resolve dependency 'Microsoft.Data.OData (≥ 5.6.0)'.
Attempting to resolve dependency 'System.Spatial (= 5.6.0)'.
Attempting to resolve dependency 'Microsoft.Data.Edm (= 5.6.0)'.
Attempting to resolve dependency 'Microsoft.Data.Services.Client (≥ 5.6.0)'.
Attempting to resolve dependency 'TransientFaultHandling.Core (≥ 5.1.1209.1)'.
Attempting to resolve dependency 'WindowsAzure.Storage (≥ 3.0.3.0)'.
Attempting to resolve dependency 'Newtonsoft.Json (≥ 5.0.6)'.
Attempting to resolve dependency 'Microsoft.WindowsAzure.ConfigurationManager (≥ 1.8.0.0)'.
Attempting to resolve dependency 'Microsoft.WindowsAzure.ConfigurationManager (≥ 2.0.3.0)'.
Installing 'System.Spatial 5.6.0'.

You are downloading System.Spatial from Microsoft Corporation, the license agreement to which is available at
http://go.microsoft.com/?linkid=9809688. Check the package for additional dependencies, which may come with
their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of
their license agreements. If you do not accept the license agreement(s), then delete the relevant
components from your device.

Successfully installed 'System.Spatial 5.6.0'.
nstalling 'Microsoft.Data.Edm 5.6.0'.

You are downloading Microsoft.Data.Edm from Microsoft Corporation, the license agreement to which is
available at http://go.microsoft.com/?linkid=9809688. Check the package for additional
dependencies, which may come with their own license agreement(s). Your use of the package and dependencies
constitutes your acceptance of their license agreements. If you do not accept the license agreement(s),
then delete the relevant components from your device.

Successfully installed 'Microsoft.Data.Edm 5.6.0'.
Installing 'Microsoft.Data.OData 5.6.0'.

You are downloading Microsoft.Data.OData from Microsoft Corporation, the license agreement to which is
available at http://go.microsoft.com/?linkid=9809688. Check the package for additional
dependencies, which may come with their own license agreement(s). Your use of
the package and dependencies constitutes your acceptance of their license agreements. If you do not
accept the license agreement(s), then delete the  relevant components from your device.

Successfully installed 'Microsoft.Data.OData 5.6.0'.
Installing 'Microsoft.Data.Services.Client 5.6.0'.

You are downloading Microsoft.Data.Services.Client from Microsoft Corporation, the license agreement to
which is available at http://go.microsoft.com/?linkid=9809688. Check the package for additional
dependencies, which may come with their own license agreement(s). Your use of the package and dependencies
constitutes your acceptance of their license agreements. If you do not accept the license agreement(s),
then delete the relevant components from your device.

Successfully installed 'Microsoft.Data.Services.Client 5.6.0'.
Installing 'TransientFaultHandling.Core 5.1.1209.1'.

You are downloading TransientFaultHandling.Core from Microsoft, the license agreement to which is available
at http://www.opensource.org/licenses/ms-pl. Check the package for additional dependencies, which may come
with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance
of their license agreements. If you do not accept the license agreement(s), then delete the relevant
components from your device.

Successfully installed 'TransientFaultHandling.Core 5.1.1209.1'.
Installing 'Newtonsoft.Json 5.0.6'.
Successfully installed 'Newtonsoft.Json 5.0.6'.
Installing 'WindowsAzure.Storage 3.0.3.0'.

You are downloading WindowsAzure.Storage from Microsoft, the license agreement to which is available at
http://go.microsoft.com/fwlink/?LinkId=331471. Check the package for additional dependencies, which may
come with their own license agreement(s). Your use of the package and dependencies constitutes your
acceptance of their license agreements. If you do not accept the license agreement(s), then delete the
relevant components from your device.

Successfully installed 'WindowsAzure.Storage 3.0.3.0'.
Installing 'Microsoft.WindowsAzure.ConfigurationManager 2.0.3'.

You are downloading Microsoft.WindowsAzure.ConfigurationManager from Microsoft, the license
agreement to which is available at http://aka.ms/windowsazureapache2. Check the package for additional
dependencies, which may come with their own license agreement(s). Your use of the package and dependencies
constitutes your acceptance of their license agreements. If you do not accept the license agreement(s),
then delete the relevant components from your device.

Successfully installed 'Microsoft.WindowsAzure.ConfigurationManager 2.0.3'.
Installing 'windowsazure.mediaservices 3.0.0.5'.

You are downloading windowsazure.mediaservices from Microsoft Corporation, the license agreement to which is
available at http://go.microsoft.com/fwlink/?LinkID=272666&clcid=0x409. Check the package for additional
dependencies, which may come with their own license agreement(s). Your use of the package and dependencies
constitutes your acceptance of their license agreements. If you do not accept the license agreement(s),
then delete the relevant components from your device.

Successfully installed 'windowsazure.mediaservices 3.0.0.5'.
Adding 'System.Spatial 5.6.0' to AzureMediaServicesDemo.
Successfully added 'System.Spatial 5.6.0' to AzureMediaServicesDemo.
Adding 'Microsoft.Data.Edm 5.6.0' to AzureMediaServicesDemo.
Successfully added 'Microsoft.Data.Edm 5.6.0' to AzureMediaServicesDemo.
Adding 'Microsoft.Data.OData 5.6.0' to AzureMediaServicesDemo.
Successfully added 'Microsoft.Data.OData 5.6.0' to AzureMediaServicesDemo.
Adding 'Microsoft.Data.Services.Client 5.6.0' to AzureMediaServicesDemo.
Successfully added 'Microsoft.Data.Services.Client 5.6.0' to AzureMediaServicesDemo.
Adding 'TransientFaultHandling.Core 5.1.1209.1' to AzureMediaServicesDemo.
Successfully added 'TransientFaultHandling.Core 5.1.1209.1' to AzureMediaServicesDemo.
Adding 'Newtonsoft.Json 5.0.6' to AzureMediaServicesDemo.
Successfully added 'Newtonsoft.Json 5.0.6' to AzureMediaServicesDemo.
Adding 'WindowsAzure.Storage 3.0.3.0' to AzureMediaServicesDemo.
Successfully added 'WindowsAzure.Storage 3.0.3.0' to AzureMediaServicesDemo.
Adding 'Microsoft.WindowsAzure.ConfigurationManager 2.0.3' to AzureMediaServicesDemo.
Successfully added 'Microsoft.WindowsAzure.ConfigurationManager 2.0.3' to AzureMediaServicesDemo.
Adding 'windowsazure.mediaservices 3.0.0.5' to AzureMediaServicesDemo.
Successfully added 'windowsazure.mediaservices 3.0.0.5' to AzureMediaServicesDemo.

Add the following using statement to the Program.cs file:

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

Next, we will a variable of type CloudMediaContext at the class level.

namespace AzureMediaServicesDemo
{
   class Program
   {
      private static CloudMediaContext _context = null;
      static void Main(string[] args)

We will now create the media context.

static void Main(string[] args)
   {
      _context = new CloudMediaContext("testmediaaccount1",
                 "QJC0ZP9+d72TN0by/z1574ZFUK3Zk0ZN9H/TS06UvMk=");

As the next step, we need to create a media asset and upload the file. This will be in its own function.

static public IAsset CreateAssetAndUploadSingleFile(AssetCreationOptions assetCreationOptions, string singleFilePath)
   {
      var asset = _context.Assets.Create("myAsset", assetCreationOptions);

      var fileName = Path.GetFileName(singleFilePath);

      var assetFile = asset.AssetFiles.Create(fileName);

      var accessPolicy = _context.AccessPolicies.Create("myAsset", TimeSpan.FromHours(100),
	                                                AccessPermissions.Read | AccessPermissions.List);

      var locator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy);

      //Upload the asset file
      assetFile.Upload(singleFilePath);

      locator.Delete();
      accessPolicy.Delete();

      return asset;
   }

Next, we will implement a method that will handle the encoding.

static IJob CreateEncodingJob(IAsset asset, string inputMediaFilePath, string outputFolder)
   {
      // Declare a new job.
      IJob job = _context.Jobs.Create("My encoding job");
      // Get a media processor reference, and pass to it the name of the
      // processor to use for the specific task.
      IMediaProcessor processor = _context.MediaProcessors.Where(p => p.Name ==
         "Azure Media Encoder").ToList().OrderBy(p => new Version(p.Version)).LastOrDefault();

      // Create a task with the encoding details, using a string preset.
      ITask task = job.Tasks.AddNew("My encoding task", processor, "H264 Broadband 720p",
         Microsoft.WindowsAzure.MediaServices.Client.TaskOptions.ProtectedConfiguration);

      // Specify the input asset to be encoded.
      task.InputAssets.Add(asset);

      // Add an output asset to contain the results of the job.
      task.OutputAssets.AddNew("Output asset", AssetCreationOptions.None);
      // Use the following event handler to check job progress.
      job.StateChanged += new EventHandler<JobStateChangedEventArgs>(StateChanged);

      // Launch the job.
      job.Submit();

      return job;
   }
   private static void StateChanged(object sender, JobStateChangedEventArgs e)
   {
      switch (e.CurrentState)
      {
         case JobState.Finished:
            Console.WriteLine("Job completed");
            break;
         case JobState.Error:
            Console.WriteLine("Job error");
            break;
         default:
            break;
       }
   }

Finally, we will call these functions from the Main method.

   static void Main(string[] args)
   {
      _context = new CloudMediaContext("testmediaaccount1",
	                               "QJC0ZP9+d72TN0by/z1574ZFUK3Zk0ZN9H/TS06UvMk=");
      IAsset asset = CreateAssetAndUploadSingleFile(AssetCreationOptions.None,
	                                            @"C:UsersVipulDownloadsBigBuckBunny.mp4");
      CreateEncodingJob(asset, @"C:UsersVipulDownloadsBigBuckBunny.mp4",
	                Path.GetFullPath("myoutputfolder"));
   }

Now, our applications is complete. When we run it and the application finishes the upload of the media content, you can view the dashboard to see that the content has been uploaded.

The completed application
Figure 5: The completed application

When the application completes its encoding, you can see it under the jobs/encoding tabs.

We just built a basic application that uploads contents and encodes it using Azure Media services.

Summary

In this article, we learned about how to use Azure Media Services. You can download sample code for this walkthrough.

About the Author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com

References

http://azure.microsoft.com/en-us/develop/media-services/developer-tools/

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories