2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file server/speaker-command.c
21 * @brief Support for @ref BACKEND_COMMAND */
30 #include "configuration.h"
33 #include "speaker-protocol.h"
36 /** @brief Pipe to subprocess
38 * This is the file descriptor to write to for @ref BACKEND_COMMAND.
40 static int cmdfd = -1;
42 /** @brief poll array slot for @ref cmdfd
44 * Set by command_beforepoll().
46 static int cmdfd_slot;
48 /** @brief Start the subprocess for @ref BACKEND_COMMAND */
49 static void fork_cmd(void) {
52 if(cmdfd != -1) close(cmdfd);
57 signal(SIGPIPE, SIG_DFL);
61 execl("/bin/sh", "sh", "-c", config->speaker_command, (char *)0);
62 fatal(errno, "error execing /bin/sh");
66 D(("forked cmd %d, fd = %d", cmdpid, cmdfd));
69 /** @brief Command backend initialization */
70 static void command_init(void) {
71 info("selected command backend");
75 /** @brief Play to a subprocess */
76 static size_t command_play(size_t frames) {
77 size_t bytes = frames * bpf;
80 written_bytes = write(cmdfd, playing->buffer + playing->start, bytes);
81 D(("actually play %zu bytes, wrote %d",
82 bytes, written_bytes));
83 if(written_bytes < 0) {
86 error(0, "hmm, command died; trying another");
92 fatal(errno, "error writing to subprocess");
95 return written_bytes / bpf;
98 /** @brief Update poll array for writing to subprocess */
99 static void command_beforepoll(int attribute((unused)) *timeoutp) {
100 /* We send sample data to the subprocess as fast as it can accept it.
101 * This isn't ideal as pause latency can be very high as a result. */
103 cmdfd_slot = addfd(cmdfd, POLLOUT);
106 /** @brief Process poll() results for subprocess play */
107 static int command_ready(void) {
108 if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR))
114 const struct speaker_backend command_backend = {