| 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 "media/mojo/services/mojo_cdm_service.h" | 5 #include "media/mojo/services/mojo_cdm_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "media/base/key_systems.h" | 8 #include "media/base/key_systems.h" |
| 9 #include "media/cdm/aes_decryptor.h" | 9 #include "media/cdm/aes_decryptor.h" |
| 10 #include "media/mojo/services/mojo_cdm_promise.h" | 10 #include "media/mojo/services/mojo_cdm_promise.h" |
| 11 #include "media/mojo/services/mojo_cdm_service_context.h" |
| 11 #include "mojo/common/common_type_converters.h" | 12 #include "mojo/common/common_type_converters.h" |
| 12 | 13 |
| 13 namespace media { | 14 namespace media { |
| 14 | 15 |
| 15 typedef MojoCdmPromise<> SimpleMojoCdmPromise; | 16 typedef MojoCdmPromise<> SimpleMojoCdmPromise; |
| 16 typedef MojoCdmPromise<std::string> NewSessionMojoCdmPromise; | 17 typedef MojoCdmPromise<std::string> NewSessionMojoCdmPromise; |
| 17 typedef MojoCdmPromise<std::vector<std::vector<uint8_t>>> KeyIdsMojoCdmPromise; | 18 typedef MojoCdmPromise<std::vector<std::vector<uint8_t>>> KeyIdsMojoCdmPromise; |
| 18 | 19 |
| 19 MojoCdmService::MojoCdmService(const mojo::String& key_system) | 20 MojoCdmService* MojoCdmService::Create(MojoCdmServiceContext* context, |
| 20 : weak_factory_(this) { | 21 const mojo::String& key_system) { |
| 22 // Only AesDecryptor is supported. |
| 23 if (!CanUseAesDecryptor(key_system)) |
| 24 return nullptr; |
| 25 |
| 26 return new MojoCdmService(context, key_system); |
| 27 } |
| 28 |
| 29 MojoCdmService::MojoCdmService(MojoCdmServiceContext* context, |
| 30 const mojo::String& key_system) |
| 31 : context_(context), weak_factory_(this) { |
| 32 DVLOG(1) << __FUNCTION__ << ": " << key_system; |
| 33 DCHECK(CanUseAesDecryptor(key_system)); |
| 34 |
| 21 base::WeakPtr<MojoCdmService> weak_this = weak_factory_.GetWeakPtr(); | 35 base::WeakPtr<MojoCdmService> weak_this = weak_factory_.GetWeakPtr(); |
| 22 if (CanUseAesDecryptor(key_system)) { | 36 cdm_.reset(new AesDecryptor( |
| 23 cdm_.reset(new AesDecryptor( | 37 base::Bind(&MojoCdmService::OnSessionMessage, weak_this), |
| 24 base::Bind(&MojoCdmService::OnSessionMessage, weak_this), | 38 base::Bind(&MojoCdmService::OnSessionClosed, weak_this), |
| 25 base::Bind(&MojoCdmService::OnSessionClosed, weak_this), | 39 base::Bind(&MojoCdmService::OnSessionKeysChange, weak_this))); |
| 26 base::Bind(&MojoCdmService::OnSessionKeysChange, weak_this))); | |
| 27 } | |
| 28 | |
| 29 // TODO(xhwang): Check key system support in the app. | |
| 30 NOTREACHED(); | |
| 31 } | 40 } |
| 32 | 41 |
| 33 MojoCdmService::~MojoCdmService() { | 42 MojoCdmService::~MojoCdmService() { |
| 34 } | 43 } |
| 35 | 44 |
| 36 // mojo::MediaRenderer implementation. | 45 // mojo::MediaRenderer implementation. |
| 37 void MojoCdmService::SetServerCertificate( | 46 void MojoCdmService::SetServerCertificate( |
| 38 mojo::Array<uint8_t> certificate_data, | 47 mojo::Array<uint8_t> certificate_data, |
| 39 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { | 48 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { |
| 49 DVLOG(2) << __FUNCTION__; |
| 40 const std::vector<uint8_t>& certificate_data_vector = | 50 const std::vector<uint8_t>& certificate_data_vector = |
| 41 certificate_data.storage(); | 51 certificate_data.storage(); |
| 42 cdm_->SetServerCertificate( | 52 cdm_->SetServerCertificate( |
| 43 certificate_data_vector.empty() ? nullptr : &certificate_data_vector[0], | 53 certificate_data_vector.empty() ? nullptr : &certificate_data_vector[0], |
| 44 certificate_data_vector.size(), | 54 certificate_data_vector.size(), |
| 45 scoped_ptr<SimpleCdmPromise>(new SimpleMojoCdmPromise(callback))); | 55 make_scoped_ptr(new SimpleMojoCdmPromise(callback))); |
| 46 } | 56 } |
| 47 | 57 |
| 48 void MojoCdmService::CreateSession( | 58 void MojoCdmService::CreateSession( |
| 49 const mojo::String& init_data_type, | 59 const mojo::String& init_data_type, |
| 50 mojo::Array<uint8_t> init_data, | 60 mojo::Array<uint8_t> init_data, |
| 51 mojo::ContentDecryptionModule::SessionType session_type, | 61 mojo::ContentDecryptionModule::SessionType session_type, |
| 52 const mojo::Callback<void(mojo::CdmPromiseResultPtr, mojo::String)>& | 62 const mojo::Callback<void(mojo::CdmPromiseResultPtr, mojo::String)>& |
| 53 callback) { | 63 callback) { |
| 64 DVLOG(2) << __FUNCTION__; |
| 54 const std::vector<uint8_t>& init_data_vector = init_data.storage(); | 65 const std::vector<uint8_t>& init_data_vector = init_data.storage(); |
| 55 cdm_->CreateSession( | 66 cdm_->CreateSession(init_data_type.To<std::string>(), |
| 56 init_data_type.To<std::string>(), | 67 init_data_vector.empty() ? nullptr : &init_data_vector[0], |
| 57 init_data_vector.empty() ? nullptr : &init_data_vector[0], | 68 init_data_vector.size(), |
| 58 init_data_vector.size(), | 69 static_cast<MediaKeys::SessionType>(session_type), |
| 59 static_cast<MediaKeys::SessionType>(session_type), | 70 make_scoped_ptr(new NewSessionMojoCdmPromise(callback))); |
| 60 scoped_ptr<NewSessionCdmPromise>(new NewSessionMojoCdmPromise(callback))); | |
| 61 } | 71 } |
| 62 | 72 |
| 63 void MojoCdmService::LoadSession( | 73 void MojoCdmService::LoadSession( |
| 64 const mojo::String& session_id, | 74 const mojo::String& session_id, |
| 65 const mojo::Callback<void(mojo::CdmPromiseResultPtr, mojo::String)>& | 75 const mojo::Callback<void(mojo::CdmPromiseResultPtr, mojo::String)>& |
| 66 callback) { | 76 callback) { |
| 67 cdm_->LoadSession( | 77 DVLOG(2) << __FUNCTION__; |
| 68 session_id.To<std::string>(), | 78 cdm_->LoadSession(session_id.To<std::string>(), |
| 69 scoped_ptr<NewSessionCdmPromise>(new NewSessionMojoCdmPromise(callback))); | 79 make_scoped_ptr(new NewSessionMojoCdmPromise(callback))); |
| 70 } | 80 } |
| 71 | 81 |
| 72 void MojoCdmService::UpdateSession( | 82 void MojoCdmService::UpdateSession( |
| 73 const mojo::String& session_id, | 83 const mojo::String& session_id, |
| 74 mojo::Array<uint8_t> response, | 84 mojo::Array<uint8_t> response, |
| 75 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { | 85 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { |
| 86 DVLOG(2) << __FUNCTION__; |
| 76 const std::vector<uint8_t>& response_vector = response.storage(); | 87 const std::vector<uint8_t>& response_vector = response.storage(); |
| 77 cdm_->UpdateSession( | 88 cdm_->UpdateSession(session_id.To<std::string>(), |
| 78 session_id.To<std::string>(), | 89 response_vector.empty() ? nullptr : &response_vector[0], |
| 79 response_vector.empty() ? nullptr : &response_vector[0], | 90 response_vector.size(), |
| 80 response_vector.size(), | 91 make_scoped_ptr(new SimpleMojoCdmPromise(callback))); |
| 81 scoped_ptr<SimpleCdmPromise>(new SimpleMojoCdmPromise(callback))); | |
| 82 } | 92 } |
| 83 | 93 |
| 84 void MojoCdmService::CloseSession( | 94 void MojoCdmService::CloseSession( |
| 85 const mojo::String& session_id, | 95 const mojo::String& session_id, |
| 86 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { | 96 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { |
| 87 cdm_->CloseSession( | 97 DVLOG(2) << __FUNCTION__; |
| 88 session_id.To<std::string>(), | 98 cdm_->CloseSession(session_id.To<std::string>(), |
| 89 scoped_ptr<SimpleCdmPromise>(new SimpleMojoCdmPromise(callback))); | 99 make_scoped_ptr(new SimpleMojoCdmPromise(callback))); |
| 90 } | 100 } |
| 91 | 101 |
| 92 void MojoCdmService::RemoveSession( | 102 void MojoCdmService::RemoveSession( |
| 93 const mojo::String& session_id, | 103 const mojo::String& session_id, |
| 94 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { | 104 const mojo::Callback<void(mojo::CdmPromiseResultPtr)>& callback) { |
| 95 cdm_->RemoveSession( | 105 DVLOG(2) << __FUNCTION__; |
| 96 session_id.To<std::string>(), | 106 cdm_->RemoveSession(session_id.To<std::string>(), |
| 97 scoped_ptr<SimpleCdmPromise>(new SimpleMojoCdmPromise(callback))); | 107 make_scoped_ptr(new SimpleMojoCdmPromise(callback))); |
| 98 } | 108 } |
| 99 | 109 |
| 100 void MojoCdmService::GetCdmContext( | 110 void MojoCdmService::GetDecryptor( |
| 101 int32_t cdm_id, | |
| 102 mojo::InterfaceRequest<mojo::Decryptor> decryptor) { | 111 mojo::InterfaceRequest<mojo::Decryptor> decryptor) { |
| 103 NOTIMPLEMENTED(); | 112 NOTIMPLEMENTED(); |
| 104 } | 113 } |
| 105 | 114 |
| 115 CdmContext* MojoCdmService::GetCdmContext() { |
| 116 DVLOG(2) << __FUNCTION__; |
| 117 return cdm_->GetCdmContext(); |
| 118 } |
| 119 |
| 120 void MojoCdmService::OnConnectionError() { |
| 121 DVLOG(2) << __FUNCTION__; |
| 122 context_->ServiceHadConnectionError(this); |
| 123 |
| 124 // The above call deleted this instance, so the only safe thing to do is |
| 125 // return. |
| 126 } |
| 127 |
| 106 void MojoCdmService::OnSessionMessage(const std::string& session_id, | 128 void MojoCdmService::OnSessionMessage(const std::string& session_id, |
| 107 const std::vector<uint8_t>& message, | 129 const std::vector<uint8_t>& message, |
| 108 const GURL& destination_url) { | 130 const GURL& destination_url) { |
| 131 DVLOG(2) << __FUNCTION__; |
| 109 client()->OnSessionMessage(session_id, mojo::Array<uint8_t>::From(message), | 132 client()->OnSessionMessage(session_id, mojo::Array<uint8_t>::From(message), |
| 110 mojo::String::From(destination_url)); | 133 mojo::String::From(destination_url)); |
| 111 } | 134 } |
| 112 | 135 |
| 113 void MojoCdmService::OnSessionKeysChange(const std::string& session_id, | 136 void MojoCdmService::OnSessionKeysChange(const std::string& session_id, |
| 114 bool has_additional_usable_key) { | 137 bool has_additional_usable_key) { |
| 138 DVLOG(2) << __FUNCTION__; |
| 115 client()->OnSessionKeysChange(session_id, has_additional_usable_key); | 139 client()->OnSessionKeysChange(session_id, has_additional_usable_key); |
| 116 } | 140 } |
| 117 | 141 |
| 118 void MojoCdmService::OnSessionExpirationUpdate( | 142 void MojoCdmService::OnSessionExpirationUpdate( |
| 119 const std::string& session_id, | 143 const std::string& session_id, |
| 120 const base::Time& new_expiry_time) { | 144 const base::Time& new_expiry_time) { |
| 145 DVLOG(2) << __FUNCTION__; |
| 121 client()->OnSessionExpirationUpdate(session_id, | 146 client()->OnSessionExpirationUpdate(session_id, |
| 122 new_expiry_time.ToInternalValue()); | 147 new_expiry_time.ToInternalValue()); |
| 123 } | 148 } |
| 124 | 149 |
| 125 void MojoCdmService::OnSessionClosed(const std::string& session_id) { | 150 void MojoCdmService::OnSessionClosed(const std::string& session_id) { |
| 151 DVLOG(2) << __FUNCTION__; |
| 126 client()->OnSessionClosed(session_id); | 152 client()->OnSessionClosed(session_id); |
| 127 } | 153 } |
| 128 | 154 |
| 129 void MojoCdmService::OnSessionError(const std::string& session_id, | 155 void MojoCdmService::OnSessionError(const std::string& session_id, |
| 130 MediaKeys::Exception exception, | 156 MediaKeys::Exception exception, |
| 131 uint32_t system_code, | 157 uint32_t system_code, |
| 132 const std::string& error_message) { | 158 const std::string& error_message) { |
| 159 DVLOG(2) << __FUNCTION__; |
| 133 client()->OnSessionError(session_id, | 160 client()->OnSessionError(session_id, |
| 134 static_cast<mojo::CdmException>(exception), | 161 static_cast<mojo::CdmException>(exception), |
| 135 system_code, error_message); | 162 system_code, error_message); |
| 136 } | 163 } |
| 137 | 164 |
| 138 } // namespace media | 165 } // namespace media |
| OLD | NEW |