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