-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstPluginWrap.h
436 lines (339 loc) · 13.2 KB
/
gstPluginWrap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* Note: the GST_PLUGIN_TYPE macro must be defined before including this file.
* It is used as a name of the structure that represents GStreamer element type.
* The defined name have to be unique among plugins installed in the system.
*
* Example:
* #define GST_PLUGIN_TYPE GstMyPluginName
* #include <gstPluginWrap.h>
*/
#ifndef GSTPLUGINWRAP_H_
#define GSTPLUGINWRAP_H_
#include <glib.h>
#include <gst/gst.h>
#include "gstPluginFrameHandler.h"
#include "gstPluginPropertyHolder.h"
namespace gstPluginWrap {
namespace details {
extern const char* name;
extern const char* longName;
extern const char* classification;
extern const char* description;
extern const char* originUrl;
extern const char* author;
extern const char* license;
extern const char* version;
} // namespace details
extern const char* allowedCaps;
typedef void (*ParametersGetter)(ParamIdSpecMap& parameters);
class Customizer;
extern void configure(Customizer*& customizer);
/**
* Basic customization class. Should be used as a singleton.
*/
class Customizer {
public:
static Customizer* getInstance() {
if (instance != NULL)
return instance;
configure(instance);
if (instance == NULL)
instance = new Customizer(); // create default customizer if not provided by user code
return instance;
}
static void deleteInstance() {
delete instance;
instance = NULL;
}
Customizer() {
getParameters = NULL;
}
virtual ~Customizer() {}
virtual PropertyHolder* createPropertyHolder() {
return NULL;
}
virtual PureFrameHandler* createFrameHandler(PropertyHolder* propertyHolder) {
return NULL;
}
virtual void deleteFrameHandler(PureFrameHandler*& frameHandler, PropertyHolder* propertyHolder) {
delete frameHandler;
frameHandler = NULL;
}
ParametersGetter getParameters;
private:
static Customizer* instance;
};
Customizer* Customizer::instance = NULL;
/**
* Plugin with frame handling.
*/
template<class HandlerT>
class HandlerCustomizer: public Customizer {
public:
virtual ~HandlerCustomizer() {}
virtual PureFrameHandler* createFrameHandler(PropertyHolder* propertyHolder) {
return new HandlerT();
}
};
/**
* Plugin with frame handling and properties.
*/
template<class HandlerT, class PropT>
class HandlerAndPropCustomizer: public HandlerCustomizer<HandlerT> {
public:
virtual ~HandlerAndPropCustomizer() {}
virtual PropertyHolder* createPropertyHolder() {
return new PropT();
}
virtual PureFrameHandler* createFrameHandler(PropertyHolder* propertyHolder) {
HandlerT* frameHandler = new HandlerT();
PropT* properties = static_cast<PropT*>(propertyHolder);
frameHandler->setProperties(properties);
return frameHandler;
}
};
/**
* Plugin with frame handling and properties bounded by listener.
*/
template<class HandlerT, class PropT>
class HandlerAndPropWithListenerCustomizer: public HandlerCustomizer<HandlerT> {
public:
virtual ~HandlerAndPropWithListenerCustomizer() {}
virtual PropertyHolder* createPropertyHolder() {
return new PropT();
}
virtual PureFrameHandler* createFrameHandler(PropertyHolder* propertyHolder) {
HandlerT* frameHandler = new HandlerT();
PropT* properties = static_cast<PropT*>(propertyHolder);
frameHandler->setProperties(properties);
properties->setListener(frameHandler);
return frameHandler;
}
virtual void deleteFrameHandler(PureFrameHandler*& frameHandler, PropertyHolder* propertyHolder) {
PropT* properties = static_cast<PropT*>(propertyHolder);
properties->setListener(NULL);
Customizer::deleteFrameHandler(frameHandler, propertyHolder);
}
};
} // namespace gstPluginWrap
G_BEGIN_DECLS // begin C code
#ifndef GST_PLUGIN_TYPE
#error "GST_PLUGIN_TYPE is not defined."
#endif
#define TYPE_JOIN(a, b) a ## b
#define MAKE_CLASS_NAME(base, postfix) TYPE_JOIN(base, postfix)
#define GST_PLUGIN_TYPE_CLASS MAKE_CLASS_NAME(GST_PLUGIN_TYPE, Class)
struct _GstThisPlugin {
GstElement element;
GstPad* sinkPad;
GstPad* srcPad;
gchar* mime;
gstPluginWrap::PureFrameHandler* frameHandler;
gstPluginWrap::PropertyHolder* propertyHolder;
};
typedef struct _GstThisPlugin GST_PLUGIN_TYPE;
struct _GstThisPluginClass {
GstElementClass parent_class;
};
typedef struct _GstThisPluginClass GST_PLUGIN_TYPE_CLASS;
GType gst_thisplugin_get_type();
#define GST_TYPE_THISPLUGIN \
gst_thisplugin_get_type()
#define GST_THISPLUGIN(obj) \
G_TYPE_CHECK_INSTANCE_CAST(obj, GST_TYPE_THISPLUGIN, GST_PLUGIN_TYPE)
#define GST_IS_THISPLUGIN(obj) \
G_TYPE_CHECK_INSTANCE_TYPE(obj, GST_TYPE_THISPLUGIN)
#define GST_THISPLUGIN_CLASS(probClass) \
G_TYPE_CHECK_CLASS_CAST(probClass, GST_TYPE_THISPLUGIN, GST_PLUGIN_TYPE_CLASS)
#define GST_IS_THISPLUGIN_CLASS(probClass) \
G_TYPE_CHECK_CLASS_TYPE(probClass, GST_TYPE_THISPLUGIN)
G_END_DECLS // end C code
/*
* The _finalize() function is called when the plugin is destroyed.
*/
static void gst_thisplugin_finalize(GObject* pluginObj);
/*
* The _set_property() function will be notified if an application changes the value of a property,
* and can then take action required for that property to change value internally.
*/
static void gst_thisplugin_set_property(GObject* pluginObj, guint id, const GValue* val,
GParamSpec* paramSpec);
/*
* The _get_property() function will be notified
* if an application requests the value of a property,
* and can then fill in the value.
*/
static void gst_thisplugin_get_property(GObject* pluginObj, guint id, GValue* val,
GParamSpec* paramSpec);
/*
* The _change_state() is called when state is changed.
*/
static GstStateChangeReturn gst_thisplugin_change_state(GstElement* element,
GstStateChange transition);
/*
* The _setcaps() function is called during caps negotiation.
* This is the process where the linked pads decide on the stream type
* that will transfer between them.
*/
static gboolean gst_thisplugin_setcaps(GstPad* pad, GstCaps* caps);
/*
* The _chain() function is the function in which all data processing takes place.
*/
static GstFlowReturn gst_thisplugin_chain(GstPad* pad, GstBuffer* buffer);
// sink pad (input)
static GstStaticPadTemplate sinkTemplate = GST_STATIC_PAD_TEMPLATE(
"sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS(gstPluginWrap::allowedCaps)
);
// source pad (output)
static GstStaticPadTemplate srcTemplate = GST_STATIC_PAD_TEMPLATE(
"src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS(gstPluginWrap::allowedCaps)
);
/*
* The plugin_init() function is called as soon as the plugin is loaded.
* It should return TRUE or FALSE depending on whether it is loaded correctly or not.
* In this function, any supported element type in the plugin should be registered.
*/
static gboolean plugin_init(GstPlugin* gstPlugin) {
using namespace gstPluginWrap;
return gst_element_register(gstPlugin, details::name, GST_RANK_NONE, GST_TYPE_THISPLUGIN);
}
#ifndef PACKAGE
#define PACKAGE ""
#endif
GST_PLUGIN_DEFINE(
GST_VERSION_MAJOR,
GST_VERSION_MINOR,
gstPluginWrap::details::name,
gstPluginWrap::details::description,
plugin_init,
gstPluginWrap::details::version,
gstPluginWrap::details::license,
PACKAGE,
gstPluginWrap::details::originUrl
)
GST_BOILERPLATE(GST_PLUGIN_TYPE, gst_thisplugin, GstElement, GST_TYPE_ELEMENT)
/*
* The _base_init() function is meant to initialize class and child class properties
* during each new child class creation.
*/
static void gst_thisplugin_base_init(gpointer typeClass) {
using namespace gstPluginWrap;
GstElementClass* elemClass = GST_ELEMENT_CLASS(typeClass);
gst_element_class_set_details_simple(elemClass,
details::longName,
details::classification,
details::description,
details::author);
gst_element_class_add_pad_template(elemClass, gst_static_pad_template_get(&srcTemplate));
gst_element_class_add_pad_template(elemClass, gst_static_pad_template_get(&sinkTemplate));
}
/*
* The _class_init() function is used to initialize the class.
* It is called only once and must specify what signals,
* arguments and virtual functions the class has.
*/
static void gst_thisplugin_class_init(GST_PLUGIN_TYPE_CLASS* pluginClass) {
using namespace gstPluginWrap;
parent_class = (GstElementClass*)g_type_class_ref(GST_TYPE_ELEMENT);
GObjectClass* gobjClass = G_OBJECT_CLASS(pluginClass);
gobjClass->finalize = gst_thisplugin_finalize;
gobjClass->set_property = gst_thisplugin_set_property;
gobjClass->get_property = gst_thisplugin_get_property;
GstElementClass* elemClass = GST_ELEMENT_CLASS(pluginClass);
elemClass->change_state = gst_thisplugin_change_state;
ParametersGetter paramGetter = Customizer::getInstance()->getParameters;
if (paramGetter != NULL) {
ParamIdSpecMap params;
(*paramGetter)(params);
for (ParamIdSpecMap::iterator it = params.begin(), end = params.end(); it != end; ++it) {
guint id = it->first;
GParamSpec* spec = it->second;
g_object_class_install_property(gobjClass, id, spec);
}
}
}
/*
* The _init() function is used to initialize a specific instance of the plugin type.
*/
static void gst_thisplugin_init(GST_PLUGIN_TYPE* plugin, GST_PLUGIN_TYPE_CLASS* pluginClass) {
using namespace gstPluginWrap;
plugin->sinkPad = gst_pad_new_from_static_template(&sinkTemplate, "sink");
gst_pad_set_setcaps_function(plugin->sinkPad, gst_thisplugin_setcaps);
gst_pad_set_getcaps_function(plugin->sinkPad, gst_pad_proxy_getcaps);
gst_pad_set_chain_function(plugin->sinkPad, gst_thisplugin_chain);
plugin->srcPad = gst_pad_new_from_static_template(&srcTemplate, "src");
gst_pad_set_getcaps_function(plugin->srcPad, gst_pad_proxy_getcaps);
gst_element_add_pad(GST_ELEMENT(plugin), plugin->sinkPad);
gst_element_add_pad(GST_ELEMENT(plugin), plugin->srcPad);
plugin->frameHandler = NULL;
plugin->propertyHolder = Customizer::getInstance()->createPropertyHolder();
}
static void gst_thisplugin_finalize(GObject* pluginObj) {
using namespace gstPluginWrap;
GST_PLUGIN_TYPE* plugin = GST_THISPLUGIN(pluginObj);
delete plugin->propertyHolder;
plugin->propertyHolder = NULL;
g_free(plugin->mime);
plugin->mime = NULL;
Customizer::deleteInstance();
G_OBJECT_CLASS(parent_class)->finalize(pluginObj);
}
static void gst_thisplugin_set_property(GObject* pluginObj, guint id, const GValue* val,
GParamSpec* paramSpec) {
GST_PLUGIN_TYPE* plugin = GST_THISPLUGIN(pluginObj);
if (!plugin->propertyHolder->set(id, val))
G_OBJECT_WARN_INVALID_PROPERTY_ID(pluginObj, id, paramSpec);
}
static void gst_thisplugin_get_property(GObject* pluginObj, guint id, GValue* val,
GParamSpec* paramSpec) {
GST_PLUGIN_TYPE* plugin = GST_THISPLUGIN(pluginObj);
if (!plugin->propertyHolder->get(id, val))
G_OBJECT_WARN_INVALID_PROPERTY_ID(pluginObj, id, paramSpec);
}
static GstStateChangeReturn gst_thisplugin_change_state(GstElement* element,
GstStateChange transition) {
using namespace gstPluginWrap;
GST_PLUGIN_TYPE* plugin = GST_THISPLUGIN(element);
GstStateChangeReturn retVal = GST_STATE_CHANGE_SUCCESS;
if (transition == GST_STATE_CHANGE_NULL_TO_READY) {
if (plugin->frameHandler == NULL) {
Customizer* customer = Customizer::getInstance();
plugin->frameHandler = customer->createFrameHandler(plugin->propertyHolder);
} else {
g_printerr("%s\n", "FrameHandler is not clean at null to ready state change");
retVal = GST_STATE_CHANGE_FAILURE;
}
}
if (retVal == GST_STATE_CHANGE_FAILURE)
return retVal;
retVal = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
if (retVal == GST_STATE_CHANGE_FAILURE)
return retVal;
if (transition == GST_STATE_CHANGE_READY_TO_NULL)
Customizer::getInstance()->deleteFrameHandler(plugin->frameHandler, plugin->propertyHolder);
return retVal;
}
static gboolean gst_thisplugin_setcaps(GstPad* pad, GstCaps* caps) {
GST_PLUGIN_TYPE* plugin = GST_THISPLUGIN(gst_pad_get_parent(pad));
GstStructure* props = gst_caps_get_structure(caps, 0);
const gchar* mime = gst_structure_get_name(props);
plugin->mime = g_strdup(mime);
if (plugin->propertyHolder != NULL)
plugin->propertyHolder->setMediaInfo(plugin->mime, props);
GstPad* otherPad = plugin->srcPad;
gst_object_unref(plugin);
return gst_pad_set_caps(otherPad, caps);
}
static GstFlowReturn gst_thisplugin_chain(GstPad* pad, GstBuffer* buffer) {
GST_PLUGIN_TYPE* plugin = GST_THISPLUGIN(GST_OBJECT_PARENT(pad));
if (plugin->frameHandler)
plugin->frameHandler->process(GST_BUFFER_DATA(buffer));
return gst_pad_push(plugin->srcPad, buffer);
}
#endif // GSTPLUGINWRAP_H_