summaryrefslogtreecommitdiff
path: root/gst/elements/gstbufferstore.c
blob: 492f64abed59788a92c93887e8c0e960e3d1c120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* GStreamer
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
 *
 * gstbufferstore.c: keep an easily accessible list of all buffers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include "gstbufferstore.h"
#include <gst/gstutils.h>
#include <string.h>

GST_DEBUG_CATEGORY_STATIC (gst_buffer_store_debug);
#define GST_CAT_DEFAULT gst_buffer_store_debug

enum
{
  CLEARED,
  BUFFER_ADDED,
  LAST_SIGNAL
};
enum
{
  ARG_0
};


static void gst_buffer_store_dispose (GObject * object);

static gboolean gst_buffer_store_add_buffer_func (GstBufferStore * store,
    GstBuffer * buffer);
static void gst_buffer_store_cleared_func (GstBufferStore * store);

static guint gst_buffer_store_signals[LAST_SIGNAL] = { 0 };

#define _do_init(bla) \
    GST_DEBUG_CATEGORY_INIT (gst_buffer_store_debug, "GstBufferStore", 0, "buffer store helper");

GST_BOILERPLATE_FULL (GstBufferStore, gst_buffer_store, GObject, G_TYPE_OBJECT,
    _do_init);


G_GNUC_UNUSED static void
debug_buffers (GstBufferStore * store)
{
  GList *walk = store->buffers;

  g_printerr ("BUFFERS in store:\n");
  while (walk) {
    g_print ("%15" G_GUINT64_FORMAT " - %7u\n", GST_BUFFER_OFFSET (walk->data),
        GST_BUFFER_SIZE (walk->data));
    walk = g_list_next (walk);
  }
  g_printerr ("\n");
}

static gboolean
continue_accu (GSignalInvocationHint * ihint, GValue * return_accu,
    const GValue * handler_return, gpointer data)
{
  gboolean do_continue = g_value_get_boolean (handler_return);

  g_value_set_boolean (return_accu, do_continue);

  return do_continue;
}
static void
gst_buffer_store_base_init (gpointer g_class)
{
}
static void
gst_buffer_store_class_init (GstBufferStoreClass * store_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (store_class);

  gobject_class->dispose = gst_buffer_store_dispose;

  gst_buffer_store_signals[CLEARED] = g_signal_new ("cleared",
      G_TYPE_FROM_CLASS (store_class), G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstBufferStoreClass, cleared), NULL, NULL,
      gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
  gst_buffer_store_signals[BUFFER_ADDED] = g_signal_new ("buffer-added",
      G_TYPE_FROM_CLASS (store_class), G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstBufferStoreClass, buffer_added), continue_accu, NULL,
      gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1, GST_TYPE_BUFFER);

  store_class->cleared = gst_buffer_store_cleared_func;
  store_class->buffer_added = gst_buffer_store_add_buffer_func;
}
static void
gst_buffer_store_init (GstBufferStore * store, GstBufferStoreClass * g_class)
{
  store->buffers = NULL;
}
static void
gst_buffer_store_dispose (GObject * object)
{
  GstBufferStore *store = GST_BUFFER_STORE (object);

  gst_buffer_store_clear (store);

  parent_class->dispose (object);
}

static gboolean
gst_buffer_store_add_buffer_func (GstBufferStore * store, GstBuffer * buffer)
{
  g_assert (buffer != NULL);

  if (!GST_BUFFER_OFFSET_IS_VALID (buffer) &&
      store->buffers && GST_BUFFER_OFFSET_IS_VALID (store->buffers->data)) {
    /* we assumed valid offsets, but suddenly they are not anymore */
    GST_DEBUG_OBJECT (store,
        "attempting to add buffer %p with invalid offset to store with valid offset, abort",
        buffer);
    return FALSE;
  } else if (!store->buffers
      || !GST_BUFFER_OFFSET_IS_VALID (store->buffers->data)) {
    /* the starting buffer had an invalid offset, in that case we assume continuous buffers */
    GST_LOG_OBJECT (store, "adding buffer %p with invalid offset and size %u",
        buffer, GST_BUFFER_SIZE (buffer));
    gst_mini_object_ref (GST_MINI_OBJECT (buffer));
    store->buffers = g_list_append (store->buffers, buffer);
    return TRUE;
  } else {
    /* both list and buffer have valid offsets, we can really go wild */
    GList *walk, *current_list = NULL;
    GstBuffer *current;

    g_assert (GST_BUFFER_OFFSET_IS_VALID (buffer));
    GST_LOG_OBJECT (store,
        "attempting to add buffer %p with offset %" G_GUINT64_FORMAT
        " and size %u", buffer, GST_BUFFER_OFFSET (buffer),
        GST_BUFFER_SIZE (buffer));
    /* we keep a sorted list of non-overlapping buffers */
    walk = store->buffers;
    while (walk) {
      current = GST_BUFFER (walk->data);
      current_list = walk;
      walk = g_list_next (walk);
      if (GST_BUFFER_OFFSET (current) < GST_BUFFER_OFFSET (buffer)) {
        continue;
      } else if (GST_BUFFER_OFFSET (current) == GST_BUFFER_OFFSET (buffer)) {
        guint needed_size;

        if (walk) {
          needed_size = MIN (GST_BUFFER_SIZE (buffer),
              GST_BUFFER_OFFSET (walk->data) - GST_BUFFER_OFFSET (current));
        } else {
          needed_size = GST_BUFFER_SIZE (buffer);
        }
        if (needed_size <= GST_BUFFER_SIZE (current)) {
          buffer = NULL;
          break;
        } else {
          if (needed_size < GST_BUFFER_SIZE (buffer)) {
            /* need to create subbuffer to not have overlapping data */
            GstBuffer *sub = gst_buffer_create_sub (buffer, 0, needed_size);

            g_assert (sub);
            buffer = sub;
          } else {
            gst_mini_object_ref (GST_MINI_OBJECT (buffer));
          }
          /* replace current buffer with new one */
          GST_INFO_OBJECT (store,
              "replacing buffer %p with buffer %p with offset %" G_GINT64_FORMAT
              " and size %u", current_list->data, buffer,
              GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
          gst_mini_object_unref (GST_MINI_OBJECT (current_list->data));
          current_list->data = buffer;
          buffer = NULL;
          break;
        }
      } else if (GST_BUFFER_OFFSET (current) > GST_BUFFER_OFFSET (buffer)) {
        GList *previous = g_list_previous (current_list);
        guint64 start_offset = previous ?
            GST_BUFFER_OFFSET (previous->data) +
            GST_BUFFER_SIZE (previous->data) : 0;

        if (start_offset == GST_BUFFER_OFFSET (current)) {
          buffer = NULL;
          break;
        } else {
          /* we have data to insert */
          if (start_offset > GST_BUFFER_OFFSET (buffer) ||
              GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer) >
              GST_BUFFER_OFFSET (current)) {
            GstBuffer *sub;

            /* need a subbuffer */
            start_offset = GST_BUFFER_OFFSET (buffer) > start_offset ? 0 :
                start_offset - GST_BUFFER_OFFSET (buffer);
            sub = gst_buffer_create_sub (buffer, start_offset,
                MIN (GST_BUFFER_SIZE (buffer) - start_offset,
                    GST_BUFFER_OFFSET (current) - start_offset -
                    GST_BUFFER_OFFSET (buffer)));
            g_assert (sub);
            GST_BUFFER_OFFSET (sub) = start_offset + GST_BUFFER_OFFSET (buffer);
            buffer = sub;
          } else {
            gst_mini_object_ref (GST_MINI_OBJECT (buffer));
          }
          GST_INFO_OBJECT (store,
              "adding buffer %p with offset %" G_GINT64_FORMAT " and size %u",
              buffer, GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
          store->buffers =
              g_list_insert_before (store->buffers, current_list, buffer);
          buffer = NULL;
          break;
        }
      }
    }
    if (buffer) {
      gst_mini_object_ref (GST_MINI_OBJECT (buffer));
      GST_INFO_OBJECT (store,
          "adding buffer %p with offset %" G_GINT64_FORMAT " and size %u",
          buffer, GST_BUFFER_OFFSET (buffer), GST_BUFFER_SIZE (buffer));
      if (current_list) {
        g_list_append (current_list, buffer);
      } else {
        g_assert (store->buffers == NULL);
        store->buffers = g_list_prepend (NULL, buffer);
      }
    }
    return TRUE;
  }
}
static void
gst_buffer_store_cleared_func (GstBufferStore * store)
{
  g_list_foreach (store->buffers, (GFunc) gst_mini_object_unref, NULL);
  g_list_free (store->buffers);
  store->buffers = NULL;
}

/**
 * gst_buffer_store_new:
 *
 * Creates a new bufferstore.
 *
 * Returns: the new bufferstore.
 */
GstBufferStore *
gst_buffer_store_new (void)
{
  return GST_BUFFER_STORE (g_object_new (GST_TYPE_BUFFER_STORE, NULL));
}

/**
 * gst_buffer_store_clear:
 * @store: a bufferstore
 *
 * Clears the buffer store. All buffers are removed and the buffer store
 * behaves like it was just created.
 */
/* FIXME: call this function _reset ? */
void
gst_buffer_store_clear (GstBufferStore * store)
{
  g_return_if_fail (GST_IS_BUFFER_STORE (store));

  g_signal_emit (store, gst_buffer_store_signals[CLEARED], 0, NULL);
}

/**
 * gst_buffer_store_add_buffer:
 * @store: a bufferstore
 * @buffer: the buffer to add
 *
 * Adds a buffer to the buffer store.
 *
 * Returns: TRUE, if the buffer was added, FALSE if an error occured.
 */
gboolean
gst_buffer_store_add_buffer (GstBufferStore * store, GstBuffer * buffer)
{
  gboolean ret;

  g_return_val_if_fail (GST_IS_BUFFER_STORE (store), FALSE);
  g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);

  if (store->buffers &&
      GST_BUFFER_OFFSET_IS_VALID (store->buffers->data) &&
      !GST_BUFFER_OFFSET_IS_VALID (buffer))
    return FALSE;

  g_signal_emit (store, gst_buffer_store_signals[BUFFER_ADDED], 0, buffer,
      &ret);

  return ret;
}

/**
 * gst_buffer_store_get_buffer:
 * @store: a bufferstore
 * @offset: starting offset of returned buffer
 * @size: size of returned buffer
 *
 * Returns a buffer that corresponds to the given area of data. If part of the
 * data is not available inside the store, NULL is returned. You have to unref
 * the buffer after use.
 *
 * Returns: a buffer with the requested data or NULL if the data was not
 *          available.
 */
GstBuffer *
gst_buffer_store_get_buffer (GstBufferStore * store, guint64 offset, guint size)
{
  GstBuffer *current;
  GList *walk;
  guint8 *data;
  guint tmp;
  gboolean have_offset;
  guint64 cur_offset = 0;
  GstBuffer *ret = NULL;

  g_return_val_if_fail (GST_IS_BUFFER_STORE (store), NULL);

  walk = store->buffers;
  if (!walk)
    return NULL;
  if (GST_BUFFER_OFFSET_IS_VALID (walk->data)) {
    have_offset = TRUE;
  } else {
    have_offset = FALSE;
  }
  while (walk) {
    current = GST_BUFFER (walk->data);
    if (have_offset) {
      cur_offset = GST_BUFFER_OFFSET (current);
    }
    walk = g_list_next (walk);
    if (cur_offset > offset) {
      /* #include <windows.h>
         do_nothing_loop (); */
    } else if (cur_offset == offset && GST_BUFFER_SIZE (current) == size) {
      GST_LOG_OBJECT (store,
          "found matching buffer %p for offset %" G_GUINT64_FORMAT
          " and size %u", current, offset, size);
      ret = current;
      gst_mini_object_ref (GST_MINI_OBJECT (ret));
      GST_LOG_OBJECT (store, "refcount %d", GST_MINI_OBJECT (ret)->refcount);
      break;
    } else if (cur_offset + GST_BUFFER_SIZE (current) > offset) {
      if (cur_offset + GST_BUFFER_SIZE (current) >= offset + size) {
        ret = gst_buffer_create_sub (current, offset - cur_offset, size);
        GST_LOG_OBJECT (store,
            "created subbuffer %p from buffer %p for offset %llu and size %u",
            ret, current, offset, size);
        break;
      }
      /* uh, the requested data spans some buffers */
      ret = gst_buffer_new_and_alloc (size);
      GST_BUFFER_OFFSET (ret) = offset;
      GST_LOG_OBJECT (store, "created buffer %p for offset %" G_GUINT64_FORMAT
          " and size %u, will fill with data now", ret, offset, size);
      data = GST_BUFFER_DATA (ret);
      tmp = GST_BUFFER_SIZE (current) - offset + cur_offset;
      memcpy (data, GST_BUFFER_DATA (current) + offset - cur_offset, tmp);
      data += tmp;
      size -= tmp;
      while (size) {
        if (walk == NULL ||
            (have_offset &&
                GST_BUFFER_OFFSET (current) + GST_BUFFER_SIZE (current) !=
                GST_BUFFER_OFFSET (walk->data))) {
          GST_DEBUG_OBJECT (store,
              "not all data for offset %" G_GUINT64_FORMAT
              " and remaining size %u available, aborting", offset, size);
          gst_mini_object_unref (GST_MINI_OBJECT (ret));
          ret = NULL;
          goto out;
        }
        current = GST_BUFFER (walk->data);
        walk = g_list_next (walk);
        tmp = MIN (GST_BUFFER_SIZE (current), size);
        memcpy (data, GST_BUFFER_DATA (current), tmp);
        data += tmp;
        size -= tmp;
      }
      goto out;
    }
    if (!have_offset) {
      cur_offset += GST_BUFFER_SIZE (current);
    }
  }
out:

  return ret;
}

/**
 * gst_buffer_store_get_size:
 * @store: a bufferstore
 * @offset: desired offset
 *
 * Calculates the number of bytes available starting from offset. This allows
 * to query a buffer with the returned size.
 *
 * Returns: the number of continuous bytes in the bufferstore starting at
 *          offset.
 */
guint
gst_buffer_store_get_size (GstBufferStore * store, guint64 offset)
{
  GList *walk;
  gboolean have_offset;
  gboolean counting = FALSE;
  guint64 cur_offset = 0;
  GstBuffer *current = NULL;
  guint ret = 0;

  g_return_val_if_fail (GST_IS_BUFFER_STORE (store), 0);

  walk = store->buffers;
  if (!walk)
    return 0;
  if (GST_BUFFER_OFFSET_IS_VALID (walk->data)) {
    have_offset = TRUE;
  } else {
    have_offset = FALSE;
  }
  while (walk) {
    if (have_offset && counting &&
        cur_offset + GST_BUFFER_SIZE (current) !=
        GST_BUFFER_OFFSET (walk->data)) {
      break;
    }
    current = GST_BUFFER (walk->data);
    if (have_offset) {
      cur_offset = GST_BUFFER_OFFSET (current);
    }
    walk = g_list_next (walk);
    if (counting) {
      ret += GST_BUFFER_SIZE (current);
    } else {
      if (cur_offset > offset)
        return 0;
      if (cur_offset + GST_BUFFER_SIZE (current) > offset) {
        /* we have at least some bytes */
        ret = cur_offset + GST_BUFFER_SIZE (current) - offset;
        counting = TRUE;
      }
    }
    if (!have_offset) {
      cur_offset += GST_BUFFER_SIZE (current);
    }
  }

  return ret;
}