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 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 /** @file server/speaker-command.c
22 * @brief Support for @ref BACKEND_COMMAND */
31 #include "configuration.h"
34 #include "speaker-protocol.h"
37 /** @brief Pipe to subprocess
39 * This is the file descriptor to write to for @ref BACKEND_COMMAND.
41 static int cmdfd
= -1;
43 /** @brief poll array slot for @ref cmdfd
45 * Set by command_beforepoll().
47 static int cmdfd_slot
;
49 /** @brief Start the subprocess for @ref BACKEND_COMMAND */
50 static void fork_cmd(void) {
53 if(cmdfd
!= -1) close(cmdfd
);
58 signal(SIGPIPE
, SIG_DFL
);
62 execl("/bin/sh", "sh", "-c", config
->speaker_command
, (char *)0);
63 fatal(errno
, "error execing /bin/sh");
67 D(("forked cmd %d, fd = %d", cmdpid
, cmdfd
));
70 /** @brief Command backend initialization */
71 static void command_init(void) {
72 info("selected command backend");
76 /** @brief Play to a subprocess */
77 static size_t command_play(size_t frames
) {
78 size_t bytes
= frames
* bpf
;
81 written_bytes
= write(cmdfd
, playing
->buffer
+ playing
->start
, bytes
);
82 D(("actually play %zu bytes, wrote %d",
83 bytes
, written_bytes
));
84 if(written_bytes
< 0) {
87 error(0, "hmm, command died; trying another");
93 fatal(errno
, "error writing to subprocess");
96 return written_bytes
/ bpf
;
99 /** @brief Update poll array for writing to subprocess */
100 static void command_beforepoll(int attribute((unused
)) *timeoutp
) {
101 /* We send sample data to the subprocess as fast as it can accept it.
102 * This isn't ideal as pause latency can be very high as a result. */
104 cmdfd_slot
= addfd(cmdfd
, POLLOUT
);
107 /** @brief Process poll() results for subprocess play */
108 static int command_ready(void) {
109 if(fds
[cmdfd_slot
].revents
& (POLLOUT
| POLLERR
))
115 const struct speaker_backend command_backend
= {