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