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, see <http://www.gnu.org/licenses/>. 18***/ 19 20#ifdef HAVE_CONFIG_H 21#include <config.h> 22#endif 23 24#include <stdlib.h> 25#include <stdio.h> 26 27#include <pulse/utf8.h> 28#include <pulse/fork-detect.h> 29 30#include <pulsecore/pstream-util.h> 31#include <pulsecore/macro.h> 32#include <pulsecore/proplist-util.h> 33 34#include "internal.h" 35#include "scache.h" 36 37int pa_stream_connect_upload(pa_stream *s, size_t length) { 38 pa_tagstruct *t; 39 uint32_t tag; 40 const char *name; 41 42 pa_assert(s); 43 pa_assert(PA_REFCNT_VALUE(s) >= 1); 44 45 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED); 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 PA_CHECK_VALIDITY(s->context, length == (size_t) (uint32_t) length, PA_ERR_INVALID); 49 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE); 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, (uint32_t) length); 67 68 if (s->context->version >= 13) 69 pa_tagstruct_put_proplist(t, s->proplist); 70 71 pa_pstream_send_tagstruct(s->context->pstream, t); 72 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_create_stream_callback, s, NULL); 73 74 pa_stream_set_state(s, PA_STREAM_CREATING); 75 76 pa_stream_unref(s); 77 return 0; 78} 79 80int pa_stream_finish_upload(pa_stream *s) { 81 pa_tagstruct *t; 82 uint32_t tag; 83 84 pa_assert(s); 85 pa_assert(PA_REFCNT_VALUE(s) >= 1); 86 87 PA_CHECK_VALIDITY(s->context, !pa_detect_fork(), PA_ERR_FORKED); 88 PA_CHECK_VALIDITY(s->context, s->channel_valid, PA_ERR_BADSTATE); 89 PA_CHECK_VALIDITY(s->context, s->context->state == PA_CONTEXT_READY, PA_ERR_BADSTATE); 90 91 pa_stream_ref(s); 92 93 t = pa_tagstruct_command(s->context, PA_COMMAND_FINISH_UPLOAD_STREAM, &tag); 94 pa_tagstruct_putu32(t, s->channel); 95 pa_pstream_send_tagstruct(s->context->pstream, t); 96 pa_pdispatch_register_reply(s->context->pdispatch, tag, DEFAULT_TIMEOUT, pa_stream_disconnect_callback, s, NULL); 97 98 pa_stream_unref(s); 99 return 0; 100} 101 102static void play_sample_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { 103 pa_operation *o = userdata; 104 int success = 1; 105 uint32_t idx = PA_INVALID_INDEX; 106 107 pa_assert(pd); 108 pa_assert(o); 109 pa_assert(PA_REFCNT_VALUE(o) >= 1); 110 111 if (!o->context) 112 goto finish; 113 114 if (command != PA_COMMAND_REPLY) { 115 if (pa_context_handle_error(o->context, command, t, false) < 0) 116 goto finish; 117 118 success = 0; 119 } else if ((o->context->version >= 13 && pa_tagstruct_getu32(t, &idx) < 0) || 120 !pa_tagstruct_eof(t)) { 121 pa_context_fail(o->context, PA_ERR_PROTOCOL); 122 goto finish; 123 } else if (o->context->version >= 13 && idx == PA_INVALID_INDEX) 124 success = 0; 125 126 if (o->callback) { 127 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback; 128 cb(o->context, success, o->userdata); 129 } 130 131finish: 132 pa_operation_done(o); 133 pa_operation_unref(o); 134} 135 136static void play_sample_with_proplist_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { 137 pa_operation *o = userdata; 138 uint32_t idx; 139 140 pa_assert(pd); 141 pa_assert(o); 142 pa_assert(PA_REFCNT_VALUE(o) >= 1); 143 144 if (!o->context) 145 goto finish; 146 147 if (command != PA_COMMAND_REPLY) { 148 if (pa_context_handle_error(o->context, command, t, false) < 0) 149 goto finish; 150 151 idx = PA_INVALID_INDEX; 152 } else if (pa_tagstruct_getu32(t, &idx) < 0 || 153 !pa_tagstruct_eof(t)) { 154 pa_context_fail(o->context, PA_ERR_PROTOCOL); 155 goto finish; 156 } 157 158 if (o->callback) { 159 pa_context_play_sample_cb_t cb = (pa_context_play_sample_cb_t) o->callback; 160 cb(o->context, idx, o->userdata); 161 } 162 163finish: 164 pa_operation_done(o); 165 pa_operation_unref(o); 166} 167 168pa_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) { 169 pa_operation *o; 170 pa_tagstruct *t; 171 uint32_t tag; 172 173 pa_assert(c); 174 pa_assert(PA_REFCNT_VALUE(c) >= 1); 175 176 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED); 177 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE); 178 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID); 179 PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID); 180 181 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata); 182 183 if (!dev) 184 dev = c->conf->default_sink; 185 186 t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag); 187 pa_tagstruct_putu32(t, PA_INVALID_INDEX); 188 pa_tagstruct_puts(t, dev); 189 190 if (!PA_VOLUME_IS_VALID(volume) && c->version < 15) 191 volume = PA_VOLUME_NORM; 192 193 pa_tagstruct_putu32(t, volume); 194 pa_tagstruct_puts(t, name); 195 196 if (c->version >= 13) { 197 pa_proplist *p = pa_proplist_new(); 198 pa_tagstruct_put_proplist(t, p); 199 pa_proplist_free(p); 200 } 201 202 pa_pstream_send_tagstruct(c->pstream, t); 203 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, play_sample_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref); 204 205 return o; 206} 207 208pa_operation *pa_context_play_sample_with_proplist(pa_context *c, const char *name, const char *dev, pa_volume_t volume, const pa_proplist *p, pa_context_play_sample_cb_t cb, void *userdata) { 209 pa_operation *o; 210 pa_tagstruct *t; 211 uint32_t tag; 212 213 pa_assert(c); 214 pa_assert(PA_REFCNT_VALUE(c) >= 1); 215 216 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED); 217 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE); 218 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID); 219 PA_CHECK_VALIDITY_RETURN_NULL(c, !dev || *dev, PA_ERR_INVALID); 220 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED); 221 222 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata); 223 224 if (!dev) 225 dev = c->conf->default_sink; 226 227 t = pa_tagstruct_command(c, PA_COMMAND_PLAY_SAMPLE, &tag); 228 pa_tagstruct_putu32(t, PA_INVALID_INDEX); 229 pa_tagstruct_puts(t, dev); 230 231 if (!PA_VOLUME_IS_VALID(volume) && c->version < 15) 232 volume = PA_VOLUME_NORM; 233 234 pa_tagstruct_putu32(t, volume); 235 pa_tagstruct_puts(t, name); 236 237 if (p) 238 pa_tagstruct_put_proplist(t, p); 239 else { 240 pa_proplist *empty_proplist = pa_proplist_new(); 241 pa_tagstruct_put_proplist(t, empty_proplist); 242 pa_proplist_free(empty_proplist); 243 } 244 245 pa_pstream_send_tagstruct(c->pstream, t); 246 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); 247 248 return o; 249} 250 251pa_operation* pa_context_remove_sample(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) { 252 pa_operation *o; 253 pa_tagstruct *t; 254 uint32_t tag; 255 256 pa_assert(c); 257 pa_assert(PA_REFCNT_VALUE(c) >= 1); 258 259 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED); 260 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE); 261 PA_CHECK_VALIDITY_RETURN_NULL(c, name && *name, PA_ERR_INVALID); 262 263 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata); 264 265 t = pa_tagstruct_command(c, PA_COMMAND_REMOVE_SAMPLE, &tag); 266 pa_tagstruct_puts(t, name); 267 268 pa_pstream_send_tagstruct(c->pstream, t); 269 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); 270 271 return o; 272} 273