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 */
29 #include "configuration.h"
32 #include "speaker-protocol.h"
35 /** @brief Pipe to subprocess
37 * This is the file descriptor to write to for @ref BACKEND_COMMAND.
39 static int cmdfd
= -1;
41 /** @brief poll array slot for @ref cmdfd
43 * Set by command_beforepoll().
45 static int cmdfd_slot
;
47 /** @brief Start the subprocess for @ref BACKEND_COMMAND */
48 static void fork_cmd(void) {
51 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
* device_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
/ device_bpf
;
96 /** @brief Update poll array for writing to subprocess */
97 static void command_beforepoll(void) {
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
= {