summaryrefslogtreecommitdiffstats
path: root/src/multi.c
blob: 68d6d6466b4855f3607e6ade3ab0475e0691c682 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/***
  This file is part of libcanberra.

  Copyright 2008 Lennart Poettering

  libcanberra is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 2.1 of the
  License, or (at your option) any later version.

  libcanberra 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with libcanberra. If not, If not, see
  <http://www.gnu.org/licenses/>.
***/

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

#include "driver.h"
#include "llist.h"
#include "malloc.h"
#include "common.h"
#include "driver-order.h"

struct backend {
    CA_LLIST_FIELDS(struct backend);
    ca_context *context;
};

struct private {
    ca_context *context;
    CA_LLIST_HEAD(struct backend, backends);
};

#define PRIVATE(c) ((struct private *) ((c)->private))

static int add_backend(struct private *p, const char *name) {
    struct backend *b;
    int ret;

    ca_assert(p);
    ca_assert(name);

    if (ca_streq(name, "multi"))
        return CA_ERROR_NOTAVAILABLE;

    for (b = p->backends; b; b = b->next)
        if (ca_streq(b->context->driver, name))
            return CA_ERROR_NOTAVAILABLE;

    if (!(b = ca_new0(struct backend, 1)))
        return CA_ERROR_OOM;

    if ((ret = ca_context_create(&b->context)) < 0)
        goto fail;

    if ((ret = ca_context_change_props_full(b->context, p->context->props)) < 0)
        goto fail;

    if ((ret = ca_context_set_driver(b->context, name)) < 0)
        goto fail;

    if ((ret = ca_context_open(b->context)) < 0)
        goto fail;

    CA_LLIST_PREPEND(struct backend, p->backends, b);

    return CA_SUCCESS;

fail:

    if (b->context)
        ca_context_destroy(b->context);

    ca_free(b);

    return ret;
}

static int remove_backend(struct private *p, struct backend *b) {
    int ret;

    ca_assert(p);
    ca_assert(b);

    ret = ca_context_destroy(b->context);
    CA_LLIST_REMOVE(struct backend, p->backends, b);
    ca_free(b);

    return ret;
}

int driver_open(ca_context *c) {
    struct private *p;
    int ret = CA_SUCCESS;

    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(!c->driver || ca_streq(c->driver, "multi"), CA_ERROR_NODRIVER);
    ca_return_val_if_fail(!PRIVATE(c), CA_ERROR_STATE);

    if (!(c->private = p = ca_new0(struct private, 1)))
        return CA_ERROR_OOM;

    p->context = c;

    if (c->driver) {
        char *e, *k;

        if (!(e = ca_strdup(c->driver))) {
            driver_destroy(c);
            return CA_ERROR_OOM;
        }

        k = e;
        for (;;)  {
            size_t n;
            ca_bool_t last;

            n = strcspn(k, ":");
            last = k[n] == 0;
            k[n] = 0;

            if (n > 0) {
                int r;

                r = add_backend(p, k);

                if (ret == CA_SUCCESS)
                    ret = r;
            }

            if (last)
                break;

            k += n+1 ;
        }

        ca_free(e);

    } else {

        const char *const *e;

        for (e = ca_driver_order; *e; e++) {
            int r;

            r = add_backend(p, *e);

            /* We return the error code of the first module that fails only */
            if (ret == CA_SUCCESS)
                ret = r;
        }
    }

    if (!p->backends) {
        driver_destroy(c);
        return ret == CA_SUCCESS ? CA_ERROR_NODRIVER : ret;
    }

    return CA_SUCCESS;
}


int driver_destroy(ca_context *c) {
    int ret = CA_SUCCESS;
    struct private *p;

    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(c->private, CA_ERROR_STATE);

    p = PRIVATE(c);

    while (p->backends) {
        int r;

        r = remove_backend(p, p->backends);

        if (ret == CA_SUCCESS)
            ret = r;
    }

    ca_free(p);

    c->private = NULL;

    return ret;
}

int driver_change_device(ca_context *c, char *device) {
    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(c->private, CA_ERROR_STATE);

    return CA_ERROR_NOTSUPPORTED;
}

int driver_change_props(ca_context *c, ca_proplist *changed, ca_proplist *merged) {
    int ret = CA_SUCCESS;
    struct private *p;
    struct backend *b;

    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(changed, CA_ERROR_INVALID);
    ca_return_val_if_fail(merged, CA_ERROR_INVALID);
    ca_return_val_if_fail(c->private, CA_ERROR_STATE);

    p = PRIVATE(c);

    for (b = p->backends; b; b = b->next) {
        int r;

        r = ca_context_change_props_full(b->context, changed);

        /* We only return the first failure */
        if (ret == CA_SUCCESS)
            ret = r;
    }

    return ret;
}

struct closure {
    ca_context *context;
    ca_finish_callback_t callback;
    void *userdata;
};

static void call_closure(ca_context *c, uint32_t id, int error_code, void *userdata) {
    struct closure *closure = userdata;

    closure->callback(closure->context, id, error_code, closure->userdata);
    ca_free(closure);
}

int driver_play(ca_context *c, uint32_t id, ca_proplist *proplist, ca_finish_callback_t cb, void *userdata) {
    int ret = CA_SUCCESS;
    struct private *p;
    struct backend *b;
    struct closure *closure;

    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(proplist, CA_ERROR_INVALID);
    ca_return_val_if_fail(!userdata || cb, CA_ERROR_INVALID);
    ca_return_val_if_fail(c->private, CA_ERROR_STATE);

    p = PRIVATE(c);

    if (cb) {
        if (!(closure = ca_new(struct closure, 1)))
            return CA_ERROR_OOM;

        closure->context = c;
        closure->callback = cb;
        closure->userdata = userdata;
    } else
        closure = NULL;

    /* The first backend that can play this, takes it */
    for (b = p->backends; b; b = b->next) {
        int r;

        if ((r = ca_context_play_full(b->context, id, proplist, closure ? call_closure : NULL, closure)) == CA_SUCCESS)
            return r;

        /* We only return the first failure */
        if (ret == CA_SUCCESS)
            ret = r;
    }

    ca_free(closure);

    return ret;
}

int driver_cancel(ca_context *c, uint32_t id) {
    int ret = CA_SUCCESS;
    struct private *p;
    struct backend *b;

    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(c->private, CA_ERROR_STATE);

    p = PRIVATE(c);

    for (b = p->backends; b; b = b->next) {
        int r;

        r = ca_context_cancel(b->context, id);

        /* We only return the first failure */
        if (ret == CA_SUCCESS)
            ret = r;
    }

    return ret;
}

int driver_cache(ca_context *c, ca_proplist *proplist) {
    int ret = CA_SUCCESS;
    struct private *p;
    struct backend *b;

    ca_return_val_if_fail(c, CA_ERROR_INVALID);
    ca_return_val_if_fail(proplist, CA_ERROR_INVALID);
    ca_return_val_if_fail(c->private, CA_ERROR_STATE);

    p = PRIVATE(c);

    /* The first backend that can cache this, takes it */
    for (b = p->backends; b; b = b->next) {
        int r;

        if ((r = ca_context_cache_full(b->context,  proplist)) == CA_SUCCESS)
            return r;

        /* We only return the first failure */
        if (ret == CA_SUCCESS)
            ret = r;
    }

    return ret;
}