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. | |
ddorwin
2012/07/17 00:19:52
// Always returns a valid pointer to a buffer of k
fgalligan1
2012/07/17 16:34:57
Done.
| |
17 static scoped_array<uint8> GenerateCounterBlock(uint64 iv) { | |
18 scoped_array<uint8> counter_block_data( | |
19 new uint8[DecryptConfig::kDecryptionKeySize]); | |
20 | |
21 // Set the IV. | |
22 memcpy(counter_block_data.get(), &iv, sizeof(iv)); | |
23 | |
24 // Set block counter to all 0's. | |
25 memset(counter_block_data.get() + sizeof(iv), | |
26 0, | |
27 DecryptConfig::kDecryptionKeySize - sizeof(iv)); | |
28 | |
29 return counter_block_data.Pass(); | |
30 } | |
31 | |
14 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 32 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
15 int audio_track_num, | 33 int audio_track_num, |
16 int video_track_num, | 34 int video_track_num, |
17 const uint8* video_encryption_key_id, | 35 const uint8* video_encryption_key_id, |
18 int video_encryption_key_id_size) | 36 int video_encryption_key_id_size) |
19 : timecode_multiplier_(timecode_scale / 1000.0), | 37 : timecode_multiplier_(timecode_scale / 1000.0), |
20 video_encryption_key_id_size_(video_encryption_key_id_size), | 38 video_encryption_key_id_size_(video_encryption_key_id_size), |
21 parser_(kWebMIdCluster, this), | 39 parser_(kWebMIdCluster, this), |
22 last_block_timecode_(-1), | 40 last_block_timecode_(-1), |
23 block_data_size_(-1), | 41 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_) { | 204 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { |
187 DVLOG(1) << "Got a block with a timecode before the previous block."; | 205 DVLOG(1) << "Got a block with a timecode before the previous block."; |
188 return false; | 206 return false; |
189 } | 207 } |
190 | 208 |
191 last_block_timecode_ = timecode; | 209 last_block_timecode_ = timecode; |
192 | 210 |
193 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 211 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
194 (cluster_timecode_ + timecode) * timecode_multiplier_); | 212 (cluster_timecode_ + timecode) * timecode_multiplier_); |
195 | 213 |
214 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted | |
215 // WebM request for comments specification is here | |
216 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
217 bool encrypted = track_num == video_.track_num() && | |
218 video_encryption_key_id_.get(); | |
219 // If encrypted skip past the HMAC. Encrypted buffers must include the IV and | |
220 // the encrypted frame because the decryptor will verify this data before | |
221 // decryption. The HMAC and IV will be copied into DecryptConfig. | |
222 int offset = (encrypted) ? kWebMHmacSize : 0; | |
223 | |
196 // The first bit of the flags is set when the block contains only keyframes. | 224 // The first bit of the flags is set when the block contains only keyframes. |
197 // http://www.matroska.org/technical/specs/index.html | 225 // http://www.matroska.org/technical/specs/index.html |
198 bool is_keyframe = (flags & 0x80) != 0; | 226 bool is_keyframe = (flags & 0x80) != 0; |
199 scoped_refptr<StreamParserBuffer> buffer = | 227 scoped_refptr<StreamParserBuffer> buffer = |
200 StreamParserBuffer::CopyFrom(data, size, is_keyframe); | 228 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); |
201 | 229 |
202 if (track_num == video_.track_num() && video_encryption_key_id_.get()) { | 230 if (encrypted) { |
231 uint64 network_iv; | |
232 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); | |
233 const uint64 iv = base::NetToHost64(network_iv); | |
234 | |
235 scoped_array<uint8> counter_block(GenerateCounterBlock(iv)); | |
203 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 236 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
204 video_encryption_key_id_.get(), video_encryption_key_id_size_))); | 237 video_encryption_key_id_.get(), video_encryption_key_id_size_, |
238 counter_block.get(), DecryptConfig::kDecryptionKeySize, | |
239 data, kWebMHmacSize, | |
240 sizeof(iv)))); | |
205 } | 241 } |
206 | 242 |
207 buffer->SetTimestamp(timestamp); | 243 buffer->SetTimestamp(timestamp); |
208 if (cluster_start_time_ == kNoTimestamp()) | 244 if (cluster_start_time_ == kNoTimestamp()) |
209 cluster_start_time_ = timestamp; | 245 cluster_start_time_ = timestamp; |
210 | 246 |
211 if (block_duration >= 0) { | 247 if (block_duration >= 0) { |
212 buffer->SetDuration(base::TimeDelta::FromMicroseconds( | 248 buffer->SetDuration(base::TimeDelta::FromMicroseconds( |
213 block_duration * timecode_multiplier_)); | 249 block_duration * timecode_multiplier_)); |
214 } | 250 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 DVLOG(2) << "AddToBufferQueue() : " << track_num_ | 341 DVLOG(2) << "AddToBufferQueue() : " << track_num_ |
306 << " ts " << buffer->GetTimestamp().InSecondsF() | 342 << " ts " << buffer->GetTimestamp().InSecondsF() |
307 << " dur " << buffer->GetDuration().InSecondsF() | 343 << " dur " << buffer->GetDuration().InSecondsF() |
308 << " kf " << buffer->IsKeyframe() | 344 << " kf " << buffer->IsKeyframe() |
309 << " size " << buffer->GetDataSize(); | 345 << " size " << buffer->GetDataSize(); |
310 | 346 |
311 buffers_.push_back(buffer); | 347 buffers_.push_back(buffer); |
312 } | 348 } |
313 | 349 |
314 } // namespace media | 350 } // namespace media |
OLD | NEW |