| 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_service.h" | 5 #include "media/mojo/services/mojo_decryptor_service.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "media/base/audio_decoder_config.h" | 10 #include "media/base/audio_decoder_config.h" |
| 9 #include "media/base/cdm_context.h" | 11 #include "media/base/cdm_context.h" |
| 10 #include "media/base/decoder_buffer.h" | 12 #include "media/base/decoder_buffer.h" |
| 11 #include "media/base/decryptor.h" | 13 #include "media/base/decryptor.h" |
| 12 #include "media/base/media_keys.h" | 14 #include "media/base/media_keys.h" |
| 13 #include "media/base/video_decoder_config.h" | 15 #include "media/base/video_decoder_config.h" |
| 16 #include "media/base/video_frame.h" |
| 14 #include "media/mojo/interfaces/demuxer_stream.mojom.h" | 17 #include "media/mojo/interfaces/demuxer_stream.mojom.h" |
| 15 #include "media/mojo/services/media_type_converters.h" | 18 #include "media/mojo/services/media_type_converters.h" |
| 16 | 19 |
| 17 namespace media { | 20 namespace media { |
| 18 | 21 |
| 19 MojoDecryptorService::MojoDecryptorService( | 22 MojoDecryptorService::MojoDecryptorService( |
| 20 const scoped_refptr<MediaKeys>& cdm, | 23 const scoped_refptr<MediaKeys>& cdm, |
| 21 mojo::InterfaceRequest<interfaces::Decryptor> request, | 24 mojo::InterfaceRequest<interfaces::Decryptor> request, |
| 22 const mojo::Closure& error_handler) | 25 const mojo::Closure& error_handler) |
| 23 : binding_(this, request.Pass()), cdm_(cdm), weak_factory_(this) { | 26 : binding_(this, request.Pass()), cdm_(cdm), weak_factory_(this) { |
| 24 decryptor_ = cdm->GetCdmContext()->GetDecryptor(); | 27 decryptor_ = cdm->GetCdmContext()->GetDecryptor(); |
| 25 DCHECK(decryptor_); | 28 DCHECK(decryptor_); |
| 26 weak_this_ = weak_factory_.GetWeakPtr(); | 29 weak_this_ = weak_factory_.GetWeakPtr(); |
| 27 binding_.set_connection_error_handler(error_handler); | 30 binding_.set_connection_error_handler(error_handler); |
| 28 } | 31 } |
| 29 | 32 |
| 30 MojoDecryptorService::~MojoDecryptorService() {} | 33 MojoDecryptorService::~MojoDecryptorService() {} |
| 31 | 34 |
| 35 void MojoDecryptorService::Initialize( |
| 36 mojo::ScopedDataPipeConsumerHandle receive_pipe, |
| 37 mojo::ScopedDataPipeProducerHandle transmit_pipe) { |
| 38 producer_handle_ = std::move(transmit_pipe); |
| 39 consumer_handle_ = std::move(receive_pipe); |
| 40 } |
| 41 |
| 32 void MojoDecryptorService::Decrypt(interfaces::DemuxerStream::Type stream_type, | 42 void MojoDecryptorService::Decrypt(interfaces::DemuxerStream::Type stream_type, |
| 33 interfaces::DecoderBufferPtr encrypted, | 43 interfaces::DecoderBufferPtr encrypted, |
| 34 const DecryptCallback& callback) { | 44 const DecryptCallback& callback) { |
| 45 DVLOG(3) << __FUNCTION__; |
| 35 decryptor_->Decrypt( | 46 decryptor_->Decrypt( |
| 36 static_cast<media::Decryptor::StreamType>(stream_type), | 47 static_cast<media::Decryptor::StreamType>(stream_type), |
| 37 encrypted.To<scoped_refptr<DecoderBuffer>>(), | 48 ReadDecoderBuffer(std::move(encrypted)), |
| 38 base::Bind(&MojoDecryptorService::OnDecryptDone, weak_this_, callback)); | 49 base::Bind(&MojoDecryptorService::OnDecryptDone, weak_this_, callback)); |
| 39 } | 50 } |
| 40 | 51 |
| 41 void MojoDecryptorService::CancelDecrypt( | 52 void MojoDecryptorService::CancelDecrypt( |
| 42 interfaces::DemuxerStream::Type stream_type) { | 53 interfaces::DemuxerStream::Type stream_type) { |
| 54 DVLOG(1) << __FUNCTION__; |
| 43 decryptor_->CancelDecrypt( | 55 decryptor_->CancelDecrypt( |
| 44 static_cast<media::Decryptor::StreamType>(stream_type)); | 56 static_cast<media::Decryptor::StreamType>(stream_type)); |
| 45 } | 57 } |
| 46 | 58 |
| 47 void MojoDecryptorService::InitializeAudioDecoder( | 59 void MojoDecryptorService::InitializeAudioDecoder( |
| 48 interfaces::AudioDecoderConfigPtr config, | 60 interfaces::AudioDecoderConfigPtr config, |
| 49 const InitializeAudioDecoderCallback& callback) { | 61 const InitializeAudioDecoderCallback& callback) { |
| 62 DVLOG(1) << __FUNCTION__; |
| 50 decryptor_->InitializeAudioDecoder( | 63 decryptor_->InitializeAudioDecoder( |
| 51 config.To<AudioDecoderConfig>(), | 64 config.To<AudioDecoderConfig>(), |
| 52 base::Bind(&MojoDecryptorService::OnAudioDecoderInitialized, weak_this_, | 65 base::Bind(&MojoDecryptorService::OnAudioDecoderInitialized, weak_this_, |
| 53 callback)); | 66 callback)); |
| 54 } | 67 } |
| 55 | 68 |
| 56 void MojoDecryptorService::InitializeVideoDecoder( | 69 void MojoDecryptorService::InitializeVideoDecoder( |
| 57 interfaces::VideoDecoderConfigPtr config, | 70 interfaces::VideoDecoderConfigPtr config, |
| 58 const InitializeVideoDecoderCallback& callback) { | 71 const InitializeVideoDecoderCallback& callback) { |
| 72 DVLOG(1) << __FUNCTION__; |
| 59 decryptor_->InitializeVideoDecoder( | 73 decryptor_->InitializeVideoDecoder( |
| 60 config.To<VideoDecoderConfig>(), | 74 config.To<VideoDecoderConfig>(), |
| 61 base::Bind(&MojoDecryptorService::OnVideoDecoderInitialized, weak_this_, | 75 base::Bind(&MojoDecryptorService::OnVideoDecoderInitialized, weak_this_, |
| 62 callback)); | 76 callback)); |
| 63 } | 77 } |
| 64 | 78 |
| 65 void MojoDecryptorService::DecryptAndDecodeAudio( | 79 void MojoDecryptorService::DecryptAndDecodeAudio( |
| 66 interfaces::DecoderBufferPtr encrypted, | 80 interfaces::DecoderBufferPtr encrypted, |
| 67 const DecryptAndDecodeAudioCallback& callback) { | 81 const DecryptAndDecodeAudioCallback& callback) { |
| 82 DVLOG(3) << __FUNCTION__; |
| 68 decryptor_->DecryptAndDecodeAudio( | 83 decryptor_->DecryptAndDecodeAudio( |
| 69 encrypted.To<scoped_refptr<DecoderBuffer>>(), | 84 ReadDecoderBuffer(std::move(encrypted)), |
| 70 base::Bind(&MojoDecryptorService::OnAudioDecoded, weak_this_, callback)); | 85 base::Bind(&MojoDecryptorService::OnAudioDecoded, weak_this_, callback)); |
| 71 } | 86 } |
| 72 | 87 |
| 73 void MojoDecryptorService::DecryptAndDecodeVideo( | 88 void MojoDecryptorService::DecryptAndDecodeVideo( |
| 74 interfaces::DecoderBufferPtr encrypted, | 89 interfaces::DecoderBufferPtr encrypted, |
| 75 const DecryptAndDecodeVideoCallback& callback) { | 90 const DecryptAndDecodeVideoCallback& callback) { |
| 91 DVLOG(3) << __FUNCTION__; |
| 76 decryptor_->DecryptAndDecodeVideo( | 92 decryptor_->DecryptAndDecodeVideo( |
| 77 encrypted.To<scoped_refptr<DecoderBuffer>>(), | 93 ReadDecoderBuffer(std::move(encrypted)), |
| 78 base::Bind(&MojoDecryptorService::OnVideoDecoded, weak_this_, callback)); | 94 base::Bind(&MojoDecryptorService::OnVideoDecoded, weak_this_, callback)); |
| 79 } | 95 } |
| 80 | 96 |
| 81 void MojoDecryptorService::ResetDecoder( | 97 void MojoDecryptorService::ResetDecoder( |
| 82 interfaces::DemuxerStream::Type stream_type) { | 98 interfaces::DemuxerStream::Type stream_type) { |
| 99 DVLOG(1) << __FUNCTION__; |
| 83 decryptor_->ResetDecoder( | 100 decryptor_->ResetDecoder( |
| 84 static_cast<media::Decryptor::StreamType>(stream_type)); | 101 static_cast<media::Decryptor::StreamType>(stream_type)); |
| 85 } | 102 } |
| 86 | 103 |
| 87 void MojoDecryptorService::DeinitializeDecoder( | 104 void MojoDecryptorService::DeinitializeDecoder( |
| 88 interfaces::DemuxerStream::Type stream_type) { | 105 interfaces::DemuxerStream::Type stream_type) { |
| 106 DVLOG(1) << __FUNCTION__; |
| 89 decryptor_->DeinitializeDecoder( | 107 decryptor_->DeinitializeDecoder( |
| 90 static_cast<media::Decryptor::StreamType>(stream_type)); | 108 static_cast<media::Decryptor::StreamType>(stream_type)); |
| 91 } | 109 } |
| 92 | 110 |
| 93 void MojoDecryptorService::OnDecryptDone( | 111 void MojoDecryptorService::OnDecryptDone( |
| 94 const DecryptCallback& callback, | 112 const DecryptCallback& callback, |
| 95 media::Decryptor::Status status, | 113 media::Decryptor::Status status, |
| 96 const scoped_refptr<DecoderBuffer>& buffer) { | 114 const scoped_refptr<DecoderBuffer>& buffer) { |
| 97 DVLOG(1) << __FUNCTION__ << "(" << status << ")"; | 115 DVLOG(status != media::Decryptor::kSuccess ? 1 : 3) << __FUNCTION__ << "(" |
| 116 << status << ")"; |
| 117 if (!buffer) { |
| 118 DCHECK_NE(status, media::Decryptor::kSuccess); |
| 119 callback.Run(static_cast<Decryptor::Status>(status), nullptr); |
| 120 return; |
| 121 } |
| 122 |
| 98 callback.Run(static_cast<Decryptor::Status>(status), | 123 callback.Run(static_cast<Decryptor::Status>(status), |
| 99 interfaces::DecoderBuffer::From(buffer)); | 124 TransferDecoderBuffer(buffer)); |
| 100 } | 125 } |
| 101 | 126 |
| 102 void MojoDecryptorService::OnAudioDecoderInitialized( | 127 void MojoDecryptorService::OnAudioDecoderInitialized( |
| 103 const InitializeAudioDecoderCallback& callback, | 128 const InitializeAudioDecoderCallback& callback, |
| 104 bool success) { | 129 bool success) { |
| 105 DVLOG(1) << __FUNCTION__ << "(" << success << ")"; | 130 DVLOG(1) << __FUNCTION__ << "(" << success << ")"; |
| 106 callback.Run(success); | 131 callback.Run(success); |
| 107 } | 132 } |
| 108 | 133 |
| 109 void MojoDecryptorService::OnVideoDecoderInitialized( | 134 void MojoDecryptorService::OnVideoDecoderInitialized( |
| 110 const InitializeVideoDecoderCallback& callback, | 135 const InitializeVideoDecoderCallback& callback, |
| 111 bool success) { | 136 bool success) { |
| 112 DVLOG(1) << __FUNCTION__ << "(" << success << ")"; | 137 DVLOG(1) << __FUNCTION__ << "(" << success << ")"; |
| 113 callback.Run(success); | 138 callback.Run(success); |
| 114 } | 139 } |
| 115 | 140 |
| 116 void MojoDecryptorService::OnAudioDecoded( | 141 void MojoDecryptorService::OnAudioDecoded( |
| 117 const DecryptAndDecodeAudioCallback& callback, | 142 const DecryptAndDecodeAudioCallback& callback, |
| 118 media::Decryptor::Status status, | 143 media::Decryptor::Status status, |
| 119 const media::Decryptor::AudioFrames& frames) { | 144 const media::Decryptor::AudioFrames& frames) { |
| 120 DVLOG(1) << __FUNCTION__ << "(" << status << ")"; | 145 DVLOG(status != media::Decryptor::kSuccess ? 1 : 3) << __FUNCTION__ << "(" |
| 121 | 146 << status << ")"; |
| 122 mojo::Array<interfaces::AudioBufferPtr> audio_buffers; | 147 mojo::Array<interfaces::AudioBufferPtr> audio_buffers; |
| 123 for (const auto& frame : frames) | 148 for (const auto& frame : frames) |
| 124 audio_buffers.push_back(interfaces::AudioBuffer::From(frame).Pass()); | 149 audio_buffers.push_back(interfaces::AudioBuffer::From(frame).Pass()); |
| 125 | 150 |
| 126 callback.Run(static_cast<Decryptor::Status>(status), audio_buffers.Pass()); | 151 callback.Run(static_cast<Decryptor::Status>(status), audio_buffers.Pass()); |
| 127 } | 152 } |
| 128 | 153 |
| 129 void MojoDecryptorService::OnVideoDecoded( | 154 void MojoDecryptorService::OnVideoDecoded( |
| 130 const DecryptAndDecodeVideoCallback& callback, | 155 const DecryptAndDecodeVideoCallback& callback, |
| 131 media::Decryptor::Status status, | 156 media::Decryptor::Status status, |
| 132 const scoped_refptr<VideoFrame>& frame) { | 157 const scoped_refptr<VideoFrame>& frame) { |
| 133 DVLOG(1) << __FUNCTION__ << "(" << status << ")"; | 158 DVLOG(status != media::Decryptor::kSuccess ? 1 : 3) << __FUNCTION__ << "(" |
| 159 << status << ")"; |
| 160 if (!frame) { |
| 161 DCHECK_NE(status, media::Decryptor::kSuccess); |
| 162 callback.Run(static_cast<Decryptor::Status>(status), nullptr); |
| 163 return; |
| 164 } |
| 134 | 165 |
| 135 callback.Run(static_cast<Decryptor::Status>(status), | 166 callback.Run(static_cast<Decryptor::Status>(status), |
| 136 interfaces::VideoFrame::From(frame).Pass()); | 167 interfaces::VideoFrame::From(frame).Pass()); |
| 137 } | 168 } |
| 138 | 169 |
| 170 interfaces::DecoderBufferPtr MojoDecryptorService::TransferDecoderBuffer( |
| 171 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 172 interfaces::DecoderBufferPtr buffer = |
| 173 interfaces::DecoderBuffer::From(encrypted); |
| 174 if (encrypted->end_of_stream()) |
| 175 return buffer; |
| 176 |
| 177 // Serialize the data section of the DecoderBuffer into our pipe. |
| 178 uint32_t num_bytes = encrypted->data_size(); |
| 179 DCHECK_GT(num_bytes, 0u); |
| 180 CHECK_EQ(WriteDataRaw(producer_handle_.get(), encrypted->data(), &num_bytes, |
| 181 MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 182 MOJO_RESULT_OK); |
| 183 CHECK_EQ(num_bytes, static_cast<uint32_t>(encrypted->data_size())); |
| 184 return buffer; |
| 185 } |
| 186 |
| 187 scoped_refptr<DecoderBuffer> MojoDecryptorService::ReadDecoderBuffer( |
| 188 interfaces::DecoderBufferPtr buffer) { |
| 189 scoped_refptr<DecoderBuffer> media_buffer( |
| 190 buffer.To<scoped_refptr<DecoderBuffer>>()); |
| 191 if (media_buffer->end_of_stream()) |
| 192 return media_buffer; |
| 193 |
| 194 // Read the inner data for the DecoderBuffer from our DataPipe. |
| 195 uint32_t num_bytes = media_buffer->data_size(); |
| 196 DCHECK_GT(num_bytes, 0u); |
| 197 CHECK_EQ(ReadDataRaw(consumer_handle_.get(), media_buffer->writable_data(), |
| 198 &num_bytes, MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 199 MOJO_RESULT_OK); |
| 200 CHECK_EQ(num_bytes, static_cast<uint32_t>(media_buffer->data_size())); |
| 201 return media_buffer; |
| 202 } |
| 203 |
| 139 } // namespace media | 204 } // namespace media |
| OLD | NEW |