Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 8 #include "media/base/cdm_config.h" | 11 #include "media/base/cdm_config.h" |
| 9 #include "media/base/cdm_context.h" | 12 #include "media/base/cdm_context.h" |
| 10 #include "media/base/cdm_factory.h" | 13 #include "media/base/cdm_factory.h" |
| 11 #include "media/base/cdm_key_information.h" | 14 #include "media/base/cdm_key_information.h" |
| 12 #include "media/base/key_systems.h" | 15 #include "media/base/key_systems.h" |
| 13 #include "media/mojo/services/media_type_converters.h" | 16 #include "media/mojo/services/media_type_converters.h" |
| 14 #include "media/mojo/services/mojo_cdm_service_context.h" | 17 #include "media/mojo/services/mojo_cdm_service_context.h" |
| 15 #include "mojo/common/common_type_converters.h" | 18 #include "mojo/common/common_type_converters.h" |
| 16 #include "mojo/common/url_type_converters.h" | 19 #include "mojo/common/url_type_converters.h" |
| 17 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 18 | 21 |
| 19 namespace media { | 22 namespace media { |
| 20 | 23 |
| 24 namespace { | |
| 25 | |
| 26 // A static map between the CDM ID and the CDM. Can be accessed on different | |
| 27 // threads (protected by the |g_cdm_map_lock_|). | |
| 28 using CdmMap = std::map<int, scoped_refptr<MediaKeys>>; | |
| 29 | |
| 30 base::LazyInstance<CdmMap>::Leaky g_cdm_map = LAZY_INSTANCE_INITIALIZER; | |
|
DaleCurtis
2015/11/17 23:57:05
Instead of two lazy instances you could put this i
xhwang
2015/11/18 00:52:53
It does make the main impl (MojoCdmService) cleane
| |
| 31 | |
| 32 base::LazyInstance<base::Lock>::Leaky g_cdm_map_lock = | |
| 33 LAZY_INSTANCE_INITIALIZER; | |
| 34 } | |
| 35 | |
| 21 using SimpleMojoCdmPromise = MojoCdmPromise<>; | 36 using SimpleMojoCdmPromise = MojoCdmPromise<>; |
| 22 using CdmIdMojoCdmPromise = MojoCdmPromise<int>; | 37 using CdmIdMojoCdmPromise = MojoCdmPromise<int>; |
| 23 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; | 38 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; |
| 24 | 39 |
| 25 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; | 40 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; |
| 26 | 41 |
| 42 // static | |
| 43 scoped_refptr<MediaKeys> MojoCdmService::GetCdm(int cdm_id) { | |
| 44 DVLOG(1) << __FUNCTION__ << ": " << cdm_id; | |
| 45 base::AutoLock lock(g_cdm_map_lock.Get()); | |
| 46 return g_cdm_map.Get().count(cdm_id) ? g_cdm_map.Get()[cdm_id] : nullptr; | |
| 47 } | |
| 48 | |
| 27 MojoCdmService::MojoCdmService( | 49 MojoCdmService::MojoCdmService( |
| 28 base::WeakPtr<MojoCdmServiceContext> context, | 50 base::WeakPtr<MojoCdmServiceContext> context, |
| 29 mojo::ServiceProvider* service_provider, | 51 mojo::ServiceProvider* service_provider, |
| 30 CdmFactory* cdm_factory, | 52 CdmFactory* cdm_factory, |
| 31 mojo::InterfaceRequest<interfaces::ContentDecryptionModule> request) | 53 mojo::InterfaceRequest<interfaces::ContentDecryptionModule> request) |
| 32 : binding_(this, request.Pass()), | 54 : binding_(this, request.Pass()), |
| 33 context_(context), | 55 context_(context), |
| 34 service_provider_(service_provider), | 56 service_provider_(service_provider), |
| 35 cdm_factory_(cdm_factory), | 57 cdm_factory_(cdm_factory), |
| 36 cdm_id_(CdmContext::kInvalidCdmId), | 58 cdm_id_(CdmContext::kInvalidCdmId), |
| 37 weak_factory_(this) { | 59 weak_factory_(this) { |
| 38 DCHECK(context_); | 60 DCHECK(context_); |
| 39 DCHECK(cdm_factory_); | 61 DCHECK(cdm_factory_); |
| 40 } | 62 } |
| 41 | 63 |
| 42 MojoCdmService::~MojoCdmService() { | 64 MojoCdmService::~MojoCdmService() { |
| 65 { | |
| 66 base::AutoLock lock(g_cdm_map_lock.Get()); | |
| 67 DCHECK(g_cdm_map.Get().count(cdm_id_)); | |
| 68 g_cdm_map.Get().erase(cdm_id_); | |
| 69 } | |
| 70 | |
| 43 if (cdm_id_ != CdmContext::kInvalidCdmId && context_) | 71 if (cdm_id_ != CdmContext::kInvalidCdmId && context_) |
| 44 context_->UnregisterCdm(cdm_id_); | 72 context_->UnregisterCdm(cdm_id_); |
| 45 } | 73 } |
| 46 | 74 |
| 47 void MojoCdmService::SetClient( | 75 void MojoCdmService::SetClient( |
| 48 interfaces::ContentDecryptionModuleClientPtr client) { | 76 interfaces::ContentDecryptionModuleClientPtr client) { |
| 49 client_ = client.Pass(); | 77 client_ = client.Pass(); |
| 50 } | 78 } |
| 51 | 79 |
| 52 void MojoCdmService::Initialize( | 80 void MojoCdmService::Initialize( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 // TODO(xhwang): This should not happen when KeySystemInfo is properly | 173 // TODO(xhwang): This should not happen when KeySystemInfo is properly |
| 146 // populated. See http://crbug.com/469366 | 174 // populated. See http://crbug.com/469366 |
| 147 if (!cdm || !context_) { | 175 if (!cdm || !context_) { |
| 148 promise->reject(MediaKeys::NOT_SUPPORTED_ERROR, 0, error_message); | 176 promise->reject(MediaKeys::NOT_SUPPORTED_ERROR, 0, error_message); |
| 149 return; | 177 return; |
| 150 } | 178 } |
| 151 | 179 |
| 152 cdm_ = cdm; | 180 cdm_ = cdm; |
| 153 cdm_id_ = next_cdm_id_++; | 181 cdm_id_ = next_cdm_id_++; |
| 154 context_->RegisterCdm(cdm_id_, this); | 182 context_->RegisterCdm(cdm_id_, this); |
| 183 | |
| 184 { | |
| 185 base::AutoLock lock(g_cdm_map_lock.Get()); | |
| 186 DCHECK(!g_cdm_map.Get().count(cdm_id_)); | |
| 187 g_cdm_map.Get()[cdm_id_] = cdm; | |
| 188 } | |
| 189 | |
| 190 DVLOG(1) << __FUNCTION__ << ": CDM successfully created with ID " << cdm_id_; | |
| 155 promise->resolve(cdm_id_); | 191 promise->resolve(cdm_id_); |
| 156 } | 192 } |
| 157 | 193 |
| 158 void MojoCdmService::OnSessionMessage(const std::string& session_id, | 194 void MojoCdmService::OnSessionMessage(const std::string& session_id, |
| 159 MediaKeys::MessageType message_type, | 195 MediaKeys::MessageType message_type, |
| 160 const std::vector<uint8_t>& message, | 196 const std::vector<uint8_t>& message, |
| 161 const GURL& legacy_destination_url) { | 197 const GURL& legacy_destination_url) { |
| 162 DVLOG(2) << __FUNCTION__; | 198 DVLOG(2) << __FUNCTION__; |
| 163 client_->OnSessionMessage( | 199 client_->OnSessionMessage( |
| 164 session_id, static_cast<interfaces::CdmMessageType>(message_type), | 200 session_id, static_cast<interfaces::CdmMessageType>(message_type), |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 194 MediaKeys::Exception exception, | 230 MediaKeys::Exception exception, |
| 195 uint32_t system_code, | 231 uint32_t system_code, |
| 196 const std::string& error_message) { | 232 const std::string& error_message) { |
| 197 DVLOG(2) << __FUNCTION__; | 233 DVLOG(2) << __FUNCTION__; |
| 198 client_->OnLegacySessionError( | 234 client_->OnLegacySessionError( |
| 199 session_id, static_cast<interfaces::CdmException>(exception), system_code, | 235 session_id, static_cast<interfaces::CdmException>(exception), system_code, |
| 200 error_message); | 236 error_message); |
| 201 } | 237 } |
| 202 | 238 |
| 203 } // namespace media | 239 } // namespace media |
| OLD | NEW |