7 #include "packager/hls/base/simple_hls_notifier.h"
11 #include "packager/base/base64.h"
12 #include "packager/base/files/file_path.h"
13 #include "packager/base/json/json_writer.h"
14 #include "packager/base/logging.h"
15 #include "packager/base/optional.h"
16 #include "packager/base/strings/string_number_conversions.h"
17 #include "packager/base/strings/stringprintf.h"
18 #include "packager/hls/base/media_playlist.h"
19 #include "packager/media/base/protection_system_specific_info.h"
20 #include "packager/media/base/raw_key_source.h"
21 #include "packager/media/base/widevine_key_source.h"
22 #include "packager/media/base/widevine_pssh_data.pb.h"
32 const char kUriBase64Prefix[] =
"data:text/plain;base64,";
33 const char kUriFairplayPrefix[] =
"skd://";
34 const char kWidevineDashIfIopUUID[] =
35 "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
37 bool IsWidevineSystemId(
const std::vector<uint8_t>& system_id) {
38 return system_id.size() == arraysize(media::kWidevineSystemId) &&
39 std::equal(system_id.begin(), system_id.end(),
40 media::kWidevineSystemId);
43 bool IsCommonSystemId(
const std::vector<uint8_t>& system_id) {
44 return system_id.size() == arraysize(media::kCommonSystemId) &&
45 std::equal(system_id.begin(), system_id.end(), media::kCommonSystemId);
48 bool IsFairplaySystemId(
const std::vector<uint8_t>& system_id) {
49 return system_id.size() == arraysize(media::kFairplaySystemId) &&
50 std::equal(system_id.begin(), system_id.end(), media::kFairplaySystemId);
53 std::string Base64EncodeData(
const std::string& prefix,
54 const std::string& data) {
55 std::string data_base64;
56 base::Base64Encode(data, &data_base64);
57 return prefix + data_base64;
60 std::string VectorToString(
const std::vector<uint8_t>& v) {
61 return std::string(v.begin(), v.end());
73 std::string MakePathRelative(
const std::string& media_path,
74 const FilePath& parent_path) {
75 FilePath relative_path;
76 const FilePath child_path = FilePath::FromUTF8Unsafe(media_path);
78 parent_path.AppendRelativePath(child_path, &relative_path);
80 relative_path = child_path;
81 return relative_path.NormalizePathSeparatorsTo(
'/').AsUTF8Unsafe();
86 std::string GenerateSegmentUrl(
const std::string& segment_name,
87 const std::string& base_url,
88 const std::string& output_dir,
89 const std::string& playlist_file_name) {
90 FilePath output_path = FilePath::FromUTF8Unsafe(output_dir);
91 if (!base_url.empty()) {
94 return base_url + MakePathRelative(segment_name, output_path);
98 const FilePath playlist_dir =
99 output_path.Append(FilePath::FromUTF8Unsafe(playlist_file_name))
101 .AsEndingWithSeparator();
102 return MakePathRelative(segment_name, playlist_dir);
105 bool WidevinePsshToJson(
const std::vector<uint8_t>& pssh_box,
106 const std::vector<uint8_t>& key_id,
107 std::string* pssh_json) {
108 media::ProtectionSystemSpecificInfo pssh_info;
109 if (!pssh_info.Parse(pssh_box.data(), pssh_box.size())) {
110 LOG(ERROR) <<
"Failed to parse PSSH box.";
114 media::WidevinePsshData pssh_proto;
115 if (!pssh_proto.ParseFromArray(pssh_info.pssh_data().data(),
116 pssh_info.pssh_data().size())) {
117 LOG(ERROR) <<
"Failed to parse protection_system_specific_data.";
120 if (!pssh_proto.has_provider() ||
121 (!pssh_proto.has_content_id() && pssh_proto.key_id_size() == 0)) {
122 LOG(ERROR) <<
"Missing fields to generate URI.";
126 base::DictionaryValue pssh_dict;
127 pssh_dict.SetString(
"provider", pssh_proto.provider());
128 if (pssh_proto.has_content_id()) {
129 std::string content_id_base64;
130 base::Base64Encode(base::StringPiece(pssh_proto.content_id().data(),
131 pssh_proto.content_id().size()),
133 pssh_dict.SetString(
"content_id", content_id_base64);
135 base::ListValue* key_ids =
new base::ListValue();
137 key_ids->AppendString(base::HexEncode(key_id.data(), key_id.size()));
138 for (
const std::string&
id : pssh_proto.key_id()) {
139 if (key_id.size() ==
id.size() &&
140 memcmp(key_id.data(),
id.data(),
id.size()) == 0) {
143 key_ids->AppendString(base::HexEncode(
id.data(),
id.size()));
145 pssh_dict.Set(
"key_ids", key_ids);
147 if (!base::JSONWriter::Write(pssh_dict, pssh_json)) {
148 LOG(ERROR) <<
"Failed to write to JSON.";
154 base::Optional<MediaPlaylist::EncryptionMethod> StringToEncryptionMethod(
155 const std::string& method) {
156 if (method ==
"cenc") {
157 return MediaPlaylist::EncryptionMethod::kSampleAesCenc;
158 }
else if (method ==
"cbcs") {
159 return MediaPlaylist::EncryptionMethod::kSampleAes;
160 }
else if (method ==
"cbca") {
162 return MediaPlaylist::EncryptionMethod::kSampleAes;
164 return base::nullopt;
168 void NotifyEncryptionToMediaPlaylist(
169 MediaPlaylist::EncryptionMethod encryption_method,
170 const std::string& uri,
171 const std::vector<uint8_t>& key_id,
172 const std::vector<uint8_t>& iv,
173 const std::string& key_format,
174 const std::string& key_format_version,
175 MediaPlaylist* media_playlist) {
176 std::string iv_string;
178 iv_string =
"0x" + base::HexEncode(iv.data(), iv.size());
180 std::string key_id_string;
181 if (!key_id.empty()) {
182 key_id_string =
"0x" + base::HexEncode(key_id.data(), key_id.size());
185 media_playlist->AddEncryptionInfo(
187 uri, key_id_string, iv_string,
188 key_format, key_format_version);
192 bool HandleWidevineKeyFormats(
193 MediaPlaylist::EncryptionMethod encryption_method,
194 const std::vector<uint8_t>& key_id,
195 const std::vector<uint8_t>& iv,
196 const std::vector<uint8_t>& protection_system_specific_data,
197 MediaPlaylist* media_playlist) {
198 if (encryption_method == MediaPlaylist::EncryptionMethod::kSampleAes) {
200 std::string key_uri_data;
201 if (!WidevinePsshToJson(protection_system_specific_data, key_id,
205 std::string key_uri_data_base64 =
206 Base64EncodeData(kUriBase64Prefix, key_uri_data);
207 NotifyEncryptionToMediaPlaylist(encryption_method, key_uri_data_base64,
208 std::vector<uint8_t>(), iv,
"com.widevine",
209 "1", media_playlist);
212 std::string pssh_as_string(
213 reinterpret_cast<const char*>(protection_system_specific_data.data()),
214 protection_system_specific_data.size());
215 std::string key_uri_data_base64 =
216 Base64EncodeData(kUriBase64Prefix, pssh_as_string);
217 NotifyEncryptionToMediaPlaylist(encryption_method, key_uri_data_base64,
218 key_id, iv, kWidevineDashIfIopUUID,
"1",
223 bool WriteMediaPlaylist(
const std::string& output_dir,
224 MediaPlaylist* playlist) {
225 std::string file_path =
226 FilePath::FromUTF8Unsafe(output_dir)
227 .Append(FilePath::FromUTF8Unsafe(playlist->file_name()))
229 if (!playlist->WriteToFile(file_path)) {
230 LOG(ERROR) <<
"Failed to write playlist " << file_path;
238 MediaPlaylistFactory::~MediaPlaylistFactory() {}
240 std::unique_ptr<MediaPlaylist> MediaPlaylistFactory::Create(
242 double time_shift_buffer_depth,
243 const std::string& file_name,
244 const std::string& name,
245 const std::string& group_id) {
246 return std::unique_ptr<MediaPlaylist>(
new MediaPlaylist(
247 type, time_shift_buffer_depth, file_name, name, group_id));
251 double time_shift_buffer_depth,
252 const std::string& prefix,
253 const std::string& output_dir,
254 const std::string& master_playlist_name)
256 time_shift_buffer_depth_(time_shift_buffer_depth),
258 output_dir_(output_dir),
262 SimpleHlsNotifier::~SimpleHlsNotifier() {}
269 const std::string& playlist_name,
270 const std::string& name,
271 const std::string& group_id,
272 uint32_t* stream_id) {
275 std::unique_ptr<MediaPlaylist> media_playlist =
276 media_playlist_factory_->Create(
playlist_type(), time_shift_buffer_depth_,
277 playlist_name, name, group_id);
280 MediaInfo media_info_copy = media_info;
281 if (media_info_copy.has_init_segment_name()) {
282 media_info_copy.set_init_segment_name(
283 GenerateSegmentUrl(media_info_copy.init_segment_name(), prefix_,
284 output_dir_, media_playlist->file_name()));
286 if (media_info_copy.has_media_file_name()) {
287 media_info_copy.set_media_file_name(
288 GenerateSegmentUrl(media_info_copy.media_file_name(), prefix_,
289 output_dir_, media_playlist->file_name()));
291 if (!media_playlist->SetMediaInfo(media_info_copy)) {
292 LOG(ERROR) <<
"Failed to set media info for playlist " << playlist_name;
296 MediaPlaylist::EncryptionMethod encryption_method =
297 MediaPlaylist::EncryptionMethod::kNone;
298 if (media_info.protected_content().has_protection_scheme()) {
299 const std::string& protection_scheme =
300 media_info.protected_content().protection_scheme();
301 base::Optional<MediaPlaylist::EncryptionMethod> enc_method =
302 StringToEncryptionMethod(protection_scheme);
304 LOG(ERROR) <<
"Failed to recognize protection scheme "
305 << protection_scheme;
308 encryption_method = enc_method.value();
311 *stream_id = sequence_number_.GetNext();
312 base::AutoLock auto_lock(lock_);
313 master_playlist_->AddMediaPlaylist(media_playlist.get());
314 stream_map_[*stream_id].reset(
315 new StreamEntry{std::move(media_playlist), encryption_method});
320 const std::string& segment_name,
323 uint64_t start_byte_offset,
325 base::AutoLock auto_lock(lock_);
326 auto stream_iterator = stream_map_.find(stream_id);
327 if (stream_iterator == stream_map_.end()) {
328 LOG(ERROR) <<
"Cannot find stream with ID: " << stream_id;
331 auto& media_playlist = stream_iterator->second->media_playlist;
332 const std::string& segment_url = GenerateSegmentUrl(
333 segment_name, prefix_, output_dir_, media_playlist->file_name());
334 media_playlist->AddSegment(segment_url, start_time, duration,
335 start_byte_offset, size);
338 uint32_t longest_segment_duration =
339 static_cast<uint32_t
>(ceil(media_playlist->GetLongestSegmentDuration()));
340 bool target_duration_updated =
false;
341 if (longest_segment_duration > target_duration_) {
342 target_duration_ = longest_segment_duration;
343 target_duration_updated =
true;
349 if (!master_playlist_->WriteMasterPlaylist(prefix_, output_dir_)) {
350 LOG(ERROR) <<
"Failed to write master playlist.";
354 if (target_duration_updated) {
355 for (
auto& streams : stream_map_) {
356 MediaPlaylist* playlist = streams.second->media_playlist.get();
358 if (!WriteMediaPlaylist(output_dir_, playlist))
362 return WriteMediaPlaylist(output_dir_, media_playlist.get());
370 const std::vector<uint8_t>& key_id,
371 const std::vector<uint8_t>& system_id,
372 const std::vector<uint8_t>& iv,
373 const std::vector<uint8_t>& protection_system_specific_data) {
374 base::AutoLock auto_lock(lock_);
375 auto stream_iterator = stream_map_.find(stream_id);
376 if (stream_iterator == stream_map_.end()) {
377 LOG(ERROR) <<
"Cannot find stream with ID: " << stream_id;
381 std::unique_ptr<MediaPlaylist>& media_playlist =
382 stream_iterator->second->media_playlist;
383 const MediaPlaylist::EncryptionMethod encryption_method =
384 stream_iterator->second->encryption_method;
385 LOG_IF(WARNING, encryption_method == MediaPlaylist::EncryptionMethod::kNone)
386 <<
"Got encryption notification but the encryption method is NONE";
387 if (IsWidevineSystemId(system_id)) {
388 return HandleWidevineKeyFormats(encryption_method,
389 key_id, iv, protection_system_specific_data,
390 media_playlist.get());
392 if (IsCommonSystemId(system_id)) {
395 std::string key_uri_data = VectorToString(key_id);
396 std::string key_uri_data_base64 =
397 Base64EncodeData(kUriBase64Prefix, key_uri_data);
398 NotifyEncryptionToMediaPlaylist(encryption_method,
399 key_uri_data_base64, std::vector<uint8_t>(),
400 iv,
"identity",
"", media_playlist.get());
404 if (IsFairplaySystemId(system_id)) {
407 std::string key_uri_data = VectorToString(key_id);
408 std::string key_uri_data_base64 =
409 Base64EncodeData(kUriFairplayPrefix, key_uri_data);
412 NotifyEncryptionToMediaPlaylist(encryption_method,
413 key_uri_data_base64, std::vector<uint8_t>(),
414 std::vector<uint8_t>(),
415 "com.apple.streamingkeydelivery",
"1",
416 media_playlist.get());
420 LOG(ERROR) <<
"Unknown system ID: "
421 << base::HexEncode(system_id.data(), system_id.size());
426 base::AutoLock auto_lock(lock_);
427 if (!master_playlist_->WriteMasterPlaylist(prefix_, output_dir_)) {
428 LOG(ERROR) <<
"Failed to write master playlist.";
431 for (
auto& streams : stream_map_) {
432 MediaPlaylist* playlist = streams.second->media_playlist.get();
434 if (!WriteMediaPlaylist(output_dir_, playlist))
SimpleHlsNotifier(HlsPlaylistType playlist_type, double time_shift_buffer_depth, const std::string &prefix, const std::string &output_dir, const std::string &master_playlist_name)
bool NotifyNewSegment(uint32_t stream_id, const std::string &segment_name, uint64_t start_time, uint64_t duration, uint64_t start_byte_offset, uint64_t size) override
HlsPlaylistType playlist_type() const
bool NotifyNewStream(const MediaInfo &media_info, const std::string &playlist_name, const std::string &stream_name, const std::string &group_id, uint32_t *stream_id) override
bool NotifyEncryptionUpdate(uint32_t stream_id, const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &system_id, const std::vector< uint8_t > &iv, const std::vector< uint8_t > &protection_system_specific_data) override