This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathSubmitEncodingJob.cs
208 lines (183 loc) · 8.21 KB
/
SubmitEncodingJob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Common_Utils;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Media.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Functions
{
public static class SubmitEncodingJob
{
/// <summary>
/// Data to pass as an input to the function
/// </summary>
private class RequestBodyModel
{
/// <summary>
/// Name of the asset to encode.
/// Mandatory, except if you provide inputUrl.
/// </summary>
[JsonProperty("inputAssetName")]
public string InputAssetName { get; set; }
/// <summary>
/// Input Url of the file to encode.
/// Example : "https://nimbuscdn-nimbuspm.streaming.mediaservices.windows.net/2b533311-b215-4409-80af-529c3e853622/Ignite-short.mp4"
/// Mandatory, except if you provide inputAssetName.
/// </summary>
[JsonProperty("inputUrl")]
public string InputUrl { get; set; }
/// <summary>
/// Name of the transform.
/// It will be created if it does not exist. In that case, please set the builtInPreset value to provide the Standard Encoder preset to use.
/// Mandatory.
/// </summary>
[JsonProperty("transformName")]
public string TransformName { get; set; }
/// <summary>
/// If transform does not exist, then the function creates it and use the provided built in preset.
/// Optional.
/// </summary>
[JsonProperty("builtInPreset")]
public string BuiltInPreset { get; set; }
/// <summary>
/// Name of the attached storage account to use for the output asset.
/// Optional.
/// </summary>
[JsonProperty("outputAssetStorageAccount")]
public string OutputAssetStorageAccount { get; set; }
}
/// <summary>
/// Data output by the function
/// </summary>
private class AnswerBodyModel
{
/// <summary>
/// Name of the output asset created.
/// </summary>
[JsonProperty("outputAssetName")]
public string OutputAssetName { get; set; }
/// <summary>
/// Name of the job created.
/// </summary>
[JsonProperty("jobName")]
public string JobName { get; set; }
}
/// <summary>
/// Function which submits an encoding job
/// </summary>
/// <param name="req"></param>
/// <param name="executionContext"></param>
/// <returns></returns>
[Function("SubmitEncodingJob")]
public static async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, FunctionContext executionContext)
{
var log = executionContext.GetLogger("SubmitEncodingJob");
log.LogInformation("C# HTTP trigger function processed a request.");
// Get request body data.
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var data = (RequestBodyModel)JsonConvert.DeserializeObject(requestBody, typeof(RequestBodyModel));
// Return bad request if input asset name is not passed in
if (data.InputAssetName == null && data.InputUrl == null)
{
return HttpRequest.ResponseBadRequest(req, "Please pass inputAssetName or inputUrl in the request body");
}
// Return bad request if input asset name is not passed in
if (data.TransformName == null)
{
return HttpRequest.ResponseBadRequest(req, "Please pass transformName in the request body");
}
ConfigWrapper config = ConfigUtils.GetConfig();
IAzureMediaServicesClient client;
try
{
client = await Authentication.CreateMediaServicesClientAsync(config);
log.LogInformation("AMS Client created.");
}
catch (Exception e)
{
if (e.Source.Contains("ActiveDirectory"))
{
log.LogError("TIP: Make sure that you have filled out the appsettings.json file before running this sample.");
}
log.LogError($"{e.Message}");
return HttpRequest.ResponseBadRequest(req, e.Message);
}
// Set the polling interval for long running operations to 2 seconds.
// The default value is 30 seconds for the .NET client SDK
client.LongRunningOperationRetryTimeout = 2;
// Creating a unique suffix so that we don't have name collisions if you run the sample
// multiple times without cleaning up.
string uniqueness = Guid.NewGuid().ToString().Substring(0, 13);
string jobName = $"job-{uniqueness}";
string outputAssetName = $"output-{uniqueness}";
Transform transform;
try
{
// Ensure that you have the encoding Transform. This is really a one time setup operation.
transform = await TransformUtils.CreateEncodingTransform(client, log, config.ResourceGroup, config.AccountName, data.TransformName, data.BuiltInPreset);
log.LogInformation("Transform retrieved.");
}
catch (ErrorResponseException ex)
{
return HttpRequest.ResponseBadRequest(req, LogUtils.LogError(log, ex, "Error when creating the transform."));
}
Asset outputAsset;
try
{
// Output from the job must be written to an Asset, so let's create one
outputAsset = await AssetUtils.CreateAssetAsync(client, log, config.ResourceGroup, config.AccountName, outputAssetName, data.OutputAssetStorageAccount);
log.LogInformation($"Output asset '{outputAssetName}' created.");
}
catch (ErrorResponseException ex)
{
return HttpRequest.ResponseBadRequest(req, LogUtils.LogError(log, ex, "Error when creating the output asset."));
}
// Job input prepration : asset or url
JobInput jobInput;
if (data.InputUrl != null)
{
jobInput = new JobInputHttp(files: new[] { data.InputUrl });
log.LogInformation("Input is a Url.");
}
else
{
jobInput = new JobInputAsset(assetName: data.InputAssetName);
log.LogInformation($"Input is asset '{data.InputAssetName}'.");
}
Job job;
try
{
// Job submission to Azure Media Services
job = await JobUtils.SubmitJobAsync(
client,
log,
config.ResourceGroup,
config.AccountName,
data.TransformName,
jobName,
jobInput,
outputAssetName
);
log.LogInformation($"Job '{jobName}' submitted.");
}
catch (ErrorResponseException ex)
{
return HttpRequest.ResponseBadRequest(req, LogUtils.LogError(log, ex, "Error when submitting the job."));
}
AnswerBodyModel dataOk = new()
{
OutputAssetName = outputAsset.Name,
JobName = job.Name
};
return HttpRequest.ResponseOk(req, dataOk, HttpStatusCode.Accepted);
}
}
}