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.
5 #include "content/renderer/media/cdm_session_adapter.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"
17 const uint32 kStartingSessionId = 1;
18 uint32 CdmSessionAdapter::next_session_id_ = kStartingSessionId;
19 COMPILE_ASSERT(kStartingSessionId > media::MediaKeys::kInvalidSessionId,
20 invalid_starting_value);
22 CdmSessionAdapter::CdmSessionAdapter() :
23 #if defined(OS_ANDROID)
26 weak_ptr_factory_(this) {}
28 CdmSessionAdapter::~CdmSessionAdapter() {}
30 bool CdmSessionAdapter::Initialize(
31 #if defined(ENABLE_PEPPER_CDMS)
32 const CreatePepperCdmCB& create_pepper_cdm_cb,
33 #endif // defined(ENABLE_PEPPER_CDMS)
34 const std::string& key_system,
35 const GURL& security_origin) {
36 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
37 media_keys_ = ContentDecryptionModuleFactory::Create(
40 #if defined(ENABLE_PEPPER_CDMS)
42 #elif defined(OS_ANDROID)
43 // TODO(xhwang): Support Android.
46 #endif // defined(ENABLE_PEPPER_CDMS)
47 base::Bind(&CdmSessionAdapter::OnSessionCreated, weak_this),
48 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this),
49 base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this),
50 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this),
51 base::Bind(&CdmSessionAdapter::OnSessionError, weak_this));
53 // Success if |media_keys_| created.
57 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession(
58 blink::WebContentDecryptionModuleSession::Client* client) {
59 // Generate a unique internal session id for the new session.
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;
68 void CdmSessionAdapter::RemoveSession(uint32 session_id) {
69 DCHECK(sessions_.find(session_id) != sessions_.end());
70 sessions_.erase(session_id);
73 void CdmSessionAdapter::InitializeNewSession(uint32 session_id,
74 const std::string& content_type,
75 const uint8* init_data,
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);
82 void CdmSessionAdapter::UpdateSession(uint32 session_id,
83 const uint8* response,
84 int response_length) {
85 DCHECK(sessions_.find(session_id) != sessions_.end());
86 media_keys_->UpdateSession(session_id, response, response_length);
89 void CdmSessionAdapter::ReleaseSession(uint32 session_id) {
90 DCHECK(sessions_.find(session_id) != sessions_.end());
91 media_keys_->ReleaseSession(session_id);
94 media::Decryptor* CdmSessionAdapter::GetDecryptor() {
95 return media_keys_->GetDecryptor();
98 #if defined(OS_ANDROID)
99 int CdmSessionAdapter::GetCdmId() const {
102 #endif // defined(OS_ANDROID)
104 void CdmSessionAdapter::OnSessionCreated(uint32 session_id,
105 const std::string& web_session_id) {
106 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
107 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
110 session->OnSessionCreated(web_session_id);
113 void CdmSessionAdapter::OnSessionMessage(uint32 session_id,
114 const std::vector<uint8>& message,
115 const std::string& destination_url) {
116 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
117 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
120 session->OnSessionMessage(message, destination_url);
123 void CdmSessionAdapter::OnSessionReady(uint32 session_id) {
124 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
125 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
128 session->OnSessionReady();
131 void CdmSessionAdapter::OnSessionClosed(uint32 session_id) {
132 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
133 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
136 session->OnSessionClosed();
139 void CdmSessionAdapter::OnSessionError(uint32 session_id,
140 media::MediaKeys::KeyError error_code,
141 uint32 system_code) {
142 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
143 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
146 session->OnSessionError(error_code, system_code);
149 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
151 // 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.
153 // We can not tell if the CDM is firing events at sessions that never existed.
154 SessionMap::iterator session = sessions_.find(session_id);
155 return (session != sessions_.end()) ? session->second : NULL;
158 } // namespace content