summaryrefslogtreecommitdiff
path: root/gst-libs/gst/audio/gstringbuffer.c
blob: 07889db203056414cbbfcb0b3fe463364ae41786 (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
/* GStreamer
 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
 *
 * gstringbuffer.c: 
 *
 * 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.
 */

#include <string.h>

#include "gstringbuffer.h"

static void gst_ringbuffer_class_init (GstRingBufferClass * klass);
static void gst_ringbuffer_init (GstRingBuffer * ringbuffer);
static void gst_ringbuffer_dispose (GObject * object);
static void gst_ringbuffer_finalize (GObject * object);

static GstObjectClass *parent_class = NULL;

/* ringbuffer abstract base class */
GType
gst_ringbuffer_get_type (void)
{
  static GType ringbuffer_type = 0;

  if (!ringbuffer_type) {
    static const GTypeInfo ringbuffer_info = {
      sizeof (GstRingBufferClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_ringbuffer_class_init,
      NULL,
      NULL,
      sizeof (GstRingBuffer),
      0,
      (GInstanceInitFunc) gst_ringbuffer_init,
      NULL
    };

    ringbuffer_type = g_type_register_static (GST_TYPE_OBJECT, "GstRingBuffer",
        &ringbuffer_info, G_TYPE_FLAG_ABSTRACT);
  }
  return ringbuffer_type;
}

static void
gst_ringbuffer_class_init (GstRingBufferClass * klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;

  gobject_class = (GObjectClass *) klass;
  gstobject_class = (GstObjectClass *) klass;

  parent_class = g_type_class_ref (GST_TYPE_OBJECT);

  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ringbuffer_dispose);
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ringbuffer_finalize);
}

static void
gst_ringbuffer_init (GstRingBuffer * ringbuffer)
{
  ringbuffer->acquired = FALSE;
  ringbuffer->state = GST_RINGBUFFER_STATE_STOPPED;
  ringbuffer->playseg = 0;
  ringbuffer->writeseg = 1;
  ringbuffer->segfilled = 0;
  ringbuffer->waiters = FALSE;
  ringbuffer->cond = g_cond_new ();
}

static void
gst_ringbuffer_dispose (GObject * object)
{
  GstRingBuffer *ringbuffer = GST_RINGBUFFER (object);

  G_OBJECT_CLASS (parent_class)->dispose (G_OBJECT (ringbuffer));
}

static void
gst_ringbuffer_finalize (GObject * object)
{
  GstRingBuffer *ringbuffer = GST_RINGBUFFER (object);

  g_cond_free (ringbuffer->cond);

  G_OBJECT_CLASS (parent_class)->finalize (G_OBJECT (ringbuffer));
}

void
gst_ringbuffer_set_callback (GstRingBuffer * buf, GstRingBufferCallback cb,
    gpointer data)
{
  GST_LOCK (buf);
  buf->callback = cb;
  buf->cb_data = data;
  GST_UNLOCK (buf);
}


gboolean
gst_ringbuffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
{
  gboolean res = FALSE;
  GstRingBufferClass *rclass;

  GST_LOCK (buf);
  if (buf->acquired) {
    res = TRUE;
    goto done;
  }
  buf->acquired = TRUE;
  GST_UNLOCK (buf);

  rclass = GST_RINGBUFFER_GET_CLASS (buf);
  if (rclass->acquire)
    res = rclass->acquire (buf, spec);

  GST_LOCK (buf);
  if (!res) {
    buf->acquired = FALSE;
  }
done:
  GST_UNLOCK (buf);

  return res;
}

gboolean
gst_ringbuffer_release (GstRingBuffer * buf)
{
  gboolean res = FALSE;
  GstRingBufferClass *rclass;

  GST_LOCK (buf);
  if (!buf->acquired) {
    res = TRUE;
    goto done;
  }
  buf->acquired = FALSE;
  GST_UNLOCK (buf);

  rclass = GST_RINGBUFFER_GET_CLASS (buf);
  if (rclass->release)
    res = rclass->release (buf);

  GST_LOCK (buf);
  if (!res) {
    buf->acquired = TRUE;
  }
done:
  GST_UNLOCK (buf);

  return res;
}

gboolean
gst_ringbuffer_play (GstRingBuffer * buf)
{
  gboolean res = FALSE;
  GstRingBufferClass *rclass;

  GST_LOCK (buf);
  if (buf->state == GST_RINGBUFFER_STATE_PLAYING) {
    res = TRUE;
    goto done;
  }
  buf->state = GST_RINGBUFFER_STATE_PLAYING;

  rclass = GST_RINGBUFFER_GET_CLASS (buf);
  if (rclass->play)
    res = rclass->play (buf);

  if (!res) {
    buf->state = GST_RINGBUFFER_STATE_STOPPED;
  }
done:
  GST_UNLOCK (buf);

  return res;
}

gboolean
gst_ringbuffer_stop (GstRingBuffer * buf)
{
  gboolean res = FALSE;
  GstRingBufferClass *rclass;

  GST_LOCK (buf);
  if (buf->state == GST_RINGBUFFER_STATE_STOPPED) {
    res = TRUE;
    goto done;
  }
  buf->state = GST_RINGBUFFER_STATE_STOPPED;

  rclass = GST_RINGBUFFER_GET_CLASS (buf);
  if (rclass->stop)
    res = rclass->stop (buf);

  if (!res) {
    buf->state = GST_RINGBUFFER_STATE_PLAYING;
  }
done:
  GST_UNLOCK (buf);

  return res;
}

void
gst_ringbuffer_callback (GstRingBuffer * buf, guint advance)
{
  GST_LOCK (buf);
  buf->playseg = (buf->playseg + advance) % buf->spec.segtotal;
  if (buf->playseg == buf->writeseg) {
    g_print ("underrun!! read %d, write %d\n", buf->playseg, buf->writeseg);
    buf->writeseg = (buf->playseg + 1) % buf->spec.segtotal;
    buf->segfilled = 0;
  }
  if (buf->waiters)
    GST_RINGBUFFER_SIGNAL (buf);
  GST_UNLOCK (buf);

  if (buf->callback)
    buf->callback (buf, advance, buf->cb_data);
}

guint
gst_ringbuffer_write (GstRingBuffer * buf, GstClockTime time, guchar * data,
    guint len)
{
  guint towrite = len;
  guint written = 0;

  GST_LOCK (buf);
  /* we write the complete buffer */
  while (towrite > 0) {
    guint segavail;
    guint segwrite;

    /* we cannot write anymore since the buffer is filled, wait for 
     * some space to become available */
    while (buf->writeseg == buf->playseg) {
      buf->waiters = TRUE;
      GST_RINGBUFFER_WAIT (buf);
      buf->waiters = FALSE;
    }

    /* this is the available size now in the current segment */
    segavail = buf->spec.segsize - buf->segfilled;

    /* we write up to the available space */
    segwrite = MIN (segavail, towrite);
    memcpy (GST_BUFFER_DATA (buf->data) + buf->writeseg * buf->spec.segsize +
        buf->segfilled, data, segwrite);
    towrite -= segwrite;
    data += segwrite;
    buf->segfilled += segwrite;
    written += segwrite;
    /* we wrote a complete segment, advance the write pointer */
    if (buf->segfilled == buf->spec.segsize) {
      buf->writeseg = (buf->writeseg + 1) % buf->spec.segtotal;
      buf->segfilled = 0;
    }
  }
  GST_UNLOCK (buf);

  return written;
}