| 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/filters/decrypting_demuxer_stream.h" | 5 #include "media/filters/decrypting_demuxer_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "media/base/audio_decoder_config.h" | 12 #include "media/base/audio_decoder_config.h" |
| 13 #include "media/base/video_decoder_config.h" | 13 #include "media/base/video_decoder_config.h" |
| 14 #include "media/base/bind_to_loop.h" | 14 #include "media/base/bind_to_loop.h" |
| 15 #include "media/base/decoder_buffer.h" | 15 #include "media/base/decoder_buffer.h" |
| 16 #include "media/base/decryptor.h" | 16 #include "media/base/decryptor.h" |
| 17 #include "media/base/demuxer_stream.h" | 17 #include "media/base/demuxer_stream.h" |
| 18 #include "media/base/pipeline.h" | 18 #include "media/base/pipeline.h" |
| 19 | 19 |
| 20 namespace media { | 20 namespace media { |
| 21 | 21 |
| 22 #define BIND_TO_LOOP(function) \ | 22 #define BIND_TO_LOOP(function) \ |
| 23 media::BindToLoop(message_loop_, base::Bind(function, this)) | 23 media::BindToLoop(message_loop_, base::Bind(function, this)) |
| 24 | 24 |
| 25 static bool IsStreamValidAndEncrypted( |
| 26 const scoped_refptr<DemuxerStream>& stream) { |
| 27 return ((stream->type() == DemuxerStream::AUDIO && |
| 28 stream->audio_decoder_config().IsValidConfig() && |
| 29 stream->audio_decoder_config().is_encrypted()) || |
| 30 (stream->type() == DemuxerStream::VIDEO && |
| 31 stream->video_decoder_config().IsValidConfig() && |
| 32 stream->video_decoder_config().is_encrypted())); |
| 33 } |
| 34 |
| 25 DecryptingDemuxerStream::DecryptingDemuxerStream( | 35 DecryptingDemuxerStream::DecryptingDemuxerStream( |
| 26 const scoped_refptr<base::MessageLoopProxy>& message_loop, | 36 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 27 const SetDecryptorReadyCB& set_decryptor_ready_cb) | 37 const SetDecryptorReadyCB& set_decryptor_ready_cb) |
| 28 : message_loop_(message_loop), | 38 : message_loop_(message_loop), |
| 29 state_(kUninitialized), | 39 state_(kUninitialized), |
| 30 stream_type_(UNKNOWN), | 40 stream_type_(UNKNOWN), |
| 31 set_decryptor_ready_cb_(set_decryptor_ready_cb), | 41 set_decryptor_ready_cb_(set_decryptor_ready_cb), |
| 32 decryptor_(NULL), | 42 decryptor_(NULL), |
| 33 key_added_while_decrypt_pending_(false) { | 43 key_added_while_decrypt_pending_(false) { |
| 34 } | 44 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 125 |
| 116 DecryptingDemuxerStream::~DecryptingDemuxerStream() {} | 126 DecryptingDemuxerStream::~DecryptingDemuxerStream() {} |
| 117 | 127 |
| 118 void DecryptingDemuxerStream::DoInitialize( | 128 void DecryptingDemuxerStream::DoInitialize( |
| 119 const scoped_refptr<DemuxerStream>& stream, | 129 const scoped_refptr<DemuxerStream>& stream, |
| 120 const PipelineStatusCB& status_cb) { | 130 const PipelineStatusCB& status_cb) { |
| 121 DVLOG(2) << "DoInitialize()"; | 131 DVLOG(2) << "DoInitialize()"; |
| 122 DCHECK(message_loop_->BelongsToCurrentThread()); | 132 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 123 DCHECK_EQ(state_, kUninitialized) << state_; | 133 DCHECK_EQ(state_, kUninitialized) << state_; |
| 124 | 134 |
| 125 // Only valid potentially encrypted audio or video stream is accepted. | |
| 126 if (!((stream->type() == AUDIO && | |
| 127 stream->audio_decoder_config().IsValidConfig() && | |
| 128 stream->audio_decoder_config().is_encrypted()) || | |
| 129 (stream->type() == VIDEO && | |
| 130 stream->video_decoder_config().IsValidConfig() && | |
| 131 stream->video_decoder_config().is_encrypted()))) { | |
| 132 status_cb.Run(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 DCHECK(!demuxer_stream_); | 135 DCHECK(!demuxer_stream_); |
| 137 demuxer_stream_ = stream; | 136 demuxer_stream_ = stream; |
| 138 stream_type_ = stream->type(); | 137 stream_type_ = stream->type(); |
| 139 init_cb_ = status_cb; | 138 init_cb_ = status_cb; |
| 140 | 139 |
| 140 SetDecoderConfig(stream); |
| 141 |
| 141 state_ = kDecryptorRequested; | 142 state_ = kDecryptorRequested; |
| 142 set_decryptor_ready_cb_.Run( | 143 set_decryptor_ready_cb_.Run( |
| 143 BIND_TO_LOOP(&DecryptingDemuxerStream::SetDecryptor)); | 144 BIND_TO_LOOP(&DecryptingDemuxerStream::SetDecryptor)); |
| 144 } | 145 } |
| 145 | 146 |
| 146 void DecryptingDemuxerStream::SetDecryptor(Decryptor* decryptor) { | 147 void DecryptingDemuxerStream::SetDecryptor(Decryptor* decryptor) { |
| 147 DVLOG(2) << "SetDecryptor()"; | 148 DVLOG(2) << "SetDecryptor()"; |
| 148 DCHECK(message_loop_->BelongsToCurrentThread()); | 149 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 149 DCHECK_EQ(state_, kDecryptorRequested) << state_; | 150 DCHECK_EQ(state_, kDecryptorRequested) << state_; |
| 150 DCHECK(!init_cb_.is_null()); | 151 DCHECK(!init_cb_.is_null()); |
| 151 DCHECK(!set_decryptor_ready_cb_.is_null()); | 152 DCHECK(!set_decryptor_ready_cb_.is_null()); |
| 152 | 153 |
| 153 set_decryptor_ready_cb_.Reset(); | 154 set_decryptor_ready_cb_.Reset(); |
| 154 decryptor_ = decryptor; | 155 decryptor_ = decryptor; |
| 155 | 156 |
| 156 switch (stream_type_) { | |
| 157 case AUDIO: { | |
| 158 const AudioDecoderConfig& input_audio_config = | |
| 159 demuxer_stream_->audio_decoder_config(); | |
| 160 audio_config_.reset(new AudioDecoderConfig()); | |
| 161 audio_config_->Initialize(input_audio_config.codec(), | |
| 162 input_audio_config.bits_per_channel(), | |
| 163 input_audio_config.channel_layout(), | |
| 164 input_audio_config.samples_per_second(), | |
| 165 input_audio_config.extra_data(), | |
| 166 input_audio_config.extra_data_size(), | |
| 167 false, // Output audio is not encrypted. | |
| 168 false); | |
| 169 break; | |
| 170 } | |
| 171 | |
| 172 case VIDEO: { | |
| 173 const VideoDecoderConfig& input_video_config = | |
| 174 demuxer_stream_->video_decoder_config(); | |
| 175 video_config_.reset(new VideoDecoderConfig()); | |
| 176 video_config_->Initialize(input_video_config.codec(), | |
| 177 input_video_config.profile(), | |
| 178 input_video_config.format(), | |
| 179 input_video_config.coded_size(), | |
| 180 input_video_config.visible_rect(), | |
| 181 input_video_config.natural_size(), | |
| 182 input_video_config.extra_data(), | |
| 183 input_video_config.extra_data_size(), | |
| 184 false, // Output video is not encrypted. | |
| 185 false); | |
| 186 break; | |
| 187 } | |
| 188 | |
| 189 default: | |
| 190 NOTREACHED(); | |
| 191 return; | |
| 192 } | |
| 193 | |
| 194 decryptor_->RegisterNewKeyCB( | 157 decryptor_->RegisterNewKeyCB( |
| 195 GetDecryptorStreamType(), | 158 GetDecryptorStreamType(), |
| 196 BIND_TO_LOOP(&DecryptingDemuxerStream::OnKeyAdded)); | 159 BIND_TO_LOOP(&DecryptingDemuxerStream::OnKeyAdded)); |
| 197 | 160 |
| 198 state_ = kIdle; | 161 state_ = kIdle; |
| 199 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); | 162 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK); |
| 200 } | 163 } |
| 201 | 164 |
| 202 void DecryptingDemuxerStream::DecryptBuffer( | 165 void DecryptingDemuxerStream::DecryptBuffer( |
| 203 DemuxerStream::Status status, | 166 DemuxerStream::Status status, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 228 | 191 |
| 229 if (status == kAborted) { | 192 if (status == kAborted) { |
| 230 DVLOG(2) << "DoDecryptBuffer() - kAborted."; | 193 DVLOG(2) << "DoDecryptBuffer() - kAborted."; |
| 231 state_ = kIdle; | 194 state_ = kIdle; |
| 232 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | 195 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); |
| 233 return; | 196 return; |
| 234 } | 197 } |
| 235 | 198 |
| 236 if (status == kConfigChanged) { | 199 if (status == kConfigChanged) { |
| 237 DVLOG(2) << "DoDecryptBuffer() - kConfigChanged."; | 200 DVLOG(2) << "DoDecryptBuffer() - kConfigChanged."; |
| 201 DCHECK_EQ(demuxer_stream_->type(), stream_type_); |
| 202 |
| 203 // Update the decoder config, which the decoder will use when it is notified |
| 204 // of kConfigChanged. |
| 205 SetDecoderConfig(demuxer_stream_); |
| 238 state_ = kIdle; | 206 state_ = kIdle; |
| 239 // TODO(xhwang): Support kConfigChanged! | 207 base::ResetAndReturn(&read_cb_).Run(kConfigChanged, NULL); |
| 240 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL); | |
| 241 return; | 208 return; |
| 242 } | 209 } |
| 243 | 210 |
| 244 if (buffer->IsEndOfStream()) { | 211 if (buffer->IsEndOfStream()) { |
| 245 DVLOG(2) << "DoDecryptBuffer() - EOS buffer."; | 212 DVLOG(2) << "DoDecryptBuffer() - EOS buffer."; |
| 246 state_ = kIdle; | 213 state_ = kIdle; |
| 247 base::ResetAndReturn(&read_cb_).Run(status, buffer); | 214 base::ResetAndReturn(&read_cb_).Run(status, buffer); |
| 248 return; | 215 return; |
| 249 } | 216 } |
| 250 | 217 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 DCHECK(read_cb_.is_null()); | 308 DCHECK(read_cb_.is_null()); |
| 342 state_ = kIdle; | 309 state_ = kIdle; |
| 343 base::ResetAndReturn(&reset_cb_).Run(); | 310 base::ResetAndReturn(&reset_cb_).Run(); |
| 344 } | 311 } |
| 345 | 312 |
| 346 Decryptor::StreamType DecryptingDemuxerStream::GetDecryptorStreamType() const { | 313 Decryptor::StreamType DecryptingDemuxerStream::GetDecryptorStreamType() const { |
| 347 DCHECK(stream_type_ == AUDIO || stream_type_ == VIDEO); | 314 DCHECK(stream_type_ == AUDIO || stream_type_ == VIDEO); |
| 348 return stream_type_ == AUDIO ? Decryptor::kAudio : Decryptor::kVideo; | 315 return stream_type_ == AUDIO ? Decryptor::kAudio : Decryptor::kVideo; |
| 349 } | 316 } |
| 350 | 317 |
| 318 void DecryptingDemuxerStream::SetDecoderConfig( |
| 319 const scoped_refptr<DemuxerStream>& stream) { |
| 320 // The decoder selector or upstream demuxer make sure the stream is valid and |
| 321 // potentially encrypted. |
| 322 DCHECK(IsStreamValidAndEncrypted(stream)); |
| 323 |
| 324 switch (stream_type_) { |
| 325 case AUDIO: { |
| 326 const AudioDecoderConfig& input_audio_config = |
| 327 stream->audio_decoder_config(); |
| 328 audio_config_.reset(new AudioDecoderConfig()); |
| 329 audio_config_->Initialize(input_audio_config.codec(), |
| 330 input_audio_config.bits_per_channel(), |
| 331 input_audio_config.channel_layout(), |
| 332 input_audio_config.samples_per_second(), |
| 333 input_audio_config.extra_data(), |
| 334 input_audio_config.extra_data_size(), |
| 335 false, // Output audio is not encrypted. |
| 336 false); |
| 337 break; |
| 338 } |
| 339 |
| 340 case VIDEO: { |
| 341 const VideoDecoderConfig& input_video_config = |
| 342 stream->video_decoder_config(); |
| 343 video_config_.reset(new VideoDecoderConfig()); |
| 344 video_config_->Initialize(input_video_config.codec(), |
| 345 input_video_config.profile(), |
| 346 input_video_config.format(), |
| 347 input_video_config.coded_size(), |
| 348 input_video_config.visible_rect(), |
| 349 input_video_config.natural_size(), |
| 350 input_video_config.extra_data(), |
| 351 input_video_config.extra_data_size(), |
| 352 false, // Output video is not encrypted. |
| 353 false); |
| 354 break; |
| 355 } |
| 356 |
| 357 default: |
| 358 NOTREACHED(); |
| 359 return; |
| 360 } |
| 361 } |
| 362 |
| 351 } // namespace media | 363 } // namespace media |
| OLD | NEW |