-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
184 lines (159 loc) · 5.51 KB
/
index.js
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
const axios = require('axios');
const querystring = require('querystring');
const config = require('./config');
const REST = require('./rest');
/**
* variables used for requesting or for storing IDs
*/
let VARS = {
TenantID: "",
AzureADSTSEndpoint: "",
RESTAPIEndpoint: "",
ClientID: "",
ClientSecret: "",
AccessToken: "",
LastAssetId: "",
UploadAccessPolicyId: "",
ReadAccessPolicyId: "",
UploadURL: "",
MediaFileName: "",
LastChannelId: "",
MediaProcessorId: "nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56",
OutputAssetId: "",
StreamingUrl: ""
};
/**
* if env variables are used set them here
* else get variables from config
*/
function setupVariables () {
if (process.env.TenantID) {
VARS.TenantID = process.env.TenantID;
VARS.AzureADSTSEndpoint = process.env.AzureADSTSEndpoint;
VARS.RESTAPIEndpoint = process.env.RESTAPIEndpoint;
VARS.ClientID = process.env.ClientID;
VARS.ClientSecret = process.env.ClientSecret;
} else {
VARS.TenantID = config.TenantID;
VARS.AzureADSTSEndpoint = config.AzureADSTSEndpoint;
VARS.RESTAPIEndpoint = config.RESTAPIEndpoint;
VARS.ClientID = config.ClientID;
VARS.ClientSecret = config.ClientSecret;
}
}
/*
* adds a delay of provided miliseconds
*/
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
async function transcode (fileName, filePath) {
setupVariables();
try {
/*
* Retrieve Access Token
*/
let tokenResp = await REST.getAADAccessToken(VARS.AzureADSTSEndpoint, VARS.ClientID, VARS.ClientSecret);
VARS.AccessToken = tokenResp.access_token;
console.log('Fetched AccessToken!');
/*
* Access Policy with write permissions. Fetch the policy if one exists with the same name
* or create it
*/
let newPolicyName = 'TravUploadPolicy';
let aPolicy = await REST.fetchOrCreateAccessPolicy(VARS.RESTAPIEndpoint, VARS.AccessToken, newPolicyName, REST.WRITE_POLICY);
VARS.UploadAccessPolicyId = aPolicy.Id;
console.log('Fetched Access Policy: ', VARS.UploadAccessPolicyId);
/*
* Prefix assets with a name and suffix with current time
*/
let assetName = 'TravAsset_' + (new Date()).getTime();
let asset = await REST.createAsset(VARS.RESTAPIEndpoint, VARS.AccessToken, assetName);
console.log('Asset created: ', asset.Id, asset.Name);
VARS.LastAssetId = asset.Id;
/*
* SAS Locator for uploading
*/
const locator = await REST.fetchLocator(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.UploadAccessPolicyId, VARS.LastAssetId, REST.WRITE_LOCATOR);
console.log('Fetched locator: ', locator.Id);
/*
* Upload URL
*/
VARS.UploadURL = locator.BaseUri + '/' + fileName + locator.ContentAccessComponent;
console.log('UploadURL: ', VARS.UploadURL);
console.log('Uploading file: ', filePath, ' ...');
let isUploaded = await REST.uploadFile(VARS.UploadURL, filePath, fileName);
if (!isUploaded) {
console.log('File Upload ERRORED');
return null;
}
console.log('File uploaded!');
/*
* create file info
*/
let fileInfoCreated = await REST.createFileInfos(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.LastAssetId);
console.log('Created file infos: ', fileInfoCreated);
/*
* no need to request media processor each time. We already know the Id of Standard Encoding Media Processor
* nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56
*/
// let mediaProcessor = await REST.getMediaProcessor(VARS.RESTAPIEndpoint, VARS.AccessToken);
// console.log('Media Processor: ', mediaProcessor);
let encodingJob = await REST.createJob(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.LastAssetId, assetName, VARS.MediaProcessorId);
let jobId = encodingJob.d.Id;
let outputMediaAsset = encodingJob.d.OutputMediaAssets;
/*
* Format: of OutputMediaAssets:
OutputMediaAssets: {
__deferred: {
uri: '<JOB_URL>/OutputMediaAssets'
}
}
*/
let outputAssetUrl = outputMediaAsset.__deferred.uri;
console.log('Encoding job started: ', jobId);
/*
* Actively tracking job state
*/
let isDone = false;
do {
console.log('Waiting for few sec before checking job state...');
await delay(5000);
let jobState = await REST.monitorJob(VARS.RESTAPIEndpoint, VARS.AccessToken, jobId);
if (jobState.d.State == 3) {
console.log('Job done.');
isDone = true;
}
} while(!isDone);
/*
* Read Access Policy
*/
let readPolicyName = 'TravReadPolicy';
let readPolicy = await REST.fetchOrCreateAccessPolicy(VARS.RESTAPIEndpoint, VARS.AccessToken, readPolicyName, REST.READ_POLICY);
VARS.ReadAccessPolicyId = readPolicy.Id;
console.log('Read Access Policy: ', readPolicy.Name, readPolicy.Id);
let outputAsset = await REST.getOutputAsset(outputAssetUrl, VARS.AccessToken);
if (outputAsset.value && outputAsset.value.length > 0) {
outputAsset = outputAsset.value[0];
console.log('Output Asset: ', outputAsset.Id);
}
/*
* Streaming locator
*/
const streamingLocator = await REST.fetchLocator(VARS.RESTAPIEndpoint, VARS.AccessToken, VARS.ReadAccessPolicyId, outputAsset.Id, REST.READ_LOCATOR);
console.log('Streaming locator created: ', streamingLocator.Name, streamingLocator.Id);
console.log(streamingLocator);
/*
* return streaming path for DASH
*/
return streamingLocator.Path;
} catch (e) {
console.log('ERROR: ', e);
return null;
}
}
module.exports = {
transcode: transcode
}