Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: media/mojo/services/mojo_cdm_service.cc

Issue 2857953006: Extract CdmManager from MojoCdmService. (Closed)
Patch Set: Rebase. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/mojo/services/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/mojo/services/mojo_cdm_service.h" 5 #include "media/mojo/services/mojo_cdm_service.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
14 #include "media/base/cdm_config.h" 14 #include "media/base/cdm_config.h"
15 #include "media/base/cdm_context.h" 15 #include "media/base/cdm_context.h"
16 #include "media/base/cdm_factory.h" 16 #include "media/base/cdm_factory.h"
17 #include "media/base/cdm_key_information.h" 17 #include "media/base/cdm_key_information.h"
18 #include "media/base/key_systems.h" 18 #include "media/base/key_systems.h"
19 #include "media/cdm/cdm_manager.h"
19 #include "media/mojo/common/media_type_converters.h" 20 #include "media/mojo/common/media_type_converters.h"
20 #include "media/mojo/services/mojo_cdm_service_context.h" 21 #include "media/mojo/services/mojo_cdm_service_context.h"
21 #include "url/gurl.h" 22 #include "url/gurl.h"
22 23
23 namespace media { 24 namespace media {
24 25
25 namespace {
26
27 // Manages all CDMs created by MojoCdmService. Can only have one instance per
28 // process, so we use a thread-safe static to achieve this.
29 class CdmManager {
30 public:
31 CdmManager() {}
32 ~CdmManager() {}
33
34 // Returns the CDM associated with |cdm_id|. Can be called on any thread.
35 scoped_refptr<ContentDecryptionModule> GetCdm(int cdm_id) {
36 base::AutoLock lock(lock_);
37 auto iter = cdm_map_.find(cdm_id);
38 return iter == cdm_map_.end() ? nullptr : iter->second;
39 }
40
41 // Registers the |cdm| for |cdm_id|.
42 void RegisterCdm(int cdm_id,
43 const scoped_refptr<ContentDecryptionModule>& cdm) {
44 base::AutoLock lock(lock_);
45 DCHECK(!cdm_map_.count(cdm_id));
46 cdm_map_[cdm_id] = cdm;
47 }
48
49 // Unregisters the CDM associated with |cdm_id|.
50 void UnregisterCdm(int cdm_id) {
51 base::AutoLock lock(lock_);
52 DCHECK(cdm_map_.count(cdm_id));
53 cdm_map_.erase(cdm_id);
54 }
55
56 private:
57 // Lock to protect |cdm_map_|.
58 base::Lock lock_;
59 std::map<int, scoped_refptr<ContentDecryptionModule>> cdm_map_;
60
61 DISALLOW_COPY_AND_ASSIGN(CdmManager);
62 };
63
64 CdmManager* GetManager() {
65 static CdmManager* manager = new CdmManager();
66 return manager;
67 }
68
69 } // namespace
70
71 using SimpleMojoCdmPromise = MojoCdmPromise<>; 26 using SimpleMojoCdmPromise = MojoCdmPromise<>;
72 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; 27 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>;
73 28
74 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; 29 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1;
75 30
76 // static
77 scoped_refptr<ContentDecryptionModule> MojoCdmService::LegacyGetCdm(
78 int cdm_id) {
79 DVLOG(1) << __func__ << ": " << cdm_id;
80 return GetManager()->GetCdm(cdm_id);
81 }
82
83 MojoCdmService::MojoCdmService(base::WeakPtr<MojoCdmServiceContext> context, 31 MojoCdmService::MojoCdmService(base::WeakPtr<MojoCdmServiceContext> context,
84 CdmFactory* cdm_factory) 32 CdmFactory* cdm_factory)
85 : context_(context), 33 : context_(context),
86 cdm_factory_(cdm_factory), 34 cdm_factory_(cdm_factory),
87 cdm_id_(CdmContext::kInvalidCdmId), 35 cdm_id_(CdmContext::kInvalidCdmId),
88 weak_factory_(this) { 36 weak_factory_(this) {
89 DCHECK(context_); 37 DCHECK(context_);
90 DCHECK(cdm_factory_); 38 DCHECK(cdm_factory_);
91 } 39 }
92 40
93 MojoCdmService::~MojoCdmService() { 41 MojoCdmService::~MojoCdmService() {
94 if (cdm_id_ == CdmContext::kInvalidCdmId) 42 if (cdm_id_ == CdmContext::kInvalidCdmId)
95 return; 43 return;
96 44
97 GetManager()->UnregisterCdm(cdm_id_); 45 CdmManager::GetInstance()->UnregisterCdm(cdm_id_);
98 46
99 if (context_) 47 if (context_)
100 context_->UnregisterCdm(cdm_id_); 48 context_->UnregisterCdm(cdm_id_);
101 } 49 }
102 50
103 void MojoCdmService::SetClient(mojom::ContentDecryptionModuleClientPtr client) { 51 void MojoCdmService::SetClient(mojom::ContentDecryptionModuleClientPtr client) {
104 client_ = std::move(client); 52 client_ = std::move(client);
105 } 53 }
106 54
107 void MojoCdmService::Initialize(const std::string& key_system, 55 void MojoCdmService::Initialize(const std::string& key_system,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 cdm_promise_result->system_code = 0; 137 cdm_promise_result->system_code = 0;
190 cdm_promise_result->error_message = error_message; 138 cdm_promise_result->error_message = error_message;
191 callback.Run(std::move(cdm_promise_result), 0, nullptr); 139 callback.Run(std::move(cdm_promise_result), 0, nullptr);
192 return; 140 return;
193 } 141 }
194 142
195 cdm_ = cdm; 143 cdm_ = cdm;
196 cdm_id_ = next_cdm_id_++; 144 cdm_id_ = next_cdm_id_++;
197 145
198 context_->RegisterCdm(cdm_id_, this); 146 context_->RegisterCdm(cdm_id_, this);
199 GetManager()->RegisterCdm(cdm_id_, cdm); 147 CdmManager::GetInstance()->RegisterCdm(cdm_id_, cdm);
200 148
201 // If |cdm| has a decryptor, create the MojoDecryptorService 149 // If |cdm| has a decryptor, create the MojoDecryptorService
202 // and pass the connection back to the client. 150 // and pass the connection back to the client.
203 mojom::DecryptorPtr decryptor_service; 151 mojom::DecryptorPtr decryptor_service;
204 CdmContext* const cdm_context = cdm_->GetCdmContext(); 152 CdmContext* const cdm_context = cdm_->GetCdmContext();
205 if (cdm_context && cdm_context->GetDecryptor()) { 153 if (cdm_context && cdm_context->GetDecryptor()) {
206 decryptor_.reset(new MojoDecryptorService( 154 decryptor_.reset(new MojoDecryptorService(
207 cdm_context->GetDecryptor(), MakeRequest(&decryptor_service), 155 cdm_context->GetDecryptor(), MakeRequest(&decryptor_service),
208 base::Bind(&MojoCdmService::OnDecryptorConnectionError, weak_this_))); 156 base::Bind(&MojoCdmService::OnDecryptorConnectionError, weak_this_)));
209 } 157 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 197
250 void MojoCdmService::OnDecryptorConnectionError() { 198 void MojoCdmService::OnDecryptorConnectionError() {
251 DVLOG(2) << __func__; 199 DVLOG(2) << __func__;
252 200
253 // MojoDecryptorService has lost connectivity to it's client, so it can be 201 // MojoDecryptorService has lost connectivity to it's client, so it can be
254 // freed. 202 // freed.
255 decryptor_.reset(); 203 decryptor_.reset();
256 } 204 }
257 205
258 } // namespace media 206 } // namespace media
OLDNEW
« no previous file with comments | « media/mojo/services/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698