OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/cdm/browser/widevine_drm_delegate_android.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/numerics/safe_conversions.h" | |
9 | |
10 namespace cdm { | |
11 | |
12 namespace { | |
13 | |
14 uint32_t ReadUint32(const uint8_t* data) { | |
15 uint32_t value = 0; | |
16 for (int i = 0; i < 4; ++i) | |
17 value = (value << 8) | data[i]; | |
18 return value; | |
19 } | |
20 | |
21 uint64_t ReadUint64(const uint8_t* data) { | |
22 uint64_t value = 0; | |
23 for (int i = 0; i < 8; ++i) | |
24 value = (value << 8) | data[i]; | |
25 return value; | |
26 } | |
27 | |
28 // The structure of an ISO CENC Protection System Specific Header (PSSH) box is | |
29 // as follows. (See ISO/IEC FDIS 23001-7:2011(E).) | |
30 // Note: ISO boxes use big-endian values. | |
31 // | |
32 // PSSH { | |
33 // uint32_t Size | |
34 // uint32_t Type | |
35 // uint64_t LargeSize # Field is only present if value(Size) == 1. | |
36 // uint32_t VersionAndFlags | |
37 // uint8_t[16] SystemId | |
38 // uint32_t DataSize | |
39 // uint8_t[DataSize] Data | |
40 // } | |
41 const int kBoxHeaderSize = 8; // Box's header contains Size and Type. | |
42 const int kBoxLargeSizeSize = 8; | |
43 const int kPsshVersionFlagSize = 4; | |
44 const int kPsshSystemIdSize = 16; | |
45 const int kPsshDataSizeSize = 4; | |
46 const uint32_t kTencType = 0x74656e63; | |
47 const uint32_t kPsshType = 0x70737368; | |
48 | |
49 const uint8_t kWidevineUuid[16] = { | |
50 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE, | |
51 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED }; | |
52 | |
53 // Tries to find a PSSH box with the Widevine UUID, parses the | |
54 // "Data" of the box and put it in |pssh_data|. Returns true if such a box is | |
55 // found and successfully parsed. Returns false otherwise. | |
56 // Notes: | |
57 // 1, If multiple PSSH boxes are found,the "Data" of the first matching PSSH box | |
58 // will be set in |pssh_data|. | |
59 // 2, Only PSSH and TENC boxes are allowed in |data|. TENC boxes are skipped. | |
60 bool GetPsshData(const std::vector<uint8_t>& data, | |
61 std::vector<uint8_t>* pssh_data) { | |
62 int bytes_left = base::checked_cast<int>(data.size()); | |
63 const uint8_t* cur = &data[0]; | |
64 const uint8_t* data_end = cur + bytes_left; | |
65 | |
66 while (bytes_left > 0) { | |
67 const uint8* box_head = cur; | |
xhwang
2015/04/28 05:25:11
uint8_t
| |
68 | |
69 if (bytes_left < kBoxHeaderSize) | |
70 return false; | |
71 | |
72 uint64_t box_size = ReadUint32(cur); | |
73 uint32 type = ReadUint32(cur + 4); | |
xhwang
2015/04/28 05:25:11
Now it's recommended to use *_t integer types. So
gunsch
2015/04/28 18:50:51
Oops, thanks for catching---I copied the new imple
| |
74 cur += kBoxHeaderSize; | |
75 bytes_left -= kBoxHeaderSize; | |
76 | |
77 if (box_size == 1) { // LargeSize is present. | |
78 if (bytes_left < kBoxLargeSizeSize) | |
79 return false; | |
80 | |
81 box_size = ReadUint64(cur); | |
82 cur += kBoxLargeSizeSize; | |
83 bytes_left -= kBoxLargeSizeSize; | |
84 } else if (box_size == 0) { | |
85 box_size = bytes_left + kBoxHeaderSize; | |
86 } | |
87 | |
88 const uint8* box_end = box_head + box_size; | |
xhwang
2015/04/28 05:25:11
ditto
gunsch
2015/04/28 18:50:51
Done.
| |
89 if (data_end < box_end) | |
90 return false; | |
91 | |
92 if (type == kTencType) { | |
93 // Skip 'tenc' box. | |
94 cur = box_end; | |
95 bytes_left = data_end - cur; | |
96 continue; | |
97 } else if (type != kPsshType) { | |
98 return false; | |
99 } | |
100 | |
101 const int kPsshBoxMinimumSize = | |
102 kPsshVersionFlagSize + kPsshSystemIdSize + kPsshDataSizeSize; | |
103 if (box_end < cur + kPsshBoxMinimumSize) | |
104 return false; | |
105 | |
106 uint32 version_and_flags = ReadUint32(cur); | |
xhwang
2015/04/28 05:25:11
ditto
gunsch
2015/04/28 18:50:51
Done.
| |
107 cur += kPsshVersionFlagSize; | |
108 bytes_left -= kPsshVersionFlagSize; | |
109 if (version_and_flags != 0) | |
110 return false; | |
111 | |
112 DCHECK_GE(bytes_left, kPsshSystemIdSize); | |
113 if (!std::equal(kWidevineUuid, | |
114 kWidevineUuid + sizeof(kWidevineUuid), cur)) { | |
115 cur = box_end; | |
116 bytes_left = data_end - cur; | |
117 continue; | |
118 } | |
119 | |
120 cur += kPsshSystemIdSize; | |
121 bytes_left -= kPsshSystemIdSize; | |
122 | |
123 uint32 data_size = ReadUint32(cur); | |
xhwang
2015/04/28 05:25:11
ditto
gunsch
2015/04/28 18:50:51
Done.
| |
124 cur += kPsshDataSizeSize; | |
125 bytes_left -= kPsshDataSizeSize; | |
126 | |
127 if (box_end < cur + data_size) | |
128 return false; | |
129 | |
130 pssh_data->assign(cur, cur + data_size); | |
131 return true; | |
132 } | |
133 | |
134 return false; | |
135 } | |
136 | |
137 } | |
138 | |
139 WidevineDrmDelegateAndroid::WidevineDrmDelegateAndroid() { | |
140 } | |
141 | |
142 WidevineDrmDelegateAndroid::~WidevineDrmDelegateAndroid() { | |
143 } | |
144 | |
145 const std::vector<uint8_t> WidevineDrmDelegateAndroid::GetUUID() const { | |
146 return std::vector<uint8_t>(kWidevineUuid, | |
147 kWidevineUuid + arraysize(kWidevineUuid)); | |
148 } | |
149 | |
150 bool WidevineDrmDelegateAndroid::OnCreateSession( | |
151 const media::EmeInitDataType init_data_type, | |
152 const std::vector<uint8_t>& init_data, | |
153 std::vector<uint8_t>* init_data_out, | |
154 std::vector<std::string>* /* optional_parameters_out */) { | |
155 if (init_data_type != media::EmeInitDataType::CENC) | |
156 return true; | |
157 | |
158 // Widevine MediaDrm plugin only accepts the "data" part of the PSSH box as | |
159 // the init data when using MP4 container. | |
160 return GetPsshData(init_data, init_data_out); | |
161 } | |
162 | |
163 } // namespace cdm | |
OLD | NEW |