-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartMediaEncoderStandardJob.cs
106 lines (94 loc) · 4.02 KB
/
StartMediaEncoderStandardJob.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaServicesV2.Services.Encoding.Services.Media;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sample.AzFunction.Advanced.Models;
namespace Sample.AzFunction.Advanced
{
/// <summary>
/// Starts a MediaEncoderStandard Job using <see cref="IMediaServicesV2Encoder"/>, which uses RestSharp to call AMS V2 APIs.
/// </summary>
public class StartMediaEncoderStandardJob
{
private readonly IMediaServicesV2Encoder _mediaServicesV2Encoder;
/// <summary>
/// Initializes a new instance of the <see cref="StartMediaEncoderStandardJob"/> class.
/// </summary>
/// <param name="mediaServicesV2Encoder">Manages the Azure Media Services Encoder business logic.</param>
public StartMediaEncoderStandardJob(
IMediaServicesV2Encoder mediaServicesV2Encoder)
{
_mediaServicesV2Encoder = mediaServicesV2Encoder;
}
/// <summary>
/// Starts a job.
/// </summary>
/// <param name="req">The request that should be parsed for parameters.</param>
/// <param name="log">log.</param>
/// <returns>JobId.</returns>
[FunctionName("StartMediaEncoderStandardJob")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", "get", Route = null)] HttpRequest req,
ILogger log)
{
string jobId;
try
{
// TODO: Parse your arguments to build a job
var todo = req?.ContentLength;
string requestBody;
using (var sr = new StreamReader(req?.Body))
{
requestBody = await sr.ReadToEndAsync().ConfigureAwait(false);
}
var parameters = JsonConvert.DeserializeObject<EncodingFunctionInputDTO>(requestBody);
string presetName = parameters?.PresetName ?? throw new Exception("Property 'presetName' is missing in the json body.");
List<Uri> sourceUris = parameters?.Inputs?.Select(i => i.BlobUri).ToList() ?? throw new Exception("Property 'inputs' is missing in the json body.");
string outputAssetStorage = parameters?.OutputAssetStorage;
var operationContext = parameters?.OperationContext;
jobId = await _mediaServicesV2Encoder.EncodeCreateAsync(
presetName,
sourceUris,
outputAssetStorage,
operationContext).ConfigureAwait(false);
}
catch (Exception e)
{
return new BadRequestObjectResult(new { error = $"Failed.\n\nException.Message:\n{e.Message}\n\nInnerException.Message:\n{e.InnerException?.Message}" });
}
var msg = $"Started: {jobId}";
log.LogInformation(msg);
return new OkObjectResult(msg);
}
}
}
/*
Curl sample:
curl -X POST \
'https://deploymentname.azurewebsites.net/api/StartMediaEncoderStandardJob?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '
{
"presetName" :"SpriteOnlySetting",
"inputs": [
{
"blobUri": "https://storageacount.blob.core.windows.net/input/BigBuckBunny.mp4"
}
],
"operationContext": {
"test": "mediaServicesV2test01",
"processId": 1002
}
}
' -v
*/