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/. */
5 #ifndef DOM_MEDIA_PLATFORM_WMF_MFMEDIASOURCE_H
6 #define DOM_MEDIA_PLATFORM_WMF_MFMEDIASOURCE_H
11 #include "MediaInfo.h"
12 #include "MediaEventSource.h"
13 #include "MFMediaEngineExtra.h"
14 #include "MFMediaEngineStream.h"
15 #include "mozilla/EnumSet.h"
16 #include "mozilla/TaskQueue.h"
22 // An event to indicate a need for a certain type of sample.
23 struct SampleRequest
{
24 SampleRequest(TrackInfo::TrackType aType
, bool aIsEnough
)
25 : mType(aType
), mIsEnough(aIsEnough
) {}
26 TrackInfo::TrackType mType
;
31 * MFMediaSource is a custom source for the media engine, the media engine would
32 * ask the source for the characteristics and the presentation descriptor to
33 * know how to react with the source. This source is also responsible to
34 * dispatch events to the media engine to notify the status changes.
36 * https://docs.microsoft.com/en-us/windows/win32/api/mfidl/nn-mfidl-imfmediasource
38 class MFMediaSource
: public Microsoft::WRL::RuntimeClass
<
39 Microsoft::WRL::RuntimeClassFlags
<
40 Microsoft::WRL::RuntimeClassType::ClassicCom
>,
41 IMFMediaSource
, IMFRateControl
, IMFRateSupport
,
42 IMFGetService
, IMFTrustedInput
> {
47 HRESULT
RuntimeClassInitialize(const Maybe
<AudioInfo
>& aAudio
,
48 const Maybe
<VideoInfo
>& aVideo
,
49 nsISerialEventTarget
* aManagerThread
);
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
;
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
,
71 const PROPVARIANT
* aValue
) override
;
74 IFACEMETHODIMP
GetService(REFGUID aGuidService
, REFIID aRiid
,
75 LPVOID
* aResult
) override
;
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
;
86 IFACEMETHODIMP
SetRate(BOOL aSupportsThinning
, float aRate
) override
;
87 IFACEMETHODIMP
GetRate(BOOL
* aSupportsThinning
, float* aRate
) override
;
90 IFACEMETHODIMP
GetInputTrustAuthority(DWORD aStreamId
, REFIID aRiid
,
91 IUnknown
** aITAOut
) override
;
93 MFMediaEngineStream
* GetAudioStream();
94 MFMediaEngineStream
* GetVideoStream();
96 MFMediaEngineStream
* GetStreamByIndentifier(DWORD aStreamId
) const;
99 void SetCDMProxy(MFCDMProxy
* aCDMProxy
);
102 TaskQueue
* GetTaskQueue() const { return mTaskQueue
; }
104 MediaEventSource
<SampleRequest
>& RequestSampleEvent() {
105 return mRequestSampleEvent
;
108 // Called from the content process to notify that no more encoded data in that
110 void NotifyEndOfStream(TrackInfo::TrackType aType
);
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
);
123 State
GetState() const;
125 void SetDCompSurfaceHandle(HANDLE aDCompSurfaceHandle
, gfx::IntSize aDisplay
);
127 void ShutdownTaskQueue();
129 bool IsEncrypted() const;
132 void AssertOnManagerThread() const;
133 void AssertOnMFThreadPool() const;
135 bool IsSeekable() const;
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
;
141 // The thread used to run the engine streams' tasks.
142 RefPtr
<TaskQueue
> mTaskQueue
;
144 // The thread used to run the media source's tasks.
145 RefPtr
<nsISerialEventTarget
> mManagerThread
;
147 // MFMediaEngineStream will notify us when we need more sample.
148 friend class MFMediaEngineStream
;
149 MediaEventProducer
<SampleRequest
> mRequestSampleEvent
;
151 MediaEventListener mAudioStreamEndedListener
;
152 MediaEventListener mVideoStreamEndedListener
;
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.
158 mutable Mutex mMutex
{"MFMediaEngineSource"};
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
);
166 // Modify on MF thread pool and the manager thread, read on any threads.
167 State mState
MOZ_GUARDED_BY(mMutex
);
169 Microsoft::WRL::ComPtr
<MFMediaEngineStream
> mAudioStream
170 MOZ_GUARDED_BY(mMutex
);
171 Microsoft::WRL::ComPtr
<MFMediaEngineStream
> mVideoStream
172 MOZ_GUARDED_BY(mMutex
);
174 // Thread-safe members END
176 // Modify and access on MF thread pool.
177 float mPlaybackRate
= 0.0f
;
180 RefPtr
<MFCDMProxy
> mCDMProxy
;
184 } // namespace mozilla
186 #endif // DOM_MEDIA_PLATFORM_WMF_MFMEDIASOURCE_H