]> code.delx.au - pulseaudio/blob - src/pulse/scache.c
Merge dead branch 'liboil-test'
[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 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
32 #include <pulsecore/pstream-util.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/proplist-util.h>
35
36 #include "internal.h"
37
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, s->state == PA_STREAM_UNCONNECTED, PA_ERR_BADSTATE);
49 PA_CHECK_VALIDITY(s->context, length > 0, PA_ERR_INVALID);
50
51 if (!(name = pa_proplist_gets(s->proplist, PA_PROP_EVENT_ID)))
52 name = pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME);
53
54 PA_CHECK_VALIDITY(s->context, name && *name && pa_utf8_valid(name), PA_ERR_INVALID);
55
56 pa_stream_ref(s);
57
58 s->direction = PA_STREAM_UPLOAD;
59 s->flags = 0;
60
61 t = pa_tagstruct_command(s->context, PA_COMMAND_CREATE_UPLOAD_STREAM, &tag);
62
63 pa_tagstruct_puts(t, name);
64 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
65 pa_tagstruct_put_channel_map(t, &s->channel_map);
66 pa_tagstruct_putu32(t, length);
67
68 if (s->context->version >= 13) {
69 pa_init_proplist(s->proplist);
70 pa_tagstruct_put_proplist(t, s->proplist);
71 }
72
73 pa_pstream_send_tagstruct(s->context->pstream, t);
74 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL);
75
76 pa_stream_set_state(s, PA_STREAM_CREATING);
77
78 pa_stream_unref(s);
79 return 0;
80 }
81
82 int pa_stream_finish_upload(pa_stream *s) {
83 pa_tagstruct *t;
84 uint32_t tag;
85
86 pa_assert(s);
87 pa_assert(PA_REFCNT_VALUE(s) >= 1);
88
89 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
90 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
91
92 pa_stream_ref(s);
93
94 t = pa_tagstruct_command(s->context, PA_COMMAND_FINISH_UPLOAD_STREAM, &tag);
95 pa_tagstruct_putu32(t, s->channel);
96 pa_pstream_send_tagstruct(s->context->pstream, t);
97 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
98
99 pa_stream_unref(s);
100 return 0;
101 }
102
103 static void play_sample_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
104 pa_operation *o = userdata;
105 int success = 1;
106 uint32_t idx = PA_INVALID_INDEX;
107
108 pa_assert(pd);
109 pa_assert(o);
110 pa_assert(PA_REFCNT_VALUE(o) >= 1);
111
112 if (!o->context)
113 goto finish;
114
115 if (command != PA_COMMAND_REPLY) {
116 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
117 goto finish;
118
119 success = 0;
120 } else if ((o->context->version >= 13 && pa_tagstruct_getu32(t, &idx) < 0) ||
121 !pa_tagstruct_eof(t)) {
122 pa_context_fail(o->context, PA_ERR_PROTOCOL);
123 goto finish;
124 } else if (o->context->version >= 13 && idx == PA_INVALID_INDEX)
125 success = 0;
126
127 if (o->callback) {
128 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
129 cb(o->context, success, o->userdata);
130 }
131
132 finish:
133 pa_operation_done(o);
134 pa_operation_unref(o);
135 }
136
137 static void play_sample_with_proplist_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
138 pa_operation *o = userdata;
139 uint32_t idx;
140
141 pa_assert(pd);
142 pa_assert(o);
143 pa_assert(PA_REFCNT_VALUE(o) >= 1);
144
145 if (!o->context)
146 goto finish;
147
148 if (command != PA_COMMAND_REPLY) {
149 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
150 goto finish;
151
152 idx = PA_INVALID_INDEX;
153 } else if (pa_tagstruct_getu32(t, &idx) < 0 ||
154 !pa_tagstruct_eof(t)) {
155 pa_context_fail(o->context, PA_ERR_PROTOCOL);
156 goto finish;
157 }
158
159 if (o->callback) {
160 pa_context_play_sample_cb_t cb = (pa_context_play_sample_cb_t) o->callback;
161 cb(o->context, idx, o->userdata);
162 }
163
164 finish:
165 pa_operation_done(o);
166 pa_operation_unref(o);
167 }
168
169
170 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) {
171 pa_operation *o;
172 pa_tagstruct *t;
173 uint32_t tag;
174
175 pa_assert(c);
176 pa_assert(PA_REFCNT_VALUE(c) >= 1);
177
178 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
179 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
180 PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID);
181
182 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
183
184 if (!dev)
185 dev = c->conf->default_sink;
186
187 t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag);
188 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
189 pa_tagstruct_puts(t, dev);
190 pa_tagstruct_putu32(t, volume);
191 pa_tagstruct_puts(t, name);
192
193 if (c->version >= 13) {
194 pa_proplist *p = pa_proplist_new();
195 pa_tagstruct_put_proplist(t, p);
196 pa_proplist_free(p);
197 }
198
199 pa_pstream_send_tagstruct(c->pstream, t);
200 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, play_sample_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
201
202 return o;
203 }
204
205 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) {
206 pa_operation *o;
207 pa_tagstruct *t;
208 uint32_t tag;
209
210 pa_assert(c);
211 pa_assert(PA_REFCNT_VALUE(c) >= 1);
212
213 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
214 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
215 PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID);
216 PA_CHECK_VALIDITY_RETURN_NULL(c, p, PA_ERR_INVALID);
217 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
218
219 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
220
221 if (!dev)
222 dev = c->conf->default_sink;
223
224 t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag);
225 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
226 pa_tagstruct_puts(t, dev);
227 pa_tagstruct_putu32(t, volume);
228 pa_tagstruct_puts(t, name);
229 pa_tagstruct_put_proplist(t, p);
230
231 pa_pstream_send_tagstruct(c->pstream, t);
232 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);
233
234 return o;
235 }
236
237 pa_operation* pa_context_remove_sample(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
238 pa_operation *o;
239 pa_tagstruct *t;
240 uint32_t tag;
241
242 pa_assert(c);
243 pa_assert(PA_REFCNT_VALUE(c) >= 1);
244
245 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
246 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
247
248 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
249
250 t = pa_tagstruct_command(c, PA_COMMAND_REMOVE_SAMPLE, &tag);
251 pa_tagstruct_puts(t, name);
252
253 pa_pstream_send_tagstruct(c->pstream, t);
254 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);
255
256 return o;
257 }