summaryrefslogtreecommitdiffstats
path: root/polyp/polyplib-simple.c
blob: 8caae87e04fde2c82fba9c6116f649a55cc02e59 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio 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.
 
  polypaudio 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 polypaudio; 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 <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include "polyplib-simple.h"
#include "polyplib.h"
#include "mainloop.h"
#include "native-common.h"
#include "xmalloc.h"
#include "log.h"

struct pa_simple {
    struct pa_mainloop *mainloop;
    struct pa_context *context;
    struct pa_stream *stream;
    enum pa_stream_direction direction;

    int dead, drained;

    void *read_data;
    size_t read_index, read_length;
};

static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata);

static int check_error(struct pa_simple *p, int *perror) {
    enum pa_context_state cst;
    enum pa_stream_state sst;
    assert(p);
    
    if ((cst = pa_context_get_state(p->context)) == PA_CONTEXT_FAILED)
        goto fail;

    assert(cst != PA_CONTEXT_TERMINATED);

    if (p->stream) {
        if ((sst = pa_stream_get_state(p->stream)) == PA_STREAM_FAILED)
            goto fail;
        
        assert(sst != PA_STREAM_TERMINATED);
    }
    
    return 0;
    
fail:
    if (perror)
        *perror = pa_context_errno(p->context);
    return -1;
}

static int iterate(struct pa_simple *p, int block, int *perror) {
    assert(p && p->context && p->mainloop);

    if (check_error(p, perror) < 0)
        return -1;
    
    if (!block && !pa_context_is_pending(p->context))
        return 0;

    do {
        if (pa_mainloop_iterate(p->mainloop, 1, NULL) < 0) {
            if (perror)
                *perror = PA_ERROR_INTERNAL;
            return -1;
        }

        if (check_error(p, perror) < 0)
            return -1;
        
    } while (pa_context_is_pending(p->context));

    return 0;
}

struct pa_simple* pa_simple_new(
    const char *server,
    const char *name,
    enum pa_stream_direction dir,
    const char *dev,
    const char *stream_name,
    const struct pa_sample_spec *ss,
    const struct pa_buffer_attr *attr,
    int *perror) {
    
    struct pa_simple *p;
    int error = PA_ERROR_INTERNAL;
    assert(ss && (dir == PA_STREAM_PLAYBACK || dir == PA_STREAM_RECORD));

    p = pa_xmalloc(sizeof(struct pa_simple));
    p->context = NULL;
    p->stream = NULL;
    p->mainloop = pa_mainloop_new();
    assert(p->mainloop);
    p->dead = 0;
    p->direction = dir;
    p->read_data = NULL;
    p->read_index = p->read_length = 0;

    if (!(p->context = pa_context_new(pa_mainloop_get_api(p->mainloop), name)))
        goto fail;
    
    pa_context_connect(p->context, server);

    /* Wait until the context is ready */
    while (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
        if (iterate(p, 1, &error) < 0)
            goto fail;
    }

    if (!(p->stream = pa_stream_new(p->context, stream_name, ss)))
        goto fail;

    if (dir == PA_STREAM_PLAYBACK)
        pa_stream_connect_playback(p->stream, dev, attr);
    else
        pa_stream_connect_record(p->stream, dev, attr);

    /* Wait until the stream is ready */
    while (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
        if (iterate(p, 1, &error) < 0)
            goto fail;
    }

    pa_stream_set_read_callback(p->stream, read_callback, p);
    
    return p;
    
fail:
    if (perror)
        *perror = error;
    pa_simple_free(p);
    return NULL;
}

void pa_simple_free(struct pa_simple *s) {
    assert(s);

    pa_xfree(s->read_data);

    if (s->stream)
        pa_stream_unref(s->stream);
    
    if (s->context)
        pa_context_unref(s->context);

    if (s->mainloop)
        pa_mainloop_free(s->mainloop);

    pa_xfree(s);
}

int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) {
    assert(p && data && p->direction == PA_STREAM_PLAYBACK);

    while (length > 0) {
        size_t l;
        
        while (!(l = pa_stream_writable_size(p->stream)))
            if (iterate(p, 1, perror) < 0)
                return -1;

        if (l > length)
            l = length;

        pa_stream_write(p->stream, data, l, NULL, 0);
        data = (uint8_t*) data + l;
        length -= l;
    }

    /* Make sure that no data is pending for write */
    if (iterate(p, 0, perror) < 0)
        return -1;

    return 0;
}

static void read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
    struct pa_simple *p = userdata;
    assert(s && data && length && p);

    if (p->read_data) {
        pa_log(__FILE__": Buffer overflow, dropping incoming memory blocks.\n");
        pa_xfree(p->read_data);
    }

    p->read_data = pa_xmemdup(data, p->read_length = length);
    p->read_index = 0;
}

int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) {
    assert(p && data && p->direction == PA_STREAM_RECORD);

    while (length > 0) {
        if (p->read_data) {
            size_t l = length;

            if (p->read_length <= l)
                l = p->read_length;

            memcpy(data, (uint8_t*) p->read_data+p->read_index, l);

            data = (uint8_t*) data + l;
            length -= l;
            
            p->read_index += l;
            p->read_length -= l;

            if (!p->read_length) {
                pa_xfree(p->read_data);
                p->read_data = NULL;
                p->read_index = 0;
            }
            
            if (!length)
                return 0;

            assert(!p->read_data);
        }

        if (iterate(p, 1, perror) < 0)
            return -1;
    }

    return 0;
}

static void drain_complete(struct pa_stream *s, int success, void *userdata) {
    struct pa_simple *p = userdata;
    assert(s && p);
    p->drained = success ? 1 : -1;
}

int pa_simple_drain(struct pa_simple *p, int *perror) {
    struct pa_operation *o;
    
    assert(p && p->direction == PA_STREAM_PLAYBACK);
    p->drained = 0;
    o = pa_stream_drain(p->stream, drain_complete, p);

    while (!p->drained) {
        if (iterate(p, 1, perror) < 0) {
            pa_operation_cancel(o);
            pa_operation_unref(o);
            return -1;
        }
    }

    pa_operation_unref(o);

    if (p->drained < 0 && perror)
        *perror = pa_context_errno(p->context);

    return 0;
}