~ubuntu-branches/ubuntu/quantal/gst-plugins-bad-multiverse0.10/quantal

« back to all changes in this revision

Viewing changes to ext/resindvd/rsnaudiodec.c

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2009-12-07 08:54:28 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20091207085428-ml6aaukf0p2ph34d
Tags: 0.10.17-0ubuntu1
* New upstream release.
* Add myself to maintainer.
* Fix misc lintian warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GStreamer
2
 
 * Copyright (C) <2009> Jan Schmidt <thaytan@noraisin.net>
3
 
 *
4
 
 * This library is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU Library General Public
6
 
 * License as published by the Free Software Foundation; either
7
 
 * version 2 of the License, or (at your option) any later version.
8
 
 *
9
 
 * This library is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * Library General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Library General Public
15
 
 * License along with this library; if not, write to the
16
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
 
 * Boston, MA 02111-1307, USA.
18
 
 */
19
 
 
20
 
#ifdef HAVE_CONFIG_H
21
 
#include "config.h"
22
 
#endif
23
 
 
24
 
#include "rsnaudiodec.h"
25
 
 
26
 
GST_DEBUG_CATEGORY_STATIC (rsn_audiodec_debug);
27
 
#define GST_CAT_DEFAULT rsn_audiodec_debug
28
 
 
29
 
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
30
 
    GST_PAD_SINK,
31
 
    GST_PAD_ALWAYS,
32
 
    GST_STATIC_CAPS ("audio/mpeg,mpegversion=(int)1;"
33
 
        "audio/x-private1-lpcm;"
34
 
        "audio/x-private1-ac3;" "audio/ac3;" "audio/x-ac3;"
35
 
        "audio/x-private1-dts;")
36
 
    );
37
 
 
38
 
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
39
 
    GST_PAD_SRC,
40
 
    GST_PAD_ALWAYS,
41
 
    GST_STATIC_CAPS ("audio/x-raw-float, "
42
 
        "rate = (int) [ 1, MAX ], "
43
 
        "channels = (int) [ 1, MAX ], "
44
 
        "endianness = (int) BYTE_ORDER, "
45
 
        "width = (int) { 32, 64 }; "
46
 
        "audio/x-raw-int, "
47
 
        "rate = (int) [ 1, MAX ], "
48
 
        "channels = (int) [ 1, MAX ], "
49
 
        "endianness = (int) BYTE_ORDER, "
50
 
        "width = (int) 32, "
51
 
        "depth = (int) 32, "
52
 
        "signed = (boolean) true; "
53
 
        "audio/x-raw-int, "
54
 
        "rate = (int) [ 1, MAX ], "
55
 
        "channels = (int) [ 1, MAX ], "
56
 
        "endianness = (int) BYTE_ORDER, "
57
 
        "width = (int) 24, "
58
 
        "depth = (int) 24, "
59
 
        "signed = (boolean) true; "
60
 
        "audio/x-raw-int, "
61
 
        "rate = (int) [ 1, MAX ], "
62
 
        "channels = (int) [ 1, MAX ], "
63
 
        "endianness = (int) BYTE_ORDER, "
64
 
        "width = (int) 16, "
65
 
        "depth = (int) 16, "
66
 
        "signed = (boolean) true; "
67
 
        "audio/x-raw-int, "
68
 
        "rate = (int) [ 1, MAX ], "
69
 
        "channels = (int) [ 1, MAX ], "
70
 
        "endianness = (int) BYTE_ORDER, "
71
 
        "width = (int) 8, " "depth = (int) 8, " "signed = (boolean) true")
72
 
    );
73
 
 
74
 
G_DEFINE_TYPE (RsnAudioDec, rsn_audiodec, GST_TYPE_BIN);
75
 
 
76
 
static gboolean rsn_audiodec_set_sink_caps (GstPad * pad, GstCaps * caps);
77
 
static GstCaps *rsn_audiodec_get_sink_caps (GstPad * pad);
78
 
static GstFlowReturn rsn_audiodec_chain (GstPad * pad, GstBuffer * buf);
79
 
static gboolean rsn_audiodec_sink_event (GstPad * pad, GstEvent * event);
80
 
static GstStateChangeReturn rsn_audiodec_change_state (GstElement * element,
81
 
    GstStateChange transition);
82
 
 
83
 
static GstCaps *rsn_audiodec_get_proxy_sink_caps (GstPad * pad);
84
 
static GstCaps *rsn_audiodec_get_proxy_src_caps (GstPad * pad);
85
 
 
86
 
static GstFlowReturn rsn_audiodec_proxy_src_chain (GstPad * pad,
87
 
    GstBuffer * buf);
88
 
static gboolean rsn_audiodec_proxy_src_event (GstPad * pad, GstEvent * event);
89
 
 
90
 
static void rsn_audiodec_dispose (GObject * gobj);
91
 
static void cleanup_child (RsnAudioDec * self);
92
 
 
93
 
static void
94
 
rsn_audiodec_class_init (RsnAudioDecClass * klass)
95
 
{
96
 
  static GstElementDetails element_details = {
97
 
    "RsnAudioDec",
98
 
    "Audio/Decoder",
99
 
    "Resin DVD audio stream decoder",
100
 
    "Jan Schmidt <thaytan@noraisin.net>"
101
 
  };
102
 
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
103
 
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104
 
 
105
 
  GST_DEBUG_CATEGORY_INIT (rsn_audiodec_debug, "rsnaudiodec",
106
 
      0, "Resin DVD audio stream decoder");
107
 
 
108
 
  object_class->dispose = rsn_audiodec_dispose;
109
 
 
110
 
  element_class->change_state = GST_DEBUG_FUNCPTR (rsn_audiodec_change_state);
111
 
 
112
 
  gst_element_class_add_pad_template (element_class,
113
 
      gst_static_pad_template_get (&src_template));
114
 
  gst_element_class_add_pad_template (element_class,
115
 
      gst_static_pad_template_get (&sink_template));
116
 
 
117
 
  gst_element_class_set_details (element_class, &element_details);
118
 
}
119
 
 
120
 
static void
121
 
rsn_audiodec_init (RsnAudioDec * self)
122
 
{
123
 
  self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
124
 
  gst_pad_set_setcaps_function (self->sinkpad,
125
 
      GST_DEBUG_FUNCPTR (rsn_audiodec_set_sink_caps));
126
 
  gst_pad_set_getcaps_function (self->sinkpad,
127
 
      GST_DEBUG_FUNCPTR (rsn_audiodec_get_sink_caps));
128
 
  gst_pad_set_chain_function (self->sinkpad,
129
 
      GST_DEBUG_FUNCPTR (rsn_audiodec_chain));
130
 
  gst_pad_set_event_function (self->sinkpad,
131
 
      GST_DEBUG_FUNCPTR (rsn_audiodec_sink_event));
132
 
  gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
133
 
 
134
 
  self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
135
 
  gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
136
 
}
137
 
 
138
 
static void
139
 
rsn_audiodec_dispose (GObject * object)
140
 
{
141
 
  RsnAudioDec *self = (RsnAudioDec *) object;
142
 
  cleanup_child (self);
143
 
 
144
 
  G_OBJECT_CLASS (rsn_audiodec_parent_class)->dispose (object);
145
 
}
146
 
 
147
 
static gboolean
148
 
rsn_audiodec_set_sink_caps (GstPad * pad, GstCaps * caps)
149
 
{
150
 
  RsnAudioDec *self = (RsnAudioDec *) gst_pad_get_parent (pad);
151
 
  gboolean res;
152
 
  if (self == NULL)
153
 
    goto error;
154
 
 
155
 
  res = gst_pad_set_caps (self->child_sink, caps);
156
 
 
157
 
  gst_object_unref (self);
158
 
  return res;
159
 
error:
160
 
  if (self)
161
 
    gst_object_unref (self);
162
 
  return FALSE;
163
 
}
164
 
 
165
 
static GstCaps *
166
 
rsn_audiodec_get_sink_caps (GstPad * sinkpad)
167
 
{
168
 
  /* FIXME: Calculate set of allowed caps for all discovered decoders */
169
 
  RsnAudioDec *self = (RsnAudioDec *) gst_pad_get_parent (sinkpad);
170
 
  GstCaps *res;
171
 
  if (self == NULL || self->child_sink == NULL)
172
 
    goto error;
173
 
 
174
 
  res = gst_pad_get_caps (self->child_sink);
175
 
  GST_INFO_OBJECT (self, "Returning caps %" GST_PTR_FORMAT, res);
176
 
 
177
 
  gst_object_unref (self);
178
 
  return res;
179
 
error:
180
 
  if (self)
181
 
    gst_object_unref (self);
182
 
  return gst_caps_copy (gst_pad_get_pad_template_caps (sinkpad));
183
 
}
184
 
 
185
 
static GstFlowReturn
186
 
rsn_audiodec_chain (GstPad * pad, GstBuffer * buf)
187
 
{
188
 
  RsnAudioDec *self = (RsnAudioDec *) gst_pad_get_parent (pad);
189
 
  GstFlowReturn res;
190
 
  if (self == NULL)
191
 
    goto error;
192
 
 
193
 
  GST_INFO_OBJECT (self, "Pushing buffer %" GST_PTR_FORMAT " into decoder",
194
 
      buf);
195
 
  res = gst_pad_chain (self->child_sink, buf);
196
 
 
197
 
  gst_object_unref (self);
198
 
  return res;
199
 
error:
200
 
  if (self)
201
 
    gst_object_unref (self);
202
 
  return GST_FLOW_ERROR;
203
 
}
204
 
 
205
 
static gboolean
206
 
rsn_audiodec_sink_event (GstPad * pad, GstEvent * event)
207
 
{
208
 
  GstPad *sinkpad = GST_PAD (gst_pad_get_parent (pad));
209
 
  RsnAudioDec *self = (RsnAudioDec *) gst_pad_get_parent (sinkpad);
210
 
  gboolean res;
211
 
 
212
 
  gst_object_unref (sinkpad);
213
 
 
214
 
  if (self == NULL)
215
 
    goto error;
216
 
 
217
 
  GST_INFO_OBJECT (self, "Sending event %" GST_PTR_FORMAT " into decoder",
218
 
      event);
219
 
  res = gst_pad_send_event (self->child_sink, event);
220
 
 
221
 
  gst_object_unref (self);
222
 
  return res;
223
 
error:
224
 
  if (self)
225
 
    gst_object_unref (self);
226
 
  return FALSE;
227
 
}
228
 
 
229
 
static GstCaps *
230
 
rsn_audiodec_get_proxy_sink_caps (GstPad * pad)
231
 
{
232
 
  GstPad *sinkpad = GST_PAD (gst_pad_get_parent (pad));
233
 
  GstCaps *ret;
234
 
 
235
 
  if (sinkpad != NULL) {
236
 
    ret = gst_pad_get_caps (sinkpad);
237
 
  } else
238
 
    ret = gst_caps_new_any ();
239
 
 
240
 
  gst_object_unref (sinkpad);
241
 
 
242
 
  return ret;
243
 
}
244
 
 
245
 
static GstCaps *
246
 
rsn_audiodec_get_proxy_src_caps (GstPad * pad)
247
 
{
248
 
  GstPad *srcpad = GST_PAD (gst_pad_get_parent (pad));
249
 
  GstCaps *ret;
250
 
 
251
 
  if (srcpad != NULL) {
252
 
    ret = gst_pad_get_caps (srcpad);
253
 
  } else
254
 
    ret = gst_caps_new_any ();
255
 
 
256
 
  gst_object_unref (srcpad);
257
 
 
258
 
  return ret;
259
 
}
260
 
 
261
 
static GstFlowReturn
262
 
rsn_audiodec_proxy_src_chain (GstPad * pad, GstBuffer * buf)
263
 
{
264
 
  GstPad *srcpad = GST_PAD (gst_pad_get_parent (pad));
265
 
  RsnAudioDec *self = (RsnAudioDec *) gst_pad_get_parent (srcpad);
266
 
  GstFlowReturn ret;
267
 
 
268
 
  gst_object_unref (srcpad);
269
 
 
270
 
  if (self == NULL)
271
 
    return GST_FLOW_ERROR;
272
 
 
273
 
  GST_DEBUG_OBJECT (self, "Data from decoder, pushing to pad %"
274
 
      GST_PTR_FORMAT, self->srcpad);
275
 
  ret = gst_pad_push (self->srcpad, buf);
276
 
 
277
 
  gst_object_unref (self);
278
 
 
279
 
  return ret;
280
 
}
281
 
 
282
 
static gboolean
283
 
rsn_audiodec_proxy_src_event (GstPad * pad, GstEvent * event)
284
 
{
285
 
  GstPad *srcpad = GST_PAD (gst_pad_get_parent (pad));
286
 
  RsnAudioDec *self = (RsnAudioDec *) gst_pad_get_parent (srcpad);
287
 
  gboolean ret;
288
 
 
289
 
  gst_object_unref (srcpad);
290
 
 
291
 
  if (self == NULL)
292
 
    return FALSE;
293
 
 
294
 
  ret = gst_pad_push_event (self->srcpad, event);
295
 
 
296
 
  gst_object_unref (self);
297
 
  return ret;
298
 
}
299
 
 
300
 
static void
301
 
create_proxy_pads (RsnAudioDec * self)
302
 
{
303
 
  if (self->child_sink_proxy == NULL) {
304
 
    /* A src pad the child can query/send events to */
305
 
    self->child_sink_proxy = gst_pad_new ("sink_proxy", GST_PAD_SRC);
306
 
    gst_object_set_parent ((GstObject *) self->child_sink_proxy,
307
 
        (GstObject *) self->sinkpad);
308
 
    gst_pad_set_getcaps_function (self->child_sink_proxy,
309
 
        GST_DEBUG_FUNCPTR (rsn_audiodec_get_proxy_sink_caps));
310
 
  }
311
 
 
312
 
  if (self->child_src_proxy == NULL) {
313
 
    /* A sink pad the child can push to */
314
 
    self->child_src_proxy = gst_pad_new ("src_proxy", GST_PAD_SINK);
315
 
    gst_object_set_parent ((GstObject *) self->child_src_proxy,
316
 
        (GstObject *) self->srcpad);
317
 
    gst_pad_set_getcaps_function (self->child_src_proxy,
318
 
        GST_DEBUG_FUNCPTR (rsn_audiodec_get_proxy_src_caps));
319
 
    gst_pad_set_chain_function (self->child_src_proxy,
320
 
        GST_DEBUG_FUNCPTR (rsn_audiodec_proxy_src_chain));
321
 
    gst_pad_set_event_function (self->child_src_proxy,
322
 
        GST_DEBUG_FUNCPTR (rsn_audiodec_proxy_src_event));
323
 
  }
324
 
}
325
 
 
326
 
static gboolean
327
 
rsn_audiodec_set_child (RsnAudioDec * self, GstElement * new_child)
328
 
{
329
 
  if (self->current_decoder) {
330
 
    gst_bin_remove ((GstBin *) self, self->current_decoder);
331
 
    self->current_decoder = NULL;
332
 
  }
333
 
  if (self->child_sink) {
334
 
    (void) gst_pad_unlink (self->child_sink_proxy, self->child_sink);
335
 
    gst_object_unref (self->child_sink);
336
 
    self->child_sink = NULL;
337
 
  }
338
 
  if (self->child_src) {
339
 
    (void) gst_pad_unlink (self->child_src, self->child_src_proxy);
340
 
    gst_object_unref (self->child_src);
341
 
    self->child_src = NULL;
342
 
  }
343
 
 
344
 
  if (new_child == NULL)
345
 
    return TRUE;
346
 
 
347
 
  self->child_sink = gst_element_get_static_pad (new_child, "sink");
348
 
  if (self->child_sink == NULL) {
349
 
    return FALSE;
350
 
  }
351
 
  self->child_src = gst_element_get_static_pad (new_child, "src");
352
 
  if (self->child_src == NULL) {
353
 
    return FALSE;
354
 
  }
355
 
  if (!gst_bin_add ((GstBin *) self, new_child)) {
356
 
    return FALSE;
357
 
  }
358
 
 
359
 
  GST_DEBUG_OBJECT (self, "Add child %" GST_PTR_FORMAT, new_child);
360
 
  self->current_decoder = new_child;
361
 
  if (gst_pad_link (self->child_sink_proxy,
362
 
          self->child_sink) != GST_PAD_LINK_OK)
363
 
    return FALSE;
364
 
  GST_DEBUG_OBJECT (self, "linked proxy sink pad %" GST_PTR_FORMAT
365
 
      " to child sink %" GST_PTR_FORMAT, self->child_sink_proxy,
366
 
      self->child_sink);
367
 
  if (gst_pad_link (self->child_src, self->child_src_proxy) != GST_PAD_LINK_OK)
368
 
    return FALSE;
369
 
  GST_DEBUG_OBJECT (self, "linked child src pad %" GST_PTR_FORMAT
370
 
      " to proxy pad %" GST_PTR_FORMAT, self->child_src, self->child_src_proxy);
371
 
 
372
 
  return TRUE;
373
 
}
374
 
 
375
 
static void
376
 
cleanup_child (RsnAudioDec * self)
377
 
{
378
 
  GST_DEBUG_OBJECT (self, "Removing child element");
379
 
  (void) rsn_audiodec_set_child (self, NULL);
380
 
  GST_DEBUG_OBJECT (self, "Destroying proxy pads");
381
 
  if (self->child_sink_proxy != NULL) {
382
 
    gst_object_unparent ((GstObject *) self->child_sink_proxy);
383
 
    self->child_sink_proxy = NULL;
384
 
  }
385
 
  if (self->child_src_proxy != NULL) {
386
 
    gst_object_unparent ((GstObject *) self->child_src_proxy);
387
 
    self->child_src_proxy = NULL;
388
 
  }
389
 
}
390
 
 
391
 
static GstStateChangeReturn
392
 
rsn_audiodec_change_state (GstElement * element, GstStateChange transition)
393
 
{
394
 
  GstStateChangeReturn ret;
395
 
  RsnAudioDec *self = RSN_AUDIODEC (element);
396
 
 
397
 
  switch (transition) {
398
 
    case GST_STATE_CHANGE_NULL_TO_READY:{
399
 
      GstElement *new_child;
400
 
      create_proxy_pads (self);
401
 
      GST_DEBUG_OBJECT (self, "Created proxy pads");
402
 
      new_child = gst_element_factory_make ("a52dec", NULL);
403
 
      if (new_child == NULL || !rsn_audiodec_set_child (self, new_child))
404
 
        ret = GST_STATE_CHANGE_FAILURE;
405
 
      break;
406
 
    }
407
 
    case GST_STATE_CHANGE_READY_TO_PAUSED:
408
 
      GST_DEBUG_OBJECT (self, "Activating proxy pads");
409
 
      if (self->child_sink_proxy)
410
 
        gst_pad_set_active (self->child_sink_proxy, TRUE);
411
 
      if (self->child_src_proxy)
412
 
        gst_pad_set_active (self->child_src_proxy, TRUE);
413
 
      break;
414
 
    default:
415
 
      break;
416
 
  }
417
 
 
418
 
  ret =
419
 
      GST_ELEMENT_CLASS (rsn_audiodec_parent_class)->change_state (element,
420
 
      transition);
421
 
  if (ret == GST_STATE_CHANGE_FAILURE)
422
 
    return ret;
423
 
 
424
 
  switch (transition) {
425
 
    case GST_STATE_CHANGE_PAUSED_TO_READY:
426
 
      GST_DEBUG_OBJECT (self, "Deactivating proxy pads");
427
 
      if (self->child_sink_proxy)
428
 
        gst_pad_set_active (self->child_sink_proxy, FALSE);
429
 
      if (self->child_src_proxy)
430
 
        gst_pad_set_active (self->child_src_proxy, FALSE);
431
 
      break;
432
 
    case GST_STATE_CHANGE_READY_TO_NULL:
433
 
      cleanup_child (self);
434
 
      break;
435
 
    default:
436
 
      break;
437
 
  }
438
 
 
439
 
  return ret;
440
 
 
441
 
}