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_TUNER_METHODS (GstV4l2Radio, gst_v4l2radio);
256 static void gst_v4l2radio_uri_handler_init (gpointer g_iface,
257 gpointer iface_data);
260 gst_v4l2radio_tuner_interface_reinit (GstTunerInterface * iface)
262 gst_v4l2radio_tuner_interface_init (iface);
265 #define gst_v4l2radio_parent_class parent_class
266 G_DEFINE_TYPE_WITH_CODE (GstV4l2Radio, gst_v4l2radio, GST_TYPE_ELEMENT,
267 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
268 gst_v4l2radio_uri_handler_init);
269 G_IMPLEMENT_INTERFACE (GST_TYPE_TUNER,
270 gst_v4l2radio_tuner_interface_reinit));
272 static void gst_v4l2radio_set_property (GObject * object, guint prop_id,
273 const GValue * value, GParamSpec * pspec);
274 static void gst_v4l2radio_get_property (GObject * object, guint prop_id,
275 GValue * value, GParamSpec * pspec);
276 static void gst_v4l2radio_finalize (GstV4l2Radio * radio);
277 static void gst_v4l2radio_dispose (GObject * object);
278 static GstStateChangeReturn gst_v4l2radio_change_state (GstElement * element,
279 GstStateChange transition);
282 gst_v4l2radio_class_init (GstV4l2RadioClass * klass)
284 GObjectClass *gobject_class;
285 GstElementClass *gstelement_class;
287 gobject_class = (GObjectClass *) klass;
288 gstelement_class = (GstElementClass *) klass;
290 gobject_class->dispose = gst_v4l2radio_dispose;
291 gobject_class->finalize = (GObjectFinalizeFunc) gst_v4l2radio_finalize;
292 gobject_class->set_property = gst_v4l2radio_set_property;
293 gobject_class->get_property = gst_v4l2radio_get_property;
295 g_object_class_install_property (gobject_class, ARG_DEVICE,
296 g_param_spec_string ("device", "Radio device location",
297 "Video4Linux2 radio device location",
298 DEFAULT_PROP_DEVICE, G_PARAM_READWRITE));
300 g_object_class_install_property (gobject_class, ARG_FREQUENCY,
301 g_param_spec_int ("frequency", "Station frequency",
302 "Station frequency in Hz",
303 MIN_FREQUENCY, MAX_FREQUENCY, DEFAULT_FREQUENCY, G_PARAM_READWRITE));
305 gstelement_class->change_state =
306 GST_DEBUG_FUNCPTR (gst_v4l2radio_change_state);
308 gst_element_class_set_details_simple (gstelement_class,
309 "Radio (video4linux2) Tuner",
311 "Controls a Video4Linux2 radio device",
312 "Alexey Chernov <4ernov@gmail.com>");
314 klass->v4l2_class_devices = NULL;
316 GST_DEBUG_CATEGORY_INIT (v4l2radio_debug, "v4l2radio", 0,
317 "V4l2 radio element");
321 gst_v4l2radio_init (GstV4l2Radio * filter)
323 filter->v4l2object = gst_v4l2_object_new (GST_ELEMENT (filter),
324 V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
325 gst_v4l2radio_get_input, gst_v4l2radio_set_input, NULL);
327 filter->v4l2object->frequency = DEFAULT_FREQUENCY;
328 g_free (filter->v4l2object->videodev);
329 filter->v4l2object->videodev = g_strdup (DEFAULT_PROP_DEVICE);
333 gst_v4l2radio_dispose (GObject * object)
335 GstV4l2Radio *radio = GST_V4L2RADIO (object);
336 gst_v4l2_close (radio->v4l2object);
337 G_OBJECT_CLASS (parent_class)->dispose (object);
341 gst_v4l2radio_finalize (GstV4l2Radio * radio)
343 gst_v4l2_object_destroy (radio->v4l2object);
344 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (radio));
348 gst_v4l2radio_open (GstV4l2Radio * radio)
350 GstV4l2Object *v4l2object;
352 v4l2object = radio->v4l2object;
353 if (gst_v4l2_open (v4l2object))
354 return gst_v4l2radio_fill_channel_list (radio);
360 gst_v4l2radio_set_defaults (GstV4l2Radio * radio)
362 GstV4l2Object *v4l2object;
363 GstTunerChannel *channel = NULL;
366 v4l2object = radio->v4l2object;
368 if (!GST_IS_TUNER (v4l2object->element))
371 tuner = GST_TUNER (v4l2object->element);
373 if (v4l2object->channel)
374 channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
376 gst_tuner_set_channel (tuner, channel);
379 GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
380 (v4l2object->element)));
382 g_free (v4l2object->channel);
383 v4l2object->channel = g_strdup (channel->label);
384 gst_tuner_channel_changed (tuner, channel);
389 && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
390 if (v4l2object->frequency != 0) {
391 gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
393 v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
394 if (v4l2object->frequency == 0) {
396 gst_tuner_set_frequency (tuner, channel, MIN_FREQUENCY);
404 gst_v4l2radio_start (GstV4l2Radio * radio)
406 if (!gst_v4l2radio_open (radio))
409 gst_v4l2radio_set_defaults (radio);
415 gst_v4l2radio_stop (GstV4l2Radio * radio)
417 if (!gst_v4l2_object_close (radio->v4l2object))
423 static GstStateChangeReturn
424 gst_v4l2radio_change_state (GstElement * element, GstStateChange transition)
427 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
429 radio = GST_V4L2RADIO (element);
430 switch (transition) {
431 case GST_STATE_CHANGE_NULL_TO_READY:
433 if (!gst_v4l2radio_start (radio))
434 ret = GST_STATE_CHANGE_FAILURE;
436 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
438 if (!gst_v4l2radio_set_unmute (radio))
439 ret = GST_STATE_CHANGE_FAILURE;
441 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
443 if (!gst_v4l2radio_set_mute (radio))
444 ret = GST_STATE_CHANGE_FAILURE;
446 case GST_STATE_CHANGE_READY_TO_NULL:
448 if (!gst_v4l2radio_stop (radio))
449 ret = GST_STATE_CHANGE_FAILURE;
459 gst_v4l2radio_set_property (GObject * object, guint prop_id,
460 const GValue * value, GParamSpec * pspec)
462 GstV4l2Radio *radio = GST_V4L2RADIO (object);
466 g_free (radio->v4l2object->videodev);
467 radio->v4l2object->videodev =
468 g_strdup ((gchar *) g_value_get_string (value));
471 frequency = g_value_get_int (value);
472 if (frequency >= MIN_FREQUENCY && frequency <= MAX_FREQUENCY) {
473 radio->v4l2object->frequency = frequency;
474 gst_v4l2_set_frequency (radio->v4l2object, 0,
475 radio->v4l2object->frequency);
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 gst_v4l2radio_get_property (GObject * object, guint prop_id,
486 GValue * value, GParamSpec * pspec)
488 GstV4l2Radio *radio = GST_V4L2RADIO (object);
492 g_value_set_string (value, radio->v4l2object->videodev);
495 if (gst_v4l2_get_frequency (radio->v4l2object,
496 0, &(radio->v4l2object->frequency)))
497 g_value_set_int (value, radio->v4l2object->frequency);
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505 /* GstURIHandler interface */
507 gst_v4l2radio_uri_get_type (GType type)
512 static const gchar *const *
513 gst_v4l2radio_uri_get_protocols (GType type)
515 static const gchar *protocols[] = { "radio", NULL };
521 gst_v4l2radio_uri_get_uri (GstURIHandler * handler)
523 GstV4l2Radio *radio = GST_V4L2RADIO (handler);
525 if (radio->v4l2object->videodev != NULL) {
526 if (gst_v4l2_get_frequency (radio->v4l2object,
527 0, &(radio->v4l2object->frequency))) {
528 return g_strdup_printf ("radio://%4.1f",
529 radio->v4l2object->frequency / 1e6);
533 return g_strdup ("radio://");
537 gst_v4l2radio_uri_set_uri (GstURIHandler * handler, const gchar * uri,
540 GstV4l2Radio *radio = GST_V4L2RADIO (handler);
546 if (strcmp (uri, "radio://") != 0) {
549 dfreq = g_ascii_strtod (freq, &end);
551 if (errno || strlen (end))
555 g_object_set (radio, "frequency", ifreq, NULL);
563 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
564 "Bad radio URI, could not parse frequency");
569 gst_v4l2radio_uri_handler_init (gpointer g_iface, gpointer iface_data)
571 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
573 iface->get_type = gst_v4l2radio_uri_get_type;
574 iface->get_protocols = gst_v4l2radio_uri_get_protocols;
575 iface->get_uri = gst_v4l2radio_uri_get_uri;
576 iface->set_uri = gst_v4l2radio_uri_set_uri;