putting i18n in place for plugins
[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 (v4l2element, RESOURCE, SETTINGS, (""),
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 (v4l2element, RESOURCE, SETTINGS, (""),
103                                                            ("Failed to get %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 (v4l2element, RESOURCE, SETTINGS, (""),
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 (v4l2element, RESOURCE, SETTINGS, (""),
165                                                            ("Failed to get %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 (v4l2element, RESOURCE, SETTINGS, (""),
204                                                            ("Failed to get %d in norm enumeration for %s: %s",
205                                                 n, v4l2element->device,
206                                                 g_strerror (errno)));
207                                 return FALSE;
208                         }
209                 }
210
211                 v4l2norm = g_object_new(GST_TYPE_V4L2_TUNER_NORM, NULL);
212                 norm = GST_TUNER_NORM (v4l2norm);
213                 norm->label = g_strdup(standard.name);
214                 norm->fps = (gfloat) standard.frameperiod.denominator /
215                                 standard.frameperiod.numerator;
216                 v4l2norm->index = standard.id;
217
218                 v4l2element->norms = g_list_append(v4l2element->norms,
219                                                    (gpointer) norm);
220         }
221
222         /* and lastly, controls+menus (if appropriate) */
223         for (n=V4L2_CID_BASE;;n++) {
224                 struct v4l2_queryctrl control;
225                 GstV4l2ColorBalanceChannel *v4l2channel;
226                 GstColorBalanceChannel *channel;
227
228                 /* hacky... */
229                 if (n == V4L2_CID_LASTP1)
230                         n = V4L2_CID_PRIVATE_BASE;
231
232                 control.id = n;
233                 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYCTRL, &control) < 0) {
234                         if (errno == EINVAL) {
235                                 if (n < V4L2_CID_PRIVATE_BASE)
236                                         continue;
237                                 else
238                                         break;
239                         } else {
240                                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
241                                                    ("Failed to get %d in control enumeration for %s: %s",
242                                                 n, v4l2element->device,
243                                                 g_strerror (errno)));
244                                 return FALSE;
245                         }
246                 }
247                 if (control.flags & V4L2_CTRL_FLAG_DISABLED)
248                         continue;
249
250                 switch (n) {
251                         case V4L2_CID_BRIGHTNESS:
252                         case V4L2_CID_CONTRAST:
253                         case V4L2_CID_SATURATION:
254                         case V4L2_CID_HUE:
255                         case V4L2_CID_BLACK_LEVEL:
256                         case V4L2_CID_AUTO_WHITE_BALANCE:
257                         case V4L2_CID_DO_WHITE_BALANCE:
258                         case V4L2_CID_RED_BALANCE:
259                         case V4L2_CID_BLUE_BALANCE:
260                         case V4L2_CID_GAMMA:
261                         case V4L2_CID_EXPOSURE:
262                         case V4L2_CID_AUTOGAIN:
263                         case V4L2_CID_GAIN:
264                                 /* we only handle these for now */
265                                 break;
266                         default:
267                                 DEBUG("ControlID %s (%d) unhandled, FIXME",
268                                       control.name, n);
269                                 control.id++;
270                                 break;
271                 }
272                 if (n != control.id)
273                         continue;
274
275                 v4l2channel = g_object_new(GST_TYPE_V4L2_COLOR_BALANCE_CHANNEL,
276                                            NULL);
277                 channel = GST_COLOR_BALANCE_CHANNEL(v4l2channel);
278                 channel->label = g_strdup(control.name);
279                 v4l2channel->index = n;
280
281 #if 0
282                 if (control.type == V4L2_CTRL_TYPE_MENU) {
283                         struct v4l2_querymenu menu, *mptr;
284                         int i;
285                         menu.id = n;
286                         for (i=0;;i++) {
287                                 menu.index = i;
288                                 if (ioctl(v4l2element->video_fd, VIDIOC_QUERYMENU, &menu) < 0) {
289                                         if (errno == EINVAL)
290                                                 break; /* end of enumeration */
291                                         else {
292                                                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
293                                                            ("Failed to get %d in menu enumeration for %s: %s",
294                                                 n, v4l2element->device,
295                                                 g_strerror (errno)));
296                                                 return FALSE;
297                                         }
298                                 }
299                                 mptr = g_malloc(sizeof(menu));
300                                 memcpy(mptr, &menu, sizeof(menu));
301                                 menus = g_list_append(menus, mptr);
302                         }
303                 }
304                 v4l2element->menus = g_list_append(v4l2element->menus, menus);
305 #endif
306
307                 switch (control.type) {
308                         case V4L2_CTRL_TYPE_INTEGER:
309                                 channel->min_value = control.minimum;
310                                 channel->max_value = control.maximum;
311                                 break;
312                         case V4L2_CTRL_TYPE_BOOLEAN:
313                                 channel->min_value = FALSE;
314                                 channel->max_value = TRUE;
315                                 break;
316                         default:
317                                 channel->min_value =
318                                         channel->max_value = 0;
319                                 break;
320                 }
321
322                 v4l2element->colors = g_list_append(v4l2element->colors,
323                                                     (gpointer) channel);
324         }
325
326         return TRUE;
327 }
328
329
330 static void
331 gst_v4l2_empty_lists (GstV4l2Element *v4l2element)
332 {
333         DEBUG("deleting enumerations");
334
335         g_list_foreach (v4l2element->channels, (GFunc) g_object_unref, NULL);
336         g_list_free (v4l2element->channels);
337         v4l2element->channels = NULL;
338
339         g_list_foreach (v4l2element->norms, (GFunc) g_object_unref, NULL);
340         g_list_free (v4l2element->norms);
341         v4l2element->norms = NULL;
342
343         g_list_foreach (v4l2element->colors, (GFunc) g_object_unref, NULL);
344         g_list_free (v4l2element->colors);
345         v4l2element->colors = NULL;
346 }
347
348 /* FIXME: move this stuff to gstv4l2tuner.c? */
349
350 static void
351 gst_v4l2_set_defaults (GstV4l2Element *v4l2element)
352 {
353   GstTunerNorm *norm = NULL;
354   GstTunerChannel *channel = NULL;
355   GstTuner *tuner = GST_TUNER (v4l2element);
356   
357   if (v4l2element->norm)
358     norm = gst_tuner_find_norm_by_name (tuner, v4l2element->norm);
359   if (norm) {
360     gst_tuner_set_norm (tuner, norm);
361   } else {
362     norm = GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2element)));
363     v4l2element->norm = g_strdup (norm->label);
364     gst_tuner_norm_changed (tuner, norm);
365     g_object_notify (G_OBJECT (v4l2element), "norm"); 
366   }
367   
368   if (v4l2element->channel) 
369     channel = gst_tuner_find_channel_by_name (tuner, v4l2element->channel);
370   if (channel) {
371     gst_tuner_set_channel (tuner, channel);
372   } else {
373     channel = GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER (v4l2element)));
374     v4l2element->channel = g_strdup (channel->label);
375     gst_tuner_channel_changed (tuner, channel);
376     g_object_notify (G_OBJECT (v4l2element), "channel"); 
377   }
378   if (v4l2element->frequency != 0) {
379     gst_tuner_set_frequency (tuner, channel, v4l2element->frequency);
380   } else {
381     v4l2element->frequency = gst_tuner_get_frequency (tuner, channel);
382     if (v4l2element->frequency == 0) {
383       /* guess */
384       gst_tuner_set_frequency (tuner, channel, 1000);
385     } else {
386       g_object_notify (G_OBJECT (v4l2element), "frequency");
387     }
388   }
389 }
390
391
392 /******************************************************
393  * gst_v4l2_open():
394  *   open the video device (v4l2element->device)
395  * return value: TRUE on success, FALSE on error
396  ******************************************************/
397
398 gboolean
399 gst_v4l2_open (GstV4l2Element *v4l2element)
400 {
401         DEBUG("Trying to open device %s", v4l2element->device);
402         GST_V4L2_CHECK_NOT_OPEN(v4l2element);
403         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
404
405         /* be sure we have a device */
406         if (!v4l2element->device)
407                 v4l2element->device = g_strdup("/dev/video");
408
409         /* open the device */
410         v4l2element->video_fd = open(v4l2element->device, O_RDWR);
411         if (!GST_V4L2_IS_OPEN(v4l2element)) {
412                 gst_element_error (v4l2element, RESOURCE, OPEN_READ_WRITE,
413                                    (_("Could not open device \"%s\" for reading and writing"), v4l2element->device),
414                                    GST_ERROR_SYSTEM);
415                 goto error;
416         }
417
418         /* get capabilities */
419         if (!gst_v4l2_get_capabilities(v4l2element)) {
420                 goto error;
421         }
422
423         /* do we need to be a capture device? */
424         if (GST_IS_V4L2SRC(v4l2element) &&
425             !(v4l2element->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
426                 gst_element_error (v4l2element, RESOURCE, NOT_FOUND,
427                                    (_("Device \"%s\" is not a capture device"), v4l2element->device),
428                                    ("Capabilities: 0x%x", v4l2element->vcap.capabilities));
429                 goto error;
430         }
431
432         /* create enumerations */
433         if (!gst_v4l2_fill_lists(v4l2element))
434                 goto error;
435
436         /* set defaults */
437         gst_v4l2_set_defaults (v4l2element);
438
439         GST_INFO_OBJECT (v4l2element, "Opened device '%s' (%s) successfully\n",
440                 v4l2element->vcap.card, v4l2element->device);
441
442         return TRUE;
443
444 error:
445         if (GST_V4L2_IS_OPEN(v4l2element)) {
446                 /* close device */
447                 close(v4l2element->video_fd);
448                 v4l2element->video_fd = -1;
449         }
450         /* empty lists */
451         gst_v4l2_empty_lists(v4l2element);
452
453         return FALSE;
454 }
455
456
457 /******************************************************
458  * gst_v4l2_close():
459  *   close the video device (v4l2element->video_fd)
460  * return value: TRUE on success, FALSE on error
461  ******************************************************/
462
463 gboolean
464 gst_v4l2_close (GstV4l2Element *v4l2element)
465 {
466         DEBUG("Trying to close %s", v4l2element->device);
467         GST_V4L2_CHECK_OPEN(v4l2element);
468         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
469
470         /* close device */
471         close(v4l2element->video_fd);
472         v4l2element->video_fd = -1;
473
474         /* empty lists */
475         gst_v4l2_empty_lists(v4l2element);
476
477         return TRUE;
478 }
479
480
481 /******************************************************
482  * gst_v4l2_get_norm()
483  *   Get the norm of the current device
484  * return value: TRUE on success, FALSE on error
485  ******************************************************/
486
487 gboolean
488 gst_v4l2_get_norm (GstV4l2Element *v4l2element,
489                    v4l2_std_id    *norm)
490 {
491         DEBUG("getting norm");
492         GST_V4L2_CHECK_OPEN(v4l2element);
493
494         if (ioctl(v4l2element->video_fd, VIDIOC_G_STD, norm) < 0) {
495                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
496                         ("Failed to get the current norm for device %s: %s",
497                          v4l2element->device, g_strerror(errno)));
498                 return FALSE;
499         }
500
501         return TRUE;
502 }
503
504
505 /******************************************************
506  * gst_v4l2_set_norm()
507  *   Set the norm of the current device
508  * return value: TRUE on success, FALSE on error
509  ******************************************************/
510
511 gboolean
512 gst_v4l2_set_norm (GstV4l2Element *v4l2element,
513                    v4l2_std_id     norm)
514 {
515         DEBUG("trying to set norm to %llx", norm);
516         GST_V4L2_CHECK_OPEN(v4l2element);
517         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
518
519         if (ioctl(v4l2element->video_fd, VIDIOC_S_STD, &norm) < 0) {
520                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
521                         ("Failed to set norm 0x%llx for device %s: %s",
522                          norm, v4l2element->device, g_strerror(errno)));
523                 return FALSE;
524         }
525
526         return TRUE;
527 }
528
529
530 /******************************************************
531  * gst_v4l2_get_input()
532  *   Get the input of the current device
533  * return value: TRUE on success, FALSE on error
534  ******************************************************/
535
536 gboolean
537 gst_v4l2_get_input (GstV4l2Element *v4l2element,
538                     gint           *input)
539 {
540         gint n;
541
542         DEBUG("trying to get input");
543         GST_V4L2_CHECK_OPEN(v4l2element);
544
545         if (ioctl(v4l2element->video_fd, VIDIOC_G_INPUT, &n) < 0) {
546                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
547                         ("Failed to get current input on device %s: %s",
548                         v4l2element->device, g_strerror(errno)));
549                 return FALSE;
550         }
551
552         *input = n;
553
554         return TRUE;
555 }
556
557
558 /******************************************************
559  * gst_v4l2_set_input()
560  *   Set the input of the current device
561  * return value: TRUE on success, FALSE on error
562  ******************************************************/
563
564 gboolean
565 gst_v4l2_set_input (GstV4l2Element *v4l2element,
566                     gint            input)
567 {
568         DEBUG("trying to set input to %d", input);
569         GST_V4L2_CHECK_OPEN(v4l2element);
570         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
571
572         if (ioctl(v4l2element->video_fd, VIDIOC_S_INPUT, &input) < 0) {
573                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
574                         ("Failed to set input %d on device %s: %s",
575                          input, v4l2element->device, g_strerror(errno)));
576                 return FALSE;
577         }
578
579         return TRUE;
580 }
581
582
583 /******************************************************
584  * gst_v4l2_get_output()
585  *   Get the output of the current device
586  * return value: TRUE on success, FALSE on error
587  ******************************************************/
588
589 gboolean
590 gst_v4l2_get_output (GstV4l2Element *v4l2element,
591                      gint           *output)
592 {
593         gint n;
594
595         DEBUG("trying to get output");
596         GST_V4L2_CHECK_OPEN(v4l2element);
597
598         if (ioctl(v4l2element->video_fd, VIDIOC_G_OUTPUT, &n) < 0) {
599                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
600                         ("Failed to get current output on device %s: %s",
601                          v4l2element->device, g_strerror(errno)));
602                 return FALSE;
603         }
604
605         *output = n;
606
607         return TRUE;
608 }
609
610
611 /******************************************************
612  * gst_v4l2_set_output()
613  *   Set the output of the current device
614  * return value: TRUE on success, FALSE on error
615  ******************************************************/
616
617 gboolean
618 gst_v4l2_set_output (GstV4l2Element *v4l2element,
619                      gint            output)
620 {
621         DEBUG("trying to set output to %d", output);
622         GST_V4L2_CHECK_OPEN(v4l2element);
623         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
624
625         if (ioctl(v4l2element->video_fd, VIDIOC_S_OUTPUT, &output) < 0) {
626                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
627                         ("Failed to set output %d on device %s: %s",
628                          output, v4l2element->device, g_strerror(errno)));
629                 return FALSE;
630         }
631
632         return TRUE;
633 }
634
635
636 /******************************************************
637  * gst_v4l2_get_frequency():
638  *   get the current frequency
639  * return value: TRUE on success, FALSE on error
640  ******************************************************/
641
642 gboolean
643 gst_v4l2_get_frequency (GstV4l2Element *v4l2element,
644                         gint            tunernum,
645                         gulong         *frequency)
646 {
647         struct v4l2_frequency freq;
648
649         DEBUG("getting current tuner frequency");
650         GST_V4L2_CHECK_OPEN(v4l2element);
651
652         freq.tuner = tunernum;
653         if (ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq) < 0) {
654                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
655                         ("Failed to get current tuner frequency for device %s: %s",
656                         v4l2element->device, g_strerror(errno)));
657                 return FALSE;
658         }
659
660         *frequency = freq.frequency;
661
662         return TRUE;
663 }
664
665
666 /******************************************************
667  * gst_v4l2_set_frequency():
668  *   set frequency
669  * return value: TRUE on success, FALSE on error
670  ******************************************************/
671
672 gboolean
673 gst_v4l2_set_frequency (GstV4l2Element *v4l2element,
674                         gint            tunernum,
675                         gulong          frequency)
676 {
677         struct v4l2_frequency freq;
678
679         DEBUG("setting current tuner frequency to %lu", frequency);
680         GST_V4L2_CHECK_OPEN(v4l2element);
681         GST_V4L2_CHECK_NOT_ACTIVE(v4l2element);
682
683         freq.tuner = tunernum;
684         /* fill in type - ignore error */
685         ioctl(v4l2element->video_fd, VIDIOC_G_FREQUENCY, &freq);
686         freq.frequency = frequency;
687
688         if (ioctl(v4l2element->video_fd, VIDIOC_S_FREQUENCY, &freq) < 0) {
689                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
690                         ("Failed to set tuner frequency to %lu for device %s: %s",
691                         frequency, v4l2element->device, g_strerror(errno)));
692                 return FALSE;
693         }
694
695         return TRUE;
696 }
697
698
699 /******************************************************
700  * gst_v4l2_signal_strength():
701  *   get the strength of the signal on the current input
702  * return value: TRUE on success, FALSE on error
703  ******************************************************/
704
705 gboolean
706 gst_v4l2_signal_strength (GstV4l2Element *v4l2element,
707                           gint            tunernum,
708                           gulong         *signal_strength)
709 {
710         struct v4l2_tuner tuner;
711
712         DEBUG("trying to get signal strength");
713         GST_V4L2_CHECK_OPEN(v4l2element);
714
715         tuner.index = tunernum;
716         if (ioctl(v4l2element->video_fd, VIDIOC_G_TUNER, &tuner) < 0) {
717                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
718                         ("Failed to get signal strength for device %s: %s",
719                          v4l2element->device, g_strerror(errno)));
720                 return FALSE;
721         }
722
723         *signal_strength = tuner.signal;
724
725         return TRUE;
726 }
727
728
729 /******************************************************
730  * gst_v4l2_get_attribute():
731  *   try to get the value of one specific attribute
732  * return value: TRUE on success, FALSE on error
733  ******************************************************/
734
735 gboolean
736 gst_v4l2_get_attribute  (GstV4l2Element *v4l2element,
737                          int             attribute_num,
738                          int            *value)
739 {
740         struct v4l2_control control;
741
742         GST_V4L2_CHECK_OPEN(v4l2element);
743
744         DEBUG("getting value of attribute %d", attribute_num);
745
746         control.id = attribute_num;
747
748         if (ioctl(v4l2element->video_fd, VIDIOC_G_CTRL, &control) < 0) {
749                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
750                         ("Failed to get value for control %d on device %s: %s",
751                          attribute_num, v4l2element->device, g_strerror(errno)));
752                 return FALSE;
753         }
754
755         *value = control.value;
756
757         return TRUE;
758 }
759
760
761 /******************************************************
762  * gst_v4l2_set_attribute():
763  *   try to set the value of one specific attribute
764  * return value: TRUE on success, FALSE on error
765  ******************************************************/
766
767 gboolean
768 gst_v4l2_set_attribute  (GstV4l2Element *v4l2element,
769                          int             attribute_num,
770                          const int       value)
771 {
772         struct v4l2_control control;
773
774         GST_V4L2_CHECK_OPEN(v4l2element);
775
776         DEBUG("setting value of attribute %d to %d", attribute_num, value);
777
778         control.id = attribute_num;
779         control.value = value;
780
781         if (ioctl(v4l2element->video_fd, VIDIOC_S_CTRL, &control) < 0) {
782                 gst_element_error (v4l2element, RESOURCE, SETTINGS, (""),
783                         ("Failed to set value %d for control %d on device %s: %s",
784                         value, attribute_num, v4l2element->device, g_strerror(errno)));
785                 return FALSE;
786         }
787
788         return TRUE;
789 }
790