Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/media/cdm_session_adapter.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "content/renderer/media/crypto/content_decryption_module_factory.h" | |
| 11 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h" | |
| 12 #include "media/base/media_keys.h" | |
| 13 #include "url/gurl.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 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() : weak_ptr_factory_(this) {} | |
| 23 | |
| 24 CdmSessionAdapter::~CdmSessionAdapter() {} | |
| 25 | |
| 26 bool CdmSessionAdapter::Initialize(const std::string& key_system) { | |
| 27 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr(); | |
| 28 media_keys_ = | |
| 29 ContentDecryptionModuleFactory::Create( | |
| 30 // TODO(ddorwin): Address lower in the stack: http://crbug.com/252065 | |
| 31 "webkit-" + key_system, | |
| 32 #if defined(ENABLE_PEPPER_CDMS) | |
| 33 // TODO(ddorwin): Support Pepper-based CDMs: http://crbug.com/250049 | |
| 34 NULL, | |
| 35 NULL, | |
| 36 base::Closure(), | |
| 37 #elif defined(OS_ANDROID) | |
| 38 // TODO(xhwang): Support Android. | |
| 39 NULL, | |
| 40 0, | |
| 41 // TODO(ddorwin): Get the URL for the frame containing the MediaKeys. | |
| 42 GURL(), | |
| 43 #endif // defined(ENABLE_PEPPER_CDMS) | |
| 44 base::Bind(&CdmSessionAdapter::OnSessionCreated, weak_this), | |
| 45 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this), | |
| 46 base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this), | |
| 47 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this), | |
| 48 base::Bind(&CdmSessionAdapter::OnSessionError, weak_this)); | |
| 49 | |
| 50 // Success if |media_keys_| created. | |
| 51 return media_keys_; | |
| 52 } | |
| 53 | |
| 54 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession( | |
| 55 blink::WebContentDecryptionModuleSession::Client* client) { | |
| 56 // Generate a unique internal session id for the new session. | |
| 57 uint32 session_id = next_session_id_++; | |
| 58 DCHECK(sessions_.find(session_id) == sessions_.end()); | |
| 59 WebContentDecryptionModuleSessionImpl* session = | |
| 60 new WebContentDecryptionModuleSessionImpl(session_id, client, this); | |
| 61 sessions_[session_id] = session; | |
| 62 return session; | |
| 63 } | |
| 64 | |
| 65 void CdmSessionAdapter::RemoveSession(uint32 session_id) { | |
| 66 DCHECK(sessions_.find(session_id) != sessions_.end()); | |
| 67 sessions_.erase(session_id); | |
| 68 } | |
| 69 | |
| 70 void CdmSessionAdapter::InitializeNewSession(uint32 session_id, | |
| 71 const std::string& content_type, | |
| 72 const uint8* init_data, | |
| 73 int init_data_length) { | |
| 74 DCHECK(sessions_.find(session_id) != sessions_.end()); | |
| 75 media_keys_->CreateSession( | |
| 76 session_id, content_type, init_data, init_data_length); | |
| 77 } | |
| 78 | |
| 79 void CdmSessionAdapter::UpdateSession(uint32 session_id, | |
| 80 const uint8* response, | |
| 81 int response_length) { | |
| 82 DCHECK(sessions_.find(session_id) != sessions_.end()); | |
| 83 media_keys_->UpdateSession(session_id, response, response_length); | |
| 84 } | |
| 85 | |
| 86 void CdmSessionAdapter::ReleaseSession(uint32 session_id) { | |
| 87 DCHECK(sessions_.find(session_id) != sessions_.end()); | |
| 88 media_keys_->ReleaseSession(session_id); | |
| 89 } | |
| 90 | |
| 91 media::Decryptor* CdmSessionAdapter::GetDecryptor() { | |
| 92 return media_keys_->GetDecryptor(); | |
| 93 } | |
| 94 | |
| 95 void CdmSessionAdapter::OnSessionCreated(uint32 session_id, | |
| 96 const std::string& web_session_id) { | |
| 97 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | |
| 98 DLOG_IF(WARNING, session) << __FUNCTION__ << " for unknown session " | |
|
ddorwin
2014/02/19 21:09:15
Oops. These should be !session.
jrummell
2014/02/19 21:23:14
Done.
| |
| 99 << session_id; | |
| 100 if (session) | |
| 101 session->OnSessionCreated(web_session_id); | |
| 102 } | |
| 103 | |
| 104 void CdmSessionAdapter::OnSessionMessage(uint32 session_id, | |
| 105 const std::vector<uint8>& message, | |
| 106 const std::string& destination_url) { | |
| 107 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | |
| 108 DLOG_IF(WARNING, session) << __FUNCTION__ << " for unknown session " | |
| 109 << session_id; | |
| 110 if (session) | |
| 111 session->OnSessionMessage(message, destination_url); | |
| 112 } | |
| 113 | |
| 114 void CdmSessionAdapter::OnSessionReady(uint32 session_id) { | |
| 115 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | |
| 116 DLOG_IF(WARNING, session) << __FUNCTION__ << " for unknown session " | |
| 117 << session_id; | |
| 118 if (session) | |
| 119 session->OnSessionReady(); | |
| 120 } | |
| 121 | |
| 122 void CdmSessionAdapter::OnSessionClosed(uint32 session_id) { | |
| 123 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | |
| 124 DLOG_IF(WARNING, session) << __FUNCTION__ << " for unknown session " | |
| 125 << session_id; | |
| 126 if (session) | |
| 127 session->OnSessionClosed(); | |
| 128 } | |
| 129 | |
| 130 void CdmSessionAdapter::OnSessionError(uint32 session_id, | |
| 131 media::MediaKeys::KeyError error_code, | |
| 132 int system_code) { | |
| 133 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id); | |
| 134 DLOG_IF(WARNING, session) << __FUNCTION__ << " for unknown session " | |
| 135 << session_id; | |
| 136 if (session) | |
| 137 session->OnSessionError(error_code, system_code); | |
| 138 } | |
| 139 | |
| 140 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession( | |
| 141 uint32 session_id) { | |
| 142 // Since session objects may get garbage collected, it is possible that there | |
| 143 // are events coming back from the CDM and the session has been unregistered. | |
| 144 // We can not tell if the CDM is firing events at sessions that never existed. | |
| 145 SessionMap::iterator session = sessions_.find(session_id); | |
| 146 return (session != sessions_.end()) ? session->second : NULL; | |
| 147 } | |
| 148 | |
| 149 } // namespace content | |
| OLD | NEW |