rtpsource: Add more information to probation warning
[platform/upstream/gst-plugins-good.git] / gst / auparse / gstauparse.c
index 2ede002..b814ad8 100644 (file)
@@ -14,8 +14,8 @@
  *
  * 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -43,6 +43,23 @@ static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_STATIC_CAPS ("audio/x-au")
     );
 
+#define GST_AU_PARSE_RAW_PAD_TEMPLATE_CAPS \
+    "audio/x-raw, "                         \
+    "format= (string) { S8, S16LE, S16BE, S24LE, S24BE, "  \
+                       "S32LE, S32BE, F32LE, F32BE, "        \
+                       "F64LE, F64BE }, " \
+    "rate = (int) [ 8000, 192000 ], "       \
+    "channels = (int) 1, "                  \
+    "layout = (string) interleaved;"        \
+    "audio/x-raw, "                         \
+    "format= (string) { S8, S16LE, S16BE, S24LE, S24BE, "  \
+                       "S32LE, S32BE, F32LE, F32BE, "        \
+                       "F64LE, F64BE }, " \
+    "rate = (int) [ 8000, 192000 ], "       \
+    "channels = (int) 2, "                  \
+    "channel-mask = (bitmask) 0x3,"         \
+    "layout = (string) interleaved"
+
 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
     "audio/x-alaw, "                        \
     "rate = (int) [ 8000, 192000 ], "       \
@@ -61,21 +78,24 @@ static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
 static GstStaticPadTemplate 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 ";"
+    GST_STATIC_CAPS (GST_AU_PARSE_RAW_PAD_TEMPLATE_CAPS "; "
         GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS ";"
         GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS ";"
         GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS));
 
 
 static void gst_au_parse_dispose (GObject * object);
-static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstBuffer * buf);
+static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstObject * parent,
+    GstBuffer * buf);
 static GstStateChangeReturn gst_au_parse_change_state (GstElement * element,
     GstStateChange transition);
 static void gst_au_parse_reset (GstAuParse * auparse);
-static gboolean gst_au_parse_src_query (GstPad * pad, GstQuery * query);
-static gboolean gst_au_parse_src_event (GstPad * pad, GstEvent * event);
-static gboolean gst_au_parse_sink_event (GstPad * pad, GstEvent * event);
+static gboolean gst_au_parse_src_query (GstPad * pad, GstObject * parent,
+    GstQuery * query);
+static gboolean gst_au_parse_src_event (GstPad * pad, GstObject * parent,
+    GstEvent * event);
+static gboolean gst_au_parse_sink_event (GstPad * pad, GstObject * parent,
+    GstEvent * event);
 static gboolean gst_au_parse_src_convert (GstAuParse * auparse,
     GstFormat src_format, gint64 srcval, GstFormat dest_format,
     gint64 * destval);
@@ -98,11 +118,9 @@ gst_au_parse_class_init (GstAuParseClass * klass)
 
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&sink_template));
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&src_template));
-  gst_element_class_set_details_simple (gstelement_class,
+  gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
+  gst_element_class_add_static_pad_template (gstelement_class, &src_template);
+  gst_element_class_set_static_metadata (gstelement_class,
       "AU audio demuxer",
       "Codec/Demuxer/Audio",
       "Parse an .au file into raw audio",
@@ -154,6 +172,8 @@ gst_au_parse_reset (GstAuParse * auparse)
 
   gst_adapter_clear (auparse->adapter);
 
+  gst_caps_replace (&auparse->src_caps, NULL);
+
   /* gst_segment_init (&auparse->segment, GST_FORMAT_TIME); */
 }
 
@@ -180,7 +200,9 @@ gst_au_parse_parse_header (GstAuParse * auparse)
   guint32 size;
   guint8 *head;
   gchar layout[7] = { 0, };
-  gint law = 0, depth = 0, ieee = 0;
+  GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN;
+  gint law = 0;
+  guint endianness;
 
   head = (guint8 *) gst_adapter_map (auparse->adapter, 24);
   g_assert (head != NULL);
@@ -190,14 +212,14 @@ gst_au_parse_parse_header (GstAuParse * auparse)
   switch (GST_READ_UINT32_BE (head)) {
       /* normal format is big endian (au is a Sparc format) */
     case 0x2e736e64:{          /* ".snd" */
-      auparse->endianness = G_BIG_ENDIAN;
+      endianness = G_BIG_ENDIAN;
       break;
     }
       /* and of course, someone had to invent a little endian
        * version.  Used by DEC systems. */
     case 0x646e732e:           /* dns.                          */
     case 0x0064732e:{          /* other source say it is "dns." */
-      auparse->endianness = G_LITTLE_ENDIAN;
+      endianness = G_LITTLE_ENDIAN;
       break;
     }
     default:{
@@ -237,33 +259,50 @@ gst_au_parse_parse_header (GstAuParse * auparse)
   switch (auparse->encoding) {
     case 1:                    /* 8-bit ISDN mu-law G.711 */
       law = 1;
-      depth = 8;
       break;
     case 27:                   /* 8-bit ISDN  A-law G.711 */
       law = 2;
-      depth = 8;
       break;
 
-    case 2:                    /*  8-bit linear PCM */
-      depth = 8;
+    case 2:                    /*  8-bit linear PCM, FIXME signed? */
+      format = GST_AUDIO_FORMAT_S8;
+      auparse->sample_size = auparse->channels;
       break;
     case 3:                    /* 16-bit linear PCM */
-      depth = 16;
+      if (endianness == G_LITTLE_ENDIAN)
+        format = GST_AUDIO_FORMAT_S16LE;
+      else
+        format = GST_AUDIO_FORMAT_S16BE;
+      auparse->sample_size = auparse->channels * 2;
       break;
     case 4:                    /* 24-bit linear PCM */
-      depth = 24;
+      if (endianness == G_LITTLE_ENDIAN)
+        format = GST_AUDIO_FORMAT_S24LE;
+      else
+        format = GST_AUDIO_FORMAT_S24BE;
+      auparse->sample_size = auparse->channels * 3;
       break;
     case 5:                    /* 32-bit linear PCM */
-      depth = 32;
+      if (endianness == G_LITTLE_ENDIAN)
+        format = GST_AUDIO_FORMAT_S32LE;
+      else
+        format = GST_AUDIO_FORMAT_S32BE;
+      auparse->sample_size = auparse->channels * 4;
       break;
 
     case 6:                    /* 32-bit IEEE floating point */
-      ieee = 1;
-      depth = 32;
+      if (endianness == G_LITTLE_ENDIAN)
+        format = GST_AUDIO_FORMAT_F32LE;
+      else
+        format = GST_AUDIO_FORMAT_F32BE;
+      auparse->sample_size = auparse->channels * 4;
       break;
     case 7:                    /* 64-bit IEEE floating point */
-      ieee = 1;
-      depth = 64;
+      if (endianness == G_LITTLE_ENDIAN)
+        format = GST_AUDIO_FORMAT_F64LE;
+      else
+        format = GST_AUDIO_FORMAT_F64BE;
+      auparse->sample_size = auparse->channels * 8;
       break;
 
     case 23:                   /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
@@ -308,34 +347,33 @@ gst_au_parse_parse_header (GstAuParse * auparse)
         "rate", G_TYPE_INT, auparse->samplerate,
         "channels", G_TYPE_INT, auparse->channels, NULL);
     auparse->sample_size = auparse->channels;
-  } else if (ieee) {
-    tempcaps = gst_caps_new_simple ("audio/x-raw-float",
+  } else if (format != GST_AUDIO_FORMAT_UNKNOWN) {
+    GstCaps *templ_caps = gst_pad_get_pad_template_caps (auparse->srcpad);
+    GstCaps *intersection;
+
+    tempcaps = gst_caps_new_simple ("audio/x-raw",
+        "format", G_TYPE_STRING, gst_audio_format_to_string (format),
         "rate", G_TYPE_INT, auparse->samplerate,
-        "channels", G_TYPE_INT, auparse->channels,
-        "endianness", G_TYPE_INT, auparse->endianness,
-        "width", G_TYPE_INT, depth, NULL);
-    auparse->sample_size = auparse->channels * depth / 8;
+        "channels", G_TYPE_INT, auparse->channels, NULL);
+
+    intersection = gst_caps_intersect (tempcaps, templ_caps);
+    gst_caps_unref (tempcaps);
+    gst_caps_unref (templ_caps);
+    tempcaps = intersection;
   } else if (layout[0]) {
     tempcaps = gst_caps_new_simple ("audio/x-adpcm",
         "layout", G_TYPE_STRING, layout, NULL);
     auparse->sample_size = 0;
-  } else {
-    tempcaps = gst_caps_new_simple ("audio/x-raw-int",
-        "rate", G_TYPE_INT, auparse->samplerate,
-        "channels", G_TYPE_INT, auparse->channels,
-        "endianness", G_TYPE_INT, auparse->endianness,
-        "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth,
-        /* FIXME: signed TRUE even for 8-bit PCM? */
-        "signed", G_TYPE_BOOLEAN, TRUE, NULL);
-    auparse->sample_size = auparse->channels * depth / 8;
-  }
+  } else
+    goto unknown_format;
 
   GST_DEBUG_OBJECT (auparse, "sample_size=%d", auparse->sample_size);
 
   gst_au_parse_negotiate_srcpad (auparse, tempcaps);
 
   GST_DEBUG_OBJECT (auparse, "offset=%" G_GINT64_FORMAT, auparse->offset);
-  gst_adapter_unmap (auparse->adapter, auparse->offset);
+  gst_adapter_unmap (auparse->adapter);
+  gst_adapter_flush (auparse->adapter, auparse->offset);
 
   gst_caps_unref (tempcaps);
   return GST_FLOW_OK;
@@ -343,27 +381,27 @@ gst_au_parse_parse_header (GstAuParse * auparse)
   /* ERRORS */
 unknown_header:
   {
-    gst_adapter_unmap (auparse->adapter, 0);
+    gst_adapter_unmap (auparse->adapter);
     GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
     return GST_FLOW_ERROR;
   }
 unsupported_sample_rate:
   {
-    gst_adapter_unmap (auparse->adapter, 0);
+    gst_adapter_unmap (auparse->adapter);
     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
         ("Unsupported samplerate: %u", auparse->samplerate));
     return GST_FLOW_ERROR;
   }
 unsupported_number_of_channels:
   {
-    gst_adapter_unmap (auparse->adapter, 0);
+    gst_adapter_unmap (auparse->adapter);
     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
         ("Unsupported number of channels: %u", auparse->channels));
     return GST_FLOW_ERROR;
   }
 unknown_format:
   {
-    gst_adapter_unmap (auparse->adapter, 0);
+    gst_adapter_unmap (auparse->adapter);
     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
         ("Unsupported encoding: %u", auparse->encoding));
     return GST_FLOW_ERROR;
@@ -373,7 +411,7 @@ unknown_format:
 #define AU_HEADER_SIZE 24
 
 static GstFlowReturn
-gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
+gst_au_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
 {
   GstFlowReturn ret = GST_FLOW_OK;
   GstAuParse *auparse;
@@ -381,11 +419,11 @@ gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
   gint64 timestamp;
   gint64 duration;
   gint64 offset;
-  GstSegment segment;
 
-  auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
+  auparse = GST_AU_PARSE (parent);
 
-  GST_LOG_OBJECT (auparse, "got buffer of size %u", gst_buffer_get_size (buf));
+  GST_LOG_OBJECT (auparse, "got buffer of size %" G_GSIZE_FORMAT,
+      gst_buffer_get_size (buf));
 
   gst_adapter_push (auparse->adapter, buf);
   buf = NULL;
@@ -402,8 +440,11 @@ gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
     if (ret != GST_FLOW_OK)
       goto out;
 
-    gst_segment_init (&segment, GST_FORMAT_TIME);
-    gst_pad_push_event (auparse->srcpad, gst_event_new_segment (&segment));
+    if (auparse->need_segment) {
+      gst_pad_push_event (auparse->srcpad,
+          gst_event_new_segment (&auparse->segment));
+      auparse->need_segment = FALSE;
+    }
   }
 
   avail = gst_adapter_available (auparse->adapter);
@@ -447,7 +488,6 @@ gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
 
 out:
 
-  gst_object_unref (auparse);
   return ret;
 }
 
@@ -524,21 +564,21 @@ gst_au_parse_src_convert (GstAuParse * auparse, GstFormat src_format,
 }
 
 static gboolean
-gst_au_parse_src_query (GstPad * pad, GstQuery * query)
+gst_au_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
 {
   GstAuParse *auparse;
   gboolean ret = FALSE;
 
-  auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
+  auparse = GST_AU_PARSE (parent);
 
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_DURATION:{
-      GstFormat bformat = GST_FORMAT_BYTES;
       GstFormat format;
       gint64 len, val;
 
       gst_query_parse_duration (query, &format, NULL);
-      if (!gst_pad_query_peer_duration (auparse->sinkpad, &bformat, &len)) {
+      if (!gst_pad_peer_query_duration (auparse->sinkpad, GST_FORMAT_BYTES,
+              &len)) {
         GST_DEBUG_OBJECT (auparse, "failed to query upstream length");
         break;
       }
@@ -546,7 +586,9 @@ gst_au_parse_src_query (GstPad * pad, GstQuery * query)
       len -= auparse->offset;
       GST_OBJECT_UNLOCK (auparse);
 
-      ret = gst_au_parse_src_convert (auparse, bformat, len, format, &val);
+      ret =
+          gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, len, format,
+          &val);
 
       if (ret) {
         gst_query_set_duration (query, format, val);
@@ -554,12 +596,12 @@ gst_au_parse_src_query (GstPad * pad, GstQuery * query)
       break;
     }
     case GST_QUERY_POSITION:{
-      GstFormat bformat = GST_FORMAT_BYTES;
       GstFormat format;
       gint64 pos, val;
 
       gst_query_parse_position (query, &format, NULL);
-      if (!gst_pad_query_peer_position (auparse->sinkpad, &bformat, &pos)) {
+      if (!gst_pad_peer_query_position (auparse->sinkpad, GST_FORMAT_BYTES,
+              &pos)) {
         GST_DEBUG_OBJECT (auparse, "failed to query upstream position");
         break;
       }
@@ -587,11 +629,10 @@ gst_au_parse_src_query (GstPad * pad, GstQuery * query)
       break;
     }
     default:
-      ret = gst_pad_query_default (pad, query);
+      ret = gst_pad_query_default (pad, parent, query);
       break;
   }
 
-  gst_object_unref (auparse);
   return ret;
 }
 
@@ -631,12 +672,12 @@ gst_au_parse_handle_seek (GstAuParse * auparse, GstEvent * event)
 }
 
 static gboolean
-gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
+gst_au_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
 {
   GstAuParse *auparse;
   gboolean ret = TRUE;
 
-  auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
+  auparse = GST_AU_PARSE (parent);
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_CAPS:
@@ -649,7 +690,6 @@ gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
     {
       gint64 start, stop, offset = 0;
       GstSegment segment;
-      GstEvent *new_event = NULL;
 
       /* some debug output */
       gst_event_copy_segment (event, &segment);
@@ -681,42 +721,55 @@ gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
       gst_segment_init (&segment, GST_FORMAT_TIME);
       segment.start = segment.time = start;
       segment.stop = stop;
-      new_event = gst_event_new_segment (&segment);
 
-      ret = gst_pad_push_event (auparse->srcpad, new_event);
+      gst_segment_copy_into (&segment, &auparse->segment);
+
+      if (!gst_pad_has_current_caps (auparse->srcpad)) {
+        auparse->need_segment = TRUE;
+        ret = TRUE;
+      } else {
+        auparse->need_segment = FALSE;
+        ret = gst_pad_push_event (auparse->srcpad,
+            gst_event_new_segment (&segment));
+      }
 
       auparse->buffer_offset = offset;
 
       gst_event_unref (event);
       break;
     }
+    case GST_EVENT_EOS:
+      if (!auparse->srcpad) {
+        GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE,
+            ("No valid input found before end of stream"), (NULL));
+      }
+      /* fall-through */
     default:
-      ret = gst_pad_event_default (pad, event);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
   }
 
-  gst_object_unref (auparse);
   return ret;
 }
 
 static gboolean
-gst_au_parse_src_event (GstPad * pad, GstEvent * event)
+gst_au_parse_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
 {
   GstAuParse *auparse;
   gboolean ret;
 
-  auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
+  auparse = GST_AU_PARSE (parent);
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_SEEK:
       ret = gst_au_parse_handle_seek (auparse, event);
+      gst_event_unref (event);
       break;
     default:
-      ret = gst_pad_event_default (pad, event);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
   }
 
-  gst_object_unref (auparse);
   return ret;
 }
 
@@ -753,6 +806,6 @@ plugin_init (GstPlugin * plugin)
 
 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
     GST_VERSION_MINOR,
-    "auparse",
+    auparse,
     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
     GST_PACKAGE_ORIGIN)