1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *                    2007 Andy Wingo <wingo at pobox.com>
6  *                    2008 Sebastian Dröge <slomo@circular-chaos.rg>
7  *
8  * interleave.c: interleave samples, mostly based on adder.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25 
26 /* TODO:
27  *       - handle caps changes
28  *       - handle more queries/events
29  */
30 
31 /**
32  * SECTION:element-interleave
33  * @see_also: deinterleave
34  *
35  * Merges separate mono inputs into one interleaved stream.
36  *
37  * This element handles all raw floating point sample formats and all signed integer sample formats. The first
38  * caps on one of the sinkpads will set the caps of the output so usually an audioconvert element should be
39  * placed before every sinkpad of interleave.
40  *
41  * It's possible to change the number of channels while the pipeline is running by adding or removing
42  * some of the request pads but this will change the caps of the output buffers. Changing the input
43  * caps is _not_ supported yet.
44  *
45  * The channel number of every sinkpad in the out can be retrieved from the "channel" property of the pad.
46  *
47  * <refsect2>
48  * <title>Example launch line</title>
49  * |[
50  * gst-launch-1.0 filesrc location=file.mp3 ! decodebin ! audioconvert ! "audio/x-raw,channels=2" ! deinterleave name=d  interleave name=i ! audioconvert ! wavenc ! filesink location=test.wav    d.src_0 ! queue ! audioconvert ! i.sink_1    d.src_1 ! queue ! audioconvert ! i.sink_0
51  * ]| Decodes and deinterleaves a Stereo MP3 file into separate channels and
52  * then interleaves the channels again to a WAV file with the channels
53  * exchanged.
54  * |[
55  * gst-launch-1.0 interleave name=i ! audioconvert ! wavenc ! filesink location=file.wav  filesrc location=file1.wav ! decodebin ! audioconvert ! "audio/x-raw,channels=1,channel-mask=(bitmask)0x1" ! queue ! i.sink_0   filesrc location=file2.wav ! decodebin ! audioconvert ! "audio/x-raw,channels=1,channel-mask=(bitmask)0x2" ! queue ! i.sink_1
56  * ]| Interleaves two Mono WAV files to a single Stereo WAV file. Having
57  * channel-masks defined in the sink pads ensures a sane mapping of the mono
58  * streams into the stereo stream. NOTE: the proper way to map channels in
59  * code is by using the channel-positions property of the interleave element.
60  * </refsect2>
61  */
62 
63 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
64  * with newer GLib versions (>= 2.31.0) */
65 #define GLIB_DISABLE_DEPRECATION_WARNINGS
66 
67 #ifdef HAVE_CONFIG_H
68 #  include "config.h"
69 #endif
70 
71 #include <gst/gst.h>
72 #include <string.h>
73 #include "interleave.h"
74 
75 #include <gst/audio/audio.h>
76 #include <gst/audio/audio-enumtypes.h>
77 
78 GST_DEBUG_CATEGORY_STATIC (gst_interleave_debug);
79 #define GST_CAT_DEFAULT gst_interleave_debug
80 
81 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
82     GST_PAD_SINK,
83     GST_PAD_REQUEST,
84     GST_STATIC_CAPS ("audio/x-raw, "
85         "rate = (int) [ 1, MAX ], "
86         "channels = (int) 1, "
87         "format = (string) " GST_AUDIO_FORMATS_ALL ", "
88         "layout = (string) {non-interleaved, interleaved}")
89     );
90 
91 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
92     GST_PAD_SRC,
93     GST_PAD_ALWAYS,
94     GST_STATIC_CAPS ("audio/x-raw, "
95         "rate = (int) [ 1, MAX ], "
96         "channels = (int) [ 1, MAX ], "
97         "format = (string) " GST_AUDIO_FORMATS_ALL ", "
98         "layout = (string) interleaved")
99     );
100 
101 #define MAKE_FUNC(type) \
102 static void interleave_##type (guint##type *out, guint##type *in, \
103     guint stride, guint nframes) \
104 { \
105   gint i; \
106   \
107   for (i = 0; i < nframes; i++) { \
108     *out = in[i]; \
109     out += stride; \
110   } \
111 }
112 
113 MAKE_FUNC (8);
114 MAKE_FUNC (16);
115 MAKE_FUNC (32);
116 MAKE_FUNC (64);
117 
118 static void
interleave_24(guint8 * out,guint8 * in,guint stride,guint nframes)119 interleave_24 (guint8 * out, guint8 * in, guint stride, guint nframes)
120 {
121   gint i;
122 
123   for (i = 0; i < nframes; i++) {
124     memcpy (out, in, 3);
125     out += stride * 3;
126     in += 3;
127   }
128 }
129 
130 typedef struct
131 {
132   GstPad parent;
133   guint channel;
134 } GstInterleavePad;
135 
136 enum
137 {
138   PROP_PAD_0,
139   PROP_PAD_CHANNEL
140 };
141 
142 static void gst_interleave_pad_class_init (GstPadClass * klass);
143 
144 #define GST_TYPE_INTERLEAVE_PAD (gst_interleave_pad_get_type())
145 #define GST_INTERLEAVE_PAD(pad) (G_TYPE_CHECK_INSTANCE_CAST((pad),GST_TYPE_INTERLEAVE_PAD,GstInterleavePad))
146 #define GST_INTERLEAVE_PAD_CAST(pad) ((GstInterleavePad *) pad)
147 #define GST_IS_INTERLEAVE_PAD(pad) (G_TYPE_CHECK_INSTANCE_TYPE((pad),GST_TYPE_INTERLEAVE_PAD))
148 static GType
gst_interleave_pad_get_type(void)149 gst_interleave_pad_get_type (void)
150 {
151   static GType type = 0;
152 
153   if (G_UNLIKELY (type == 0)) {
154     type = g_type_register_static_simple (GST_TYPE_PAD,
155         g_intern_static_string ("GstInterleavePad"), sizeof (GstPadClass),
156         (GClassInitFunc) gst_interleave_pad_class_init,
157         sizeof (GstInterleavePad), NULL, 0);
158   }
159   return type;
160 }
161 
162 static void
gst_interleave_pad_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)163 gst_interleave_pad_get_property (GObject * object,
164     guint prop_id, GValue * value, GParamSpec * pspec)
165 {
166   GstInterleavePad *self = GST_INTERLEAVE_PAD (object);
167 
168   switch (prop_id) {
169     case PROP_PAD_CHANNEL:
170       g_value_set_uint (value, self->channel);
171       break;
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175   }
176 }
177 
178 static void
gst_interleave_pad_class_init(GstPadClass * klass)179 gst_interleave_pad_class_init (GstPadClass * klass)
180 {
181   GObjectClass *gobject_class = (GObjectClass *) klass;
182 
183   gobject_class->get_property = gst_interleave_pad_get_property;
184 
185   g_object_class_install_property (gobject_class,
186       PROP_PAD_CHANNEL,
187       g_param_spec_uint ("channel",
188           "Channel number",
189           "Number of the channel of this pad in the output", 0, G_MAXUINT, 0,
190           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
191 }
192 
193 #define gst_interleave_parent_class parent_class
194 G_DEFINE_TYPE (GstInterleave, gst_interleave, GST_TYPE_ELEMENT);
195 
196 enum
197 {
198   PROP_0,
199   PROP_CHANNEL_POSITIONS,
200   PROP_CHANNEL_POSITIONS_FROM_INPUT
201 };
202 
203 static void gst_interleave_set_property (GObject * object,
204     guint prop_id, const GValue * value, GParamSpec * pspec);
205 static void gst_interleave_get_property (GObject * object,
206     guint prop_id, GValue * value, GParamSpec * pspec);
207 
208 static GstPad *gst_interleave_request_new_pad (GstElement * element,
209     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
210 static void gst_interleave_release_pad (GstElement * element, GstPad * pad);
211 
212 static GstStateChangeReturn gst_interleave_change_state (GstElement * element,
213     GstStateChange transition);
214 
215 static gboolean gst_interleave_src_query (GstPad * pad, GstObject * parent,
216     GstQuery * query);
217 
218 static gboolean gst_interleave_src_event (GstPad * pad, GstObject * parent,
219     GstEvent * event);
220 
221 static gboolean gst_interleave_sink_event (GstCollectPads * pads,
222     GstCollectData * data, GstEvent * event, gpointer user_data);
223 static gboolean gst_interleave_sink_query (GstCollectPads * pads,
224     GstCollectData * data, GstQuery * query, gpointer user_data);
225 
226 static gboolean gst_interleave_sink_setcaps (GstInterleave * self,
227     GstPad * pad, const GstCaps * caps, const GstAudioInfo * info);
228 
229 static GstCaps *gst_interleave_sink_getcaps (GstPad * pad, GstInterleave * self,
230     GstCaps * filter);
231 
232 static GstFlowReturn gst_interleave_collected (GstCollectPads * pads,
233     GstInterleave * self);
234 
235 static void
gst_interleave_finalize(GObject * object)236 gst_interleave_finalize (GObject * object)
237 {
238   GstInterleave *self = GST_INTERLEAVE (object);
239 
240   if (self->collect) {
241     gst_object_unref (self->collect);
242     self->collect = NULL;
243   }
244 
245   if (self->channel_positions
246       && self->channel_positions != self->input_channel_positions) {
247     g_value_array_free (self->channel_positions);
248     self->channel_positions = NULL;
249   }
250 
251   if (self->input_channel_positions) {
252     g_value_array_free (self->input_channel_positions);
253     self->input_channel_positions = NULL;
254   }
255 
256   gst_caps_replace (&self->sinkcaps, NULL);
257 
258   G_OBJECT_CLASS (parent_class)->finalize (object);
259 }
260 
261 static gint
compare_positions(gconstpointer a,gconstpointer b,gpointer user_data)262 compare_positions (gconstpointer a, gconstpointer b, gpointer user_data)
263 {
264   const gint i = *(const gint *) a;
265   const gint j = *(const gint *) b;
266   const gint *pos = (const gint *) user_data;
267 
268   if (pos[i] < pos[j])
269     return -1;
270   else if (pos[i] > pos[j])
271     return 1;
272   else
273     return 0;
274 }
275 
276 static gboolean
gst_interleave_channel_positions_to_mask(GValueArray * positions,gint default_ordering_map[64],guint64 * mask)277 gst_interleave_channel_positions_to_mask (GValueArray * positions,
278     gint default_ordering_map[64], guint64 * mask)
279 {
280   gint i;
281   guint channels;
282   GstAudioChannelPosition *pos;
283   gboolean ret;
284 
285   channels = positions->n_values;
286   pos = g_new (GstAudioChannelPosition, channels);
287 
288   for (i = 0; i < channels; i++) {
289     GValue *val;
290 
291     val = g_value_array_get_nth (positions, i);
292     pos[i] = g_value_get_enum (val);
293   }
294 
295   /* sort the default ordering map according to the position order */
296   for (i = 0; i < channels; i++) {
297     default_ordering_map[i] = i;
298   }
299   g_qsort_with_data (default_ordering_map, channels,
300       sizeof (*default_ordering_map), compare_positions, pos);
301 
302   ret = gst_audio_channel_positions_to_mask (pos, channels, FALSE, mask);
303   g_free (pos);
304 
305   return ret;
306 }
307 
308 static void
gst_interleave_set_channel_positions(GstInterleave * self,GstStructure * s)309 gst_interleave_set_channel_positions (GstInterleave * self, GstStructure * s)
310 {
311   if (self->channels <= 64 &&
312       self->channel_positions != NULL &&
313       self->channels == self->channel_positions->n_values) {
314     if (!gst_interleave_channel_positions_to_mask (self->channel_positions,
315             self->default_channels_ordering_map, &self->channel_mask)) {
316       GST_WARNING_OBJECT (self, "Invalid channel positions, using NONE");
317       self->channel_mask = 0;
318     }
319   } else {
320     self->channel_mask = 0;
321     if (self->channels <= 64) {
322       GST_WARNING_OBJECT (self, "Using NONE channel positions");
323     }
324   }
325   gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK, self->channel_mask,
326       NULL);
327 }
328 
329 static void
gst_interleave_send_stream_start(GstInterleave * self)330 gst_interleave_send_stream_start (GstInterleave * self)
331 {
332   GST_OBJECT_LOCK (self);
333   if (self->send_stream_start) {
334     gchar s_id[32];
335 
336     self->send_stream_start = FALSE;
337     GST_OBJECT_UNLOCK (self);
338 
339     /* stream-start (FIXME: create id based on input ids) */
340     g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
341     gst_pad_push_event (self->src, gst_event_new_stream_start (s_id));
342   } else {
343     GST_OBJECT_UNLOCK (self);
344   }
345 }
346 
347 static void
gst_interleave_class_init(GstInterleaveClass * klass)348 gst_interleave_class_init (GstInterleaveClass * klass)
349 {
350   GstElementClass *gstelement_class;
351   GObjectClass *gobject_class;
352 
353   gobject_class = G_OBJECT_CLASS (klass);
354   gstelement_class = GST_ELEMENT_CLASS (klass);
355 
356   GST_DEBUG_CATEGORY_INIT (gst_interleave_debug, "interleave", 0,
357       "interleave element");
358 
359   gst_element_class_set_static_metadata (gstelement_class, "Audio interleaver",
360       "Filter/Converter/Audio",
361       "Folds many mono channels into one interleaved audio stream",
362       "Andy Wingo <wingo at pobox.com>, "
363       "Sebastian Dröge <slomo@circular-chaos.org>");
364 
365   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
366   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
367 
368   /* Reference GstInterleavePad class to have the type registered from
369    * a threadsafe context
370    */
371   g_type_class_ref (GST_TYPE_INTERLEAVE_PAD);
372 
373   gobject_class->finalize = gst_interleave_finalize;
374   gobject_class->set_property = gst_interleave_set_property;
375   gobject_class->get_property = gst_interleave_get_property;
376 
377   /**
378    * GstInterleave:channel-positions
379    *
380    * Channel positions: This property controls the channel positions
381    * that are used on the src caps. The number of elements should be
382    * the same as the number of sink pads and the array should contain
383    * a valid list of channel positions. The n-th element of the array
384    * is the position of the n-th sink pad.
385    *
386    * These channel positions will only be used if they're valid and the
387    * number of elements is the same as the number of channels. If this
388    * is not given a NONE layout will be used.
389    *
390    */
391   g_object_class_install_property (gobject_class, PROP_CHANNEL_POSITIONS,
392       g_param_spec_value_array ("channel-positions", "Channel positions",
393           "Channel positions used on the output",
394           g_param_spec_enum ("channel-position", "Channel position",
395               "Channel position of the n-th input",
396               GST_TYPE_AUDIO_CHANNEL_POSITION,
397               GST_AUDIO_CHANNEL_POSITION_NONE,
398               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
399           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 
401   /**
402    * GstInterleave:channel-positions-from-input
403    *
404    * Channel positions from input: If this property is set to %TRUE the channel
405    * positions will be taken from the input caps if valid channel positions for
406    * the output can be constructed from them. If this is set to %TRUE setting the
407    * channel-positions property overwrites this property again.
408    *
409    */
410   g_object_class_install_property (gobject_class,
411       PROP_CHANNEL_POSITIONS_FROM_INPUT,
412       g_param_spec_boolean ("channel-positions-from-input",
413           "Channel positions from input",
414           "Take channel positions from the input", TRUE,
415           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 
417   gstelement_class->request_new_pad =
418       GST_DEBUG_FUNCPTR (gst_interleave_request_new_pad);
419   gstelement_class->release_pad =
420       GST_DEBUG_FUNCPTR (gst_interleave_release_pad);
421   gstelement_class->change_state =
422       GST_DEBUG_FUNCPTR (gst_interleave_change_state);
423 }
424 
425 static void
gst_interleave_init(GstInterleave * self)426 gst_interleave_init (GstInterleave * self)
427 {
428   self->src = gst_pad_new_from_static_template (&src_template, "src");
429 
430   gst_pad_set_query_function (self->src,
431       GST_DEBUG_FUNCPTR (gst_interleave_src_query));
432   gst_pad_set_event_function (self->src,
433       GST_DEBUG_FUNCPTR (gst_interleave_src_event));
434 
435   gst_element_add_pad (GST_ELEMENT (self), self->src);
436 
437   self->collect = gst_collect_pads_new ();
438   gst_collect_pads_set_function (self->collect,
439       (GstCollectPadsFunction) gst_interleave_collected, self);
440 
441   self->input_channel_positions = g_value_array_new (0);
442   self->channel_positions_from_input = TRUE;
443   self->channel_positions = self->input_channel_positions;
444 }
445 
446 static void
gst_interleave_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)447 gst_interleave_set_property (GObject * object, guint prop_id,
448     const GValue * value, GParamSpec * pspec)
449 {
450   GstInterleave *self = GST_INTERLEAVE (object);
451 
452   switch (prop_id) {
453     case PROP_CHANNEL_POSITIONS:
454       if (self->channel_positions &&
455           self->channel_positions != self->input_channel_positions)
456         g_value_array_free (self->channel_positions);
457 
458       self->channel_positions = g_value_dup_boxed (value);
459       self->channel_positions_from_input = FALSE;
460       break;
461     case PROP_CHANNEL_POSITIONS_FROM_INPUT:
462       self->channel_positions_from_input = g_value_get_boolean (value);
463 
464       if (self->channel_positions_from_input) {
465         if (self->channel_positions &&
466             self->channel_positions != self->input_channel_positions)
467           g_value_array_free (self->channel_positions);
468         self->channel_positions = self->input_channel_positions;
469       }
470       break;
471     default:
472       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
473       break;
474   }
475 }
476 
477 static void
gst_interleave_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)478 gst_interleave_get_property (GObject * object, guint prop_id,
479     GValue * value, GParamSpec * pspec)
480 {
481   GstInterleave *self = GST_INTERLEAVE (object);
482 
483   switch (prop_id) {
484     case PROP_CHANNEL_POSITIONS:
485       g_value_set_boxed (value, self->channel_positions);
486       break;
487     case PROP_CHANNEL_POSITIONS_FROM_INPUT:
488       g_value_set_boolean (value, self->channel_positions_from_input);
489       break;
490     default:
491       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
492       break;
493   }
494 }
495 
496 static GstPad *
gst_interleave_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * req_name,const GstCaps * caps)497 gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
498     const gchar * req_name, const GstCaps * caps)
499 {
500   GstInterleave *self = GST_INTERLEAVE (element);
501   GstPad *new_pad;
502   gchar *pad_name;
503   gint channel, padnumber;
504   GValue val = { 0, };
505 
506   if (templ->direction != GST_PAD_SINK)
507     goto not_sink_pad;
508 
509   padnumber = g_atomic_int_add (&self->padcounter, 1);
510 
511   channel = g_atomic_int_add (&self->channels, 1);
512   if (!self->channel_positions_from_input)
513     channel = padnumber;
514 
515   pad_name = g_strdup_printf ("sink_%u", padnumber);
516   new_pad = GST_PAD_CAST (g_object_new (GST_TYPE_INTERLEAVE_PAD,
517           "name", pad_name, "direction", templ->direction,
518           "template", templ, NULL));
519   GST_INTERLEAVE_PAD_CAST (new_pad)->channel = channel;
520   GST_DEBUG_OBJECT (self, "requested new pad %s", pad_name);
521   g_free (pad_name);
522 
523   gst_pad_use_fixed_caps (new_pad);
524 
525   gst_collect_pads_add_pad (self->collect, new_pad, sizeof (GstCollectData),
526       NULL, TRUE);
527 
528   gst_collect_pads_set_event_function (self->collect,
529       (GstCollectPadsEventFunction)
530       GST_DEBUG_FUNCPTR (gst_interleave_sink_event), self);
531 
532   gst_collect_pads_set_query_function (self->collect,
533       (GstCollectPadsQueryFunction)
534       GST_DEBUG_FUNCPTR (gst_interleave_sink_query), self);
535 
536   if (!gst_element_add_pad (element, new_pad))
537     goto could_not_add;
538 
539   g_value_init (&val, GST_TYPE_AUDIO_CHANNEL_POSITION);
540   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_NONE);
541   self->input_channel_positions =
542       g_value_array_append (self->input_channel_positions, &val);
543   g_value_unset (&val);
544 
545   /* Update the src caps if we already have them */
546   if (self->sinkcaps) {
547     GstCaps *srccaps;
548     GstStructure *s;
549 
550     /* Take lock to make sure processing finishes first */
551     GST_OBJECT_LOCK (self->collect);
552 
553     srccaps = gst_caps_copy (self->sinkcaps);
554     s = gst_caps_get_structure (srccaps, 0);
555 
556     gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
557     gst_interleave_set_channel_positions (self, s);
558 
559     gst_interleave_send_stream_start (self);
560     gst_pad_set_caps (self->src, srccaps);
561     gst_caps_unref (srccaps);
562 
563     GST_OBJECT_UNLOCK (self->collect);
564   }
565 
566   return new_pad;
567 
568   /* errors */
569 not_sink_pad:
570   {
571     g_warning ("interleave: requested new pad that is not a SINK pad\n");
572     return NULL;
573   }
574 could_not_add:
575   {
576     GST_DEBUG_OBJECT (self, "could not add pad %s", GST_PAD_NAME (new_pad));
577     gst_collect_pads_remove_pad (self->collect, new_pad);
578     gst_object_unref (new_pad);
579     return NULL;
580   }
581 }
582 
583 static void
gst_interleave_release_pad(GstElement * element,GstPad * pad)584 gst_interleave_release_pad (GstElement * element, GstPad * pad)
585 {
586   GstInterleave *self = GST_INTERLEAVE (element);
587   GList *l;
588   GstAudioChannelPosition position;
589 
590   g_return_if_fail (GST_IS_INTERLEAVE_PAD (pad));
591 
592   /* Take lock to make sure we're not changing this when processing buffers */
593   GST_OBJECT_LOCK (self->collect);
594 
595   g_atomic_int_add (&self->channels, -1);
596 
597   if (gst_pad_has_current_caps (pad))
598     g_atomic_int_add (&self->configured_sinkpads_counter, -1);
599 
600   position = GST_INTERLEAVE_PAD_CAST (pad)->channel;
601   g_value_array_remove (self->input_channel_positions, position);
602 
603   /* Update channel numbers */
604   GST_OBJECT_LOCK (self);
605   for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
606     GstInterleavePad *ipad = GST_INTERLEAVE_PAD (l->data);
607 
608     if (GST_INTERLEAVE_PAD_CAST (pad)->channel < ipad->channel)
609       ipad->channel--;
610   }
611   GST_OBJECT_UNLOCK (self);
612 
613   /* Update the src caps if we already have them */
614   if (self->sinkcaps) {
615     if (self->channels > 0) {
616       GstCaps *srccaps;
617       GstStructure *s;
618 
619       srccaps = gst_caps_copy (self->sinkcaps);
620       s = gst_caps_get_structure (srccaps, 0);
621 
622       gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
623       gst_interleave_set_channel_positions (self, s);
624 
625       gst_interleave_send_stream_start (self);
626       gst_pad_set_caps (self->src, srccaps);
627       gst_caps_unref (srccaps);
628     } else {
629       gst_caps_replace (&self->sinkcaps, NULL);
630     }
631   }
632 
633   GST_OBJECT_UNLOCK (self->collect);
634 
635   gst_collect_pads_remove_pad (self->collect, pad);
636   gst_element_remove_pad (element, pad);
637 }
638 
639 static GstStateChangeReturn
gst_interleave_change_state(GstElement * element,GstStateChange transition)640 gst_interleave_change_state (GstElement * element, GstStateChange transition)
641 {
642   GstInterleave *self;
643   GstStateChangeReturn ret;
644 
645   self = GST_INTERLEAVE (element);
646 
647   switch (transition) {
648     case GST_STATE_CHANGE_NULL_TO_READY:
649       break;
650     case GST_STATE_CHANGE_READY_TO_PAUSED:
651       self->timestamp = 0;
652       self->offset = 0;
653       gst_event_replace (&self->pending_segment, NULL);
654       self->send_stream_start = TRUE;
655       gst_collect_pads_start (self->collect);
656       break;
657     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
658       break;
659     default:
660       break;
661   }
662 
663   /* Stop before calling the parent's state change function as
664    * GstCollectPads might take locks and we would deadlock in that
665    * case
666    */
667   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
668     gst_collect_pads_stop (self->collect);
669 
670   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
671 
672   switch (transition) {
673     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
674       break;
675     case GST_STATE_CHANGE_PAUSED_TO_READY:
676       gst_caps_replace (&self->sinkcaps, NULL);
677       gst_event_replace (&self->pending_segment, NULL);
678       break;
679     case GST_STATE_CHANGE_READY_TO_NULL:
680       break;
681     default:
682       break;
683   }
684 
685   return ret;
686 }
687 
688 static void
__remove_channels(GstCaps * caps)689 __remove_channels (GstCaps * caps)
690 {
691   GstStructure *s;
692   gint i, size;
693 
694   size = gst_caps_get_size (caps);
695   for (i = 0; i < size; i++) {
696     s = gst_caps_get_structure (caps, i);
697     gst_structure_remove_field (s, "channel-mask");
698     gst_structure_remove_field (s, "channels");
699   }
700 }
701 
702 static void
__set_channels(GstCaps * caps,gint channels)703 __set_channels (GstCaps * caps, gint channels)
704 {
705   GstStructure *s;
706   gint i, size;
707 
708   size = gst_caps_get_size (caps);
709   for (i = 0; i < size; i++) {
710     s = gst_caps_get_structure (caps, i);
711     if (channels > 0)
712       gst_structure_set (s, "channels", G_TYPE_INT, channels, NULL);
713     else
714       gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
715   }
716 }
717 
718 /* we can only accept caps that we and downstream can handle. */
719 static GstCaps *
gst_interleave_sink_getcaps(GstPad * pad,GstInterleave * self,GstCaps * filter)720 gst_interleave_sink_getcaps (GstPad * pad, GstInterleave * self,
721     GstCaps * filter)
722 {
723   GstCaps *result, *peercaps, *sinkcaps;
724 
725   GST_OBJECT_LOCK (self);
726 
727   /* If we already have caps on one of the sink pads return them */
728   if (self->sinkcaps) {
729     result = gst_caps_copy (self->sinkcaps);
730   } else {
731     /* get the downstream possible caps */
732     peercaps = gst_pad_peer_query_caps (self->src, NULL);
733 
734     /* get the allowed caps on this sinkpad */
735     sinkcaps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
736     __remove_channels (sinkcaps);
737     if (peercaps) {
738       peercaps = gst_caps_make_writable (peercaps);
739       __remove_channels (peercaps);
740       /* if the peer has caps, intersect */
741       GST_DEBUG_OBJECT (pad, "intersecting peer and template caps");
742       result = gst_caps_intersect (peercaps, sinkcaps);
743       gst_caps_unref (peercaps);
744       gst_caps_unref (sinkcaps);
745     } else {
746       /* the peer has no caps (or there is no peer), just use the allowed caps
747        * of this sinkpad. */
748       GST_DEBUG_OBJECT (pad, "no peer caps, using sinkcaps");
749       result = sinkcaps;
750     }
751     __set_channels (result, 1);
752   }
753 
754   GST_OBJECT_UNLOCK (self);
755 
756   if (filter != NULL) {
757     GstCaps *caps = result;
758 
759     GST_LOG_OBJECT (pad, "intersecting filter caps %" GST_PTR_FORMAT " with "
760         "preliminary result %" GST_PTR_FORMAT, filter, caps);
761 
762     result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
763     gst_caps_unref (caps);
764   }
765 
766   GST_DEBUG_OBJECT (pad, "Returning caps %" GST_PTR_FORMAT, result);
767 
768   return result;
769 }
770 
771 static void
gst_interleave_set_process_function(GstInterleave * self)772 gst_interleave_set_process_function (GstInterleave * self)
773 {
774   switch (self->width) {
775     case 8:
776       self->func = (GstInterleaveFunc) interleave_8;
777       break;
778     case 16:
779       self->func = (GstInterleaveFunc) interleave_16;
780       break;
781     case 24:
782       self->func = (GstInterleaveFunc) interleave_24;
783       break;
784     case 32:
785       self->func = (GstInterleaveFunc) interleave_32;
786       break;
787     case 64:
788       self->func = (GstInterleaveFunc) interleave_64;
789       break;
790     default:
791       g_assert_not_reached ();
792       break;
793   }
794 }
795 
796 static gboolean
gst_interleave_sink_setcaps(GstInterleave * self,GstPad * pad,const GstCaps * caps,const GstAudioInfo * info)797 gst_interleave_sink_setcaps (GstInterleave * self, GstPad * pad,
798     const GstCaps * caps, const GstAudioInfo * info)
799 {
800   g_return_val_if_fail (GST_IS_INTERLEAVE_PAD (pad), FALSE);
801 
802   /* TODO: handle caps changes */
803   if (self->sinkcaps && !gst_caps_is_subset (caps, self->sinkcaps)) {
804     goto cannot_change_caps;
805   } else {
806     GstCaps *srccaps;
807     GstStructure *s;
808     gboolean res;
809 
810     self->width = GST_AUDIO_INFO_WIDTH (info);
811     self->rate = GST_AUDIO_INFO_RATE (info);
812 
813     gst_interleave_set_process_function (self);
814 
815     srccaps = gst_caps_copy (caps);
816     s = gst_caps_get_structure (srccaps, 0);
817 
818     gst_structure_remove_field (s, "channel-mask");
819 
820     gst_structure_set (s, "channels", G_TYPE_INT, self->channels, "layout",
821         G_TYPE_STRING, "interleaved", NULL);
822     gst_interleave_set_channel_positions (self, s);
823 
824     gst_interleave_send_stream_start (self);
825     res = gst_pad_set_caps (self->src, srccaps);
826     gst_caps_unref (srccaps);
827 
828     if (!res)
829       goto src_did_not_accept;
830   }
831 
832   if (!self->sinkcaps) {
833     GstCaps *sinkcaps = gst_caps_copy (caps);
834     GstStructure *s = gst_caps_get_structure (sinkcaps, 0);
835 
836     gst_structure_remove_field (s, "channel-mask");
837 
838     GST_DEBUG_OBJECT (self, "setting sinkcaps %" GST_PTR_FORMAT, sinkcaps);
839 
840     gst_caps_replace (&self->sinkcaps, sinkcaps);
841 
842     gst_caps_unref (sinkcaps);
843   }
844 
845   return TRUE;
846 
847 cannot_change_caps:
848   {
849     GST_WARNING_OBJECT (self, "caps of %" GST_PTR_FORMAT " already set, can't "
850         "change", self->sinkcaps);
851     return FALSE;
852   }
853 src_did_not_accept:
854   {
855     GST_WARNING_OBJECT (self, "src did not accept setcaps()");
856     return FALSE;
857   }
858 }
859 
860 static gboolean
gst_interleave_sink_event(GstCollectPads * pads,GstCollectData * data,GstEvent * event,gpointer user_data)861 gst_interleave_sink_event (GstCollectPads * pads, GstCollectData * data,
862     GstEvent * event, gpointer user_data)
863 {
864   GstInterleave *self = GST_INTERLEAVE (user_data);
865   gboolean ret = TRUE;
866 
867   GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
868       GST_DEBUG_PAD_NAME (data->pad));
869 
870   switch (GST_EVENT_TYPE (event)) {
871     case GST_EVENT_FLUSH_STOP:
872       GST_OBJECT_LOCK (self);
873       gst_event_replace (&self->pending_segment, NULL);
874       GST_OBJECT_UNLOCK (self);
875       break;
876     case GST_EVENT_SEGMENT:
877     {
878       GST_OBJECT_LOCK (self);
879       gst_event_replace (&self->pending_segment, event);
880       GST_OBJECT_UNLOCK (self);
881       break;
882     }
883     case GST_EVENT_CAPS:
884     {
885       GstCaps *caps;
886       GstAudioInfo info;
887       GValue *val;
888       guint channel;
889 
890       gst_event_parse_caps (event, &caps);
891 
892       if (!gst_audio_info_from_caps (&info, caps)) {
893         GST_WARNING_OBJECT (self, "invalid sink caps");
894         gst_event_unref (event);
895         event = NULL;
896         ret = FALSE;
897         break;
898       }
899 
900       if (self->channel_positions_from_input
901           && GST_AUDIO_INFO_CHANNELS (&info) == 1) {
902         channel = GST_INTERLEAVE_PAD_CAST (data->pad)->channel;
903         val = g_value_array_get_nth (self->input_channel_positions, channel);
904         g_value_set_enum (val, GST_AUDIO_INFO_POSITION (&info, 0));
905       }
906 
907       if (!gst_pad_has_current_caps (data->pad))
908         g_atomic_int_add (&self->configured_sinkpads_counter, 1);
909 
910       /* Last caps that are set on a sink pad are used as output caps */
911       if (g_atomic_int_get (&self->configured_sinkpads_counter) ==
912           self->channels) {
913         ret = gst_interleave_sink_setcaps (self, data->pad, caps, &info);
914         gst_event_unref (event);
915         event = NULL;
916       }
917       break;
918     }
919     case GST_EVENT_TAG:
920       GST_FIXME_OBJECT (self, "FIXME: merge tags and send after stream-start");
921       break;
922     default:
923       break;
924   }
925 
926   /* now GstCollectPads can take care of the rest, e.g. EOS */
927   if (event != NULL)
928     return gst_collect_pads_event_default (pads, data, event, FALSE);
929 
930   return ret;
931 }
932 
933 static gboolean
gst_interleave_sink_query(GstCollectPads * pads,GstCollectData * data,GstQuery * query,gpointer user_data)934 gst_interleave_sink_query (GstCollectPads * pads,
935     GstCollectData * data, GstQuery * query, gpointer user_data)
936 {
937   GstInterleave *self = GST_INTERLEAVE (user_data);
938   gboolean ret = TRUE;
939 
940   GST_DEBUG ("Got %s query on pad %s:%s", GST_QUERY_TYPE_NAME (query),
941       GST_DEBUG_PAD_NAME (data->pad));
942 
943   switch (GST_QUERY_TYPE (query)) {
944     case GST_QUERY_CAPS:
945     {
946       GstCaps *filter, *caps;
947 
948       gst_query_parse_caps (query, &filter);
949       caps = gst_interleave_sink_getcaps (data->pad, self, filter);
950       gst_query_set_caps_result (query, caps);
951       gst_caps_unref (caps);
952       ret = TRUE;
953       break;
954     }
955     default:
956       ret = gst_collect_pads_query_default (pads, data, query, FALSE);
957       break;
958   }
959 
960   return ret;
961 }
962 
963 static gboolean
gst_interleave_src_query_duration(GstInterleave * self,GstQuery * query)964 gst_interleave_src_query_duration (GstInterleave * self, GstQuery * query)
965 {
966   gint64 max;
967   gboolean res;
968   GstFormat format;
969   GstIterator *it;
970   gboolean done;
971 
972   /* parse format */
973   gst_query_parse_duration (query, &format, NULL);
974 
975   max = -1;
976   res = TRUE;
977   done = FALSE;
978 
979   /* Take maximum of all durations */
980   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
981   while (!done) {
982     GstIteratorResult ires;
983 
984     GValue item = { 0, };
985 
986     ires = gst_iterator_next (it, &item);
987     switch (ires) {
988       case GST_ITERATOR_DONE:
989         done = TRUE;
990         break;
991       case GST_ITERATOR_OK:
992       {
993         GstPad *pad = GST_PAD_CAST (g_value_dup_object (&item));
994 
995         gint64 duration;
996 
997         /* ask sink peer for duration */
998         res &= gst_pad_peer_query_duration (pad, format, &duration);
999         /* take max from all valid return values */
1000         if (res) {
1001           /* valid unknown length, stop searching */
1002           if (duration == -1) {
1003             max = duration;
1004             done = TRUE;
1005           }
1006           /* else see if bigger than current max */
1007           else if (duration > max)
1008             max = duration;
1009         }
1010         gst_object_unref (pad);
1011         g_value_unset (&item);
1012         break;
1013       }
1014       case GST_ITERATOR_RESYNC:
1015         max = -1;
1016         res = TRUE;
1017         gst_iterator_resync (it);
1018         break;
1019       default:
1020         res = FALSE;
1021         done = TRUE;
1022         break;
1023     }
1024   }
1025   gst_iterator_free (it);
1026 
1027   if (res) {
1028     /* If in bytes format we have to multiply with the number of channels
1029      * to get the correct results. All other formats should be fine */
1030     if (format == GST_FORMAT_BYTES && max != -1)
1031       max *= self->channels;
1032 
1033     /* and store the max */
1034     GST_DEBUG_OBJECT (self, "Total duration in format %s: %"
1035         GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1036     gst_query_set_duration (query, format, max);
1037   }
1038 
1039   return res;
1040 }
1041 
1042 static gboolean
gst_interleave_src_query(GstPad * pad,GstObject * parent,GstQuery * query)1043 gst_interleave_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1044 {
1045   GstInterleave *self = GST_INTERLEAVE (parent);
1046   gboolean res = FALSE;
1047 
1048   switch (GST_QUERY_TYPE (query)) {
1049     case GST_QUERY_POSITION:
1050     {
1051       GstFormat format;
1052 
1053       gst_query_parse_position (query, &format, NULL);
1054 
1055       switch (format) {
1056         case GST_FORMAT_TIME:
1057           /* FIXME, bring to stream time, might be tricky */
1058           gst_query_set_position (query, format, self->timestamp);
1059           res = TRUE;
1060           break;
1061         case GST_FORMAT_BYTES:
1062           gst_query_set_position (query, format,
1063               self->offset * self->channels * self->width);
1064           res = TRUE;
1065           break;
1066         case GST_FORMAT_DEFAULT:
1067           gst_query_set_position (query, format, self->offset);
1068           res = TRUE;
1069           break;
1070         default:
1071           break;
1072       }
1073       break;
1074     }
1075     case GST_QUERY_DURATION:
1076       res = gst_interleave_src_query_duration (self, query);
1077       break;
1078     default:
1079       /* FIXME, needs a custom query handler because we have multiple
1080        * sinkpads */
1081       res = gst_pad_query_default (pad, parent, query);
1082       break;
1083   }
1084 
1085   return res;
1086 }
1087 
1088 static gboolean
forward_event_func(const GValue * item,GValue * ret,GstEvent * event)1089 forward_event_func (const GValue * item, GValue * ret, GstEvent * event)
1090 {
1091   GstPad *pad = GST_PAD_CAST (g_value_dup_object (item));
1092   gst_event_ref (event);
1093   GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
1094   if (!gst_pad_push_event (pad, event)) {
1095     g_value_set_boolean (ret, FALSE);
1096     GST_WARNING_OBJECT (pad, "Sending event  %p (%s) failed.",
1097         event, GST_EVENT_TYPE_NAME (event));
1098   } else {
1099     GST_LOG_OBJECT (pad, "Sent event  %p (%s).",
1100         event, GST_EVENT_TYPE_NAME (event));
1101   }
1102   gst_object_unref (pad);
1103   return TRUE;
1104 }
1105 
1106 static gboolean
forward_event(GstInterleave * self,GstEvent * event)1107 forward_event (GstInterleave * self, GstEvent * event)
1108 {
1109   GstIterator *it;
1110   GValue vret = { 0 };
1111 
1112   GST_LOG_OBJECT (self, "Forwarding event %p (%s)", event,
1113       GST_EVENT_TYPE_NAME (event));
1114 
1115   g_value_init (&vret, G_TYPE_BOOLEAN);
1116   g_value_set_boolean (&vret, TRUE);
1117   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
1118   gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
1119       event);
1120   gst_iterator_free (it);
1121   gst_event_unref (event);
1122 
1123   return g_value_get_boolean (&vret);
1124 }
1125 
1126 
1127 static gboolean
gst_interleave_src_event(GstPad * pad,GstObject * parent,GstEvent * event)1128 gst_interleave_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1129 {
1130   GstInterleave *self = GST_INTERLEAVE (parent);
1131   gboolean result;
1132 
1133   switch (GST_EVENT_TYPE (event)) {
1134     case GST_EVENT_QOS:
1135       /* QoS might be tricky */
1136       result = FALSE;
1137       break;
1138     case GST_EVENT_SEEK:
1139     {
1140       GstSeekFlags flags;
1141 
1142       gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
1143 
1144       /* check if we are flushing */
1145       if (flags & GST_SEEK_FLAG_FLUSH) {
1146         /* make sure we accept nothing anymore and return WRONG_STATE */
1147         gst_collect_pads_set_flushing (self->collect, TRUE);
1148 
1149         /* flushing seek, start flush downstream, the flush will be done
1150          * when all pads received a FLUSH_STOP. */
1151         gst_pad_push_event (self->src, gst_event_new_flush_start ());
1152       }
1153       result = forward_event (self, event);
1154       break;
1155     }
1156     case GST_EVENT_NAVIGATION:
1157       /* navigation is rather pointless. */
1158       result = FALSE;
1159       break;
1160     default:
1161       /* just forward the rest for now */
1162       result = forward_event (self, event);
1163       break;
1164   }
1165 
1166   return result;
1167 }
1168 
1169 static GstFlowReturn
gst_interleave_collected(GstCollectPads * pads,GstInterleave * self)1170 gst_interleave_collected (GstCollectPads * pads, GstInterleave * self)
1171 {
1172   guint size;
1173   GstBuffer *outbuf = NULL;
1174   GstFlowReturn ret = GST_FLOW_OK;
1175   GSList *collected;
1176   guint nsamples;
1177   guint ncollected = 0;
1178   gboolean empty = TRUE;
1179   gint width = self->width / 8;
1180   GstMapInfo write_info;
1181   GstClockTime timestamp = -1;
1182 
1183   size = gst_collect_pads_available (pads);
1184   if (size == 0)
1185     goto eos;
1186 
1187   g_return_val_if_fail (self->func != NULL, GST_FLOW_NOT_NEGOTIATED);
1188   g_return_val_if_fail (self->width > 0, GST_FLOW_NOT_NEGOTIATED);
1189   g_return_val_if_fail (self->channels > 0, GST_FLOW_NOT_NEGOTIATED);
1190   g_return_val_if_fail (self->rate > 0, GST_FLOW_NOT_NEGOTIATED);
1191 
1192   g_return_val_if_fail (size % width == 0, GST_FLOW_ERROR);
1193 
1194   GST_DEBUG_OBJECT (self, "Starting to collect %u bytes from %d channels", size,
1195       self->channels);
1196 
1197   nsamples = size / width;
1198 
1199   outbuf = gst_buffer_new_allocate (NULL, size * self->channels, NULL);
1200 
1201   if (outbuf == NULL || gst_buffer_get_size (outbuf) < size * self->channels) {
1202     gst_buffer_unref (outbuf);
1203     return GST_FLOW_NOT_NEGOTIATED;
1204   }
1205 
1206   gst_buffer_map (outbuf, &write_info, GST_MAP_WRITE);
1207   memset (write_info.data, 0, size * self->channels);
1208 
1209   for (collected = pads->data; collected != NULL; collected = collected->next) {
1210     GstCollectData *cdata;
1211     GstBuffer *inbuf;
1212     guint8 *outdata;
1213     GstMapInfo input_info;
1214     gint channel;
1215 
1216     cdata = (GstCollectData *) collected->data;
1217 
1218     inbuf = gst_collect_pads_take_buffer (pads, cdata, size);
1219     if (inbuf == NULL) {
1220       GST_DEBUG_OBJECT (cdata->pad, "No buffer available");
1221       goto next;
1222     }
1223     ncollected++;
1224 
1225     if (timestamp == -1)
1226       timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1227 
1228     if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))
1229       goto next;
1230 
1231     empty = FALSE;
1232     channel = GST_INTERLEAVE_PAD_CAST (cdata->pad)->channel;
1233     if (self->channels <= 64 && self->channel_mask) {
1234       channel = self->default_channels_ordering_map[channel];
1235     }
1236     outdata = write_info.data + width * channel;
1237 
1238     gst_buffer_map (inbuf, &input_info, GST_MAP_READ);
1239     self->func (outdata, input_info.data, self->channels, nsamples);
1240     gst_buffer_unmap (inbuf, &input_info);
1241 
1242   next:
1243     if (inbuf)
1244       gst_buffer_unref (inbuf);
1245   }
1246 
1247   if (ncollected == 0) {
1248     gst_buffer_unmap (outbuf, &write_info);
1249     goto eos;
1250   }
1251 
1252   GST_OBJECT_LOCK (self);
1253   if (self->pending_segment) {
1254     GstEvent *event;
1255     GstSegment segment;
1256 
1257     event = self->pending_segment;
1258     self->pending_segment = NULL;
1259     GST_OBJECT_UNLOCK (self);
1260 
1261     /* convert the input segment to time now */
1262     gst_event_copy_segment (event, &segment);
1263 
1264     if (segment.format != GST_FORMAT_TIME) {
1265       gst_event_unref (event);
1266 
1267       /* not time, convert */
1268       switch (segment.format) {
1269         case GST_FORMAT_BYTES:
1270           segment.start *= width;
1271           if (segment.stop != -1)
1272             segment.stop *= width;
1273           if (segment.position != -1)
1274             segment.position *= width;
1275           /* fallthrough for the samples case */
1276         case GST_FORMAT_DEFAULT:
1277           segment.start =
1278               gst_util_uint64_scale_int (segment.start, GST_SECOND, self->rate);
1279           if (segment.stop != -1)
1280             segment.stop =
1281                 gst_util_uint64_scale_int (segment.stop, GST_SECOND,
1282                 self->rate);
1283           if (segment.position != -1)
1284             segment.position =
1285                 gst_util_uint64_scale_int (segment.position, GST_SECOND,
1286                 self->rate);
1287           break;
1288         default:
1289           GST_WARNING ("can't convert segment values");
1290           segment.start = 0;
1291           segment.stop = -1;
1292           segment.position = 0;
1293           break;
1294       }
1295       event = gst_event_new_segment (&segment);
1296     }
1297     gst_pad_push_event (self->src, event);
1298 
1299     GST_OBJECT_LOCK (self);
1300   }
1301   GST_OBJECT_UNLOCK (self);
1302 
1303   if (timestamp != -1) {
1304     self->offset = gst_util_uint64_scale_int (timestamp, self->rate,
1305         GST_SECOND);
1306     self->timestamp = timestamp;
1307   }
1308 
1309   GST_BUFFER_TIMESTAMP (outbuf) = self->timestamp;
1310   GST_BUFFER_OFFSET (outbuf) = self->offset;
1311 
1312   self->offset += nsamples;
1313   self->timestamp = gst_util_uint64_scale_int (self->offset,
1314       GST_SECOND, self->rate);
1315 
1316   GST_BUFFER_DURATION (outbuf) =
1317       self->timestamp - GST_BUFFER_TIMESTAMP (outbuf);
1318 
1319   if (empty)
1320     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
1321 
1322   gst_buffer_unmap (outbuf, &write_info);
1323 
1324   GST_LOG_OBJECT (self, "pushing outbuf, timestamp %" GST_TIME_FORMAT,
1325       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
1326   ret = gst_pad_push (self->src, outbuf);
1327 
1328   return ret;
1329 
1330 eos:
1331   {
1332     GST_DEBUG_OBJECT (self, "no data available, must be EOS");
1333     if (outbuf)
1334       gst_buffer_unref (outbuf);
1335     gst_pad_push_event (self->src, gst_event_new_eos ());
1336     return GST_FLOW_EOS;
1337   }
1338 }
1339