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/blink/cdm_session_adapter.h" | 5 #include "media/blink/cdm_session_adapter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "media/base/cdm_factory.h" | 11 #include "media/base/cdm_factory.h" |
12 #include "media/base/cdm_key_information.h" | 12 #include "media/base/cdm_key_information.h" |
13 #include "media/base/cdm_promise.h" | 13 #include "media/base/cdm_promise.h" |
14 #include "media/base/key_systems.h" | 14 #include "media/base/key_systems.h" |
15 #include "media/base/media_keys.h" | 15 #include "media/base/media_keys.h" |
| 16 #include "media/blink/webcontentdecryptionmodule_impl.h" |
16 #include "media/blink/webcontentdecryptionmodulesession_impl.h" | 17 #include "media/blink/webcontentdecryptionmodulesession_impl.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 | 21 |
21 const char kMediaEME[] = "Media.EME."; | 22 const char kMediaEME[] = "Media.EME."; |
22 const char kDot[] = "."; | 23 const char kDot[] = "."; |
23 | 24 |
24 CdmSessionAdapter::CdmSessionAdapter() : weak_ptr_factory_(this) { | 25 CdmSessionAdapter::CdmSessionAdapter() : weak_ptr_factory_(this) { |
25 } | 26 } |
26 | 27 |
27 CdmSessionAdapter::~CdmSessionAdapter() {} | 28 CdmSessionAdapter::~CdmSessionAdapter() {} |
28 | 29 |
29 bool CdmSessionAdapter::Initialize(CdmFactory* cdm_factory, | 30 void CdmSessionAdapter::CreateCdm( |
30 const std::string& key_system, | 31 CdmFactory* cdm_factory, |
31 bool allow_distinctive_identifier, | 32 const std::string& key_system, |
32 bool allow_persistent_state, | 33 bool allow_distinctive_identifier, |
33 const GURL& security_origin) { | 34 bool allow_persistent_state, |
34 key_system_ = key_system; | 35 const GURL& security_origin, |
35 key_system_uma_prefix_ = | 36 blink::WebContentDecryptionModuleResult result) { |
36 kMediaEME + GetKeySystemNameForUMA(key_system) + kDot; | 37 // Note: WebContentDecryptionModuleImpl::Create() calls this method without |
37 | 38 // holding a reference to the CdmSessionAdapter. Bind OnCdmCreated() with |
| 39 // |this| instead of |weak_this| to prevent |this| from being desctructed. |
38 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); | 40 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); |
39 media_keys_ = cdm_factory->Create( | 41 cdm_factory->Create( |
40 key_system, allow_distinctive_identifier, allow_persistent_state, | 42 key_system, allow_distinctive_identifier, allow_persistent_state, |
41 security_origin, | 43 security_origin, |
42 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this), | 44 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this), |
43 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this), | 45 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this), |
44 base::Bind(&CdmSessionAdapter::OnLegacySessionError, weak_this), | 46 base::Bind(&CdmSessionAdapter::OnLegacySessionError, weak_this), |
45 base::Bind(&CdmSessionAdapter::OnSessionKeysChange, weak_this), | 47 base::Bind(&CdmSessionAdapter::OnSessionKeysChange, weak_this), |
46 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate, weak_this)); | 48 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate, weak_this), |
47 return media_keys_.get() != nullptr; | 49 base::Bind(&CdmSessionAdapter::OnCdmCreated, this, key_system, result)); |
48 } | 50 } |
49 | 51 |
50 void CdmSessionAdapter::SetServerCertificate( | 52 void CdmSessionAdapter::SetServerCertificate( |
51 const uint8* server_certificate, | 53 const uint8* server_certificate, |
52 int server_certificate_length, | 54 int server_certificate_length, |
53 scoped_ptr<SimpleCdmPromise> promise) { | 55 scoped_ptr<SimpleCdmPromise> promise) { |
54 media_keys_->SetServerCertificate( | 56 cdm_->SetServerCertificate(server_certificate, server_certificate_length, |
55 server_certificate, server_certificate_length, promise.Pass()); | 57 promise.Pass()); |
56 } | 58 } |
57 | 59 |
58 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession() { | 60 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession() { |
59 return new WebContentDecryptionModuleSessionImpl(this); | 61 return new WebContentDecryptionModuleSessionImpl(this); |
60 } | 62 } |
61 | 63 |
62 bool CdmSessionAdapter::RegisterSession( | 64 bool CdmSessionAdapter::RegisterSession( |
63 const std::string& session_id, | 65 const std::string& session_id, |
64 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session) { | 66 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session) { |
65 // If this session ID is already registered, don't register it again. | 67 // If this session ID is already registered, don't register it again. |
66 if (ContainsKey(sessions_, session_id)) | 68 if (ContainsKey(sessions_, session_id)) |
67 return false; | 69 return false; |
68 | 70 |
69 sessions_[session_id] = session; | 71 sessions_[session_id] = session; |
70 return true; | 72 return true; |
71 } | 73 } |
72 | 74 |
73 void CdmSessionAdapter::UnregisterSession(const std::string& session_id) { | 75 void CdmSessionAdapter::UnregisterSession(const std::string& session_id) { |
74 DCHECK(ContainsKey(sessions_, session_id)); | 76 DCHECK(ContainsKey(sessions_, session_id)); |
75 sessions_.erase(session_id); | 77 sessions_.erase(session_id); |
76 } | 78 } |
77 | 79 |
78 void CdmSessionAdapter::InitializeNewSession( | 80 void CdmSessionAdapter::InitializeNewSession( |
79 EmeInitDataType init_data_type, | 81 EmeInitDataType init_data_type, |
80 const uint8* init_data, | 82 const uint8* init_data, |
81 int init_data_length, | 83 int init_data_length, |
82 MediaKeys::SessionType session_type, | 84 MediaKeys::SessionType session_type, |
83 scoped_ptr<NewSessionCdmPromise> promise) { | 85 scoped_ptr<NewSessionCdmPromise> promise) { |
84 media_keys_->CreateSessionAndGenerateRequest(session_type, init_data_type, | 86 cdm_->CreateSessionAndGenerateRequest(session_type, init_data_type, init_data, |
85 init_data, init_data_length, | 87 init_data_length, promise.Pass()); |
86 promise.Pass()); | |
87 } | 88 } |
88 | 89 |
89 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type, | 90 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type, |
90 const std::string& session_id, | 91 const std::string& session_id, |
91 scoped_ptr<NewSessionCdmPromise> promise) { | 92 scoped_ptr<NewSessionCdmPromise> promise) { |
92 media_keys_->LoadSession(session_type, session_id, promise.Pass()); | 93 cdm_->LoadSession(session_type, session_id, promise.Pass()); |
93 } | 94 } |
94 | 95 |
95 void CdmSessionAdapter::UpdateSession(const std::string& session_id, | 96 void CdmSessionAdapter::UpdateSession(const std::string& session_id, |
96 const uint8* response, | 97 const uint8* response, |
97 int response_length, | 98 int response_length, |
98 scoped_ptr<SimpleCdmPromise> promise) { | 99 scoped_ptr<SimpleCdmPromise> promise) { |
99 media_keys_->UpdateSession(session_id, response, response_length, | 100 cdm_->UpdateSession(session_id, response, response_length, promise.Pass()); |
100 promise.Pass()); | |
101 } | 101 } |
102 | 102 |
103 void CdmSessionAdapter::CloseSession(const std::string& session_id, | 103 void CdmSessionAdapter::CloseSession(const std::string& session_id, |
104 scoped_ptr<SimpleCdmPromise> promise) { | 104 scoped_ptr<SimpleCdmPromise> promise) { |
105 media_keys_->CloseSession(session_id, promise.Pass()); | 105 cdm_->CloseSession(session_id, promise.Pass()); |
106 } | 106 } |
107 | 107 |
108 void CdmSessionAdapter::RemoveSession(const std::string& session_id, | 108 void CdmSessionAdapter::RemoveSession(const std::string& session_id, |
109 scoped_ptr<SimpleCdmPromise> promise) { | 109 scoped_ptr<SimpleCdmPromise> promise) { |
110 media_keys_->RemoveSession(session_id, promise.Pass()); | 110 cdm_->RemoveSession(session_id, promise.Pass()); |
111 } | 111 } |
112 | 112 |
113 CdmContext* CdmSessionAdapter::GetCdmContext() { | 113 CdmContext* CdmSessionAdapter::GetCdmContext() { |
114 return media_keys_->GetCdmContext(); | 114 return cdm_->GetCdmContext(); |
115 } | 115 } |
116 | 116 |
117 const std::string& CdmSessionAdapter::GetKeySystem() const { | 117 const std::string& CdmSessionAdapter::GetKeySystem() const { |
118 return key_system_; | 118 return key_system_; |
119 } | 119 } |
120 | 120 |
121 const std::string& CdmSessionAdapter::GetKeySystemUMAPrefix() const { | 121 const std::string& CdmSessionAdapter::GetKeySystemUMAPrefix() const { |
122 return key_system_uma_prefix_; | 122 return key_system_uma_prefix_; |
123 } | 123 } |
124 | 124 |
| 125 void CdmSessionAdapter::OnCdmCreated( |
| 126 const std::string& key_system, |
| 127 blink::WebContentDecryptionModuleResult result, |
| 128 scoped_ptr<MediaKeys> cdm) { |
| 129 DVLOG(2) << __FUNCTION__; |
| 130 if (!cdm) { |
| 131 result.completeWithError( |
| 132 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0, |
| 133 "Failed to create the CDM instance."); |
| 134 return; |
| 135 } |
| 136 |
| 137 key_system_ = key_system; |
| 138 key_system_uma_prefix_ = |
| 139 kMediaEME + GetKeySystemNameForUMA(key_system) + kDot; |
| 140 cdm_ = cdm.Pass(); |
| 141 |
| 142 result.completeWithContentDecryptionModule( |
| 143 new WebContentDecryptionModuleImpl(this)); |
| 144 } |
| 145 |
125 void CdmSessionAdapter::OnSessionMessage( | 146 void CdmSessionAdapter::OnSessionMessage( |
126 const std::string& session_id, | 147 const std::string& session_id, |
127 MediaKeys::MessageType message_type, | 148 MediaKeys::MessageType message_type, |
128 const std::vector<uint8>& message, | 149 const std::vector<uint8>& message, |
129 const GURL& /* legacy_destination_url */) { | 150 const GURL& /* legacy_destination_url */) { |
130 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | 151 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); |
131 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " | 152 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " |
132 << session_id; | 153 << session_id; |
133 if (session) | 154 if (session) |
134 session->OnSessionMessage(message_type, message); | 155 session->OnSessionMessage(message_type, message); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession( | 196 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession( |
176 const std::string& session_id) { | 197 const std::string& session_id) { |
177 // Since session objects may get garbage collected, it is possible that there | 198 // Since session objects may get garbage collected, it is possible that there |
178 // are events coming back from the CDM and the session has been unregistered. | 199 // are events coming back from the CDM and the session has been unregistered. |
179 // We can not tell if the CDM is firing events at sessions that never existed. | 200 // We can not tell if the CDM is firing events at sessions that never existed. |
180 SessionMap::iterator session = sessions_.find(session_id); | 201 SessionMap::iterator session = sessions_.find(session_id); |
181 return (session != sessions_.end()) ? session->second.get() : NULL; | 202 return (session != sessions_.end()) ? session->second.get() : NULL; |
182 } | 203 } |
183 | 204 |
184 } // namespace media | 205 } // namespace media |
OLD | NEW |