| 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" |
| 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(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, const scoped_refptr<MediaKeys>& cdm) { |
| 43 base::AutoLock lock(lock_); |
| 44 DCHECK(!cdm_map_.count(cdm_id)); |
| 45 cdm_map_[cdm_id] = cdm; |
| 46 } |
| 47 |
| 48 // Unregisters the CDM associated with |cdm_id|. |
| 49 void UnregisterCdm(int cdm_id) { |
| 50 base::AutoLock lock(lock_); |
| 51 DCHECK(cdm_map_.count(cdm_id)); |
| 52 cdm_map_.erase(cdm_id); |
| 53 } |
| 54 |
| 55 private: |
| 56 // Lock to protect |cdm_map_|. |
| 57 base::Lock lock_; |
| 58 std::map<int, scoped_refptr<MediaKeys>> cdm_map_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(CdmManager); |
| 61 }; |
| 62 |
| 63 base::LazyInstance<CdmManager>::Leaky g_cdm_manager = LAZY_INSTANCE_INITIALIZER; |
| 64 |
| 65 } // namespace |
| 66 |
| 21 using SimpleMojoCdmPromise = MojoCdmPromise<>; | 67 using SimpleMojoCdmPromise = MojoCdmPromise<>; |
| 22 using CdmIdMojoCdmPromise = MojoCdmPromise<int>; | 68 using CdmIdMojoCdmPromise = MojoCdmPromise<int>; |
| 23 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; | 69 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; |
| 24 | 70 |
| 25 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; | 71 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; |
| 26 | 72 |
| 73 // static |
| 74 scoped_refptr<MediaKeys> MojoCdmService::GetCdm(int cdm_id) { |
| 75 DVLOG(1) << __FUNCTION__ << ": " << cdm_id; |
| 76 return g_cdm_manager.Get().GetCdm(cdm_id); |
| 77 } |
| 78 |
| 27 MojoCdmService::MojoCdmService( | 79 MojoCdmService::MojoCdmService( |
| 28 base::WeakPtr<MojoCdmServiceContext> context, | 80 base::WeakPtr<MojoCdmServiceContext> context, |
| 29 mojo::ServiceProvider* service_provider, | 81 mojo::ServiceProvider* service_provider, |
| 30 CdmFactory* cdm_factory, | 82 CdmFactory* cdm_factory, |
| 31 mojo::InterfaceRequest<interfaces::ContentDecryptionModule> request) | 83 mojo::InterfaceRequest<interfaces::ContentDecryptionModule> request) |
| 32 : binding_(this, request.Pass()), | 84 : binding_(this, request.Pass()), |
| 33 context_(context), | 85 context_(context), |
| 34 service_provider_(service_provider), | 86 service_provider_(service_provider), |
| 35 cdm_factory_(cdm_factory), | 87 cdm_factory_(cdm_factory), |
| 36 cdm_id_(CdmContext::kInvalidCdmId), | 88 cdm_id_(CdmContext::kInvalidCdmId), |
| 37 weak_factory_(this) { | 89 weak_factory_(this) { |
| 38 DCHECK(context_); | 90 DCHECK(context_); |
| 39 DCHECK(cdm_factory_); | 91 DCHECK(cdm_factory_); |
| 40 } | 92 } |
| 41 | 93 |
| 42 MojoCdmService::~MojoCdmService() { | 94 MojoCdmService::~MojoCdmService() { |
| 95 g_cdm_manager.Get().UnregisterCdm(cdm_id_); |
| 96 |
| 43 if (cdm_id_ != CdmContext::kInvalidCdmId && context_) | 97 if (cdm_id_ != CdmContext::kInvalidCdmId && context_) |
| 44 context_->UnregisterCdm(cdm_id_); | 98 context_->UnregisterCdm(cdm_id_); |
| 45 } | 99 } |
| 46 | 100 |
| 47 void MojoCdmService::SetClient( | 101 void MojoCdmService::SetClient( |
| 48 interfaces::ContentDecryptionModuleClientPtr client) { | 102 interfaces::ContentDecryptionModuleClientPtr client) { |
| 49 client_ = client.Pass(); | 103 client_ = client.Pass(); |
| 50 } | 104 } |
| 51 | 105 |
| 52 void MojoCdmService::Initialize( | 106 void MojoCdmService::Initialize( |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 const std::string& error_message) { | 198 const std::string& error_message) { |
| 145 // TODO(xhwang): This should not happen when KeySystemInfo is properly | 199 // TODO(xhwang): This should not happen when KeySystemInfo is properly |
| 146 // populated. See http://crbug.com/469366 | 200 // populated. See http://crbug.com/469366 |
| 147 if (!cdm || !context_) { | 201 if (!cdm || !context_) { |
| 148 promise->reject(MediaKeys::NOT_SUPPORTED_ERROR, 0, error_message); | 202 promise->reject(MediaKeys::NOT_SUPPORTED_ERROR, 0, error_message); |
| 149 return; | 203 return; |
| 150 } | 204 } |
| 151 | 205 |
| 152 cdm_ = cdm; | 206 cdm_ = cdm; |
| 153 cdm_id_ = next_cdm_id_++; | 207 cdm_id_ = next_cdm_id_++; |
| 208 |
| 154 context_->RegisterCdm(cdm_id_, this); | 209 context_->RegisterCdm(cdm_id_, this); |
| 210 g_cdm_manager.Get().RegisterCdm(cdm_id_, cdm); |
| 211 |
| 212 DVLOG(1) << __FUNCTION__ << ": CDM successfully created with ID " << cdm_id_; |
| 155 promise->resolve(cdm_id_); | 213 promise->resolve(cdm_id_); |
| 156 } | 214 } |
| 157 | 215 |
| 158 void MojoCdmService::OnSessionMessage(const std::string& session_id, | 216 void MojoCdmService::OnSessionMessage(const std::string& session_id, |
| 159 MediaKeys::MessageType message_type, | 217 MediaKeys::MessageType message_type, |
| 160 const std::vector<uint8_t>& message, | 218 const std::vector<uint8_t>& message, |
| 161 const GURL& legacy_destination_url) { | 219 const GURL& legacy_destination_url) { |
| 162 DVLOG(2) << __FUNCTION__; | 220 DVLOG(2) << __FUNCTION__; |
| 163 client_->OnSessionMessage( | 221 client_->OnSessionMessage( |
| 164 session_id, static_cast<interfaces::CdmMessageType>(message_type), | 222 session_id, static_cast<interfaces::CdmMessageType>(message_type), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 194 MediaKeys::Exception exception, | 252 MediaKeys::Exception exception, |
| 195 uint32_t system_code, | 253 uint32_t system_code, |
| 196 const std::string& error_message) { | 254 const std::string& error_message) { |
| 197 DVLOG(2) << __FUNCTION__; | 255 DVLOG(2) << __FUNCTION__; |
| 198 client_->OnLegacySessionError( | 256 client_->OnLegacySessionError( |
| 199 session_id, static_cast<interfaces::CdmException>(exception), system_code, | 257 session_id, static_cast<interfaces::CdmException>(exception), system_code, |
| 200 error_message); | 258 error_message); |
| 201 } | 259 } |
| 202 | 260 |
| 203 } // namespace media | 261 } // namespace media |
| OLD | NEW |