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 | |
| 10 | #include "base/bind.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 12 | #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 | |
| 19 | namespace media { |
| 20 | |
| 21 | DecryptingMediaResource::DecryptingMediaResource( |
| 22 | MediaResource* media_resource, |
| 23 | CdmContext* cdm_context, |
| 24 | MediaLog* media_log, |
Frank Liberato | a36c4bb | 2020-11-06 23:33:04 | [diff] [blame] | 25 | scoped_refptr<base::SequencedTaskRunner> task_runner) |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 26 | : media_resource_(media_resource), |
| 27 | cdm_context_(cdm_context), |
| 28 | media_log_(media_log), |
Jeremy Roman | 3294839 | 2019-07-09 18:34:37 | [diff] [blame] | 29 | task_runner_(task_runner) { |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 30 | DCHECK(media_resource); |
Chad Duffin | a25aac8 | 2019-01-09 17:09:11 | [diff] [blame] | 31 | DCHECK_EQ(MediaResource::STREAM, media_resource->GetType()); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 32 | DCHECK(cdm_context_); |
| 33 | DCHECK(cdm_context_->GetDecryptor()); |
| 34 | DCHECK(cdm_context_->GetDecryptor()->CanAlwaysDecrypt()); |
| 35 | DCHECK(media_log_); |
Frank Liberato | a36c4bb | 2020-11-06 23:33:04 | [diff] [blame] | 36 | DCHECK(task_runner->RunsTasksInCurrentSequence()); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | DecryptingMediaResource::~DecryptingMediaResource() = default; |
| 40 | |
| 41 | MediaResource::Type DecryptingMediaResource::GetType() const { |
Xiaohan Wang | 20f3e2e | 2019-01-11 01:22:57 | [diff] [blame] | 42 | DCHECK_EQ(MediaResource::STREAM, media_resource_->GetType()); |
| 43 | return MediaResource::STREAM; |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | std::vector<DemuxerStream*> DecryptingMediaResource::GetAllStreams() { |
| 47 | if (streams_.size()) |
| 48 | return streams_; |
| 49 | |
| 50 | return media_resource_->GetAllStreams(); |
| 51 | } |
| 52 | |
Chad Duffin | ed9a55e | 2019-01-08 03:19:13 | [diff] [blame] | 53 | void DecryptingMediaResource::Initialize(InitCB init_cb, WaitingCB waiting_cb) { |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 54 | 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 Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 64 | auto decrypting_demuxer_stream = std::make_unique<DecryptingDemuxerStream>( |
Chad Duffin | ed9a55e | 2019-01-08 03:19:13 | [diff] [blame] | 65 | task_runner_, media_log_, waiting_cb); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 66 | |
| 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 Lopes | 25ba7b3 | 2020-03-12 16:01:27 | [diff] [blame] | 72 | base::BindOnce(&DecryptingMediaResource::OnDecryptingDemuxerInitialized, |
| 73 | weak_factory_.GetWeakPtr())); |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 74 | |
| 75 | streams_.push_back(decrypting_demuxer_stream.get()); |
| 76 | owned_streams_.push_back(std::move(decrypting_demuxer_stream)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | int DecryptingMediaResource::DecryptingDemuxerStreamCountForTesting() const { |
| 81 | return owned_streams_.size(); |
| 82 | } |
| 83 | |
| 84 | void DecryptingMediaResource::OnDecryptingDemuxerInitialized( |
| 85 | PipelineStatus status) { |
| 86 | DVLOG(2) << __func__ << ": DecryptingDemuxerStream initialization ended " |
Xiaohan Wang | 866b0cd | 2019-10-31 16:57:51 | [diff] [blame] | 87 | << "with the status: " << status; |
Chad Duffin | 9062d33 | 2019-01-04 21:51:30 | [diff] [blame] | 88 | |
| 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 |