2 * PCM - Direct Stream Mixing
3 * Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/ioctl.h>
37 #include <sys/socket.h>
41 #include "pcm_direct.h"
48 int val; /* Value for SETVAL */
49 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
50 unsigned short *array; /* Array for GETALL, SETALL */
51 struct seminfo *__buf; /* Buffer for IPC_INFO (Linux specific) */
56 * add possibility to use futexes here
59 int snd_pcm_direct_semaphore_create_or_connect(snd_pcm_direct_t *dmix)
65 dmix->semid = semget(dmix->ipc_key, DIRECT_IPC_SEMS,
66 IPC_CREAT | dmix->ipc_perm);
69 if (dmix->ipc_gid < 0)
71 for (i = 0; i < DIRECT_IPC_SEMS; i++) {
73 if (semctl(dmix->semid, i, IPC_STAT, s) < 0) {
75 snd_pcm_direct_semaphore_discard(dmix);
78 buf.sem_perm.gid = dmix->ipc_gid;
80 semctl(dmix->semid, i, IPC_SET, s);
85 #define SND_PCM_DIRECT_MAGIC (0xa15ad300 + sizeof(snd_pcm_direct_share_t))
88 * global shared memory area
91 int snd_pcm_direct_shm_create_or_connect(snd_pcm_direct_t *dmix)
94 int tmpid, err, first_instance = 0;
97 dmix->shmid = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t),
99 if (dmix->shmid < 0) {
101 if ((dmix->shmid = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t),
102 IPC_CREAT | IPC_EXCL | dmix->ipc_perm)) != -1)
106 if (dmix->shmid < 0) {
108 if ((tmpid = shmget(dmix->ipc_key, 0, dmix->ipc_perm)) != -1)
109 if (!shmctl(tmpid, IPC_STAT, &buf))
111 /* no users so destroy the segment */
112 if (!shmctl(tmpid, IPC_RMID, NULL))
116 dmix->shmptr = shmat(dmix->shmid, 0, 0);
117 if (dmix->shmptr == (void *) -1) {
119 snd_pcm_direct_shm_discard(dmix);
122 mlock(dmix->shmptr, sizeof(snd_pcm_direct_share_t));
123 if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0) {
125 snd_pcm_direct_shm_discard(dmix);
128 if (first_instance) { /* we're the first user, clear the segment */
129 memset(dmix->shmptr, 0, sizeof(snd_pcm_direct_share_t));
130 if (dmix->ipc_gid >= 0) {
131 buf.shm_perm.gid = dmix->ipc_gid;
132 shmctl(dmix->shmid, IPC_SET, &buf);
134 dmix->shmptr->magic = SND_PCM_DIRECT_MAGIC;
137 if (dmix->shmptr->magic != SND_PCM_DIRECT_MAGIC) {
138 snd_pcm_direct_shm_discard(dmix);
145 /* discard shared memory */
147 * Define snd_* functions to be used in server.
148 * Since objects referred in a plugin can be released dynamically, a forked
149 * server should have statically linked functions.
150 * (e.g. Novell bugzilla #105772)
152 static int _snd_pcm_direct_shm_discard(snd_pcm_direct_t *dmix)
159 if (dmix->shmptr != (void *) -1 && shmdt(dmix->shmptr) < 0)
161 dmix->shmptr = (void *) -1;
162 if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0)
164 if (buf.shm_nattch == 0) { /* we're the last user, destroy the segment */
165 if (shmctl(dmix->shmid, IPC_RMID, NULL) < 0)
173 /* ... and an exported version */
174 int snd_pcm_direct_shm_discard(snd_pcm_direct_t *dmix)
176 return _snd_pcm_direct_shm_discard(dmix);
183 static int get_tmp_name(char *filename, size_t size)
187 gettimeofday(&tv, NULL);
188 snprintf(filename, size, TMPDIR "/alsa-dmix-%i-%li-%li", (int)getpid(), (long)tv.tv_sec, (long)tv.tv_usec);
189 filename[size-1] = '\0';
193 static int make_local_socket(const char *filename, int server, mode_t ipc_perm, int ipc_gid)
195 size_t l = strlen(filename);
196 size_t size = offsetof(struct sockaddr_un, sun_path) + l;
197 struct sockaddr_un *addr = alloca(size);
200 sock = socket(PF_LOCAL, SOCK_STREAM, 0);
203 SYSERR("socket failed");
209 memset(addr, 0, size); /* make valgrind happy */
210 addr->sun_family = AF_LOCAL;
211 memcpy(addr->sun_path, filename, l);
214 if (bind(sock, (struct sockaddr *) addr, size) < 0) {
216 SYSERR("bind failed: %s", filename);
220 if (chmod(filename, ipc_perm) < 0) {
222 SYSERR("chmod failed: %s", filename);
227 if (chown(filename, -1, ipc_gid) < 0) {
228 #if 0 /* it's not fatal */
230 SYSERR("chown failed: %s", filename);
238 if (connect(sock, (struct sockaddr *) addr, size) < 0) {
240 SYSERR("connect failed: %s", filename);
249 #define SERVER_JOB_DEBUG
250 #define server_printf(fmt, args...) printf(fmt, ##args)
252 #undef SERVER_JOB_DEBUG
253 #define server_printf(fmt, args...) /* nothing */
256 static snd_pcm_direct_t *server_job_dmix;
258 static void server_cleanup(snd_pcm_direct_t *dmix)
260 close(dmix->server_fd);
262 if (dmix->server_free)
263 dmix->server_free(dmix);
264 unlink(dmix->shmptr->socket_name);
265 _snd_pcm_direct_shm_discard(dmix);
266 snd_pcm_direct_semaphore_discard(dmix);
269 static void server_job_signal(int sig ATTRIBUTE_UNUSED)
271 snd_pcm_direct_semaphore_down(server_job_dmix, DIRECT_IPC_SEM_CLIENT);
272 server_cleanup(server_job_dmix);
273 server_printf("DIRECT SERVER EXIT - SIGNAL\n");
277 /* This is a copy from ../socket.c, provided here only for a server job
278 * (see the comment above)
280 static int _snd_send_fd(int sock, void *data, size_t len, int fd)
283 size_t cmsg_len = CMSG_LEN(sizeof(int));
284 struct cmsghdr *cmsg = alloca(cmsg_len);
285 int *fds = (int *) CMSG_DATA(cmsg);
286 struct msghdr msghdr;
289 vec.iov_base = (void *)&data;
292 cmsg->cmsg_len = cmsg_len;
293 cmsg->cmsg_level = SOL_SOCKET;
294 cmsg->cmsg_type = SCM_RIGHTS;
297 msghdr.msg_name = NULL;
298 msghdr.msg_namelen = 0;
299 msghdr.msg_iov = &vec;
300 msghdr.msg_iovlen = 1;
301 msghdr.msg_control = cmsg;
302 msghdr.msg_controllen = cmsg_len;
303 msghdr.msg_flags = 0;
305 ret = sendmsg(sock, &msghdr, 0 );
311 static void server_job(snd_pcm_direct_t *dmix)
314 int max = 128, current = 0;
315 struct pollfd pfds[max + 1];
317 server_job_dmix = dmix;
318 /* don't allow to be killed */
319 signal(SIGHUP, server_job_signal);
320 signal(SIGQUIT, server_job_signal);
321 signal(SIGTERM, server_job_signal);
322 signal(SIGKILL, server_job_signal);
323 /* close all files to free resources */
324 i = sysconf(_SC_OPEN_MAX);
325 #ifdef SERVER_JOB_DEBUG
330 if (i != dmix->server_fd && i != dmix->hw_fd)
334 /* detach from parent */
337 pfds[0].fd = dmix->server_fd;
338 pfds[0].events = POLLIN | POLLERR | POLLHUP;
340 server_printf("DIRECT SERVER STARTED\n");
342 ret = poll(pfds, current + 1, 500);
343 server_printf("DIRECT SERVER: poll ret = %i, revents[0] = 0x%x, errno = %i\n", ret, pfds[0].revents, errno);
350 if (ret == 0 || (pfds[0].revents & (POLLERR | POLLHUP))) { /* timeout or error? */
352 snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT);
353 if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0) {
354 _snd_pcm_direct_shm_discard(dmix);
355 snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT);
358 server_printf("DIRECT SERVER: nattch = %i\n", (int)buf.shm_nattch);
359 if (buf.shm_nattch == 1) /* server is the last user, exit */
361 snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT);
364 if (pfds[0].revents & POLLIN) {
366 sck = accept(dmix->server_fd, 0, 0);
368 server_printf("DIRECT SERVER: new connection %i\n", sck);
369 if (current == max) {
372 unsigned char buf = 'A';
373 pfds[current+1].fd = sck;
374 pfds[current+1].events = POLLIN | POLLERR | POLLHUP;
375 _snd_send_fd(sck, &buf, 1, dmix->hw_fd);
376 server_printf("DIRECT SERVER: fd sent ok\n");
381 for (i = 0; i < current && ret > 0; i++) {
382 struct pollfd *pfd = &pfds[i+1];
384 server_printf("client %i revents = 0x%x\n", pfd->fd, pfd->revents);
385 if (pfd->revents & (POLLERR | POLLHUP)) {
391 if (!(pfd->revents & POLLIN))
394 if (read(pfd->fd, &cmd, 1) == 1)
395 cmd = 0 /*process command */;
397 for (i = 0; i < current; i++) {
398 if (pfds[i+1].fd < 0) {
400 memcpy(&pfds[i+1], &pfds[i+2], sizeof(struct pollfd) * (max - i - 1));
405 server_cleanup(dmix);
406 server_printf("DIRECT SERVER EXIT\n");
407 #ifdef SERVER_JOB_DEBUG
408 close(0); close(1); close(2);
413 int snd_pcm_direct_server_create(snd_pcm_direct_t *dmix)
417 dmix->server_fd = -1;
419 ret = get_tmp_name(dmix->shmptr->socket_name, sizeof(dmix->shmptr->socket_name));
423 ret = make_local_socket(dmix->shmptr->socket_name, 1, dmix->ipc_perm, dmix->ipc_gid);
426 dmix->server_fd = ret;
428 ret = listen(dmix->server_fd, 4);
430 close(dmix->server_fd);
436 close(dmix->server_fd);
438 } else if (ret == 0) {
444 waitpid(ret, NULL, 0);
446 dmix->server_pid = ret;
451 int snd_pcm_direct_server_discard(snd_pcm_direct_t *dmix)
454 //kill(dmix->server_pid, SIGTERM);
455 //waitpid(dmix->server_pid, NULL, 0);
456 dmix->server_pid = (pid_t)-1;
458 if (dmix->server_fd > 0) {
459 close(dmix->server_fd);
460 dmix->server_fd = -1;
470 int snd_pcm_direct_client_connect(snd_pcm_direct_t *dmix)
475 ret = make_local_socket(dmix->shmptr->socket_name, 0, -1, -1);
480 ret = snd_receive_fd(dmix->comm_fd, &buf, 1, &dmix->hw_fd);
481 if (ret < 1 || buf != 'A') {
482 close(dmix->comm_fd);
491 int snd_pcm_direct_client_discard(snd_pcm_direct_t *dmix)
494 close(dmix->comm_fd);
504 int snd_pcm_direct_nonblock(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int nonblock ATTRIBUTE_UNUSED)
506 /* value is cached for us in pcm->mode (SND_PCM_NONBLOCK flag) */
510 int snd_pcm_direct_async(snd_pcm_t *pcm, int sig, pid_t pid)
512 snd_pcm_direct_t *dmix = pcm->private_data;
513 return snd_timer_async(dmix->timer, sig, pid);
516 /* empty the timer read queue */
517 void snd_pcm_direct_clear_timer_queue(snd_pcm_direct_t *dmix)
519 if (dmix->timer_need_poll) {
520 while (poll(&dmix->timer_fd, 1, 0) > 0) {
521 /* we don't need the value */
523 snd_timer_tread_t rbuf[4];
524 snd_timer_read(dmix->timer, rbuf, sizeof(rbuf));
526 snd_timer_read_t rbuf;
527 snd_timer_read(dmix->timer, &rbuf, sizeof(rbuf));
532 snd_timer_tread_t rbuf[4];
534 while ((len = snd_timer_read(dmix->timer, rbuf,
535 sizeof(rbuf))) > 0 &&
536 len != sizeof(rbuf[0]))
539 snd_timer_read_t rbuf;
540 while (snd_timer_read(dmix->timer, &rbuf, sizeof(rbuf)) > 0)
546 int snd_pcm_direct_timer_stop(snd_pcm_direct_t *dmix)
548 snd_timer_stop(dmix->timer);
552 int snd_pcm_direct_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
554 snd_pcm_direct_t *dmix = pcm->private_data;
555 unsigned short events;
558 assert(pfds && nfds == 1 && revents);
559 events = pfds[0].revents;
560 if (events & POLLIN) {
561 snd_pcm_uframes_t avail;
562 __snd_pcm_avail_update(pcm);
563 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
566 avail = snd_pcm_mmap_playback_avail(pcm);
568 avail = snd_pcm_mmap_capture_avail(pcm);
570 empty = avail < pcm->avail_min;
572 switch (snd_pcm_state(dmix->spcm)) {
573 case SND_PCM_STATE_XRUN:
574 case SND_PCM_STATE_SUSPENDED:
575 case SND_PCM_STATE_SETUP:
580 snd_pcm_direct_clear_timer_queue(dmix);
581 events &= ~(POLLOUT|POLLIN);
582 /* additional check */
583 switch (__snd_pcm_state(pcm)) {
584 case SND_PCM_STATE_XRUN:
585 case SND_PCM_STATE_SUSPENDED:
586 case SND_PCM_STATE_SETUP:
599 int snd_pcm_direct_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
601 snd_pcm_direct_t *dmix = pcm->private_data;
603 if (dmix->spcm && !dmix->shmptr->use_server)
604 return snd_pcm_info(dmix->spcm, info);
606 memset(info, 0, sizeof(*info));
607 info->stream = pcm->stream;
609 /* FIXME: fill this with something more useful: we know the hardware name */
611 strncpy((char *)info->id, pcm->name, sizeof(info->id));
612 strncpy((char *)info->name, pcm->name, sizeof(info->name));
613 strncpy((char *)info->subname, pcm->name, sizeof(info->subname));
615 info->subdevices_count = 1;
619 static inline snd_mask_t *hw_param_mask(snd_pcm_hw_params_t *params,
620 snd_pcm_hw_param_t var)
622 return ¶ms->masks[var - SND_PCM_HW_PARAM_FIRST_MASK];
625 static inline snd_interval_t *hw_param_interval(snd_pcm_hw_params_t *params,
626 snd_pcm_hw_param_t var)
628 return ¶ms->intervals[var - SND_PCM_HW_PARAM_FIRST_INTERVAL];
631 static int hw_param_interval_refine_one(snd_pcm_hw_params_t *params,
632 snd_pcm_hw_param_t var,
637 if (!(params->rmask & (1<<var))) /* nothing to do? */
639 i = hw_param_interval(params, var);
640 if (snd_interval_empty(i)) {
641 SNDERR("dshare interval %i empty?", (int)var);
644 if (snd_interval_refine(i, src))
645 params->cmask |= 1<<var;
649 static int hw_param_interval_refine_minmax(snd_pcm_hw_params_t *params,
650 snd_pcm_hw_param_t var,
656 memset(&t, 0, sizeof(t));
657 snd_interval_set_minmax(&t, imin, imax);
659 return hw_param_interval_refine_one(params, var, &t);
664 int snd_pcm_direct_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
666 snd_pcm_direct_t *dshare = pcm->private_data;
667 static const snd_mask_t access = { .bits = {
668 (1<<SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) |
669 (1<<SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED) |
670 (1<<SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
671 (1<<SNDRV_PCM_ACCESS_RW_NONINTERLEAVED),
677 snd_output_stdio_attach(&log, stderr, 0);
678 snd_output_puts(log, "DMIX REFINE (begin):\n");
679 snd_pcm_hw_params_dump(params, log);
681 if (params->rmask & (1<<SND_PCM_HW_PARAM_ACCESS)) {
682 if (snd_mask_empty(hw_param_mask(params, SND_PCM_HW_PARAM_ACCESS))) {
683 SNDERR("dshare access mask empty?");
686 if (snd_mask_refine(hw_param_mask(params, SND_PCM_HW_PARAM_ACCESS), &access))
687 params->cmask |= 1<<SND_PCM_HW_PARAM_ACCESS;
689 if (params->rmask & (1<<SND_PCM_HW_PARAM_FORMAT)) {
690 if (snd_mask_empty(hw_param_mask(params, SND_PCM_HW_PARAM_FORMAT))) {
691 SNDERR("dshare format mask empty?");
694 if (snd_mask_refine_set(hw_param_mask(params, SND_PCM_HW_PARAM_FORMAT),
695 dshare->shmptr->hw.format))
696 params->cmask |= 1<<SND_PCM_HW_PARAM_FORMAT;
698 //snd_mask_none(hw_param_mask(params, SND_PCM_HW_PARAM_SUBFORMAT));
699 if (params->rmask & (1<<SND_PCM_HW_PARAM_CHANNELS)) {
700 if (snd_interval_empty(hw_param_interval(params, SND_PCM_HW_PARAM_CHANNELS))) {
701 SNDERR("dshare channels mask empty?");
704 err = snd_interval_refine_set(hw_param_interval(params, SND_PCM_HW_PARAM_CHANNELS), dshare->channels);
708 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_RATE,
709 &dshare->shmptr->hw.rate);
712 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_PERIOD_SIZE,
713 &dshare->shmptr->hw.period_size);
716 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_PERIOD_TIME,
717 &dshare->shmptr->hw.period_time);
720 if (dshare->max_periods < 0) {
721 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_BUFFER_SIZE,
722 &dshare->shmptr->hw.buffer_size);
725 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_BUFFER_TIME,
726 &dshare->shmptr->hw.buffer_time);
729 } else if (params->rmask & ((1<<SND_PCM_HW_PARAM_PERIODS)|
730 (1<<SND_PCM_HW_PARAM_BUFFER_BYTES)|
731 (1<<SND_PCM_HW_PARAM_BUFFER_SIZE)|
732 (1<<SND_PCM_HW_PARAM_BUFFER_TIME))) {
734 unsigned int max_periods = dshare->max_periods;
736 max_periods = dshare->slave_buffer_size / dshare->slave_period_size;
739 err = hw_param_interval_refine_minmax(params, SND_PCM_HW_PARAM_PERIODS,
744 err = snd_pcm_hw_refine_soft(pcm, params);
750 params->info = dshare->shmptr->s.info;
752 snd_output_puts(log, "DMIX REFINE (end):\n");
753 snd_pcm_hw_params_dump(params, log);
754 snd_output_close(log);
759 int snd_pcm_direct_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
761 snd_pcm_direct_t *dmix = pcm->private_data;
763 params->info = dmix->shmptr->s.info;
764 params->rate_num = dmix->shmptr->s.rate;
765 params->rate_den = 1;
766 params->fifo_size = 0;
767 params->msbits = dmix->shmptr->s.msbits;
771 int snd_pcm_direct_hw_free(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
773 /* values are cached in the pcm structure */
777 int snd_pcm_direct_sw_params(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t * params ATTRIBUTE_UNUSED)
779 /* values are cached in the pcm structure */
783 int snd_pcm_direct_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
785 return snd_pcm_channel_info_shm(pcm, info, -1);
788 int snd_pcm_direct_mmap(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
793 int snd_pcm_direct_munmap(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
798 snd_pcm_chmap_query_t **snd_pcm_direct_query_chmaps(snd_pcm_t *pcm)
800 snd_pcm_direct_t *dmix = pcm->private_data;
801 return snd_pcm_query_chmaps(dmix->spcm);
804 snd_pcm_chmap_t *snd_pcm_direct_get_chmap(snd_pcm_t *pcm)
806 snd_pcm_direct_t *dmix = pcm->private_data;
807 return snd_pcm_get_chmap(dmix->spcm);
810 int snd_pcm_direct_set_chmap(snd_pcm_t *pcm, const snd_pcm_chmap_t *map)
812 snd_pcm_direct_t *dmix = pcm->private_data;
813 return snd_pcm_set_chmap(dmix->spcm, map);
816 int snd_pcm_direct_prepare(snd_pcm_t *pcm)
818 snd_pcm_direct_t *dmix = pcm->private_data;
821 switch (snd_pcm_state(dmix->spcm)) {
822 case SND_PCM_STATE_SETUP:
823 case SND_PCM_STATE_XRUN:
824 case SND_PCM_STATE_SUSPENDED:
825 err = snd_pcm_prepare(dmix->spcm);
828 snd_pcm_start(dmix->spcm);
830 case SND_PCM_STATE_OPEN:
831 case SND_PCM_STATE_DISCONNECTED:
836 snd_pcm_direct_check_interleave(dmix, pcm);
837 dmix->state = SND_PCM_STATE_PREPARED;
838 dmix->appl_ptr = dmix->last_appl_ptr = 0;
840 return snd_pcm_direct_set_timer_params(dmix);
843 int snd_pcm_direct_resume(snd_pcm_t *pcm)
845 snd_pcm_direct_t *dmix = pcm->private_data;
846 snd_pcm_t *spcm = dmix->spcm;
848 snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT);
849 /* some buggy drivers require the device resumed before prepared;
850 * when a device has RESUME flag and is in SUSPENDED state, resume
851 * here but immediately drop to bring it to a sane active state.
853 if ((spcm->info & SND_PCM_INFO_RESUME) &&
854 snd_pcm_state(spcm) == SND_PCM_STATE_SUSPENDED) {
855 snd_pcm_resume(spcm);
857 snd_pcm_direct_timer_stop(dmix);
858 snd_pcm_direct_clear_timer_queue(dmix);
859 snd_pcm_areas_silence(snd_pcm_mmap_areas(spcm), 0,
860 spcm->channels, spcm->buffer_size,
862 snd_pcm_prepare(spcm);
865 snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT);
869 #define COPY_SLAVE(field) (dmix->shmptr->s.field = spcm->field)
871 /* copy the slave setting */
872 static void save_slave_setting(snd_pcm_direct_t *dmix, snd_pcm_t *spcm)
874 spcm->info &= ~SND_PCM_INFO_PAUSE;
878 COPY_SLAVE(subformat);
879 COPY_SLAVE(channels);
881 COPY_SLAVE(period_size);
882 COPY_SLAVE(period_time);
884 COPY_SLAVE(tstamp_mode);
885 COPY_SLAVE(tstamp_type);
886 COPY_SLAVE(period_step);
887 COPY_SLAVE(avail_min);
888 COPY_SLAVE(start_threshold);
889 COPY_SLAVE(stop_threshold);
890 COPY_SLAVE(silence_threshold);
891 COPY_SLAVE(silence_size);
892 COPY_SLAVE(boundary);
895 COPY_SLAVE(rate_num);
896 COPY_SLAVE(rate_den);
897 COPY_SLAVE(hw_flags);
898 COPY_SLAVE(fifo_size);
899 COPY_SLAVE(buffer_size);
900 COPY_SLAVE(buffer_time);
901 COPY_SLAVE(sample_bits);
902 COPY_SLAVE(frame_bits);
904 dmix->shmptr->s.info &= ~SND_PCM_INFO_RESUME;
910 * this function initializes hardware and starts playback operation with
911 * no stop threshold (it operates all time without xrun checking)
912 * also, the driver silences the unused ring buffer areas for us
914 int snd_pcm_direct_initialize_slave(snd_pcm_direct_t *dmix, snd_pcm_t *spcm, struct slave_params *params)
916 snd_pcm_hw_params_t hw_params = {0};
917 snd_pcm_sw_params_t sw_params = {0};
918 int ret, buffer_is_not_initialized;
919 snd_pcm_uframes_t boundary;
925 SNDERR("unable to find a valid configuration for slave");
928 ret = snd_pcm_hw_params_any(spcm, &hw_params);
930 SNDERR("snd_pcm_hw_params_any failed");
933 ret = snd_pcm_hw_params_set_access(spcm, &hw_params,
934 SND_PCM_ACCESS_MMAP_INTERLEAVED);
936 ret = snd_pcm_hw_params_set_access(spcm, &hw_params,
937 SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
939 SNDERR("slave plugin does not support mmap interleaved or mmap noninterleaved access");
943 if (params->format == SND_PCM_FORMAT_UNKNOWN)
946 ret = snd_pcm_hw_params_set_format(spcm, &hw_params,
949 static const snd_pcm_format_t dmix_formats[] = {
951 SND_PCM_FORMAT_S32 ^ SND_PCM_FORMAT_S32_LE ^
952 SND_PCM_FORMAT_S32_BE,
954 SND_PCM_FORMAT_S16 ^ SND_PCM_FORMAT_S16_LE ^
955 SND_PCM_FORMAT_S16_BE,
956 SND_PCM_FORMAT_S24_LE,
957 SND_PCM_FORMAT_S24_3LE,
960 snd_pcm_format_t format;
963 for (i = 0; i < ARRAY_SIZE(dmix_formats); ++i) {
964 format = dmix_formats[i];
965 ret = snd_pcm_hw_params_set_format(spcm, &hw_params,
970 if (ret < 0 && dmix->type != SND_PCM_TYPE_DMIX) {
971 /* TODO: try to choose a good format */
972 ret = INTERNAL(snd_pcm_hw_params_set_format_first)(spcm,
973 &hw_params, &format);
976 SNDERR("requested or auto-format is not available");
979 params->format = format;
981 ret = INTERNAL(snd_pcm_hw_params_set_channels_near)(spcm, &hw_params,
982 (unsigned int *)¶ms->channels);
984 SNDERR("requested count of channels is not available");
987 ret = INTERNAL(snd_pcm_hw_params_set_rate_near)(spcm, &hw_params,
988 (unsigned int *)¶ms->rate, 0);
990 SNDERR("requested rate is not available");
994 buffer_is_not_initialized = 0;
995 if (params->buffer_time > 0) {
996 ret = INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(spcm,
997 &hw_params, (unsigned int *)¶ms->buffer_time, 0);
999 SNDERR("unable to set buffer time");
1002 } else if (params->buffer_size > 0) {
1003 ret = INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(spcm,
1004 &hw_params, (snd_pcm_uframes_t *)¶ms->buffer_size);
1006 SNDERR("unable to set buffer size");
1010 buffer_is_not_initialized = 1;
1013 if (params->period_time > 0) {
1014 ret = INTERNAL(snd_pcm_hw_params_set_period_time_near)(spcm,
1015 &hw_params, (unsigned int *)¶ms->period_time, 0);
1017 SNDERR("unable to set period_time");
1020 } else if (params->period_size > 0) {
1021 ret = INTERNAL(snd_pcm_hw_params_set_period_size_near)(spcm,
1022 &hw_params, (snd_pcm_uframes_t *)¶ms->period_size,
1025 SNDERR("unable to set period_size");
1030 if (buffer_is_not_initialized && params->periods > 0) {
1031 unsigned int periods = params->periods;
1032 ret = INTERNAL(snd_pcm_hw_params_set_periods_near)(spcm,
1033 &hw_params, ¶ms->periods, 0);
1035 SNDERR("unable to set requested periods");
1038 if (params->periods == 1) {
1039 params->periods = periods;
1040 if (params->period_time > 0) {
1041 params->period_time /= 2;
1043 } else if (params->period_size > 0) {
1044 params->period_size /= 2;
1047 SNDERR("unable to use stream with periods == 1");
1052 ret = snd_pcm_hw_params(spcm, &hw_params);
1054 SNDERR("unable to install hw params");
1058 /* store some hw_params values to shared info */
1059 dmix->shmptr->hw.format =
1060 snd_mask_value(hw_param_mask(&hw_params,
1061 SND_PCM_HW_PARAM_FORMAT));
1062 dmix->shmptr->hw.rate =
1063 *hw_param_interval(&hw_params, SND_PCM_HW_PARAM_RATE);
1064 dmix->shmptr->hw.buffer_size =
1065 *hw_param_interval(&hw_params, SND_PCM_HW_PARAM_BUFFER_SIZE);
1066 dmix->shmptr->hw.buffer_time =
1067 *hw_param_interval(&hw_params, SND_PCM_HW_PARAM_BUFFER_TIME);
1068 dmix->shmptr->hw.period_size =
1069 *hw_param_interval(&hw_params, SND_PCM_HW_PARAM_PERIOD_SIZE);
1070 dmix->shmptr->hw.period_time =
1071 *hw_param_interval(&hw_params, SND_PCM_HW_PARAM_PERIOD_TIME);
1072 dmix->shmptr->hw.periods =
1073 *hw_param_interval(&hw_params, SND_PCM_HW_PARAM_PERIODS);
1076 ret = snd_pcm_sw_params_current(spcm, &sw_params);
1078 SNDERR("unable to get current sw_params");
1082 ret = snd_pcm_sw_params_get_boundary(&sw_params, &boundary);
1084 SNDERR("unable to get boundary");
1087 ret = snd_pcm_sw_params_set_stop_threshold(spcm, &sw_params, boundary);
1089 SNDERR("unable to set stop threshold");
1093 /* set timestamp mode to MMAP
1094 * the slave timestamp is copied appropriately in dsnoop/dmix/dshare
1095 * based on the tstamp_mode of each client
1097 ret = snd_pcm_sw_params_set_tstamp_mode(spcm, &sw_params,
1098 SND_PCM_TSTAMP_ENABLE);
1100 SNDERR("unable to tstamp mode MMAP");
1104 if (dmix->type != SND_PCM_TYPE_DMIX)
1105 goto __skip_silencing;
1107 ret = snd_pcm_sw_params_set_silence_threshold(spcm, &sw_params, 0);
1109 SNDERR("unable to set silence threshold");
1112 ret = snd_pcm_sw_params_set_silence_size(spcm, &sw_params, boundary);
1114 SNDERR("unable to set silence threshold (please upgrade to 0.9.0rc8+ driver)");
1120 ret = snd_pcm_sw_params(spcm, &sw_params);
1122 SNDERR("unable to install sw params (please upgrade to 0.9.0rc8+ driver)");
1126 if (dmix->type == SND_PCM_TYPE_DSHARE) {
1127 const snd_pcm_channel_area_t *dst_areas;
1128 dst_areas = snd_pcm_mmap_areas(spcm);
1129 snd_pcm_areas_silence(dst_areas, 0, spcm->channels,
1130 spcm->buffer_size, spcm->format);
1133 ret = snd_pcm_start(spcm);
1135 SNDERR("unable to start PCM stream");
1139 if (snd_pcm_poll_descriptors_count(spcm) != 1) {
1140 SNDERR("unable to use hardware pcm with fd more than one!!!");
1143 snd_pcm_poll_descriptors(spcm, &fd, 1);
1144 dmix->hw_fd = fd.fd;
1146 save_slave_setting(dmix, spcm);
1148 /* Currently, we assume that each dmix client has the same
1149 * hw_params setting.
1150 * If the arbitrary hw_parmas is supported in future,
1151 * boundary has to be taken from the slave config but
1152 * recalculated for the native boundary size (for 32bit
1153 * emulation on 64bit arch).
1155 dmix->slave_buffer_size = spcm->buffer_size;
1156 dmix->slave_period_size = spcm->period_size;
1157 dmix->slave_boundary = spcm->boundary;
1159 spcm->donot_close = 1;
1163 ioctl(spcm->poll_fd, SNDRV_PCM_IOCTL_PVERSION, &ver);
1164 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 8))
1165 dmix->shmptr->use_server = 1;
1172 * the trick is used here; we cannot use effectively the hardware handle because
1173 * we cannot drive multiple accesses to appl_ptr; so we use slave timer of given
1174 * PCM hardware handle; it's not this easy and cheap?
1176 int snd_pcm_direct_initialize_poll_fd(snd_pcm_direct_t *dmix)
1179 snd_pcm_info_t *info;
1181 int capture = dmix->type == SND_PCM_TYPE_DSNOOP ? 1 : 0;
1184 dmix->timer_need_poll = 0;
1185 snd_pcm_info_alloca(&info);
1186 ret = snd_pcm_info(dmix->spcm, info);
1188 SNDERR("unable to info for slave pcm");
1191 sprintf(name, "hw:CLASS=%i,SCLASS=0,CARD=%i,DEV=%i,SUBDEV=%i",
1192 (int)SND_TIMER_CLASS_PCM,
1193 snd_pcm_info_get_card(info),
1194 snd_pcm_info_get_device(info),
1195 snd_pcm_info_get_subdevice(info) * 2 + capture);
1196 ret = snd_timer_open(&dmix->timer, name,
1197 SND_TIMER_OPEN_NONBLOCK | SND_TIMER_OPEN_TREAD);
1200 ret = snd_timer_open(&dmix->timer, name,
1201 SND_TIMER_OPEN_NONBLOCK);
1203 SNDERR("unable to open timer '%s'", name);
1208 if (snd_timer_poll_descriptors_count(dmix->timer) != 1) {
1209 SNDERR("unable to use timer '%s' with more than one fd!", name);
1212 snd_timer_poll_descriptors(dmix->timer, &dmix->timer_fd, 1);
1213 dmix->poll_fd = dmix->timer_fd.fd;
1215 dmix->timer_events = (1<<SND_TIMER_EVENT_MSUSPEND) |
1216 (1<<SND_TIMER_EVENT_MRESUME) |
1217 (1<<SND_TIMER_EVENT_MSTOP) |
1218 (1<<SND_TIMER_EVENT_STOP);
1221 * Some hacks for older kernel drivers
1225 ioctl(dmix->poll_fd, SNDRV_TIMER_IOCTL_PVERSION, &ver);
1226 /* In older versions, check via poll before read() is needed
1227 * because of the confliction between TIMER_START and
1230 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 4))
1231 dmix->timer_need_poll = 1;
1233 * In older versions, timer uses pause events instead
1234 * suspend/resume events.
1236 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 5)) {
1237 dmix->timer_events &= ~((1<<SND_TIMER_EVENT_MSUSPEND) |
1238 (1<<SND_TIMER_EVENT_MRESUME));
1239 dmix->timer_events |= (1<<SND_TIMER_EVENT_MPAUSE) |
1240 (1<<SND_TIMER_EVENT_MCONTINUE);
1242 /* In older versions, use SND_TIMER_EVENT_START too.
1244 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 6))
1245 dmix->timer_events |= 1<<SND_TIMER_EVENT_START;
1250 static snd_pcm_uframes_t recalc_boundary_size(unsigned long long bsize, snd_pcm_uframes_t buffer_size)
1252 if (bsize > LONG_MAX) {
1253 bsize = buffer_size;
1254 while (bsize * 2 <= LONG_MAX - buffer_size)
1257 return (snd_pcm_uframes_t)bsize;
1260 #define COPY_SLAVE(field) (spcm->field = dmix->shmptr->s.field)
1262 /* copy the slave setting */
1263 static void copy_slave_setting(snd_pcm_direct_t *dmix, snd_pcm_t *spcm)
1267 COPY_SLAVE(subformat);
1268 COPY_SLAVE(channels);
1270 COPY_SLAVE(period_size);
1271 COPY_SLAVE(period_time);
1272 COPY_SLAVE(periods);
1273 COPY_SLAVE(tstamp_mode);
1274 COPY_SLAVE(tstamp_type);
1275 COPY_SLAVE(period_step);
1276 COPY_SLAVE(avail_min);
1277 COPY_SLAVE(start_threshold);
1278 COPY_SLAVE(stop_threshold);
1279 COPY_SLAVE(silence_threshold);
1280 COPY_SLAVE(silence_size);
1281 COPY_SLAVE(boundary);
1284 COPY_SLAVE(rate_num);
1285 COPY_SLAVE(rate_den);
1286 COPY_SLAVE(hw_flags);
1287 COPY_SLAVE(fifo_size);
1288 COPY_SLAVE(buffer_size);
1289 COPY_SLAVE(buffer_time);
1290 COPY_SLAVE(sample_bits);
1291 COPY_SLAVE(frame_bits);
1293 spcm->info &= ~SND_PCM_INFO_PAUSE;
1294 spcm->boundary = recalc_boundary_size(dmix->shmptr->s.boundary, spcm->buffer_size);
1301 * open a slave PCM as secondary client (dup'ed fd)
1303 int snd_pcm_direct_open_secondary_client(snd_pcm_t **spcmp, snd_pcm_direct_t *dmix, const char *client_name)
1308 ret = snd_pcm_hw_open_fd(spcmp, client_name, dmix->hw_fd, 0, 0);
1310 SNDERR("unable to open hardware");
1315 spcm->donot_close = 1;
1318 copy_slave_setting(dmix, spcm);
1320 /* Use the slave setting as SPCM, so far */
1321 dmix->slave_buffer_size = spcm->buffer_size;
1322 dmix->slave_period_size = dmix->shmptr->s.period_size;
1323 dmix->slave_boundary = spcm->boundary;
1325 ret = snd_pcm_mmap(spcm);
1327 SNDERR("unable to mmap channels");
1334 * open a slave PCM as secondary client (dup'ed fd)
1336 int snd_pcm_direct_initialize_secondary_slave(snd_pcm_direct_t *dmix,
1338 struct slave_params *params ATTRIBUTE_UNUSED)
1342 spcm->donot_close = 1;
1345 copy_slave_setting(dmix, spcm);
1347 /* Use the slave setting as SPCM, so far */
1348 dmix->slave_buffer_size = spcm->buffer_size;
1349 dmix->slave_period_size = dmix->shmptr->s.period_size;
1350 dmix->slave_boundary = spcm->boundary;
1352 ret = snd_pcm_mmap(spcm);
1354 SNDERR("unable to mmap channels");
1360 int snd_pcm_direct_set_timer_params(snd_pcm_direct_t *dmix)
1362 snd_timer_params_t *params;
1363 unsigned int filter;
1366 snd_timer_params_alloca(¶ms);
1367 snd_timer_params_set_auto_start(params, 1);
1368 if (dmix->type != SND_PCM_TYPE_DSNOOP)
1369 snd_timer_params_set_early_event(params, 1);
1370 snd_timer_params_set_ticks(params, 1);
1372 filter = (1<<SND_TIMER_EVENT_TICK) |
1374 snd_timer_params_set_filter(params, filter);
1376 ret = snd_timer_params(dmix->timer, params);
1378 SNDERR("unable to set timer parameters");
1385 * ring buffer operation
1387 int snd_pcm_direct_check_interleave(snd_pcm_direct_t *dmix, snd_pcm_t *pcm)
1389 unsigned int chn, channels;
1390 int bits, interleaved = 1;
1391 const snd_pcm_channel_area_t *dst_areas;
1392 const snd_pcm_channel_area_t *src_areas;
1394 bits = snd_pcm_format_physical_width(pcm->format);
1395 if ((bits % 8) != 0)
1397 channels = dmix->channels;
1398 dst_areas = snd_pcm_mmap_areas(dmix->spcm);
1399 src_areas = snd_pcm_mmap_areas(pcm);
1400 for (chn = 1; chn < channels; chn++) {
1401 if (dst_areas[chn-1].addr != dst_areas[chn].addr) {
1405 if (src_areas[chn-1].addr != src_areas[chn].addr) {
1410 for (chn = 0; chn < channels; chn++) {
1411 if (dmix->bindings && dmix->bindings[chn] != chn) {
1415 if (dst_areas[chn].first != chn * bits ||
1416 dst_areas[chn].step != channels * bits) {
1420 if (src_areas[chn].first != chn * bits ||
1421 src_areas[chn].step != channels * bits) {
1426 return dmix->interleaved = interleaved;
1430 * parse the channel map
1431 * id == client channel
1432 * value == slave's channel
1434 int snd_pcm_direct_parse_bindings(snd_pcm_direct_t *dmix,
1435 struct slave_params *params,
1438 snd_config_iterator_t i, next;
1439 unsigned int chn, chn1, count = 0;
1440 unsigned int *bindings;
1443 dmix->channels = UINT_MAX;
1446 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1447 SNDERR("invalid type for bindings");
1450 snd_config_for_each(i, next, cfg) {
1451 snd_config_t *n = snd_config_iterator_entry(i);
1454 if (snd_config_get_id(n, &id) < 0)
1456 err = safe_strtol(id, &cchannel);
1457 if (err < 0 || cchannel < 0) {
1458 SNDERR("invalid client channel in binding: %s\n", id);
1461 if ((unsigned)cchannel >= count)
1462 count = cchannel + 1;
1467 SNDERR("client channel out of range");
1470 bindings = malloc(count * sizeof(unsigned int));
1471 if (bindings == NULL)
1473 for (chn = 0; chn < count; chn++)
1474 bindings[chn] = UINT_MAX; /* don't route */
1475 snd_config_for_each(i, next, cfg) {
1476 snd_config_t *n = snd_config_iterator_entry(i);
1478 long cchannel, schannel;
1479 if (snd_config_get_id(n, &id) < 0)
1481 safe_strtol(id, &cchannel);
1482 if (snd_config_get_integer(n, &schannel) < 0) {
1483 SNDERR("unable to get slave channel (should be integer type) in binding: %s\n", id);
1487 if (schannel < 0 || schannel >= params->channels) {
1488 SNDERR("invalid slave channel number %ld in binding to %ld",
1489 schannel, cchannel);
1493 bindings[cchannel] = schannel;
1495 if (dmix->type == SND_PCM_TYPE_DSNOOP ||
1497 goto __skip_same_dst;
1498 for (chn = 0; chn < count; chn++) {
1499 for (chn1 = 0; chn1 < count; chn1++) {
1502 if (bindings[chn] == dmix->bindings[chn1]) {
1503 SNDERR("unable to route channels %d,%d to same destination %d", chn, chn1, bindings[chn]);
1510 dmix->bindings = bindings;
1511 dmix->channels = count;
1516 * parse slave config and calculate the ipc_key offset
1519 static int _snd_pcm_direct_get_slave_ipc_offset(snd_config_t *root,
1520 snd_config_t *sconf,
1524 snd_config_iterator_t i, next;
1525 snd_config_t *pcm_conf, *pcm_conf2;
1527 long card = 0, device = 0, subdevice = 0;
1530 if (snd_config_get_string(sconf, &str) >= 0) {
1531 if (hop > SND_CONF_MAX_HOPS) {
1532 SNDERR("Too many definition levels (looped?)");
1535 err = snd_config_search_definition(root, "pcm", str, &pcm_conf);
1537 SNDERR("Unknown slave PCM %s", str);
1540 err = _snd_pcm_direct_get_slave_ipc_offset(root, pcm_conf,
1543 snd_config_delete(pcm_conf);
1547 #if 0 /* for debug purposes */
1550 snd_output_stdio_attach(&out, stderr, 0);
1551 snd_config_save(sconf, out);
1552 snd_output_close(out);
1556 if (snd_config_search(sconf, "slave", &pcm_conf) >= 0) {
1557 if (snd_config_search(pcm_conf, "pcm", &pcm_conf) >= 0) {
1558 return _snd_pcm_direct_get_slave_ipc_offset(root,
1563 if (snd_config_get_string(pcm_conf, &str) >= 0 &&
1564 snd_config_search_definition(root, "pcm_slave",
1565 str, &pcm_conf) >= 0) {
1566 if (snd_config_search(pcm_conf, "pcm",
1569 _snd_pcm_direct_get_slave_ipc_offset(
1570 root, pcm_conf2, direction, hop + 1);
1571 snd_config_delete(pcm_conf);
1574 snd_config_delete(pcm_conf);
1579 snd_config_for_each(i, next, sconf) {
1580 snd_config_t *n = snd_config_iterator_entry(i);
1581 const char *id, *str;
1582 if (snd_config_get_id(n, &id) < 0)
1584 if (strcmp(id, "type") == 0) {
1585 err = snd_config_get_string(n, &str);
1587 SNDERR("Invalid value for PCM type definition\n");
1590 if (strcmp(str, "hw")) {
1591 SNDERR("Invalid type '%s' for slave PCM\n", str);
1596 if (strcmp(id, "card") == 0) {
1597 err = snd_config_get_integer(n, &card);
1599 err = snd_config_get_string(n, &str);
1601 SNDERR("Invalid type for %s", id);
1604 card = snd_card_get_index(str);
1606 SNDERR("Invalid value for %s", id);
1612 if (strcmp(id, "device") == 0) {
1613 err = snd_config_get_integer(n, &device);
1615 SNDERR("Invalid type for %s", id);
1620 if (strcmp(id, "subdevice") == 0) {
1621 err = snd_config_get_integer(n, &subdevice);
1623 SNDERR("Invalid type for %s", id);
1635 return (direction << 1) + (device << 2) + (subdevice << 8) + (card << 12);
1638 static int snd_pcm_direct_get_slave_ipc_offset(snd_config_t *root,
1639 snd_config_t *sconf,
1642 return _snd_pcm_direct_get_slave_ipc_offset(root, sconf, direction, 0);
1645 int snd_pcm_direct_parse_open_conf(snd_config_t *root, snd_config_t *conf,
1646 int stream, struct snd_pcm_direct_open_conf *rec)
1648 snd_config_iterator_t i, next;
1649 int ipc_key_add_uid = 0;
1654 rec->bindings = NULL;
1656 rec->ipc_perm = 0600;
1659 rec->max_periods = 0;
1662 if (snd_config_search(root, "defaults.pcm.dmix_max_periods", &n) >= 0) {
1664 err = snd_config_get_integer(n, &val);
1666 rec->max_periods = val;
1669 snd_config_for_each(i, next, conf) {
1671 n = snd_config_iterator_entry(i);
1672 if (snd_config_get_id(n, &id) < 0)
1674 if (snd_pcm_conf_generic_id(id))
1676 if (strcmp(id, "ipc_key") == 0) {
1678 err = snd_config_get_integer(n, &key);
1680 SNDERR("The field ipc_key must be an integer type");
1687 if (strcmp(id, "ipc_perm") == 0) {
1689 err = snd_config_get_integer(n, &perm);
1691 SNDERR("Invalid type for %s", id);
1694 if ((perm & ~0777) != 0) {
1695 SNDERR("The field ipc_perm must be a valid file permission");
1698 rec->ipc_perm = perm;
1701 if (strcmp(id, "ipc_gid") == 0) {
1704 err = snd_config_get_ascii(n, &group);
1706 SNDERR("The field ipc_gid must be a valid group");
1714 if (isdigit(*group) == 0) {
1715 long clen = sysconf(_SC_GETGR_R_SIZE_MAX);
1716 size_t len = (clen == -1) ? 1024 : (size_t)clen;
1717 struct group grp, *pgrp;
1718 char *buffer = (char *)malloc(len);
1721 int st = getgrnam_r(group, &grp, buffer, len, &pgrp);
1722 if (st != 0 || !pgrp) {
1723 SNDERR("The field ipc_gid must be a valid group (create group %s)", group);
1727 rec->ipc_gid = pgrp->gr_gid;
1730 rec->ipc_gid = strtol(group, &endp, 10);
1735 if (strcmp(id, "ipc_key_add_uid") == 0) {
1736 if ((err = snd_config_get_bool(n)) < 0) {
1737 SNDERR("The field ipc_key_add_uid must be a boolean type");
1740 ipc_key_add_uid = err;
1743 if (strcmp(id, "slave") == 0) {
1747 if (strcmp(id, "bindings") == 0) {
1751 if (strcmp(id, "slowptr") == 0) {
1752 err = snd_config_get_bool(n);
1758 if (strcmp(id, "max_periods") == 0) {
1760 err = snd_config_get_integer(n, &val);
1763 rec->max_periods = val;
1766 SNDERR("Unknown field %s", id);
1770 SNDERR("slave is not defined");
1773 if (!rec->ipc_key) {
1774 SNDERR("Unique IPC key is not defined");
1777 if (ipc_key_add_uid)
1778 rec->ipc_key += getuid();
1779 err = snd_pcm_direct_get_slave_ipc_offset(root, conf, stream);
1782 rec->ipc_key += err;