Encrypt with PlayReady in Microsoft Azure Media Services

Another thing we can do with Microsoft Azure Media Services, in addition to ingest content, encoding and dynamic packaging already seen, is encryption using an external PlayReady server. In this post we will use Microsoft PlayReady Test Server.

PlayReady protection job

        static void Main(string[] args)
        {
            //0. Constants
            const string AssetName = "WOW_Mists_of_Pandaria_H264_Smooth";
            const string AccountName = "[YOUR_ACCOUNT_NAME]";
            const string AccountKey = "[YOUR_ACCOUNT_KEY]";

            //1. Nuget: Install-Package windowsazure.mediaservices

            //2. Create context
            var _context = new CloudMediaContext(AccountName, AccountKey);

            //3. Get asset
            var asset = (from a in _context.Assets
                         where a.Name == AssetName
                         select a).FirstOrDefault();

            //4. Create job

            var job = _context.Jobs.Create(string.Format("{0} - PlayReady Protection", AssetName));

            //5. Get encryptor processor
            var playReadyprocessor = _context.MediaProcessors.Where(p => p.Name == "Windows Azure Media Encryptor").ToList().OrderBy(wame => new Version(wame.Version)).LastOrDefault();

            //6. Get PlayReady configuration
            var configPlayReady = File.ReadAllText("PlayReadyProtection.xml");

            //7. Second task
            var task = job.Tasks.AddNew("PlayReady task",
                playReadyprocessor,
                configPlayReady,
                TaskOptions.ProtectedConfiguration);

            task.InputAssets.Add(asset);

            task.OutputAssets.AddNew(string.Format("{0} protected with PlayReady", AssetName), AssetCreationOptions.None);

            //8. Submit job
            job.StateChanged += StateChanged;

            job.Submit();

            //9. Check job execution and wait for job to finish
            var progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);

            progressJobTask.Wait();

            Console.WriteLine("The job is ended: {0}", job.RunningDuration);

            Console.ReadLine();
        }

Once you have installed the SDK and got the context, the first thing we need is to recover an asset we already have uploaded to the platform, or we can create a new one following the steps in the post Dynamic packaging in Microsoft Azure Media Services.
Create a job through context and retrieve the encryption processor that provides the platform, Windows Azure Media Encryptor. In order to know the PlayReady server and how to encrypt the content, we need a configuration file like this one( PlayReadyProtection.xml):

<taskDefinition xmlns="http://schemas.microsoft.com/iis/media/v4/TM/TaskDefinition#">
  <name>PlayReady Protection</name>
  <id>9A3BFEAC-F8AE-41CA-87FA-D639E4D1C753</id>
  <description xml:lang="en" />
  <properties namespace="http://schemas.microsoft.com/iis/media/v4/SharedData#" prefix="sd">
    <property name="adjustSubSamples" value="true" />
    <property name="contentKey" value="" />
    <property name="customAttributes" value="" />
    <property name="dataFormats" value="h264, avc1, mp4a, vc1, wma, owma, ovc1, aacl, aach, ac-3, ec-3, mlpa, dtsc, dtsh, dtsl, dtse" />
    <property name="keyId" value="" />
    <property name="keySeedValue" value="XVBovsmzhP9gRIZxWfFta3VVRPzVEWmJsazEJ46I" />
    <property name="licenseAcquisitionUrl" value="http://playready.directtaps.net/pr/svc/rightsmanager.asmx" />
    <property name="useSencBox" value="true" />
    <property name="serviceId" value="" />
  </properties>
  <inputFolder/>
  <outputFolder>Protected</outputFolder>
  <taskCode>
    <type>Microsoft.Web.Media.TransformManager.DigitalRightsManagementTask, Microsoft.Web.Media.TransformManager.DigitalRightsManagement, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</type>
  </taskCode>
</taskDefinition>

This preset was taken from the MSDN article Preset for Azure Media Encryptor where values ​​are used PlayReady Server Test: licenseAcquisitionUrl with http://playready.directtaps.net/pr/svc/rightsmanager.asmx keySeedValue value and the value XVBovsmzhP9gRIZxWfFta3VVRPzVEWmJsazEJ46I, you can confirm at test server page .

We need to create a new task, add the processor, PlayReady configuration XML and use the option TaskOptions.ProtectedConfiguration. We add the asset we want to process, specify an output name for the new file and launched the job:
A method called StateChanged is also used to know the status of the job:

        static void StateChanged(object sender, JobStateChangedEventArgs e)
        {
            Console.WriteLine("Job stated change event:");
            Console.WriteLine("  Previous state: {0}", e.PreviousState);
            Console.WriteLine("  Current state: {0}", e.CurrentState);
        }

The result of the operation will be similar to the following:

Azure Media Services - Encryption process

Publication of the asset

Once the process is complete, we can publish the asset from the portal and retrieve the encrypted asset URL:

Publish content

Smooth Streaming Health Monitor

The quickest way to check if the file is able to reproduce is through the Smooth Streaming Health Monitor. In it, simply put the URL of the new encryption asset and the player will be in charge of negotiating with the PlayReady server. Once the negotiation is complete we will check if the player is able to play the new video encryption via Azure Media Services.

Smooth Streaming Health Monitor

Happy encrypting!