]> code.delx.au - pulseaudio/blob - src/modules/module-solaris.c
split polypcore/util.[ch] into polypcore/core-util.[ch] and polyp/util.[ch]
[pulseaudio] / src / modules / module-solaris.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/ioctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 #include <signal.h>
39 #include <stropts.h>
40 #include <sys/conf.h>
41 #include <sys/audio.h>
42
43 #include <polyp/mainloop-signal.h>
44 #include <polyp/xmalloc.h>
45
46 #include <polypcore/iochannel.h>
47 #include <polypcore/sink.h>
48 #include <polypcore/source.h>
49 #include <polypcore/module.h>
50 #include <polypcore/sample-util.h>
51 #include <polypcore/core-util.h>
52 #include <polypcore/modargs.h>
53 #include <polypcore/log.h>
54
55 #include "module-solaris-symdef.h"
56
57 PA_MODULE_AUTHOR("Pierre Ossman")
58 PA_MODULE_DESCRIPTION("Solaris Sink/Source")
59 PA_MODULE_VERSION(PACKAGE_VERSION)
60 PA_MODULE_USAGE(
61 "sink_name=<name for the sink> "
62 "source_name=<name for the source> "
63 "device=<OSS device> record=<enable source?> "
64 "playback=<enable sink?> "
65 "format=<sample format> "
66 "channels=<number of channels> "
67 "rate=<sample rate> "
68 "buffer_size=<record buffer size> "
69 "channel_map=<channel map>")
70
71 struct userdata {
72 pa_sink *sink;
73 pa_source *source;
74 pa_iochannel *io;
75 pa_core *core;
76 pa_time_event *timer;
77 pa_usec_t poll_timeout;
78 pa_signal_event *sig;
79
80 pa_memchunk memchunk, silence;
81
82 uint32_t frame_size;
83 uint32_t buffer_size;
84 unsigned int written_bytes, read_bytes;
85
86 int fd;
87 pa_module *module;
88 };
89
90 static const char* const valid_modargs[] = {
91 "sink_name",
92 "source_name",
93 "device",
94 "record",
95 "playback",
96 "buffer_size",
97 "format",
98 "rate",
99 "channels",
100 "channel_map",
101 NULL
102 };
103
104 #define DEFAULT_SINK_NAME "solaris_output"
105 #define DEFAULT_SOURCE_NAME "solaris_input"
106 #define DEFAULT_DEVICE "/dev/audio"
107
108 #define CHUNK_SIZE 2048
109
110 static void update_usage(struct userdata *u) {
111 pa_module_set_used(u->module,
112 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
113 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
114 (u->source ? pa_idxset_size(u->source->outputs) : 0));
115 }
116
117 static void do_write(struct userdata *u) {
118 audio_info_t info;
119 int err;
120 pa_memchunk *memchunk;
121 size_t len;
122 ssize_t r;
123
124 assert(u);
125
126 /* We cannot check pa_iochannel_is_writable() because of our buffer hack */
127 if (!u->sink)
128 return;
129
130 update_usage(u);
131
132 err = ioctl(u->fd, AUDIO_GETINFO, &info);
133 assert(err >= 0);
134
135 /*
136 * Since we cannot modify the size of the output buffer we fake it
137 * by not filling it more than u->buffer_size.
138 */
139 len = u->buffer_size;
140 len -= u->written_bytes - (info.play.samples * u->frame_size);
141
142 /* The sample counter can sometimes go backwards :( */
143 if (len > u->buffer_size)
144 len = 0;
145
146 if (len == u->buffer_size)
147 pa_log_debug(__FILE__": Solaris buffer underflow!");
148
149 len -= len % u->frame_size;
150
151 if (len == 0)
152 return;
153
154 memchunk = &u->memchunk;
155
156 if (!memchunk->length)
157 if (pa_sink_render(u->sink, len, memchunk) < 0)
158 memchunk = &u->silence;
159
160 assert(memchunk->memblock);
161 assert(memchunk->memblock->data);
162 assert(memchunk->length);
163
164 if (memchunk->length < len) {
165 len = memchunk->length;
166 len -= len % u->frame_size;
167 assert(len);
168 }
169
170 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, len)) < 0) {
171 pa_log(__FILE__": write() failed: %s", strerror(errno));
172 return;
173 }
174
175 assert(r % u->frame_size == 0);
176
177 if (memchunk != &u->silence) {
178 u->memchunk.index += r;
179 u->memchunk.length -= r;
180
181 if (u->memchunk.length <= 0) {
182 pa_memblock_unref(u->memchunk.memblock);
183 u->memchunk.memblock = NULL;
184 }
185 }
186
187 u->written_bytes += r;
188 }
189
190 static void do_read(struct userdata *u) {
191 pa_memchunk memchunk;
192 int err, l;
193 ssize_t r;
194 assert(u);
195
196 if (!u->source || !pa_iochannel_is_readable(u->io))
197 return;
198
199 update_usage(u);
200
201 err = ioctl(u->fd, I_NREAD, &l);
202 assert(err >= 0);
203
204 memchunk.memblock = pa_memblock_new(l, u->core->memblock_stat);
205 assert(memchunk.memblock);
206 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
207 pa_memblock_unref(memchunk.memblock);
208 if (errno != EAGAIN)
209 pa_log(__FILE__": read() failed: %s", strerror(errno));
210 return;
211 }
212
213 assert(r <= (ssize_t) memchunk.memblock->length);
214 memchunk.length = memchunk.memblock->length = r;
215 memchunk.index = 0;
216
217 pa_source_post(u->source, &memchunk);
218 pa_memblock_unref(memchunk.memblock);
219
220 u->read_bytes += r;
221 }
222
223 static void io_callback(pa_iochannel *io, void*userdata) {
224 struct userdata *u = userdata;
225 assert(u);
226 do_write(u);
227 do_read(u);
228 }
229
230 static void timer_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
231 struct userdata *u = userdata;
232 struct timeval ntv;
233
234 assert(u);
235
236 do_write(u);
237
238 pa_gettimeofday(&ntv);
239 pa_timeval_add(&ntv, u->poll_timeout);
240
241 a->time_restart(e, &ntv);
242 }
243
244 static void sig_callback(pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata) {
245 struct userdata *u = userdata;
246 pa_cvolume old_vol;
247
248 assert(u);
249
250 if (u->sink) {
251 assert(u->sink->get_hw_volume);
252 memcpy(&old_vol, &u->sink->hw_volume, sizeof(pa_cvolume));
253 if (u->sink->get_hw_volume(u->sink) < 0)
254 return;
255 if (memcmp(&old_vol, &u->sink->hw_volume, sizeof(pa_cvolume)) != 0) {
256 pa_subscription_post(u->sink->core,
257 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
258 u->sink->index);
259 }
260 }
261
262 if (u->source) {
263 assert(u->source->get_hw_volume);
264 memcpy(&old_vol, &u->source->hw_volume, sizeof(pa_cvolume));
265 if (u->source->get_hw_volume(u->source) < 0)
266 return;
267 if (memcmp(&old_vol, &u->source->hw_volume, sizeof(pa_cvolume)) != 0) {
268 pa_subscription_post(u->source->core,
269 PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
270 u->source->index);
271 }
272 }
273 }
274
275 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
276 pa_usec_t r = 0;
277 audio_info_t info;
278 int err;
279 struct userdata *u = s->userdata;
280 assert(s && u && u->sink);
281
282 err = ioctl(u->fd, AUDIO_GETINFO, &info);
283 assert(err >= 0);
284
285 r += pa_bytes_to_usec(u->written_bytes, &s->sample_spec);
286 r -= pa_bytes_to_usec(info.play.samples * u->frame_size, &s->sample_spec);
287
288 if (u->memchunk.memblock)
289 r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
290
291 return r;
292 }
293
294 static pa_usec_t source_get_latency_cb(pa_source *s) {
295 pa_usec_t r = 0;
296 struct userdata *u = s->userdata;
297 audio_info_t info;
298 int err;
299 assert(s && u && u->source);
300
301 err = ioctl(u->fd, AUDIO_GETINFO, &info);
302 assert(err >= 0);
303
304 r += pa_bytes_to_usec(info.record.samples * u->frame_size, &s->sample_spec);
305 r -= pa_bytes_to_usec(u->read_bytes, &s->sample_spec);
306
307 return r;
308 }
309
310 static int sink_get_hw_volume_cb(pa_sink *s) {
311 struct userdata *u = s->userdata;
312 audio_info_t info;
313 int err;
314
315 err = ioctl(u->fd, AUDIO_GETINFO, &info);
316 assert(err >= 0);
317
318 pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
319 info.play.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
320
321 return 0;
322 }
323
324 static int sink_set_hw_volume_cb(pa_sink *s) {
325 struct userdata *u = s->userdata;
326 audio_info_t info;
327
328 AUDIO_INITINFO(&info);
329
330 info.play.gain = pa_cvolume_avg(&s->hw_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
331 assert(info.play.gain <= AUDIO_MAX_GAIN);
332
333 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
334 if (errno == EINVAL)
335 pa_log(__FILE__": AUDIO_SETINFO: Unsupported volume.");
336 else
337 pa_log(__FILE__": AUDIO_SETINFO: %s", strerror(errno));
338 return -1;
339 }
340
341 return 0;
342 }
343
344 static int sink_get_hw_mute_cb(pa_sink *s) {
345 struct userdata *u = s->userdata;
346 audio_info_t info;
347 int err;
348
349 err = ioctl(u->fd, AUDIO_GETINFO, &info);
350 assert(err >= 0);
351
352 s->hw_muted = !!info.output_muted;
353
354 return 0;
355 }
356
357 static int sink_set_hw_mute_cb(pa_sink *s) {
358 struct userdata *u = s->userdata;
359 audio_info_t info;
360
361 AUDIO_INITINFO(&info);
362
363 info.output_muted = !!s->hw_muted;
364
365 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
366 pa_log(__FILE__": AUDIO_SETINFO: %s", strerror(errno));
367 return -1;
368 }
369
370 return 0;
371 }
372
373 static int source_get_hw_volume_cb(pa_source *s) {
374 struct userdata *u = s->userdata;
375 audio_info_t info;
376 int err;
377
378 err = ioctl(u->fd, AUDIO_GETINFO, &info);
379 assert(err >= 0);
380
381 pa_cvolume_set(&s->hw_volume, s->hw_volume.channels,
382 info.record.gain * PA_VOLUME_NORM / AUDIO_MAX_GAIN);
383
384 return 0;
385 }
386
387 static int source_set_hw_volume_cb(pa_source *s) {
388 struct userdata *u = s->userdata;
389 audio_info_t info;
390
391 AUDIO_INITINFO(&info);
392
393 info.record.gain = pa_cvolume_avg(&s->hw_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
394 assert(info.record.gain <= AUDIO_MAX_GAIN);
395
396 if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
397 if (errno == EINVAL)
398 pa_log(__FILE__": AUDIO_SETINFO: Unsupported volume.");
399 else
400 pa_log(__FILE__": AUDIO_SETINFO: %s", strerror(errno));
401 return -1;
402 }
403
404 return 0;
405 }
406
407 static int pa_solaris_auto_format(int fd, int mode, pa_sample_spec *ss) {
408 audio_info_t info;
409
410 AUDIO_INITINFO(&info);
411
412 if (mode != O_RDONLY) {
413 info.play.sample_rate = ss->rate;
414 info.play.channels = ss->channels;
415 switch (ss->format) {
416 case PA_SAMPLE_U8:
417 info.play.precision = 8;
418 info.play.encoding = AUDIO_ENCODING_LINEAR;
419 break;
420 case PA_SAMPLE_ALAW:
421 info.play.precision = 8;
422 info.play.encoding = AUDIO_ENCODING_ALAW;
423 break;
424 case PA_SAMPLE_ULAW:
425 info.play.precision = 8;
426 info.play.encoding = AUDIO_ENCODING_ULAW;
427 break;
428 case PA_SAMPLE_S16NE:
429 info.play.precision = 16;
430 info.play.encoding = AUDIO_ENCODING_LINEAR;
431 break;
432 default:
433 return -1;
434 }
435 }
436
437 if (mode != O_WRONLY) {
438 info.record.sample_rate = ss->rate;
439 info.record.channels = ss->channels;
440 switch (ss->format) {
441 case PA_SAMPLE_U8:
442 info.record.precision = 8;
443 info.record.encoding = AUDIO_ENCODING_LINEAR;
444 break;
445 case PA_SAMPLE_ALAW:
446 info.record.precision = 8;
447 info.record.encoding = AUDIO_ENCODING_ALAW;
448 break;
449 case PA_SAMPLE_ULAW:
450 info.record.precision = 8;
451 info.record.encoding = AUDIO_ENCODING_ULAW;
452 break;
453 case PA_SAMPLE_S16NE:
454 info.record.precision = 16;
455 info.record.encoding = AUDIO_ENCODING_LINEAR;
456 break;
457 default:
458 return -1;
459 }
460 }
461
462 if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
463 if (errno == EINVAL)
464 pa_log(__FILE__": AUDIO_SETINFO: Unsupported sample format.");
465 else
466 pa_log(__FILE__": AUDIO_SETINFO: %s", strerror(errno));
467 return -1;
468 }
469
470 return 0;
471 }
472
473 static int pa_solaris_set_buffer(int fd, int buffer_size) {
474 audio_info_t info;
475
476 AUDIO_INITINFO(&info);
477
478 info.record.buffer_size = buffer_size;
479
480 if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
481 if (errno == EINVAL)
482 pa_log(__FILE__": AUDIO_SETINFO: Unsupported buffer size.");
483 else
484 pa_log(__FILE__": AUDIO_SETINFO: %s", strerror(errno));
485 return -1;
486 }
487
488 return 0;
489 }
490
491 int pa__init(pa_core *c, pa_module*m) {
492 struct userdata *u = NULL;
493 const char *p;
494 int fd = -1;
495 int buffer_size;
496 int mode;
497 int record = 1, playback = 1;
498 pa_sample_spec ss;
499 pa_channel_map map;
500 pa_modargs *ma = NULL;
501 struct timeval tv;
502 assert(c && m);
503
504 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
505 pa_log(__FILE__": failed to parse module arguments.");
506 goto fail;
507 }
508
509 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
510 pa_log(__FILE__": record= and playback= expect numeric argument.");
511 goto fail;
512 }
513
514 if (!playback && !record) {
515 pa_log(__FILE__": neither playback nor record enabled for device.");
516 goto fail;
517 }
518
519 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
520
521 buffer_size = 16384;
522 if (pa_modargs_get_value_s32(ma, "buffer_size", &buffer_size) < 0) {
523 pa_log(__FILE__": failed to parse buffer size argument");
524 goto fail;
525 }
526
527 ss = c->default_sample_spec;
528 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
529 pa_log(__FILE__": failed to parse sample specification");
530 goto fail;
531 }
532
533 if ((fd = open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), mode | O_NONBLOCK)) < 0)
534 goto fail;
535
536 pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
537
538 if (pa_solaris_auto_format(fd, mode, &ss) < 0)
539 goto fail;
540
541 if ((mode != O_WRONLY) && (buffer_size >= 1))
542 if (pa_solaris_set_buffer(fd, buffer_size) < 0)
543 goto fail;
544
545 u = pa_xmalloc(sizeof(struct userdata));
546 u->core = c;
547
548 if (mode != O_WRONLY) {
549 u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
550 assert(u->source);
551 u->source->userdata = u;
552 u->source->get_latency = source_get_latency_cb;
553 u->source->get_hw_volume = source_get_hw_volume_cb;
554 u->source->set_hw_volume = source_set_hw_volume_cb;
555 pa_source_set_owner(u->source, m);
556 u->source->description = pa_sprintf_malloc("Solaris PCM on '%s'", p);
557 } else
558 u->source = NULL;
559
560 if (mode != O_RDONLY) {
561 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
562 assert(u->sink);
563 u->sink->get_latency = sink_get_latency_cb;
564 u->sink->get_hw_volume = sink_get_hw_volume_cb;
565 u->sink->set_hw_volume = sink_set_hw_volume_cb;
566 u->sink->get_hw_mute = sink_get_hw_mute_cb;
567 u->sink->set_hw_mute = sink_set_hw_mute_cb;
568 u->sink->userdata = u;
569 pa_sink_set_owner(u->sink, m);
570 u->sink->description = pa_sprintf_malloc("Solaris PCM on '%s'", p);
571 } else
572 u->sink = NULL;
573
574 assert(u->source || u->sink);
575
576 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
577 assert(u->io);
578 pa_iochannel_set_callback(u->io, io_callback, u);
579 u->fd = fd;
580
581 u->memchunk.memblock = NULL;
582 u->memchunk.length = 0;
583 u->frame_size = pa_frame_size(&ss);
584 u->buffer_size = buffer_size;
585
586 u->silence.memblock = pa_memblock_new(u->silence.length = CHUNK_SIZE, u->core->memblock_stat);
587 assert(u->silence.memblock);
588 pa_silence_memblock(u->silence.memblock, &ss);
589 u->silence.index = 0;
590
591 u->written_bytes = 0;
592 u->read_bytes = 0;
593
594 u->module = m;
595 m->userdata = u;
596
597 u->poll_timeout = pa_bytes_to_usec(u->buffer_size / 10, &ss);
598
599 pa_gettimeofday(&tv);
600 pa_timeval_add(&tv, u->poll_timeout);
601
602 u->timer = c->mainloop->time_new(c->mainloop, &tv, timer_cb, u);
603 assert(u->timer);
604
605 u->sig = pa_signal_new(SIGPOLL, sig_callback, u);
606 assert(u->sig);
607 ioctl(u->fd, I_SETSIG, S_MSG);
608
609 pa_modargs_free(ma);
610
611 /* Read mixer settings */
612 if (u->source)
613 source_get_hw_volume_cb(u->source);
614 if (u->sink) {
615 sink_get_hw_volume_cb(u->sink);
616 sink_get_hw_mute_cb(u->sink);
617 }
618
619 return 0;
620
621 fail:
622 if (fd >= 0)
623 close(fd);
624
625 if (ma)
626 pa_modargs_free(ma);
627
628 return -1;
629 }
630
631 void pa__done(pa_core *c, pa_module*m) {
632 struct userdata *u;
633 assert(c && m);
634
635 if (!(u = m->userdata))
636 return;
637
638 if (u->timer)
639 c->mainloop->time_free(u->timer);
640 ioctl(u->fd, I_SETSIG, 0);
641 pa_signal_free(u->sig);
642
643 if (u->memchunk.memblock)
644 pa_memblock_unref(u->memchunk.memblock);
645 if (u->silence.memblock)
646 pa_memblock_unref(u->silence.memblock);
647
648 if (u->sink) {
649 pa_sink_disconnect(u->sink);
650 pa_sink_unref(u->sink);
651 }
652
653 if (u->source) {
654 pa_source_disconnect(u->source);
655 pa_source_unref(u->source);
656 }
657
658 pa_iochannel_free(u->io);
659 pa_xfree(u);
660 }