Chromium Code Reviews| Index: media/mojo/services/mojo_decryptor.cc |
| diff --git a/media/mojo/services/mojo_decryptor.cc b/media/mojo/services/mojo_decryptor.cc |
| index 74af5cf7772c8e0c9f813517f92cf91c0c43f03f..28f5255c9395fd27368e627c9adcd3f0de7883ef 100644 |
| --- a/media/mojo/services/mojo_decryptor.cc |
| +++ b/media/mojo/services/mojo_decryptor.cc |
| @@ -17,7 +17,9 @@ |
| namespace media { |
| MojoDecryptor::MojoDecryptor(interfaces::DecryptorPtr remote_decryptor) |
| - : remote_decryptor_(std::move(remote_decryptor)), weak_factory_(this) {} |
| + : remote_decryptor_(std::move(remote_decryptor)), weak_factory_(this) { |
| + CreateDataPipes(); |
| +} |
| MojoDecryptor::~MojoDecryptor() {} |
| @@ -38,6 +40,8 @@ void MojoDecryptor::RegisterNewKeyCB(StreamType stream_type, |
| void MojoDecryptor::Decrypt(StreamType stream_type, |
| const scoped_refptr<DecoderBuffer>& encrypted, |
| const DecryptCB& decrypt_cb) { |
| + DVLOG(1) << __FUNCTION__; |
| + WriteDataIntoDataPipe(encrypted); |
| remote_decryptor_->Decrypt( |
| static_cast<interfaces::DemuxerStream::Type>(stream_type), |
| interfaces::DecoderBuffer::From(encrypted), |
| @@ -46,18 +50,21 @@ void MojoDecryptor::Decrypt(StreamType stream_type, |
| } |
| void MojoDecryptor::CancelDecrypt(StreamType stream_type) { |
| + DVLOG(1) << __FUNCTION__; |
| remote_decryptor_->CancelDecrypt( |
| static_cast<interfaces::DemuxerStream::Type>(stream_type)); |
| } |
| void MojoDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, |
| const DecoderInitCB& init_cb) { |
| + DVLOG(1) << __FUNCTION__; |
| remote_decryptor_->InitializeAudioDecoder( |
| interfaces::AudioDecoderConfig::From(config), init_cb); |
| } |
| void MojoDecryptor::InitializeVideoDecoder(const VideoDecoderConfig& config, |
| const DecoderInitCB& init_cb) { |
| + DVLOG(1) << __FUNCTION__; |
| remote_decryptor_->InitializeVideoDecoder( |
| interfaces::VideoDecoderConfig::From(config), init_cb); |
| } |
| @@ -65,6 +72,8 @@ void MojoDecryptor::InitializeVideoDecoder(const VideoDecoderConfig& config, |
| void MojoDecryptor::DecryptAndDecodeAudio( |
| const scoped_refptr<DecoderBuffer>& encrypted, |
| const AudioDecodeCB& audio_decode_cb) { |
| + DVLOG(1) << __FUNCTION__; |
|
xhwang
2015/12/19 18:16:30
Probably use DVLOG(3) for repeating events.
jrummell
2015/12/21 23:20:55
Done.
|
| + WriteDataIntoDataPipe(encrypted); |
| remote_decryptor_->DecryptAndDecodeAudio( |
| interfaces::DecoderBuffer::From(encrypted), |
|
xhwang
2015/12/19 18:16:30
It might be more clear to do:
buffer = interfaces
jrummell
2015/12/21 23:20:55
Done.
|
| base::Bind(&MojoDecryptor::OnAudioDecoded, weak_factory_.GetWeakPtr(), |
| @@ -74,6 +83,9 @@ void MojoDecryptor::DecryptAndDecodeAudio( |
| void MojoDecryptor::DecryptAndDecodeVideo( |
| const scoped_refptr<DecoderBuffer>& encrypted, |
| const VideoDecodeCB& video_decode_cb) { |
| + DVLOG(1) << __FUNCTION__; |
| + DVLOG(3) << " encrypted: " << encrypted->AsHumanReadableString(); |
| + WriteDataIntoDataPipe(encrypted); |
| remote_decryptor_->DecryptAndDecodeVideo( |
| interfaces::DecoderBuffer::From(encrypted), |
| base::Bind(&MojoDecryptor::OnVideoDecoded, weak_factory_.GetWeakPtr(), |
| @@ -81,16 +93,19 @@ void MojoDecryptor::DecryptAndDecodeVideo( |
| } |
| void MojoDecryptor::ResetDecoder(StreamType stream_type) { |
| + DVLOG(1) << __FUNCTION__; |
| remote_decryptor_->ResetDecoder( |
| static_cast<interfaces::DemuxerStream::Type>(stream_type)); |
| } |
| void MojoDecryptor::DeinitializeDecoder(StreamType stream_type) { |
| + DVLOG(1) << __FUNCTION__; |
| remote_decryptor_->DeinitializeDecoder( |
| static_cast<interfaces::DemuxerStream::Type>(stream_type)); |
| } |
| void MojoDecryptor::OnKeyAdded() { |
| + DVLOG(1) << __FUNCTION__; |
| if (!new_audio_key_cb_.is_null()) |
| new_audio_key_cb_.Run(); |
| @@ -101,14 +116,24 @@ void MojoDecryptor::OnKeyAdded() { |
| void MojoDecryptor::OnBufferDecrypted(const DecryptCB& decrypt_cb, |
| interfaces::Decryptor::Status status, |
| interfaces::DecoderBufferPtr buffer) { |
| - decrypt_cb.Run(static_cast<Decryptor::Status>(status), |
| - std::move(buffer.To<scoped_refptr<DecoderBuffer>>())); |
| + DVLOG(1) << __FUNCTION__ << "(" << status << ")"; |
| + if (buffer.is_null()) { |
| + decrypt_cb.Run(static_cast<Decryptor::Status>(status), nullptr); |
| + return; |
| + } |
| + |
| + scoped_refptr<DecoderBuffer> media_buffer( |
| + buffer.To<scoped_refptr<DecoderBuffer>>()); |
| + ReadDataFromDataPipe(media_buffer); |
| + DVLOG(3) << " media_buffer: " << media_buffer->AsHumanReadableString(); |
| + decrypt_cb.Run(static_cast<Decryptor::Status>(status), media_buffer); |
| } |
| void MojoDecryptor::OnAudioDecoded( |
| const AudioDecodeCB& audio_decode_cb, |
| interfaces::Decryptor::Status status, |
| mojo::Array<interfaces::AudioBufferPtr> audio_buffers) { |
| + DVLOG(1) << __FUNCTION__ << "(" << status << ")"; |
|
xhwang
2015/12/19 18:16:30
ditto about log level
jrummell
2015/12/21 23:20:55
Done.
|
| Decryptor::AudioFrames audio_frames; |
| for (size_t i = 0; i < audio_buffers.size(); ++i) |
| audio_frames.push_back(audio_buffers[i].To<scoped_refptr<AudioBuffer>>()); |
| @@ -119,8 +144,64 @@ void MojoDecryptor::OnAudioDecoded( |
| void MojoDecryptor::OnVideoDecoded(const VideoDecodeCB& video_decode_cb, |
| interfaces::Decryptor::Status status, |
| interfaces::VideoFramePtr video_frame) { |
| - video_decode_cb.Run(static_cast<Decryptor::Status>(status), |
| - std::move(video_frame.To<scoped_refptr<VideoFrame>>())); |
| + DVLOG(1) << __FUNCTION__ << "(" << status << ")"; |
|
xhwang
2015/12/19 18:16:30
ditto about log level
jrummell
2015/12/21 23:20:55
Done.
|
| + if (video_frame.is_null()) { |
| + video_decode_cb.Run(static_cast<Decryptor::Status>(status), nullptr); |
| + return; |
| + } |
| + |
| + scoped_refptr<VideoFrame> frame(video_frame.To<scoped_refptr<VideoFrame>>()); |
| + DVLOG(3) << " video_frame: " << frame->AsHumanReadableString(); |
|
xhwang
2015/12/19 18:16:30
We probably don't need this by default.
jrummell
2015/12/21 23:20:55
Done.
|
| + video_decode_cb.Run(static_cast<Decryptor::Status>(status), frame); |
| +} |
| + |
| +void MojoDecryptor::CreateDataPipes() { |
| + // Allocate DataPipe size based on video content. Video can get quite large; |
| + // at 4K, VP9 delivers packets which are ~1MB in size; so allow for 50% |
| + // headroom. |
| + MojoCreateDataPipeOptions options; |
| + options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| + options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| + options.element_num_bytes = 1; |
| + options.capacity_num_bytes = 1.5 * (1024 * 1024); |
| + |
| + // Create 2 pipes, one for each direction. |
| + mojo::DataPipe data_pipe1(options); |
| + mojo::DataPipe data_pipe2(options); |
|
xhwang
2015/12/19 18:16:30
naming: it seems these should be write/read pipe,
jrummell
2015/12/21 23:20:55
Done.
|
| + |
| + // Keep one end of each pipe. |
| + write_pipe_ = data_pipe1.producer_handle.Pass(); |
| + read_pipe_ = data_pipe2.consumer_handle.Pass(); |
| + |
| + // Pass the other end of each pipe to |remote_decryptor_|. |
| + remote_decryptor_->Initialize(data_pipe1.consumer_handle.Pass(), |
| + data_pipe2.producer_handle.Pass()); |
| +} |
| + |
| +void MojoDecryptor::WriteDataIntoDataPipe( |
| + const scoped_refptr<DecoderBuffer>& buffer) { |
| + if (!buffer->end_of_stream()) { |
|
xhwang
2015/12/19 18:16:30
return early:
if (buffer->end_of_stream())
retu
jrummell
2015/12/21 23:20:55
Done.
|
| + // Serialize the data section of the DecoderBuffer into our pipe. |
| + uint32_t num_bytes = buffer->data_size(); |
| + DCHECK_GT(num_bytes, 0u); |
| + CHECK_EQ(WriteDataRaw(write_pipe_.get(), buffer->data(), &num_bytes, |
| + MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| + MOJO_RESULT_OK); |
| + CHECK_EQ(num_bytes, static_cast<uint32_t>(buffer->data_size())); |
| + } |
| +} |
| + |
| +void MojoDecryptor::ReadDataFromDataPipe( |
| + const scoped_refptr<DecoderBuffer>& buffer) { |
| + if (!buffer->end_of_stream()) { |
|
xhwang
2015/12/19 18:16:30
ditto
jrummell
2015/12/21 23:20:55
Done.
|
| + // Read the inner data for the DecoderBuffer from our DataPipe. |
| + uint32_t num_bytes = buffer->data_size(); |
| + DCHECK_GT(num_bytes, 0u); |
| + CHECK_EQ(ReadDataRaw(read_pipe_.get(), buffer->writable_data(), &num_bytes, |
| + MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| + MOJO_RESULT_OK); |
| + CHECK_EQ(num_bytes, static_cast<uint32_t>(buffer->data_size())); |
| + } |
| } |
| } // namespace media |