[go: nahoru, domu]

blob: 4d1621ff00a3d55e982a5b5f270bb81135bd4ff1 [file] [log] [blame]
Avi Drissmand387f0922022-09-14 20:51:311// Copyright 2018 The Chromium Authors
Chad Duffin9062d332019-01-04 21:51:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "media/filters/decrypting_media_resource.h"
6
7#include <memory>
8#include <utility>
9
10#include "base/bind.h"
danakjdb9ae7942020-11-11 16:01:3511#include "base/callback_helpers.h"
Chad Duffin9062d332019-01-04 21:51:3012#include "base/logging.h"
Sean Mahere672a662023-01-09 21:42:2813#include "base/task/sequenced_task_runner.h"
Chad Duffin9062d332019-01-04 21:51:3014#include "media/base/cdm_context.h"
15#include "media/base/demuxer_stream.h"
16#include "media/base/media_log.h"
17#include "media/base/pipeline_status.h"
18#include "media/filters/decrypting_demuxer_stream.h"
19
20namespace media {
21
22DecryptingMediaResource::DecryptingMediaResource(
23 MediaResource* media_resource,
24 CdmContext* cdm_context,
25 MediaLog* media_log,
Frank Liberatoa36c4bb2020-11-06 23:33:0426 scoped_refptr<base::SequencedTaskRunner> task_runner)
Chad Duffin9062d332019-01-04 21:51:3027 : media_resource_(media_resource),
28 cdm_context_(cdm_context),
29 media_log_(media_log),
Jeremy Roman32948392019-07-09 18:34:3730 task_runner_(task_runner) {
Chad Duffin9062d332019-01-04 21:51:3031 DCHECK(media_resource);
Chad Duffina25aac82019-01-09 17:09:1132 DCHECK_EQ(MediaResource::STREAM, media_resource->GetType());
Chad Duffin9062d332019-01-04 21:51:3033 DCHECK(cdm_context_);
34 DCHECK(cdm_context_->GetDecryptor());
35 DCHECK(cdm_context_->GetDecryptor()->CanAlwaysDecrypt());
36 DCHECK(media_log_);
Frank Liberatoa36c4bb2020-11-06 23:33:0437 DCHECK(task_runner->RunsTasksInCurrentSequence());
Chad Duffin9062d332019-01-04 21:51:3038}
39
40DecryptingMediaResource::~DecryptingMediaResource() = default;
41
42MediaResource::Type DecryptingMediaResource::GetType() const {
Xiaohan Wang20f3e2e2019-01-11 01:22:5743 DCHECK_EQ(MediaResource::STREAM, media_resource_->GetType());
44 return MediaResource::STREAM;
Chad Duffin9062d332019-01-04 21:51:3045}
46
47std::vector<DemuxerStream*> DecryptingMediaResource::GetAllStreams() {
48 if (streams_.size())
49 return streams_;
50
51 return media_resource_->GetAllStreams();
52}
53
Chad Duffined9a55e2019-01-08 03:19:1354void DecryptingMediaResource::Initialize(InitCB init_cb, WaitingCB waiting_cb) {
Chad Duffin9062d332019-01-04 21:51:3055 DCHECK(init_cb);
56
57 auto streams = media_resource_->GetAllStreams();
58
59 // Save the callback so that we can invoke it when the
60 // DecryptingDemuxerStreams have finished initialization.
61 init_cb_ = std::move(init_cb);
62 num_dds_pending_init_ = streams.size();
63
64 for (auto* stream : streams) {
Chad Duffin9062d332019-01-04 21:51:3065 auto decrypting_demuxer_stream = std::make_unique<DecryptingDemuxerStream>(
Chad Duffined9a55e2019-01-08 03:19:1366 task_runner_, media_log_, waiting_cb);
Chad Duffin9062d332019-01-04 21:51:3067
68 // DecryptingDemuxerStream always invokes the callback asynchronously so
69 // that we have no reentrancy issues. "All public APIs and callbacks are
70 // trampolined to the |task_runner_|."
71 decrypting_demuxer_stream->Initialize(
72 stream, cdm_context_,
Jose Lopes25ba7b32020-03-12 16:01:2773 base::BindOnce(&DecryptingMediaResource::OnDecryptingDemuxerInitialized,
74 weak_factory_.GetWeakPtr()));
Chad Duffin9062d332019-01-04 21:51:3075
76 streams_.push_back(decrypting_demuxer_stream.get());
77 owned_streams_.push_back(std::move(decrypting_demuxer_stream));
78 }
79}
80
81int DecryptingMediaResource::DecryptingDemuxerStreamCountForTesting() const {
82 return owned_streams_.size();
83}
84
85void DecryptingMediaResource::OnDecryptingDemuxerInitialized(
86 PipelineStatus status) {
87 DVLOG(2) << __func__ << ": DecryptingDemuxerStream initialization ended "
Xiaohan Wang866b0cd2019-10-31 16:57:5188 << "with the status: " << status;
Chad Duffin9062d332019-01-04 21:51:3089
90 // Decrement the count of DecryptingDemuxerStreams that need to be
91 // initialized.
92 --num_dds_pending_init_;
93
94 if (!init_cb_)
95 return;
96
97 if (status != PIPELINE_OK)
98 std::move(init_cb_).Run(false);
99 else if (num_dds_pending_init_ == 0)
100 std::move(init_cb_).Run(true);
101}
102
103} // namespace media