| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/mojo/services/mojo_decryptor.h" | 5 #include "media/mojo/services/mojo_decryptor.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "media/base/audio_buffer.h" | 10 #include "media/base/audio_buffer.h" |
| 11 #include "media/base/decoder_buffer.h" | 11 #include "media/base/decoder_buffer.h" |
| 12 #include "media/base/video_frame.h" | 12 #include "media/base/video_frame.h" |
| 13 #include "media/mojo/interfaces/decryptor.mojom.h" | 13 #include "media/mojo/interfaces/decryptor.mojom.h" |
| 14 #include "media/mojo/services/media_type_converters.h" | 14 #include "media/mojo/services/media_type_converters.h" |
| 15 #include "mojo/application/public/cpp/connect.h" | 15 #include "mojo/application/public/cpp/connect.h" |
| 16 | 16 |
| 17 namespace media { | 17 namespace media { |
| 18 | 18 |
| 19 MojoDecryptor::MojoDecryptor(interfaces::DecryptorPtr remote_decryptor) | 19 MojoDecryptor::MojoDecryptor(interfaces::DecryptorPtr remote_decryptor) |
| 20 : remote_decryptor_(std::move(remote_decryptor)), weak_factory_(this) {} | 20 : remote_decryptor_(std::move(remote_decryptor)), weak_factory_(this) { |
| 21 CreateDataPipes(); |
| 22 } |
| 21 | 23 |
| 22 MojoDecryptor::~MojoDecryptor() {} | 24 MojoDecryptor::~MojoDecryptor() {} |
| 23 | 25 |
| 24 void MojoDecryptor::RegisterNewKeyCB(StreamType stream_type, | 26 void MojoDecryptor::RegisterNewKeyCB(StreamType stream_type, |
| 25 const NewKeyCB& key_added_cb) { | 27 const NewKeyCB& key_added_cb) { |
| 26 switch (stream_type) { | 28 switch (stream_type) { |
| 27 case kAudio: | 29 case kAudio: |
| 28 new_audio_key_cb_ = key_added_cb; | 30 new_audio_key_cb_ = key_added_cb; |
| 29 break; | 31 break; |
| 30 case kVideo: | 32 case kVideo: |
| 31 new_video_key_cb_ = key_added_cb; | 33 new_video_key_cb_ = key_added_cb; |
| 32 break; | 34 break; |
| 33 default: | 35 default: |
| 34 NOTREACHED(); | 36 NOTREACHED(); |
| 35 } | 37 } |
| 36 } | 38 } |
| 37 | 39 |
| 38 void MojoDecryptor::Decrypt(StreamType stream_type, | 40 void MojoDecryptor::Decrypt(StreamType stream_type, |
| 39 const scoped_refptr<DecoderBuffer>& encrypted, | 41 const scoped_refptr<DecoderBuffer>& encrypted, |
| 40 const DecryptCB& decrypt_cb) { | 42 const DecryptCB& decrypt_cb) { |
| 43 DVLOG(3) << __FUNCTION__; |
| 41 remote_decryptor_->Decrypt( | 44 remote_decryptor_->Decrypt( |
| 42 static_cast<interfaces::DemuxerStream::Type>(stream_type), | 45 static_cast<interfaces::DemuxerStream::Type>(stream_type), |
| 43 interfaces::DecoderBuffer::From(encrypted), | 46 TransferDecoderBuffer(encrypted), |
| 44 base::Bind(&MojoDecryptor::OnBufferDecrypted, weak_factory_.GetWeakPtr(), | 47 base::Bind(&MojoDecryptor::OnBufferDecrypted, weak_factory_.GetWeakPtr(), |
| 45 decrypt_cb)); | 48 decrypt_cb)); |
| 46 } | 49 } |
| 47 | 50 |
| 48 void MojoDecryptor::CancelDecrypt(StreamType stream_type) { | 51 void MojoDecryptor::CancelDecrypt(StreamType stream_type) { |
| 52 DVLOG(1) << __FUNCTION__; |
| 49 remote_decryptor_->CancelDecrypt( | 53 remote_decryptor_->CancelDecrypt( |
| 50 static_cast<interfaces::DemuxerStream::Type>(stream_type)); | 54 static_cast<interfaces::DemuxerStream::Type>(stream_type)); |
| 51 } | 55 } |
| 52 | 56 |
| 53 void MojoDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, | 57 void MojoDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, |
| 54 const DecoderInitCB& init_cb) { | 58 const DecoderInitCB& init_cb) { |
| 59 DVLOG(1) << __FUNCTION__; |
| 55 remote_decryptor_->InitializeAudioDecoder( | 60 remote_decryptor_->InitializeAudioDecoder( |
| 56 interfaces::AudioDecoderConfig::From(config), init_cb); | 61 interfaces::AudioDecoderConfig::From(config), init_cb); |
| 57 } | 62 } |
| 58 | 63 |
| 59 void MojoDecryptor::InitializeVideoDecoder(const VideoDecoderConfig& config, | 64 void MojoDecryptor::InitializeVideoDecoder(const VideoDecoderConfig& config, |
| 60 const DecoderInitCB& init_cb) { | 65 const DecoderInitCB& init_cb) { |
| 66 DVLOG(1) << __FUNCTION__; |
| 61 remote_decryptor_->InitializeVideoDecoder( | 67 remote_decryptor_->InitializeVideoDecoder( |
| 62 interfaces::VideoDecoderConfig::From(config), init_cb); | 68 interfaces::VideoDecoderConfig::From(config), init_cb); |
| 63 } | 69 } |
| 64 | 70 |
| 65 void MojoDecryptor::DecryptAndDecodeAudio( | 71 void MojoDecryptor::DecryptAndDecodeAudio( |
| 66 const scoped_refptr<DecoderBuffer>& encrypted, | 72 const scoped_refptr<DecoderBuffer>& encrypted, |
| 67 const AudioDecodeCB& audio_decode_cb) { | 73 const AudioDecodeCB& audio_decode_cb) { |
| 74 DVLOG(3) << __FUNCTION__; |
| 68 remote_decryptor_->DecryptAndDecodeAudio( | 75 remote_decryptor_->DecryptAndDecodeAudio( |
| 69 interfaces::DecoderBuffer::From(encrypted), | 76 TransferDecoderBuffer(encrypted), |
| 70 base::Bind(&MojoDecryptor::OnAudioDecoded, weak_factory_.GetWeakPtr(), | 77 base::Bind(&MojoDecryptor::OnAudioDecoded, weak_factory_.GetWeakPtr(), |
| 71 audio_decode_cb)); | 78 audio_decode_cb)); |
| 72 } | 79 } |
| 73 | 80 |
| 74 void MojoDecryptor::DecryptAndDecodeVideo( | 81 void MojoDecryptor::DecryptAndDecodeVideo( |
| 75 const scoped_refptr<DecoderBuffer>& encrypted, | 82 const scoped_refptr<DecoderBuffer>& encrypted, |
| 76 const VideoDecodeCB& video_decode_cb) { | 83 const VideoDecodeCB& video_decode_cb) { |
| 84 DVLOG(3) << __FUNCTION__; |
| 77 remote_decryptor_->DecryptAndDecodeVideo( | 85 remote_decryptor_->DecryptAndDecodeVideo( |
| 78 interfaces::DecoderBuffer::From(encrypted), | 86 TransferDecoderBuffer(encrypted), |
| 79 base::Bind(&MojoDecryptor::OnVideoDecoded, weak_factory_.GetWeakPtr(), | 87 base::Bind(&MojoDecryptor::OnVideoDecoded, weak_factory_.GetWeakPtr(), |
| 80 video_decode_cb)); | 88 video_decode_cb)); |
| 81 } | 89 } |
| 82 | 90 |
| 83 void MojoDecryptor::ResetDecoder(StreamType stream_type) { | 91 void MojoDecryptor::ResetDecoder(StreamType stream_type) { |
| 92 DVLOG(1) << __FUNCTION__; |
| 84 remote_decryptor_->ResetDecoder( | 93 remote_decryptor_->ResetDecoder( |
| 85 static_cast<interfaces::DemuxerStream::Type>(stream_type)); | 94 static_cast<interfaces::DemuxerStream::Type>(stream_type)); |
| 86 } | 95 } |
| 87 | 96 |
| 88 void MojoDecryptor::DeinitializeDecoder(StreamType stream_type) { | 97 void MojoDecryptor::DeinitializeDecoder(StreamType stream_type) { |
| 98 DVLOG(1) << __FUNCTION__; |
| 89 remote_decryptor_->DeinitializeDecoder( | 99 remote_decryptor_->DeinitializeDecoder( |
| 90 static_cast<interfaces::DemuxerStream::Type>(stream_type)); | 100 static_cast<interfaces::DemuxerStream::Type>(stream_type)); |
| 91 } | 101 } |
| 92 | 102 |
| 93 void MojoDecryptor::OnKeyAdded() { | 103 void MojoDecryptor::OnKeyAdded() { |
| 104 DVLOG(1) << __FUNCTION__; |
| 94 if (!new_audio_key_cb_.is_null()) | 105 if (!new_audio_key_cb_.is_null()) |
| 95 new_audio_key_cb_.Run(); | 106 new_audio_key_cb_.Run(); |
| 96 | 107 |
| 97 if (!new_video_key_cb_.is_null()) | 108 if (!new_video_key_cb_.is_null()) |
| 98 new_video_key_cb_.Run(); | 109 new_video_key_cb_.Run(); |
| 99 } | 110 } |
| 100 | 111 |
| 101 void MojoDecryptor::OnBufferDecrypted(const DecryptCB& decrypt_cb, | 112 void MojoDecryptor::OnBufferDecrypted(const DecryptCB& decrypt_cb, |
| 102 interfaces::Decryptor::Status status, | 113 interfaces::Decryptor::Status status, |
| 103 interfaces::DecoderBufferPtr buffer) { | 114 interfaces::DecoderBufferPtr buffer) { |
| 115 DVLOG(status != interfaces::Decryptor::STATUS_SUCCESS ? 1 : 3) |
| 116 << __FUNCTION__ << "(" << status << ")"; |
| 117 if (buffer.is_null()) { |
| 118 decrypt_cb.Run(static_cast<Decryptor::Status>(status), nullptr); |
| 119 return; |
| 120 } |
| 121 |
| 104 decrypt_cb.Run(static_cast<Decryptor::Status>(status), | 122 decrypt_cb.Run(static_cast<Decryptor::Status>(status), |
| 105 std::move(buffer.To<scoped_refptr<DecoderBuffer>>())); | 123 ReadDecoderBuffer(std::move(buffer))); |
| 106 } | 124 } |
| 107 | 125 |
| 108 void MojoDecryptor::OnAudioDecoded( | 126 void MojoDecryptor::OnAudioDecoded( |
| 109 const AudioDecodeCB& audio_decode_cb, | 127 const AudioDecodeCB& audio_decode_cb, |
| 110 interfaces::Decryptor::Status status, | 128 interfaces::Decryptor::Status status, |
| 111 mojo::Array<interfaces::AudioBufferPtr> audio_buffers) { | 129 mojo::Array<interfaces::AudioBufferPtr> audio_buffers) { |
| 130 DVLOG(status != interfaces::Decryptor::STATUS_SUCCESS ? 1 : 3) |
| 131 << __FUNCTION__ << "(" << status << ")"; |
| 112 Decryptor::AudioFrames audio_frames; | 132 Decryptor::AudioFrames audio_frames; |
| 113 for (size_t i = 0; i < audio_buffers.size(); ++i) | 133 for (size_t i = 0; i < audio_buffers.size(); ++i) |
| 114 audio_frames.push_back(audio_buffers[i].To<scoped_refptr<AudioBuffer>>()); | 134 audio_frames.push_back(audio_buffers[i].To<scoped_refptr<AudioBuffer>>()); |
| 115 | 135 |
| 116 audio_decode_cb.Run(static_cast<Decryptor::Status>(status), audio_frames); | 136 audio_decode_cb.Run(static_cast<Decryptor::Status>(status), audio_frames); |
| 117 } | 137 } |
| 118 | 138 |
| 119 void MojoDecryptor::OnVideoDecoded(const VideoDecodeCB& video_decode_cb, | 139 void MojoDecryptor::OnVideoDecoded(const VideoDecodeCB& video_decode_cb, |
| 120 interfaces::Decryptor::Status status, | 140 interfaces::Decryptor::Status status, |
| 121 interfaces::VideoFramePtr video_frame) { | 141 interfaces::VideoFramePtr video_frame) { |
| 122 video_decode_cb.Run(static_cast<Decryptor::Status>(status), | 142 DVLOG(status != interfaces::Decryptor::STATUS_SUCCESS ? 1 : 3) |
| 123 std::move(video_frame.To<scoped_refptr<VideoFrame>>())); | 143 << __FUNCTION__ << "(" << status << ")"; |
| 144 if (video_frame.is_null()) { |
| 145 video_decode_cb.Run(static_cast<Decryptor::Status>(status), nullptr); |
| 146 return; |
| 147 } |
| 148 |
| 149 scoped_refptr<VideoFrame> frame(video_frame.To<scoped_refptr<VideoFrame>>()); |
| 150 video_decode_cb.Run(static_cast<Decryptor::Status>(status), frame); |
| 151 } |
| 152 |
| 153 void MojoDecryptor::CreateDataPipes() { |
| 154 // Allocate DataPipe size based on video content. Video can get quite large; |
| 155 // at 4K, VP9 delivers packets which are ~1MB in size; so allow for 50% |
| 156 // headroom. |
| 157 MojoCreateDataPipeOptions options; |
| 158 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 159 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 160 options.element_num_bytes = 1; |
| 161 options.capacity_num_bytes = 1.5 * (1024 * 1024); |
| 162 |
| 163 // Create 2 pipes, one for each direction. |
| 164 mojo::DataPipe write_pipe(options); |
| 165 mojo::DataPipe read_pipe(options); |
| 166 |
| 167 // Keep one end of each pipe. |
| 168 producer_handle_ = std::move(write_pipe.producer_handle); |
| 169 consumer_handle_ = std::move(read_pipe.consumer_handle); |
| 170 |
| 171 // Pass the other end of each pipe to |remote_decryptor_|. |
| 172 remote_decryptor_->Initialize(std::move(write_pipe.consumer_handle), |
| 173 std::move(read_pipe.producer_handle)); |
| 174 } |
| 175 |
| 176 interfaces::DecoderBufferPtr MojoDecryptor::TransferDecoderBuffer( |
| 177 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 178 interfaces::DecoderBufferPtr buffer = |
| 179 interfaces::DecoderBuffer::From(encrypted); |
| 180 if (encrypted->end_of_stream()) |
| 181 return buffer; |
| 182 |
| 183 // Serialize the data section of the DecoderBuffer into our pipe. |
| 184 uint32_t num_bytes = encrypted->data_size(); |
| 185 DCHECK_GT(num_bytes, 0u); |
| 186 CHECK_EQ(WriteDataRaw(producer_handle_.get(), encrypted->data(), &num_bytes, |
| 187 MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 188 MOJO_RESULT_OK); |
| 189 CHECK_EQ(num_bytes, static_cast<uint32_t>(encrypted->data_size())); |
| 190 return buffer; |
| 191 } |
| 192 |
| 193 scoped_refptr<DecoderBuffer> MojoDecryptor::ReadDecoderBuffer( |
| 194 interfaces::DecoderBufferPtr buffer) { |
| 195 scoped_refptr<DecoderBuffer> media_buffer( |
| 196 buffer.To<scoped_refptr<DecoderBuffer>>()); |
| 197 if (media_buffer->end_of_stream()) |
| 198 return media_buffer; |
| 199 |
| 200 // Read the inner data for the DecoderBuffer from our DataPipe. |
| 201 uint32_t num_bytes = media_buffer->data_size(); |
| 202 DCHECK_GT(num_bytes, 0u); |
| 203 CHECK_EQ(ReadDataRaw(consumer_handle_.get(), media_buffer->writable_data(), |
| 204 &num_bytes, MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 205 MOJO_RESULT_OK); |
| 206 CHECK_EQ(num_bytes, static_cast<uint32_t>(media_buffer->data_size())); |
| 207 return media_buffer; |
| 124 } | 208 } |
| 125 | 209 |
| 126 } // namespace media | 210 } // namespace media |
| OLD | NEW |