Ocean
MFFrameMedium.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_FRAME_MEDIUM_H
9 #define META_OCEAN_MEDIA_MF_FRAME_MEDIUM_H
10 
14 
16 
17 #include <vector>
18 
19 namespace Ocean
20 {
21 
22 namespace Media
23 {
24 
25 namespace MediaFoundation
26 {
27 
28 /**
29  * This is the base class for all Media Foundation frame mediums.
30  * @ingroup mediamf
31  */
32 class OCEAN_MEDIA_MF_EXPORT MFFrameMedium :
33  virtual public MFMedium,
34  virtual public FrameMedium
35 {
36  public:
37 
38  /**
39  * Sets the preferred frame dimension.
40  * @see FrameMedium::setPreferredFrameDimension().
41  */
42  bool setPreferredFrameDimension(const unsigned int width, const unsigned int height) override;
43 
44  /**
45  * Sets the preferred frame pixel format.
46  * @see FrameMedium::setPreferredFramePixelFormat().
47  */
49 
50  /**
51  * Sets the preferred frame frequency in Hz.
52  * @see FrameMedium::setPreferredFrameFrequency().
53  */
54  bool setPreferredFrameFrequency(const FrameFrequency frequency) override;
55 
56  /**
57  * Extracts the video format of a given Media Foundation media type.
58  * @param mediaType Media Foundation media type, must be valid
59  * @param frameType Resulting frame type
60  * @return True, if succeeded
61  */
62  static bool extractFrameFormat(IMFMediaType* mediaType, MediaFrameType& frameType);
63 
64  /**
65  * Creates the video format of a given Media Foundation media type.
66  * @param frameType Frame type used for modification, must be valid
67  * @return The resulting media type, invalid otherwise
68  */
70 
71  /**
72  * Determines the video media type of a given topology.
73  * @param topology The topology for that the media type is determined, must be valid
74  * @param mediaFrameType Resulting media frame type
75  * @return True, if succeeded
76  */
77  static bool determineMediaType(IMFTopology* topology, FrameMedium::MediaFrameType& mediaFrameType);
78 
79  /**
80  * Determines the video media type of a given topology node.
81  * @param node The topology node for that the media type is determined, must be valid
82  * @param mediaFrameType Resulting media frame type
83  * @return True, if succeeded
84  */
85  static bool determineMediaType(IMFTopologyNode* node, FrameMedium::MediaFrameType& mediaFrameType);
86 
87  protected:
88 
89  /**
90  * Creates a new frame medium by a given URL.
91  * @param url The URL of the frame medium
92  */
93  explicit MFFrameMedium(const std::string& url);
94 
95  /**
96  * Destructs a MFFrameMedium object.
97  */
98  ~MFFrameMedium() override;
99 
100  /**
101  * Returns the timestamp of the most recent media sample.
102  * @see MFMedium::recentMediaSampleTimestamp().
103  */
104  bool recentMediaSampleTimestamp(LONGLONG& timestamp, LONGLONG* nextTimestamp) const override;
105 
106  /**
107  * Called by if a new sample arrived.
108  * @param guidMajorMediaType Major media type of the sample
109  * @param dwSampleFlags Reserved
110  * @param llSampleTime The presentation time for this sample, in 100-nanosecond units
111  * @param llSampleDuration The duration of the sample, in 100-nanosecond units
112  * @param pSampleBuffer A pointer to a buffer that contains the sample data
113  * @param dwSampleSize Size of the pSampleBuffer buffer, in bytes
114  */
115  virtual void onNewSample(const GUID& guidMajorMediaType, const unsigned int dwSampleFlags, const long long llSampleTime, const long long llSampleDuration, const void* pSampleBuffer, const unsigned int dwSampleSize);
116 
117  /**
118  * Topology set event function.
119  * @see MFMedium::onTopologySet().
120  */
121  void onTopologySet(IMFTopology* topology) override;
122 
123  /**
124  * Format type changed event function.
125  * @see MFMedium::onFormatTypeChanged().
126  */
127  void onFormatTypeChanged(const TOPOID nodeId) override;
128 
129  /**
130  * Builds the frame topology of this object.
131  * @param respectPlaybackTime True, to deliver samples based on the presentation time; False, to ignore the presentation clock and to deliver samples as fast as possible
132  * @return True, if succeeded
133  */
134  virtual bool buildFrameTopology(const bool respectPlaybackTime);
135 
136  /**
137  * Releases the frame topology of this object.
138  */
139  virtual void releaseFrameTopology();
140 
141  /**
142  * Connects the selected video stream with a given sink activate object.
143  * @param sinkActivate The sink activate object, must be valid
144  * @param preferredMediaFrameType The preferred media frame type
145  * @return True, if succeeded
146  */
147  bool connectSelectedStream(IMFActivate* sinkActivate, const MediaFrameType& preferredMediaFrameType);
148 
149  /**
150  * Returns the best matching media type for a given preferred media type.
151  * @param mediaTypeHandler The type handler for which the best matching type will be determined, must be valid
152  * @param preferredMediaFrameType The preferred media frame type
153  * @return The best matching media type, invalid if no type could be found
154  */
155  static ScopedIMFMediaType determineBestMatchingMediaType(IMFMediaTypeHandler* mediaTypeHandler, const MediaFrameType& preferredMediaFrameType);
156 
157  protected:
158 
159  /// Frame sample grabber.
161 
162  /// The media type handler which is associated with the stream.
164 
165  /// The input node of the source.
167 
168  /// The output node of the sink.
170 
171  /// Frame type of the most recent (upcoming frame) frame.
173 
174  /// Internal frame type of the most recent (upcoming frame) frame.
176 
177  /// Optional cropping position left.
178  unsigned int croppingLeft_ = 0u;
179 
180  /// Optional cropping position top.
181  unsigned int croppingTop_ = 0u;
182 
183  /// Optional cropping width, 0 if no cropping is defined.
184  unsigned int croppingWidth_ = 0u;
185 
186  /// Optional cropping height, 0 if no cropping is defined.
187  unsigned int croppingHeight_ = 0u;
188 
189  /// True, if the medium waits for the first frame after the topology has been changed.
190  bool waitingForFirstFrame_ = true;
191 };
192 
193 }
194 
195 }
196 
197 }
198 
199 #endif // META_OCEAN_MEDIA_MF_FRAME_MEDIUM_H
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition: Frame.h:30
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
Definition of a media frame type composed by the frame dimension, pixel format, pixel origin and a fr...
Definition: FrameMedium.h:65
This is the base class for all frame mediums.
Definition: FrameMedium.h:53
double FrameFrequency
Definition of a frame frequency defined in Hz.
Definition: FrameMedium.h:59
This is the base class for all Media Foundation frame mediums.
Definition: MFFrameMedium.h:35
ScopedSampleGrabber frameSampleGrabber_
Frame sample grabber.
Definition: MFFrameMedium.h:160
virtual bool buildFrameTopology(const bool respectPlaybackTime)
Builds the frame topology of this object.
bool setPreferredFrameFrequency(const FrameFrequency frequency) override
Sets the preferred frame frequency in Hz.
virtual void onNewSample(const GUID &guidMajorMediaType, const unsigned int dwSampleFlags, const long long llSampleTime, const long long llSampleDuration, const void *pSampleBuffer, const unsigned int dwSampleSize)
Called by if a new sample arrived.
bool connectSelectedStream(IMFActivate *sinkActivate, const MediaFrameType &preferredMediaFrameType)
Connects the selected video stream with a given sink activate object.
MFFrameMedium(const std::string &url)
Creates a new frame medium by a given URL.
static ScopedIMFMediaType determineBestMatchingMediaType(IMFMediaTypeHandler *mediaTypeHandler, const MediaFrameType &preferredMediaFrameType)
Returns the best matching media type for a given preferred media type.
bool setPreferredFrameDimension(const unsigned int width, const unsigned int height) override
Sets the preferred frame dimension.
bool setPreferredFramePixelFormat(const FrameType::PixelFormat format) override
Sets the preferred frame pixel format.
static bool extractFrameFormat(IMFMediaType *mediaType, MediaFrameType &frameType)
Extracts the video format of a given Media Foundation media type.
FrameType internalRecentFrameType_
Internal frame type of the most recent (upcoming frame) frame.
Definition: MFFrameMedium.h:175
ScopedIMFTopologyNode inputNode_
The input node of the source.
Definition: MFFrameMedium.h:166
bool recentMediaSampleTimestamp(LONGLONG &timestamp, LONGLONG *nextTimestamp) const override
Returns the timestamp of the most recent media sample.
~MFFrameMedium() override
Destructs a MFFrameMedium object.
ScopedIMFTopologyNode outputNode_
The output node of the sink.
Definition: MFFrameMedium.h:169
ScopedMediaFoundationObject< IMFMediaTypeHandler > mediaTypeHandler_
The media type handler which is associated with the stream.
Definition: MFFrameMedium.h:163
void onFormatTypeChanged(const TOPOID nodeId) override
Format type changed event function.
virtual void releaseFrameTopology()
Releases the frame topology of this object.
static bool determineMediaType(IMFTopologyNode *node, FrameMedium::MediaFrameType &mediaFrameType)
Determines the video media type of a given topology node.
static bool determineMediaType(IMFTopology *topology, FrameMedium::MediaFrameType &mediaFrameType)
Determines the video media type of a given topology.
static ScopedIMFMediaType createMediaType(const MediaFrameType &frameType)
Creates the video format of a given Media Foundation media type.
void onTopologySet(IMFTopology *topology) override
Topology set event function.
FrameType recentFrameType_
Frame type of the most recent (upcoming frame) frame.
Definition: MFFrameMedium.h:172
This is the base class for all MediaFoundation mediums.
Definition: MFMedium.h:31
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15