7 #include "packager/app/packager_util.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/base/strings/string_split.h"
12 #include "packager/file/file.h"
13 #include "packager/media/base/fixed_key_source.h"
14 #include "packager/media/base/media_handler.h"
15 #include "packager/media/base/muxer_options.h"
16 #include "packager/media/base/playready_key_source.h"
17 #include "packager/media/base/request_signer.h"
18 #include "packager/media/base/widevine_key_source.h"
19 #include "packager/media/chunking/chunking_handler.h"
20 #include "packager/media/crypto/encryption_handler.h"
21 #include "packager/mpd/base/mpd_options.h"
22 #include "packager/packager.h"
23 #include "packager/status.h"
29 FourCC GetProtectionScheme(
const std::string& protection_scheme) {
30 if (protection_scheme ==
"cenc") {
32 }
else if (protection_scheme ==
"cens") {
34 }
else if (protection_scheme ==
"cbc1") {
36 }
else if (protection_scheme ==
"cbcs") {
39 LOG(ERROR) <<
"Unknown protection scheme: " << protection_scheme;
46 std::unique_ptr<RequestSigner> CreateSigner(
const WidevineSigner& signer) {
47 std::unique_ptr<RequestSigner> request_signer;
48 switch (signer.signing_key_type) {
49 case WidevineSigner::SigningKeyType::kAes:
51 signer.signer_name, signer.aes.key, signer.aes.iv));
53 case WidevineSigner::SigningKeyType::kRsa:
57 case WidevineSigner::SigningKeyType::kNone:
61 LOG(ERROR) <<
"Failed to create the signer object.";
62 return request_signer;
65 std::unique_ptr<KeySource> CreateEncryptionKeySource(
66 FourCC protection_scheme,
67 const EncryptionParams& encryption_params) {
68 std::unique_ptr<KeySource> encryption_key_source;
69 switch (encryption_params.key_provider) {
70 case KeyProvider::kWidevine: {
71 const WidevineEncryptionParams& widevine = encryption_params.widevine;
72 if (widevine.key_server_url.empty()) {
73 LOG(ERROR) <<
"'key_server_url' should not be empty.";
74 return std::unique_ptr<KeySource>();
76 if (widevine.content_id.empty()) {
77 LOG(ERROR) <<
"'content_id' should not be empty.";
78 return std::unique_ptr<KeySource>();
80 std::unique_ptr<WidevineKeySource> widevine_key_source(
81 new WidevineKeySource(widevine.key_server_url,
82 widevine.include_common_pssh));
83 widevine_key_source->set_protection_scheme(protection_scheme);
84 if (!widevine.signer.signer_name.empty()) {
85 std::unique_ptr<RequestSigner> request_signer(
86 CreateSigner(widevine.signer));
88 return std::unique_ptr<KeySource>();
89 widevine_key_source->set_signer(std::move(request_signer));
93 widevine_key_source->FetchKeys(widevine.content_id, widevine.policy);
95 LOG(ERROR) <<
"Widevine encryption key source failed to fetch keys: "
97 return std::unique_ptr<KeySource>();
99 encryption_key_source = std::move(widevine_key_source);
102 case KeyProvider::kRawKey: {
103 const RawKeyEncryptionParams& raw_key = encryption_params.raw_key;
104 const std::string kDefaultTrackType;
107 raw_key.key_map.find(
"")->second.key_id,
108 raw_key.key_map.find(
"")->second.key, raw_key.pssh, raw_key.iv);
111 case KeyProvider::kPlayready: {
112 const PlayreadyEncryptionParams& playready = encryption_params.playready;
113 if (!playready.key_id.empty() && !playready.key.empty()) {
115 playready.key_id, playready.key);
116 }
else if (!playready.key_server_url.empty() &&
117 !playready.program_identifier.empty()) {
118 std::unique_ptr<PlayReadyKeySource> playready_key_source;
119 if (!playready.client_cert_file.empty() &&
120 !playready.client_cert_private_key_file.empty() &&
121 !playready.client_cert_private_key_password.empty()) {
122 playready_key_source.reset(
new PlayReadyKeySource(
123 playready.key_server_url, playready.client_cert_file,
124 playready.client_cert_private_key_file,
125 playready.client_cert_private_key_password));
127 playready_key_source.reset(
128 new PlayReadyKeySource(playready.key_server_url));
130 if (!playready.ca_file.empty()) {
131 playready_key_source->
SetCaFile(playready.ca_file);
133 playready_key_source->FetchKeysWithProgramIdentifier(
134 playready.program_identifier);
135 encryption_key_source = std::move(playready_key_source);
137 LOG(ERROR) <<
"Error creating PlayReady key source.";
138 return std::unique_ptr<KeySource>();
142 case KeyProvider::kNone:
145 return encryption_key_source;
148 std::unique_ptr<KeySource> CreateDecryptionKeySource(
149 const DecryptionParams& decryption_params) {
150 std::unique_ptr<KeySource> decryption_key_source;
151 switch (decryption_params.key_provider) {
152 case KeyProvider::kWidevine: {
153 const WidevineDecryptionParams& widevine = decryption_params.widevine;
154 if (widevine.key_server_url.empty()) {
155 LOG(ERROR) <<
"'key_server_url' should not be empty.";
156 return std::unique_ptr<KeySource>();
158 std::unique_ptr<WidevineKeySource> widevine_key_source(
159 new WidevineKeySource(widevine.key_server_url,
161 if (!widevine.signer.signer_name.empty()) {
162 std::unique_ptr<RequestSigner> request_signer(
163 CreateSigner(widevine.signer));
165 return std::unique_ptr<KeySource>();
166 widevine_key_source->set_signer(std::move(request_signer));
169 decryption_key_source = std::move(widevine_key_source);
172 case KeyProvider::kRawKey: {
173 const RawKeyDecryptionParams& raw_key = decryption_params.raw_key;
174 const std::vector<uint8_t> kNoPssh;
175 const std::vector<uint8_t> kNoIv;
177 raw_key.key_map.find(
"")->second.key_id,
178 raw_key.key_map.find(
"")->second.key, kNoPssh, kNoIv);
181 case KeyProvider::kNone:
182 case KeyProvider::kPlayready:
185 return decryption_key_source;
188 ChunkingOptions GetChunkingOptions(
const ChunkingParams& chunking_params) {
189 ChunkingOptions chunking_options;
191 chunking_params.segment_duration_in_seconds;
192 chunking_options.subsegment_duration_in_seconds =
193 chunking_params.subsegment_duration_in_seconds;
194 chunking_options.segment_sap_aligned = chunking_params.segment_sap_aligned;
195 chunking_options.subsegment_sap_aligned =
196 chunking_params.subsegment_sap_aligned;
197 return chunking_options;
200 EncryptionOptions GetEncryptionOptions(
201 const EncryptionParams& encryption_params) {
202 EncryptionOptions encryption_options;
204 encryption_params.clear_lead_in_seconds;
205 encryption_options.protection_scheme =
206 GetProtectionScheme(encryption_params.protection_scheme);
207 encryption_options.crypto_period_duration_in_seconds =
208 encryption_params.crypto_period_duration_in_seconds;
209 encryption_options.vp9_subsample_encryption =
210 encryption_params.vp9_subsample_encryption;
211 encryption_options.stream_label_func = encryption_params.stream_label_func;
212 return encryption_options;
215 MuxerOptions GetMuxerOptions(
const std::string& temp_dir,
216 const Mp4OutputParams& mp4_params) {
217 MuxerOptions muxer_options;
219 muxer_options.mp4_include_pssh_in_stream = mp4_params.include_pssh_in_stream;
220 muxer_options.mp4_use_decoding_timestamp_in_timeline =
221 mp4_params.use_decoding_timestamp_in_timeline;
222 muxer_options.temp_dir = temp_dir;
223 return muxer_options;
226 MpdOptions GetMpdOptions(
bool on_demand_profile,
const MpdParams& mpd_params) {
227 MpdOptions mpd_options;
228 mpd_options.dash_profile =
229 on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
230 mpd_options.mpd_type =
231 (on_demand_profile || mpd_params.generate_static_live_mpd)
234 mpd_options.minimum_update_period = mpd_params.minimum_update_period;
235 mpd_options.min_buffer_time = mpd_params.min_buffer_time;
236 mpd_options.time_shift_buffer_depth = mpd_params.time_shift_buffer_depth;
237 mpd_options.suggested_presentation_delay =
238 mpd_params.suggested_presentation_delay;
239 mpd_options.default_language = mpd_params.default_language;
243 Status ConnectHandlers(std::vector<std::shared_ptr<MediaHandler>>& handlers) {
244 size_t num_handlers = handlers.size();
246 for (
size_t i = 1; i < num_handlers; ++i) {
247 status.Update(handlers[i - 1]->AddHandler(handlers[i]));