Shaka Packager SDK
Loading...
Searching...
No Matches
packager_main.cc
1// Copyright 2014 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <iostream>
8#include <optional>
9#include <vector>
10
11#if defined(OS_WIN)
12#include <codecvt>
13#include <functional>
14#endif // defined(OS_WIN)
15
16#include <absl/flags/flag.h>
17#include <absl/flags/parse.h>
18#include <absl/flags/usage.h>
19#include <absl/flags/usage_config.h>
20#include <absl/log/globals.h>
21#include <absl/log/initialize.h>
22#include <absl/log/log.h>
23#include <absl/strings/numbers.h>
24#include <absl/strings/str_format.h>
25#include <absl/strings/str_split.h>
26
27#include <packager/app/ad_cue_generator_flags.h>
28#include <packager/app/crypto_flags.h>
29#include <packager/app/hls_flags.h>
30#include <packager/app/manifest_flags.h>
31#include <packager/app/mpd_flags.h>
32#include <packager/app/muxer_flags.h>
33#include <packager/app/playready_key_encryption_flags.h>
34#include <packager/app/protection_system_flags.h>
35#include <packager/app/raw_key_encryption_flags.h>
36#include <packager/app/retired_flags.h>
37#include <packager/app/stream_descriptor.h>
38#include <packager/app/widevine_encryption_flags.h>
39#include <packager/file.h>
40#include <packager/kv_pairs/kv_pairs.h>
41#include <packager/tools/license_notice.h>
42#include <packager/utils/string_trim_split.h>
43
44ABSL_FLAG(bool, dump_stream_info, false, "Dump demuxed stream info.");
45ABSL_FLAG(bool, licenses, false, "Dump licenses.");
46ABSL_FLAG(bool, quiet, false, "When enabled, LOG(INFO) output is suppressed.");
47ABSL_FLAG(bool,
48 use_fake_clock_for_muxer,
49 false,
50 "Set to true to use a fake clock for muxer. With this flag set, "
51 "creation time and modification time in outputs are set to 0. "
52 "Should only be used for testing.");
53ABSL_FLAG(std::string,
54 test_packager_version,
55 "",
56 "Packager version for testing. Should be used for testing only.");
57ABSL_FLAG(bool,
58 single_threaded,
59 false,
60 "If enabled, only use one thread when generating content.");
61
62// From absl/log:
63ABSL_DECLARE_FLAG(int, stderrthreshold);
64
65namespace shaka {
66namespace {
67
68const char kUsage[] =
69 "%s [flags] <stream_descriptor> ...\n\n"
70 " stream_descriptor consists of comma separated field_name/value pairs:\n"
71 " field_name=value,[field_name=value,]...\n"
72 " Supported field names are as follows (names in parenthesis are alias):\n"
73 " - input (in): Required input/source media file path or network stream\n"
74 " URL.\n"
75 " - stream_selector (stream): Required field with value 'audio',\n"
76 " 'video', 'text', or stream number (zero based).\n"
77 " - output (out,init_segment): Required output file (single file) or\n"
78 " initialization file path (multiple file).\n"
79 " - segment_template (segment): Optional value which specifies the\n"
80 " naming pattern for the segment files, and that the stream should be\n"
81 " split into multiple files. Its presence should be consistent across\n"
82 " streams.\n"
83 " - bandwidth (bw): Optional value which contains a user-specified\n"
84 " maximum bit rate for the stream, in bits/sec. If specified, this\n"
85 " value is propagated to (HLS) EXT-X-STREAM-INF:BANDWIDTH or (DASH)\n"
86 " Representation@bandwidth and the $Bandwidth$ template parameter for\n"
87 " segment names. If not specified, the bandwidth value is estimated\n"
88 " from content bitrate. Note that it only affects the generated\n"
89 " manifests/playlists; it has no effect on the media content itself.\n"
90 " - language (lang): Optional value which contains a user-specified\n"
91 " language tag. If specified, this value overrides any language\n"
92 " metadata in the input stream.\n"
93 " - output_format (format): Optional value which specifies the format\n"
94 " of the output files (MP4 or WebM). If not specified, it will be\n"
95 " derived from the file extension of the output file.\n"
96 " - input_format (format): Optional value which specifies the format\n"
97 " of the input files or streams. If not specified, it will be\n"
98 " autodetected, which in some cases (such as live UDP webvtt) may\n"
99 " fail.\n"
100 " - skip_encryption=0|1: Optional. Defaults to 0 if not specified. If\n"
101 " it is set to 1, no encryption of the stream will be made.\n"
102 " - drm_label: Optional value for custom DRM label, which defines the\n"
103 " encryption key applied to the stream. Typical values include AUDIO,\n"
104 " SD, HD, UHD1, UHD2. For raw key, it should be a label defined in\n"
105 " --keys. If not provided, the DRM label is derived from stream type\n"
106 " (video, audio), resolution, etc.\n"
107 " Note that it is case sensitive.\n"
108 " - trick_play_factor (tpf): Optional value which specifies the trick\n"
109 " play, a.k.a. trick mode, stream sampling rate among key frames.\n"
110 " If specified, the output is a trick play stream.\n"
111 " - hls_name: Used for HLS audio to set the NAME attribute for\n"
112 " EXT-X-MEDIA. Defaults to the base of the playlist name.\n"
113 " - hls_group_id: Used for HLS audio to set the GROUP-ID attribute for\n"
114 " EXT-X-MEDIA. Defaults to 'audio' if not specified.\n"
115 " - playlist_name: The HLS playlist file to create. Usually ends with\n"
116 " '.m3u8', and is relative to --hls_master_playlist_output. If\n"
117 " unspecified, defaults to something of the form 'stream_0.m3u8',\n"
118 " 'stream_1.m3u8', 'stream_2.m3u8', etc.\n"
119 " - iframe_playlist_name: The optional HLS I-Frames only playlist file\n"
120 " to create. Usually ends with '.m3u8', and is relative to\n"
121 " hls_master_playlist_output. Should only be set for video streams. If\n"
122 " unspecified, no I-Frames only playlist is created.\n"
123 " - hls_characteristics (charcs): Optional colon/semicolon separated\n"
124 " list of values for the CHARACTERISTICS attribute for EXT-X-MEDIA.\n"
125 " See CHARACTERISTICS attribute in http://bit.ly/2OOUkdB for details.\n"
126 " - dash_accessibilities (accessibilities): Optional semicolon separated\n"
127 " list of values for DASH Accessibility elements. The value should be\n"
128 " in the format: scheme_id_uri=value.\n"
129 " - dash_roles (roles): Optional semicolon separated list of values for\n"
130 " DASH Role elements. The value should be one of: caption, subtitle, \n"
131 " main, alternate, supplementary, commentary, dub, description, sign, \n"
132 " metadata, enhanced-audio- intelligibility, emergency, \n"
133 " forced-subtitle, easyreader, and karaoke. See DASH\n"
134 " (ISO/IEC 23009-1) specification for details.\n"
135 " - forced_subtitle: Optional boolean value (0|1). If set to 1 \n"
136 " indicates that this stream is a Forced Narrative subtitle that \n"
137 " should be displayed when subtitles are otherwise off, for example \n"
138 " used to caption short portions of the audio that might be in a \n"
139 " foreign language. For DASH this will set role to forced_subtitle, \n"
140 " for HLS it will set FORCED=YES and AUTOSELECT=YES. \n"
141 " Only valid for subtitles.\n";
142
143// Labels for parameters in RawKey key info.
144const char kDrmLabelLabel[] = "label";
145const char kKeyIdLabel[] = "key_id";
146const char kKeyLabel[] = "key";
147const char kKeyIvLabel[] = "iv";
148
149enum ExitStatus {
150 kSuccess = 0,
151 kArgumentValidationFailed,
152 kPackagingFailed,
153 kInternalError,
154};
155
156bool GetWidevineSigner(WidevineSigner* signer) {
157 signer->signer_name = absl::GetFlag(FLAGS_signer);
158 if (!absl::GetFlag(FLAGS_aes_signing_key).bytes.empty()) {
159 signer->signing_key_type = WidevineSigner::SigningKeyType::kAes;
160 signer->aes.key = absl::GetFlag(FLAGS_aes_signing_key).bytes;
161 signer->aes.iv = absl::GetFlag(FLAGS_aes_signing_iv).bytes;
162 } else if (!absl::GetFlag(FLAGS_rsa_signing_key_path).empty()) {
163 signer->signing_key_type = WidevineSigner::SigningKeyType::kRsa;
164 if (!File::ReadFileToString(
165 absl::GetFlag(FLAGS_rsa_signing_key_path).c_str(),
166 &signer->rsa.key)) {
167 LOG(ERROR) << "Failed to read from '"
168 << absl::GetFlag(FLAGS_rsa_signing_key_path) << "'.";
169 return false;
170 }
171 }
172 return true;
173}
174
175bool GetHlsPlaylistType(const std::string& playlist_type,
176 HlsPlaylistType* playlist_type_enum) {
177 if (absl::AsciiStrToUpper(playlist_type) == "VOD") {
178 *playlist_type_enum = HlsPlaylistType::kVod;
179 } else if (absl::AsciiStrToUpper(playlist_type) == "LIVE") {
180 *playlist_type_enum = HlsPlaylistType::kLive;
181 } else if (absl::AsciiStrToUpper(playlist_type) == "EVENT") {
182 *playlist_type_enum = HlsPlaylistType::kEvent;
183 } else {
184 LOG(ERROR) << "Unrecognized playlist type " << playlist_type;
185 return false;
186 }
187 return true;
188}
189
190bool GetProtectionScheme(uint32_t* protection_scheme) {
191 if (absl::GetFlag(FLAGS_protection_scheme) == "cenc") {
192 *protection_scheme = EncryptionParams::kProtectionSchemeCenc;
193 return true;
194 }
195 if (absl::GetFlag(FLAGS_protection_scheme) == "cbc1") {
196 *protection_scheme = EncryptionParams::kProtectionSchemeCbc1;
197 return true;
198 }
199 if (absl::GetFlag(FLAGS_protection_scheme) == "cbcs") {
200 *protection_scheme = EncryptionParams::kProtectionSchemeCbcs;
201 return true;
202 }
203 if (absl::GetFlag(FLAGS_protection_scheme) == "cens") {
204 *protection_scheme = EncryptionParams::kProtectionSchemeCens;
205 return true;
206 }
207 LOG(ERROR) << "Unrecognized protection_scheme "
208 << absl::GetFlag(FLAGS_protection_scheme);
209 return false;
210}
211
212bool ParseKeys(const std::string& keys, RawKeyParams* raw_key) {
213 std::vector<std::string> keys_data = SplitAndTrimSkipEmpty(keys, ',');
214
215 for (const std::string& key_data : keys_data) {
216 std::vector<KVPair> string_pairs =
217 SplitStringIntoKeyValuePairs(key_data, '=', ':');
218
219 std::map<std::string, std::string> value_map;
220 for (const auto& string_pair : string_pairs)
221 value_map[string_pair.first] = string_pair.second;
222 const std::string drm_label = value_map[kDrmLabelLabel];
223 if (raw_key->key_map.find(drm_label) != raw_key->key_map.end()) {
224 LOG(ERROR) << "Seeing duplicated DRM label '" << drm_label << "'.";
225 return false;
226 }
227 auto& key_info = raw_key->key_map[drm_label];
228 if (value_map[kKeyIdLabel].empty() ||
229 !shaka::ValidHexStringToBytes(value_map[kKeyIdLabel],
230 &key_info.key_id)) {
231 LOG(ERROR) << "Empty key id or invalid hex string for key id: "
232 << value_map[kKeyIdLabel];
233 return false;
234 }
235 if (value_map[kKeyLabel].empty() ||
236 !shaka::ValidHexStringToBytes(value_map[kKeyLabel], &key_info.key)) {
237 LOG(ERROR) << "Empty key or invalid hex string for key: "
238 << value_map[kKeyLabel];
239 return false;
240 }
241 if (!value_map[kKeyIvLabel].empty()) {
242 if (!raw_key->iv.empty()) {
243 LOG(ERROR) << "IV already specified with --iv";
244 return false;
245 }
246 if (!shaka::ValidHexStringToBytes(value_map[kKeyIvLabel], &key_info.iv)) {
247 LOG(ERROR) << "Empty IV or invalid hex string for IV: "
248 << value_map[kKeyIvLabel];
249 return false;
250 }
251 }
252 }
253 return true;
254}
255
256bool GetRawKeyParams(RawKeyParams* raw_key) {
257 raw_key->iv = absl::GetFlag(FLAGS_iv).bytes;
258 raw_key->pssh = absl::GetFlag(FLAGS_pssh).bytes;
259 if (!absl::GetFlag(FLAGS_keys).empty()) {
260 if (!ParseKeys(absl::GetFlag(FLAGS_keys), raw_key)) {
261 LOG(ERROR) << "Failed to parse --keys " << absl::GetFlag(FLAGS_keys);
262 return false;
263 }
264 } else {
265 // An empty StreamLabel specifies the default key info.
266 RawKeyParams::KeyInfo& key_info = raw_key->key_map[""];
267 key_info.key_id = absl::GetFlag(FLAGS_key_id).bytes;
268 key_info.key = absl::GetFlag(FLAGS_key).bytes;
269 }
270 return true;
271}
272
273bool ParseAdCues(const std::string& ad_cues, std::vector<Cuepoint>* cuepoints) {
274 // Track if optional field is supplied consistently across all cue points.
275 size_t duration_count = 0;
276 std::vector<std::string> ad_cues_vec = SplitAndTrimSkipEmpty(ad_cues, ';');
277
278 for (const std::string& ad_cue : ad_cues_vec) {
279 Cuepoint cuepoint;
280 std::vector<std::string> split_ad_cue = SplitAndTrimSkipEmpty(ad_cue, ',');
281
282 if (split_ad_cue.size() > 2) {
283 LOG(ERROR) << "Failed to parse --ad_cues " << ad_cues
284 << " Each ad cue must contain no more than 2 components.";
285 }
286 if (!absl::SimpleAtod(split_ad_cue.front(),
287 &cuepoint.start_time_in_seconds)) {
288 LOG(ERROR) << "Failed to parse --ad_cues " << ad_cues
289 << " Start time component must be of type double.";
290 return false;
291 }
292 if (split_ad_cue.size() > 1) {
293 duration_count++;
294 if (!absl::SimpleAtod(split_ad_cue[1], &cuepoint.duration_in_seconds)) {
295 LOG(ERROR) << "Failed to parse --ad_cues " << ad_cues
296 << " Duration component must be of type double.";
297 return false;
298 }
299 }
300 cuepoints->push_back(cuepoint);
301 }
302
303 if (duration_count > 0 && duration_count != cuepoints->size()) {
304 LOG(ERROR) << "Failed to parse --ad_cues " << ad_cues
305 << " Duration component is optional. However if it is supplied,"
306 << " it must be supplied consistently across all cuepoints.";
307 return false;
308 }
309 return true;
310}
311
312bool ParseProtectionSystems(const std::string& protection_systems_str,
313 ProtectionSystem* protection_systems) {
314 *protection_systems = ProtectionSystem::kNone;
315
316 std::map<std::string, ProtectionSystem> mapping = {
317 {"common", ProtectionSystem::kCommon},
318 {"commonsystem", ProtectionSystem::kCommon},
319 {"fairplay", ProtectionSystem::kFairPlay},
320 {"marlin", ProtectionSystem::kMarlin},
321 {"playready", ProtectionSystem::kPlayReady},
322 {"widevine", ProtectionSystem::kWidevine},
323 };
324
325 std::vector<std::string> protection_systems_vec =
326 SplitAndTrimSkipEmpty(absl::AsciiStrToLower(protection_systems_str), ',');
327
328 for (const std::string& protection_system : protection_systems_vec) {
329 auto iter = mapping.find(protection_system);
330 if (iter == mapping.end()) {
331 LOG(ERROR) << "Seeing unrecognized protection system: "
332 << protection_system;
333 return false;
334 }
335 *protection_systems |= iter->second;
336 }
337 return true;
338}
339
340bool ParseClosedCaptions(const std::string& captions_str,
341 std::vector<CeaCaption>* captions) {
342 std::vector<std::string> captions_list =
343 SplitAndTrimSkipEmpty(captions_str, ';');
344 for (const std::string& caption_str : captions_list) {
345 CeaCaption caption;
346 std::vector<KVPair> caption_parts =
347 SplitStringIntoKeyValuePairs(caption_str, '=', ',');
348 for (const auto& part : caption_parts) {
349 if (part.first == "channel") {
350 caption.channel = part.second;
351 } else if (part.first == "lang") {
352 caption.language = part.second;
353 } else if (part.first == "name") {
354 caption.name = part.second;
355 } else if (part.first == "default") {
356 caption.is_default = (part.second == "yes");
357 } else if (part.first == "autoselect") {
358 caption.autoselect = (part.second == "yes");
359 }
360 }
361 if (caption.channel.empty()) {
362 LOG(ERROR) << "Missing channel in CEA caption: " << caption_str;
363 return false;
364 }
365 if (caption.name.empty()) {
366 caption.name = caption.channel;
367 }
368 captions->push_back(caption);
369 }
370 return true;
371}
372
373std::optional<PackagingParams> GetPackagingParams() {
374 PackagingParams packaging_params;
375
376 packaging_params.temp_dir = absl::GetFlag(FLAGS_temp_dir);
377 packaging_params.single_threaded = absl::GetFlag(FLAGS_single_threaded);
378
379 AdCueGeneratorParams& ad_cue_generator_params =
380 packaging_params.ad_cue_generator_params;
381 if (!ParseAdCues(absl::GetFlag(FLAGS_ad_cues),
382 &ad_cue_generator_params.cue_points)) {
383 return std::nullopt;
384 }
385
386 ChunkingParams& chunking_params = packaging_params.chunking_params;
387 chunking_params.segment_duration_in_seconds =
388 absl::GetFlag(FLAGS_segment_duration);
389 chunking_params.subsegment_duration_in_seconds =
390 absl::GetFlag(FLAGS_fragment_duration);
391 chunking_params.low_latency_dash_mode =
392 absl::GetFlag(FLAGS_low_latency_dash_mode);
393 chunking_params.segment_sap_aligned =
394 absl::GetFlag(FLAGS_segment_sap_aligned);
395 chunking_params.subsegment_sap_aligned =
396 absl::GetFlag(FLAGS_fragment_sap_aligned);
397 chunking_params.start_segment_number =
398 absl::GetFlag(FLAGS_start_segment_number);
399
400 int num_key_providers = 0;
401 EncryptionParams& encryption_params = packaging_params.encryption_params;
402 if (absl::GetFlag(FLAGS_enable_widevine_encryption)) {
403 encryption_params.key_provider = KeyProvider::kWidevine;
404 ++num_key_providers;
405 }
406 if (absl::GetFlag(FLAGS_enable_playready_encryption)) {
407 encryption_params.key_provider = KeyProvider::kPlayReady;
408 ++num_key_providers;
409 }
410 if (absl::GetFlag(FLAGS_enable_raw_key_encryption)) {
411 encryption_params.key_provider = KeyProvider::kRawKey;
412 ++num_key_providers;
413 }
414 if (num_key_providers > 1) {
415 LOG(ERROR) << "Only one of --enable_widevine_encryption, "
416 "--enable_playready_encryption, "
417 "--enable_raw_key_encryption can be enabled.";
418 return std::nullopt;
419 }
420
421 if (!ParseProtectionSystems(absl::GetFlag(FLAGS_protection_systems),
422 &encryption_params.protection_systems)) {
423 return std::nullopt;
424 }
425
426 if (encryption_params.key_provider != KeyProvider::kNone) {
427 encryption_params.clear_lead_in_seconds = absl::GetFlag(FLAGS_clear_lead);
428 if (!GetProtectionScheme(&encryption_params.protection_scheme))
429 return std::nullopt;
430 encryption_params.crypt_byte_block = absl::GetFlag(FLAGS_crypt_byte_block);
431 encryption_params.skip_byte_block = absl::GetFlag(FLAGS_skip_byte_block);
432
433 encryption_params.crypto_period_duration_in_seconds =
434 absl::GetFlag(FLAGS_crypto_period_duration);
435 encryption_params.vp9_subsample_encryption =
436 absl::GetFlag(FLAGS_vp9_subsample_encryption);
437 encryption_params.cencv1 = absl::GetFlag(FLAGS_cencv1);
438 encryption_params.stream_label_func = std::bind(
439 &Packager::DefaultStreamLabelFunction,
440 absl::GetFlag(FLAGS_max_sd_pixels), absl::GetFlag(FLAGS_max_hd_pixels),
441 absl::GetFlag(FLAGS_max_uhd1_pixels), std::placeholders::_1);
442 encryption_params.playready_extra_header_data =
443 absl::GetFlag(FLAGS_playready_extra_header_data);
444 }
445 switch (encryption_params.key_provider) {
446 case KeyProvider::kWidevine: {
447 WidevineEncryptionParams& widevine = encryption_params.widevine;
448 widevine.key_server_url = absl::GetFlag(FLAGS_key_server_url);
449
450 widevine.content_id = absl::GetFlag(FLAGS_content_id).bytes;
451 widevine.policy = absl::GetFlag(FLAGS_policy);
452 widevine.group_id = absl::GetFlag(FLAGS_group_id).bytes;
453 widevine.enable_entitlement_license =
454 absl::GetFlag(FLAGS_enable_entitlement_license);
455 if (!GetWidevineSigner(&widevine.signer))
456 return std::nullopt;
457 break;
458 }
459 case KeyProvider::kPlayReady: {
460 PlayReadyEncryptionParams& playready = encryption_params.playready;
461 playready.key_server_url = absl::GetFlag(FLAGS_playready_server_url);
462 playready.program_identifier = absl::GetFlag(FLAGS_program_identifier);
463 break;
464 }
465 case KeyProvider::kRawKey: {
466 if (!GetRawKeyParams(&encryption_params.raw_key))
467 return std::nullopt;
468 break;
469 }
470 case KeyProvider::kNone:
471 break;
472 }
473
474 num_key_providers = 0;
475 DecryptionParams& decryption_params = packaging_params.decryption_params;
476 if (absl::GetFlag(FLAGS_enable_widevine_decryption)) {
477 decryption_params.key_provider = KeyProvider::kWidevine;
478 ++num_key_providers;
479 }
480 if (absl::GetFlag(FLAGS_enable_raw_key_decryption)) {
481 decryption_params.key_provider = KeyProvider::kRawKey;
482 ++num_key_providers;
483 }
484 if (num_key_providers > 1) {
485 LOG(ERROR) << "Only one of --enable_widevine_decryption, "
486 "--enable_raw_key_decryption can be enabled.";
487 return std::nullopt;
488 }
489 switch (decryption_params.key_provider) {
490 case KeyProvider::kWidevine: {
491 WidevineDecryptionParams& widevine = decryption_params.widevine;
492 widevine.key_server_url = absl::GetFlag(FLAGS_key_server_url);
493 if (!GetWidevineSigner(&widevine.signer))
494 return std::nullopt;
495 break;
496 }
497 case KeyProvider::kRawKey: {
498 if (!GetRawKeyParams(&decryption_params.raw_key))
499 return std::nullopt;
500 break;
501 }
502 case KeyProvider::kPlayReady:
503 case KeyProvider::kNone:
504 break;
505 }
506
507 Mp4OutputParams& mp4_params = packaging_params.mp4_output_params;
508 mp4_params.generate_sidx_in_media_segments =
509 absl::GetFlag(FLAGS_generate_sidx_in_media_segments);
510 mp4_params.include_pssh_in_stream =
511 absl::GetFlag(FLAGS_mp4_include_pssh_in_stream);
512 mp4_params.low_latency_dash_mode = absl::GetFlag(FLAGS_low_latency_dash_mode);
513
514 packaging_params.transport_stream_timestamp_offset_ms =
515 absl::GetFlag(FLAGS_transport_stream_timestamp_offset_ms);
516 packaging_params.default_text_zero_bias_ms =
517 absl::GetFlag(FLAGS_default_text_zero_bias_ms);
518
519 packaging_params.output_media_info = absl::GetFlag(FLAGS_output_media_info);
520
521 MpdParams& mpd_params = packaging_params.mpd_params;
522 mpd_params.mpd_output = absl::GetFlag(FLAGS_mpd_output);
523
524 std::vector<std::string> base_urls =
525 SplitAndTrimSkipEmpty(absl::GetFlag(FLAGS_base_urls), ',');
526
527 mpd_params.base_urls = base_urls;
528 mpd_params.min_buffer_time = absl::GetFlag(FLAGS_min_buffer_time);
529 mpd_params.minimum_update_period = absl::GetFlag(FLAGS_minimum_update_period);
530 mpd_params.suggested_presentation_delay =
531 absl::GetFlag(FLAGS_suggested_presentation_delay);
532 mpd_params.time_shift_buffer_depth =
533 absl::GetFlag(FLAGS_time_shift_buffer_depth);
534 mpd_params.preserved_segments_outside_live_window =
535 absl::GetFlag(FLAGS_preserved_segments_outside_live_window);
536 mpd_params.use_segment_list = absl::GetFlag(FLAGS_dash_force_segment_list);
537
538 if (!absl::GetFlag(FLAGS_utc_timings).empty()) {
539 std::vector<KVPair> pairs = SplitStringIntoKeyValuePairs(
540 absl::GetFlag(FLAGS_utc_timings), '=', ',');
541 if (pairs.empty()) {
542 LOG(ERROR) << "Invalid --utc_timings scheme_id_uri/value pairs.";
543 return std::nullopt;
544 }
545 for (const auto& string_pair : pairs) {
546 mpd_params.utc_timings.push_back({string_pair.first, string_pair.second});
547 }
548 }
549
550 mpd_params.default_language = absl::GetFlag(FLAGS_default_language);
551 mpd_params.default_text_language = absl::GetFlag(FLAGS_default_text_language);
552 mpd_params.generate_static_live_mpd =
553 absl::GetFlag(FLAGS_generate_static_live_mpd);
554 mpd_params.generate_dash_if_iop_compliant_mpd =
555 absl::GetFlag(FLAGS_generate_dash_if_iop_compliant_mpd);
556 mpd_params.allow_approximate_segment_timeline =
557 absl::GetFlag(FLAGS_allow_approximate_segment_timeline);
558 mpd_params.allow_codec_switching = absl::GetFlag(FLAGS_allow_codec_switching);
559 mpd_params.include_mspr_pro =
560 absl::GetFlag(FLAGS_include_mspr_pro_for_playready);
561 mpd_params.low_latency_dash_mode = absl::GetFlag(FLAGS_low_latency_dash_mode);
562
563 HlsParams& hls_params = packaging_params.hls_params;
564 if (!GetHlsPlaylistType(absl::GetFlag(FLAGS_hls_playlist_type),
565 &hls_params.playlist_type)) {
566 return std::nullopt;
567 }
568 hls_params.master_playlist_output =
569 absl::GetFlag(FLAGS_hls_master_playlist_output);
570 hls_params.base_url = absl::GetFlag(FLAGS_hls_base_url);
571 hls_params.key_uri = absl::GetFlag(FLAGS_hls_key_uri);
572 hls_params.time_shift_buffer_depth =
573 absl::GetFlag(FLAGS_time_shift_buffer_depth);
574 hls_params.preserved_segments_outside_live_window =
575 absl::GetFlag(FLAGS_preserved_segments_outside_live_window);
576 hls_params.default_language = absl::GetFlag(FLAGS_default_language);
577 hls_params.default_text_language = absl::GetFlag(FLAGS_default_text_language);
578 hls_params.media_sequence_number =
579 absl::GetFlag(FLAGS_hls_media_sequence_number);
580 hls_params.start_time_offset = absl::GetFlag(FLAGS_hls_start_time_offset);
581 hls_params.create_session_keys = absl::GetFlag(FLAGS_create_session_keys);
582 hls_params.add_program_date_time = absl::GetFlag(FLAGS_add_program_date_time);
583 hls_params.per_playlist_target_duration =
584 absl::GetFlag(FLAGS_per_playlist_target_duration);
585
586 if (!ParseClosedCaptions(absl::GetFlag(FLAGS_closed_captions),
587 &hls_params.closed_captions)) {
588 LOG(ERROR) << "Failed to parse --closed_captions "
589 << absl::GetFlag(FLAGS_closed_captions);
590 return std::nullopt;
591 }
592
593 TestParams& test_params = packaging_params.test_params;
594 test_params.dump_stream_info = absl::GetFlag(FLAGS_dump_stream_info);
595 test_params.inject_fake_clock = absl::GetFlag(FLAGS_use_fake_clock_for_muxer);
596 if (!absl::GetFlag(FLAGS_test_packager_version).empty())
597 test_params.injected_library_version =
598 absl::GetFlag(FLAGS_test_packager_version);
599
600 return packaging_params;
601}
602
603int PackagerMain(int argc, char** argv) {
604 absl::FlagsUsageConfig flag_config;
605 flag_config.version_string = []() -> std::string {
606 return "packager version " + shaka::Packager::GetLibraryVersion() + "\n";
607 };
608 flag_config.contains_help_flags =
609 [](absl::string_view flag_file_name) -> bool { return true; };
610 absl::SetFlagsUsageConfig(flag_config);
611
612 auto usage = absl::StrFormat(kUsage, argv[0]);
613 absl::SetProgramUsageMessage(usage);
614
615 // Before parsing the command line, change the default value of some flags
616 // provided by libraries.
617
618 // Always log to stderr. Log levels are still controlled by --minloglevel.
619 absl::SetFlag(&FLAGS_stderrthreshold, 0);
620
621 auto remaining_args = absl::ParseCommandLine(argc, argv);
622 if (absl::GetFlag(FLAGS_licenses)) {
623 for (const char* line : kLicenseNotice)
624 std::cout << line << std::endl;
625 return kSuccess;
626 }
627
628 if (remaining_args.size() < 2) {
629 std::cerr << "Usage: " << absl::ProgramUsageMessage();
630 return kSuccess;
631 }
632
633 if (absl::GetFlag(FLAGS_quiet)) {
634 absl::SetMinLogLevel(absl::LogSeverityAtLeast::kWarning);
635 }
636
637 absl::InitializeLog();
638
640 !ValidatePRCryptoFlags() || !ValidateCryptoFlags() ||
641 !ValidateRetiredFlags()) {
642 return kArgumentValidationFailed;
643 }
644
645 std::optional<PackagingParams> packaging_params = GetPackagingParams();
646 if (!packaging_params)
647 return kArgumentValidationFailed;
648
649 std::vector<StreamDescriptor> stream_descriptors;
650 for (size_t i = 1; i < remaining_args.size(); ++i) {
651 std::optional<StreamDescriptor> stream_descriptor =
652 ParseStreamDescriptor(remaining_args[i]);
653 if (!stream_descriptor)
654 return kArgumentValidationFailed;
655 stream_descriptors.push_back(stream_descriptor.value());
656 }
657
658 if (absl::GetFlag(FLAGS_force_cl_index)) {
659 int index = 0;
660 for (auto& descriptor : stream_descriptors) {
661 descriptor.index = index++;
662 }
663 }
664
665 Packager packager;
666 Status status =
667 packager.Initialize(packaging_params.value(), stream_descriptors);
668 if (!status.ok()) {
669 LOG(ERROR) << "Failed to initialize packager: " << status.ToString();
670 return kArgumentValidationFailed;
671 }
672 status = packager.Run();
673 if (!status.ok()) {
674 LOG(ERROR) << "Packaging Error: " << status.ToString();
675 return kPackagingFailed;
676 }
677 if (!absl::GetFlag(FLAGS_quiet))
678 printf("Packaging completed successfully.\n");
679 return kSuccess;
680}
681
682} // namespace
683} // namespace shaka
684
685#if defined(OS_WIN)
686// Windows wmain, which converts wide character arguments to UTF-8.
687int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) {
688 std::unique_ptr<char*[], std::function<void(char**)>> utf8_argv(
689 new char*[argc], [argc](char** utf8_args) {
690 // TODO(tinskip): This leaks, but if this code is enabled, it crashes.
691 // Figure out why. I suspect gflags does something funny with the
692 // argument array.
693 // for (int idx = 0; idx < argc; ++idx)
694 // delete[] utf8_args[idx];
695 delete[] utf8_args;
696 });
697 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
698
699 for (int idx = 0; idx < argc; ++idx) {
700 std::string utf8_arg(converter.to_bytes(argv[idx]));
701 utf8_arg += '\0';
702 utf8_argv[idx] = new char[utf8_arg.size()];
703 memcpy(utf8_argv[idx], &utf8_arg[0], utf8_arg.size());
704 }
705
706 // Because we just converted wide character args into UTF8, and because
707 // std::filesystem::u8path is used to interpret all std::string paths as
708 // UTF8, we should set the locale to UTF8 as well, for the transition point
709 // to C library functions like fopen to work correctly with non-ASCII paths.
710 std::setlocale(LC_ALL, ".UTF8");
711
712 return shaka::PackagerMain(argc, utf8_argv.get());
713}
714#else
715int main(int argc, char** argv) {
716 return shaka::PackagerMain(argc, argv);
717}
718#endif // defined(OS_WIN)
All the methods that are virtual are virtual for mocking.
std::optional< StreamDescriptor > ParseStreamDescriptor(const std::string &descriptor_string)
bool ValidateRawKeyCryptoFlags()
bool ValidateWidevineCryptoFlags()