Esempio n. 1
0
        public static async Task <AssetEntry> GenerateAssetInformation(ConfigWrapper config,
                                                                       IAzureMediaServicesClient client, Asset asset, VodSemaphore semaphore, string contentId = null, string region = null)
        {
            var assetEntry = new AssetEntry()
            {
                AMSAccountName          = config.AccountName,
                Region                  = config.Region,
                ResourceGroup           = config.ResourceGroup,
                AssetStorageAccountName = asset?.StorageAccountName,
                AssetName               = asset.Name,
                Urn               = semaphore.Urn,
                Semaphore         = semaphore,
                StreamingLocators = new List <StreamingLocatorEntry>(),
                CreatedTime       = asset.Created.ToUniversalTime().ToString(AssetEntry.DateFormat),
                ContentId         = contentId,
            };

            var             urls = new List <OutputUrl>();
            string          streamingPolicyName = null;
            StreamingPolicy streamingPolicy     = null;

            var streamingLocatorsNames = client.Assets.ListStreamingLocators(config.ResourceGroup, config.AccountName, asset.Name).StreamingLocators.Select(l => l.Name);

            foreach (var locatorName in streamingLocatorsNames)
            {
                string           cenckeyId        = null;
                string           cbcskeyId        = null;
                StreamingLocator streamingLocator = null;

                if (locatorName != null)
                {
                    streamingLocator = client.StreamingLocators.Get(config.ResourceGroup,
                                                                    config.AccountName, locatorName);
                    if (streamingLocator != null)
                    {
                        streamingPolicyName = streamingLocator.StreamingPolicyName;

                        if (streamingLocator.ContentKeys
                            .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCenc)
                            .FirstOrDefault() != null && streamingLocator.ContentKeys
                            .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCbcs)
                            .FirstOrDefault() != null)
                        {
                            cenckeyId = streamingLocator.ContentKeys
                                        .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCenc)
                                        .FirstOrDefault().Id.ToString();
                            cbcskeyId = streamingLocator.ContentKeys
                                        .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCbcs)
                                        .FirstOrDefault().Id.ToString();
                        }


                        // let's get the manifest name
                        string manifestName        = null;
                        List <IListBlobItem> blobs = new List <IListBlobItem>();
                        try
                        {
                            ListContainerSasInput input = new ListContainerSasInput()
                            {
                                Permissions = AssetContainerPermission.Read,
                                ExpiryTime  = DateTime.Now.AddHours(2).ToUniversalTime()
                            };

                            var    responseListSas = client.Assets.ListContainerSas(config.ResourceGroup, config.AccountName, asset.Name, input.Permissions, input.ExpiryTime);
                            string uploadSasUrl    = responseListSas.AssetContainerSasUrls.First();

                            var sasUri    = new Uri(uploadSasUrl);
                            var container = new CloudBlobContainer(sasUri);
                            var blobsR    = await container.ListBlobsSegmentedAsync(null);

                            blobs = blobsR.Results.ToList();
                            // let's take the first manifest file. It should exist
                            manifestName = blobs.Where(b => (b.GetType() == typeof(CloudBlockBlob))).Select(b => (CloudBlockBlob)b).Where(b => b.Name.ToLower().EndsWith(".ism")).FirstOrDefault().Name;
                        }
                        catch
                        {
                        }
                        if (manifestName != null) // there is a manifest
                        {
                            urls = MediaServicesHelpers.GetUrls(config, client, streamingLocator, manifestName, true, true, true, true, true);
                        }
                        else // no manifest
                        {
                            urls = MediaServicesHelpers.GetDownloadUrls(config, client, streamingLocator, blobs);
                        }
                    }
                }

                if (streamingPolicyName != null)
                {
                    streamingPolicy = client.StreamingPolicies.Get(config.ResourceGroup, config.AccountName,
                                                                   streamingPolicyName);
                }

                var drmlist = new List <Drm>();
                if (streamingPolicy != null)
                {
                    if (streamingPolicy.CommonEncryptionCbcs != null)
                    {
                        var enProt =
                            MediaServicesHelpers.ReturnOutputProtocolsListCbcs(streamingPolicy
                                                                               .CommonEncryptionCbcs.EnabledProtocols);
                        drmlist.Add(new Drm
                        {
                            Type       = "FairPlay",
                            LicenseUrl =
                                streamingPolicy?.CommonEncryptionCbcs?.Drm.FairPlay
                                .CustomLicenseAcquisitionUrlTemplate.Replace("{ContentKeyId}", cbcskeyId).Replace("{AlternativeMediaId}", streamingLocator.AlternativeMediaId),
                            Protocols      = enProt,
                            CertificateUrl = config.IrdetoFairPlayCertificateUrl
                        });
                    }

                    if (streamingPolicy.CommonEncryptionCenc != null)
                    {
                        var enProtW =
                            MediaServicesHelpers.ReturnOutputProtocolsListCencWidevine(streamingPolicy
                                                                                       .CommonEncryptionCbcs.EnabledProtocols);
                        var enProtP =
                            MediaServicesHelpers.ReturnOutputProtocolsListCencPlayReady(streamingPolicy
                                                                                        .CommonEncryptionCbcs.EnabledProtocols);

                        drmlist.Add(new Drm
                        {
                            Type       = "PlayReady",
                            LicenseUrl = streamingPolicy?.CommonEncryptionCenc?.Drm.PlayReady
                                         .CustomLicenseAcquisitionUrlTemplate.Replace("{AlternativeMediaId}", streamingLocator.AlternativeMediaId),
                            Protocols = enProtP
                        });
                        drmlist.Add(new Drm
                        {
                            Type       = "Widevine",
                            LicenseUrl = streamingPolicy?.CommonEncryptionCenc?.Drm.Widevine
                                         .CustomLicenseAcquisitionUrlTemplate.Replace("{AlternativeMediaId}", streamingLocator.AlternativeMediaId),
                            Protocols = enProtW
                        });
                    }
                }

                var StreamingLocatorInfo = new StreamingLocatorEntry
                {
                    StreamingLocatorName = locatorName,
                    StreamingPolicyName  = streamingPolicyName,
                    CencKeyId            = cenckeyId,
                    CbcsKeyId            = cbcskeyId,
                    Drm  = drmlist,
                    Urls = urls.Select(url => new UrlEntry {
                        Protocol = url.Protocol.ToString(), Url = url.Url
                    })
                           .ToList()
                };

                assetEntry.StreamingLocators.Add(StreamingLocatorInfo);
            }

            return(assetEntry);
        }
Esempio n. 2
0
        // </CreateMediaServicesClient>


        public static List <OutputUrl> GetUrls(ConfigWrapper config,
                                               IAzureMediaServicesClient client,
                                               StreamingLocator locator,
                                               string manifestFileName,
                                               bool smoothStreaming = true,
                                               bool dashCsf         = true,
                                               bool hlsTs           = true,
                                               bool dashCmaf        = true,
                                               bool hlsCmaf         = true
                                               )
        {
            if (!manifestFileName.ToLower().EndsWith(".ism"))
            {
                manifestFileName = manifestFileName + ".ism";
            }

            var streamingEndpoints = client.StreamingEndpoints.List(config.ResourceGroup, config.AccountName);

            var encString   = "(encryption=cenc)";
            var encString2  = ",encryption=cenc";
            var cbcsString2 = ",encryption=cbcs-aapl";

            if (locator.StreamingPolicyName == PredefinedStreamingPolicy.ClearStreamingOnly || locator.StreamingPolicyName == PredefinedStreamingPolicy.DownloadAndClearStreaming)
            {
                encString   = "";
                encString2  = "";
                cbcsString2 = "";
            }


            // Get the URls to stream the output
            var urls = new List <OutputUrl>();

            foreach (var se in streamingEndpoints)
            {
                if (se.ResourceState == StreamingEndpointResourceState.Running)
                {
                    var uriBuilder = new UriBuilder();
                    uriBuilder.Scheme = "https";
                    uriBuilder.Host   = se.HostName;
                    uriBuilder.Path   = "/" + locator.StreamingLocatorId + "/" + manifestFileName + "/manifest";
                    var myPath = uriBuilder.ToString();
                    if (smoothStreaming)
                    {
                        urls.Add(new OutputUrl
                        {
                            Url      = myPath + encString,
                            Protocol = OutputProtocol.SmoothStreaming
                        });
                    }
                    if (dashCsf)
                    {
                        urls.Add(new OutputUrl
                        {
                            Url      = myPath + "(format=mpd-time-csf" + encString2 + ")",
                            Protocol = OutputProtocol.DashCsf
                        });
                    }
                    if (dashCmaf)
                    {
                        urls.Add(new OutputUrl
                        {
                            Url      = myPath + "(format=mpd-time-cmaf" + encString2 + ")",
                            Protocol = OutputProtocol.DashCmaf
                        });
                    }
                    if (hlsCmaf)
                    {
                        urls.Add(new OutputUrl
                        {
                            Url      = myPath + "(format=m3u8-cmaf" + cbcsString2 + ")",
                            Protocol = OutputProtocol.HlsCmaf
                        });
                    }
                    if (hlsTs)
                    {
                        urls.Add(new OutputUrl
                        {
                            Url      = myPath + "(format=m3u8-aapl" + cbcsString2 + ")",
                            Protocol = OutputProtocol.HlsTs
                        });
                    }
                }
            }

            return(urls);
        }
        public static GeneralOutputInfo GenerateOutputInformation(ConfigWrapper config,
                                                                  IAzureMediaServicesClient client, List <LiveEvent> liveEvents)
        {
            var generalOutputInfo = new GeneralOutputInfo {
                Success = true, LiveEvents = new List <LiveEventEntry>()
            };

            foreach (var liveEventenum in liveEvents)
            {
                var liveEvent =
                    client.LiveEvents.Get(config.ResourceGroup, config.AccountName,
                                          liveEventenum.Name); // we refresh the object

                var liveOutputs = client.LiveOutputs.List(config.ResourceGroup, config.AccountName, liveEvent.Name);

                // let's provide DASH format for preview
                var previewE = liveEvent.Preview.Endpoints
                               .Select(a => new PreviewInfo {
                    Protocol = a.Protocol, Url = a.Url
                }).ToList();

                /*
                 * // Code to expose Preview DASH and not Smooth
                 * if (previewE.Count == 1 && previewE[0].Protocol == "FragmentedMP4")
                 * {
                 *  previewE[0].Protocol = "DashCsf";
                 *  previewE[0].Url = previewE[0].Url + "(format=mpd-time-csf)";
                 * }
                 */


                // output info
                var liveEventInfo = new LiveEventEntry()
                {
                    LiveEventName     = liveEvent.Name,
                    UseStaticHostname = liveEvent.UseStaticHostname,
                    ResourceState     = liveEvent.ResourceState.ToString(),
                    Input             = liveEvent.Input.Endpoints
                                        .Select(endPoint => new UrlEntry {
                        Protocol = endPoint.Protocol, Url = endPoint.Url
                    }).ToList(),
                    InputACL = liveEvent.Input.AccessControl?.Ip.Allow
                               .Select(ip => ip.Address + "/" + ip.SubnetPrefixLength).ToList(),
                    Preview = previewE
                              .Select(endPoint => new UrlEntry {
                        Protocol = endPoint.Protocol, Url = endPoint.Url
                    }).ToList(),
                    PreviewACL = liveEvent.Preview.AccessControl?.Ip.Allow
                                 .Select(ip => ip.Address + "/" + ip.SubnetPrefixLength).ToList(),
                    LiveOutputs    = new List <LiveOutputEntry>(),
                    AMSAccountName = config.AccountName,
                    Region         = config.Region,
                    ResourceGroup  = config.ResourceGroup,
                    LowLatency     = liveEvent.StreamOptions?.Contains(StreamOptionsFlag.LowLatency)
                };
                generalOutputInfo.LiveEvents.Add(liveEventInfo);

                foreach (var liveOutput in liveOutputs)
                {
                    var             urls = new List <OutputUrl>();
                    string          streamingPolicyName = null;
                    StreamingPolicy streamingPolicy     = null;

                    var asset = client.Assets.Get(config.ResourceGroup, config.AccountName, liveOutput.AssetName);

                    // output info
                    var liveOutputInfo = new LiveOutputEntry
                    {
                        LiveOutputName          = liveOutput.Name,
                        ResourceState           = liveOutput.ResourceState,
                        ArchiveWindowLength     = (int)liveOutput.ArchiveWindowLength.TotalMinutes,
                        AssetName               = liveOutput.AssetName,
                        AssetStorageAccountName = asset?.StorageAccountName,
                        StreamingLocators       = new List <StreamingLocatorEntry>()
                    };
                    liveEventInfo.LiveOutputs.Add(liveOutputInfo);

                    var streamingLocatorsNames = client.Assets.ListStreamingLocators(config.ResourceGroup, config.AccountName, liveOutput.AssetName).StreamingLocators.Select(l => l.Name);
                    foreach (var locatorName in streamingLocatorsNames)
                    {
                        string           cenckeyId        = null;
                        string           cbcskeyId        = null;
                        StreamingLocator streamingLocator = null;

                        if (locatorName != null)
                        {
                            streamingLocator = client.StreamingLocators.Get(config.ResourceGroup,
                                                                            config.AccountName, locatorName);
                            if (streamingLocator != null)
                            {
                                streamingPolicyName = streamingLocator.StreamingPolicyName;

                                if (streamingLocator.ContentKeys
                                    .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCenc)
                                    .FirstOrDefault() != null && streamingLocator.ContentKeys
                                    .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCbcs)
                                    .FirstOrDefault() != null)
                                {
                                    cenckeyId = streamingLocator.ContentKeys
                                                .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCenc)
                                                .FirstOrDefault().Id.ToString();
                                    cbcskeyId = streamingLocator.ContentKeys
                                                .Where(k => k.LabelReferenceInStreamingPolicy == IrdetoHelpers.labelCbcs)
                                                .FirstOrDefault().Id.ToString();
                                }

                                urls = MediaServicesHelpers.GetUrls(config, client, streamingLocator,
                                                                    liveOutput.ManifestName, true, true, true, true, true);
                            }
                        }

                        if (streamingPolicyName != null)
                        {
                            streamingPolicy = client.StreamingPolicies.Get(config.ResourceGroup, config.AccountName,
                                                                           streamingPolicyName);
                        }

                        var drmlist = new List <Drm>();
                        if (streamingPolicy != null)
                        {
                            if (streamingPolicy.CommonEncryptionCbcs != null)
                            {
                                var enProt =
                                    MediaServicesHelpers.ReturnOutputProtocolsListCbcs(streamingPolicy
                                                                                       .CommonEncryptionCbcs.EnabledProtocols);
                                drmlist.Add(new Drm
                                {
                                    Type       = "FairPlay",
                                    LicenseUrl =
                                        streamingPolicy?.CommonEncryptionCbcs?.Drm.FairPlay
                                        .CustomLicenseAcquisitionUrlTemplate.Replace("{ContentKeyId}", cbcskeyId).Replace("{AlternativeMediaId}", streamingLocator.AlternativeMediaId),
                                    Protocols      = enProt,
                                    CertificateUrl = config.IrdetoFairPlayCertificateUrl
                                });
                            }

                            if (streamingPolicy.CommonEncryptionCenc != null)
                            {
                                var enProtW =
                                    MediaServicesHelpers.ReturnOutputProtocolsListCencWidevine(streamingPolicy
                                                                                               .CommonEncryptionCbcs.EnabledProtocols);
                                var enProtP =
                                    MediaServicesHelpers.ReturnOutputProtocolsListCencPlayReady(streamingPolicy
                                                                                                .CommonEncryptionCbcs.EnabledProtocols);

                                drmlist.Add(new Drm
                                {
                                    Type       = "PlayReady",
                                    LicenseUrl = streamingPolicy?.CommonEncryptionCenc?.Drm.PlayReady
                                                 .CustomLicenseAcquisitionUrlTemplate.Replace("{AlternativeMediaId}", streamingLocator.AlternativeMediaId),
                                    Protocols = enProtP
                                });
                                drmlist.Add(new Drm
                                {
                                    Type       = "Widevine",
                                    LicenseUrl = streamingPolicy?.CommonEncryptionCenc?.Drm.Widevine
                                                 .CustomLicenseAcquisitionUrlTemplate.Replace("{AlternativeMediaId}", streamingLocator.AlternativeMediaId),
                                    Protocols = enProtW
                                });
                            }
                        }

                        var StreamingLocatorInfo = new StreamingLocatorEntry
                        {
                            StreamingLocatorName = locatorName,
                            StreamingPolicyName  = streamingPolicyName,
                            CencKeyId            = cenckeyId,
                            CbcsKeyId            = cbcskeyId,
                            Drm  = drmlist,
                            Urls = urls.Select(url => new UrlEntry {
                                Protocol = url.Protocol.ToString(), Url = url.Url
                            })
                                   .ToList()
                        };

                        liveOutputInfo.StreamingLocators.Add(StreamingLocatorInfo);
                    }
                }
            }

            return(generalOutputInfo);
        }
Esempio n. 4
0
        // </GetCredentialsAsync>

        /// <summary>
        ///     Creates the AzureMediaServicesClient object based on the credentials
        ///     supplied in local configuration file.
        /// </summary>
        /// <param name="config">The parm is of type ConfigWrapper. This class reads values from local configuration file.</param>
        /// <returns></returns>
        // <CreateMediaServicesClient>
        public static async Task <IAzureMediaServicesClient> CreateMediaServicesClientAsync(ConfigWrapper config)
        {
            var credentials = await GetCredentialsAsync(config);

            return(new AzureMediaServicesClient(config.ArmEndpoint, credentials)
            {
                SubscriptionId = config.SubscriptionId
            });
        }