-
Notifications
You must be signed in to change notification settings - Fork 27
/
RtmpLiveHdEncoding.java
560 lines (499 loc) · 23.2 KB
/
RtmpLiveHdEncoding.java
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import com.bitmovin.api.sdk.BitmovinApi;
import com.bitmovin.api.sdk.common.BitmovinException;
import com.bitmovin.api.sdk.model.AacAudioConfiguration;
import com.bitmovin.api.sdk.model.AclEntry;
import com.bitmovin.api.sdk.model.AclPermission;
import com.bitmovin.api.sdk.model.CodecConfiguration;
import com.bitmovin.api.sdk.model.DashManifest;
import com.bitmovin.api.sdk.model.DashManifestDefault;
import com.bitmovin.api.sdk.model.DashManifestDefaultVersion;
import com.bitmovin.api.sdk.model.Encoding;
import com.bitmovin.api.sdk.model.EncodingOutput;
import com.bitmovin.api.sdk.model.Fmp4Muxing;
import com.bitmovin.api.sdk.model.H264VideoConfiguration;
import com.bitmovin.api.sdk.model.HlsManifest;
import com.bitmovin.api.sdk.model.HlsManifestDefault;
import com.bitmovin.api.sdk.model.HlsManifestDefaultVersion;
import com.bitmovin.api.sdk.model.Input;
import com.bitmovin.api.sdk.model.LiveAutoShutdownConfiguration;
import com.bitmovin.api.sdk.model.LiveDashManifest;
import com.bitmovin.api.sdk.model.LiveEncoding;
import com.bitmovin.api.sdk.model.LiveHlsManifest;
import com.bitmovin.api.sdk.model.ManifestGenerator;
import com.bitmovin.api.sdk.model.MuxingStream;
import com.bitmovin.api.sdk.model.Output;
import com.bitmovin.api.sdk.model.PresetConfiguration;
import com.bitmovin.api.sdk.model.RtmpInput;
import com.bitmovin.api.sdk.model.S3Output;
import com.bitmovin.api.sdk.model.StartLiveChannelEncodingRequest;
import com.bitmovin.api.sdk.model.Status;
import com.bitmovin.api.sdk.model.Stream;
import com.bitmovin.api.sdk.model.StreamInput;
import com.bitmovin.api.sdk.model.StreamSelectionMode;
import com.bitmovin.api.sdk.model.Task;
import common.ConfigProvider;
import feign.Logger.Level;
import feign.slf4j.Slf4jLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* 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>
*/
public class RtmpLiveHdEncoding {
private static final Logger logger = LoggerFactory.getLogger(RtmpLiveHdEncoding.class);
private static BitmovinApi bitmovinApi;
private static ConfigProvider configProvider;
private static int maxMinutesToWaitForLiveEncodingDetails = 5;
private static int maxMinutesToWaitForEncodingStatus = 5;
/** This list defines the video renditions that will be generated.
* Starting from encoder version <a href="https://developer.bitmovin.com/encoding/docs/encoder-21000-21490#21170">
* 2.117.0</a>, you don't need to set both width and height. If you set one, the other is calculated automatically.
* In this example, we've set only the height for each version.
*/
private static List<VideoConfig> videoProfile =
Arrays.asList(
new VideoConfig("H.264 480p live", 800_000L, 480, "/video/480p"),
new VideoConfig("H.264 720p live", 1_200_000L, 720, "/video/720p"),
new VideoConfig("H.264 1080p live", 3_000_000L, 1080, "/video/1080p"));
/** This list defines the audio renditions that will be generated */
private static List<AudioConfig> audioProfile =
Collections.singletonList(new AudioConfig("AAC 128 kbit/s", 128_000L, "/audio/128kb"));
/**
* Automatically shutdown the live stream if there is no input anymore for a predefined number of seconds.
*/
private static long bytesReadTimeoutSeconds = 3600; // 1 hour
/**
* Automatically shutdown the live stream after a predefined runtime in minutes.
*/
private static long streamTimeoutMinutes = 12 * 60; // 12 hours
private static String streamKey = "bitmovin";
public static void main(String[] args) throws Exception {
configProvider = new ConfigProvider(args);
bitmovinApi =
BitmovinApi.builder()
.withApiKey(configProvider.getBitmovinApiKey())
// uncomment the following line if you are working with a multi-tenant account
.withTenantOrgId(configProvider.getBitmovinTenantOrgId())
.withLogger(
new Slf4jLogger(), Level.BASIC) // set the logger and log level for the API client
.build();
Encoding encoding =
createEncoding("Live Encoding HD Test", "Live encoding with HLS and DASH manifest");
RtmpInput input = getRtmpInput();
Output output =
createS3Output(
configProvider.getS3OutputBucketName(),
configProvider.getS3OutputAccessKey(),
configProvider.getS3OutputSecretKey());
for (VideoConfig videoConfig : videoProfile) {
H264VideoConfiguration h264Config =
createH264VideoConfig(videoConfig.name, videoConfig.height, videoConfig.bitRate);
Stream stream = createStream(encoding, input, h264Config);
createFmp4Muxing(encoding, stream, output, videoConfig.outputPath);
}
for (AudioConfig audioConfig : audioProfile) {
AacAudioConfiguration aacConfig = createAacAudioConfig(audioConfig.name, audioConfig.bitrate);
Stream audioStream = createStream(encoding, input, aacConfig);
createFmp4Muxing(encoding, audioStream, output, audioConfig.outputPath);
}
DashManifest dashManifest = createDefaultDashManifest(output, "/", encoding);
HlsManifest hlsManifest = createDefaultHlsManifest(output, "/", encoding);
LiveDashManifest liveDashManifest = new LiveDashManifest();
liveDashManifest.setManifestId(dashManifest.getId());
LiveHlsManifest liveHlsManifest = new LiveHlsManifest();
liveHlsManifest.setManifestId(hlsManifest.getId());
StartLiveChannelEncodingRequest startRequest = new StartLiveChannelEncodingRequest();
startRequest.addDashManifestsItem(liveDashManifest);
startRequest.addHlsManifestsItem(liveHlsManifest);
startRequest.setManifestGenerator(ManifestGenerator.V2);
startRequest.setStreamKey(streamKey);
/*
Setting the autoShutdownConfiguration is optional,
if omitted the live encoding will not shut down automatically.
*/
LiveAutoShutdownConfiguration autoShutdownConfiguration = new LiveAutoShutdownConfiguration();
autoShutdownConfiguration.setBytesReadTimeoutSeconds(bytesReadTimeoutSeconds);
autoShutdownConfiguration.setStreamTimeoutMinutes(streamTimeoutMinutes);
startRequest.setAutoShutdownConfiguration(autoShutdownConfiguration);
startLiveEncodingAndWaitUntilRunning(encoding, startRequest);
LiveEncoding liveEncoding = waitForLiveEncodingDetails(encoding);
logger.info(
"Live encoding is up and ready for ingest. RTMP URL: rtmp://{}/live StreamKey: {}",
liveEncoding.getEncoderIp(),
liveEncoding.getStreamKey());
/*
This will enable you to shut down the live encoding from within your script.
In production, it is naturally recommended to stop the encoding by using the Bitmovin dashboard
or an independent API call - https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsLiveStopByEncodingId
*/
Scanner scanner = new Scanner(System.in);
logger.info("Press Enter to shutdown the live encoding...");
scanner.nextLine();
logger.info("Shutting down live encoding!");
bitmovinApi.encoding.encodings.live.stop(encoding.getId());
waitUntilEncodingIsInState(encoding, Status.FINISHED);
}
/**
* Tries to get the live details of the encoding. It could take a few minutes until this info is
* available.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/getencodingencodingslivebyencodingid
*
* @param encoding The encoding for which the live encoding details should be retrieved
*/
private static LiveEncoding waitForLiveEncodingDetails(Encoding encoding)
throws InterruptedException {
logger.info(
"Waiting until live encoding details are available (max {} minutes) ...",
maxMinutesToWaitForLiveEncodingDetails);
int checkIntervalInSeconds = 10;
int maxAttempts = maxMinutesToWaitForLiveEncodingDetails * (60 / checkIntervalInSeconds);
int attempt = 0;
BitmovinException bitmovinException;
do {
try {
return bitmovinApi.encoding.encodings.live.get(encoding.getId());
} catch (BitmovinException e) {
attempt++;
bitmovinException = e;
Thread.sleep(checkIntervalInSeconds * (long) 1000);
}
} while (attempt < maxAttempts);
throw new Error(
String.format(
"Failed to retrieve live encoding details within %d minutes. Aborting.",
maxMinutesToWaitForLiveEncodingDetails),
bitmovinException);
}
/**
* 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://developer.bitmovin.com/encoding/reference/getencodingencodingsstatusbyencodingid
*
* @param encoding The encoding that should have the expected status
* @param expectedStatus The expected status the provided encoding should have. See {@link Status}
*/
private static void waitUntilEncodingIsInState(Encoding encoding, Status expectedStatus)
throws InterruptedException, BitmovinException {
logger.info(
"Waiting for encoding to have status {} (max {} minutes) ...",
expectedStatus,
maxMinutesToWaitForEncodingStatus);
int checkIntervalInSeconds = 10;
int maxAttempts = maxMinutesToWaitForEncodingStatus * (60 / checkIntervalInSeconds);
int attempt = 0;
Task task;
do {
task = bitmovinApi.encoding.encodings.status(encoding.getId());
logger.info("Encoding with id {} has status: {}", encoding.getId(), task.getStatus());
if (task.getStatus() == Status.ERROR) {
throw new Error(
String.format(
"Error while waiting for encoding with ID %s to have the status %s",
encoding.getId(), expectedStatus));
}
if (task.getStatus() == expectedStatus) {
return;
}
Thread.sleep(checkIntervalInSeconds * (long) 1000);
} while (attempt++ < maxAttempts);
throw new Error(
String.format(
"Live encoding did not switch to state %s within %d minutes. Aborting.",
expectedStatus, maxMinutesToWaitForEncodingStatus));
}
/**
* This method starts the live encoding with HD option
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/postencodingencodingslivechannelstartbyencodingid
*
* @param encoding The encoding that should be started and checked until it is running
* @param startEncodingRequest The request object that is sent with the start call
*/
private static void startLiveEncodingAndWaitUntilRunning(
Encoding encoding, StartLiveChannelEncodingRequest startEncodingRequest)
throws InterruptedException, BitmovinException {
bitmovinApi.encoding.encodings.live.hd.start(encoding.getId(), startEncodingRequest);
waitUntilEncodingIsInState(encoding, Status.RUNNING);
}
/**
* Creates a default DASH manifest that automatically includes all the representations configured.
* in the encoding.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/postencodingmanifestsdashdefault
*
* @param output The output to which the manifest should be written
* @param outputPath The path where the generated manifest should be located
* @param encoding The encoding for which the manifest should be generated
*/
private static DashManifestDefault createDefaultDashManifest(
Output output, String outputPath, Encoding encoding) {
DashManifestDefault dashManifestDefault = new DashManifestDefault();
dashManifestDefault.setEncodingId(encoding.getId());
dashManifestDefault.setManifestName("stream.mpd");
dashManifestDefault.setVersion(DashManifestDefaultVersion.V1);
dashManifestDefault.addOutputsItem(buildEncodingOutput(output, outputPath));
return bitmovinApi.encoding.manifests.dash.defaultapi.create(dashManifestDefault);
}
/**
* Creates a default HLS manifest that automatically includes all representations configured in.
* the encoding.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/postencodingmanifestshlsdefault
*
* @param output The output to which the manifest should be written
* @param outputPath The path where the generated manifest should be located
* @param encoding The encoding for which the manifest should be generated
*/
private static HlsManifestDefault createDefaultHlsManifest(
Output output, String outputPath, Encoding encoding) {
HlsManifestDefault hlsManifestDefault = new HlsManifestDefault();
hlsManifestDefault.setEncodingId(encoding.getId());
hlsManifestDefault.addOutputsItem(buildEncodingOutput(output, outputPath));
hlsManifestDefault.setName("master.m3u8");
hlsManifestDefault.setVersion(HlsManifestDefaultVersion.V1);
return bitmovinApi.encoding.manifests.hls.defaultapi.create(hlsManifestDefault);
}
/**
* Builds an absolute path by concatenating the S3_OUTPUT_BASE_PATH configuration parameter, the
* name of this example class and the given relative path
*
* <p>e.g.: /s3/base/path/ClassName/relative/path
*
* @param relativePath The relative path that is concatenated
* @return The absolute path
*/
public static String buildAbsolutePath(String relativePath) {
String className = RtmpLiveHdEncoding.class.getSimpleName();
return Paths.get(configProvider.getS3OutputBasePath(), className, relativePath).toString();
}
/**
* Creates a fragmented MP4 muxing. This will generate segments with a given segment length for
* adaptive streaming.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/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 that is associated with the muxing
*/
private static void createFmp4Muxing(
Encoding encoding, Stream stream, Output output, String outputPath) throws BitmovinException {
MuxingStream muxingStream = new MuxingStream();
muxingStream.setStreamId(stream.getId());
Fmp4Muxing muxing = new Fmp4Muxing();
muxing.addOutputsItem(buildEncodingOutput(output, outputPath));
muxing.addStreamsItem(muxingStream);
muxing.setSegmentLength(4.0);
bitmovinApi.encoding.encodings.muxings.fmp4.create(encoding.getId(), muxing);
}
/**
* 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
*/
private static EncodingOutput buildEncodingOutput(Output output, String outputPath) {
AclEntry aclEntry = new AclEntry();
aclEntry.setPermission(AclPermission.PUBLIC_READ);
EncodingOutput encodingOutput = new EncodingOutput();
encodingOutput.setOutputPath(buildAbsolutePath(outputPath));
encodingOutput.setOutputId(output.getId());
encodingOutput.addAclItem(aclEntry);
return encodingOutput;
}
/**
* Retrieves the first RTMP input. This is an automatically generated resource and read-only.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/getencodinginputsrtmpbyinputid
*/
private static RtmpInput getRtmpInput() throws BitmovinException {
return bitmovinApi.encoding.inputs.rtmp.list().getItems().get(0);
}
/**
* 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://developer.bitmovin.com/encoding/reference/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
*/
private static S3Output createS3Output(String bucketName, String accessKey, String secretKey)
throws BitmovinException {
S3Output s3Output = new S3Output();
s3Output.setBucketName(bucketName);
s3Output.setAccessKey(accessKey);
s3Output.setSecretKey(secretKey);
return bitmovinApi.encoding.outputs.s3.create(s3Output);
}
/**
* Creates an Encoding object. This is the base object to configure your encoding.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/postencodingencodings
*
* @param name This is the name of the encoding
* @param description This is the description of the encoding
*/
private static Encoding createEncoding(String name, String description) throws BitmovinException {
Encoding encoding = new Encoding();
encoding.setName(name);
encoding.setDescription(description);
return bitmovinApi.encoding.encodings.create(encoding);
}
/**
* Creates a stream which binds an input file to a codec configuration. The stream is used later
* for muxings. For RTMP live inputs, the input path should be the application name and the
* position of the input streams must be provided.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/postencodingencodingsstreamsbyencodingid
*
* @param encoding The encoding where to add the stream to
* @param codecConfiguration The codec configuration to be applied to the stream
*/
private static Stream createStream(
Encoding encoding, Input input, CodecConfiguration codecConfiguration)
throws BitmovinException {
StreamInput streamInput = new StreamInput();
streamInput.setInputId(input.getId());
streamInput.setInputPath("live");
streamInput.setSelectionMode(StreamSelectionMode.AUTO);
Stream stream = new Stream();
stream.addInputStreamsItem(streamInput);
stream.setCodecConfigId(codecConfiguration.getId());
return bitmovinApi.encoding.encodings.streams.create(encoding.getId(), stream);
}
/**
* Creates a configuration for the H.264 video codec to be applied to video streams.
*
* <p>API endpoint:
* https://developer.bitmovin.com/encoding/reference/postencodingconfigurationsvideoh264
*
* To keep things simple, we use a standard live preset configuration, which will
* apply proven settings for the codec. See <a
* href="https://developer.bitmovin.com/encoding/docs/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.
*
* @param name The name of the configuration resource being created
* @param height The height of the output video
* @param bitrate The target bitrate of the output video
*/
private static H264VideoConfiguration createH264VideoConfig(String name, int height, long bitrate)
throws BitmovinException {
H264VideoConfiguration config = new H264VideoConfiguration();
config.setName(name);
config.setPresetConfiguration(PresetConfiguration.LIVE_STANDARD);
config.setHeight(height);
config.setBitrate(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://developer.bitmovin.com/encoding/reference/postencodingconfigurationsaudioaac
*
* @param name The name of the configuration resource being created
* @param bitrate The target bitrate for the encoded audio
*/
private static AacAudioConfiguration createAacAudioConfig(String name, long bitrate) throws BitmovinException {
AacAudioConfiguration config = new AacAudioConfiguration();
config.setName(name);
config.setBitrate(bitrate);
return bitmovinApi.encoding.configurations.audio.aac.create(config);
}
private static class VideoConfig {
private String name;
private Long bitRate;
private Integer height;
private String outputPath;
/**
* @param name The name of the video configuration
* @param bitRate The target output bitrate of the video configuration
* @param height The target output height of the video configuration
* @param outputPath The output path for this video configuration
*/
private VideoConfig(
String name, Long bitRate, Integer height, String outputPath) {
this.name = name;
this.bitRate = bitRate;
this.height = height;
this.outputPath = outputPath;
}
}
private static class AudioConfig {
private String name;
private Long bitrate;
private String outputPath;
/**
* @param name The name of the audio configuration
* @param bitrate The target output bitrate of the audio configuration
* @param outputPath The output path for this audio configuration
*/
public AudioConfig(String name, Long bitrate, String outputPath) {
this.name = name;
this.bitrate = bitrate;
this.outputPath = outputPath;
}
}
}