Chromium Code Reviews| 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" | 10 #include "base/sys_byteorder.h" |
| 11 #include "media/base/data_buffer.h" | 11 #include "media/base/data_buffer.h" |
| 12 #include "media/base/decrypt_config.h" | 12 #include "media/base/decrypt_config.h" |
| 13 #include "media/webm/webm_constants.h" | 13 #include "media/webm/webm_constants.h" |
| 14 | 14 |
| 15 namespace media { | 15 namespace media { |
| 16 | 16 |
| 17 // Generates a 16 byte CTR counter block. The CTR counter block format is a | 17 // 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. | 18 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. |
| 19 // Returns a string of kDecryptionKeySize bytes. | 19 // Returns a string of kDecryptionKeySize bytes. |
| 20 static std::string GenerateCounterBlock(uint64 iv) { | 20 static std::string GenerateCounterBlock(uint64 iv) { |
| 21 std::string counter_block(reinterpret_cast<char*>(&iv), sizeof(iv)); | 21 std::string counter_block(reinterpret_cast<char*>(&iv), sizeof(iv)); |
| 22 counter_block.append(DecryptConfig::kDecryptionKeySize - sizeof(iv), 0); | 22 counter_block.append(DecryptConfig::kDecryptionKeySize - sizeof(iv), 0); |
| 23 return counter_block; | 23 return counter_block; |
| 24 } | 24 } |
| 25 | 25 |
| 26 WebMClusterParser::WebMClusterParser(int64 timecode_scale, | 26 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
| 27 int audio_track_num, | 27 int audio_track_num, |
| 28 int video_track_num, | 28 int video_track_num, |
| 29 const std::string& audio_encryption_key_id, | |
| 29 const std::string& video_encryption_key_id) | 30 const std::string& video_encryption_key_id) |
| 30 : timecode_multiplier_(timecode_scale / 1000.0), | 31 : timecode_multiplier_(timecode_scale / 1000.0), |
| 32 audio_encryption_key_id_(audio_encryption_key_id), | |
| 31 video_encryption_key_id_(video_encryption_key_id), | 33 video_encryption_key_id_(video_encryption_key_id), |
| 32 parser_(kWebMIdCluster, this), | 34 parser_(kWebMIdCluster, this), |
| 33 last_block_timecode_(-1), | 35 last_block_timecode_(-1), |
| 34 block_data_size_(-1), | 36 block_data_size_(-1), |
| 35 block_duration_(-1), | 37 block_duration_(-1), |
| 36 cluster_timecode_(-1), | 38 cluster_timecode_(-1), |
| 37 cluster_start_time_(kNoTimestamp()), | 39 cluster_start_time_(kNoTimestamp()), |
| 38 cluster_ended_(false), | 40 cluster_ended_(false), |
| 39 audio_(audio_track_num), | 41 audio_(audio_track_num), |
| 40 video_(video_track_num) { | 42 video_(video_track_num) { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 if (timecode < 0) { | 193 if (timecode < 0) { |
| 192 DVLOG(1) << "Got a block with negative timecode offset " << timecode; | 194 DVLOG(1) << "Got a block with negative timecode offset " << timecode; |
| 193 return false; | 195 return false; |
| 194 } | 196 } |
| 195 | 197 |
| 196 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { | 198 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { |
| 197 DVLOG(1) << "Got a block with a timecode before the previous block."; | 199 DVLOG(1) << "Got a block with a timecode before the previous block."; |
| 198 return false; | 200 return false; |
| 199 } | 201 } |
| 200 | 202 |
| 203 Track* track = NULL; | |
| 204 std::string encryption_key_id; | |
|
ddorwin
2012/10/10 07:14:52
const ref?
xhwang
2012/10/11 00:49:41
Ref has to be initialized upon declaration. If we
ddorwin
2012/10/11 05:18:22
Oh, duh. Nevermind.
| |
| 205 if (track_num == audio_.track_num()) { | |
| 206 track = &audio_; | |
| 207 encryption_key_id = audio_encryption_key_id_; | |
| 208 } else if (track_num == video_.track_num()) { | |
| 209 track = &video_; | |
| 210 encryption_key_id = video_encryption_key_id_; | |
| 211 } else { | |
| 212 DVLOG(1) << "Unexpected track number " << track_num; | |
|
ddorwin
2012/10/10 07:14:52
I know this was copied from below, but it seems li
acolwell GONE FROM CHROMIUM
2012/10/10 16:00:06
I don't think this condition is any more important
| |
| 213 return false; | |
| 214 } | |
| 215 | |
| 201 last_block_timecode_ = timecode; | 216 last_block_timecode_ = timecode; |
| 202 | 217 |
| 203 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( | 218 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
| 204 (cluster_timecode_ + timecode) * timecode_multiplier_); | 219 (cluster_timecode_ + timecode) * timecode_multiplier_); |
| 205 | 220 |
| 206 // Every encrypted Block has a signal byte and IV prepended to it. Current | |
| 207 // encrypted WebM request for comments specification is here | |
| 208 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
| 209 bool is_track_encrypted = | |
| 210 track_num == video_.track_num() && !video_encryption_key_id_.empty(); | |
| 211 | |
| 212 // The first bit of the flags is set when the block contains only keyframes. | 221 // The first bit of the flags is set when the block contains only keyframes. |
| 213 // http://www.matroska.org/technical/specs/index.html | 222 // http://www.matroska.org/technical/specs/index.html |
| 214 bool is_keyframe = (flags & 0x80) != 0; | 223 bool is_keyframe = (flags & 0x80) != 0; |
| 215 scoped_refptr<StreamParserBuffer> buffer = | 224 scoped_refptr<StreamParserBuffer> buffer = |
| 216 StreamParserBuffer::CopyFrom(data, size, is_keyframe); | 225 StreamParserBuffer::CopyFrom(data, size, is_keyframe); |
| 217 | 226 |
| 218 if (is_track_encrypted) { | 227 // Every encrypted Block has a signal byte and IV prepended to it. Current |
| 228 // encrypted WebM request for comments specification is here | |
| 229 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
| 230 if (!encryption_key_id.empty()) { | |
| 219 uint8 signal_byte = data[0]; | 231 uint8 signal_byte = data[0]; |
| 220 int data_offset = sizeof(signal_byte); | 232 int data_offset = sizeof(signal_byte); |
| 221 | 233 |
| 222 // Setting the DecryptConfig object of the buffer while leaving the | 234 // Setting the DecryptConfig object of the buffer while leaving the |
| 223 // initialization vector empty will tell the decryptor that the frame is | 235 // initialization vector empty will tell the decryptor that the frame is |
| 224 // unencrypted. | 236 // unencrypted. |
| 225 std::string counter_block; | 237 std::string counter_block; |
| 226 | 238 |
| 227 if (signal_byte & kWebMFlagEncryptedFrame) { | 239 if (signal_byte & kWebMFlagEncryptedFrame) { |
| 228 uint64 network_iv; | 240 uint64 network_iv; |
| 229 memcpy(&network_iv, data + data_offset, sizeof(network_iv)); | 241 memcpy(&network_iv, data + data_offset, sizeof(network_iv)); |
| 230 const uint64 iv = base::NetToHost64(network_iv); | 242 const uint64 iv = base::NetToHost64(network_iv); |
| 231 counter_block = GenerateCounterBlock(iv); | 243 counter_block = GenerateCounterBlock(iv); |
| 232 data_offset += sizeof(iv); | 244 data_offset += sizeof(iv); |
|
acolwell GONE FROM CHROMIUM
2012/10/10 16:00:06
nit:s/sizeof(iv)/sizeof(network_iv) since network_
xhwang
2012/10/11 00:49:41
Done.
| |
| 233 } | 245 } |
| 234 | 246 |
| 235 // TODO(fgalligan): Revisit if DecryptConfig needs to be set on unencrypted | 247 // TODO(fgalligan): Revisit if DecryptConfig needs to be set on unencrypted |
| 236 // frames after the CDM API is finalized. | 248 // frames after the CDM API is finalized. |
| 237 // Unencrypted frames of potentially encrypted streams currently set | 249 // Unencrypted frames of potentially encrypted streams currently set |
| 238 // DecryptConfig. | 250 // DecryptConfig. |
| 239 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( | 251 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( |
| 240 video_encryption_key_id_, | 252 video_encryption_key_id_, |
|
acolwell GONE FROM CHROMIUM
2012/10/10 16:00:06
s/video_encryption_key_id_/encryption_key_id ?
xhwang
2012/10/11 00:49:41
good catch. thanks!
| |
| 241 counter_block, | 253 counter_block, |
| 242 data_offset, | 254 data_offset, |
| 243 std::vector<SubsampleEntry>()))); | 255 std::vector<SubsampleEntry>()))); |
| 244 } | 256 } |
| 245 | 257 |
| 246 buffer->SetTimestamp(timestamp); | 258 buffer->SetTimestamp(timestamp); |
| 247 if (cluster_start_time_ == kNoTimestamp()) | 259 if (cluster_start_time_ == kNoTimestamp()) |
| 248 cluster_start_time_ = timestamp; | 260 cluster_start_time_ = timestamp; |
| 249 | 261 |
| 250 if (block_duration >= 0) { | 262 if (block_duration >= 0) { |
| 251 buffer->SetDuration(base::TimeDelta::FromMicroseconds( | 263 buffer->SetDuration(base::TimeDelta::FromMicroseconds( |
| 252 block_duration * timecode_multiplier_)); | 264 block_duration * timecode_multiplier_)); |
| 253 } | 265 } |
| 254 | 266 |
| 255 if (track_num == audio_.track_num()) { | 267 return track->AddBuffer(buffer); |
| 256 return audio_.AddBuffer(buffer); | |
| 257 } else if (track_num == video_.track_num()) { | |
| 258 return video_.AddBuffer(buffer); | |
| 259 } | |
| 260 | |
| 261 DVLOG(1) << "Unexpected track number " << track_num; | |
| 262 return false; | |
| 263 } | 268 } |
| 264 | 269 |
| 265 WebMClusterParser::Track::Track(int track_num) | 270 WebMClusterParser::Track::Track(int track_num) |
| 266 : track_num_(track_num) { | 271 : track_num_(track_num) { |
| 267 } | 272 } |
| 268 | 273 |
| 269 WebMClusterParser::Track::~Track() {} | 274 WebMClusterParser::Track::~Track() {} |
| 270 | 275 |
| 271 bool WebMClusterParser::Track::AddBuffer( | 276 bool WebMClusterParser::Track::AddBuffer( |
| 272 const scoped_refptr<StreamParserBuffer>& buffer) { | 277 const scoped_refptr<StreamParserBuffer>& buffer) { |
| 273 DVLOG(2) << "AddBuffer() : " << track_num_ | 278 DVLOG(2) << "AddBuffer() : " << track_num_ |
| 274 << " ts " << buffer->GetTimestamp().InSecondsF() | 279 << " ts " << buffer->GetTimestamp().InSecondsF() |
| 275 << " dur " << buffer->GetDuration().InSecondsF() | 280 << " dur " << buffer->GetDuration().InSecondsF() |
| 276 << " kf " << buffer->IsKeyframe() | 281 << " kf " << buffer->IsKeyframe() |
| 277 << " size " << buffer->GetDataSize(); | 282 << " size " << buffer->GetDataSize(); |
| 278 | 283 |
| 279 buffers_.push_back(buffer); | 284 buffers_.push_back(buffer); |
| 280 return true; | 285 return true; |
| 281 } | 286 } |
| 282 | 287 |
| 283 void WebMClusterParser::Track::Reset() { | 288 void WebMClusterParser::Track::Reset() { |
| 284 buffers_.clear(); | 289 buffers_.clear(); |
| 285 } | 290 } |
| 286 | 291 |
| 287 } // namespace media | 292 } // namespace media |
| OLD | NEW |