OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/webm/webm_cluster_parser.h" | 5 #include "media/webm/webm_cluster_parser.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
9 #include "media/base/data_buffer.h" | 9 #include "media/base/data_buffer.h" |
10 #include "media/base/decrypt_config.h" | 10 #include "media/base/decrypt_config.h" |
11 #include "media/webm/webm_constants.h" | 11 #include "media/webm/webm_constants.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
14 | 14 |
15 // Generates a 16 byte CTR counter block. The CTR counter block format is a | 15 // Generates a 16 byte CTR counter block. The CTR counter block format is a |
16 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. | 16 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. |
17 // Always returns a valid pointer to a buffer of kDecryptionKeySize bytes. | 17 // Always returns a valid pointer to a buffer of kDecryptionKeySize bytes. |
18 static scoped_array<uint8> GenerateCounterBlock(uint64 iv) { | 18 static scoped_array<uint8> GenerateCounterBlock(uint64 iv) { |
xhwang
2012/08/01 00:45:41
CounterBlock is only 16bytes, let's just make it a
fgalligan1
2012/08/01 01:36:10
Done.
| |
19 scoped_array<uint8> counter_block_data( | 19 scoped_array<uint8> counter_block_data( |
20 new uint8[DecryptConfig::kDecryptionKeySize]); | 20 new uint8[DecryptConfig::kDecryptionKeySize]); |
21 | 21 |
22 // Set the IV. | 22 // Set the IV. |
23 memcpy(counter_block_data.get(), &iv, sizeof(iv)); | 23 memcpy(counter_block_data.get(), &iv, sizeof(iv)); |
24 | 24 |
25 // Set block counter to all 0's. | 25 // Set block counter to all 0's. |
26 memset(counter_block_data.get() + sizeof(iv), | 26 memset(counter_block_data.get() + sizeof(iv), |
27 0, | 27 0, |
28 DecryptConfig::kDecryptionKeySize - sizeof(iv)); | 28 DecryptConfig::kDecryptionKeySize - sizeof(iv)); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 } | 213 } |
214 | 214 |
215 last_block_timecode_ = timecode; | 215 last_block_timecode_ = timecode; |
216 | 216 |
217 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 217 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
218 (cluster_timecode_ + timecode) * timecode_multiplier_); | 218 (cluster_timecode_ + timecode) * timecode_multiplier_); |
219 | 219 |
220 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted | 220 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted |
221 // WebM request for comments specification is here | 221 // WebM request for comments specification is here |
222 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | 222 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
223 bool encrypted = track_num == video_.track_num() && | 223 bool encrypted_stream = track_num == video_.track_num() && |
xhwang
2012/08/01 00:45:41
"encrypted_stream" is a noun. How about something
fgalligan1
2012/08/01 01:36:10
Done.
| |
224 video_encryption_key_id_.get(); | 224 video_encryption_key_id_.get(); |
225 // If encrypted skip past the HMAC. Encrypted buffers must include the IV and | 225 |
226 // the encrypted frame because the decryptor will verify this data before | 226 // If stream is encrypted skip past the HMAC. Encrypted buffers must include |
227 // decryption. The HMAC and IV will be copied into DecryptConfig. | 227 // the signal byte, the IV (if frame is encrypted) and |
228 int offset = (encrypted) ? kWebMHmacSize : 0; | 228 // the frame because the decryptor will verify this data before decryption. |
229 // The HMAC and IV will be copied into DecryptConfig. | |
230 int offset = (encrypted_stream) ? kWebMHmacSize : 0; | |
229 | 231 |
230 // The first bit of the flags is set when the block contains only keyframes. | 232 // The first bit of the flags is set when the block contains only keyframes. |
231 // http://www.matroska.org/technical/specs/index.html | 233 // http://www.matroska.org/technical/specs/index.html |
232 bool is_keyframe = (flags & 0x80) != 0; | 234 bool is_keyframe = (flags & 0x80) != 0; |
233 scoped_refptr<StreamParserBuffer> buffer = | 235 scoped_refptr<StreamParserBuffer> buffer = |
234 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); | 236 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); |
235 | 237 |
236 if (encrypted) { | 238 if (encrypted_stream) { |
237 uint64 network_iv; | 239 int data_offset = 1; |
238 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); | 240 uint8 signal_byte = data[kWebMHmacSize]; |
xhwang
2012/08/01 00:45:41
How about moving "int data_offset = 1" after this
fgalligan1
2012/08/01 01:36:10
Done.
| |
239 const uint64 iv = base::NetToHost64(network_iv); | |
240 | 241 |
241 scoped_array<uint8> counter_block(GenerateCounterBlock(iv)); | 242 // Setting the DecryptConfig object of the buffer while leaving the |
243 // initialization vector empty will tell the decryptor that the frame is | |
244 // unencrypted but integrity should still be checked. | |
245 std::string counter_block_str; | |
246 | |
247 if (signal_byte & kWebMEncryptedFrame) { | |
248 uint64 network_iv; | |
249 memcpy(&network_iv, data + kWebMHmacSize + 1, sizeof(network_iv)); | |
xhwang
2012/08/01 00:45:41
s/1/data_offset/
fgalligan1
2012/08/01 01:36:10
Done.
| |
250 const uint64 iv = base::NetToHost64(network_iv); | |
251 | |
252 scoped_array<uint8> counter_block(GenerateCounterBlock(iv)); | |
253 counter_block_str.assign( | |
254 reinterpret_cast<const char*>(counter_block.get()), | |
255 DecryptConfig::kDecryptionKeySize); | |
xhwang
2012/08/01 00:45:41
This code will be much easier if GenerateCounterBl
fgalligan1
2012/08/01 01:36:10
Done.
| |
256 data_offset += sizeof(iv); | |
257 } | |
258 | |
242 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 259 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
243 std::string( | 260 std::string( |
244 reinterpret_cast<const char*>(video_encryption_key_id_.get()), | 261 reinterpret_cast<const char*>(video_encryption_key_id_.get()), |
245 video_encryption_key_id_size_), | 262 video_encryption_key_id_size_), |
246 std::string( | 263 counter_block_str, |
247 reinterpret_cast<const char*>(counter_block.get()), | |
248 DecryptConfig::kDecryptionKeySize), | |
249 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), | 264 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), |
250 sizeof(iv), | 265 data_offset, |
251 std::vector<SubsampleEntry>()))); | 266 std::vector<SubsampleEntry>()))); |
252 } | 267 } |
253 | 268 |
254 buffer->SetTimestamp(timestamp); | 269 buffer->SetTimestamp(timestamp); |
255 if (cluster_start_time_ == kNoTimestamp()) | 270 if (cluster_start_time_ == kNoTimestamp()) |
256 cluster_start_time_ = timestamp; | 271 cluster_start_time_ = timestamp; |
257 | 272 |
258 if (block_duration >= 0) { | 273 if (block_duration >= 0) { |
259 buffer->SetDuration(base::TimeDelta::FromMicroseconds( | 274 buffer->SetDuration(base::TimeDelta::FromMicroseconds( |
260 block_duration * timecode_multiplier_)); | 275 block_duration * timecode_multiplier_)); |
(...skipping 25 matching lines...) Expand all Loading... | |
286 | 301 |
287 buffers_.push_back(buffer); | 302 buffers_.push_back(buffer); |
288 return true; | 303 return true; |
289 } | 304 } |
290 | 305 |
291 void WebMClusterParser::Track::Reset() { | 306 void WebMClusterParser::Track::Reset() { |
292 buffers_.clear(); | 307 buffers_.clear(); |
293 } | 308 } |
294 | 309 |
295 } // namespace media | 310 } // namespace media |
OLD | NEW |