gstbaseaudiosrc.c 23.6 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)
 */

35
36
37
38
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

Wim Taymans's avatar
Wim Taymans committed
39
40
41
42
#include <string.h>

#include "gstbaseaudiosrc.h"

43
44
#include "gst/gst-i18n-plugin.h"

45
46
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
47

48
49
50
51
52
53
54
55
#define GST_BASE_AUDIO_SRC_GET_PRIVATE(obj)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_SRC, GstBaseAudioSrcPrivate))

struct _GstBaseAudioSrcPrivate
{
  gboolean provide_clock;
};

Wim Taymans's avatar
Wim Taymans committed
56
57
58
59
60
61
62
/* BaseAudioSrc signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

63
64
#define DEFAULT_BUFFER_TIME     ((200 * GST_MSECOND) / GST_USECOND)
#define DEFAULT_LATENCY_TIME    ((10 * GST_MSECOND) / GST_USECOND)
65
#define DEFAULT_PROVIDE_CLOCK   TRUE
66

Wim Taymans's avatar
Wim Taymans committed
67
68
69
70
71
enum
{
  PROP_0,
  PROP_BUFFER_TIME,
  PROP_LATENCY_TIME,
72
  PROP_PROVIDE_CLOCK
Wim Taymans's avatar
Wim Taymans committed
73
74
};

75
76
77
78
79
80
81
82
83
84
85
86
static void
_do_init (GType type)
{
  GST_DEBUG_CATEGORY_INIT (gst_base_audio_src_debug, "baseaudiosrc", 0,
      "baseaudiosrc element");

#ifdef ENABLE_NLS
  GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
      LOCALEDIR);
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
#endif /* ENABLE_NLS */
}
Wim Taymans's avatar
Wim Taymans committed
87

88
GST_BOILERPLATE_FULL (GstBaseAudioSrc, gst_base_audio_src, GstPushSrc,
89
    GST_TYPE_PUSH_SRC, _do_init);
Wim Taymans's avatar
Wim Taymans committed
90

91
static void gst_base_audio_src_set_property (GObject * object, guint prop_id,
Wim Taymans's avatar
Wim Taymans committed
92
    const GValue * value, GParamSpec * pspec);
93
static void gst_base_audio_src_get_property (GObject * object, guint prop_id,
Wim Taymans's avatar
Wim Taymans committed
94
    GValue * value, GParamSpec * pspec);
95
static void gst_base_audio_src_dispose (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
96

97
98
static GstStateChangeReturn gst_base_audio_src_change_state (GstElement *
    element, GstStateChange transition);
Wim Taymans's avatar
Wim Taymans committed
99

100
static GstClock *gst_base_audio_src_provide_clock (GstElement * elem);
101
static GstClockTime gst_base_audio_src_get_time (GstClock * clock,
Wim Taymans's avatar
Wim Taymans committed
102
103
    GstBaseAudioSrc * src);

104
105
106
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
107

108
109
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
110
    GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
111
static gboolean gst_base_audio_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
112
static gboolean gst_base_audio_src_query (GstBaseSrc * bsrc, GstQuery * query);
113
static void gst_base_audio_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
Wim Taymans's avatar
Wim Taymans committed
114

115
/* static guint gst_base_audio_src_signals[LAST_SIGNAL] = { 0 }; */
Wim Taymans's avatar
Wim Taymans committed
116
117

static void
118
gst_base_audio_src_base_init (gpointer g_class)
Wim Taymans's avatar
Wim Taymans committed
119
120
121
122
{
}

static void
123
gst_base_audio_src_class_init (GstBaseAudioSrcClass * klass)
Wim Taymans's avatar
Wim Taymans committed
124
125
126
127
128
129
130
131
132
133
134
{
  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;

135
136
  g_type_class_add_private (klass, sizeof (GstBaseAudioSrcPrivate));

Wim Taymans's avatar
Wim Taymans committed
137
  gobject_class->set_property =
138
      GST_DEBUG_FUNCPTR (gst_base_audio_src_set_property);
Wim Taymans's avatar
Wim Taymans committed
139
  gobject_class->get_property =
140
      GST_DEBUG_FUNCPTR (gst_base_audio_src_get_property);
141
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_base_audio_src_dispose);
Wim Taymans's avatar
Wim Taymans committed
142

143
144
145
  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,
146
          G_MAXINT64, DEFAULT_BUFFER_TIME, G_PARAM_READWRITE));
147
148
149
150

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

153
154
155
156
157
  g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
      g_param_spec_boolean ("provide-clock", "Provide Clock",
          "Provide a clock to be used as the global pipeline clock",
          DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE));

Wim Taymans's avatar
Wim Taymans committed
158
  gstelement_class->change_state =
159
      GST_DEBUG_FUNCPTR (gst_base_audio_src_change_state);
160
161
  gstelement_class->provide_clock =
      GST_DEBUG_FUNCPTR (gst_base_audio_src_provide_clock);
Wim Taymans's avatar
Wim Taymans committed
162

163
164
  gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_base_audio_src_setcaps);
  gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_base_audio_src_event);
165
  gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_base_audio_src_query);
166
167
  gstbasesrc_class->get_times =
      GST_DEBUG_FUNCPTR (gst_base_audio_src_get_times);
168
169
170
  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);
171
  gstbasesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_base_audio_src_fixate);
Wim Taymans's avatar
Wim Taymans committed
172
173
174
}

static void
175
176
gst_base_audio_src_init (GstBaseAudioSrc * baseaudiosrc,
    GstBaseAudioSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
177
{
178
179
  baseaudiosrc->priv = GST_BASE_AUDIO_SRC_GET_PRIVATE (baseaudiosrc);

Wim Taymans's avatar
Wim Taymans committed
180
181
  baseaudiosrc->buffer_time = DEFAULT_BUFFER_TIME;
  baseaudiosrc->latency_time = DEFAULT_LATENCY_TIME;
182
  baseaudiosrc->priv->provide_clock = DEFAULT_PROVIDE_CLOCK;
183
184
185
  /* 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
186

187
  baseaudiosrc->clock = gst_audio_clock_new ("GstAudioSrcClock",
188
      (GstAudioClockGetTimeFunc) gst_base_audio_src_get_time, baseaudiosrc);
Wim Taymans's avatar
Wim Taymans committed
189

190
  /* we are always a live source */
191
  gst_base_src_set_live (GST_BASE_SRC (baseaudiosrc), TRUE);
Wim Taymans's avatar
Wim Taymans committed
192
  /* we operate in time */
193
  gst_base_src_set_format (GST_BASE_SRC (baseaudiosrc), GST_FORMAT_TIME);
Wim Taymans's avatar
Wim Taymans committed
194
195
}

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
static void
gst_base_audio_src_dispose (GObject * object)
{
  GstBaseAudioSrc *src;

  src = GST_BASE_AUDIO_SRC (object);

  if (src->clock)
    gst_object_unref (src->clock);
  src->clock = NULL;

  if (src->ringbuffer) {
    gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
    src->ringbuffer = NULL;
  }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

Wim Taymans's avatar
Wim Taymans committed
215
static GstClock *
216
gst_base_audio_src_provide_clock (GstElement * elem)
Wim Taymans's avatar
Wim Taymans committed
217
218
{
  GstBaseAudioSrc *src;
219
  GstClock *clock;
Wim Taymans's avatar
Wim Taymans committed
220

221
  src = GST_BASE_AUDIO_SRC (elem);
Wim Taymans's avatar
Wim Taymans committed
222

223
224
225
226
227
228
229
  /* 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;

230
231
232
233
  GST_OBJECT_LOCK (src);
  if (!src->priv->provide_clock)
    goto clock_disabled;

234
  clock = GST_CLOCK_CAST (gst_object_ref (src->clock));
235
  GST_OBJECT_UNLOCK (src);
236
237
238
239
240
241
242
243
244

  return clock;

  /* ERRORS */
wrong_state:
  {
    GST_DEBUG_OBJECT (src, "ringbuffer not acquired");
    return NULL;
  }
245
246
247
248
249
250
clock_disabled:
  {
    GST_DEBUG_OBJECT (src, "clock provide disabled");
    GST_OBJECT_UNLOCK (src);
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
251
252
253
}

static GstClockTime
254
gst_base_audio_src_get_time (GstClock * clock, GstBaseAudioSrc * src)
Wim Taymans's avatar
Wim Taymans committed
255
{
256
257
  guint64 raw, samples;
  guint delay;
Wim Taymans's avatar
Wim Taymans committed
258
259
  GstClockTime result;

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

263
264
265
266
267
268
269
  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
270

271
272
  result = gst_util_uint64_scale_int (samples, GST_SECOND,
      src->ringbuffer->spec.rate);
Wim Taymans's avatar
Wim Taymans committed
273

274
275
276
277
  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
278
279
280
  return result;
}

281
282
283
284
285
286
287
288
289
290
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;
}

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
 * gst_base_audio_src_set_provide_clock:
 * @src: a #GstBaseAudioSrc
 * @provide: new state
 *
 * Controls whether @src will provide a clock or not. If @provide is %TRUE, 
 * gst_element_provide_clock() will return a clock that reflects the datarate
 * of @src. If @provide is %FALSE, gst_element_provide_clock() will return NULL.
 *
 * Since: 0.10.16
 */
void
gst_base_audio_src_set_provide_clock (GstBaseAudioSrc * src, gboolean provide)
{
  g_return_if_fail (GST_IS_BASE_AUDIO_SRC (src));

  GST_OBJECT_LOCK (src);
  src->priv->provide_clock = provide;
  GST_OBJECT_UNLOCK (src);
}

/**
 * gst_base_audio_src_get_provide_clock:
 * @src: a #GstBaseAudioSrc
 *
 * Queries whether @src will provide a clock or not. See also
 * gst_base_audio_src_set_provide_clock.
 *
 * Returns: %TRUE if @src will provide a clock.
 *
 * Since: 0.10.16
 */
gboolean
gst_base_audio_src_get_provide_clock (GstBaseAudioSrc * src)
{
  gboolean result;

  g_return_val_if_fail (GST_IS_BASE_AUDIO_SRC (src), FALSE);

  GST_OBJECT_LOCK (src);
  result = src->priv->provide_clock;
  GST_OBJECT_UNLOCK (src);

  return result;
}

Wim Taymans's avatar
Wim Taymans committed
337
static void
338
gst_base_audio_src_set_property (GObject * object, guint prop_id,
Wim Taymans's avatar
Wim Taymans committed
339
340
341
342
    const GValue * value, GParamSpec * pspec)
{
  GstBaseAudioSrc *src;

343
  src = GST_BASE_AUDIO_SRC (object);
Wim Taymans's avatar
Wim Taymans committed
344
345
346
347
348
349
350
351

  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;
352
    case PROP_PROVIDE_CLOCK:
353
      gst_base_audio_src_set_provide_clock (src, g_value_get_boolean (value));
354
      break;
Wim Taymans's avatar
Wim Taymans committed
355
356
357
358
359
360
361
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
362
363
gst_base_audio_src_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
Wim Taymans's avatar
Wim Taymans committed
364
365
366
{
  GstBaseAudioSrc *src;

367
  src = GST_BASE_AUDIO_SRC (object);
Wim Taymans's avatar
Wim Taymans committed
368
369
370
371
372
373
374
375

  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;
376
    case PROP_PROVIDE_CLOCK:
377
      g_value_set_boolean (value, gst_base_audio_src_get_provide_clock (src));
378
      break;
Wim Taymans's avatar
Wim Taymans committed
379
380
381
382
383
384
385
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
386
gst_base_audio_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
Wim Taymans's avatar
Wim Taymans committed
387
388
{
  GstStructure *s;
389
  gint width, depth;
Wim Taymans's avatar
Wim Taymans committed
390
391
392

  s = gst_caps_get_structure (caps, 0);

393
  /* fields for all formats */
394
395
396
  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);
397
398
399
400
401
402
403
404
405
406

  /* 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);
407
  if (gst_structure_has_field (s, "endianness"))
408
    gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
Wim Taymans's avatar
Wim Taymans committed
409
410
411
}

static gboolean
412
gst_base_audio_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
Wim Taymans's avatar
Wim Taymans committed
413
{
414
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
415
416
417
418
419
420
421
  GstRingBufferSpec *spec;

  spec = &src->ringbuffer->spec;

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

422
  if (!gst_ring_buffer_parse_caps (spec, caps))
Wim Taymans's avatar
Wim Taymans committed
423
424
425
426
427
428
429
430
431
    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");

432
  gst_ring_buffer_release (src->ringbuffer);
Wim Taymans's avatar
Wim Taymans committed
433

434
  gst_ring_buffer_debug_spec_buff (spec);
Wim Taymans's avatar
Wim Taymans committed
435
436
437

  GST_DEBUG ("acquire new ringbuffer");

438
  if (!gst_ring_buffer_acquire (src->ringbuffer, spec))
Wim Taymans's avatar
Wim Taymans committed
439
440
441
442
443
444
445
446
447
    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);

448
  gst_ring_buffer_debug_spec_buff (spec);
Wim Taymans's avatar
Wim Taymans committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

  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
466
gst_base_audio_src_get_times (GstBaseSrc * bsrc, GstBuffer * buffer,
Wim Taymans's avatar
Wim Taymans committed
467
468
    GstClockTime * start, GstClockTime * end)
{
469
470
  /* 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
471
472
473
474
  *start = GST_CLOCK_TIME_NONE;
  *end = GST_CLOCK_TIME_NONE;
}

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
static gboolean
gst_base_audio_src_query (GstBaseSrc * bsrc, GstQuery * query)
{
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
  gboolean res = FALSE;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_LATENCY:
    {
      GstClockTime min_latency, max_latency;
      GstRingBufferSpec *spec;

      if (G_UNLIKELY (src->ringbuffer == NULL
              || src->ringbuffer->spec.rate == 0))
        goto done;

      spec = &src->ringbuffer->spec;

493
      /* we have at least 1 segment of latency */
494
495
496
      min_latency =
          gst_util_uint64_scale_int (spec->segsize, GST_SECOND,
          spec->rate * spec->bytes_per_sample);
497
      /* we cannot delay more than the buffersize else we lose data */
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
      max_latency =
          gst_util_uint64_scale_int (spec->segtotal * spec->segsize, GST_SECOND,
          spec->rate * spec->bytes_per_sample);

      GST_DEBUG_OBJECT (src,
          "report latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
          GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));

      /* we are always live, the min latency is 1 segment and the max latency is
       * the complete buffer of segments. */
      gst_query_set_latency (query, TRUE, min_latency, max_latency);

      res = TRUE;
      break;
    }
    default:
      res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
      break;
  }
done:
  return res;
}

Wim Taymans's avatar
Wim Taymans committed
521
static gboolean
522
gst_base_audio_src_event (GstBaseSrc * bsrc, GstEvent * event)
Wim Taymans's avatar
Wim Taymans committed
523
{
524
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
525
526

  switch (GST_EVENT_TYPE (event)) {
527
528
    case GST_EVENT_FLUSH_START:
      gst_ring_buffer_pause (src->ringbuffer);
529
      gst_ring_buffer_clear_all (src->ringbuffer);
530
531
      break;
    case GST_EVENT_FLUSH_STOP:
532
533
534
      /* 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
535
536
537
538
539
540
541
      break;
    default:
      break;
  }
  return TRUE;
}

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* 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;

569
570
  GST_DEBUG_OBJECT (src, "reading from %d, we are at %d", readseg, segdone);

571
572
573
574
575
  /* 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) {
576
    GST_DEBUG_OBJECT (src, "dropped, align to segment %d", segdone);
577
578
579
580
581
582
583
    /* sample would be dropped, position to next playable position */
    sample = (segdone - segtotal + 1) * sps;
  }

  return sample;
}

Wim Taymans's avatar
Wim Taymans committed
584
static GstFlowReturn
585
586
gst_base_audio_src_create (GstBaseSrc * bsrc, guint64 offset, guint length,
    GstBuffer ** outbuf)
Wim Taymans's avatar
Wim Taymans committed
587
{
588
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (bsrc);
Wim Taymans's avatar
Wim Taymans committed
589
590
  GstBuffer *buf;
  guchar *data;
591
  guint samples, total_samples;
592
  guint64 sample;
593
  gint bps;
594
  GstRingBuffer *ringbuffer;
595
  GstRingBufferSpec *spec;
596
  guint read;
Wim Taymans's avatar
Wim Taymans committed
597
  GstClockTime timestamp;
598
  GstClock *clock;
599
600

  ringbuffer = src->ringbuffer;
601
  spec = &ringbuffer->spec;
Wim Taymans's avatar
Wim Taymans committed
602

603
  if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuffer)))
Wim Taymans's avatar
Wim Taymans committed
604
605
    goto wrong_state;

606
  bps = spec->bytes_per_sample;
Wim Taymans's avatar
Wim Taymans committed
607

608
609
  if ((length == 0 && bsrc->blocksize == 0) || length == -1)
    /* no length given, use the default segment size */
610
    length = spec->segsize;
611
612
613
  else
    /* make sure we round down to an integral number of samples */
    length -= length % bps;
Wim Taymans's avatar
Wim Taymans committed
614

615
  /* figure out the offset in the ringbuffer */
616
  if (G_UNLIKELY (offset != -1)) {
617
618
619
620
    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)
621
      goto wrong_offset;
622
623
624
625
  } 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);
626
627
  }

Wim Taymans's avatar
Wim Taymans committed
628
629
  GST_DEBUG_OBJECT (src, "reading from sample %" G_GUINT64_FORMAT, sample);

630
  /* get the number of samples to read */
631
  total_samples = samples = length / bps;
632
633
634
635

  /* 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
636

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  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
653

654
655
656
657
658
  /* 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);
659
660
    GST_ELEMENT_WARNING (src, CORE, CLOCK,
        (_("Can't record audio fast enough")),
661
        ("dropped %" G_GUINT64_FORMAT " samples", sample - src->next_sample));
662
663
664
    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
  }

665
666
  GST_OBJECT_LOCK (src);
  clock = GST_ELEMENT_CLOCK (src);
667
668
669
  if (clock == NULL || clock == src->clock) {
    /* timestamp against our own clock. We do this also when no external clock
     * was provided to us. */
670
671
672
673
    timestamp = gst_util_uint64_scale_int (sample, GST_SECOND, spec->rate);
  } else {
    GstClockTime base_time, latency;

674
675
676
    /* We are slaved to another clock, take running time of the clock and just
     * timestamp against it. Somebody else in the pipeline should figure out the
     * clock drift, for now. */
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
    timestamp = gst_clock_get_time (clock);
    base_time = GST_ELEMENT_CAST (src)->base_time;

    if (timestamp > base_time)
      timestamp -= base_time;
    else
      timestamp = 0;

    /* subtract latency */
    latency = gst_util_uint64_scale_int (total_samples, GST_SECOND, spec->rate);
    if (timestamp > latency)
      timestamp -= latency;
    else
      timestamp = 0;
  }
  GST_OBJECT_UNLOCK (src);
Wim Taymans's avatar
Wim Taymans committed
693
694

  GST_BUFFER_TIMESTAMP (buf) = timestamp;
Wim Taymans's avatar
Wim Taymans committed
695
  src->next_sample = sample + samples;
696
  GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (src->next_sample,
697
      GST_SECOND, spec->rate) - GST_BUFFER_TIMESTAMP (buf);
698
699
  GST_BUFFER_OFFSET (buf) = sample;
  GST_BUFFER_OFFSET_END (buf) = sample + samples;
700

701
  gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (bsrc)));
Wim Taymans's avatar
Wim Taymans committed
702
703
704
705
706

  *outbuf = buf;

  return GST_FLOW_OK;

707
  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
708
709
wrong_state:
  {
710
    GST_DEBUG_OBJECT (src, "ringbuffer in wrong state");
Wim Taymans's avatar
Wim Taymans committed
711
712
    return GST_FLOW_WRONG_STATE;
  }
713
714
715
716
717
718
719
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
720
721
722
stopped:
  {
    gst_buffer_unref (buf);
723
    GST_DEBUG_OBJECT (src, "ringbuffer stopped");
Wim Taymans's avatar
Wim Taymans committed
724
725
726
727
    return GST_FLOW_WRONG_STATE;
  }
}

Wim Taymans's avatar
Wim Taymans committed
728
729
730
731
732
733
734
735
736
737
/**
 * 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
738
GstRingBuffer *
739
gst_base_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
Wim Taymans's avatar
Wim Taymans committed
740
741
742
743
{
  GstBaseAudioSrcClass *bclass;
  GstRingBuffer *buffer = NULL;

744
  bclass = GST_BASE_AUDIO_SRC_GET_CLASS (src);
Wim Taymans's avatar
Wim Taymans committed
745
746
747
  if (bclass->create_ringbuffer)
    buffer = bclass->create_ringbuffer (src);

748
749
  if (G_LIKELY (buffer))
    gst_object_set_parent (GST_OBJECT_CAST (buffer), GST_OBJECT_CAST (src));
Wim Taymans's avatar
Wim Taymans committed
750
751
752
753

  return buffer;
}

754
755
756
static GstStateChangeReturn
gst_base_audio_src_change_state (GstElement * element,
    GstStateChange transition)
Wim Taymans's avatar
Wim Taymans committed
757
{
758
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
759
  GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (element);
Wim Taymans's avatar
Wim Taymans committed
760
761

  switch (transition) {
762
    case GST_STATE_CHANGE_NULL_TO_READY:
763
764
765
766
      if (src->ringbuffer == NULL) {
        src->ringbuffer = gst_base_audio_src_create_ringbuffer (src);
      }
      if (!gst_ring_buffer_open_device (src->ringbuffer))
767
        goto open_failed;
Wim Taymans's avatar
Wim Taymans committed
768
      break;
769
    case GST_STATE_CHANGE_READY_TO_PAUSED:
770
      src->next_sample = -1;
771
      gst_ring_buffer_set_flushing (src->ringbuffer, FALSE);
Wim Taymans's avatar
Wim Taymans committed
772
      break;
773
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
774
      gst_ring_buffer_may_start (src->ringbuffer, TRUE);
Wim Taymans's avatar
Wim Taymans committed
775
      break;
776
777
778
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_ring_buffer_set_flushing (src->ringbuffer, TRUE);
      break;
Wim Taymans's avatar
Wim Taymans committed
779
780
781
782
    default:
      break;
  }

783
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
Wim Taymans's avatar
Wim Taymans committed
784
785

  switch (transition) {
786
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
787
      gst_ring_buffer_may_start (src->ringbuffer, FALSE);
788
      gst_ring_buffer_pause (src->ringbuffer);
Wim Taymans's avatar
Wim Taymans committed
789
      break;
790
    case GST_STATE_CHANGE_PAUSED_TO_READY:
791
      gst_ring_buffer_release (src->ringbuffer);
Wim Taymans's avatar
Wim Taymans committed
792
      break;
793
    case GST_STATE_CHANGE_READY_TO_NULL:
794
      gst_ring_buffer_close_device (src->ringbuffer);
795
      gst_object_unparent (GST_OBJECT_CAST (src->ringbuffer));
796
      src->ringbuffer = NULL;
Wim Taymans's avatar
Wim Taymans committed
797
798
799
800
801
802
      break;
    default:
      break;
  }

  return ret;
803
804
805
806
807
808
809
810
811

  /* ERRORS */
open_failed:
  {
    /* subclass must post a meaningfull error message */
    GST_DEBUG_OBJECT (src, "open failed");
    return GST_STATE_CHANGE_FAILURE;
  }

Wim Taymans's avatar
Wim Taymans committed
812
}