Shaka Packager SDK
Loading...
Searching...
No Matches
crypto_flags.cc
1// Copyright 2017 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 <packager/app/crypto_flags.h>
8
9#include <cstdint>
10#include <cstdio>
11#include <string>
12
13#include <absl/flags/flag.h>
14
15ABSL_FLAG(std::string,
16 protection_scheme,
17 "cenc",
18 "Specify a protection scheme, 'cenc' or 'cbc1' or pattern-based "
19 "protection schemes 'cens' or 'cbcs', or 'aes128' for HLS AES-128 "
20 "full-segment CBC encryption (TS/HLS only, no DRM system required).");
21ABSL_FLAG(
22 int32_t,
23 crypt_byte_block,
24 1,
25 "Specify the count of the encrypted blocks in the protection pattern, "
26 "where block is of size 16-bytes. There are three common "
27 "patterns (crypt_byte_block:skip_byte_block): 1:9 (default), 5:5, 10:0. "
28 "Apply to video streams with 'cbcs' and 'cens' protection schemes only; "
29 "ignored otherwise.");
30ABSL_FLAG(
31 int32_t,
32 skip_byte_block,
33 9,
34 "Specify the count of the unencrypted blocks in the protection pattern. "
35 "Apply to video streams with 'cbcs' and 'cens' protection schemes only; "
36 "ignored otherwise.");
37ABSL_FLAG(bool,
38 vp9_subsample_encryption,
39 true,
40 "Enable VP9 subsample encryption.");
41ABSL_FLAG(bool,
42 cencv1,
43 false,
44 "Use CENC v1 (2012) instead of v3 (2016+) for encryption.");
45ABSL_FLAG(std::string,
46 playready_extra_header_data,
47 "",
48 "Extra XML data to add to PlayReady headers.");
49
50bool ValueNotGreaterThanTen(const char* flagname, int32_t value) {
51 if (value > 10) {
52 fprintf(stderr, "ERROR: %s must not be greater than 10.\n", flagname);
53 return false;
54 }
55 if (value < 0) {
56 fprintf(stderr, "ERROR: %s must be non-negative.\n", flagname);
57 return false;
58 }
59 return true;
60}
61
62bool ValueIsXml(const char* flagname, const std::string& value) {
63 if (value.empty())
64 return true;
65
66 if (value[0] != '<' || value[value.size() - 1] != '>') {
67 fprintf(stderr, "ERROR: %s must be valid XML.\n", flagname);
68 return false;
69 }
70 return true;
71}
72
73namespace shaka {
74bool ValidateCryptoFlags() {
75 bool success = true;
76
77 auto cencv1 = absl::GetFlag(FLAGS_cencv1);
78 auto scheme = absl::GetFlag(FLAGS_protection_scheme);
79 if (cencv1 && scheme != "cenc") {
80 fprintf(stderr, "ERROR: CENC v1 only supports 'cenc' scheme.\n");
81 success = false;
82 }
83
84 auto crypt_byte_block = absl::GetFlag(FLAGS_crypt_byte_block);
85 if (!ValueNotGreaterThanTen("crypt_byte_block", crypt_byte_block)) {
86 success = false;
87 }
88
89 auto skip_byte_block = absl::GetFlag(FLAGS_skip_byte_block);
90 if (!ValueNotGreaterThanTen("skip_byte_block", skip_byte_block)) {
91 success = false;
92 }
93
94 auto playready_extra_header_data =
95 absl::GetFlag(FLAGS_playready_extra_header_data);
96 if (!ValueIsXml("playready_extra_header_data", playready_extra_header_data)) {
97 success = false;
98 }
99
100 return success;
101}
102} // namespace shaka
All the methods that are virtual are virtual for mocking.