My favorites | Sign in
Project Logo
                
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* GStreamer
* Copyright (C) 2008 Andre Moreira Magalhaes <andrunko@gmail.com>
*
* gstcrossfade.c: Crossfade element, N* in, one out
*
* 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.
*/
/**
* SECTION:element-crossfade
*
* <refsect2>
* <para>
* The Crossfade allows to crossfade between audio streams.
* It will mix the data using an adder and proper crossfade them.
* Mixed data is clamped to the min/max values of the data format.
* </para>
* <title>Example launch line</title>
* <para>
* <programlisting>
* gst-launch filesrc location=testcrossfade1.ogg ! decodebin ! audioconvert ! crossfade duration=5000 name=crossfade ! alsasink filesrc location=testcrossfade2.ogg ! decodebin ! audioconvert ! crossfade.
* </programlisting>
* This pipeline crossfade from testcrossfade1.ogg to testcrossfade2.ogg with a 5000 miliseconds duration.
* </para>
* </refsect2>
*/

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

#include "gstcrossfade.h"

#include <gst/audio/audio.h>
#include <gst/base/gstbasetransform.h>

#define GST_CAT_DEFAULT gst_crossfade_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

enum
{
PROP_0,
PROP_DURATION
};

/* elementfactory information */
static const GstElementDetails gst_crossfade_details = GST_ELEMENT_DETAILS ("Crossfade",
"Generic/Audio",
"Crossfade audio",
"Andre Moreira Magalhaes <andrunko@gmail.com>");

static GstStaticPadTemplate gst_crossfade_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
);

static GstStaticPadTemplate gst_crossfade_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink%d",
GST_PAD_SINK,
GST_PAD_REQUEST,
GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
);

static void gst_crossfade_class_init (GstCrossfadeClass * klass);
static void gst_crossfade_init (GstCrossfade * crossfade);
static void gst_crossfade_dispose (GObject * object);
static void gst_crossfade_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_crossfade_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);

static GstPad *gst_crossfade_request_new_pad (GstElement * element,
GstPadTemplate * temp, const gchar * unused);
static void gst_crossfade_release_pad (GstElement * element, GstPad * pad);
static GstPadLinkReturn gst_crossfade_volume_pad_link (GstPad * pad,
GstPad * peer);

static GstController *gst_crossfade_start_xfade (GstElement *volume,
gdouble start, gdouble end,
guint64 position, guint64 duration);

static GstElementClass *parent_class = NULL;

GType
gst_crossfade_get_type (void)
{
static GType crossfade_type = 0;

if (G_UNLIKELY (crossfade_type == 0)) {
static const GTypeInfo crossfade_info = {
sizeof (GstCrossfadeClass),
NULL,
NULL,
(GClassInitFunc) gst_crossfade_class_init,
NULL,
NULL,
sizeof (GstCrossfade),
0,
(GInstanceInitFunc) gst_crossfade_init,
};

crossfade_type = g_type_register_static (GST_TYPE_BIN, "GstCrossfade",
&crossfade_info, 0);
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "crossfade", 0,
"audio crossfade element");
}
return crossfade_type;
}

static void
gst_crossfade_class_init (GstCrossfadeClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;

gobject_class = (GObjectClass *) klass;

gobject_class->dispose = gst_crossfade_dispose;
gobject_class->set_property = gst_crossfade_set_property;
gobject_class->get_property = gst_crossfade_get_property;

g_object_class_install_property (gobject_class, PROP_DURATION,
g_param_spec_uint64 ("duration", "Duration", "Crossfade duration in miliseconds",
0, G_MAXUINT64, 5000, G_PARAM_READWRITE));

gstelement_class = (GstElementClass *) klass;

gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_crossfade_src_template));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_crossfade_sink_template));

gst_element_class_set_details (gstelement_class, &gst_crossfade_details);

parent_class = g_type_class_peek_parent (klass);

gstelement_class->request_new_pad = gst_crossfade_request_new_pad;
gstelement_class->release_pad = gst_crossfade_release_pad;
}

static void
gst_crossfade_init (GstCrossfade * crossfade)
{
GstPad *pad, *gpad;

/* we create the adder element only once */
crossfade->adder = gst_element_factory_make ("adder", "adder");
if (!crossfade->adder) {
GST_WARNING_OBJECT (crossfade, "can't find adder element, crossfade will not work");
return;
}

/* add the adder element */
if (!gst_bin_add (GST_BIN (crossfade), crossfade->adder)) {
GST_WARNING_OBJECT (crossfade, "Could not add adder element, crossfade will not work");
gst_object_unref (crossfade->adder);
crossfade->adder = NULL;
return;
}

/* get the sinkpad */
pad = gst_element_get_pad (crossfade->adder, "src");

/* ghost the sink pad to ourself */
gpad = gst_ghost_pad_new ("src", pad);
gst_pad_set_active (gpad, TRUE);
gst_element_add_pad (GST_ELEMENT (crossfade), gpad);

gst_object_unref (pad);

crossfade->padcount = 0;
crossfade->duration = 5000;
crossfade->cur_volume = crossfade->old_volume = NULL;
crossfade->cntrl_xfadeout = crossfade->cntrl_xfadein = NULL;
}

static void
gst_crossfade_dispose (GObject * object)
{
GstCrossfade *crossfade = GST_CROSSFADE (object);

if (crossfade->cntrl_xfadeout)
g_object_unref (crossfade->cntrl_xfadeout);
if (crossfade->cntrl_xfadein)
g_object_unref (crossfade->cntrl_xfadein);

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

static void
gst_crossfade_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec)
{
GstCrossfade *crossfade;

crossfade = GST_CROSSFADE (object);

switch (prop_id) {
case PROP_DURATION:
g_value_set_uint64 (value, crossfade->duration);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}

static void
gst_crossfade_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec)
{
GstCrossfade *crossfade;

crossfade = GST_CROSSFADE (object);

switch (prop_id) {
case PROP_DURATION:
crossfade->duration = g_value_get_uint64 (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}

static GstPad *
gst_crossfade_request_new_pad (GstElement * element, GstPadTemplate * templ,
const gchar * unused)
{
GstCrossfade *crossfade;
GstElement *volume;
gint padcount;
gchar *name;
GstPad *pad, *gpad;

if (templ->direction != GST_PAD_SINK) {
GST_WARNING_OBJECT (element, "request new pad that is not a SINK pad");
return NULL;
}

crossfade = GST_CROSSFADE (element);

volume = gst_element_factory_make ("volume", NULL);
if (!volume) {
GST_WARNING_OBJECT (crossfade, "Could not create volume element");
return NULL;
}

if (!gst_bin_add (GST_BIN (crossfade), volume)) {
GST_WARNING_OBJECT (crossfade, "Could not add volume element");
gst_object_unref (volume);
return NULL;
}

gst_element_sync_state_with_parent (volume);

if (!gst_element_link (volume, crossfade->adder)) {
GST_WARNING_OBJECT (crossfade, "Could not link volume element to adder element");
gst_object_unref (volume);
return NULL;
}

/* increment pad counter */
padcount = g_atomic_int_exchange_and_add (&crossfade->padcount, 1);
name = g_strdup_printf ("sink%d", padcount);
GST_DEBUG_OBJECT (crossfade, "request new pad %s", name);
/* get the sinkpad */
pad = gst_element_get_pad (volume, "sink");
gst_pad_set_link_function (pad, gst_crossfade_volume_pad_link);
/* ghost the sink pad to ourself */
gpad = gst_ghost_pad_new (name, pad);
gst_pad_set_active (gpad, TRUE);
gst_element_add_pad (GST_ELEMENT (crossfade), gpad);
g_free (name);
gst_object_unref (pad);

return gpad;
}

static void
gst_crossfade_release_pad (GstElement * element, GstPad * pad)
{
GstCrossfade *crossfade;

crossfade = GST_CROSSFADE (element);

GST_DEBUG_OBJECT (crossfade, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));

gst_element_remove_pad (element, pad);
}

static GstPadLinkReturn
gst_crossfade_volume_pad_link (GstPad * pad, GstPad * peer)
{
GstElement *volume;
GstCrossfade *crossfade;

volume = GST_ELEMENT (gst_pad_get_parent (pad));
crossfade = GST_CROSSFADE (gst_element_get_parent (volume));

GST_DEBUG_OBJECT (crossfade, "Volume pad linked");

crossfade->old_volume = crossfade->cur_volume;
crossfade->cur_volume = volume;

if (crossfade->old_volume) {
GstFormat format = GST_FORMAT_TIME;
gint64 position = -1;

if (crossfade->cntrl_xfadeout) {
g_object_unref (crossfade->cntrl_xfadeout);
}
if (crossfade->cntrl_xfadein) {
g_object_unref (crossfade->cntrl_xfadein);
}

gst_element_query_position (crossfade->old_volume, &format, &position);
if (position == -1)
position = 0;
crossfade->cntrl_xfadeout = gst_crossfade_start_xfade (crossfade->old_volume,
1.0, 0.0,
position, crossfade->duration);

gst_element_query_position (crossfade->cur_volume, &format, &position);
/* FIXME should we use position here also ? */
crossfade->cntrl_xfadein = gst_crossfade_start_xfade (crossfade->cur_volume,
0.0, 1.0,
0, crossfade->duration);
}

gst_object_unref (crossfade);
gst_object_unref (volume);

return GST_PAD_LINK_OK;
}

static GstController *
gst_crossfade_start_xfade (GstElement *volume, gdouble start, gdouble end,
guint64 position, guint64 duration)
{
GstController *c;
GstInterpolationControlSource *csource;
GValue value = {0,};

c = gst_controller_new (G_OBJECT (volume), "volume", NULL);
gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (volume), FALSE);

csource = gst_interpolation_control_source_new ();
gst_interpolation_control_source_set_interpolation_mode (csource,
GST_INTERPOLATE_LINEAR);
gst_controller_set_control_source (c, "volume",
GST_CONTROL_SOURCE (csource));
g_object_unref (csource);

g_value_init (&value, G_TYPE_DOUBLE);
g_value_set_double (&value, start);
gst_interpolation_control_source_set (csource,
position, &value);
g_value_set_double (&value, end);
gst_interpolation_control_source_set (csource,
position + duration * GST_MSECOND, &value);
g_value_unset (&value);

return c;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "crossfade",
GST_RANK_NONE, GST_TYPE_CROSSFADE)) {
return FALSE;
}

return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"crossfade",
"Crossfade audio",
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
Show details Hide details

Change log

r44 by andrunko on Feb 27, 2008   Diff
Missing initialization.
Go to: 
Project members, sign in to write a code review

Older revisions

r43 by andrunko on Feb 27, 2008   Diff
Ooops fixed copy&paste. Calling
finalize on dispose.
r42 by andrunko on Feb 27, 2008   Diff
Fixed typo.
r41 by andrunko on Feb 27, 2008   Diff
Added initial crossfade plugin.
All revisions of this file

File info

Size: 11793 bytes, 398 lines
Hosted by Google Code