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