[go: nahoru, domu]

blob: 3501ea2f0a67973d97d9dc78329e3bb1e508380e [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"
13#include "media/base/cdm_context.h"
14#include "media/base/demuxer_stream.h"
15#include "media/base/media_log.h"
16#include "media/base/pipeline_status.h"
17#include "media/filters/decrypting_demuxer_stream.h"
18
19namespace media {
20
21DecryptingMediaResource::DecryptingMediaResource(
22 MediaResource* media_resource,
23 CdmContext* cdm_context,
24 MediaLog* media_log,
Frank Liberatoa36c4bb2020-11-06 23:33:0425 scoped_refptr<base::SequencedTaskRunner> task_runner)
Chad Duffin9062d332019-01-04 21:51:3026 : media_resource_(media_resource),
27 cdm_context_(cdm_context),
28 media_log_(media_log),
Jeremy Roman32948392019-07-09 18:34:3729 task_runner_(task_runner) {
Chad Duffin9062d332019-01-04 21:51:3030 DCHECK(media_resource);
Chad Duffina25aac82019-01-09 17:09:1131 DCHECK_EQ(MediaResource::STREAM, media_resource->GetType());
Chad Duffin9062d332019-01-04 21:51:3032 DCHECK(cdm_context_);
33 DCHECK(cdm_context_->GetDecryptor());
34 DCHECK(cdm_context_->GetDecryptor()->CanAlwaysDecrypt());
35 DCHECK(media_log_);
Frank Liberatoa36c4bb2020-11-06 23:33:0436 DCHECK(task_runner->RunsTasksInCurrentSequence());
Chad Duffin9062d332019-01-04 21:51:3037}
38
39DecryptingMediaResource::~DecryptingMediaResource() = default;
40
41MediaResource::Type DecryptingMediaResource::GetType() const {
Xiaohan Wang20f3e2e2019-01-11 01:22:5742 DCHECK_EQ(MediaResource::STREAM, media_resource_->GetType());
43 return MediaResource::STREAM;
Chad Duffin9062d332019-01-04 21:51:3044}
45
46std::vector<DemuxerStream*> DecryptingMediaResource::GetAllStreams() {
47 if (streams_.size())
48 return streams_;
49
50 return media_resource_->GetAllStreams();
51}
52
Chad Duffined9a55e2019-01-08 03:19:1353void DecryptingMediaResource::Initialize(InitCB init_cb, WaitingCB waiting_cb) {
Chad Duffin9062d332019-01-04 21:51:3054 DCHECK(init_cb);
55
56 auto streams = media_resource_->GetAllStreams();
57
58 // Save the callback so that we can invoke it when the
59 // DecryptingDemuxerStreams have finished initialization.
60 init_cb_ = std::move(init_cb);
61 num_dds_pending_init_ = streams.size();
62
63 for (auto* stream : streams) {
Chad Duffin9062d332019-01-04 21:51:3064 auto decrypting_demuxer_stream = std::make_unique<DecryptingDemuxerStream>(
Chad Duffined9a55e2019-01-08 03:19:1365 task_runner_, media_log_, waiting_cb);
Chad Duffin9062d332019-01-04 21:51:3066
67 // DecryptingDemuxerStream always invokes the callback asynchronously so
68 // that we have no reentrancy issues. "All public APIs and callbacks are
69 // trampolined to the |task_runner_|."
70 decrypting_demuxer_stream->Initialize(
71 stream, cdm_context_,
Jose Lopes25ba7b32020-03-12 16:01:2772 base::BindOnce(&DecryptingMediaResource::OnDecryptingDemuxerInitialized,
73 weak_factory_.GetWeakPtr()));
Chad Duffin9062d332019-01-04 21:51:3074
75 streams_.push_back(decrypting_demuxer_stream.get());
76 owned_streams_.push_back(std::move(decrypting_demuxer_stream));
77 }
78}
79
80int DecryptingMediaResource::DecryptingDemuxerStreamCountForTesting() const {
81 return owned_streams_.size();
82}
83
84void DecryptingMediaResource::OnDecryptingDemuxerInitialized(
85 PipelineStatus status) {
86 DVLOG(2) << __func__ << ": DecryptingDemuxerStream initialization ended "
Xiaohan Wang866b0cd2019-10-31 16:57:5187 << "with the status: " << status;
Chad Duffin9062d332019-01-04 21:51:3088
89 // Decrement the count of DecryptingDemuxerStreams that need to be
90 // initialized.
91 --num_dds_pending_init_;
92
93 if (!init_cb_)
94 return;
95
96 if (status != PIPELINE_OK)
97 std::move(init_cb_).Run(false);
98 else if (num_dds_pending_init_ == 0)
99 std::move(init_cb_).Run(true);
100}
101
102} // namespace media