AbstractMediaDecoder.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef AbstractMediaDecoder_h_
  7. #define AbstractMediaDecoder_h_
  8. #include "mozilla/Attributes.h"
  9. #include "mozilla/StateMirroring.h"
  10. #include "FrameStatistics.h"
  11. #include "MediaEventSource.h"
  12. #include "MediaInfo.h"
  13. #include "nsISupports.h"
  14. #include "nsDataHashtable.h"
  15. #include "nsThreadUtils.h"
  16. class GMPCrashHelper;
  17. namespace mozilla
  18. {
  19. namespace layers
  20. {
  21. class ImageContainer;
  22. class KnowsCompositor;
  23. } // namespace layers
  24. class MediaResource;
  25. class ReentrantMonitor;
  26. class VideoFrameContainer;
  27. class MediaDecoderOwner;
  28. #ifdef MOZ_EME
  29. class CDMProxy;
  30. #endif
  31. typedef nsDataHashtable<nsCStringHashKey, nsCString> MetadataTags;
  32. static inline bool IsCurrentThread(nsIThread* aThread) {
  33. return NS_GetCurrentThread() == aThread;
  34. }
  35. /**
  36. * The AbstractMediaDecoder class describes the public interface for a media decoder
  37. * and is used by the MediaReader classes.
  38. */
  39. class AbstractMediaDecoder : public nsIObserver
  40. {
  41. public:
  42. // A special version of the above for the ogg decoder that is allowed to be
  43. // called cross-thread.
  44. virtual bool IsOggDecoderShutdown() { return false; }
  45. // Get the current MediaResource being used. Its URI will be returned
  46. // by currentSrc. Returns what was passed to Load(), if Load() has been called.
  47. virtual MediaResource* GetResource() const = 0;
  48. // Increments the parsed, decoded and dropped frame counters by the passed in
  49. // counts.
  50. // Can be called on any thread.
  51. virtual void NotifyDecodedFrames(const FrameStatisticsData& aStats) = 0;
  52. virtual AbstractCanonical<media::NullableTimeUnit>* CanonicalDurationOrNull() { return nullptr; };
  53. // Return an event that will be notified when data arrives in MediaResource.
  54. // MediaDecoderReader will register with this event to receive notifications
  55. // in order to update buffer ranges.
  56. // Return null if this decoder doesn't support the event.
  57. virtual MediaEventSource<void>* DataArrivedEvent()
  58. {
  59. return nullptr;
  60. }
  61. // Returns an event that will be notified when the owning document changes state
  62. // and we might have a new compositor. If this new compositor requires us to
  63. // recreate our decoders, then we expect the existing decoderis to return an
  64. // error independently of this.
  65. virtual MediaEventSource<RefPtr<layers::KnowsCompositor>>* CompositorUpdatedEvent()
  66. {
  67. return nullptr;
  68. }
  69. // Notify the media decoder that a decryption key is required before emitting
  70. // further output. This only needs to be overridden for decoders that expect
  71. // encryption, such as the MediaSource decoder.
  72. virtual void NotifyWaitingForKey() {}
  73. // Return an event that will be notified when a decoder is waiting for a
  74. // decryption key before it can return more output.
  75. virtual MediaEventSource<void>* WaitingForKeyEvent()
  76. {
  77. return nullptr;
  78. }
  79. protected:
  80. virtual void UpdateEstimatedMediaDuration(int64_t aDuration) {};
  81. public:
  82. void DispatchUpdateEstimatedMediaDuration(int64_t aDuration)
  83. {
  84. NS_DispatchToMainThread(NewRunnableMethod<int64_t>(this,
  85. &AbstractMediaDecoder::UpdateEstimatedMediaDuration,
  86. aDuration));
  87. }
  88. virtual VideoFrameContainer* GetVideoFrameContainer() = 0;
  89. virtual mozilla::layers::ImageContainer* GetImageContainer() = 0;
  90. // Returns the owner of this decoder or null when the decoder is shutting
  91. // down. The owner should only be used on the main thread.
  92. virtual MediaDecoderOwner* GetOwner() const = 0;
  93. // Set by Reader if the current audio track can be offloaded
  94. virtual void SetPlatformCanOffloadAudio(bool aCanOffloadAudio) {}
  95. virtual already_AddRefed<GMPCrashHelper> GetCrashHelper() { return nullptr; }
  96. // Stack based class to assist in notifying the frame statistics of
  97. // parsed and decoded frames. Use inside video demux & decode functions
  98. // to ensure all parsed and decoded frames are reported on all return paths.
  99. class AutoNotifyDecoded {
  100. public:
  101. explicit AutoNotifyDecoded(AbstractMediaDecoder* aDecoder)
  102. : mDecoder(aDecoder)
  103. {}
  104. ~AutoNotifyDecoded() {
  105. if (mDecoder) {
  106. mDecoder->NotifyDecodedFrames(mStats);
  107. }
  108. }
  109. FrameStatisticsData mStats;
  110. private:
  111. AbstractMediaDecoder* mDecoder;
  112. };
  113. // Classes directly inheriting from AbstractMediaDecoder do not support
  114. // Observe and it should never be called directly.
  115. NS_IMETHOD Observe(nsISupports *aSubject, const char * aTopic, const char16_t * aData) override
  116. { MOZ_CRASH("Forbidden method"); return NS_OK; }
  117. };
  118. } // namespace mozilla
  119. #endif