File: example-device-provider.c

package info (click to toggle)
gst-plugins-base1.0 1.26.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,840 kB
  • sloc: ansic: 398,488; cpp: 3,961; objc: 2,502; python: 292; sh: 66; makefile: 51
file content (331 lines) | stat: -rw-r--r-- 9,506 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2019 Mathieu Duponchelle <mathieu@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/* Simple device provider example.
 *
 * Usage:
 *
 * GST_PLUGIN_PATH=$GST_PLUGIN_PATH:/path/to/libexample_device_provider.so/folder gst-device-monitor-1.0 -f
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>

#define NEW_DEVICE_INTERVAL 1   /* seconds */

#define EXAMPLE_TYPE_DEVICE_PROVIDER example_device_provider_get_type()
#define EXAMPLE_DEVICE_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),EXAMPLE_TYPE_DEVICE_PROVIDER,ExampleDeviceProvider))

typedef struct _ExampleDeviceProvider ExampleDeviceProvider;
typedef struct _ExampleDeviceProviderClass ExampleDeviceProviderClass;

struct _ExampleDeviceProviderClass
{
  GstDeviceProviderClass parent_class;
};

/**
 * Our device provider instance.
 *
 * @factory: the videotestsrc factory
 * @patterns: When started, the list of videotestsrc pattern
 *            (as strings) to iterate through when adding new devices,
 *            eg "smpte", "snow", ...
 * @timeout_id: When started, we will add a new device every
 *            %NEW_DEVICE_INTERVAL seconds
 */
struct _ExampleDeviceProvider
{
  GstDeviceProvider parent;
  GstElementFactory *factory;
  GList *patterns;
  guint timeout_id;
};

static GType example_device_provider_get_type (void);

G_DEFINE_TYPE (ExampleDeviceProvider, example_device_provider,
    GST_TYPE_DEVICE_PROVIDER);

#define EXAMPLE_TYPE_DEVICE example_device_get_type()
#define EXAMPLE_DEVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),EXAMPLE_TYPE_DEVICE,ExampleDevice))

typedef struct _ExampleDevice ExampleDevice;
typedef struct _ExampleDeviceClass ExampleDeviceClass;

struct _ExampleDeviceClass
{
  GstDeviceClass parent_class;
};

/* Our example device, it simply exposes a videotestsrc with a specific
 * pattern.
 */
struct _ExampleDevice
{
  GstDevice parent;

  gchar *pattern;
  GstElementFactory *factory;
};

static GType example_device_get_type (void);

G_DEFINE_TYPE (ExampleDevice, example_device, GST_TYPE_DEVICE);

static void
example_device_init (ExampleDevice * self)
{
}

static void
example_device_finalize (GObject * object)
{
  ExampleDevice *self = EXAMPLE_DEVICE (object);

  g_free (self->pattern);

  G_OBJECT_CLASS (example_device_parent_class)->finalize (object);
}

static void
example_device_dispose (GObject * object)
{
  ExampleDevice *self = EXAMPLE_DEVICE (object);

  gst_object_replace ((GstObject **) & self->factory, NULL);

  G_OBJECT_CLASS (example_device_parent_class)->dispose (object);
}

static GstElement *
example_device_create_element (GstDevice * device, const gchar * name)
{
  ExampleDevice *self = EXAMPLE_DEVICE (device);
  GstElement *ret;

  ret = gst_element_factory_create (self->factory, name);

  gst_util_set_object_arg (G_OBJECT (ret), "pattern", self->pattern);

  return ret;
}

static void
example_device_class_init (ExampleDeviceClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstDeviceClass *gst_device_class = GST_DEVICE_CLASS (klass);

  gobject_class->finalize = GST_DEBUG_FUNCPTR (example_device_finalize);
  gobject_class->dispose = GST_DEBUG_FUNCPTR (example_device_dispose);

  gst_device_class->create_element =
      GST_DEBUG_FUNCPTR (example_device_create_element);
}

static GstDevice *
example_device_new (GstElementFactory * factory, const gchar * pattern)
{
  GstDevice *ret;
  gchar *display_name;
  GstCaps *caps;
  const GList *templates;

  templates = gst_element_factory_get_static_pad_templates (factory);
  caps = gst_static_pad_template_get_caps ((GstStaticPadTemplate *)
      templates->data);

  display_name = g_strdup_printf ("example-device-%s", pattern);

  ret = GST_DEVICE (g_object_new (EXAMPLE_TYPE_DEVICE,
          "display-name", display_name,
          "device-class", "Video/Source", "caps", caps, NULL));

  g_free (display_name);
  gst_caps_unref (caps);

  EXAMPLE_DEVICE (ret)->pattern = g_strdup (pattern);
  EXAMPLE_DEVICE (ret)->factory =
      GST_ELEMENT_FACTORY (gst_object_ref (factory));

  return ret;
}

static void
example_device_provider_init (ExampleDeviceProvider * self)
{
  self->factory = gst_element_factory_find ("videotestsrc");

  /* Ensure we can introspect the factory */
  gst_object_unref (gst_plugin_feature_load (GST_PLUGIN_FEATURE
          (self->factory)));

}

/* Called when gst_device_provider_get_devices() is called on a provider that
 * hasn't been started, or doesn't implement #GstDeviceProvider.start().
 *
 * In that case, let's return a single example device, with a snow pattern.
 */
static GList *
example_device_provider_probe (GstDeviceProvider * provider)
{
  ExampleDeviceProvider *self = EXAMPLE_DEVICE_PROVIDER (provider);
  GList *ret = NULL;

  ret = g_list_prepend (ret, example_device_new (self->factory, "snow"));

  return ret;
}

static gboolean
example_device_provider_next_device (ExampleDeviceProvider * self)
{
  GstDevice *device;
  gboolean ret = G_SOURCE_CONTINUE;

  if (!self->patterns)
    goto no_more_patterns;

  device = example_device_new (self->factory, (gchar *) self->patterns->data);
  gst_device_provider_device_add (GST_DEVICE_PROVIDER (self), device);
  g_free (self->patterns->data);
  self->patterns = g_list_delete_link (self->patterns, self->patterns);

done:
  return ret;

no_more_patterns:
  GST_DEBUG_OBJECT (self, "Went through all videotestsrc patterns!");
  ret = G_SOURCE_REMOVE;
  goto done;
}

/* Start adding devices every %NEW_DEVICE_INTERVAL seconds.
 * We will stop once we have consumed all the available videotestsrc
 * patterns, or when our #GstDeviceProvider.stop() implementation is
 * called.
 */
static gboolean
example_device_provider_start (GstDeviceProvider * provider)
{
  ExampleDeviceProvider *self = EXAMPLE_DEVICE_PROVIDER (provider);
  GType element_type;
  GTypeClass *element_class;
  GParamSpec *pspec;
  GEnumClass *value_class;
  guint i;

  g_assert (!self->timeout_id);

  element_type = gst_element_factory_get_element_type (self->factory);
  element_class = (GTypeClass *) g_type_class_ref (element_type);
  pspec =
      g_object_class_find_property ((GObjectClass *) element_class, "pattern");
  value_class = (GEnumClass *) g_type_class_ref (pspec->value_type);

  for (i = 0; i < value_class->n_values; i++) {
    GEnumValue *val = &value_class->values[i];

    self->patterns = g_list_append (self->patterns, g_strdup (val->value_nick));
  }

  g_type_class_unref (value_class);
  g_type_class_unref (element_class);

  self->timeout_id =
      g_timeout_add_seconds (NEW_DEVICE_INTERVAL,
      (GSourceFunc) example_device_provider_next_device, self);

  return TRUE;
}

/* Simply stop adding devices by removing our timeout. */
static void
example_device_provider_stop (GstDeviceProvider * provider)
{
  ExampleDeviceProvider *self = EXAMPLE_DEVICE_PROVIDER (provider);

  g_assert (self->timeout_id);

  if (self->patterns) {
    g_list_free_full (self->patterns, g_free);
    self->patterns = NULL;
  }

  g_source_remove (self->timeout_id);
  self->timeout_id = 0;
}

static void
example_device_provider_dispose (GObject * object)
{
  ExampleDeviceProvider *self = EXAMPLE_DEVICE_PROVIDER (object);

  gst_object_replace ((GstObject **) & self->factory, NULL);

  G_OBJECT_CLASS (example_device_provider_parent_class)->dispose (object);
}

static void
example_device_provider_finalize (GObject * object)
{
  ExampleDeviceProvider *self = EXAMPLE_DEVICE_PROVIDER (object);

  if (self->patterns)
    g_list_free_full (self->patterns, g_free);

  G_OBJECT_CLASS (example_device_provider_parent_class)->finalize (object);
}

static void
example_device_provider_class_init (ExampleDeviceProviderClass * klass)
{
  GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = GST_DEBUG_FUNCPTR (example_device_provider_dispose);
  gobject_class->finalize =
      GST_DEBUG_FUNCPTR (example_device_provider_finalize);

  dm_class->probe = GST_DEBUG_FUNCPTR (example_device_provider_probe);
  dm_class->start = GST_DEBUG_FUNCPTR (example_device_provider_start);
  dm_class->stop = GST_DEBUG_FUNCPTR (example_device_provider_stop);

  gst_device_provider_class_set_static_metadata (dm_class,
      "Example Device Provider", "Source/Video",
      "List and provides example source devices",
      "Mathieu Duponchelle <mathieu@centricular.com>");
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  return gst_device_provider_register (plugin, "exampledeviceprovider",
      GST_RANK_PRIMARY, EXAMPLE_TYPE_DEVICE_PROVIDER);
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    example_device_provider,
    "Example device provider",
    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)