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