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" | |
9 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
10 #include "media/base/decrypt_config.h" | 9 #include "media/base/decrypt_config.h" |
11 #include "media/webm/webm_constants.h" | 10 #include "media/webm/webm_constants.h" |
12 | 11 |
13 namespace media { | 12 namespace media { |
14 | 13 |
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 | |
33 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 14 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
34 int audio_track_num, | 15 int audio_track_num, |
35 int video_track_num, | 16 int video_track_num, |
36 const uint8* video_encryption_key_id, | 17 const uint8* video_encryption_key_id, |
37 int video_encryption_key_id_size) | 18 int video_encryption_key_id_size) |
38 : timecode_multiplier_(timecode_scale / 1000.0), | 19 : timecode_multiplier_(timecode_scale / 1000.0), |
39 video_encryption_key_id_size_(video_encryption_key_id_size), | 20 video_encryption_key_id_size_(video_encryption_key_id_size), |
40 parser_(kWebMIdCluster, this), | 21 parser_(kWebMIdCluster, this), |
41 last_block_timecode_(-1), | 22 last_block_timecode_(-1), |
42 block_data_size_(-1), | 23 block_data_size_(-1), |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { | 186 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { |
206 DVLOG(1) << "Got a block with a timecode before the previous block."; | 187 DVLOG(1) << "Got a block with a timecode before the previous block."; |
207 return false; | 188 return false; |
208 } | 189 } |
209 | 190 |
210 last_block_timecode_ = timecode; | 191 last_block_timecode_ = timecode; |
211 | 192 |
212 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 193 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
213 (cluster_timecode_ + timecode) * timecode_multiplier_); | 194 (cluster_timecode_ + timecode) * timecode_multiplier_); |
214 | 195 |
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 | |
225 // The first bit of the flags is set when the block contains only keyframes. | 196 // The first bit of the flags is set when the block contains only keyframes. |
226 // http://www.matroska.org/technical/specs/index.html | 197 // http://www.matroska.org/technical/specs/index.html |
227 bool is_keyframe = (flags & 0x80) != 0; | 198 bool is_keyframe = (flags & 0x80) != 0; |
228 scoped_refptr<StreamParserBuffer> buffer = | 199 scoped_refptr<StreamParserBuffer> buffer = |
229 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); | 200 StreamParserBuffer::CopyFrom(data, size, is_keyframe); |
230 | 201 |
231 if (encrypted) { | 202 if (track_num == video_.track_num() && video_encryption_key_id_.get()) { |
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)); | |
237 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 203 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
238 video_encryption_key_id_.get(), video_encryption_key_id_size_, | 204 video_encryption_key_id_.get(), video_encryption_key_id_size_))); |
239 counter_block.get(), DecryptConfig::kDecryptionKeySize, | |
240 data, kWebMHmacSize, | |
241 sizeof(iv)))); | |
242 } | 205 } |
243 | 206 |
244 buffer->SetTimestamp(timestamp); | 207 buffer->SetTimestamp(timestamp); |
245 if (cluster_start_time_ == kNoTimestamp()) | 208 if (cluster_start_time_ == kNoTimestamp()) |
246 cluster_start_time_ = timestamp; | 209 cluster_start_time_ = timestamp; |
247 | 210 |
248 if (block_duration >= 0) { | 211 if (block_duration >= 0) { |
249 buffer->SetDuration(base::TimeDelta::FromMicroseconds( | 212 buffer->SetDuration(base::TimeDelta::FromMicroseconds( |
250 block_duration * timecode_multiplier_)); | 213 block_duration * timecode_multiplier_)); |
251 } | 214 } |
(...skipping 24 matching lines...) Expand all Loading... |
276 | 239 |
277 buffers_.push_back(buffer); | 240 buffers_.push_back(buffer); |
278 return true; | 241 return true; |
279 } | 242 } |
280 | 243 |
281 void WebMClusterParser::Track::Reset() { | 244 void WebMClusterParser::Track::Reset() { |
282 buffers_.clear(); | 245 buffers_.clear(); |
283 } | 246 } |
284 | 247 |
285 } // namespace media | 248 } // namespace media |
OLD | NEW |