chiark / gitweb /
8a8f63cbbf21ad3e16b440d1f80ddcd80a939be5
[disorder] / server / speaker-command.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
4  *
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.
9  *
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.
14  *
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
18  * USA
19  */
20 /** @file server/speaker-command.c
21  * @brief Support for @ref BACKEND_COMMAND */
22
23 #include <config.h>
24 #include "types.h"
25
26 #include <unistd.h>
27 #include <poll.h>
28
29 #include "configuration.h"
30 #include "syscalls.h"
31 #include "log.h"
32 #include "speaker-protocol.h"
33 #include "speaker.h"
34
35 /** @brief Pipe to subprocess
36  *
37  * This is the file descriptor to write to for @ref BACKEND_COMMAND.
38  */
39 static int cmdfd = -1;
40
41 /** @brief poll array slot for @ref cmdfd
42  *
43  * Set by command_beforepoll().
44  */
45 static int cmdfd_slot;
46
47 /** @brief Start the subprocess for @ref BACKEND_COMMAND */
48 static void fork_cmd(void) {
49   pid_t cmdpid;
50   int pfd[2];
51   if(cmdfd != -1) close(cmdfd);
52   xpipe(pfd);
53   cmdpid = xfork();
54   if(!cmdpid) {
55     signal(SIGPIPE, SIG_DFL);
56     xdup2(pfd[0], 0);
57     close(pfd[0]);
58     close(pfd[1]);
59     execl("/bin/sh", "sh", "-c", config->speaker_command, (char *)0);
60     fatal(errno, "error execing /bin/sh");
61   }
62   close(pfd[0]);
63   cmdfd = pfd[1];
64   D(("forked cmd %d, fd = %d", cmdpid, cmdfd));
65 }
66
67 /** @brief Command backend initialization */
68 static void command_init(void) {
69   info("selected command backend");
70   fork_cmd();
71 }
72
73 /** @brief Play to a subprocess */
74 static size_t command_play(size_t frames) {
75   size_t bytes = frames * device_bpf;
76   int written_bytes;
77
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) {
82     switch(errno) {
83     case EPIPE:
84       error(0, "hmm, command died; trying another");
85       fork_cmd();
86       return 0;
87     case EAGAIN:
88       return 0;
89     default:
90       fatal(errno, "error writing to subprocess");
91     }
92   } else
93     return written_bytes / device_bpf;
94 }
95
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. */
100   if(cmdfd >= 0)
101     cmdfd_slot = addfd(cmdfd, POLLOUT);
102 }
103
104 /** @brief Process poll() results for subprocess play */
105 static int command_ready(void) {
106   if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR))
107     return 1;
108   else
109     return 0;
110 }
111
112 const struct speaker_backend command_backend = {
113   BACKEND_COMMAND,
114   FIXED_FORMAT,
115   command_init,
116   0,                                    /* activate */
117   command_play,
118   0,                                    /* deactivate */
119   command_beforepoll,
120   command_ready
121 };
122
123 /*
124 Local Variables:
125 c-basic-offset:2
126 comment-column:40
127 fill-column:79
128 indent-tabs-mode:nil
129 End:
130 */