Commit ea55047f authored by Xiaohan Wang's avatar Xiaohan Wang Committed by Commit Bot

media: Support Decryptor in MediaService

This CL provides a way for a MediaService client to create a decryptor
which is backed by a CDM or a CdmProxy, also running in the MediaService.

Detailed changes:
- Add CreateDecryptor() in media::mojom::InterfaceFactory
- Plumb CreateDecryptor() through multiple layers to reach
  InterfaceFactoryImpl in MediaService
- In InterfaceFactoryImpl, use the |cdm_id| to find the CdmContextRef,
  and then if a Decryptor is supported, create a MojoDecryptorService.
- Add a dummy D3D11Decryptor implementation.
- Update ClearKeyCdmProxy to support Decryptor.
- Add MediaServiceTest to test this path.

Bug: 806018
Test: media_service_unittests updated.
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: I0eba8aef333cb55ad5180ce1a401f3384c95c9c8
Reviewed-on: https://chromium-review.googlesource.com/1041660Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarFrank Liberato <liberato@chromium.org>
Commit-Queue: Xiaohan Wang <xhwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#556946}
parent d18f3ac6
......@@ -195,6 +195,14 @@ void MediaInterfaceProxy::CreateCdm(
#endif
}
void MediaInterfaceProxy::CreateDecryptor(
int cdm_id,
media::mojom::DecryptorRequest request) {
InterfaceFactory* factory = GetMediaInterfaceFactory();
if (factory)
factory->CreateDecryptor(cdm_id, std::move(request));
}
void MediaInterfaceProxy::CreateCdmProxy(
const std::string& cdm_guid,
media::mojom::CdmProxyRequest request) {
......
......@@ -51,6 +51,8 @@ class MediaInterfaceProxy : public media::mojom::InterfaceFactory {
media::mojom::RendererRequest request) final;
void CreateCdm(const std::string& key_system,
media::mojom::ContentDecryptionModuleRequest request) final;
void CreateDecryptor(int cdm_id,
media::mojom::DecryptorRequest request) final;
void CreateCdmProxy(const std::string& cdm_guid,
media::mojom::CdmProxyRequest request) final;
......
......@@ -82,6 +82,20 @@ void MediaInterfaceFactory::CreateCdm(
GetMediaInterfaceFactory()->CreateCdm(key_system, std::move(request));
}
void MediaInterfaceFactory::CreateDecryptor(
int cdm_id,
media::mojom::DecryptorRequest request) {
if (!task_runner_->BelongsToCurrentThread()) {
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&MediaInterfaceFactory::CreateDecryptor,
weak_this_, cdm_id, std::move(request)));
return;
}
DVLOG(1) << __func__;
GetMediaInterfaceFactory()->CreateDecryptor(cdm_id, std::move(request));
}
void MediaInterfaceFactory::CreateCdmProxy(
const std::string& cdm_guid,
media::mojom::CdmProxyRequest request) {
......
......@@ -37,6 +37,8 @@ class CONTENT_EXPORT MediaInterfaceFactory
media::mojom::RendererRequest request) final;
void CreateCdm(const std::string& key_system,
media::mojom::ContentDecryptionModuleRequest request) final;
void CreateDecryptor(int cdm_id,
media::mojom::DecryptorRequest request) final;
// TODO(xhwang): We should not expose this here.
void CreateCdmProxy(const std::string& cdm_guid,
media::mojom::CdmProxyRequest request) final;
......
......@@ -4,18 +4,20 @@
#include "media/cdm/library_cdm/clear_key_cdm/clear_key_cdm_proxy.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "media/base/content_decryption_module.h"
#include "media/cdm/library_cdm/clear_key_cdm/cdm_proxy_common.h"
namespace media {
ClearKeyCdmProxy::ClearKeyCdmProxy() {}
ClearKeyCdmProxy::ClearKeyCdmProxy() : weak_factory_(this) {}
ClearKeyCdmProxy::~ClearKeyCdmProxy() {}
// TODO(xhwang): Returns a non-null pointer and add a test covering this path.
base::WeakPtr<CdmContext> ClearKeyCdmProxy::GetCdmContext() {
return nullptr;
DVLOG(1) << __func__;
return weak_factory_.GetWeakPtr();
}
void ClearKeyCdmProxy::Initialize(Client* client, InitializeCB init_cb) {
......@@ -31,7 +33,7 @@ void ClearKeyCdmProxy::Process(Function function,
const std::vector<uint8_t>& input_data,
uint32_t expected_output_data_size,
ProcessCB process_cb) {
DVLOG(1) << __func__;
DVLOG(2) << __func__;
if (crypto_session_id != kClearKeyCdmProxyCryptoSessionId ||
!std::equal(input_data.begin(), input_data.end(),
......@@ -50,7 +52,7 @@ void ClearKeyCdmProxy::Process(Function function,
void ClearKeyCdmProxy::CreateMediaCryptoSession(
const std::vector<uint8_t>& input_data,
CreateMediaCryptoSessionCB create_media_crypto_session_cb) {
DVLOG(1) << __func__;
DVLOG(2) << __func__;
if (!std::equal(input_data.begin(), input_data.end(),
kClearKeyCdmProxyInputData.begin(),
......@@ -70,4 +72,16 @@ void ClearKeyCdmProxy::SetKey(uint32_t crypto_session_id,
void ClearKeyCdmProxy::RemoveKey(uint32_t crypto_session_id,
const std::vector<uint8_t>& key_id) {}
Decryptor* ClearKeyCdmProxy::GetDecryptor() {
DVLOG(1) << __func__;
if (!aes_decryptor_) {
aes_decryptor_ = base::MakeRefCounted<AesDecryptor>(
base::DoNothing(), base::DoNothing(), base::DoNothing(),
base::DoNothing());
}
return aes_decryptor_.get();
}
} // namespace media
......@@ -7,12 +7,16 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "media/base/cdm_context.h"
#include "media/cdm/aes_decryptor.h"
#include "media/cdm/cdm_proxy.h"
namespace media {
// CdmProxy implementation for Clear Key CDM to test CDM Proxy support.
class ClearKeyCdmProxy : public CdmProxy {
class ClearKeyCdmProxy : public CdmProxy, public CdmContext {
public:
ClearKeyCdmProxy();
~ClearKeyCdmProxy() final;
......@@ -34,7 +38,14 @@ class ClearKeyCdmProxy : public CdmProxy {
void RemoveKey(uint32_t crypto_session_id,
const std::vector<uint8_t>& key_id) final;
// CdmContext implementation.
Decryptor* GetDecryptor() final;
private:
scoped_refptr<AesDecryptor> aes_decryptor_;
base::WeakPtrFactory<ClearKeyCdmProxy> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ClearKeyCdmProxy);
};
......
......@@ -306,6 +306,8 @@ component("gpu") {
sources += [
"windows/d3d11_cdm_proxy.cc",
"windows/d3d11_cdm_proxy.h",
"windows/d3d11_decryptor.cc",
"windows/d3d11_decryptor.h",
]
}
}
......
......@@ -12,6 +12,7 @@
#include "media/base/callback_registry.h"
#include "media/base/cdm_context.h"
#include "media/base/cdm_proxy_context.h"
#include "media/gpu/windows/d3d11_decryptor.h"
namespace media {
......@@ -112,9 +113,18 @@ class D3D11CdmContext : public CdmContext {
}
CdmProxyContext* GetCdmProxyContext() override { return &cdm_proxy_context_; }
Decryptor* GetDecryptor() override {
if (!decryptor_)
decryptor_.reset(new D3D11Decryptor(&cdm_proxy_context_));
return decryptor_.get();
}
private:
D3D11CdmProxyContext cdm_proxy_context_;
std::unique_ptr<D3D11Decryptor> decryptor_;
// TODO(rkuroiwa): Call Notify() when new usable key is available.
ClosureRegistry new_key_callbacks_;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/gpu/windows/d3d11_decryptor.h"
#include "base/logging.h"
#include "media/base/decoder_buffer.h"
namespace media {
D3D11Decryptor::D3D11Decryptor(CdmProxyContext* cdm_proxy_context)
: cdm_proxy_context_(cdm_proxy_context), weak_factory_(this) {
DCHECK(cdm_proxy_context_);
}
D3D11Decryptor::~D3D11Decryptor() {}
void D3D11Decryptor::RegisterNewKeyCB(StreamType stream_type,
const NewKeyCB& new_key_cb) {
// TODO(xhwang): Use RegisterNewKeyCB() on CdmContext, and remove
// RegisterNewKeyCB from Decryptor interface.
NOTREACHED();
}
void D3D11Decryptor::Decrypt(StreamType stream_type,
scoped_refptr<DecoderBuffer> encrypted,
const DecryptCB& decrypt_cb) {
// TODO(rkuroiwa): Implemented this function using |cdm_proxy_context_|.
NOTIMPLEMENTED();
}
void D3D11Decryptor::CancelDecrypt(StreamType stream_type) {
// Decrypt() calls the DecryptCB synchronously so there's nothing to cancel.
}
void D3D11Decryptor::InitializeAudioDecoder(const AudioDecoderConfig& config,
const DecoderInitCB& init_cb) {
// D3D11Decryptor does not support audio decoding.
init_cb.Run(false);
}
void D3D11Decryptor::InitializeVideoDecoder(const VideoDecoderConfig& config,
const DecoderInitCB& init_cb) {
// D3D11Decryptor does not support video decoding.
init_cb.Run(false);
}
void D3D11Decryptor::DecryptAndDecodeAudio(
scoped_refptr<DecoderBuffer> encrypted,
const AudioDecodeCB& audio_decode_cb) {
NOTREACHED() << "D3D11Decryptor does not support audio decoding";
}
void D3D11Decryptor::DecryptAndDecodeVideo(
scoped_refptr<DecoderBuffer> encrypted,
const VideoDecodeCB& video_decode_cb) {
NOTREACHED() << "D3D11Decryptor does not support video decoding";
}
void D3D11Decryptor::ResetDecoder(StreamType stream_type) {
NOTREACHED() << "D3D11Decryptor does not support audio/video decoding";
}
void D3D11Decryptor::DeinitializeDecoder(StreamType stream_type) {
// D3D11Decryptor does not support audio/video decoding, but since this can be
// called any time after InitializeAudioDecoder/InitializeVideoDecoder,
// nothing to be done here.
}
} // namespace media
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_GPU_WINDOWS_D3D11_DECRYPTOR_H_
#define MEDIA_GPU_WINDOWS_D3D11_DECRYPTOR_H_
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "media/base/decryptor.h"
#include "media/gpu/media_gpu_export.h"
namespace media {
class CdmProxyContext;
class MEDIA_GPU_EXPORT D3D11Decryptor : public Decryptor {
public:
explicit D3D11Decryptor(CdmProxyContext* cdm_proxy_context);
~D3D11Decryptor() final;
// Decryptor implementation.
void RegisterNewKeyCB(StreamType stream_type,
const NewKeyCB& key_added_cb) final;
void Decrypt(StreamType stream_type,
scoped_refptr<DecoderBuffer> encrypted,
const DecryptCB& decrypt_cb) final;
void CancelDecrypt(StreamType stream_type) final;
void InitializeAudioDecoder(const AudioDecoderConfig& config,
const DecoderInitCB& init_cb) final;
void InitializeVideoDecoder(const VideoDecoderConfig& config,
const DecoderInitCB& init_cb) final;
void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
const AudioDecodeCB& audio_decode_cb) final;
void DecryptAndDecodeVideo(scoped_refptr<DecoderBuffer> encrypted,
const VideoDecodeCB& video_decode_cb) final;
void ResetDecoder(StreamType stream_type) final;
void DeinitializeDecoder(StreamType stream_type) final;
private:
CdmProxyContext* cdm_proxy_context_;
base::WeakPtrFactory<D3D11Decryptor> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(D3D11Decryptor);
};
} // namespace media
#endif // MEDIA_GPU_WINDOWS_D3D11_DECRYPTOR_H_
......@@ -6,6 +6,7 @@ module media.mojom;
import "media/mojo/interfaces/audio_decoder.mojom";
import "media/mojo/interfaces/cdm_proxy.mojom";
import "media/mojo/interfaces/decryptor.mojom";
import "media/mojo/interfaces/content_decryption_module.mojom";
import "media/mojo/interfaces/renderer.mojom";
import "media/mojo/interfaces/video_decoder.mojom";
......@@ -60,6 +61,9 @@ interface InterfaceFactory {
// implementation must fully validate |key_system| before creating the CDM.
CreateCdm(string key_system, ContentDecryptionModule& cdm);
// Creates a Decryptor associated with the |cdm_id|.
CreateDecryptor(int32 cdm_id, Decryptor& decryptor);
// Creates a CdmProxy that proxies part of CDM functionalities to a different
// entity, e.g. hardware CDM modules. The created |cdm_proxy| must match the
// type of the CDM, identified by |cdm_guid|.
......
......@@ -11,6 +11,7 @@
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "media/base/media_log.h"
#include "media/mojo/services/mojo_decryptor_service.h"
#include "media/mojo/services/mojo_media_client.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "services/service_manager/public/mojom/interface_provider.mojom.h"
......@@ -167,6 +168,20 @@ void InterfaceFactoryImpl::CreateCdm(
#endif // BUILDFLAG(ENABLE_MOJO_CDM)
}
void InterfaceFactoryImpl::CreateDecryptor(int cdm_id,
mojom::DecryptorRequest request) {
DVLOG(2) << __func__;
auto mojo_decryptor_service =
MojoDecryptorService::Create(cdm_id, &cdm_service_context_);
if (!mojo_decryptor_service) {
DLOG(ERROR) << "MojoDecryptorService creation failed.";
return;
}
decryptor_bindings_.AddBinding(std::move(mojo_decryptor_service),
std::move(request));
}
void InterfaceFactoryImpl::CreateCdmProxy(const std::string& cdm_guid,
mojom::CdmProxyRequest request) {
DVLOG(2) << __func__;
......@@ -223,6 +238,9 @@ bool InterfaceFactoryImpl::IsEmpty() {
return false;
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
if (!decryptor_bindings_.empty())
return false;
return true;
}
......@@ -252,6 +270,8 @@ void InterfaceFactoryImpl::SetBindingConnectionErrorHandler() {
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
cdm_proxy_bindings_.set_connection_error_handler(connection_error_cb);
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
decryptor_bindings_.set_connection_error_handler(connection_error_cb);
}
void InterfaceFactoryImpl::OnBindingConnectionError() {
......
......@@ -40,6 +40,7 @@ class InterfaceFactoryImpl : public DeferredDestroy<mojom::InterfaceFactory> {
mojom::RendererRequest request) final;
void CreateCdm(const std::string& key_system,
mojom::ContentDecryptionModuleRequest request) final;
void CreateDecryptor(int cdm_id, mojom::DecryptorRequest request) final;
void CreateCdmProxy(const std::string& cdm_guid,
mojom::CdmProxyRequest request) final;
......@@ -91,6 +92,8 @@ class InterfaceFactoryImpl : public DeferredDestroy<mojom::InterfaceFactory> {
mojo::StrongBindingSet<mojom::CdmProxy> cdm_proxy_bindings_;
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
mojo::StrongBindingSet<mojom::Decryptor> decryptor_bindings_;
std::unique_ptr<service_manager::ServiceContextRef> connection_ref_;
MojoMediaClient* mojo_media_client_;
base::OnceClosure destroy_cb_;
......
......@@ -38,13 +38,14 @@ class CdmProxyContextRef : public CdmContextRef, public CdmContext {
private:
// CdmContext implementation.
CdmProxyContext* GetCdmProxyContext() final {
Decryptor* GetDecryptor() final {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return cdm_context_ ? cdm_context_->GetDecryptor() : nullptr;
}
if (!cdm_context_)
return nullptr;
return cdm_context_->GetCdmProxyContext();
CdmProxyContext* GetCdmProxyContext() final {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return cdm_context_ ? cdm_context_->GetCdmProxyContext() : nullptr;
}
base::WeakPtr<CdmContext> cdm_context_;
......
......@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/numerics/safe_conversions.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/cdm_context.h"
#include "media/base/decoder_buffer.h"
#include "media/base/decryptor.h"
#include "media/base/video_decoder_config.h"
......@@ -17,6 +18,7 @@
#include "media/mojo/common/mojo_decoder_buffer_converter.h"
#include "media/mojo/common/mojo_shared_buffer_video_frame.h"
#include "media/mojo/interfaces/demuxer_stream.mojom.h"
#include "media/mojo/services/mojo_cdm_service_context.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
namespace media {
......@@ -45,6 +47,29 @@ class FrameResourceReleaserImpl final : public mojom::FrameResourceReleaser {
} // namespace
// static
std::unique_ptr<MojoDecryptorService> MojoDecryptorService::Create(
int cdm_id,
MojoCdmServiceContext* mojo_cdm_service_context) {
auto cdm_context_ref = mojo_cdm_service_context->GetCdmContextRef(cdm_id);
if (!cdm_context_ref) {
DVLOG(1) << "CdmContextRef not found for CDM ID: " << cdm_id;
return nullptr;
}
auto* cdm_context = cdm_context_ref->GetCdmContext();
DCHECK(cdm_context);
auto* decryptor = cdm_context->GetDecryptor();
if (!decryptor) {
DVLOG(1) << "CdmContext does not support Decryptor";
return nullptr;
}
return std::make_unique<MojoDecryptorService>(decryptor,
std::move(cdm_context_ref));
}
MojoDecryptorService::MojoDecryptorService(
media::Decryptor* decryptor,
std::unique_ptr<CdmContextRef> cdm_context_ref)
......
......@@ -21,6 +21,7 @@
namespace media {
class DecoderBuffer;
class MojoCdmServiceContext;
class MojoDecoderBufferReader;
class MojoDecoderBufferWriter;
......@@ -31,6 +32,10 @@ class MEDIA_MOJO_EXPORT MojoDecryptorService : public mojom::Decryptor {
using StreamType = media::Decryptor::StreamType;
using Status = media::Decryptor::Status;
static std::unique_ptr<MojoDecryptorService> Create(
int cdm_id,
MojoCdmServiceContext* mojo_cdm_service_context);
// If |cdm_context_ref| is null, caller must ensure that |decryptor| outlives
// |this|. Otherwise, |decryptor| is guaranteed to be valid as long as
// |cdm_context_ref| is held.
......@@ -101,6 +106,9 @@ class MEDIA_MOJO_EXPORT MojoDecryptorService : public mojom::Decryptor {
std::unique_ptr<MojoDecoderBufferWriter> decrypted_buffer_writer_;
media::Decryptor* decryptor_;
// Holds the CdmContextRef to keep the CdmContext alive for the lifetime of
// the |decryptor_|.
std::unique_ptr<CdmContextRef> cdm_context_ref_;
base::WeakPtr<MojoDecryptorService> weak_this_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment