chiark / gitweb /
Check whether libao has a working plugin interface and don't build the
[disorder] / driver / disorder.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
86541f5a 3 * Copyright (C) 2005, 2007, 2010 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
6d2d327c
RK
18/** @file driver/disorder.c
19 * @brief libao driver used by DisOrder
20 *
21 * The output from this driver is expected to be fed to @c
22 * disorder-normalize to convert to the confnigured target format.
86541f5a
RK
23 *
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.
6d2d327c 26 */
460b9539 27
05b75f8d 28#include "common.h"
460b9539 29
460b9539 30#include <errno.h>
31#include <unistd.h>
32#include <poll.h>
33#include <ao/ao.h>
34#include <ao/plugin.h>
35
6d2d327c
RK
36#include "speaker-protocol.h"
37
460b9539 38/* extra declarations to help out lazy <ao/plugin.h> */
39int ao_plugin_test(void);
40ao_info *ao_plugin_driver_info(void);
41char *ao_plugin_file_extension(void);
42
6d2d327c 43/** @brief Private data structure for this driver */
460b9539 44struct internal {
45 int fd; /* output file descriptor */
46 int exit_on_error; /* exit on write error */
6d2d327c
RK
47
48 /** @brief Record of sample format */
49 struct stream_header header;
50
460b9539 51};
52
53/* like write() but never returns EINTR/EAGAIN or short */
54static int do_write(int fd, const void *ptr, size_t n) {
55 size_t written = 0;
56 int ret;
57 struct pollfd ufd;
58
59 memset(&ufd, 0, sizeof ufd);
60 ufd.fd = fd;
61 ufd.events = POLLOUT;
62 while(written < n) {
63 ret = write(fd, (const char *)ptr + written, n - written);
64 if(ret < 0) {
65 switch(errno) {
66 case EINTR: break;
67 case EAGAIN:
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;
72 break;
73 default:
74 return -1;
75 }
76 } else
77 written += ret;
78 }
79 return written;
80}
81
82/* return 1 if this driver can be opened */
83int ao_plugin_test(void) {
84 return 1;
85}
86
87/* return info about this driver */
88ao_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 */
97 0, /* priority */
98 (char **)options, /* options */
99 1, /* option_count */
100 };
101 return (ao_info *)&info;
102}
103
104/* initialize the private data structure */
105int ao_plugin_device_init(ao_device *device) {
106 struct internal *i = malloc(sizeof (struct internal));
107 const char *e;
108
109 if(!i) return 0;
110 memset(i, 0, sizeof *i);
111 if((e = getenv("DISORDER_RAW_FD")))
112 i->fd = atoi(e);
113 else
114 i->fd = 1;
115 device->internal = i;
116 return 1;
117}
118
119/* set an option */
120int ao_plugin_set_option(ao_device *device,
121 const char *key,
122 const char *value) {
123 struct internal *i = device->internal;
124
125 if(!strcmp(key, "fd"))
126 i->fd = atoi(value);
127 else if(!strcmp(key, "fragile"))
128 i->exit_on_error = atoi(value);
129 /* unknown options are required to be ignored */
130 return 1;
131}
132
133/* open the device */
134int ao_plugin_open(ao_device *device, ao_sample_format *format) {
135 struct internal *i = device->internal;
136
137 /* we would like native-order samples */
138 device->driver_byte_format = AO_FMT_NATIVE;
6d2d327c
RK
139 i->header.rate = format->rate;
140 i->header.channels = format->channels;
141 i->header.bits = format->bits;
142 i->header.endian = ENDIAN_NATIVE;
460b9539 143 return 1;
144}
145
146/* play some samples */
147int ao_plugin_play(ao_device *device, const char *output_samples,
148 uint_32 num_bytes) {
149 struct internal *i = device->internal;
150
6d2d327c
RK
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);
155 return 0;
156 }
157
158 /* Write the sample data */
460b9539 159 if(do_write(i->fd, output_samples, num_bytes) < 0) {
160 if(i->exit_on_error) _exit(-1);
161 return 0;
162 }
163 return 1;
164}
165
166/* close the device */
167int ao_plugin_close(ao_device attribute((unused)) *device) {
168 return 1;
169}
170
171/* delete private data structures */
172void ao_plugin_device_clear(ao_device *device) {
173 free(device->internal);
174 device->internal = 0;
175}
176
177/* report preferred filename extension */
178char *ao_plugin_file_extension(void) {
179 return 0;
180}
181
182/*
183Local Variables:
184c-basic-offset:2
185comment-column:40
186End:
187*/