2 * \file pcm/pcm_dshare.c
4 * \brief PCM Direct Sharing of Channels Plugin Interface
5 * \author Jaroslav Kysela <perex@suse.cz>
9 * PCM - Direct Sharing of Channels
10 * Copyright (c) 2003 by Jaroslav Kysela <perex@suse.cz>
13 * This library is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as
15 * published by the Free Software Foundation; either version 2.1 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include <sys/ioctl.h>
42 #include <sys/socket.h>
45 #include "pcm_direct.h"
48 /* entry for static linking */
49 const char *_snd_module_pcm_dshare = "";
52 static void do_silence(snd_pcm_t *pcm)
54 snd_pcm_direct_t *dshare = pcm->private_data;
55 const snd_pcm_channel_area_t *dst_areas;
56 unsigned int chn, dchn, channels;
57 snd_pcm_format_t format;
59 dst_areas = snd_pcm_mmap_areas(dshare->spcm);
60 channels = dshare->channels;
61 format = dshare->shmptr->s.format;
62 for (chn = 0; chn < channels; chn++) {
63 dchn = dshare->bindings ? dshare->bindings[chn] : chn;
64 snd_pcm_area_silence(&dst_areas[dchn], 0, dshare->shmptr->s.buffer_size, format);
68 static void share_areas(snd_pcm_direct_t *dshare,
69 const snd_pcm_channel_area_t *src_areas,
70 const snd_pcm_channel_area_t *dst_areas,
71 snd_pcm_uframes_t src_ofs,
72 snd_pcm_uframes_t dst_ofs,
73 snd_pcm_uframes_t size)
75 unsigned int chn, dchn, channels;
76 snd_pcm_format_t format;
78 channels = dshare->channels;
79 format = dshare->shmptr->s.format;
80 if (dshare->interleaved) {
81 unsigned int fbytes = snd_pcm_format_physical_width(format) / 8;
82 memcpy(((char *)dst_areas[0].addr) + (dst_ofs * channels * fbytes),
83 ((char *)src_areas[0].addr) + (src_ofs * channels * fbytes),
84 size * channels * fbytes);
86 for (chn = 0; chn < channels; chn++) {
87 dchn = dshare->bindings ? dshare->bindings[chn] : chn;
88 snd_pcm_area_copy(&dst_areas[dchn], dst_ofs, &src_areas[chn], src_ofs, size, format);
95 * synchronize shm ring buffer with hardware
97 static void snd_pcm_dshare_sync_area(snd_pcm_t *pcm, snd_pcm_uframes_t size)
99 snd_pcm_direct_t *dshare = pcm->private_data;
100 snd_pcm_uframes_t appl_ptr, slave_appl_ptr, transfer;
101 const snd_pcm_channel_area_t *src_areas, *dst_areas;
103 /* get the start of update area */
104 appl_ptr = dshare->appl_ptr - size;
105 if (appl_ptr > pcm->boundary)
106 appl_ptr += pcm->boundary;
107 appl_ptr %= pcm->buffer_size;
108 /* add sample areas here */
109 src_areas = snd_pcm_mmap_areas(pcm);
110 dst_areas = snd_pcm_mmap_areas(dshare->spcm);
111 slave_appl_ptr = dshare->slave_appl_ptr % dshare->shmptr->s.buffer_size;
112 dshare->slave_appl_ptr += size;
113 dshare->slave_appl_ptr %= dshare->shmptr->s.boundary;
115 transfer = appl_ptr + size > pcm->buffer_size ? pcm->buffer_size - appl_ptr : size;
116 transfer = slave_appl_ptr + transfer > dshare->shmptr->s.buffer_size ? dshare->shmptr->s.buffer_size - slave_appl_ptr : transfer;
118 share_areas(dshare, src_areas, dst_areas, appl_ptr, slave_appl_ptr, transfer);
119 slave_appl_ptr += transfer;
120 slave_appl_ptr %= dshare->shmptr->s.buffer_size;
121 appl_ptr += transfer;
122 appl_ptr %= pcm->buffer_size;
127 * synchronize hardware pointer (hw_ptr) with ours
129 static int snd_pcm_dshare_sync_ptr(snd_pcm_t *pcm)
131 snd_pcm_direct_t *dshare = pcm->private_data;
132 snd_pcm_uframes_t slave_hw_ptr, old_slave_hw_ptr, avail;
133 snd_pcm_sframes_t diff;
135 switch (snd_pcm_state(dshare->spcm)) {
136 case SND_PCM_STATE_DISCONNECTED:
137 dshare->state = SNDRV_PCM_STATE_DISCONNECTED;
143 snd_pcm_hwsync(dshare->spcm);
144 old_slave_hw_ptr = dshare->slave_hw_ptr;
145 slave_hw_ptr = dshare->slave_hw_ptr = *dshare->spcm->hw.ptr;
146 diff = slave_hw_ptr - old_slave_hw_ptr;
147 if (diff == 0) /* fast path */
150 slave_hw_ptr += dshare->shmptr->s.boundary;
151 diff = slave_hw_ptr - old_slave_hw_ptr;
153 dshare->hw_ptr += diff;
154 dshare->hw_ptr %= pcm->boundary;
155 // printf("sync ptr diff = %li\n", diff);
156 if (pcm->stop_threshold >= pcm->boundary) /* don't care */
158 if ((avail = snd_pcm_mmap_playback_avail(pcm)) >= pcm->stop_threshold) {
160 gettimeofday(&tv, 0);
161 dshare->trigger_tstamp.tv_sec = tv.tv_sec;
162 dshare->trigger_tstamp.tv_nsec = tv.tv_usec * 1000L;
163 dshare->state = SND_PCM_STATE_XRUN;
164 dshare->avail_max = avail;
167 if (avail > dshare->avail_max)
168 dshare->avail_max = avail;
173 * plugin implementation
176 static int snd_pcm_dshare_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
178 snd_pcm_direct_t *dshare = pcm->private_data;
179 snd_pcm_state_t state;
181 switch (dshare->state) {
182 case SNDRV_PCM_STATE_DRAINING:
183 case SNDRV_PCM_STATE_RUNNING:
184 snd_pcm_dshare_sync_ptr(pcm);
189 memset(status, 0, sizeof(*status));
190 state = snd_pcm_state(dshare->spcm);
191 status->state = state == SND_PCM_STATE_RUNNING ? dshare->state : state;
192 status->trigger_tstamp = dshare->trigger_tstamp;
193 status->tstamp = snd_pcm_hw_fast_tstamp(dshare->spcm);
194 status->avail = snd_pcm_mmap_playback_avail(pcm);
195 status->avail_max = status->avail > dshare->avail_max ? status->avail : dshare->avail_max;
196 dshare->avail_max = 0;
200 static snd_pcm_state_t snd_pcm_dshare_state(snd_pcm_t *pcm)
202 snd_pcm_direct_t *dshare = pcm->private_data;
203 switch (snd_pcm_state(dshare->spcm)) {
204 case SND_PCM_STATE_SUSPENDED:
206 case SND_PCM_STATE_DISCONNECTED:
207 dshare->state = SNDRV_PCM_STATE_DISCONNECTED;
212 return dshare->state;
215 static int snd_pcm_dshare_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
217 snd_pcm_direct_t *dshare = pcm->private_data;
220 switch (dshare->state) {
221 case SNDRV_PCM_STATE_DRAINING:
222 case SNDRV_PCM_STATE_RUNNING:
223 err = snd_pcm_dshare_sync_ptr(pcm);
226 case SNDRV_PCM_STATE_PREPARED:
227 case SNDRV_PCM_STATE_SUSPENDED:
228 *delayp = snd_pcm_mmap_playback_hw_avail(pcm);
230 case SNDRV_PCM_STATE_XRUN:
232 case SNDRV_PCM_STATE_DISCONNECTED:
239 static int snd_pcm_dshare_hwsync(snd_pcm_t *pcm)
241 snd_pcm_direct_t *dshare = pcm->private_data;
243 switch(dshare->state) {
244 case SNDRV_PCM_STATE_DRAINING:
245 case SNDRV_PCM_STATE_RUNNING:
246 return snd_pcm_dshare_sync_ptr(pcm);
247 case SNDRV_PCM_STATE_PREPARED:
248 case SNDRV_PCM_STATE_SUSPENDED:
250 case SNDRV_PCM_STATE_XRUN:
252 case SNDRV_PCM_STATE_DISCONNECTED:
259 static int snd_pcm_dshare_prepare(snd_pcm_t *pcm)
261 snd_pcm_direct_t *dshare = pcm->private_data;
263 snd_pcm_direct_check_interleave(dshare, pcm);
264 // assert(pcm->boundary == dshare->shmptr->s.boundary); /* for sure */
265 dshare->state = SND_PCM_STATE_PREPARED;
266 dshare->appl_ptr = 0;
271 static int snd_pcm_dshare_reset(snd_pcm_t *pcm)
273 snd_pcm_direct_t *dshare = pcm->private_data;
274 dshare->hw_ptr %= pcm->period_size;
275 dshare->appl_ptr = dshare->hw_ptr;
276 dshare->slave_appl_ptr = dshare->slave_hw_ptr = *dshare->spcm->hw.ptr;
280 static int snd_pcm_dshare_start(snd_pcm_t *pcm)
282 snd_pcm_direct_t *dshare = pcm->private_data;
283 snd_pcm_sframes_t avail;
287 if (dshare->state != SND_PCM_STATE_PREPARED)
289 err = snd_timer_start(dshare->timer);
292 dshare->state = SND_PCM_STATE_RUNNING;
293 snd_pcm_hwsync(dshare->spcm);
294 dshare->slave_appl_ptr = dshare->slave_hw_ptr = *dshare->spcm->hw.ptr;
295 avail = snd_pcm_mmap_playback_hw_avail(pcm);
298 if (avail > (snd_pcm_sframes_t)pcm->buffer_size)
299 avail = pcm->buffer_size;
300 snd_pcm_dshare_sync_area(pcm, avail);
301 gettimeofday(&tv, 0);
302 dshare->trigger_tstamp.tv_sec = tv.tv_sec;
303 dshare->trigger_tstamp.tv_nsec = tv.tv_usec * 1000L;
307 static int snd_pcm_dshare_drop(snd_pcm_t *pcm)
309 snd_pcm_direct_t *dshare = pcm->private_data;
310 if (dshare->state == SND_PCM_STATE_OPEN)
312 snd_timer_stop(dshare->timer);
314 dshare->state = SND_PCM_STATE_SETUP;
318 static int snd_pcm_dshare_drain(snd_pcm_t *pcm)
320 snd_pcm_direct_t *dshare = pcm->private_data;
321 snd_pcm_uframes_t stop_threshold;
324 if (dshare->state == SND_PCM_STATE_OPEN)
326 stop_threshold = pcm->stop_threshold;
327 if (pcm->stop_threshold > pcm->buffer_size)
328 pcm->stop_threshold = pcm->buffer_size;
329 while (dshare->state == SND_PCM_STATE_RUNNING) {
330 err = snd_pcm_dshare_sync_ptr(pcm);
333 if (pcm->mode & SND_PCM_NONBLOCK)
335 snd_pcm_wait(pcm, -1);
337 pcm->stop_threshold = stop_threshold;
338 return snd_pcm_dshare_drop(pcm);
341 static int snd_pcm_dshare_pause(snd_pcm_t *pcm, int enable)
343 snd_pcm_direct_t *dshare = pcm->private_data;
345 if (dshare->state != SND_PCM_STATE_RUNNING)
347 dshare->state = SND_PCM_STATE_PAUSED;
348 snd_timer_stop(dshare->timer);
351 if (dshare->state != SND_PCM_STATE_PAUSED)
353 dshare->state = SND_PCM_STATE_RUNNING;
354 snd_timer_start(dshare->timer);
359 static snd_pcm_sframes_t snd_pcm_dshare_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
361 /* FIXME: substract samples from the mix ring buffer, too? */
362 snd_pcm_mmap_appl_backward(pcm, frames);
366 static snd_pcm_sframes_t snd_pcm_dshare_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
368 snd_pcm_sframes_t avail;
370 avail = snd_pcm_mmap_avail(pcm);
373 if (frames > (snd_pcm_uframes_t)avail)
375 snd_pcm_mmap_appl_forward(pcm, frames);
379 static int snd_pcm_dshare_resume(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
381 // snd_pcm_direct_t *dshare = pcm->private_data;
386 static snd_pcm_sframes_t snd_pcm_dshare_readi(snd_pcm_t *pcm ATTRIBUTE_UNUSED, void *buffer ATTRIBUTE_UNUSED, snd_pcm_uframes_t size ATTRIBUTE_UNUSED)
391 static snd_pcm_sframes_t snd_pcm_dshare_readn(snd_pcm_t *pcm ATTRIBUTE_UNUSED, void **bufs ATTRIBUTE_UNUSED, snd_pcm_uframes_t size ATTRIBUTE_UNUSED)
396 static int snd_pcm_dshare_close(snd_pcm_t *pcm)
398 snd_pcm_direct_t *dshare = pcm->private_data;
401 snd_timer_close(dshare->timer);
403 snd_pcm_direct_semaphore_down(dshare, DIRECT_IPC_SEM_CLIENT);
404 dshare->shmptr->u.dshare.chn_mask &= ~dshare->u.dshare.chn_mask;
405 snd_pcm_close(dshare->spcm);
407 snd_pcm_direct_server_discard(dshare);
409 snd_pcm_direct_client_discard(dshare);
410 if (snd_pcm_direct_shm_discard(dshare) > 0) {
411 if (snd_pcm_direct_semaphore_discard(dshare) < 0)
412 snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
414 snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
416 if (dshare->bindings)
417 free(dshare->bindings);
418 pcm->private_data = NULL;
423 static snd_pcm_sframes_t snd_pcm_dshare_mmap_commit(snd_pcm_t *pcm,
424 snd_pcm_uframes_t offset ATTRIBUTE_UNUSED,
425 snd_pcm_uframes_t size)
427 snd_pcm_direct_t *dshare = pcm->private_data;
430 snd_pcm_mmap_appl_forward(pcm, size);
431 if (dshare->state == SND_PCM_STATE_RUNNING) {
432 err = snd_pcm_dshare_sync_ptr(pcm);
435 /* ok, we commit the changes after the validation of area */
436 /* it's intended, although the result might be crappy */
437 snd_pcm_dshare_sync_area(pcm, size);
442 static snd_pcm_sframes_t snd_pcm_dshare_avail_update(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
444 snd_pcm_direct_t *dshare = pcm->private_data;
447 if (dshare->state == SND_PCM_STATE_RUNNING) {
448 err = snd_pcm_dshare_sync_ptr(pcm);
452 return snd_pcm_mmap_playback_avail(pcm);
455 static void snd_pcm_dshare_dump(snd_pcm_t *pcm, snd_output_t *out)
457 snd_pcm_direct_t *dshare = pcm->private_data;
459 snd_output_printf(out, "Direct Stream Mixing PCM\n");
461 snd_output_printf(out, "\nIts setup is:\n");
462 snd_pcm_dump_setup(pcm, out);
465 snd_pcm_dump(dshare->spcm, out);
468 static snd_pcm_ops_t snd_pcm_dshare_ops = {
469 .close = snd_pcm_dshare_close,
470 .info = snd_pcm_direct_info,
471 .hw_refine = snd_pcm_direct_hw_refine,
472 .hw_params = snd_pcm_direct_hw_params,
473 .hw_free = snd_pcm_direct_hw_free,
474 .sw_params = snd_pcm_direct_sw_params,
475 .channel_info = snd_pcm_direct_channel_info,
476 .dump = snd_pcm_dshare_dump,
477 .nonblock = snd_pcm_direct_nonblock,
478 .async = snd_pcm_direct_async,
479 .poll_revents = snd_pcm_direct_poll_revents,
480 .mmap = snd_pcm_direct_mmap,
481 .munmap = snd_pcm_direct_munmap,
484 static snd_pcm_fast_ops_t snd_pcm_dshare_fast_ops = {
485 .status = snd_pcm_dshare_status,
486 .state = snd_pcm_dshare_state,
487 .hwsync = snd_pcm_dshare_hwsync,
488 .delay = snd_pcm_dshare_delay,
489 .prepare = snd_pcm_dshare_prepare,
490 .reset = snd_pcm_dshare_reset,
491 .start = snd_pcm_dshare_start,
492 .drop = snd_pcm_dshare_drop,
493 .drain = snd_pcm_dshare_drain,
494 .pause = snd_pcm_dshare_pause,
495 .rewind = snd_pcm_dshare_rewind,
496 .forward = snd_pcm_dshare_forward,
497 .resume = snd_pcm_dshare_resume,
499 .writei = snd_pcm_mmap_writei,
500 .writen = snd_pcm_mmap_writen,
501 .readi = snd_pcm_dshare_readi,
502 .readn = snd_pcm_dshare_readn,
503 .avail_update = snd_pcm_dshare_avail_update,
504 .mmap_commit = snd_pcm_dshare_mmap_commit,
508 * \brief Creates a new dshare PCM
509 * \param pcmp Returns created PCM handle
510 * \param name Name of PCM
511 * \param ipc_key IPC key for semaphore and shared memory
512 * \param ipc_mode IPC permissions for semaphore and shared memory
513 * \param params Parameters for slave
514 * \param bindings Channel bindings
515 * \param slowptr Slow but more precise pointer updates
516 * \param root Configuration root
517 * \param sconf Slave configuration
518 * \param stream PCM Direction (stream)
519 * \param mode PCM Mode
520 * \retval zero on success otherwise a negative error code
521 * \warning Using of this function might be dangerous in the sense
522 * of compatibility reasons. The prototype might be freely
525 int snd_pcm_dshare_open(snd_pcm_t **pcmp, const char *name,
526 key_t ipc_key, mode_t ipc_perm,
527 struct slave_params *params,
528 snd_config_t *bindings,
530 snd_config_t *root, snd_config_t *sconf,
531 snd_pcm_stream_t stream, int mode)
533 snd_pcm_t *pcm = NULL, *spcm = NULL;
534 snd_pcm_direct_t *dshare = NULL;
535 int ret, first_instance;
537 int fail_sem_loop = 10;
541 if (stream != SND_PCM_STREAM_PLAYBACK) {
542 SNDERR("The dshare plugin supports only playback stream");
546 dshare = calloc(1, sizeof(snd_pcm_direct_t));
552 ret = snd_pcm_direct_parse_bindings(dshare, bindings);
556 if (!dshare->bindings) {
557 SNDERR("dshare: specify bindings!!!");
562 dshare->ipc_key = ipc_key;
563 dshare->ipc_perm = ipc_perm;
567 ret = snd_pcm_new(&pcm, dshare->type = SND_PCM_TYPE_DSHARE, name, stream, mode);
572 ret = snd_pcm_direct_semaphore_create_or_connect(dshare);
574 SNDERR("unable to create IPC semaphore");
578 ret = snd_pcm_direct_semaphore_down(dshare, DIRECT_IPC_SEM_CLIENT);
580 snd_pcm_direct_semaphore_discard(dshare);
581 if (--fail_sem_loop <= 0)
588 first_instance = ret = snd_pcm_direct_shm_create_or_connect(dshare);
590 SNDERR("unable to create IPC shm instance");
594 pcm->ops = &snd_pcm_dshare_ops;
595 pcm->fast_ops = &snd_pcm_dshare_fast_ops;
596 pcm->private_data = dshare;
597 dshare->state = SND_PCM_STATE_OPEN;
598 dshare->slowptr = slowptr;
599 dshare->sync_ptr = snd_pcm_dshare_sync_ptr;
601 if (first_instance) {
602 ret = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
604 SNDERR("unable to open slave");
608 if (snd_pcm_type(spcm) != SND_PCM_TYPE_HW) {
609 SNDERR("dshare plugin can be only connected to hw plugin");
613 ret = snd_pcm_direct_initialize_slave(dshare, spcm, params);
615 SNDERR("unable to initialize slave");
621 ret = snd_pcm_direct_server_create(dshare);
623 SNDERR("unable to create server");
627 dshare->shmptr->type = spcm->type;
629 ret = snd_pcm_direct_client_connect(dshare);
631 SNDERR("unable to connect client");
635 ret = snd_pcm_hw_open_fd(&spcm, "dshare_client", dshare->hw_fd, 0);
637 SNDERR("unable to open hardware");
641 spcm->donot_close = 1;
643 spcm->buffer_size = dshare->shmptr->s.buffer_size;
644 spcm->sample_bits = dshare->shmptr->s.sample_bits;
645 spcm->channels = dshare->shmptr->s.channels;
646 spcm->format = dshare->shmptr->s.format;
647 spcm->boundary = dshare->shmptr->s.boundary;
648 spcm->info = dshare->shmptr->s.info;
649 ret = snd_pcm_mmap(spcm);
651 SNDERR("unable to mmap channels");
657 for (chn = 0; chn < dshare->channels; chn++)
658 dshare->u.dshare.chn_mask |= (1ULL<<dshare->bindings[chn]);
659 if (dshare->shmptr->u.dshare.chn_mask & dshare->u.dshare.chn_mask) {
660 SNDERR("destination channel specified in bindings is already used");
661 dshare->u.dshare.chn_mask = 0;
665 dshare->shmptr->u.dshare.chn_mask |= dshare->u.dshare.chn_mask;
667 ret = snd_pcm_direct_initialize_poll_fd(dshare);
669 SNDERR("unable to initialize poll_fd");
673 pcm->poll_fd = dshare->poll_fd;
674 pcm->poll_events = POLLIN; /* it's different than other plugins */
677 snd_pcm_set_hw_ptr(pcm, &dshare->hw_ptr, -1, 0);
678 snd_pcm_set_appl_ptr(pcm, &dshare->appl_ptr, -1, 0);
680 snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
688 dshare->shmptr->u.dshare.chn_mask &= ~dshare->u.dshare.chn_mask;
690 snd_timer_close(dshare->timer);
692 snd_pcm_direct_server_discard(dshare);
694 snd_pcm_direct_client_discard(dshare);
697 if (dshare->shmid >= 0) {
698 if (snd_pcm_direct_shm_discard(dshare) > 0) {
699 if (dshare->semid >= 0) {
700 if (snd_pcm_direct_semaphore_discard(dshare) < 0)
701 snd_pcm_direct_semaphore_up(dshare, DIRECT_IPC_SEM_CLIENT);
705 if (dshare->bindings)
706 free(dshare->bindings);
714 /*! \page pcm_plugins
716 \section pcm_plugins_dshare Plugin: dshare
718 This plugin provides sharing channels.
719 Unlike \ref pcm_plugins_share "share plugin", this plugin doesn't need
720 the explicit server program but accesses the shared buffer concurrently
721 from each client as well as \ref pcm_plugins_dmix "dmix" and
722 \ref pcm_plugins_dsnoop "dsnoop" plugins do.
723 The parameters below are almost identical with these plugins.
727 type dshare # Direct sharing
728 ipc_key INT # unique IPC key
729 ipc_key_add_uid BOOL # add current uid to unique IPC key
732 slave { # Slave definition
733 pcm STR # slave PCM name
735 pcm { } # slave PCM definition
736 format STR # format definition
737 rate INT # rate definition
739 period_time INT # in usec
741 period_size INT # in bytes
742 buffer_time INT # in usec
744 buffer_size INT # in bytes
745 periods INT # when buffer_size or buffer_time is not specified
747 bindings { # note: this is client independent!!!
748 N INT # maps slave channel to client channel N
753 \subsection pcm_plugins_dshare_funcref Function reference
756 <LI>snd_pcm_dshare_open()
757 <LI>_snd_pcm_dshare_open()
763 * \brief Creates a new dshare PCM
764 * \param pcmp Returns created PCM handle
765 * \param name Name of PCM
766 * \param root Root configuration node
767 * \param conf Configuration node with dshare PCM description
768 * \param stream PCM Stream
769 * \param mode PCM Mode
770 * \warning Using of this function might be dangerous in the sense
771 * of compatibility reasons. The prototype might be freely
774 int _snd_pcm_dshare_open(snd_pcm_t **pcmp, const char *name,
775 snd_config_t *root, snd_config_t *conf,
776 snd_pcm_stream_t stream, int mode)
778 snd_config_iterator_t i, next;
779 snd_config_t *slave = NULL, *bindings = NULL, *sconf;
780 struct slave_params params;
781 int bsize, psize, ipc_key_add_uid = 0, slowptr = 0;
783 mode_t ipc_perm = 0600;
786 snd_config_for_each(i, next, conf) {
787 snd_config_t *n = snd_config_iterator_entry(i);
789 if (snd_config_get_id(n, &id) < 0)
791 if (snd_pcm_conf_generic_id(id))
793 if (strcmp(id, "ipc_key") == 0) {
795 err = snd_config_get_integer(n, &key);
797 SNDERR("The field ipc_key must be an integer type");
803 if (strcmp(id, "ipc_perm") == 0) {
806 err = snd_config_get_ascii(n, &perm);
808 SNDERR("The field ipc_perm must be a valid file permission");
811 if (isdigit(*perm) == 0) {
812 SNDERR("The field ipc_perm must be a valid file permission");
815 ipc_perm = strtol(perm, &endp, 8);
818 if (strcmp(id, "ipc_key_add_uid") == 0) {
820 err = snd_config_get_ascii(n, &tmp);
822 SNDERR("The field ipc_key_add_uid must be a boolean type");
825 err = snd_config_get_bool_ascii(tmp);
828 SNDERR("The field ipc_key_add_uid must be a boolean type");
831 ipc_key_add_uid = err;
834 if (strcmp(id, "slave") == 0) {
838 if (strcmp(id, "bindings") == 0) {
842 if (strcmp(id, "slowptr") == 0) {
844 err = snd_config_get_ascii(n, &tmp);
846 SNDERR("The field slowptr must be a boolean type");
849 err = snd_config_get_bool_ascii(tmp);
852 SNDERR("The field slowptr must be a boolean type");
858 SNDERR("Unknown field %s", id);
862 SNDERR("slave is not defined");
868 SNDERR("Unique IPC key is not defined");
871 /* the default settings, it might be invalid for some hardware */
872 params.format = SND_PCM_FORMAT_S16;
875 params.period_time = 125000; /* 0.125 seconds */
876 params.buffer_time = -1;
879 err = snd_pcm_slave_conf(root, slave, &sconf, 8,
880 SND_PCM_HW_PARAM_FORMAT, 0, ¶ms.format,
881 SND_PCM_HW_PARAM_RATE, 0, ¶ms.rate,
882 SND_PCM_HW_PARAM_CHANNELS, 0, ¶ms.channels,
883 SND_PCM_HW_PARAM_PERIOD_TIME, 0, ¶ms.period_time,
884 SND_PCM_HW_PARAM_BUFFER_TIME, 0, ¶ms.buffer_time,
885 SND_PCM_HW_PARAM_PERIOD_SIZE, 0, &psize,
886 SND_PCM_HW_PARAM_BUFFER_SIZE, 0, &bsize,
887 SND_PCM_HW_PARAM_PERIODS, 0, ¶ms.periods);
891 params.period_size = psize;
892 params.buffer_size = bsize;
893 err = snd_pcm_dshare_open(pcmp, name, ipc_key, ipc_perm, ¶ms, bindings, slowptr, root, sconf, stream, mode);
895 snd_config_delete(sconf);
899 SND_DLSYM_BUILD_VERSION(_snd_pcm_dshare_open, SND_PCM_DLSYM_VERSION);