2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #ifndef __GST_RING_BUFFER_H__
24 #define __GST_RING_BUFFER_H__
30 #define GST_TYPE_RING_BUFFER (gst_ring_buffer_get_type())
31 #define GST_RING_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RING_BUFFER,GstRingBuffer))
32 #define GST_RING_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RING_BUFFER,GstRingBufferClass))
33 #define GST_RING_BUFFER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_RING_BUFFER, GstRingBufferClass))
34 #define GST_RING_BUFFER_CAST(obj) ((GstRingBuffer *)obj)
35 #define GST_IS_RING_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RING_BUFFER))
36 #define GST_IS_RING_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RING_BUFFER))
38 typedef struct _GstRingBuffer GstRingBuffer;
39 typedef struct _GstRingBufferClass GstRingBufferClass;
40 typedef struct _GstRingBufferSpec GstRingBufferSpec;
43 * GstRingBufferCallback:
44 * @rbuf: a #GstRingBuffer
45 * @data: target to fill
46 * @len: amount to fill
47 * @user_data: user data
49 * This function is set with gst_ring_buffer_set_callback() and is
50 * called to fill the memory at @data with @len bytes of samples.
52 typedef void (*GstRingBufferCallback) (GstRingBuffer *rbuf, guint8* data, guint len, gpointer user_data);
56 * @GST_RING_BUFFER_STATE_STOPPED: The ringbuffer is stopped
57 * @GST_RING_BUFFER_STATE_PAUSED: The ringbuffer is paused
58 * @GST_RING_BUFFER_STATE_STARTED: The ringbuffer is started
60 * The state of the ringbuffer.
63 GST_RING_BUFFER_STATE_STOPPED,
64 GST_RING_BUFFER_STATE_PAUSED,
65 GST_RING_BUFFER_STATE_STARTED
69 * GstRingBufferSegState:
70 * @GST_SEGSTATE_INVALID: The content of the segment is invalid
71 * @GST_SEGSTATE_EMPTY: The segment is empty
72 * @GST_SEGSTATE_FILLED: The segment contains valid data
73 * @GST_SEGSTATE_PARTIAL: The segment partially contains valid data
75 * The state of a segment in the ringbuffer.
82 } GstRingBufferSegState;
85 * GstBufferFormatType:
86 * @GST_BUFTYPE_LINEAR: samples in linear PCM
87 * @GST_BUFTYPE_FLOAT: samples in float
88 * @GST_BUFTYPE_MU_LAW: samples in mulaw
89 * @GST_BUFTYPE_A_LAW: samples in alaw
90 * @GST_BUFTYPE_IMA_ADPCM: samples in ima adpcm
91 * @GST_BUFTYPE_MPEG: samples in mpeg audio (but not AAC) format
92 * @GST_BUFTYPE_GSM: samples in gsm format
93 * @GST_BUFTYPE_IEC958: samples in IEC958 frames (e.g. AC3)
94 * @GST_BUFTYPE_AC3: samples in AC3 format
95 * @GST_BUFTYPE_EAC3: samples in EAC3 format
96 * @GST_BUFTYPE_DTS: samples in DTS format
97 * @GST_BUFTYPE_MPEG2_AAC: samples in MPEG-2 AAC format
98 * @GST_BUFTYPE_MPEG4_AAC: samples in MPEG-4 AAC format
100 * The format of the samples in the ringbuffer.
108 GST_BUFTYPE_IMA_ADPCM,
115 GST_BUFTYPE_MPEG2_AAC,
116 GST_BUFTYPE_MPEG4_AAC,
117 } GstBufferFormatType;
121 * @GST_UNKNOWN: unspecified
122 * @GST_S8: integer signed 8 bit
123 * @GST_U8: integer unsigned 8 bit
124 * @GST_S16_LE: integer signed 16 bit little endian
125 * @GST_S16_BE: integer signed 16 bit big endian
126 * @GST_U16_LE: integer unsigned 16 bit little endian
127 * @GST_U16_BE: integer unsigned 16 bit big endian
128 * @GST_S24_LE: integer signed 24 bit little endian
129 * @GST_S24_BE: integer signed 24 bit big endian
130 * @GST_U24_LE: integer unsigned 24 bit little endian
131 * @GST_U24_BE: integer unsigned 24 bit big endian
132 * @GST_S32_LE: integer signed 32 bit little endian
133 * @GST_S32_BE: integer signed 32 bit big endian
134 * @GST_U32_LE: integer unsigned 32 bit little endian
135 * @GST_U32_BE: integer unsigned 32 bit big endian
136 * @GST_S24_3LE: integer signed 24 bit little endian packed in 3 bytes
137 * @GST_S24_3BE: integer signed 24 bit big endian packed in 3 bytes
138 * @GST_U24_3LE: integer unsigned 24 bit little endian packed in 3 bytes
139 * @GST_U24_3BE: integer unsigned 24 bit big endian packed in 3 bytes
140 * @GST_S20_3LE: integer signed 20 bit little endian packed in 3 bytes
141 * @GST_S20_3BE: integer signed 20 bit big endian packed in 3 bytes
142 * @GST_U20_3LE: integer unsigned 20 bit little endian packed in 3 bytes
143 * @GST_U20_3BE: integer unsigned 20 bit big endian packed in 3 bytes
144 * @GST_S18_3LE: integer signed 18 bit little endian packed in 3 bytes
145 * @GST_S18_3BE: integer signed 18 bit big endian packed in 3 bytes
146 * @GST_U18_3LE: integer unsigned 18 bit little endian packed in 3 bytes
147 * @GST_U18_3BE: integer unsigned 18 bit big endian packed in 3 bytes
148 * @GST_FLOAT32_LE: floating 32 bit little endian
149 * @GST_FLOAT32_BE: floating 32 bit big endian
150 * @GST_FLOAT64_LE: floating 64 bit little endian
151 * @GST_FLOAT64_BE: floating 64 bit big endian
152 * @GST_MU_LAW: mu-law
154 * @GST_IMA_ADPCM: ima adpcm
155 * @GST_MPEG: mpeg audio (but not aac)
157 * @GST_IEC958: IEC958 frames
161 * @GST_MPEG2_AAC: mpeg-2 aac
162 * @GST_MPEG4_AAC: mpeg-4 aac
164 * The detailed format of the samples in the ringbuffer.
222 * @caps: The caps that generated the Spec.
223 * @type: the sample type
224 * @format: the sample format
225 * @sign: the sample sign
226 * @bigend: the endianness of the samples
227 * @width: the width of the samples
228 * @depth: th depth of the samples
229 * @rate: the samplerate
230 * @channels: the number of channels
231 * @latency_time: the latency in microseconds
232 * @buffer_time: the total buffer size in microseconds
233 * @segsize: the size of one segment in bytes
234 * @segtotal: the total number of segments
235 * @bytes_per_sample: number of bytes in one sample
236 * @silence_sample: bytes representing one sample of silence
237 * @seglatency: number of segments queued in the lower level device,
238 * defaults to segtotal
240 * The structure containing the format specification of the ringbuffer.
242 struct _GstRingBufferSpec
246 GstCaps *caps; /* the caps of the buffer */
249 GstBufferFormatType type;
250 GstBufferFormat format;
258 guint64 latency_time; /* the required/actual latency time, this is the
259 * actual the size of one segment and the
260 * minimum possible latency we can achieve. */
261 guint64 buffer_time; /* the required/actual time of the buffer, this is
262 * the total size of the buffer and maximum
263 * latency we can compensate for. */
264 gint segsize; /* size of one buffer segment in bytes, this value
265 * should be chosen to match latency_time as
266 * well as possible. */
267 gint segtotal; /* total number of segments, this value is the
268 * number of segments of @segsize and should be
269 * chosen so that it matches buffer_time as
270 * close as possible. */
272 gint bytes_per_sample; /* number of bytes of one sample */
273 guint8 silence_sample[32]; /* bytes representing silence */
275 /* ABI added 0.10.20 */
276 gint seglatency; /* number of segments queued in the lower
277 * level device, defaults to segtotal. */
280 /* gpointer _gst_reserved[GST_PADDING]; */
281 guint8 _gst_reserved[(sizeof (gpointer) * GST_PADDING) - sizeof (gint)];
284 #define GST_RING_BUFFER_GET_COND(buf) (((GstRingBuffer *)buf)->cond)
285 #define GST_RING_BUFFER_WAIT(buf) (g_cond_wait (GST_RING_BUFFER_GET_COND (buf), GST_OBJECT_GET_LOCK (buf)))
286 #define GST_RING_BUFFER_SIGNAL(buf) (g_cond_signal (GST_RING_BUFFER_GET_COND (buf)))
287 #define GST_RING_BUFFER_BROADCAST(buf)(g_cond_broadcast (GST_RING_BUFFER_GET_COND (buf)))
291 * @cond: used to signal start/stop/pause/resume actions
292 * @open: boolean indicating that the ringbuffer is open
293 * @acquired: boolean indicating that the ringbuffer is acquired
294 * @data: data in the ringbuffer
295 * @spec: format and layout of the ringbuffer data
296 * @segstate: status of each segment in the ringbuffer (unused)
297 * @samples_per_seg: number of samples in one segment
298 * @empty_seg: pointer to memory holding one segment of silence samples
299 * @state: state of the buffer
300 * @segdone: readpointer in the ringbuffer
301 * @segbase: segment corresponding to segment 0 (unused)
302 * @waiting: is a reader or writer waiting for a free segment
304 * The ringbuffer base class structure.
306 struct _GstRingBuffer {
309 /*< public >*/ /* with LOCK */
315 GstRingBufferSpec spec;
316 GstRingBufferSegState *segstate;
317 gint samples_per_seg;
320 /*< public >*/ /* ATOMIC */
327 GstRingBufferCallback callback;
338 /* adding + 0 to mark ABI change to be undone later */
339 gpointer _gst_reserved[GST_PADDING + 0];
344 * GstRingBufferClass:
345 * @parent_class: parent class
346 * @open_device: open the device, don't set any params or allocate anything
347 * @acquire: allocate the resources for the ringbuffer using the given spec
348 * @release: free resources of the ringbuffer
349 * @close_device: close the device
350 * @start: start processing of samples
351 * @pause: pause processing of samples
352 * @resume: resume processing of samples after pause
353 * @stop: stop processing of samples
354 * @delay: get number of samples queued in device
355 * @activate: activate the thread that starts pulling and monitoring the
356 * consumed segments in the device. Since 0.10.22
357 * @commit: write samples into the ringbuffer
358 * @clear_all: clear the entire ringbuffer Since 0.10.24
360 * The vmethods that subclasses can override to implement the ringbuffer.
362 struct _GstRingBufferClass {
363 GstObjectClass parent_class;
366 gboolean (*open_device) (GstRingBuffer *buf);
367 gboolean (*acquire) (GstRingBuffer *buf, GstRingBufferSpec *spec);
368 gboolean (*release) (GstRingBuffer *buf);
369 gboolean (*close_device) (GstRingBuffer *buf);
371 gboolean (*start) (GstRingBuffer *buf);
372 gboolean (*pause) (GstRingBuffer *buf);
373 gboolean (*resume) (GstRingBuffer *buf);
374 gboolean (*stop) (GstRingBuffer *buf);
376 guint (*delay) (GstRingBuffer *buf);
379 gboolean (*activate) (GstRingBuffer *buf, gboolean active);
381 guint (*commit) (GstRingBuffer * buf, guint64 *sample,
382 guchar * data, gint in_samples,
383 gint out_samples, gint * accum);
385 void (*clear_all) (GstRingBuffer * buf);
388 gpointer _gst_reserved[GST_PADDING - 3];
391 GType gst_ring_buffer_get_type(void);
394 void gst_ring_buffer_set_callback (GstRingBuffer *buf, GstRingBufferCallback cb,
397 gboolean gst_ring_buffer_parse_caps (GstRingBufferSpec *spec, GstCaps *caps);
398 void gst_ring_buffer_debug_spec_caps (GstRingBufferSpec *spec);
399 void gst_ring_buffer_debug_spec_buff (GstRingBufferSpec *spec);
401 gboolean gst_ring_buffer_convert (GstRingBuffer * buf, GstFormat src_fmt,
402 gint64 src_val, GstFormat dest_fmt,
406 gboolean gst_ring_buffer_open_device (GstRingBuffer *buf);
407 gboolean gst_ring_buffer_close_device (GstRingBuffer *buf);
409 gboolean gst_ring_buffer_device_is_open (GstRingBuffer *buf);
411 /* allocate resources */
412 gboolean gst_ring_buffer_acquire (GstRingBuffer *buf, GstRingBufferSpec *spec);
413 gboolean gst_ring_buffer_release (GstRingBuffer *buf);
415 gboolean gst_ring_buffer_is_acquired (GstRingBuffer *buf);
418 gboolean gst_ring_buffer_activate (GstRingBuffer *buf, gboolean active);
419 gboolean gst_ring_buffer_is_active (GstRingBuffer *buf);
422 void gst_ring_buffer_set_flushing (GstRingBuffer *buf, gboolean flushing);
425 gboolean gst_ring_buffer_start (GstRingBuffer *buf);
426 gboolean gst_ring_buffer_pause (GstRingBuffer *buf);
427 gboolean gst_ring_buffer_stop (GstRingBuffer *buf);
430 guint gst_ring_buffer_delay (GstRingBuffer *buf);
431 guint64 gst_ring_buffer_samples_done (GstRingBuffer *buf);
433 void gst_ring_buffer_set_sample (GstRingBuffer *buf, guint64 sample);
435 /* clear all segments */
436 void gst_ring_buffer_clear_all (GstRingBuffer *buf);
439 guint gst_ring_buffer_commit (GstRingBuffer *buf, guint64 sample,
440 guchar *data, guint len);
441 guint gst_ring_buffer_commit_full (GstRingBuffer * buf, guint64 *sample,
442 guchar * data, gint in_samples,
443 gint out_samples, gint * accum);
446 guint gst_ring_buffer_read (GstRingBuffer *buf, guint64 sample,
447 guchar *data, guint len);
449 /* mostly protected */
450 /* not yet implemented
451 gboolean gst_ring_buffer_prepare_write (GstRingBuffer *buf, gint *segment, guint8 **writeptr, gint *len);
453 gboolean gst_ring_buffer_prepare_read (GstRingBuffer *buf, gint *segment, guint8 **readptr, gint *len);
454 void gst_ring_buffer_clear (GstRingBuffer *buf, gint segment);
455 void gst_ring_buffer_advance (GstRingBuffer *buf, guint advance);
457 void gst_ring_buffer_may_start (GstRingBuffer *buf, gboolean allowed);
462 #endif /* __GST_RING_BUFFER_H__ */