2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gstbufferstore.c: keep an easily accessible list of all buffers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include "gstbufferstore.h"
26 #include <gst/gstutils.h>
29 GST_DEBUG_CATEGORY_STATIC (gst_buffer_store_debug);
30 #define GST_CAT_DEFAULT gst_buffer_store_debug
42 static void gst_buffer_store_dispose (GObject * object);
44 static gboolean gst_buffer_store_add_buffer_func (GstBufferStore * store,
46 static void gst_buffer_store_cleared_func (GstBufferStore * store);
48 static guint gst_buffer_store_signals[LAST_SIGNAL] = { 0 };
50 GST_BOILERPLATE (GstBufferStore, gst_buffer_store, GObject, G_TYPE_OBJECT);
53 G_GNUC_UNUSED static void
54 debug_buffers (GstBufferStore *store)
56 GList *walk = store->buffers;
58 g_printerr ("BUFFERS in store:\n");
60 g_print ("%15"G_GUINT64_FORMAT" - %7u\n", GST_BUFFER_OFFSET (walk->data), GST_BUFFER_SIZE (walk->data));
61 walk = g_list_next (walk);
66 continue_accu (GSignalInvocationHint *ihint, GValue *return_accu,
67 const GValue *handler_return, gpointer data)
69 gboolean do_continue = g_value_get_boolean (handler_return);
70 g_value_set_boolean (return_accu, do_continue);
75 gst_buffer_store_base_init (gpointer g_class)
79 gst_buffer_store_class_init (GstBufferStoreClass *store_class)
81 GObjectClass *gobject_class = G_OBJECT_CLASS (store_class);
83 gobject_class->dispose = gst_buffer_store_dispose;
85 gst_buffer_store_signals[CLEARED] = g_signal_new ("cleared",
86 G_TYPE_FROM_CLASS (store_class), G_SIGNAL_RUN_LAST,
87 G_STRUCT_OFFSET (GstBufferStoreClass, cleared), NULL, NULL,
88 gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
89 gst_buffer_store_signals[BUFFER_ADDED] = g_signal_new ("buffer-added",
90 G_TYPE_FROM_CLASS (store_class), G_SIGNAL_RUN_LAST,
91 G_STRUCT_OFFSET (GstBufferStoreClass, buffer_added), continue_accu, NULL,
92 gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, GST_TYPE_BUFFER);
94 store_class->cleared = gst_buffer_store_cleared_func;
95 store_class->buffer_added = gst_buffer_store_add_buffer_func;
98 gst_buffer_store_init (GstBufferStore *store)
100 store->buffers = NULL;
103 gst_buffer_store_dispose (GObject *object)
105 GstBufferStore *store = GST_BUFFER_STORE (object);
107 gst_buffer_store_clear (store);
109 parent_class->dispose (object);
112 gst_buffer_store_add_buffer_func (GstBufferStore *store, GstBuffer *buffer)
114 g_assert (buffer != NULL);
116 if (!GST_BUFFER_OFFSET_IS_VALID (buffer) &&
118 GST_BUFFER_OFFSET_IS_VALID (store->buffers->data)) {
119 /* we assumed valid offsets, but suddenly they are not anymore */
120 GST_DEBUG_OBJECT (store, "attempting to add buffer %p with invalid offset to store with valid offset, abort",
123 } else if (!store->buffers || !GST_BUFFER_OFFSET_IS_VALID (store->buffers->data)) {
124 /* the starting buffer had an invalid offset, in that case we assume continuous buffers */
125 GST_LOG_OBJECT (store, "adding buffer %p with invalid offset and size %u",
126 buffer, GST_BUFFER_SIZE (buffer));
127 gst_data_ref (GST_DATA (buffer));
128 store->buffers = g_list_append (store->buffers, buffer);
131 /* both list and buffer have valid offsets, we can really go wild */
132 GList *walk, *current_list = NULL;
135 g_assert (GST_BUFFER_OFFSET_IS_VALID (buffer));
136 GST_LOG_OBJECT (store, "attempting to add buffer %p with offset %"G_GUINT64_FORMAT" and size %u",
137 buffer, GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
138 /* we keep a sorted list of non-overlapping buffers */
139 walk = store->buffers;
141 current = GST_BUFFER (walk->data);
143 walk = g_list_next (walk);
144 if (GST_BUFFER_OFFSET (current) < GST_BUFFER_OFFSET (buffer)) {
146 } else if (GST_BUFFER_OFFSET (current) == GST_BUFFER_OFFSET (buffer)) {
149 needed_size = MIN (GST_BUFFER_SIZE (buffer),
150 GST_BUFFER_OFFSET (walk->data) - GST_BUFFER_OFFSET (current));
152 needed_size = GST_BUFFER_SIZE (buffer);
154 if (needed_size <= GST_BUFFER_SIZE (current)) {
158 if (needed_size < GST_BUFFER_SIZE (buffer)) {
159 /* need to create subbuffer to not have overlapping data */
160 GstBuffer *sub = gst_buffer_create_sub (buffer, 0, needed_size);
164 gst_data_ref (GST_DATA (buffer));
166 /* replace current buffer with new one */
167 GST_INFO_OBJECT (store, "replacing buffer %p with buffer %p with offset %"G_GINT64_FORMAT" and size %u",
168 current_list->data, buffer, GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
169 gst_data_unref (GST_DATA (current_list->data));
170 current_list->data = buffer;
174 } else if (GST_BUFFER_OFFSET (current) > GST_BUFFER_OFFSET (buffer)) {
175 GList *previous = g_list_previous (current_list);
176 guint64 start_offset = previous ?
177 GST_BUFFER_OFFSET (previous->data) + GST_BUFFER_SIZE (previous->data) : 0;
179 if (start_offset == GST_BUFFER_OFFSET (current)) {
183 /* we have data to insert */
184 if (start_offset > GST_BUFFER_OFFSET (buffer) ||
185 GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) > GST_BUFFER_OFFSET (current)) {
188 /* need a subbuffer */
189 start_offset = GST_BUFFER_OFFSET (buffer) > start_offset ? 0 :
190 start_offset - GST_BUFFER_OFFSET (buffer);
191 sub = gst_buffer_create_sub (buffer, start_offset,
192 MIN (GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (current) - start_offset - GST_BUFFER_OFFSET (buffer)));
194 GST_BUFFER_OFFSET (sub) = start_offset + GST_BUFFER_OFFSET (buffer);
197 gst_data_ref (GST_DATA (buffer));
199 GST_INFO_OBJECT (store, "adding buffer %p with offset %"G_GINT64_FORMAT" and size %u",
200 buffer, GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
201 store->buffers = g_list_insert_before (store->buffers, current_list, buffer);
208 gst_data_ref (GST_DATA (buffer));
209 GST_INFO_OBJECT (store, "adding buffer %p with offset %"G_GINT64_FORMAT" and size %u",
210 buffer, GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
212 g_list_append (current_list, buffer);
214 g_assert (store->buffers == NULL);
215 store->buffers = g_list_prepend (NULL, buffer);
222 gst_buffer_store_cleared_func (GstBufferStore *store)
224 g_list_foreach (store->buffers, (GFunc) gst_data_unref, NULL);
225 g_list_free (store->buffers);
226 store->buffers = NULL;
229 * gst_buffer_store_new:
231 * Creates a new bufferstore.
233 * Returns: the new bufferstore.
236 gst_buffer_store_new (void)
238 return GST_BUFFER_STORE (g_object_new (GST_TYPE_BUFFER_STORE, NULL));
241 * gst_buffer_store_clear:
242 * @store: a bufferstore
244 * Clears the buffer store. All buffers are removed and the buffer store
245 * behaves like it was just created.
247 /* FIXME: call this function _reset ? */
249 gst_buffer_store_clear (GstBufferStore *store)
251 g_return_if_fail (GST_IS_BUFFER_STORE (store));
253 g_signal_emit (store, gst_buffer_store_signals [CLEARED], 0, NULL);
256 * gst_buffer_store_add_buffer:
257 * @store: a bufferstore
258 * @buffer: the buffer to add
260 * Adds a buffer to the buffer store.
262 * Returns: TRUE, if the buffer was added, FALSE if an error occured.
265 gst_buffer_store_add_buffer (GstBufferStore *store, GstBuffer *buffer)
269 g_return_val_if_fail (GST_IS_BUFFER_STORE (store), FALSE);
270 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
272 if (store->buffers &&
273 GST_BUFFER_OFFSET_IS_VALID (store->buffers->data) &&
274 !GST_BUFFER_OFFSET_IS_VALID (buffer))
277 g_signal_emit (store, gst_buffer_store_signals [BUFFER_ADDED], 0, buffer, &ret);
282 * gst_buffer_store_get_buffer:
283 * @store: a bufferstore
284 * @offset: starting offset of returned buffer
285 * @size: size of returned buffer
287 * Returns a buffer that corresponds to the given area of data. If part of the
288 * data is not available inside the store, NULL is returned. You have to unref
289 * the buffer after use.
291 * Returns: a buffer with the requested data or NULL if the data was not
295 gst_buffer_store_get_buffer (GstBufferStore *store, guint64 offset, guint size)
301 gboolean have_offset;
302 guint64 cur_offset = 0;
303 GstBuffer *ret = NULL;
305 g_return_val_if_fail (GST_IS_BUFFER_STORE (store), NULL);
307 walk = store->buffers;
310 if (GST_BUFFER_OFFSET_IS_VALID (walk->data)) {
316 current = GST_BUFFER (walk->data);
318 cur_offset = GST_BUFFER_OFFSET (current);
320 walk = g_list_next (walk);
321 if (cur_offset > offset) {
322 /* #include <windows.h>
323 do_nothing_loop (); */
324 } else if (cur_offset == offset &&
325 GST_BUFFER_SIZE (current) == size) {
326 GST_LOG_OBJECT (store, "found matching buffer %p for offset %"G_GUINT64_FORMAT" and size %u",
327 current, offset, size);
329 gst_data_ref (GST_DATA (ret));
330 GST_LOG_OBJECT (store, "refcount %d",
331 GST_DATA_REFCOUNT_VALUE(ret));
333 } else if (cur_offset + GST_BUFFER_SIZE (current) > offset) {
334 if (cur_offset + GST_BUFFER_SIZE (current) >= offset + size) {
335 ret = gst_buffer_create_sub (current, offset - cur_offset, size);
336 GST_LOG_OBJECT (store, "created subbuffer %p from buffer %p for offset %llu and size %u",
337 ret, current, offset, size);
340 /* uh, the requested data spans some buffers */
341 ret = gst_buffer_new_and_alloc (size);
342 GST_LOG_OBJECT (store, "created buffer %p for offset %"G_GUINT64_FORMAT
343 " and size %u, will fill with data now",
345 data = GST_BUFFER_DATA (ret);
346 tmp = GST_BUFFER_SIZE (current) - offset + cur_offset;
347 memcpy (data, GST_BUFFER_DATA (current) + offset - cur_offset, tmp);
353 GST_BUFFER_OFFSET (current) + GST_BUFFER_SIZE (current) != GST_BUFFER_OFFSET (walk->data))) {
354 GST_DEBUG_OBJECT (store, "not all data for offset %"G_GUINT64_FORMAT" and remaining size %u available, aborting",
356 gst_data_unref (GST_DATA (ret));
360 current = GST_BUFFER (walk->data);
361 walk = g_list_next (walk);
362 tmp = MIN (GST_BUFFER_SIZE (current), size);
363 memcpy (data, GST_BUFFER_DATA (current), tmp);
368 cur_offset += GST_BUFFER_SIZE (current);
376 * gst_buffer_store_get_size:
377 * @store: a bufferstore
378 * @offset: desired offset
380 * Calculates the number of bytes available starting from offset. This allows
381 * to query a buffer with the returned size.
383 * Returns: the number of continuous bytes in the bufferstore starting at
387 gst_buffer_store_get_size (GstBufferStore *store, guint64 offset)
390 gboolean have_offset;
391 gboolean counting = FALSE;
392 guint64 cur_offset = 0;
393 GstBuffer *current = NULL;
396 g_return_val_if_fail (GST_IS_BUFFER_STORE (store), 0);
398 walk = store->buffers;
401 if (GST_BUFFER_OFFSET_IS_VALID (walk->data)) {
407 if (have_offset && counting &&
408 cur_offset + GST_BUFFER_SIZE (current) != GST_BUFFER_OFFSET (walk->data)) {
411 current = GST_BUFFER (walk->data);
413 cur_offset = GST_BUFFER_OFFSET (current);
415 walk = g_list_next (walk);
417 ret += GST_BUFFER_SIZE (current);
419 if (cur_offset > offset)
421 if (cur_offset + GST_BUFFER_SIZE (current) > offset) {
422 /* we have at least some bytes */
423 ret = cur_offset + GST_BUFFER_SIZE (current) - offset;
428 cur_offset += GST_BUFFER_SIZE (current);