示例#1
0
        public static string GeneratesEncoder([ActivityTrigger] InitialSetupResult initialSetupResult, TraceWriter log)
        {
            IJob job;

            // Step 1: Setting up queue, context and endpoint
            string endPointAddress = Guid.NewGuid().ToString();

            AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(_tenantDomain, new AzureAdClientSymmetricKey(_clientId, _clientSecret), AzureEnvironments.AzureCloudEnvironment);
            var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

            _context = new CloudMediaContext(new Uri(_restApiUrl), tokenProvider);

            // Create the queue that will be receiving the notification messages.
            _queue = MediaServices.CreateQueue(_storageConnection, endPointAddress);

            // Create the notification point that is mapped to the queue.
            _notificationEndPoint = _context.NotificationEndPoints.Create(Guid.NewGuid().ToString(), NotificationEndPointType.AzureQueue, endPointAddress);

            // Step 2: Creating the encoding job
            try
            {
                log.Info("Starting encoding job...");
                IMediaProcessor mediaProcessor = MediaServices.GetLatestMediaProcessorByName("Media Encoder Standard", _context);
                job = MediaServices.SubmitEncodingJobWithNotificationEndPoint(_context, mediaProcessor.Name, initialSetupResult, _notificationEndPoint);
                log.Info("Done. Encoding job successfuly scheduled.");

                log.Info("Waiting the encoding process get completed...");
                MediaServices.WaitForJobToReachedFinishedState(job.Id, _queue, log);
            }
            catch (Exception ex)
            {
                return(string.Empty);
            }

            log.Info("Done. Encoding completed.");

            // Step 3: Cleaning up temporary resources
            _queue.Delete();
            _notificationEndPoint.Delete();

            // Step 4: Returns the final result
            return(job.Id);
        }