Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / decrypting_media_resource.h
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.
4
5 #ifndef MEDIA_FILTERS_DECRYPTING_MEDIA_RESOURCE_H_
6 #define MEDIA_FILTERS_DECRYPTING_MEDIA_RESOURCE_H_
7
8 #include <vector>
9
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"
17
18 namespace media {
19
20 class CdmContext;
21 class DemuxerStream;
22 class DecryptingDemuxerStream;
23
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 {
33  public:
34   using InitCB = base::OnceCallback<void(bool success)>;
35
36   DecryptingMediaResource(MediaResource* media_resource,
37                           CdmContext* cdm_context,
38                           MediaLog* media_log,
39                           scoped_refptr<base::SequencedTaskRunner> task_runner);
40
41   DecryptingMediaResource(const DecryptingMediaResource&) = delete;
42   DecryptingMediaResource& operator=(const DecryptingMediaResource&) = delete;
43
44   ~DecryptingMediaResource() override;
45
46   // MediaResource implementation:
47   MediaResource::Type GetType() const override;
48   std::vector<DemuxerStream*> GetAllStreams() override;
49
50   void Initialize(InitCB init_cb, WaitingCB waiting_cb_);
51
52   // Returns the number of DecryptingDemuxerStreams that were created.
53   virtual int DecryptingDemuxerStreamCountForTesting() const;
54
55  private:
56   void OnDecryptingDemuxerInitialized(PipelineStatus status);
57
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_;
62
63   // Number of DecryptingDemuxerStreams that have yet to be initialized.
64   int num_dds_pending_init_ = 0;
65
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_;
72
73   // Called when the final DecryptingDemuxerStream has been initialized *or*
74   // if one of the DecryptingDemuxerStreams failed to initialize correctly.
75   InitCB init_cb_;
76   base::WeakPtrFactory<DecryptingMediaResource> weak_factory_{this};
77 };
78
79 }  // namespace media
80
81 #endif  // MEDIA_FILTERS_DECRYPTING_MEDIA_RESOURCE_H_