1 /* GStreamer
2 *
3 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * 2006 Edgard Lima <edgard.lima@gmail.com>
5 *
6 * gstv4l2tuner.c: tuner interface implementation for V4L2
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29
30 #include "gstv4l2object.h"
31 #include "gstv4l2tuner.h"
32 #include "gstv4l2object.h"
33
34 G_DEFINE_TYPE (GstV4l2TunerChannel, gst_v4l2_tuner_channel,
35 GST_TYPE_TUNER_CHANNEL);
36
37 G_DEFINE_TYPE (GstV4l2TunerNorm, gst_v4l2_tuner_norm, GST_TYPE_TUNER_NORM);
38
39
40 static void
gst_v4l2_tuner_channel_class_init(GstV4l2TunerChannelClass * klass)41 gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass * klass)
42 {
43 }
44
45 static void
gst_v4l2_tuner_channel_init(GstV4l2TunerChannel * channel)46 gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel)
47 {
48 channel->index = (guint32) - 1;
49 channel->tuner = (guint32) - 1;
50 channel->audio = (guint32) - 1;
51 }
52
53 static void
gst_v4l2_tuner_norm_class_init(GstV4l2TunerNormClass * klass)54 gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass)
55 {
56 }
57
58 static void
gst_v4l2_tuner_norm_init(GstV4l2TunerNorm * norm)59 gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm)
60 {
61 norm->index = 0;
62 }
63
64 static G_GNUC_UNUSED gboolean
gst_v4l2_tuner_contains_channel(GstV4l2Object * v4l2object,GstV4l2TunerChannel * v4l2channel)65 gst_v4l2_tuner_contains_channel (GstV4l2Object * v4l2object,
66 GstV4l2TunerChannel * v4l2channel)
67 {
68 const GList *item;
69
70 for (item = v4l2object->channels; item != NULL; item = item->next)
71 if (item->data == v4l2channel)
72 return TRUE;
73
74 return FALSE;
75 }
76
77 const GList *
gst_v4l2_tuner_list_channels(GstV4l2Object * v4l2object)78 gst_v4l2_tuner_list_channels (GstV4l2Object * v4l2object)
79 {
80 return v4l2object->channels;
81 }
82
83 gboolean
gst_v4l2_tuner_set_channel(GstV4l2Object * v4l2object,GstTunerChannel * channel)84 gst_v4l2_tuner_set_channel (GstV4l2Object * v4l2object,
85 GstTunerChannel * channel)
86 {
87 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
88
89 /* assert that we're opened and that we're using a known item */
90 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
91 g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
92 v4l2channel), FALSE);
93
94 if (v4l2object->set_in_out_func (v4l2object, v4l2channel->index)) {
95 gst_tuner_channel_changed (GST_TUNER (v4l2object->element), channel);
96 /* can FPS change here? */
97 return TRUE;
98 }
99
100 return FALSE;
101
102 }
103
104 GstTunerChannel *
gst_v4l2_tuner_get_channel(GstV4l2Object * v4l2object)105 gst_v4l2_tuner_get_channel (GstV4l2Object * v4l2object)
106 {
107 GList *item;
108 guint32 channel;
109
110 /* assert that we're opened and that we're using a known item */
111 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), NULL);
112
113 if (v4l2object->get_in_out_func (v4l2object, &channel)) {
114
115 for (item = v4l2object->channels; item != NULL; item = item->next) {
116 if (channel == GST_V4L2_TUNER_CHANNEL (item->data)->index)
117 return (GstTunerChannel *) item->data;
118 }
119
120 }
121
122 return NULL;
123 }
124
125 static G_GNUC_UNUSED gboolean
gst_v4l2_tuner_contains_norm(GstV4l2Object * v4l2object,GstV4l2TunerNorm * v4l2norm)126 gst_v4l2_tuner_contains_norm (GstV4l2Object * v4l2object,
127 GstV4l2TunerNorm * v4l2norm)
128 {
129 const GList *item;
130
131 for (item = v4l2object->norms; item != NULL; item = item->next)
132 if (item->data == v4l2norm)
133 return TRUE;
134
135 return FALSE;
136 }
137
138 const GList *
gst_v4l2_tuner_list_norms(GstV4l2Object * v4l2object)139 gst_v4l2_tuner_list_norms (GstV4l2Object * v4l2object)
140 {
141 return v4l2object->norms;
142 }
143
144 void
gst_v4l2_tuner_set_norm_and_notify(GstV4l2Object * v4l2object,GstTunerNorm * norm)145 gst_v4l2_tuner_set_norm_and_notify (GstV4l2Object * v4l2object,
146 GstTunerNorm * norm)
147 {
148 if (gst_v4l2_tuner_set_norm (v4l2object, norm)) {
149 #if 0
150 g_object_notify (G_OBJECT (v4l2object->element), "norm");
151 #endif
152 }
153 }
154
155 gboolean
gst_v4l2_tuner_set_norm(GstV4l2Object * v4l2object,GstTunerNorm * norm)156 gst_v4l2_tuner_set_norm (GstV4l2Object * v4l2object, GstTunerNorm * norm)
157 {
158 GstV4l2TunerNorm *v4l2norm = GST_V4L2_TUNER_NORM (norm);
159
160 /* assert that we're opened and that we're using a known item */
161 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
162 g_return_val_if_fail (gst_v4l2_tuner_contains_norm (v4l2object, v4l2norm),
163 FALSE);
164
165 if (gst_v4l2_set_norm (v4l2object, v4l2norm->index)) {
166 gst_tuner_norm_changed (GST_TUNER (v4l2object->element), norm);
167 if (v4l2object->update_fps_func)
168 v4l2object->update_fps_func (v4l2object);
169 return TRUE;
170 }
171
172 return FALSE;
173
174 }
175
176 GstTunerNorm *
gst_v4l2_tuner_get_norm(GstV4l2Object * v4l2object)177 gst_v4l2_tuner_get_norm (GstV4l2Object * v4l2object)
178 {
179 v4l2_std_id norm;
180
181 /* assert that we're opened and that we're using a known item */
182 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), NULL);
183
184 if (!gst_v4l2_get_norm (v4l2object, &norm))
185 return NULL;
186
187 return gst_v4l2_tuner_get_norm_by_std_id (v4l2object, norm);
188 }
189
190 GstTunerNorm *
gst_v4l2_tuner_get_norm_by_std_id(GstV4l2Object * v4l2object,v4l2_std_id norm)191 gst_v4l2_tuner_get_norm_by_std_id (GstV4l2Object * v4l2object, v4l2_std_id norm)
192 {
193 GList *item;
194
195 for (item = v4l2object->norms; item != NULL; item = item->next) {
196 if (norm & GST_V4L2_TUNER_NORM (item->data)->index)
197 return (GstTunerNorm *) item->data;
198 }
199
200 return NULL;
201 }
202
203 v4l2_std_id
gst_v4l2_tuner_get_std_id_by_norm(GstV4l2Object * v4l2object,GstTunerNorm * norm)204 gst_v4l2_tuner_get_std_id_by_norm (GstV4l2Object * v4l2object,
205 GstTunerNorm * norm)
206 {
207 GList *item;
208
209 for (item = v4l2object->norms; item != NULL; item = item->next) {
210 if (norm == GST_TUNER_NORM (item->data))
211 return GST_V4L2_TUNER_NORM (item->data)->index;
212 }
213
214 return 0;
215 }
216
217 void
gst_v4l2_tuner_set_frequency_and_notify(GstV4l2Object * v4l2object,GstTunerChannel * channel,gulong frequency)218 gst_v4l2_tuner_set_frequency_and_notify (GstV4l2Object * v4l2object,
219 GstTunerChannel * channel, gulong frequency)
220 {
221 if (gst_v4l2_tuner_set_frequency (v4l2object, channel, frequency)) {
222 #if 0
223 g_object_notify (G_OBJECT (v4l2object->element), "frequency");
224 #endif
225 }
226 }
227
228 gboolean
gst_v4l2_tuner_set_frequency(GstV4l2Object * v4l2object,GstTunerChannel * channel,gulong frequency)229 gst_v4l2_tuner_set_frequency (GstV4l2Object * v4l2object,
230 GstTunerChannel * channel, gulong frequency)
231 {
232 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
233 guint32 chan;
234
235 /* assert that we're opened and that we're using a known item */
236 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
237 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
238 GST_TUNER_CHANNEL_FREQUENCY), FALSE);
239 g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
240 v4l2channel), FALSE);
241
242 if (v4l2object->get_in_out_func (v4l2object, &chan)) {
243 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
244 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
245 if (gst_v4l2_set_frequency (v4l2object, v4l2channel->tuner, frequency)) {
246 gst_tuner_frequency_changed (GST_TUNER (v4l2object->element), channel,
247 frequency);
248 return TRUE;
249 }
250 }
251 }
252
253 return FALSE;
254 }
255
256 gulong
gst_v4l2_tuner_get_frequency(GstV4l2Object * v4l2object,GstTunerChannel * channel)257 gst_v4l2_tuner_get_frequency (GstV4l2Object * v4l2object,
258 GstTunerChannel * channel)
259 {
260 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
261 guint32 chan;
262 gulong frequency = 0;
263
264 /* assert that we're opened and that we're using a known item */
265 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), 0);
266 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
267 GST_TUNER_CHANNEL_FREQUENCY), 0);
268 g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
269 v4l2channel), 0);
270
271 if (v4l2object->get_in_out_func (v4l2object, &chan)) {
272 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
273 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
274 gst_v4l2_get_frequency (v4l2object, v4l2channel->tuner, &frequency);
275 }
276 }
277
278 return frequency;
279 }
280
281 gint
gst_v4l2_tuner_signal_strength(GstV4l2Object * v4l2object,GstTunerChannel * channel)282 gst_v4l2_tuner_signal_strength (GstV4l2Object * v4l2object,
283 GstTunerChannel * channel)
284 {
285 GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
286 guint32 chan;
287 gulong signal = 0;
288
289 /* assert that we're opened and that we're using a known item */
290 g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), 0);
291 g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
292 GST_TUNER_CHANNEL_FREQUENCY), 0);
293 g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
294 v4l2channel), 0);
295
296 if (v4l2object->get_in_out_func (v4l2object, &chan)) {
297 if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
298 GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
299 gst_v4l2_signal_strength (v4l2object, v4l2channel->tuner, &signal);
300 }
301 }
302
303 return signal;
304 }
305