Adding module-bt-discover
[pulseaudio-mirror.git] / src / pulse / scache.c
blobfd3b9876b0fea2ae4b99c1338b4325985d52bb25
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <pulse/utf8.h>
32 #include <pulsecore/pstream-util.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/proplist-util.h>
36 #include "internal.h"
38 #include "scache.h"
40 int pa_stream_connect_upload(pa_stream *s, size_t length) {
41 pa_tagstruct *t;
42 uint32_t tag;
43 const char *name;
45 pa_assert(s);
46 pa_assert(PA_REFCNT_VALUE(s) >= 1);
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 PA_CHECK_VALIDITY(s->context, length == (size_t) (uint32_t) length, PA_ERR_INVALID);
52 if (!(name = pa_proplist_gets(s->proplist, PA_PROP_EVENT_ID)))
53 name = pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME);
55 PA_CHECK_VALIDITY(s->context, name && *name && pa_utf8_valid(name), PA_ERR_INVALID);
57 pa_stream_ref(s);
59 s->direction = PA_STREAM_UPLOAD;
60 s->flags = 0;
62 t = pa_tagstruct_command(s->context, PA_COMMAND_CREATE_UPLOAD_STREAM, &tag);
64 pa_tagstruct_puts(t, name);
65 pa_tagstruct_put_sample_spec(t, &s->sample_spec);
66 pa_tagstruct_put_channel_map(t, &s->channel_map);
67 pa_tagstruct_putu32(t, (uint32_t) length);
69 if (s->context->version >= 13) {
70 pa_init_proplist(s->proplist);
71 pa_tagstruct_put_proplist(t, s->proplist);
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);
77 pa_stream_set_state(s, PA_STREAM_CREATING);
79 pa_stream_unref(s);
80 return 0;
83 int pa_stream_finish_upload(pa_stream *s) {
84 pa_tagstruct *t;
85 uint32_t tag;
87 pa_assert(s);
88 pa_assert(PA_REFCNT_VALUE(s) >= 1);
90 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE);
91 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
93 pa_stream_ref(s);
95 t = pa_tagstruct_command(s->context, PA_COMMAND_FINISH_UPLOAD_STREAM, &tag);
96 pa_tagstruct_putu32(t, s->channel);
97 pa_pstream_send_tagstruct(s->context->pstream, t);
98 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL);
100 pa_stream_unref(s);
101 return 0;
104 static void play_sample_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
105 pa_operation *o = userdata;
106 int success = 1;
107 uint32_t idx = PA_INVALID_INDEX;
109 pa_assert(pd);
110 pa_assert(o);
111 pa_assert(PA_REFCNT_VALUE(o) >= 1);
113 if (!o->context)
114 goto finish;
116 if (command != PA_COMMAND_REPLY) {
117 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
118 goto finish;
120 success = 0;
121 } else if ((o->context->version >= 13 && pa_tagstruct_getu32(t, &idx) < 0) ||
122 !pa_tagstruct_eof(t)) {
123 pa_context_fail(o->context, PA_ERR_PROTOCOL);
124 goto finish;
125 } else if (o->context->version >= 13 && idx == PA_INVALID_INDEX)
126 success = 0;
128 if (o->callback) {
129 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
130 cb(o->context, success, o->userdata);
133 finish:
134 pa_operation_done(o);
135 pa_operation_unref(o);
138 static void play_sample_with_proplist_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
139 pa_operation *o = userdata;
140 uint32_t idx;
142 pa_assert(pd);
143 pa_assert(o);
144 pa_assert(PA_REFCNT_VALUE(o) >= 1);
146 if (!o->context)
147 goto finish;
149 if (command != PA_COMMAND_REPLY) {
150 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
151 goto finish;
153 idx = PA_INVALID_INDEX;
154 } else if (pa_tagstruct_getu32(t, &idx) < 0 ||
155 !pa_tagstruct_eof(t)) {
156 pa_context_fail(o->context, PA_ERR_PROTOCOL);
157 goto finish;
160 if (o->callback) {
161 pa_context_play_sample_cb_t cb = (pa_context_play_sample_cb_t) o->callback;
162 cb(o->context, idx, o->userdata);
165 finish:
166 pa_operation_done(o);
167 pa_operation_unref(o);
171 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) {
172 pa_operation *o;
173 pa_tagstruct *t;
174 uint32_t tag;
176 pa_assert(c);
177 pa_assert(PA_REFCNT_VALUE(c) >= 1);
179 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
180 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
181 PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID);
183 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
185 if (!dev)
186 dev = c->conf->default_sink;
188 t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag);
189 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
190 pa_tagstruct_puts(t, dev);
191 pa_tagstruct_putu32(t, volume);
192 pa_tagstruct_puts(t, name);
194 if (c->version >= 13) {
195 pa_proplist *p = pa_proplist_new();
196 pa_tagstruct_put_proplist(t, p);
197 pa_proplist_free(p);
200 pa_pstream_send_tagstruct(c->pstream, t);
201 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, play_sample_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
203 return o;
206 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) {
207 pa_operation *o;
208 pa_tagstruct *t;
209 uint32_t tag;
211 pa_assert(c);
212 pa_assert(PA_REFCNT_VALUE(c) >= 1);
214 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
215 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
216 PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID);
217 PA_CHECK_VALIDITY_RETURN_NULL(c, p, PA_ERR_INVALID);
218 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
220 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
222 if (!dev)
223 dev = c->conf->default_sink;
225 t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag);
226 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
227 pa_tagstruct_puts(t, dev);
228 pa_tagstruct_putu32(t, volume);
229 pa_tagstruct_puts(t, name);
230 pa_tagstruct_put_proplist(t, p);
232 pa_pstream_send_tagstruct(c->pstream, t);
233 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);
235 return o;
238 pa_operation* pa_context_remove_sample(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
239 pa_operation *o;
240 pa_tagstruct *t;
241 uint32_t tag;
243 pa_assert(c);
244 pa_assert(PA_REFCNT_VALUE(c) >= 1);
246 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
247 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID);
249 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
251 t = pa_tagstruct_command(c, PA_COMMAND_REMOVE_SAMPLE, &tag);
252 pa_tagstruct_puts(t, name);
254 pa_pstream_send_tagstruct(c->pstream, t);
255 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);
257 return o;