MFMediaSource.h (6591B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef DOM_MEDIA_PLATFORM_WMF_MFMEDIASOURCE_H 6 #define DOM_MEDIA_PLATFORM_WMF_MFMEDIASOURCE_H 7 8 #include <mfidl.h> 9 #include <wrl.h> 10 11 #include "MFMediaEngineExtra.h" 12 #include "MFMediaEngineStream.h" 13 #include "MediaEventSource.h" 14 #include "MediaInfo.h" 15 #include "mozilla/TaskQueue.h" 16 17 namespace mozilla { 18 19 class MFCDMProxy; 20 21 // An event to indicate a need for a certain type of sample. 22 struct SampleRequest { 23 SampleRequest(TrackInfo::TrackType aType, bool aIsEnough) 24 : mType(aType), mIsEnough(aIsEnough) {} 25 TrackInfo::TrackType mType; 26 bool mIsEnough; 27 }; 28 29 /** 30 * MFMediaSource is a custom source for the media engine, the media engine would 31 * ask the source for the characteristics and the presentation descriptor to 32 * know how to react with the source. This source is also responsible to 33 * dispatch events to the media engine to notify the status changes. 34 * 35 * https://docs.microsoft.com/en-us/windows/win32/api/mfidl/nn-mfidl-imfmediasource 36 */ 37 class MFMediaSource : public Microsoft::WRL::RuntimeClass< 38 Microsoft::WRL::RuntimeClassFlags< 39 Microsoft::WRL::RuntimeClassType::ClassicCom>, 40 IMFMediaSource, IMFRateControl, IMFRateSupport, 41 IMFGetService, IMFTrustedInput> { 42 public: 43 MFMediaSource(); 44 ~MFMediaSource(); 45 46 HRESULT RuntimeClassInitialize(const Maybe<AudioInfo>& aAudio, 47 const Maybe<VideoInfo>& aVideo, 48 nsISerialEventTarget* aManagerThread, 49 bool aIsEncryptedCustomInit); 50 51 // Methods for IMFMediaSource 52 IFACEMETHODIMP GetCharacteristics(DWORD* aCharacteristics) override; 53 IFACEMETHODIMP CreatePresentationDescriptor( 54 IMFPresentationDescriptor** aPresentationDescriptor) override; 55 IFACEMETHODIMP Start(IMFPresentationDescriptor* aPresentationDescriptor, 56 const GUID* aGuidTimeFormat, 57 const PROPVARIANT* aStartPosition) override; 58 IFACEMETHODIMP Stop() override; 59 IFACEMETHODIMP Pause() override; 60 IFACEMETHODIMP Shutdown() override; 61 62 // Methods for IMFMediaEventGenerator, IMFMediaSource derives from 63 // IMFMediaEventGenerator. 64 IFACEMETHODIMP GetEvent(DWORD aFlags, IMFMediaEvent** aEvent) override; 65 IFACEMETHODIMP BeginGetEvent(IMFAsyncCallback* aCallback, 66 IUnknown* aState) override; 67 IFACEMETHODIMP EndGetEvent(IMFAsyncResult* aResult, 68 IMFMediaEvent** aEvent) override; 69 IFACEMETHODIMP QueueEvent(MediaEventType aType, REFGUID aExtendedType, 70 HRESULT aStatus, 71 const PROPVARIANT* aValue) override; 72 73 // IMFGetService 74 IFACEMETHODIMP GetService(REFGUID aGuidService, REFIID aRiid, 75 LPVOID* aResult) override; 76 77 // IMFRateSupport 78 IFACEMETHODIMP GetSlowestRate(MFRATE_DIRECTION aDirection, 79 BOOL aSupportsThinning, float* aRate) override; 80 IFACEMETHODIMP GetFastestRate(MFRATE_DIRECTION aDirection, 81 BOOL aSupportsThinning, float* aRate) override; 82 IFACEMETHODIMP IsRateSupported(BOOL aSupportsThinning, float aNewRate, 83 float* aSupportedRate) override; 84 85 // IMFRateControl 86 IFACEMETHODIMP SetRate(BOOL aSupportsThinning, float aRate) override; 87 IFACEMETHODIMP GetRate(BOOL* aSupportsThinning, float* aRate) override; 88 89 // IMFTrustedInput 90 IFACEMETHODIMP GetInputTrustAuthority(DWORD aStreamId, REFIID aRiid, 91 IUnknown** aITAOut) override; 92 93 MFMediaEngineStream* GetAudioStream(); 94 MFMediaEngineStream* GetVideoStream(); 95 96 MFMediaEngineStream* GetStreamByIndentifier(DWORD aStreamId) const; 97 98 #ifdef MOZ_WMF_CDM 99 void SetCDMProxy(MFCDMProxy* aCDMProxy); 100 #endif 101 102 TaskQueue* GetTaskQueue() const { return mTaskQueue; } 103 104 MediaEventSource<SampleRequest>& RequestSampleEvent() { 105 return mRequestSampleEvent; 106 } 107 108 // Called from the content process to notify that no more encoded data in that 109 // type of track. 110 void NotifyEndOfStream(TrackInfo::TrackType aType); 111 112 // Called from the MF stream to indicate that the stream has provided last 113 // encoded sample to the media engine. 114 void HandleStreamEnded(TrackInfo::TrackType aType); 115 116 enum class State { 117 Initialized, 118 Started, 119 Stopped, 120 Paused, 121 Shutdowned, 122 }; 123 State GetState() const; 124 125 void SetDCompSurfaceHandle(HANDLE aDCompSurfaceHandle, gfx::IntSize aDisplay); 126 127 void ShutdownTaskQueue(); 128 129 bool IsEncrypted() const; 130 131 private: 132 void AssertOnManagerThread() const; 133 void AssertOnMFThreadPool() const; 134 135 bool IsSeekable() const; 136 137 // A thread-safe event queue. 138 // https://docs.microsoft.com/en-us/windows/win32/medfound/media-event-generators#implementing-imfmediaeventgenerator 139 Microsoft::WRL::ComPtr<IMFMediaEventQueue> mMediaEventQueue; 140 141 // The thread used to run the engine streams' tasks. 142 RefPtr<TaskQueue> mTaskQueue; 143 144 // The thread used to run the media source's tasks. 145 RefPtr<nsISerialEventTarget> mManagerThread; 146 147 // MFMediaEngineStream will notify us when we need more sample. 148 friend class MFMediaEngineStream; 149 MediaEventProducer<SampleRequest> mRequestSampleEvent; 150 151 MediaEventListener mAudioStreamEndedListener; 152 MediaEventListener mVideoStreamEndedListener; 153 154 // This class would be run/accessed on two threads, MF thread pool and the 155 // manager thread. Following members could be used across threads so they need 156 // to be thread-safe. 157 158 mutable Mutex mMutex{"MFMediaEngineSource"}; 159 160 // True if the playback is ended. Use and modify on both the manager thread 161 // and MF thread pool. 162 bool mPresentationEnded MOZ_GUARDED_BY(mMutex); 163 bool mIsAudioEnded MOZ_GUARDED_BY(mMutex); 164 bool mIsVideoEnded MOZ_GUARDED_BY(mMutex); 165 166 // Modify on MF thread pool and the manager thread, read on any threads. 167 State mState MOZ_GUARDED_BY(mMutex); 168 169 Microsoft::WRL::ComPtr<MFMediaEngineStream> mAudioStream 170 MOZ_GUARDED_BY(mMutex); 171 Microsoft::WRL::ComPtr<MFMediaEngineStream> mVideoStream 172 MOZ_GUARDED_BY(mMutex); 173 174 // Thread-safe members END 175 176 // Modify and access on MF thread pool. 177 float mPlaybackRate = 0.0f; 178 179 #ifdef MOZ_WMF_CDM 180 RefPtr<MFCDMProxy> mCDMProxy; 181 #endif 182 }; 183 184 } // namespace mozilla 185 186 #endif // DOM_MEDIA_PLATFORM_WMF_MFMEDIASOURCE_H