Avi Drissman | d387f092 | 2022-09-14 20:51:31 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 2 | // 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 | |
Avi Drissman | d70f89a | 2023-01-11 23:52:55 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback_helpers.h" |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 12 | #include "base/logging.h" |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 14 | #include "base/task/sequenced_task_runner.h" |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 15 | #include "media/base/cdm_context.h" |
| 16 | #include "media/base/demuxer_stream.h" |
| 17 | #include "media/base/media_log.h" |
| 18 | #include "media/base/pipeline_status.h" |
| 19 | #include "media/filters/decrypting_demuxer_stream.h" |
| 20 | |
| 21 | namespace media { |
| 22 | |
| 23 | DecryptingMediaResource::DecryptingMediaResource( |
| 24 | MediaResource* media_resource, |
| 25 | CdmContext* cdm_context, |
| 26 | MediaLog* media_log, |
Frank Liberato | a36c4bb | 2020-11-06 23:33:04 | [diff] [blame] | 27 | scoped_refptr<base::SequencedTaskRunner> task_runner) |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 28 | : media_resource_(media_resource), |
| 29 | cdm_context_(cdm_context), |
| 30 | media_log_(media_log), |
Jeremy Roman | 3294839 | 2019-07-09 18:34:37 | [diff] [blame] | 31 | task_runner_(task_runner) { |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 32 | DCHECK(media_resource); |
Rahul Yadav | 2e767f6 | 2023-08-09 17:32:04 | [diff] [blame] | 33 | DCHECK_EQ(MediaResource::Type::kStream, media_resource->GetType()); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 34 | DCHECK(cdm_context_); |
| 35 | DCHECK(cdm_context_->GetDecryptor()); |
| 36 | DCHECK(cdm_context_->GetDecryptor()->CanAlwaysDecrypt()); |
| 37 | DCHECK(media_log_); |
Frank Liberato | a36c4bb | 2020-11-06 23:33:04 | [diff] [blame] | 38 | DCHECK(task_runner->RunsTasksInCurrentSequence()); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | DecryptingMediaResource::~DecryptingMediaResource() = default; |
| 42 | |
| 43 | MediaResource::Type DecryptingMediaResource::GetType() const { |
Rahul Yadav | 2e767f6 | 2023-08-09 17:32:04 | [diff] [blame] | 44 | DCHECK_EQ(MediaResource::Type::kStream, media_resource_->GetType()); |
| 45 | return MediaResource::Type::kStream; |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 46 | } |
| 47 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 48 | std::vector<raw_ptr<DemuxerStream, VectorExperimental>> |
| 49 | DecryptingMediaResource::GetAllStreams() { |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 50 | if (streams_.size()) |
| 51 | return streams_; |
| 52 | |
| 53 | return media_resource_->GetAllStreams(); |
| 54 | } |
| 55 | |
Chad Duffin | ed9a55e | 2019-01-08 03:19:13 | [diff] [blame] | 56 | void DecryptingMediaResource::Initialize(InitCB init_cb, WaitingCB waiting_cb) { |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 57 | DCHECK(init_cb); |
| 58 | |
| 59 | auto streams = media_resource_->GetAllStreams(); |
| 60 | |
| 61 | // Save the callback so that we can invoke it when the |
| 62 | // DecryptingDemuxerStreams have finished initialization. |
| 63 | init_cb_ = std::move(init_cb); |
| 64 | num_dds_pending_init_ = streams.size(); |
| 65 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 66 | for (media::DemuxerStream* stream : streams) { |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 67 | auto decrypting_demuxer_stream = std::make_unique<DecryptingDemuxerStream>( |
Chad Duffin | ed9a55e | 2019-01-08 03:19:13 | [diff] [blame] | 68 | task_runner_, media_log_, waiting_cb); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 69 | |
| 70 | // DecryptingDemuxerStream always invokes the callback asynchronously so |
| 71 | // that we have no reentrancy issues. "All public APIs and callbacks are |
| 72 | // trampolined to the |task_runner_|." |
| 73 | decrypting_demuxer_stream->Initialize( |
| 74 | stream, cdm_context_, |
Jose Lopes | 25ba7b3 | 2020-03-12 16:01:27 | [diff] [blame] | 75 | base::BindOnce(&DecryptingMediaResource::OnDecryptingDemuxerInitialized, |
| 76 | weak_factory_.GetWeakPtr())); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 77 | |
| 78 | streams_.push_back(decrypting_demuxer_stream.get()); |
| 79 | owned_streams_.push_back(std::move(decrypting_demuxer_stream)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | int DecryptingMediaResource::DecryptingDemuxerStreamCountForTesting() const { |
| 84 | return owned_streams_.size(); |
| 85 | } |
| 86 | |
| 87 | void DecryptingMediaResource::OnDecryptingDemuxerInitialized( |
| 88 | PipelineStatus status) { |
| 89 | DVLOG(2) << __func__ << ": DecryptingDemuxerStream initialization ended " |
Xiaohan Wang | 866b0cd | 2019-10-31 16:57:51 | [diff] [blame] | 90 | << "with the status: " << status; |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 91 | |
| 92 | // Decrement the count of DecryptingDemuxerStreams that need to be |
| 93 | // initialized. |
| 94 | --num_dds_pending_init_; |
| 95 | |
| 96 | if (!init_cb_) |
| 97 | return; |
| 98 | |
| 99 | if (status != PIPELINE_OK) |
| 100 | std::move(init_cb_).Run(false); |
| 101 | else if (num_dds_pending_init_ == 0) |
| 102 | std::move(init_cb_).Run(true); |
| 103 | } |
| 104 | |
| 105 | } // namespace media |