2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
4 * Portions (C) 2007 Mark Wooding
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /** @file server/speaker-command.c
20 * @brief Support for @ref BACKEND_COMMAND */
28 #include "configuration.h"
31 #include "speaker-protocol.h"
34 /** @brief Pipe to subprocess
36 * This is the file descriptor to write to for @ref BACKEND_COMMAND.
38 static int cmdfd
= -1;
40 /** @brief poll array slot for @ref cmdfd
42 * Set by command_beforepoll().
44 static int cmdfd_slot
;
46 /** @brief Start the subprocess for @ref BACKEND_COMMAND */
47 static void fork_cmd(void) {
50 if(cmdfd
!= -1) close(cmdfd
);
55 signal(SIGPIPE
, SIG_DFL
);
59 execl("/bin/sh", "sh", "-c", config
->speaker_command
, (char *)0);
60 fatal(errno
, "error execing /bin/sh");
64 D(("forked cmd %d, fd = %d", cmdpid
, cmdfd
));
67 /** @brief Command backend initialization */
68 static void command_init(void) {
69 info("selected command backend");
73 /** @brief Play to a subprocess */
74 static size_t command_play(size_t frames
) {
75 size_t bytes
= frames
* bpf
;
78 written_bytes
= write(cmdfd
, playing
->buffer
+ playing
->start
, bytes
);
79 D(("actually play %zu bytes, wrote %d",
80 bytes
, written_bytes
));
81 if(written_bytes
< 0) {
84 error(0, "hmm, command died; trying another");
90 fatal(errno
, "error writing to subprocess");
93 return written_bytes
/ bpf
;
96 /** @brief Update poll array for writing to subprocess */
97 static void command_beforepoll(int attribute((unused
)) *timeoutp
) {
98 /* We send sample data to the subprocess as fast as it can accept it.
99 * This isn't ideal as pause latency can be very high as a result. */
101 cmdfd_slot
= addfd(cmdfd
, POLLOUT
);
104 /** @brief Process poll() results for subprocess play */
105 static int command_ready(void) {
106 if(fds
[cmdfd_slot
].revents
& (POLLOUT
| POLLERR
))
112 const struct speaker_backend command_backend
= {