gstbaseaudiosrc.c 18.5 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2005 Wim Taymans <wim@fluendo.com>
 *
 * gstbaseaudiosrc.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.
 */

Wim Taymans's avatar
Wim Taymans committed
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * SECTION:gstbaseaudiosrc
 * @short_description: Base class for audio sources
 * @see_also: #GstAudioSrc, #GstRingBuffer.
 *
 * This is the base class for audio sources. Subclasses need to implement the
 * ::create_ringbuffer vmethod. This base class will then take care of
 * reading samples from the ringbuffer, synchronisation and flushing.
 *
 * Last reviewed on 2006-09-27 (0.10.12)
 */

Wim Taymans's avatar
Wim Taymans committed
35
36
37
38
#include <string.h>

#include "gstbaseaudiosrc.h"

39
40
GST_DEBUG_CATEGORY_STATIC (gst_base_audio_src_debug);
#define GST_CAT_DEFAULT gst_base_audio_src_debug
Wim Taymans's avatar
Wim Taymans committed
41
42
43
44
45
46
47
48

/* BaseAudioSrc signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

49
50
51
#define DEFAULT_BUFFER_TIME     ((200 * GST_MSECOND) / GST_USECOND)
#define DEFAULT_LATENCY_TIME    ((10 * GST_MSECOND) / GST_USECOND)

Wim Taymans's avatar
Wim Taymans committed
52
53
54
55
56
57
58
59
enum
{
  PROP_0,
  PROP_BUFFER_TIME,
  PROP_LATENCY_TIME,
};

#define _do_init(bla) \
60
    GST_DEBUG_CATEGORY_INIT (gst_base_audio_src_debug, "baseaudiosrc", 0, "baseaudiosrc element");
Wim Taymans's avatar
Wim Taymans committed
61

62
GST_BOILERPLATE_FULL (GstBaseAudioSrc, gst_base_audio_src, GstPushSrc,
63
    GST_TYPE_PUSH_SRC, _do_init);
Wim Taymans's avatar
Wim Taymans committed
64

65
static void gst_base_audio_src_set_property (GObject * object, guint prop_id,
Wim Taymans's avatar
Wim Taymans committed
66
    const GValue * value, GParamSpec * pspec);
67
static void gst_base_audio_src_get_property (GObject * object, guint prop_id,
Wim Taymans's avatar
Wim Taymans committed
68
69
    GValue * value, GParamSpec * pspec);

70
71
static GstStateChangeReturn gst_base_audio_src_change_state (GstElement *
    element, GstStateChange transition);
Wim Taymans's avatar
Wim Taymans committed
72

73
static GstClock *gst_base_audio_src_provide_clock (GstElement * elem);
74
75
static gboolean gst_base_audio_src_set_clock (GstElement * elem,
    GstClock * clock);
76
static GstClockTime gst_base_audio_src_get_time (GstClock * clock,
Wim Taymans's avatar
Wim Taymans committed
77
78
    GstBaseAudioSrc * src);

79
80
81
static GstFlowReturn gst_base_audio_src_create (GstBaseSrc * bsrc,
    guint64 offset, guint length, GstBuffer ** buf);
static gboolean gst_base_audio_src_check_get_range (GstBaseSrc * bsrc);
Wim Taymans's avatar
Wim Taymans committed
82

83
84
static gboolean gst_base_audio_src_event (GstBaseSrc * bsrc, GstEvent * event);
static void gst_base_audio_src_get_times (GstBaseSrc * bsrc,
Wim Taymans's avatar
Wim Taymans committed
85
    GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
86
static gboolean gst_base_audio_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
87
static void gst_base_audio_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
Wim Taymans's avatar
Wim Taymans committed
88

89
/* static guint gst_base_audio_src_signals[LAST_SIGNAL] = { 0 }; */
Wim Taymans's avatar
Wim Taymans committed
90
91

static void
92
gst_base_audio_src_base_init (gpointer g_class)
Wim Taymans's avatar
Wim Taymans committed
93
94
95
96
{
}

static void
97
gst_base_audio_src_class_init (GstBaseAudioSrcClass * klass)
Wim Taymans's avatar
Wim Taymans committed
98
99
100
101
102
103
104
105
106
107
108
109
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
  GstBaseSrcClass *gstbasesrc_class;
  GstPushSrcClass *gstpushsrc_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
  gstbasesrc_class = (GstBaseSrcClass *) klass;
  gstpushsrc_class = (GstPushSrcClass *) klass;

  gobject_class->set_property =
110
      GST_DEBUG_FUNCPTR (gst_base_audio_src_set_property);
Wim Taymans's avatar
Wim Taymans committed
111
  gobject_class->get_property =
112
      GST_DEBUG_FUNCPTR (gst_base_audio_src_get_property);
Wim Taymans's avatar
Wim Taymans committed
113

114
115
116
  g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
      g_param_spec_int64 ("buffer-time", "Buffer Time",
          "Size of audio buffer in microseconds", 1,
117
          G_MAXINT64, DEFAULT_BUFFER_TIME, G_PARAM_READWRITE));
118
119
120
121

  g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
      g_param_spec_int64 ("latency-time", "Latency Time",
          "Audio latency in microseconds", 1,
122
          G_MAXINT64, DEFAULT_LATENCY_TIME, G_PARAM_READWRITE));
Wim Taymans's avatar
Wim Taymans committed
123
124

  gstelement_class->change_state =
125
      GST_DEBUG_FUNCPTR (gst_base_audio_src_change_state);
126
127
  gstelement_class->provide_clock =
      GST_DEBUG_FUNCPTR (gst_base_audio_src_provide_clock);
128
129
  gstelement_class->set_clock =
      GST_DEBUG_FUNCPTR (gst_base_audio_src_set_clock);
Wim Taymans's avatar
Wim Taymans committed
130

131
132
133
134
  gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_base_audio_src_setcaps);
  gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_base_audio_src_event);
  gstbasesrc_class->get_times =
      GST_DEBUG_FUNCPTR (gst_base_audio_src_get_times);
135
136
137
  gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_base_audio_src_create);
  gstbasesrc_class->check_get_range =
      GST_DEBUG_FUNCPTR (gst_base_audio_src_check_get_range);
138
  gstbasesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_base_audio_src_fixate);
Wim Taymans's avatar
Wim Taymans committed
139
140
141
}

static void
142
143
gst_base_audio_src_init (GstBaseAudioSrc * baseaudiosrc,
    GstBaseAudioSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
144
145
146
{
  baseaudiosrc->buffer_time = DEFAULT_BUFFER_TIME;
  baseaudiosrc->latency_time = DEFAULT_LATENCY_TIME;
147
148
149
  /* reset blocksize we use latency time to calculate a more useful 
   * value based on negotiated format. */
  GST_BASE_SRC (baseaudiosrc)->blocksize = 0;
Wim Taymans's avatar
Wim Taymans committed
150
151

  baseaudiosrc->clock = gst_audio_clock_new ("clock",
152
      (GstAudioClockGetTimeFunc) gst_base_audio_src_get_time, baseaudiosrc);
Wim Taymans's avatar
Wim Taymans committed
153

154
  /* we are always a live source */
155
  gst_base_src_set_live (GST_BASE_SRC (baseaudiosrc), TRUE);
Wim Taymans's avatar
Wim Taymans committed
156
  /* we operate in time */
157
  gst_base_src_set_format (GST_BASE_SRC (baseaudiosrc), GST_FORMAT_TIME);
Wim Taymans's avatar
Wim Taymans committed
158
159
}

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
static gboolean
gst_base_audio_src_set_clock (GstElement * elem, GstClock * clock)
{
  GstBaseAudioSrc *src;

  src = GST_BASE_AUDIO_SRC (elem);

  /* FIXME, we cannot slave to another clock yet, better fail 
   * than to give a bad user experience (tm). */
  if (clock && clock != src->clock)
    goto wrong_clock;

  return TRUE;

  /* ERRORS */
wrong_clock:
  {
177
178
179
    /* no error message, this method is called with the parent
     * lock helt.. sigh.. long live recursive locks.. */
    GST_DEBUG_OBJECT (src, "Cannot operate with this clock.");
180
181
182
183
    return FALSE;
  }
}

Wim Taymans's avatar
Wim Taymans committed
184
static GstClock *
185
gst_base_audio_src_provide_clock (GstElement * elem)
Wim Taymans's avatar
Wim Taymans committed
186
187
{
  GstBaseAudioSrc *src;
188
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
189

190
  src = GST_BASE_AUDIO_SRC (elem);
Wim Taymans's avatar
Wim Taymans committed
191

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  /* we have no ringbuffer (must be NULL state) */
  if (src->ringbuffer == NULL)
    goto wrong_state;

  if (!gst_ring_buffer_is_acquired (src->ringbuffer))
    goto wrong_state;

  clock = GST_CLOCK_CAST (gst_object_ref (src->clock));

  return clock;

  /* ERRORS */
wrong_state:
  {
    GST_DEBUG_OBJECT (src, "ringbuffer not acquired");
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
209
210
211
}

static GstClockTime
212
gst_base_audio_src_get_time (GstClock * clock, GstBaseAudioSrc * src)
Wim Taymans's avatar
Wim Taymans committed
213
{
214
215
  guint64 raw, samples;
  guint delay;
Wim Taymans's avatar
Wim Taymans committed
216
217
  GstClockTime result;

218
219
  if (G_UNLIKELY (src->ringbuffer == NULL || src->ringbuffer->spec.rate == 0))
    return GST_CLOCK_TIME_NONE;
Wim Taymans's avatar
Wim Taymans committed
220

221
222
223
224
225
226
227
  raw = samples = gst_ring_buffer_samples_done (src->ringbuffer);

  /* the number of samples not yet processed, this is still queued in the
   * device (not yet read for capture). */
  delay = gst_ring_buffer_delay (src->ringbuffer);

  samples += delay;
Wim Taymans's avatar
Wim Taymans committed
228

229
230
  result = gst_util_uint64_scale_int (samples, GST_SECOND,
      src->ringbuffer->spec.rate);
Wim Taymans's avatar
Wim Taymans committed
231

232
233
234
235
  GST_DEBUG_OBJECT (src,
      "processed samples: raw %llu, delay %u, real %llu, time %"
      GST_TIME_FORMAT, raw, delay, samples, GST_TIME_ARGS (result));

Wim Taymans's avatar
Wim Taymans committed
236
237
238
  return result;
}

239
240
241
242
243
244
245
246
247
248
static gboolean
gst_base_audio_src_check_get_range (GstBaseSrc * bsrc)
{
  /* we allow limited pull base operation of which the details
   * will eventually exposed in an as of yet non-existing query.
   * Basically pulling can be done on any number of bytes as long
   * as the offset is -1 or sequentially increasing. */
  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
249
static void
250
gst_base_audio_src_set_property (GObject * object, guint prop_id,
Wim Taymans's avatar
Wim Taymans committed
251
252
253
254
    const GValue * value, GParamSpec * pspec)
{
  GstBaseAudioSrc *src;

255
  src = GST_BASE_AUDIO_SRC (object);
Wim Taymans's avatar
Wim Taymans committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

  switch (prop_id) {
    case PROP_BUFFER_TIME:
      src->buffer_time = g_value_get_int64 (value);
      break;
    case PROP_LATENCY_TIME:
      src->latency_time = g_value_get_int64 (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
271
272
gst_base_audio_src_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
Wim Taymans's avatar
Wim Taymans committed
273
274
275
{
  GstBaseAudioSrc *src;

276
  src = GST_BASE_AUDIO_SRC (object);
Wim Taymans's avatar
Wim Taymans committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

  switch (prop_id) {
    case PROP_BUFFER_TIME:
      g_value_set_int64 (value, src->buffer_time);
      break;
    case PROP_LATENCY_TIME:
      g_value_set_int64 (value, src->latency_time);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
292
gst_base_audio_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
Wim Taymans's avatar
Wim Taymans committed
293
294
{
  GstStructure *s;
295
  gint width, depth;
Wim Taymans's avatar
Wim Taymans committed
296
297
298

  s = gst_caps_get_structure (caps, 0);

299
  /* fields for all formats */
300
301
302
  gst_structure_fixate_field_nearest_int (s, "rate", 44100);
  gst_structure_fixate_field_nearest_int (s, "channels", 2);
  gst_structure_fixate_field_nearest_int (s, "width", 16);
303
304
305
306
307
308
309
310
311
312

  /* fields for int */
  if (gst_structure_has_field (s, "depth")) {
    gst_structure_get_int (s, "width", &width);
    /* round width to nearest multiple of 8 for the depth */
    depth = GST_ROUND_UP_8 (width);
    gst_structure_fixate_field_nearest_int (s, "depth", depth);
  }
  if (gst_structure_has_field (s, "signed"))
    gst_structure_fixate_field_boolean (s, "signed", TRUE);
313
  if (gst_structure_has_field (s, "endianness"))
314
    gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
Wim Taymans's avatar
Wim Taymans committed
315
316
317
}

static gboolean
318
gst_base_audio_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
Wim Taymans's avatar
Wim Taymans committed
319
{
320
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
321
322
323
324
325
326
327
  GstRingBufferSpec *spec;

  spec = &src->ringbuffer->spec;

  spec->buffer_time = src->buffer_time;
  spec->latency_time = src->latency_time;

328
  if (!gst_ring_buffer_parse_caps (spec, caps))
Wim Taymans's avatar
Wim Taymans committed
329
330
331
332
333
334
335
336
337
    goto parse_error;

  /* calculate suggested segsize and segtotal */
  spec->segsize =
      spec->rate * spec->bytes_per_sample * spec->latency_time / GST_MSECOND;
  spec->segtotal = spec->buffer_time / spec->latency_time;

  GST_DEBUG ("release old ringbuffer");

338
  gst_ring_buffer_release (src->ringbuffer);
Wim Taymans's avatar
Wim Taymans committed
339

340
  gst_ring_buffer_debug_spec_buff (spec);
Wim Taymans's avatar
Wim Taymans committed
341
342
343

  GST_DEBUG ("acquire new ringbuffer");

344
  if (!gst_ring_buffer_acquire (src->ringbuffer, spec))
Wim Taymans's avatar
Wim Taymans committed
345
346
347
348
349
350
351
352
353
    goto acquire_error;

  /* calculate actual latency and buffer times */
  spec->latency_time =
      spec->segsize * GST_MSECOND / (spec->rate * spec->bytes_per_sample);
  spec->buffer_time =
      spec->segtotal * spec->segsize * GST_MSECOND / (spec->rate *
      spec->bytes_per_sample);

354
  gst_ring_buffer_debug_spec_buff (spec);
Wim Taymans's avatar
Wim Taymans committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

  return TRUE;

  /* ERRORS */
parse_error:
  {
    GST_DEBUG ("could not parse caps");
    return FALSE;
  }
acquire_error:
  {
    GST_DEBUG ("could not acquire ringbuffer");
    return FALSE;
  }
}

static void
372
gst_base_audio_src_get_times (GstBaseSrc * bsrc, GstBuffer * buffer,
Wim Taymans's avatar
Wim Taymans committed
373
374
    GstClockTime * start, GstClockTime * end)
{
375
376
  /* no need to sync to a clock here, we schedule the samples based
   * on our own clock for the moment. */
Wim Taymans's avatar
Wim Taymans committed
377
378
379
380
381
  *start = GST_CLOCK_TIME_NONE;
  *end = GST_CLOCK_TIME_NONE;
}

static gboolean
382
gst_base_audio_src_event (GstBaseSrc * bsrc, GstEvent * event)
Wim Taymans's avatar
Wim Taymans committed
383
{
384
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
385
386

  switch (GST_EVENT_TYPE (event)) {
387
388
    case GST_EVENT_FLUSH_START:
      gst_ring_buffer_pause (src->ringbuffer);
389
      gst_ring_buffer_clear_all (src->ringbuffer);
390
391
      break;
    case GST_EVENT_FLUSH_STOP:
392
393
394
      /* always resync on sample after a flush */
      src->next_sample = -1;
      gst_ring_buffer_clear_all (src->ringbuffer);
Wim Taymans's avatar
Wim Taymans committed
395
396
397
398
399
400
401
      break;
    default:
      break;
  }
  return TRUE;
}

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
/* get the next offset in the ringbuffer for reading samples.
 * If the next sample is too far away, this function will position itself to the
 * next most recent sample, creating discontinuity */
static guint64
gst_base_audio_src_get_offset (GstBaseAudioSrc * src)
{
  guint64 sample;
  gint readseg, segdone, segtotal, sps;
  gint diff;

  /* assume we can append to the previous sample */
  sample = src->next_sample;
  /* no previous sample, try to read from position 0 */
  if (sample == -1)
    sample = 0;

  sps = src->ringbuffer->samples_per_seg;
  segtotal = src->ringbuffer->spec.segtotal;

  /* figure out the segment and the offset inside the segment where
   * the sample should be read from. */
  readseg = sample / sps;

  /* get the currently processed segment */
  segdone = g_atomic_int_get (&src->ringbuffer->segdone)
      - src->ringbuffer->segbase;

  /* see how far away it is from the read segment, normally segdone (where new
   * data is written in the ringbuffer) is bigger than readseg (where we are
   * reading). */
  diff = segdone - readseg;
  if (diff >= segtotal) {
    /* sample would be dropped, position to next playable position */
    sample = (segdone - segtotal + 1) * sps;
  }

  return sample;
}

Wim Taymans's avatar
Wim Taymans committed
441
static GstFlowReturn
442
443
gst_base_audio_src_create (GstBaseSrc * bsrc, guint64 offset, guint length,
    GstBuffer ** outbuf)
Wim Taymans's avatar
Wim Taymans committed
444
{
445
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
446
447
  GstBuffer *buf;
  guchar *data;
448
  guint samples;
449
  guint64 sample;
450
  gint bps;
451
  GstRingBuffer *ringbuffer;
452
  guint read;
453
454

  ringbuffer = src->ringbuffer;
Wim Taymans's avatar
Wim Taymans committed
455

456
  if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuffer)))
Wim Taymans's avatar
Wim Taymans committed
457
458
    goto wrong_state;

459
  bps = ringbuffer->spec.bytes_per_sample;
Wim Taymans's avatar
Wim Taymans committed
460

461
462
463
464
465
466
  if ((length == 0 && bsrc->blocksize == 0) || length == -1)
    /* no length given, use the default segment size */
    length = ringbuffer->spec.segsize;
  else
    /* make sure we round down to an integral number of samples */
    length -= length % bps;
Wim Taymans's avatar
Wim Taymans committed
467

468
  /* figure out the offset in the ringbuffer */
469
  if (G_UNLIKELY (offset != -1)) {
470
471
472
473
    sample = offset / bps;
    /* if a specific offset was given it must be the next sequential
     * offset we expect or we fail for now. */
    if (src->next_sample != -1 && sample != src->next_sample)
474
      goto wrong_offset;
475
476
477
478
  } else {
    /* calculate the sequentially next sample we need to read. This can jump and
     * create a DISCONT. */
    sample = gst_base_audio_src_get_offset (src);
479
480
  }

481
482
483
484
485
486
  /* get the number of samples to read */
  samples = length / bps;

  /* FIXME, using a bufferpool would be nice here */
  buf = gst_buffer_new_and_alloc (length);
  data = GST_BUFFER_DATA (buf);
Wim Taymans's avatar
Wim Taymans committed
487

488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
  do {
    read = gst_ring_buffer_read (ringbuffer, sample, data, samples);
    GST_DEBUG_OBJECT (src, "read %u of %u", read, samples);
    /* if we read all, we're done */
    if (read == samples)
      break;

    /* else something interrupted us and we wait for playing again. */
    if (gst_base_src_wait_playing (bsrc) != GST_FLOW_OK)
      goto stopped;

    /* read next samples */
    sample += read;
    samples -= read;
    data += read * bps;
  } while (TRUE);
Wim Taymans's avatar
Wim Taymans committed
504

505
506
507
508
509
510
511
512
  /* mark discontinuity if needed */
  if (G_UNLIKELY (sample != src->next_sample) && src->next_sample != -1) {
    GST_WARNING_OBJECT (src,
        "create DISCONT of %" G_GUINT64_FORMAT " samples at sample %"
        G_GUINT64_FORMAT, sample - src->next_sample, sample);
    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
  }

513
514
515
516
  /* FIXME, we timestamp against our own clock, also handle the case
   * where we are slaved to another clock. We currently refuse to accept
   * any other clock than the one we provide, so this code is fine for
   * now. */
517
518
  GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (sample,
      GST_SECOND, ringbuffer->spec.rate);
Wim Taymans's avatar
Wim Taymans committed
519
  src->next_sample = sample + samples;
520
521
  GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (src->next_sample,
      GST_SECOND, ringbuffer->spec.rate) - GST_BUFFER_TIMESTAMP (buf);
522
523
  GST_BUFFER_OFFSET (buf) = sample;
  GST_BUFFER_OFFSET_END (buf) = sample + samples;
524

525
  gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (bsrc)));
Wim Taymans's avatar
Wim Taymans committed
526
527
528
529
530

  *outbuf = buf;

  return GST_FLOW_OK;

531
  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
532
533
wrong_state:
  {
534
    GST_DEBUG_OBJECT (src, "ringbuffer in wrong state");
Wim Taymans's avatar
Wim Taymans committed
535
536
    return GST_FLOW_WRONG_STATE;
  }
537
538
539
540
541
542
543
wrong_offset:
  {
    GST_ELEMENT_ERROR (src, RESOURCE, SEEK,
        (NULL), ("resource can only be operated on sequentially but offset %"
            G_GUINT64_FORMAT " was given", offset));
    return GST_FLOW_ERROR;
  }
Wim Taymans's avatar
Wim Taymans committed
544
545
546
stopped:
  {
    gst_buffer_unref (buf);
547
    GST_DEBUG_OBJECT (src, "ringbuffer stopped");
Wim Taymans's avatar
Wim Taymans committed
548
549
550
551
    return GST_FLOW_WRONG_STATE;
  }
}

Wim Taymans's avatar
Wim Taymans committed
552
553
554
555
556
557
558
559
560
561
/**
 * gst_base_audio_src_create_ringbuffer:
 * @src: a #GstBaseAudioSrc.
 *
 * Create and return the #GstRingBuffer for @src. This function will call the
 * ::create_ringbuffer vmethod and will set @src as the parent of the returned
 * buffer (see gst_object_set_parent()).
 *
 * Returns: The new ringbuffer of @src.
 */
Wim Taymans's avatar
Wim Taymans committed
562
GstRingBuffer *
563
gst_base_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
Wim Taymans's avatar
Wim Taymans committed
564
565
566
567
{
  GstBaseAudioSrcClass *bclass;
  GstRingBuffer *buffer = NULL;

568
  bclass = GST_BASE_AUDIO_SRC_GET_CLASS (src);
Wim Taymans's avatar
Wim Taymans committed
569
570
571
  if (bclass->create_ringbuffer)
    buffer = bclass->create_ringbuffer (src);

572
573
  if (G_LIKELY (buffer))
    gst_object_set_parent (GST_OBJECT_CAST (buffer), GST_OBJECT_CAST (src));
Wim Taymans's avatar
Wim Taymans committed
574
575
576
577

  return buffer;
}

578
579
580
static GstStateChangeReturn
gst_base_audio_src_change_state (GstElement * element,
    GstStateChange transition)
Wim Taymans's avatar
Wim Taymans committed
581
{
582
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
583
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (element);
Wim Taymans's avatar
Wim Taymans committed
584
585

  switch (transition) {
586
    case GST_STATE_CHANGE_NULL_TO_READY:
587
588
589
590
      if (src->ringbuffer == NULL) {
        src->ringbuffer = gst_base_audio_src_create_ringbuffer (src);
      }
      if (!gst_ring_buffer_open_device (src->ringbuffer))
591
        return GST_STATE_CHANGE_FAILURE;
592
      src->next_sample = -1;
Wim Taymans's avatar
Wim Taymans committed
593
      break;
594
    case GST_STATE_CHANGE_READY_TO_PAUSED:
595
      gst_ring_buffer_set_flushing (src->ringbuffer, FALSE);
Wim Taymans's avatar
Wim Taymans committed
596
      break;
597
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
598
      gst_ring_buffer_may_start (src->ringbuffer, TRUE);
Wim Taymans's avatar
Wim Taymans committed
599
600
601
602
603
      break;
    default:
      break;
  }

604
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
Wim Taymans's avatar
Wim Taymans committed
605
606

  switch (transition) {
607
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
608
      gst_ring_buffer_may_start (src->ringbuffer, FALSE);
609
      gst_ring_buffer_pause (src->ringbuffer);
Wim Taymans's avatar
Wim Taymans committed
610
      break;
611
    case GST_STATE_CHANGE_PAUSED_TO_READY:
612
      gst_ring_buffer_set_flushing (src->ringbuffer, TRUE);
613
      gst_ring_buffer_release (src->ringbuffer);
614
      src->next_sample = -1;
Wim Taymans's avatar
Wim Taymans committed
615
      break;
616
    case GST_STATE_CHANGE_READY_TO_NULL:
617
      gst_ring_buffer_close_device (src->ringbuffer);
618
      gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
619
      src->ringbuffer = NULL;
Wim Taymans's avatar
Wim Taymans committed
620
621
622
623
624
625
626
      break;
    default:
      break;
  }

  return ret;
}