Skip to content

Commit

Permalink
Extract CdmManager from MojoCdmService.
Browse files Browse the repository at this point in the history
This allows us to remove the conditional dependency from //media/gpu to
//media/mojo/services, thus allowing future dependencies in the other
direction.
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel

Review-Url: https://codereview.chromium.org/2857953006
Cr-Commit-Position: refs/heads/master@{#469544}
  • Loading branch information
sandersd authored and Commit bot committed May 5, 2017
1 parent 8d03afa commit 2c899c7
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 61 deletions.
25 changes: 25 additions & 0 deletions media/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,31 @@ component("media") {
}
}

config("cdm_manager_implementation") {
defines = [ "CDM_MANAGER_IMPLEMENTATION" ]
}

# cdm_manager must not be a source_set() because CdmManager exposes a static
# singleton, shared by multiple component()s.
#
# TODO(xhwang): Remove this component once AVDA no longer depends on it.
component("cdm_manager") {
visibility = [
"//media/gpu",
"//media/mojo/services:lib",
]
sources = [
"cdm/cdm_manager.cc",
"cdm/cdm_manager.h",
"cdm/cdm_manager_export.h",
]
configs += [ ":cdm_manager_implementation" ]
deps = [
":media",
"//base",
]
}

static_library("cdm_paths") {
sources = [
"cdm/cdm_paths.cc",
Expand Down
43 changes: 43 additions & 0 deletions media/cdm/cdm_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2017 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/cdm/cdm_manager.h"

#include <utility>

#include "base/logging.h"
#include "media/base/content_decryption_module.h"

namespace media {

CdmManager::CdmManager() {}

CdmManager::~CdmManager() {}

// static
CdmManager* CdmManager::GetInstance() {
static CdmManager* manager = new CdmManager();
return manager;
}

scoped_refptr<ContentDecryptionModule> CdmManager::GetCdm(int cdm_id) {
base::AutoLock lock(lock_);
auto iter = cdm_map_.find(cdm_id);
return iter == cdm_map_.end() ? nullptr : iter->second;
}

void CdmManager::RegisterCdm(int cdm_id,
scoped_refptr<ContentDecryptionModule> cdm) {
base::AutoLock lock(lock_);
DCHECK(!cdm_map_.count(cdm_id));
cdm_map_[cdm_id] = cdm;
}

void CdmManager::UnregisterCdm(int cdm_id) {
base::AutoLock lock(lock_);
DCHECK(cdm_map_.count(cdm_id));
cdm_map_.erase(cdm_id);
}

} // namespace media
50 changes: 50 additions & 0 deletions media/cdm/cdm_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2017 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_CDM_CDM_MANAGER_H_
#define MEDIA_CDM_CDM_MANAGER_H_

#include <map>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "media/base/media_export.h"
#include "media/cdm/cdm_manager_export.h"

namespace media {

class ContentDecryptionModule;

// Provides a singleton registry of CDM instances. This is used to share
// ContentDecryptionModules between the MojoMediaService and
// AndroidVideoDecodeAccelerator, and should be removed along with AVDA in the
// future. (MojoCdmServiceContext serves the same purpose for Media Mojo
// services, but scoped to a single InterfaceFactory.)
class CDM_MANAGER_EXPORT CdmManager {
public:
CdmManager();
~CdmManager();

static CdmManager* GetInstance();

// Returns the CDM associated with |cdm_id|. Can be called on any thread.
scoped_refptr<ContentDecryptionModule> GetCdm(int cdm_id);

// Registers the |cdm| for |cdm_id|.
void RegisterCdm(int cdm_id, scoped_refptr<ContentDecryptionModule> cdm);

// Unregisters the CDM associated with |cdm_id|.
void UnregisterCdm(int cdm_id);

private:
base::Lock lock_;
std::map<int, scoped_refptr<ContentDecryptionModule>> cdm_map_;

DISALLOW_COPY_AND_ASSIGN(CdmManager);
};

} // namespace media

#endif // MEDIA_CDM_CDM_MANAGER_H_
32 changes: 32 additions & 0 deletions media/cdm/cdm_manager_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017 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_CDM_CDM_MANAGER_EXPORT_H_
#define MEDIA_CDM_CDM_MANAGER_EXPORT_H_

// Define CDM_MANAGER_EXPORT so that functionality implemented by the
// cdm_manager component can be exported to consumers.

#if defined(COMPONENT_BUILD)
#if defined(WIN32)

#if defined(CDM_MANAGER_IMPLEMENTATION)
#define CDM_MANAGER_EXPORT __declspec(dllexport)
#else
#define CDM_MANAGER_EXPORT __declspec(dllimport)
#endif // defined(CDM_MANAGER_IMPLEMENTATION)

#else // defined(WIN32)
#if defined(CDM_MANAGER_IMPLEMENTATION)
#define CDM_MANAGER_EXPORT __attribute__((visibility("default")))
#else
#define CDM_MANAGER_EXPORT
#endif
#endif

#else // defined(COMPONENT_BUILD)
#define CDM_MANAGER_EXPORT
#endif

#endif // MEDIA_BASE_CDM_MANAGER_EXPORT_H_
2 changes: 1 addition & 1 deletion media/gpu/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ component("gpu") {
# Remove this dependency after VDAs are also running as part of the mojo
# media service. See http://crbug.com/522298
if (mojo_media_host == "gpu") {
deps += [ "//media/mojo/services" ]
deps += [ "//media:cdm_manager" ]
}
}

Expand Down
7 changes: 2 additions & 5 deletions media/gpu/android_video_decode_accelerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "media/base/media.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_decoder_config.h"
#include "media/cdm/cdm_manager.h"
#include "media/gpu/android_video_surface_chooser_impl.h"
#include "media/gpu/avda_picture_buffer_manager.h"
#include "media/gpu/content_video_view_overlay.h"
Expand All @@ -44,10 +45,6 @@
#include "ui/gl/android/surface_texture.h"
#include "ui/gl/gl_bindings.h"

#if defined(ENABLE_MOJO_MEDIA_IN_GPU_PROCESS)
#include "media/mojo/services/mojo_cdm_service.h" // nogncheck
#endif

#define NOTIFY_ERROR(error_code, error_message) \
do { \
DLOG(ERROR) << error_message; \
Expand Down Expand Up @@ -1413,7 +1410,7 @@ void AndroidVideoDecodeAccelerator::InitializeCdm() {
#else
// Store the CDM to hold a reference to it.
cdm_for_reference_holding_only_ =
MojoCdmService::LegacyGetCdm(config_.cdm_id);
CdmManager::GetInstance()->GetCdm(config_.cdm_id);
DCHECK(cdm_for_reference_holding_only_);

// On Android platform the CdmContext must be a MediaDrmBridgeCdmContext.
Expand Down
1 change: 1 addition & 0 deletions media/mojo/services/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ source_set("lib") {

deps = [
"//media:cdm_api",
"//media:cdm_manager",
"//media:shared_memory_support",
"//media/mojo/common",
"//mojo/common",
Expand Down
58 changes: 3 additions & 55 deletions media/mojo/services/mojo_cdm_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,18 @@
#include "media/base/cdm_factory.h"
#include "media/base/cdm_key_information.h"
#include "media/base/key_systems.h"
#include "media/cdm/cdm_manager.h"
#include "media/mojo/common/media_type_converters.h"
#include "media/mojo/services/mojo_cdm_service_context.h"
#include "url/gurl.h"

namespace media {

namespace {

// Manages all CDMs created by MojoCdmService. Can only have one instance per
// process, so we use a thread-safe static to achieve this.
class CdmManager {
public:
CdmManager() {}
~CdmManager() {}

// Returns the CDM associated with |cdm_id|. Can be called on any thread.
scoped_refptr<ContentDecryptionModule> GetCdm(int cdm_id) {
base::AutoLock lock(lock_);
auto iter = cdm_map_.find(cdm_id);
return iter == cdm_map_.end() ? nullptr : iter->second;
}

// Registers the |cdm| for |cdm_id|.
void RegisterCdm(int cdm_id,
const scoped_refptr<ContentDecryptionModule>& cdm) {
base::AutoLock lock(lock_);
DCHECK(!cdm_map_.count(cdm_id));
cdm_map_[cdm_id] = cdm;
}

// Unregisters the CDM associated with |cdm_id|.
void UnregisterCdm(int cdm_id) {
base::AutoLock lock(lock_);
DCHECK(cdm_map_.count(cdm_id));
cdm_map_.erase(cdm_id);
}

private:
// Lock to protect |cdm_map_|.
base::Lock lock_;
std::map<int, scoped_refptr<ContentDecryptionModule>> cdm_map_;

DISALLOW_COPY_AND_ASSIGN(CdmManager);
};

CdmManager* GetManager() {
static CdmManager* manager = new CdmManager();
return manager;
}

} // namespace

using SimpleMojoCdmPromise = MojoCdmPromise<>;
using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>;

int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1;

// static
scoped_refptr<ContentDecryptionModule> MojoCdmService::LegacyGetCdm(
int cdm_id) {
DVLOG(1) << __func__ << ": " << cdm_id;
return GetManager()->GetCdm(cdm_id);
}

MojoCdmService::MojoCdmService(base::WeakPtr<MojoCdmServiceContext> context,
CdmFactory* cdm_factory)
: context_(context),
Expand All @@ -94,7 +42,7 @@ MojoCdmService::~MojoCdmService() {
if (cdm_id_ == CdmContext::kInvalidCdmId)
return;

GetManager()->UnregisterCdm(cdm_id_);
CdmManager::GetInstance()->UnregisterCdm(cdm_id_);

if (context_)
context_->UnregisterCdm(cdm_id_);
Expand Down Expand Up @@ -196,7 +144,7 @@ void MojoCdmService::OnCdmCreated(
cdm_id_ = next_cdm_id_++;

context_->RegisterCdm(cdm_id_, this);
GetManager()->RegisterCdm(cdm_id_, cdm);
CdmManager::GetInstance()->RegisterCdm(cdm_id_, cdm);

// If |cdm| has a decryptor, create the MojoDecryptorService
// and pass the connection back to the client.
Expand Down

0 comments on commit 2c899c7

Please sign in to comment.