source: roaraudio/plugins/alsavs/pcm_roar.c @ 5166:ab81dec0e209

Last change on this file since 5166:ab81dec0e209 was 5166:ab81dec0e209, checked in by phi, 12 years ago

also ignore hint subsection

File size: 14.4 KB
Line 
1//pcm_roar.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *      Copyright (C) Hans-Kristian 'maister' Arntzen - 2010
6 *
7 *  This file is part of libroar a part of RoarAudio,
8 *  a cross-platform sound system for both, home and professional use.
9 *  See README for details.
10 *
11 *  This file is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License version 3
13 *  as published by the Free Software Foundation.
14 *
15 *  libroar is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with this software; see the file COPYING.  If not, write to
22 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 *  Boston, MA 02110-1301, USA.
24 *
25 *  NOTE for everyone want's to change something and send patches:
26 *  read README and HACKING! There a addition information on
27 *  the license of this document you need to read before you send
28 *  any patches.
29 *
30 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
31 *  or libpulse*:
32 *  The libs libroaresd, libroararts and libroarpulse link this lib
33 *  and are therefore GPL. Because of this it may be illigal to use
34 *  them with any software that uses libesd, libartsc or libpulse*.
35 */
36
37//#define DEBUG
38#include "roar.h"
39
40// Equvivalent to prepare(). Starts a stream. Might/will be called several times during a program!
41/////////////////////////////////////////////////
42// Status: Should be mostly complete.
43// Condition for invalid fh is not solved here.
44////////////////////////////////////////////////
45static int roar_pcm_start (snd_pcm_ioplug_t * io) {
46 struct roar_alsa_pcm * self = io->private_data;
47 int fh;
48
49 ROAR_DBG("roar_pcm_start(*) = ?");
50
51 // If start is called several times in a row, just ignore it.
52 if (self->stream_opened)
53  return 0;
54
55 if ( roar_vio_simple_new_stream_obj(&(self->stream_vio), &(self->roar.con), &(self->stream),
56    self->info.rate, self->info.channels, self->info.bits, self->info.codec,
57    io->stream == SND_PCM_STREAM_PLAYBACK ? ROAR_DIR_PLAY : ROAR_DIR_MONITOR
58    ) == -1 ) {
59  return -EINVAL;
60 }
61
62 if ( roar_vio_ctl(&(self->stream_vio),
63    io->stream == SND_PCM_STREAM_PLAYBACK ? ROAR_VIO_CTL_GET_SELECT_WRITE_FH :
64    ROAR_VIO_CTL_GET_SELECT_READ_FH, &fh) != 1 ) {
65  io->poll_fd = fh;
66  io->poll_events = io->stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
67 }
68
69 // Oh, no, what should we do here if roar_vio_ctl() fails to grab a valid fh to poll?
70 // In any case, ALSA will error out the next time it tries to poll() if we don't give it a valid fh.
71
72 snd_pcm_ioplug_reinit_status(io);
73
74 // Stream is now active, yay.
75 self->stream_opened = 1;
76
77 self->bufptr = 0;
78 self->thread_active = 1; // We have to activate the thread before starting it, because the thread lives on that thread_active is 1.
79 if ( pthread_create(&(self->thread), NULL, roar_plugin_thread, self) < 0 ) {
80  self->thread_active = 0;
81  return -1;
82 }
83
84 return 0;
85}
86
87void roar_plugin_reset(struct roar_alsa_pcm *self) {
88 if ( !self->stream_opened )
89  return;
90
91 roar_vio_close(&(self->stream_vio));
92 self->stream_opened = 0;
93 self->thread_active = 0;
94 self->bufptr = 0;
95 self->last_ptr = 0;
96 self->total_written = 0;
97 self->has_written = 0;
98}
99
100
101
102// Simply stopping the stream. Will need to be restarted to play more.
103// Will be called several times together with roar_pcm_start()
104///////////////////////////////////////////////////
105// Status: Still needs some error checking for the pthread calls, but
106// should work.
107//////////////////////////////////////////////////
108static int roar_pcm_stop (snd_pcm_ioplug_t *io) {
109 struct roar_alsa_pcm * self = io->private_data;
110
111 // If this is called several times in a row, just ignore.
112
113 ROAR_DBG("roar_pcm_stop(*) = 0");
114
115
116 if ( self->thread_active ) {
117  self->thread_active = 0;
118  pthread_cond_signal(&(self->cond));
119  pthread_join(self->thread, NULL);
120 }
121
122 roar_plugin_reset(self);
123
124 return 0;
125}
126
127///////////////////////////////
128// Status: Should be complete.
129///////////////////////////////
130static int roar_hw_constraint(struct roar_alsa_pcm * self) {
131 snd_pcm_ioplug_t *io = &(self->io);
132 static const snd_pcm_access_t access_list[] = {
133  SND_PCM_ACCESS_RW_INTERLEAVED
134 };
135 static const unsigned int formats[] = {
136  SND_PCM_FORMAT_S8,
137  SND_PCM_FORMAT_U8,
138  SND_PCM_FORMAT_A_LAW,
139  SND_PCM_FORMAT_MU_LAW,
140  SND_PCM_FORMAT_S16_LE,
141  SND_PCM_FORMAT_S16_BE,
142  SND_PCM_FORMAT_U16_LE,
143  SND_PCM_FORMAT_U16_BE,
144  SND_PCM_FORMAT_S32_LE,
145  SND_PCM_FORMAT_S32_BE,
146  SND_PCM_FORMAT_U32_LE,
147  SND_PCM_FORMAT_U32_BE,
148  SND_PCM_FORMAT_S24_3LE,
149  SND_PCM_FORMAT_S24_3BE,
150  SND_PCM_FORMAT_U24_3LE,
151  SND_PCM_FORMAT_U24_3BE,
152 };
153 int ret;
154
155 ROAR_DBG("roar_hw_constraint(*) = ?");
156
157 if ( (ret = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
158     _as(access_list), access_list)) < 0 )
159  return ret;
160
161 if ( (ret = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
162     _as(formats), formats)) < 0 )
163  return ret;
164
165 if ( (ret = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
166     1, ROAR_MAX_CHANNELS)) < 0 )
167  return ret;
168
169 if ( (ret = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE, 8000, 192000)) < 0 )
170  return ret;
171
172 // We shouldn't let ALSA use extremely low or high values, it will kill a kitty most likely. :v
173 if ( (ret = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES, 1 << 6, 1 << 18)) < 0 )
174  return ret;
175
176 if ( (ret = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS, 1, 1024)) < 0 )
177  return ret;
178
179 if ( (ret = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_BUFFER_BYTES, 1 << 13, 1 << 24)) < 0 )
180  return ret;
181
182 ROAR_DBG("roar_hw_constraint(*) = 0");
183
184 return 0;
185}
186
187// Referring to alsa-lib/src/pcm/pcm_ioplug.c : snd_pcm_ioplug_hw_ptr_update
188///////////////////////////////////////////////////////////
189// Status: Mostly complete, but uses a really nasty hack!
190///////////////////////////////////////////////////////////
191static snd_pcm_sframes_t roar_pcm_pointer(snd_pcm_ioplug_t *io) {
192 struct roar_alsa_pcm * self = io->private_data;
193 int ptr;
194
195 ROAR_DBG("roar_pcm_pointer(*) = ?");
196 // Did ALSA just call snd_pcm_reset() or something like that without calling the plugin?
197 // We should restart our stream as well.
198 if ( io->appl_ptr < self->last_ptr ) {
199  roar_pcm_stop(io);
200  roar_pcm_start(io);
201 }
202
203 // ALSA has a weird way of calculating how much data can be written to the audio buffer.
204 // It uses the formula:
205 // avail = bufsize + ptr - io->appl_ptr; (??!?)
206 // We really want this:
207 // avail = bufsize - ptr;
208 // This is the obvious way, so we have to manipulate ptr like this:
209 // ptr = io->appl_ptr - ptr;
210
211 pthread_mutex_lock(&(self->lock));
212 ptr = snd_pcm_bytes_to_frames(io->pcm, self->bufptr);
213 pthread_mutex_unlock(&(self->lock));
214
215 ptr = io->appl_ptr - ptr;
216 self->last_ptr = io->appl_ptr;
217
218 ROAR_DBG("roar_pcm_pointer(*) appl_ptr: %i, ptr: %i, calculated avail frames: %i", (int)io->appl_ptr, (int)ptr, (int)(io->appl_ptr - ptr));
219 ROAR_DBG("roar_pcm_pointer(*) = %i", ptr);
220 return ptr;
221}
222
223// TODO: FIXME: add support for reading data!
224//////////////////////////////////////////////////
225// Status: For writing, this should be complete.
226//////////////////////////////////////////////////
227static snd_pcm_sframes_t roar_pcm_transfer(snd_pcm_ioplug_t *io,
228  const snd_pcm_channel_area_t *areas,
229  snd_pcm_uframes_t offset,
230  snd_pcm_uframes_t size) {
231 struct roar_alsa_pcm * self = io->private_data;
232 char * buf;
233 size_t len = size * self->info.channels * self->info.bits / 8;
234 ssize_t ret;
235
236 ROAR_DBG("roar_pcm_transfer(*) = ?");
237 ROAR_DBG("roar_pcm_transfer(*): len=%lu", (long unsigned int) len);
238
239 // Weird ALSA stuff.
240 buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
241
242 ret = roar_plugin_write(self, buf, len);
243
244 if ( ret == -1 )
245  return -EIO;
246
247 ROAR_DBG("roar_pcm_transfer(*) = %lli", (long long int)size);
248 return size;
249}
250
251///////////////////////////////////////////////////////////////////
252// Status: Still missing proper delay measurements from the roar server.
253// Only uses a blind timer now. In ideal conditions, this will work well.
254///////////////////////////////////////////////////////////////////
255static int roar_pcm_delay(snd_pcm_ioplug_t *io, snd_pcm_sframes_t *delayp) {
256 struct roar_alsa_pcm * self = io->private_data;
257
258 ROAR_DBG("roar_pcm_delay(*) = ?");
259
260 // TODO: We need to set *delayp the latency in frames.
261 pthread_mutex_lock(&(self->lock));
262 roar_plugin_drain(self);
263 *delayp = snd_pcm_bytes_to_frames(io->pcm, self->bytes_in_buffer);
264 pthread_mutex_unlock(&(self->lock));
265
266 return 0;
267}
268
269////////////////////
270// Status: Complete
271////////////////////
272static int roar_pcm_prepare(snd_pcm_ioplug_t *io) {
273 ROAR_DBG("roar_pcm_prepare(*) = ?");
274
275 return roar_pcm_start(io);
276}
277
278///////////////////////////////////////////////////////////////////////////////////////////////////////////////
279// Status: This should be mostly complete.
280// I'm not sure if hw_params can be called several times during one stream without stop() start() in between.
281// This will mean a memory leak, and possibly breakage should the buffer size change itself.
282///////////////////////////////////////////////////////////////////////////////////////////////////////////////
283static int roar_pcm_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params) {
284 struct roar_alsa_pcm * self = io->private_data;
285
286 ROAR_DBG("roar_pcm_hw_params(*) = ?");
287
288 self->info.channels = io->channels;
289 self->info.rate     = io->rate;
290
291 switch (io->format) {
292  case SND_PCM_FORMAT_S8:
293   self->info.codec = ROAR_CODEC_PCM_U_LE;
294   self->info.bits  = 8;
295   break;
296  case SND_PCM_FORMAT_U8:
297   self->info.codec = ROAR_CODEC_PCM_U_LE;
298   self->info.bits  = 8;
299   break;
300  case SND_PCM_FORMAT_A_LAW:
301   self->info.codec = ROAR_CODEC_ALAW;
302   self->info.bits  = 8;
303   break;
304  case SND_PCM_FORMAT_MU_LAW:
305   self->info.codec = ROAR_CODEC_MULAW;
306   self->info.bits  = 8;
307   break;
308  case SND_PCM_FORMAT_S16_LE:
309   self->info.codec = ROAR_CODEC_PCM_S_LE;
310   self->info.bits  = 16;
311   break;
312  case SND_PCM_FORMAT_S16_BE:
313   self->info.codec = ROAR_CODEC_PCM_S_BE;
314   self->info.bits  = 16;
315   break;
316  case SND_PCM_FORMAT_U16_LE:
317   self->info.codec = ROAR_CODEC_PCM_U_LE;
318   self->info.bits  = 16;
319   break;
320  case SND_PCM_FORMAT_U16_BE:
321   self->info.codec = ROAR_CODEC_PCM_U_BE;
322   self->info.bits  = 16;
323   break;
324  case SND_PCM_FORMAT_S32_LE:
325   self->info.codec = ROAR_CODEC_PCM_S_LE;
326   self->info.bits  = 32;
327   break;
328  case SND_PCM_FORMAT_S32_BE:
329   self->info.codec = ROAR_CODEC_PCM_S_BE;
330   self->info.bits  = 32;
331   break;
332  case SND_PCM_FORMAT_U32_LE:
333   self->info.codec = ROAR_CODEC_PCM_U_LE;
334   self->info.bits  = 32;
335   break;
336  case SND_PCM_FORMAT_U32_BE:
337   self->info.codec = ROAR_CODEC_PCM_U_BE;
338   self->info.bits  = 32;
339   break;
340  case SND_PCM_FORMAT_S24_3LE:
341   self->info.codec = ROAR_CODEC_PCM_S_LE;
342   self->info.bits  = 24;
343   break;
344  case SND_PCM_FORMAT_S24_3BE:
345   self->info.codec = ROAR_CODEC_PCM_S_BE;
346   self->info.bits  = 24;
347   break;
348  case SND_PCM_FORMAT_U24_3LE:
349   self->info.codec = ROAR_CODEC_PCM_U_LE;
350   self->info.bits  = 24;
351   break;
352  case SND_PCM_FORMAT_U24_3BE:
353   self->info.codec = ROAR_CODEC_PCM_U_BE;
354   self->info.bits  = 24;
355   break;
356  default:
357   return -EINVAL;
358 }
359
360 snd_pcm_uframes_t buffersize;
361 int err;
362
363 if ((err = snd_pcm_hw_params_get_buffer_size(params, &buffersize) < 0))
364  return err;
365
366 ROAR_DBG("roar_pcm_hw_params(*) buffersize (bytes): %i", (int)buffersize);
367
368 //self->bufsize = snd_pcm_frames_to_bytes(io->pcm, buffersize);
369 self->bufsize = self->info.bits * self->info.channels * buffersize / 8;
370 self->buffer = malloc(self->bufsize);
371 if (self->buffer == NULL)
372  return -1;
373 self->bufptr = 0;
374
375 ROAR_DBG("roar_pcm_hw_params(*) Setting buffersize (bytes): %i", (int)self->bufsize);
376 ROAR_DBG("roar_pcm_hw_params(*) = 0");
377 return 0;
378}
379
380///////////////////////////////////
381// Status: This should be complete.
382// This is the last cleanup function to be called by ALSA.
383///////////////////////////////////
384static int roar_pcm_close (snd_pcm_ioplug_t * io) {
385 struct roar_alsa_pcm * self = io->private_data;
386
387 ROAR_DBG("roar_pcm_close(*) = ?");
388
389 roar_disconnect(&(self->roar.con));
390
391 pthread_mutex_destroy(&(self->lock));
392 pthread_mutex_destroy(&(self->cond_lock));
393 pthread_cond_destroy(&(self->cond));
394
395 free(self->buffer);
396 free(self);
397
398 return 0;
399}
400
401static snd_pcm_ioplug_callback_t roar_pcm_callback = {
402 .start                  = roar_pcm_start,
403 .stop                   = roar_pcm_stop,
404 .pointer                = roar_pcm_pointer,
405 .transfer               = roar_pcm_transfer,
406 .delay                  = roar_pcm_delay,
407 .prepare                = roar_pcm_prepare,
408 .hw_params              = roar_pcm_hw_params,
409 .close                  = roar_pcm_close,
410};
411
412SND_PCM_PLUGIN_DEFINE_FUNC(roar) {
413 struct roar_alsa_pcm * self;
414 snd_config_iterator_t i, next;
415 snd_config_t * n;
416 const char   * para;
417 const char   * server = NULL;
418 int            ret;
419
420 (void)root;
421
422 ROAR_DBG("SND_PCM_PLUGIN_DEFINE_FUNC(roar) = ?");
423
424 snd_config_for_each(i, next, conf) {
425  n = snd_config_iterator_entry(i);
426  if ( snd_config_get_id(n, &para) < 0 )
427   continue;
428
429  if ( !strcmp(para, "type") || !strcmp(para, "comment") || !strcmp(para, "hint") )
430   continue;
431
432  if ( !strcmp(para, "server") ) {
433   if (snd_config_get_string(n, &server) < 0) {
434    return -EINVAL;
435   }
436  } else {
437   return -EINVAL;
438  }
439 }
440
441 errno = ENOSYS;
442
443 if ( (self = malloc(sizeof(struct roar_alsa_pcm))) == NULL )
444  return -errno;
445
446 memset(self, 0, sizeof(struct roar_alsa_pcm));
447
448 errno = ENOSYS;
449 if ( roar_simple_connect(&(self->roar.con), (char*)server, "ALSA Plugin") == -1 ) {
450  free(self);
451  return -errno;
452 }
453
454 self->io.version      = SND_PCM_IOPLUG_VERSION;
455 self->io.name         = "RoarAudio Plugin";
456 self->io.poll_fd      =  -1;
457 self->io.poll_events  =  POLLOUT;
458 self->io.mmap_rw      =  0;
459 self->io.callback     = &roar_pcm_callback;
460 self->io.private_data =  self;
461
462 if ( (ret = snd_pcm_ioplug_create(&(self->io), name, stream, mode)) < 0 ) {
463  roar_disconnect(&(self->roar.con));
464  free(self);
465  return ret;
466 }
467
468 pthread_mutex_init(&(self->lock), NULL);
469 pthread_mutex_init(&(self->cond_lock), NULL);
470 pthread_cond_init(&(self->cond), NULL);
471
472
473 if ( (ret = roar_hw_constraint(self)) < 0 ) {
474  snd_pcm_ioplug_delete(&(self->io));
475  roar_disconnect(&(self->roar.con));
476  free(self);
477  return ret;
478 }
479
480 *pcmp = self->io.pcm;
481
482 ROAR_DBG("SND_PCM_PLUGIN_DEFINE_FUNC(roar) = 0");
483
484 return 0;
485}
486
487SND_PCM_PLUGIN_SYMBOL(roar);
488
489//ll
Note: See TracBrowser for help on using the repository browser.