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_;
......
......@@ -17,6 +17,7 @@
#include "media/base/mock_filters.h"
#include "media/base/test_helpers.h"
#include "media/mojo/buildflags.h"
#include "media/mojo/clients/mojo_decryptor.h"
#include "media/mojo/clients/mojo_demuxer_stream_impl.h"
#include "media/mojo/common/media_type_converters.h"
#include "media/mojo/interfaces/constants.mojom.h"
......@@ -34,8 +35,8 @@
#include "url/origin.h"
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
#include "media/cdm/cdm_paths.h" // nogncheck
#include "media/mojo/interfaces/cdm_proxy.mojom.h" // nogncheck
#include "media/cdm/cdm_paths.h" // nogncheck
#include "media/mojo/interfaces/cdm_proxy.mojom.h"
#endif
namespace media {
......@@ -43,11 +44,13 @@ namespace media {
namespace {
using testing::_;
using testing::DoAll;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::NiceMock;
using testing::SaveArg;
using testing::StrictMock;
using testing::WithoutArgs;
using testing::WithArg;
MATCHER_P(MatchesResult, success, "") {
return arg->success == success;
......@@ -60,6 +63,14 @@ const char kInvalidKeySystem[] = "invalid.key.system";
const char kSecurityOrigin[] = "https://foo.com";
// Returns a trivial encrypted DecoderBuffer.
scoped_refptr<DecoderBuffer> CreateEncryptedBuffer() {
scoped_refptr<DecoderBuffer> encrypted_buffer(new DecoderBuffer(100));
encrypted_buffer->set_decrypt_config(
DecryptConfig::CreateCencConfig("dummy_key_id", "0123456789ABCDEF", {}));
return encrypted_buffer;
}
class MockCdmProxyClient : public mojom::CdmProxyClient {
public:
MockCdmProxyClient() = default;
......@@ -119,14 +130,16 @@ class MediaServiceTest : public service_manager::test::ServiceTest {
void SetUp() override {
ServiceTest::SetUp();
media::mojom::MediaServicePtr media_service;
connector()->BindInterface(media::mojom::kMediaServiceName, &media_service);
service_manager::mojom::InterfaceProviderPtr interfaces;
service_manager::mojom::InterfaceProviderPtr host_interfaces;
auto provider = std::make_unique<MediaInterfaceProvider>(
mojo::MakeRequest(&interfaces));
media_service->CreateInterfaceFactory(
mojo::MakeRequest(&interface_factory_), std::move(interfaces));
mojo::MakeRequest(&host_interfaces));
connector()->BindInterface(mojom::kMediaServiceName, &media_service_);
media_service_.set_connection_error_handler(
base::BindRepeating(&MediaServiceTest::MediaServiceConnectionClosed,
base::Unretained(this)));
media_service_->CreateInterfaceFactory(
mojo::MakeRequest(&interface_factory_), std::move(host_interfaces));
}
MOCK_METHOD3(OnCdmInitialized,
......@@ -135,23 +148,26 @@ class MediaServiceTest : public service_manager::test::ServiceTest {
mojom::DecryptorPtr decryptor));
MOCK_METHOD0(OnCdmConnectionError, void());
void InitializeCdm(const std::string& key_system,
bool expected_result,
int cdm_id) {
// Returns the CDM ID associated with the CDM.
int InitializeCdm(const std::string& key_system, bool expected_result) {
base::RunLoop run_loop;
interface_factory_->CreateCdm(key_system, mojo::MakeRequest(&cdm_));
cdm_.set_connection_error_handler(base::BindRepeating(
&MediaServiceTest::OnCdmConnectionError, base::Unretained(this)));
// Have to use WithoutArgs since move-only types do not work with actions.
EXPECT_CALL(*this,
OnCdmInitialized(MatchesResult(expected_result), cdm_id, _))
.WillOnce(WithoutArgs(QuitLoop(&run_loop)));
int cdm_id = CdmContext::kInvalidCdmId;
// The last parameter mojom::DecryptorPtr is move-only and not supported by
// DoAll. Hence use WithArg to only extract the "int cdm_id" out and then
// call DoAll.
EXPECT_CALL(*this, OnCdmInitialized(MatchesResult(expected_result), _, _))
.WillOnce(WithArg<1>(DoAll(SaveArg<0>(&cdm_id), QuitLoop(&run_loop))));
cdm_->Initialize(key_system, url::Origin::Create(GURL(kSecurityOrigin)),
CdmConfig(),
base::BindOnce(&MediaServiceTest::OnCdmInitialized,
base::Unretained(this)));
run_loop.Run();
return cdm_id;
}
MOCK_METHOD4(OnCdmProxyInitialized,
......@@ -160,20 +176,47 @@ class MediaServiceTest : public service_manager::test::ServiceTest {
uint32_t crypto_session_id,
int cdm_id));
void InitializeCdmProxy(const std::string& cdm_guid) {
// Returns the CDM ID associated with the CdmProxy.
int InitializeCdmProxy(const std::string& cdm_guid) {
base::RunLoop run_loop;
interface_factory_->CreateCdmProxy(cdm_guid,
mojo::MakeRequest(&cdm_proxy_));
EXPECT_CALL(*this, OnCdmProxyInitialized(CdmProxy::Status::kOk, _, _, _))
.WillOnce(QuitLoop(&run_loop));
mojom::CdmProxyClientAssociatedPtrInfo client_ptr_info;
cdm_proxy_client_binding_.Bind(mojo::MakeRequest(&client_ptr_info));
int cdm_id = CdmContext::kInvalidCdmId;
EXPECT_CALL(*this, OnCdmProxyInitialized(CdmProxy::Status::kOk, _, _, _))
.WillOnce(DoAll(SaveArg<3>(&cdm_id), QuitLoop(&run_loop)));
cdm_proxy_->Initialize(
std::move(client_ptr_info),
base::BindOnce(&MediaServiceTest::OnCdmProxyInitialized,
base::Unretained(this)));
run_loop.Run();
return cdm_id;
}
MOCK_METHOD2(OnDecrypted,
void(Decryptor::Status, scoped_refptr<DecoderBuffer>));
void CreateDecryptor(int cdm_id, bool expected_result) {
base::RunLoop run_loop;
mojom::DecryptorPtr decryptor_ptr;
interface_factory_->CreateDecryptor(cdm_id,
mojo::MakeRequest(&decryptor_ptr));
MojoDecryptor mojo_decryptor(std::move(decryptor_ptr));
// In the success case, there's no decryption key to decrypt the buffer so
// we would expect no-key.
auto expected_status =
expected_result ? Decryptor::kNoKey : Decryptor::kError;
EXPECT_CALL(*this, OnDecrypted(expected_status, _))
.WillOnce(QuitLoop(&run_loop));
mojo_decryptor.Decrypt(Decryptor::kVideo, CreateEncryptedBuffer(),
base::BindRepeating(&MediaServiceTest::OnDecrypted,
base::Unretained(this)));
run_loop.Run();
}
MOCK_METHOD1(OnRendererInitialized, void(bool));
......@@ -194,10 +237,11 @@ class MediaServiceTest : public service_manager::test::ServiceTest {
mojom::RendererClientAssociatedPtrInfo client_ptr_info;
renderer_client_binding_.Bind(mojo::MakeRequest(&client_ptr_info));
EXPECT_CALL(*this, OnRendererInitialized(expected_result))
.WillOnce(QuitLoop(&run_loop));
std::vector<mojom::DemuxerStreamPtrInfo> streams;
streams.push_back(std::move(video_stream_proxy_info));
EXPECT_CALL(*this, OnRendererInitialized(expected_result))
.WillOnce(QuitLoop(&run_loop));
renderer_->Initialize(
std::move(client_ptr_info), std::move(streams), base::nullopt,
base::nullopt,
......@@ -209,6 +253,7 @@ class MediaServiceTest : public service_manager::test::ServiceTest {
MOCK_METHOD0(MediaServiceConnectionClosed, void());
protected:
mojom::MediaServicePtr media_service_;
mojom::InterfaceFactoryPtr interface_factory_;
mojom::ContentDecryptionModulePtr cdm_;
mojom::CdmProxyPtr cdm_proxy_;
......@@ -232,18 +277,24 @@ class MediaServiceTest : public service_manager::test::ServiceTest {
// Note: base::RunLoop::RunUntilIdle() does not work well in these tests because
// even when the loop is idle, we may still have pending events in the pipe.
// - If you have an InterfacePtr hosted by the service in the service process,
// you can use InterfacePtr::FlushForTesting().
// you can use InterfacePtr::FlushForTesting(). Note that this doesn't drain
// the task runner in the test process and doesn't cover all negative cases.
// - If you expect a callback on an InterfacePtr call or connection error, use
// base::RunLoop::Run() and QuitLoop().
// TODO(crbug.com/829233): Enable these tests on Android.
#if BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)
TEST_F(MediaServiceTest, InitializeCdm_Success) {
InitializeCdm(kClearKeyKeySystem, true, 1);
InitializeCdm(kClearKeyKeySystem, true);
}
TEST_F(MediaServiceTest, InitializeCdm_InvalidKeySystem) {
InitializeCdm(kInvalidKeySystem, false, 0);
InitializeCdm(kInvalidKeySystem, false);
}
TEST_F(MediaServiceTest, Decryptor_WithCdm) {
int cdm_id = InitializeCdm(kClearKeyKeySystem, true);
CreateDecryptor(cdm_id, true);
}
#endif // BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)
......@@ -257,19 +308,66 @@ TEST_F(MediaServiceTest, InitializeRenderer) {
TEST_F(MediaServiceTest, CdmProxy) {
InitializeCdmProxy(kClearKeyCdmGuid);
}
TEST_F(MediaServiceTest, Decryptor_WithCdmProxy) {
int cdm_id = InitializeCdmProxy(kClearKeyCdmGuid);
CreateDecryptor(cdm_id, true);
}
TEST_F(MediaServiceTest, Decryptor_WrongCdmId) {
int cdm_id = InitializeCdmProxy(kClearKeyCdmGuid);
CreateDecryptor(cdm_id + 1, false);
}
TEST_F(MediaServiceTest, DeferredDestruction_CdmProxy) {
InitializeCdmProxy(kClearKeyCdmGuid);
// Disconnecting InterfaceFactory should not terminate the MediaService since
// there is still a CdmProxy hosted.
interface_factory_.reset();
cdm_proxy_.FlushForTesting();
// Disconnecting CdmProxy will now terminate the MediaService.
base::RunLoop run_loop;
EXPECT_CALL(*this, MediaServiceConnectionClosed())
.WillOnce(QuitLoop(&run_loop));
cdm_proxy_.reset();
run_loop.Run();
}
#endif // BUILDFLAG(ENABLE_LIBRARY_CDMS)
TEST_F(MediaServiceTest, Lifetime) {
// The lifetime of the media service is controlled by the number of
// live InterfaceFactory impls, which are then deferred destroyed until no
// media components (CDM or Renderer) are hosted.
media::mojom::MediaServicePtr media_service;
connector()->BindInterface(media::mojom::kMediaServiceName, &media_service);
media_service.set_connection_error_handler(base::BindRepeating(
&MediaServiceTest::MediaServiceConnectionClosed, base::Unretained(this)));
TEST_F(MediaServiceTest, Decryptor_WithoutCdmOrCdmProxy) {
// Creating decryptor without creating CDM or CdmProxy.
CreateDecryptor(1, false);
}
TEST_F(MediaServiceTest, Lifetime_DestroyMediaService) {
// Disconnecting |media_service_| doesn't terminate MediaService
// since |interface_factory_| is still alive. This is ensured here since
// MediaServiceConnectionClosed() is not called.
EXPECT_CALL(*this, MediaServiceConnectionClosed()).Times(0);
media_service_.reset();
interface_factory_.FlushForTesting();
}
TEST_F(MediaServiceTest, Lifetime_DestroyInterfaceFactory) {
// Disconnecting InterfaceFactory will now terminate the MediaService since
// there's no media components hosted.
base::RunLoop run_loop;
EXPECT_CALL(*this, MediaServiceConnectionClosed())
.WillOnce(QuitLoop(&run_loop));
interface_factory_.reset();
run_loop.Run();
}
#if (BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)) || \
BUILDFLAG(ENABLE_MOJO_RENDERER)
// MediaService stays alive as long as there are InterfaceFactory impls, which
// are then deferred destroyed until no media components (e.g. CDM or Renderer)
// are hosted.
TEST_F(MediaServiceTest, Lifetime) {
#if BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)
InitializeCdm(kClearKeyKeySystem, true, 1);
InitializeCdm(kClearKeyKeySystem, true);
#endif
#if BUILDFLAG(ENABLE_MOJO_RENDERER)
......@@ -290,19 +388,9 @@ TEST_F(MediaServiceTest, Lifetime) {
run_loop.Run();
}
#if (BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)) || \
BUILDFLAG(ENABLE_MOJO_RENDERER)
TEST_F(MediaServiceTest, DeferredDestruction) {
// The lifetime of the media service is controlled by the number of
// live InterfaceFactory impls, which are then deferred destroyed until no
// media components (CDM or Renderer) are hosted.
media::mojom::MediaServicePtr media_service;
connector()->BindInterface(media::mojom::kMediaServiceName, &media_service);
media_service.set_connection_error_handler(base::BindRepeating(
&MediaServiceTest::MediaServiceConnectionClosed, base::Unretained(this)));
#if BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)
InitializeCdm(kClearKeyKeySystem, true, 1);
InitializeCdm(kClearKeyKeySystem, true);
#endif
#if BUILDFLAG(ENABLE_MOJO_RENDERER)
......@@ -323,10 +411,10 @@ TEST_F(MediaServiceTest, DeferredDestruction) {
// Disconnecting CDM and Renderer will now terminate the MediaService.
base::RunLoop run_loop;
cdm_.reset();
renderer_.reset();
EXPECT_CALL(*this, MediaServiceConnectionClosed())
.WillOnce(QuitLoop(&run_loop));
cdm_.reset();
renderer_.reset();
run_loop.Run();
}
#endif // (BUILDFLAG(ENABLE_MOJO_CDM) && !defined(OS_ANDROID)) ||
......
......@@ -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