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