Added _seek and _tell // cleanup

Original commit message from CVS:
Added _seek and _tell
// cleanup
This commit is contained in:
Wim Taymans 2002-02-02 13:23:07 +00:00
parent b37f73757a
commit a7c0c874b5
2 changed files with 92 additions and 70 deletions

View file

@ -26,7 +26,7 @@
#include <gst/gstinfo.h> #include <gst/gstinfo.h>
#include <gst/bytestream/bytestream.h> #include <gst/bytestream/bytestream.h>
//#define BS_DEBUG /*#define BS_DEBUG */
#ifdef BS_DEBUG #ifdef BS_DEBUG
# define bs_print(format,args...) GST_DEBUG (GST_CAT_BUFFER, format, ## args) # define bs_print(format,args...) GST_DEBUG (GST_CAT_BUFFER, format, ## args)
@ -80,39 +80,41 @@ gst_bytestream_destroy (GstByteStream * bs)
g_free (bs); g_free (bs);
} }
// HOW THIS WORKS: /* HOW THIS WORKS:
// *
// The fundamental structure is a singly-linked list of buffers. The * The fundamental structure is a singly-linked list of buffers. The
// buffer on the front is the oldest, and thus the first to read data * buffer on the front is the oldest, and thus the first to read data
// from. The number of bytes left to be read in this buffer is stored * from. The number of bytes left to be read in this buffer is stored
// in bs->headbufavail. The number of bytes available in the entire * in bs->headbufavail. The number of bytes available in the entire
// list (including the head buffer) is in bs->listavail. * list (including the head buffer) is in bs->listavail.
// *
// When a request is made for data (peek), _fill_bytes is called with * When a request is made for data (peek), _fill_bytes is called with
// the number of bytes needed, but only if the listavail indicates * the number of bytes needed, but only if the listavail indicates
// that there aren't already enough. This calls _get_next_buf until * that there aren't already enough. This calls _get_next_buf until
// the listavail is sufficient to satisfy the demand. * the listavail is sufficient to satisfy the demand.
// *
// _get_next_buf pulls a buffer from the pad the bytestream is attached * _get_next_buf pulls a buffer from the pad the bytestream is attached
// to, and shoves it in the list. There are actually two things it can * to, and shoves it in the list. There are actually two things it can
// do. If there's already a buffer in the list, and the _is_span_fast() * do. If there's already a buffer in the list, and the _is_span_fast()
// test returns true, it will merge it with that last buffer. Otherwise * test returns true, it will merge it with that last buffer. Otherwise
// it will simply tack it onto the end of the list. * it will simply tack it onto the end of the list.
// *
// The _peek itself first checks the simple case of the request fitting * The _peek itself first checks the simple case of the request fitting
// within the head buffer, and if so creates a subbuffer and returns. * within the head buffer, and if so creates a subbuffer and returns.
// Otherwise, it creates a new buffer and allocates space for the request * Otherwise, it creates a new buffer and allocates space for the request
// and calls _assemble to fill it. We know we have to copy because this * and calls _assemble to fill it. We know we have to copy because this
// case only happens when the _merge wasn't feasible during _get_next_buf. * case only happens when the _merge wasn't feasible during _get_next_buf.
// *
// The _flush method repeatedly inspects the head buffer and flushes as * The _flush method repeatedly inspects the head buffer and flushes as
// much data from it as it needs to, up to the size of the buffer. If * much data from it as it needs to, up to the size of the buffer. If
// the flush decimates the buffer, it's stripped, unref'd, and removed. * the flush decimates the buffer, it's stripped, unref'd, and removed.
*/
// get the next buffer /* get the next buffer
// if the buffer can be merged with the head buffer, do so * if the buffer can be merged with the head buffer, do so
// else add it onto the head of the * else add it onto the head of the
*/
static gboolean static gboolean
gst_bytestream_get_next_buf (GstByteStream * bs) gst_bytestream_get_next_buf (GstByteStream * bs)
{ {
@ -135,23 +137,23 @@ gst_bytestream_get_next_buf (GstByteStream * bs)
bs_print ("get_next_buf: got buffer of %d bytes\n", GST_BUFFER_SIZE (nextbuf)); bs_print ("get_next_buf: got buffer of %d bytes\n", GST_BUFFER_SIZE (nextbuf));
// first see if there are any buffers in the list at all /* first see if there are any buffers in the list at all */
if (bs->buflist) { if (bs->buflist) {
bs_print ("gst_next_buf: there is at least one buffer in the list\n"); bs_print ("gst_next_buf: there is at least one buffer in the list\n");
// now find the end of the list /* now find the end of the list */
end = g_slist_last (bs->buflist); end = g_slist_last (bs->buflist);
// get the buffer that's there /* get the buffer that's there */
lastbuf = GST_BUFFER (end->data); lastbuf = GST_BUFFER (end->data);
// see if we can marge cheaply /* see if we can marge cheaply */
if (gst_buffer_is_span_fast (lastbuf, nextbuf)) { if (gst_buffer_is_span_fast (lastbuf, nextbuf)) {
bs_print ("get_next_buf: merging new buffer with last buf on list\n"); bs_print ("get_next_buf: merging new buffer with last buf on list\n");
// it is, let's merge them (this is really an append, but...) /* it is, let's merge them (this is really an append, but...) */
end->data = gst_buffer_merge (lastbuf, nextbuf); end->data = gst_buffer_merge (lastbuf, nextbuf);
// add to the length of the list /* add to the length of the list */
bs->listavail += GST_BUFFER_SIZE (nextbuf); bs->listavail += GST_BUFFER_SIZE (nextbuf);
// have to check to see if we merged with the head buffer /* have to check to see if we merged with the head buffer */
if (end == bs->buflist) { if (end == bs->buflist) {
bs->headbufavail += GST_BUFFER_SIZE (nextbuf); bs->headbufavail += GST_BUFFER_SIZE (nextbuf);
} }
@ -159,24 +161,24 @@ gst_bytestream_get_next_buf (GstByteStream * bs)
gst_buffer_unref (lastbuf); gst_buffer_unref (lastbuf);
gst_buffer_unref (nextbuf); gst_buffer_unref (nextbuf);
// if we can't, we just append this buffer /* if we can't, we just append this buffer */
} }
else { else {
bs_print ("get_next_buf: adding new buffer to the end of the list\n"); bs_print ("get_next_buf: adding new buffer to the end of the list\n");
end = g_slist_append (end, nextbuf); end = g_slist_append (end, nextbuf);
// also need to increment length of list and buffer count /* also need to increment length of list and buffer count */
bs->listavail += GST_BUFFER_SIZE (nextbuf); bs->listavail += GST_BUFFER_SIZE (nextbuf);
} }
// if there are no buffers in the list /* if there are no buffers in the list */
} }
else { else {
bs_print ("get_next_buf: buflist is empty, adding new buffer to list\n"); bs_print ("get_next_buf: buflist is empty, adding new buffer to list\n");
// put this on the end of the list /* put this on the end of the list */
bs->buflist = g_slist_append (bs->buflist, nextbuf); bs->buflist = g_slist_append (bs->buflist, nextbuf);
// and increment the number of bytes in the list /* and increment the number of bytes in the list */
bs->listavail = GST_BUFFER_SIZE (nextbuf); bs->listavail = GST_BUFFER_SIZE (nextbuf);
// set the head buffer avail to the size /* set the head buffer avail to the size */
bs->headbufavail = GST_BUFFER_SIZE (nextbuf); bs->headbufavail = GST_BUFFER_SIZE (nextbuf);
} }
@ -186,7 +188,7 @@ gst_bytestream_get_next_buf (GstByteStream * bs)
static gboolean static gboolean
gst_bytestream_fill_bytes (GstByteStream * bs, guint32 len) gst_bytestream_fill_bytes (GstByteStream * bs, guint32 len)
{ {
// as long as we don't have enough, we get more buffers /* as long as we don't have enough, we get more buffers */
while (bs->listavail < len) { while (bs->listavail < len) {
bs_print ("fill_bytes: there are %d bytes in the list, we need %d\n", bs->listavail, len); bs_print ("fill_bytes: there are %d bytes in the list, we need %d\n", bs->listavail, len);
if (!gst_bytestream_get_next_buf (bs)) if (!gst_bytestream_get_next_buf (bs))
@ -207,7 +209,7 @@ gst_bytestream_peek (GstByteStream * bs, guint32 len)
bs_print ("peek: asking for %d bytes\n", len); bs_print ("peek: asking for %d bytes\n", len);
// make sure we have enough /* make sure we have enough */
bs_print ("peek: there are %d bytes in the list\n", bs->listavail); bs_print ("peek: there are %d bytes in the list\n", bs->listavail);
if (len > bs->listavail) { if (len > bs->listavail) {
if (!gst_bytestream_fill_bytes (bs, len)) if (!gst_bytestream_fill_bytes (bs, len))
@ -216,17 +218,17 @@ gst_bytestream_peek (GstByteStream * bs, guint32 len)
} }
bs_status (bs); bs_status (bs);
// extract the head buffer /* extract the head buffer */
headbuf = GST_BUFFER (bs->buflist->data); headbuf = GST_BUFFER (bs->buflist->data);
// if the requested bytes are in the current buffer /* if the requested bytes are in the current buffer */
bs_print ("peek: headbufavail is %d\n", bs->headbufavail); bs_print ("peek: headbufavail is %d\n", bs->headbufavail);
if (len <= bs->headbufavail) { if (len <= bs->headbufavail) {
bs_print ("peek: there are enough bytes in headbuf (need %d, have %d)\n", len, bs->headbufavail); bs_print ("peek: there are enough bytes in headbuf (need %d, have %d)\n", len, bs->headbufavail);
// create a sub-buffer of the headbuf /* create a sub-buffer of the headbuf */
retbuf = gst_buffer_create_sub (headbuf, GST_BUFFER_SIZE (headbuf) - bs->headbufavail, len); retbuf = gst_buffer_create_sub (headbuf, GST_BUFFER_SIZE (headbuf) - bs->headbufavail, len);
// otherwise we need to figure out how to assemble one /* otherwise we need to figure out how to assemble one */
} }
else { else {
bs_print ("peek: current buffer is not big enough for len %d\n", len); bs_print ("peek: current buffer is not big enough for len %d\n", len);
@ -256,7 +258,7 @@ gst_bytestream_peek_bytes (GstByteStream * bs, guint32 len)
bs->assembled = NULL; bs->assembled = NULL;
} }
// make sure we have enough /* make sure we have enough */
bs_print ("peek_bytes: there are %d bytes in the list\n", bs->listavail); bs_print ("peek_bytes: there are %d bytes in the list\n", bs->listavail);
if (len > bs->listavail) { if (len > bs->listavail) {
if (!gst_bytestream_fill_bytes (bs, len)) if (!gst_bytestream_fill_bytes (bs, len))
@ -265,17 +267,17 @@ gst_bytestream_peek_bytes (GstByteStream * bs, guint32 len)
} }
bs_status (bs); bs_status (bs);
// extract the head buffer /* extract the head buffer */
headbuf = GST_BUFFER (bs->buflist->data); headbuf = GST_BUFFER (bs->buflist->data);
// if the requested bytes are in the current buffer /* if the requested bytes are in the current buffer */
bs_print ("peek_bytes: headbufavail is %d\n", bs->headbufavail); bs_print ("peek_bytes: headbufavail is %d\n", bs->headbufavail);
if (len <= bs->headbufavail) { if (len <= bs->headbufavail) {
bs_print ("peek_bytes: there are enough bytes in headbuf (need %d, have %d)\n", len, bs->headbufavail); bs_print ("peek_bytes: there are enough bytes in headbuf (need %d, have %d)\n", len, bs->headbufavail);
// create a sub-buffer of the headbuf /* create a sub-buffer of the headbuf */
data = GST_BUFFER_DATA (headbuf) + (GST_BUFFER_SIZE (headbuf) - bs->headbufavail); data = GST_BUFFER_DATA (headbuf) + (GST_BUFFER_SIZE (headbuf) - bs->headbufavail);
// otherwise we need to figure out how to assemble one /* otherwise we need to figure out how to assemble one */
} }
else { else {
bs_print ("peek_bytes: current buffer is not big enough for len %d\n", len); bs_print ("peek_bytes: current buffer is not big enough for len %d\n", len);
@ -296,14 +298,14 @@ gst_bytestream_assemble (GstByteStream * bs, guint32 len)
guint32 copied = 0; guint32 copied = 0;
GstBuffer *buf; GstBuffer *buf;
// copy the data from the curbuf /* copy the data from the curbuf */
buf = GST_BUFFER (bs->buflist->data); buf = GST_BUFFER (bs->buflist->data);
bs_print ("assemble: copying %d bytes from curbuf at %d to *data\n", bs->headbufavail, bs_print ("assemble: copying %d bytes from curbuf at %d to *data\n", bs->headbufavail,
GST_BUFFER_SIZE (buf) - bs->headbufavail); GST_BUFFER_SIZE (buf) - bs->headbufavail);
memcpy (data, GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf) - bs->headbufavail, bs->headbufavail); memcpy (data, GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf) - bs->headbufavail, bs->headbufavail);
copied += bs->headbufavail; copied += bs->headbufavail;
// asumption is made that the buffers all exist in the list /* asumption is made that the buffers all exist in the list */
walk = g_slist_next (bs->buflist); walk = g_slist_next (bs->buflist);
while (copied < len) { while (copied < len) {
buf = GST_BUFFER (walk->data); buf = GST_BUFFER (walk->data);
@ -328,7 +330,7 @@ gst_bytestream_flush (GstByteStream * bs, guint32 len)
{ {
bs_print ("flush: flushing %d bytes\n", len); bs_print ("flush: flushing %d bytes\n", len);
// make sure we have enough /* make sure we have enough */
bs_print ("flush: there are %d bytes in the list\n", bs->listavail); bs_print ("flush: there are %d bytes in the list\n", bs->listavail);
if (len > bs->listavail) { if (len > bs->listavail) {
if (!gst_bytestream_fill_bytes (bs, len)) if (!gst_bytestream_fill_bytes (bs, len))
@ -353,35 +355,35 @@ gst_bytestream_flush_fast (GstByteStream * bs, guint32 len)
bs->assembled = NULL; bs->assembled = NULL;
} }
// repeat until we've flushed enough data /* repeat until we've flushed enough data */
while (len > 0) { while (len > 0) {
headbuf = GST_BUFFER (bs->buflist->data); headbuf = GST_BUFFER (bs->buflist->data);
bs_print ("flush: analyzing buffer that's %d bytes long, offset %d\n", GST_BUFFER_SIZE (headbuf), bs_print ("flush: analyzing buffer that's %d bytes long, offset %d\n", GST_BUFFER_SIZE (headbuf),
GST_BUFFER_OFFSET (headbuf)); GST_BUFFER_OFFSET (headbuf));
// if there's enough to complete the flush /* if there's enough to complete the flush */
if (bs->headbufavail > len) { if (bs->headbufavail > len) {
// just trim it off /* just trim it off */
bs_print ("flush: trimming %d bytes off end of headbuf\n", len); bs_print ("flush: trimming %d bytes off end of headbuf\n", len);
bs->headbufavail -= len; bs->headbufavail -= len;
bs->listavail -= len; bs->listavail -= len;
len = 0; len = 0;
// otherwise we have to trim the whole buffer /* otherwise we have to trim the whole buffer */
} }
else { else {
bs_print ("flush: removing head buffer completely\n"); bs_print ("flush: removing head buffer completely\n");
// remove it from the list /* remove it from the list */
bs->buflist = g_slist_delete_link (bs->buflist, bs->buflist); bs->buflist = g_slist_delete_link (bs->buflist, bs->buflist);
// trim it from the avail size /* trim it from the avail size */
bs->listavail -= bs->headbufavail; bs->listavail -= bs->headbufavail;
// record that we've trimmed this many bytes /* record that we've trimmed this many bytes */
len -= bs->headbufavail; len -= bs->headbufavail;
// unref it /* unref it */
gst_buffer_unref (headbuf); gst_buffer_unref (headbuf);
// record the new headbufavail /* record the new headbufavail */
if (bs->buflist) { if (bs->buflist) {
bs->headbufavail = GST_BUFFER_SIZE (GST_BUFFER (bs->buflist->data)); bs->headbufavail = GST_BUFFER_SIZE (GST_BUFFER (bs->buflist->data));
bs_print ("flush: next headbuf is %d bytes\n", bs->headbufavail); bs_print ("flush: next headbuf is %d bytes\n", bs->headbufavail);
@ -395,6 +397,24 @@ gst_bytestream_flush_fast (GstByteStream * bs, guint32 len)
} }
} }
gboolean
gst_bytestream_seek (GstByteStream *bs, GstSeekType type, gint64 offset)
{
GstRealPad *peer = GST_RPAD_PEER (bs->pad);
if (gst_pad_send_event (GST_PAD (peer), gst_event_new_seek (type, offset, TRUE))) {
gst_bytestream_flush_fast (bs, bs->listavail);
return TRUE;
}
return FALSE;
}
guint64
gst_bytestream_tell (GstByteStream *bs)
{
return 0;
}
GstBuffer * GstBuffer *
gst_bytestream_read (GstByteStream * bs, guint32 len) gst_bytestream_read (GstByteStream * bs, guint32 len)
{ {

View file

@ -37,7 +37,7 @@ struct _GstByteStream {
guint32 headbufavail; guint32 headbufavail;
guint32 listavail; guint32 listavail;
// we keep state of assembled pieces /* we keep state of assembled pieces */
guint8 *assembled; guint8 *assembled;
guint32 assembled_len; guint32 assembled_len;
}; };
@ -46,10 +46,12 @@ GstByteStream* gst_bytestream_new (GstPad *pad);
void gst_bytestream_destroy (GstByteStream *bs); void gst_bytestream_destroy (GstByteStream *bs);
GstBuffer* gst_bytestream_read (GstByteStream *bs, guint32 len); GstBuffer* gst_bytestream_read (GstByteStream *bs, guint32 len);
guint64 gst_bytestream_tell (GstByteStream *bs);
gboolean gst_bytestream_seek (GstByteStream *bs, GstSeekType type, gint64 offset);
GstBuffer* gst_bytestream_peek (GstByteStream *bs, guint32 len); GstBuffer* gst_bytestream_peek (GstByteStream *bs, guint32 len);
guint8* gst_bytestream_peek_bytes (GstByteStream *bs, guint32 len); guint8* gst_bytestream_peek_bytes (GstByteStream *bs, guint32 len);
gboolean gst_bytestream_flush (GstByteStream *bs, guint32 len); gboolean gst_bytestream_flush (GstByteStream *bs, guint32 len);
void gst_bytestream_flush_fast (GstByteStream * bs, guint32 len); void gst_bytestream_flush_fast (GstByteStream *bs, guint32 len);
void gst_bytestream_get_status (GstByteStream *bs, guint32 *avail_out, GstEvent **event_out); void gst_bytestream_get_status (GstByteStream *bs, guint32 *avail_out, GstEvent **event_out);
void gst_bytestream_print_status (GstByteStream *bs); void gst_bytestream_print_status (GstByteStream *bs);