gst/interleave/interleave.*: Allow setting channel positions via a property and allow...
authorSebastian Dröge <slomo@circular-chaos.org>
Mon, 2 Jun 2008 12:42:14 +0000 (12:42 +0000)
committerSebastian Dröge <slomo@circular-chaos.org>
Mon, 2 Jun 2008 12:42:14 +0000 (12:42 +0000)
Original commit message from CVS:
* gst/interleave/interleave.c: (gst_interleave_pad_get_type),
(gst_interleave_finalize), (gst_audio_check_channel_positions),
(gst_interleave_set_channel_positions),
(gst_interleave_class_init), (gst_interleave_init),
(gst_interleave_set_property), (gst_interleave_get_property),
(gst_interleave_request_new_pad), (gst_interleave_release_pad),
(gst_interleave_sink_setcaps), (gst_interleave_src_query_duration),
(gst_interleave_src_query_latency), (gst_interleave_collected):
* gst/interleave/interleave.h:
Allow setting channel positions via a property and allow using the
channel positions on the input as the channel positions of the output.
Fix some broken logic and memory leaks.
* tests/check/Makefile.am:
* tests/check/elements/interleave.c: (src_handoff_float32),
(sink_handoff_float32), (GST_START_TEST), (interleave_suite):
Add unit tests for checking correct handling of channel positions.

gst/interleave/interleave.c
gst/interleave/interleave.h
tests/check/elements/interleave.c

index 699a1f0..15332da 100644 (file)
@@ -25,8 +25,7 @@
 
 /* TODO:
  *       - handle caps changes
- *       - set channel positions / keep from upstream
- *       - handle more queries
+ *       - handle more queries/events
  */
 
 /**
@@ -71,6 +70,8 @@
 #include <string.h>
 #include "interleave.h"
 
+#include <gst/audio/multichannel.h>
+
 GST_DEBUG_CATEGORY_STATIC (gst_interleave_debug);
 #define GST_CAT_DEFAULT gst_interleave_debug
 
@@ -90,6 +91,7 @@ static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink%d",
         "endianness = (int) { LITTLE_ENDIAN , BIG_ENDIAN }, "
         "width = (int) { 32, 64 }")
     );
+
 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_SRC,
     GST_PAD_ALWAYS,
@@ -138,23 +140,67 @@ interleave_24 (guint8 * out, guint8 * in, guint stride, guint nframes)
 
 typedef struct
 {
-  GstCollectData data;
+  GstPad parent;
   guint channel;
-} GstInterleaveCollectData;
+} GstInterleavePad;
+
+#define GST_TYPE_INTERLEAVE_PAD (gst_interleave_pad_get_type())
+#define GST_INTERLEAVE_PAD(pad) (G_TYPE_CHECK_INSTANCE_CAST((pad),GST_TYPE_INTERLEAVE_PAD,GstInterleavePad))
+#define GST_INTERLEAVE_PAD_CAST(pad) ((GstInterleavePad *) pad)
+#define GST_IS_INTERLEAVE_PAD(pad) (G_TYPE_CHECK_INSTANCE_TYPE((pad),GST_TYPE_INTERLEAVE_PAD))
+static GType
+gst_interleave_pad_get_type (void)
+{
+  static GType type = 0;
+
+  if (!type) {
+    static const GTypeInfo info = {
+      sizeof (GstPadClass),
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      sizeof (GstInterleavePad),
+      0,
+      NULL
+    };
+
+    type = g_type_register_static (GST_TYPE_PAD, "GstInterleavePad", &info, 0);
+  }
+  return type;
+}
+
 
 GST_BOILERPLATE (GstInterleave, gst_interleave, GstElement, GST_TYPE_ELEMENT);
 
+enum
+{
+  PROP_0,
+  PROP_CHANNEL_POSITIONS,
+  PROP_CHANNEL_POSITIONS_FROM_INPUT
+};
+
+static void gst_interleave_set_property (GObject * object,
+    guint prop_id, const GValue * value, GParamSpec * pspec);
+static void gst_interleave_get_property (GObject * object,
+    guint prop_id, GValue * value, GParamSpec * pspec);
+
 static GstPad *gst_interleave_request_new_pad (GstElement * element,
     GstPadTemplate * templ, const gchar * name);
 static void gst_interleave_release_pad (GstElement * element, GstPad * pad);
+
 static GstStateChangeReturn gst_interleave_change_state (GstElement * element,
     GstStateChange transition);
 
 static gboolean gst_interleave_src_query (GstPad * pad, GstQuery * query);
+
 static gboolean gst_interleave_src_event (GstPad * pad, GstEvent * event);
 
 static gboolean gst_interleave_sink_event (GstPad * pad, GstEvent * event);
+
 static gboolean gst_interleave_sink_setcaps (GstPad * pad, GstCaps * caps);
+
 static GstCaps *gst_interleave_sink_getcaps (GstPad * pad);
 
 static GstFlowReturn gst_interleave_collected (GstCollectPads * pads,
@@ -170,11 +216,151 @@ gst_interleave_finalize (GObject * object)
     self->collect = NULL;
   }
 
+  if (self->channel_positions
+      && self->channel_positions != self->input_channel_positions) {
+    g_value_array_free (self->channel_positions);
+    self->channel_positions = NULL;
+  }
+
+  if (self->input_channel_positions) {
+    g_value_array_free (self->input_channel_positions);
+    self->input_channel_positions = NULL;
+  }
+
   gst_caps_replace (&self->sinkcaps, NULL);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
+/* Copied from base/gst-libs/gst/audio/multichannel.c and adjusted to our needs */
+static gboolean
+gst_audio_check_channel_positions (GValueArray * positions)
+{
+  gint i, n;
+
+  gint channels;
+
+  GstAudioChannelPosition *pos;
+
+  const struct
+  {
+    const GstAudioChannelPosition pos1[2];
+    const GstAudioChannelPosition pos2[1];
+  } conf[] = {
+    /* front: mono <-> stereo */
+    { {
+    GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+            GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
+    GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, { {
+    GST_AUDIO_CHANNEL_POSITION_INVALID}}
+  };
+
+  channels = positions->n_values;
+  pos = g_new (GstAudioChannelPosition, positions->n_values);
+
+  for (i = 0; i < channels; i++) {
+    GValue *v = g_value_array_get_nth (positions, i);
+
+    pos[i] = g_value_get_enum (v);
+  }
+
+  /* check for invalid channel positions */
+  for (n = 0; n < channels; n++) {
+    if (pos[n] <= GST_AUDIO_CHANNEL_POSITION_INVALID ||
+        pos[n] >= GST_AUDIO_CHANNEL_POSITION_NUM) {
+      goto fail;
+    }
+  }
+
+  /* either all channel positions are NONE or all are defined,
+   * but having only some channel positions NONE and others not
+   * is not allowed */
+  if (pos[0] == GST_AUDIO_CHANNEL_POSITION_NONE) {
+    for (n = 1; n < channels; ++n) {
+      if (pos[n] != GST_AUDIO_CHANNEL_POSITION_NONE) {
+        goto fail;
+      }
+    }
+    /* all positions are NONE, we are done here */
+    goto fail;
+  }
+
+  /* check for multiple position occurrences */
+  for (i = GST_AUDIO_CHANNEL_POSITION_INVALID + 1;
+      i < GST_AUDIO_CHANNEL_POSITION_NUM; i++) {
+    gint count = 0;
+
+    for (n = 0; n < channels; n++) {
+      if (pos[n] == i)
+        count++;
+    }
+
+    /* NONE may not occur mixed with other channel positions */
+    if (i == GST_AUDIO_CHANNEL_POSITION_NONE && count > 0) {
+      goto fail;
+    }
+
+    if (count > 1) {
+      goto fail;
+    }
+  }
+
+  /* check for position conflicts */
+  for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) {
+    gboolean found1 = FALSE, found2 = FALSE;
+
+    for (n = 0; n < channels; n++) {
+      if (pos[n] == conf[i].pos1[0] || pos[n] == conf[i].pos1[1])
+        found1 = TRUE;
+      else if (pos[n] == conf[i].pos2[0])
+        found2 = TRUE;
+    }
+
+    if (found1 && found2) {
+      goto fail;
+    }
+  }
+
+  g_free (pos);
+  return TRUE;
+
+fail:
+  g_free (pos);
+  return FALSE;
+}
+
+static void
+gst_interleave_set_channel_positions (GstInterleave * self, GstStructure * s)
+{
+  GValue pos_array = { 0, };
+  gint i;
+
+  g_value_init (&pos_array, GST_TYPE_ARRAY);
+
+  if (self->channel_positions
+      && self->channels == self->channel_positions->n_values
+      && gst_audio_check_channel_positions (self->channel_positions)) {
+    GST_DEBUG_OBJECT (self, "Using provided channel positions");
+    for (i = 0; i < self->channels; i++)
+      gst_value_array_append_value (&pos_array,
+          g_value_array_get_nth (self->channel_positions, i));
+  } else {
+    GValue pos_none = { 0, };
+
+    GST_WARNING_OBJECT (self, "Using NONE channel positions");
+
+    g_value_init (&pos_none, GST_TYPE_AUDIO_CHANNEL_POSITION);
+    g_value_set_enum (&pos_none, GST_AUDIO_CHANNEL_POSITION_NONE);
+
+    for (i = 0; i < self->channels; i++)
+      gst_value_array_append_value (&pos_array, &pos_none);
+
+    g_value_unset (&pos_none);
+  }
+  gst_structure_set_value (s, "channel-positions", &pos_array);
+  g_value_unset (&pos_array);
+}
+
 static void
 gst_interleave_base_init (gpointer g_class)
 {
@@ -194,6 +380,7 @@ static void
 gst_interleave_class_init (GstInterleaveClass * klass)
 {
   GstElementClass *gstelement_class;
+
   GObjectClass *gobject_class;
 
   gobject_class = G_OBJECT_CLASS (klass);
@@ -203,6 +390,48 @@ gst_interleave_class_init (GstInterleaveClass * klass)
       "interleave element");
 
   gobject_class->finalize = gst_interleave_finalize;
+  gobject_class->set_property = gst_interleave_set_property;
+  gobject_class->get_property = gst_interleave_get_property;
+
+  /**
+   * GstInterleave:channel-positions
+   * 
+   * Channel positions: This property controls the channel positions
+   * that are used on the src caps. The number of elements should be
+   * the same as the number of sink pads and the array should contain
+   * a valid list of channel positions. The n-th element of the array
+   * is the position of the n-th sink pad.
+   *
+   * These channel positions will only be used if they're valid and the
+   * number of elements is the same as the number of channels. If this
+   * is not given a NONE layout will be used.
+   *
+   */
+  g_object_class_install_property (gobject_class, PROP_CHANNEL_POSITIONS,
+      g_param_spec_value_array ("channel-positions", "Channel positions",
+          "Channel positions used on the output",
+          g_param_spec_enum ("channel-position", "Channel position",
+              "Channel position of the n-th input",
+              GST_TYPE_AUDIO_CHANNEL_POSITION,
+              GST_AUDIO_CHANNEL_POSITION_NONE,
+              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  /**
+   * GstInterleave:channel-positions-from-input
+   * 
+   * Channel positions from input: If this property is set to %TRUE the channel
+   * positions will be taken from the input caps if valid channel positions for
+   * the output can be constructed from them. If this is set to %TRUE setting the
+   * channel-positions property overwrites this property again.
+   *
+   */
+  g_object_class_install_property (gobject_class,
+      PROP_CHANNEL_POSITIONS_FROM_INPUT,
+      g_param_spec_boolean ("channel-positions-from-input",
+          "Channel positions from input",
+          "Take channel positions from the input", TRUE,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   gstelement_class->request_new_pad =
       GST_DEBUG_FUNCPTR (gst_interleave_request_new_pad);
@@ -227,6 +456,60 @@ gst_interleave_init (GstInterleave * self, GstInterleaveClass * klass)
   self->collect = gst_collect_pads_new ();
   gst_collect_pads_set_function (self->collect,
       (GstCollectPadsFunction) gst_interleave_collected, self);
+
+  self->input_channel_positions = g_value_array_new (0);
+  self->channel_positions_from_input = TRUE;
+  self->channel_positions = self->input_channel_positions;
+}
+
+static void
+gst_interleave_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstInterleave *self = GST_INTERLEAVE (object);
+
+  switch (prop_id) {
+    case PROP_CHANNEL_POSITIONS:
+      if (self->channel_positions &&
+          self->channel_positions != self->input_channel_positions)
+        g_value_array_free (self->channel_positions);
+
+      self->channel_positions = g_value_dup_boxed (value);
+      self->channel_positions_from_input = FALSE;
+      break;
+    case PROP_CHANNEL_POSITIONS_FROM_INPUT:
+      self->channel_positions_from_input = g_value_get_boolean (value);
+
+      if (self->channel_positions_from_input) {
+        if (self->channel_positions &&
+            self->channel_positions != self->input_channel_positions)
+          g_value_array_free (self->channel_positions);
+        self->channel_positions = self->input_channel_positions;
+      }
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_interleave_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec)
+{
+  GstInterleave *self = GST_INTERLEAVE (object);
+
+  switch (prop_id) {
+    case PROP_CHANNEL_POSITIONS:
+      g_value_set_boxed (value, self->channel_positions);
+      break;
+    case PROP_CHANNEL_POSITIONS_FROM_INPUT:
+      g_value_set_boolean (value, self->channel_positions_from_input);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
 }
 
 static GstPad *
@@ -234,10 +517,13 @@ gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
     const gchar * req_name)
 {
   GstInterleave *self = GST_INTERLEAVE (element);
+
   GstPad *new_pad;
+
   gchar *pad_name;
+
   gint channels;
-  GstInterleaveCollectData *cdata;
+  GValue val = { 0, };
 
   if (templ->direction != GST_PAD_SINK)
     goto not_sink_pad;
@@ -245,7 +531,10 @@ gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
   channels = g_atomic_int_exchange_and_add (&self->channels, 1);
 
   pad_name = g_strdup_printf ("sink%d", channels);
-  new_pad = gst_pad_new_from_template (templ, pad_name);
+  new_pad = GST_PAD_CAST (g_object_new (GST_TYPE_INTERLEAVE_PAD,
+          "name", pad_name, "direction", templ->direction,
+          "template", templ, NULL));
+  GST_INTERLEAVE_PAD_CAST (new_pad)->channel = channels;
   GST_DEBUG_OBJECT (self, "requested new pad %s", pad_name);
   g_free (pad_name);
 
@@ -254,10 +543,7 @@ gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
   gst_pad_set_getcaps_function (new_pad,
       GST_DEBUG_FUNCPTR (gst_interleave_sink_getcaps));
 
-  cdata = (GstInterleaveCollectData *)
-      gst_collect_pads_add_pad (self->collect, new_pad,
-      sizeof (GstInterleaveCollectData));
-  cdata->channel = channels;
+  gst_collect_pads_add_pad (self->collect, new_pad, sizeof (GstCollectData));
 
   /* FIXME: hacked way to override/extend the event function of
    * GstCollectPads; because it sets its own event function giving the
@@ -269,9 +555,16 @@ gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
   if (!gst_element_add_pad (element, new_pad))
     goto could_not_add;
 
+  g_value_init (&val, GST_TYPE_AUDIO_CHANNEL_POSITION);
+  g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_NONE);
+  self->input_channel_positions =
+      g_value_array_append (self->input_channel_positions, &val);
+  g_value_unset (&val);
+
   /* Update the src caps if we already have them */
   if (self->sinkcaps) {
     GstCaps *srccaps;
+
     GstStructure *s;
 
     /* Take lock to make sure processing finishes first */
@@ -281,6 +574,7 @@ gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
     s = gst_caps_get_structure (srccaps, 0);
 
     gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
+    gst_interleave_set_channel_positions (self, s);
 
     gst_pad_set_caps (self->src, srccaps);
     gst_caps_unref (srccaps);
@@ -310,34 +604,46 @@ gst_interleave_release_pad (GstElement * element, GstPad * pad)
 {
   GstInterleave *self = GST_INTERLEAVE (element);
 
+  GList *l;
+
+  g_return_if_fail (GST_IS_INTERLEAVE_PAD (pad));
+
   /* Take lock to make sure we're not changing this when processing buffers */
   GST_OBJECT_LOCK (self->collect);
 
   self->channels--;
 
+  g_value_array_remove (self->input_channel_positions,
+      GST_INTERLEAVE_PAD_CAST (pad)->channel);
+
+  /* Update channel numbers */
+  GST_OBJECT_LOCK (self);
+  for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
+    GstInterleavePad *ipad = GST_INTERLEAVE_PAD (l->data);
+
+    if (GST_INTERLEAVE_PAD_CAST (pad)->channel < ipad->channel)
+      ipad->channel--;
+  }
+  GST_OBJECT_UNLOCK (self);
+
   /* Update the src caps if we already have them */
   if (self->sinkcaps) {
-    GstCaps *srccaps;
-    GstStructure *s;
-    GSList *l;
-    gint i = 0;
+    if (self->channels > 0) {
+      GstCaps *srccaps;
 
-    srccaps = gst_caps_copy (self->sinkcaps);
-    s = gst_caps_get_structure (srccaps, 0);
+      GstStructure *s;
 
-    gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
+      srccaps = gst_caps_copy (self->sinkcaps);
+      s = gst_caps_get_structure (srccaps, 0);
 
-    gst_pad_set_caps (self->src, srccaps);
-    gst_caps_unref (srccaps);
+      gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
+      gst_interleave_set_channel_positions (self, s);
 
-    /* Update channel numbers */
-    for (l = self->collect->data; l != NULL; l = l->next) {
-      GstInterleaveCollectData *cdata = l->data;
-
-      if (cdata == NULL || cdata->data.pad == pad)
-        continue;
-      cdata->channel = i;
-      i++;
+      gst_pad_set_caps (self->src, srccaps);
+      gst_caps_unref (srccaps);
+    } else {
+      gst_caps_replace (&self->sinkcaps, NULL);
+      gst_pad_set_caps (self->src, NULL);
     }
   }
 
@@ -351,6 +657,7 @@ static GstStateChangeReturn
 gst_interleave_change_state (GstElement * element, GstStateChange transition)
 {
   GstInterleave *self;
+
   GstStateChangeReturn ret;
 
   self = GST_INTERLEAVE (element);
@@ -396,6 +703,7 @@ static void
 __remove_channels (GstCaps * caps)
 {
   GstStructure *s;
+
   gint i, size;
 
   size = gst_caps_get_size (caps);
@@ -410,6 +718,7 @@ static void
 __set_channels (GstCaps * caps, gint channels)
 {
   GstStructure *s;
+
   gint i, size;
 
   size = gst_caps_get_size (caps);
@@ -427,6 +736,7 @@ static GstCaps *
 gst_interleave_sink_getcaps (GstPad * pad)
 {
   GstInterleave *self = GST_INTERLEAVE (gst_pad_get_parent (pad));
+
   GstCaps *result, *peercaps, *sinkcaps;
 
   GST_OBJECT_LOCK (self);
@@ -493,15 +803,21 @@ gst_interleave_set_process_function (GstInterleave * self)
 static gboolean
 gst_interleave_sink_setcaps (GstPad * pad, GstCaps * caps)
 {
-  GstInterleave *self = GST_INTERLEAVE (gst_pad_get_parent (pad));
+  GstInterleave *self;
+
+  g_return_val_if_fail (GST_IS_INTERLEAVE_PAD (pad), FALSE);
+
+  self = GST_INTERLEAVE (gst_pad_get_parent (pad));
 
   /* First caps that are set on a sink pad are used as output caps */
   /* TODO: handle caps changes */
-  if (self->sinkcaps && !gst_caps_is_equal (caps, self->sinkcaps)) {
+  if (self->sinkcaps && !gst_caps_is_subset (caps, self->sinkcaps)) {
     goto cannot_change_caps;
   } else {
     GstCaps *srccaps;
+
     GstStructure *s;
+
     gboolean res;
 
     s = gst_caps_get_structure (caps, 0);
@@ -514,12 +830,26 @@ gst_interleave_sink_setcaps (GstPad * pad, GstCaps * caps)
 
     gst_interleave_set_process_function (self);
 
+    if (gst_structure_has_field (s, "channel-positions")) {
+      const GValue *pos_array;
+
+      pos_array = gst_structure_get_value (s, "channel-positions");
+      if (GST_VALUE_HOLDS_ARRAY (pos_array)
+          && gst_value_array_get_size (pos_array) == 1) {
+        const GValue *pos = gst_value_array_get_value (pos_array, 0);
+
+        GValue *apos = g_value_array_get_nth (self->input_channel_positions,
+            GST_INTERLEAVE_PAD_CAST (pad)->channel);
+
+        g_value_set_enum (apos, g_value_get_enum (pos));
+      }
+    }
+
     srccaps = gst_caps_copy (caps);
     s = gst_caps_get_structure (srccaps, 0);
 
-    /* TODO: channel positions */
     gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
-    gst_structure_remove_field (s, "channel-positions");
+    gst_interleave_set_channel_positions (self, s);
 
     res = gst_pad_set_caps (self->src, srccaps);
     gst_caps_unref (srccaps);
@@ -528,8 +858,17 @@ gst_interleave_sink_setcaps (GstPad * pad, GstCaps * caps)
       goto src_did_not_accept;
   }
 
-  if (!self->sinkcaps)
-    gst_caps_replace (&self->sinkcaps, caps);
+  if (!self->sinkcaps) {
+    GstCaps *sinkcaps = gst_caps_copy (caps);
+
+    GstStructure *s = gst_caps_get_structure (sinkcaps, 0);
+
+    gst_structure_remove_field (s, "channel-positions");
+
+    gst_caps_replace (&self->sinkcaps, sinkcaps);
+
+    gst_caps_unref (sinkcaps);
+  }
 
   gst_object_unref (self);
 
@@ -567,6 +906,7 @@ static gboolean
 gst_interleave_sink_event (GstPad * pad, GstEvent * event)
 {
   GstInterleave *self = GST_INTERLEAVE (gst_pad_get_parent (pad));
+
   gboolean ret;
 
   GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
@@ -597,9 +937,13 @@ static gboolean
 gst_interleave_src_query_duration (GstInterleave * self, GstQuery * query)
 {
   gint64 max;
+
   gboolean res;
+
   GstFormat format;
+
   GstIterator *it;
+
   gboolean done;
 
   /* parse format */
@@ -613,6 +957,7 @@ gst_interleave_src_query_duration (GstInterleave * self, GstQuery * query)
   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
   while (!done) {
     GstIteratorResult ires;
+
     gpointer item;
 
     ires = gst_iterator_next (it, &item);
@@ -623,6 +968,7 @@ gst_interleave_src_query_duration (GstInterleave * self, GstQuery * query)
       case GST_ITERATOR_OK:
       {
         GstPad *pad = GST_PAD_CAST (item);
+
         gint64 duration;
 
         /* ask sink peer for duration */
@@ -638,6 +984,7 @@ gst_interleave_src_query_duration (GstInterleave * self, GstQuery * query)
           else if (duration > max)
             max = duration;
         }
+        gst_object_unref (pad);
         break;
       }
       case GST_ITERATOR_RESYNC:
@@ -667,9 +1014,13 @@ static gboolean
 gst_interleave_src_query_latency (GstInterleave * self, GstQuery * query)
 {
   GstClockTime min, max;
+
   gboolean live;
+
   gboolean res;
+
   GstIterator *it;
+
   gboolean done;
 
   res = TRUE;
@@ -683,6 +1034,7 @@ gst_interleave_src_query_latency (GstInterleave * self, GstQuery * query)
   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
   while (!done) {
     GstIteratorResult ires;
+
     gpointer item;
 
     ires = gst_iterator_next (it, &item);
@@ -693,8 +1045,11 @@ gst_interleave_src_query_latency (GstInterleave * self, GstQuery * query)
       case GST_ITERATOR_OK:
       {
         GstPad *pad = GST_PAD_CAST (item);
+
         GstQuery *peerquery;
+
         GstClockTime min_cur, max_cur;
+
         gboolean live_cur;
 
         peerquery = gst_query_new_latency ();
@@ -718,6 +1073,7 @@ gst_interleave_src_query_latency (GstInterleave * self, GstQuery * query)
         }
 
         gst_query_unref (peerquery);
+        gst_object_unref (pad);
         break;
       }
       case GST_ITERATOR_RESYNC:
@@ -750,6 +1106,7 @@ static gboolean
 gst_interleave_src_query (GstPad * pad, GstQuery * query)
 {
   GstInterleave *self = GST_INTERLEAVE (gst_pad_get_parent (pad));
+
   gboolean res = FALSE;
 
   switch (GST_QUERY_TYPE (query)) {
@@ -812,6 +1169,7 @@ static gboolean
 forward_event (GstInterleave * self, GstEvent * event)
 {
   gboolean ret;
+
   GstIterator *it;
   GValue vret = { 0 };
 
@@ -838,6 +1196,7 @@ static gboolean
 gst_interleave_src_event (GstPad * pad, GstEvent * event)
 {
   GstInterleave *self = GST_INTERLEAVE (gst_pad_get_parent (pad));
+
   gboolean result;
 
   switch (GST_EVENT_TYPE (event)) {
@@ -848,7 +1207,9 @@ gst_interleave_src_event (GstPad * pad, GstEvent * event)
     case GST_EVENT_SEEK:
     {
       GstSeekFlags flags;
+
       GstSeekType curtype;
+
       gint64 cur;
 
       /* parse the seek parameters */
@@ -896,12 +1257,19 @@ static GstFlowReturn
 gst_interleave_collected (GstCollectPads * pads, GstInterleave * self)
 {
   guint size;
+
   GstBuffer *outbuf;
+
   GstFlowReturn ret = GST_FLOW_OK;
+
   GSList *collected;
+
   guint nsamples;
+
   guint ncollected = 0;
+
   gboolean empty = TRUE;
+
   gint width = self->width / 8;
 
   g_return_val_if_fail (self->func != NULL, GST_FLOW_NOT_NEGOTIATED);
@@ -936,15 +1304,17 @@ gst_interleave_collected (GstCollectPads * pads, GstInterleave * self)
   memset (GST_BUFFER_DATA (outbuf), 0, size * self->channels);
 
   for (collected = pads->data; collected != NULL; collected = collected->next) {
-    GstInterleaveCollectData *cdata;
+    GstCollectData *cdata;
+
     GstBuffer *inbuf;
+
     guint8 *outdata;
 
-    cdata = (GstInterleaveCollectData *) collected->data;
+    cdata = (GstCollectData *) collected->data;
 
-    inbuf = gst_collect_pads_take_buffer (pads, (GstCollectData *) cdata, size);
+    inbuf = gst_collect_pads_take_buffer (pads, cdata, size);
     if (inbuf == NULL) {
-      GST_DEBUG_OBJECT (cdata->data.pad, "No buffer available");
+      GST_DEBUG_OBJECT (cdata->pad, "No buffer available");
       goto next;
     }
     ncollected++;
@@ -953,7 +1323,9 @@ gst_interleave_collected (GstCollectPads * pads, GstInterleave * self)
       goto next;
 
     empty = FALSE;
-    outdata = GST_BUFFER_DATA (outbuf) + width * cdata->channel;
+    outdata =
+        GST_BUFFER_DATA (outbuf) +
+        width * GST_INTERLEAVE_PAD_CAST (cdata->pad)->channel;
 
     self->func (outdata, GST_BUFFER_DATA (inbuf), self->channels, nsamples);
 
index 6500695..c98cc43 100644 (file)
@@ -55,6 +55,10 @@ struct _GstInterleave
   gint rate;
   gint width;
 
+  GValueArray *channel_positions;
+  GValueArray *input_channel_positions;
+  gboolean channel_positions_from_input;
+
   GstCaps *sinkcaps;
 
   GstClockTime timestamp;
index 433e2c5..6b47604 100644 (file)
@@ -23,6 +23,7 @@
 #endif
 
 #include <gst/check/gstcheck.h>
+#include <gst/audio/multichannel.h>
 
 GST_START_TEST (test_create_and_unref)
 {
@@ -40,6 +41,7 @@ GST_END_TEST;
 GST_START_TEST (test_request_pads)
 {
   GstElement *interleave;
+
   GstPad *pad1, *pad2;
 
   interleave = gst_element_factory_make ("interleave", NULL);
@@ -65,9 +67,13 @@ GST_START_TEST (test_request_pads)
 GST_END_TEST;
 
 static GstPad **mysrcpads, *mysinkpad;
+
 static GstBus *bus;
+
 static GstElement *interleave;
+
 static gint have_data;
+
 static gfloat input[2];
 
 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
@@ -97,6 +103,7 @@ static GstFlowReturn
 interleave_chain_func (GstPad * pad, GstBuffer * buffer)
 {
   gfloat *outdata;
+
   gint i;
 
   fail_unless (GST_IS_BUFFER (buffer));
@@ -121,11 +128,15 @@ interleave_chain_func (GstPad * pad, GstBuffer * buffer)
 GST_START_TEST (test_interleave_2ch)
 {
   GstElement *queue;
+
   GstPad *sink0, *sink1, *src, *tmp;
+
   GstCaps *caps;
+
   gint i;
 
   GstBuffer *inbuf;
+
   gfloat *indata;
 
   mysrcpads = g_new0 (GstPad *, 2);
@@ -242,11 +253,15 @@ GST_END_TEST;
 GST_START_TEST (test_interleave_2ch_1eos)
 {
   GstElement *queue;
+
   GstPad *sink0, *sink1, *src, *tmp;
+
   GstCaps *caps;
+
   gint i;
 
   GstBuffer *inbuf;
+
   gfloat *indata;
 
   mysrcpads = g_new0 (GstPad *, 2);
@@ -362,14 +377,32 @@ src_handoff_float32 (GstElement * element, GstBuffer * buffer, GstPad * pad,
     gpointer user_data)
 {
   gint n = GPOINTER_TO_INT (user_data);
+
   GstCaps *caps;
+
   gfloat *data;
+
   gint i;
 
-  caps = gst_caps_new_simple ("audio/x-raw-float",
-      "width", G_TYPE_INT, 32,
-      "channels", G_TYPE_INT, 1,
-      "rate", G_TYPE_INT, 48000, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
+  if (GST_PAD_CAPS (pad))
+    caps = gst_caps_ref (GST_PAD_CAPS (pad));
+  else {
+    caps = gst_caps_new_simple ("audio/x-raw-float",
+        "width", G_TYPE_INT, 32,
+        "channels", G_TYPE_INT, 1,
+        "rate", G_TYPE_INT, 48000, "endianness", G_TYPE_INT, G_BYTE_ORDER,
+        NULL);
+
+    if (n == 2) {
+      GstAudioChannelPosition pos[1] =
+          { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT };
+      gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
+    } else if (n == 3) {
+      GstAudioChannelPosition pos[1] =
+          { GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT };
+      gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
+    }
+  }
 
   data = g_new (gfloat, 48000);
   GST_BUFFER_MALLOCDATA (buffer) = (guint8 *) data;
@@ -385,7 +418,7 @@ src_handoff_float32 (GstElement * element, GstBuffer * buffer, GstPad * pad,
   gst_caps_unref (caps);
 
   for (i = 0; i < 48000; i++)
-    data[i] = (n == 0) ? -1.0 : 1.0;
+    data[i] = (n % 2 == 0) ? -1.0 : 1.0;
 }
 
 static void
@@ -393,9 +426,13 @@ sink_handoff_float32 (GstElement * element, GstBuffer * buffer, GstPad * pad,
     gpointer user_data)
 {
   gint i;
+
   gfloat *data;
+
   GstCaps *caps;
 
+  gint n = GPOINTER_TO_INT (user_data);
+
   fail_unless (GST_IS_BUFFER (buffer));
   fail_unless_equals_int (GST_BUFFER_SIZE (buffer),
       48000 * 2 * sizeof (gfloat));
@@ -406,6 +443,22 @@ sink_handoff_float32 (GstElement * element, GstBuffer * buffer, GstPad * pad,
       "channels", G_TYPE_INT, 2,
       "rate", G_TYPE_INT, 48000, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
 
+  if (n == 0) {
+    GstAudioChannelPosition pos[2] =
+        { GST_AUDIO_CHANNEL_POSITION_NONE, GST_AUDIO_CHANNEL_POSITION_NONE };
+    gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
+  } else if (n == 1) {
+    GstAudioChannelPosition pos[2] = { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+      GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
+    };
+    gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
+  } else if (n == 2) {
+    GstAudioChannelPosition pos[2] = { GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
+      GST_AUDIO_CHANNEL_POSITION_REAR_CENTER
+    };
+    gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
+  }
+
   fail_unless (gst_caps_is_equal (caps, GST_BUFFER_CAPS (buffer)));
   gst_caps_unref (caps);
 
@@ -422,7 +475,9 @@ sink_handoff_float32 (GstElement * element, GstBuffer * buffer, GstPad * pad,
 GST_START_TEST (test_interleave_2ch_pipeline)
 {
   GstElement *pipeline, *queue, *src1, *src2, *interleave, *sink;
+
   GstPad *sinkpad0, *sinkpad1, *tmp, *tmp2;
+
   GstMessage *msg;
 
   have_data = 0;
@@ -474,7 +529,8 @@ GST_START_TEST (test_interleave_2ch_pipeline)
   sink = gst_element_factory_make ("fakesink", "sink");
   fail_unless (sink != NULL);
   g_object_set (sink, "signal-handoffs", TRUE, NULL);
-  g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff_float32), NULL);
+  g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff_float32),
+      GINT_TO_POINTER (0));
   gst_bin_add (GST_BIN (pipeline), sink);
   tmp = gst_element_get_static_pad (interleave, "src");
   tmp2 = gst_element_get_static_pad (sink, "sink");
@@ -490,12 +546,195 @@ GST_START_TEST (test_interleave_2ch_pipeline)
   fail_unless (have_data == 4);
 
   gst_element_set_state (pipeline, GST_STATE_NULL);
+  gst_element_release_request_pad (interleave, sinkpad0);
+  gst_object_unref (sinkpad0);
+  gst_element_release_request_pad (interleave, sinkpad1);
+  gst_object_unref (sinkpad1);
+  gst_object_unref (interleave);
   gst_object_unref (pipeline);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_interleave_2ch_pipeline_input_chanpos)
+{
+  GstElement *pipeline, *queue, *src1, *src2, *interleave, *sink;
+
+  GstPad *sinkpad0, *sinkpad1, *tmp, *tmp2;
+
+  GstMessage *msg;
+
+  have_data = 0;
+
+  pipeline = (GstElement *) gst_pipeline_new ("pipeline");
+  fail_unless (pipeline != NULL);
+
+  src1 = gst_element_factory_make ("fakesrc", "src1");
+  fail_unless (src1 != NULL);
+  g_object_set (src1, "num-buffers", 4, NULL);
+  g_object_set (src1, "signal-handoffs", TRUE, NULL);
+  g_signal_connect (src1, "handoff", G_CALLBACK (src_handoff_float32),
+      GINT_TO_POINTER (2));
+  gst_bin_add (GST_BIN (pipeline), src1);
+
+  src2 = gst_element_factory_make ("fakesrc", "src2");
+  fail_unless (src2 != NULL);
+  g_object_set (src2, "num-buffers", 4, NULL);
+  g_object_set (src2, "signal-handoffs", TRUE, NULL);
+  g_signal_connect (src2, "handoff", G_CALLBACK (src_handoff_float32),
+      GINT_TO_POINTER (3));
+  gst_bin_add (GST_BIN (pipeline), src2);
+
+  queue = gst_element_factory_make ("queue", "queue");
+  fail_unless (queue != NULL);
+  gst_bin_add (GST_BIN (pipeline), queue);
+
+  interleave = gst_element_factory_make ("interleave", "interleave");
+  fail_unless (interleave != NULL);
+  g_object_set (interleave, "channel-positions-from-input", TRUE, NULL);
+  gst_bin_add (GST_BIN (pipeline), gst_object_ref (interleave));
+
+  sinkpad0 = gst_element_get_request_pad (interleave, "sink%d");
+  fail_unless (sinkpad0 != NULL);
+  tmp = gst_element_get_static_pad (src1, "src");
+  fail_unless (gst_pad_link (tmp, sinkpad0) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+
+  sinkpad1 = gst_element_get_request_pad (interleave, "sink%d");
+  fail_unless (sinkpad1 != NULL);
+  tmp = gst_element_get_static_pad (src2, "src");
+  tmp2 = gst_element_get_static_pad (queue, "sink");
+  fail_unless (gst_pad_link (tmp, tmp2) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+  gst_object_unref (tmp2);
+  tmp = gst_element_get_static_pad (queue, "src");
+  fail_unless (gst_pad_link (tmp, sinkpad1) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+
+  sink = gst_element_factory_make ("fakesink", "sink");
+  fail_unless (sink != NULL);
+  g_object_set (sink, "signal-handoffs", TRUE, NULL);
+  g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff_float32),
+      GINT_TO_POINTER (1));
+  gst_bin_add (GST_BIN (pipeline), sink);
+  tmp = gst_element_get_static_pad (interleave, "src");
+  tmp2 = gst_element_get_static_pad (sink, "sink");
+  fail_unless (gst_pad_link (tmp, tmp2) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+  gst_object_unref (tmp2);
+
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
+
+  msg = gst_bus_poll (GST_ELEMENT_BUS (pipeline), GST_MESSAGE_EOS, -1);
+  gst_message_unref (msg);
+
+  fail_unless (have_data == 4);
+
+  gst_element_set_state (pipeline, GST_STATE_NULL);
+  gst_element_release_request_pad (interleave, sinkpad0);
+  gst_object_unref (sinkpad0);
+  gst_element_release_request_pad (interleave, sinkpad1);
+  gst_object_unref (sinkpad1);
+  gst_object_unref (interleave);
+  gst_object_unref (pipeline);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_interleave_2ch_pipeline_custom_chanpos)
+{
+  GstElement *pipeline, *queue, *src1, *src2, *interleave, *sink;
+
+  GstPad *sinkpad0, *sinkpad1, *tmp, *tmp2;
+
+  GstMessage *msg;
+
+  GValueArray *arr;
+  GValue val = { 0, };
+
+  have_data = 0;
+
+  pipeline = (GstElement *) gst_pipeline_new ("pipeline");
+  fail_unless (pipeline != NULL);
+
+  src1 = gst_element_factory_make ("fakesrc", "src1");
+  fail_unless (src1 != NULL);
+  g_object_set (src1, "num-buffers", 4, NULL);
+  g_object_set (src1, "signal-handoffs", TRUE, NULL);
+  g_signal_connect (src1, "handoff", G_CALLBACK (src_handoff_float32),
+      GINT_TO_POINTER (0));
+  gst_bin_add (GST_BIN (pipeline), src1);
+
+  src2 = gst_element_factory_make ("fakesrc", "src2");
+  fail_unless (src2 != NULL);
+  g_object_set (src2, "num-buffers", 4, NULL);
+  g_object_set (src2, "signal-handoffs", TRUE, NULL);
+  g_signal_connect (src2, "handoff", G_CALLBACK (src_handoff_float32),
+      GINT_TO_POINTER (1));
+  gst_bin_add (GST_BIN (pipeline), src2);
+
+  queue = gst_element_factory_make ("queue", "queue");
+  fail_unless (queue != NULL);
+  gst_bin_add (GST_BIN (pipeline), queue);
+
+  interleave = gst_element_factory_make ("interleave", "interleave");
+  fail_unless (interleave != NULL);
+  g_object_set (interleave, "channel-positions-from-input", FALSE, NULL);
+  arr = g_value_array_new (2);
+  g_value_init (&val, GST_TYPE_AUDIO_CHANNEL_POSITION);
+  g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER);
+  g_value_array_append (arr, &val);
+  g_value_reset (&val);
+  g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER);
+  g_value_array_append (arr, &val);
+  g_value_unset (&val);
+  g_object_set (interleave, "channel-positions", arr, NULL);
+  g_value_array_free (arr);
+  gst_bin_add (GST_BIN (pipeline), gst_object_ref (interleave));
+
+  sinkpad0 = gst_element_get_request_pad (interleave, "sink%d");
+  fail_unless (sinkpad0 != NULL);
+  tmp = gst_element_get_static_pad (src1, "src");
+  fail_unless (gst_pad_link (tmp, sinkpad0) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+
+  sinkpad1 = gst_element_get_request_pad (interleave, "sink%d");
+  fail_unless (sinkpad1 != NULL);
+  tmp = gst_element_get_static_pad (src2, "src");
+  tmp2 = gst_element_get_static_pad (queue, "sink");
+  fail_unless (gst_pad_link (tmp, tmp2) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+  gst_object_unref (tmp2);
+  tmp = gst_element_get_static_pad (queue, "src");
+  fail_unless (gst_pad_link (tmp, sinkpad1) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+
+  sink = gst_element_factory_make ("fakesink", "sink");
+  fail_unless (sink != NULL);
+  g_object_set (sink, "signal-handoffs", TRUE, NULL);
+  g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff_float32),
+      GINT_TO_POINTER (2));
+  gst_bin_add (GST_BIN (pipeline), sink);
+  tmp = gst_element_get_static_pad (interleave, "src");
+  tmp2 = gst_element_get_static_pad (sink, "sink");
+  fail_unless (gst_pad_link (tmp, tmp2) == GST_PAD_LINK_OK);
+  gst_object_unref (tmp);
+  gst_object_unref (tmp2);
+
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
+
+  msg = gst_bus_poll (GST_ELEMENT_BUS (pipeline), GST_MESSAGE_EOS, -1);
+  gst_message_unref (msg);
+
+  fail_unless (have_data == 4);
+
+  gst_element_set_state (pipeline, GST_STATE_NULL);
   gst_element_release_request_pad (interleave, sinkpad0);
   gst_object_unref (sinkpad0);
   gst_element_release_request_pad (interleave, sinkpad1);
   gst_object_unref (sinkpad1);
   gst_object_unref (interleave);
+  gst_object_unref (pipeline);
 }
 
 GST_END_TEST;
@@ -504,6 +743,7 @@ static Suite *
 interleave_suite (void)
 {
   Suite *s = suite_create ("interleave");
+
   TCase *tc_chain = tcase_create ("general");
 
   suite_add_tcase (s, tc_chain);
@@ -512,6 +752,8 @@ interleave_suite (void)
   tcase_add_test (tc_chain, test_interleave_2ch);
   tcase_add_test (tc_chain, test_interleave_2ch_1eos);
   tcase_add_test (tc_chain, test_interleave_2ch_pipeline);
+  tcase_add_test (tc_chain, test_interleave_2ch_pipeline_input_chanpos);
+  tcase_add_test (tc_chain, test_interleave_2ch_pipeline_custom_chanpos);
 
   return s;
 }