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