sirenenc: fix timestamping

Handle DISCONT and reset adapter.
code cleanups.
Put timestamps and discont flags on output buffers.
Fix error handling.
Remove bogus object locks, it's all protected by the STREAM_LOCK.
This commit is contained in:
Wim Taymans 2009-09-03 18:43:26 +02:00
parent 9a1597e58b
commit 823f06f039
2 changed files with 95 additions and 41 deletions

View file

@ -40,6 +40,8 @@
GST_DEBUG_CATEGORY (sirenenc_debug); GST_DEBUG_CATEGORY (sirenenc_debug);
#define GST_CAT_DEFAULT (sirenenc_debug) #define GST_CAT_DEFAULT (sirenenc_debug)
#define FRAME_DURATION (20 * GST_MSECOND)
/* elementfactory information */ /* elementfactory information */
static const GstElementDetails gst_siren_enc_details = static const GstElementDetails gst_siren_enc_details =
GST_ELEMENT_DETAILS ("Siren Encoder element", GST_ELEMENT_DETAILS ("Siren Encoder element",
@ -176,14 +178,22 @@ gst_siren_enc_chain (GstPad * pad, GstBuffer * buf)
{ {
GstSirenEnc *enc = GST_SIREN_ENC (gst_pad_get_parent_element (pad)); GstSirenEnc *enc = GST_SIREN_ENC (gst_pad_get_parent_element (pad));
GstFlowReturn ret = GST_FLOW_OK; GstFlowReturn ret = GST_FLOW_OK;
GstBuffer *encoded = NULL; GstBuffer *out_buf;
guint8 *data = NULL; guint8 *in_data, *out_data;
gint out_offset = 0; guint8 *to_free = NULL;
gint encode_ret = 0; guint i, size, num_frames;
gint size = 0; gint out_size, in_size;
guint in_offset = 0; gint encode_ret;
gboolean discont;
GstClockTime timestamp;
guint64 distance;
GST_OBJECT_LOCK (enc); discont = GST_BUFFER_IS_DISCONT (buf);
if (discont) {
GST_DEBUG_OBJECT (enc, "received DISCONT, flush adapter");
gst_adapter_clear (enc->adapter);
enc->discont = TRUE;
}
gst_adapter_push (enc->adapter, buf); gst_adapter_push (enc->adapter, buf);
@ -191,50 +201,90 @@ gst_siren_enc_chain (GstPad * pad, GstBuffer * buf)
GST_BUFFER_SIZE (buf), gst_adapter_available (enc->adapter)); GST_BUFFER_SIZE (buf), gst_adapter_available (enc->adapter));
size = gst_adapter_available (enc->adapter); size = gst_adapter_available (enc->adapter);
size /= 16;
size -= size % 40;
if (size == 0) { /* we need to process 640 input bytes to produce 40 output bytes */
GST_OBJECT_UNLOCK (enc); /* calculate the amount of frames we will handle */
goto out; num_frames = size / 640;
}
data = gst_adapter_take (enc->adapter, size * 16); /* no frames, wait some more */
if (num_frames == 0)
goto done;
GST_OBJECT_UNLOCK (enc); /* this is the output size */
in_size = num_frames * 640;
out_size = num_frames * 40;
ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad, GST_LOG_OBJECT (enc, "we have %u frames, %u in, %u out", num_frames, in_size,
GST_BUFFER_OFFSET (buf) / 16, size, enc->srccaps, &encoded); out_size);
/* get a buffer */
ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad, -1,
out_size, enc->srccaps, &out_buf);
if (ret != GST_FLOW_OK) if (ret != GST_FLOW_OK)
goto out; goto alloc_failed;
while (out_offset < size && ret == GST_FLOW_OK) { /* get the timestamp for the output buffer */
GST_LOG_OBJECT (enc, "Encoding frame"); timestamp = gst_adapter_prev_timestamp (enc->adapter, &distance);
encode_ret = Siren7_EncodeFrame (enc->encoder, /* add the amount of time taken by the distance */
data + in_offset, GST_BUFFER_DATA (encoded) + out_offset); timestamp += gst_util_uint64_scale_int (distance / 2, GST_SECOND, 16000);
if (encode_ret != 0) {
GST_ERROR_OBJECT (enc, "Siren7_EncodeFrame returned %d", encode_ret);
ret = GST_FLOW_ERROR;
gst_buffer_unref (encoded);
goto out;
}
out_offset += 40; GST_LOG_OBJECT (enc,
in_offset += 640; "timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
GST_TIME_ARGS (timestamp), distance);
/* get the input data for all the frames */
to_free = in_data = gst_adapter_take (enc->adapter, in_size);
out_data = GST_BUFFER_DATA (out_buf);
for (i = 0; i < num_frames; i++) {
GST_LOG_OBJECT (enc, "Encoding frame %u/%u", i, num_frames);
/* encode 640 input bytes to 40 output bytes */
encode_ret = Siren7_EncodeFrame (enc->encoder, in_data, out_data);
if (encode_ret != 0)
goto encode_error;
/* move to next frame */
out_data += 40;
in_data += 640;
} }
GST_LOG_OBJECT (enc, "Finished encoding : %d", out_offset); GST_LOG_OBJECT (enc, "Finished encoding");
ret = gst_pad_push (enc->srcpad, encoded); /* mark discont */
if (enc->discont) {
GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
enc->discont = FALSE;
}
GST_BUFFER_TIMESTAMP (out_buf) = timestamp;
GST_BUFFER_DURATION (out_buf) = num_frames * FRAME_DURATION;
out: ret = gst_pad_push (enc->srcpad, out_buf);
if (data)
g_free (data); done:
if (to_free)
g_free (to_free);
gst_object_unref (enc); gst_object_unref (enc);
return ret; return ret;
/* ERRORS */
alloc_failed:
{
GST_DEBUG_OBJECT (enc, "failed to pad_alloc buffer: %d (%d)", ret,
gst_flow_get_name (ret));
goto done;
}
encode_error:
{
GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
("Error encoding frame: %d", encode_ret));
ret = GST_FLOW_ERROR;
gst_buffer_unref (out_buf);
goto done;
}
} }
static GstStateChangeReturn static GstStateChangeReturn
@ -243,16 +293,19 @@ gst_siren_change_state (GstElement * element, GstStateChange transition)
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GstSirenEnc *enc = GST_SIREN_ENC (element); GstSirenEnc *enc = GST_SIREN_ENC (element);
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
enc->discont = FALSE;
break;
default:
break;
}
if (ret == GST_STATE_CHANGE_FAILURE) ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
return ret;
switch (transition) { switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY: case GST_STATE_CHANGE_PAUSED_TO_READY:
GST_OBJECT_LOCK (element);
gst_adapter_clear (enc->adapter); gst_adapter_clear (enc->adapter);
GST_OBJECT_UNLOCK (element);
break; break;
default: default:
break; break;

View file

@ -52,9 +52,10 @@ struct _GstSirenEnc
/* protected by the stream lock */ /* protected by the stream lock */
SirenEncoder encoder; SirenEncoder encoder;
/* protected by the object lock */
GstAdapter *adapter; GstAdapter *adapter;
gboolean discont;
GstPad *srcpad; GstPad *srcpad;
GstPad *sinkpad; GstPad *sinkpad;