chiark / gitweb /
Disobedience now keeps track of known playlists and has a (not yet
[disorder] / driver / disorder.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007 Richard Kettlewell
4  *
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.
9  *
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.
14  *
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
18  * USA
19  */
20 /** @file driver/disorder.c
21  * @brief libao driver used by DisOrder
22  *
23  * The output from this driver is expected to be fed to @c
24  * disorder-normalize to convert to the confnigured target format.
25  */
26
27 #include "common.h"
28
29 #include <errno.h>
30 #include <unistd.h>
31 #include <poll.h>
32 #include <ao/ao.h>
33 #include <ao/plugin.h>
34
35 #include "speaker-protocol.h"
36
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);
41
42 /** @brief Private data structure for this driver */
43 struct internal {
44   int fd;                               /* output file descriptor */
45   int exit_on_error;                    /* exit on write error */
46
47   /** @brief Record of sample format */
48   struct stream_header header;
49
50 };
51
52 /* like write() but never returns EINTR/EAGAIN or short */
53 static int do_write(int fd, const void *ptr, size_t n) {
54   size_t written = 0;
55   int ret;
56   struct pollfd ufd;
57
58   memset(&ufd, 0, sizeof ufd);
59   ufd.fd = fd;
60   ufd.events = POLLOUT;
61   while(written < n) {
62     ret = write(fd, (const char *)ptr + written, n - written);
63     if(ret < 0) {
64       switch(errno) {
65       case EINTR: break;
66       case EAGAIN:
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;
71         break;
72       default:
73         return -1;
74       }
75     } else
76       written += ret;
77   }
78   return written;
79 }
80
81 /* return 1 if this driver can be opened */
82 int ao_plugin_test(void) {
83   return 1;
84 }
85
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 */
96     0,                                  /* priority */
97     (char **)options,                   /* options */
98     1,                                  /* option_count */
99   };
100   return (ao_info *)&info;
101 }
102
103 /* initialize the private data structure */
104 int ao_plugin_device_init(ao_device *device) {
105   struct internal *i = malloc(sizeof (struct internal));
106   const char *e;
107
108   if(!i) return 0;
109   memset(i, 0, sizeof *i);
110   if((e = getenv("DISORDER_RAW_FD")))
111     i->fd = atoi(e);
112   else
113     i->fd = 1;
114   device->internal = i;
115   return 1;
116 }
117
118 /* set an option */
119 int ao_plugin_set_option(ao_device *device,
120                          const char *key,
121                          const char *value) {
122   struct internal *i = device->internal;
123
124   if(!strcmp(key, "fd"))
125     i->fd = atoi(value);
126   else if(!strcmp(key, "fragile"))
127     i->exit_on_error = atoi(value);
128   /* unknown options are required to be ignored */
129   return 1;
130 }
131
132 /* open the device */
133 int ao_plugin_open(ao_device *device, ao_sample_format *format) {
134   struct internal *i = device->internal;
135   
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;
142   return 1;
143 }
144
145 /* play some samples */
146 int ao_plugin_play(ao_device *device, const char *output_samples, 
147                    uint_32 num_bytes) {
148   struct internal *i = device->internal;
149
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);
154     return 0;
155   }
156
157   /* Write the sample data */
158   if(do_write(i->fd, output_samples, num_bytes) < 0) {
159     if(i->exit_on_error) _exit(-1);
160     return 0;
161   }
162   return 1;
163 }
164
165 /* close the device */
166 int ao_plugin_close(ao_device attribute((unused)) *device) {
167   return 1;
168 }
169
170 /* delete private data structures */
171 void ao_plugin_device_clear(ao_device *device) {
172   free(device->internal);
173   device->internal = 0;
174 }
175
176 /* report preferred filename extension */
177 char *ao_plugin_file_extension(void) {
178   return 0;
179 }
180
181 /*
182 Local Variables:
183 c-basic-offset:2
184 comment-column:40
185 End:
186 */