2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2010 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file driver/disorder.c
19 * @brief libao driver used by DisOrder
21 * The output from this driver is expected to be fed to @c
22 * disorder-normalize to convert to the confnigured target format.
24 * @attention This driver will not build with libao 1.0.0. libao has
25 * taken away half the plugin API and not provided any replacement.
34 #include <ao/plugin.h>
36 #include "speaker-protocol.h"
38 /* extra declarations to help out lazy <ao/plugin.h> */
39 int ao_plugin_test(void);
40 ao_info *ao_plugin_driver_info(void);
41 char *ao_plugin_file_extension(void);
43 /** @brief Private data structure for this driver */
45 int fd; /* output file descriptor */
46 int exit_on_error; /* exit on write error */
48 /** @brief Record of sample format */
49 struct stream_header header;
53 /* like write() but never returns EINTR/EAGAIN or short */
54 static int do_write(int fd, const void *ptr, size_t n) {
59 memset(&ufd, 0, sizeof ufd);
63 ret = write(fd, (const char *)ptr + written, n - written);
68 /* Someone sneakily gave us a nonblocking file descriptor, wait until
69 * we can write again */
70 ret = poll(&ufd, 1, -1);
71 if(ret < 0 && errno != EINTR) return -1;
82 /* return 1 if this driver can be opened */
83 int ao_plugin_test(void) {
87 /* return info about this driver */
88 ao_info *ao_plugin_driver_info(void) {
89 static const char *options[] = { "fd" };
90 static const ao_info info = {
91 AO_TYPE_LIVE, /* type */
92 (char *)"DisOrder format driver", /* name */
93 (char *)"disorder", /* short_name */
94 (char *)"http://www.greenend.org.uk/rjk/disorder/", /* comment */
95 (char *)"Richard Kettlewell", /* author */
96 AO_FMT_NATIVE, /* preferred_byte_format */
98 (char **)options, /* options */
101 return (ao_info *)&info;
104 /* initialize the private data structure */
105 int ao_plugin_device_init(ao_device *device) {
106 struct internal *i = malloc(sizeof (struct internal));
110 memset(i, 0, sizeof *i);
111 if((e = getenv("DISORDER_RAW_FD")))
115 device->internal = i;
120 int ao_plugin_set_option(ao_device *device,
123 struct internal *i = device->internal;
125 if(!strcmp(key, "fd"))
127 else if(!strcmp(key, "fragile"))
128 i->exit_on_error = atoi(value);
129 /* unknown options are required to be ignored */
133 /* open the device */
134 int ao_plugin_open(ao_device *device, ao_sample_format *format) {
135 struct internal *i = device->internal;
137 /* we would like native-order samples */
138 device->driver_byte_format = AO_FMT_NATIVE;
139 i->header.rate = format->rate;
140 i->header.channels = format->channels;
141 i->header.bits = format->bits;
142 i->header.endian = ENDIAN_NATIVE;
146 /* play some samples */
147 int ao_plugin_play(ao_device *device, const char *output_samples,
149 struct internal *i = device->internal;
151 /* Fill in and write the header */
152 i->header.nbytes = num_bytes;
153 if(do_write(i->fd, &i->header, sizeof i->header) < 0) {
154 if(i->exit_on_error) _exit(-1);
158 /* Write the sample data */
159 if(do_write(i->fd, output_samples, num_bytes) < 0) {
160 if(i->exit_on_error) _exit(-1);
166 /* close the device */
167 int ao_plugin_close(ao_device attribute((unused)) *device) {
171 /* delete private data structures */
172 void ao_plugin_device_clear(ao_device *device) {
173 free(device->internal);
174 device->internal = 0;
177 /* report preferred filename extension */
178 char *ao_plugin_file_extension(void) {