private void CreatePrograms(IChannel channel, int?archiveWindowMinutes, bool archiveEncryption)
        {
            string assetName = string.Concat(channel.Name, Constant.Media.Live.ProgramClearSuffix);
            IAsset asset     = _media.Assets.Create(assetName, AssetCreationOptions.None);

            CreateProgram(channel, asset, archiveWindowMinutes);

            if (archiveEncryption)
            {
                assetName = string.Concat(channel.Name, Constant.Media.Live.ProgramAesSuffix);
                asset     = _media.Assets.Create(assetName, AssetCreationOptions.StorageEncrypted);
                ContentProtection contentProtection = new ContentProtection()
                {
                    ContentAuthTypeToken = true,
                    Aes = true
                };
                AddDeliveryPolicies(asset, contentProtection);
                CreateProgram(channel, asset, archiveWindowMinutes);

                assetName         = string.Concat(channel.Name, Constant.Media.Live.ProgramDrmSuffix);
                asset             = _media.Assets.Create(assetName, AssetCreationOptions.StorageEncrypted);
                contentProtection = new ContentProtection()
                {
                    ContentAuthTypeToken = true,
                    DrmPlayReady         = true,
                    DrmWidevine          = true
                };
                AddDeliveryPolicies(asset, contentProtection);
                CreateProgram(channel, asset, archiveWindowMinutes);
            }
        }
Exemple #2
0
        private IContentKey[] GetContentKeys(ContentProtection contentProtection)
        {
            List <IContentKey> contentKeys = new List <IContentKey>();

            if (contentProtection.AES)
            {
                ContentKeyType keyType    = ContentKeyType.EnvelopeEncryption;
                string         keyName    = Constants.Media.ContentProtection.ContentKeyNameAes;
                IContentKey    contentKey = GetContentKey(keyType, keyName, contentProtection);
                contentKeys.Add(contentKey);
            }
            if (contentProtection.DRMPlayReady && contentProtection.DRMWidevine)
            {
                ContentKeyType keyType    = ContentKeyType.CommonEncryption;
                string         keyName    = Constants.Media.ContentProtection.ContentKeyNameDrmPlayReadyWidevine;
                IContentKey    contentKey = GetContentKey(keyType, keyName, contentProtection);
                contentKeys.Add(contentKey);
            }
            else if (contentProtection.DRMPlayReady)
            {
                ContentKeyType keyType    = ContentKeyType.CommonEncryption;
                string         keyName    = Constants.Media.ContentProtection.ContentKeyNameDrmPlayReady;
                IContentKey    contentKey = GetContentKey(keyType, keyName, contentProtection);
                contentKeys.Add(contentKey);
            }
            else if (contentProtection.DRMWidevine)
            {
                ContentKeyType keyType    = ContentKeyType.CommonEncryption;
                string         keyName    = Constants.Media.ContentProtection.ContentKeyNameDrmWidevine;
                IContentKey    contentKey = GetContentKey(keyType, keyName, contentProtection);
                contentKeys.Add(contentKey);
            }
            return(contentKeys.ToArray());
        }
Exemple #3
0
 private static void PublishAsset(MediaClient mediaClient, IAsset asset, ContentProtection contentProtection)
 {
     if (asset.IsStreamable)
     {
         string          locatorId   = null;
         LocatorType     locatorType = LocatorType.OnDemandOrigin;
         List <ILocator> locators    = asset.Locators.Where(l => l.Type == locatorType).ToList();
         foreach (ILocator locator in locators)
         {
             if (locatorId == null)
             {
                 locatorId = locator.Id;
             }
             locator.Delete();
         }
         List <IAssetDeliveryPolicy> deliveryPolicies = asset.DeliveryPolicies.ToList();
         foreach (IAssetDeliveryPolicy deliveryPolicy in deliveryPolicies)
         {
             asset.DeliveryPolicies.Remove(deliveryPolicy);
         }
         if (contentProtection != null)
         {
             mediaClient.AddDeliveryPolicies(asset, contentProtection);
         }
         mediaClient.CreateLocator(locatorId, locatorType, asset, false);
     }
 }
Exemple #4
0
        public static string PublishJob(MediaJobNotification jobNotification, bool webHook)
        {
            string jobPublication = string.Empty;

            if (jobNotification != null && jobNotification.EventType == MediaJobNotificationEvent.JobStateChange &&
                (jobNotification.Properties.NewState == JobState.Error ||
                 jobNotification.Properties.NewState == JobState.Canceled ||
                 jobNotification.Properties.NewState == JobState.Finished))
            {
                if (webHook)
                {
                    string        settingKey    = Constants.AppSettingKey.MediaJobNotificationStorageQueueName;
                    string        queueName     = AppSetting.GetValue(settingKey);
                    MessageClient messageClient = new MessageClient();
                    messageClient.AddMessage(queueName, jobNotification);
                }
                else
                {
                    EntityClient entityClient = new EntityClient();
                    string       tableName    = Constants.Storage.TableName.JobPublish;
                    string       partitionKey = jobNotification.Properties.AccountName;
                    string       rowKey       = jobNotification.Properties.JobId;
                    JobPublish   jobPublish   = entityClient.GetEntity <JobPublish>(tableName, partitionKey, rowKey);
                    if (jobPublish != null)
                    {
                        tableName = Constants.Storage.TableName.JobPublishProtection;
                        ContentProtection contentProtection = entityClient.GetEntity <ContentProtection>(tableName, partitionKey, rowKey);
                        string            accountName       = jobPublish.PartitionKey;
                        string            accountKey        = jobPublish.MediaAccountKey;
                        MediaClient       mediaClient       = new MediaClient(accountName, accountKey);
                        IJob job = mediaClient.GetEntityById(MediaEntity.Job, rowKey) as IJob;
                        if (job != null)
                        {
                            mediaClient.SetProcessorUnits(job, ReservedUnitType.Basic);
                            if (jobNotification.Properties.NewState == JobState.Finished)
                            {
                                PublishContent(mediaClient, job, jobPublish, contentProtection);
                            }
                            string messageText = GetNotificationMessage(accountName, job);
                            MessageClient.SendText(messageText, jobPublish.MobileNumber);
                        }
                        if (contentProtection != null)
                        {
                            tableName = Constants.Storage.TableName.JobPublishProtection;
                            entityClient.DeleteEntity(tableName, contentProtection);
                        }
                        tableName = Constants.Storage.TableName.JobPublish;
                        entityClient.DeleteEntity(tableName, jobPublish);
                    }
                }
            }
            return(jobPublication);
        }
Exemple #5
0
        private IContentKey GetContentKey(ContentKeyType keyType, string keyName, ContentProtection contentProtection)
        {
            IContentKey contentKey = GetEntityByName(MediaEntity.ContentKey, keyName, true) as IContentKey;

            if (contentKey == null)
            {
                Guid   keyId         = Guid.NewGuid();
                byte[] encryptionKey = CreateEncryptionKey();
                contentKey = _media.ContentKeys.Create(keyId, encryptionKey, keyName, keyType);
                SetContentKeyAuthPolicy(contentKey, contentProtection);
            }
            return(contentKey);
        }
 public void AddDeliveryPolicies(IAsset asset, ContentProtection contentProtection)
 {
     IContentKey[] contentKeys = GetContentKeys(contentProtection);
     foreach (IContentKey contentKey in contentKeys)
     {
         asset.ContentKeys.Add(contentKey);
     }
     IAssetDeliveryPolicy[] deliveryPolicies = GetDeliveryPolicies(contentProtection);
     foreach (IAssetDeliveryPolicy deliveryPolicy in deliveryPolicies)
     {
         asset.DeliveryPolicies.Add(deliveryPolicy);
     }
 }
Exemple #7
0
        private void SetContentKeyAuthPolicy(IContentKey contentKey, ContentProtection contentProtection)
        {
            string policyName = string.Concat(contentKey.Name, Constants.Media.ContentProtection.AuthPolicyName);
            IContentKeyAuthorizationPolicy authPolicy = GetEntityByName(MediaEntity.ContentKeyAuthPolicy, policyName, true) as IContentKeyAuthorizationPolicy;

            if (authPolicy == null)
            {
                authPolicy = CreateContentKeyAuthPolicy(policyName, contentKey, contentProtection);
            }
            if (contentKey.AuthorizationPolicyId != authPolicy.Id)
            {
                contentKey.AuthorizationPolicyId = authPolicy.Id;
                contentKey.Update();
            }
        }
Exemple #8
0
 internal static void PublishStream(MediaClient mediaClient, IAsset asset, ContentProtection contentProtection)
 {
     if (asset.IsStreamable || asset.AssetType == AssetType.MP4)
     {
         if (asset.Options == AssetCreationOptions.StorageEncrypted && asset.DeliveryPolicies.Count == 0)
         {
             mediaClient.AddDeliveryPolicies(asset, contentProtection);
         }
         if (asset.Locators.Count == 0)
         {
             LocatorType locatorType = LocatorType.OnDemandOrigin;
             mediaClient.CreateLocator(null, locatorType, asset, null);
         }
     }
 }
Exemple #9
0
 public void AddDeliveryPolicies(IAsset asset, ContentProtection contentProtection)
 {
     if (asset.Options == AssetCreationOptions.StorageEncrypted)
     {
         IContentKey[] contentKeys = GetContentKeys(contentProtection);
         foreach (IContentKey contentKey in contentKeys)
         {
             asset.ContentKeys.Add(contentKey);
         }
         IAssetDeliveryPolicy[] deliveryPolicies = GetDeliveryPolicies(contentProtection);
         foreach (IAssetDeliveryPolicy deliveryPolicy in deliveryPolicies)
         {
             asset.DeliveryPolicies.Add(deliveryPolicy);
         }
     }
 }
        public static ContentProtection[] GetContentProtections(string directoryId, IJob job, MediaJobTask[] jobTasks)
        {
            List <ContentProtection> contentProtections = new List <ContentProtection>();
            string settingKey = Constant.AppSettingKey.DirectoryClientId;

            settingKey = string.Format(settingKey, directoryId);
            string directoryClientId = AppSetting.GetValue(settingKey);

            for (int i = 0; i < job.Tasks.Count; i++)
            {
                if (jobTasks[i].ContentProtection != null)
                {
                    ContentProtection contentProtection = jobTasks[i].ContentProtection;
                    contentProtection.RowKey      = job.Tasks[i].Id;
                    contentProtection.DirectoryId = directoryId;
                    contentProtection.Audience    = directoryClientId;
                    contentProtections.Add(contentProtection);
                }
            }
            return(contentProtections.ToArray());
        }
Exemple #11
0
        public void CreateContentKeyPolicyDRM(ContentProtection contentProtection)
        {
            ContentKeyPolicyPlayReadyLicenseType licenseType = ContentKeyPolicyPlayReadyLicenseType.NonPersistent;

            if (contentProtection != null && contentProtection.PersistentLicense)
            {
                licenseType = ContentKeyPolicyPlayReadyLicenseType.Persistent;
            }
            ContentKeyPolicyPlayReadyLicense playReadyLicense = new ContentKeyPolicyPlayReadyLicense()
            {
                LicenseType        = licenseType,
                ContentType        = ContentKeyPolicyPlayReadyContentType.Unspecified,
                ContentKeyLocation = new ContentKeyPolicyPlayReadyContentEncryptionKeyFromHeader()
            };
            ContentKeyPolicyPlayReadyConfiguration playReadyConfiguration = new ContentKeyPolicyPlayReadyConfiguration()
            {
                Licenses = new ContentKeyPolicyPlayReadyLicense[] { playReadyLicense }
            };
            WidevineTemplate widevineTemplate = new WidevineTemplate();

            if (contentProtection != null && contentProtection.PersistentLicense)
            {
                widevineTemplate.PolicyOverrides = new PolicyOverrides()
                {
                    CanPersist = true
                };
            }
            ContentKeyPolicyWidevineConfiguration widevineConfiguration = new ContentKeyPolicyWidevineConfiguration()
            {
                WidevineTemplate = JsonConvert.SerializeObject(widevineTemplate)
            };
            string policyName = Constant.Media.ContentKey.PolicyDRM;

            ContentKeyPolicyConfiguration[] policyConfigurations = new ContentKeyPolicyConfiguration[]
            {
                playReadyConfiguration,
                widevineConfiguration
            };
            CreateContentKeyPolicy(policyName, policyConfigurations);
        }
Exemple #12
0
 private static void PublishJob(MediaClient mediaClient, IJob job, MediaPublish contentPublish)
 {
     ITask[] encoderTasks = GetEncoderTasks(job);
     if (encoderTasks.Length == 0)
     {
         foreach (IAsset inputAsset in job.InputMediaAssets)
         {
             PublishAnalytics(mediaClient, contentPublish, job, encoderTasks);
         }
     }
     else
     {
         foreach (ITask encoderTask in encoderTasks)
         {
             ContentProtection contentProtection = GetContentProtection(job.Id, encoderTask.Id);
             foreach (IAsset outputAsset in encoderTask.OutputAssets)
             {
                 PublishAsset(mediaClient, outputAsset, contentProtection);
                 PublishAnalytics(mediaClient, contentPublish, job, encoderTasks);
             }
         }
     }
 }
Exemple #13
0
        private static void PublishContent(MediaClient mediaClient, IJob job, JobPublish jobPublish, ContentProtection contentProtection)
        {
            string processorId1 = Constants.Media.ProcessorId.EncoderStandard;
            string processorId2 = Constants.Media.ProcessorId.EncoderPremium;
            string processorId3 = Constants.Media.ProcessorId.EncoderUltra;

            string[] processorIds = new string[] { processorId1, processorId2, processorId3 };
            ITask[]  jobTasks     = GetJobTasks(job, processorIds);
            foreach (ITask jobTask in jobTasks)
            {
                foreach (IAsset outputAsset in jobTask.OutputAssets)
                {
                    PublishStream(mediaClient, outputAsset, contentProtection);
                    PublishIndex(job, outputAsset, jobPublish);
                    PublishAnalytics(job, outputAsset, jobPublish);
                }
            }
        }
Exemple #14
0
        private List <ContentKeyAuthorizationPolicyRestriction> CreateContentKeyAuthPolicyRestrictions(string policyName, ContentProtection contentProtection)
        {
            List <ContentKeyAuthorizationPolicyRestriction> policyRestrictions = new List <ContentKeyAuthorizationPolicyRestriction>();

            if (contentProtection.ContentAuthTypeAddress)
            {
                ContentKeyAuthorizationPolicyRestriction policyRestriction = new ContentKeyAuthorizationPolicyRestriction();
                policyRestriction.Name = string.Concat(policyName, Constants.Media.ContentProtection.AuthPolicyAddressRestrictionName);
                policyRestriction.SetKeyRestrictionTypeValue(ContentKeyRestrictionType.IPRestricted);
                policyRestriction.Requirements = GetAddressRangeXml(contentProtection.ContentAuthAddressRange);
                policyRestrictions.Add(policyRestriction);
            }
            if (contentProtection.ContentAuthTypeToken)
            {
                string settingKey   = Constants.AppSettingKey.DirectoryDiscoveryUrl;
                string discoveryUrl = AppSetting.GetValue(settingKey);

                settingKey = Constants.AppSettingKey.DirectoryIssuerUrl;
                string issuerUrl = AppSetting.GetValue(settingKey);

                settingKey = Constants.AppSettingKey.DirectoryClientId;
                string clientId = AppSetting.GetValue(settingKey);

                TokenRestrictionTemplate tokenTemplate = new TokenRestrictionTemplate(TokenType.JWT);
                tokenTemplate.OpenIdConnectDiscoveryDocument = new OpenIdConnectDiscoveryDocument(discoveryUrl);
                tokenTemplate.Issuer   = issuerUrl;
                tokenTemplate.Audience = clientId;

                ContentKeyAuthorizationPolicyRestriction policyRestriction = new ContentKeyAuthorizationPolicyRestriction();
                policyRestriction.Name = string.Concat(policyName, Constants.Media.ContentProtection.AuthPolicyTokenRestrictionName);
                policyRestriction.SetKeyRestrictionTypeValue(ContentKeyRestrictionType.TokenRestricted);
                policyRestriction.Requirements = TokenRestrictionTemplateSerializer.Serialize(tokenTemplate);
                policyRestrictions.Add(policyRestriction);
            }
            if (policyRestrictions.Count == 0)
            {
                ContentKeyAuthorizationPolicyRestriction policyRestriction = new ContentKeyAuthorizationPolicyRestriction();
                policyRestriction.Name = string.Concat(policyName, Constants.Media.ContentProtection.AuthPolicyOpenRestrictionName);
                policyRestriction.SetKeyRestrictionTypeValue(ContentKeyRestrictionType.Open);
                policyRestrictions.Add(policyRestriction);
            }
            return(policyRestrictions);
        }
        public StreamingLocator CreateLocator(string locatorName, string assetName, string streamingPolicyName, ContentProtection contentProtection)
        {
            StreamingLocator locator = _media.StreamingLocators.Get(MediaAccount.ResourceGroupName, MediaAccount.Name, locatorName);

            if (locator == null)
            {
                string contentKeyPolicyName = null;
                if (streamingPolicyName == PredefinedStreamingPolicy.ClearKey)
                {
                    CreateContentKeyPolicyAES();
                    contentKeyPolicyName = Constant.Media.ContentKey.PolicyAES;
                }
                else if (streamingPolicyName == PredefinedStreamingPolicy.SecureStreaming)
                {
                    CreateContentKeyPolicyDRM(contentProtection);
                    contentKeyPolicyName = Constant.Media.ContentKey.PolicyDRM;
                }
                locator = new StreamingLocator(assetName, streamingPolicyName)
                {
                    DefaultContentKeyPolicyName = contentKeyPolicyName
                };
                locator = _media.StreamingLocators.Create(MediaAccount.ResourceGroupName, MediaAccount.Name, locatorName, locator);
            }
            return(locator);
        }
Exemple #16
0
        private IAssetDeliveryPolicy[] GetDeliveryPolicies(ContentProtection contentProtection)
        {
            List <IAssetDeliveryPolicy> deliveryPolicies = new List <IAssetDeliveryPolicy>();
            AssetDeliveryPolicyType     policyType       = AssetDeliveryPolicyType.NoDynamicEncryption;
            Dictionary <AssetDeliveryPolicyConfigurationKey, string> policyConfig = null;
            string policyName = Constants.Media.DeliveryPolicy.DecryptionStorage;

            if (contentProtection.AES)
            {
                policyType = AssetDeliveryPolicyType.DynamicEnvelopeEncryption;
                policyName = Constants.Media.DeliveryPolicy.EncryptionAes;
                ContentKeyType keyType        = ContentKeyType.EnvelopeEncryption;
                string         keyName        = Constants.Media.ContentProtection.ContentKeyNameAes;
                IContentKey    contentKey     = GetContentKey(keyType, keyName, contentProtection);
                Uri            keyDeliveryUrl = contentKey.GetKeyDeliveryUrl(ContentKeyDeliveryType.BaselineHttp);
                byte[]         encryptionIV   = CreateEncryptionKey();
                policyConfig = new Dictionary <AssetDeliveryPolicyConfigurationKey, string>();
                policyConfig.Add(AssetDeliveryPolicyConfigurationKey.EnvelopeKeyAcquisitionUrl, keyDeliveryUrl.ToString());
                policyConfig.Add(AssetDeliveryPolicyConfigurationKey.EnvelopeEncryptionIVAsBase64, Convert.ToBase64String(encryptionIV));
                IAssetDeliveryPolicy deliveryPolicy = GetDeliveryPolicy(policyType, policyConfig, policyName, ContentKeyDeliveryType.BaselineHttp);
                deliveryPolicies.Add(deliveryPolicy);
            }
            if (contentProtection.DRMPlayReady && contentProtection.DRMWidevine)
            {
                policyType = AssetDeliveryPolicyType.DynamicCommonEncryption;
                policyName = Constants.Media.DeliveryPolicy.EncryptionDrmPlayReadyWidevine;
                ContentKeyType keyType    = ContentKeyType.CommonEncryption;
                string         keyName    = Constants.Media.ContentProtection.ContentKeyNameDrmPlayReadyWidevine;
                IContentKey    contentKey = GetContentKey(keyType, keyName, contentProtection);
                policyConfig = new Dictionary <AssetDeliveryPolicyConfigurationKey, string>();
                Uri keyDeliveryUrl = contentKey.GetKeyDeliveryUrl(ContentKeyDeliveryType.PlayReadyLicense);
                policyConfig.Add(AssetDeliveryPolicyConfigurationKey.PlayReadyLicenseAcquisitionUrl, keyDeliveryUrl.ToString());
                keyDeliveryUrl = contentKey.GetKeyDeliveryUrl(ContentKeyDeliveryType.Widevine);
                policyConfig.Add(AssetDeliveryPolicyConfigurationKey.WidevineLicenseAcquisitionUrl, keyDeliveryUrl.ToString());
                IAssetDeliveryPolicy deliveryPolicy = GetDeliveryPolicy(policyType, policyConfig, policyName, ContentKeyDeliveryType.PlayReadyLicense);
                deliveryPolicies.Add(deliveryPolicy);
                deliveryPolicy = GetDeliveryPolicy(policyType, policyConfig, policyName, ContentKeyDeliveryType.Widevine);
                deliveryPolicies.Add(deliveryPolicy);
            }
            else if (contentProtection.DRMPlayReady)
            {
                policyType = AssetDeliveryPolicyType.DynamicCommonEncryption;
                policyName = Constants.Media.DeliveryPolicy.EncryptionDrmPlayReady;
                ContentKeyType keyType        = ContentKeyType.CommonEncryption;
                string         keyName        = Constants.Media.ContentProtection.ContentKeyNameDrmPlayReady;
                IContentKey    contentKey     = GetContentKey(keyType, keyName, contentProtection);
                Uri            keyDeliveryUrl = contentKey.GetKeyDeliveryUrl(ContentKeyDeliveryType.PlayReadyLicense);
                policyConfig = new Dictionary <AssetDeliveryPolicyConfigurationKey, string>();
                policyConfig.Add(AssetDeliveryPolicyConfigurationKey.PlayReadyLicenseAcquisitionUrl, keyDeliveryUrl.ToString());
                IAssetDeliveryPolicy deliveryPolicy = GetDeliveryPolicy(policyType, policyConfig, policyName, ContentKeyDeliveryType.PlayReadyLicense);
                deliveryPolicies.Add(deliveryPolicy);
            }
            else if (contentProtection.DRMWidevine)
            {
                policyType = AssetDeliveryPolicyType.DynamicCommonEncryption;
                policyName = Constants.Media.DeliveryPolicy.EncryptionDrmWidevine;
                ContentKeyType keyType        = ContentKeyType.CommonEncryption;
                string         keyName        = Constants.Media.ContentProtection.ContentKeyNameDrmWidevine;
                IContentKey    contentKey     = GetContentKey(keyType, keyName, contentProtection);
                Uri            keyDeliveryUrl = contentKey.GetKeyDeliveryUrl(ContentKeyDeliveryType.Widevine);
                policyConfig = new Dictionary <AssetDeliveryPolicyConfigurationKey, string>();
                policyConfig.Add(AssetDeliveryPolicyConfigurationKey.WidevineLicenseAcquisitionUrl, keyDeliveryUrl.ToString());
                IAssetDeliveryPolicy deliveryPolicy = GetDeliveryPolicy(policyType, policyConfig, policyName, ContentKeyDeliveryType.Widevine);
                deliveryPolicies.Add(deliveryPolicy);
            }
            return(deliveryPolicies.ToArray());
        }
        private List <ContentKeyAuthorizationPolicyRestriction> CreateContentKeyAuthPolicyRestrictions(string policyName, ContentProtection contentProtection)
        {
            List <ContentKeyAuthorizationPolicyRestriction> policyRestrictions = new List <ContentKeyAuthorizationPolicyRestriction>();

            if (contentProtection.ContentAuthTypeAddress)
            {
                ContentKeyAuthorizationPolicyRestriction policyRestriction = new ContentKeyAuthorizationPolicyRestriction();
                policyRestriction.Name = string.Concat(policyName, Constant.Media.ContentProtection.AuthPolicyAddressRestrictionName);
                policyRestriction.SetKeyRestrictionTypeValue(ContentKeyRestrictionType.IPRestricted);
                policyRestriction.Requirements = GetAddressRangeXml(contentProtection.ContentAuthAddressRange);
                policyRestrictions.Add(policyRestriction);
            }
            if (contentProtection.ContentAuthTypeToken)
            {
                string settingKey = Constant.AppSettingKey.DirectoryTenantId;
                settingKey = string.Format(settingKey, contentProtection.DirectoryId);
                string directoryTenantId = AppSetting.GetValue(settingKey);

                settingKey = Constant.AppSettingKey.DirectoryTenantDomain;
                settingKey = string.Format(settingKey, contentProtection.DirectoryId);
                string directoryTenantDomain = AppSetting.GetValue(settingKey);

                string discoveryUrl = Account.GetDiscoveryUrl(contentProtection.DirectoryId, directoryTenantDomain);
                if (string.Equals(contentProtection.DirectoryId, Constant.DirectoryService.B2C, StringComparison.OrdinalIgnoreCase))
                {
                    settingKey = Constant.AppSettingKey.DirectoryPolicyIdSignUpIn;
                    string policyIdSignUpIn = AppSetting.GetValue(settingKey);
                    discoveryUrl = string.Concat(discoveryUrl, "?p=", policyIdSignUpIn);
                }

                TokenRestrictionTemplate tokenTemplate = new TokenRestrictionTemplate(TokenType.JWT);
                tokenTemplate.OpenIdConnectDiscoveryDocument = new OpenIdConnectDiscoveryDocument(discoveryUrl);
                tokenTemplate.Issuer   = Account.GetIssuerUrl(contentProtection.DirectoryId, directoryTenantId);
                tokenTemplate.Audience = contentProtection.Audience;

                ContentKeyAuthorizationPolicyRestriction policyRestriction = new ContentKeyAuthorizationPolicyRestriction();
                policyRestriction.Name = string.Concat(policyName, Constant.Media.ContentProtection.AuthPolicyTokenRestrictionName);
                policyRestriction.SetKeyRestrictionTypeValue(ContentKeyRestrictionType.TokenRestricted);
                policyRestriction.Requirements = TokenRestrictionTemplateSerializer.Serialize(tokenTemplate);
                policyRestrictions.Add(policyRestriction);
            }
            if (policyRestrictions.Count == 0)
            {
                ContentKeyAuthorizationPolicyRestriction policyRestriction = new ContentKeyAuthorizationPolicyRestriction();
                policyRestriction.Name = string.Concat(policyName, Constant.Media.ContentProtection.AuthPolicyOpenRestrictionName);
                policyRestriction.SetKeyRestrictionTypeValue(ContentKeyRestrictionType.Open);
                policyRestrictions.Add(policyRestriction);
            }
            return(policyRestrictions);
        }
Exemple #18
0
        private IContentKeyAuthorizationPolicy CreateContentKeyAuthPolicy(string policyName, IContentKey contentKey, ContentProtection contentProtection)
        {
            IContentKeyAuthorizationPolicy authPolicy = _media.ContentKeyAuthorizationPolicies.CreateAsync(policyName).Result;
            List <ContentKeyAuthorizationPolicyRestriction> policyRestrictions = CreateContentKeyAuthPolicyRestrictions(policyName, contentProtection);

            switch (contentKey.ContentKeyType)
            {
            case ContentKeyType.EnvelopeEncryption:
                string policyOptionName = string.Concat(policyName, Constants.Media.ContentProtection.AuthPolicyOptionNameAes);
                IContentKeyAuthorizationPolicyOption policyOption = GetEntityByName(MediaEntity.ContentKeyAuthPolicyOption, policyOptionName, true) as IContentKeyAuthorizationPolicyOption;
                if (policyOption == null)
                {
                    ContentKeyDeliveryType deliveryType = ContentKeyDeliveryType.BaselineHttp;
                    string deliveryConfig = string.Empty;
                    policyOption = _media.ContentKeyAuthorizationPolicyOptions.Create(policyOptionName, deliveryType, policyRestrictions, deliveryConfig);
                }
                authPolicy.Options.Add(policyOption);
                break;

            case ContentKeyType.CommonEncryption:
                if (contentProtection.DRMPlayReady)
                {
                    policyOptionName = string.Concat(policyName, Constants.Media.ContentProtection.AuthPolicyOptionNameDrmPlayReady);
                    policyOption     = GetEntityByName(MediaEntity.ContentKeyAuthPolicyOption, policyOptionName, true) as IContentKeyAuthorizationPolicyOption;
                    if (policyOption == null)
                    {
                        PlayReadyLicenseResponseTemplate responseTemplate = new PlayReadyLicenseResponseTemplate();
                        PlayReadyLicenseTemplate         licenseTemplate  = new PlayReadyLicenseTemplate();
                        licenseTemplate.PlayRight.AllowPassingVideoContentToUnknownOutput = UnknownOutputPassingOption.NotAllowed;
                        licenseTemplate.LicenseType      = PlayReadyLicenseType.Nonpersistent;
                        licenseTemplate.AllowTestDevices = true;
                        responseTemplate.LicenseTemplates.Add(licenseTemplate);

                        ContentKeyDeliveryType deliveryType = ContentKeyDeliveryType.PlayReadyLicense;
                        string deliveryConfig = MediaServicesLicenseTemplateSerializer.Serialize(responseTemplate);
                        policyOption = _media.ContentKeyAuthorizationPolicyOptions.Create(policyOptionName, deliveryType, policyRestrictions, deliveryConfig);
                    }
                    authPolicy.Options.Add(policyOption);
                }
                if (contentProtection.DRMWidevine)
                {
                    policyOptionName = string.Concat(policyName, Constants.Media.ContentProtection.AuthPolicyOptionNameDrmWidevine);
                    policyOption     = GetEntityByName(MediaEntity.ContentKeyAuthPolicyOption, policyOptionName, true) as IContentKeyAuthorizationPolicyOption;
                    if (policyOption == null)
                    {
                        ContentKeySpecs contentKeySpecs = new ContentKeySpecs();
                        contentKeySpecs.required_output_protection      = new RequiredOutputProtection();
                        contentKeySpecs.required_output_protection.hdcp = Hdcp.HDCP_NONE;
                        contentKeySpecs.security_level = 1;
                        contentKeySpecs.track_type     = "SD";

                        WidevineMessage widevineMessage = new WidevineMessage();
                        widevineMessage.allowed_track_types = AllowedTrackTypes.SD_HD;
                        widevineMessage.content_key_specs   = new ContentKeySpecs[] { contentKeySpecs };
                        widevineMessage.policy_overrides    = new
                        {
                            can_play    = true,
                            can_renew   = true,
                            can_persist = false
                        };

                        ContentKeyDeliveryType deliveryType = ContentKeyDeliveryType.Widevine;
                        string deliveryConfig = JsonConvert.SerializeObject(widevineMessage);
                        policyOption = _media.ContentKeyAuthorizationPolicyOptions.Create(policyOptionName, deliveryType, policyRestrictions, deliveryConfig);
                    }
                    authPolicy.Options.Add(policyOption);
                }
                break;
            }
            return(authPolicy);
        }
        public StreamingLocator GetStreamingLocator(string locatorName, string assetName, string streamingPolicyName, ContentProtection contentProtection)
        {
            StreamingLocator streamingLocator = _media.StreamingLocators.Get(MediaAccount.ResourceGroupName, MediaAccount.Name, locatorName);

            if (streamingLocator == null)
            {
                string contentKeyPolicyName = null;
                if (string.IsNullOrEmpty(streamingPolicyName))
                {
                    streamingPolicyName = PredefinedStreamingPolicy.DownloadAndClearStreaming;
                }
                else if (streamingPolicyName == PredefinedStreamingPolicy.ClearKey)
                {
                    contentKeyPolicyName = Constant.Media.ContentKeyPolicy.Aes;
                    CreateContentKeyPolicyAes(contentKeyPolicyName);
                }
                else if (streamingPolicyName == PredefinedStreamingPolicy.MultiDrmStreaming)
                {
                    contentKeyPolicyName = Constant.Media.ContentKeyPolicy.Drm;
                    CreateContentKeyPolicyDrm(contentKeyPolicyName, contentProtection);
                }
                else if (streamingPolicyName == PredefinedStreamingPolicy.MultiDrmCencStreaming)
                {
                    contentKeyPolicyName = Constant.Media.ContentKeyPolicy.DrmCenc;
                    CreateContentKeyPolicyDrm(contentKeyPolicyName, contentProtection);
                }
                streamingLocator = new StreamingLocator(assetName, streamingPolicyName)
                {
                    DefaultContentKeyPolicyName = contentKeyPolicyName
                };
                streamingLocator = _media.StreamingLocators.Create(MediaAccount.ResourceGroupName, MediaAccount.Name, locatorName, streamingLocator);
            }
            return(streamingLocator);
        }