2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2006, 2007, 2009 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 lib/uaudio-command.c
20 * @brief Support for commmand backend
22 * We use the code in @ref lib/uaudio-schedule.c to ensure that we write at
23 * approximately the 'real' rate. For disorder-playrtp this isn't very useful
24 * (thought it might reduce the size of various buffers downstream of us) but
25 * when run from the speaker it means that pausing stands a chance of working.
37 #include "configuration.h"
39 /** @brief Pipe to subprocess */
40 static int command_fd
;
42 /** @brief Child process ID */
43 static pid_t command_pid
;
45 static const char *const command_options
[] = {
51 /** @brief Close pipe and wait for subprocess to terminate */
52 static void command_wait(void) {
58 while((rc
= waitpid(command_pid
, &w
, 0) < 0 && errno
== EINTR
))
61 fatal(errno
, "waitpid");
64 error(0, "command subprocess %s", ws
);
69 /** @brief Create subprocess */
70 static void command_open(void) {
74 if(!(command
= uaudio_get("command", NULL
)))
75 fatal(0, "'command' not set");
77 command_pid
= xfork();
80 signal(SIGPIPE
, SIG_DFL
);
84 /* TODO it would be nice to set some environment variables given the sample
85 * format. The original intended model is that you adapt DisOrder to the
86 * command you run but it'd be nice to support the opposite. */
87 execl("/bin/sh", "sh", "-c", command
, (char *)0);
88 fatal(errno
, "error executing /bin/sh");
94 /** @brief Send audio data to subprocess */
95 static size_t command_play(void *buffer
, size_t nsamples
) {
96 uaudio_schedule_synchronize();
97 const size_t bytes
= nsamples
* uaudio_sample_size
;
98 int written
= write(command_fd
, buffer
, bytes
);
102 return 0; /* will retry */
104 error(0, "audio command subprocess terminated");
107 return 0; /* will retry */
109 fatal(errno
, "error writing to audio command subprocess");
112 const size_t written_samples
= written
/ uaudio_sample_size
;
113 uaudio_schedule_update(written_samples
);
114 return written_samples
;
117 static void command_start(uaudio_callback
*callback
,
119 const char *pausemode
= uaudio_get("pause-mode", "silence");
122 if(!strcmp(pausemode
, "silence"))
123 flags
|= UAUDIO_THREAD_FAKE_PAUSE
;
124 else if(!strcmp(pausemode
, "suspend"))
127 fatal(0, "unknown pause mode '%s'", pausemode
);
129 uaudio_schedule_init();
130 uaudio_thread_start(callback
,
134 4096 / uaudio_sample_size
,
138 static void command_stop(void) {
139 uaudio_thread_stop();
143 static void command_activate(void) {
144 uaudio_schedule_reactivated
= 1;
145 uaudio_thread_activate();
148 static void command_deactivate(void) {
149 uaudio_thread_deactivate();
152 static void command_configure(void) {
153 uaudio_set("command", config
->speaker_command
);
154 uaudio_set("pause-mode", config
->pause_mode
);
157 const struct uaudio uaudio_command
= {
159 .options
= command_options
,
160 .start
= command_start
,
161 .stop
= command_stop
,
162 .activate
= command_activate
,
163 .deactivate
= command_deactivate
,
164 .configure
= command_configure
,