git stults online alsa-signals / master transfer.c
master

Tree @master (Download .tar.gz)

transfer.c @masterraw · history · blame

  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
283
284
285
286
287
288
289
290
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "transfer.h"

/*
 * Transfer method - write only
 */
static int write_loop(snd_pcm_t *handle,
		      signed short *samples,
		      snd_pcm_channel_area_t *areas,
		      void (*loopfn)(void *), void *args)
{
  double phase = 0;
  signed short *ptr;
  int err, cptr;
  while (1) {
    loopfn (args);
    //    generate_sine(areas, 0, period_size, &phase);
    ptr = samples;
    cptr = period_size;
    while (cptr > 0) {
      err = snd_pcm_writei(handle, ptr, cptr);
      if (err == -EAGAIN)
	continue;
      if (err < 0) {
	if (xrun_recovery(handle, err) < 0) {
	  printf("Write error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	break; /* skip one period */
      }
      ptr += err * channels;
      cptr -= err;
    }
  }
}

/*
 * Transfer method - write and wait for room in buffer using poll
 */
static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
{
  unsigned short revents;
  while (1) {
    poll(ufds, count, -1);
    snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
    if (revents & POLLERR)
      return -EIO;
    if (revents & POLLOUT)
      return 0;
  }
}
static int write_and_poll_loop(snd_pcm_t *handle,
			       signed short *samples,
			       snd_pcm_channel_area_t *areas,
			       void (*loopfn)(void *), void *args)
{
  struct pollfd *ufds;
  double phase = 0;
  signed short *ptr;
  int err, count, cptr, init;
  count = snd_pcm_poll_descriptors_count (handle);
  if (count <= 0) {
    printf("Invalid poll descriptors count\n");
    return count;
  }
  ufds = malloc(sizeof(struct pollfd) * count);
  if (ufds == NULL) {
    printf("No enough memory\n");
    return -ENOMEM;
  }
  if ((err = snd_pcm_poll_descriptors(handle, ufds, count)) < 0) {
    printf("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
    return err;
  }
  init = 1;
  while (1) {
    if (!init) {
      err = wait_for_poll(handle, ufds, count);
      if (err < 0) {
	if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
	    snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
	  err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
	  if (xrun_recovery(handle, err) < 0) {
	    printf("Write error: %s\n", snd_strerror(err));
	    exit(EXIT_FAILURE);
	  }
	  init = 1;
	} else {
	  printf("Wait for poll failed\n");
	  return err;
	}
      }
    }
    loopfn (args);
    //generate_sine(areas, 0, period_size, &phase);
    ptr = samples;
    cptr = period_size;
    while (cptr > 0) {
      err = snd_pcm_writei(handle, ptr, cptr);
      if (err < 0) {
	if (xrun_recovery(handle, err) < 0) {
	  printf("Write error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	init = 1;
	break; /* skip one period */
      }
      if (snd_pcm_state(handle) == SND_PCM_STATE_RUNNING)
	init = 0;
      ptr += err * channels;
      cptr -= err;
      if (cptr == 0)
	break;
      /* it is possible, that the initial buffer cannot store */
      /* all data from the last period, so wait awhile */
      err = wait_for_poll(handle, ufds, count);
      if (err < 0) {
	if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
	    snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
	  err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
	  if (xrun_recovery(handle, err) < 0) {
	    printf("Write error: %s\n", snd_strerror(err));
	    exit(EXIT_FAILURE);
	  }
	  init = 1;
	} else {
	  printf("Wait for poll failed\n");
	  return err;
	}
      }
    }
  }
}

/*
 * Transfer method - asynchronous notification
 */
struct async_private_data
{
  signed short *samples;
  snd_pcm_channel_area_t *areas;
  void (*loopfn)(void *);
  void *args;
  double phase;
};

static void async_callback(snd_async_handler_t *ahandler)
{
  snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
  struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
  signed short *samples = data->samples;
  snd_pcm_channel_area_t *areas = data->areas;
  snd_pcm_sframes_t avail;
  int err;
  avail = snd_pcm_avail_update(handle);
  while (avail >= period_size) {
    data->loopfn (data->args);
    //generate_sine(areas, 0, period_size, &data->phase);
    err = snd_pcm_writei(handle, samples, period_size);
    if (err < 0) {
      printf("Write error: %s\n", snd_strerror(err));
      exit(EXIT_FAILURE);
    }
    if (err != period_size) {
      printf("Write error: written %i expected %li\n", err, period_size);
      exit(EXIT_FAILURE);
    }
    avail = snd_pcm_avail_update(handle);
  }
}
static int async_loop(snd_pcm_t *handle,
		      signed short *samples,
		      snd_pcm_channel_area_t *areas,
		      void (*loopfn)(void *), void *args)
{
  struct async_private_data data;
  snd_async_handler_t *ahandler;
  int err, count;
  data.samples = samples;
  data.areas = areas;
  data.phase = 0;
  err = snd_async_add_pcm_handler(&ahandler, handle, async_callback, &data);
  if (err < 0) {
    printf("Unable to register async handler\n");
    exit(EXIT_FAILURE);
  }
  for (count = 0; count < 2; count++) {
    loopfn (args);
    //generate_sine(areas, 0, period_size, &data.phase);
    err = snd_pcm_writei(handle, samples, period_size);
    if (err < 0) {
      printf("Initial write error: %s\n", snd_strerror(err));
      exit(EXIT_FAILURE);
    }
    if (err != period_size) {
      printf("Initial write error: written %i expected %li\n", err, period_size);
      exit(EXIT_FAILURE);
    }
  }
  if (snd_pcm_state(handle) == SND_PCM_STATE_PREPARED) {
    err = snd_pcm_start(handle);
    if (err < 0) {
      printf("Start error: %s\n", snd_strerror(err));
      exit(EXIT_FAILURE);
    }
  }
  /* because all other work is done in the signal handler,
     suspend the process */
  while (1) {
    sleep(1);
  }
}
/*
 * Transfer method - asynchronous notification + direct write
 */
static void async_direct_callback(snd_async_handler_t *ahandler)
{
  snd_pcm_t *handle = snd_async_handler_get_pcm(ahandler);
  struct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
  const snd_pcm_channel_area_t *my_areas;
  snd_pcm_uframes_t offset, frames, size;
  snd_pcm_sframes_t avail, commitres;
  snd_pcm_state_t state;
  int first = 0, err;
  while (1) {
    state = snd_pcm_state(handle);
    if (state == SND_PCM_STATE_XRUN) {
      err = xrun_recovery(handle, -EPIPE);
      if (err < 0) {
	printf("XRUN recovery failed: %s\n", snd_strerror(err));
	exit(EXIT_FAILURE);
      }
      first = 1;
    } else if (state == SND_PCM_STATE_SUSPENDED) {
      err = xrun_recovery(handle, -ESTRPIPE);
      if (err < 0) {
	printf("SUSPEND recovery failed: %s\n", snd_strerror(err));
	exit(EXIT_FAILURE);
      }
    }
    avail = snd_pcm_avail_update(handle);
    if (avail < 0) {
      err = xrun_recovery(handle, avail);
      if (err < 0) {
	printf("avail update failed: %s\n", snd_strerror(err));
	exit(EXIT_FAILURE);
      }
      first = 1;
      continue;
    }
    if (avail < period_size) {
      if (first) {
	first = 0;
	err = snd_pcm_start(handle);
	if (err < 0) {
	  printf("Start error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
      } else {
	break;
      }
      continue;
    }
    size = period_size;
    while (size > 0) {
      frames = size;
      err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
      if (err < 0) {
	if ((err = xrun_recovery(handle, err)) < 0) {
	  printf("MMAP begin avail error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	first = 1;
      }
      data->loopfn (data->args);
      //generate_sine(my_areas, offset, frames, &data->phase);
      commitres = snd_pcm_mmap_commit(handle, offset, frames);
      if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
	if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
	  printf("MMAP commit error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	first = 1;
      }
      size -= frames;
    }
  }
}

static int async_direct_loop(snd_pcm_t *handle,
			     signed short *samples __attribute__((unused)),
			     snd_pcm_channel_area_t *areas __attribute__((unused)),
			     void (*loopfn)(void *), void *args)
{
  struct async_private_data data;
  snd_async_handler_t *ahandler;
  const snd_pcm_channel_area_t *my_areas;
  snd_pcm_uframes_t offset, frames, size;
  snd_pcm_sframes_t commitres;
  int err, count;
  data.samples = NULL; /* we do not require the global sample area for direct write */
  data.areas = NULL; /* we do not require the global areas for direct write */
  data.phase = 0;
  err = snd_async_add_pcm_handler(&ahandler, handle, async_direct_callback, &data);
  if (err < 0) {
    printf("Unable to register async handler\n");
    exit(EXIT_FAILURE);
  }
  for (count = 0; count < 2; count++) {
    size = period_size;
    while (size > 0) {
      frames = size;
      err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
      if (err < 0) {
	if ((err = xrun_recovery(handle, err)) < 0) {
	  printf("MMAP begin avail error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
      }
      loopfn (args);
      //      generate_sine(my_areas, offset, frames, &data.phase);
      commitres = snd_pcm_mmap_commit(handle, offset, frames);
      if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
	if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
	  printf("MMAP commit error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
      }
      size -= frames;
    }
  }
  err = snd_pcm_start(handle);
  if (err < 0) {
    printf("Start error: %s\n", snd_strerror(err));
    exit(EXIT_FAILURE);
  }
  /* because all other work is done in the signal handler,
     suspend the process */
  while (1) {
    sleep(1);
  }
}

/*
 * Transfer method - direct write only
 */
static int direct_loop(snd_pcm_t *handle,
		       signed short *samples __attribute__((unused)),
		       snd_pcm_channel_area_t *areas __attribute__((unused)),
		       void (*loopfn)(void *), void *args)
{
  double phase = 0;
  const snd_pcm_channel_area_t *my_areas;
  snd_pcm_uframes_t offset, frames, size;
  snd_pcm_sframes_t avail, commitres;
  snd_pcm_state_t state;
  int err, first = 1;
  while (1) {
    state = snd_pcm_state(handle);
    if (state == SND_PCM_STATE_XRUN) {
      err = xrun_recovery(handle, -EPIPE);
      if (err < 0) {
	printf("XRUN recovery failed: %s\n", snd_strerror(err));
	return err;
      }
      first = 1;
    } else if (state == SND_PCM_STATE_SUSPENDED) {
      err = xrun_recovery(handle, -ESTRPIPE);
      if (err < 0) {
	printf("SUSPEND recovery failed: %s\n", snd_strerror(err));
	return err;
      }
    }
    avail = snd_pcm_avail_update(handle);
    if (avail < 0) {
      err = xrun_recovery(handle, avail);
      if (err < 0) {
	printf("avail update failed: %s\n", snd_strerror(err));
	return err;
      }
      first = 1;
      continue;
    }
    if (avail < period_size) {
      if (first) {
	first = 0;
	err = snd_pcm_start(handle);
	if (err < 0) {
	  printf("Start error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
      } else {
	err = snd_pcm_wait(handle, -1);
	if (err < 0) {
	  if ((err = xrun_recovery(handle, err)) < 0) {
	    printf("snd_pcm_wait error: %s\n", snd_strerror(err));
	    exit(EXIT_FAILURE);
	  }
	  first = 1;
	}
      }
      continue;
    }
    size = period_size;
    while (size > 0) {
      frames = size;
      err = snd_pcm_mmap_begin(handle, &my_areas, &offset, &frames);
      if (err < 0) {
	if ((err = xrun_recovery(handle, err)) < 0) {
	  printf("MMAP begin avail error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	first = 1;
      }
      loopfn (args);
      //generate_sine(my_areas, offset, frames, &phase);
      commitres = snd_pcm_mmap_commit(handle, offset, frames);
      if (commitres < 0 || (snd_pcm_uframes_t)commitres != frames) {
	if ((err = xrun_recovery(handle, commitres >= 0 ? -EPIPE : commitres)) < 0) {
	  printf("MMAP commit error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	first = 1;
      }
      size -= frames;
    }
  }
}

/*
 * Transfer method - direct write only using mmap_write functions
 */
static int direct_write_loop(snd_pcm_t *handle,
			     signed short *samples,
			     snd_pcm_channel_area_t *areas,
			     void (*loopfn)(void *), void *args)
{
  double phase = 0;
  signed short *ptr;
  int err, cptr;
  while (1) {
    loopfn (args);
    //generate_sine(areas, 0, period_size, &phase);
    ptr = samples;
    cptr = period_size;
    while (cptr > 0) {
      err = snd_pcm_mmap_writei(handle, ptr, cptr);
      if (err == -EAGAIN)
	continue;
      if (err < 0) {
	if (xrun_recovery(handle, err) < 0) {
	  printf("Write error: %s\n", snd_strerror(err));
	  exit(EXIT_FAILURE);
	}
	break; /* skip one period */
      }
      ptr += err * channels;
      cptr -= err;
    }
  }
}

struct transfer_method_t TRANSFER_METHODS[] = {
  { "write", SND_PCM_ACCESS_RW_INTERLEAVED, write_loop },
  { "write_and_poll", SND_PCM_ACCESS_RW_INTERLEAVED, write_and_poll_loop },
  { "async", SND_PCM_ACCESS_RW_INTERLEAVED, async_loop },
  { "async_direct", SND_PCM_ACCESS_MMAP_INTERLEAVED, async_direct_loop },
  { "direct_interleaved", SND_PCM_ACCESS_MMAP_INTERLEAVED, direct_loop },
  { "direct_noninterleaved", SND_PCM_ACCESS_MMAP_NONINTERLEAVED, direct_loop },
  { "direct_write", SND_PCM_ACCESS_MMAP_INTERLEAVED, direct_write_loop },
  { NULL, SND_PCM_ACCESS_RW_INTERLEAVED, NULL }
};

transfer_method_t *transfer_method_create (const char *method)
{
  int curr_method;
  transfer_method_t *tm;
  /* look through list for transfer method */
  for (curr_method = 0; TRANSFER_METHODS[curr_method].name; ++curr_method)
    if (!strncasecmp(TRANSFER_METHODS[curr_method].name, method, 1024))
      break;
  /* didn't find it, return */
  if (TRANSFER_METHODS[curr_method].name == NULL)    
    return NULL;
  /* found it, factory go: */
  if ((tm = calloc (1, sizeof(transfer_method_t))) == NULL)
    return NULL;
  /* copy entry out of the struct */
  memcpy (tm, TRANSFER_METHODS + curr_method * sizeof(transfer_method_t), 
	  sizeof(transfer_method_t));
  return tm;
}

void transfer_method_destroy (transfer_method_t *tm)
{
  if (tm == NULL)
    return;
  free (tm);
  tm = NULL;
  return;
}