コード例 #1
0
 public SelectDeliveryPolicy(CloudMediaContext context, AssetDeliveryPolicyType delpoltype = AssetDeliveryPolicyType.None)
 {
     InitializeComponent();
     this.Icon   = Bitmaps.Azure_Explorer_ico;
     _context    = context;
     _delpoltype = delpoltype;
 }
        /// <summary>
        /// Asynchronously creates an <see cref="IAssetDeliveryPolicy"/>.
        /// </summary>
        /// <param name="name">Friendly name for the policy.</param>
        /// <param name="policyType">Type of the policy.</param>
        /// <param name="deliveryProtocol">Delivery protocol.</param>
        /// <param name="configuration">Configuration.</param>
        /// <returns>An <see cref="IAssetDeliveryPolicy"/>.</returns>
        public Task <IAssetDeliveryPolicy> CreateAsync(
            string name,
            AssetDeliveryPolicyType policyType,
            AssetDeliveryProtocol deliveryProtocol,
            Dictionary <AssetDeliveryPolicyConfigurationKey, string> configuration)
        {
            IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();
            var policy = new AssetDeliveryPolicyData
            {
                Name = name,
            };

            ((IAssetDeliveryPolicy)policy).AssetDeliveryPolicyType    = policyType;
            ((IAssetDeliveryPolicy)policy).AssetDeliveryProtocol      = deliveryProtocol;
            ((IAssetDeliveryPolicy)policy).AssetDeliveryConfiguration = configuration;

            policy.SetMediaContext(this.MediaContext);
            dataContext.AddObject(DeliveryPolicySet, policy);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(policy))
                   .ContinueWith <IAssetDeliveryPolicy>(
                       t =>
            {
                t.ThrowIfFaulted();

                return (AssetDeliveryPolicyData)t.Result.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
        private void buttonExistingDelPol_Click(object sender, EventArgs e)
        {
            AssetDeliveryPolicyType poltype = AssetDeliveryPolicyType.None;

            if (_form1.GetContentKeyType == ContentKeyType.CommonEncryption)
            {
                poltype = AssetDeliveryPolicyType.DynamicCommonEncryption;
            }
            else if (_form1.GetContentKeyType == ContentKeyType.CommonEncryptionCbcs)
            {
                poltype = AssetDeliveryPolicyType.DynamicCommonEncryptionCbcs;
            }
            else if (_form1.GetContentKeyType == ContentKeyType.EnvelopeEncryption)
            {
                poltype = AssetDeliveryPolicyType.DynamicEnvelopeEncryption;
            }

            var form = new SelectDeliveryPolicy(_context, poltype);

            if (form.ShowDialog() == DialogResult.OK)
            {
                var pol = form.SelectedPolicy;
                if (pol != null)
                {
                    _existingDeliveryPolicy = pol;
                    textBoxDelPolId.Text    = string.Format("{0} ({1})", pol.Name, pol.Id);
                    ValidateButtonOk();
                }
            }
        }
コード例 #4
0
        private IAssetDeliveryPolicy GetDeliveryPolicy(AssetDeliveryPolicyType policyType, Dictionary <AssetDeliveryPolicyConfigurationKey,
                                                                                                       string> policyConfig, string policyName, ContentKeyDeliveryType deliveryType)
        {
            IAssetDeliveryPolicy deliveryPolicy = GetEntityByName(MediaEntity.DeliveryPolicy, policyName, true) as IAssetDeliveryPolicy;

            if (deliveryPolicy == null)
            {
                AssetDeliveryProtocol policyProtocols = GetDeliveryProtocols(deliveryType);
                deliveryPolicy = _media.AssetDeliveryPolicies.Create(policyName, policyType, policyProtocols, policyConfig);
            }
            return(deliveryPolicy);
        }
        /// <summary>
        /// Creates a delivery policy.
        /// </summary>
        /// <param name="name">Friendly name for the policy.</param>
        /// <param name="policyType">Type of the policy.</param>
        /// <param name="deliveryProtocol">Delivery protocol.</param>
        /// <param name="configuration">Configuration.</param>
        /// <returns>An <see cref="IAssetDeliveryPolicy"/>.</returns>
        public IAssetDeliveryPolicy Create(
            string name,
            AssetDeliveryPolicyType policyType,
            AssetDeliveryProtocol deliveryProtocol,
            Dictionary <AssetDeliveryPolicyConfigurationKey, string> configuration)
        {
            try
            {
                Task <IAssetDeliveryPolicy> task = this.CreateAsync(name, policyType, deliveryProtocol, configuration);
                task.Wait();

                return(task.Result);
            }
            catch (AggregateException exception)
            {
                throw exception.InnerException;
            }
        }
        public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"AMS v2 Function - CreateAssetDeliveryPolicy was triggered!");

            string jsonContent = await req.Content.ReadAsStringAsync();

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            // Validate input objects
            if (data.assetDeliveryPolicyName == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetDeliveryPolicyName in the input object" }));
            }
            if (data.assetDeliveryPolicyType == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetDeliveryPolicyType in the input object" }));
            }
            if (data.assetDeliveryPolicyProtocol == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetDeliveryPolicyProtocol in the input object" }));
            }
            string assetDeliveryPolicyName = data.assetDeliveryPolicyName;
            string assetDeliveryPolicyType = data.assetDeliveryPolicyType;

            if (!MediaServicesHelper.AMSAssetDeliveryPolicyType.ContainsKey(assetDeliveryPolicyType))
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass a valid assetDeliveryPolicyType in the input object" }));
            }
            List <string> assetDeliveryPolicyProtocol = ((JArray)data.assetDeliveryPolicyProtocol).ToObject <List <string> >();

            foreach (var p in assetDeliveryPolicyProtocol)
            {
                if (!MediaServicesHelper.AMSAssetDeliveryProtocol.ContainsKey(p))
                {
                    return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass a valid assetDeliveryPolicyProtocol in the input object" }));
                }
            }
            List <string> assetDeliveryPolicyContentProtectionNames = null;
            string        fairPlayPolicyId = null;

            if (data.assetDeliveryPolicyContentProtection != null)
            {
                assetDeliveryPolicyContentProtectionNames = ((JArray)data.assetDeliveryPolicyContentProtection).ToObject <List <string> >();
                foreach (var p in assetDeliveryPolicyContentProtectionNames)
                {
                    if (!MediaServicesHelper.AMSAssetDeliveryContentProtection.ContainsKey(p))
                    {
                        return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass a valid assetDeliveryPolicyContentProtection in the input object" }));
                    }
                    if (MediaServicesHelper.AMSAssetDeliveryContentProtection[p] == MediaServicesHelper.AssetDeliveryContentProtection.FairPlay)
                    {
                        if (fairPlayPolicyId == null)
                        {
                            return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass fairPlayContentKeyAuthorizationPolicyOptionId in the input object" }));
                        }
                        fairPlayPolicyId = data.fairPlayContentKeyAuthorizationPolicyOptionId;
                    }
                }
            }


            MediaServicesCredentials amsCredentials = new MediaServicesCredentials();
            IAssetDeliveryPolicy     policy         = null;

            try
            {
                // Load AMS account context
                log.Info($"Using AMS v2 REST API Endpoint : {amsCredentials.AmsRestApiEndpoint.ToString()}");

                AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain,
                                                                                       new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret),
                                                                                       AzureEnvironments.AzureCloudEnvironment);
                AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials);
                _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider);

                AssetDeliveryPolicyType assetDeliveryPolicyTypeValue     = MediaServicesHelper.AMSAssetDeliveryPolicyType[assetDeliveryPolicyType];
                AssetDeliveryProtocol   assetDeliveryPolicyProtocolValue = AssetDeliveryProtocol.None;
                foreach (var p in assetDeliveryPolicyProtocol)
                {
                    assetDeliveryPolicyProtocolValue |= MediaServicesHelper.AMSAssetDeliveryProtocol[p];
                }
                Dictionary <Asset​Delivery​Policy​Configuration​Key, String> assetDeliveryPolicyConfigurationValue = null;
                if (assetDeliveryPolicyContentProtectionNames != null)
                {
                    IContentKeyAuthorizationPolicyOption fairplayPolicyOpiton = null;
                    if (fairPlayPolicyId != null)
                    {
                        fairplayPolicyOpiton = _context.ContentKeyAuthorizationPolicyOptions.Where(p => p.Id == fairPlayPolicyId).Single();
                        if (fairplayPolicyOpiton == null)
                        {
                            return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "FairPlay ContentKeyAuthorizationPolicyOption not found" }));
                        }
                    }
                    assetDeliveryPolicyConfigurationValue = CreateDeliveryPolicyConfiguration(assetDeliveryPolicyContentProtectionNames, fairplayPolicyOpiton);
                }
                policy = _context.AssetDeliveryPolicies.Create(assetDeliveryPolicyName,
                                                               assetDeliveryPolicyTypeValue, assetDeliveryPolicyProtocolValue, assetDeliveryPolicyConfigurationValue);
            }
            catch (Exception e)
            {
                log.Info($"ERROR: Exception {e}");
                return(req.CreateResponse(HttpStatusCode.BadRequest));
            }

            return(req.CreateResponse(HttpStatusCode.OK, new
            {
                assetDeliveryPolicyId = policy.Id
            }));
        }
        public void RunAllGetEffectiveDeliveryPolicyTestCases()
        {
            string testCaseDataFilePath = WindowsAzureMediaServicesTestConfiguration.GetVideoSampleFilePath(TestContext, c_TestCaseDataFile);

            string[] testCases = File.ReadAllLines(testCaseDataFilePath);
            Assert.IsNotNull(testCases);
            Assert.AreEqual(401, testCases.Length); // ensure we have the expected number of cases

            int failureCount = 0;

            StringBuilder builder = new StringBuilder();

            builder.Append(testCases[0]);
            builder.Append(",ActualAssetType,ActualIsStreamable,ActualEffectiveEncryptionState");
            builder.AppendLine();

            for (int i = 1; i < testCases.Length; i++)
            {
                string[]                parameters              = testCases[i].Split(',');
                AssetCreationOptions    options                 = (AssetCreationOptions)Enum.Parse(typeof(AssetCreationOptions), parameters[0]);
                AssetType               assetType               = (AssetType)Enum.Parse(typeof(AssetType), parameters[1]);
                AssetDeliveryProtocol   assetDeliveryProtocol   = (AssetDeliveryProtocol)Enum.Parse(typeof(AssetDeliveryProtocol), parameters[2]);
                AssetDeliveryPolicyType assetDeliveryPolicyType = (AssetDeliveryPolicyType)Enum.Parse(typeof(AssetDeliveryPolicyType), parameters[3]);
                AssetEncryptionState    expectedEncryptionState = (AssetEncryptionState)Enum.Parse(typeof(AssetEncryptionState), parameters[4]);
                bool      expectedIsStreamable = bool.Parse(parameters[5]);
                AssetType expectedAssetType    = (AssetType)Enum.Parse(typeof(AssetType), parameters[6]);

                IAsset asset = GetTestAsset(options, assetType, assetDeliveryProtocol, assetDeliveryPolicyType);

                AssetEncryptionState actualEncryptionState = asset.GetEncryptionState(assetDeliveryProtocol);

                if (false == ((expectedAssetType == asset.AssetType) &&
                              (expectedIsStreamable == asset.IsStreamable) &&
                              (expectedEncryptionState == actualEncryptionState)
                              )
                    )
                {
                    // We had a failure so increase our failed count and then save the details of the test case and where it failed
                    failureCount++;

                    builder.Append(testCases[i]);
                    builder.Append(",");
                    builder.Append(asset.AssetType.ToString());
                    builder.Append(",");
                    builder.Append(asset.IsStreamable.ToString());
                    builder.Append(",");
                    builder.Append(actualEncryptionState.ToString());
                    builder.AppendLine();
                }
            }

            if (failureCount > 0)
            {
                Assert.Fail("Some RunAllGetEffectiveDeliveryPolicyTestCases failed");

                // If there are a lot of failures the best way to debug then is to dump
                // failed test case input and output data to a csv file for more detailed
                // analysis
                //File.WriteAllText("output.csv", builder.ToString());
            }
        }
        private IAsset GetTestAsset(AssetCreationOptions options, AssetType assetType, AssetDeliveryProtocol protocol, AssetDeliveryPolicyType deliveryType)
        {
            IAsset asset = _mediaContext.Assets.Create("Test", options);

            AddTestAssetFiles(asset, assetType);

            AddTestDeliveryPolicies(asset, protocol, deliveryType);

            return(asset);
        }
 private void AddTestDeliveryPolicies(IAsset asset, AssetDeliveryProtocol protocol, AssetDeliveryPolicyType deliveryType)
 {
     if (deliveryType != AssetDeliveryPolicyType.None)
     {
         IAssetDeliveryPolicy policy = _mediaContext.AssetDeliveryPolicies.Create("Test Asset Delivery Policy", deliveryType, protocol, null);
         asset.DeliveryPolicies.Add(policy);
     }
 }
コード例 #10
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 IAsset GetTestAsset(AssetCreationOptions options, AssetType assetType, AssetDeliveryProtocol protocol, AssetDeliveryPolicyType deliveryType)
        {
            IAsset asset = _mediaContext.Assets.Create("Test", options);

            AddTestAssetFiles(asset, assetType);

            AddTestDeliveryPolicies(asset, protocol, deliveryType);

            return asset;
        }
 private void AddTestDeliveryPolicies(IAsset asset, AssetDeliveryProtocol protocol, AssetDeliveryPolicyType deliveryType)
 {
     if (deliveryType != AssetDeliveryPolicyType.None)
     {
         IAssetDeliveryPolicy policy = _mediaContext.AssetDeliveryPolicies.Create("Test Asset Delivery Policy", deliveryType, protocol, null);
         asset.DeliveryPolicies.Add(policy);
     }
 }