Ocean
media/mediafoundation/Utilities.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_UTILITIES_H
9 #define META_OCEAN_MEDIA_MF_UTILITIES_H
10 
12 
14 
15 #include "ocean/base/Frame.h"
16 
17 #include <map>
18 
19 namespace Ocean
20 {
21 
22 namespace Media
23 {
24 
25 namespace MediaFoundation
26 {
27 
28 /**
29  * This class implements utilities functions for the Microsoft Media Foundation.
30  * @ingroup mediamf
31  */
32 class OCEAN_MEDIA_MF_EXPORT Utilities
33 {
34  private:
35 
36  /**
37  * GUID helper struct.
38  */
39  struct GUIDCompare
40  {
41  /**
42  * Compares two GUID objects.
43  * @param object0 First object
44  * @param object1 Second object
45  * @return True, if the first object is lesser than the second one
46  */
47  inline bool operator()(const GUID& object0, const GUID& object1) const;
48  };
49 
50  /**
51  * Definition of a map mapping GUID objects to strings.
52  */
53  typedef std::map<GUID, std::string, GUIDCompare> IdMap;
54 
55  public:
56 
57  /**
58  * Translates a media foundation attribute to a readable string.
59  * @param attribute The attribute to be translated
60  * @return Translated attribute
61  */
62  static std::string attribute2String(const GUID& attribute);
63 
64  /**
65  * Translates a major media type to a readable string.
66  * @param type The type to be translated
67  * @return Translated type
68  */
69  static std::string majorMediaType2String(const GUID& type);
70 
71  /**
72  * Translates a video subtype to a readable string.
73  * @param type The type to be translated
74  * @return Translated type
75  */
76  static std::string videoSubtype2String(const GUID& type);
77 
78  /**
79  * Translates an unregistered video subtype to a readable string.
80  * @param type The type to be translated
81  * @return Translated type
82  */
83  static std::string unregisteredVideoSubtype2String(const GUID& type);
84 
85  /**
86  * Translates an audio subtype to a readable string.
87  * @param type The type to be translated
88  * @return Translated type
89  */
90  static std::string audioSubtype2String(const GUID& type);
91 
92  /**
93  * Translates an transform category to a readable string.
94  * @param category The category to be translated
95  * @return Translated type
96  */
97  static std::string transformCategory2String(const GUID& category);
98 
99  /**
100  * Translates the GUID key to a string.
101  * @param value The value to be translated
102  * @return Translated key
103  */
104  static std::string guid2String(const GUID& value);
105 
106  /**
107  * Dumps a set of attributes to a string object.
108  * @param attributes The attributes to dump
109  * @param result Resulting dump
110  * @return True, if succeeded
111  */
112  static bool dumpAttributes(IMFAttributes* attributes, std::string& result);
113 
114  /**
115  * Enumerates the registered media foundation transforms.
116  * @param result The resulting enumerated transforms
117  * @return True, if succeeded
118  */
119  static bool enumerateTransforms(std::string& result);
120 
121  /**
122  * Converts a Media Foundation media subtype to a pixel format.
123  * @param mediaSubtype Media Foundation media type to convert
124  * @return Resulting pixel format
125  */
126  static FrameType::PixelFormat convertMediaSubtype(const GUID& mediaSubtype);
127 
128  /**
129  * Returns the pixel origin of a Media Foundation media subtype.
130  * @param mediaSubtype Media Foundation media type to return the pixel origin for
131  * @return Pixel origin
132  */
133  static FrameType::PixelOrigin extractPixelOrigin(const GUID& mediaSubtype);
134 
135  /**
136  * Converts a pixel format to a Media Foundation media subtype.
137  * @param pixelFormat Pixel format
138  * @return Media Foundation media type
139  */
140  static GUID convertPixelFormat(const FrameType::PixelFormat pixelFormat);
141 
142  /**
143  * Creates the media source object for a given URL.
144  * The resulting object has to be released by the caller.<br>
145  * @param url URL for that the media source object will be returned
146  * @return Resulting media source object, invalid otherwise
147  */
148  static ScopedIMFMediaSource createMediaSourceByUrl(const std::wstring& url);
149 
150  /**
151  * Adds a source node to a given topology.
152  * @param topology The topology to that the node will be added, must be valid
153  * @param mediaSource Media source object, must be valid
154  * @param presentationDescriptor Presentation descriptor, must be valid
155  * @param streamDescriptor Stream descriptor, must be valid
156  * @return Resulting topology node, invalid otherwise
157  */
158  static ScopedIMFTopologyNode addSourceNodeToTopology(IMFTopology* topology, IMFMediaSource* mediaSource, IMFPresentationDescriptor* presentationDescriptor, IMFStreamDescriptor* streamDescriptor);
159 
160  /**
161  * Adds an output node to a given topology.
162  * @param topology The topology to that the node will be added, must be valid
163  * @param sinkActivate Sink activate object, must be valid
164  * @param streamIndex Stream index
165  * @return Resulting topology node, invalid otherwise
166  */
167  static ScopedIMFTopologyNode addOutputNodeToTopology(IMFTopology* topology, IMFActivate* sinkActivate, const DWORD streamIndex = 0);
168 
169  /**
170  * Connects the selected media source with a given sink activate object.
171  * @param topology The topology that will receive the connection, must be valid
172  * @param mediaSource The media source object, must be valid
173  * @param sinkActivate The sink activate object, must be valid
174  * @param majorMediaType Major media type
175  * @return True, if succeeded
176  */
177  static bool connectSelectedStream(IMFTopology* topology, IMFMediaSource* mediaSource, IMFActivate* sinkActivate, const GUID& majorMediaType);
178 
179  /**
180  * Returns an allocated string from an activate.
181  * @param activate The activate from which the string will be returned, must be valid
182  * @param key The key identifying which value to retrieve, must be valid
183  * @param value The resulting string value
184  * @reutrn True, if succeeded
185  */
186  static bool getAllocatedString(IMFActivate* activate, const GUID& key, std::string& value);
187 
188  /**
189  * Extracts an object from a topology node.
190  * @param node The node from that the object will be extracted, must be valid
191  * @return Resulting object which must be released by the caller, otherwise nullptr
192  * @tparam T Data type of the desired object
193  */
194  template <typename T>
195  static ScopedMediaFoundationObject<T> topologyNodeObject(IMFTopologyNode* node);
196 
197  /**
198  * Extracts an object from an event.
199  * @param mediaEvent Media event from that the object will be extracted, must be valid
200  * @return Resulting object which must be released by the caller, otherwise nullptr
201  * @tparam T Data type of the desired object
202  */
203  template <typename T>
204  static ScopedMediaFoundationObject<T> eventObject(IMFMediaEvent *mediaEvent);
205 
206  private:
207 
208  /**
209  * Returns a map mapping all GUID ids to readable strings.
210  * @return GUID map
211  */
212  static IdMap guidMap();
213 
214  /**
215  * Returns a map mapping attribute GUID ids to readable strings.
216  * @return GUID map
217  */
218  static IdMap attributeMap();
219 
220  /**
221  * Returns a map mapping major media type GUID ids to readable strings.
222  * @return GUID map
223  */
225 
226  /**
227  * Returns a map mapping video subtype GUID ids to readable strings.
228  * @return GUID map
229  */
231 
232  /**
233  * Returns a map mapping audio subtype GUID ids to readable strings.
234  * @return GUID map
235  */
237 
238  /**
239  * Returns a map mapping transform category GUID ids to readable strings.
240  * @return GUID map
241  */
243 };
244 
245 inline bool Utilities::GUIDCompare::operator()(const GUID& object0, const GUID& object1) const
246 {
247  ocean_assert(sizeof(object0) == sizeof(uint64_t) * 2);
248 
249  const uint64_t* value0 = (const uint64_t*)(&object0);
250  const uint64_t* value1 = (const uint64_t*)(&object1);
251 
252  return value0[0] < value1[0] || (value0[0] == value1[0] && value0[1] < value1[1]);
253 }
254 
255 template <typename T>
257 {
258  ocean_assert(node);
259 
262 
263  if (S_OK == node->GetObject(&object.resetObject()))
264  {
265  object->QueryInterface(IID_PPV_ARGS(&result.resetObject()));
266  }
267 
268  return result;
269 }
270 
271 template <typename T>
273 {
274  ocean_assert(mediaEvent);
275 
276  PROPVARIANT variant;
277  PropVariantInit(&variant);
278 
280 
281  if (S_OK == mediaEvent->GetValue(&variant))
282  {
283  variant.punkVal->QueryInterface(__uuidof(T), (void**)(&result.resetObject()));
284  }
285 
286  PropVariantClear(&variant);
287 
288  return result;
289 }
290 
291 }
292 
293 }
294 
295 }
296 
297 #endif // META_OCEAN_MEDIA_MF_UTILITIES_H
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
PixelOrigin
Defines different types of frame origin positions.
Definition: Frame.h:1046
This class implements utilities functions for the Microsoft Media Foundation.
Definition: media/mediafoundation/Utilities.h:33
static ScopedMediaFoundationObject< T > topologyNodeObject(IMFTopologyNode *node)
Extracts an object from a topology node.
Definition: media/mediafoundation/Utilities.h:256
static std::string videoSubtype2String(const GUID &type)
Translates a video subtype to a readable string.
static IdMap transformCategoryMap()
Returns a map mapping transform category GUID ids to readable strings.
static bool connectSelectedStream(IMFTopology *topology, IMFMediaSource *mediaSource, IMFActivate *sinkActivate, const GUID &majorMediaType)
Connects the selected media source with a given sink activate object.
static bool dumpAttributes(IMFAttributes *attributes, std::string &result)
Dumps a set of attributes to a string object.
static std::string unregisteredVideoSubtype2String(const GUID &type)
Translates an unregistered video subtype to a readable string.
static IdMap majorMediaTypeMap()
Returns a map mapping major media type GUID ids to readable strings.
static ScopedIMFMediaSource createMediaSourceByUrl(const std::wstring &url)
Creates the media source object for a given URL.
static ScopedIMFTopologyNode addOutputNodeToTopology(IMFTopology *topology, IMFActivate *sinkActivate, const DWORD streamIndex=0)
Adds an output node to a given topology.
static FrameType::PixelOrigin extractPixelOrigin(const GUID &mediaSubtype)
Returns the pixel origin of a Media Foundation media subtype.
static ScopedIMFTopologyNode addSourceNodeToTopology(IMFTopology *topology, IMFMediaSource *mediaSource, IMFPresentationDescriptor *presentationDescriptor, IMFStreamDescriptor *streamDescriptor)
Adds a source node to a given topology.
static IdMap videoSubtypeMap()
Returns a map mapping video subtype GUID ids to readable strings.
static FrameType::PixelFormat convertMediaSubtype(const GUID &mediaSubtype)
Converts a Media Foundation media subtype to a pixel format.
static std::string guid2String(const GUID &value)
Translates the GUID key to a string.
static bool getAllocatedString(IMFActivate *activate, const GUID &key, std::string &value)
Returns an allocated string from an activate.
static bool enumerateTransforms(std::string &result)
Enumerates the registered media foundation transforms.
static std::string majorMediaType2String(const GUID &type)
Translates a major media type to a readable string.
std::map< GUID, std::string, GUIDCompare > IdMap
Definition of a map mapping GUID objects to strings.
Definition: media/mediafoundation/Utilities.h:53
static ScopedMediaFoundationObject< T > eventObject(IMFMediaEvent *mediaEvent)
Extracts an object from an event.
Definition: media/mediafoundation/Utilities.h:272
static IdMap attributeMap()
Returns a map mapping attribute GUID ids to readable strings.
static GUID convertPixelFormat(const FrameType::PixelFormat pixelFormat)
Converts a pixel format to a Media Foundation media subtype.
static IdMap guidMap()
Returns a map mapping all GUID ids to readable strings.
static std::string attribute2String(const GUID &attribute)
Translates a media foundation attribute to a readable string.
static std::string audioSubtype2String(const GUID &type)
Translates an audio subtype to a readable string.
static std::string transformCategory2String(const GUID &category)
Translates an transform category to a readable string.
static IdMap audioSubtypeMap()
Returns a map mapping audio subtype GUID ids to readable strings.
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
T & resetObject(const bool needsRelease=true)
Releases the current wrapped object and returns a new wrapped object.
Definition: ScopedObject.h:488
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
GUID helper struct.
Definition: media/mediafoundation/Utilities.h:40
bool operator()(const GUID &object0, const GUID &object1) const
Compares two GUID objects.
Definition: media/mediafoundation/Utilities.h:245