chiark / gitweb /
The command backend now sends 0s rather than suspending output when a
[disorder] / lib / uaudio-command.c
CommitLineData
e8c185c3
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2006, 2007, 2009 Richard Kettlewell
4 * Portions (C) 2007 Mark Wooding
5 *
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.
10 *
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.
15 *
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/>.
18 */
19/** @file lib/uaudio-command.c
ec57f6c9
RK
20 * @brief Support for commmand backend
21 *
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.
26 */
e8c185c3
RK
27#include "common.h"
28
29#include <errno.h>
30#include <unistd.h>
31
32#include "syscalls.h"
33#include "log.h"
34#include "mem.h"
35#include "wstat.h"
36#include "uaudio.h"
ba70caca 37#include "configuration.h"
e8c185c3
RK
38
39/** @brief Pipe to subprocess */
40static int command_fd;
41
42/** @brief Child process ID */
43static pid_t command_pid;
44
45static const char *const command_options[] = {
46 "command",
47 NULL
48};
49
50/** @brief Close pipe and wait for subprocess to terminate */
51static void command_wait(void) {
52 int w;
53 pid_t rc;
54 char *ws;
55
56 close(command_fd);
57 while((rc = waitpid(command_pid, &w, 0) < 0 && errno == EINTR))
58 ;
59 if(rc < 0)
60 fatal(errno, "waitpid");
61 if(w) {
62 ws = wstat(w);
63 error(0, "command subprocess %s", ws);
64 xfree(ws);
65 }
66}
67
68/** @brief Create subprocess */
69static void command_open(void) {
70 int pfd[2];
71 const char *command;
72
b50cfb8a 73 if(!(command = uaudio_get("command", NULL)))
e8c185c3
RK
74 fatal(0, "'command' not set");
75 xpipe(pfd);
76 command_pid = xfork();
77 if(!command_pid) {
78 exitfn = _exit;
79 signal(SIGPIPE, SIG_DFL);
80 xdup2(pfd[0], 0);
81 close(pfd[0]);
82 close(pfd[1]);
83 /* TODO it would be nice to set some environment variables given the sample
84 * format. The original intended model is that you adapt DisOrder to the
85 * command you run but it'd be nice to support the opposite. */
86 execl("/bin/sh", "sh", "-c", command, (char *)0);
87 fatal(errno, "error executing /bin/sh");
88 }
89 close(pfd[0]);
e03eb69e 90 command_fd = pfd[1];
e8c185c3
RK
91}
92
93/** @brief Send audio data to subprocess */
94static size_t command_play(void *buffer, size_t nsamples) {
ec57f6c9 95 uaudio_schedule_synchronize();
e8c185c3
RK
96 const size_t bytes = nsamples * uaudio_sample_size;
97 int written = write(command_fd, buffer, bytes);
98 if(written < 0) {
99 switch(errno) {
100 case EINTR:
101 return 0; /* will retry */
102 case EPIPE:
103 error(0, "audio command subprocess terminated");
104 command_wait();
105 command_open();
106 return 0; /* will retry */
107 default:
108 fatal(errno, "error writing to audio command subprocess");
109 }
110 }
ec57f6c9
RK
111 const size_t written_samples = written / uaudio_sample_size;
112 uaudio_schedule_update(written_samples);
113 return written_samples;
e8c185c3
RK
114}
115
116static void command_start(uaudio_callback *callback,
117 void *userdata) {
118 command_open();
ec57f6c9 119 uaudio_schedule_init();
e8c185c3
RK
120 uaudio_thread_start(callback,
121 userdata,
122 command_play,
123 uaudio_channels,
63761c19
RK
124 4096 / uaudio_sample_size,
125 UAUDIO_THREAD_FAKE_PAUSE);
e8c185c3
RK
126}
127
128static void command_stop(void) {
129 uaudio_thread_stop();
130 command_wait();
131}
132
133static void command_activate(void) {
ec57f6c9 134 uaudio_schedule_reactivated = 1;
e8c185c3
RK
135 uaudio_thread_activate();
136}
137
138static void command_deactivate(void) {
139 uaudio_thread_deactivate();
140}
141
ba70caca
RK
142static void command_configure(void) {
143 uaudio_set("command", config->speaker_command);
144}
145
e8c185c3
RK
146const struct uaudio uaudio_command = {
147 .name = "command",
148 .options = command_options,
149 .start = command_start,
150 .stop = command_stop,
151 .activate = command_activate,
ba70caca
RK
152 .deactivate = command_deactivate,
153 .configure = command_configure,
e8c185c3
RK
154};
155
156/*
157Local Variables:
158c-basic-offset:2
159comment-column:40
160fill-column:79
161indent-tabs-mode:nil
162End:
163*/