~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/modules/module-oss.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2009-05-05 14:18:20 UTC
  • mfrom: (1.2.4 upstream) (1.1.8 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090505141820-rrr2mtdd1jkllvr8
Tags: 1:0.9.15-1ubuntu1
* Merge from unreleased Debian pulseaudio git, remaining changes:
  - epoch (my stupid fault :S)
  - Don't build against, and create jack package. Jack is not in main
  - use linear resampler to work better with lack of PREEMPT in jaunty's
    -generic kernel config, also change buffer size
  - Add alsa configuration files to route alsa applications via pulseaudio
  - Move libasound2-plugins from Recommends to Depends
  - Add pm-utils sleep hook to suspend (and resume) users' pulseaudio
    daemons
  - patch to fix source/sink and suspend-on-idle race
  - Make initscript more informative in the default case of per-user
    sessions
  - create /var/run/pulse, and make restart more robust
  - add status check for system wide pulseaudio instance
  - LSB {Required-*,Should-*} should specify hal instead of dbus,
    since hal is required (and already requires dbus)
  - indicate that the system pulseaudio instance is being started from the init
    script
  - Install more upstream man pages
  - Link to pacat for parec man page
  - check whether pulseaudio is running before preloading the padsp library
  - Add DEB_OPT_FLAG = -O3 as per recommendation from
    pulseaudio-discuss/2007-December/001017.html
  - cache /usr/share/sounds/ubuntu/stereo/ wav files on pulseaudio load
  - disable glitch free (use tsched=0)
  - Generate a PO template on build
  - add special case to disable pulseaudio loading if accessibility/speech
    is being used
  - the sd wrapper script should not load pulseaudio if pulseaudio is being
    used as a system service
  - add a pulseaudio apport hook
  - fix some typos in README.Debian
  - demote paprefs to suggests
  - drop padevchooser(Recommends) and pavucontrol (Suggests)
  - drop libasyncns-dev build dependency, its in universe
* add libudev-dev as a build-dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***
2
 
  This file is part of PulseAudio.
3
 
 
4
 
  Copyright 2004-2006 Lennart Poettering
5
 
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
 
 
7
 
  PulseAudio is free software; you can redistribute it and/or modify
8
 
  it under the terms of the GNU Lesser General Public License as published
9
 
  by the Free Software Foundation; either version 2 of the License,
10
 
  or (at your option) any later version.
11
 
 
12
 
  PulseAudio is distributed in the hope that it will be useful, but
13
 
  WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
  General Public License for more details.
16
 
 
17
 
  You should have received a copy of the GNU Lesser General Public License
18
 
  along with PulseAudio; if not, write to the Free Software
19
 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
 
  USA.
21
 
***/
22
 
 
23
 
/* General power management rules:
24
 
 *
25
 
 *   When SUSPENDED we close the audio device.
26
 
 *
27
 
 *   We make no difference between IDLE and RUNNING in our handling.
28
 
 *
29
 
 *   As long as we are in RUNNING/IDLE state we will *always* write data to
30
 
 *   the device. If none is avilable from the inputs, we write silence
31
 
 *   instead.
32
 
 *
33
 
 *   If power should be saved on IDLE module-suspend-on-idle should be used.
34
 
 *
35
 
 */
36
 
 
37
 
#ifdef HAVE_CONFIG_H
38
 
#include <config.h>
39
 
#endif
40
 
 
41
 
#ifdef HAVE_SYS_MMAN_H
42
 
#include <sys/mman.h>
43
 
#endif
44
 
 
45
 
#include <sys/soundcard.h>
46
 
#include <sys/ioctl.h>
47
 
#include <stdlib.h>
48
 
#include <sys/stat.h>
49
 
#include <stdio.h>
50
 
#include <errno.h>
51
 
#include <string.h>
52
 
#include <fcntl.h>
53
 
#include <unistd.h>
54
 
#include <limits.h>
55
 
#include <signal.h>
56
 
#include <poll.h>
57
 
 
58
 
#include <pulse/xmalloc.h>
59
 
#include <pulse/util.h>
60
 
 
61
 
#include <pulsecore/core-error.h>
62
 
#include <pulsecore/thread.h>
63
 
#include <pulsecore/sink.h>
64
 
#include <pulsecore/source.h>
65
 
#include <pulsecore/module.h>
66
 
#include <pulsecore/sample-util.h>
67
 
#include <pulsecore/core-util.h>
68
 
#include <pulsecore/modargs.h>
69
 
#include <pulsecore/log.h>
70
 
#include <pulsecore/macro.h>
71
 
#include <pulsecore/thread-mq.h>
72
 
#include <pulsecore/rtpoll.h>
73
 
 
74
 
#include "oss-util.h"
75
 
#include "module-oss-symdef.h"
76
 
 
77
 
PA_MODULE_AUTHOR("Lennart Poettering");
78
 
PA_MODULE_DESCRIPTION("OSS Sink/Source");
79
 
PA_MODULE_VERSION(PACKAGE_VERSION);
80
 
PA_MODULE_LOAD_ONCE(FALSE);
81
 
PA_MODULE_USAGE(
82
 
        "sink_name=<name for the sink> "
83
 
        "source_name=<name for the source> "
84
 
        "device=<OSS device> "
85
 
        "record=<enable source?> "
86
 
        "playback=<enable sink?> "
87
 
        "format=<sample format> "
88
 
        "channels=<number of channels> "
89
 
        "rate=<sample rate> "
90
 
        "fragments=<number of fragments> "
91
 
        "fragment_size=<fragment size> "
92
 
        "channel_map=<channel map> "
93
 
        "mmap=<enable memory mapping?>");
94
 
 
95
 
#define DEFAULT_DEVICE "/dev/dsp"
96
 
 
97
 
struct userdata {
98
 
    pa_core *core;
99
 
    pa_module *module;
100
 
    pa_sink *sink;
101
 
    pa_source *source;
102
 
 
103
 
    pa_thread *thread;
104
 
    pa_thread_mq thread_mq;
105
 
    pa_rtpoll *rtpoll;
106
 
 
107
 
    char *device_name;
108
 
 
109
 
    pa_memchunk memchunk;
110
 
 
111
 
    size_t frame_size;
112
 
    uint32_t in_fragment_size, out_fragment_size, in_nfrags, out_nfrags, in_hwbuf_size, out_hwbuf_size;
113
 
    pa_bool_t use_getospace, use_getispace;
114
 
    pa_bool_t use_getodelay;
115
 
 
116
 
    pa_bool_t sink_suspended, source_suspended;
117
 
 
118
 
    int fd;
119
 
    int mode;
120
 
 
121
 
    int mixer_fd;
122
 
    int mixer_devmask;
123
 
 
124
 
    int nfrags, frag_size;
125
 
 
126
 
    pa_bool_t use_mmap;
127
 
    unsigned out_mmap_current, in_mmap_current;
128
 
    void *in_mmap, *out_mmap;
129
 
    pa_memblock **in_mmap_memblocks, **out_mmap_memblocks;
130
 
 
131
 
    int in_mmap_saved_nfrags, out_mmap_saved_nfrags;
132
 
 
133
 
    pa_rtpoll_item *rtpoll_item;
134
 
};
135
 
 
136
 
static const char* const valid_modargs[] = {
137
 
    "sink_name",
138
 
    "source_name",
139
 
    "device",
140
 
    "record",
141
 
    "playback",
142
 
    "fragments",
143
 
    "fragment_size",
144
 
    "format",
145
 
    "rate",
146
 
    "channels",
147
 
    "channel_map",
148
 
    "mmap",
149
 
    NULL
150
 
};
151
 
 
152
 
static void trigger(struct userdata *u, pa_bool_t quick) {
153
 
    int enable_bits = 0, zero = 0;
154
 
 
155
 
    pa_assert(u);
156
 
 
157
 
    if (u->fd < 0)
158
 
        return;
159
 
 
160
 
     pa_log_debug("trigger");
161
 
 
162
 
    if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state))
163
 
        enable_bits |= PCM_ENABLE_INPUT;
164
 
 
165
 
    if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state))
166
 
        enable_bits |= PCM_ENABLE_OUTPUT;
167
 
 
168
 
    pa_log_debug("trigger: %i", enable_bits);
169
 
 
170
 
 
171
 
    if (u->use_mmap) {
172
 
 
173
 
        if (!quick)
174
 
            ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero);
175
 
 
176
 
#ifdef SNDCTL_DSP_HALT
177
 
        if (enable_bits == 0)
178
 
            if (ioctl(u->fd, SNDCTL_DSP_HALT, NULL) < 0)
179
 
                pa_log_warn("SNDCTL_DSP_HALT: %s", pa_cstrerror(errno));
180
 
#endif
181
 
 
182
 
        if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0)
183
 
            pa_log_warn("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
184
 
 
185
 
        if (u->sink && !(enable_bits & PCM_ENABLE_OUTPUT)) {
186
 
            pa_log_debug("clearing playback buffer");
187
 
            pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &u->sink->sample_spec);
188
 
        }
189
 
 
190
 
    } else {
191
 
 
192
 
        if (enable_bits)
193
 
            if (ioctl(u->fd, SNDCTL_DSP_POST, NULL) < 0)
194
 
                pa_log_warn("SNDCTL_DSP_POST: %s", pa_cstrerror(errno));
195
 
 
196
 
        if (!quick) {
197
 
            /*
198
 
             * Some crappy drivers do not start the recording until we
199
 
             * read something.  Without this snippet, poll will never
200
 
             * register the fd as ready.
201
 
             */
202
 
 
203
 
            if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
204
 
                uint8_t *buf = pa_xnew(uint8_t, u->in_fragment_size);
205
 
                pa_read(u->fd, buf, u->in_fragment_size, NULL);
206
 
                pa_xfree(buf);
207
 
            }
208
 
        }
209
 
    }
210
 
}
211
 
 
212
 
static void mmap_fill_memblocks(struct userdata *u, unsigned n) {
213
 
    pa_assert(u);
214
 
    pa_assert(u->out_mmap_memblocks);
215
 
 
216
 
/*     pa_log("Mmmap writing %u blocks", n); */
217
 
 
218
 
    while (n > 0) {
219
 
        pa_memchunk chunk;
220
 
 
221
 
        if (u->out_mmap_memblocks[u->out_mmap_current])
222
 
            pa_memblock_unref_fixed(u->out_mmap_memblocks[u->out_mmap_current]);
223
 
 
224
 
        chunk.memblock = u->out_mmap_memblocks[u->out_mmap_current] =
225
 
            pa_memblock_new_fixed(
226
 
                    u->core->mempool,
227
 
                    (uint8_t*) u->out_mmap + u->out_fragment_size * u->out_mmap_current,
228
 
                    u->out_fragment_size,
229
 
                    1);
230
 
 
231
 
        chunk.length = pa_memblock_get_length(chunk.memblock);
232
 
        chunk.index = 0;
233
 
 
234
 
        pa_sink_render_into_full(u->sink, &chunk);
235
 
 
236
 
        u->out_mmap_current++;
237
 
        while (u->out_mmap_current >= u->out_nfrags)
238
 
            u->out_mmap_current -= u->out_nfrags;
239
 
 
240
 
        n--;
241
 
    }
242
 
}
243
 
 
244
 
static int mmap_write(struct userdata *u) {
245
 
    struct count_info info;
246
 
 
247
 
    pa_assert(u);
248
 
    pa_assert(u->sink);
249
 
 
250
 
/*     pa_log("Mmmap writing..."); */
251
 
 
252
 
    if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
253
 
        pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
254
 
        return -1;
255
 
    }
256
 
 
257
 
    info.blocks += u->out_mmap_saved_nfrags;
258
 
    u->out_mmap_saved_nfrags = 0;
259
 
 
260
 
    if (info.blocks > 0)
261
 
        mmap_fill_memblocks(u, (unsigned) info.blocks);
262
 
 
263
 
    return info.blocks;
264
 
}
265
 
 
266
 
static void mmap_post_memblocks(struct userdata *u, unsigned n) {
267
 
    pa_assert(u);
268
 
    pa_assert(u->in_mmap_memblocks);
269
 
 
270
 
/*     pa_log("Mmmap reading %u blocks", n); */
271
 
 
272
 
    while (n > 0) {
273
 
        pa_memchunk chunk;
274
 
 
275
 
        if (!u->in_mmap_memblocks[u->in_mmap_current]) {
276
 
 
277
 
            chunk.memblock = u->in_mmap_memblocks[u->in_mmap_current] =
278
 
                pa_memblock_new_fixed(
279
 
                        u->core->mempool,
280
 
                        (uint8_t*) u->in_mmap + u->in_fragment_size*u->in_mmap_current,
281
 
                        u->in_fragment_size,
282
 
                        1);
283
 
 
284
 
            chunk.length = pa_memblock_get_length(chunk.memblock);
285
 
            chunk.index = 0;
286
 
 
287
 
            pa_source_post(u->source, &chunk);
288
 
        }
289
 
 
290
 
        u->in_mmap_current++;
291
 
        while (u->in_mmap_current >= u->in_nfrags)
292
 
            u->in_mmap_current -= u->in_nfrags;
293
 
 
294
 
        n--;
295
 
    }
296
 
}
297
 
 
298
 
static void mmap_clear_memblocks(struct userdata*u, unsigned n) {
299
 
    unsigned i = u->in_mmap_current;
300
 
 
301
 
    pa_assert(u);
302
 
    pa_assert(u->in_mmap_memblocks);
303
 
 
304
 
    if (n > u->in_nfrags)
305
 
        n = u->in_nfrags;
306
 
 
307
 
    while (n > 0) {
308
 
        if (u->in_mmap_memblocks[i]) {
309
 
            pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
310
 
            u->in_mmap_memblocks[i] = NULL;
311
 
        }
312
 
 
313
 
        i++;
314
 
        while (i >= u->in_nfrags)
315
 
            i -= u->in_nfrags;
316
 
 
317
 
        n--;
318
 
    }
319
 
}
320
 
 
321
 
static int mmap_read(struct userdata *u) {
322
 
    struct count_info info;
323
 
    pa_assert(u);
324
 
    pa_assert(u->source);
325
 
 
326
 
/*     pa_log("Mmmap reading..."); */
327
 
 
328
 
    if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
329
 
        pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
330
 
        return -1;
331
 
    }
332
 
 
333
 
/*     pa_log("... %i", info.blocks); */
334
 
 
335
 
    info.blocks += u->in_mmap_saved_nfrags;
336
 
    u->in_mmap_saved_nfrags = 0;
337
 
 
338
 
    if (info.blocks > 0) {
339
 
        mmap_post_memblocks(u, (unsigned) info.blocks);
340
 
        mmap_clear_memblocks(u, u->in_nfrags/2);
341
 
    }
342
 
 
343
 
    return info.blocks;
344
 
}
345
 
 
346
 
static pa_usec_t mmap_sink_get_latency(struct userdata *u) {
347
 
    struct count_info info;
348
 
    size_t bpos, n;
349
 
 
350
 
    pa_assert(u);
351
 
 
352
 
    if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
353
 
        pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
354
 
        return 0;
355
 
    }
356
 
 
357
 
    u->out_mmap_saved_nfrags += info.blocks;
358
 
 
359
 
    bpos = ((u->out_mmap_current + (unsigned) u->out_mmap_saved_nfrags) * u->out_fragment_size) % u->out_hwbuf_size;
360
 
 
361
 
    if (bpos <= (size_t) info.ptr)
362
 
        n = u->out_hwbuf_size - ((size_t) info.ptr - bpos);
363
 
    else
364
 
        n = bpos - (size_t) info.ptr;
365
 
 
366
 
/*     pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
367
 
 
368
 
    return pa_bytes_to_usec(n, &u->sink->sample_spec);
369
 
}
370
 
 
371
 
static pa_usec_t mmap_source_get_latency(struct userdata *u) {
372
 
    struct count_info info;
373
 
    size_t bpos, n;
374
 
 
375
 
    pa_assert(u);
376
 
 
377
 
    if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
378
 
        pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
379
 
        return 0;
380
 
    }
381
 
 
382
 
    u->in_mmap_saved_nfrags += info.blocks;
383
 
    bpos = ((u->in_mmap_current + (unsigned) u->in_mmap_saved_nfrags) * u->in_fragment_size) % u->in_hwbuf_size;
384
 
 
385
 
    if (bpos <= (size_t) info.ptr)
386
 
        n = (size_t) info.ptr - bpos;
387
 
    else
388
 
        n = u->in_hwbuf_size - bpos + (size_t) info.ptr;
389
 
 
390
 
/*     pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments);  */
391
 
 
392
 
    return pa_bytes_to_usec(n, &u->source->sample_spec);
393
 
}
394
 
 
395
 
static pa_usec_t io_sink_get_latency(struct userdata *u) {
396
 
    pa_usec_t r = 0;
397
 
 
398
 
    pa_assert(u);
399
 
 
400
 
    if (u->use_getodelay) {
401
 
        int arg;
402
 
 
403
 
        if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
404
 
            pa_log_info("Device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
405
 
            u->use_getodelay = 0;
406
 
        } else
407
 
            r = pa_bytes_to_usec((size_t) arg, &u->sink->sample_spec);
408
 
 
409
 
    }
410
 
 
411
 
    if (!u->use_getodelay && u->use_getospace) {
412
 
        struct audio_buf_info info;
413
 
 
414
 
        if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
415
 
            pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
416
 
            u->use_getospace = 0;
417
 
        } else
418
 
            r = pa_bytes_to_usec((size_t) info.bytes, &u->sink->sample_spec);
419
 
    }
420
 
 
421
 
    if (u->memchunk.memblock)
422
 
        r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
423
 
 
424
 
    return r;
425
 
}
426
 
 
427
 
 
428
 
static pa_usec_t io_source_get_latency(struct userdata *u) {
429
 
    pa_usec_t r = 0;
430
 
 
431
 
    pa_assert(u);
432
 
 
433
 
    if (u->use_getispace) {
434
 
        struct audio_buf_info info;
435
 
 
436
 
        if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
437
 
            pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
438
 
            u->use_getispace = 0;
439
 
        } else
440
 
            r = pa_bytes_to_usec((size_t) info.bytes, &u->source->sample_spec);
441
 
    }
442
 
 
443
 
    return r;
444
 
}
445
 
 
446
 
static void build_pollfd(struct userdata *u) {
447
 
    struct pollfd *pollfd;
448
 
 
449
 
    pa_assert(u);
450
 
    pa_assert(u->fd >= 0);
451
 
 
452
 
    if (u->rtpoll_item)
453
 
        pa_rtpoll_item_free(u->rtpoll_item);
454
 
 
455
 
    u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
456
 
    pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
457
 
    pollfd->fd = u->fd;
458
 
    pollfd->events = 0;
459
 
    pollfd->revents = 0;
460
 
}
461
 
 
462
 
static int suspend(struct userdata *u) {
463
 
    pa_assert(u);
464
 
    pa_assert(u->fd >= 0);
465
 
 
466
 
    pa_log_info("Suspending...");
467
 
 
468
 
    if (u->out_mmap_memblocks) {
469
 
        unsigned i;
470
 
        for (i = 0; i < u->out_nfrags; i++)
471
 
            if (u->out_mmap_memblocks[i]) {
472
 
                pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
473
 
                u->out_mmap_memblocks[i] = NULL;
474
 
            }
475
 
    }
476
 
 
477
 
    if (u->in_mmap_memblocks) {
478
 
        unsigned i;
479
 
        for (i = 0; i < u->in_nfrags; i++)
480
 
            if (u->in_mmap_memblocks[i]) {
481
 
                pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
482
 
                u->in_mmap_memblocks[i] = NULL;
483
 
            }
484
 
    }
485
 
 
486
 
    if (u->in_mmap && u->in_mmap != MAP_FAILED) {
487
 
        munmap(u->in_mmap, u->in_hwbuf_size);
488
 
        u->in_mmap = NULL;
489
 
    }
490
 
 
491
 
    if (u->out_mmap && u->out_mmap != MAP_FAILED) {
492
 
        munmap(u->out_mmap, u->out_hwbuf_size);
493
 
        u->out_mmap = NULL;
494
 
    }
495
 
 
496
 
    /* Let's suspend */
497
 
    ioctl(u->fd, SNDCTL_DSP_SYNC, NULL);
498
 
    pa_close(u->fd);
499
 
    u->fd = -1;
500
 
 
501
 
    if (u->rtpoll_item) {
502
 
        pa_rtpoll_item_free(u->rtpoll_item);
503
 
        u->rtpoll_item = NULL;
504
 
    }
505
 
 
506
 
    pa_log_info("Device suspended...");
507
 
 
508
 
    return 0;
509
 
}
510
 
 
511
 
static int sink_get_volume(pa_sink *s);
512
 
static int source_get_volume(pa_source *s);
513
 
 
514
 
static int unsuspend(struct userdata *u) {
515
 
    int m;
516
 
    pa_sample_spec ss, *ss_original;
517
 
    int frag_size, in_frag_size, out_frag_size;
518
 
    int in_nfrags, out_nfrags;
519
 
    struct audio_buf_info info;
520
 
 
521
 
    pa_assert(u);
522
 
    pa_assert(u->fd < 0);
523
 
 
524
 
    m = u->mode;
525
 
 
526
 
    pa_log_info("Trying resume...");
527
 
 
528
 
    if ((u->fd = pa_oss_open(u->device_name, &m, NULL)) < 0) {
529
 
        pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno));
530
 
        return -1;
531
 
    }
532
 
 
533
 
    if (m != u->mode) {
534
 
        pa_log_warn("Resume failed, couldn't open device with original access mode.");
535
 
        goto fail;
536
 
    }
537
 
 
538
 
    if (u->nfrags >= 2 && u->frag_size >= 1)
539
 
        if (pa_oss_set_fragments(u->fd, u->nfrags, u->frag_size) < 0) {
540
 
            pa_log_warn("Resume failed, couldn't set original fragment settings.");
541
 
            goto fail;
542
 
        }
543
 
 
544
 
    ss = *(ss_original = u->sink ? &u->sink->sample_spec : &u->source->sample_spec);
545
 
    if (pa_oss_auto_format(u->fd, &ss) < 0 || !pa_sample_spec_equal(&ss, ss_original)) {
546
 
        pa_log_warn("Resume failed, couldn't set original sample format settings.");
547
 
        goto fail;
548
 
    }
549
 
 
550
 
    if (ioctl(u->fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
551
 
        pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
552
 
        goto fail;
553
 
    }
554
 
 
555
 
    in_frag_size = out_frag_size = frag_size;
556
 
    in_nfrags = out_nfrags = u->nfrags;
557
 
 
558
 
    if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
559
 
        in_frag_size = info.fragsize;
560
 
        in_nfrags = info.fragstotal;
561
 
    }
562
 
 
563
 
    if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
564
 
        out_frag_size = info.fragsize;
565
 
        out_nfrags = info.fragstotal;
566
 
    }
567
 
 
568
 
    if ((u->source && (in_frag_size != (int) u->in_fragment_size || in_nfrags != (int) u->in_nfrags)) ||
569
 
        (u->sink && (out_frag_size != (int) u->out_fragment_size || out_nfrags != (int) u->out_nfrags))) {
570
 
        pa_log_warn("Resume failed, input fragment settings don't match.");
571
 
        goto fail;
572
 
    }
573
 
 
574
 
    if (u->use_mmap) {
575
 
        if (u->source) {
576
 
            if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
577
 
                pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
578
 
                goto fail;
579
 
            }
580
 
        }
581
 
 
582
 
        if (u->sink) {
583
 
            if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
584
 
                pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
585
 
                if (u->in_mmap && u->in_mmap != MAP_FAILED) {
586
 
                    munmap(u->in_mmap, u->in_hwbuf_size);
587
 
                    u->in_mmap = NULL;
588
 
                }
589
 
 
590
 
                goto fail;
591
 
            }
592
 
 
593
 
            pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
594
 
        }
595
 
    }
596
 
 
597
 
    u->out_mmap_current = u->in_mmap_current = 0;
598
 
    u->out_mmap_saved_nfrags = u->in_mmap_saved_nfrags = 0;
599
 
 
600
 
    pa_assert(!u->rtpoll_item);
601
 
 
602
 
    build_pollfd(u);
603
 
 
604
 
    if (u->sink)
605
 
        sink_get_volume(u->sink);
606
 
    if (u->source)
607
 
        source_get_volume(u->source);
608
 
 
609
 
    pa_log_info("Resumed successfully...");
610
 
 
611
 
    return 0;
612
 
 
613
 
fail:
614
 
    pa_close(u->fd);
615
 
    u->fd = -1;
616
 
    return -1;
617
 
}
618
 
 
619
 
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
620
 
    struct userdata *u = PA_SINK(o)->userdata;
621
 
    int ret;
622
 
    pa_bool_t do_trigger = FALSE, quick = TRUE;
623
 
 
624
 
    switch (code) {
625
 
 
626
 
        case PA_SINK_MESSAGE_GET_LATENCY: {
627
 
            pa_usec_t r = 0;
628
 
 
629
 
            if (u->fd >= 0) {
630
 
                if (u->use_mmap)
631
 
                    r = mmap_sink_get_latency(u);
632
 
                else
633
 
                    r = io_sink_get_latency(u);
634
 
            }
635
 
 
636
 
            *((pa_usec_t*) data) = r;
637
 
 
638
 
            return 0;
639
 
        }
640
 
 
641
 
        case PA_SINK_MESSAGE_SET_STATE:
642
 
 
643
 
            switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
644
 
 
645
 
                case PA_SINK_SUSPENDED:
646
 
                    pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
647
 
 
648
 
                    if (!u->source || u->source_suspended) {
649
 
                        if (suspend(u) < 0)
650
 
                            return -1;
651
 
                    }
652
 
 
653
 
                    do_trigger = TRUE;
654
 
 
655
 
                    u->sink_suspended = TRUE;
656
 
                    break;
657
 
 
658
 
                case PA_SINK_IDLE:
659
 
                case PA_SINK_RUNNING:
660
 
 
661
 
                    if (u->sink->thread_info.state == PA_SINK_INIT) {
662
 
                        do_trigger = TRUE;
663
 
                        quick = u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state);
664
 
                    }
665
 
 
666
 
                    if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
667
 
 
668
 
                        if (!u->source || u->source_suspended) {
669
 
                            if (unsuspend(u) < 0)
670
 
                                return -1;
671
 
                            quick = FALSE;
672
 
                        }
673
 
 
674
 
                        do_trigger = TRUE;
675
 
 
676
 
                        u->out_mmap_current = 0;
677
 
                        u->out_mmap_saved_nfrags = 0;
678
 
 
679
 
                        u->sink_suspended = FALSE;
680
 
                    }
681
 
 
682
 
                    break;
683
 
 
684
 
                case PA_SINK_UNLINKED:
685
 
                case PA_SINK_INIT:
686
 
                    ;
687
 
            }
688
 
 
689
 
            break;
690
 
 
691
 
    }
692
 
 
693
 
    ret = pa_sink_process_msg(o, code, data, offset, chunk);
694
 
 
695
 
    if (do_trigger)
696
 
        trigger(u, quick);
697
 
 
698
 
    return ret;
699
 
}
700
 
 
701
 
static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
702
 
    struct userdata *u = PA_SOURCE(o)->userdata;
703
 
    int ret;
704
 
    int do_trigger = FALSE, quick = TRUE;
705
 
 
706
 
    switch (code) {
707
 
 
708
 
        case PA_SOURCE_MESSAGE_GET_LATENCY: {
709
 
            pa_usec_t r = 0;
710
 
 
711
 
            if (u->fd >= 0) {
712
 
                if (u->use_mmap)
713
 
                    r = mmap_source_get_latency(u);
714
 
                else
715
 
                    r = io_source_get_latency(u);
716
 
            }
717
 
 
718
 
            *((pa_usec_t*) data) = r;
719
 
            return 0;
720
 
        }
721
 
 
722
 
        case PA_SOURCE_MESSAGE_SET_STATE:
723
 
 
724
 
            switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
725
 
                case PA_SOURCE_SUSPENDED:
726
 
                    pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
727
 
 
728
 
                    if (!u->sink || u->sink_suspended) {
729
 
                        if (suspend(u) < 0)
730
 
                            return -1;
731
 
                    }
732
 
 
733
 
                    do_trigger = TRUE;
734
 
 
735
 
                    u->source_suspended = TRUE;
736
 
                    break;
737
 
 
738
 
                case PA_SOURCE_IDLE:
739
 
                case PA_SOURCE_RUNNING:
740
 
 
741
 
                    if (u->source->thread_info.state == PA_SOURCE_INIT) {
742
 
                        do_trigger = TRUE;
743
 
                        quick = u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state);
744
 
                    }
745
 
 
746
 
                    if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
747
 
 
748
 
                        if (!u->sink || u->sink_suspended) {
749
 
                            if (unsuspend(u) < 0)
750
 
                                return -1;
751
 
                            quick = FALSE;
752
 
                        }
753
 
 
754
 
                        do_trigger = TRUE;
755
 
 
756
 
                        u->in_mmap_current = 0;
757
 
                        u->in_mmap_saved_nfrags = 0;
758
 
 
759
 
                        u->source_suspended = FALSE;
760
 
                    }
761
 
                    break;
762
 
 
763
 
                case PA_SOURCE_UNLINKED:
764
 
                case PA_SOURCE_INIT:
765
 
                    ;
766
 
 
767
 
            }
768
 
            break;
769
 
 
770
 
    }
771
 
 
772
 
    ret = pa_source_process_msg(o, code, data, offset, chunk);
773
 
 
774
 
    if (do_trigger)
775
 
        trigger(u, quick);
776
 
 
777
 
    return ret;
778
 
}
779
 
 
780
 
static int sink_get_volume(pa_sink *s) {
781
 
    struct userdata *u;
782
 
    int r;
783
 
 
784
 
    pa_assert_se(u = s->userdata);
785
 
 
786
 
    pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
787
 
 
788
 
    if (u->mixer_devmask & SOUND_MASK_VOLUME)
789
 
        if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_VOLUME, &s->sample_spec, &s->volume)) >= 0)
790
 
            return r;
791
 
 
792
 
    if (u->mixer_devmask & SOUND_MASK_PCM)
793
 
        if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_PCM, &s->sample_spec, &s->volume)) >= 0)
794
 
            return r;
795
 
 
796
 
    pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
797
 
    return -1;
798
 
}
799
 
 
800
 
static int sink_set_volume(pa_sink *s) {
801
 
    struct userdata *u;
802
 
    int r;
803
 
 
804
 
    pa_assert_se(u = s->userdata);
805
 
 
806
 
    pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
807
 
 
808
 
    if (u->mixer_devmask & SOUND_MASK_VOLUME)
809
 
        if ((r = pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_VOLUME, &s->sample_spec, &s->volume)) >= 0)
810
 
            return r;
811
 
 
812
 
    if (u->mixer_devmask & SOUND_MASK_PCM)
813
 
        if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_PCM, &s->sample_spec, &s->volume)) >= 0)
814
 
            return r;
815
 
 
816
 
    pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
817
 
    return -1;
818
 
}
819
 
 
820
 
static int source_get_volume(pa_source *s) {
821
 
    struct userdata *u;
822
 
    int r;
823
 
 
824
 
    pa_assert_se(u = s->userdata);
825
 
 
826
 
    pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
827
 
 
828
 
    if (u->mixer_devmask & SOUND_MASK_IGAIN)
829
 
        if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->volume)) >= 0)
830
 
            return r;
831
 
 
832
 
    if (u->mixer_devmask & SOUND_MASK_RECLEV)
833
 
        if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->volume)) >= 0)
834
 
            return r;
835
 
 
836
 
    pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
837
 
    return -1;
838
 
}
839
 
 
840
 
static int source_set_volume(pa_source *s) {
841
 
    struct userdata *u;
842
 
    int r;
843
 
 
844
 
    pa_assert_se(u = s->userdata);
845
 
 
846
 
    pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
847
 
 
848
 
    if (u->mixer_devmask & SOUND_MASK_IGAIN)
849
 
        if ((r = pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->volume)) >= 0)
850
 
            return r;
851
 
 
852
 
    if (u->mixer_devmask & SOUND_MASK_RECLEV)
853
 
        if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->volume)) >= 0)
854
 
            return r;
855
 
 
856
 
    pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
857
 
    return -1;
858
 
}
859
 
 
860
 
static void thread_func(void *userdata) {
861
 
    struct userdata *u = userdata;
862
 
    int write_type = 0, read_type = 0;
863
 
    short revents = 0;
864
 
 
865
 
    pa_assert(u);
866
 
 
867
 
    pa_log_debug("Thread starting up");
868
 
 
869
 
    if (u->core->realtime_scheduling)
870
 
        pa_make_realtime(u->core->realtime_priority);
871
 
 
872
 
    pa_thread_mq_install(&u->thread_mq);
873
 
    pa_rtpoll_install(u->rtpoll);
874
 
 
875
 
    for (;;) {
876
 
        int ret;
877
 
 
878
 
/*        pa_log("loop");    */
879
 
 
880
 
        if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
881
 
            if (u->sink->thread_info.rewind_requested)
882
 
                pa_sink_process_rewind(u->sink, 0);
883
 
 
884
 
        /* Render some data and write it to the dsp */
885
 
 
886
 
        if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && ((revents & POLLOUT) || u->use_mmap || u->use_getospace)) {
887
 
 
888
 
            if (u->use_mmap) {
889
 
 
890
 
                if ((ret = mmap_write(u)) < 0)
891
 
                    goto fail;
892
 
 
893
 
                revents &= ~POLLOUT;
894
 
 
895
 
                if (ret > 0)
896
 
                    continue;
897
 
 
898
 
            } else {
899
 
                ssize_t l;
900
 
                pa_bool_t loop = FALSE, work_done = FALSE;
901
 
 
902
 
                l = (ssize_t) u->out_fragment_size;
903
 
 
904
 
                if (u->use_getospace) {
905
 
                    audio_buf_info info;
906
 
 
907
 
                    if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
908
 
                        pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
909
 
                        u->use_getospace = FALSE;
910
 
                    } else {
911
 
                        l = info.bytes;
912
 
 
913
 
                        /* We loop only if GETOSPACE worked and we
914
 
                         * actually *know* that we can write more than
915
 
                         * one fragment at a time */
916
 
                        loop = TRUE;
917
 
                    }
918
 
                }
919
 
 
920
 
                /* Round down to multiples of the fragment size,
921
 
                 * because OSS needs that (at least some versions
922
 
                 * do) */
923
 
                l = (l/(ssize_t) u->out_fragment_size) * (ssize_t) u->out_fragment_size;
924
 
 
925
 
                /* Hmm, so poll() signalled us that we can read
926
 
                 * something, but GETOSPACE told us there was nothing?
927
 
                 * Hmm, make the best of it, try to read some data, to
928
 
                 * avoid spinning forever. */
929
 
                if (l <= 0 && (revents & POLLOUT)) {
930
 
                    l = (ssize_t) u->out_fragment_size;
931
 
                    loop = FALSE;
932
 
                }
933
 
 
934
 
                while (l > 0) {
935
 
                    void *p;
936
 
                    ssize_t t;
937
 
 
938
 
                    if (u->memchunk.length <= 0)
939
 
                        pa_sink_render(u->sink, (size_t) l, &u->memchunk);
940
 
 
941
 
                    pa_assert(u->memchunk.length > 0);
942
 
 
943
 
                    p = pa_memblock_acquire(u->memchunk.memblock);
944
 
                    t = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
945
 
                    pa_memblock_release(u->memchunk.memblock);
946
 
 
947
 
/*                     pa_log("wrote %i bytes of %u", t, l); */
948
 
 
949
 
                    pa_assert(t != 0);
950
 
 
951
 
                    if (t < 0) {
952
 
 
953
 
                        if (errno == EINTR)
954
 
                            continue;
955
 
 
956
 
                        else if (errno == EAGAIN) {
957
 
                            pa_log_debug("EAGAIN");
958
 
 
959
 
                            revents &= ~POLLOUT;
960
 
                            break;
961
 
 
962
 
                        } else {
963
 
                            pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno));
964
 
                            goto fail;
965
 
                        }
966
 
 
967
 
                    } else {
968
 
 
969
 
                        u->memchunk.index += (size_t) t;
970
 
                        u->memchunk.length -= (size_t) t;
971
 
 
972
 
                        if (u->memchunk.length <= 0) {
973
 
                            pa_memblock_unref(u->memchunk.memblock);
974
 
                            pa_memchunk_reset(&u->memchunk);
975
 
                        }
976
 
 
977
 
                        l -= t;
978
 
 
979
 
                        revents &= ~POLLOUT;
980
 
                        work_done = TRUE;
981
 
                    }
982
 
 
983
 
                    if (!loop)
984
 
                        break;
985
 
                }
986
 
 
987
 
                if (work_done)
988
 
                    continue;
989
 
            }
990
 
        }
991
 
 
992
 
        /* Try to read some data and pass it on to the source driver. */
993
 
 
994
 
        if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) && ((revents & POLLIN) || u->use_mmap || u->use_getispace)) {
995
 
 
996
 
            if (u->use_mmap) {
997
 
 
998
 
                if ((ret = mmap_read(u)) < 0)
999
 
                    goto fail;
1000
 
 
1001
 
                revents &= ~POLLIN;
1002
 
 
1003
 
                if (ret > 0)
1004
 
                    continue;
1005
 
 
1006
 
            } else {
1007
 
 
1008
 
                void *p;
1009
 
                ssize_t l;
1010
 
                pa_memchunk memchunk;
1011
 
                pa_bool_t loop = FALSE, work_done = FALSE;
1012
 
 
1013
 
                l = (ssize_t) u->in_fragment_size;
1014
 
 
1015
 
                if (u->use_getispace) {
1016
 
                    audio_buf_info info;
1017
 
 
1018
 
                    if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
1019
 
                        pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
1020
 
                        u->use_getispace = FALSE;
1021
 
                    } else {
1022
 
                        l = info.bytes;
1023
 
                        loop = TRUE;
1024
 
                    }
1025
 
                }
1026
 
 
1027
 
                l = (l/(ssize_t) u->in_fragment_size) * (ssize_t) u->in_fragment_size;
1028
 
 
1029
 
                if (l <= 0 && (revents & POLLIN)) {
1030
 
                    l = (ssize_t) u->in_fragment_size;
1031
 
                    loop = FALSE;
1032
 
                }
1033
 
 
1034
 
                while (l > 0) {
1035
 
                    ssize_t t;
1036
 
                    size_t k;
1037
 
 
1038
 
                    pa_assert(l > 0);
1039
 
 
1040
 
                    memchunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
1041
 
 
1042
 
                    k = pa_memblock_get_length(memchunk.memblock);
1043
 
 
1044
 
                    if (k > (size_t) l)
1045
 
                        k = (size_t) l;
1046
 
 
1047
 
                    k = (k/u->frame_size)*u->frame_size;
1048
 
 
1049
 
                    p = pa_memblock_acquire(memchunk.memblock);
1050
 
                    t = pa_read(u->fd, p, k, &read_type);
1051
 
                    pa_memblock_release(memchunk.memblock);
1052
 
 
1053
 
                    pa_assert(t != 0); /* EOF cannot happen */
1054
 
 
1055
 
/*                     pa_log("read %i bytes of %u", t, l); */
1056
 
 
1057
 
                    if (t < 0) {
1058
 
                        pa_memblock_unref(memchunk.memblock);
1059
 
 
1060
 
                        if (errno == EINTR)
1061
 
                            continue;
1062
 
 
1063
 
                        else if (errno == EAGAIN) {
1064
 
                            pa_log_debug("EAGAIN");
1065
 
 
1066
 
                            revents &= ~POLLIN;
1067
 
                            break;
1068
 
 
1069
 
                        } else {
1070
 
                            pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
1071
 
                            goto fail;
1072
 
                        }
1073
 
 
1074
 
                    } else {
1075
 
                        memchunk.index = 0;
1076
 
                        memchunk.length = (size_t) t;
1077
 
 
1078
 
                        pa_source_post(u->source, &memchunk);
1079
 
                        pa_memblock_unref(memchunk.memblock);
1080
 
 
1081
 
                        l -= t;
1082
 
 
1083
 
                        revents &= ~POLLIN;
1084
 
                        work_done = TRUE;
1085
 
                    }
1086
 
 
1087
 
                    if (!loop)
1088
 
                        break;
1089
 
                }
1090
 
 
1091
 
                if (work_done)
1092
 
                    continue;
1093
 
            }
1094
 
        }
1095
 
 
1096
 
/*         pa_log("loop2 revents=%i", revents); */
1097
 
 
1098
 
        if (u->rtpoll_item) {
1099
 
            struct pollfd *pollfd;
1100
 
 
1101
 
            pa_assert(u->fd >= 0);
1102
 
 
1103
 
            pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1104
 
            pollfd->events = (short)
1105
 
                (((u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) ? POLLIN : 0) |
1106
 
                 ((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state)) ? POLLOUT : 0));
1107
 
        }
1108
 
 
1109
 
        /* Hmm, nothing to do. Let's sleep */
1110
 
        if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
1111
 
            goto fail;
1112
 
 
1113
 
        if (ret == 0)
1114
 
            goto finish;
1115
 
 
1116
 
        if (u->rtpoll_item) {
1117
 
            struct pollfd *pollfd;
1118
 
 
1119
 
            pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1120
 
 
1121
 
            if (pollfd->revents & ~(POLLOUT|POLLIN)) {
1122
 
                pa_log("DSP shutdown.");
1123
 
                goto fail;
1124
 
            }
1125
 
 
1126
 
            revents = pollfd->revents;
1127
 
        } else
1128
 
            revents = 0;
1129
 
    }
1130
 
 
1131
 
fail:
1132
 
    /* If this was no regular exit from the loop we have to continue
1133
 
     * processing messages until we received PA_MESSAGE_SHUTDOWN */
1134
 
    pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
1135
 
    pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
1136
 
 
1137
 
finish:
1138
 
    pa_log_debug("Thread shutting down");
1139
 
}
1140
 
 
1141
 
int pa__init(pa_module*m) {
1142
 
 
1143
 
    struct audio_buf_info info;
1144
 
    struct userdata *u = NULL;
1145
 
    const char *dev;
1146
 
    int fd = -1;
1147
 
    int nfrags, frag_size;
1148
 
    int mode, caps;
1149
 
    pa_bool_t record = TRUE, playback = TRUE, use_mmap = TRUE;
1150
 
    pa_sample_spec ss;
1151
 
    pa_channel_map map;
1152
 
    pa_modargs *ma = NULL;
1153
 
    char hwdesc[64];
1154
 
    const char *name;
1155
 
    pa_bool_t namereg_fail;
1156
 
    pa_sink_new_data sink_new_data;
1157
 
    pa_source_new_data source_new_data;
1158
 
 
1159
 
    pa_assert(m);
1160
 
 
1161
 
    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1162
 
        pa_log("Failed to parse module arguments.");
1163
 
        goto fail;
1164
 
    }
1165
 
 
1166
 
    if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
1167
 
        pa_log("record= and playback= expect boolean argument.");
1168
 
        goto fail;
1169
 
    }
1170
 
 
1171
 
    if (!playback && !record) {
1172
 
        pa_log("Neither playback nor record enabled for device.");
1173
 
        goto fail;
1174
 
    }
1175
 
 
1176
 
    mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
1177
 
 
1178
 
    ss = m->core->default_sample_spec;
1179
 
    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
1180
 
        pa_log("Failed to parse sample specification or channel map");
1181
 
        goto fail;
1182
 
    }
1183
 
 
1184
 
    nfrags = (int) m->core->default_n_fragments;
1185
 
    frag_size = (int) pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
1186
 
    if (frag_size <= 0)
1187
 
        frag_size = (int) pa_frame_size(&ss);
1188
 
 
1189
 
    if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
1190
 
        pa_log("Failed to parse fragments arguments");
1191
 
        goto fail;
1192
 
    }
1193
 
 
1194
 
    if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1195
 
        pa_log("Failed to parse mmap argument.");
1196
 
        goto fail;
1197
 
    }
1198
 
 
1199
 
    if ((fd = pa_oss_open(dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
1200
 
        goto fail;
1201
 
 
1202
 
    if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) {
1203
 
        pa_log_info("OSS device not mmap capable, falling back to UNIX read/write mode.");
1204
 
        use_mmap = 0;
1205
 
    }
1206
 
 
1207
 
    if (use_mmap && mode == O_WRONLY) {
1208
 
        pa_log_info("Device opened for playback only, cannot do memory mapping, falling back to UNIX write() mode.");
1209
 
        use_mmap = 0;
1210
 
    }
1211
 
 
1212
 
    if (pa_oss_get_hw_description(dev, hwdesc, sizeof(hwdesc)) >= 0)
1213
 
        pa_log_info("Hardware name is '%s'.", hwdesc);
1214
 
    else
1215
 
        hwdesc[0] = 0;
1216
 
 
1217
 
    pa_log_info("Device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
1218
 
 
1219
 
    if (nfrags >= 2 && frag_size >= 1)
1220
 
        if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
1221
 
            goto fail;
1222
 
 
1223
 
    if (pa_oss_auto_format(fd, &ss) < 0)
1224
 
        goto fail;
1225
 
 
1226
 
    if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
1227
 
        pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
1228
 
        goto fail;
1229
 
    }
1230
 
    pa_assert(frag_size > 0);
1231
 
 
1232
 
    u = pa_xnew0(struct userdata, 1);
1233
 
    u->core = m->core;
1234
 
    u->module = m;
1235
 
    m->userdata = u;
1236
 
    u->fd = fd;
1237
 
    u->mixer_fd = -1;
1238
 
    u->use_getospace = u->use_getispace = TRUE;
1239
 
    u->use_getodelay = TRUE;
1240
 
    u->mode = mode;
1241
 
    u->frame_size = pa_frame_size(&ss);
1242
 
    u->device_name = pa_xstrdup(dev);
1243
 
    u->in_nfrags = u->out_nfrags = (uint32_t) (u->nfrags = nfrags);
1244
 
    u->out_fragment_size = u->in_fragment_size = (uint32_t) (u->frag_size = frag_size);
1245
 
    u->use_mmap = use_mmap;
1246
 
    u->rtpoll = pa_rtpoll_new();
1247
 
    pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1248
 
    u->rtpoll_item = NULL;
1249
 
    build_pollfd(u);
1250
 
 
1251
 
    if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
1252
 
        pa_log_info("Input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
1253
 
        u->in_fragment_size = (uint32_t) info.fragsize;
1254
 
        u->in_nfrags = (uint32_t) info.fragstotal;
1255
 
        u->use_getispace = TRUE;
1256
 
    }
1257
 
 
1258
 
    if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
1259
 
        pa_log_info("Output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
1260
 
        u->out_fragment_size = (uint32_t) info.fragsize;
1261
 
        u->out_nfrags = (uint32_t) info.fragstotal;
1262
 
        u->use_getospace = TRUE;
1263
 
    }
1264
 
 
1265
 
    u->in_hwbuf_size = u->in_nfrags * u->in_fragment_size;
1266
 
    u->out_hwbuf_size = u->out_nfrags * u->out_fragment_size;
1267
 
 
1268
 
    if (mode != O_WRONLY) {
1269
 
        char *name_buf = NULL;
1270
 
 
1271
 
        if (use_mmap) {
1272
 
            if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1273
 
                pa_log_warn("mmap(PROT_READ) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1274
 
                use_mmap = u->use_mmap = FALSE;
1275
 
                u->in_mmap = NULL;
1276
 
            } else
1277
 
                pa_log_debug("Successfully mmap()ed input buffer.");
1278
 
        }
1279
 
 
1280
 
        if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
1281
 
            namereg_fail = TRUE;
1282
 
        else {
1283
 
            name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
1284
 
            namereg_fail = FALSE;
1285
 
        }
1286
 
 
1287
 
        pa_source_new_data_init(&source_new_data);
1288
 
        source_new_data.driver = __FILE__;
1289
 
        source_new_data.module = m;
1290
 
        pa_source_new_data_set_name(&source_new_data, name);
1291
 
        source_new_data.namereg_fail = namereg_fail;
1292
 
        pa_source_new_data_set_sample_spec(&source_new_data, &ss);
1293
 
        pa_source_new_data_set_channel_map(&source_new_data, &map);
1294
 
        pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_STRING, dev);
1295
 
        pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_API, "oss");
1296
 
        pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
1297
 
        pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
1298
 
        pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->in_hwbuf_size));
1299
 
        pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->in_fragment_size));
1300
 
 
1301
 
        u->source = pa_source_new(m->core, &source_new_data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
1302
 
        pa_source_new_data_done(&source_new_data);
1303
 
        pa_xfree(name_buf);
1304
 
 
1305
 
        if (!u->source) {
1306
 
            pa_log("Failed to create source object");
1307
 
            goto fail;
1308
 
        }
1309
 
 
1310
 
        u->source->parent.process_msg = source_process_msg;
1311
 
        u->source->userdata = u;
1312
 
 
1313
 
        pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1314
 
        pa_source_set_rtpoll(u->source, u->rtpoll);
1315
 
        u->source->refresh_volume = TRUE;
1316
 
 
1317
 
        if (use_mmap)
1318
 
            u->in_mmap_memblocks = pa_xnew0(pa_memblock*, u->in_nfrags);
1319
 
    }
1320
 
 
1321
 
    if (mode != O_RDONLY) {
1322
 
        char *name_buf = NULL;
1323
 
 
1324
 
        if (use_mmap) {
1325
 
            if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1326
 
                if (mode == O_RDWR) {
1327
 
                    pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1328
 
                    mode = O_WRONLY;
1329
 
                    goto go_on;
1330
 
                } else {
1331
 
                    pa_log_warn("mmap(PROT_WRITE) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1332
 
                    u->use_mmap = use_mmap = FALSE;
1333
 
                    u->out_mmap = NULL;
1334
 
                }
1335
 
            } else {
1336
 
                pa_log_debug("Successfully mmap()ed output buffer.");
1337
 
                pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
1338
 
            }
1339
 
        }
1340
 
 
1341
 
        if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
1342
 
            namereg_fail = TRUE;
1343
 
        else {
1344
 
            name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev));
1345
 
            namereg_fail = FALSE;
1346
 
        }
1347
 
 
1348
 
        pa_sink_new_data_init(&sink_new_data);
1349
 
        sink_new_data.driver = __FILE__;
1350
 
        sink_new_data.module = m;
1351
 
        pa_sink_new_data_set_name(&sink_new_data, name);
1352
 
        sink_new_data.namereg_fail = namereg_fail;
1353
 
        pa_sink_new_data_set_sample_spec(&sink_new_data, &ss);
1354
 
        pa_sink_new_data_set_channel_map(&sink_new_data, &map);
1355
 
        pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_STRING, dev);
1356
 
        pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_API, "oss");
1357
 
        pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
1358
 
        pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
1359
 
        pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->out_hwbuf_size));
1360
 
        pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->out_fragment_size));
1361
 
 
1362
 
        u->sink = pa_sink_new(m->core, &sink_new_data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
1363
 
        pa_sink_new_data_done(&sink_new_data);
1364
 
        pa_xfree(name_buf);
1365
 
 
1366
 
        if (!u->sink) {
1367
 
            pa_log("Failed to create sink object");
1368
 
            goto fail;
1369
 
        }
1370
 
 
1371
 
        u->sink->parent.process_msg = sink_process_msg;
1372
 
        u->sink->userdata = u;
1373
 
 
1374
 
        pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1375
 
        pa_sink_set_rtpoll(u->sink, u->rtpoll);
1376
 
        u->sink->refresh_volume = TRUE;
1377
 
 
1378
 
        u->sink->thread_info.max_request = u->out_hwbuf_size;
1379
 
 
1380
 
        if (use_mmap)
1381
 
            u->out_mmap_memblocks = pa_xnew0(pa_memblock*, u->out_nfrags);
1382
 
    }
1383
 
 
1384
 
    if ((u->mixer_fd = pa_oss_open_mixer_for_device(u->device_name)) >= 0) {
1385
 
        pa_bool_t do_close = TRUE;
1386
 
        u->mixer_devmask = 0;
1387
 
 
1388
 
        if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &u->mixer_devmask) < 0)
1389
 
            pa_log_warn("SOUND_MIXER_READ_DEVMASK failed: %s", pa_cstrerror(errno));
1390
 
 
1391
 
        else {
1392
 
            if (u->sink && (u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM))) {
1393
 
                pa_log_debug("Found hardware mixer track for playback.");
1394
 
                u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
1395
 
                u->sink->get_volume = sink_get_volume;
1396
 
                u->sink->set_volume = sink_set_volume;
1397
 
                do_close = FALSE;
1398
 
            }
1399
 
 
1400
 
            if (u->source && (u->mixer_devmask & (SOUND_MASK_RECLEV|SOUND_MASK_IGAIN))) {
1401
 
                pa_log_debug("Found hardware mixer track for recording.");
1402
 
                u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL;
1403
 
                u->source->get_volume = source_get_volume;
1404
 
                u->source->set_volume = source_set_volume;
1405
 
                do_close = FALSE;
1406
 
            }
1407
 
        }
1408
 
 
1409
 
        if (do_close) {
1410
 
            pa_close(u->mixer_fd);
1411
 
            u->mixer_fd = -1;
1412
 
        }
1413
 
    }
1414
 
 
1415
 
go_on:
1416
 
 
1417
 
    pa_assert(u->source || u->sink);
1418
 
 
1419
 
    pa_memchunk_reset(&u->memchunk);
1420
 
 
1421
 
    if (!(u->thread = pa_thread_new(thread_func, u))) {
1422
 
        pa_log("Failed to create thread.");
1423
 
        goto fail;
1424
 
    }
1425
 
 
1426
 
    /* Read mixer settings */
1427
 
    if (u->sink) {
1428
 
        if (sink_new_data.volume_is_set) {
1429
 
            if (u->sink->set_volume)
1430
 
                u->sink->set_volume(u->sink);
1431
 
        } else {
1432
 
            if (u->sink->get_volume)
1433
 
                u->sink->get_volume(u->sink);
1434
 
        }
1435
 
    }
1436
 
 
1437
 
    if (u->source) {
1438
 
        if (source_new_data.volume_is_set) {
1439
 
            if (u->source->set_volume)
1440
 
                u->source->set_volume(u->source);
1441
 
        } else {
1442
 
            if (u->source->get_volume)
1443
 
                u->source->get_volume(u->source);
1444
 
        }
1445
 
    }
1446
 
 
1447
 
    if (u->sink)
1448
 
        pa_sink_put(u->sink);
1449
 
    if (u->source)
1450
 
        pa_source_put(u->source);
1451
 
 
1452
 
    pa_modargs_free(ma);
1453
 
 
1454
 
    return 0;
1455
 
 
1456
 
fail:
1457
 
 
1458
 
    if (u)
1459
 
        pa__done(m);
1460
 
    else if (fd >= 0)
1461
 
        pa_close(fd);
1462
 
 
1463
 
    if (ma)
1464
 
        pa_modargs_free(ma);
1465
 
 
1466
 
    return -1;
1467
 
}
1468
 
 
1469
 
void pa__done(pa_module*m) {
1470
 
    struct userdata *u;
1471
 
 
1472
 
    pa_assert(m);
1473
 
 
1474
 
    if (!(u = m->userdata))
1475
 
        return;
1476
 
 
1477
 
    if (u->sink)
1478
 
        pa_sink_unlink(u->sink);
1479
 
 
1480
 
    if (u->source)
1481
 
        pa_source_unlink(u->source);
1482
 
 
1483
 
    if (u->thread) {
1484
 
        pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1485
 
        pa_thread_free(u->thread);
1486
 
    }
1487
 
 
1488
 
    pa_thread_mq_done(&u->thread_mq);
1489
 
 
1490
 
    if (u->sink)
1491
 
        pa_sink_unref(u->sink);
1492
 
 
1493
 
    if (u->source)
1494
 
        pa_source_unref(u->source);
1495
 
 
1496
 
    if (u->memchunk.memblock)
1497
 
        pa_memblock_unref(u->memchunk.memblock);
1498
 
 
1499
 
    if (u->rtpoll_item)
1500
 
        pa_rtpoll_item_free(u->rtpoll_item);
1501
 
 
1502
 
    if (u->rtpoll)
1503
 
        pa_rtpoll_free(u->rtpoll);
1504
 
 
1505
 
    if (u->out_mmap_memblocks) {
1506
 
        unsigned i;
1507
 
        for (i = 0; i < u->out_nfrags; i++)
1508
 
            if (u->out_mmap_memblocks[i])
1509
 
                pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
1510
 
        pa_xfree(u->out_mmap_memblocks);
1511
 
    }
1512
 
 
1513
 
    if (u->in_mmap_memblocks) {
1514
 
        unsigned i;
1515
 
        for (i = 0; i < u->in_nfrags; i++)
1516
 
            if (u->in_mmap_memblocks[i])
1517
 
                pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
1518
 
        pa_xfree(u->in_mmap_memblocks);
1519
 
    }
1520
 
 
1521
 
    if (u->in_mmap && u->in_mmap != MAP_FAILED)
1522
 
        munmap(u->in_mmap, u->in_hwbuf_size);
1523
 
 
1524
 
    if (u->out_mmap && u->out_mmap != MAP_FAILED)
1525
 
        munmap(u->out_mmap, u->out_hwbuf_size);
1526
 
 
1527
 
    if (u->fd >= 0)
1528
 
        pa_close(u->fd);
1529
 
 
1530
 
    if (u->mixer_fd >= 0)
1531
 
        pa_close(u->mixer_fd);
1532
 
 
1533
 
    pa_xfree(u->device_name);
1534
 
 
1535
 
    pa_xfree(u);
1536
 
}