| 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 | #include <errno.h> |
| 29 | |
| 30 | #include "configuration.h" |
| 31 | #include "syscalls.h" |
| 32 | #include "log.h" |
| 33 | #include "speaker-protocol.h" |
| 34 | #include "speaker.h" |
| 35 | |
| 36 | /** @brief Pipe to subprocess |
| 37 | * |
| 38 | * This is the file descriptor to write to for @ref BACKEND_COMMAND. |
| 39 | */ |
| 40 | static int cmdfd = -1; |
| 41 | |
| 42 | /** @brief poll array slot for @ref cmdfd |
| 43 | * |
| 44 | * Set by command_beforepoll(). |
| 45 | */ |
| 46 | static int cmdfd_slot; |
| 47 | |
| 48 | /** @brief Start the subprocess for @ref BACKEND_COMMAND */ |
| 49 | static void fork_cmd(void) { |
| 50 | pid_t cmdpid; |
| 51 | int pfd[2]; |
| 52 | if(cmdfd != -1) close(cmdfd); |
| 53 | xpipe(pfd); |
| 54 | cmdpid = xfork(); |
| 55 | if(!cmdpid) { |
| 56 | exitfn = _exit; |
| 57 | signal(SIGPIPE, SIG_DFL); |
| 58 | xdup2(pfd[0], 0); |
| 59 | close(pfd[0]); |
| 60 | close(pfd[1]); |
| 61 | execl("/bin/sh", "sh", "-c", config->speaker_command, (char *)0); |
| 62 | fatal(errno, "error execing /bin/sh"); |
| 63 | } |
| 64 | close(pfd[0]); |
| 65 | cmdfd = pfd[1]; |
| 66 | D(("forked cmd %d, fd = %d", cmdpid, cmdfd)); |
| 67 | } |
| 68 | |
| 69 | /** @brief Command backend initialization */ |
| 70 | static void command_init(void) { |
| 71 | info("selected command backend"); |
| 72 | fork_cmd(); |
| 73 | } |
| 74 | |
| 75 | /** @brief Play to a subprocess */ |
| 76 | static size_t command_play(size_t frames) { |
| 77 | size_t bytes = frames * bpf; |
| 78 | int written_bytes; |
| 79 | |
| 80 | written_bytes = write(cmdfd, playing->buffer + playing->start, bytes); |
| 81 | D(("actually play %zu bytes, wrote %d", |
| 82 | bytes, written_bytes)); |
| 83 | if(written_bytes < 0) { |
| 84 | switch(errno) { |
| 85 | case EPIPE: |
| 86 | error(0, "hmm, command died; trying another"); |
| 87 | fork_cmd(); |
| 88 | return 0; |
| 89 | case EAGAIN: |
| 90 | return 0; |
| 91 | default: |
| 92 | fatal(errno, "error writing to subprocess"); |
| 93 | } |
| 94 | } else |
| 95 | return written_bytes / bpf; |
| 96 | } |
| 97 | |
| 98 | /** @brief Update poll array for writing to subprocess */ |
| 99 | static void command_beforepoll(int attribute((unused)) *timeoutp) { |
| 100 | /* We send sample data to the subprocess as fast as it can accept it. |
| 101 | * This isn't ideal as pause latency can be very high as a result. */ |
| 102 | if(cmdfd >= 0) |
| 103 | cmdfd_slot = addfd(cmdfd, POLLOUT); |
| 104 | } |
| 105 | |
| 106 | /** @brief Process poll() results for subprocess play */ |
| 107 | static int command_ready(void) { |
| 108 | if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR)) |
| 109 | return 1; |
| 110 | else |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | const struct speaker_backend command_backend = { |
| 115 | BACKEND_COMMAND, |
| 116 | 0, |
| 117 | command_init, |
| 118 | 0, /* activate */ |
| 119 | command_play, |
| 120 | 0, /* deactivate */ |
| 121 | command_beforepoll, |
| 122 | command_ready |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | Local Variables: |
| 127 | c-basic-offset:2 |
| 128 | comment-column:40 |
| 129 | fill-column:79 |
| 130 | indent-tabs-mode:nil |
| 131 | End: |
| 132 | */ |