summaryrefslogtreecommitdiff
path: root/ext/ladspa/gstladspasink.c
blob: 3cb321c7992477380dae181ca72fc547d9e6e6a3 (plain)
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* GStreamer LADSPA sink category
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *               2001 Steve Baker <stevebaker_org@yahoo.co.uk>
 *               2003 Andy Wingo <wingo at pobox.com>
 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com> (fakesink)
 * Copyright (C) 2013 Juan Manuel Borges CaƱo <juanmabcmail@gmail.com>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include "gstladspasink.h"
#include "gstladspa.h"
#include "gstladspautils.h"
#include <gst/base/gstbasetransform.h>

#include <string.h>

GST_DEBUG_CATEGORY_EXTERN (ladspa_debug);
#define GST_CAT_DEFAULT ladspa_debug

#define GST_LADSPA_SINK_CLASS_TAGS                   "Sink/Audio/LADSPA"
#define GST_LADSPA_SINK_DEFAULT_SYNC                 TRUE
#define GST_LADSPA_SINK_DEFAULT_CAN_ACTIVATE_PUSH    TRUE
#define GST_LADSPA_SINK_DEFAULT_CAN_ACTIVATE_PULL    FALSE
#define GST_LADSPA_SINK_DEFAULT_NUM_BUFFERS          -1

enum
{
  GST_LADSPA_SINK_PROP_0,
  GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PUSH,
  GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PULL,
  GST_LADSPA_SINK_PROP_NUM_BUFFERS,
  GST_LADSPA_SINK_PROP_LAST
};

static GstLADSPASinkClass *gst_ladspa_sink_type_parent_class = NULL;

/*
 * Boilerplates BaseSink add pad.
 */
void
gst_my_base_sink_class_add_pad_template (GstBaseSinkClass * base_class,
    GstCaps * sinkcaps)
{
  GstElementClass *elem_class = GST_ELEMENT_CLASS (base_class);
  GstPadTemplate *pad_template;

  g_return_if_fail (GST_IS_CAPS (sinkcaps));

  pad_template =
      gst_pad_template_new (GST_BASE_TRANSFORM_SINK_NAME, GST_PAD_SINK,
      GST_PAD_ALWAYS, sinkcaps);
  gst_element_class_add_pad_template (elem_class, pad_template);
}

static gboolean
gst_ladspa_sink_type_set_caps (GstBaseSink * base, GstCaps * caps)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (base);
  GstAudioInfo info;

  if (!gst_audio_info_from_caps (&info, caps)) {
    GST_ERROR_OBJECT (base, "received invalid caps");
    return FALSE;
  }

  GST_DEBUG_OBJECT (ladspa, "negotiated to caps %" GST_PTR_FORMAT, caps);

  ladspa->info = info;

  return gst_ladspa_setup (&ladspa->ladspa, GST_AUDIO_INFO_RATE (&info));
}

static gboolean
gst_ladspa_sink_type_query (GstBaseSink * base, GstQuery * query)
{
  gboolean ret;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_SEEKING:{
      GstFormat fmt;

      /* we don't supporting seeking */
      gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
      gst_query_set_seeking (query, fmt, FALSE, 0, -1);
      ret = TRUE;
      break;
    }
    default:
      ret =
          GST_BASE_SINK_CLASS (gst_ladspa_sink_type_parent_class)->query
          (base, query);
      break;
  }

  return ret;
}

static GstFlowReturn
gst_ladspa_sink_type_preroll (GstBaseSink * base, GstBuffer * buffer)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (base);

  if (ladspa->num_buffers_left == 0) {
    GST_DEBUG_OBJECT (ladspa, "we are EOS");
    return GST_FLOW_EOS;
  }

  return GST_FLOW_OK;
}

static GstFlowReturn
gst_ladspa_sink_type_render (GstBaseSink * base, GstBuffer * buf)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (base);
  GstMapInfo info;

  if (ladspa->num_buffers_left == 0)
    goto eos;

  if (ladspa->num_buffers_left != -1)
    ladspa->num_buffers_left--;

  gst_object_sync_values (GST_OBJECT (ladspa), GST_BUFFER_TIMESTAMP (buf));

  gst_buffer_map (buf, &info, GST_MAP_READ);
  gst_ladspa_transform (&ladspa->ladspa, NULL,
      info.size / sizeof (LADSPA_Data) / ladspa->ladspa.klass->count.audio.in,
      info.data);
  gst_buffer_unmap (buf, &info);

  if (ladspa->num_buffers_left == 0)
    goto eos;

  return GST_FLOW_OK;

  /* ERRORS */
eos:
  {
    GST_DEBUG_OBJECT (ladspa, "we are EOS");
    return GST_FLOW_EOS;
  }
}

static GstStateChangeReturn
gst_ladspa_sink_type_change_state (GstElement * element,
    GstStateChange transition)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (element);
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      ladspa->num_buffers_left = ladspa->num_buffers;
      break;
    default:
      break;
  }

  ret =
      GST_ELEMENT_CLASS (gst_ladspa_sink_type_parent_class)->change_state
      (element, transition);

  return ret;
}


static void
gst_ladspa_sink_type_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (object);

  switch (prop_id) {
    case GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PUSH:
      GST_BASE_SINK (ladspa)->can_activate_push = g_value_get_boolean (value);
      break;
    case GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PULL:
      GST_BASE_SINK (ladspa)->can_activate_pull = g_value_get_boolean (value);
      break;
    case GST_LADSPA_SINK_PROP_NUM_BUFFERS:
      ladspa->num_buffers = g_value_get_int (value);
      break;
    default:
      gst_ladspa_object_set_property (&ladspa->ladspa, object, prop_id, value,
          pspec);
      break;
  }
}

static void
gst_ladspa_sink_type_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (object);

  switch (prop_id) {
    case GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PUSH:
      g_value_set_boolean (value, GST_BASE_SINK (ladspa)->can_activate_push);
      break;
    case GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PULL:
      g_value_set_boolean (value, GST_BASE_SINK (ladspa)->can_activate_pull);
      break;
    case GST_LADSPA_SINK_PROP_NUM_BUFFERS:
      g_value_set_int (value, ladspa->num_buffers);
      break;
    default:
      gst_ladspa_object_get_property (&ladspa->ladspa, object, prop_id, value,
          pspec);
      break;
  }
}

static void
gst_ladspa_sink_type_init (GstLADSPASink * ladspa, LADSPA_Descriptor * desc)
{
  GstLADSPASinkClass *ladspa_class = GST_LADSPA_SINK_GET_CLASS (ladspa);
  GstBaseSink *base = GST_BASE_SINK (ladspa);

  gst_ladspa_init (&ladspa->ladspa, &ladspa_class->ladspa);

  ladspa->num_buffers = GST_LADSPA_SINK_DEFAULT_NUM_BUFFERS;

  gst_base_sink_set_sync (base, GST_LADSPA_SINK_DEFAULT_SYNC);
}

static void
gst_ladspa_sink_type_dispose (GObject * object)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (object);

  gst_ladspa_cleanup (&ladspa->ladspa);

  G_OBJECT_CLASS (gst_ladspa_sink_type_parent_class)->dispose (object);
}

static void
gst_ladspa_sink_type_finalize (GObject * object)
{
  GstLADSPASink *ladspa = GST_LADSPA_SINK (object);

  gst_ladspa_finalize (&ladspa->ladspa);

  G_OBJECT_CLASS (gst_ladspa_sink_type_parent_class)->finalize (object);
}

/*
 * It is okay for plugins to 'leak' a one-time allocation. This will be freed when
 * the application exits. When the plugins are scanned for the first time, this is
 * done from a separate process to not impose the memory overhead on the calling
 * application (among other reasons). Hence no need for class_finalize.
 */
static void
gst_ladspa_sink_type_base_init (GstLADSPASinkClass * ladspa_class)
{
  GstElementClass *elem_class = GST_ELEMENT_CLASS (ladspa_class);
  GstBaseSinkClass *base_class = GST_BASE_SINK_CLASS (ladspa_class);

  gst_ladspa_class_init (&ladspa_class->ladspa,
      G_TYPE_FROM_CLASS (ladspa_class));

  gst_ladspa_element_class_set_metadata (&ladspa_class->ladspa, elem_class,
      GST_LADSPA_SINK_CLASS_TAGS);

  gst_ladspa_sink_type_class_add_pad_template (&ladspa_class->ladspa,
      base_class);
}


static void
gst_ladspa_sink_type_base_finalize (GstLADSPASinkClass * ladspa_class)
{
  gst_ladspa_class_finalize (&ladspa_class->ladspa);
}

static void
gst_ladspa_sink_type_class_init (GstLADSPASinkClass * ladspa_class,
    LADSPA_Descriptor * desc)
{
  GObjectClass *object_class = G_OBJECT_CLASS (ladspa_class);
  GstElementClass *elem_class = GST_ELEMENT_CLASS (ladspa_class);
  GstBaseSinkClass *base_class = base_class =
      GST_BASE_SINK_CLASS (ladspa_class);

  gst_ladspa_sink_type_parent_class = g_type_class_peek_parent (ladspa_class);

  object_class->dispose = GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_dispose);
  object_class->finalize = GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_finalize);
  object_class->set_property =
      GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_set_property);
  object_class->get_property =
      GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_get_property);

  elem_class->change_state =
      GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_change_state);

  base_class->set_caps = GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_set_caps);
  base_class->preroll = GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_preroll);
  base_class->render = GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_render);
  base_class->query = GST_DEBUG_FUNCPTR (gst_ladspa_sink_type_query);

  g_object_class_install_property (object_class,
      GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PUSH,
      g_param_spec_boolean ("can-activate-push", "Can activate push",
          "Can activate in push mode",
          GST_LADSPA_SINK_DEFAULT_CAN_ACTIVATE_PUSH,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class,
      GST_LADSPA_SINK_PROP_CAN_ACTIVATE_PULL,
      g_param_spec_boolean ("can-activate-pull", "Can activate pull",
          "Can activate in pull mode",
          GST_LADSPA_SINK_DEFAULT_CAN_ACTIVATE_PULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class,
      GST_LADSPA_SINK_PROP_NUM_BUFFERS, g_param_spec_int ("num-buffers",
          "num-buffers", "Number of buffers to accept going EOS", -1, G_MAXINT,
          GST_LADSPA_SINK_DEFAULT_NUM_BUFFERS,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  gst_ladspa_object_class_install_properties (&ladspa_class->ladspa,
      object_class, GST_LADSPA_SINK_PROP_LAST);

}

G_DEFINE_ABSTRACT_TYPE (GstLADSPASink, gst_ladspa_sink, GST_TYPE_BASE_SINK);

static void
gst_ladspa_sink_init (GstLADSPASink * ladspa)
{
}

static void
gst_ladspa_sink_class_init (GstLADSPASinkClass * ladspa_class)
{
}

/*
 * Construct the type.
 */
void
ladspa_register_sink_element (GstPlugin * plugin, GstStructure * ladspa_meta)
{
  GTypeInfo info = {
    sizeof (GstLADSPASinkClass),
    (GBaseInitFunc) gst_ladspa_sink_type_base_init,
    (GBaseFinalizeFunc) gst_ladspa_sink_type_base_finalize,
    (GClassInitFunc) gst_ladspa_sink_type_class_init,
    NULL,
    NULL,
    sizeof (GstLADSPASink),
    0,
    (GInstanceInitFunc) gst_ladspa_sink_type_init,
    NULL
  };
  ladspa_register_element (plugin, GST_TYPE_LADSPA_SINK, &info, ladspa_meta);
}