merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / pulse / scache.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <pulsecore/pstream-util.h>
33 #include <pulsecore/macro.h>
34
35 #include "internal.h"
36
37 #include "scache.h"
38
39 int pa_stream_connect_upload(pa_stream *s, size_t length) {
40     pa_tagstruct *t;
41     uint32_t tag;
42
43     pa_assert(s);
44     pa_assert(PA_REFCNT_VALUE(s) >= 1);
45
46     PA_CHECK_VALIDITY(s->context, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
47     PA_CHECK_VALIDITY(s->context, length > 0, PA_ERR_INVALID);
48
49     pa_stream_ref(s);
50
51     s->direction = PA_STREAM_UPLOAD;
52     s->flags = 0;
53
54     t = pa_tagstruct_command(s->context, PA_COMMAND_CREATE_UPLOAD_STREAM, &tag);
55
56     if (s->context->version < 13)
57         pa_tagstruct_puts(t, pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME));
58
59     pa_tagstruct_put_sample_spec(t, &s->sample_spec);
60     pa_tagstruct_put_channel_map(t, &s->channel_map);
61     pa_tagstruct_putu32(t, length);
62
63     if (s->context->version >= 13) {
64         pa_init_proplist(s->proplist);
65         pa_tagstruct_put_proplist(t, s->proplist);
66     }
67
68     pa_pstream_send_tagstruct(s->context->pstream, t);
69     pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
70
71     pa_stream_set_state(s, PA_STREAM_CREATING);
72
73     pa_stream_unref(s);
74     return 0;
75 }
76
77 int pa_stream_finish_upload(pa_stream *s) {
78     pa_tagstruct *t;
79     uint32_t tag;
80
81     pa_assert(s);
82     pa_assert(PA_REFCNT_VALUE(s) >= 1);
83
84     PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
85     PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
86
87     pa_stream_ref(s);
88
89     t = pa_tagstruct_command(s->context, PA_COMMAND_FINISH_UPLOAD_STREAM, &tag);
90     pa_tagstruct_putu32(t, s->channel);
91     pa_pstream_send_tagstruct(s->context->pstream, t);
92     pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
93
94     pa_stream_unref(s);
95     return 0;
96 }
97
98 static void play_sample_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
99     pa_operation *o = userdata;
100     int success = 1;
101     uint32_t idx = PA_INVALID_INDEX;
102
103     pa_assert(pd);
104     pa_assert(o);
105     pa_assert(PA_REFCNT_VALUE(o) >= 1);
106
107     if (!o->context)
108         goto finish;
109
110     if (command != PA_COMMAND_REPLY) {
111         if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
112             goto finish;
113
114         success = 0;
115     } else if ((o->context->version >= 13 && pa_tagstruct_getu32(t, &idx) < 0) ||
116                !pa_tagstruct_eof(t)) {
117         pa_context_fail(o->context, PA_ERR_PROTOCOL);
118         goto finish;
119     } else if (o->context->version >= 13 && idx == PA_INVALID_INDEX)
120         success = 0;
121
122     if (o->callback) {
123         pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
124         cb(o->context, success, o->userdata);
125     }
126
127 finish:
128     pa_operation_done(o);
129     pa_operation_unref(o);
130 }
131
132 static void play_sample_with_proplist_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
133     pa_operation *o = userdata;
134     uint32_t idx;
135
136     pa_assert(pd);
137     pa_assert(o);
138     pa_assert(PA_REFCNT_VALUE(o) >= 1);
139
140     if (!o->context)
141         goto finish;
142
143     if (command != PA_COMMAND_REPLY) {
144         if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
145             goto finish;
146
147         idx = PA_INVALID_INDEX;
148     } else if (pa_tagstruct_getu32(t, &idx) < 0 ||
149                !pa_tagstruct_eof(t)) {
150         pa_context_fail(o->context, PA_ERR_PROTOCOL);
151         goto finish;
152     }
153
154     if (o->callback) {
155         pa_context_play_sample_cb_t cb = (pa_context_play_sample_cb_t) o->callback;
156         cb(o->context, idx, o->userdata);
157     }
158
159 finish:
160     pa_operation_done(o);
161     pa_operation_unref(o);
162 }
163
164
165 pa_operation *pa_context_play_sample(pa_context *c, const char *name, const char *dev, pa_volume_t volume, pa_context_success_cb_t cb, void *userdata) {
166     pa_operation *o;
167     pa_tagstruct *t;
168     uint32_t tag;
169
170     pa_assert(c);
171     pa_assert(PA_REFCNT_VALUE(c) >= 1);
172
173     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
174     PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
175     PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID);
176
177     o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
178
179     if (!dev)
180         dev = c->conf->default_sink;
181
182     t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag);
183     pa_tagstruct_putu32(t, PA_INVALID_INDEX);
184     pa_tagstruct_puts(t, dev);
185     pa_tagstruct_putu32(t, volume);
186     pa_tagstruct_puts(t, name);
187
188     if (c->version >= 13) {
189         pa_proplist *p = pa_proplist_new();
190         pa_tagstruct_put_proplist(t, p);
191         pa_proplist_free(p);
192     }
193
194     pa_pstream_send_tagstruct(c->pstream, t);
195     pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, play_sample_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
196
197     return o;
198 }
199
200 pa_operation *pa_context_play_sample_with_proplist(pa_context *c, const char *name, const char *dev, pa_volume_t volume, pa_proplist *p, pa_context_play_sample_cb_t cb, void *userdata) {
201     pa_operation *o;
202     pa_tagstruct *t;
203     uint32_t tag;
204
205     pa_assert(c);
206     pa_assert(PA_REFCNT_VALUE(c) >= 1);
207
208     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
209     PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
210     PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID);
211     PA_CHECK_VALIDITY_RETURN_NULL(c, p, PA_ERR_INVALID);
212     PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
213
214     o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
215
216     if (!dev)
217         dev = c->conf->default_sink;
218
219     t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag);
220     pa_tagstruct_putu32(t, PA_INVALID_INDEX);
221     pa_tagstruct_puts(t, dev);
222     pa_tagstruct_putu32(t, volume);
223     pa_tagstruct_puts(t, name);
224     pa_tagstruct_put_proplist(t, p);
225
226     pa_pstream_send_tagstruct(c->pstream, t);
227     pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, play_sample_with_proplist_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
228
229     return o;
230 }
231
232 pa_operation* pa_context_remove_sample(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
233     pa_operation *o;
234     pa_tagstruct *t;
235     uint32_t tag;
236
237     pa_assert(c);
238     pa_assert(PA_REFCNT_VALUE(c) >= 1);
239
240     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
241     PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
242
243     o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
244
245     t = pa_tagstruct_command(c, PA_COMMAND_REMOVE_SAMPLE, &tag);
246     pa_tagstruct_puts(t, name);
247
248     pa_pstream_send_tagstruct(c->pstream, t);
249     pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
250
251     return o;
252 }