]> code.delx.au - pulseaudio/blob - src/modules/module-solaris.c
remove all occurences of
[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_sink_used_by(u->sink) : 0) +
114 (u->source ? pa_source_used_by(u->source) : 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("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("write() failed: %s", pa_cstrerror(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("read() failed: %s", pa_cstrerror(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("AUDIO_SETINFO: Unsupported volume.");
336 else
337 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(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("AUDIO_SETINFO: %s", pa_cstrerror(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("AUDIO_SETINFO: Unsupported volume.");
399 else
400 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(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("AUDIO_SETINFO: Unsupported sample format.");
465 else
466 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(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("AUDIO_SETINFO: Unsupported buffer size.");
483 else
484 pa_log("AUDIO_SETINFO: %s", pa_cstrerror(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 char *t;
503 assert(c && m);
504
505 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
506 pa_log("failed to parse module arguments.");
507 goto fail;
508 }
509
510 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
511 pa_log("record= and playback= expect numeric argument.");
512 goto fail;
513 }
514
515 if (!playback && !record) {
516 pa_log("neither playback nor record enabled for device.");
517 goto fail;
518 }
519
520 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
521
522 buffer_size = 16384;
523 if (pa_modargs_get_value_s32(ma, "buffer_size", &buffer_size) < 0) {
524 pa_log("failed to parse buffer size argument");
525 goto fail;
526 }
527
528 ss = c->default_sample_spec;
529 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
530 pa_log("failed to parse sample specification");
531 goto fail;
532 }
533
534 if ((fd = open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), mode | O_NONBLOCK)) < 0)
535 goto fail;
536
537 pa_log_info("device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
538
539 if (pa_solaris_auto_format(fd, mode, &ss) < 0)
540 goto fail;
541
542 if ((mode != O_WRONLY) && (buffer_size >= 1))
543 if (pa_solaris_set_buffer(fd, buffer_size) < 0)
544 goto fail;
545
546 u = pa_xmalloc(sizeof(struct userdata));
547 u->core = c;
548
549 if (mode != O_WRONLY) {
550 u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
551 assert(u->source);
552 u->source->userdata = u;
553 u->source->get_latency = source_get_latency_cb;
554 u->source->get_hw_volume = source_get_hw_volume_cb;
555 u->source->set_hw_volume = source_set_hw_volume_cb;
556 pa_source_set_owner(u->source, m);
557 pa_source_set_description(u->source, t = pa_sprintf_malloc("Solaris PCM on '%s'", p));
558 pa_xfree(t);
559 u->source->is_hardware = 1;
560 } else
561 u->source = NULL;
562
563 if (mode != O_RDONLY) {
564 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
565 assert(u->sink);
566 u->sink->get_latency = sink_get_latency_cb;
567 u->sink->get_hw_volume = sink_get_hw_volume_cb;
568 u->sink->set_hw_volume = sink_set_hw_volume_cb;
569 u->sink->get_hw_mute = sink_get_hw_mute_cb;
570 u->sink->set_hw_mute = sink_set_hw_mute_cb;
571 u->sink->userdata = u;
572 pa_sink_set_owner(u->sink, m);
573 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Solaris PCM on '%s'", p));
574 pa_xfree(t);
575 u->sink->is_hardware = 1;
576 } else
577 u->sink = NULL;
578
579 assert(u->source || u->sink);
580
581 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
582 assert(u->io);
583 pa_iochannel_set_callback(u->io, io_callback, u);
584 u->fd = fd;
585
586 u->memchunk.memblock = NULL;
587 u->memchunk.length = 0;
588 u->frame_size = pa_frame_size(&ss);
589 u->buffer_size = buffer_size;
590
591 u->silence.memblock = pa_memblock_new(u->silence.length = CHUNK_SIZE, u->core->memblock_stat);
592 assert(u->silence.memblock);
593 pa_silence_memblock(u->silence.memblock, &ss);
594 u->silence.index = 0;
595
596 u->written_bytes = 0;
597 u->read_bytes = 0;
598
599 u->module = m;
600 m->userdata = u;
601
602 u->poll_timeout = pa_bytes_to_usec(u->buffer_size / 10, &ss);
603
604 pa_gettimeofday(&tv);
605 pa_timeval_add(&tv, u->poll_timeout);
606
607 u->timer = c->mainloop->time_new(c->mainloop, &tv, timer_cb, u);
608 assert(u->timer);
609
610 u->sig = pa_signal_new(SIGPOLL, sig_callback, u);
611 assert(u->sig);
612 ioctl(u->fd, I_SETSIG, S_MSG);
613
614 pa_modargs_free(ma);
615
616 /* Read mixer settings */
617 if (u->source)
618 source_get_hw_volume_cb(u->source);
619 if (u->sink) {
620 sink_get_hw_volume_cb(u->sink);
621 sink_get_hw_mute_cb(u->sink);
622 }
623
624 return 0;
625
626 fail:
627 if (fd >= 0)
628 close(fd);
629
630 if (ma)
631 pa_modargs_free(ma);
632
633 return -1;
634 }
635
636 void pa__done(pa_core *c, pa_module*m) {
637 struct userdata *u;
638 assert(c && m);
639
640 if (!(u = m->userdata))
641 return;
642
643 if (u->timer)
644 c->mainloop->time_free(u->timer);
645 ioctl(u->fd, I_SETSIG, 0);
646 pa_signal_free(u->sig);
647
648 if (u->memchunk.memblock)
649 pa_memblock_unref(u->memchunk.memblock);
650 if (u->silence.memblock)
651 pa_memblock_unref(u->silence.memblock);
652
653 if (u->sink) {
654 pa_sink_disconnect(u->sink);
655 pa_sink_unref(u->sink);
656 }
657
658 if (u->source) {
659 pa_source_disconnect(u->source);
660 pa_source_unref(u->source);
661 }
662
663 pa_iochannel_free(u->io);
664 pa_xfree(u);
665 }