2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-02-10 17:51:15 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/protection_system_specific_info.h>
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
#include <map>
|
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/buffer_reader.h>
|
|
|
|
#include <packager/media/base/buffer_writer.h>
|
|
|
|
#include <packager/media/base/fourccs.h>
|
|
|
|
#include <packager/media/base/rcheck.h>
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
#define RETURN_NULL_IF_FALSE(x) \
|
|
|
|
do { \
|
|
|
|
if (!(x)) { \
|
|
|
|
LOG(ERROR) << "Failure while processing: " << #x; \
|
|
|
|
return nullptr; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-02-10 17:51:15 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const size_t kSystemIdSize = 16u;
|
2018-05-03 00:54:10 +00:00
|
|
|
// 4-byte size, 4-byte fourcc, 4-byte version_and_flags.
|
|
|
|
const size_t kPsshBoxHeaderSize = 12u;
|
2016-02-10 17:51:15 +00:00
|
|
|
const size_t kKeyIdSize = 16u;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool ProtectionSystemSpecificInfo::ParseBoxes(
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size,
|
2018-05-03 00:54:10 +00:00
|
|
|
std::vector<ProtectionSystemSpecificInfo>* pssh_infos) {
|
2018-05-11 00:19:28 +00:00
|
|
|
std::map<std::vector<uint8_t>, size_t> info_map;
|
2018-05-03 00:54:10 +00:00
|
|
|
pssh_infos->clear();
|
2018-05-11 00:19:28 +00:00
|
|
|
|
2016-02-10 17:51:15 +00:00
|
|
|
BufferReader reader(data, data_size);
|
|
|
|
while (reader.HasBytes(1)) {
|
|
|
|
uint32_t size;
|
|
|
|
RCHECK(reader.Read4(&size));
|
|
|
|
RCHECK(reader.SkipBytes(size - 4));
|
2018-05-03 00:54:10 +00:00
|
|
|
RCHECK(size > kPsshBoxHeaderSize + kSystemIdSize);
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
const std::vector<uint8_t> system_id(
|
|
|
|
data + kPsshBoxHeaderSize, data + kPsshBoxHeaderSize + kSystemIdSize);
|
|
|
|
auto iter = info_map.find(system_id);
|
|
|
|
if (iter != info_map.end()) {
|
|
|
|
ProtectionSystemSpecificInfo& info = (*pssh_infos)[iter->second];
|
|
|
|
info.psshs.insert(info.psshs.end(), data, data + size);
|
|
|
|
} else {
|
|
|
|
pssh_infos->push_back(
|
|
|
|
{system_id, std::vector<uint8_t>(data, data + size)});
|
|
|
|
info_map[system_id] = pssh_infos->size() - 1;
|
|
|
|
}
|
2018-05-03 00:54:10 +00:00
|
|
|
|
|
|
|
data += size;
|
2016-02-10 17:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
std::unique_ptr<PsshBoxBuilder> PsshBoxBuilder::ParseFromBox(
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size) {
|
|
|
|
std::unique_ptr<PsshBoxBuilder> pssh_builder(new PsshBoxBuilder);
|
|
|
|
BufferReader reader(data, data_size);
|
|
|
|
|
2016-02-10 17:51:15 +00:00
|
|
|
uint32_t size;
|
|
|
|
uint32_t box_type;
|
|
|
|
uint32_t version_and_flags;
|
2018-05-03 00:54:10 +00:00
|
|
|
RETURN_NULL_IF_FALSE(reader.Read4(&size));
|
|
|
|
RETURN_NULL_IF_FALSE(reader.Read4(&box_type));
|
|
|
|
RETURN_NULL_IF_FALSE(box_type == FOURCC_pssh);
|
|
|
|
RETURN_NULL_IF_FALSE(reader.Read4(&version_and_flags));
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
pssh_builder->version_ = (version_and_flags >> 24);
|
|
|
|
RETURN_NULL_IF_FALSE(pssh_builder->version_ < 2);
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
RETURN_NULL_IF_FALSE(
|
|
|
|
reader.ReadToVector(&pssh_builder->system_id_, kSystemIdSize));
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
if (pssh_builder->version_ == 1) {
|
2016-02-10 17:51:15 +00:00
|
|
|
uint32_t key_id_count;
|
2018-05-03 00:54:10 +00:00
|
|
|
RETURN_NULL_IF_FALSE(reader.Read4(&key_id_count));
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
pssh_builder->key_ids_.resize(key_id_count);
|
2016-02-10 17:51:15 +00:00
|
|
|
for (uint32_t i = 0; i < key_id_count; i++) {
|
2018-05-03 00:54:10 +00:00
|
|
|
RETURN_NULL_IF_FALSE(
|
|
|
|
reader.ReadToVector(&pssh_builder->key_ids_[i], kKeyIdSize));
|
2016-02-10 17:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Consider parsing key IDs from Widevine PSSH data.
|
|
|
|
uint32_t pssh_data_size;
|
2018-05-03 00:54:10 +00:00
|
|
|
RETURN_NULL_IF_FALSE(reader.Read4(&pssh_data_size));
|
|
|
|
RETURN_NULL_IF_FALSE(
|
|
|
|
reader.ReadToVector(&pssh_builder->pssh_data_, pssh_data_size));
|
2016-02-10 17:51:15 +00:00
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
// Ignore extra data if there is any.
|
2018-05-03 00:54:10 +00:00
|
|
|
return pssh_builder;
|
2016-02-10 17:51:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
std::vector<uint8_t> PsshBoxBuilder::CreateBox() const {
|
2016-02-10 17:51:15 +00:00
|
|
|
DCHECK_EQ(kSystemIdSize, system_id_.size());
|
|
|
|
|
2016-04-06 23:21:45 +00:00
|
|
|
const uint32_t box_type = FOURCC_pssh;
|
2016-02-10 17:51:15 +00:00
|
|
|
const uint32_t version_and_flags = (static_cast<uint32_t>(version_) << 24);
|
|
|
|
const uint32_t pssh_data_size = pssh_data_.size();
|
|
|
|
|
|
|
|
const uint32_t key_id_count = key_ids_.size();
|
|
|
|
const uint32_t key_ids_size =
|
|
|
|
sizeof(key_id_count) + kKeyIdSize * key_id_count;
|
|
|
|
const uint32_t extra_size = version_ == 1 ? key_ids_size : 0;
|
|
|
|
|
|
|
|
const uint32_t total_size =
|
|
|
|
sizeof(total_size) + sizeof(box_type) + sizeof(version_and_flags) +
|
|
|
|
kSystemIdSize + extra_size + sizeof(pssh_data_size) + pssh_data_size;
|
|
|
|
|
|
|
|
BufferWriter writer;
|
|
|
|
writer.AppendInt(total_size);
|
|
|
|
writer.AppendInt(box_type);
|
|
|
|
writer.AppendInt(version_and_flags);
|
|
|
|
writer.AppendVector(system_id_);
|
|
|
|
if (version_ == 1) {
|
|
|
|
writer.AppendInt(key_id_count);
|
|
|
|
for (size_t i = 0; i < key_id_count; i++) {
|
|
|
|
DCHECK_EQ(kKeyIdSize, key_ids_[i].size());
|
|
|
|
writer.AppendVector(key_ids_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.AppendInt(pssh_data_size);
|
|
|
|
writer.AppendVector(pssh_data_);
|
|
|
|
|
|
|
|
DCHECK_EQ(total_size, writer.Size());
|
|
|
|
return std::vector<uint8_t>(writer.Buffer(), writer.Buffer() + writer.Size());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|