| 1 | |
| 2 | /* |
| 3 | * This file is part of DisOrder. |
| 4 | * Copyright (C) 2005 Richard Kettlewell |
| 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 | |
| 22 | #include <config.h> |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <errno.h> |
| 27 | #include <unistd.h> |
| 28 | #include <poll.h> |
| 29 | #include <ao/ao.h> |
| 30 | #include <ao/plugin.h> |
| 31 | |
| 32 | /* extra declarations to help out lazy <ao/plugin.h> */ |
| 33 | int ao_plugin_test(void); |
| 34 | ao_info *ao_plugin_driver_info(void); |
| 35 | char *ao_plugin_file_extension(void); |
| 36 | |
| 37 | /* private data structure for this driver */ |
| 38 | struct internal { |
| 39 | int fd; /* output file descriptor */ |
| 40 | int exit_on_error; /* exit on write error */ |
| 41 | }; |
| 42 | |
| 43 | /* like write() but never returns EINTR/EAGAIN or short */ |
| 44 | static int do_write(int fd, const void *ptr, size_t n) { |
| 45 | size_t written = 0; |
| 46 | int ret; |
| 47 | struct pollfd ufd; |
| 48 | |
| 49 | memset(&ufd, 0, sizeof ufd); |
| 50 | ufd.fd = fd; |
| 51 | ufd.events = POLLOUT; |
| 52 | while(written < n) { |
| 53 | ret = write(fd, (const char *)ptr + written, n - written); |
| 54 | if(ret < 0) { |
| 55 | switch(errno) { |
| 56 | case EINTR: break; |
| 57 | case EAGAIN: |
| 58 | /* Someone sneakily gave us a nonblocking file descriptor, wait until |
| 59 | * we can write again */ |
| 60 | ret = poll(&ufd, 1, -1); |
| 61 | if(ret < 0 && errno != EINTR) return -1; |
| 62 | break; |
| 63 | default: |
| 64 | return -1; |
| 65 | } |
| 66 | } else |
| 67 | written += ret; |
| 68 | } |
| 69 | return written; |
| 70 | } |
| 71 | |
| 72 | /* return 1 if this driver can be opened */ |
| 73 | int ao_plugin_test(void) { |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | /* return info about this driver */ |
| 78 | ao_info *ao_plugin_driver_info(void) { |
| 79 | static const char *options[] = { "fd" }; |
| 80 | static const ao_info info = { |
| 81 | AO_TYPE_LIVE, /* type */ |
| 82 | (char *)"DisOrder format driver", /* name */ |
| 83 | (char *)"disorder", /* short_name */ |
| 84 | (char *)"http://www.greenend.org.uk/rjk/disorder/", /* comment */ |
| 85 | (char *)"Richard Kettlewell", /* author */ |
| 86 | AO_FMT_NATIVE, /* preferred_byte_format */ |
| 87 | 0, /* priority */ |
| 88 | (char **)options, /* options */ |
| 89 | 1, /* option_count */ |
| 90 | }; |
| 91 | return (ao_info *)&info; |
| 92 | } |
| 93 | |
| 94 | /* initialize the private data structure */ |
| 95 | int ao_plugin_device_init(ao_device *device) { |
| 96 | struct internal *i = malloc(sizeof (struct internal)); |
| 97 | const char *e; |
| 98 | |
| 99 | if(!i) return 0; |
| 100 | memset(i, 0, sizeof *i); |
| 101 | if((e = getenv("DISORDER_RAW_FD"))) |
| 102 | i->fd = atoi(e); |
| 103 | else |
| 104 | i->fd = 1; |
| 105 | device->internal = i; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | /* set an option */ |
| 110 | int ao_plugin_set_option(ao_device *device, |
| 111 | const char *key, |
| 112 | const char *value) { |
| 113 | struct internal *i = device->internal; |
| 114 | |
| 115 | if(!strcmp(key, "fd")) |
| 116 | i->fd = atoi(value); |
| 117 | else if(!strcmp(key, "fragile")) |
| 118 | i->exit_on_error = atoi(value); |
| 119 | /* unknown options are required to be ignored */ |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /* open the device */ |
| 124 | int ao_plugin_open(ao_device *device, ao_sample_format *format) { |
| 125 | struct internal *i = device->internal; |
| 126 | |
| 127 | /* we would like native-order samples */ |
| 128 | device->driver_byte_format = AO_FMT_NATIVE; |
| 129 | if(do_write(i->fd, format, sizeof *format) < 0) { |
| 130 | if(i->exit_on_error) exit(-1); |
| 131 | return 0; |
| 132 | } |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | /* play some samples */ |
| 137 | int ao_plugin_play(ao_device *device, const char *output_samples, |
| 138 | uint_32 num_bytes) { |
| 139 | struct internal *i = device->internal; |
| 140 | |
| 141 | if(do_write(i->fd, output_samples, num_bytes) < 0) { |
| 142 | if(i->exit_on_error) _exit(-1); |
| 143 | return 0; |
| 144 | } |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | /* close the device */ |
| 149 | int ao_plugin_close(ao_device attribute((unused)) *device) { |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | /* delete private data structures */ |
| 154 | void ao_plugin_device_clear(ao_device *device) { |
| 155 | free(device->internal); |
| 156 | device->internal = 0; |
| 157 | } |
| 158 | |
| 159 | /* report preferred filename extension */ |
| 160 | char *ao_plugin_file_extension(void) { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | Local Variables: |
| 166 | c-basic-offset:2 |
| 167 | comment-column:40 |
| 168 | End: |
| 169 | */ |