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