summaryrefslogtreecommitdiff
path: root/sys/v4l2/v4l2_calls.c
blob: cb108416d789c3f5dea5607740dfef66bc2e14ec (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/* G-Streamer generic V4L2 element - generic V4L2 calls handling
 * 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include "v4l2_calls.h"

#define DEBUG(format, args...) \
	GST_DEBUG_ELEMENT(GST_CAT_PLUGIN_INFO, \
		GST_ELEMENT(v4l2element), \
		"V4L2: " format, ##args)


/******************************************************
 * gst_v4l2_get_capabilities():
 *   get the device's capturing capabilities
 * return value: TRUE on success, FALSE on error
 ******************************************************/

static gboolean
gst_v4l2_get_capabilities (GstV4l2Element *v4l2element)
{
	DEBUG("getting capabilities");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCAP, &(v4l2element->vcap)) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Error getting %s capabilities: %s",
			v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	return TRUE;
}


/******************************************************
 * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
 *   fill/empty the lists of enumerations
 * return value: TRUE on success, FALSE on error
 ******************************************************/

static gboolean
gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
{
	gint n;

	DEBUG("getting enumerations");
	GST_V4L2_CHECK_OPEN(v4l2element);

	/* and now, the inputs */
	for (n=0;;n++) {
		struct v4l2_input input, *inpptr;
		input.index = n;
		if (ioctl(v4l2element->video_fd, VIDIOC_ENUMINPUT, &input) < 0) {
			if (errno == EINVAL)
				break; /* end of enumeration */
			else {
				gst_element_error(GST_ELEMENT(v4l2element),
					"Failed to get no. %d in input enumeration for %s: %s",
					n, v4l2element->device, g_strerror(errno));
				return FALSE;
			}
		}
		inpptr = g_malloc(sizeof(input));
		memcpy(inpptr, &input, sizeof(input));
		v4l2element->inputs = g_list_append(v4l2element->inputs, inpptr);
	}

	/* outputs */
	for (n=0;;n++) {
		struct v4l2_output output, *outptr;
		output.index = n;
		if (ioctl(v4l2element->video_fd, VIDIOC_ENUMOUTPUT, &output) < 0) {
			if (errno == EINVAL)
				break; /* end of enumeration */
			else {
				gst_element_error(GST_ELEMENT(v4l2element),
					"Failed to get no. %d in output enumeration for %s: %s",
					n, v4l2element->device, g_strerror(errno));
				return FALSE;
			}
		}
		outptr = g_malloc(sizeof(output));
		memcpy(outptr, &output, sizeof(output));
		v4l2element->outputs = g_list_append(v4l2element->outputs, outptr);
	}

	/* norms... */
	for (n=0;;n++) {
		struct v4l2_standard standard, *stdptr;
		standard.index = n;
		if (ioctl(v4l2element->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
			if (errno == EINVAL)
				break; /* end of enumeration */
			else {
				gst_element_error(GST_ELEMENT(v4l2element),
					"Failed to get no. %d in norm enumeration for %s: %s",
					n, v4l2element->device, g_strerror(errno));
				return FALSE;
			}
		}
		stdptr = g_malloc(sizeof(standard));
		memcpy(stdptr, &standard, sizeof(standard));
		v4l2element->norms = g_list_append(v4l2element->norms, stdptr);
	}

	/* and lastly, controls+menus (if appropriate) */
	for (n=V4L2_CID_BASE;;n++) {
		struct v4l2_queryctrl control, *ctrlptr;
		GList *menus = NULL;
		/* hacky... */
		if (n == V4L2_CID_LASTP1)
			n = V4L2_CID_PRIVATE_BASE;
		control.id = n;
		if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
			if (errno == EINVAL) {
				if (n < V4L2_CID_PRIVATE_BASE)
					continue;
				else
					break;
			} else {
				gst_element_error(GST_ELEMENT(v4l2element),
					"Failed to get no. %d in control enumeration for %s: %s",
					n, v4l2element->device, g_strerror(errno));
				return FALSE;
			}
		}
		if (control.flags & V4L2_CTRL_FLAG_DISABLED)
			continue;
		ctrlptr = g_malloc(sizeof(control));
		memcpy(ctrlptr, &control, sizeof(control));
		v4l2element->controls = g_list_append(v4l2element->controls, ctrlptr);
		if (control.type == V4L2_CTRL_TYPE_MENU) {
			struct v4l2_querymenu menu, *mptr;
			int i;
			menu.id = n;
			for (i=0;;i++) {
				menu.index = i;
				if (ioctl(v4l2element->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
					if (errno == EINVAL)
						break; /* end of enumeration */
					else {
						gst_element_error(GST_ELEMENT(v4l2element),
							"Failed to get no. %d in menu %d enumeration for %s: %s",
							i, n, v4l2element->device, g_strerror(errno));
						return FALSE;
					}
				}
				mptr = g_malloc(sizeof(menu));
				memcpy(mptr, &menu, sizeof(menu));
				menus = g_list_append(menus, mptr);
			}
		}
		v4l2element->menus = g_list_append(v4l2element->menus, menus);
	}

	return TRUE;
}


static void
gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
{
	DEBUG("deleting enumerations");

	/* empty lists */
	while (g_list_length(v4l2element->inputs) > 0) {
		gpointer data = g_list_nth_data(v4l2element->inputs, 0);
		v4l2element->inputs = g_list_remove(v4l2element->inputs, data);
		g_free(data);
	}
	while (g_list_length(v4l2element->outputs) > 0) {
		gpointer data = g_list_nth_data(v4l2element->outputs, 0);
		v4l2element->outputs = g_list_remove(v4l2element->outputs, data);
		g_free(data);
	}
	while (g_list_length(v4l2element->norms) > 0) {
		gpointer data = g_list_nth_data(v4l2element->norms, 0);
		v4l2element->norms = g_list_remove(v4l2element->norms, data);
		g_free(data);
	}
	while (g_list_length(v4l2element->controls) > 0) {
		gpointer data = g_list_nth_data(v4l2element->controls, 0);
		v4l2element->controls = g_list_remove(v4l2element->controls, data);
		g_free(data);
	}
	v4l2element->menus = g_list_remove_all(v4l2element->menus, NULL);
	while (g_list_length(v4l2element->menus) > 0) {
		GList *items = (GList *) g_list_nth_data(v4l2element->menus, 0);
		v4l2element->inputs = g_list_remove(v4l2element->inputs, items);
		while (g_list_length(items) > 0) {
			gpointer data = g_list_nth_data(v4l2element->menus, 0);
			items = g_list_remove(items, data);
			g_free(data);
		}
	}
}


/******************************************************
 * gst_v4l2_open():
 *   open the video device (v4l2element->device)
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_open (GstV4l2Element *v4l2element)
{
	DEBUG("Trying to open device %s", v4l2element->device);
	GST_V4L2_CHECK_NOT_OPEN(v4l2element);
	GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);

	/* be sure we have a device */
	if (!v4l2element->device)
		v4l2element->device = g_strdup("/dev/video");

	/* open the device */
	v4l2element->video_fd = open(v4l2element->device, O_RDWR);
	if (!GST_V4L2_IS_OPEN(v4l2element)) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to open device %s: %s",
			v4l2element->device, g_strerror(errno));
		goto error;
	}

	/* get capabilities */
	if (!gst_v4l2_get_capabilities(v4l2element)) {
		goto error;
	}

	/* create enumerations */
	if (!gst_v4l2_fill_lists(v4l2element))
		goto error;

	gst_info("Opened device '%s' (%s) successfully\n",
		v4l2element->vcap.card, v4l2element->device);

	return TRUE;

error:
	if (GST_V4L2_IS_OPEN(v4l2element)) {
		/* close device */
		close(v4l2element->video_fd);
		v4l2element->video_fd = -1;
	}
	/* empty lists */
	gst_v4l2_empty_lists(v4l2element);

	return FALSE;
}


/******************************************************
 * gst_v4l2_close():
 *   close the video device (v4l2element->video_fd)
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_close (GstV4l2Element *v4l2element)
{
	DEBUG("Trying to close %s", v4l2element->device);
	GST_V4L2_CHECK_OPEN(v4l2element);
	GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);

	/* close device */
	close(v4l2element->video_fd);
	v4l2element->video_fd = -1;

	/* empty lists */
	gst_v4l2_empty_lists(v4l2element);

	return TRUE;
}


/******************************************************
 * gst_v4l2_get_norm()
 *   Get the norm of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_norm (GstV4l2Element *v4l2element,
                   gint           *norm)
{
	v4l2_std_id std_id;
	gint n;

	DEBUG("getting norm");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, &std_id) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to get the current norm for device %s: %s",
			v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	/* try to find out what norm number this actually is */
	for (n=0;n<g_list_length(v4l2element->norms);n++) {
		struct v4l2_standard *stdptr = (struct v4l2_standard *) g_list_nth_data(v4l2element->norms, n);
		if (stdptr->id == std_id) {
			*norm = n;
			return TRUE;
		}
	}

	gst_element_error(GST_ELEMENT(v4l2element),
		"Failed to find norm '%llu' in our list of available norms for device %s",
		std_id, v4l2element->device);
	return FALSE;
}


/******************************************************
 * gst_v4l2_set_norm()
 *   Set the norm of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_norm (GstV4l2Element *v4l2element,
                   gint            norm)
{
	struct v4l2_standard *standard;

	DEBUG("trying to set norm to %d", norm);
	GST_V4L2_CHECK_OPEN(v4l2element);
	GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);

	if (norm < 0 || norm >= g_list_length(v4l2element->norms)) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Invalid norm number %d (%d-%d)",
			norm, 0, g_list_length(v4l2element->norms));
		return FALSE;
	}

	standard = (struct v4l2_standard *) g_list_nth_data(v4l2element->norms, norm);

	if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &standard->id) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to set norm '%s' (%llu) for device %s: %s",
			standard->name, standard->id, v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	return TRUE;
}


/******************************************************
 * gst_v4l2_get_norm_names()
 *   Get the list of available norms
 * return value: the list
 ******************************************************/

GList *
gst_v4l2_get_norm_names (GstV4l2Element *v4l2element)
{
	GList *names = NULL;
	gint n;

	DEBUG("getting a list of norm names");

	for (n=0;n<g_list_length(v4l2element->norms);n++) {
		struct v4l2_standard *standard = (struct v4l2_standard *) g_list_nth_data(v4l2element->norms, n);
		names = g_list_append(names, g_strdup(standard->name));
	}

	return names;
}


/******************************************************
 * gst_v4l2_get_input()
 *   Get the input of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_input (GstV4l2Element *v4l2element,
                    gint           *input)
{
	gint n;

	DEBUG("trying to get input");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (ioctl(v4l2element->video_fd, VIDIOC_G_INPUT, &n) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to get current input on device %s: %s",
			v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	*input = n;

	return TRUE;
}


/******************************************************
 * gst_v4l2_set_input()
 *   Set the input of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_input (GstV4l2Element *v4l2element,
                    gint            input)
{
	DEBUG("trying to set input to %d", input);
	GST_V4L2_CHECK_OPEN(v4l2element);
	GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);

	if (input < 0 || input >= g_list_length(v4l2element->inputs)) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Invalid input number %d (%d-%d)",
			input, 0, g_list_length(v4l2element->inputs));
		return FALSE;
	}

	if (ioctl(v4l2element->video_fd, VIDIOC_S_INPUT, &input) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to set input %d on device %s: %s",
			input, v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	return TRUE;
}


/******************************************************
 * gst_v4l2_get_input_names()
 *   Get the list of available input channels
 * return value: the list
 ******************************************************/

GList *
gst_v4l2_get_input_names (GstV4l2Element *v4l2element)
{
	GList *names = NULL;
	gint n;

	DEBUG("getting a list of input names");

	for (n=0;n<g_list_length(v4l2element->inputs);n++) {
		struct v4l2_input *input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, n);
		names = g_list_append(names, g_strdup(input->name));
	}

	return names;
}


/******************************************************
 * gst_v4l2_get_output()
 *   Get the output of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_output (GstV4l2Element *v4l2element,
                     gint           *output)
{
	gint n;

	DEBUG("trying to get output");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (ioctl(v4l2element->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to get current output on device %s: %s",
			v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	*output = n;

	return TRUE;
}


/******************************************************
 * gst_v4l2_set_output()
 *   Set the output of the current device
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_output (GstV4l2Element *v4l2element,
                     gint            output)
{
	DEBUG("trying to set output to %d", output);
	GST_V4L2_CHECK_OPEN(v4l2element);
	GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);

	if (output < 0 || output >= g_list_length(v4l2element->outputs)) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Invalid output number %d (%d-%d)",
			output, 0, g_list_length(v4l2element->outputs));
		return FALSE;
	}

	if (ioctl(v4l2element->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to set output %d on device %s: %s",
			output, v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	return TRUE;
}


/******************************************************
 * gst_v4l2_get_output_names()
 *   Get the list of available output channels
 * return value: the list, or NULL on error
 ******************************************************/

GList *
gst_v4l2_get_output_names (GstV4l2Element *v4l2element)
{
	GList *names = NULL;
	gint n;

	DEBUG("getting a list of output names");

	for (n=0;n<g_list_length(v4l2element->outputs);n++) {
		struct v4l2_output *output = (struct v4l2_output *) g_list_nth_data(v4l2element->outputs, n);
		names = g_list_append(names, g_strdup(output->name));
	}

	return names;
}


/******************************************************
 * gst_v4l_has_tuner():
 *   Check whether the device has a tuner
 * return value: TRUE if it has a tuner, else FALSE
 ******************************************************/

gint
gst_v4l2_has_tuner (GstV4l2Element *v4l2element,
                    gint           *tuner_num)
{
	gint input_num;
	struct v4l2_input *input;

	DEBUG("detecting whether device has a tuner");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (!gst_v4l2_get_input(v4l2element, &input_num))
		return FALSE;

	input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, input_num);

	if (input->type == V4L2_INPUT_TYPE_TUNER &&
	    v4l2element->vcap.capabilities & V4L2_CAP_TUNER) {
		*tuner_num = input->tuner;
		return TRUE;
	}
	return FALSE;
}


/******************************************************
 * gst_v4l_get_frequency():
 *   get the current frequency
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
                        gulong         *frequency)
{
	struct v4l2_frequency freq;

	DEBUG("getting current tuner frequency");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (!gst_v4l2_has_tuner(v4l2element, &freq.tuner))
		return FALSE;

	freq.type = 0;

	if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to get current tuner frequency for device %s: %s",
			v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	*frequency = freq.frequency;

	return TRUE;
}


/******************************************************
 * gst_v4l_set_frequency():
 *   set frequency
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
                        gulong          frequency)
{
	struct v4l2_frequency freq;

	DEBUG("setting current tuner frequency to %lu", frequency);
	GST_V4L2_CHECK_OPEN(v4l2element);
	GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);

	if (!gst_v4l2_has_tuner(v4l2element, &freq.tuner))
		return FALSE;

	freq.frequency = frequency;
	freq.type = 0;

	if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to set tuner frequency to %lu for device %s: %s",
			frequency, v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	return TRUE;
}


/******************************************************
 * gst_v4l_signal_strength():
 *   get the strength of the signal on the current input
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
                          gulong         *signal_strength)
{
	struct v4l2_tuner tuner;

	DEBUG("trying to get signal strength");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to get signal strength for device %s: %s",
			v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	*signal_strength = tuner.signal;

	return TRUE;
}


/******************************************************
 * gst_v4l_has_audio():
 *   Check whether the device has audio capabilities
 * return value: TRUE if it has a tuner, else FALSE
 ******************************************************/

gboolean
gst_v4l2_has_audio (GstV4l2Element *v4l2element)
{
	gint input_num;
	struct v4l2_input *input;

	DEBUG("detecting whether device has audio");
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (!gst_v4l2_get_input(v4l2element, &input_num))
		return FALSE;

	input = (struct v4l2_input *) g_list_nth_data(v4l2element->inputs, input_num);

	return (input->audioset != 0);
}


/******************************************************
 * gst_v4l_get_attributes():
 *   get a list of attributes available on this device
 * return value: the list
 ******************************************************/

GList *
gst_v4l2_get_attributes	(GstV4l2Element *v4l2element)
{
	gint i;
	GList *list = NULL;

	DEBUG("getting a list of available attributes");

	for (i=0;i<g_list_length(v4l2element->controls);i++) {
		struct v4l2_queryctrl *control = (struct v4l2_queryctrl *) g_list_nth_data(v4l2element->controls, i);
		GstV4l2Attribute* attribute = g_malloc(sizeof(GstV4l2Attribute));
		attribute->name = g_strdup(control->name);
		attribute->index = i;
		attribute->list_items = NULL;
		attribute->val_type = control->type;
		if (control->type == V4L2_CTRL_TYPE_MENU) {
			/* list items */
			gint n;
			GList *menus = (GList *) g_list_nth_data(v4l2element->menus, i);
			for (n=0;n<g_list_length(menus);n++) {
				struct v4l2_querymenu *menu = g_list_nth_data(menus, n);
				attribute->list_items = g_list_append(attribute->list_items,
									g_strdup(menu->name));
			}
		}
		switch (control->id) {
			case V4L2_CID_BRIGHTNESS:
			case V4L2_CID_CONTRAST:
			case V4L2_CID_SATURATION:
			case V4L2_CID_HUE:
			case V4L2_CID_BLACK_LEVEL:
			case V4L2_CID_AUTO_WHITE_BALANCE:
			case V4L2_CID_DO_WHITE_BALANCE:
			case V4L2_CID_RED_BALANCE:
			case V4L2_CID_BLUE_BALANCE:
			case V4L2_CID_GAMMA:
			case V4L2_CID_EXPOSURE:
			case V4L2_CID_AUTOGAIN:
			case V4L2_CID_GAIN:
			case V4L2_CID_HFLIP:
			case V4L2_CID_VFLIP:
			case V4L2_CID_HCENTER:
			case V4L2_CID_VCENTER:
				attribute->type = GST_V4L2_ATTRIBUTE_TYPE_VIDEO;
				break;
			case V4L2_CID_AUDIO_VOLUME:
			case V4L2_CID_AUDIO_BALANCE:
			case V4L2_CID_AUDIO_BASS:
			case V4L2_CID_AUDIO_TREBLE:
			case V4L2_CID_AUDIO_MUTE:
			case V4L2_CID_AUDIO_LOUDNESS:
				attribute->type = GST_V4L2_ATTRIBUTE_TYPE_AUDIO;
				break;
			default:
				attribute->type = GST_V4L2_ATTRIBUTE_TYPE_OTHER;
				break;
		}
		gst_v4l2_get_attribute(v4l2element, i, &attribute->value);
		attribute->min = control->minimum;
		attribute->max = control->maximum;
	}

	return list;
}


/******************************************************
 * gst_v4l_get_attribute():
 *   try to get the value of one specific attribute
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_get_attribute	(GstV4l2Element *v4l2element,
                         gint            attribute_num,
                         gint           *value)
{
	struct v4l2_control control;

	DEBUG("getting value of attribute %d", attribute_num);
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (attribute_num < 0 || attribute_num >= g_list_length(v4l2element->controls)) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Invalid control ID %d", attribute_num);
		return FALSE;
	}

	control.id = attribute_num;

	if (ioctl(v4l2element->video_fd, VIDIOC_G_CTRL, &control) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to get value for control %d on device %s: %s",
			attribute_num, v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	*value = control.value;

	return TRUE;
}


/******************************************************
 * gst_v4l_set_attribute():
 *   try to set the value of one specific attribute
 * return value: TRUE on success, FALSE on error
 ******************************************************/

gboolean
gst_v4l2_set_attribute	(GstV4l2Element *v4l2element,
                         gint            attribute_num,
                         gint            value)
{
	struct v4l2_control control;

	DEBUG("setting value of attribute %d to %d", attribute_num, value);
	GST_V4L2_CHECK_OPEN(v4l2element);

	if (attribute_num < 0 || attribute_num >= g_list_length(v4l2element->controls)) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Invalid control ID %d", attribute_num);
		return FALSE;
	}

	control.id = attribute_num;
	control.value = value;

	if (ioctl(v4l2element->video_fd, VIDIOC_S_CTRL, &control) < 0) {
		gst_element_error(GST_ELEMENT(v4l2element),
			"Failed to set value %d for control %d on device %s: %s",
			value, attribute_num, v4l2element->device, g_strerror(errno));
		return FALSE;
	}

	return TRUE;
}