DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
packager_util.cc
1 // Copyright 2014 Google Inc. 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/packager_util.h"
8 
9 #include <gflags/gflags.h>
10 #include <iostream>
11 
12 #include "packager/app/fixed_key_encryption_flags.h"
13 #include "packager/app/mpd_flags.h"
14 #include "packager/app/muxer_flags.h"
15 #include "packager/app/widevine_encryption_flags.h"
16 #include "packager/base/logging.h"
17 #include "packager/base/strings/string_number_conversions.h"
18 #include "packager/media/base/fixed_key_source.h"
19 #include "packager/media/base/media_stream.h"
20 #include "packager/media/base/muxer.h"
21 #include "packager/media/base/muxer_options.h"
22 #include "packager/media/base/request_signer.h"
23 #include "packager/media/base/stream_info.h"
24 #include "packager/media/base/widevine_key_source.h"
25 #include "packager/media/file/file.h"
26 #include "packager/mpd/base/mpd_builder.h"
27 
28 DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info.");
29 
30 namespace shaka {
31 namespace media {
32 
33 void DumpStreamInfo(const std::vector<MediaStream*>& streams) {
34  printf("Found %zu stream(s).\n", streams.size());
35  for (size_t i = 0; i < streams.size(); ++i)
36  printf("Stream [%zu] %s\n", i, streams[i]->info()->ToString().c_str());
37 }
38 
39 scoped_ptr<RequestSigner> CreateSigner() {
40  scoped_ptr<RequestSigner> signer;
41 
42  if (!FLAGS_aes_signing_key.empty()) {
43  signer.reset(AesRequestSigner::CreateSigner(
44  FLAGS_signer, FLAGS_aes_signing_key, FLAGS_aes_signing_iv));
45  if (!signer) {
46  LOG(ERROR) << "Cannot create an AES signer object from '"
47  << FLAGS_aes_signing_key << "':'" << FLAGS_aes_signing_iv
48  << "'.";
49  return scoped_ptr<RequestSigner>();
50  }
51  } else if (!FLAGS_rsa_signing_key_path.empty()) {
52  std::string rsa_private_key;
53  if (!File::ReadFileToString(FLAGS_rsa_signing_key_path.c_str(),
54  &rsa_private_key)) {
55  LOG(ERROR) << "Failed to read from '" << FLAGS_rsa_signing_key_path
56  << "'.";
57  return scoped_ptr<RequestSigner>();
58  }
59  signer.reset(RsaRequestSigner::CreateSigner(FLAGS_signer, rsa_private_key));
60  if (!signer) {
61  LOG(ERROR) << "Cannot create a RSA signer object from '"
62  << FLAGS_rsa_signing_key_path << "'.";
63  return scoped_ptr<RequestSigner>();
64  }
65  }
66  return signer.Pass();
67 }
68 
69 scoped_ptr<KeySource> CreateEncryptionKeySource() {
70  scoped_ptr<KeySource> encryption_key_source;
71  if (FLAGS_enable_widevine_encryption) {
72  scoped_ptr<WidevineKeySource> widevine_key_source(
73  new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
74  if (!FLAGS_signer.empty()) {
75  scoped_ptr<RequestSigner> request_signer(CreateSigner());
76  if (!request_signer)
77  return scoped_ptr<KeySource>();
78  widevine_key_source->set_signer(request_signer.Pass());
79  }
80 
81  std::vector<uint8_t> content_id;
82  if (!base::HexStringToBytes(FLAGS_content_id, &content_id)) {
83  LOG(ERROR) << "Invalid content_id hex string specified.";
84  return scoped_ptr<KeySource>();
85  }
86  Status status = widevine_key_source->FetchKeys(content_id, FLAGS_policy);
87  if (!status.ok()) {
88  LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
89  << status.ToString();
90  return scoped_ptr<KeySource>();
91  }
92  encryption_key_source = widevine_key_source.Pass();
93  } else if (FLAGS_enable_fixed_key_encryption) {
94  encryption_key_source = FixedKeySource::CreateFromHexStrings(
95  FLAGS_key_id, FLAGS_key, FLAGS_pssh, FLAGS_iv);
96  }
97  return encryption_key_source.Pass();
98 }
99 
100 scoped_ptr<KeySource> CreateDecryptionKeySource() {
101  scoped_ptr<KeySource> decryption_key_source;
102  if (FLAGS_enable_widevine_decryption) {
103  scoped_ptr<WidevineKeySource> widevine_key_source(
104  new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
105  if (!FLAGS_signer.empty()) {
106  scoped_ptr<RequestSigner> request_signer(CreateSigner());
107  if (!request_signer)
108  return scoped_ptr<KeySource>();
109  widevine_key_source->set_signer(request_signer.Pass());
110  }
111 
112  decryption_key_source = widevine_key_source.Pass();
113  } else if (FLAGS_enable_fixed_key_decryption) {
114  const char kNoPssh[] = "";
115  const char kNoIv[] = "";
116  decryption_key_source = FixedKeySource::CreateFromHexStrings(
117  FLAGS_key_id, FLAGS_key, kNoPssh, kNoIv);
118  }
119  return decryption_key_source.Pass();
120 }
121 
122 bool AssignFlagsFromProfile() {
123  bool single_segment = FLAGS_single_segment;
124  if (FLAGS_profile == "on-demand") {
125  single_segment = true;
126  } else if (FLAGS_profile == "live") {
127  single_segment = false;
128  } else if (FLAGS_profile != "") {
129  fprintf(stderr, "ERROR: --profile '%s' is not supported.\n",
130  FLAGS_profile.c_str());
131  return false;
132  }
133 
134  if (FLAGS_single_segment != single_segment) {
135  FLAGS_single_segment = single_segment;
136  fprintf(stdout, "Profile %s: set --single_segment to %s.\n",
137  FLAGS_profile.c_str(), single_segment ? "true" : "false");
138  }
139  return true;
140 }
141 
142 bool GetMuxerOptions(MuxerOptions* muxer_options) {
143  DCHECK(muxer_options);
144 
145  muxer_options->single_segment = FLAGS_single_segment;
146  muxer_options->segment_duration = FLAGS_segment_duration;
147  muxer_options->fragment_duration = FLAGS_fragment_duration;
148  muxer_options->segment_sap_aligned = FLAGS_segment_sap_aligned;
149  muxer_options->fragment_sap_aligned = FLAGS_fragment_sap_aligned;
150  muxer_options->num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx;
151  muxer_options->temp_dir = FLAGS_temp_dir;
152  return true;
153 }
154 
155 bool GetMpdOptions(MpdOptions* mpd_options) {
156  DCHECK(mpd_options);
157 
158  mpd_options->availability_time_offset = FLAGS_availability_time_offset;
159  mpd_options->minimum_update_period = FLAGS_minimum_update_period;
160  mpd_options->min_buffer_time = FLAGS_min_buffer_time;
161  mpd_options->time_shift_buffer_depth = FLAGS_time_shift_buffer_depth;
162  mpd_options->suggested_presentation_delay =
163  FLAGS_suggested_presentation_delay;
164  return true;
165 }
166 
167 MediaStream* FindFirstStreamOfType(const std::vector<MediaStream*>& streams,
168  StreamType stream_type) {
169  typedef std::vector<MediaStream*>::const_iterator StreamIterator;
170  for (StreamIterator it = streams.begin(); it != streams.end(); ++it) {
171  if ((*it)->info()->stream_type() == stream_type)
172  return *it;
173  }
174  return NULL;
175 }
176 MediaStream* FindFirstVideoStream(const std::vector<MediaStream*>& streams) {
177  return FindFirstStreamOfType(streams, kStreamVideo);
178 }
179 MediaStream* FindFirstAudioStream(const std::vector<MediaStream*>& streams) {
180  return FindFirstStreamOfType(streams, kStreamAudio);
181 }
182 
183 bool AddStreamToMuxer(const std::vector<MediaStream*>& streams,
184  const std::string& stream_selector,
185  const std::string& language_override,
186  Muxer* muxer) {
187  DCHECK(muxer);
188 
189  MediaStream* stream = NULL;
190  if (stream_selector == "video") {
191  stream = FindFirstVideoStream(streams);
192  } else if (stream_selector == "audio") {
193  stream = FindFirstAudioStream(streams);
194  } else {
195  // Expect stream_selector to be a zero based stream id.
196  size_t stream_id;
197  if (!base::StringToSizeT(stream_selector, &stream_id) ||
198  stream_id >= streams.size()) {
199  LOG(ERROR) << "Invalid argument --stream=" << stream_selector << "; "
200  << "should be 'audio', 'video', or a number within [0, "
201  << streams.size() - 1 << "].";
202  return false;
203  }
204  stream = streams[stream_id];
205  DCHECK(stream);
206  }
207 
208  // This could occur only if stream_selector=audio|video and the corresponding
209  // stream does not exist in the input.
210  if (!stream) {
211  LOG(ERROR) << "No " << stream_selector << " stream found in the input.";
212  return false;
213  }
214 
215  if (!language_override.empty()) {
216  stream->info()->set_language(language_override);
217  }
218 
219  muxer->AddStream(stream);
220  return true;
221 }
222 
223 } // namespace media
224 } // namespace shaka
static RsaRequestSigner * CreateSigner(const std::string &signer_name, const std::string &pkcs1_rsa_key)
static scoped_ptr< FixedKeySource > CreateFromHexStrings(const std::string &key_id_hex, const std::string &key_hex, const std::string &pssh_boxes_hex, const std::string &iv_hex)
static AesRequestSigner * CreateSigner(const std::string &signer_name, const std::string &aes_key_hex, const std::string &iv_hex)
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:184