| 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 "media/base/data_buffer.h" | 9 #include "media/base/data_buffer.h" |
| 9 #include "media/base/decrypt_config.h" | 10 #include "media/base/decrypt_config.h" |
| 10 #include "media/webm/webm_constants.h" | 11 #include "media/webm/webm_constants.h" |
| 11 | 12 |
| 12 namespace media { | 13 namespace media { |
| 13 | 14 |
| 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. |
| 17 // Always returns a valid pointer to a buffer of kDecryptionKeySize bytes. |
| 18 static scoped_array<uint8> GenerateCounterBlock(uint64 iv) { |
| 19 scoped_array<uint8> counter_block_data( |
| 20 new uint8[DecryptConfig::kDecryptionKeySize]); |
| 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(); |
| 31 } |
| 32 |
| 14 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 33 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
| 15 int audio_track_num, | 34 int audio_track_num, |
| 16 int video_track_num, | 35 int video_track_num, |
| 17 const uint8* video_encryption_key_id, | 36 const uint8* video_encryption_key_id, |
| 18 int video_encryption_key_id_size) | 37 int video_encryption_key_id_size) |
| 19 : timecode_multiplier_(timecode_scale / 1000.0), | 38 : timecode_multiplier_(timecode_scale / 1000.0), |
| 20 video_encryption_key_id_size_(video_encryption_key_id_size), | 39 video_encryption_key_id_size_(video_encryption_key_id_size), |
| 21 parser_(kWebMIdCluster, this), | 40 parser_(kWebMIdCluster, this), |
| 22 last_block_timecode_(-1), | 41 last_block_timecode_(-1), |
| 23 block_data_size_(-1), | 42 block_data_size_(-1), |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { | 205 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { |
| 187 DVLOG(1) << "Got a block with a timecode before the previous block."; | 206 DVLOG(1) << "Got a block with a timecode before the previous block."; |
| 188 return false; | 207 return false; |
| 189 } | 208 } |
| 190 | 209 |
| 191 last_block_timecode_ = timecode; | 210 last_block_timecode_ = timecode; |
| 192 | 211 |
| 193 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 212 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
| 194 (cluster_timecode_ + timecode) * timecode_multiplier_); | 213 (cluster_timecode_ + timecode) * timecode_multiplier_); |
| 195 | 214 |
| 215 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted |
| 216 // WebM request for comments specification is here |
| 217 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
| 218 bool encrypted = track_num == video_.track_num() && |
| 219 video_encryption_key_id_.get(); |
| 220 // If encrypted skip past the HMAC. Encrypted buffers must include the IV and |
| 221 // the encrypted frame because the decryptor will verify this data before |
| 222 // decryption. The HMAC and IV will be copied into DecryptConfig. |
| 223 int offset = (encrypted) ? kWebMHmacSize : 0; |
| 224 |
| 196 // The first bit of the flags is set when the block contains only keyframes. | 225 // The first bit of the flags is set when the block contains only keyframes. |
| 197 // http://www.matroska.org/technical/specs/index.html | 226 // http://www.matroska.org/technical/specs/index.html |
| 198 bool is_keyframe = (flags & 0x80) != 0; | 227 bool is_keyframe = (flags & 0x80) != 0; |
| 199 scoped_refptr<StreamParserBuffer> buffer = | 228 scoped_refptr<StreamParserBuffer> buffer = |
| 200 StreamParserBuffer::CopyFrom(data, size, is_keyframe); | 229 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); |
| 201 | 230 |
| 202 if (track_num == video_.track_num() && video_encryption_key_id_.get()) { | 231 if (encrypted) { |
| 232 uint64 network_iv; |
| 233 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); |
| 234 const uint64 iv = base::NetToHost64(network_iv); |
| 235 |
| 236 scoped_array<uint8> counter_block(GenerateCounterBlock(iv)); |
| 203 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 237 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
| 204 video_encryption_key_id_.get(), video_encryption_key_id_size_))); | 238 video_encryption_key_id_.get(), video_encryption_key_id_size_, |
| 239 counter_block.get(), DecryptConfig::kDecryptionKeySize, |
| 240 data, kWebMHmacSize, |
| 241 sizeof(iv)))); |
| 205 } | 242 } |
| 206 | 243 |
| 207 buffer->SetTimestamp(timestamp); | 244 buffer->SetTimestamp(timestamp); |
| 208 if (cluster_start_time_ == kNoTimestamp()) | 245 if (cluster_start_time_ == kNoTimestamp()) |
| 209 cluster_start_time_ = timestamp; | 246 cluster_start_time_ = timestamp; |
| 210 | 247 |
| 211 if (block_duration >= 0) { | 248 if (block_duration >= 0) { |
| 212 buffer->SetDuration(base::TimeDelta::FromMicroseconds( | 249 buffer->SetDuration(base::TimeDelta::FromMicroseconds( |
| 213 block_duration * timecode_multiplier_)); | 250 block_duration * timecode_multiplier_)); |
| 214 } | 251 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 239 | 276 |
| 240 buffers_.push_back(buffer); | 277 buffers_.push_back(buffer); |
| 241 return true; | 278 return true; |
| 242 } | 279 } |
| 243 | 280 |
| 244 void WebMClusterParser::Track::Reset() { | 281 void WebMClusterParser::Track::Reset() { |
| 245 buffers_.clear(); | 282 buffers_.clear(); |
| 246 } | 283 } |
| 247 | 284 |
| 248 } // namespace media | 285 } // namespace media |
| OLD | NEW |