-
Notifications
You must be signed in to change notification settings - Fork 27
/
RtmpLiveEncoding.ts
482 lines (432 loc) · 17.2 KB
/
RtmpLiveEncoding.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import ConfigProvider from '../common/ConfigProvider';
import {join} from 'path';
import BitmovinApi, {
AacAudioConfiguration,
AclEntry,
AclPermission,
CodecConfiguration,
ConsoleLogger,
DashManifestDefault,
DashManifestDefaultVersion,
Encoding,
EncodingOutput,
Fmp4Muxing,
H264VideoConfiguration,
HlsManifestDefault,
HlsManifestDefaultVersion,
Input,
LiveAutoShutdownConfiguration,
LiveDashManifest,
LiveEncoding,
LiveHlsManifest,
MessageType,
MuxingStream,
Output,
PresetConfiguration,
RtmpInput,
S3Output,
StartLiveEncodingRequest,
Status,
Stream,
StreamInput,
StreamSelectionMode,
Task
} from '@bitmovin/api-sdk';
/**
* This example shows how to configure and start a live encoding using default DASH and HLS
* manifests. For more information see: https://bitmovin.com/live-encoding-live-streaming/
*
* <p>The following configuration parameters are expected:
*
* <ul>
* <li>BITMOVIN_API_KEY - Your API key for the Bitmovin API
* <li>BITMOVIN_TENANT_ORG_ID - (optional) The ID of the Organisation in which you want to perform the encoding.
* <li>S3_OUTPUT_BUCKET_NAME - The name of your S3 output bucket. Example: my-bucket-name
* <li>S3_OUTPUT_ACCESS_KEY - The access key of your S3 output bucket
* <li>S3_OUTPUT_SECRET_KEY - The secret key of your S3 output bucket
* <li>S3_OUTPUT_BASE_PATH - The base path on your S3 output bucket where content will be written.
* Example: /outputs
* </ul>
*
* <p>Configuration parameters will be retrieved from these sources in the listed order: *
*
* <ol>
* <li>command line arguments (eg BITMOVIN_API_KEY=xyz)
* <li>properties file located in the root folder of the JAVA examples at ./examples.properties
* (see examples.properties.template as reference)
* <li>environment variables
* <li>properties file located in the home folder at ~/.bitmovin/examples.properties (see
* examples.properties.template as reference)
* </ol>
*/
const exampleName = 'RtmpLiveEncoding';
const streamKey = 'myStreamKey';
/**
* Make sure to set the correct resolution of your input video, so the aspect ratio can be
* calculated.
*/
const inputVideoWidth = 1920;
const inputVideoHeight = 1080;
const aspectRatio = inputVideoWidth / inputVideoHeight;
const maxMinutesToWaitForLiveEncodingDetails = 5;
const maxMinutesToWaitForEncodingStatus = 5;
/**
* Automatically shutdown the live stream if there is no input anymore for a predefined number of seconds.
*/
const bytesReadTimeoutSeconds = 3600; // 1 hour
/**
* Automatically shutdown the live stream after a predefined runtime in minutes.
*/
const streamTimeoutMinutes = 12 * 60; // 12 hours
const configProvider: ConfigProvider = new ConfigProvider();
const bitmovinApi = new BitmovinApi({
apiKey: configProvider.getBitmovinApiKey(),
// uncomment the following line if you are working with a multi-tenant account
// tenantOrgId: configProvider.getBitmovinTenantOrgId(),
logger: new ConsoleLogger()
});
async function main() {
const encoding: Encoding = await createEncoding(exampleName, 'Live encoding with RTMP input');
const input: RtmpInput = await getRtmpInput();
const output = await createS3Output(
configProvider.getS3OutputBucketName(),
configProvider.getS3OutputAccessKey(),
configProvider.getS3OutputSecretKey()
);
const h264VideoConfiguration = await createH264VideoConfig(1080, 3000000);
const aacAudioConfiguration = await createAacAudioConfig(128000);
const videoStream = await createStream(encoding, input, 'live', h264VideoConfiguration);
const audioStream = await createStream(encoding, input, 'live', aacAudioConfiguration);
await createFmp4Muxing(encoding, output, `/video/${h264VideoConfiguration.height}p`, videoStream);
await createFmp4Muxing(encoding, output, `/audio/${aacAudioConfiguration.bitrate! / 1000}kbps`, audioStream);
const dashManifest: DashManifestDefault = await createDefaultDashManifest(encoding, output, '/');
const hlsManifest: HlsManifestDefault = await createDefaultHlsManifest(encoding, output, '/');
const liveDashManifest = new LiveDashManifest({
manifestId: dashManifest.id
});
const liveHlsManifest = new LiveHlsManifest({
manifestId: hlsManifest.id
});
/*
Setting the autoShutdownConfiguration is optional,
if omitted the live encoding will not shut down automatically.
*/
const autoShutdownConfiguration = new LiveAutoShutdownConfiguration({
bytesReadTimeoutSeconds: bytesReadTimeoutSeconds,
streamTimeoutMinutes: streamTimeoutMinutes
});
const startLiveEncodingRequest = new StartLiveEncodingRequest({
dashManifests: [liveDashManifest],
hlsManifests: [liveHlsManifest],
autoShutdownConfiguration: autoShutdownConfiguration,
streamKey: streamKey
});
await startLiveEncodingAndWaitUntilRunning(encoding, startLiveEncodingRequest);
const liveEncoding: LiveEncoding = await waitForLiveEncodingDetails(encoding);
console.log('Live encoding started successfully and is ready for streaming', liveEncoding);
}
/**
* Periodically checks the status of the encoding.
*
* <p>Note: You can also use our webhooks API instead of polling the status. For more information
* checkout the API spec:
* https://bitmovin.com/docs/encoding/api-reference/sections/notifications-webhooks
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/GetEncodingEncodingsStatusByEncodingId
*
* @param encoding The encoding that should have the expected status
* @param expectedStatus The expected status the provided encoding should have. See {@link Status}
*/
async function waitUntilEncodingIsInState(encoding: Encoding, expectedStatus: Status) {
const checkIntervalInSeconds = 5;
const maxAttempts = maxMinutesToWaitForEncodingStatus * (60 / checkIntervalInSeconds);
let attempt = 0;
let task: Task;
do {
task = await bitmovinApi.encoding.encodings.status(encoding.id!);
if (task.status === expectedStatus) {
return;
}
if (task.status === Status.ERROR) {
logTaskErrors(task);
throw new Error('Encoding failed');
}
console.log(
`Encoding status is ${task.status}. Waiting for status ${expectedStatus} (${attempt} / ${maxAttempts})`
);
await timeout(checkIntervalInSeconds * 1000);
} while (attempt++ < maxAttempts);
throw new Error(
`Encoding did not switch to state ${expectedStatus} within ${maxMinutesToWaitForEncodingStatus} minutes. Aborting.`
);
}
/**
* This method starts the live encoding
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsLiveStartByEncodingId
*
* @param encoding The encoding that should be started and checked until it is running
* @param startLiveEncodingRequest The request object that is sent with the start call
*/
async function startLiveEncodingAndWaitUntilRunning(
encoding: Encoding,
startLiveEncodingRequest: StartLiveEncodingRequest
) {
await bitmovinApi.encoding.encodings.live.start(encoding.id!, startLiveEncodingRequest);
return waitUntilEncodingIsInState(encoding, Status.RUNNING);
}
/**
* Tries to get the live details of the encoding. It could take a few minutes until this info is
* available.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/GetEncodingEncodingsLiveByEncodingId
*
* @param encoding The encoding for which the live encoding details should be retrieved
*/
async function waitForLiveEncodingDetails(encoding: Encoding): Promise<LiveEncoding> {
const timeoutIntervalSeconds = 5;
let retries = 0;
const maxRetries = (60 / timeoutIntervalSeconds) * maxMinutesToWaitForLiveEncodingDetails;
do {
try {
return await bitmovinApi.encoding.encodings.live.get(encoding.id!);
} catch (e) {
console.log(`Failed to fetch live encoding details. Retrying... ${retries} / ${maxRetries}`);
retries++;
await timeout(timeoutIntervalSeconds * 1000);
}
} while (retries < maxRetries);
throw new Error(`Live encoding details could not be fetched after ${maxMinutesToWaitForLiveEncodingDetails} minutes`);
}
/**
* Creates an Encoding object. This is the base object to configure your encoding.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodings
*
* @param name A name that will help you identify the encoding in our dashboard (required)
* @param description A description of the encoding (optional)
*/
function createEncoding(name: string, description: string): Promise<Encoding> {
const encoding = new Encoding({
name: name,
description: description
});
return bitmovinApi.encoding.encodings.create(encoding);
}
/**
* Creates a resource representing an AWS S3 cloud storage bucket to which generated content will
* be transferred. For alternative output methods see <a
* href="https://bitmovin.com/docs/encoding/articles/supported-input-output-storages">list of
* supported input and output storages</a>
*
* <p>The provided credentials need to allow <i>read</i>, <i>write</i> and <i>list</i> operations.
* <i>delete</i> should also be granted to allow overwriting of existings files. See <a
* href="https://bitmovin.com/docs/encoding/faqs/how-do-i-create-a-aws-s3-bucket-which-can-be-used-as-output-location">creating
* an S3 bucket and setting permissions</a> for further information
*
* <p>For reasons of simplicity, a new output resource is created on each execution of this
* example. In production use, this method should be replaced by a <a
* href="https://bitmovin.com/docs/encoding/api-reference/sections/outputs#/Encoding/GetEncodingOutputsS3">get
* call</a> retrieving an existing resource.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/outputs#/Encoding/PostEncodingOutputsS3
*
* @param bucketName The name of the S3 bucket
* @param accessKey The access key of your S3 account
* @param secretKey The secret key of your S3 account
*/
function createS3Output(bucketName: string, accessKey: string, secretKey: string): Promise<S3Output> {
const s3Output = new S3Output({
bucketName: bucketName,
accessKey: accessKey,
secretKey: secretKey
});
return bitmovinApi.encoding.outputs.s3.create(s3Output);
}
/**
* Retrieves the first RTMP input. This is an automatically generated resource and read-only.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/inputs#/Encoding/GetEncodingInputsRtmp
*/
async function getRtmpInput(): Promise<RtmpInput> {
const resp = await bitmovinApi.encoding.inputs.rtmp.list();
return resp.items![0];
}
/**
* Builds an EncodingOutput object which defines where the output content (e.g. of a muxing) will
* be written to. Public read permissions will be set for the files written, so they can be
* accessed easily via HTTP.
*
* @param output The output resource to be used by the EncodingOutput
* @param outputPath The path where the content will be written to
*/
function buildEncodingOutput(output: Output, outputPath: string): EncodingOutput {
const aclEntry = new AclEntry({
permission: AclPermission.PUBLIC_READ
});
return new EncodingOutput({
outputPath: buildAbsolutePath(outputPath),
outputId: output.id,
acl: [aclEntry]
});
}
/**
* Builds an absolute path by concatenating the S3_OUTPUT_BASE_PATH configuration parameter, the
* name of this example and the given relative path
*
* <p>e.g.: /s3/base/path/exampleName/relative/path
*
* @param relativePath The relative path that is concatenated
* @return The absolute path
*/
function buildAbsolutePath(relativePath: string): string {
return join(configProvider.getS3OutputBasePath(), exampleName, relativePath);
}
/**
* Creates a configuration for the H.264 video codec to be applied to video streams.
*
* <p>The output resolution is defined by setting the height to 1080 pixels. Width will be
* determined automatically to maintain the aspect ratio of your input video.
*
* <p>To keep things simple, we use a quality-optimized VoD preset configuration, which will apply
* proven settings for the codec. See <a
* href="https://bitmovin.com/docs/encoding/tutorials/how-to-optimize-your-h264-codec-configuration-for-different-use-cases">How
* to optimize your H264 codec configuration for different use-cases</a> for alternative presets.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsVideoH264
*/
function createH264VideoConfig(height: number, bitrate: number): Promise<H264VideoConfiguration> {
const config = new H264VideoConfiguration({
name: `H.264 ${height}p ${bitrate / (1000 * 1000)} Mbit/s`,
presetConfiguration: PresetConfiguration.LIVE_STANDARD,
height,
width: Math.ceil(aspectRatio * height),
bitrate
});
return bitmovinApi.encoding.configurations.video.h264.create(config);
}
/**
* Creates a configuration for the AAC audio codec to be applied to audio streams.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsAudioAac
*/
function createAacAudioConfig(bitrate): Promise<AacAudioConfiguration> {
const config = new AacAudioConfiguration({
name: `AAC ${bitrate / 1000} kbit/s`,
bitrate
});
return bitmovinApi.encoding.configurations.audio.aac.create(config);
}
/**
* Adds a video or audio stream to an encoding
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsStreamsByEncodingId
*
* @param encoding The encoding to which the stream will be added
* @param input The input resource providing the input file
* @param inputPath The path to the input file
* @param codecConfiguration The codec configuration to be applied to the stream
*/
function createStream(
encoding: Encoding,
input: Input,
inputPath: string,
codecConfiguration: CodecConfiguration
): Promise<Stream> {
const streamInput = new StreamInput({
inputId: input.id,
inputPath: inputPath,
selectionMode: StreamSelectionMode.AUTO,
});
const stream = new Stream({
inputStreams: [streamInput],
codecConfigId: codecConfiguration.id
});
return bitmovinApi.encoding.encodings.streams.create(encoding.id!, stream);
}
/**
* Creates a DASH default manifest that automatically includes all representations configured in
* the encoding.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsDash
*
* @param encoding The encoding for which the manifest should be generated
* @param output The output where the manifest should be written to
* @param outputPath The path to which the manifest should be written
*/
function createDefaultDashManifest(
encoding: Encoding,
output: Output,
outputPath: string
): Promise<DashManifestDefault> {
let dashManifestDefault = new DashManifestDefault({
encodingId: encoding.id,
manifestName: 'stream.mpd',
version: DashManifestDefaultVersion.V1,
outputs: [buildEncodingOutput(output, outputPath)]
});
return bitmovinApi.encoding.manifests.dash.default.create(dashManifestDefault);
}
/**
* Creates an HLS default manifest that automatically includes all representations configured in
* the encoding.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsHlsDefault
*
* @param encoding The encoding for which the manifest should be generated
* @param output The output to which the manifest should be written
* @param outputPath The path to which the manifest should be written
*/
function createDefaultHlsManifest(encoding: Encoding, output: Output, outputPath: string): Promise<HlsManifestDefault> {
let hlsManifestDefault = new HlsManifestDefault({
encodingId: encoding.id,
outputs: [buildEncodingOutput(output, outputPath)],
name: 'master.m3u8',
version: HlsManifestDefaultVersion.V1
});
return bitmovinApi.encoding.manifests.hls.default.create(hlsManifestDefault);
}
/**
* Creates a fragmented MP4 muxing. This will generate segments with a given segment length for
* adaptive streaming.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsMuxingsFmp4ByEncodingId
*
* @param encoding The encoding where to add the muxing to
* @param output The output that should be used for the muxing to write the segments to
* @param outputPath The output path where the fragmented segments will be written to
* @param stream The stream to be muxed
*/
function createFmp4Muxing(encoding: Encoding, output: Output, outputPath: string, stream: Stream): Promise<Fmp4Muxing> {
const muxingStream = new MuxingStream({
streamId: stream.id
});
const muxing = new Fmp4Muxing({
outputs: [buildEncodingOutput(output, outputPath)],
streams: [muxingStream],
segmentLength: 4
});
return bitmovinApi.encoding.encodings.muxings.fmp4.create(encoding.id!, muxing);
}
function logTaskErrors(task: Task): void {
if (task == undefined) {
return;
}
task.messages!.filter(msg => msg.type === MessageType.ERROR).forEach(msg => console.error(msg.text));
}
function timeout(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
main();