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 const 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(tstamp_mode);
821 COPY_SLAVE(period_step);
822 COPY_SLAVE(avail_min);
823 COPY_SLAVE(start_threshold);
824 COPY_SLAVE(stop_threshold);
825 COPY_SLAVE(silence_threshold);
826 COPY_SLAVE(silence_size);
827 COPY_SLAVE(boundary);
830 COPY_SLAVE(rate_num);
831 COPY_SLAVE(rate_den);
832 COPY_SLAVE(hw_flags);
833 COPY_SLAVE(fifo_size);
834 COPY_SLAVE(buffer_size);
835 COPY_SLAVE(buffer_time);
836 COPY_SLAVE(sample_bits);
837 COPY_SLAVE(frame_bits);
843 * this function initializes hardware and starts playback operation with
844 * no stop threshold (it operates all time without xrun checking)
845 * also, the driver silences the unused ring buffer areas for us
847 int snd_pcm_direct_initialize_slave(snd_pcm_direct_t *dmix, snd_pcm_t *spcm, struct slave_params *params)
849 snd_pcm_hw_params_t *hw_params;
850 snd_pcm_sw_params_t *sw_params;
851 int ret, buffer_is_not_initialized;
852 snd_pcm_uframes_t boundary;
856 snd_pcm_hw_params_alloca(&hw_params);
857 snd_pcm_sw_params_alloca(&sw_params);
861 SNDERR("unable to find a valid configuration for slave");
864 ret = snd_pcm_hw_params_any(spcm, hw_params);
866 SNDERR("snd_pcm_hw_params_any failed");
869 ret = snd_pcm_hw_params_set_access(spcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
871 ret = snd_pcm_hw_params_set_access(spcm, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
873 SNDERR("slave plugin does not support mmap interleaved or mmap noninterleaved access");
877 if (params->format == SND_PCM_FORMAT_UNKNOWN)
880 ret = snd_pcm_hw_params_set_format(spcm, hw_params,
883 static const snd_pcm_format_t dmix_formats[] = {
885 SND_PCM_FORMAT_S32 ^ SND_PCM_FORMAT_S32_LE ^ SND_PCM_FORMAT_S32_BE,
887 SND_PCM_FORMAT_S16 ^ SND_PCM_FORMAT_S16_LE ^ SND_PCM_FORMAT_S16_BE,
888 SND_PCM_FORMAT_S24_3LE,
891 snd_pcm_format_t format;
894 for (i = 0; i < sizeof dmix_formats / sizeof dmix_formats[0]; ++i) {
895 format = dmix_formats[i];
896 ret = snd_pcm_hw_params_set_format(spcm, hw_params, format);
900 if (ret < 0 && dmix->type != SND_PCM_TYPE_DMIX) {
901 /* TODO: try to choose a good format */
902 ret = INTERNAL(snd_pcm_hw_params_set_format_first)(spcm, hw_params, &format);
905 SNDERR("requested or auto-format is not available");
908 params->format = format;
910 ret = INTERNAL(snd_pcm_hw_params_set_channels_near)(spcm, hw_params, (unsigned int *)¶ms->channels);
912 SNDERR("requested count of channels is not available");
915 ret = INTERNAL(snd_pcm_hw_params_set_rate_near)(spcm, hw_params, (unsigned int *)¶ms->rate, 0);
917 SNDERR("requested rate is not available");
921 buffer_is_not_initialized = 0;
922 if (params->buffer_time > 0) {
923 ret = INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(spcm, hw_params, (unsigned int *)¶ms->buffer_time, 0);
925 SNDERR("unable to set buffer time");
928 } else if (params->buffer_size > 0) {
929 ret = INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(spcm, hw_params, (snd_pcm_uframes_t *)¶ms->buffer_size);
931 SNDERR("unable to set buffer size");
935 buffer_is_not_initialized = 1;
938 if (params->period_time > 0) {
939 ret = INTERNAL(snd_pcm_hw_params_set_period_time_near)(spcm, hw_params, (unsigned int *)¶ms->period_time, 0);
941 SNDERR("unable to set period_time");
944 } else if (params->period_size > 0) {
945 ret = INTERNAL(snd_pcm_hw_params_set_period_size_near)(spcm, hw_params, (snd_pcm_uframes_t *)¶ms->period_size, 0);
947 SNDERR("unable to set period_size");
952 if (buffer_is_not_initialized && params->periods > 0) {
953 unsigned int periods = params->periods;
954 ret = INTERNAL(snd_pcm_hw_params_set_periods_near)(spcm, hw_params, ¶ms->periods, 0);
956 SNDERR("unable to set requested periods");
959 if (params->periods == 1) {
960 params->periods = periods;
961 if (params->period_time > 0) {
962 params->period_time /= 2;
964 } else if (params->period_size > 0) {
965 params->period_size /= 2;
968 SNDERR("unable to use stream with periods == 1");
973 ret = snd_pcm_hw_params(spcm, hw_params);
975 SNDERR("unable to install hw params");
979 /* store some hw_params values to shared info */
980 dmix->shmptr->hw.format = snd_mask_value(hw_param_mask(hw_params, SND_PCM_HW_PARAM_FORMAT));
981 dmix->shmptr->hw.rate = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_RATE);
982 dmix->shmptr->hw.buffer_size = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_BUFFER_SIZE);
983 dmix->shmptr->hw.buffer_time = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_BUFFER_TIME);
984 dmix->shmptr->hw.period_size = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_PERIOD_SIZE);
985 dmix->shmptr->hw.period_time = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_PERIOD_TIME);
986 dmix->shmptr->hw.periods = *hw_param_interval(hw_params, SND_PCM_HW_PARAM_PERIODS);
989 ret = snd_pcm_sw_params_current(spcm, sw_params);
991 SNDERR("unable to get current sw_params");
995 ret = snd_pcm_sw_params_get_boundary(sw_params, &boundary);
997 SNDERR("unable to get boundary");
1000 ret = snd_pcm_sw_params_set_stop_threshold(spcm, sw_params, boundary);
1002 SNDERR("unable to set stop threshold");
1006 /* set timestamp mode to MMAP
1007 * the slave timestamp is copied appropriately in dsnoop/dmix/dshare
1008 * based on the tstamp_mode of each client
1010 ret = snd_pcm_sw_params_set_tstamp_mode(spcm, sw_params,
1011 SND_PCM_TSTAMP_ENABLE);
1013 SNDERR("unable to tstamp mode MMAP");
1017 if (dmix->type != SND_PCM_TYPE_DMIX)
1018 goto __skip_silencing;
1020 ret = snd_pcm_sw_params_set_silence_threshold(spcm, sw_params, 0);
1022 SNDERR("unable to set silence threshold");
1025 ret = snd_pcm_sw_params_set_silence_size(spcm, sw_params, boundary);
1027 SNDERR("unable to set silence threshold (please upgrade to 0.9.0rc8+ driver)");
1033 ret = snd_pcm_sw_params(spcm, sw_params);
1035 SNDERR("unable to install sw params (please upgrade to 0.9.0rc8+ driver)");
1039 if (dmix->type == SND_PCM_TYPE_DSHARE) {
1040 const snd_pcm_channel_area_t *dst_areas;
1041 dst_areas = snd_pcm_mmap_areas(spcm);
1042 snd_pcm_areas_silence(dst_areas, 0, spcm->channels, spcm->buffer_size, spcm->format);
1045 ret = snd_pcm_start(spcm);
1047 SNDERR("unable to start PCM stream");
1051 if (snd_pcm_poll_descriptors_count(spcm) != 1) {
1052 SNDERR("unable to use hardware pcm with fd more than one!!!");
1055 snd_pcm_poll_descriptors(spcm, &fd, 1);
1056 dmix->hw_fd = fd.fd;
1058 save_slave_setting(dmix, spcm);
1060 /* Currently, we assume that each dmix client has the same
1061 * hw_params setting.
1062 * If the arbitrary hw_parmas is supported in future,
1063 * boundary has to be taken from the slave config but
1064 * recalculated for the native boundary size (for 32bit
1065 * emulation on 64bit arch).
1067 dmix->slave_buffer_size = spcm->buffer_size;
1068 dmix->slave_period_size = spcm->period_size;
1069 dmix->slave_boundary = spcm->boundary;
1071 spcm->donot_close = 1;
1075 ioctl(spcm->poll_fd, SNDRV_PCM_IOCTL_PVERSION, &ver);
1076 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 8))
1077 dmix->shmptr->use_server = 1;
1084 * the trick is used here; we cannot use effectively the hardware handle because
1085 * we cannot drive multiple accesses to appl_ptr; so we use slave timer of given
1086 * PCM hardware handle; it's not this easy and cheap?
1088 int snd_pcm_direct_initialize_poll_fd(snd_pcm_direct_t *dmix)
1091 snd_pcm_info_t *info;
1093 int capture = dmix->type == SND_PCM_TYPE_DSNOOP ? 1 : 0;
1096 dmix->timer_need_poll = 0;
1097 snd_pcm_info_alloca(&info);
1098 ret = snd_pcm_info(dmix->spcm, info);
1100 SNDERR("unable to info for slave pcm");
1103 sprintf(name, "hw:CLASS=%i,SCLASS=0,CARD=%i,DEV=%i,SUBDEV=%i",
1104 (int)SND_TIMER_CLASS_PCM,
1105 snd_pcm_info_get_card(info),
1106 snd_pcm_info_get_device(info),
1107 snd_pcm_info_get_subdevice(info) * 2 + capture);
1108 ret = snd_timer_open(&dmix->timer, name, SND_TIMER_OPEN_NONBLOCK | SND_TIMER_OPEN_TREAD);
1111 ret = snd_timer_open(&dmix->timer, name, SND_TIMER_OPEN_NONBLOCK);
1113 SNDERR("unable to open timer '%s'", name);
1118 if (snd_timer_poll_descriptors_count(dmix->timer) != 1) {
1119 SNDERR("unable to use timer '%s' with more than one fd!", name);
1122 snd_timer_poll_descriptors(dmix->timer, &dmix->timer_fd, 1);
1123 dmix->poll_fd = dmix->timer_fd.fd;
1125 dmix->timer_event_suspend = 1<<SND_TIMER_EVENT_MSUSPEND;
1126 dmix->timer_event_resume = 1<<SND_TIMER_EVENT_MRESUME;
1129 * Some hacks for older kernel drivers
1133 ioctl(dmix->poll_fd, SNDRV_TIMER_IOCTL_PVERSION, &ver);
1134 /* In older versions, check via poll before read() is needed
1135 * because of the confliction between TIMER_START and
1138 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 4))
1139 dmix->timer_need_poll = 1;
1141 * In older versions, timer uses pause events instead
1142 * suspend/resume events.
1144 if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 5)) {
1145 dmix->timer_event_suspend = 1<<SND_TIMER_EVENT_MPAUSE;
1146 dmix->timer_event_resume = 1<<SND_TIMER_EVENT_MCONTINUE;
1152 static snd_pcm_uframes_t recalc_boundary_size(unsigned long long bsize, snd_pcm_uframes_t buffer_size)
1154 if (bsize > LONG_MAX) {
1155 bsize = buffer_size;
1156 while (bsize * 2 <= LONG_MAX - buffer_size)
1159 return (snd_pcm_uframes_t)bsize;
1162 #define COPY_SLAVE(field) (spcm->field = dmix->shmptr->s.field)
1164 /* copy the slave setting */
1165 static void copy_slave_setting(snd_pcm_direct_t *dmix, snd_pcm_t *spcm)
1169 COPY_SLAVE(subformat);
1170 COPY_SLAVE(channels);
1172 COPY_SLAVE(period_size);
1173 COPY_SLAVE(period_time);
1174 COPY_SLAVE(periods);
1175 COPY_SLAVE(tstamp_mode);
1176 COPY_SLAVE(period_step);
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(pcm->format);
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 (snd_config_get_string(pcm_conf, &str) >= 0 &&
1461 snd_config_search_definition(root, "pcm_slave", str, &pcm_conf) >= 0 &&
1462 snd_config_search(pcm_conf, "pcm", &pcm_conf) >= 0)))
1463 return _snd_pcm_direct_get_slave_ipc_offset(root, pcm_conf,
1467 snd_config_for_each(i, next, sconf) {
1468 snd_config_t *n = snd_config_iterator_entry(i);
1469 const char *id, *str;
1470 if (snd_config_get_id(n, &id) < 0)
1472 if (strcmp(id, "type") == 0) {
1473 err = snd_config_get_string(n, &str);
1475 SNDERR("Invalid value for PCM type definition\n");
1478 if (strcmp(str, "hw")) {
1479 SNDERR("Invalid type '%s' for slave PCM\n", str);
1484 if (strcmp(id, "card") == 0) {
1485 err = snd_config_get_integer(n, &card);
1487 err = snd_config_get_string(n, &str);
1489 SNDERR("Invalid type for %s", id);
1492 card = snd_card_get_index(str);
1494 SNDERR("Invalid value for %s", id);
1500 if (strcmp(id, "device") == 0) {
1501 err = snd_config_get_integer(n, &device);
1503 SNDERR("Invalid type for %s", id);
1508 if (strcmp(id, "subdevice") == 0) {
1509 err = snd_config_get_integer(n, &subdevice);
1511 SNDERR("Invalid type for %s", id);
1523 return (direction << 1) + (device << 2) + (subdevice << 8) + (card << 12);
1526 static int snd_pcm_direct_get_slave_ipc_offset(snd_config_t *root,
1527 snd_config_t *sconf,
1530 return _snd_pcm_direct_get_slave_ipc_offset(root, sconf, direction, 0);
1533 int snd_pcm_direct_parse_open_conf(snd_config_t *root, snd_config_t *conf,
1534 int stream, struct snd_pcm_direct_open_conf *rec)
1536 snd_config_iterator_t i, next;
1537 int ipc_key_add_uid = 0;
1542 rec->bindings = NULL;
1544 rec->ipc_perm = 0600;
1547 rec->max_periods = 0;
1550 if (snd_config_search(root, "defaults.pcm.dmix_max_periods", &n) >= 0) {
1552 err = snd_config_get_integer(n, &val);
1554 rec->max_periods = val;
1557 snd_config_for_each(i, next, conf) {
1559 n = snd_config_iterator_entry(i);
1560 if (snd_config_get_id(n, &id) < 0)
1562 if (snd_pcm_conf_generic_id(id))
1564 if (strcmp(id, "ipc_key") == 0) {
1566 err = snd_config_get_integer(n, &key);
1568 SNDERR("The field ipc_key must be an integer type");
1575 if (strcmp(id, "ipc_perm") == 0) {
1577 err = snd_config_get_integer(n, &perm);
1579 SNDERR("Invalid type for %s", id);
1582 if ((perm & ~0777) != 0) {
1583 SNDERR("The field ipc_perm must be a valid file permission");
1586 rec->ipc_perm = perm;
1589 if (strcmp(id, "ipc_gid") == 0) {
1592 err = snd_config_get_ascii(n, &group);
1594 SNDERR("The field ipc_gid must be a valid group");
1602 if (isdigit(*group) == 0) {
1603 struct group *grp = getgrnam(group);
1605 SNDERR("The field ipc_gid must be a valid group (create group %s)", group);
1609 rec->ipc_gid = grp->gr_gid;
1611 rec->ipc_gid = strtol(group, &endp, 10);
1616 if (strcmp(id, "ipc_key_add_uid") == 0) {
1617 if ((err = snd_config_get_bool(n)) < 0) {
1618 SNDERR("The field ipc_key_add_uid must be a boolean type");
1621 ipc_key_add_uid = err;
1624 if (strcmp(id, "slave") == 0) {
1628 if (strcmp(id, "bindings") == 0) {
1632 if (strcmp(id, "slowptr") == 0) {
1633 err = snd_config_get_bool(n);
1639 if (strcmp(id, "max_periods") == 0) {
1641 err = snd_config_get_integer(n, &val);
1644 rec->max_periods = val;
1647 SNDERR("Unknown field %s", id);
1651 SNDERR("slave is not defined");
1654 if (!rec->ipc_key) {
1655 SNDERR("Unique IPC key is not defined");
1658 if (ipc_key_add_uid)
1659 rec->ipc_key += getuid();
1660 err = snd_pcm_direct_get_slave_ipc_offset(root, conf, stream);
1663 rec->ipc_key += err;