Debian Codegraph @

src/libout123/modules/nas.c

raw
1 /*
2 nas: audio output via NAS
3
4 copyright ?-2016 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Martin Denn
7 */
8
9 #include "../out123_int.h"
10 #include <fcntl.h>
11 #include <audio/audiolib.h>
12 #include <audio/soundlib.h>
13 #include "../../common/debug.h"
14
15 typedef struct
16 {
17 AuServer *aud;
18 AuFlowID flow;
19 AuDeviceAttributes *da;
20 int numDevices;
21 char *buf;
22 AuUint32 buf_size;
23 AuUint32 buf_cnt;
24 AuBool data_sent;
25 AuBool finished;
26 } InfoRec, *InfoPtr;
27
28 /* seconds */
29 #define NAS_SOUND_PORT_DURATION (ao->device_buffer > 0. ? ao->device_buffer : 5)
30 #define NAS_SOUND_LOW_WATER_MARK 25 /* percent */
31 #define NAS_MAX_FORMAT 10 /* currently, there are 7 supported formats */
32
33
34 /* FIXME: stick this inside userptr inside out123_handle instead */
35 static InfoRec info;
36
37 /* NAS specific routines */
38
39 static void nas_sendData(AuServer *aud, InfoPtr i, AuUint32 numBytes)
40 {
41 if (numBytes < i->buf_cnt) {
42 AuWriteElement(aud, i->flow, 0, numBytes, i->buf, AuFalse, NULL);
43 memmove(i->buf, i->buf + numBytes, i->buf_cnt - numBytes);
44 i->buf_cnt = i->buf_cnt - numBytes;
45 }
46 else {
47 AuWriteElement(aud, i->flow, 0, i->buf_cnt, i->buf,
48 (numBytes > i->buf_cnt), NULL);
49 i->buf_cnt = 0;
50 }
51 i->data_sent = AuTrue;
52 }
53
54 static AuBool nas_eventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler)
55 {
56 InfoPtr i = (InfoPtr) handler->data;
57
58 switch (ev->type)
59 {
60 case AuEventTypeMonitorNotify:
61 i->finished = AuTrue;
62 break;
63 case AuEventTypeElementNotify:
64 {
65 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
66
67 switch (event->kind)
68 {
69 case AuElementNotifyKindLowWater:
70 nas_sendData(aud, i, event->num_bytes);
71 break;
72 case AuElementNotifyKindState:
73 switch (event->cur_state)
74 {
75 case AuStatePause:
76 if (event->reason != AuReasonUser)
77 nas_sendData(aud, i, event->num_bytes);
78 break;
79 case AuStateStop:
80 i->finished = AuTrue;
81 break;
82 }
83 }
84 }
85 }
86 return AuTrue;
87 }
88
89 /* 0 on error */
90 static int nas_createFlow(out123_handle *ao)
91 {
92 AuDeviceID device = AuNone;
93 AuElement elements[2];
94 unsigned char format;
95 AuUint32 buf_samples;
96 int i;
97
98
99 switch(ao->format) {
100 case MPG123_ENC_SIGNED_16:
101 default:
102 if (((char) *(short *)"x")=='x') /* ugly, but painless */
103 format = AuFormatLinearSigned16LSB; /* little endian */
104 else
105 format = AuFormatLinearSigned16MSB; /* big endian */
106 break;
107 case MPG123_ENC_UNSIGNED_8:
108 format = AuFormatLinearUnsigned8;
109 break;
110 case MPG123_ENC_SIGNED_8:
111 format = AuFormatLinearSigned8;
112 break;
113 case MPG123_ENC_ULAW_8:
114 format = AuFormatULAW8;
115 break;
116 }
117 /* look for an output device */
118 for (i = 0; i < AuServerNumDevices(info.aud); i++)
119 if (((AuDeviceKind(AuServerDevice(info.aud, i)) ==
120 AuComponentKindPhysicalOutput) &&
121 AuDeviceNumTracks(AuServerDevice(info.aud, i))
122 == ao->channels )) {
123 device = AuDeviceIdentifier(AuServerDevice(info.aud, i));
124 break;
125 }
126 if (device == AuNone) {
127 if(!AOQUIET)
128 error1( "Couldn't find an output device providing %d channels."
129 , ao->channels );
130 return 0;
131 }
132
133 /* set gain */
134 if(ao->gain >= 0) {
135 info.da = AuGetDeviceAttributes(info.aud, device, NULL);
136 if ((info.da)!=NULL) {
137 AuDeviceGain(info.da) = AuFixedPointFromSum(ao->gain, 0);
138 AuSetDeviceAttributes(info.aud, AuDeviceIdentifier(info.da),
139 AuCompDeviceGainMask, info.da, NULL);
140 }
141 else if(!AOQUIET)
142 error("audio/gain: setable Volume/PCM-Level not supported");
143 }
144
145 if (!(info.flow = AuCreateFlow(info.aud, NULL))) {
146 if(!AOQUIET)
147 error("Couldn't create flow");
148 return 0;
149 }
150
151 buf_samples = ao->rate * NAS_SOUND_PORT_DURATION;
152
153 AuMakeElementImportClient(&elements[0], /* element */
154 (unsigned short) ao->rate,
155 /* rate */
156 format, /* format */
157 ao->channels, /* channels */
158 AuTrue, /* ??? */
159 buf_samples, /* max samples */
160 (AuUint32) (buf_samples / 100
161 * NAS_SOUND_LOW_WATER_MARK),
162 /* low water mark */
163 0, /* num actions */
164 NULL); /* actions */
165 AuMakeElementExportDevice(&elements[1], /* element */
166 0, /* input */
167 device, /* device */
168 (unsigned short) ao->rate,
169 /* rate */
170 AuUnlimitedSamples, /* num samples */
171 0, /* num actions */
172 NULL); /* actions */
173 AuSetElements(info.aud, /* Au server */
174 info.flow, /* flow ID */
175 AuTrue, /* clocked */
176 2, /* num elements */
177 elements, /* elements */
178 NULL); /* return status */
179
180 AuRegisterEventHandler(info.aud, /* Au server */
181 AuEventHandlerIDMask, /* value mask */
182 0, /* type */
183 info.flow, /* id */
184 nas_eventHandler, /* callback */
185 (AuPointer) &info); /* data */
186
187 info.buf_size = buf_samples * ao->channels * AuSizeofFormat(format);
188 info.buf = (char *) malloc(info.buf_size);
189 if (info.buf == NULL) {
190 if(!AOQUIET)
191 error1("Unable to allocate input/output buffer of size %ld",
192 (long)info.buf_size);
193 return 0;
194 }
195 info.buf_cnt = 0;
196 info.data_sent = AuFalse;
197 info.finished = AuFalse;
198
199 AuStartFlow(info.aud, /* Au server */
200 info.flow, /* id */
201 NULL); /* status */
202 return 1; /* success */
203 }
204
205
206 static void flush_nas(out123_handle *ao)
207 {
208 AuEvent ev;
209
210 while ((!info.data_sent) && (!info.finished)) {
211 AuNextEvent(info.aud, AuTrue, &ev);
212 AuDispatchEvent(info.aud, &ev);
213 }
214 info.data_sent = AuFalse;
215 }
216
217
218 /* returning -1 on error, 0 on success... */
219 static int open_nas(out123_handle *ao)
220 {
221 if(!ao) return -1;
222
223 if (!(info.aud = AuOpenServer(ao->device, 0, NULL, 0, NULL, NULL)))
224 {
225 if (ao->device==NULL)
226 {
227 if(!AOQUIET)
228 error("could not open default NAS server");
229 } else
230 {
231 if(!AOQUIET)
232 error1("could not open NAS server %s\n", ao->device);
233 }
234 return -1;
235 }
236 info.buf_size = 0;
237
238 return 0;
239 }
240
241
242 static int get_formats_nas(out123_handle *ao)
243 {
244 int i, j, k, ret;
245
246 ret=0;
247 j = AuServerNumFormats(info.aud);
248 for (i=0; i<j; i++) {
249 k=AuServerFormat(info.aud,i);
250 switch (k)
251 {
252 case AuFormatULAW8:
253 ret |= MPG123_ENC_ULAW_8;
254 break;
255 case AuFormatLinearUnsigned8:
256 ret |= MPG123_ENC_UNSIGNED_8;
257 break;
258 case AuFormatLinearSigned8:
259 ret |= MPG123_ENC_SIGNED_8;
260 break;
261 case AuFormatLinearSigned16LSB:
262 ret |= MPG123_ENC_SIGNED_16;
263 break;
264 }
265 }
266 return ret;
267 }
268
269 static int write_nas(out123_handle *ao,unsigned char *buf,int len)
270 {
271 int buf_cnt = 0;
272
273 if (info.buf_size == 0)
274 if(!nas_createFlow(ao)) return -1;
275
276 while ((info.buf_cnt + (len - buf_cnt)) > info.buf_size) {
277 memcpy(info.buf + info.buf_cnt,
278 buf + buf_cnt,
279 (info.buf_size - info.buf_cnt));
280 buf_cnt += (info.buf_size - info.buf_cnt);
281 info.buf_cnt += (info.buf_size - info.buf_cnt);
282 flush_nas(ao);
283 }
284 memcpy(info.buf + info.buf_cnt,
285 buf + buf_cnt,
286 (len - buf_cnt));
287 info.buf_cnt += (len - buf_cnt);
288
289 return len;
290 }
291
292 static int close_nas(out123_handle *ao)
293 {
294 if (info.aud == NULL) {
295 return 0;
296 }
297
298 if (info.buf_size == 0) {
299 /* Au server opened, but not yet initialized */
300 AuCloseServer(info.aud);
301 return 0;
302 }
303
304 while (!info.finished) {
305 flush_nas(ao);
306 }
307 AuCloseServer(info.aud);
308 free(info.buf);
309
310 return 0;
311 }
312
313
314
315 static int init_nas(out123_handle* ao)
316 {
317 if (ao==NULL) return -1;
318
319 /* Set callbacks */
320 ao->open = open_nas;
321 ao->flush = flush_nas;
322 ao->write = write_nas;
323 ao->get_formats = get_formats_nas;
324 ao->close = close_nas;
325
326 /* Success */
327 return 0;
328 }
329
330
331
332 /*
333 Module information data structure
334 */
335 mpg123_module_t mpg123_output_module_info = {
336 /* api_version */ MPG123_MODULE_API_VERSION,
337 /* name */ "nas",
338 /* description */ "Output audio using NAS (Network Audio System)",
339 /* revision */ "$Rev:$",
340 /* handle */ NULL,
341
342 /* init_output */ init_nas,
343 };
344
345