1 /* GStreamer Tuner interface implementation
2 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * gstv4l2tuner.c: tuner interface implementation for V4L2
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.
27 #include <gst/tuner/tuner.h>
29 #include "gstv4l2tuner.h"
30 #include "gstv4l2element.h"
31 #include "v4l2_calls.h"
33 static void gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass *
35 static void gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel);
37 static void gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass);
38 static void gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm);
40 static const GList *gst_v4l2_tuner_list_channels (GstTuner * mixer);
41 static void gst_v4l2_tuner_set_channel (GstTuner * mixer,
42 GstTunerChannel * channel);
43 static GstTunerChannel *gst_v4l2_tuner_get_channel (GstTuner * mixer);
45 static const GList *gst_v4l2_tuner_list_norms (GstTuner * mixer);
46 static void gst_v4l2_tuner_set_norm (GstTuner * mixer, GstTunerNorm * norm);
47 static GstTunerNorm *gst_v4l2_tuner_get_norm (GstTuner * mixer);
49 static void gst_v4l2_tuner_set_frequency (GstTuner * mixer,
50 GstTunerChannel * channel, gulong frequency);
51 static gulong gst_v4l2_tuner_get_frequency (GstTuner * mixer,
52 GstTunerChannel * channel);
53 static gint gst_v4l2_tuner_signal_strength (GstTuner * mixer,
54 GstTunerChannel * channel);
56 static GstTunerNormClass *norm_parent_class = NULL;
57 static GstTunerChannelClass *channel_parent_class = NULL;
60 gst_v4l2_tuner_channel_get_type (void)
62 static GType gst_v4l2_tuner_channel_type = 0;
64 if (!gst_v4l2_tuner_channel_type) {
65 static const GTypeInfo v4l2_tuner_channel_info = {
66 sizeof (GstV4l2TunerChannelClass),
69 (GClassInitFunc) gst_v4l2_tuner_channel_class_init,
72 sizeof (GstV4l2TunerChannel),
74 (GInstanceInitFunc) gst_v4l2_tuner_channel_init,
78 gst_v4l2_tuner_channel_type =
79 g_type_register_static (GST_TYPE_TUNER_CHANNEL,
80 "GstV4l2TunerChannel", &v4l2_tuner_channel_info, 0);
83 return gst_v4l2_tuner_channel_type;
87 gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass * klass)
89 channel_parent_class = g_type_class_ref (GST_TYPE_TUNER_CHANNEL);
93 gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel)
101 gst_v4l2_tuner_norm_get_type (void)
103 static GType gst_v4l2_tuner_norm_type = 0;
105 if (!gst_v4l2_tuner_norm_type) {
106 static const GTypeInfo v4l2_tuner_norm_info = {
107 sizeof (GstV4l2TunerNormClass),
110 (GClassInitFunc) gst_v4l2_tuner_norm_class_init,
113 sizeof (GstV4l2TunerNorm),
115 (GInstanceInitFunc) gst_v4l2_tuner_norm_init,
119 gst_v4l2_tuner_norm_type =
120 g_type_register_static (GST_TYPE_TUNER_NORM,
121 "GstV4l2TunerNorm", &v4l2_tuner_norm_info, 0);
124 return gst_v4l2_tuner_norm_type;
128 gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass)
130 norm_parent_class = g_type_class_ref (GST_TYPE_TUNER_NORM);
134 gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm)
140 gst_v4l2_tuner_interface_init (GstTunerClass * klass)
142 /* default virtual functions */
143 klass->list_channels = gst_v4l2_tuner_list_channels;
144 klass->set_channel = gst_v4l2_tuner_set_channel;
145 klass->get_channel = gst_v4l2_tuner_get_channel;
147 klass->list_norms = gst_v4l2_tuner_list_norms;
148 klass->set_norm = gst_v4l2_tuner_set_norm;
149 klass->get_norm = gst_v4l2_tuner_get_norm;
151 klass->set_frequency = gst_v4l2_tuner_set_frequency;
152 klass->get_frequency = gst_v4l2_tuner_get_frequency;
153 klass->signal_strength = gst_v4l2_tuner_signal_strength;
157 gst_v4l2_tuner_is_sink (GstV4l2Element * v4l2element)
159 const GList *pads = gst_element_get_pad_list (GST_ELEMENT (v4l2element));
160 GstPadDirection dir = GST_PAD_UNKNOWN;
163 if (pads && g_list_length ((GList *) pads) == 1)
164 dir = GST_PAD_DIRECTION (GST_PAD (pads->data));
166 return (dir == GST_PAD_SINK);
170 gst_v4l2_tuner_contains_channel (GstV4l2Element * v4l2element,
171 GstV4l2TunerChannel * v4l2channel)
175 for (item = v4l2element->channels; item != NULL; item = item->next)
176 if (item->data == v4l2channel)
183 gst_v4l2_tuner_list_channels (GstTuner * mixer)
185 /* ... or output, if we're a sink... */
186 return GST_V4L2ELEMENT (mixer)->channels;
190 gst_v4l2_tuner_set_channel (GstTuner * mixer, GstTunerChannel * channel)
192 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
193 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
195 /* assert that we're opened and that we're using a known item */
196 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2element));
197 g_return_if_fail (gst_v4l2_tuner_contains_channel (v4l2element, v4l2channel));
199 /* ... or output, if we're a sink... */
200 if (gst_v4l2_tuner_is_sink (v4l2element) ?
201 gst_v4l2_set_output (v4l2element, v4l2channel->index) :
202 gst_v4l2_set_input (v4l2element, v4l2channel->index)) {
203 gst_tuner_channel_changed (mixer, channel);
204 g_object_notify (G_OBJECT (v4l2element), "channel");
208 static GstTunerChannel *
209 gst_v4l2_tuner_get_channel (GstTuner * mixer)
211 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
215 /* assert that we're opened and that we're using a known item */
216 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), NULL);
218 /* ... or output, if we're a sink... */
219 if (gst_v4l2_tuner_is_sink (v4l2element))
220 gst_v4l2_get_output (v4l2element, &channel);
222 gst_v4l2_get_input (v4l2element, &channel);
224 for (item = v4l2element->channels; item != NULL; item = item->next) {
225 if (channel == GST_V4L2_TUNER_CHANNEL (item->data)->index)
226 return (GstTunerChannel *) item->data;
233 gst_v4l2_tuner_contains_norm (GstV4l2Element * v4l2element,
234 GstV4l2TunerNorm * v4l2norm)
238 for (item = v4l2element->norms; item != NULL; item = item->next)
239 if (item->data == v4l2norm)
246 gst_v4l2_tuner_list_norms (GstTuner * mixer)
248 return GST_V4L2ELEMENT (mixer)->norms;
252 gst_v4l2_tuner_set_norm (GstTuner * mixer, GstTunerNorm * norm)
254 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
255 GstV4l2TunerNorm *v4l2norm = GST_V4L2_TUNER_NORM (norm);
257 /* assert that we're opened and that we're using a known item */
258 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2element));
259 g_return_if_fail (gst_v4l2_tuner_contains_norm (v4l2element, v4l2norm));
261 if (gst_v4l2_set_norm (v4l2element, v4l2norm->index)) {
262 gst_tuner_norm_changed (mixer, norm);
263 g_object_notify (G_OBJECT (v4l2element), "norm");
267 static GstTunerNorm *
268 gst_v4l2_tuner_get_norm (GstTuner * mixer)
270 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
274 /* assert that we're opened and that we're using a known item */
275 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), NULL);
277 gst_v4l2_get_norm (v4l2element, &norm);
279 for (item = v4l2element->norms; item != NULL; item = item->next) {
280 if (norm == GST_V4L2_TUNER_NORM (item->data)->index)
281 return (GstTunerNorm *) item->data;
288 gst_v4l2_tuner_set_frequency (GstTuner * mixer,
289 GstTunerChannel * channel, gulong frequency)
291 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
292 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
295 /* assert that we're opened and that we're using a known item */
296 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2element));
297 g_return_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
298 GST_TUNER_CHANNEL_FREQUENCY));
299 g_return_if_fail (gst_v4l2_tuner_contains_channel (v4l2element, v4l2channel));
301 gst_v4l2_get_input (v4l2element, &chan);
302 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
303 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
304 if (gst_v4l2_set_frequency (v4l2element, v4l2channel->tuner, frequency)) {
305 gst_tuner_frequency_changed (mixer, channel, frequency);
306 g_object_notify (G_OBJECT (v4l2element), "frequency");
312 gst_v4l2_tuner_get_frequency (GstTuner * mixer, GstTunerChannel * channel)
314 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
315 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
317 gulong frequency = 0;
319 /* assert that we're opened and that we're using a known item */
320 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), 0);
321 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
322 GST_TUNER_CHANNEL_FREQUENCY), 0);
323 g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2element,
326 gst_v4l2_get_input (v4l2element, &chan);
327 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
328 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
329 gst_v4l2_get_frequency (v4l2element, v4l2channel->tuner, &frequency);
336 gst_v4l2_tuner_signal_strength (GstTuner * mixer, GstTunerChannel * channel)
338 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
339 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
343 /* assert that we're opened and that we're using a known item */
344 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), 0);
345 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
346 GST_TUNER_CHANNEL_FREQUENCY), 0);
347 g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2element,
350 gst_v4l2_get_input (v4l2element, &chan);
351 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
352 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
353 gst_v4l2_signal_strength (v4l2element, v4l2channel->tuner, &signal);