sys/v4l2/: add norm, channel and frequency properties.
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / v4l2_calls.c
1 /* G-Streamer generic V4L2 element - generic V4L2 calls handling
2  * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include "v4l2_calls.h"
33 #include "gstv4l2tuner.h"
34 #include "gstv4l2xoverlay.h"
35 #include "gstv4l2colorbalance.h"
36
37 #include "gstv4l2src.h"
38
39 #define DEBUG(format, args...) \
40         GST_DEBUG_OBJECT (\
41                 GST_ELEMENT(v4l2element), \
42                 "V4L2: " format, ##args)
43
44
45 /******************************************************
46  * gst_v4l2_get_capabilities():
47  *   get the device's capturing capabilities
48  * return value: TRUE on success, FALSE on error
49  ******************************************************/
50
51 static gboolean
52 gst_v4l2_get_capabilities (GstV4l2Element *v4l2element)
53 {
54         DEBUG("getting capabilities");
55         GST_V4L2_CHECK_OPEN(v4l2element);
56
57         if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCAP, &(v4l2element->vcap)) < 0) {
58                 gst_element_error(GST_ELEMENT(v4l2element),
59                         "Error getting %s capabilities: %s",
60                         v4l2element->device, g_strerror(errno));
61                 return FALSE;
62         }
63
64         return TRUE;
65 }
66
67
68 /******************************************************
69  * gst_v4l2_empty_lists() and gst_v4l2_fill_lists():
70  *   fill/empty the lists of enumerations
71  * return value: TRUE on success, FALSE on error
72  ******************************************************/
73
74 static gboolean
75 gst_v4l2_fill_lists (GstV4l2Element *v4l2element)
76 {
77         gint n;
78         const GList *pads =
79                 gst_element_get_pad_list (GST_ELEMENT (v4l2element));
80         GstPadDirection dir = GST_PAD_UNKNOWN;
81
82         DEBUG("getting enumerations");
83         GST_V4L2_CHECK_OPEN(v4l2element);
84
85         /* sinks have outputs, all others have inputs */
86         if (pads && g_list_length ((GList *) pads) == 1)
87                 dir = GST_PAD_DIRECTION (GST_PAD (pads->data));
88
89         if (dir != GST_PAD_SINK) {
90         /* and now, the inputs */
91                 for (n=0;;n++) {
92                         struct v4l2_input input;
93                         GstV4l2TunerChannel *v4l2channel;
94                         GstTunerChannel *channel;
95
96                         input.index = n;
97                         if (ioctl(v4l2element->video_fd, VIDIOC_ENUMINPUT,
98                                   &input) < 0) {
99                                 if (errno == EINVAL)
100                                         break; /* end of enumeration */
101                                 else {
102                                         gst_element_error(GST_ELEMENT(v4l2element),
103                                                 "Failed to get no. %d in input enumeration for %s: %s",
104                                                 n, v4l2element->device,
105                                                 g_strerror(errno));
106                                         return FALSE;
107                                 }
108                         }
109
110                         v4l2channel =
111                                 g_object_new(GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
112                         channel = GST_TUNER_CHANNEL(v4l2channel);
113                         channel->label = g_strdup(input.name);
114                         channel->flags = GST_TUNER_CHANNEL_INPUT;
115                         v4l2channel->index = n;
116                         if (input.type == V4L2_INPUT_TYPE_TUNER) {
117                                 struct v4l2_tuner vtun;
118
119                                 v4l2channel->tuner = input.tuner;
120                                 channel->flags |= GST_TUNER_CHANNEL_FREQUENCY;
121
122                                 vtun.index = input.tuner;
123                                 if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER,
124                                           &vtun) < 0) {
125                                         gst_element_error(GST_ELEMENT(v4l2element),
126                                                 "Failed to get tuner %d settings on %s: %s",
127                                                 input.tuner,
128                                                 v4l2element->device,
129                                                 g_strerror(errno));
130                                         g_object_unref(G_OBJECT(channel));
131                                         return FALSE;
132                                 }
133                                 channel->min_frequency = vtun.rangelow;
134                                 channel->max_frequency = vtun.rangehigh;
135                                 channel->min_signal = 0;
136                                 channel->max_signal = 0xffff;
137                         }
138                         if (input.audioset) {
139                                 /* we take the first. We don't care for
140                                  * the others for now */
141                                 while (!(input.audioset &
142                                          (1<<v4l2channel->audio)))
143                                         v4l2channel->audio++;
144                                 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
145                         }
146
147                         v4l2element->channels =
148                                 g_list_append(v4l2element->channels,
149                                               (gpointer) channel);
150                 }
151         } else {
152                 /* outputs */
153                 for (n=0;;n++) {
154                         struct v4l2_output output;
155                         GstV4l2TunerChannel *v4l2channel;
156                         GstTunerChannel *channel;
157
158                         output.index = n;
159                         if (ioctl(v4l2element->video_fd, VIDIOC_ENUMOUTPUT,
160                                   &output) < 0) {
161                                 if (errno == EINVAL)
162                                         break; /* end of enumeration */
163                                 else {
164                                         gst_element_error(GST_ELEMENT(v4l2element),
165                                                 "Failed to get no. %d in output enumeration for %s: %s",
166                                                 n, v4l2element->device,
167                                                 g_strerror(errno));
168                                         return FALSE;
169                                 }
170                         }
171
172                         v4l2channel = g_object_new(GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
173                         channel = GST_TUNER_CHANNEL(v4l2channel);
174                         channel->label = g_strdup(output.name);
175                         channel->flags = GST_TUNER_CHANNEL_OUTPUT;
176                         v4l2channel->index = n;
177                         if (output.audioset) {
178                                 /* we take the first. We don't care for
179                                  * the others for now */
180                                 while (!(output.audioset &
181                                          (1<<v4l2channel->audio)))
182                                         v4l2channel->audio++;
183                                 channel->flags |= GST_TUNER_CHANNEL_AUDIO;
184                         }
185
186                         v4l2element->channels =
187                                 g_list_append(v4l2element->channels,
188                                               (gpointer) channel);
189                 }
190         }
191
192         /* norms... */
193         for (n=0;;n++) {
194                 struct v4l2_standard standard;
195                 GstV4l2TunerNorm *v4l2norm;
196                 GstTunerNorm *norm;
197
198                 standard.index = n;
199                 if (ioctl(v4l2element->video_fd, VIDIOC_ENUMSTD, &standard) < 0) {
200                         if (errno == EINVAL)
201                                 break; /* end of enumeration */
202                         else {
203                                 gst_element_error(GST_ELEMENT(v4l2element),
204                                         "Failed to get no. %d in norm enumeration for %s: %s",
205                                         n, v4l2element->device, g_strerror(errno));
206                                 return FALSE;
207                         }
208                 }
209
210                 v4l2norm = g_object_new(GST_TYPE_V4L2_TUNER_NORM, NULL);
211                 norm = GST_TUNER_NORM (v4l2norm);
212                 norm->label = g_strdup(standard.name);
213                 norm->fps = (gfloat) standard.frameperiod.denominator /
214                                 standard.frameperiod.numerator;
215                 v4l2norm->index = standard.id;
216
217                 v4l2element->norms = g_list_append(v4l2element->norms,
218                                                    (gpointer) norm);
219         }
220
221         /* and lastly, controls+menus (if appropriate) */
222         for (n=V4L2_CID_BASE;;n++) {
223                 struct v4l2_queryctrl control;
224                 GstV4l2ColorBalanceChannel *v4l2channel;
225                 GstColorBalanceChannel *channel;
226
227                 /* hacky... */
228                 if (n == V4L2_CID_LASTP1)
229                         n = V4L2_CID_PRIVATE_BASE;
230
231                 control.id = n;
232                 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
233                         if (errno == EINVAL) {
234                                 if (n < V4L2_CID_PRIVATE_BASE)
235                                         continue;
236                                 else
237                                         break;
238                         } else {
239                                 gst_element_error(GST_ELEMENT(v4l2element),
240                                         "Failed to get no. %d in control enumeration for %s: %s",
241                                         n, v4l2element->device, g_strerror(errno));
242                                 return FALSE;
243                         }
244                 }
245                 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
246                         continue;
247
248                 switch (n) {
249                         case V4L2_CID_BRIGHTNESS:
250                         case V4L2_CID_CONTRAST:
251                         case V4L2_CID_SATURATION:
252                         case V4L2_CID_HUE:
253                         case V4L2_CID_BLACK_LEVEL:
254                         case V4L2_CID_AUTO_WHITE_BALANCE:
255                         case V4L2_CID_DO_WHITE_BALANCE:
256                         case V4L2_CID_RED_BALANCE:
257                         case V4L2_CID_BLUE_BALANCE:
258                         case V4L2_CID_GAMMA:
259                         case V4L2_CID_EXPOSURE:
260                         case V4L2_CID_AUTOGAIN:
261                         case V4L2_CID_GAIN:
262                                 /* we only handle these for now */
263                                 break;
264                         default:
265                                 DEBUG("ControlID %s (%d) unhandled, FIXME",
266                                       control.name, n);
267                                 control.id++;
268                                 break;
269                 }
270                 if (n != control.id)
271                         continue;
272
273                 v4l2channel = g_object_new(GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL,
274                                            NULL);
275                 channel = GST_COLOR_BALANCE_CHANNEL(v4l2channel);
276                 channel->label = g_strdup(control.name);
277                 v4l2channel->index = n;
278
279 #if 0
280                 if (control.type == V4L2_CTRL_TYPE_MENU) {
281                         struct v4l2_querymenu menu, *mptr;
282                         int i;
283                         menu.id = n;
284                         for (i=0;;i++) {
285                                 menu.index = i;
286                                 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
287                                         if (errno == EINVAL)
288                                                 break; /* end of enumeration */
289                                         else {
290                                                 gst_element_error(GST_ELEMENT(v4l2element),
291                                                         "Failed to get no. %d in menu %d enumeration for %s: %s",
292                                                         i, n, v4l2element->device, g_strerror(errno));
293                                                 return FALSE;
294                                         }
295                                 }
296                                 mptr = g_malloc(sizeof(menu));
297                                 memcpy(mptr, &menu, sizeof(menu));
298                                 menus = g_list_append(menus, mptr);
299                         }
300                 }
301                 v4l2element->menus = g_list_append(v4l2element->menus, menus);
302 #endif
303
304                 switch (control.type) {
305                         case V4L2_CTRL_TYPE_INTEGER:
306                                 channel->min_value = control.minimum;
307                                 channel->max_value = control.maximum;
308                                 break;
309                         case V4L2_CTRL_TYPE_BOOLEAN:
310                                 channel->min_value = FALSE;
311                                 channel->max_value = TRUE;
312                                 break;
313                         default:
314                                 channel->min_value =
315                                         channel->max_value = 0;
316                                 break;
317                 }
318
319                 v4l2element->colors = g_list_append(v4l2element->colors,
320                                                     (gpointer) channel);
321         }
322
323         return TRUE;
324 }
325
326
327 static void
328 gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
329 {
330         DEBUG("deleting enumerations");
331
332         g_list_foreach (v4l2element->channels, (GFunc) g_object_unref, NULL);
333         g_list_free (v4l2element->channels);
334         v4l2element->channels = NULL;
335
336         g_list_foreach (v4l2element->norms, (GFunc) g_object_unref, NULL);
337         g_list_free (v4l2element->norms);
338         v4l2element->norms = NULL;
339
340         g_list_foreach (v4l2element->colors, (GFunc) g_object_unref, NULL);
341         g_list_free (v4l2element->colors);
342         v4l2element->colors = NULL;
343 }
344
345 /* FIXME: move this stuff to gstv4l2tuner.c? */
346
347 static void
348 gst_v4l2_set_defaults (GstV4l2Element *v4l2element)
349 {
350   GstTunerNorm *norm = NULL;
351   GstTunerChannel *channel = NULL;
352   GstTuner *tuner = GST_TUNER (v4l2element);
353   
354   if (v4l2element->norm)
355     norm = gst_tuner_find_norm_by_name (tuner, v4l2element->norm);
356   if (norm) {
357     gst_tuner_set_norm (tuner, norm);
358   } else {
359     norm = GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2element)));
360     v4l2element->norm = g_strdup (norm->label);
361     gst_tuner_norm_changed (tuner, norm);
362     g_object_notify (G_OBJECT (v4l2element), "norm"); 
363   }
364   
365   if (v4l2element->channel) 
366     channel = gst_tuner_find_channel_by_name (tuner, v4l2element->channel);
367   if (channel) {
368     gst_tuner_set_channel (tuner, channel);
369   } else {
370     channel = GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER (v4l2element)));
371     v4l2element->channel = g_strdup (channel->label);
372     gst_tuner_channel_changed (tuner, channel);
373     g_object_notify (G_OBJECT (v4l2element), "channel"); 
374   }
375   if (v4l2element->frequency != 0) {
376     gst_tuner_set_frequency (tuner, channel, v4l2element->frequency);
377   } else {
378     v4l2element->frequency = gst_tuner_get_frequency (tuner, channel);
379     if (v4l2element->frequency == 0) {
380       /* guess */
381       gst_tuner_set_frequency (tuner, channel, 1000);
382     } else {
383       g_object_notify (G_OBJECT (v4l2element), "frequency");
384     }
385   }
386 }
387
388
389 /******************************************************
390  * gst_v4l2_open():
391  *   open the video device (v4l2element->device)
392  * return value: TRUE on success, FALSE on error
393  ******************************************************/
394
395 gboolean
396 gst_v4l2_open (GstV4l2Element *v4l2element)
397 {
398         DEBUG("Trying to open device %s", v4l2element->device);
399         GST_V4L2_CHECK_NOT_OPEN(v4l2element);
400         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
401
402         /* be sure we have a device */
403         if (!v4l2element->device)
404                 v4l2element->device = g_strdup("/dev/video");
405
406         /* open the device */
407         v4l2element->video_fd = open(v4l2element->device, O_RDWR);
408         if (!GST_V4L2_IS_OPEN(v4l2element)) {
409                 gst_element_error(GST_ELEMENT(v4l2element),
410                         "Failed to open device %s: %s",
411                         v4l2element->device, g_strerror(errno));
412                 goto error;
413         }
414
415         /* get capabilities */
416         if (!gst_v4l2_get_capabilities(v4l2element)) {
417                 goto error;
418         }
419
420         /* do we need to be a capture device? */
421         if (GST_IS_V4L2SRC(v4l2element) &&
422             !(v4l2element->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
423                 gst_element_error(GST_ELEMENT(v4l2element),
424                                   "Not a capture device (0x%x)",
425                                   v4l2element->vcap.capabilities);
426                 goto error;
427         }
428
429         /* create enumerations */
430         if (!gst_v4l2_fill_lists(v4l2element))
431                 goto error;
432
433         /* set defaults */
434         gst_v4l2_set_defaults (v4l2element);
435
436         GST_INFO_OBJECT (v4l2element, "Opened device '%s' (%s) successfully\n",
437                 v4l2element->vcap.card, v4l2element->device);
438
439         return TRUE;
440
441 error:
442         if (GST_V4L2_IS_OPEN(v4l2element)) {
443                 /* close device */
444                 close(v4l2element->video_fd);
445                 v4l2element->video_fd = -1;
446         }
447         /* empty lists */
448         gst_v4l2_empty_lists(v4l2element);
449
450         return FALSE;
451 }
452
453
454 /******************************************************
455  * gst_v4l2_close():
456  *   close the video device (v4l2element->video_fd)
457  * return value: TRUE on success, FALSE on error
458  ******************************************************/
459
460 gboolean
461 gst_v4l2_close (GstV4l2Element *v4l2element)
462 {
463         DEBUG("Trying to close %s", v4l2element->device);
464         GST_V4L2_CHECK_OPEN(v4l2element);
465         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
466
467         /* close device */
468         close(v4l2element->video_fd);
469         v4l2element->video_fd = -1;
470
471         /* empty lists */
472         gst_v4l2_empty_lists(v4l2element);
473
474         return TRUE;
475 }
476
477
478 /******************************************************
479  * gst_v4l2_get_norm()
480  *   Get the norm of the current device
481  * return value: TRUE on success, FALSE on error
482  ******************************************************/
483
484 gboolean
485 gst_v4l2_get_norm (GstV4l2Element *v4l2element,
486                    v4l2_std_id    *norm)
487 {
488         DEBUG("getting norm");
489         GST_V4L2_CHECK_OPEN(v4l2element);
490
491         if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, norm) < 0) {
492                 gst_element_error(GST_ELEMENT(v4l2element),
493                         "Failed to get the current norm for device %s: %s",
494                         v4l2element->device, g_strerror(errno));
495                 return FALSE;
496         }
497
498         return TRUE;
499 }
500
501
502 /******************************************************
503  * gst_v4l2_set_norm()
504  *   Set the norm of the current device
505  * return value: TRUE on success, FALSE on error
506  ******************************************************/
507
508 gboolean
509 gst_v4l2_set_norm (GstV4l2Element *v4l2element,
510                    v4l2_std_id     norm)
511 {
512         DEBUG("trying to set norm to %llx", norm);
513         GST_V4L2_CHECK_OPEN(v4l2element);
514         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
515
516         if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &norm) < 0) {
517                 gst_element_error(GST_ELEMENT(v4l2element),
518                         "Failed to set norm 0x%llx for device %s: %s",
519                         norm, v4l2element->device, g_strerror(errno));
520                 return FALSE;
521         }
522
523         return TRUE;
524 }
525
526
527 /******************************************************
528  * gst_v4l2_get_input()
529  *   Get the input of the current device
530  * return value: TRUE on success, FALSE on error
531  ******************************************************/
532
533 gboolean
534 gst_v4l2_get_input (GstV4l2Element *v4l2element,
535                     gint           *input)
536 {
537         gint n;
538
539         DEBUG("trying to get input");
540         GST_V4L2_CHECK_OPEN(v4l2element);
541
542         if (ioctl(v4l2element->video_fd, VIDIOC_G_INPUT, &n) < 0) {
543                 gst_element_error(GST_ELEMENT(v4l2element),
544                         "Failed to get current input on device %s: %s",
545                         v4l2element->device, g_strerror(errno));
546                 return FALSE;
547         }
548
549         *input = n;
550
551         return TRUE;
552 }
553
554
555 /******************************************************
556  * gst_v4l2_set_input()
557  *   Set the input of the current device
558  * return value: TRUE on success, FALSE on error
559  ******************************************************/
560
561 gboolean
562 gst_v4l2_set_input (GstV4l2Element *v4l2element,
563                     gint            input)
564 {
565         DEBUG("trying to set input to %d", input);
566         GST_V4L2_CHECK_OPEN(v4l2element);
567         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
568
569         if (ioctl(v4l2element->video_fd, VIDIOC_S_INPUT, &input) < 0) {
570                 gst_element_error(GST_ELEMENT(v4l2element),
571                         "Failed to set input %d on device %s: %s",
572                         input, v4l2element->device, g_strerror(errno));
573                 return FALSE;
574         }
575
576         return TRUE;
577 }
578
579
580 /******************************************************
581  * gst_v4l2_get_output()
582  *   Get the output of the current device
583  * return value: TRUE on success, FALSE on error
584  ******************************************************/
585
586 gboolean
587 gst_v4l2_get_output (GstV4l2Element *v4l2element,
588                      gint           *output)
589 {
590         gint n;
591
592         DEBUG("trying to get output");
593         GST_V4L2_CHECK_OPEN(v4l2element);
594
595         if (ioctl(v4l2element->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
596                 gst_element_error(GST_ELEMENT(v4l2element),
597                         "Failed to get current output on device %s: %s",
598                         v4l2element->device, g_strerror(errno));
599                 return FALSE;
600         }
601
602         *output = n;
603
604         return TRUE;
605 }
606
607
608 /******************************************************
609  * gst_v4l2_set_output()
610  *   Set the output of the current device
611  * return value: TRUE on success, FALSE on error
612  ******************************************************/
613
614 gboolean
615 gst_v4l2_set_output (GstV4l2Element *v4l2element,
616                      gint            output)
617 {
618         DEBUG("trying to set output to %d", output);
619         GST_V4L2_CHECK_OPEN(v4l2element);
620         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
621
622         if (ioctl(v4l2element->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
623                 gst_element_error(GST_ELEMENT(v4l2element),
624                         "Failed to set output %d on device %s: %s",
625                         output, v4l2element->device, g_strerror(errno));
626                 return FALSE;
627         }
628
629         return TRUE;
630 }
631
632
633 /******************************************************
634  * gst_v4l2_get_frequency():
635  *   get the current frequency
636  * return value: TRUE on success, FALSE on error
637  ******************************************************/
638
639 gboolean
640 gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
641                         gint            tunernum,
642                         gulong         *frequency)
643 {
644         struct v4l2_frequency freq;
645
646         DEBUG("getting current tuner frequency");
647         GST_V4L2_CHECK_OPEN(v4l2element);
648
649         freq.tuner = tunernum;
650         if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
651                 gst_element_error(GST_ELEMENT(v4l2element),
652                         "Failed to get current tuner frequency for device %s: %s",
653                         v4l2element->device, g_strerror(errno));
654                 return FALSE;
655         }
656
657         *frequency = freq.frequency;
658
659         return TRUE;
660 }
661
662
663 /******************************************************
664  * gst_v4l2_set_frequency():
665  *   set frequency
666  * return value: TRUE on success, FALSE on error
667  ******************************************************/
668
669 gboolean
670 gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
671                         gint            tunernum,
672                         gulong          frequency)
673 {
674         struct v4l2_frequency freq;
675
676         DEBUG("setting current tuner frequency to %lu", frequency);
677         GST_V4L2_CHECK_OPEN(v4l2element);
678         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
679
680         freq.tuner = tunernum;
681         /* fill in type - ignore error */
682         ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq);
683         freq.frequency = frequency;
684
685         if (ioctl(v4l2element->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0) {
686                 gst_element_error(GST_ELEMENT(v4l2element),
687                         "Failed to set tuner frequency to %lu for device %s: %s",
688                         frequency, v4l2element->device, g_strerror(errno));
689                 return FALSE;
690         }
691
692         return TRUE;
693 }
694
695
696 /******************************************************
697  * gst_v4l2_signal_strength():
698  *   get the strength of the signal on the current input
699  * return value: TRUE on success, FALSE on error
700  ******************************************************/
701
702 gboolean
703 gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
704                           gint            tunernum,
705                           gulong         *signal_strength)
706 {
707         struct v4l2_tuner tuner;
708
709         DEBUG("trying to get signal strength");
710         GST_V4L2_CHECK_OPEN(v4l2element);
711
712         tuner.index = tunernum;
713         if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
714                 gst_element_error(GST_ELEMENT(v4l2element),
715                         "Failed to get signal strength for device %s: %s",
716                         v4l2element->device, g_strerror(errno));
717                 return FALSE;
718         }
719
720         *signal_strength = tuner.signal;
721
722         return TRUE;
723 }
724
725
726 /******************************************************
727  * gst_v4l2_get_attribute():
728  *   try to get the value of one specific attribute
729  * return value: TRUE on success, FALSE on error
730  ******************************************************/
731
732 gboolean
733 gst_v4l2_get_attribute  (GstV4l2Element *v4l2element,
734                          int             attribute_num,
735                          int            *value)
736 {
737         struct v4l2_control control;
738
739         GST_V4L2_CHECK_OPEN(v4l2element);
740
741         DEBUG("getting value of attribute %d", attribute_num);
742
743         control.id = attribute_num;
744
745         if (ioctl(v4l2element->video_fd, VIDIOC_G_CTRL, &control) < 0) {
746                 gst_element_error(GST_ELEMENT(v4l2element),
747                         "Failed to get value for control %d on device %s: %s",
748                         attribute_num, v4l2element->device, g_strerror(errno));
749                 return FALSE;
750         }
751
752         *value = control.value;
753
754         return TRUE;
755 }
756
757
758 /******************************************************
759  * gst_v4l2_set_attribute():
760  *   try to set the value of one specific attribute
761  * return value: TRUE on success, FALSE on error
762  ******************************************************/
763
764 gboolean
765 gst_v4l2_set_attribute  (GstV4l2Element *v4l2element,
766                          int             attribute_num,
767                          const int       value)
768 {
769         struct v4l2_control control;
770
771         GST_V4L2_CHECK_OPEN(v4l2element);
772
773         DEBUG("setting value of attribute %d to %d", attribute_num, value);
774
775         control.id = attribute_num;
776         control.value = value;
777
778         if (ioctl(v4l2element->video_fd, VIDIOC_S_CTRL, &control) < 0) {
779                 gst_element_error(GST_ELEMENT(v4l2element),
780                         "Failed to set value %d for control %d on device %s: %s",
781                         value, attribute_num, v4l2element->device, g_strerror(errno));
782                 return FALSE;
783         }
784
785         return TRUE;
786 }
787