#include <gst/gsttagsetter.h>
#include <gst/tag/tag.h>
+#include <gst/audio/audio.h>
#include "gstspeexenc.h"
GST_DEBUG_CATEGORY_STATIC (speexenc_debug);
static GstStateChangeReturn gst_speex_enc_change_state (GstElement * element,
GstStateChange transition);
+static GstFlowReturn gst_speex_enc_encode (GstSpeexEnc * enc, gboolean flush);
+
static void
gst_speex_enc_setup_interfaces (GType speexenc_type)
{
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
- enc->eos = TRUE;
+ gst_speex_enc_encode (enc, TRUE);
res = gst_pad_event_default (pad, event);
break;
case GST_EVENT_TAG:
return res;
}
+static GstFlowReturn
+gst_speex_enc_encode (GstSpeexEnc * enc, gboolean flush)
+{
+ gint frame_size = enc->frame_size;
+ gint bytes = frame_size * 2 * enc->channels;
+ GstFlowReturn ret = GST_FLOW_OK;
+
+ if (flush && gst_adapter_available (enc->adapter) % bytes != 0) {
+ guint diff = gst_adapter_available (enc->adapter) % bytes;
+ GstBuffer *buf = gst_buffer_new_and_alloc (diff);
+
+ memset (GST_BUFFER_DATA (buf), 0, diff);
+ gst_adapter_push (enc->adapter, buf);
+ }
+
+ while (gst_adapter_available (enc->adapter) >= bytes) {
+ gint16 *data;
+ gint i;
+ gint outsize, written;
+ GstBuffer *outbuf;
+
+ data = (gint16 *) gst_adapter_peek (enc->adapter, bytes);
+
+ for (i = 0; i < frame_size * enc->channels; i++) {
+ enc->input[i] = (gfloat) data[i];
+ }
+ gst_adapter_flush (enc->adapter, bytes);
+
+ enc->samples_in += frame_size;
+
+ GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", frame_size, bytes);
+
+ if (enc->channels == 2) {
+ speex_encode_stereo (enc->input, frame_size, &enc->bits);
+ }
+ speex_encode (enc->state, enc->input, &enc->bits);
+
+ enc->frameno++;
+ enc->frameno_out++;
+
+ if ((enc->frameno % enc->nframes) != 0)
+ continue;
+
+ speex_bits_insert_terminator (&enc->bits);
+ outsize = speex_bits_nbytes (&enc->bits);
+
+ ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad,
+ GST_BUFFER_OFFSET_NONE, outsize, GST_PAD_CAPS (enc->srcpad), &outbuf);
+
+ if ((GST_FLOW_OK != ret))
+ goto done;
+
+ written = speex_bits_write (&enc->bits,
+ (gchar *) GST_BUFFER_DATA (outbuf), outsize);
+ g_assert (written == outsize);
+ speex_bits_reset (&enc->bits);
+
+ GST_BUFFER_TIMESTAMP (outbuf) = enc->start_ts +
+ gst_util_uint64_scale_int (enc->frameno_out * frame_size -
+ enc->lookahead, GST_SECOND, enc->rate);
+ GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (frame_size,
+ GST_SECOND, enc->rate);
+ /* set gp time and granulepos; see gst-plugins-base/ext/ogg/README */
+ GST_BUFFER_OFFSET_END (outbuf) = enc->granulepos_offset +
+ ((enc->frameno_out + 1) * frame_size - enc->lookahead);
+ GST_BUFFER_OFFSET (outbuf) =
+ gst_util_uint64_scale_int (GST_BUFFER_OFFSET_END (outbuf), GST_SECOND,
+ enc->rate);
+
+ ret = gst_speex_enc_push_buffer (enc, outbuf);
+
+ if ((GST_FLOW_OK != ret) && (GST_FLOW_NOT_LINKED != ret))
+ goto done;
+ }
+
+done:
+
+ return ret;
+}
static GstFlowReturn
gst_speex_enc_chain (GstPad * pad, GstBuffer * buf)
enc->header_sent = TRUE;
}
- {
- gint frame_size = enc->frame_size;
- gint bytes = frame_size * 2 * enc->channels;
-
- GST_DEBUG_OBJECT (enc, "received buffer of %u bytes",
- GST_BUFFER_SIZE (buf));
-
- /* push buffer to adapter */
- gst_adapter_push (enc->adapter, buf);
- buf = NULL;
-
- while (gst_adapter_available (enc->adapter) >= bytes) {
- gint16 *data;
- gint i;
- gint outsize, written;
- GstBuffer *outbuf;
-
- data = (gint16 *) gst_adapter_peek (enc->adapter, bytes);
-
- for (i = 0; i < frame_size * enc->channels; i++) {
- enc->input[i] = (gfloat) data[i];
- }
- gst_adapter_flush (enc->adapter, bytes);
-
- enc->samples_in += frame_size;
-
- GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", frame_size,
- bytes);
+ /* Save the timestamp of the first buffer. This will be later
+ * used as offset for all following buffers */
+ if (enc->start_ts == GST_CLOCK_TIME_NONE) {
+ if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
+ enc->start_ts = GST_BUFFER_TIMESTAMP (buf);
+ enc->granulepos_offset = gst_util_uint64_scale
+ (GST_BUFFER_TIMESTAMP (buf), enc->rate, GST_SECOND);
+ } else {
+ enc->start_ts = 0;
+ enc->granulepos_offset = 0;
+ }
+ }
- if (enc->channels == 2) {
- speex_encode_stereo (enc->input, frame_size, &enc->bits);
- }
- speex_encode (enc->state, enc->input, &enc->bits);
+ /* Check if we have a continous stream, if not drop some samples or the buffer or
+ * insert some silence samples */
+ if (enc->next_ts != GST_CLOCK_TIME_NONE &&
+ GST_BUFFER_TIMESTAMP (buf) < enc->next_ts) {
+ guint64 diff = enc->next_ts - GST_BUFFER_TIMESTAMP (buf);
+ guint64 diff_bytes;
+
+ GST_WARNING_OBJECT (enc, "Buffer is older than previous "
+ "timestamp + duration (%" GST_TIME_FORMAT "< %" GST_TIME_FORMAT
+ "), cannot handle. Clipping buffer.",
+ GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
+ GST_TIME_ARGS (enc->next_ts));
+
+ diff_bytes = GST_CLOCK_TIME_TO_FRAMES (diff, enc->rate) * enc->channels * 2;
+ if (diff_bytes >= GST_BUFFER_SIZE (buf)) {
+ gst_buffer_unref (buf);
+ return GST_FLOW_OK;
+ }
+ buf = gst_buffer_make_metadata_writable (buf);
+ GST_BUFFER_DATA (buf) += diff_bytes;
+ GST_BUFFER_SIZE (buf) -= diff_bytes;
- enc->frameno++;
+ GST_BUFFER_TIMESTAMP (buf) += diff;
+ if (GST_BUFFER_DURATION_IS_VALID (buf))
+ GST_BUFFER_DURATION (buf) -= diff;
+ }
- if ((enc->frameno % enc->nframes) != 0)
- continue;
+ if (enc->next_ts != GST_CLOCK_TIME_NONE
+ && GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
+ guint64 max_diff =
+ gst_util_uint64_scale (enc->frame_size, GST_SECOND, enc->rate);
- speex_bits_insert_terminator (&enc->bits);
- outsize = speex_bits_nbytes (&enc->bits);
+ if (GST_BUFFER_TIMESTAMP (buf) != enc->next_ts &&
+ GST_BUFFER_TIMESTAMP (buf) - enc->next_ts > max_diff) {
+ GST_WARNING_OBJECT (enc,
+ "Discontinuity detected: %" G_GUINT64_FORMAT " > %" G_GUINT64_FORMAT,
+ GST_BUFFER_TIMESTAMP (buf) - enc->next_ts, max_diff);
- ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad,
- GST_BUFFER_OFFSET_NONE, outsize, GST_PAD_CAPS (enc->srcpad), &outbuf);
+ gst_speex_enc_encode (enc, TRUE);
- if ((GST_FLOW_OK != ret))
- goto done;
+ enc->frameno_out = 0;
+ enc->start_ts = GST_BUFFER_TIMESTAMP (buf);
+ enc->granulepos_offset = gst_util_uint64_scale
+ (GST_BUFFER_TIMESTAMP (buf), enc->rate, GST_SECOND);
+ }
+ }
- written = speex_bits_write (&enc->bits,
- (gchar *) GST_BUFFER_DATA (outbuf), outsize);
- g_assert (written == outsize);
- speex_bits_reset (&enc->bits);
+ if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)
+ && GST_BUFFER_DURATION_IS_VALID (buf))
+ enc->next_ts = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
+ else
+ enc->next_ts = GST_CLOCK_TIME_NONE;
- GST_BUFFER_TIMESTAMP (outbuf) =
- gst_util_uint64_scale_int (enc->frameno * frame_size -
- enc->lookahead, GST_SECOND, enc->rate);
- GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (frame_size,
- GST_SECOND, enc->rate);
- /* set gp time and granulepos; see gst-plugins-base/ext/ogg/README */
- GST_BUFFER_OFFSET_END (outbuf) =
- ((enc->frameno + 1) * frame_size - enc->lookahead);
- GST_BUFFER_OFFSET (outbuf) =
- gst_util_uint64_scale_int (GST_BUFFER_OFFSET_END (outbuf), GST_SECOND,
- enc->rate);
+ GST_DEBUG_OBJECT (enc, "received buffer of %u bytes", GST_BUFFER_SIZE (buf));
- ret = gst_speex_enc_push_buffer (enc, outbuf);
+ /* push buffer to adapter */
+ gst_adapter_push (enc->adapter, buf);
+ buf = NULL;
- if ((GST_FLOW_OK != ret) && (GST_FLOW_NOT_LINKED != ret))
- goto done;
- }
- }
+ ret = gst_speex_enc_encode (enc, FALSE);
done:
case GST_STATE_CHANGE_READY_TO_PAUSED:
speex_bits_init (&enc->bits);
enc->frameno = 0;
+ enc->frameno_out = 0;
enc->samples_in = 0;
+ enc->start_ts = GST_CLOCK_TIME_NONE;
+ enc->next_ts = GST_CLOCK_TIME_NONE;
+ enc->granulepos_offset = 0;
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
/* fall through */