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)
97 dmix->shmid = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t),
98 IPC_CREAT | dmix->ipc_perm);
100 if (dmix->shmid < 0){
102 if ((tmpid = shmget(dmix->ipc_key, 0, dmix->ipc_perm)) != -1)
103 if (!shmctl(tmpid, IPC_STAT, &buf))
105 /* no users so destroy the segment */
106 if (!shmctl(tmpid, IPC_RMID, NULL))
110 dmix->shmptr = shmat(dmix->shmid, 0, 0);
111 if (dmix->shmptr == (void *) -1) {
113 snd_pcm_direct_shm_discard(dmix);
116 mlock(dmix->shmptr, sizeof(snd_pcm_direct_share_t));
117 if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0) {
119 snd_pcm_direct_shm_discard(dmix);
122 if (buf.shm_nattch == 1) { /* we're the first user, clear the segment */
123 memset(dmix->shmptr, 0, sizeof(snd_pcm_direct_share_t));
124 if (dmix->ipc_gid >= 0) {
125 buf.shm_perm.gid = dmix->ipc_gid;
126 shmctl(dmix->shmid, IPC_SET, &buf);
128 dmix->shmptr->magic = SND_PCM_DIRECT_MAGIC;
131 if (dmix->shmptr->magic != SND_PCM_DIRECT_MAGIC) {
132 snd_pcm_direct_shm_discard(dmix);
139 /* discard shared memory */
141 * Define snd_* functions to be used in server.
142 * Since objects referred in a plugin can be released dynamically, a forked
143 * server should have statically linked functions.
144 * (e.g. Novell bugzilla #105772)
146 static int _snd_pcm_direct_shm_discard(snd_pcm_direct_t *dmix)
153 if (dmix->shmptr != (void *) -1 && shmdt(dmix->shmptr) < 0)
155 dmix->shmptr = (void *) -1;
156 if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0)
158 if (buf.shm_nattch == 0) { /* we're the last user, destroy the segment */
159 if (shmctl(dmix->shmid, IPC_RMID, NULL) < 0)
167 /* ... and an exported version */
168 int snd_pcm_direct_shm_discard(snd_pcm_direct_t *dmix)
170 return _snd_pcm_direct_shm_discard(dmix);
177 static int get_tmp_name(char *filename, size_t size)
181 gettimeofday(&tv, NULL);
182 snprintf(filename, size, TMPDIR "/alsa-dmix-%i-%li-%li", (int)getpid(), (long)tv.tv_sec, (long)tv.tv_usec);
183 filename[size-1] = '\0';
187 static int make_local_socket(const char *filename, int server, mode_t ipc_perm, int ipc_gid)
189 size_t l = strlen(filename);
190 size_t size = offsetof(struct sockaddr_un, sun_path) + l;
191 struct sockaddr_un *addr = alloca(size);
194 sock = socket(PF_LOCAL, SOCK_STREAM, 0);
197 SYSERR("socket failed");
203 memset(addr, 0, size); /* make valgrind happy */
204 addr->sun_family = AF_LOCAL;
205 memcpy(addr->sun_path, filename, l);
208 if (bind(sock, (struct sockaddr *) addr, size) < 0) {
210 SYSERR("bind failed: %s", filename);
214 if (chmod(filename, ipc_perm) < 0) {
216 SYSERR("chmod failed: %s", filename);
221 if (chown(filename, -1, ipc_gid) < 0) {
222 #if 0 /* it's not fatal */
224 SYSERR("chown failed: %s", filename);
232 if (connect(sock, (struct sockaddr *) addr, size) < 0) {
234 SYSERR("connect failed: %s", filename);
243 #define SERVER_JOB_DEBUG
244 #define server_printf(fmt, args...) printf(fmt, ##args)
246 #undef SERVER_JOB_DEBUG
247 #define server_printf(fmt, args...) /* nothing */
250 static snd_pcm_direct_t *server_job_dmix;
252 static void server_cleanup(snd_pcm_direct_t *dmix)
254 close(dmix->server_fd);
256 if (dmix->server_free)
257 dmix->server_free(dmix);
258 unlink(dmix->shmptr->socket_name);
259 _snd_pcm_direct_shm_discard(dmix);
260 snd_pcm_direct_semaphore_discard(dmix);
263 static void server_job_signal(int sig ATTRIBUTE_UNUSED)
265 snd_pcm_direct_semaphore_down(server_job_dmix, DIRECT_IPC_SEM_CLIENT);
266 server_cleanup(server_job_dmix);
267 server_printf("DIRECT SERVER EXIT - SIGNAL\n");
271 /* This is a copy from ../socket.c, provided here only for a server job
272 * (see the comment above)
274 static int _snd_send_fd(int sock, void *data, size_t len, int fd)
277 size_t cmsg_len = CMSG_LEN(sizeof(int));
278 struct cmsghdr *cmsg = alloca(cmsg_len);
279 int *fds = (int *) CMSG_DATA(cmsg);
280 struct msghdr msghdr;
283 vec.iov_base = (void *)&data;
286 cmsg->cmsg_len = cmsg_len;
287 cmsg->cmsg_level = SOL_SOCKET;
288 cmsg->cmsg_type = SCM_RIGHTS;
291 msghdr.msg_name = NULL;
292 msghdr.msg_namelen = 0;
293 msghdr.msg_iov = &vec;
294 msghdr.msg_iovlen = 1;
295 msghdr.msg_control = cmsg;
296 msghdr.msg_controllen = cmsg_len;
297 msghdr.msg_flags = 0;
299 ret = sendmsg(sock, &msghdr, 0 );
305 static void server_job(snd_pcm_direct_t *dmix)
308 int max = 128, current = 0;
309 struct pollfd pfds[max + 1];
311 server_job_dmix = dmix;
312 /* don't allow to be killed */
313 signal(SIGHUP, server_job_signal);
314 signal(SIGQUIT, server_job_signal);
315 signal(SIGTERM, server_job_signal);
316 signal(SIGKILL, server_job_signal);
317 /* close all files to free resources */
318 i = sysconf(_SC_OPEN_MAX);
319 #ifdef SERVER_JOB_DEBUG
324 if (i != dmix->server_fd && i != dmix->hw_fd)
328 /* detach from parent */
331 pfds[0].fd = dmix->server_fd;
332 pfds[0].events = POLLIN | POLLERR | POLLHUP;
334 server_printf("DIRECT SERVER STARTED\n");
336 ret = poll(pfds, current + 1, 500);
337 server_printf("DIRECT SERVER: poll ret = %i, revents[0] = 0x%x, errno = %i\n", ret, pfds[0].revents, errno);
344 if (ret == 0 || (pfds[0].revents & (POLLERR | POLLHUP))) { /* timeout or error? */
346 snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT);
347 if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0) {
348 _snd_pcm_direct_shm_discard(dmix);
349 snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT);
352 server_printf("DIRECT SERVER: nattch = %i\n", (int)buf.shm_nattch);
353 if (buf.shm_nattch == 1) /* server is the last user, exit */
355 snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT);
358 if (pfds[0].revents & POLLIN) {
360 sck = accept(dmix->server_fd, 0, 0);
362 server_printf("DIRECT SERVER: new connection %i\n", sck);
363 if (current == max) {
366 unsigned char buf = 'A';
367 pfds[current+1].fd = sck;
368 pfds[current+1].events = POLLIN | POLLERR | POLLHUP;
369 _snd_send_fd(sck, &buf, 1, dmix->hw_fd);
370 server_printf("DIRECT SERVER: fd sent ok\n");
375 for (i = 0; i < current && ret > 0; i++) {
376 struct pollfd *pfd = &pfds[i+1];
378 server_printf("client %i revents = 0x%x\n", pfd->fd, pfd->revents);
379 if (pfd->revents & (POLLERR | POLLHUP)) {
385 if (!(pfd->revents & POLLIN))
388 if (read(pfd->fd, &cmd, 1) == 1)
389 cmd = 0 /*process command */;
391 for (i = 0; i < current; i++) {
392 if (pfds[i+1].fd < 0) {
394 memcpy(&pfds[i+1], &pfds[i+2], sizeof(struct pollfd) * (max - i - 1));
399 server_cleanup(dmix);
400 server_printf("DIRECT SERVER EXIT\n");
401 #ifdef SERVER_JOB_DEBUG
402 close(0); close(1); close(2);
407 int snd_pcm_direct_server_create(snd_pcm_direct_t *dmix)
411 dmix->server_fd = -1;
413 ret = get_tmp_name(dmix->shmptr->socket_name, sizeof(dmix->shmptr->socket_name));
417 ret = make_local_socket(dmix->shmptr->socket_name, 1, dmix->ipc_perm, dmix->ipc_gid);
420 dmix->server_fd = ret;
422 ret = listen(dmix->server_fd, 4);
424 close(dmix->server_fd);
430 close(dmix->server_fd);
432 } else if (ret == 0) {
438 waitpid(ret, NULL, 0);
440 dmix->server_pid = ret;
445 int snd_pcm_direct_server_discard(snd_pcm_direct_t *dmix)
448 //kill(dmix->server_pid, SIGTERM);
449 //waitpid(dmix->server_pid, NULL, 0);
450 dmix->server_pid = (pid_t)-1;
452 if (dmix->server_fd > 0) {
453 close(dmix->server_fd);
454 dmix->server_fd = -1;
464 int snd_pcm_direct_client_connect(snd_pcm_direct_t *dmix)
469 ret = make_local_socket(dmix->shmptr->socket_name, 0, -1, -1);
474 ret = snd_receive_fd(dmix->comm_fd, &buf, 1, &dmix->hw_fd);
475 if (ret < 1 || buf != 'A') {
476 close(dmix->comm_fd);
485 int snd_pcm_direct_client_discard(snd_pcm_direct_t *dmix)
488 close(dmix->comm_fd);
498 int snd_pcm_direct_nonblock(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int nonblock ATTRIBUTE_UNUSED)
500 /* value is cached for us in pcm->mode (SND_PCM_NONBLOCK flag) */
504 int snd_pcm_direct_async(snd_pcm_t *pcm, int sig, pid_t pid)
506 snd_pcm_direct_t *dmix = pcm->private_data;
507 return snd_timer_async(dmix->timer, sig, pid);
510 /* empty the timer read queue */
511 void snd_pcm_direct_clear_timer_queue(snd_pcm_direct_t *dmix)
513 if (dmix->timer_need_poll) {
514 while (poll(&dmix->timer_fd, 1, 0) > 0) {
515 /* we don't need the value */
517 snd_timer_tread_t rbuf[4];
518 snd_timer_read(dmix->timer, rbuf, sizeof(rbuf));
520 snd_timer_read_t rbuf;
521 snd_timer_read(dmix->timer, &rbuf, sizeof(rbuf));
526 snd_timer_tread_t rbuf[4];
528 while ((len = snd_timer_read(dmix->timer, rbuf,
529 sizeof(rbuf))) > 0 &&
530 len != sizeof(rbuf[0]))
533 snd_timer_read_t rbuf;
534 while (snd_timer_read(dmix->timer, &rbuf, sizeof(rbuf)) > 0)
540 int snd_pcm_direct_timer_stop(snd_pcm_direct_t *dmix)
542 snd_timer_stop(dmix->timer);
543 snd_pcm_direct_clear_timer_queue(dmix);
547 int snd_pcm_direct_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
549 snd_pcm_direct_t *dmix = pcm->private_data;
550 unsigned short events;
553 assert(pfds && nfds == 1 && revents);
554 events = pfds[0].revents;
555 if (events & POLLIN) {
556 snd_pcm_uframes_t avail;
557 snd_pcm_avail_update(pcm);
558 if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
561 avail = snd_pcm_mmap_playback_avail(pcm);
563 avail = snd_pcm_mmap_capture_avail(pcm);
565 empty = avail < pcm->avail_min;
567 switch (snd_pcm_state(dmix->spcm)) {
568 case SND_PCM_STATE_XRUN:
569 case SND_PCM_STATE_SUSPENDED:
574 snd_pcm_direct_clear_timer_queue(dmix);
575 events &= ~(POLLOUT|POLLIN);
576 /* additional check */
577 switch (snd_pcm_state(pcm)) {
578 case SND_PCM_STATE_XRUN:
579 case SND_PCM_STATE_SUSPENDED:
592 int snd_pcm_direct_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
594 // snd_pcm_direct_t *dmix = pcm->private_data;
596 memset(info, 0, sizeof(*info));
597 info->stream = pcm->stream;
599 /* FIXME: fill this with something more useful: we know the hardware name */
601 strncpy((char *)info->id, pcm->name, sizeof(info->id));
602 strncpy((char *)info->name, pcm->name, sizeof(info->name));
603 strncpy((char *)info->subname, pcm->name, sizeof(info->subname));
605 info->subdevices_count = 1;
609 static inline snd_mask_t *hw_param_mask(snd_pcm_hw_params_t *params,
610 snd_pcm_hw_param_t var)
612 return ¶ms->masks[var - SND_PCM_HW_PARAM_FIRST_MASK];
615 static inline snd_interval_t *hw_param_interval(snd_pcm_hw_params_t *params,
616 snd_pcm_hw_param_t var)
618 return ¶ms->intervals[var - SND_PCM_HW_PARAM_FIRST_INTERVAL];
621 static int hw_param_interval_refine_one(snd_pcm_hw_params_t *params,
622 snd_pcm_hw_param_t var,
627 if (!(params->rmask & (1<<var))) /* nothing to do? */
629 i = hw_param_interval(params, var);
630 if (snd_interval_empty(i)) {
631 SNDERR("dshare interval %i empty?", (int)var);
634 if (snd_interval_refine(i, src))
635 params->cmask |= 1<<var;
639 static int hw_param_interval_refine_minmax(snd_pcm_hw_params_t *params,
640 snd_pcm_hw_param_t var,
646 memset(&t, 0, sizeof(t));
647 snd_interval_set_minmax(&t, imin, imax);
649 return hw_param_interval_refine_one(params, var, &t);
654 int snd_pcm_direct_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
656 snd_pcm_direct_t *dshare = pcm->private_data;
657 static snd_mask_t access = { .bits = {
658 (1<<SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) |
659 (1<<SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED) |
660 (1<<SNDRV_PCM_ACCESS_RW_INTERLEAVED) |
661 (1<<SNDRV_PCM_ACCESS_RW_NONINTERLEAVED),
667 snd_output_stdio_attach(&log, stderr, 0);
668 snd_output_puts(log, "DMIX REFINE (begin):\n");
669 snd_pcm_hw_params_dump(params, log);
671 if (params->rmask & (1<<SND_PCM_HW_PARAM_ACCESS)) {
672 if (snd_mask_empty(hw_param_mask(params, SND_PCM_HW_PARAM_ACCESS))) {
673 SNDERR("dshare access mask empty?");
676 if (snd_mask_refine(hw_param_mask(params, SND_PCM_HW_PARAM_ACCESS), &access))
677 params->cmask |= 1<<SND_PCM_HW_PARAM_ACCESS;
679 if (params->rmask & (1<<SND_PCM_HW_PARAM_FORMAT)) {
680 if (snd_mask_empty(hw_param_mask(params, SND_PCM_HW_PARAM_FORMAT))) {
681 SNDERR("dshare format mask empty?");
684 if (snd_mask_refine_set(hw_param_mask(params, SND_PCM_HW_PARAM_FORMAT),
685 dshare->shmptr->hw.format))
686 params->cmask |= 1<<SND_PCM_HW_PARAM_FORMAT;
688 //snd_mask_none(hw_param_mask(params, SND_PCM_HW_PARAM_SUBFORMAT));
689 if (params->rmask & (1<<SND_PCM_HW_PARAM_CHANNELS)) {
690 if (snd_interval_empty(hw_param_interval(params, SND_PCM_HW_PARAM_CHANNELS))) {
691 SNDERR("dshare channels mask empty?");
694 err = snd_interval_refine_set(hw_param_interval(params, SND_PCM_HW_PARAM_CHANNELS), dshare->channels);
698 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_RATE,
699 &dshare->shmptr->hw.rate);
702 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_PERIOD_SIZE,
703 &dshare->shmptr->hw.period_size);
706 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_PERIOD_TIME,
707 &dshare->shmptr->hw.period_time);
710 if (dshare->max_periods < 0) {
711 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_BUFFER_SIZE,
712 &dshare->shmptr->hw.buffer_size);
715 err = hw_param_interval_refine_one(params, SND_PCM_HW_PARAM_BUFFER_TIME,
716 &dshare->shmptr->hw.buffer_time);
719 } else if (params->rmask & ((1<<SND_PCM_HW_PARAM_PERIODS)|
720 (1<<SND_PCM_HW_PARAM_BUFFER_BYTES)|
721 (1<<SND_PCM_HW_PARAM_BUFFER_SIZE)|
722 (1<<SND_PCM_HW_PARAM_BUFFER_TIME))) {
724 unsigned int max_periods = dshare->max_periods;
726 max_periods = dshare->slave_buffer_size / dshare->slave_period_size;
729 err = hw_param_interval_refine_minmax(params, SND_PCM_HW_PARAM_PERIODS,
734 err = snd_pcm_hw_refine_soft(pcm, params);
740 params->info = dshare->shmptr->s.info;
742 snd_output_puts(log, "DMIX REFINE (end):\n");
743 snd_pcm_hw_params_dump(params, log);
744 snd_output_close(log);
749 int snd_pcm_direct_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
751 snd_pcm_direct_t *dmix = pcm->private_data;
753 params->info = dmix->shmptr->s.info;
754 params->rate_num = dmix->shmptr->s.rate;
755 params->rate_den = 1;
756 params->fifo_size = 0;
757 params->msbits = dmix->shmptr->s.msbits;
761 int snd_pcm_direct_hw_free(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
763 /* values are cached in the pcm structure */
767 int snd_pcm_direct_sw_params(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t * params ATTRIBUTE_UNUSED)
769 /* values are cached in the pcm structure */
773 int snd_pcm_direct_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
775 return snd_pcm_channel_info_shm(pcm, info, -1);
778 int snd_pcm_direct_mmap(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
783 int snd_pcm_direct_munmap(snd_pcm_t *pcm ATTRIBUTE_UNUSED)
788 int snd_pcm_direct_resume(snd_pcm_t *pcm)
790 snd_pcm_direct_t *dmix = pcm->private_data;
793 snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT);
794 err = snd_pcm_resume(dmix->spcm);
795 if (err == -ENOSYS) {
796 /* FIXME: error handling? */
797 snd_pcm_prepare(dmix->spcm);
798 snd_pcm_start(dmix->spcm);
801 snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT);
805 #define COPY_SLAVE(field) (dmix->shmptr->s.field = spcm->field)
807 /* copy the slave setting */
808 static void save_slave_setting(snd_pcm_direct_t *dmix, snd_pcm_t *spcm)
810 spcm->info &= ~SND_PCM_INFO_PAUSE;
814 COPY_SLAVE(subformat);
815 COPY_SLAVE(channels);
817 COPY_SLAVE(period_size);
818 COPY_SLAVE(period_time);
820 COPY_SLAVE(tick_time);
821 COPY_SLAVE(tstamp_mode);
822 COPY_SLAVE(period_step);
823 COPY_SLAVE(sleep_min);
824 COPY_SLAVE(avail_min);
825 COPY_SLAVE(start_threshold);
826 COPY_SLAVE(stop_threshold);
827 COPY_SLAVE(silence_threshold);
828 COPY_SLAVE(silence_size);
829 COPY_SLAVE(boundary);
832 COPY_SLAVE(rate_num);
833 COPY_SLAVE(rate_den);
834 COPY_SLAVE(hw_flags);
835 COPY_SLAVE(fifo_size);
836 COPY_SLAVE(buffer_size);
837 COPY_SLAVE(buffer_time);
838 COPY_SLAVE(sample_bits);
839 COPY_SLAVE(frame_bits);
845 * this function initializes hardware and starts playback operation with
846 * no stop threshold (it operates all time without xrun checking)
847 * also, the driver silences the unused ring buffer areas for us
849 int snd_pcm_direct_initialize_slave(snd_pcm_direct_t *dmix, snd_pcm_t *spcm, struct slave_params *params)
851 snd_pcm_hw_params_t *hw_params;
852 snd_pcm_sw_params_t *sw_params;
853 int ret, buffer_is_not_initialized;
854 snd_pcm_uframes_t boundary;
858 snd_pcm_hw_params_alloca(&hw_params);
859 snd_pcm_sw_params_alloca(&sw_params);
863 SNDERR("unable to find a valid configuration for slave");
866 ret = snd_pcm_hw_params_any(spcm, hw_params);
868 SNDERR("snd_pcm_hw_params_any failed");
871 ret = snd_pcm_hw_params_set_access(spcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
873 ret = snd_pcm_hw_params_set_access(spcm, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
875 SNDERR("slave plugin does not support mmap interleaved or mmap noninterleaved access");
879 ret = snd_pcm_hw_params_set_format(spcm, hw_params, params->format);
881 static const snd_pcm_format_t dmix_formats[] = {
883 SND_PCM_FORMAT_S32 ^ SND_PCM_FORMAT_S32_LE ^ SND_PCM_FORMAT_S32_BE,
885 SND_PCM_FORMAT_S16 ^ SND_PCM_FORMAT_S16_LE ^ SND_PCM_FORMAT_S16_BE,
886 SND_PCM_FORMAT_S24_3LE,
889 snd_pcm_format_t format;
892 for (i = 0; i < sizeof dmix_formats / sizeof dmix_formats[0]; ++i) {
893 format = dmix_formats[i];
894 ret = snd_pcm_hw_params_set_format(spcm, hw_params, format);
898 if (ret < 0 && dmix->type != SND_PCM_TYPE_DMIX) {
899 /* TODO: try to choose a good format */
900 ret = INTERNAL(snd_pcm_hw_params_set_format_first)(spcm, hw_params, &format);
903 SNDERR("requested or auto-format is not available");
906 params->format = format;
908 ret = INTERNAL(snd_pcm_hw_params_set_channels_near)(spcm, hw_params, (unsigned int *)¶ms->channels);
910 SNDERR("requested count of channels is not available");
913 ret = INTERNAL(snd_pcm_hw_params_set_rate_near)(spcm, hw_params, (unsigned int *)¶ms->rate, 0);
915 SNDERR("requested rate is not available");
919 buffer_is_not_initialized = 0;
920 if (params->buffer_time > 0) {
921 ret = INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(spcm, hw_params, (unsigned int *)¶ms->buffer_time, 0);
923 SNDERR("unable to set buffer time");
926 } else if (params->buffer_size > 0) {
927 ret = INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(spcm, hw_params, (snd_pcm_uframes_t *)¶ms->buffer_size);
929 SNDERR("unable to set buffer size");
933 buffer_is_not_initialized = 1;
936 if (params->period_time > 0) {
937 ret = INTERNAL(snd_pcm_hw_params_set_period_time_near)(spcm, hw_params, (unsigned int *)¶ms->period_time, 0);
939 SNDERR("unable to set period_time");
942 } else if (params->period_size > 0) {
943 ret = INTERNAL(snd_pcm_hw_params_set_period_size_near)(spcm, hw_params, (snd_pcm_uframes_t *)¶ms->period_size, 0);
945 SNDERR("unable to set period_size");
950 if (buffer_is_not_initialized && params->periods > 0) {
951 unsigned int periods = params->periods;
952 ret = INTERNAL(snd_pcm_hw_params_set_periods_near)(spcm, hw_params, ¶ms->periods, 0);
954 SNDERR("unable to set requested periods");
957 if (params->periods == 1) {
958 params->periods = periods;
959 if (params->period_time > 0) {
960 params->period_time /= 2;
962 } else if (params->period_size > 0) {
963 params->period_size /= 2;
966 SNDERR("unable to use stream with periods == 1");
971 ret = snd_pcm_hw_params(spcm, hw_params);
973 SNDERR("unable to install hw params");
977 /* store some hw_params values to shared info */
978 dmix->shmptr->hw.format = snd_mask_value(hw_param_mask(hw_params, SND_PCM_HW_PARAM_FORMAT));
979 dmix->shmptr->hw.rate = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_RATE);
980 dmix->shmptr->hw.buffer_size = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_BUFFER_SIZE);
981 dmix->shmptr->hw.buffer_time = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_BUFFER_TIME);
982 dmix->shmptr->hw.period_size = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_PERIOD_SIZE);
983 dmix->shmptr->hw.period_time = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_PERIOD_TIME);
984 dmix->shmptr->hw.periods = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_PERIODS);
987 ret = snd_pcm_sw_params_current(spcm, sw_params);
989 SNDERR("unable to get current sw_params");
993 ret = snd_pcm_sw_params_get_boundary(sw_params, &boundary);
995 SNDERR("unable to get boundary");
998 ret = snd_pcm_sw_params_set_stop_threshold(spcm, sw_params, boundary);
1000 SNDERR("unable to set stop threshold");
1004 /* set timestamp mode to MMAP
1005 * the slave timestamp is copied appropriately in dsnoop/dmix/dshare
1006 * based on the tstamp_mode of each client
1008 ret = snd_pcm_sw_params_set_tstamp_mode(spcm, sw_params,
1009 SND_PCM_TSTAMP_MMAP);
1011 SNDERR("unable to tstamp mode MMAP");
1015 if (dmix->type != SND_PCM_TYPE_DMIX)
1016 goto __skip_silencing;
1018 ret = snd_pcm_sw_params_set_silence_threshold(spcm, sw_params, 0);
1020 SNDERR("unable to set silence threshold");
1023 ret = snd_pcm_sw_params_set_silence_size(spcm, sw_params, boundary);
1025 SNDERR("unable to set silence threshold (please upgrade to 0.9.0rc8+ driver)");
1031 ret = snd_pcm_sw_params(spcm, sw_params);
1033 SNDERR("unable to install sw params (please upgrade to 0.9.0rc8+ driver)");
1037 if (dmix->type == SND_PCM_TYPE_DSHARE) {
1038 const snd_pcm_channel_area_t *dst_areas;
1039 dst_areas = snd_pcm_mmap_areas(spcm);
1040 snd_pcm_areas_silence(dst_areas, 0, spcm->channels, spcm->buffer_size, spcm->format);
1043 ret = snd_pcm_start(spcm);
1045 SNDERR("unable to start PCM stream");
1049 if (snd_pcm_poll_descriptors_count(spcm) != 1) {
1050 SNDERR("unable to use hardware pcm with fd more than one!!!");
1053 snd_pcm_poll_descriptors(spcm, &fd, 1);
1054 dmix->hw_fd = fd.fd;
1056 save_slave_setting(dmix, spcm);
1058 /* Currently, we assume that each dmix client has the same
1059 * hw_params setting.
1060 * If the arbitrary hw_parmas is supported in future,
1061 * boundary has to be taken from the slave config but
1062 * recalculated for the native boundary size (for 32bit
1063 * emulation on 64bit arch).
1065 dmix->slave_buffer_size = spcm->buffer_size;
1066 dmix->slave_period_size = spcm->period_size;
1067 dmix->slave_boundary = spcm->boundary;
1069 spcm->donot_close = 1;
1073 ioctl(spcm->poll_fd, SNDRV_PCM_IOCTL_PVERSION, &ver);
1074 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 8))
1075 dmix->shmptr->use_server = 1;
1082 * the trick is used here; we cannot use effectively the hardware handle because
1083 * we cannot drive multiple accesses to appl_ptr; so we use slave timer of given
1084 * PCM hardware handle; it's not this easy and cheap?
1086 int snd_pcm_direct_initialize_poll_fd(snd_pcm_direct_t *dmix)
1089 snd_pcm_info_t *info;
1091 int capture = dmix->type == SND_PCM_TYPE_DSNOOP ? 1 : 0;
1094 dmix->timer_need_poll = 0;
1095 snd_pcm_info_alloca(&info);
1096 ret = snd_pcm_info(dmix->spcm, info);
1098 SNDERR("unable to info for slave pcm");
1101 sprintf(name, "hw:CLASS=%i,SCLASS=0,CARD=%i,DEV=%i,SUBDEV=%i",
1102 (int)SND_TIMER_CLASS_PCM,
1103 snd_pcm_info_get_card(info),
1104 snd_pcm_info_get_device(info),
1105 snd_pcm_info_get_subdevice(info) * 2 + capture);
1106 ret = snd_timer_open(&dmix->timer, name, SND_TIMER_OPEN_NONBLOCK | SND_TIMER_OPEN_TREAD);
1109 ret = snd_timer_open(&dmix->timer, name, SND_TIMER_OPEN_NONBLOCK);
1111 SNDERR("unable to open timer '%s'", name);
1116 if (snd_timer_poll_descriptors_count(dmix->timer) != 1) {
1117 SNDERR("unable to use timer '%s' with more than one fd!", name);
1120 snd_timer_poll_descriptors(dmix->timer, &dmix->timer_fd, 1);
1121 dmix->poll_fd = dmix->timer_fd.fd;
1123 dmix->timer_event_suspend = 1<<SND_TIMER_EVENT_MSUSPEND;
1124 dmix->timer_event_resume = 1<<SND_TIMER_EVENT_MRESUME;
1127 * Some hacks for older kernel drivers
1131 ioctl(dmix->poll_fd, SNDRV_TIMER_IOCTL_PVERSION, &ver);
1132 /* In older versions, check via poll before read() is needed
1133 * because of the confliction between TIMER_START and
1136 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 4))
1137 dmix->timer_need_poll = 1;
1139 * In older versions, timer uses pause events instead
1140 * suspend/resume events.
1142 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 5)) {
1143 dmix->timer_event_suspend = 1<<SND_TIMER_EVENT_MPAUSE;
1144 dmix->timer_event_resume = 1<<SND_TIMER_EVENT_MCONTINUE;
1150 static snd_pcm_uframes_t recalc_boundary_size(unsigned long long bsize, snd_pcm_uframes_t buffer_size)
1152 if (bsize > LONG_MAX) {
1153 bsize = buffer_size;
1154 while (bsize * 2 <= LONG_MAX - buffer_size)
1157 return (snd_pcm_uframes_t)bsize;
1160 #define COPY_SLAVE(field) (spcm->field = dmix->shmptr->s.field)
1162 /* copy the slave setting */
1163 static void copy_slave_setting(snd_pcm_direct_t *dmix, snd_pcm_t *spcm)
1167 COPY_SLAVE(subformat);
1168 COPY_SLAVE(channels);
1170 COPY_SLAVE(period_size);
1171 COPY_SLAVE(period_time);
1172 COPY_SLAVE(periods);
1173 COPY_SLAVE(tick_time);
1174 COPY_SLAVE(tstamp_mode);
1175 COPY_SLAVE(period_step);
1176 COPY_SLAVE(sleep_min);
1177 COPY_SLAVE(avail_min);
1178 COPY_SLAVE(start_threshold);
1179 COPY_SLAVE(stop_threshold);
1180 COPY_SLAVE(silence_threshold);
1181 COPY_SLAVE(silence_size);
1182 COPY_SLAVE(boundary);
1185 COPY_SLAVE(rate_num);
1186 COPY_SLAVE(rate_den);
1187 COPY_SLAVE(hw_flags);
1188 COPY_SLAVE(fifo_size);
1189 COPY_SLAVE(buffer_size);
1190 COPY_SLAVE(buffer_time);
1191 COPY_SLAVE(sample_bits);
1192 COPY_SLAVE(frame_bits);
1194 spcm->info &= ~SND_PCM_INFO_PAUSE;
1195 spcm->boundary = recalc_boundary_size(dmix->shmptr->s.boundary, spcm->buffer_size);
1202 * open a slave PCM as secondary client (dup'ed fd)
1204 int snd_pcm_direct_open_secondary_client(snd_pcm_t **spcmp, snd_pcm_direct_t *dmix, const char *client_name)
1209 ret = snd_pcm_hw_open_fd(spcmp, client_name, dmix->hw_fd, 0, 0);
1211 SNDERR("unable to open hardware");
1216 spcm->donot_close = 1;
1219 copy_slave_setting(dmix, spcm);
1221 /* Use the slave setting as SPCM, so far */
1222 dmix->slave_buffer_size = spcm->buffer_size;
1223 dmix->slave_period_size = dmix->shmptr->s.period_size;
1224 dmix->slave_boundary = spcm->boundary;
1226 ret = snd_pcm_mmap(spcm);
1228 SNDERR("unable to mmap channels");
1235 * open a slave PCM as secondary client (dup'ed fd)
1237 int snd_pcm_direct_initialize_secondary_slave(snd_pcm_direct_t *dmix,
1239 struct slave_params *params ATTRIBUTE_UNUSED)
1243 spcm->donot_close = 1;
1246 copy_slave_setting(dmix, spcm);
1248 /* Use the slave setting as SPCM, so far */
1249 dmix->slave_buffer_size = spcm->buffer_size;
1250 dmix->slave_period_size = dmix->shmptr->s.period_size;
1251 dmix->slave_boundary = spcm->boundary;
1253 ret = snd_pcm_mmap(spcm);
1255 SNDERR("unable to mmap channels");
1261 int snd_pcm_direct_set_timer_params(snd_pcm_direct_t *dmix)
1263 snd_timer_params_t *params;
1264 unsigned int filter;
1267 snd_timer_params_alloca(¶ms);
1268 snd_timer_params_set_auto_start(params, 1);
1269 if (dmix->type != SND_PCM_TYPE_DSNOOP)
1270 snd_timer_params_set_early_event(params, 1);
1271 snd_timer_params_set_ticks(params, 1);
1273 filter = (1<<SND_TIMER_EVENT_TICK) |
1274 dmix->timer_event_suspend |
1275 dmix->timer_event_resume;
1276 snd_timer_params_set_filter(params, filter);
1278 ret = snd_timer_params(dmix->timer, params);
1280 SNDERR("unable to set timer parameters");
1287 * ring buffer operation
1289 int snd_pcm_direct_check_interleave(snd_pcm_direct_t *dmix, snd_pcm_t *pcm)
1291 unsigned int chn, channels;
1292 int bits, interleaved = 1;
1293 const snd_pcm_channel_area_t *dst_areas;
1294 const snd_pcm_channel_area_t *src_areas;
1296 bits = snd_pcm_format_physical_width(dmix->type);
1297 if ((bits % 8) != 0)
1299 channels = dmix->channels;
1300 dst_areas = snd_pcm_mmap_areas(dmix->spcm);
1301 src_areas = snd_pcm_mmap_areas(pcm);
1302 for (chn = 1; chn < channels; chn++) {
1303 if (dst_areas[chn-1].addr != dst_areas[chn].addr) {
1307 if (src_areas[chn-1].addr != src_areas[chn].addr) {
1312 for (chn = 0; chn < channels; chn++) {
1313 if (dmix->bindings && dmix->bindings[chn] != chn) {
1317 if (dst_areas[chn].first != chn * bits ||
1318 dst_areas[chn].step != channels * bits) {
1322 if (src_areas[chn].first != chn * bits ||
1323 src_areas[chn].step != channels * bits) {
1328 return dmix->interleaved = interleaved;
1332 * parse the channel map
1333 * id == client channel
1334 * value == slave's channel
1336 int snd_pcm_direct_parse_bindings(snd_pcm_direct_t *dmix,
1337 struct slave_params *params,
1340 snd_config_iterator_t i, next;
1341 unsigned int chn, chn1, count = 0;
1342 unsigned int *bindings;
1345 dmix->channels = UINT_MAX;
1348 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1349 SNDERR("invalid type for bindings");
1352 snd_config_for_each(i, next, cfg) {
1353 snd_config_t *n = snd_config_iterator_entry(i);
1356 if (snd_config_get_id(n, &id) < 0)
1358 err = safe_strtol(id, &cchannel);
1359 if (err < 0 || cchannel < 0) {
1360 SNDERR("invalid client channel in binding: %s\n", id);
1363 if ((unsigned)cchannel >= count)
1364 count = cchannel + 1;
1369 SNDERR("client channel out of range");
1372 bindings = malloc(count * sizeof(unsigned int));
1373 if (bindings == NULL)
1375 for (chn = 0; chn < count; chn++)
1376 bindings[chn] = UINT_MAX; /* don't route */
1377 snd_config_for_each(i, next, cfg) {
1378 snd_config_t *n = snd_config_iterator_entry(i);
1380 long cchannel, schannel;
1381 if (snd_config_get_id(n, &id) < 0)
1383 safe_strtol(id, &cchannel);
1384 if (snd_config_get_integer(n, &schannel) < 0) {
1385 SNDERR("unable to get slave channel (should be integer type) in binding: %s\n", id);
1389 if (schannel < 0 || schannel >= params->channels) {
1390 SNDERR("invalid slave channel number %ld in binding to %ld",
1391 schannel, cchannel);
1395 bindings[cchannel] = schannel;
1397 if (dmix->type == SND_PCM_TYPE_DSNOOP ||
1399 goto __skip_same_dst;
1400 for (chn = 0; chn < count; chn++) {
1401 for (chn1 = 0; chn1 < count; chn1++) {
1404 if (bindings[chn] == dmix->bindings[chn1]) {
1405 SNDERR("unable to route channels %d,%d to same destination %d", chn, chn1, bindings[chn]);
1412 dmix->bindings = bindings;
1413 dmix->channels = count;
1418 * parse slave config and calculate the ipc_key offset
1421 static int _snd_pcm_direct_get_slave_ipc_offset(snd_config_t *root,
1422 snd_config_t *sconf,
1426 snd_config_iterator_t i, next;
1427 snd_config_t *pcm_conf;
1429 long card = 0, device = 0, subdevice = 0;
1432 if (snd_config_get_string(sconf, &str) >= 0) {
1433 if (hop > SND_CONF_MAX_HOPS) {
1434 SNDERR("Too many definition levels (looped?)");
1437 err = snd_config_search_definition(root, "pcm", str, &pcm_conf);
1439 SNDERR("Unknown slave PCM %s", str);
1442 err = _snd_pcm_direct_get_slave_ipc_offset(root, pcm_conf,
1445 snd_config_delete(pcm_conf);
1449 #if 0 /* for debug purposes */
1452 snd_output_stdio_attach(&out, stderr, 0);
1453 snd_config_save(sconf, out);
1454 snd_output_close(out);
1458 if (snd_config_search(sconf, "slave", &pcm_conf) >= 0 &&
1459 snd_config_search(pcm_conf, "pcm", &pcm_conf) >= 0)
1460 return _snd_pcm_direct_get_slave_ipc_offset(root, pcm_conf,
1464 snd_config_for_each(i, next, sconf) {
1465 snd_config_t *n = snd_config_iterator_entry(i);
1466 const char *id, *str;
1467 if (snd_config_get_id(n, &id) < 0)
1469 if (strcmp(id, "type") == 0) {
1470 err = snd_config_get_string(n, &str);
1472 SNDERR("Invalid value for PCM type definition\n");
1475 if (strcmp(str, "hw")) {
1476 SNDERR("Invalid type '%s' for slave PCM\n", str);
1481 if (strcmp(id, "card") == 0) {
1482 err = snd_config_get_integer(n, &card);
1484 err = snd_config_get_string(n, &str);
1486 SNDERR("Invalid type for %s", id);
1489 card = snd_card_get_index(str);
1491 SNDERR("Invalid value for %s", id);
1497 if (strcmp(id, "device") == 0) {
1498 err = snd_config_get_integer(n, &device);
1500 SNDERR("Invalid type for %s", id);
1505 if (strcmp(id, "subdevice") == 0) {
1506 err = snd_config_get_integer(n, &subdevice);
1508 SNDERR("Invalid type for %s", id);
1520 return (direction << 1) + (device << 2) + (subdevice << 8) + (card << 12);
1523 static int snd_pcm_direct_get_slave_ipc_offset(snd_config_t *root,
1524 snd_config_t *sconf,
1527 return _snd_pcm_direct_get_slave_ipc_offset(root, sconf, direction, 0);
1530 int snd_pcm_direct_parse_open_conf(snd_config_t *root, snd_config_t *conf,
1531 int stream, struct snd_pcm_direct_open_conf *rec)
1533 snd_config_iterator_t i, next;
1534 int ipc_key_add_uid = 0;
1539 rec->bindings = NULL;
1541 rec->ipc_perm = 0600;
1544 rec->max_periods = 0;
1547 if (snd_config_search(root, "defaults.pcm.dmix_max_periods", &n) >= 0) {
1549 err = snd_config_get_integer(n, &val);
1551 rec->max_periods = val;
1554 snd_config_for_each(i, next, conf) {
1556 n = snd_config_iterator_entry(i);
1557 if (snd_config_get_id(n, &id) < 0)
1559 if (snd_pcm_conf_generic_id(id))
1561 if (strcmp(id, "ipc_key") == 0) {
1563 err = snd_config_get_integer(n, &key);
1565 SNDERR("The field ipc_key must be an integer type");
1572 if (strcmp(id, "ipc_perm") == 0) {
1574 err = snd_config_get_integer(n, &perm);
1576 SNDERR("Invalid type for %s", id);
1579 if ((perm & ~0777) != 0) {
1580 SNDERR("The field ipc_perm must be a valid file permission");
1583 rec->ipc_perm = perm;
1586 if (strcmp(id, "ipc_gid") == 0) {
1589 err = snd_config_get_ascii(n, &group);
1591 SNDERR("The field ipc_gid must be a valid group");
1599 if (isdigit(*group) == 0) {
1600 struct group *grp = getgrnam(group);
1602 SNDERR("The field ipc_gid must be a valid group (create group %s)", group);
1606 rec->ipc_gid = grp->gr_gid;
1608 rec->ipc_gid = strtol(group, &endp, 10);
1613 if (strcmp(id, "ipc_key_add_uid") == 0) {
1614 if ((err = snd_config_get_bool(n)) < 0) {
1615 SNDERR("The field ipc_key_add_uid must be a boolean type");
1618 ipc_key_add_uid = err;
1621 if (strcmp(id, "slave") == 0) {
1625 if (strcmp(id, "bindings") == 0) {
1629 if (strcmp(id, "slowptr") == 0) {
1630 err = snd_config_get_bool(n);
1636 if (strcmp(id, "max_periods") == 0) {
1638 err = snd_config_get_integer(n, &val);
1641 rec->max_periods = val;
1644 SNDERR("Unknown field %s", id);
1648 SNDERR("slave is not defined");
1651 if (!rec->ipc_key) {
1652 SNDERR("Unique IPC key is not defined");
1655 if (ipc_key_add_uid)
1656 rec->ipc_key += getuid();
1657 err = snd_pcm_direct_get_slave_ipc_offset(root, conf, stream);
1660 rec->ipc_key += err;