summaryrefslogtreecommitdiffstats
path: root/src/ao_polyp.c
blob: 82146629bb0e61d7387b2be98d61b537241fa42f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* $Id$ */

/***
  This file is part of libao-polyp.
 
  libao-polyp is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  libao-polyp is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with libao-polyp; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <string.h>
#include <signal.h>
#include <limits.h>

#include <polyp/polyplib-simple.h>
#include <ao/ao.h>
#include <ao/plugin.h>

/* Unfortunately libao doesn't allow "const" for these structures... */
static char * ao_polyp_options[] = {
    "server",
    "sink"
};

static ao_info ao_polyp_info = {
    AO_TYPE_LIVE,
    "polypaudio output",
    "polyp",
    PACKAGE_BUGREPORT,
    "Outputs to the Polypaudio Sound Server",
    AO_FMT_NATIVE,
    41,
    ao_polyp_options,
    2
};

typedef struct ao_polyp_internal {
    struct pa_simple *simple;
    char *server, *sink;
} ao_polyp_internal;

/* Dirty trick: import these two functions from polyplib */
char *pa_get_binary_name(char *s, size_t l);
char *pa_path_get_filename(const char *p);

/* Yes, this is very ugly, but required nonetheless... */
static void disable_sigpipe(void) {
    struct sigaction sa;

    sigaction(SIGPIPE, NULL, &sa);
    if (sa.sa_handler != SIG_IGN) {
        memset(&sa, 0, sizeof(sa));
        sa.sa_handler = SIG_IGN;
        sa.sa_flags = SA_RESTART;
        sigaction(SIGPIPE, &sa, NULL);
    }
}

int ao_plugin_test(void) {
    char p[PATH_MAX], t[256], t2[256], *fn = NULL;
    struct pa_simple *s;
    static const struct pa_sample_spec ss = {
        .format = PA_SAMPLE_S16LE,
        .rate = 44100,
        .channels = 2
    };

    disable_sigpipe();
    
    if (getenv("POLYP_SERVER") || getenv("POLYP_SINK"))
        return 1;

    if (pa_get_binary_name(p, sizeof(p))) {
        fn = pa_path_get_filename(p);
        snprintf(t, sizeof(t), "libao[%s]", fn);
        snprintf(t2, sizeof(t2), "libao[%s] test", fn);
    }

    if (!(s = pa_simple_new(NULL, fn ? t : "libao", PA_STREAM_PLAYBACK, NULL, fn ? t2 : "libao test", &ss, NULL, PA_VOLUME_NORM, NULL)))
        return 0;

    pa_simple_free(s);
    return 1;
}

ao_info *ao_plugin_driver_info(void) {
    return &ao_polyp_info;
}

int ao_plugin_device_init(ao_device *device) {
    ao_polyp_internal *internal;
    assert(device);

    internal = (ao_polyp_internal *) malloc(sizeof(ao_polyp_internal));
    
    if (internal == NULL)	
        return 0; 

    internal->simple = NULL;
    internal->server = NULL;
    internal->sink = NULL;
    device->internal = internal;
    
    return 1;
}

int ao_plugin_set_option(ao_device *device, const char *key, const char *value) {
    ao_polyp_internal *internal;
    assert(device && device->internal && key && value);
    internal = (ao_polyp_internal *) device->internal;
    
    if (!strcmp(key, "server")) {
        free(internal->server);
        internal->server = strdup(value);
    } else if (!strcmp(key, "sink")) {
        free(internal->sink);
        internal->sink = strdup(value);
    } else
        return 0;
    
    return 1;
}

int ao_plugin_open(ao_device *device, ao_sample_format *format) {
    char p[PATH_MAX], t[256], t2[256], *fn = NULL;
    ao_polyp_internal *internal;
    struct pa_sample_spec ss;

    assert(device && device->internal && format);

    internal = (ao_polyp_internal *) device->internal;

    if (format->bits == 8)
        ss.format = PA_SAMPLE_U8;
    else if (format->bits == 16)
        ss.format = PA_SAMPLE_S16NE;
    else
        return 0;

    if (format->channels <= 0)
        return 0;

    ss.channels = format->channels;
    ss.rate = format->rate;

    disable_sigpipe();

    if (pa_get_binary_name(p, sizeof(p))) {
        fn = pa_path_get_filename(p);
        snprintf(t, sizeof(t), "libao[%s]", fn);
        snprintf(t2, sizeof(t2), "libao[%s] playback stream", fn);
    }
    
    if (!(internal->simple = pa_simple_new(internal->server, fn ? t : "libao", PA_STREAM_PLAYBACK, internal->sink, fn ? t2 : "libao playback stream", &ss, NULL, PA_VOLUME_NORM, NULL)))
        return 0;

    device->driver_byte_format = AO_FMT_NATIVE;
    return 1;
}

int ao_plugin_play(ao_device *device, const char* output_samples, uint_32 num_bytes) {
    assert(device && device->internal);
    ao_polyp_internal *internal = (ao_polyp_internal *) device->internal;

    return pa_simple_write(internal->simple, output_samples, num_bytes, NULL) >= 0;
}


int ao_plugin_close(ao_device *device) {
    assert(device && device->internal);
    ao_polyp_internal *internal = (ao_polyp_internal *) device->internal;

    pa_simple_drain(internal->simple, NULL);
    pa_simple_free(internal->simple);
    internal->simple = NULL;

    return 1;
}

void ao_plugin_device_clear(ao_device *device) {
    assert(device && device->internal);
    ao_polyp_internal *internal = (ao_polyp_internal *) device->internal;

    free(internal->server);
    free(internal->sink);
    free(internal);
    device->internal = NULL;
}