2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007 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 2 of the License, or
8 * (at your option) any later version.
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.
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
20 /** @file driver/disorder.c
21 * @brief libao driver used by DisOrder
23 * The output from this driver is expected to be fed to @c
24 * disorder-normalize to convert to the confnigured target format.
33 #include <ao/plugin.h>
35 #include "speaker-protocol.h"
37 /* extra declarations to help out lazy <ao/plugin.h> */
38 int ao_plugin_test(void);
39 ao_info *ao_plugin_driver_info(void);
40 char *ao_plugin_file_extension(void);
42 /** @brief Private data structure for this driver */
44 int fd; /* output file descriptor */
45 int exit_on_error; /* exit on write error */
47 /** @brief Record of sample format */
48 struct stream_header header;
52 /* like write() but never returns EINTR/EAGAIN or short */
53 static int do_write(int fd, const void *ptr, size_t n) {
58 memset(&ufd, 0, sizeof ufd);
62 ret = write(fd, (const char *)ptr + written, n - written);
67 /* Someone sneakily gave us a nonblocking file descriptor, wait until
68 * we can write again */
69 ret = poll(&ufd, 1, -1);
70 if(ret < 0 && errno != EINTR) return -1;
81 /* return 1 if this driver can be opened */
82 int ao_plugin_test(void) {
86 /* return info about this driver */
87 ao_info *ao_plugin_driver_info(void) {
88 static const char *options[] = { "fd" };
89 static const ao_info info = {
90 AO_TYPE_LIVE, /* type */
91 (char *)"DisOrder format driver", /* name */
92 (char *)"disorder", /* short_name */
93 (char *)"http://www.greenend.org.uk/rjk/disorder/", /* comment */
94 (char *)"Richard Kettlewell", /* author */
95 AO_FMT_NATIVE, /* preferred_byte_format */
97 (char **)options, /* options */
100 return (ao_info *)&info;
103 /* initialize the private data structure */
104 int ao_plugin_device_init(ao_device *device) {
105 struct internal *i = malloc(sizeof (struct internal));
109 memset(i, 0, sizeof *i);
110 if((e = getenv("DISORDER_RAW_FD")))
114 device->internal = i;
119 int ao_plugin_set_option(ao_device *device,
122 struct internal *i = device->internal;
124 if(!strcmp(key, "fd"))
126 else if(!strcmp(key, "fragile"))
127 i->exit_on_error = atoi(value);
128 /* unknown options are required to be ignored */
132 /* open the device */
133 int ao_plugin_open(ao_device *device, ao_sample_format *format) {
134 struct internal *i = device->internal;
136 /* we would like native-order samples */
137 device->driver_byte_format = AO_FMT_NATIVE;
138 i->header.rate = format->rate;
139 i->header.channels = format->channels;
140 i->header.bits = format->bits;
141 i->header.endian = ENDIAN_NATIVE;
145 /* play some samples */
146 int ao_plugin_play(ao_device *device, const char *output_samples,
148 struct internal *i = device->internal;
150 /* Fill in and write the header */
151 i->header.nbytes = num_bytes;
152 if(do_write(i->fd, &i->header, sizeof i->header) < 0) {
153 if(i->exit_on_error) _exit(-1);
157 /* Write the sample data */
158 if(do_write(i->fd, output_samples, num_bytes) < 0) {
159 if(i->exit_on_error) _exit(-1);
165 /* close the device */
166 int ao_plugin_close(ao_device attribute((unused)) *device) {
170 /* delete private data structures */
171 void ao_plugin_device_clear(ao_device *device) {
172 free(device->internal);
173 device->internal = 0;
176 /* report preferred filename extension */
177 char *ao_plugin_file_extension(void) {