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