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 "content/renderer/media/cdm_session_adapter.h" | 5 #include "content/renderer/media/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 "content/renderer/media/crypto/content_decryption_module_factory.h" | 11 #include "content/renderer/media/crypto/content_decryption_module_factory.h" |
11 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h" | 12 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h" |
| 13 #include "media/base/cdm_promise.h" |
12 #include "media/base/media_keys.h" | 14 #include "media/base/media_keys.h" |
13 #include "url/gurl.h" | 15 #include "url/gurl.h" |
14 | 16 |
15 namespace content { | 17 namespace content { |
16 | 18 |
17 const uint32 kStartingSessionId = 1; | |
18 uint32 CdmSessionAdapter::next_session_id_ = kStartingSessionId; | |
19 COMPILE_ASSERT(kStartingSessionId > media::MediaKeys::kInvalidSessionId, | |
20 invalid_starting_value); | |
21 | |
22 CdmSessionAdapter::CdmSessionAdapter() : | 19 CdmSessionAdapter::CdmSessionAdapter() : |
23 #if defined(OS_ANDROID) | 20 #if defined(OS_ANDROID) |
24 cdm_id_(0), | 21 cdm_id_(0), |
25 #endif | 22 #endif |
26 weak_ptr_factory_(this) {} | 23 weak_ptr_factory_(this) {} |
27 | 24 |
28 CdmSessionAdapter::~CdmSessionAdapter() {} | 25 CdmSessionAdapter::~CdmSessionAdapter() {} |
29 | 26 |
30 bool CdmSessionAdapter::Initialize( | 27 bool CdmSessionAdapter::Initialize( |
31 #if defined(ENABLE_PEPPER_CDMS) | 28 #if defined(ENABLE_PEPPER_CDMS) |
32 const CreatePepperCdmCB& create_pepper_cdm_cb, | 29 const CreatePepperCdmCB& create_pepper_cdm_cb, |
33 #endif // defined(ENABLE_PEPPER_CDMS) | 30 #endif // defined(ENABLE_PEPPER_CDMS) |
34 const std::string& key_system, | 31 const std::string& key_system, |
35 const GURL& security_origin) { | 32 const GURL& security_origin) { |
36 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); | 33 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); |
37 media_keys_ = ContentDecryptionModuleFactory::Create( | 34 media_keys_ = ContentDecryptionModuleFactory::Create( |
38 key_system, | 35 key_system, |
39 security_origin, | 36 security_origin, |
40 #if defined(ENABLE_PEPPER_CDMS) | 37 #if defined(ENABLE_PEPPER_CDMS) |
41 create_pepper_cdm_cb, | 38 create_pepper_cdm_cb, |
42 #elif defined(OS_ANDROID) | 39 #elif defined(OS_ANDROID) |
43 // TODO(xhwang): Support Android. | 40 // TODO(xhwang): Support Android. |
44 NULL, | 41 NULL, |
45 &cdm_id_, | 42 &cdm_id_, |
46 #endif // defined(ENABLE_PEPPER_CDMS) | 43 #endif // defined(ENABLE_PEPPER_CDMS) |
47 base::Bind(&CdmSessionAdapter::OnSessionCreated, weak_this), | |
48 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this), | 44 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this), |
49 base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this), | 45 base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this), |
50 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this), | 46 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this), |
51 base::Bind(&CdmSessionAdapter::OnSessionError, weak_this)); | 47 base::Bind(&CdmSessionAdapter::OnSessionError, weak_this)); |
52 | 48 |
53 // Success if |media_keys_| created. | 49 // Success if |media_keys_| created. |
54 return media_keys_; | 50 return media_keys_; |
55 } | 51 } |
56 | 52 |
57 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession( | 53 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession( |
58 blink::WebContentDecryptionModuleSession::Client* client) { | 54 blink::WebContentDecryptionModuleSession::Client* client) { |
59 // Generate a unique internal session id for the new session. | 55 return new WebContentDecryptionModuleSessionImpl(client, this); |
60 uint32 session_id = next_session_id_++; | |
61 DCHECK(sessions_.find(session_id) == sessions_.end()); | |
62 WebContentDecryptionModuleSessionImpl* session = | |
63 new WebContentDecryptionModuleSessionImpl(session_id, client, this); | |
64 sessions_[session_id] = session; | |
65 return session; | |
66 } | 56 } |
67 | 57 |
68 void CdmSessionAdapter::RemoveSession(uint32 session_id) { | 58 void CdmSessionAdapter::RegisterSession( |
69 DCHECK(sessions_.find(session_id) != sessions_.end()); | 59 const std::string& web_session_id, |
70 sessions_.erase(session_id); | 60 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session) { |
| 61 DCHECK(!ContainsKey(sessions_, web_session_id)); |
| 62 sessions_[web_session_id] = session; |
71 } | 63 } |
72 | 64 |
73 void CdmSessionAdapter::InitializeNewSession(uint32 session_id, | 65 void CdmSessionAdapter::RemoveSession(const std::string& web_session_id) { |
74 const std::string& content_type, | 66 DCHECK(ContainsKey(sessions_, web_session_id)); |
75 const uint8* init_data, | 67 sessions_.erase(web_session_id); |
76 int init_data_length) { | |
77 DCHECK(sessions_.find(session_id) != sessions_.end()); | |
78 media_keys_->CreateSession( | |
79 session_id, content_type, init_data, init_data_length); | |
80 } | 68 } |
81 | 69 |
82 void CdmSessionAdapter::UpdateSession(uint32 session_id, | 70 void CdmSessionAdapter::InitializeNewSession( |
83 const uint8* response, | 71 const std::string& init_data_type, |
84 int response_length) { | 72 const uint8* init_data, |
85 DCHECK(sessions_.find(session_id) != sessions_.end()); | 73 int init_data_length, |
86 media_keys_->UpdateSession(session_id, response, response_length); | 74 media::MediaKeys::SessionType session_type, |
| 75 scoped_ptr<media::NewSessionCdmPromise> promise) { |
| 76 media_keys_->CreateSession(init_data_type, |
| 77 init_data, |
| 78 init_data_length, |
| 79 session_type, |
| 80 promise.Pass()); |
87 } | 81 } |
88 | 82 |
89 void CdmSessionAdapter::ReleaseSession(uint32 session_id) { | 83 void CdmSessionAdapter::UpdateSession( |
90 DCHECK(sessions_.find(session_id) != sessions_.end()); | 84 const std::string& web_session_id, |
91 media_keys_->ReleaseSession(session_id); | 85 const uint8* response, |
| 86 int response_length, |
| 87 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 88 media_keys_->UpdateSession( |
| 89 web_session_id, response, response_length, promise.Pass()); |
| 90 } |
| 91 |
| 92 void CdmSessionAdapter::ReleaseSession( |
| 93 const std::string& web_session_id, |
| 94 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 95 media_keys_->ReleaseSession(web_session_id, promise.Pass()); |
92 } | 96 } |
93 | 97 |
94 media::Decryptor* CdmSessionAdapter::GetDecryptor() { | 98 media::Decryptor* CdmSessionAdapter::GetDecryptor() { |
95 return media_keys_->GetDecryptor(); | 99 return media_keys_->GetDecryptor(); |
96 } | 100 } |
97 | 101 |
98 #if defined(OS_ANDROID) | 102 #if defined(OS_ANDROID) |
99 int CdmSessionAdapter::GetCdmId() const { | 103 int CdmSessionAdapter::GetCdmId() const { |
100 return cdm_id_; | 104 return cdm_id_; |
101 } | 105 } |
102 #endif // defined(OS_ANDROID) | 106 #endif // defined(OS_ANDROID) |
103 | 107 |
104 void CdmSessionAdapter::OnSessionCreated(uint32 session_id, | 108 void CdmSessionAdapter::OnSessionMessage(const std::string& web_session_id, |
105 const std::string& web_session_id) { | |
106 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | |
107 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " | |
108 << session_id; | |
109 if (session) | |
110 session->OnSessionCreated(web_session_id); | |
111 } | |
112 | |
113 void CdmSessionAdapter::OnSessionMessage(uint32 session_id, | |
114 const std::vector<uint8>& message, | 109 const std::vector<uint8>& message, |
115 const GURL& destination_url) { | 110 const GURL& destination_url) { |
116 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | 111 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id); |
117 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " | 112 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " |
118 << session_id; | 113 << web_session_id; |
119 if (session) | 114 if (session) |
120 session->OnSessionMessage(message, destination_url); | 115 session->OnSessionMessage(message, destination_url); |
121 } | 116 } |
122 | 117 |
123 void CdmSessionAdapter::OnSessionReady(uint32 session_id) { | 118 void CdmSessionAdapter::OnSessionReady(const std::string& web_session_id) { |
124 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | 119 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id); |
125 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " | 120 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " |
126 << session_id; | 121 << web_session_id; |
127 if (session) | 122 if (session) |
128 session->OnSessionReady(); | 123 session->OnSessionReady(); |
129 } | 124 } |
130 | 125 |
131 void CdmSessionAdapter::OnSessionClosed(uint32 session_id) { | 126 void CdmSessionAdapter::OnSessionClosed(const std::string& web_session_id) { |
132 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | 127 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id); |
133 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " | 128 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " |
134 << session_id; | 129 << web_session_id; |
135 if (session) | 130 if (session) |
136 session->OnSessionClosed(); | 131 session->OnSessionClosed(); |
137 } | 132 } |
138 | 133 |
139 void CdmSessionAdapter::OnSessionError(uint32 session_id, | 134 void CdmSessionAdapter::OnSessionError( |
140 media::MediaKeys::KeyError error_code, | 135 const std::string& web_session_id, |
141 uint32 system_code) { | 136 media::MediaKeys::Exception exception_code, |
142 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | 137 uint32 system_code, |
| 138 const std::string& error_message) { |
| 139 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id); |
143 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " | 140 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session " |
144 << session_id; | 141 << web_session_id; |
145 if (session) | 142 if (session) |
146 session->OnSessionError(error_code, system_code); | 143 session->OnSessionError(exception_code, system_code, error_message); |
147 } | 144 } |
148 | 145 |
149 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession( | 146 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession( |
150 uint32 session_id) { | 147 const std::string& web_session_id) { |
151 // Since session objects may get garbage collected, it is possible that there | 148 // Since session objects may get garbage collected, it is possible that there |
152 // are events coming back from the CDM and the session has been unregistered. | 149 // are events coming back from the CDM and the session has been unregistered. |
153 // We can not tell if the CDM is firing events at sessions that never existed. | 150 // We can not tell if the CDM is firing events at sessions that never existed. |
154 SessionMap::iterator session = sessions_.find(session_id); | 151 SessionMap::iterator session = sessions_.find(web_session_id); |
155 return (session != sessions_.end()) ? session->second : NULL; | 152 return (session != sessions_.end()) ? session->second.get() : NULL; |
156 } | 153 } |
157 | 154 |
158 } // namespace content | 155 } // namespace content |
OLD | NEW |