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