summaryrefslogtreecommitdiffstats
path: root/sys/v4l2/gstv4l2element.c
blob: 2c3a304d5ce124587b32e514d8e3ff78b6c69e8d (plain)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* G-Streamer generic V4L2 element
 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "v4l2_calls.h"

/* elementfactory details */
static GstElementDetails gst_v4l2element_details = {
	"Generic video4linux2 Element",
	"None/Video",
	"LGPL",
	"Generic plugin for handling common video4linux2 calls",
	VERSION,
	"Ronald Bultje <rbultje@ronald.bitfreak.net>",
	"(C) 2002",
};

/* V4l2Element signals and args */
enum {
	/* FILL ME */
	LAST_SIGNAL
};

enum {
	ARG_0,
	ARG_CHANNEL,
	ARG_CHANNEL_NAMES,
	ARG_OUTPUT,
	ARG_OUTPUT_NAMES,
	ARG_NORM,
	ARG_NORM_NAMES,
	ARG_HAS_TUNER,
	ARG_FREQUENCY,
	ARG_SIGNAL_STRENGTH,
	ARG_HAS_AUDIO,
	ARG_ATTRIBUTE,
	ARG_ATTRIBUTE_SETS,
	ARG_DEVICE,
	ARG_DEVICE_NAME,
	ARG_DEVICE_HAS_CAPTURE,
	ARG_DEVICE_HAS_OVERLAY,
	ARG_DEVICE_HAS_CODEC,
	ARG_DISPLAY,
	ARG_VIDEOWINDOW,
	ARG_DO_OVERLAY,
};


static void			gst_v4l2element_class_init	(GstV4l2ElementClass *klass);
static void			gst_v4l2element_init		(GstV4l2Element      *v4lelement);
static void			gst_v4l2element_set_property	(GObject             *object,
								 guint               prop_id,
								 const GValue        *value,
								 GParamSpec          *pspec);
static void			gst_v4l2element_get_property	(GObject             *object,
								 guint               prop_id,
								 GValue              *value,
								 GParamSpec          *pspec);
static GstElementStateReturn	gst_v4l2element_change_state	(GstElement          *element);


static GstElementClass *parent_class = NULL;
/*static guint gst_v4l2element_signals[LAST_SIGNAL] = { 0 }; */


GType
gst_v4l2element_get_type (void)
{
	static GType v4l2element_type = 0;

	if (!v4l2element_type) {
		static const GTypeInfo v4l2element_info = {
			sizeof(GstV4l2ElementClass),
			NULL,
			NULL,
			(GClassInitFunc) gst_v4l2element_class_init,
			NULL,
			NULL,
			sizeof(GstV4l2Element),
			0,
			(GInstanceInitFunc) gst_v4l2element_init,
			NULL
		};
		v4l2element_type = g_type_register_static(GST_TYPE_ELEMENT,
					"GstV4l2Element", &v4l2element_info, 0);
	}
	return v4l2element_type;
}



static void
gst_v4l2element_class_init (GstV4l2ElementClass *klass)
{
	GObjectClass *gobject_class;
	GstElementClass *gstelement_class;

	gobject_class = (GObjectClass*)klass;
	gstelement_class = (GstElementClass*)klass;

	parent_class = g_type_class_ref(GST_TYPE_ELEMENT);

	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNEL,
		g_param_spec_int("channel","channel","channel",
		G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNEL_NAMES,
		g_param_spec_pointer("channel_names","channel_names","channel_names",
		G_PARAM_READABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_OUTPUT,
		g_param_spec_int("output","output","output",
		G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_OUTPUT_NAMES,
		g_param_spec_pointer("output_names","output_names","output_names",
		G_PARAM_READABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NORM,
		g_param_spec_int("norm","norm","norm",
		G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NORM_NAMES,
		g_param_spec_pointer("norm_names","norm_names","norm_names",
		G_PARAM_READABLE));

	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_HAS_TUNER,
		g_param_spec_boolean("has_tuner","has_tuner","has_tuner",
		0,G_PARAM_READABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FREQUENCY,
		g_param_spec_ulong("frequency","frequency","frequency",
		0,G_MAXULONG,0,G_PARAM_READWRITE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SIGNAL_STRENGTH,
		g_param_spec_ulong("signal_strength","signal_strength","signal_strength",
		0,G_MAXULONG,0,G_PARAM_READABLE));

	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_HAS_AUDIO,
		g_param_spec_boolean("has_audio","has_audio","has_audio",
		0,G_PARAM_READABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ATTRIBUTE,
		g_param_spec_pointer("attribute","attribute","attribute",
		G_PARAM_READWRITE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ATTRIBUTE_SETS,
		g_param_spec_pointer("attribute_sets","attribute_sets","attribute_sets",
		G_PARAM_READABLE));

	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE,
		g_param_spec_string("device","device","device",
		NULL, G_PARAM_READWRITE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE_NAME,
		g_param_spec_string("device_name","device_name","device_name",
		NULL, G_PARAM_READABLE));

	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE_HAS_CAPTURE,
		g_param_spec_boolean("can_capture","can_capture","can_capture",
		0,G_PARAM_READABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE_HAS_OVERLAY,
		g_param_spec_boolean("has_overlay","has_overlay","has_overlay",
		0,G_PARAM_READABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE_HAS_CODEC,
		g_param_spec_boolean("has_compression","has_compression","has_compression",
		0,G_PARAM_READABLE));

	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DISPLAY,
		g_param_spec_string("display","display","display",
		NULL, G_PARAM_WRITABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DO_OVERLAY,
		g_param_spec_boolean("do_overlay","do_overlay","do_overlay",
		0,G_PARAM_WRITABLE));
	g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_VIDEOWINDOW,
		g_param_spec_pointer("videowindow","videowindow","videowindow",
		G_PARAM_WRITABLE));

	gobject_class->set_property = gst_v4l2element_set_property;
	gobject_class->get_property = gst_v4l2element_get_property;

	gstelement_class->change_state = gst_v4l2element_change_state;
}


static void
gst_v4l2element_init (GstV4l2Element *v4l2element)
{
	/* some default values */
	v4l2element->video_fd = -1;
	v4l2element->buffer = NULL;
	v4l2element->device = NULL;

	v4l2element->norm = -1;
	v4l2element->channel = -1;
	v4l2element->output = -1;
	v4l2element->frequency = 0;

	v4l2element->controls = NULL;
	v4l2element->formats = NULL;
	v4l2element->outputs = NULL;
	v4l2element->inputs = NULL;
	v4l2element->norms = NULL;
}


static void
gst_v4l2element_set_property (GObject      *object,
                              guint        prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
	GstV4l2Element *v4l2element;

	/* it's not null if we got it, but it might not be ours */
	g_return_if_fail(GST_IS_V4L2ELEMENT(object));
	v4l2element = GST_V4L2ELEMENT(object);

	switch (prop_id) {
		case ARG_CHANNEL:
			v4l2element->channel = g_value_get_int(value);
			if (GST_V4L2_IS_OPEN(v4l2element) && !GST_V4L2_IS_ACTIVE(v4l2element)) {
				if (!gst_v4l2_set_input(v4l2element, g_value_get_int(value)))
					return;
			}
			break;
		case ARG_OUTPUT:
			v4l2element->output = g_value_get_int(value);
			if (GST_V4L2_IS_OPEN(v4l2element) && !GST_V4L2_IS_ACTIVE(v4l2element)) {
				if (!gst_v4l2_set_output(v4l2element, g_value_get_int(value)))
					return;
			}
			break;
		case ARG_NORM:
			v4l2element->norm = g_value_get_int(value);
			if (GST_V4L2_IS_OPEN(v4l2element) && !GST_V4L2_IS_ACTIVE(v4l2element)) {
				if (!gst_v4l2_set_norm(v4l2element, g_value_get_int(value)))
					return;
			}
			break;
		case ARG_FREQUENCY:
			v4l2element->frequency = g_value_get_ulong(value);
			if (GST_V4L2_IS_OPEN(v4l2element) && !GST_V4L2_IS_ACTIVE(v4l2element)) {
				if (!gst_v4l2_set_frequency(v4l2element, g_value_get_ulong(value)))
					return;
			}
			break;
		case ARG_ATTRIBUTE:
			if (GST_V4L2_IS_OPEN(v4l2element)) {
				gst_v4l2_set_attribute(v4l2element,
					((GstV4l2Attribute*)g_value_get_pointer(value))->index,
					((GstV4l2Attribute*)g_value_get_pointer(value))->value);
			}
			break;
		case ARG_DEVICE:
			if (!GST_V4L2_IS_OPEN(v4l2element)) {
				if (v4l2element->device)
					g_free(v4l2element->device);
				v4l2element->device = g_strdup(g_value_get_string(value));
			}
			break;
		case ARG_DISPLAY:
			if (!gst_v4l2_set_display(v4l2element, g_value_get_string(value)))
				return;
			break;
		case ARG_VIDEOWINDOW:
			if (GST_V4L2_IS_OPEN(v4l2element)) {
				GByteArray *array = (GByteArray *) g_value_get_pointer(value);
				struct v4l2_clip *clips = (struct v4l2_clip *) array->data;
				gst_v4l2_set_window(v4l2element,
					clips->x, clips->y, clips->width, clips->height,
					&clips[1], array->len/sizeof(struct v4l2_clip)-1);
			}
			break;
		case ARG_DO_OVERLAY:
			if (GST_V4L2_IS_OPEN(v4l2element)) {
				if (!gst_v4l2_enable_overlay(v4l2element, g_value_get_boolean(value)))
					return;
			}
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}


static void
gst_v4l2element_get_property (GObject    *object,
                              guint      prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
	GstV4l2Element *v4l2element;
	gint temp_i = 0;
	gulong temp_ul = 0;
	GList *list = NULL;

	/* it's not null if we got it, but it might not be ours */
	g_return_if_fail(GST_IS_V4L2ELEMENT(object));
	v4l2element = GST_V4L2ELEMENT(object);

	switch (prop_id) {
		case ARG_CHANNEL:
			if (GST_V4L2_IS_OPEN(v4l2element))
				gst_v4l2_get_input(v4l2element, &temp_i);
			g_value_set_int(value, temp_i);
			break;
		case ARG_CHANNEL_NAMES:
			if (GST_V4L2_IS_OPEN(v4l2element))
				list = gst_v4l2_get_input_names(v4l2element);
			g_value_set_pointer(value, list);
			break;
		case ARG_OUTPUT:
			if (GST_V4L2_IS_OPEN(v4l2element))
				gst_v4l2_get_output(v4l2element, &temp_i);
			g_value_set_int(value, temp_i);
			break;
		case ARG_OUTPUT_NAMES:
			if (GST_V4L2_IS_OPEN(v4l2element))
				list = gst_v4l2_get_output_names(v4l2element);
			g_value_set_pointer(value, list);
			break;
		case ARG_NORM:
			if (GST_V4L2_IS_OPEN(v4l2element))
				gst_v4l2_get_norm(v4l2element, &temp_i);
			g_value_set_int(value, temp_i);
			break;
		case ARG_NORM_NAMES:
			if (GST_V4L2_IS_OPEN(v4l2element))
				list = gst_v4l2_get_norm_names(v4l2element);
			g_value_set_pointer(value, list);
			break;
		case ARG_HAS_TUNER:
			if (GST_V4L2_IS_OPEN(v4l2element))
				temp_i = gst_v4l2_has_tuner(v4l2element);
			g_value_set_boolean(value, temp_i>0?TRUE:FALSE);
			break;
		case ARG_FREQUENCY:
			if (GST_V4L2_IS_OPEN(v4l2element))
				gst_v4l2_get_frequency(v4l2element, &temp_ul);
			g_value_set_ulong(value, temp_ul);
			break;
		case ARG_SIGNAL_STRENGTH:
			if (GST_V4L2_IS_OPEN(v4l2element))
				gst_v4l2_signal_strength(v4l2element, &temp_ul);
			g_value_set_ulong(value, temp_ul);
			break;
		case ARG_HAS_AUDIO:
			if (GST_V4L2_IS_OPEN(v4l2element))
				temp_i = gst_v4l2_has_audio(v4l2element);
			g_value_set_boolean(value, temp_i>0?TRUE:FALSE);
			break;
		case ARG_ATTRIBUTE:
			if (GST_V4L2_IS_OPEN(v4l2element))
				gst_v4l2_get_attribute(v4l2element,
					((GstV4l2Attribute*)g_value_get_pointer(value))->index, &temp_i);
			((GstV4l2Attribute*)g_value_get_pointer(value))->value = temp_i;
			break;
		case ARG_ATTRIBUTE_SETS:
			if (GST_V4L2_IS_OPEN(v4l2element))
				list = gst_v4l2_get_attributes(v4l2element);
			g_value_set_pointer(value, list);
			break;
		case ARG_DEVICE:
			g_value_set_string(value, g_strdup(v4l2element->device));
			break;
		case ARG_DEVICE_NAME:
			if (GST_V4L2_IS_OPEN(v4l2element))
				g_value_set_string(value, g_strdup(v4l2element->vcap.name));
			break;
		case ARG_DEVICE_HAS_CAPTURE:
			if (GST_V4L2_IS_OPEN(v4l2element) &&
			    (v4l2element->vcap.type == V4L2_TYPE_CODEC ||
			     v4l2element->vcap.type == V4L2_TYPE_CAPTURE) &&
			    v4l2element->vcap.flags & V4L2_FLAG_STREAMING)
				temp_i = 1;
			g_value_set_boolean(value, temp_i>0?TRUE:FALSE);
			break;
		case ARG_DEVICE_HAS_OVERLAY:
			if (GST_V4L2_IS_OPEN(v4l2element) &&
			    v4l2element->vcap.flags & V4L2_FLAG_PREVIEW)
				temp_i = 1;
			g_value_set_boolean(value, temp_i>0?TRUE:FALSE);
			break;
		case ARG_DEVICE_HAS_CODEC:
			if (GST_V4L2_IS_OPEN(v4l2element) &&
			    v4l2element->vcap.type == V4L2_TYPE_CODEC)
				temp_i = 1;
			g_value_set_boolean(value, temp_i>0?TRUE:FALSE);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
			break;
	}
}


static GstElementStateReturn
gst_v4l2element_change_state (GstElement *element)
{
	GstV4l2Element *v4l2element;

	g_return_val_if_fail(GST_IS_V4L2ELEMENT(element), GST_STATE_FAILURE);
  
	v4l2element = GST_V4L2ELEMENT(element);

	/* if going down into NULL state, close the device if it's open
	 * if going to READY, open the device (and set some options)
	 */
	switch (GST_STATE_TRANSITION(element)) {
		case GST_STATE_NULL_TO_READY:
			if (!gst_v4l2_open(v4l2element))
				return GST_STATE_FAILURE;

			/* now, sync options */
			if (v4l2element->norm >= 0)
				if (!gst_v4l2_set_norm(v4l2element, v4l2element->norm))
					return GST_STATE_FAILURE;
			if (v4l2element->channel >= 0)
				if (!gst_v4l2_set_input(v4l2element, v4l2element->channel))
					return GST_STATE_FAILURE;
			if (v4l2element->output >= 0)
				if (!gst_v4l2_set_output(v4l2element, v4l2element->output))
					return GST_STATE_FAILURE;
			if (v4l2element->frequency > 0)
				if (!gst_v4l2_set_frequency(v4l2element, v4l2element->frequency))
					return GST_STATE_FAILURE;
			break;
		case GST_STATE_READY_TO_NULL:
			if (!gst_v4l2_close(v4l2element))
				return GST_STATE_FAILURE;
			break;
	}

	if (GST_ELEMENT_CLASS(parent_class)->change_state)
		return GST_ELEMENT_CLASS(parent_class)->change_state(element);

	return GST_STATE_SUCCESS;
}


static gboolean
plugin_init (GModule   *module,
             GstPlugin *plugin)
{
	GstElementFactory *factory;

	/* create an elementfactory for the v4l2element */
	factory = gst_element_factory_new("v4l2element", GST_TYPE_V4L2ELEMENT,
				&gst_v4l2element_details);
	g_return_val_if_fail(factory != NULL, FALSE);
	gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

	return TRUE;
}


GstPluginDesc plugin_desc = {
	GST_VERSION_MAJOR,
	GST_VERSION_MINOR,
	"v4l2element",
	plugin_init
};