Example #1
0
        static public PublishAssetOutput ConvertToPublishAssetOutput(string locatorName, string streamingUrlPrefx, ListPathsResponse paths)
        {
            PublishAssetOutput output = new PublishAssetOutput();

            output.locatorName              = locatorName;
            output.streamingUrl             = "";
            output.captionVttUrl            = "";
            output.annotationsJsonUrl       = "";
            output.contentModerationJsonUrl = "";
            output.facesJsonUrl             = "";
            output.insightsJsonUrl          = "";
            output.ocrJsonUrl = "";

            List <PublishStreamingUrls> psUrls = new List <PublishStreamingUrls>();

            foreach (var path in paths.StreamingPaths)
            {
                var s = new PublishStreamingUrls();
                s.streamingProtocol = path.StreamingProtocol;
                s.encryptionScheme  = path.EncryptionScheme;
                s.urls = new string[path.Paths.Count];
                for (int i = 0; i < path.Paths.Count; i++)
                {
                    s.urls[i] = "https://" + streamingUrlPrefx + path.Paths[i];
                }
                if (path.StreamingProtocol == "SmoothStreaming")
                {
                    output.streamingUrl = "https://" + streamingUrlPrefx + path.Paths[0];
                }
                psUrls.Add(s);
            }
            output.streamingUrls = psUrls.ToArray();

            List <string> dUrls = new List <string>();

            foreach (var path in paths.DownloadPaths)
            {
                dUrls.Add("https://" + streamingUrlPrefx + path);
                if (path.EndsWith("annotations.json"))
                {
                    output.annotationsJsonUrl = "https://" + streamingUrlPrefx + path;
                }
                if (path.EndsWith("contentmoderation.json"))
                {
                    output.contentModerationJsonUrl = "https://" + streamingUrlPrefx + path;
                }
                if (path.EndsWith("faces.json"))
                {
                    output.facesJsonUrl = "https://" + streamingUrlPrefx + path;
                }
                if (path.EndsWith("insights.json"))
                {
                    output.insightsJsonUrl = "https://" + streamingUrlPrefx + path;
                }
                if (path.EndsWith("transcript.vtt"))
                {
                    output.captionVttUrl = "https://" + streamingUrlPrefx + path;
                }
            }
            output.downloadUrls = dUrls.ToArray();

            return(output);
        }
        public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"AMS v3 Function - publish_asset was triggered!");

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

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            // Validate input objects
            if (data.publishAssetName == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass publishAssetName in the input object" }));
            }
            string publishAssetName = data.publishAssetName;
            PredefinedStreamingPolicy streamingPolicy = PredefinedStreamingPolicy.ClearStreamingOnly; // default

            if (data.streamingPolicy != null)
            {
                string streamingPolicyName = data.streamingPolicy;
                if (predefinedStreamingPolicy.ContainsKey(streamingPolicyName))
                {
                    streamingPolicy = predefinedStreamingPolicy[streamingPolicyName];
                }
            }
            string streamingEndpointName = "default"; // default

            if (data.streamingEndpointName != null)
            {
                streamingEndpointName = data.streamingEndpointName;
            }

            MediaServicesConfigWrapper amsconfig = new MediaServicesConfigWrapper();
            string             guid        = Guid.NewGuid().ToString();
            string             locatorName = "locator-" + guid;
            PublishAssetOutput output      = null;

            try
            {
                IAzureMediaServicesClient client  = CreateMediaServicesClient(amsconfig);
                StreamingLocator          locator =
                    client.StreamingLocators.Create(amsconfig.ResourceGroup,
                                                    amsconfig.AccountName,
                                                    locatorName,
                                                    new StreamingLocator()
                {
                    AssetName           = publishAssetName,
                    StreamingPolicyName = streamingPolicy,
                });

                string            streamingUrlPrefx = "";
                StreamingEndpoint streamingEndpoint = client.StreamingEndpoints.Get(amsconfig.ResourceGroup, amsconfig.AccountName, streamingEndpointName);
                if (streamingEndpoint != null)
                {
                    streamingUrlPrefx = streamingEndpoint.HostName;
                }
                ListPathsResponse paths = client.StreamingLocators.ListPaths(amsconfig.ResourceGroup, amsconfig.AccountName, locatorName);
                output = MediaServicesHelper.ConvertToPublishAssetOutput(locatorName, streamingUrlPrefx, paths);
            }
            catch (ApiErrorException e)
            {
                log.Info($"ERROR: AMS API call failed with error code: {e.Body.Error.Code} and message: {e.Body.Error.Message}");
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "AMS API call error: " + e.Message
                }));
            }

            return(req.CreateResponse(HttpStatusCode.OK, output));
        }