1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chromeos/components/cdm_factory_daemon/content_decryption_module_adapter.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "chromeos/components/cdm_factory_daemon/chromeos_cdm_factory.h"
#include "media/base/cdm_promise.h"
#include "media/base/decoder_buffer.h"
#include "media/base/eme_constants.h"
#include "media/base/subsample_entry.h"
#include "media/cdm/cdm_context_ref_impl.h"
namespace {
void RejectPromiseConnectionLost(std::unique_ptr<media::CdmPromise> promise) {
promise->reject(media::CdmPromise::Exception::INVALID_STATE_ERROR, 0,
"Mojo connection lost");
}
void ReportSystemCodeUMA(uint32_t system_code) {
base::UmaHistogramSparse("Media.EME.CrosPlatformCdm.SystemCode", system_code);
}
} // namespace
namespace chromeos {
ContentDecryptionModuleAdapter::ContentDecryptionModuleAdapter(
std::unique_ptr<CdmStorageAdapter> storage,
mojo::AssociatedRemote<cdm::mojom::ContentDecryptionModule> cros_cdm_remote,
const media::SessionMessageCB& session_message_cb,
const media::SessionClosedCB& session_closed_cb,
const media::SessionKeysChangeCB& session_keys_change_cb,
const media::SessionExpirationUpdateCB& session_expiration_update_cb)
: storage_(std::move(storage)),
cros_cdm_remote_(std::move(cros_cdm_remote)),
session_message_cb_(session_message_cb),
session_closed_cb_(session_closed_cb),
session_keys_change_cb_(session_keys_change_cb),
session_expiration_update_cb_(session_expiration_update_cb),
mojo_task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {
DVLOG(1) << "Created ContentDecryptionModuleAdapter";
cros_cdm_remote_.set_disconnect_handler(
base::BindOnce(&ContentDecryptionModuleAdapter::OnConnectionError,
base::Unretained(this)));
}
mojo::PendingAssociatedRemote<cdm::mojom::ContentDecryptionModuleClient>
ContentDecryptionModuleAdapter::GetClientInterface() {
CHECK(!cros_client_receiver_.is_bound());
auto ret = cros_client_receiver_.BindNewEndpointAndPassRemote();
cros_client_receiver_.set_disconnect_handler(
base::BindOnce(&ContentDecryptionModuleAdapter::OnConnectionError,
base::Unretained(this)));
return ret;
}
void ContentDecryptionModuleAdapter::SetServerCertificate(
const std::vector<uint8_t>& certificate_data,
std::unique_ptr<media::SimpleCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->SetServerCertificate(
certificate_data,
base::BindOnce(&ContentDecryptionModuleAdapter::OnSimplePromiseResult,
base::Unretained(this), promise_id));
}
void ContentDecryptionModuleAdapter::GetStatusForPolicy(
media::HdcpVersion min_hdcp_version,
std::unique_ptr<media::KeyStatusCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->GetStatusForPolicy(
min_hdcp_version,
base::BindOnce(&ContentDecryptionModuleAdapter::OnGetStatusForPolicy,
base::Unretained(this), promise_id));
}
void ContentDecryptionModuleAdapter::CreateSessionAndGenerateRequest(
media::CdmSessionType session_type,
media::EmeInitDataType init_data_type,
const std::vector<uint8_t>& init_data,
std::unique_ptr<media::NewSessionCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->CreateSessionAndGenerateRequest(
session_type, init_data_type, init_data,
base::BindOnce(&ContentDecryptionModuleAdapter::OnSessionPromiseResult,
base::Unretained(this), promise_id));
}
void ContentDecryptionModuleAdapter::LoadSession(
media::CdmSessionType session_type,
const std::string& session_id,
std::unique_ptr<media::NewSessionCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->LoadSession(
session_type, session_id,
base::BindOnce(&ContentDecryptionModuleAdapter::OnSessionPromiseResult,
base::Unretained(this), promise_id));
}
void ContentDecryptionModuleAdapter::UpdateSession(
const std::string& session_id,
const std::vector<uint8_t>& response,
std::unique_ptr<media::SimpleCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->UpdateSession(
session_id, response,
base::BindOnce(&ContentDecryptionModuleAdapter::OnSimplePromiseResult,
base::Unretained(this), promise_id));
}
void ContentDecryptionModuleAdapter::CloseSession(
const std::string& session_id,
std::unique_ptr<media::SimpleCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->CloseSession(
session_id,
base::BindOnce(&ContentDecryptionModuleAdapter::OnSimplePromiseResult,
base::Unretained(this), promise_id));
}
void ContentDecryptionModuleAdapter::RemoveSession(
const std::string& session_id,
std::unique_ptr<media::SimpleCdmPromise> promise) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
if (!cros_cdm_remote_) {
RejectPromiseConnectionLost(std::move(promise));
return;
}
uint32_t promise_id = cdm_promise_adapter_.SavePromise(std::move(promise));
cros_cdm_remote_->RemoveSession(
session_id,
base::BindOnce(&ContentDecryptionModuleAdapter::OnSimplePromiseResult,
base::Unretained(this), promise_id));
}
media::CdmContext* ContentDecryptionModuleAdapter::GetCdmContext() {
return this;
}
void ContentDecryptionModuleAdapter::DeleteOnCorrectThread() const {
DVLOG(1) << __func__;
if (!mojo_task_runner_->RunsTasksInCurrentSequence()) {
// When DeleteSoon returns false, |this| will be leaked, which is okay.
mojo_task_runner_->DeleteSoon(FROM_HERE, this);
} else {
delete this;
}
}
std::unique_ptr<media::CallbackRegistration>
ContentDecryptionModuleAdapter::RegisterEventCB(EventCB event_cb) {
return event_callbacks_.Register(std::move(event_cb));
}
media::Decryptor* ContentDecryptionModuleAdapter::GetDecryptor() {
return this;
}
ChromeOsCdmContext* ContentDecryptionModuleAdapter::GetChromeOsCdmContext() {
return this;
}
void ContentDecryptionModuleAdapter::GetHwKeyData(
const media::DecryptConfig* decrypt_config,
const std::vector<uint8_t>& hw_identifier,
GetHwKeyDataCB callback) {
// Take the fields we want out of the |decrypt_config| in case the pointer
// becomes invalid when we are re-posting the task.
GetHwKeyDataInternal(decrypt_config->Clone(), hw_identifier,
std::move(callback));
}
void ContentDecryptionModuleAdapter::GetHwKeyDataInternal(
std::unique_ptr<media::DecryptConfig> decrypt_config,
const std::vector<uint8_t>& hw_identifier,
GetHwKeyDataCB callback) {
// This can get called from decoder threads or mojo threads, so we may need
// to repost the task.
if (!mojo_task_runner_->RunsTasksInCurrentSequence()) {
mojo_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ContentDecryptionModuleAdapter::GetHwKeyDataInternal,
weak_factory_.GetWeakPtr(), std::move(decrypt_config),
hw_identifier, std::move(callback)));
return;
}
if (!cros_cdm_remote_) {
std::move(callback).Run(media::Decryptor::Status::kError,
std::vector<uint8_t>());
return;
}
cros_cdm_remote_->GetHwKeyData(std::move(decrypt_config), hw_identifier,
std::move(callback));
}
void ContentDecryptionModuleAdapter::GetHwConfigData(
GetHwConfigDataCB callback) {
ChromeOsCdmFactory::GetHwConfigData(std::move(callback));
}
void ContentDecryptionModuleAdapter::GetScreenResolutions(
GetScreenResolutionsCB callback) {
ChromeOsCdmFactory::GetScreenResolutions(std::move(callback));
}
std::unique_ptr<media::CdmContextRef>
ContentDecryptionModuleAdapter::GetCdmContextRef() {
return std::make_unique<media::CdmContextRefImpl>(base::WrapRefCounted(this));
}
bool ContentDecryptionModuleAdapter::UsingArcCdm() const {
return false;
}
bool ContentDecryptionModuleAdapter::IsRemoteCdm() const {
return false;
}
void ContentDecryptionModuleAdapter::AllocateSecureBuffer(
uint32_t size,
AllocateSecureBufferCB callback) {
ChromeOsCdmFactory::AllocateSecureBuffer(size, std::move(callback));
}
void ContentDecryptionModuleAdapter::ParseEncryptedSliceHeader(
uint64_t secure_handle,
uint32_t offset,
const std::vector<uint8_t>& stream_data,
ParseEncryptedSliceHeaderCB callback) {
ChromeOsCdmFactory::ParseEncryptedSliceHeader(
secure_handle, offset, stream_data, std::move(callback));
}
void ContentDecryptionModuleAdapter::OnSessionMessage(
const std::string& session_id,
media::CdmMessageType message_type,
const std::vector<uint8_t>& message) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
session_message_cb_.Run(session_id, message_type, message);
}
void ContentDecryptionModuleAdapter::OnSessionClosed(
const std::string& session_id) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
cdm_session_tracker_.RemoveSession(session_id);
// TODO(crbug.com/40181810): Update cdm::mojom::ContentDecryptionModuleClient
// to support CdmSessionClosedReason.
session_closed_cb_.Run(session_id, media::CdmSessionClosedReason::kClose);
}
void ContentDecryptionModuleAdapter::OnSessionKeysChange(
const std::string& session_id,
bool has_additional_usable_key,
std::vector<std::unique_ptr<media::CdmKeyInformation>> keys_info) {
DVLOG(2) << __func__
<< " has_additional_usable_key: " << has_additional_usable_key;
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (has_additional_usable_key)
event_callbacks_.Notify(Event::kHasAdditionalUsableKey);
session_keys_change_cb_.Run(session_id, has_additional_usable_key,
std::move(keys_info));
}
void ContentDecryptionModuleAdapter::OnSessionExpirationUpdate(
const std::string& session_id,
double new_expiry_time_sec) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
session_expiration_update_cb_.Run(
session_id, base::Time::FromSecondsSinceUnixEpoch(new_expiry_time_sec));
}
void ContentDecryptionModuleAdapter::Decrypt(
StreamType stream_type,
scoped_refptr<media::DecoderBuffer> encrypted,
DecryptCB decrypt_cb) {
// This can get called from decoder threads or mojo threads, so we may need
// to repost the task.
if (!mojo_task_runner_->RunsTasksInCurrentSequence()) {
mojo_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&ContentDecryptionModuleAdapter::Decrypt,
weak_factory_.GetWeakPtr(), stream_type,
encrypted, std::move(decrypt_cb)));
return;
}
DVLOG(2) << __func__ << ": " << encrypted->AsHumanReadableString(true);
if (!cros_cdm_remote_) {
std::move(decrypt_cb).Run(media::Decryptor::kError, nullptr);
return;
}
const media::DecryptConfig* decrypt_config = encrypted->decrypt_config();
if (!encrypted->decrypt_config()) {
// We still want to send this to the decryptor even if it is not encrypted
// because we need that for tracking video on AMD of the clear headers. This
// will not be inefficient in other cases because we won't be invoked for
// clear content otherwise.
DCHECK_EQ(stream_type, Decryptor::kVideo);
cros_cdm_remote_->Decrypt(
std::vector<uint8_t>(encrypted->begin(), encrypted->end()), nullptr,
true,
encrypted->side_data() ? encrypted->side_data()->secure_handle : 0,
base::BindOnce(&ContentDecryptionModuleAdapter::OnDecrypt,
base::Unretained(this), stream_type, encrypted,
std::move(decrypt_cb)));
return;
}
// Subsampling will be undone in the daemon itself, don't undo it here. We
// need the clear samples as well on AMD.
const std::vector<media::SubsampleEntry>& subsamples =
decrypt_config->subsamples();
if (!subsamples.empty() &&
!VerifySubsamplesMatchSize(subsamples, encrypted->size())) {
LOG(ERROR) << "Subsample sizes do not match input size";
std::move(decrypt_cb).Run(kError, nullptr);
return;
}
// TODO(jkardatzke): Evaluate the performance cost here of copying the data
// and see if want to use something like MojoDecoderBufferWriter instead.
cros_cdm_remote_->Decrypt(
std::vector<uint8_t>(encrypted->begin(), encrypted->end()),
decrypt_config->Clone(), stream_type == Decryptor::kVideo,
encrypted->side_data() ? encrypted->side_data()->secure_handle : 0,
base::BindOnce(&ContentDecryptionModuleAdapter::OnDecrypt,
base::Unretained(this), stream_type, encrypted,
std::move(decrypt_cb)));
}
void ContentDecryptionModuleAdapter::CancelDecrypt(StreamType stream_type) {
// This method is racey since decryption is on another thread, so don't do
// anything special for cancellation since the caller needs to handle the case
// where the normal callback occurs even after calling CancelDecrypt anyways.
}
void ContentDecryptionModuleAdapter::InitializeAudioDecoder(
const media::AudioDecoderConfig& config,
DecoderInitCB init_cb) {
// ContentDecryptionModuleAdapter does not support audio decoding.
std::move(init_cb).Run(false);
}
void ContentDecryptionModuleAdapter::InitializeVideoDecoder(
const media::VideoDecoderConfig& config,
DecoderInitCB init_cb) {
// ContentDecryptionModuleAdapter does not support video decoding.
std::move(init_cb).Run(false);
}
void ContentDecryptionModuleAdapter::DecryptAndDecodeAudio(
scoped_refptr<media::DecoderBuffer> encrypted,
AudioDecodeCB audio_decode_cb) {
NOTREACHED()
<< "ContentDecryptionModuleAdapter does not support audio decoding";
}
void ContentDecryptionModuleAdapter::DecryptAndDecodeVideo(
scoped_refptr<media::DecoderBuffer> encrypted,
VideoDecodeCB video_decode_cb) {
NOTREACHED()
<< "ContentDecryptionModuleAdapter does not support video decoding";
}
void ContentDecryptionModuleAdapter::ResetDecoder(StreamType stream_type) {
NOTREACHED() << "ContentDecryptionModuleAdapter does not support decoding";
}
void ContentDecryptionModuleAdapter::DeinitializeDecoder(
StreamType stream_type) {
// We do not support audio/video decoding, but since this can be called any
// time after InitializeAudioDecoder/InitializeVideoDecoder, nothing to be
// done here.
}
bool ContentDecryptionModuleAdapter::CanAlwaysDecrypt() {
return false;
}
ContentDecryptionModuleAdapter::~ContentDecryptionModuleAdapter() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(2) << __func__;
cdm_session_tracker_.CloseRemainingSessions(
session_closed_cb_, media::CdmSessionClosedReason::kInternalError);
}
void ContentDecryptionModuleAdapter::OnConnectionError() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DVLOG(1) << __func__;
// The mojo connection doesn't manage our lifecycle, that's handled by the
// owner of the media::ContentDecryptionModule implementation we provide; so
// only drop the bindings and notify the other end about all of the closed
// sessions; don't destruct here.
cros_client_receiver_.reset();
cros_cdm_remote_.reset();
// We've lost our communication, so reject all outstanding promises and close
// any open sessions.
cdm_promise_adapter_.Clear(
media::CdmPromiseAdapter::ClearReason::kConnectionError);
cdm_session_tracker_.CloseRemainingSessions(
session_closed_cb_, media::CdmSessionClosedReason::kInternalError);
}
void ContentDecryptionModuleAdapter::RejectTrackedPromise(
uint32_t promise_id,
cdm::mojom::CdmPromiseResultPtr promise_result) {
ReportSystemCodeUMA(promise_result->system_code);
cdm_promise_adapter_.RejectPromise(promise_id, promise_result->exception,
promise_result->system_code,
promise_result->error_message);
}
void ContentDecryptionModuleAdapter::OnSimplePromiseResult(
uint32_t promise_id,
cdm::mojom::CdmPromiseResultPtr promise_result) {
DVLOG(1) << __func__ << " received result: " << promise_result->success;
if (!promise_result->success) {
RejectTrackedPromise(promise_id, std::move(promise_result));
return;
}
cdm_promise_adapter_.ResolvePromise(promise_id);
}
void ContentDecryptionModuleAdapter::OnGetStatusForPolicy(
uint32_t promise_id,
cdm::mojom::CdmPromiseResultPtr promise_result,
media::CdmKeyInformation::KeyStatus key_status) {
if (!promise_result->success) {
RejectTrackedPromise(promise_id, std::move(promise_result));
return;
}
cdm_promise_adapter_.ResolvePromise(promise_id, key_status);
}
void ContentDecryptionModuleAdapter::OnSessionPromiseResult(
uint32_t promise_id,
cdm::mojom::CdmPromiseResultPtr promise_result,
const std::string& session_id) {
DVLOG(1) << __func__ << " received result: " << promise_result->success
<< " session: " << session_id;
if (!promise_result->success) {
RejectTrackedPromise(promise_id, std::move(promise_result));
return;
}
cdm_session_tracker_.AddSession(session_id);
cdm_promise_adapter_.ResolvePromise(promise_id, session_id);
}
void ContentDecryptionModuleAdapter::OnDecrypt(
StreamType stream_type,
scoped_refptr<media::DecoderBuffer> encrypted,
media::Decryptor::DecryptCB decrypt_cb,
media::Decryptor::Status status,
const std::vector<uint8_t>& decrypted_data,
std::unique_ptr<media::DecryptConfig> decrypt_config_out) {
if (status != media::Decryptor::kSuccess) {
if (status == media::Decryptor::kNoKey) {
DVLOG(1) << "Decryption failed due to no key";
} else {
LOG(ERROR) << "Failure decrypting data: " << status;
}
std::move(decrypt_cb).Run(status, nullptr);
return;
}
// If we decrypted to secure memory, then just send the original buffer back
// because the result is stored in the secure world.
if (encrypted->side_data() && encrypted->side_data()->secure_handle) {
std::move(decrypt_cb).Run(media::Decryptor::kSuccess, std::move(encrypted));
return;
}
scoped_refptr<media::DecoderBuffer> decrypted =
media::DecoderBuffer::CopyFrom(decrypted_data);
// Copy the auxiliary fields.
decrypted->set_timestamp(encrypted->timestamp());
decrypted->set_duration(encrypted->duration());
decrypted->set_is_key_frame(encrypted->is_key_frame());
if (encrypted->side_data()) {
decrypted->set_side_data(encrypted->side_data()->Clone());
}
if (decrypt_config_out)
decrypted->set_decrypt_config(std::move(decrypt_config_out));
std::move(decrypt_cb).Run(media::Decryptor::kSuccess, std::move(decrypted));
}
} // namespace chromeos
|