gstv4l2tuner.c 9.17 KB
Newer Older
1
2
/* GStreamer
 *
3
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
4
 *               2006 Edgard Lima <edgard.lima@indt.org.br>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * gstv4l2tuner.c: tuner interface implementation for V4L2
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>

#include "gstv4l2tuner.h"
31
#include "gstv4l2object.h"
32
#include "v4l2_calls.h"
33
34
#include "v4l2src_calls.h"

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
35
36
37
static void gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass *
    klass);
static void gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel);
38

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
39
40
static void gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass);
static void gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm);
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

static GstTunerNormClass *norm_parent_class = NULL;
static GstTunerChannelClass *channel_parent_class = NULL;

GType
gst_v4l2_tuner_channel_get_type (void)
{
  static GType gst_v4l2_tuner_channel_type = 0;

  if (!gst_v4l2_tuner_channel_type) {
    static const GTypeInfo v4l2_tuner_channel_info = {
      sizeof (GstV4l2TunerChannelClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_v4l2_tuner_channel_class_init,
      NULL,
      NULL,
      sizeof (GstV4l2TunerChannel),
      0,
      (GInstanceInitFunc) gst_v4l2_tuner_channel_init,
      NULL
    };

    gst_v4l2_tuner_channel_type =
65
66
        g_type_register_static (GST_TYPE_TUNER_CHANNEL,
        "GstV4l2TunerChannel", &v4l2_tuner_channel_info, 0);
67
68
69
70
71
72
  }

  return gst_v4l2_tuner_channel_type;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
73
gst_v4l2_tuner_channel_class_init (GstV4l2TunerChannelClass * klass)
74
{
75
  channel_parent_class = g_type_class_peek_parent (klass);
76
77
78
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
79
gst_v4l2_tuner_channel_init (GstV4l2TunerChannel * channel)
80
{
81
82
83
  channel->index = (guint32) - 1;
  channel->tuner = (guint32) - 1;
  channel->audio = (guint32) - 1;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

GType
gst_v4l2_tuner_norm_get_type (void)
{
  static GType gst_v4l2_tuner_norm_type = 0;

  if (!gst_v4l2_tuner_norm_type) {
    static const GTypeInfo v4l2_tuner_norm_info = {
      sizeof (GstV4l2TunerNormClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_v4l2_tuner_norm_class_init,
      NULL,
      NULL,
      sizeof (GstV4l2TunerNorm),
      0,
      (GInstanceInitFunc) gst_v4l2_tuner_norm_init,
      NULL
    };

    gst_v4l2_tuner_norm_type =
106
107
        g_type_register_static (GST_TYPE_TUNER_NORM,
        "GstV4l2TunerNorm", &v4l2_tuner_norm_info, 0);
108
109
110
111
112
113
  }

  return gst_v4l2_tuner_norm_type;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
114
gst_v4l2_tuner_norm_class_init (GstV4l2TunerNormClass * klass)
115
{
116
  norm_parent_class = g_type_class_peek_parent (klass);
117
118
119
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
120
gst_v4l2_tuner_norm_init (GstV4l2TunerNorm * norm)
121
122
123
124
{
  norm->index = 0;
}

125
static G_GNUC_UNUSED gboolean
126
gst_v4l2_tuner_contains_channel (GstV4l2Object * v4l2object,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
127
    GstV4l2TunerChannel * v4l2channel)
128
129
130
{
  const GList *item;

131
  for (item = v4l2object->channels; item != NULL; item = item->next)
132
133
134
135
136
137
    if (item->data == v4l2channel)
      return TRUE;

  return FALSE;
}

138
139
const GList *
gst_v4l2_tuner_list_channels (GstV4l2Object * v4l2object)
140
{
141
  return v4l2object->channels;
142
143
}

144
gboolean
145
146
gst_v4l2_tuner_set_channel (GstV4l2Object * v4l2object,
    GstTunerChannel * channel)
147
148
149
150
{
  GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);

  /* assert that we're opened and that we're using a known item */
151
152
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
  g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
153
          v4l2channel), FALSE);
154

155
156
157
  if (v4l2object->set_in_out_func (v4l2object, v4l2channel->index)) {
    gst_tuner_channel_changed (GST_TUNER (v4l2object->element), channel);
    v4l2object->update_fps_func (v4l2object);
158
    return TRUE;
159
  }
160
161
162
163
164

  return FALSE;

}

165
166
GstTunerChannel *
gst_v4l2_tuner_get_channel (GstV4l2Object * v4l2object)
167
{
168
169
170
171
  GList *item;
  gint channel;

  /* assert that we're opened and that we're using a known item */
172
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), NULL);
173

174
  v4l2object->get_in_out_func (v4l2object, &channel);
175

176
  for (item = v4l2object->channels; item != NULL; item = item->next) {
177
    if (channel == GST_V4L2_TUNER_CHANNEL (item->data)->index)
178
      return (GstTunerChannel *) item->data;
179
180
181
182
183
  }

  return NULL;
}

184
static G_GNUC_UNUSED gboolean
185
gst_v4l2_tuner_contains_norm (GstV4l2Object * v4l2object,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
186
    GstV4l2TunerNorm * v4l2norm)
187
188
189
{
  const GList *item;

190
  for (item = v4l2object->norms; item != NULL; item = item->next)
191
192
193
194
195
196
    if (item->data == v4l2norm)
      return TRUE;

  return FALSE;
}

197
198
const GList *
gst_v4l2_tuner_list_norms (GstV4l2Object * v4l2object)
199
{
200
  return v4l2object->norms;
201
202
}

203
204
205
void
gst_v4l2_tuner_set_norm_and_notify (GstV4l2Object * v4l2object,
    GstTunerNorm * norm)
206
{
207
  if (gst_v4l2_tuner_set_norm (v4l2object, norm)) {
208
    g_object_notify (G_OBJECT (v4l2object->element), "norm");
209
210
211
212
  }
}

gboolean
213
gst_v4l2_tuner_set_norm (GstV4l2Object * v4l2object, GstTunerNorm * norm)
214
215
216
217
{
  GstV4l2TunerNorm *v4l2norm = GST_V4L2_TUNER_NORM (norm);

  /* assert that we're opened and that we're using a known item */
218
219
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
  g_return_val_if_fail (gst_v4l2_tuner_contains_norm (v4l2object, v4l2norm),
220
      FALSE);
221

222
223
224
  if (gst_v4l2_set_norm (v4l2object, v4l2norm->index)) {
    gst_tuner_norm_changed (GST_TUNER (v4l2object->element), norm);
    v4l2object->update_fps_func (v4l2object);
225
    return TRUE;
226
  }
227
228
229

  return FALSE;

230
231
}

232
233
GstTunerNorm *
gst_v4l2_tuner_get_norm (GstV4l2Object * v4l2object)
234
235
236
237
238
{
  GList *item;
  v4l2_std_id norm;

  /* assert that we're opened and that we're using a known item */
239
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), NULL);
240

241
  gst_v4l2_get_norm (v4l2object, &norm);
242

243
  for (item = v4l2object->norms; item != NULL; item = item->next) {
244
    if (norm == GST_V4L2_TUNER_NORM (item->data)->index)
245
      return (GstTunerNorm *) item->data;
246
247
248
249
250
  }

  return NULL;
}

251
252
void
gst_v4l2_tuner_set_frequency_and_notify (GstV4l2Object * v4l2object,
253
254
    GstTunerChannel * channel, gulong frequency)
{
255
256
  if (gst_v4l2_tuner_set_frequency (v4l2object, channel, frequency)) {
    g_object_notify (G_OBJECT (v4l2object->element), "frequency");
257
258
259
260
  }
}

gboolean
261
gst_v4l2_tuner_set_frequency (GstV4l2Object * v4l2object,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
262
    GstTunerChannel * channel, gulong frequency)
263
264
265
266
267
{
  GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
  gint chan;

  /* assert that we're opened and that we're using a known item */
268
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), FALSE);
269
270
  g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
          GST_TUNER_CHANNEL_FREQUENCY), FALSE);
271
  g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
272
          v4l2channel), FALSE);
273

274
  v4l2object->get_in_out_func (v4l2object, &chan);
275
276
  if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
      GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
277
278
279
    if (gst_v4l2_set_frequency (v4l2object, v4l2channel->tuner, frequency)) {
      gst_tuner_frequency_changed (GST_TUNER (v4l2object->element), channel,
          frequency);
280
      return TRUE;
281
    }
282
  }
283
  return FALSE;
284
285
}

286
287
288
gulong
gst_v4l2_tuner_get_frequency (GstV4l2Object * v4l2object,
    GstTunerChannel * channel)
289
290
291
292
293
294
{
  GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
  gint chan;
  gulong frequency = 0;

  /* assert that we're opened and that we're using a known item */
295
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), 0);
296
  g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
297
          GST_TUNER_CHANNEL_FREQUENCY), 0);
298
299
  g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
          v4l2channel), 0);
300

301
  v4l2object->get_in_out_func (v4l2object, &chan);
302
303
  if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
      GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
304
    gst_v4l2_get_frequency (v4l2object, v4l2channel->tuner, &frequency);
305
306
307
308
309
  }

  return frequency;
}

310
311
312
gint
gst_v4l2_tuner_signal_strength (GstV4l2Object * v4l2object,
    GstTunerChannel * channel)
313
314
315
316
317
318
{
  GstV4l2TunerChannel *v4l2channel = GST_V4L2_TUNER_CHANNEL (channel);
  gint chan;
  gulong signal = 0;

  /* assert that we're opened and that we're using a known item */
319
  g_return_val_if_fail (GST_V4L2_IS_OPEN (v4l2object), 0);
320
  g_return_val_if_fail (GST_TUNER_CHANNEL_HAS_FLAG (channel,
321
          GST_TUNER_CHANNEL_FREQUENCY), 0);
322
323
  g_return_val_if_fail (gst_v4l2_tuner_contains_channel (v4l2object,
          v4l2channel), 0);
324

325
  v4l2object->get_in_out_func (v4l2object, &chan);
326
327
  if (chan == GST_V4L2_TUNER_CHANNEL (channel)->index &&
      GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
328
    gst_v4l2_signal_strength (v4l2object, v4l2channel->tuner, &signal);
329
330
331
332
  }

  return signal;
}