[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / mojo_cdm_service_context.cc
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.
4
5 #include "media/mojo/services/mojo_cdm_service_context.h"
6
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"
13
14 namespace media {
15
16 namespace {
17
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
20 // same process.
21 base::UnguessableToken GetNextCdmId() {
22   return base::UnguessableToken::Create();
23 }
24
25 }  // namespace
26
27 MojoCdmServiceContext::MojoCdmServiceContext() = default;
28
29 MojoCdmServiceContext::~MojoCdmServiceContext() = default;
30
31 base::UnguessableToken MojoCdmServiceContext::RegisterCdm(
32     MojoCdmService* cdm_service) {
33   DCHECK(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;
38   return cdm_id;
39 }
40
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);
47 }
48
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 "
56            << cdm_id;
57   return cdm_id;
58 }
59
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);
65 }
66 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
67
68 std::unique_ptr<CdmContextRef> MojoCdmServiceContext::GetCdmContextRef(
69     const base::UnguessableToken& cdm_id) {
70   DVLOG(1) << __func__ << ": cdm_id = " << cdm_id;
71
72   // Check all CDMs first.
73   {
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.";
79         return nullptr;
80       }
81       return std::make_unique<CdmContextRefImpl>(cdm_service->second->GetCdm());
82     }
83   }
84
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)
91
92   LOG(ERROR) << "CdmContextRef cannot be obtained for CDM ID: " << cdm_id;
93   return nullptr;
94 }
95
96 }  // namespace media