Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Side by Side Diff: media/webm/webm_cluster_parser.cc

Issue 10535029: Add support for encrypted WebM files as defined in the RFC. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressing comments from Patch Set 12. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 a CTR IV. |iv_size| is
17 // the size of |iv| in bytes. Returns counter block on success. Returns empty
18 // string on failure.
19 static std::string GenerateCounterBlock(const uint8* iv, int iv_size) {
20 if (iv_size != WebMClusterParser::kIvSize)
21 return std::string();
22
23 char counter_block_data[WebMClusterParser::kDecryptionKeySize];
24
25 // Set the IV.
26 memcpy(counter_block_data, iv, iv_size);
27
28 // Set block counter to all 0's.
29 memset(counter_block_data + iv_size,
30 0,
31 WebMClusterParser::kDecryptionKeySize - iv_size);
32
33 return std::string(counter_block_data, WebMClusterParser::kDecryptionKeySize);
34 }
35
14 WebMClusterParser::WebMClusterParser(int64 timecode_scale, 36 WebMClusterParser::WebMClusterParser(int64 timecode_scale,
15 int audio_track_num, 37 int audio_track_num,
16 int video_track_num, 38 int video_track_num,
17 const uint8* video_encryption_key_id, 39 const uint8* video_encryption_key_id,
18 int video_encryption_key_id_size) 40 int video_encryption_key_id_size)
19 : timecode_multiplier_(timecode_scale / 1000.0), 41 : timecode_multiplier_(timecode_scale / 1000.0),
20 video_encryption_key_id_size_(video_encryption_key_id_size), 42 video_encryption_key_id_size_(video_encryption_key_id_size),
21 parser_(kWebMIdCluster, this), 43 parser_(kWebMIdCluster, this),
22 last_block_timecode_(-1), 44 last_block_timecode_(-1),
23 block_data_size_(-1), 45 block_data_size_(-1),
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { 208 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) {
187 DVLOG(1) << "Got a block with a timecode before the previous block."; 209 DVLOG(1) << "Got a block with a timecode before the previous block.";
188 return false; 210 return false;
189 } 211 }
190 212
191 last_block_timecode_ = timecode; 213 last_block_timecode_ = timecode;
192 214
193 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( 215 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds(
194 (cluster_timecode_ + timecode) * timecode_multiplier_); 216 (cluster_timecode_ + timecode) * timecode_multiplier_);
195 217
218 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted
219 // WebM request for comments specification is here
220 // http://wiki.webmproject.org/encryption/webm-encryption-rfc
221 bool encrypted = track_num == video_.track_num() &&
222 video_encryption_key_id_.get();
ddorwin 2012/07/14 00:50:31 any idea why this is "video_"?
fgalligan1 2012/07/16 23:51:42 No, besides the usual private member to the class.
223 // If encrypted skip past the HMAC. Encrypted buffers must include the IV and
224 // the encrypted frame because the decryptor will verify this data before
225 // decryption. The HMAC and IV will be copied into DecryptConfig.
226 int offset = (encrypted) ? kWebMHmacSize : 0;
227
196 // The first bit of the flags is set when the block contains only keyframes. 228 // The first bit of the flags is set when the block contains only keyframes.
197 // http://www.matroska.org/technical/specs/index.html 229 // http://www.matroska.org/technical/specs/index.html
198 bool is_keyframe = (flags & 0x80) != 0; 230 bool is_keyframe = (flags & 0x80) != 0;
199 scoped_refptr<StreamParserBuffer> buffer = 231 scoped_refptr<StreamParserBuffer> buffer =
200 StreamParserBuffer::CopyFrom(data, size, is_keyframe); 232 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe);
201 233
202 if (track_num == video_.track_num() && video_encryption_key_id_.get()) { 234 if (encrypted) {
235 uint64 network_iv;
236 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv));
237 const uint64 iv = base::NetToHost64(network_iv);
238
239 std::string counter_block =
240 GenerateCounterBlock(reinterpret_cast<const uint8*>(&iv), sizeof(iv));
241 const uint8* counter_block_data =
242 reinterpret_cast<const uint8*>(counter_block.data());
203 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( 243 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig(
204 video_encryption_key_id_.get(), video_encryption_key_id_size_))); 244 video_encryption_key_id_.get(), video_encryption_key_id_size_,
245 counter_block_data, counter_block.size(),
246 data, kWebMHmacSize,
247 sizeof(iv))));
205 } 248 }
206 249
207 buffer->SetTimestamp(timestamp); 250 buffer->SetTimestamp(timestamp);
208 if (cluster_start_time_ == kNoTimestamp()) 251 if (cluster_start_time_ == kNoTimestamp())
209 cluster_start_time_ = timestamp; 252 cluster_start_time_ = timestamp;
210 253
211 if (block_duration >= 0) { 254 if (block_duration >= 0) {
212 buffer->SetDuration(base::TimeDelta::FromMicroseconds( 255 buffer->SetDuration(base::TimeDelta::FromMicroseconds(
213 block_duration * timecode_multiplier_)); 256 block_duration * timecode_multiplier_));
214 } 257 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 DVLOG(2) << "AddToBufferQueue() : " << track_num_ 348 DVLOG(2) << "AddToBufferQueue() : " << track_num_
306 << " ts " << buffer->GetTimestamp().InSecondsF() 349 << " ts " << buffer->GetTimestamp().InSecondsF()
307 << " dur " << buffer->GetDuration().InSecondsF() 350 << " dur " << buffer->GetDuration().InSecondsF()
308 << " kf " << buffer->IsKeyframe() 351 << " kf " << buffer->IsKeyframe()
309 << " size " << buffer->GetDataSize(); 352 << " size " << buffer->GetDataSize();
310 353
311 buffers_.push_back(buffer); 354 buffers_.push_back(buffer);
312 } 355 }
313 356
314 } // namespace media 357 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698