decrypting_media_resource.h 3.1 KB

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