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