speexdec: port to audiodecoder
authorMark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Thu, 29 Sep 2011 15:21:22 +0000 (17:21 +0200)
committerMark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Thu, 29 Sep 2011 15:33:25 +0000 (17:33 +0200)
ext/speex/gstspeexdec.c
ext/speex/gstspeexdec.h

index 46f774b..6db9319 100644 (file)
@@ -76,36 +76,22 @@ GST_STATIC_PAD_TEMPLATE ("sink",
     GST_STATIC_CAPS ("audio/x-speex")
     );
 
-GST_BOILERPLATE (GstSpeexDec, gst_speex_dec, GstElement, GST_TYPE_ELEMENT);
-
-static gboolean speex_dec_sink_event (GstPad * pad, GstEvent * event);
-static GstFlowReturn speex_dec_chain (GstPad * pad, GstBuffer * buf);
-static GstStateChangeReturn speex_dec_change_state (GstElement * element,
-    GstStateChange transition);
-
-static gboolean speex_dec_src_event (GstPad * pad, GstEvent * event);
-static gboolean speex_dec_src_query (GstPad * pad, GstQuery * query);
-static gboolean speex_dec_sink_query (GstPad * pad, GstQuery * query);
-static gboolean speex_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
-static const GstQueryType *speex_get_src_query_types (GstPad * pad);
-static const GstQueryType *speex_get_sink_query_types (GstPad * pad);
-static gboolean speex_dec_convert (GstPad * pad,
-    GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value);
+GST_BOILERPLATE (GstSpeexDec, gst_speex_dec, GstAudioDecoder,
+    GST_TYPE_AUDIO_DECODER);
+
+
+static gboolean gst_speex_dec_start (GstAudioDecoder * dec);
+static gboolean gst_speex_dec_stop (GstAudioDecoder * dec);
+static gboolean gst_speex_dec_set_format (GstAudioDecoder * bdec,
+    GstCaps * caps);
+static GstFlowReturn gst_speex_dec_handle_frame (GstAudioDecoder * dec,
+    GstBuffer * buffer);
 
 static void gst_speex_dec_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
 static void gst_speex_dec_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
 
-static GstFlowReturn speex_dec_chain_parse_data (GstSpeexDec * dec,
-    GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
-
-static GstFlowReturn speex_dec_chain_parse_header (GstSpeexDec * dec,
-    GstBuffer * buf);
-static GstFlowReturn speex_dec_chain_parse_comments (GstSpeexDec * dec,
-    GstBuffer * buf);
-
 static void
 gst_speex_dec_base_init (gpointer g_class)
 {
@@ -124,20 +110,23 @@ static void
 gst_speex_dec_class_init (GstSpeexDecClass * klass)
 {
   GObjectClass *gobject_class;
-  GstElementClass *gstelement_class;
+  GstAudioDecoderClass *base_class;
 
   gobject_class = (GObjectClass *) klass;
-  gstelement_class = (GstElementClass *) klass;
+  base_class = (GstAudioDecoderClass *) klass;
 
   gobject_class->set_property = gst_speex_dec_set_property;
   gobject_class->get_property = gst_speex_dec_get_property;
 
+  base_class->start = GST_DEBUG_FUNCPTR (gst_speex_dec_start);
+  base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_dec_stop);
+  base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_dec_set_format);
+  base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_dec_handle_frame);
+
   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
       g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
           DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-  gstelement_class->change_state = GST_DEBUG_FUNCPTR (speex_dec_change_state);
-
   GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
       "speex decoding element");
 }
@@ -145,7 +134,6 @@ gst_speex_dec_class_init (GstSpeexDecClass * klass)
 static void
 gst_speex_dec_reset (GstSpeexDec * dec)
 {
-  gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
   dec->packetno = 0;
   dec->frame_size = 0;
   dec->frame_duration = 0;
@@ -171,396 +159,38 @@ gst_speex_dec_reset (GstSpeexDec * dec)
 static void
 gst_speex_dec_init (GstSpeexDec * dec, GstSpeexDecClass * g_class)
 {
-  dec->sinkpad =
-      gst_pad_new_from_static_template (&speex_dec_sink_factory, "sink");
-  gst_pad_set_chain_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (speex_dec_chain));
-  gst_pad_set_event_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (speex_dec_sink_event));
-  gst_pad_set_query_type_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (speex_get_sink_query_types));
-  gst_pad_set_query_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (speex_dec_sink_query));
-  gst_pad_set_setcaps_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (speex_dec_sink_setcaps));
-  gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
-
-  dec->srcpad =
-      gst_pad_new_from_static_template (&speex_dec_src_factory, "src");
-  gst_pad_use_fixed_caps (dec->srcpad);
-  gst_pad_set_event_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (speex_dec_src_event));
-  gst_pad_set_query_type_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (speex_get_src_query_types));
-  gst_pad_set_query_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (speex_dec_src_query));
-  gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
-
   dec->enh = DEFAULT_ENH;
 
   gst_speex_dec_reset (dec);
 }
 
 static gboolean
-speex_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
+gst_speex_dec_start (GstAudioDecoder * dec)
 {
-  GstSpeexDec *dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
-  gboolean ret = TRUE;
-  GstStructure *s;
-  const GValue *streamheader;
+  GstSpeexDec *sd = GST_SPEEX_DEC (dec);
 
-  s = gst_caps_get_structure (caps, 0);
-  if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
-      G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
-      gst_value_array_get_size (streamheader) >= 2) {
-    const GValue *header, *vorbiscomment;
-    GstBuffer *buf;
-    GstFlowReturn res = GST_FLOW_OK;
+  GST_DEBUG_OBJECT (dec, "start");
+  gst_speex_dec_reset (sd);
 
-    header = gst_value_array_get_value (streamheader, 0);
-    if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
-      buf = gst_value_get_buffer (header);
-      res = speex_dec_chain_parse_header (dec, buf);
-      if (res != GST_FLOW_OK)
-        goto done;
-      gst_buffer_replace (&dec->streamheader, buf);
-    }
+  /* we know about concealment */
+  gst_audio_decoder_set_plc_aware (dec, TRUE);
 
-    vorbiscomment = gst_value_array_get_value (streamheader, 1);
-    if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
-      buf = gst_value_get_buffer (vorbiscomment);
-      res = speex_dec_chain_parse_comments (dec, buf);
-      if (res != GST_FLOW_OK)
-        goto done;
-      gst_buffer_replace (&dec->vorbiscomment, buf);
-    }
-  }
-
-done:
-  gst_object_unref (dec);
-  return ret;
+  return TRUE;
 }
 
 static gboolean
-speex_dec_convert (GstPad * pad,
-    GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value)
+gst_speex_dec_stop (GstAudioDecoder * dec)
 {
-  gboolean res = TRUE;
-  GstSpeexDec *dec;
-  guint64 scale = 1;
-
-  dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
-
-  if (src_format == *dest_format) {
-    *dest_value = src_value;
-    res = TRUE;
-    goto cleanup;
-  }
+  GstSpeexDec *sd = GST_SPEEX_DEC (dec);
 
-  if (dec->packetno < 1) {
-    res = FALSE;
-    goto cleanup;
-  }
-
-  if (pad == dec->sinkpad &&
-      (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES)) {
-    res = FALSE;
-    goto cleanup;
-  }
+  GST_DEBUG_OBJECT (dec, "stop");
+  gst_speex_dec_reset (sd);
 
-  switch (src_format) {
-    case GST_FORMAT_TIME:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          scale = 2 * dec->header->nb_channels;
-        case GST_FORMAT_DEFAULT:
-          *dest_value =
-              gst_util_uint64_scale_int (scale * src_value, dec->header->rate,
-              GST_SECOND);
-          break;
-        default:
-          res = FALSE;
-          break;
-      }
-      break;
-    case GST_FORMAT_DEFAULT:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          *dest_value = src_value * 2 * dec->header->nb_channels;
-          break;
-        case GST_FORMAT_TIME:
-          *dest_value =
-              gst_util_uint64_scale_int (src_value, GST_SECOND,
-              dec->header->rate);
-          break;
-        default:
-          res = FALSE;
-          break;
-      }
-      break;
-    case GST_FORMAT_BYTES:
-      switch (*dest_format) {
-        case GST_FORMAT_DEFAULT:
-          *dest_value = src_value / (2 * dec->header->nb_channels);
-          break;
-        case GST_FORMAT_TIME:
-          *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
-              dec->header->rate * 2 * dec->header->nb_channels);
-          break;
-        default:
-          res = FALSE;
-          break;
-      }
-      break;
-    default:
-      res = FALSE;
-      break;
-  }
-
-cleanup:
-  gst_object_unref (dec);
-  return res;
-}
-
-static const GstQueryType *
-speex_get_sink_query_types (GstPad * pad)
-{
-  static const GstQueryType speex_dec_sink_query_types[] = {
-    GST_QUERY_CONVERT,
-    0
-  };
-
-  return speex_dec_sink_query_types;
-}
-
-static gboolean
-speex_dec_sink_query (GstPad * pad, GstQuery * query)
-{
-  GstSpeexDec *dec;
-  gboolean res;
-
-  dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_CONVERT:
-    {
-      GstFormat src_fmt, dest_fmt;
-      gint64 src_val, dest_val;
-
-      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      res = speex_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
-      if (res) {
-        gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
-      }
-      break;
-    }
-    default:
-      res = gst_pad_query_default (pad, query);
-      break;
-  }
-
-  gst_object_unref (dec);
-  return res;
-}
-
-static const GstQueryType *
-speex_get_src_query_types (GstPad * pad)
-{
-  static const GstQueryType speex_dec_src_query_types[] = {
-    GST_QUERY_POSITION,
-    GST_QUERY_DURATION,
-    0
-  };
-
-  return speex_dec_src_query_types;
-}
-
-static gboolean
-speex_dec_src_query (GstPad * pad, GstQuery * query)
-{
-  GstSpeexDec *dec;
-  gboolean res = FALSE;
-
-  dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_POSITION:{
-      GstSegment segment;
-      GstFormat format;
-      gint64 cur;
-
-      gst_query_parse_position (query, &format, NULL);
-
-      GST_PAD_STREAM_LOCK (dec->sinkpad);
-      segment = dec->segment;
-      GST_PAD_STREAM_UNLOCK (dec->sinkpad);
-
-      if (segment.format != GST_FORMAT_TIME) {
-        GST_DEBUG_OBJECT (dec, "segment not initialised yet");
-        break;
-      }
-
-      if ((res = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME,
-                  segment.last_stop, &format, &cur))) {
-        gst_query_set_position (query, format, cur);
-      }
-      break;
-    }
-    case GST_QUERY_DURATION:{
-      GstFormat format = GST_FORMAT_TIME;
-      gint64 dur;
-
-      /* get duration from demuxer */
-      if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &dur))
-        break;
-
-      gst_query_parse_duration (query, &format, NULL);
-
-      /* and convert it into the requested format */
-      if ((res = speex_dec_convert (dec->srcpad, GST_FORMAT_TIME,
-                  dur, &format, &dur))) {
-        gst_query_set_duration (query, format, dur);
-      }
-      break;
-    }
-    default:
-      res = gst_pad_query_default (pad, query);
-      break;
-  }
-
-  gst_object_unref (dec);
-  return res;
-}
-
-static gboolean
-speex_dec_src_event (GstPad * pad, GstEvent * event)
-{
-  gboolean res = FALSE;
-  GstSpeexDec *dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
-
-  GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
-
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_SEEK:{
-      GstFormat format, tformat;
-      gdouble rate;
-      GstEvent *real_seek;
-      GstSeekFlags flags;
-      GstSeekType cur_type, stop_type;
-      gint64 cur, stop;
-      gint64 tcur, tstop;
-
-      gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
-          &stop_type, &stop);
-
-      /* we have to ask our peer to seek to time here as we know
-       * nothing about how to generate a granulepos from the src
-       * formats or anything.
-       *
-       * First bring the requested format to time
-       */
-      tformat = GST_FORMAT_TIME;
-      if (!(res = speex_dec_convert (pad, format, cur, &tformat, &tcur)))
-        break;
-      if (!(res = speex_dec_convert (pad, format, stop, &tformat, &tstop)))
-        break;
-
-      /* then seek with time on the peer */
-      real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
-          flags, cur_type, tcur, stop_type, tstop);
-
-      GST_LOG_OBJECT (dec, "seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (tcur));
-
-      res = gst_pad_push_event (dec->sinkpad, real_seek);
-      gst_event_unref (event);
-      break;
-    }
-    default:
-      res = gst_pad_event_default (pad, event);
-      break;
-  }
-
-  gst_object_unref (dec);
-  return res;
-}
-
-static gboolean
-speex_dec_sink_event (GstPad * pad, GstEvent * event)
-{
-  GstSpeexDec *dec;
-  gboolean ret = FALSE;
-
-  dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
-
-  GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
-
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_NEWSEGMENT:{
-      GstFormat format;
-      gdouble rate, arate;
-      gint64 start, stop, time;
-      gboolean update;
-
-      gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
-          &start, &stop, &time);
-
-      if (format != GST_FORMAT_TIME)
-        goto newseg_wrong_format;
-
-      if (rate <= 0.0)
-        goto newseg_wrong_rate;
-
-      if (update) {
-        /* time progressed without data, see if we can fill the gap with
-         * some concealment data */
-        if (dec->segment.last_stop < start) {
-          GstClockTime duration;
-
-          duration = start - dec->segment.last_stop;
-          speex_dec_chain_parse_data (dec, NULL, dec->segment.last_stop,
-              duration);
-        }
-      }
-
-      /* now configure the values */
-      gst_segment_set_newsegment_full (&dec->segment, update,
-          rate, arate, GST_FORMAT_TIME, start, stop, time);
-
-      GST_DEBUG_OBJECT (dec, "segment now: cur = %" GST_TIME_FORMAT " [%"
-          GST_TIME_FORMAT " - %" GST_TIME_FORMAT "]",
-          GST_TIME_ARGS (dec->segment.last_stop),
-          GST_TIME_ARGS (dec->segment.start),
-          GST_TIME_ARGS (dec->segment.stop));
-
-      ret = gst_pad_push_event (dec->srcpad, event);
-      break;
-    }
-    default:
-      ret = gst_pad_event_default (pad, event);
-      break;
-  }
-
-  gst_object_unref (dec);
-  return ret;
-
-  /* ERRORS */
-newseg_wrong_format:
-  {
-    GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
-    gst_object_unref (dec);
-    return FALSE;
-  }
-newseg_wrong_rate:
-  {
-    GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
-    gst_object_unref (dec);
-    return FALSE;
-  }
+  return TRUE;
 }
 
 static GstFlowReturn
-speex_dec_chain_parse_header (GstSpeexDec * dec, GstBuffer * buf)
+gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf)
 {
   GstCaps *caps;
 
@@ -607,7 +237,7 @@ speex_dec_chain_parse_header (GstSpeexDec * dec, GstBuffer * buf)
       "endianness", G_TYPE_INT, G_BYTE_ORDER,
       "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
 
-  if (!gst_pad_set_caps (dec->srcpad, caps))
+  if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
     goto nego_failed;
 
   gst_caps_unref (caps);
@@ -644,7 +274,7 @@ nego_failed:
 }
 
 static GstFlowReturn
-speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
+gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
 {
   GstTagList *list;
   gchar *ver, *encoder = NULL;
@@ -679,7 +309,8 @@ speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
 
   GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
 
-  gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list);
+  gst_element_found_tags_for_pad (GST_ELEMENT (dec),
+      GST_AUDIO_DECODER_SRC_PAD (dec), list);
 
   g_free (encoder);
   g_free (ver);
@@ -687,9 +318,47 @@ speex_dec_chain_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
   return GST_FLOW_OK;
 }
 
+static gboolean
+gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
+{
+  GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
+  gboolean ret = TRUE;
+  GstStructure *s;
+  const GValue *streamheader;
+
+  s = gst_caps_get_structure (caps, 0);
+  if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
+      G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
+      gst_value_array_get_size (streamheader) >= 2) {
+    const GValue *header, *vorbiscomment;
+    GstBuffer *buf;
+    GstFlowReturn res = GST_FLOW_OK;
+
+    header = gst_value_array_get_value (streamheader, 0);
+    if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
+      buf = gst_value_get_buffer (header);
+      res = gst_speex_dec_parse_header (dec, buf);
+      if (res != GST_FLOW_OK)
+        goto done;
+      gst_buffer_replace (&dec->streamheader, buf);
+    }
+
+    vorbiscomment = gst_value_array_get_value (streamheader, 1);
+    if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
+      buf = gst_value_get_buffer (vorbiscomment);
+      res = gst_speex_dec_parse_comments (dec, buf);
+      if (res != GST_FLOW_OK)
+        goto done;
+      gst_buffer_replace (&dec->vorbiscomment, buf);
+    }
+  }
+
+done:
+  return ret;
+}
+
 static GstFlowReturn
-speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
-    GstClockTime timestamp, GstClockTime duration)
+gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
 {
   GstFlowReturn res = GST_FLOW_OK;
   gint i, fpp;
@@ -700,13 +369,7 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
   if (!dec->frame_duration)
     goto not_negotiated;
 
-  if (timestamp != -1) {
-    dec->segment.last_stop = timestamp;
-  } else {
-    timestamp = dec->segment.last_stop;
-  }
-
-  if (buf) {
+  if (G_LIKELY (GST_BUFFER_SIZE (buf))) {
     data = GST_BUFFER_DATA (buf);
     size = GST_BUFFER_SIZE (buf);
 
@@ -716,16 +379,16 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
     fpp = dec->header->frames_per_packet;
     bits = &dec->bits;
 
-    GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits", size,
-        fpp, speex_bits_remaining (bits));
+    GST_DEBUG_OBJECT (dec, "received buffer of size %u, fpp %d, %d bits",
+        size, fpp, speex_bits_remaining (bits));
   } else {
+    /* FIXME ? actually consider how much concealment is needed */
     /* concealment data, pass NULL as the bits parameters */
     GST_DEBUG_OBJECT (dec, "creating concealment data");
     fpp = dec->header->frames_per_packet;
     bits = NULL;
   }
 
-
   /* now decode each frame, catering for unknown number of them (e.g. rtp) */
   for (i = 0; i < fpp; i++) {
     GstBuffer *outbuf;
@@ -735,9 +398,10 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
     GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
         bits ? speex_bits_remaining (bits) : -1);
 
-    res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
+    res =
+        gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
         GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
-        GST_PAD_CAPS (dec->srcpad), &outbuf);
+        GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
 
     if (res != GST_FLOW_OK) {
       GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
@@ -752,39 +416,27 @@ speex_dec_chain_parse_data (GstSpeexDec * dec, GstBuffer * buf,
       if (fpp == 0 && speex_bits_remaining (bits) < 8) {
         /* if we did not know how many frames to expect, then we get this
            at the end if there are leftover bits to pad to the next byte */
+        GST_DEBUG_OBJECT (dec, "Discarding leftover bits");
       } else {
         GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
       }
+      gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
       gst_buffer_unref (outbuf);
-      outbuf = NULL;
-      break;
     } else if (ret == -2) {
       GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
+      gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
       gst_buffer_unref (outbuf);
-      outbuf = NULL;
-      break;
     }
 
     if (bits && speex_bits_remaining (bits) < 0) {
       GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
+      gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
       gst_buffer_unref (outbuf);
-      outbuf = NULL;
-      break;
     }
     if (dec->header->nb_channels == 2)
       speex_decode_stereo_int (out_data, dec->frame_size, dec->stereo);
 
-    GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
-    GST_BUFFER_DURATION (outbuf) = dec->frame_duration;
-
-    dec->segment.last_stop += dec->frame_duration;
-    timestamp = dec->segment.last_stop;
-
-    GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
-        GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
-        GST_TIME_ARGS (dec->frame_duration));
-
-    res = gst_pad_push (dec->srcpad, outbuf);
+    res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
 
     if (res != GST_FLOW_OK) {
       GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
@@ -804,12 +456,16 @@ not_negotiated:
 }
 
 static GstFlowReturn
-speex_dec_chain (GstPad * pad, GstBuffer * buf)
+gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
 {
   GstFlowReturn res;
   GstSpeexDec *dec;
 
-  dec = GST_SPEEX_DEC (gst_pad_get_parent (pad));
+  /* no fancy draining */
+  if (G_UNLIKELY (!buf))
+    return GST_FLOW_OK;
+
+  dec = GST_SPEEX_DEC (bdec);
 
   /* If we have the streamheader and vorbiscomment from the caps already
    * ignore them here */
@@ -817,39 +473,42 @@ speex_dec_chain (GstPad * pad, GstBuffer * buf)
     if (GST_BUFFER_SIZE (dec->streamheader) == GST_BUFFER_SIZE (buf)
         && memcmp (GST_BUFFER_DATA (dec->streamheader), GST_BUFFER_DATA (buf),
             GST_BUFFER_SIZE (buf)) == 0) {
+      GST_DEBUG_OBJECT (dec, "found streamheader");
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
       res = GST_FLOW_OK;
     } else if (GST_BUFFER_SIZE (dec->vorbiscomment) == GST_BUFFER_SIZE (buf)
         && memcmp (GST_BUFFER_DATA (dec->vorbiscomment), GST_BUFFER_DATA (buf),
             GST_BUFFER_SIZE (buf)) == 0) {
+      GST_DEBUG_OBJECT (dec, "found vorbiscomments");
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
       res = GST_FLOW_OK;
     } else {
-      res =
-          speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
-          GST_BUFFER_DURATION (buf));
+      res = gst_speex_dec_parse_data (dec, buf);
     }
   } else {
     /* Otherwise fall back to packet counting and assume that the
      * first two packets are the headers. */
     switch (dec->packetno) {
       case 0:
-        res = speex_dec_chain_parse_header (dec, buf);
+        GST_DEBUG_OBJECT (dec, "counted streamheader");
+        res = gst_speex_dec_parse_header (dec, buf);
+        gst_audio_decoder_finish_frame (bdec, NULL, 1);
         break;
       case 1:
-        res = speex_dec_chain_parse_comments (dec, buf);
+        GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
+        res = gst_speex_dec_parse_comments (dec, buf);
+        gst_audio_decoder_finish_frame (bdec, NULL, 1);
         break;
       default:
-        res =
-            speex_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
-            GST_BUFFER_DURATION (buf));
+      {
+        res = gst_speex_dec_parse_data (dec, buf);
         break;
+      }
     }
   }
 
   dec->packetno++;
 
-  gst_buffer_unref (buf);
-  gst_object_unref (dec);
-
   return res;
 }
 
@@ -888,37 +547,3 @@ gst_speex_dec_set_property (GObject * object, guint prop_id,
       break;
   }
 }
-
-
-static GstStateChangeReturn
-speex_dec_change_state (GstElement * element, GstStateChange transition)
-{
-  GstStateChangeReturn ret;
-  GstSpeexDec *dec = GST_SPEEX_DEC (element);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_NULL_TO_READY:
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-    default:
-      break;
-  }
-
-  ret = parent_class->change_state (element, transition);
-  if (ret != GST_STATE_CHANGE_SUCCESS)
-    return ret;
-
-  switch (transition) {
-    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_READY:
-      gst_speex_dec_reset (dec);
-      break;
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      break;
-    default:
-      break;
-  }
-
-  return ret;
-}
index 660d805..8187af8 100644 (file)
@@ -22,6 +22,8 @@
 #define __GST_SPEEX_DEC_H__
 
 #include <gst/gst.h>
+#include <gst/audio/gstaudiodecoder.h>
+
 #include <speex/speex.h>
 #include <speex/speex_callbacks.h>
 #include <speex/speex_header.h>
@@ -44,11 +46,7 @@ typedef struct _GstSpeexDec GstSpeexDec;
 typedef struct _GstSpeexDecClass GstSpeexDecClass;
 
 struct _GstSpeexDec {
-  GstElement            element;
-
-  /* pads */
-  GstPad                *sinkpad;
-  GstPad                *srcpad;
+  GstAudioDecoder   element;
 
   void                  *state;
   SpeexStereoState      *stereo;
@@ -67,14 +65,12 @@ struct _GstSpeexDec {
   GstClockTime          frame_duration;
   guint64               packetno;
 
-  GstSegment            segment;    /* STREAM LOCK */
-
   GstBuffer             *streamheader;
   GstBuffer             *vorbiscomment;
 };
 
 struct _GstSpeexDecClass {
-  GstElementClass parent_class;
+  GstAudioDecoderClass parent_class;
 };
 
 GType gst_speex_dec_get_type (void);