How to Use Media Services
This guide shows you how to start programming with Windows Azure Media Services. The guide includes a technical overview of Media Services, steps to configure your Windows Azure account for Media Services, a setup guide for development, and topics that show how to accomplish typical programming tasks. The scenarios demonstrated include: uploading assets, encrypting or encoding assets, and delivering assets by downloading or by generating URLs for streaming content. The samples are written in C# and use the Media Services SDK for .NET. For more information on Windows Azure Media Services, refer to the Next Steps section.
You can also program Media Services using the OData-based REST APIs. You can build an application making REST API calls to Media Services from .NET languages or other programming languages. For a full documentation series on programming with the Media Services REST API, see Building Applications with the Windows Azure Media Services REST API. To start programming with REST, first enable your Windows Azure account for Media Services as described in the section Setting Up a Windows Azure Account for Media Services.
The most up-to-date Media Services SDK documentation is located here.
Table of Contents
What are Media Services?
Windows Azure Media Services form an extensible media platform that integrates the best of the Microsoft Media Platform and third-party media components in Windows Azure. Media Services provide a media pipeline in the cloud that enables industry partners to extend or replace component technologies. ISVs and media providers can use Media Services to build end-to-end media solutions. This overview describes the general architecture and common development scenarios for Media Services.
The following diagram illustrates the basic Media Services architecture.

Media Services Feature Support
The current release of Media Services provides the following feature set for developing media applications in the cloud.
- Ingest. Ingest operations bring assets into the system, for example by uploading them and encrypting them before they are placed into Windows Azure Storage. By the RTM release, Media Services will offer integration with partner components to provide fast UDP (User Datagram Protocol) upload solutions.
- Encode. Encode operations include encoding, transforming and converting media assets. You can run encoding tasks in the cloud using the Media Encoder that is included in Media Services. Encoding options include the following:
- Use the Windows Azure Media Encoder and work with a range of standard codecs and formats, including industry-leading IIS Smooth Streaming, MP4, and conversion to Apple HTTP Live Streaming.
- Convert entire libraries or individual files with total control over input and output.
- A large set of supported file types, formats, and codecs (see Supported File Types for Media Services).
- Supported format conversions. Media Services enable you to convert ISO MP4 (.mp4) to Smooth Streaming File Format (PIFF 1.3) (.ismv; .isma). You can also convert Smooth Streaming File Format (PIFF) to Apple HTTP Live Streaming (.msu8, .ts).
- Protect. Protecting content means encrypting live streaming or on demand content for secure transport, storage, and delivery. Media Services provide a DRM technology-agnostic solution for protecting content. Currently supported DRM technologies are Microsoft PlayReady Protection and MPEG Common Encryption. Support for additional DRM technologies will be available.
- Stream. Streaming content involves sending it live or on demand to clients, or you can retrieve or download specific media files from the cloud. Media Services provide a format-agnostic solution for streaming content. Media Services provide streaming origin support for Smooth Streaming, Apple HTTP Live Streaming, and MP4 formats. Support for additional formats will be available. You can also seamlessly deliver streaming content by using Windows Azure CDN or a third-party CDN, which enables the option to scale to millions of users.
Media Services Development Scenarios
Media Services support several common media development scenarios as described in the following table.
| Scenario | Description |
| Building end-to-end workflows | Build comprehensive media workflows entirely in the cloud. From uploading media to distributing content, Media Services provide a range of components that can be combined to handle specific application workflows. Current capabilities include upload, storage, encoding, format conversion, content protection, and on-demand streaming delivery. |
| Building hybrid workflows | You can integrate Media Services with existing tools and processes. For example, encode content on-site then upload to Media Services for transcoding into multiple formats and deliver through Windows Azure CDN, or a third-party CDN. Media Services can be called individually via standard REST APIs for integration with external applications and services. |
| Providing cloud support for media players | You can create, manage, and deliver media across multiple devices (including iOS, Android, and Windows devices) and platforms. |
Media Services Client Development
Extend the reach of your Media Services solution by using SDKs and player frameworks to build media client applications. These clients are for developers who want to build Media Services applications that offer compelling user experiences across a range of devices and platforms. Depending on the devices that you want to build client applications for, there are options for SDKs and player frameworks available from Microsoft and other third-party partners.
The following provides a list of available client SDKs and player frameworks. For more information on these and other planned SDKs and player frameworks, and the functionality they can support, see Media Services Client Development in the Media Services forums.
Mac and PC client support
For PCs and Macs you can target a streaming experience using Microsoft Silverlight or Adobe Open Source Media Framework.
Windows 8 applications
For Windows 8, you can build Windows Store applications using any of the supported development languages and constructs like HTML, Javascript, XAML, C# and C+.
Xbox
Xbox supports Xbox LIVE applications that can consume Smooth Streaming content. The Xbox LIVE Application Development Kit (ADK) includes:
- Smooth Streaming client for Xbox LIVE ADK
- Microsoft Media Platform: Player Framework for Xbox LIVE ADK
Embedded or dedicated devices
Devices such as connected TVs, set-top boxes, Blu-Ray players, OTT TV boxes, and mobile devices that have a custom application development framework and a custom media pipeline. Microsoft provides the following porting kits that can be licensed, and enables partners to port Smooth Streaming playback for the platform.
Windows Phone
Microsoft provides an SDK that can be used to build premium video applications for Windows Phone.
iOS devices
For iOS devices including iPhone, iPod, and iPad, Microsoft ships an SDK that you can use to build applications for these platforms to deliver premium video content: Smooth Streaming SDK for iOS Devices with PlayReady. The SDK is available only to licensees, so for more information, please email Microsoft. For information on iOS development, see the iOS Developer Center.
Android devices
Several Microsoft partners ship SDKs for the Android platform that add the capability to play back Smooth Streaming on an Android device. Please email Microsoft for more details on the partners.
Setting up a Windows Azure account for Media Services
To set up your Media Services account, use the Windows Azure Management Portal (recommended). See the topic How to Create a Media Services Account. After creating your account in the Management Portal, you are ready to set up your computer for Media Services development.
This section contains general prerequisites for Media Services development using the Media Services SDK for .NET. It also shows developers how to create a Visual Studio application for Media Services SDK development.
Prerequisites
- A Media Services account in a new or existing Windows Azure subscription. See the topic How to Create a Media Services Account.
- Operating Systems: Windows 7, Windows 2008 R2, or Windows 8.
- .NET Framework 4.5 or .NET Framework 4.
- Visual Studio 2012 or Visual Studio 2010 SP1 (Professional, Premium, Ultimate, or Express).
- Use the windowsazure.mediaservices Nuget package to install Windows Azure SDK for .NET. The following section shows how to use Nuget to install the Windows Azure SDK.
Creating an Application in Visual Studio
This section shows you how to create a project in Visual Studio and set it up for Media Services development. In this case the project is a C# Windows console application, but the same setup steps shown here apply to other types of projects you can create for Media Services applications (for example, a Windows Forms application or an ASP.NET Web application).
- Create a new C# Console Application in Visual Studio 2012 or Visual Studio 2010 SP1. Enter the Name, Location, and Solution name, and then click OK.
- Add a reference to System.Configuration assembly. To add a reference to System.Configuration, in Solution Explorer, right-click the References node and select Add Reference…. In the Manage References dialog, select System.Configuration and click OK.
-
Add references to Windows Azure SDK for .NET. (Microsoft.WindowsAzure.StorageClient.dll), Windows Azure Media Services SDK for .NET (Microsoft.WindowsAzure.MediaServices.Client.dll), and WCF Data Services 5.0 for OData V3 (Microsoft.Data.OData.dll) libraries using the windowsazure.mediaservices Nuget package.
To add references using Nuget, do the following. In Visual Studio Main Menu, select TOOLS -> Library Package Manager -> Package Manager Console. In the console window type Install-Package windowsazure.mediaservices and press Enter.
-
Overwrite the existing using statements at the beginning of the Program.cs file with the following code.
using System;
using System.Linq;
using System.Configuration;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.MediaServices.Client;
At this point, you are ready to start developing a Media Services application.
How to: Connect to Media Services programmatically
Nearly everything you do in Media Services programming requires you to have a reference to the server context object. The server context gives you programmatic access to all Media Services programming objects.
To get a reference to the server context, return a new instance of the context type as in the following code example. Pass your Media Services account name and account key (which you obtained during the account setup process) to the constructor.
static CloudMediaContext GetContext()
{
// Gets the service context.
return new CloudMediaContext(_accountName, _accountKey);
} It is often useful to define a module-level variable of type CloudMediaContext to hold a reference to the server context returned by a method such as GetContext. The rest of the code examples in this topic use a variable called _context to refer to the server context.
How to: Create an encrypted Asset and upload to storage
To get media content into Media Services, first create an asset and add files to it, and then upload the asset. This process is also called ingesting content.
When you create assets, you can specify three different options for encrypting them.
- AssetCreationOptions.None: no encryption. If you want to create an unencrypted asset, you must set this option.
- AssetCreationOptions.CommonEncryptionProtected: for Common Encryption Protected (CENC) files. An example is a set of files that are already PlayReady encrypted.
- AssetCreationOptions.StorageEncrypted: storage encryption. Encrypts a clear input file before it is uploaded to Azure storage.
NOTE: Note that Media Services provide on-disk storage encryption, not over the wire like Digital Rights Manager (DRM.)
The following does the following:
- Creates an empty Asset.
- Creates an AssetFile instance that we want to associate with the asset.
- Creates an AccessPolicy instance that defines the permissions and duration of access to the asset.
- Creates a Locator instance that provides access to the asset.
- Uploads a single media file into Media Services. The process of creating and uploading is also called ingesting assets.
static private IAsset CreateEmptyAsset(string assetName, AssetCreationOptions assetCreationOptions)
{
var asset = _context.Assets.Create(assetName, assetCreationOptions);
Console.WriteLine("Asset name: " + asset.Name);
Console.WriteLine("Time created: " + asset.Created.Date.ToString());
return asset;
}
static public IAsset CreateAssetAndUploadSingleFile(AssetCreationOptions assetCreationOptions, string singleFilePath)
{
var assetName = "UploadSingleFile_" + DateTime.UtcNow.ToString();
var asset = CreateEmptyAsset(assetName, assetCreationOptions);
var fileName = Path.GetFileName(singleFilePath);
var assetFile = asset.AssetFiles.Create(fileName);
Console.WriteLine("Created assetFile {0}", assetFile.Name);
var accessPolicy = _context.AccessPolicies.Create(assetName, TimeSpan.FromDays(3),
AccessPermissions.Write | AccessPermissions.List);
var locator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy);
Console.WriteLine("Upload {0}", assetFile.Name);
assetFile.Upload(singleFilePath);
Console.WriteLine("Done uploading of {0} using Upload()", assetFile.Name);
locator.Delete();
accessPolicy.Delete();
return asset;
} The following code shows how to upload multiple files.
static public IAsset CreateAssetAndUploadMultipleFiles( AssetCreationOptions assetCreationOptions, string folderPath)
{
var assetName = "UploadMultipleFiles_" + DateTime.UtcNow.ToString();
var asset = CreateEmptyAsset(assetName, assetCreationOptions);
var accessPolicy = _context.AccessPolicies.Create(assetName, TimeSpan.FromDays(30),
AccessPermissions.Write | AccessPermissions.List);
var locator = _context.Locators.CreateLocator(LocatorType.Sas, asset, accessPolicy);
var blobTransferClient = new BlobTransferClient();
blobTransferClient.NumberOfConcurrentTransfers = 20;
blobTransferClient.ParallelTransferThreadCount = 20;
blobTransferClient.TransferProgressChanged += blobTransferClient_TransferProgressChanged;
var filePaths = Directory.EnumerateFiles(folderPath);
Console.WriteLine("There are {0} files in {1}", filePaths.Count(), folderPath);
if (!filePaths.Any())
{
throw new FileNotFoundException(String.Format("No files in directory, check folderPath: {0}", folderPath));
}
var uploadTasks = new List<Task>();
foreach (var filePath in filePaths)
{
var assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath));
Console.WriteLine("Created assetFile {0}", assetFile.Name);
// It is recommended to validate AccestFiles before upload.
Console.WriteLine("Start uploading of {0}", assetFile.Name);
uploadTasks.Add(assetFile.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None));
}
Task.WaitAll(uploadTasks.ToArray());
Console.WriteLine("Done uploading the files");
blobTransferClient.TransferProgressChanged -= blobTransferClient_TransferProgressChanged;
locator.Delete();
accessPolicy.Delete();
return asset;
}
static void blobTransferClient_TransferProgressChanged(object sender, BlobTransferProgressChangedEventArgs e)
{
if (e.ProgressPercentage > 4) // Avoid startup jitter, as the upload tasks are added.
{
Console.WriteLine("{0}% upload competed for {1}.", e.ProgressPercentage, e.LocalFile);
}
} How to: Get a Media Processor instance
In Media Services a media processor is a component that handles a specific processing task, such as encoding, format conversion, encrypting, or decrypting media content. You typically create a media processor when you are creating a task to encode, encrypt, or convert the format of media content.
The following table provides the name and description of each available media processor.
| Media Processor Name | Description |
| Windows Azure Media Encoder | Lets you run encoding tasks using the Media Encoder. |
| Windows Azure Media Packager | Lets you convert media assets from .mp4 to smooth streaming format. Also, lets you convert media assets from smooth streaming to the Apple HTTP Live Streaming (HLS) format. |
| Windows Azure Media Encryptor | Lets you encrypt media assets using PlayReady Protection. |
| Storage Decryption | Lets you decrypt media assets that were encrypted using storage encryption. |
To create a media processor instance, create a method like the following example, and pass to it the string name of one of the listed media processors. The code example assumes the use of a module-level variable named _context to reference the server context as described in the section How to: Connect to Media Services Programmatically.
private static IMediaProcessor GetLatestMediaProcessorByName(string mediaProcessorName)
{
var processor = _context.MediaProcessors.Where(p => p.Name == mediaProcessorName).
ToList().OrderBy(p => new Version(p.Version)).LastOrDefault();
if (processor == null)
throw new ArgumentException(string.Format("Unknown media processor", mediaProcessorName));
return processor;
} How to: Check Job Progress
When you run jobs, you often require a way to track job progress. The following code example defines the StateChanged event handler. This event handler tracks job progress and provides updated status, depending on the state. The code also defines the LogJobStop method. This helper method logs error details.
private static void StateChanged(object sender, JobStateChangedEventArgs e)
{
Console.WriteLine("Job state changed event:");
Console.WriteLine(" Previous state: " + e.PreviousState);
Console.WriteLine(" Current state: " + e.CurrentState);
switch (e.CurrentState)
{
case JobState.Finished:
Console.WriteLine();
Console.WriteLine("********************");
Console.WriteLine("Job is finished.");
Console.WriteLine("Please wait while local tasks or downloads complete...");
Console.WriteLine("********************");
Console.WriteLine();
Console.WriteLine();
break;
case JobState.Canceling:
case JobState.Queued:
case JobState.Scheduled:
case JobState.Processing:
Console.WriteLine("Please wait...\n");
break;
case JobState.Canceled:
case JobState.Error:
// Cast sender as a job.
IJob job = (IJob)sender;
// Display or log error details as needed.
LogJobStop(job.Id);
break;
default:
break;
}
}
private static void LogJobStop(string jobId)
{
StringBuilder builder = new StringBuilder();
IJob job = GetJob(jobId);
builder.AppendLine("\nThe job stopped due to cancellation or an error.");
builder.AppendLine("***************************");
builder.AppendLine("Job ID: " + job.Id);
builder.AppendLine("Job Name: " + job.Name);
builder.AppendLine("Job State: " + job.State.ToString());
builder.AppendLine("Job started (server UTC time): " + job.StartTime.ToString());
builder.AppendLine("Media Services account name: " + _accountName);
builder.AppendLine("Media Services account location: " + _accountLocation);
// Log job errors if they exist.
if (job.State == JobState.Error)
{
builder.Append("Error Details: \n");
foreach (ITask task in job.Tasks)
{
foreach (ErrorDetail detail in task.ErrorDetails)
{
builder.AppendLine(" Task Id: " + task.Id);
builder.AppendLine(" Error Code: " + detail.Code);
builder.AppendLine(" Error Message: " + detail.Message + "\n");
}
}
}
builder.AppendLine("***************************\n");
// Write the output to a local file and to the console. The template
// for an error output file is: JobStop-{JobId}.txt
string outputFile = _outputFilesFolder + @"\JobStop-" + JobIdAsFileName(job.Id) + ".txt";
WriteToFile(outputFile, builder.ToString());
Console.Write(builder.ToString());
}
private static string JobIdAsFileName(string jobID)
{
return jobID.Replace(":", "_");
} How to: Encode an Asset
For media content on the server, you can encode the content with a number of media encodings and formats using Windows Azure Media Encoder. You can also use an encoder provided by a Media Services partner; third-party encoders are available through the Windows Azure Marketplace. You can specify the details of encoding tasks by using preset strings defined for your encoder, or by using preset configuration files.
The following steps are required to encode media content using the Media Encoder:
- Create an asset (or reference an existing asset).
- Declare a new job, using the Jobs.Create method.
- Declare a media processor to process the job. To do this, implement a method such as the GetLatestMediaProcessorByName method shown in the section How to: Get a Media Processor Instance. In this example, the code uses the Windows Azure Media Encoder to encode the input media file.
- Declare a task. To declare a task, you must give the task a friendly name, and then pass to it a media processor instance, a configuration string for handling the processing job, and a TaskCreationOptions setting (which specifies whether to encrypt the configuration data). In this example, the code uses a preset configuration string to tell Media Encoder how to encode the asset.
- Specify an input asset for the task. In this example, the input asset is the one that the code creates in the first step.
- Specify an output asset for the task. By default, all assets are created as encrypted for transport and storage in Media Services. To output a clear, unencrypted asset for playback, the code specifies AssetCreationOptions.None.
- Submit the job. To access the output of a job, reference the job.OutputMediaAssets collection. See the section How to: Deliver an Asset by Download.
The following method combines the previous steps to encode a media file:
static IJob CreateEncodingJob(string inputMediaFilePath, string outputFolder)
{
//Create an encrypted asset and upload to storage.
IAsset asset = CreateAssetAndUploadSingleFile(AssetCreationOptions.StorageEncrypted, inputMediaFilePath);
// 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 = GetLatestMediaProcessorByName("Windows Azure Media Encoder");
// Create a task with the encoding details, using a string preset.
ITask task = job.Tasks.AddNew("My encoding task",
processor,
"H264 Broadband 720p",
_protectedConfig);
// Specify the input asset to be encoded.
task.InputAssets.Add(asset);
// Add an output asset to contain the results of the job.
// This output is specified as AssetCreationOptions.None, which
// means the output asset is in the clear (unencrypted).
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();
// Optionally log job details. This displays basic job details
// to the console and saves them to a JobDetails-{JobId}.txt file
// in your output folder.
LogJobDetails(job.Id);
// Check job execution and wait for job to finish.
Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
progressJobTask.Wait();
// If job state is Error, the event handling
// method for job progress should log errors. Here we check
// for error state and exit if needed.
if (job.State == JobState.Error)
{
Console.WriteLine("\nExiting method due to job error.");
return job;
}
// Perform other tasks. For example, access the assets that are the output of a job,
// either by creating URLs to the asset on the server, or by downloading.
return job;
} How to: Protect an Asset with PlayReady Protection
In Media Services you can submit a job that integrates with Microsoft PlayReady Protection to encrypt a set of media files. The code in this section takes several streaming files from an input folder, creates a task and encrypts them with PlayReady Protection.
The following example shows several steps in creating a simple job to provide PlayReady Protection.
- Shows how to retrieve configuration data. You can get an example configuration file from the topic Task Preset for Windows Azure Media Encryptor.
- Uploads an mp4 input file.
- Converts the file to smooth streaming.
- Encrypts the file with PlayReady.
- Generates an origin locator URL.
- Submits the job and generates the origin locator.
The following code example shows how to implement the steps:
private static IJob CreatePlayReadyProtectionJob(string inputMediaFilePath, string primaryFilePath, string configFilePath)
{
// Create a storage-encrypted asset and upload the mp4.
IAsset asset = CreateAssetAndUploadSingleFile(AssetCreationOptions.StorageEncrypted, inputMediaFilePath);
// Declare a new job to contain the tasks.
IJob job = _context.Jobs.Create("My PlayReady Protection job");
// Set up the first task.
// Read the task configuration data into a string.
string configMp4ToSmooth = File.ReadAllText(Path.GetFullPath(configFilePath + @"\MediaPackager_MP4ToSmooth.xml"));
// Get a media processor reference, and pass to it the name of the
// processor to use for the specific task.
IMediaProcessor processor = GetLatestMediaProcessorByName("Windows Azure Media Packager");
// Create a task with the conversion details, using the configuration data.
ITask task = job.Tasks.AddNew("My Mp4 to Smooth Task",
processor,
configMp4ToSmooth,
_clearConfig);
// Specify the input asset to be converted.
task.InputAssets.Add(asset);
// Add an output asset to contain the results of the job. We do not need
// to persist the output asset to storage, so set the shouldPersistOutputOnCompletion
// param to false.
task.OutputAssets.AddNew("Streaming output asset",
AssetCreationOptions.None);
IAsset smoothOutputAsset = task.OutputAssets[0];
// Set up the second task.
// Read the configuration data into a string.
string configPlayReady = File.ReadAllText(Path.GetFullPath(configFilePath + @"\MediaEncryptor_PlayReadyProtection.xml"));
// Get a media processor reference, and pass to it the name of the
// processor to use for the specific task.
IMediaProcessor playreadyProcessor = GetLatestMediaProcessorByName("Windows Azure Media Encryptor");
// Create a second task.
ITask playreadyTask = job.Tasks.AddNew("My PlayReady Task",
playreadyProcessor,
configPlayReady,
_protectedConfig);
// Add the input asset, which is the smooth streaming output asset from the first task.
playreadyTask.InputAssets.Add(smoothOutputAsset);
// Add an output asset to contain the results of the job. Persist the output by setting
// the shouldPersistOutputOnCompletion param to true.
playreadyTask.OutputAssets.AddNew("PlayReady protected output asset",
AssetCreationOptions.None);
// Use the following event handler to check job progress.
job.StateChanged += new
EventHandler<JobStateChangedEventArgs>(StateChanged);
// Launch the job.
job.Submit();
// Optionally log job details. This displays basic job details
// to the console and saves them to a JobDetails-{JobId}.txt file
// in your output folder.
LogJobDetails(job.Id);
// Check job execution and wait for job to finish.
Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
progressJobTask.Wait();
// If job state is Error, the event handling
// method for job progress should log errors. Here we check
// for error state and exit if needed.
if (job.State == JobState.Error)
{
Console.WriteLine("\nExiting method due to job error.");
return job;
}
// Perform other tasks. For example, access the assets that are the output of a job,
// either by creating URLs to the asset on the server, or by downloading.
return job;
} How to: Manage Assets in storage
After you create media assets and upload them to Media Services, you can access and manage the assets on the server. You can also manage other objects on the server that are part of Media Services, including jobs, tasks, access policies, locators, and more.
The following example shows how to query for and return a reference to an asset , when you have an Id for the asset.
static IAsset GetAsset(string assetId)
{
// Use a LINQ Select query to get an asset.
var assetInstance =
from a in _context.Assets
where a.Id == assetId
select a;
// Reference the asset as an IAsset.
IAsset asset = assetInstance.FirstOrDefault();
return asset;
} To list all assets that you have available on the server, you can use code similar to the following example. Iterate through the assets collection, and optionally, you can display details about each asset.
static void ListAssets()
{
string waitMessage = "Building the list. This may take a few "
+ "seconds to a few minutes depending on how many assets "
+ "you have."
+ Environment.NewLine + Environment.NewLine
+ "Please wait..."
+ Environment.NewLine;
Console.Write(waitMessage);
// Create a Stringbuilder to store the list that we build.
StringBuilder builder = new StringBuilder();
foreach (IAsset asset in _context.Assets)
{
// Display the collection of assets.
builder.AppendLine("");
builder.AppendLine("******ASSET******");
builder.AppendLine("Asset ID: " + asset.Id);
builder.AppendLine("Name: " + asset.Name);
builder.AppendLine("==============");
builder.AppendLine("******ASSET FILES******");
// Display the files associated with each asset.
foreach (IAssetFile fileItem in asset.AssetFiles)
{
builder.AppendLine("Name: " + fileItem.Name);
builder.AppendLine("Size: " + fileItem.ContentFileSize);
builder.AppendLine("==============");
}
}
// Display output in console.
Console.Write(builder.ToString());
} The following example deletes all the assets from the collection.
foreach (IAsset asset in _context.Assets)
{
asset.Delete();
} For more information, see Manage Assets.
How to: Deliver an Asset by download
This section shows options for delivering media assets that you have previously added to Media Services. You can deliver Media Services content in numerous application scenarios. You can download media assets, or access them by using locator URLs (these are the delivery scenarios described in this topic). You can send media content to another application or to another content provider. For improved performance and scalability, you can also deliver content by using a Content Delivery Network (CDN), such as the Windows Azure CDN.
The code example in this section shows how to download media assets from Media Services. You can pass in an existing job reference, and then access its OutputMediaAssets collection (which is the set of one or more output media assets that results from running a job). This code example shows how to download output media assets from a job, but you can apply the same approach to download other assets.
// Download an asset that is the output of a job to a local folder.
static IAsset DownloadAssetToLocal( string jobId, string outputFolder)
{
// This method illustrates how to download a single asset.
// However, you can iterate through the OutputAssets
// collection, and download all assets if there are many.
// Get a reference to the job.
IJob job = GetJob(jobId);
// Get a reference to the first output asset. If there were multiple
// output media assets you could iterate and handle each one.
IAsset outputAsset = job.OutputMediaAssets[0];
IAccessPolicy accessPolicy = _context.AccessPolicies.Create("File Download Policy", TimeSpan.FromDays(30), AccessPermissions.Read);
ILocator locator = _context.Locators.CreateSasLocator(outputAsset, accessPolicy);
BlobTransferClient blobTransfer = new BlobTransferClient
{
NumberOfConcurrentTransfers = 20,
ParallelTransferThreadCount = 20
};
var downloadTasks = new List<Task>();
foreach (IAssetFile outputFile in outputAsset.AssetFiles)
{
// Use the following event handler to check download progress.
outputFile.DownloadProgressChanged += DownloadProgress;
string localDownloadPath = Path.Combine(outputFolder, outputFile.Name);
Console.WriteLine("File download path: " + localDownloadPath);
downloadTasks.Add(outputFile.DownloadAsync(Path.GetFullPath(localDownloadPath), blobTransfer, locator, CancellationToken.None));
outputFile.DownloadProgressChanged -= DownloadProgress;
}
Task.WaitAll(downloadTasks.ToArray());
return outputAsset;
}
static void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(string.Format("{0} % download progress. ", e.Progress));
} How to: Deliver streaming content
In addition to downloading media content from Media Services, you can use adaptive bitrate streaming to deliver content. For example, you can create a direct URL, called a locator, to streaming content on a Media Services origin server. Client applications such as Microsoft Silverlight can play the streaming content directly if you provide the locator.
The following code example shows the necessary steps to create an origin locator for an output asset produced by a job. The example assumes that you have already obtained a reference to an asset that contains smooth streaming files, and the variable named assetToStream is referenced in the code. After you have run this code to generate an origin locator on the asset, you should be able to use the resulting locator URL to directly play back the streaming content in a streaming client player such as Silverlight.
To create an origin locator to streaming content, follow these steps:
- Get a reference to the streaming manifest file in the asset. This is the file with the .ism extension.
- Define an access policy. This is how you determine the type of permissions and the duration for accessing an asset.
- Create the origin locator by calling the CreateLocator method (var originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, assetToStream, policy, DateTime.UtcNow.AddMinutes(-5))). Pass to the method an existing asset with streaming media files, an access policy, and a start time.
- Build a URL to the manifest file. To do this, add the manifest file name, followed by the string /manifest to the locator Path property.
The following code shows how to implement the steps:
private static ILocator GetStreamingOriginLocator( string targetAssetID)
{
// Get a reference to the asset you want to stream.
IAsset assetToStream = GetAsset(targetAssetID);
// Get a reference to the streaming manifest file from the
// collection of files in the asset.
var theManifest =
from f in assetToStream.AssetFiles
where f.Name.EndsWith(".ism")
select f;
// Cast the reference to a true IAssetFile type.
IAssetFile manifestFile = theManifest.First();
IAccessPolicy policy = null;
ILocator originLocator = null;
// Create a 30-day readonly access policy.
policy = _context.AccessPolicies.Create("Streaming policy",
TimeSpan.FromDays(30),
AccessPermissions.Read);
// Create a locator to the streaming content on an origin.
originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, assetToStream,
policy,
DateTime.UtcNow.AddMinutes(-5));
// Display some useful values based on the locator.
// Display the base path to the streaming asset on the origin server.
Console.WriteLine("Streaming asset base path on origin: ");
Console.WriteLine(originLocator.Path);
Console.WriteLine();
// Create a full URL to the manifest file. Use this for playback
// in streaming media clients.
string urlForClientStreaming = originLocator.Path + manifestFile.Name + "/manifest";
Console.WriteLine("URL to manifest for client streaming: ");
Console.WriteLine(urlForClientStreaming);
Console.WriteLine();
// Display the ID of the origin locator, the access policy, and the asset.
Console.WriteLine("Origin locator Id: " + originLocator.Id);
Console.WriteLine("Access policy Id: " + policy.Id);
Console.WriteLine("Streaming asset Id: " + assetToStream.Id);
// Return the locator.
return originLocator;
} How to: Deliver Apple HLS streaming content
A previous section shows how to create a locator to streaming media content on a Media Services origin server. This section shows how to create a locator to Apple HTTP Live Streaming (HLS) content on a Media Services origin server. Using this approach, you can build a URL to Apple HLS content, and provide it to Apple iOS devices for playback. The basic approach to building the locator URL is the same. Build a locator to the Apple HLS streaming asset path on an origin server, and then build a full URL that links to the manifest for the streaming content.
The following code example assumes that you have already obtained a reference to an HLS streaming asset, and the variable named assetToStream is referenced in the code. After you have run this code to generate an origin locator on the asset, you can use the resulting URL to play back the streaming content in an iOS device such as an IPAD or an IPhone.
To build a locator to Apple HLS streaming content, perform these steps as shown in the code example:
- Get a reference to the manifest file in the asset.
- Define an access policy.
- Create the origin locator by calling the CreateLocator method (var originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, assetToStream, policy, DateTime.UtcNow.AddMinutes(-5))). Pass to the method an existing HLS asset with streaming media files, an access policy, and a start time.
- Build a URL to the manifest file. To do this, add the manifest file name, followed by the string /manifest(format=m3u8-aapl) to the locator Path property.
The following code shows how to implement the steps:
static ILocator GetStreamingHLSOriginLocator( string targetAssetID)
{
// Get a reference to the asset you want to stream.
IAsset assetToStream = GetAsset(targetAssetID);
// Get a reference to the HLS streaming manifest file from the
// collection of files in the asset.
var theManifest =
from f in assetToStream.AssetFiles
where f.Name.EndsWith(".ism")
select f;
// Cast the reference to a true IAssetFile type.
IAssetFile manifestFile = theManifest.First();
IAccessPolicy policy = null;
ILocator originLocator = null;
// Create a 30-day readonly access policy.
policy = _context.AccessPolicies.Create("Streaming HLS Policy",
TimeSpan.FromDays(30),
AccessPermissions.Read);
originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, assetToStream,
policy,
DateTime.UtcNow.AddMinutes(-5));
// Create a URL to the HLS streaming manifest file. Use this for playback
// in Apple iOS streaming clients.
string urlForClientStreaming = originLocator.Path
+ manifestFile.Name + "/manifest(format=m3u8-aapl)";
Console.WriteLine("URL to manifest for client streaming: ");
Console.WriteLine(urlForClientStreaming);
Console.WriteLine();
// Return the locator.
return originLocator;
} For more information, see Deliver Assets.
How to: Enable Windows Azure CDN support
You can use a Content Delivery Network (CDN) to improve performance, scalability, and availability of content provided over the Internet. This section describes how to enable a Windows Azure CDN with a Media Services origin server.
Prerequisites
You must have a Windows Azure CDN configured for an existing Windows Azure Storage account. See the following topic: Getting Started with the Windows Azure CDN.
Enabling a Windows Azure CDN for Media Services
After you follow these setup steps, expect a two to three-week delay before your Windows Azure CDN endpoint is enabled. You will receive a confirmation email when the endpoint is enabled. For updates on the status of Media Services including the Windows Azure CDN capability, see the Media Services Forum.
To enable your Windows Azure CDN endpoint with a Media Services origin, send email to MediaServices@Microsoft.com and include the following information:
- Your Media Services account name (you created this account when you followed the directions in the section Setting Up a Windows Azure Account for Media Services).
- Your Windows Azure CDN endpoint that has been configured against an existing Windows Azure Storage account. You can get your endpoint by following these steps:
- Log into the Windows Azure Management Portal.
- In the navigation pane, click the option for Hosted Services, Storage Accounts & CDN.
- In the upper part of the navigation pane, click CDN (you may have to scroll to find this option).
- Select the CDN endpoint associated with the storage account.
- In the Properties for the CDN endpoint, copy the value for the Default HTTP endpoint. This is the value to supply in your email.
Next steps
Now that you have learned how to set up for Media Services development and perform some typical programming tasks, see the following resources to learn more about building Media Services applications.