Пример #1
0
/**
 * gst_adapter_take_buffer:
 * @adapter: a #GstAdapter
 * @nbytes: the number of bytes to take
 *
 * Returns a #GstBuffer containing the first @nbytes bytes of the
 * @adapter. The returned bytes will be flushed from the adapter.
 * This function is potentially more performant than gst_adapter_take()
 * since it can reuse the memory in pushed buffers by subbuffering
 * or merging.
 *
 * Caller owns returned value. gst_buffer_unref() after usage.
 *
 * Since: 0.10.6
 *
 * Returns: a #GstBuffer containing the first @nbytes of the adapter,
 * or #NULL if @nbytes bytes are not available
 */
GstBuffer *
gst_adapter_take_buffer (GstAdapter * adapter, guint nbytes)
{
    GstBuffer *buffer;
    GstBuffer *cur;
    guint hsize, skip;
    guint8 *data;

    g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
    g_return_val_if_fail (nbytes > 0, NULL);

    GST_LOG_OBJECT (adapter, "taking buffer of %u bytes", nbytes);

    /* we don't have enough data, return NULL. This is unlikely
     * as one usually does an _available() first instead of grabbing a
     * random size. */
    if (G_UNLIKELY (nbytes > adapter->size))
        return NULL;

    cur = adapter->buflist->data;
    skip = adapter->skip;
    hsize = GST_BUFFER_SIZE (cur);

    /* our head buffer has enough data left, return it */
    if (skip == 0 && hsize == nbytes) {
        GST_LOG_OBJECT (adapter, "providing buffer of %d bytes as head buffer",
                        nbytes);
        buffer = gst_buffer_ref (cur);
        goto done;
    } else if (hsize >= nbytes + skip) {
        GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
                        nbytes);
        buffer = gst_buffer_create_sub (cur, skip, nbytes);
        goto done;
    }

    if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
        /* Merged something, let's try again for sub-buffering */
        cur = adapter->buflist->data;
        if (GST_BUFFER_SIZE (cur) >= nbytes + skip) {
            GST_LOG_OBJECT (adapter, "providing buffer of %d bytes via sub-buffer",
                            nbytes);
            buffer = gst_buffer_create_sub (cur, skip, nbytes);
            goto done;
        }
    }

    data = gst_adapter_take_internal (adapter, nbytes);

    buffer = gst_buffer_new ();
    GST_BUFFER_SIZE (buffer) = nbytes;
    GST_BUFFER_DATA (buffer) = data;
    GST_BUFFER_MALLOCDATA (buffer) = data;

done:
    gst_adapter_flush_unchecked (adapter, nbytes);

    return buffer;
}
Пример #2
0
/**
 * gst_adapter_take_buffer:
 * @adapter: a #GstAdapter
 * @nbytes: the number of bytes to take
 *
 * Returns a #GstBuffer containing the first @nbytes bytes of the
 * @adapter. The returned bytes will be flushed from the adapter.
 * This function is potentially more performant than gst_adapter_take()
 * since it can reuse the memory in pushed buffers by subbuffering
 * or merging.
 *
 * Note that no assumptions should be made as to whether certain buffer
 * flags such as the DISCONT flag are set on the returned buffer, or not.
 * The caller needs to explicitly set or unset flags that should be set or
 * unset.
 *
 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
 * usage.
 *
 * Free-function: gst_buffer_unref
 *
 * Returns: (transfer full): a #GstBuffer containing the first @nbytes of
 *     the adapter, or #NULL if @nbytes bytes are not available.
 *     gst_buffer_unref() when no longer needed.
 */
GstBuffer *
gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
{
  GstBuffer *buffer;
  GstBuffer *cur;
  gsize hsize, skip;
  guint8 *data;

  g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
  g_return_val_if_fail (nbytes > 0, NULL);

  GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
      nbytes);

  /* we don't have enough data, return NULL. This is unlikely
   * as one usually does an _available() first instead of grabbing a
   * random size. */
  if (G_UNLIKELY (nbytes > adapter->size))
    return NULL;

  cur = adapter->buflist->data;
  skip = adapter->skip;
  hsize = gst_buffer_get_size (cur);

  /* our head buffer has enough data left, return it */
  if (skip == 0 && hsize == nbytes) {
    GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
        " as head buffer", nbytes);
    buffer = gst_buffer_ref (cur);
    goto done;
  } else if (hsize >= nbytes + skip) {
    GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
        " via region copy", nbytes);
    buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
    goto done;
  }
#if 0
  if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
    /* Merged something, let's try again for sub-buffering */
    cur = adapter->buflist->data;
    skip = adapter->skip;
    if (gst_buffer_get_size (cur) >= nbytes + skip) {
      GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
          " via sub-buffer", nbytes);
      buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
      goto done;
    }
  }
#endif

  data = gst_adapter_take_internal (adapter, nbytes);

  buffer = gst_buffer_new_wrapped (data, nbytes);

done:
  gst_adapter_flush_unchecked (adapter, nbytes);

  return buffer;
}
Пример #3
0
/**
 * gst_adapter_take:
 * @adapter: a #GstAdapter
 * @nbytes: the number of bytes to take
 *
 * Returns a freshly allocated buffer containing the first @nbytes bytes of the
 * @adapter. The returned bytes will be flushed from the adapter.
 *
 * Caller owns returned value. g_free after usage.
 *
 * Returns: oven-fresh hot data, or #NULL if @nbytes bytes are not available
 */
guint8 *
gst_adapter_take (GstAdapter * adapter, guint nbytes)
{
    guint8 *data;

    g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
    g_return_val_if_fail (nbytes > 0, NULL);

    /* we don't have enough data, return NULL. This is unlikely
     * as one usually does an _available() first instead of peeking a
     * random size. */
    if (G_UNLIKELY (nbytes > adapter->size))
        return NULL;

    data = gst_adapter_take_internal (adapter, nbytes);

    gst_adapter_flush_unchecked (adapter, nbytes);

    return data;
}