2016-02-10 17:51:15 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "packager/media/base/protection_system_specific_info.h"
|
|
|
|
|
2018-05-03 00:54:10 +00:00
|
|
|
#include "packager/media/base/buffer_reader.h"
|
2016-02-10 17:51:15 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
2016-04-06 23:21:45 +00:00
|
|
|
#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) {
|
|
|
|
pssh_infos->clear();
|
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-03 00:54:10 +00:00
|
|
|
pssh_infos->push_back(
|
|
|
|
{std::vector<uint8_t>(data + kPsshBoxHeaderSize,
|
|
|
|
data + kPsshBoxHeaderSize + kSystemIdSize),
|
|
|
|
std::vector<uint8_t>(data, data + size)});
|
|
|
|
|
|
|
|
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(size == data_size);
|
|
|
|
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
|
|
|
|
|
|
|
// We should be at the end of the data. The reader should be initialized to
|
|
|
|
// the data and size according to the size field of the box; therefore it
|
|
|
|
// is an error if there are bytes remaining.
|
2018-05-03 00:54:10 +00:00
|
|
|
RETURN_NULL_IF_FALSE(!reader.HasBytes(1));
|
|
|
|
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
|