1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/mojo/services/mojo_cdm_service_context.h"
7 #include "base/logging.h"
8 #include "media/base/callback_registry.h"
9 #include "media/base/cdm_context.h"
10 #include "media/base/content_decryption_module.h"
11 #include "media/cdm/cdm_context_ref_impl.h"
12 #include "media/mojo/services/mojo_cdm_service.h"
18 // Helper function to get the next unique (per-process) CDM ID to be assigned to
19 // a CDM. It will be used to locate the CDM by the media players living in the
21 base::UnguessableToken GetNextCdmId() {
22 return base::UnguessableToken::Create();
27 MojoCdmServiceContext::MojoCdmServiceContext() = default;
29 MojoCdmServiceContext::~MojoCdmServiceContext() = default;
31 base::UnguessableToken MojoCdmServiceContext::RegisterCdm(
32 MojoCdmService* cdm_service) {
34 base::UnguessableToken cdm_id = GetNextCdmId();
35 base::AutoLock guard(cdm_services_lock_);
36 cdm_services_[cdm_id] = cdm_service;
37 DVLOG(1) << __func__ << ": CdmService registered with CDM ID " << cdm_id;
41 void MojoCdmServiceContext::UnregisterCdm(
42 const base::UnguessableToken& cdm_id) {
43 DVLOG(1) << __func__ << ": cdm_id = " << cdm_id;
44 base::AutoLock guard(cdm_services_lock_);
45 DCHECK(cdm_services_.count(cdm_id));
46 cdm_services_.erase(cdm_id);
49 #if BUILDFLAG(IS_CHROMEOS_ASH)
50 base::UnguessableToken MojoCdmServiceContext::RegisterRemoteCdmContext(
51 chromeos::RemoteCdmContext* remote_context) {
52 DCHECK(remote_context);
53 base::UnguessableToken cdm_id = GetNextCdmId();
54 remote_cdm_contexts_[cdm_id] = remote_context;
55 DVLOG(1) << __func__ << ": RemoteCdmContext registered with CDM ID "
60 void MojoCdmServiceContext::UnregisterRemoteCdmContext(
61 const base::UnguessableToken& cdm_id) {
62 DVLOG(1) << __func__ << ": cdm_id = " << cdm_id;
63 DCHECK(remote_cdm_contexts_.count(cdm_id));
64 remote_cdm_contexts_.erase(cdm_id);
66 #endif // BUILDFLAG(IS_CHROMEOS_ASH)
68 std::unique_ptr<CdmContextRef> MojoCdmServiceContext::GetCdmContextRef(
69 const base::UnguessableToken& cdm_id) {
70 DVLOG(1) << __func__ << ": cdm_id = " << cdm_id;
72 // Check all CDMs first.
74 base::AutoLock guard(cdm_services_lock_);
75 auto cdm_service = cdm_services_.find(cdm_id);
76 if (cdm_service != cdm_services_.end()) {
77 if (!cdm_service->second->GetCdm()->GetCdmContext()) {
78 NOTREACHED() << "All CDMs should support CdmContext.";
81 return std::make_unique<CdmContextRefImpl>(cdm_service->second->GetCdm());
85 #if BUILDFLAG(IS_CHROMEOS_ASH)
86 // Try the remote contexts now.
87 auto remote_context = remote_cdm_contexts_.find(cdm_id);
88 if (remote_context != remote_cdm_contexts_.end())
89 return remote_context->second->GetCdmContextRef();
90 #endif // BUILDFLAG(IS_CHROMEOS_ASH)
92 LOG(ERROR) << "CdmContextRef cannot be obtained for CDM ID: " << cdm_id;