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

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 in the CL. 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 <= 0 || iv_size > WebMClusterParser::kDecryptionKeySize)
ddorwin 2012/07/13 00:48:00 so we do have a specific key size. are we not spec
fgalligan1 2012/07/13 21:40:41 We have a specific key size. I.e. 128 bits. We hav
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();
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 webm_iv =
ddorwin 2012/07/13 00:48:00 not really an IV anymore is it?
fgalligan1 2012/07/13 21:40:41 Done.
240 GenerateCounterBlock(reinterpret_cast<const uint8*>(&iv), sizeof(iv));
ddorwin 2012/07/13 00:48:00 Why did you move this logic here? Is this what ISO
fgalligan1 2012/07/13 21:40:41 Yeah. And ISO can generate 2 different types of co
241
203 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( 242 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig(
204 video_encryption_key_id_.get(), video_encryption_key_id_size_))); 243 video_encryption_key_id_.get(), video_encryption_key_id_size_,
244 reinterpret_cast<const uint8*>(webm_iv.data()), webm_iv.size(),
245 data, kWebMHmacSize,
246 sizeof(iv))));
205 } 247 }
206 248
207 buffer->SetTimestamp(timestamp); 249 buffer->SetTimestamp(timestamp);
208 if (cluster_start_time_ == kNoTimestamp()) 250 if (cluster_start_time_ == kNoTimestamp())
209 cluster_start_time_ = timestamp; 251 cluster_start_time_ = timestamp;
210 252
211 if (block_duration >= 0) { 253 if (block_duration >= 0) {
212 buffer->SetDuration(base::TimeDelta::FromMicroseconds( 254 buffer->SetDuration(base::TimeDelta::FromMicroseconds(
213 block_duration * timecode_multiplier_)); 255 block_duration * timecode_multiplier_));
214 } 256 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 DVLOG(2) << "AddToBufferQueue() : " << track_num_ 347 DVLOG(2) << "AddToBufferQueue() : " << track_num_
306 << " ts " << buffer->GetTimestamp().InSecondsF() 348 << " ts " << buffer->GetTimestamp().InSecondsF()
307 << " dur " << buffer->GetDuration().InSecondsF() 349 << " dur " << buffer->GetDuration().InSecondsF()
308 << " kf " << buffer->IsKeyframe() 350 << " kf " << buffer->IsKeyframe()
309 << " size " << buffer->GetDataSize(); 351 << " size " << buffer->GetDataSize();
310 352
311 buffers_.push_back(buffer); 353 buffers_.push_back(buffer);
312 } 354 }
313 355
314 } // namespace media 356 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698