File: dummy-device-provider.c

package info (click to toggle)
libaperture-0 0.1.0%2Bgit20221220-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 424 kB
  • sloc: ansic: 2,318; xml: 63; sh: 32; makefile: 9
file content (215 lines) | stat: -rw-r--r-- 5,721 bytes parent folder | download | duplicates (2)
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
/* dummy-device-provider.c
 *
 * Copyright 2020 James Westman <james@flyingpimonster.net>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */


#include "dummy-device-provider.h"
#include "dummy-device.h"


struct _DummyDeviceProvider
{
  GstDeviceProvider parent_instance;

  GList *devices;
};

G_DEFINE_TYPE (DummyDeviceProvider, dummy_device_provider, GST_TYPE_DEVICE_PROVIDER)

enum {
  PROP_0,
  N_PROPS
};

static GParamSpec *properties [N_PROPS];


DummyDevice *dummy_device_provider_add (DummyDeviceProvider *self);
void dummy_device_provider_remove (DummyDeviceProvider *self);


/* VFUNCS */


static void
dummy_device_provider_finalize (GObject *object)
{
  DummyDeviceProvider *self = (DummyDeviceProvider *)object;

  g_list_free_full (self->devices, g_object_unref);

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


static GList *
dummy_device_provider_probe (GstDeviceProvider *self)
{
  return g_list_copy_deep (self->devices, (GCopyFunc) g_object_ref, NULL);
}


static gboolean
dummy_device_provider_start (GstDeviceProvider *provider)
{
  return TRUE;
}


static void
dummy_device_provider_stop (GstDeviceProvider *provider)
{
  DummyDeviceProvider *self = DUMMY_DEVICE_PROVIDER (provider);

  while (self->devices) {
    dummy_device_provider_remove (self);
  }
}


/* INIT */


static void
dummy_device_provider_class_init (DummyDeviceProviderClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstDeviceProviderClass *device_provider_class = GST_DEVICE_PROVIDER_CLASS (klass);

  object_class->finalize = dummy_device_provider_finalize;

  device_provider_class->probe = dummy_device_provider_probe;
  device_provider_class->start = dummy_device_provider_start;
  device_provider_class->stop = dummy_device_provider_stop;

  gst_device_provider_class_set_static_metadata (device_provider_class,
                                                 "Dummy device provider",
                                                 "Source/Video",
                                                 "Dummy device provider for unit tests",
                                                 "James Westman <james@flyingpimonster.net>");
}


static void
dummy_device_provider_init (DummyDeviceProvider *self)
{
}


/* PUBLIC */


/**
 * PRIVATE:dummy_device_provider_new:
 *
 * Creates a new #DummyDeviceProvider.
 *
 * Returns: (transfer full): a new #DummyDeviceProvider
 */
DummyDeviceProvider *
dummy_device_provider_new (void)
{
  return g_object_new (DUMMY_TYPE_DEVICE_PROVIDER, NULL);
}


/**
 * PRIVATE:dummy_device_provider_add:
 * @self: a #DummyDeviceProvider
 *
 * Adds a new device to the dummy provider, causing a device added message to
 * be posted on the device monitor's bus.
 *
 * Note that the device manager starts with one device by default, so this is
 * unnecessary unless you're testing adding and removing devices, or testing
 * multiple devices.
 *
 * Returns: (transfer none): the new device
 */
DummyDevice *
dummy_device_provider_add (DummyDeviceProvider *self)
{
  DummyDevice *device;

  g_return_val_if_fail (DUMMY_IS_DEVICE_PROVIDER (self), NULL);

  device = dummy_device_new ();
  gst_device_provider_device_add (GST_DEVICE_PROVIDER (self), GST_DEVICE (device));
  self->devices = g_list_prepend (self->devices, device);

  return device;
}


/**
 * PRIVATE:dummy_device_provider_remove:
 * @self: a #DummyDeviceProvider
 *
 * Removes a device from the device provider, causing a device removed message
 * to be posted on the device monitor's bus. Devices are removed opposite the
 * order they were added (in a stack structure).
 */
void
dummy_device_provider_remove (DummyDeviceProvider *self)
{
  GList *first;

  g_return_if_fail (DUMMY_IS_DEVICE_PROVIDER (self));

  if (!self->devices) {
    return;
  }

  first = g_list_first (self->devices);
  gst_device_provider_device_remove (GST_DEVICE_PROVIDER (self), GST_DEVICE (first->data));
  self->devices = g_list_delete_link (first, first);
}


/**
 * PRIVATE:dummy_device_provider_register:
 *
 * Registers the dummy device provider.
 *
 * This should only be called once.
 */
void
dummy_device_provider_register (void)
{
  g_autoptr(GstDeviceProvider) dummy = NULL;
  g_autolist(GstDeviceProviderFactory) providers = NULL;
  GList *i;

  /* Register the dummy provider */
  gst_device_provider_register (NULL, "dummy-device-provider", G_MAXINT, DUMMY_TYPE_DEVICE_PROVIDER);

  /* Hide all the other providers so we're only working with our dummy devices.
   * This allows us to test things like no-cameras state, even on machines
   * that have real cameras attached. */
  dummy = gst_device_provider_factory_get_by_name ("dummy-device-provider");
  providers = gst_device_provider_factory_list_get_device_providers (GST_RANK_NONE);

  for (i = providers; i != NULL; i = i->next) {
    g_autofree char *name = gst_object_get_name (GST_OBJECT (i->data));
    if (g_strcmp0 (name, "dummy-device-provider") != 0) {
      g_print ("%s\n", name);
      gst_device_provider_hide_provider (dummy, name);
    }
  }
}