Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2radio.c
1 /* GStreamer v4l2 radio tuner element
2  * Copyright (C) 2010, 2011 Alexey Chernov <4ernov@gmail.com>
3  *
4  * gstv4l2radio.c - V4L2 radio tuner element
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-v4l2radio
24  *
25  * v4l2radio can be used to control radio device
26  * and to tune it to different radiostations.
27  *
28  * <refsect2>
29  * <title>Example launch lines</title>
30  * |[
31  * gst-launch v4l2radio device=/dev/radio0 frequency=101200000
32  * gst-launch alsasrc device=hw:1 ! audioconvert ! audioresample ! alsasink
33  * ]|
34  * First pipeline tunes the radio device /dev/radio0 to station 101.2 MHz,
35  * second pipeline connects digital audio out (hw:1) to default sound card.
36  * </refsect2>
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include <string.h>
44
45 #include "gst/gst-i18n-plugin.h"
46
47 #include "gstv4l2tuner.h"
48 #include "gstv4l2radio.h"
49 #include "v4l2_calls.h"
50
51 GST_DEBUG_CATEGORY_STATIC (v4l2radio_debug);
52 #define GST_CAT_DEFAULT v4l2radio_debug
53
54 #define DEFAULT_PROP_DEVICE "/dev/radio0"
55 #define MIN_FREQUENCY            87500000
56 #define DEFAULT_FREQUENCY       100000000
57 #define MAX_FREQUENCY           108000000
58
59 enum
60 {
61   ARG_0,
62   ARG_DEVICE,
63   ARG_FREQUENCY
64 };
65
66 static gboolean
67 gst_v4l2radio_fill_channel_list (GstV4l2Radio * radio)
68 {
69   int res;
70   struct v4l2_tuner vtun;
71   struct v4l2_capability vc;
72   GstV4l2TunerChannel *v4l2channel;
73   GstTunerChannel *channel;
74
75   GstElement *e;
76
77   GstV4l2Object *v4l2object;
78
79   e = GST_ELEMENT (radio);
80   v4l2object = radio->v4l2object;
81
82   GST_DEBUG_OBJECT (e, "getting audio enumeration");
83   GST_V4L2_CHECK_OPEN (v4l2object);
84
85   GST_DEBUG_OBJECT (e, "  audio input");
86
87   memset (&vc, 0, sizeof (vc));
88
89   res = v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &vc);
90   if (res < 0)
91     goto caps_failed;
92
93   if (!(vc.capabilities & V4L2_CAP_TUNER))
94     goto not_a_tuner;
95
96   /* getting audio input */
97   memset (&vtun, 0, sizeof (vtun));
98   vtun.index = 0;
99
100   res = v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun);
101   if (res < 0)
102     goto tuner_failed;
103
104   GST_LOG_OBJECT (e, "   index:     %d", vtun.index);
105   GST_LOG_OBJECT (e, "   name:      '%s'", vtun.name);
106   GST_LOG_OBJECT (e, "   type:      %016x", (guint) vtun.type);
107   GST_LOG_OBJECT (e, "   caps:      %016x", (guint) vtun.capability);
108   GST_LOG_OBJECT (e, "   rlow:      %016x", (guint) vtun.rangelow);
109   GST_LOG_OBJECT (e, "   rhigh:     %016x", (guint) vtun.rangehigh);
110   GST_LOG_OBJECT (e, "   audmode:   %016x", (guint) vtun.audmode);
111
112   v4l2channel = g_object_new (GST_TYPE_V4L2_TUNER_CHANNEL, NULL);
113   channel = GST_TUNER_CHANNEL (v4l2channel);
114   channel->label = g_strdup ((const gchar *) vtun.name);
115   channel->flags = GST_TUNER_CHANNEL_FREQUENCY | GST_TUNER_CHANNEL_AUDIO;
116   v4l2channel->index = 0;
117   v4l2channel->tuner = 0;
118
119   channel->freq_multiplicator =
120       62.5 * ((vtun.capability & V4L2_TUNER_CAP_LOW) ? 1 : 1000);
121   channel->min_frequency = vtun.rangelow * channel->freq_multiplicator;
122   channel->max_frequency = vtun.rangehigh * channel->freq_multiplicator;
123   channel->min_signal = 0;
124   channel->max_signal = 0xffff;
125
126   v4l2object->channels =
127       g_list_prepend (v4l2object->channels, (gpointer) channel);
128
129   v4l2object->channels = g_list_reverse (v4l2object->channels);
130
131   GST_DEBUG_OBJECT (e, "done");
132   return TRUE;
133
134   /* ERRORS */
135 tuner_failed:
136   {
137     GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
138         (_("Failed to get settings of tuner %d on device '%s'."),
139             vtun.index, v4l2object->videodev), GST_ERROR_SYSTEM);
140     return FALSE;
141   }
142 caps_failed:
143   {
144     GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
145         (_("Error getting capabilities for device '%s'."),
146             v4l2object->videodev), GST_ERROR_SYSTEM);
147     return FALSE;
148   }
149 not_a_tuner:
150   {
151     GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
152         (_("Device '%s' is not a tuner."),
153             v4l2object->videodev), GST_ERROR_SYSTEM);
154     return FALSE;
155   }
156 }
157
158 static gboolean
159 gst_v4l2radio_get_input (GstV4l2Object * v4l2object, gint * input)
160 {
161   GST_DEBUG_OBJECT (v4l2object->element, "trying to get radio input");
162
163   if (!GST_V4L2_IS_OPEN (v4l2object))
164     return FALSE;
165
166   if (!v4l2object->channels)
167     goto input_failed;
168
169   *input = 0;
170
171   GST_DEBUG_OBJECT (v4l2object->element, "input: %d", 0);
172
173   return TRUE;
174
175   /* ERRORS */
176 input_failed:
177   {
178     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
179         (_("Failed to get radio input on device '%s'. "),
180             v4l2object->videodev), GST_ERROR_SYSTEM);
181     return FALSE;
182   }
183 }
184
185 static gboolean
186 gst_v4l2radio_set_input (GstV4l2Object * v4l2object, gint input)
187 {
188   GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
189
190   if (!GST_V4L2_IS_OPEN (v4l2object))
191     return FALSE;
192
193   if (!v4l2object->channels)
194     goto input_failed;
195
196   return TRUE;
197
198   /* ERRORS */
199 input_failed:
200   {
201     GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
202         (_("Failed to set input %d on device %s."),
203             input, v4l2object->videodev), GST_ERROR_SYSTEM);
204     return FALSE;
205   }
206 }
207
208 static gboolean
209 gst_v4l2radio_set_mute_on (GstV4l2Radio * radio, gboolean on)
210 {
211   gint res;
212   struct v4l2_control vctrl;
213
214   GST_DEBUG_OBJECT (radio, "setting current tuner mute state: %d", on);
215
216   if (!GST_V4L2_IS_OPEN (radio->v4l2object))
217     return FALSE;
218
219   memset (&vctrl, 0, sizeof (vctrl));
220   vctrl.id = V4L2_CID_AUDIO_MUTE;
221   vctrl.value = on;
222
223   GST_DEBUG_OBJECT (radio, "radio fd: %d", radio->v4l2object->video_fd);
224
225   res = ioctl (radio->v4l2object->video_fd, VIDIOC_S_CTRL, &vctrl);
226   GST_DEBUG_OBJECT (radio, "mute state change result: %d", res);
227   if (res < 0)
228     goto freq_failed;
229
230   return TRUE;
231
232   /* ERRORS */
233 freq_failed:
234   {
235     GST_ELEMENT_WARNING (radio, RESOURCE, SETTINGS,
236         (_("Failed to change mute state for device '%s'."),
237             radio->v4l2object->videodev), GST_ERROR_SYSTEM);
238     return FALSE;
239   }
240 }
241
242 static gboolean
243 gst_v4l2radio_set_mute (GstV4l2Radio * radio)
244 {
245   return gst_v4l2radio_set_mute_on (radio, TRUE);
246 }
247
248 static gboolean
249 gst_v4l2radio_set_unmute (GstV4l2Radio * radio)
250 {
251   return gst_v4l2radio_set_mute_on (radio, FALSE);
252 }
253
254 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2RadioClass, gst_v4l2radio);
255 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Radio, gst_v4l2radio);
256
257 static void gst_v4l2radio_uri_handler_init (gpointer g_iface,
258     gpointer iface_data);
259
260 static void
261 gst_v4l2radio_tuner_interface_reinit (GstTunerClass * iface)
262 {
263   gst_v4l2radio_tuner_interface_init (iface);
264 }
265
266 #define gst_v4l2radio_parent_class parent_class
267 G_DEFINE_TYPE_WITH_CODE (GstV4l2Radio, gst_v4l2radio, GST_TYPE_ELEMENT,
268     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
269         gst_v4l2radio_uri_handler_init);
270     G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER,
271         gst_v4l2radio_tuner_interface_reinit);
272     G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
273         gst_v4l2radio_property_probe_interface_init));
274
275 static void gst_v4l2radio_set_property (GObject * object, guint prop_id,
276     const GValue * value, GParamSpec * pspec);
277 static void gst_v4l2radio_get_property (GObject * object, guint prop_id,
278     GValue * value, GParamSpec * pspec);
279 static void gst_v4l2radio_finalize (GstV4l2Radio * radio);
280 static void gst_v4l2radio_dispose (GObject * object);
281 static GstStateChangeReturn gst_v4l2radio_change_state (GstElement * element,
282     GstStateChange transition);
283
284 static void
285 gst_v4l2radio_class_init (GstV4l2RadioClass * klass)
286 {
287   GObjectClass *gobject_class;
288   GstElementClass *gstelement_class;
289
290   gobject_class = (GObjectClass *) klass;
291   gstelement_class = (GstElementClass *) klass;
292
293   gobject_class->dispose = gst_v4l2radio_dispose;
294   gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2radio_finalize;
295   gobject_class->set_property = gst_v4l2radio_set_property;
296   gobject_class->get_property = gst_v4l2radio_get_property;
297
298   g_object_class_install_property (gobject_class, ARG_DEVICE,
299       g_param_spec_string ("device", "Radio device location",
300           "Video4Linux2 radio device location",
301           DEFAULT_PROP_DEVICE, G_PARAM_READWRITE));
302
303   g_object_class_install_property (gobject_class, ARG_FREQUENCY,
304       g_param_spec_int ("frequency", "Station frequency",
305           "Station frequency in Hz",
306           MIN_FREQUENCY, MAX_FREQUENCY, DEFAULT_FREQUENCY, G_PARAM_READWRITE));
307
308   gstelement_class->change_state =
309       GST_DEBUG_FUNCPTR (gst_v4l2radio_change_state);
310
311   gst_element_class_set_details_simple (gstelement_class,
312       "Radio (video4linux2) Tuner",
313       "Tuner",
314       "Controls a Video4Linux2 radio device",
315       "Alexey Chernov <4ernov@gmail.com>");
316
317   klass->v4l2_class_devices = NULL;
318
319   GST_DEBUG_CATEGORY_INIT (v4l2radio_debug, "v4l2radio", 0,
320       "V4l2 radio element");
321 }
322
323 static void
324 gst_v4l2radio_init (GstV4l2Radio * filter)
325 {
326   filter->v4l2object = gst_v4l2_object_new (GST_ELEMENT (filter),
327       V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
328       gst_v4l2radio_get_input, gst_v4l2radio_set_input, NULL);
329
330   filter->v4l2object->frequency = DEFAULT_FREQUENCY;
331   g_free (filter->v4l2object->videodev);
332   filter->v4l2object->videodev = g_strdup (DEFAULT_PROP_DEVICE);
333 }
334
335 static void
336 gst_v4l2radio_dispose (GObject * object)
337 {
338   GstV4l2Radio *radio = GST_V4L2RADIO (object);
339   gst_v4l2_close (radio->v4l2object);
340   G_OBJECT_CLASS (parent_class)->dispose (object);
341 }
342
343 static void
344 gst_v4l2radio_finalize (GstV4l2Radio * radio)
345 {
346   gst_v4l2_object_destroy (radio->v4l2object);
347   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (radio));
348 }
349
350 static gboolean
351 gst_v4l2radio_open (GstV4l2Radio * radio)
352 {
353   GstV4l2Object *v4l2object;
354
355   v4l2object = radio->v4l2object;
356   if (gst_v4l2_open (v4l2object))
357     return gst_v4l2radio_fill_channel_list (radio);
358   else
359     return FALSE;
360 }
361
362 static void
363 gst_v4l2radio_set_defaults (GstV4l2Radio * radio)
364 {
365   GstV4l2Object *v4l2object;
366   GstTunerChannel *channel = NULL;
367   GstTuner *tuner;
368
369   v4l2object = radio->v4l2object;
370
371   if (!GST_IS_TUNER (v4l2object->element))
372     return;
373
374   tuner = GST_TUNER (v4l2object->element);
375
376   if (v4l2object->channel)
377     channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
378   if (channel) {
379     gst_tuner_set_channel (tuner, channel);
380   } else {
381     channel =
382         GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
383             (v4l2object->element)));
384     if (channel) {
385       g_free (v4l2object->channel);
386       v4l2object->channel = g_strdup (channel->label);
387       gst_tuner_channel_changed (tuner, channel);
388     }
389   }
390
391   if (channel
392       && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
393     if (v4l2object->frequency != 0) {
394       gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
395     } else {
396       v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
397       if (v4l2object->frequency == 0) {
398         /* guess */
399         gst_tuner_set_frequency (tuner, channel, MIN_FREQUENCY);
400       } else {
401       }
402     }
403   }
404 }
405
406 static gboolean
407 gst_v4l2radio_start (GstV4l2Radio * radio)
408 {
409   if (!gst_v4l2radio_open (radio))
410     return FALSE;
411
412   gst_v4l2radio_set_defaults (radio);
413
414   return TRUE;
415 }
416
417 static gboolean
418 gst_v4l2radio_stop (GstV4l2Radio * radio)
419 {
420   if (!gst_v4l2_object_close (radio->v4l2object))
421     return FALSE;
422
423   return TRUE;
424 }
425
426 static GstStateChangeReturn
427 gst_v4l2radio_change_state (GstElement * element, GstStateChange transition)
428 {
429   GstV4l2Radio *radio;
430   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
431
432   radio = GST_V4L2RADIO (element);
433   switch (transition) {
434     case GST_STATE_CHANGE_NULL_TO_READY:
435       /*start radio */
436       if (!gst_v4l2radio_start (radio))
437         ret = GST_STATE_CHANGE_FAILURE;
438       break;
439     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
440       /*unmute radio */
441       if (!gst_v4l2radio_set_unmute (radio))
442         ret = GST_STATE_CHANGE_FAILURE;
443       break;
444     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
445       /*mute radio */
446       if (!gst_v4l2radio_set_mute (radio))
447         ret = GST_STATE_CHANGE_FAILURE;
448       break;
449     case GST_STATE_CHANGE_READY_TO_NULL:
450       /*stop radio */
451       if (!gst_v4l2radio_stop (radio))
452         ret = GST_STATE_CHANGE_FAILURE;
453       break;
454     default:
455       break;
456   }
457
458   return ret;
459 }
460
461 static void
462 gst_v4l2radio_set_property (GObject * object, guint prop_id,
463     const GValue * value, GParamSpec * pspec)
464 {
465   GstV4l2Radio *radio = GST_V4L2RADIO (object);
466   gint frequency;
467   switch (prop_id) {
468     case ARG_DEVICE:
469       g_free (radio->v4l2object->videodev);
470       radio->v4l2object->videodev =
471           g_strdup ((gchar *) g_value_get_string (value));
472       break;
473     case ARG_FREQUENCY:
474       frequency = g_value_get_int (value);
475       if (frequency >= MIN_FREQUENCY && frequency <= MAX_FREQUENCY) {
476         radio->v4l2object->frequency = frequency;
477         gst_v4l2_set_frequency (radio->v4l2object, 0,
478             radio->v4l2object->frequency);
479       }
480       break;
481     default:
482       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483       break;
484   }
485 }
486
487 static void
488 gst_v4l2radio_get_property (GObject * object, guint prop_id,
489     GValue * value, GParamSpec * pspec)
490 {
491   GstV4l2Radio *radio = GST_V4L2RADIO (object);
492
493   switch (prop_id) {
494     case ARG_DEVICE:
495       g_value_set_string (value, radio->v4l2object->videodev);
496       break;
497     case ARG_FREQUENCY:
498       if (gst_v4l2_get_frequency (radio->v4l2object,
499               0, &(radio->v4l2object->frequency)))
500         g_value_set_int (value, radio->v4l2object->frequency);
501       break;
502     default:
503       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504       break;
505   }
506 }
507
508 /* GstURIHandler interface */
509 static GstURIType
510 gst_v4l2radio_uri_get_type (GType type)
511 {
512   return GST_URI_SRC;
513 }
514
515 static gchar **
516 gst_v4l2radio_uri_get_protocols (GType type)
517 {
518   static gchar *protocols[] = { (char *) "radio", NULL };
519   return protocols;
520 }
521
522 static const gchar *
523 gst_v4l2radio_uri_get_uri (GstURIHandler * handler)
524 {
525   GstV4l2Radio *radio = GST_V4L2RADIO (handler);
526
527   if (radio->v4l2object->videodev != NULL) {
528     if (gst_v4l2_get_frequency (radio->v4l2object,
529             0, &(radio->v4l2object->frequency))) {
530       gchar uri[20];
531       gchar freq[6];
532       g_ascii_formatd (freq, 6, "%4.1f", radio->v4l2object->frequency / 1e6);
533       g_snprintf (uri, sizeof (uri), "radio://%s", freq);
534       return g_intern_string (uri);
535     }
536   }
537
538   return "radio://";
539 }
540
541 static gboolean
542 gst_v4l2radio_uri_set_uri (GstURIHandler * handler, const gchar * uri)
543 {
544   GstV4l2Radio *radio = GST_V4L2RADIO (handler);
545   gdouble dfreq;
546   gint ifreq;
547   const gchar *freq;
548   gchar *end;
549
550   if (strcmp (uri, "radio://") != 0) {
551     freq = uri + 8;
552
553     dfreq = g_ascii_strtod (freq, &end);
554
555     if (errno || strlen (end))
556       goto uri_failed;
557
558     ifreq = dfreq * 1e6;
559     g_object_set (radio, "frequency", ifreq, NULL);
560
561   } else
562     goto uri_failed;
563
564   return TRUE;
565
566 uri_failed:
567   return FALSE;
568 }
569
570 static void
571 gst_v4l2radio_uri_handler_init (gpointer g_iface, gpointer iface_data)
572 {
573   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
574
575   iface->get_type = gst_v4l2radio_uri_get_type;
576   iface->get_protocols = gst_v4l2radio_uri_get_protocols;
577   iface->get_uri = gst_v4l2radio_uri_get_uri;
578   iface->set_uri = gst_v4l2radio_uri_set_uri;
579 }