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 // Returns a string of kDecryptionKeySize bytes. |
18 static scoped_array<uint8> GenerateCounterBlock(uint64 iv) { | 18 static std::string GenerateCounterBlock(uint64 iv) { |
19 scoped_array<uint8> counter_block_data( | 19 std::string counter_block(reinterpret_cast<char*>(&iv), sizeof(iv)); |
20 new uint8[DecryptConfig::kDecryptionKeySize]); | 20 counter_block.append(DecryptConfig::kDecryptionKeySize - sizeof(iv), 0); |
21 | 21 return counter_block; |
22 // Set the IV. | |
23 memcpy(counter_block_data.get(), &iv, sizeof(iv)); | |
24 | |
25 // Set block counter to all 0's. | |
26 memset(counter_block_data.get() + sizeof(iv), | |
27 0, | |
28 DecryptConfig::kDecryptionKeySize - sizeof(iv)); | |
29 | |
30 return counter_block_data.Pass(); | |
31 } | 22 } |
32 | 23 |
33 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 24 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
34 int audio_track_num, | 25 int audio_track_num, |
35 int video_track_num, | 26 int video_track_num, |
36 const uint8* video_encryption_key_id, | 27 const uint8* video_encryption_key_id, |
37 int video_encryption_key_id_size) | 28 int video_encryption_key_id_size) |
38 : timecode_multiplier_(timecode_scale / 1000.0), | 29 : timecode_multiplier_(timecode_scale / 1000.0), |
39 video_encryption_key_id_size_(video_encryption_key_id_size), | 30 video_encryption_key_id_size_(video_encryption_key_id_size), |
40 parser_(kWebMIdCluster, this), | 31 parser_(kWebMIdCluster, this), |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 } | 204 } |
214 | 205 |
215 last_block_timecode_ = timecode; | 206 last_block_timecode_ = timecode; |
216 | 207 |
217 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 208 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
218 (cluster_timecode_ + timecode) * timecode_multiplier_); | 209 (cluster_timecode_ + timecode) * timecode_multiplier_); |
219 | 210 |
220 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted | 211 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted |
221 // WebM request for comments specification is here | 212 // WebM request for comments specification is here |
222 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | 213 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
223 bool encrypted = track_num == video_.track_num() && | 214 bool is_track_encrypted = track_num == video_.track_num() && |
224 video_encryption_key_id_.get(); | 215 video_encryption_key_id_.get(); |
225 // If encrypted skip past the HMAC. Encrypted buffers must include the IV and | 216 |
226 // the encrypted frame because the decryptor will verify this data before | 217 // If stream is encrypted skip past the HMAC. Encrypted buffers must include |
227 // decryption. The HMAC and IV will be copied into DecryptConfig. | 218 // the signal byte, the IV (if frame is encrypted) and |
228 int offset = (encrypted) ? kWebMHmacSize : 0; | 219 // the frame because the decryptor will verify this data before decryption. |
| 220 // The HMAC and IV will be copied into DecryptConfig. |
| 221 int offset = (is_track_encrypted) ? kWebMHmacSize : 0; |
229 | 222 |
230 // The first bit of the flags is set when the block contains only keyframes. | 223 // The first bit of the flags is set when the block contains only keyframes. |
231 // http://www.matroska.org/technical/specs/index.html | 224 // http://www.matroska.org/technical/specs/index.html |
232 bool is_keyframe = (flags & 0x80) != 0; | 225 bool is_keyframe = (flags & 0x80) != 0; |
233 scoped_refptr<StreamParserBuffer> buffer = | 226 scoped_refptr<StreamParserBuffer> buffer = |
234 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); | 227 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); |
235 | 228 |
236 if (encrypted) { | 229 if (is_track_encrypted) { |
237 uint64 network_iv; | 230 uint8 signal_byte = data[kWebMHmacSize]; |
238 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); | 231 int data_offset = sizeof(signal_byte); |
239 const uint64 iv = base::NetToHost64(network_iv); | |
240 | 232 |
241 scoped_array<uint8> counter_block(GenerateCounterBlock(iv)); | 233 // Setting the DecryptConfig object of the buffer while leaving the |
| 234 // initialization vector empty will tell the decryptor that the frame is |
| 235 // unencrypted but integrity should still be checked. |
| 236 std::string counter_block; |
| 237 |
| 238 if (signal_byte & kWebMFlagEncryptedFrame) { |
| 239 uint64 network_iv; |
| 240 memcpy(&network_iv, data + kWebMHmacSize + data_offset, |
| 241 sizeof(network_iv)); |
| 242 const uint64 iv = base::NetToHost64(network_iv); |
| 243 counter_block = GenerateCounterBlock(iv); |
| 244 data_offset += sizeof(iv); |
| 245 } |
| 246 |
242 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 247 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
243 std::string( | 248 std::string( |
244 reinterpret_cast<const char*>(video_encryption_key_id_.get()), | 249 reinterpret_cast<const char*>(video_encryption_key_id_.get()), |
245 video_encryption_key_id_size_), | 250 video_encryption_key_id_size_), |
246 std::string( | 251 counter_block, |
247 reinterpret_cast<const char*>(counter_block.get()), | |
248 DecryptConfig::kDecryptionKeySize), | |
249 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), | 252 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), |
250 sizeof(iv), | 253 data_offset, |
251 std::vector<SubsampleEntry>()))); | 254 std::vector<SubsampleEntry>()))); |
252 } | 255 } |
253 | 256 |
254 buffer->SetTimestamp(timestamp); | 257 buffer->SetTimestamp(timestamp); |
255 if (cluster_start_time_ == kNoTimestamp()) | 258 if (cluster_start_time_ == kNoTimestamp()) |
256 cluster_start_time_ = timestamp; | 259 cluster_start_time_ = timestamp; |
257 | 260 |
258 if (block_duration >= 0) { | 261 if (block_duration >= 0) { |
259 buffer->SetDuration(base::TimeDelta::FromMicroseconds( | 262 buffer->SetDuration(base::TimeDelta::FromMicroseconds( |
260 block_duration * timecode_multiplier_)); | 263 block_duration * timecode_multiplier_)); |
(...skipping 25 matching lines...) Expand all Loading... |
286 | 289 |
287 buffers_.push_back(buffer); | 290 buffers_.push_back(buffer); |
288 return true; | 291 return true; |
289 } | 292 } |
290 | 293 |
291 void WebMClusterParser::Track::Reset() { | 294 void WebMClusterParser::Track::Reset() { |
292 buffers_.clear(); | 295 buffers_.clear(); |
293 } | 296 } |
294 | 297 |
295 } // namespace media | 298 } // namespace media |
OLD | NEW |