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

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

Issue 1448303002: media: Add static MojoCdmService::GetCdm(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add CdmManager Created 5 years, 1 month 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
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>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h"
11 #include "base/synchronization/lock.h"
8 #include "media/base/cdm_config.h" 12 #include "media/base/cdm_config.h"
9 #include "media/base/cdm_context.h" 13 #include "media/base/cdm_context.h"
10 #include "media/base/cdm_factory.h" 14 #include "media/base/cdm_factory.h"
11 #include "media/base/cdm_key_information.h" 15 #include "media/base/cdm_key_information.h"
12 #include "media/base/key_systems.h" 16 #include "media/base/key_systems.h"
13 #include "media/mojo/services/media_type_converters.h" 17 #include "media/mojo/services/media_type_converters.h"
14 #include "media/mojo/services/mojo_cdm_service_context.h" 18 #include "media/mojo/services/mojo_cdm_service_context.h"
15 #include "mojo/common/common_type_converters.h" 19 #include "mojo/common/common_type_converters.h"
16 #include "mojo/common/url_type_converters.h" 20 #include "mojo/common/url_type_converters.h"
17 #include "url/gurl.h" 21 #include "url/gurl.h"
18 22
19 namespace media { 23 namespace media {
20 24
25 namespace {
26
27 // Manages all CDMs created by MojoCdmService. Can only have one instance per
28 // process so use a LazyInstance to ensure 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<MediaKeys> GetCdm(int cdm_id) {
36 base::AutoLock lock(cdm_map_lock_);
37 return cdm_map_.count(cdm_id) ? cdm_map_[cdm_id] : nullptr;
DaleCurtis 2015/11/18 01:00:21 General question: Why count instead of find? For a
xhwang 2015/11/18 18:45:39 Done.
38 }
39
40 // Registers the |cdm| for |cdm_id|.
41 void RegisterCdm(int cdm_id, const scoped_refptr<MediaKeys>& cdm) {
42 base::AutoLock lock(cdm_map_lock_);
43 DCHECK(!cdm_map_.count(cdm_id));
44 cdm_map_[cdm_id] = cdm;
45 }
46
47 // Unregisters the CDM associated with |cdm_id|.
48 void UnregisterCdm(int cdm_id) {
49 base::AutoLock lock(cdm_map_lock_);
50 DCHECK(cdm_map_.count(cdm_id));
51 cdm_map_.erase(cdm_id);
52 }
53
54 private:
55 base::Lock cdm_map_lock_;
Tima Vaisburd 2015/11/18 01:24:20 nit: I would rename |cdm_map_lock_| into |lock_| b
xhwang 2015/11/18 18:45:39 Done.
56 std::map<int, scoped_refptr<MediaKeys>> cdm_map_;
DaleCurtis 2015/11/18 01:00:21 Map is fine, but if you only ever expect 1 or 2 of
xhwang 2015/11/18 18:45:39 I don't really see performance difference here and
57
58 DISALLOW_COPY_AND_ASSIGN(CdmManager);
59 };
60
61 base::LazyInstance<CdmManager>::Leaky g_cdm_manager = LAZY_INSTANCE_INITIALIZER;
62
63 } // namespace
64
21 using SimpleMojoCdmPromise = MojoCdmPromise<>; 65 using SimpleMojoCdmPromise = MojoCdmPromise<>;
22 using CdmIdMojoCdmPromise = MojoCdmPromise<int>; 66 using CdmIdMojoCdmPromise = MojoCdmPromise<int>;
23 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; 67 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>;
24 68
25 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; 69 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1;
26 70
71 // static
72 scoped_refptr<MediaKeys> MojoCdmService::GetCdm(int cdm_id) {
73 DVLOG(1) << __FUNCTION__ << ": " << cdm_id;
74 return g_cdm_manager.Get().GetCdm(cdm_id);
75 }
76
27 MojoCdmService::MojoCdmService( 77 MojoCdmService::MojoCdmService(
28 base::WeakPtr<MojoCdmServiceContext> context, 78 base::WeakPtr<MojoCdmServiceContext> context,
29 mojo::ServiceProvider* service_provider, 79 mojo::ServiceProvider* service_provider,
30 CdmFactory* cdm_factory, 80 CdmFactory* cdm_factory,
31 mojo::InterfaceRequest<interfaces::ContentDecryptionModule> request) 81 mojo::InterfaceRequest<interfaces::ContentDecryptionModule> request)
32 : binding_(this, request.Pass()), 82 : binding_(this, request.Pass()),
33 context_(context), 83 context_(context),
34 service_provider_(service_provider), 84 service_provider_(service_provider),
35 cdm_factory_(cdm_factory), 85 cdm_factory_(cdm_factory),
36 cdm_id_(CdmContext::kInvalidCdmId), 86 cdm_id_(CdmContext::kInvalidCdmId),
37 weak_factory_(this) { 87 weak_factory_(this) {
38 DCHECK(context_); 88 DCHECK(context_);
39 DCHECK(cdm_factory_); 89 DCHECK(cdm_factory_);
40 } 90 }
41 91
42 MojoCdmService::~MojoCdmService() { 92 MojoCdmService::~MojoCdmService() {
93 g_cdm_manager.Get().UnregisterCdm(cdm_id_);
94
43 if (cdm_id_ != CdmContext::kInvalidCdmId && context_) 95 if (cdm_id_ != CdmContext::kInvalidCdmId && context_)
44 context_->UnregisterCdm(cdm_id_); 96 context_->UnregisterCdm(cdm_id_);
45 } 97 }
46 98
47 void MojoCdmService::SetClient( 99 void MojoCdmService::SetClient(
48 interfaces::ContentDecryptionModuleClientPtr client) { 100 interfaces::ContentDecryptionModuleClientPtr client) {
49 client_ = client.Pass(); 101 client_ = client.Pass();
50 } 102 }
51 103
52 void MojoCdmService::Initialize( 104 void MojoCdmService::Initialize(
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 const std::string& error_message) { 196 const std::string& error_message) {
145 // TODO(xhwang): This should not happen when KeySystemInfo is properly 197 // TODO(xhwang): This should not happen when KeySystemInfo is properly
146 // populated. See http://crbug.com/469366 198 // populated. See http://crbug.com/469366
147 if (!cdm || !context_) { 199 if (!cdm || !context_) {
148 promise->reject(MediaKeys::NOT_SUPPORTED_ERROR, 0, error_message); 200 promise->reject(MediaKeys::NOT_SUPPORTED_ERROR, 0, error_message);
149 return; 201 return;
150 } 202 }
151 203
152 cdm_ = cdm; 204 cdm_ = cdm;
153 cdm_id_ = next_cdm_id_++; 205 cdm_id_ = next_cdm_id_++;
206
154 context_->RegisterCdm(cdm_id_, this); 207 context_->RegisterCdm(cdm_id_, this);
208 g_cdm_manager.Get().RegisterCdm(cdm_id_, cdm);
209
210 DVLOG(1) << __FUNCTION__ << ": CDM successfully created with ID " << cdm_id_;
155 promise->resolve(cdm_id_); 211 promise->resolve(cdm_id_);
156 } 212 }
157 213
158 void MojoCdmService::OnSessionMessage(const std::string& session_id, 214 void MojoCdmService::OnSessionMessage(const std::string& session_id,
159 MediaKeys::MessageType message_type, 215 MediaKeys::MessageType message_type,
160 const std::vector<uint8_t>& message, 216 const std::vector<uint8_t>& message,
161 const GURL& legacy_destination_url) { 217 const GURL& legacy_destination_url) {
162 DVLOG(2) << __FUNCTION__; 218 DVLOG(2) << __FUNCTION__;
163 client_->OnSessionMessage( 219 client_->OnSessionMessage(
164 session_id, static_cast<interfaces::CdmMessageType>(message_type), 220 session_id, static_cast<interfaces::CdmMessageType>(message_type),
(...skipping 29 matching lines...) Expand all
194 MediaKeys::Exception exception, 250 MediaKeys::Exception exception,
195 uint32_t system_code, 251 uint32_t system_code,
196 const std::string& error_message) { 252 const std::string& error_message) {
197 DVLOG(2) << __FUNCTION__; 253 DVLOG(2) << __FUNCTION__;
198 client_->OnLegacySessionError( 254 client_->OnLegacySessionError(
199 session_id, static_cast<interfaces::CdmException>(exception), system_code, 255 session_id, static_cast<interfaces::CdmException>(exception), system_code,
200 error_message); 256 error_message);
201 } 257 }
202 258
203 } // namespace media 259 } // namespace media
OLDNEW
« media/mojo/services/mojo_cdm_service.h ('K') | « media/mojo/services/mojo_cdm_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698