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.
28 #include "gstv4l2tuner.h"
29 #include "gstv4l2element.h"
30 #include "v4l2_calls.h"
32 static void gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass *
34 static void gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel);
36 static void gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass);
37 static void gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm);
39 static const GList *gst_v4l2_tuner_list_channels (GstTuner * mixer);
40 static void gst_v4l2_tuner_set_channel (GstTuner * mixer,
41 GstTunerChannel * channel);
42 static GstTunerChannel *gst_v4l2_tuner_get_channel (GstTuner * mixer);
44 static const GList *gst_v4l2_tuner_list_norms (GstTuner * mixer);
45 static void gst_v4l2_tuner_set_norm (GstTuner * mixer, GstTunerNorm * norm);
46 static GstTunerNorm *gst_v4l2_tuner_get_norm (GstTuner * mixer);
48 static void gst_v4l2_tuner_set_frequency (GstTuner * mixer,
49 GstTunerChannel * channel, gulong frequency);
50 static gulong gst_v4l2_tuner_get_frequency (GstTuner * mixer,
51 GstTunerChannel * channel);
52 static gint gst_v4l2_tuner_signal_strength (GstTuner * mixer,
53 GstTunerChannel * channel);
55 static GstTunerNormClass *norm_parent_class = NULL;
56 static GstTunerChannelClass *channel_parent_class = NULL;
59 gst_v4l2_tuner_channel_get_type (void)
61 static GType gst_v4l2_tuner_channel_type = 0;
63 if (!gst_v4l2_tuner_channel_type) {
64 static const GTypeInfo v4l2_tuner_channel_info = {
65 sizeof (GstV4l2TunerChannelClass),
68 (GClassInitFunc) gst_v4l2_tuner_channel_class_init,
71 sizeof (GstV4l2TunerChannel),
73 (GInstanceInitFunc) gst_v4l2_tuner_channel_init,
77 gst_v4l2_tuner_channel_type =
78 g_type_register_static (GST_TYPE_TUNER_CHANNEL,
79 "GstV4l2TunerChannel", &v4l2_tuner_channel_info, 0);
82 return gst_v4l2_tuner_channel_type;
86 gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass * klass)
88 channel_parent_class = g_type_class_ref (GST_TYPE_TUNER_CHANNEL);
92 gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel)
94 channel->index = (guint32) - 1;
95 channel->tuner = (guint32) - 1;
96 channel->audio = (guint32) - 1;
100 gst_v4l2_tuner_norm_get_type (void)
102 static GType gst_v4l2_tuner_norm_type = 0;
104 if (!gst_v4l2_tuner_norm_type) {
105 static const GTypeInfo v4l2_tuner_norm_info = {
106 sizeof (GstV4l2TunerNormClass),
109 (GClassInitFunc) gst_v4l2_tuner_norm_class_init,
112 sizeof (GstV4l2TunerNorm),
114 (GInstanceInitFunc) gst_v4l2_tuner_norm_init,
118 gst_v4l2_tuner_norm_type =
119 g_type_register_static (GST_TYPE_TUNER_NORM,
120 "GstV4l2TunerNorm", &v4l2_tuner_norm_info, 0);
123 return gst_v4l2_tuner_norm_type;
127 gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass)
129 norm_parent_class = g_type_class_ref (GST_TYPE_TUNER_NORM);
133 gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm)
139 gst_v4l2_tuner_interface_init (GstTunerClass * klass)
141 /* default virtual functions */
142 klass->list_channels = gst_v4l2_tuner_list_channels;
143 klass->set_channel = gst_v4l2_tuner_set_channel;
144 klass->get_channel = gst_v4l2_tuner_get_channel;
146 klass->list_norms = gst_v4l2_tuner_list_norms;
147 klass->set_norm = gst_v4l2_tuner_set_norm;
148 klass->get_norm = gst_v4l2_tuner_get_norm;
150 klass->set_frequency = gst_v4l2_tuner_set_frequency;
151 klass->get_frequency = gst_v4l2_tuner_get_frequency;
152 klass->signal_strength = gst_v4l2_tuner_signal_strength;
156 gst_v4l2_tuner_is_sink (GstV4l2Element * v4l2element)
158 GstPadDirection dir = GST_PAD_UNKNOWN;
160 return (dir == GST_PAD_SINK);
163 static G_GNUC_UNUSED gboolean
164 gst_v4l2_tuner_contains_channel (GstV4l2Element * v4l2element,
165 GstV4l2TunerChannel * v4l2channel)
169 for (item = v4l2element->inputs; item != NULL; item = item->next)
170 if (item->data == v4l2channel)
177 gst_v4l2_tuner_list_channels (GstTuner * mixer)
179 /* ... or output, if we're a sink... */
180 return GST_V4L2ELEMENT (mixer)->inputs;
184 gst_v4l2_tuner_set_channel (GstTuner * mixer, GstTunerChannel * channel)
186 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
187 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
189 /* assert that we're opened and that we're using a known item */
190 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2element));
191 g_return_if_fail (gst_v4l2_tuner_contains_channel (v4l2element, v4l2channel));
193 /* ... or output, if we're a sink... */
194 if (gst_v4l2_tuner_is_sink (v4l2element) ?
195 gst_v4l2_set_output (v4l2element, v4l2channel->index) :
196 gst_v4l2_set_input (v4l2element, v4l2channel->index)) {
197 gst_tuner_channel_changed (mixer, channel);
198 g_object_notify (G_OBJECT (v4l2element), "input");
202 static GstTunerChannel *
203 gst_v4l2_tuner_get_channel (GstTuner * mixer)
205 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
209 /* assert that we're opened and that we're using a known item */
210 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), NULL);
212 /* ... or output, if we're a sink... */
213 if (gst_v4l2_tuner_is_sink (v4l2element))
214 gst_v4l2_get_output (v4l2element, &channel);
216 gst_v4l2_get_input (v4l2element, &channel);
218 for (item = v4l2element->inputs; item != NULL; item = item->next) {
219 if (channel == GST_V4L2_TUNER_CHANNEL (item->data)->index)
220 return (GstTunerChannel *) item->data;
226 static G_GNUC_UNUSED gboolean
227 gst_v4l2_tuner_contains_norm (GstV4l2Element * v4l2element,
228 GstV4l2TunerNorm * v4l2norm)
232 for (item = v4l2element->stds; item != NULL; item = item->next)
233 if (item->data == v4l2norm)
240 gst_v4l2_tuner_list_norms (GstTuner * mixer)
242 return GST_V4L2ELEMENT (mixer)->stds;
246 gst_v4l2_tuner_set_norm (GstTuner * mixer, GstTunerNorm * norm)
248 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
249 GstV4l2TunerNorm *v4l2norm = GST_V4L2_TUNER_NORM (norm);
251 /* assert that we're opened and that we're using a known item */
252 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2element));
253 g_return_if_fail (gst_v4l2_tuner_contains_norm (v4l2element, v4l2norm));
255 if (gst_v4l2_set_norm (v4l2element, v4l2norm->index)) {
256 gst_tuner_norm_changed (mixer, norm);
257 g_object_notify (G_OBJECT (v4l2element), "std");
261 static GstTunerNorm *
262 gst_v4l2_tuner_get_norm (GstTuner * mixer)
264 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
268 /* assert that we're opened and that we're using a known item */
269 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), NULL);
271 gst_v4l2_get_norm (v4l2element, &norm);
273 for (item = v4l2element->stds; item != NULL; item = item->next) {
274 if (norm == GST_V4L2_TUNER_NORM (item->data)->index)
275 return (GstTunerNorm *) item->data;
282 gst_v4l2_tuner_set_frequency (GstTuner * mixer,
283 GstTunerChannel * channel, gulong frequency)
285 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
286 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
289 /* assert that we're opened and that we're using a known item */
290 g_return_if_fail (GST_V4L2_IS_OPEN (v4l2element));
291 g_return_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
292 GST_TUNER_CHANNEL_FREQUENCY));
293 g_return_if_fail (gst_v4l2_tuner_contains_channel (v4l2element, v4l2channel));
295 gst_v4l2_get_input (v4l2element, &chan);
296 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
297 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
298 if (gst_v4l2_set_frequency (v4l2element, v4l2channel->tuner, frequency)) {
299 gst_tuner_frequency_changed (mixer, channel, frequency);
300 g_object_notify (G_OBJECT (v4l2element), "frequency");
306 gst_v4l2_tuner_get_frequency (GstTuner * mixer, GstTunerChannel * channel)
308 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
309 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
311 gulong frequency = 0;
313 /* assert that we're opened and that we're using a known item */
314 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), 0);
315 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
316 GST_TUNER_CHANNEL_FREQUENCY), 0);
317 g_return_val_if_fail (gst_v4l2_tuner_contains_channel
318 (v4l2element, v4l2channel), 0);
320 gst_v4l2_get_input (v4l2element, &chan);
321 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
322 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
323 gst_v4l2_get_frequency (v4l2element, v4l2channel->tuner, &frequency);
330 gst_v4l2_tuner_signal_strength (GstTuner * mixer, GstTunerChannel * channel)
332 GstV4l2Element *v4l2element = GST_V4L2ELEMENT (mixer);
333 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
337 /* assert that we're opened and that we're using a known item */
338 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2element), 0);
339 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
340 GST_TUNER_CHANNEL_FREQUENCY), 0);
341 g_return_val_if_fail (gst_v4l2_tuner_contains_channel
342 (v4l2element, v4l2channel), 0);
344 gst_v4l2_get_input (v4l2element, &chan);
345 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
346 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
347 gst_v4l2_signal_strength (v4l2element, v4l2channel->tuner, &signal);