Shaka Packager SDK
Loading...
Searching...
No Matches
pssh_generator.cc
1// Copyright 2017 Google LLC. 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/media/base/pssh_generator.h>
8
9#include <cstdint>
10#include <optional>
11#include <vector>
12
13#include <packager/media/base/protection_system_specific_info.h>
14#include <packager/status.h>
15
16namespace shaka {
17namespace media {
18namespace {
19
20std::vector<uint8_t> CreatePsshBox(
21 const std::vector<uint8_t>& system_id,
22 uint8_t version,
23 const std::vector<std::vector<uint8_t>>& key_ids,
24 const std::vector<uint8_t>& pssh_data) {
25 PsshBoxBuilder pssh_builder;
26 pssh_builder.set_pssh_data(pssh_data);
27 for (const std::vector<uint8_t>& key_id : key_ids) {
28 pssh_builder.add_key_id(key_id);
29 }
30 pssh_builder.set_pssh_box_version(version);
31 pssh_builder.set_system_id(system_id.data(), system_id.size());
32
33 return pssh_builder.CreateBox();
34}
35
36} // namespace
37
38PsshGenerator::PsshGenerator(const std::vector<uint8_t>& system_id,
39 uint8_t box_version)
40 : system_id_(system_id), box_version_(box_version) {}
41
42PsshGenerator::~PsshGenerator() = default;
43
45 const std::vector<std::vector<uint8_t>>& key_ids,
46 ProtectionSystemSpecificInfo* info) const {
47 std::optional<std::vector<uint8_t>> pssh_data =
48 GeneratePsshDataFromKeyIds(key_ids);
49 if (!pssh_data) {
50 return Status(error::ENCRYPTION_FAILURE,
51 "Fail to generate PSSH data from multiple Key IDs.");
52 }
53 info->system_id = system_id_;
54 info->psshs =
55 CreatePsshBox(system_id_, box_version_, key_ids, pssh_data.value());
56 return Status::OK;
57}
58
60 const std::vector<uint8_t>& key_id,
61 const std::vector<uint8_t>& key,
62 ProtectionSystemSpecificInfo* info) const {
63 std::optional<std::vector<uint8_t>> pssh_data =
64 GeneratePsshDataFromKeyIdAndKey(key_id, key);
65 if (!pssh_data) {
66 return Status(error::ENCRYPTION_FAILURE,
67 "Fail to generate PSSH data from Key ID and Key.");
68 }
69 info->system_id = system_id_;
70 info->psshs =
71 CreatePsshBox(system_id_, box_version_, {key_id}, pssh_data.value());
72 return Status::OK;
73}
74
75} // namespace media
76} // namespace shaka
Status GeneratePsshFromKeyIds(const std::vector< std::vector< uint8_t > > &key_ids, ProtectionSystemSpecificInfo *info) const
PsshGenerator(const std::vector< uint8_t > &system_id, uint8_t box_version)
Status GeneratePsshFromKeyIdAndKey(const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &key, ProtectionSystemSpecificInfo *info) const
All the methods that are virtual are virtual for mocking.