## GstAudioRingBuffer

GstAudioRingBuffer — Base class for audio ringbuffer implementations

## Functions

void | (*GstAudioRingBufferCallback) ()
#define | GST_AUDIO_RING_BUFFER_BROADCAST()
#define | GST_AUDIO_RING_BUFFER_GET_COND()
#define | GST_AUDIO_RING_BUFFER_SIGNAL()
#define | GST_AUDIO_RING_BUFFER_WAIT()
void | gst_audio_ring_buffer_set_callback ()
void | gst_audio_ring_buffer_set_callback_full ()
gboolean | gst_audio_ring_buffer_acquire ()
gboolean | gst_audio_ring_buffer_release ()
gboolean | gst_audio_ring_buffer_is_acquired ()
gboolean | gst_audio_ring_buffer_activate ()
gboolean | gst_audio_ring_buffer_is_active ()
gboolean | gst_audio_ring_buffer_start ()
gboolean | gst_audio_ring_buffer_pause ()
gboolean | gst_audio_ring_buffer_stop ()
guint | gst_audio_ring_buffer_delay ()
guint64 | gst_audio_ring_buffer_samples_done ()
void | gst_audio_ring_buffer_set_sample ()
guint | gst_audio_ring_buffer_commit ()
gboolean | gst_audio_ring_buffer_convert ()
gboolean | gst_audio_ring_buffer_prepare_read ()
guint | gst_audio_ring_buffer_read ()
void | gst_audio_ring_buffer_clear ()
void | gst_audio_ring_buffer_clear_all ()
void | gst_audio_ring_buffer_advance ()
gboolean | gst_audio_ring_buffer_close_device ()
gboolean | gst_audio_ring_buffer_open_device ()
gboolean | gst_audio_ring_buffer_device_is_open ()
void | gst_audio_ring_buffer_may_start ()
gboolean | gst_audio_ring_buffer_parse_caps ()
void | gst_audio_ring_buffer_set_flushing ()
gboolean | gst_audio_ring_buffer_is_flushing ()
void | gst_audio_ring_buffer_set_channel_positions ()
void | gst_audio_ring_buffer_set_timestamp ()

## Types and Values

struct | GstAudioRingBuffer
struct | GstAudioRingBufferClass
struct | GstAudioRingBufferSpec
enum | GstAudioRingBufferState
enum | GstAudioRingBufferFormatType

## Object Hierarchy

        GObject
        ╰── GInitiallyUnowned
            ╰── GstObject
                ╰── GstAudioRingBuffer

## Includes

    #include <gst/audio/audio.h>

## Description

This object is the base class for audio ringbuffers used by the base audio source and sink classes.

The ringbuffer abstracts a circular buffer of data. One reader and one writer can operate on the data from different threads in a lockfree manner. The base class is sufficiently flexible to be used as an abstraction for DMA based ringbuffers as well as a pure software implementations.

## Functions

### GstAudioRingBufferCallback ()

    void
    (*GstAudioRingBufferCallback) (GstAudioRingBuffer *rbuf,
                                   guint8 *data,
                                   guint len,
                                   gpointer user_data);

This function is set with `gst_audio_ring_buffer_set_callback()` and is called to fill the memory at `data` with `len` bytes of samples.

#### Parameters

rbuf | a GstAudioRingBuffer |
data | target to fill. | [array length=len]
len | amount to fill |
user_data | user data |

### GST_AUDIO_RING_BUFFER_BROADCAST()

    #define GST_AUDIO_RING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_AUDIO_RING_BUFFER_GET_COND (buf)))

### GST_AUDIO_RING_BUFFER_GET_COND()

    #define GST_AUDIO_RING_BUFFER_GET_COND(buf) (&(((GstAudioRingBuffer *)buf)->cond))

### GST_AUDIO_RING_BUFFER_SIGNAL()

    #define GST_AUDIO_RING_BUFFER_SIGNAL(buf)   (g_cond_signal (GST_AUDIO_RING_BUFFER_GET_COND (buf)))

### GST_AUDIO_RING_BUFFER_WAIT()

    #define GST_AUDIO_RING_BUFFER_WAIT(buf)     (g_cond_wait (GST_AUDIO_RING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))

### gst_audio_ring_buffer_set_callback ()

    void
    gst_audio_ring_buffer_set_callback (GstAudioRingBuffer *buf,
                                        GstAudioRingBufferCallback cb,
                                        gpointer user_data);

Sets the given callback function on the buffer. This function will be called every time a segment has been written to a device.

MT safe.

[skip]

#### Parameters

buf | the GstAudioRingBuffer to set the callback on |
cb | the callback to set. | [allow-none]
user_data | user data passed to the callback |

### gst_audio_ring_buffer_set_callback_full ()

    void
    gst_audio_ring_buffer_set_callback_full
                                   (GstAudioRingBuffer *buf,
                                    GstAudioRingBufferCallback cb,
                                    gpointer user_data,
                                    GDestroyNotify notify);

Sets the given callback function on the buffer. This function will be called every time a segment has been written to a device.

MT safe.

[rename-to gst_audio_ring_buffer_set_callback]

#### Parameters

buf | the GstAudioRingBuffer to set the callback on |
cb | the callback to set. | [allow-none]
user_data | user data passed to the callback |
notify | function to be called when `user_data` is no longer needed |

Since: 1.12

### gst_audio_ring_buffer_acquire ()

    gboolean
    gst_audio_ring_buffer_acquire (GstAudioRingBuffer *buf,
                                   GstAudioRingBufferSpec *spec);

Allocate the resources for the ringbuffer. This function fills in the data pointer of the ring buffer with a valid GstBuffer to which samples can be written.

#### Parameters

buf | the GstAudioRingBuffer to acquire |
spec | the specs of the buffer |

#### Returns

TRUE if the device could be acquired, FALSE on error.

MT safe.

### gst_audio_ring_buffer_release ()

    gboolean
    gst_audio_ring_buffer_release (GstAudioRingBuffer *buf);

Free the resources of the ringbuffer.

#### Parameters

buf | the GstAudioRingBuffer to release |

#### Returns

TRUE if the device could be released, FALSE on error.

MT safe.

### gst_audio_ring_buffer_is_acquired ()

    gboolean
    gst_audio_ring_buffer_is_acquired (GstAudioRingBuffer *buf);

Check if the ringbuffer is acquired and ready to use.

#### Parameters

buf | the GstAudioRingBuffer to check |

#### Returns

TRUE if the ringbuffer is acquired, FALSE on error.

MT safe.

### gst_audio_ring_buffer_activate ()

    gboolean
    gst_audio_ring_buffer_activate (GstAudioRingBuffer *buf,
                                    gboolean active);

Activate `buf` to start or stop pulling data.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to activate |
active | the new mode |

#### Returns

TRUE if the device could be activated in the requested mode, FALSE on error.

### gst_audio_ring_buffer_is_active ()

    gboolean
    gst_audio_ring_buffer_is_active (GstAudioRingBuffer *buf);

Check if `buf` is activated.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer |

#### Returns

TRUE if the device is active.

### gst_audio_ring_buffer_start ()

    gboolean
    gst_audio_ring_buffer_start (GstAudioRingBuffer *buf);

Start processing samples from the ringbuffer.

#### Parameters

buf | the GstAudioRingBuffer to start |

#### Returns

TRUE if the device could be started, FALSE on error.

MT safe.

### gst_audio_ring_buffer_pause ()

    gboolean
    gst_audio_ring_buffer_pause (GstAudioRingBuffer *buf);

Pause processing samples from the ringbuffer.

#### Parameters

buf | the GstAudioRingBuffer to pause |

#### Returns

TRUE if the device could be paused, FALSE on error.

MT safe.

### gst_audio_ring_buffer_stop ()

    gboolean
    gst_audio_ring_buffer_stop (GstAudioRingBuffer *buf);

Stop processing samples from the ringbuffer.

#### Parameters

buf | the GstAudioRingBuffer to stop |

#### Returns

TRUE if the device could be stopped, FALSE on error.

MT safe.

### gst_audio_ring_buffer_delay ()

    guint
    gst_audio_ring_buffer_delay (GstAudioRingBuffer *buf);

Get the number of samples queued in the audio device. This is usually less than the segment size but can be bigger when the implementation uses another internal buffer between the audio device.

For playback ringbuffers this is the amount of samples transfered from the ringbuffer to the device but still not played.

For capture ringbuffers this is the amount of samples in the device that are not yet transfered to the ringbuffer.

#### Parameters

buf | the GstAudioRingBuffer to query |

#### Returns

The number of samples queued in the audio device.

MT safe.

### gst_audio_ring_buffer_samples_done ()

    guint64
    gst_audio_ring_buffer_samples_done (GstAudioRingBuffer *buf);

Get the number of samples that were processed by the ringbuffer since it was last started. This does not include the number of samples not yet processed (see `gst_audio_ring_buffer_delay()`).

#### Parameters

buf | the GstAudioRingBuffer to query |

#### Returns

The number of samples processed by the ringbuffer.

MT safe.

### gst_audio_ring_buffer_set_sample ()

    void
    gst_audio_ring_buffer_set_sample (GstAudioRingBuffer *buf,
                                      guint64 sample);

Make sure that the next sample written to the device is accounted for as being the `sample` sample written to the device. This value will be used in reporting the current sample position of the ringbuffer.

This function will also clear the buffer with silence.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to use |
sample | the sample number to set |

### gst_audio_ring_buffer_commit ()

    guint
    gst_audio_ring_buffer_commit (GstAudioRingBuffer *buf,
                                  guint64 *sample,
                                  guint8 *data,
                                  gint in_samples,
                                  gint out_samples,
                                  gint *accum);

Commit `in_samples` samples pointed to by `data` to the ringbuffer `buf` .

`in_samples` and `out_samples` define the rate conversion to perform on the samples in `data` . For negative rates, `out_samples` must be negative and `in_samples` positive.

When `out_samples` is positive, the first sample will be written at position `sample` in the ringbuffer. When `out_samples` is negative, the last sample will be written to `sample` in reverse order.

`out_samples` does not need to be a multiple of the segment size of the ringbuffer although it is recommended for optimal performance.

`accum` will hold a temporary accumulator used in rate conversion and should be set to 0 when this function is first called. In case the commit operation is interrupted, one can resume the processing by passing the previously returned `accum` value back to this function.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to commit |
sample | the sample position of the data |
data | the data to commit. | [array length=in_samples]
in_samples | the number of samples in the data to commit |
out_samples | the number of samples to write to the ringbuffer |
accum | accumulator for rate conversion. | [inout]

#### Returns

The number of samples written to the ringbuffer or -1 on error. The number of samples written can be less than `out_samples` when `buf` was interrupted with a flush or stop.

### gst_audio_ring_buffer_convert ()

    gboolean
    gst_audio_ring_buffer_convert (GstAudioRingBuffer *buf,
                                   GstFormat src_fmt,
                                   gint64 src_val,
                                   GstFormat dest_fmt,
                                   gint64 *dest_val);

Convert `src_val` in `src_fmt` to the equivalent value in `dest_fmt` . The result will be put in `dest_val` .

#### Parameters

buf | the GstAudioRingBuffer |
src_fmt | the source format |
src_val | the source value |
dest_fmt | the destination format |
dest_val | a location to store the converted value. | [out]

#### Returns

TRUE if the conversion succeeded.

### gst_audio_ring_buffer_prepare_read ()

    gboolean
    gst_audio_ring_buffer_prepare_read (GstAudioRingBuffer *buf,
                                        gint *segment,
                                        guint8 **readptr,
                                        gint *len);

Returns a pointer to memory where the data from segment `segment` can be found. This function is mostly used by subclasses.

#### Parameters

buf | the GstAudioRingBuffer to read from |
segment | the segment to read. | [out]
readptr | the pointer to the memory where samples can be read. | [out][array length=len]
len | the number of bytes to read. | [out]

#### Returns

FALSE if the buffer is not started.

MT safe.

### gst_audio_ring_buffer_read ()

    guint
    gst_audio_ring_buffer_read (GstAudioRingBuffer *buf,
                                guint64 sample,
                                guint8 *data,
                                guint len,
                                GstClockTime *timestamp);

Read `len` samples from the ringbuffer into the memory pointed to by `data` . The first sample should be read from position `sample` in the ringbuffer.

`len` should not be a multiple of the segment size of the ringbuffer although it is recommended.

`timestamp` will return the timestamp associated with the data returned.

#### Parameters

buf | the GstAudioRingBuffer to read from |
sample | the sample position of the data |
data | where the data should be read. | [array length=len]
len | the number of samples in data to read |
timestamp | where the timestamp is returned. | [out]

#### Returns

The number of samples read from the ringbuffer or -1 on error.

MT safe.

### gst_audio_ring_buffer_clear ()

    void
    gst_audio_ring_buffer_clear (GstAudioRingBuffer *buf,
                                 gint segment);

Clear the given segment of the buffer with silence samples. This function is used by subclasses.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to clear |
segment | the segment to clear |

### gst_audio_ring_buffer_clear_all ()

    void
    gst_audio_ring_buffer_clear_all (GstAudioRingBuffer *buf);

Fill the ringbuffer with silence.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to clear |

### gst_audio_ring_buffer_advance ()

    void
    gst_audio_ring_buffer_advance (GstAudioRingBuffer *buf,
                                   guint advance);

Subclasses should call this function to notify the fact that `advance` segments are now processed by the device.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to advance |
advance | the number of segments written |

### gst_audio_ring_buffer_close_device ()

    gboolean
    gst_audio_ring_buffer_close_device (GstAudioRingBuffer *buf);

Close the audio device associated with the ring buffer. The ring buffer should already have been released via `gst_audio_ring_buffer_release()`.

#### Parameters

buf | the GstAudioRingBuffer |

#### Returns

TRUE if the device could be closed, FALSE on error.

MT safe.

### gst_audio_ring_buffer_open_device ()

    gboolean
    gst_audio_ring_buffer_open_device (GstAudioRingBuffer *buf);

Open the audio device associated with the ring buffer. Does not perform any setup on the device. You must open the device before acquiring the ring buffer.

#### Parameters

buf | the GstAudioRingBuffer |

#### Returns

TRUE if the device could be opened, FALSE on error.

MT safe.

### gst_audio_ring_buffer_device_is_open ()

    gboolean
    gst_audio_ring_buffer_device_is_open (GstAudioRingBuffer *buf);

Checks the status of the device associated with the ring buffer.

#### Parameters

buf | the GstAudioRingBuffer |

#### Returns

TRUE if the device was open, FALSE if it was closed.

MT safe.

### gst_audio_ring_buffer_may_start ()

    void
    gst_audio_ring_buffer_may_start (GstAudioRingBuffer *buf,
                                     gboolean allowed);

Tell the ringbuffer that it is allowed to start playback when the ringbuffer is filled with samples.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer |
allowed | the new value |

### gst_audio_ring_buffer_parse_caps ()

    gboolean
    gst_audio_ring_buffer_parse_caps (GstAudioRingBufferSpec *spec,
                                      GstCaps *caps);

Parse `caps` into `spec` .

#### Parameters

spec | a spec |
caps | a GstCaps |

#### Returns

TRUE if the caps could be parsed.

### gst_audio_ring_buffer_set_flushing ()

    void
    gst_audio_ring_buffer_set_flushing (GstAudioRingBuffer *buf,
                                        gboolean flushing);

Set the ringbuffer to flushing mode or normal mode.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer to flush |
flushing | the new mode |

### gst_audio_ring_buffer_is_flushing ()

    gboolean
    gst_audio_ring_buffer_is_flushing (GstAudioRingBuffer *buf);

Check if `buf` is flushing.

MT safe.

#### Parameters

buf | the GstAudioRingBuffer |

#### Returns

TRUE if the device is flushing.

### gst_audio_ring_buffer_set_channel_positions ()

    void
    gst_audio_ring_buffer_set_channel_positions
                                   (GstAudioRingBuffer *buf,
                                    const GstAudioChannelPosition *position);

Tell the ringbuffer about the device's channel positions. This must be called in when the ringbuffer is acquired.

#### Parameters

buf | the GstAudioRingBuffer |
position | the device channel positions. | [array]

### gst_audio_ring_buffer_set_timestamp ()

    void
    gst_audio_ring_buffer_set_timestamp (GstAudioRingBuffer *buf,
                                         gint readseg,
                                         GstClockTime timestamp);

## Types and Values

### struct GstAudioRingBuffer

    struct GstAudioRingBuffer {
      GCond                      cond;
      gboolean                    open;
      gboolean                    acquired;
      guint8                     *memory;
      gsize                       size;
      GstAudioRingBufferSpec      spec;
      gint                        samples_per_seg;
      guint8                     *empty_seg;

      gint                        state;
      gint                        segdone;
      gint                        segbase;
      gint                        waiting;
    };

The ringbuffer base class structure.

#### Members

GCond `cond`; | used to signal start/stop/pause/resume actions |
gboolean `open`; | boolean indicating that the ringbuffer is open |
gboolean `acquired`; | boolean indicating that the ringbuffer is acquired |
guint8 *`memory`; | data in the ringbuffer |
gsize `size`; | size of data in the ringbuffer |
GstAudioRingBufferSpec `spec`; | format and layout of the ringbuffer data |
gint `samples_per_seg`; | number of samples in one segment |
guint8 *`empty_seg`; | pointer to memory holding one segment of silence samples |
gint `state`; | state of the buffer |
gint `segdone`; | readpointer in the ringbuffer |
gint `segbase`; | segment corresponding to segment 0 (unused) |
gint `waiting`; | is a reader or writer waiting for a free segment |

### struct GstAudioRingBufferClass

    struct GstAudioRingBufferClass {
      GstObjectClass parent_class;

      gboolean     (*open_device)  (GstAudioRingBuffer *buf);
      gboolean     (*acquire)      (GstAudioRingBuffer *buf, GstAudioRingBufferSpec *spec);
      gboolean     (*release)      (GstAudioRingBuffer *buf);
      gboolean     (*close_device) (GstAudioRingBuffer *buf);

      gboolean     (*start)        (GstAudioRingBuffer *buf);
      gboolean     (*pause)        (GstAudioRingBuffer *buf);
      gboolean     (*resume)       (GstAudioRingBuffer *buf);
      gboolean     (*stop)         (GstAudioRingBuffer *buf);

      guint        (*delay)        (GstAudioRingBuffer *buf);

      /* ABI added */
      gboolean     (*activate)     (GstAudioRingBuffer *buf, gboolean active);

      guint        (*commit)       (GstAudioRingBuffer * buf, guint64 *sample,
                                    guint8 * data, gint in_samples,
                                    gint out_samples, gint * accum);

      void         (*clear_all)    (GstAudioRingBuffer * buf);
    };

The vmethods that subclasses can override to implement the ringbuffer.

#### Members

`open_device` () | open the device, don't set any params or allocate anything |
`acquire` () | allocate the resources for the ringbuffer using the given spec |
`release` () | free resources of the ringbuffer |
`close_device` () | close the device |
`start` () | start processing of samples |
`pause` () | pause processing of samples |
`resume` () | resume processing of samples after pause |
`stop` () | stop processing of samples |
`delay` () | get number of frames queued in device |
`activate` () | activate the thread that starts pulling and monitoring the consumed segments in the device. |
`commit` () | write samples into the ringbuffer |
`clear_all` () | clear the entire ringbuffer. |

### struct GstAudioRingBufferSpec

    struct GstAudioRingBufferSpec {
      /* in */
      GstCaps  *caps;               /* the caps of the buffer */

      /* in/out */
      GstAudioRingBufferFormatType  type;
      GstAudioInfo                  info;

      guint64  latency_time;        /* the required/actual latency time, this is the
    				 * actual the size of one segment and the
    				 * minimum possible latency we can achieve. */
      guint64  buffer_time;         /* the required/actual time of the buffer, this is
    				 * the total size of the buffer and maximum
    				 * latency we can compensate for. */
      gint     segsize;             /* size of one buffer segment in bytes, this value
    				 * should be chosen to match latency_time as
    				 * well as possible. */
      gint     segtotal;            /* total number of segments, this value is the
    				 * number of segments of @segsize and should be
    				 * chosen so that it matches buffer_time as
    				 * close as possible. */
      /* ABI added 0.10.20 */
      gint     seglatency;          /* number of segments queued in the lower
    				 * level device, defaults to segtotal. */
    };

The structure containing the format specification of the ringbuffer.

#### Members

GstCaps *`caps`; | The caps that generated the Spec. |
GstAudioRingBufferFormatType `type`; | the sample type |
GstAudioInfo `info`; | the GstAudioInfo |
guint64 `latency_time`; | the latency in microseconds |
guint64 `buffer_time`; | the total buffer size in microseconds |
gint `segsize`; | the size of one segment in bytes |
gint `segtotal`; | the total number of segments |
gint `seglatency`; | number of segments queued in the lower level device, defaults to segtotal |

### enum GstAudioRingBufferState

The state of the ringbuffer.

#### Members

GST_AUDIO_RING_BUFFER_STATE_STOPPED | The ringbuffer is stopped |
GST_AUDIO_RING_BUFFER_STATE_PAUSED | The ringbuffer is paused |
GST_AUDIO_RING_BUFFER_STATE_STARTED | The ringbuffer is started |
GST_AUDIO_RING_BUFFER_STATE_ERROR | The ringbuffer has encountered an error after it has been started, e.g. because the device was disconnected (Since: 1.2) |

### enum GstAudioRingBufferFormatType

The format of the samples in the ringbuffer.

#### Members

GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW | samples in linear or float |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW | samples in mulaw |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW | samples in alaw |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_IMA_ADPCM | samples in ima adpcm |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG | samples in mpeg audio (but not AAC) format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_GSM | samples in gsm format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_IEC958 | samples in IEC958 frames (e.g. AC3) |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3 | samples in AC3 format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_EAC3 | samples in EAC3 format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS | samples in DTS format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC | samples in MPEG-2 AAC ADTS format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC | samples in MPEG-4 AAC ADTS format |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG2_AAC_RAW | samples in MPEG-2 AAC raw format (Since: 1.12) |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG4_AAC_RAW | samples in MPEG-4 AAC raw format (Since: 1.12) |
GST_AUDIO_RING_BUFFER_FORMAT_TYPE_FLAC | samples in FLAC format (Since: 1.12) |

## See Also

GstAudioBaseSink, GstAudioSink