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 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.
31 #include <ao/plugin.h>
33 #include "speaker-protocol.h"
35 /* extra declarations to help out lazy <ao/plugin.h> */
36 int ao_plugin_test(void);
37 ao_info
*ao_plugin_driver_info(void);
38 char *ao_plugin_file_extension(void);
40 /** @brief Private data structure for this driver */
42 int fd
; /* output file descriptor */
43 int exit_on_error
; /* exit on write error */
45 /** @brief Record of sample format */
46 struct stream_header header
;
50 /* like write() but never returns EINTR/EAGAIN or short */
51 static int do_write(int fd
, const void *ptr
, size_t n
) {
56 memset(&ufd
, 0, sizeof ufd
);
60 ret
= write(fd
, (const char *)ptr
+ written
, n
- written
);
65 /* Someone sneakily gave us a nonblocking file descriptor, wait until
66 * we can write again */
67 ret
= poll(&ufd
, 1, -1);
68 if(ret
< 0 && errno
!= EINTR
) return -1;
79 /* return 1 if this driver can be opened */
80 int ao_plugin_test(void) {
84 /* return info about this driver */
85 ao_info
*ao_plugin_driver_info(void) {
86 static const char *options
[] = { "fd" };
87 static const ao_info info
= {
88 AO_TYPE_LIVE
, /* type */
89 (char *)"DisOrder format driver", /* name */
90 (char *)"disorder", /* short_name */
91 (char *)"http://www.greenend.org.uk/rjk/disorder/", /* comment */
92 (char *)"Richard Kettlewell", /* author */
93 AO_FMT_NATIVE
, /* preferred_byte_format */
95 (char **)options
, /* options */
98 return (ao_info
*)&info
;
101 /* initialize the private data structure */
102 int ao_plugin_device_init(ao_device
*device
) {
103 struct internal
*i
= malloc(sizeof (struct internal
));
107 memset(i
, 0, sizeof *i
);
108 if((e
= getenv("DISORDER_RAW_FD")))
112 device
->internal
= i
;
117 int ao_plugin_set_option(ao_device
*device
,
120 struct internal
*i
= device
->internal
;
122 if(!strcmp(key
, "fd"))
124 else if(!strcmp(key
, "fragile"))
125 i
->exit_on_error
= atoi(value
);
126 /* unknown options are required to be ignored */
130 /* open the device */
131 int ao_plugin_open(ao_device
*device
, ao_sample_format
*format
) {
132 struct internal
*i
= device
->internal
;
134 /* we would like native-order samples */
135 device
->driver_byte_format
= AO_FMT_NATIVE
;
136 i
->header
.rate
= format
->rate
;
137 i
->header
.channels
= format
->channels
;
138 i
->header
.bits
= format
->bits
;
139 i
->header
.endian
= ENDIAN_NATIVE
;
143 /* play some samples */
144 int ao_plugin_play(ao_device
*device
, const char *output_samples
,
146 struct internal
*i
= device
->internal
;
148 /* Fill in and write the header */
149 i
->header
.nbytes
= num_bytes
;
150 if(do_write(i
->fd
, &i
->header
, sizeof i
->header
) < 0) {
151 if(i
->exit_on_error
) _exit(-1);
155 /* Write the sample data */
156 if(do_write(i
->fd
, output_samples
, num_bytes
) < 0) {
157 if(i
->exit_on_error
) _exit(-1);
163 /* close the device */
164 int ao_plugin_close(ao_device
attribute((unused
)) *device
) {
168 /* delete private data structures */
169 void ao_plugin_device_clear(ao_device
*device
) {
170 free(device
->internal
);
171 device
->internal
= 0;
174 /* report preferred filename extension */
175 char *ao_plugin_file_extension(void) {