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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/sys_byteorder.h" | |
11 #include "media/base/data_buffer.h" | 10 #include "media/base/data_buffer.h" |
12 #include "media/base/decrypt_config.h" | 11 #include "media/base/decrypt_config.h" |
13 #include "media/webm/webm_constants.h" | 12 #include "media/webm/webm_constants.h" |
14 | 13 |
15 namespace media { | 14 namespace media { |
16 | 15 |
17 // Generates a 16 byte CTR counter block. The CTR counter block format is a | 16 // Generates a 16 byte CTR counter block. The CTR counter block format is a |
18 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. | 17 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. |
19 // Returns a string of kDecryptionKeySize bytes. | 18 // Returns a string of kDecryptionKeySize bytes. |
20 static std::string GenerateCounterBlock(uint64 iv) { | 19 static std::string GenerateCounterBlock(const uint8* iv, size_t iv_size) { |
21 std::string counter_block(reinterpret_cast<char*>(&iv), sizeof(iv)); | 20 std::string counter_block(reinterpret_cast<const char*>(iv), iv_size); |
ddorwin
2012/10/13 17:21:27
Should we counter_block.resize(DecryptConfig::kDec
fgalligan1
2012/10/14 16:45:24
What about this?
std::string counter_block(Decrypt
ddorwin
2012/10/15 02:32:49
SGTM
| |
22 counter_block.append(DecryptConfig::kDecryptionKeySize - sizeof(iv), 0); | 21 counter_block.append(DecryptConfig::kDecryptionKeySize - iv_size, 0); |
23 return counter_block; | 22 return counter_block; |
24 } | 23 } |
25 | 24 |
26 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 25 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
27 int audio_track_num, | 26 int audio_track_num, |
28 int video_track_num, | 27 int video_track_num, |
29 const std::string& audio_encryption_key_id, | 28 const std::string& audio_encryption_key_id, |
30 const std::string& video_encryption_key_id) | 29 const std::string& video_encryption_key_id) |
31 : timecode_multiplier_(timecode_scale / 1000.0), | 30 : timecode_multiplier_(timecode_scale / 1000.0), |
32 audio_encryption_key_id_(audio_encryption_key_id), | 31 audio_encryption_key_id_(audio_encryption_key_id), |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 | 176 |
178 block_data_.reset(new uint8[size]); | 177 block_data_.reset(new uint8[size]); |
179 memcpy(block_data_.get(), data, size); | 178 memcpy(block_data_.get(), data, size); |
180 block_data_size_ = size; | 179 block_data_size_ = size; |
181 return true; | 180 return true; |
182 } | 181 } |
183 | 182 |
184 bool WebMClusterParser::OnBlock(int track_num, int timecode, | 183 bool WebMClusterParser::OnBlock(int track_num, int timecode, |
185 int block_duration, | 184 int block_duration, |
186 int flags, | 185 int flags, |
187 const uint8* data, int size) { | 186 const uint8* data, int size) { |
ddorwin
2012/10/13 17:21:27
There is no check that size is not negative. DCHEC
fgalligan1
2012/10/14 16:45:24
Done.
| |
188 if (cluster_timecode_ == -1) { | 187 if (cluster_timecode_ == -1) { |
189 DVLOG(1) << "Got a block before cluster timecode."; | 188 DVLOG(1) << "Got a block before cluster timecode."; |
190 return false; | 189 return false; |
191 } | 190 } |
192 | 191 |
193 if (timecode < 0) { | 192 if (timecode < 0) { |
194 DVLOG(1) << "Got a block with negative timecode offset " << timecode; | 193 DVLOG(1) << "Got a block with negative timecode offset " << timecode; |
195 return false; | 194 return false; |
196 } | 195 } |
197 | 196 |
(...skipping 23 matching lines...) Expand all Loading... | |
221 // The first bit of the flags is set when the block contains only keyframes. | 220 // The first bit of the flags is set when the block contains only keyframes. |
222 // http://www.matroska.org/technical/specs/index.html | 221 // http://www.matroska.org/technical/specs/index.html |
223 bool is_keyframe = (flags & 0x80) != 0; | 222 bool is_keyframe = (flags & 0x80) != 0; |
224 scoped_refptr<StreamParserBuffer> buffer = | 223 scoped_refptr<StreamParserBuffer> buffer = |
225 StreamParserBuffer::CopyFrom(data, size, is_keyframe); | 224 StreamParserBuffer::CopyFrom(data, size, is_keyframe); |
226 | 225 |
227 // Every encrypted Block has a signal byte and IV prepended to it. Current | 226 // Every encrypted Block has a signal byte and IV prepended to it. Current |
228 // encrypted WebM request for comments specification is here | 227 // encrypted WebM request for comments specification is here |
229 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | 228 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
230 if (!encryption_key_id.empty()) { | 229 if (!encryption_key_id.empty()) { |
231 uint8 signal_byte = data[0]; | 230 uint8 signal_byte = data[0]; |
ddorwin
2012/10/13 17:21:27
We don't check that size is >0 here or >8 at 239.
fgalligan1
2012/10/14 16:45:24
I don't think we can do one check as we need to ch
| |
232 int data_offset = sizeof(signal_byte); | 231 int data_offset = sizeof(signal_byte); |
233 | 232 |
234 // Setting the DecryptConfig object of the buffer while leaving the | 233 // Setting the DecryptConfig object of the buffer while leaving the |
235 // initialization vector empty will tell the decryptor that the frame is | 234 // initialization vector empty will tell the decryptor that the frame is |
236 // unencrypted. | 235 // unencrypted. |
237 std::string counter_block; | 236 std::string counter_block; |
238 | 237 |
239 if (signal_byte & kWebMFlagEncryptedFrame) { | 238 if (signal_byte & kWebMFlagEncryptedFrame) { |
240 uint64 network_iv; | 239 counter_block = GenerateCounterBlock(data + data_offset, kWebMIvSize); |
241 memcpy(&network_iv, data + data_offset, sizeof(network_iv)); | 240 data_offset += kWebMIvSize; |
242 data_offset += sizeof(network_iv); | |
243 counter_block = GenerateCounterBlock(base::NetToHost64(network_iv)); | |
244 } | 241 } |
245 | 242 |
246 // TODO(fgalligan): Revisit if DecryptConfig needs to be set on unencrypted | 243 // TODO(fgalligan): Revisit if DecryptConfig needs to be set on unencrypted |
247 // frames after the CDM API is finalized. | 244 // frames after the CDM API is finalized. |
248 // Unencrypted frames of potentially encrypted streams currently set | 245 // Unencrypted frames of potentially encrypted streams currently set |
249 // DecryptConfig. | 246 // DecryptConfig. |
250 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 247 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
251 encryption_key_id, | 248 encryption_key_id, |
252 counter_block, | 249 counter_block, |
253 data_offset, | 250 data_offset, |
(...skipping 28 matching lines...) Expand all Loading... | |
282 | 279 |
283 buffers_.push_back(buffer); | 280 buffers_.push_back(buffer); |
284 return true; | 281 return true; |
285 } | 282 } |
286 | 283 |
287 void WebMClusterParser::Track::Reset() { | 284 void WebMClusterParser::Track::Reset() { |
288 buffers_.clear(); | 285 buffers_.clear(); |
289 } | 286 } |
290 | 287 |
291 } // namespace media | 288 } // namespace media |
OLD | NEW |