1 /* GStreamer v4l2 radio tuner element
2 * Copyright (C) 2010, 2011 Alexey Chernov <4ernov@gmail.com>
4 * gstv4l2radio.c - V4L2 radio tuner element
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.
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.
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.
23 * SECTION:element-v4l2radio
25 * v4l2radio can be used to control radio device
26 * and to tune it to different radiostations.
29 * <title>Example launch lines</title>
31 * gst-launch v4l2radio device=/dev/radio0 frequency=101200000
32 * gst-launch alsasrc device=hw:1 ! audioconvert ! audioresample ! alsasink
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.
45 #include "gst/gst-i18n-plugin.h"
47 #include "gstv4l2tuner.h"
48 #include "gstv4l2radio.h"
49 #include "v4l2_calls.h"
51 GST_DEBUG_CATEGORY_STATIC (v4l2radio_debug);
52 #define GST_CAT_DEFAULT v4l2radio_debug
54 #define DEFAULT_PROP_DEVICE "/dev/radio0"
55 #define MIN_FREQUENCY 87500000
56 #define DEFAULT_FREQUENCY 100000000
57 #define MAX_FREQUENCY 108000000
67 gst_v4l2radio_fill_channel_list (GstV4l2Radio * radio)
70 struct v4l2_tuner vtun;
71 struct v4l2_capability vc;
72 GstV4l2TunerChannel *v4l2channel;
73 GstTunerChannel *channel;
77 GstV4l2Object *v4l2object;
79 e = GST_ELEMENT (radio);
80 v4l2object = radio->v4l2object;
82 GST_DEBUG_OBJECT (e, "getting audio enumeration");
83 GST_V4L2_CHECK_OPEN (v4l2object);
85 GST_DEBUG_OBJECT (e, " audio input");
87 memset (&vc, 0, sizeof (vc));
89 res = v4l2_ioctl (v4l2object->video_fd, VIDIOC_QUERYCAP, &vc);
93 if (!(vc.capabilities & V4L2_CAP_TUNER))
96 /* getting audio input */
97 memset (&vtun, 0, sizeof (vtun));
100 res = v4l2_ioctl (v4l2object->video_fd, VIDIOC_G_TUNER, &vtun);
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);
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;
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;
126 v4l2object->channels =
127 g_list_prepend (v4l2object->channels, (gpointer) channel);
129 v4l2object->channels = g_list_reverse (v4l2object->channels);
131 GST_DEBUG_OBJECT (e, "done");
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);
144 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
145 (_("Error getting capabilities for device '%s'."),
146 v4l2object->videodev), GST_ERROR_SYSTEM);
151 GST_ELEMENT_ERROR (e, RESOURCE, SETTINGS,
152 (_("Device '%s' is not a tuner."),
153 v4l2object->videodev), GST_ERROR_SYSTEM);
159 gst_v4l2radio_get_input (GstV4l2Object * v4l2object, gint * input)
161 GST_DEBUG_OBJECT (v4l2object->element, "trying to get radio input");
163 if (!GST_V4L2_IS_OPEN (v4l2object))
166 if (!v4l2object->channels)
171 GST_DEBUG_OBJECT (v4l2object->element, "input: %d", 0);
178 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
179 (_("Failed to get radio input on device '%s'. "),
180 v4l2object->videodev), GST_ERROR_SYSTEM);
186 gst_v4l2radio_set_input (GstV4l2Object * v4l2object, gint input)
188 GST_DEBUG_OBJECT (v4l2object->element, "trying to set input to %d", input);
190 if (!GST_V4L2_IS_OPEN (v4l2object))
193 if (!v4l2object->channels)
201 GST_ELEMENT_WARNING (v4l2object->element, RESOURCE, SETTINGS,
202 (_("Failed to set input %d on device %s."),
203 input, v4l2object->videodev), GST_ERROR_SYSTEM);
209 gst_v4l2radio_set_mute_on (GstV4l2Radio * radio, gboolean on)
212 struct v4l2_control vctrl;
214 GST_DEBUG_OBJECT (radio, "setting current tuner mute state: %d", on);
216 if (!GST_V4L2_IS_OPEN (radio->v4l2object))
219 memset (&vctrl, 0, sizeof (vctrl));
220 vctrl.id = V4L2_CID_AUDIO_MUTE;
223 GST_DEBUG_OBJECT (radio, "radio fd: %d", radio->v4l2object->video_fd);
225 res = ioctl (radio->v4l2object->video_fd, VIDIOC_S_CTRL, &vctrl);
226 GST_DEBUG_OBJECT (radio, "mute state change result: %d", res);
235 GST_ELEMENT_WARNING (radio, RESOURCE, SETTINGS,
236 (_("Failed to change mute state for device '%s'."),
237 radio->v4l2object->videodev), GST_ERROR_SYSTEM);
243 gst_v4l2radio_set_mute (GstV4l2Radio * radio)
245 return gst_v4l2radio_set_mute_on (radio, TRUE);
249 gst_v4l2radio_set_unmute (GstV4l2Radio * radio)
251 return gst_v4l2radio_set_mute_on (radio, FALSE);
254 GST_IMPLEMENT_V4L2_PROBE_METHODS (GstV4l2RadioClass, gst_v4l2radio);
255 GST_IMPLEMENT_V4L2_TUNER_METHODS (GstV4l2Radio, gst_v4l2radio);
257 static void gst_v4l2radio_uri_handler_init (gpointer g_iface,
258 gpointer iface_data);
261 gst_v4l2radio_tuner_interface_reinit (GstTunerClass * iface)
263 gst_v4l2radio_tuner_interface_init (iface);
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));
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);
285 gst_v4l2radio_class_init (GstV4l2RadioClass * klass)
287 GObjectClass *gobject_class;
288 GstElementClass *gstelement_class;
290 gobject_class = (GObjectClass *) klass;
291 gstelement_class = (GstElementClass *) klass;
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;
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));
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));
308 gstelement_class->change_state =
309 GST_DEBUG_FUNCPTR (gst_v4l2radio_change_state);
311 gst_element_class_set_details_simple (gstelement_class,
312 "Radio (video4linux2) Tuner",
314 "Controls a Video4Linux2 radio device",
315 "Alexey Chernov <4ernov@gmail.com>");
317 klass->v4l2_class_devices = NULL;
319 GST_DEBUG_CATEGORY_INIT (v4l2radio_debug, "v4l2radio", 0,
320 "V4l2 radio element");
324 gst_v4l2radio_init (GstV4l2Radio * filter)
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);
330 filter->v4l2object->frequency = DEFAULT_FREQUENCY;
331 g_free (filter->v4l2object->videodev);
332 filter->v4l2object->videodev = g_strdup (DEFAULT_PROP_DEVICE);
336 gst_v4l2radio_dispose (GObject * object)
338 GstV4l2Radio *radio = GST_V4L2RADIO (object);
339 gst_v4l2_close (radio->v4l2object);
340 G_OBJECT_CLASS (parent_class)->dispose (object);
344 gst_v4l2radio_finalize (GstV4l2Radio * radio)
346 gst_v4l2_object_destroy (radio->v4l2object);
347 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (radio));
351 gst_v4l2radio_open (GstV4l2Radio * radio)
353 GstV4l2Object *v4l2object;
355 v4l2object = radio->v4l2object;
356 if (gst_v4l2_open (v4l2object))
357 return gst_v4l2radio_fill_channel_list (radio);
363 gst_v4l2radio_set_defaults (GstV4l2Radio * radio)
365 GstV4l2Object *v4l2object;
366 GstTunerChannel *channel = NULL;
369 v4l2object = radio->v4l2object;
371 if (!GST_IS_TUNER (v4l2object->element))
374 tuner = GST_TUNER (v4l2object->element);
376 if (v4l2object->channel)
377 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
379 gst_tuner_set_channel (tuner, channel);
382 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
383 (v4l2object->element)));
385 g_free (v4l2object->channel);
386 v4l2object->channel = g_strdup (channel->label);
387 gst_tuner_channel_changed (tuner, 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);
396 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
397 if (v4l2object->frequency == 0) {
399 gst_tuner_set_frequency (tuner, channel, MIN_FREQUENCY);
407 gst_v4l2radio_start (GstV4l2Radio * radio)
409 if (!gst_v4l2radio_open (radio))
412 gst_v4l2radio_set_defaults (radio);
418 gst_v4l2radio_stop (GstV4l2Radio * radio)
420 if (!gst_v4l2_object_close (radio->v4l2object))
426 static GstStateChangeReturn
427 gst_v4l2radio_change_state (GstElement * element, GstStateChange transition)
430 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
432 radio = GST_V4L2RADIO (element);
433 switch (transition) {
434 case GST_STATE_CHANGE_NULL_TO_READY:
436 if (!gst_v4l2radio_start (radio))
437 ret = GST_STATE_CHANGE_FAILURE;
439 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
441 if (!gst_v4l2radio_set_unmute (radio))
442 ret = GST_STATE_CHANGE_FAILURE;
444 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
446 if (!gst_v4l2radio_set_mute (radio))
447 ret = GST_STATE_CHANGE_FAILURE;
449 case GST_STATE_CHANGE_READY_TO_NULL:
451 if (!gst_v4l2radio_stop (radio))
452 ret = GST_STATE_CHANGE_FAILURE;
462 gst_v4l2radio_set_property (GObject * object, guint prop_id,
463 const GValue * value, GParamSpec * pspec)
465 GstV4l2Radio *radio = GST_V4L2RADIO (object);
469 g_free (radio->v4l2object->videodev);
470 radio->v4l2object->videodev =
471 g_strdup ((gchar *) g_value_get_string (value));
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);
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gst_v4l2radio_get_property (GObject * object, guint prop_id,
489 GValue * value, GParamSpec * pspec)
491 GstV4l2Radio *radio = GST_V4L2RADIO (object);
495 g_value_set_string (value, radio->v4l2object->videodev);
498 if (gst_v4l2_get_frequency (radio->v4l2object,
499 0, &(radio->v4l2object->frequency)))
500 g_value_set_int (value, radio->v4l2object->frequency);
503 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
508 /* GstURIHandler interface */
510 gst_v4l2radio_uri_get_type (GType type)
516 gst_v4l2radio_uri_get_protocols (GType type)
518 static gchar *protocols[] = { (char *) "radio", NULL };
523 gst_v4l2radio_uri_get_uri (GstURIHandler * handler)
525 GstV4l2Radio *radio = GST_V4L2RADIO (handler);
527 if (radio->v4l2object->videodev != NULL) {
528 if (gst_v4l2_get_frequency (radio->v4l2object,
529 0, &(radio->v4l2object->frequency))) {
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);
542 gst_v4l2radio_uri_set_uri (GstURIHandler * handler, const gchar * uri)
544 GstV4l2Radio *radio = GST_V4L2RADIO (handler);
550 if (strcmp (uri, "radio://") != 0) {
553 dfreq = g_ascii_strtod (freq, &end);
555 if (errno || strlen (end))
559 g_object_set (radio, "frequency", ifreq, NULL);
571 gst_v4l2radio_uri_handler_init (gpointer g_iface, gpointer iface_data)
573 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
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;