Skip to content

Instantly share code, notes, and snippets.

@fabriciosanchez
Created October 31, 2019 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabriciosanchez/1aefc6260d55f36998193e6502645771 to your computer and use it in GitHub Desktop.
Save fabriciosanchez/1aefc6260d55f36998193e6502645771 to your computer and use it in GitHub Desktop.
[FunctionName("A_JobEncodingGenerator")]
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment