Ocean
MFMedium.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MEDIA_MF_MEDIUM_H
9 #define META_OCEAN_MEDIA_MF_MEDIUM_H
10 
12 
13 #include "ocean/base/Signal.h"
14 
15 #include "ocean/media/Medium.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Media
21 {
22 
23 namespace MediaFoundation
24 {
25 
26 /**
27  * This is the base class for all MediaFoundation mediums.
28  * @ingroup mediamf
29  */
30 class OCEAN_MEDIA_MF_EXPORT MFMedium : virtual public Medium
31 {
32  protected:
33 
34  /**
35  * This class implements a callback class allowing to determine Media Foundation events.
36  */
38  {
39  public:
40 
41  /**
42  * Definition of a callback function without parameter.
43  */
45 
46  /**
47  * Definition of a callback function with topology as parameter.
48  */
50 
51  /**
52  * Definition of a callback function with node as parameter.
53  */
55 
56  public:
57 
58  /**
59  * Creates a new callback object.
60  * @param eventGenerator Event generator on that this callback object will listen
61  * @param topologySet Callback function for topology-set events
62  * @param sessionStarted Callback function for session-started events
63  * @param sessionStopped Callback function for session-stopped events
64  * @param sessionEnded Callback function for session-ended events
65  * @param frameTypeChanged Callback function for format-changed events
66  */
67  EventCallback(IMFMediaEventGenerator& eventGenerator, const TopologyEventFunction& topologySet, const EventFunction& sessionStarted, const EventFunction& sessionStopped, const EventFunction& sessionEnded, const TopologyNodeEventFunction& frameTypeChanged);
68 
69  /**
70  * Destructs this object.
71  */
72  virtual ~EventCallback() = default;
73 
74  /**
75  * Deactivates the callback.
76  */
77  void deactivate();
78 
79  /**
80  * Returns whether the media session is closed.
81  * @return True, if so
82  */
83  bool isMediaSessionClosed() const;
84 
85  /**
86  * Interface query function.
87  * @param riid The identifier of the interface being requested
88  * @param ppvObject The address of a pointer variable that receives the interface pointer requested in the riid parameter
89  * @return This method returns S_OK if the interface is supported
90  */
91  HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) override;
92 
93  /**
94  * Increments the reference count for an interface on an object.
95  * @return The method returns the new reference count
96  */
97  ULONG STDMETHODCALLTYPE AddRef() override;
98 
99  /**
100  * Decrements the reference count for an interface on an object.
101  * @return The method returns the new reference count
102  */
103  ULONG STDMETHODCALLTYPE Release() override;
104 
105  /**
106  * Provides configuration information to the dispatching thread for a callback.
107  * @param pdwFlags Receives a flag indicating the behavior of the callback object's IMFAsyncCallback::Invoke method
108  * @param pdwQueue Receives the identifier of the work queue on which the callback is dispatched
109  * @return The method returns an HRESULT
110  */
111  HRESULT STDMETHODCALLTYPE GetParameters(DWORD* pdwFlags, DWORD* pdwQueue) override;
112 
113  /**
114  * Called when an asynchronous operation is completed.
115  * @param pAsyncResult Pointer to the IMFAsyncResult interface
116  * @return The method returns an HRESULT
117  */
118  HRESULT STDMETHODCALLTYPE Invoke(IMFAsyncResult *pAsyncResult) override;
119 
120  protected:
121 
122  /// Event generator on that this callback object will listen.
123  IMFMediaEventGenerator& eventGenerator_;
124 
125  /// True, if the callback is active.
126  bool active_ = true;
127 
128  /// Reference counter.
129  long referenceCounter_ = 1;
130 
131  /// Callback function for topology-set events.
133 
134  /// Callback function for session started events.
136 
137  /// Callback function for session started events.
139 
140  /// Callback function for session ended events.
142 
143  /// Callback function for format type changed events.
145 
146  /// True, if the session is closed.
147  std::atomic<bool> isMediaSessionClosed_ = false;
148  };
149 
150  /**
151  * Definition of a scoped object holding a EventCallback object.
152  * The wrapped EventCallback object will be released automatically once the scoped object does not exist anymore.
153  */
155 
156  public:
157 
158  /**
159  * Returns whether the medium is started currently.
160  * @see Medium::isStarted().
161  */
162  bool isStarted() const override;
163 
164  /**
165  * Returns the start timestamp.
166  * @see FiniteMedium::startTimestamp().
167  */
168  Timestamp startTimestamp() const override;
169 
170  /**
171  * Returns the pause timestamp.
172  * @see FiniteMedium::pauseTimestamp().
173  */
174  Timestamp pauseTimestamp() const override;
175 
176  /**
177  * Returns the stop timestamp.
178  * @see FiniteMedium::stopTimestamp().
179  */
180  Timestamp stopTimestamp() const override;
181 
182  protected:
183 
184  /**
185  * Creates a new medium by a given URL.
186  * @param url The URL of the medium
187  */
188  explicit MFMedium(const std::string& url);
189 
190  /**
191  * Destructs a medium object.
192  */
193  ~MFMedium() override;
194 
195  /**
196  * Starts the medium.
197  * @see Medium::start().
198  */
199  bool start() override;
200 
201  /**
202  * Pauses the medium.
203  * @see Medium::pause().
204  */
205  bool pause() override;
206 
207  /**
208  * Stops the medium.
209  * @see Medium::stop().
210  */
211  bool stop() override;
212 
213  /**
214  * Creates the media session object.
215  * @return True, if succeeded or if the media session exists already
216  */
218 
219  /**
220  * Releases the media session object.
221  */
223 
224  /**
225  * Creates and builds the topology of this object.
226  * @param respectPlaybackTime True, to deliver the media content based on the presentation time; False, to ignore the presentation clock and to deliver the media content as fast as possible
227  * @return True, if succeeded
228  */
229  virtual bool createTopology(const bool respectPlaybackTime) = 0;
230 
231  /**
232  * Releases the topology.
233  */
234  virtual void releaseTopology();
235 
236  /**
237  * Creates the media source object.
238  * @return True, if succeeded
239  */
240  virtual bool createMediaSource();
241 
242  /**
243  * Releases the media source.
244  */
246 
247  /**
248  * Starts the media session.
249  * @return True, if succeeded or if the session is already started
250  */
251  virtual bool startMediaSession();
252 
253  /**
254  * Pauses the media session.
255  * @return True, if succeeded or if the session is already paused
256  */
257  virtual bool pauseMediaSession();
258 
259  /**
260  * Stops the media session.
261  * @return True, if the succeeded or if the session is already stopped
262  */
263  virtual bool stopMediaSession();
264 
265  /**
266  * Returns the timestamp of the most recent media sample.
267  * @param timestamp The resulting timestamp of the most recent media sample
268  * @param nextTimestamp Optional resulting timestamp of the next media sample; nullptr if not of interest
269  * @return True, if succeeded
270  */
271  virtual bool recentMediaSampleTimestamp(LONGLONG& timestamp, LONGLONG* nextTimestamp) const;
272 
273  /**
274  * Topology set event function.
275  * @param topology New topology that has been set
276  */
277  virtual void onTopologySet(IMFTopology* topology);
278 
279  /**
280  * Session started event function.
281  */
282  virtual void onSessionStarted();
283 
284  /**
285  * Session stopped event function.
286  */
287  virtual void onSessionStopped();
288 
289  /**
290  * Session ended event function.
291  */
292  virtual void onSessionEnded();
293 
294  /**
295  * Format type changed event function.
296  * @param nodeId Id of the topology node that holds the changed format type
297  */
298  virtual void onFormatTypeChanged(const TOPOID nodeId);
299 
300  /**
301  * Creates the pipeline.
302  * @param respectPlaybackTime True, to deliver the media content based on the presentation time; False, to ignore the presentation clock and to deliver the media content as fast as possible
303  * @return True, if succeeded
304  */
305  virtual bool createPipeline(const bool respectPlaybackTime);
306 
307  /**
308  * Releases the pipeline.
309  */
310  virtual void releasePipeline();
311 
312  protected:
313 
314  /// Start timestamp.
316 
317  /// Pause timestamp.
319 
320  /// Stop timestamp.
322 
323  /// Media session of this object.
325 
326  /// Media topology of this object.
328 
329  /// Media source object.
331 
332  /// Event callback object for this medium.
334 
335  /// True, to deliver the media content based on the presentation time; False, to ignore the presentation clock and to deliver the media content as fast as possible.
336  bool respectPlaybackTime_ = true;
337 };
338 
339 }
340 
341 }
342 
343 }
344 
345 #endif // META_OCEAN_MEDIA_MF_MEDIUM_H
This class implements a callback class allowing to determine Media Foundation events.
Definition: MFMedium.h:38
ULONG STDMETHODCALLTYPE AddRef() override
Increments the reference count for an interface on an object.
HRESULT STDMETHODCALLTYPE Invoke(IMFAsyncResult *pAsyncResult) override
Called when an asynchronous operation is completed.
EventFunction callbackSessionStopped_
Callback function for session started events.
Definition: MFMedium.h:138
TopologyNodeEventFunction callbackFormatTypeChanged_
Callback function for format type changed events.
Definition: MFMedium.h:144
Callback< void > EventFunction
Definition of a callback function without parameter.
Definition: MFMedium.h:44
bool isMediaSessionClosed() const
Returns whether the media session is closed.
TopologyEventFunction callbackTopologySet_
Callback function for topology-set events.
Definition: MFMedium.h:132
HRESULT STDMETHODCALLTYPE GetParameters(DWORD *pdwFlags, DWORD *pdwQueue) override
Provides configuration information to the dispatching thread for a callback.
EventFunction callbackSessionEnded_
Callback function for session ended events.
Definition: MFMedium.h:141
Callback< void, IMFTopology * > TopologyEventFunction
Definition of a callback function with topology as parameter.
Definition: MFMedium.h:49
EventFunction callbackSessionStarted_
Callback function for session started events.
Definition: MFMedium.h:135
IMFMediaEventGenerator & eventGenerator_
Event generator on that this callback object will listen.
Definition: MFMedium.h:123
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) override
Interface query function.
EventCallback(IMFMediaEventGenerator &eventGenerator, const TopologyEventFunction &topologySet, const EventFunction &sessionStarted, const EventFunction &sessionStopped, const EventFunction &sessionEnded, const TopologyNodeEventFunction &frameTypeChanged)
Creates a new callback object.
virtual ~EventCallback()=default
Destructs this object.
ULONG STDMETHODCALLTYPE Release() override
Decrements the reference count for an interface on an object.
Callback< void, const TOPOID > TopologyNodeEventFunction
Definition of a callback function with node as parameter.
Definition: MFMedium.h:54
This is the base class for all MediaFoundation mediums.
Definition: MFMedium.h:31
virtual bool createPipeline(const bool respectPlaybackTime)
Creates the pipeline.
virtual void onSessionStarted()
Session started event function.
ScopedMediaFoundationObject< EventCallback > ScopedEventCallback
Definition of a scoped object holding a EventCallback object.
Definition: MFMedium.h:154
ScopedIMFMediaSource mediaSource_
Media source object.
Definition: MFMedium.h:330
virtual void onSessionStopped()
Session stopped event function.
Timestamp pauseTimestamp() const override
Returns the pause timestamp.
virtual void onTopologySet(IMFTopology *topology)
Topology set event function.
Timestamp stopTimestamp_
Stop timestamp.
Definition: MFMedium.h:321
virtual bool stopMediaSession()
Stops the media session.
virtual void releasePipeline()
Releases the pipeline.
bool createMediaSession()
Creates the media session object.
Timestamp stopTimestamp() const override
Returns the stop timestamp.
bool start() override
Starts the medium.
void releaseMediaSession()
Releases the media session object.
ScopedIMFMediaSession mediaSession_
Media session of this object.
Definition: MFMedium.h:324
~MFMedium() override
Destructs a medium object.
bool stop() override
Stops the medium.
Timestamp pauseTimestamp_
Pause timestamp.
Definition: MFMedium.h:318
Timestamp startTimestamp() const override
Returns the start timestamp.
virtual bool createMediaSource()
Creates the media source object.
virtual void onFormatTypeChanged(const TOPOID nodeId)
Format type changed event function.
bool isStarted() const override
Returns whether the medium is started currently.
MFMedium(const std::string &url)
Creates a new medium by a given URL.
bool pause() override
Pauses the medium.
ScopedEventCallback eventCallback_
Event callback object for this medium.
Definition: MFMedium.h:333
Timestamp startTimestamp_
Start timestamp.
Definition: MFMedium.h:315
virtual bool createTopology(const bool respectPlaybackTime)=0
Creates and builds the topology of this object.
virtual bool recentMediaSampleTimestamp(LONGLONG &timestamp, LONGLONG *nextTimestamp) const
Returns the timestamp of the most recent media sample.
virtual bool pauseMediaSession()
Pauses the media session.
virtual void releaseTopology()
Releases the topology.
ScopedIMFTopology topology_
Media topology of this object.
Definition: MFMedium.h:327
virtual void onSessionEnded()
Session ended event function.
void releaseMediaSource()
Releases the media source.
virtual bool startMediaSession()
Starts the media session.
This is the base class for all mediums.
Definition: Medium.h:48
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15