7 #include <packager/media/base/protection_system_specific_info.h>
11 #include <absl/log/check.h>
13 #include <packager/media/base/buffer_reader.h>
14 #include <packager/media/base/buffer_writer.h>
15 #include <packager/media/base/fourccs.h>
16 #include <packager/media/base/rcheck.h>
18 #define RETURN_NULL_IF_FALSE(x) \
21 LOG(ERROR) << "Failure while processing: " << #x; \
30 const size_t kSystemIdSize = 16u;
32 const size_t kPsshBoxHeaderSize = 12u;
33 const size_t kKeyIdSize = 16u;
39 std::vector<ProtectionSystemSpecificInfo>* pssh_infos) {
40 std::map<std::vector<uint8_t>,
size_t> info_map;
46 RCHECK(reader.Read4(&size));
48 RCHECK(size > kPsshBoxHeaderSize + kSystemIdSize);
50 const std::vector<uint8_t> system_id(
51 data + kPsshBoxHeaderSize, data + kPsshBoxHeaderSize + kSystemIdSize);
52 auto iter = info_map.find(system_id);
53 if (iter != info_map.end()) {
55 info.psshs.insert(info.psshs.end(), data, data + size);
57 pssh_infos->push_back(
58 {system_id, std::vector<uint8_t>(data, data + size)});
59 info_map[system_id] = pssh_infos->size() - 1;
76 uint32_t version_and_flags;
77 RETURN_NULL_IF_FALSE(reader.Read4(&size));
78 RETURN_NULL_IF_FALSE(reader.Read4(&box_type));
79 RETURN_NULL_IF_FALSE(box_type == FOURCC_pssh);
80 RETURN_NULL_IF_FALSE(reader.Read4(&version_and_flags));
82 pssh_builder->version_ = (version_and_flags >> 24);
83 RETURN_NULL_IF_FALSE(pssh_builder->version_ < 2);
86 reader.ReadToVector(&pssh_builder->system_id_, kSystemIdSize));
88 if (pssh_builder->version_ == 1) {
89 uint32_t key_id_count;
90 RETURN_NULL_IF_FALSE(reader.Read4(&key_id_count));
92 pssh_builder->key_ids_.resize(key_id_count);
93 for (uint32_t i = 0; i < key_id_count; i++) {
95 reader.ReadToVector(&pssh_builder->key_ids_[i], kKeyIdSize));
100 uint32_t pssh_data_size;
101 RETURN_NULL_IF_FALSE(reader.Read4(&pssh_data_size));
102 RETURN_NULL_IF_FALSE(
103 reader.ReadToVector(&pssh_builder->pssh_data_, pssh_data_size));
110 DCHECK_EQ(kSystemIdSize, system_id_.size());
112 const uint32_t box_type = FOURCC_pssh;
113 const uint32_t version_and_flags = (
static_cast<uint32_t
>(version_) << 24);
114 const uint32_t pssh_data_size = pssh_data_.size();
116 const uint32_t key_id_count = key_ids_.size();
117 const uint32_t key_ids_size =
118 sizeof(key_id_count) + kKeyIdSize * key_id_count;
119 const uint32_t extra_size = version_ == 1 ? key_ids_size : 0;
121 const uint32_t total_size =
122 sizeof(total_size) +
sizeof(box_type) +
sizeof(version_and_flags) +
123 kSystemIdSize + extra_size +
sizeof(pssh_data_size) + pssh_data_size;
129 writer.AppendVector(system_id_);
132 for (
size_t i = 0; i < key_id_count; i++) {
133 DCHECK_EQ(kKeyIdSize, key_ids_[i].size());
134 writer.AppendVector(key_ids_[i]);
138 writer.AppendVector(pssh_data_);
140 DCHECK_EQ(total_size, writer.Size());
141 return std::vector<uint8_t>(writer.
Buffer(), writer.
Buffer() + writer.Size());
All the methods that are virtual are virtual for mocking.