1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FILTERS_DECRYPTING_MEDIA_RESOURCE_H_
6 #define MEDIA_FILTERS_DECRYPTING_MEDIA_RESOURCE_H_
10 #include "base/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/task/sequenced_task_runner.h"
15 #include "media/base/media_resource.h"
16 #include "media/base/pipeline.h"
22 class DecryptingDemuxerStream;
24 // DecryptingMediaResource is a wrapper for a MediaResource implementation that
25 // provides decryption. It should only be created when:
26 // - The |media_resource| has type MediaResource::STREAM, and
27 // - The |cdm_context| has a Decryptor that always supports decrypt-only.
28 // Internally DecryptingDemuxerStreams will be created for all streams in
29 // |media_resource| and decrypt them into clear streams. These clear streams are
30 // then passed downstream to the rest of the media pipeline, which should no
31 // longer need to worry about decryption.
32 class MEDIA_EXPORT DecryptingMediaResource : public MediaResource {
34 using InitCB = base::OnceCallback<void(bool success)>;
36 DecryptingMediaResource(MediaResource* media_resource,
37 CdmContext* cdm_context,
39 scoped_refptr<base::SequencedTaskRunner> task_runner);
41 DecryptingMediaResource(const DecryptingMediaResource&) = delete;
42 DecryptingMediaResource& operator=(const DecryptingMediaResource&) = delete;
44 ~DecryptingMediaResource() override;
46 // MediaResource implementation:
47 MediaResource::Type GetType() const override;
48 std::vector<DemuxerStream*> GetAllStreams() override;
50 void Initialize(InitCB init_cb, WaitingCB waiting_cb_);
52 // Returns the number of DecryptingDemuxerStreams that were created.
53 virtual int DecryptingDemuxerStreamCountForTesting() const;
56 void OnDecryptingDemuxerInitialized(PipelineStatus status);
58 const raw_ptr<MediaResource> media_resource_;
59 const raw_ptr<CdmContext> cdm_context_;
60 const raw_ptr<MediaLog> media_log_;
61 scoped_refptr<base::SequencedTaskRunner> task_runner_;
63 // Number of DecryptingDemuxerStreams that have yet to be initialized.
64 int num_dds_pending_init_ = 0;
66 // |streams_| is the set of streams that this implementation does not own and
67 // will be returned when GetAllStreams() is invoked. |owned_streams_| is the
68 // set of DecryptingDemuxerStreams that we have created and own (i.e.
69 // responsible for destructing).
70 std::vector<DemuxerStream*> streams_;
71 std::vector<std::unique_ptr<DecryptingDemuxerStream>> owned_streams_;
73 // Called when the final DecryptingDemuxerStream has been initialized *or*
74 // if one of the DecryptingDemuxerStreams failed to initialize correctly.
76 base::WeakPtrFactory<DecryptingMediaResource> weak_factory_{this};
81 #endif // MEDIA_FILTERS_DECRYPTING_MEDIA_RESOURCE_H_