4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include "protocol-native.h"
32 #include "native-common.h"
35 #include "source-output.h"
36 #include "sink-input.h"
38 #include "tagstruct.h"
39 #include "pdispatch.h"
40 #include "pstream-util.h"
46 #include "subscribe.h"
49 struct pa_protocol_native
;
51 struct record_stream
{
52 struct connection
*connection
;
54 struct pa_source_output
*source_output
;
55 struct pa_memblockq
*memblockq
;
59 struct playback_stream
{
61 struct connection
*connection
;
63 struct pa_sink_input
*sink_input
;
64 struct pa_memblockq
*memblockq
;
65 size_t requested_bytes
;
70 struct upload_stream
{
72 struct connection
*connection
;
74 struct pa_memchunk memchunk
;
77 struct pa_sample_spec sample_spec
;
80 struct output_stream
{
91 struct pa_protocol_native
*protocol
;
92 struct pa_client
*client
;
93 struct pa_pstream
*pstream
;
94 struct pa_pdispatch
*pdispatch
;
95 struct pa_idxset
*record_streams
, *output_streams
;
96 uint32_t rrobin_index
;
97 struct pa_subscription
*subscription
;
100 struct pa_protocol_native
{
101 struct pa_module
*module
;
103 struct pa_core
*core
;
104 struct pa_socket_server
*server
;
105 struct pa_idxset
*connections
;
106 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
109 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
110 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
);
111 static void sink_input_kill_cb(struct pa_sink_input
*i
);
112 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
);
114 static void request_bytes(struct playback_stream
*s
);
116 static void source_output_kill_cb(struct pa_source_output
*o
);
117 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
119 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
120 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
121 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
122 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
123 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
124 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
125 static void command_set_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
126 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
127 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
128 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
129 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
130 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
131 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
132 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
133 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
134 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
135 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
136 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
137 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
139 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
140 [PA_COMMAND_ERROR
] = { NULL
},
141 [PA_COMMAND_TIMEOUT
] = { NULL
},
142 [PA_COMMAND_REPLY
] = { NULL
},
143 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = { command_create_playback_stream
},
144 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = { command_delete_stream
},
145 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = { command_drain_playback_stream
},
146 [PA_COMMAND_CREATE_RECORD_STREAM
] = { command_create_record_stream
},
147 [PA_COMMAND_DELETE_RECORD_STREAM
] = { command_delete_stream
},
148 [PA_COMMAND_AUTH
] = { command_auth
},
149 [PA_COMMAND_REQUEST
] = { NULL
},
150 [PA_COMMAND_EXIT
] = { command_exit
},
151 [PA_COMMAND_SET_NAME
] = { command_set_name
},
152 [PA_COMMAND_LOOKUP_SINK
] = { command_lookup
},
153 [PA_COMMAND_LOOKUP_SOURCE
] = { command_lookup
},
154 [PA_COMMAND_STAT
] = { command_stat
},
155 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = { command_get_playback_latency
},
156 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = { command_create_upload_stream
},
157 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = { command_delete_stream
},
158 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = { command_finish_upload_stream
},
159 [PA_COMMAND_PLAY_SAMPLE
] = { command_play_sample
},
160 [PA_COMMAND_REMOVE_SAMPLE
] = { command_remove_sample
},
161 [PA_COMMAND_GET_SINK_INFO
] = { command_get_info
},
162 [PA_COMMAND_GET_SOURCE_INFO
] = { command_get_info
},
163 [PA_COMMAND_GET_CLIENT_INFO
] = { command_get_info
},
164 [PA_COMMAND_GET_MODULE_INFO
] = { command_get_info
},
165 [PA_COMMAND_GET_SINK_INPUT_INFO
] = { command_get_info
},
166 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO
] = { command_get_info
},
167 [PA_COMMAND_GET_SAMPLE_INFO
] = { command_get_info
},
168 [PA_COMMAND_GET_SINK_INFO_LIST
] = { command_get_info_list
},
169 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = { command_get_info_list
},
170 [PA_COMMAND_GET_MODULE_INFO_LIST
] = { command_get_info_list
},
171 [PA_COMMAND_GET_CLIENT_INFO_LIST
] = { command_get_info_list
},
172 [PA_COMMAND_GET_SINK_INPUT_INFO_LIST
] = { command_get_info_list
},
173 [PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
] = { command_get_info_list
},
174 [PA_COMMAND_GET_SAMPLE_INFO_LIST
] = { command_get_info_list
},
175 [PA_COMMAND_GET_SERVER_INFO
] = { command_get_server_info
},
176 [PA_COMMAND_SUBSCRIBE
] = { command_subscribe
},
177 [PA_COMMAND_SET_SINK_VOLUME
] = { command_set_volume
},
178 [PA_COMMAND_SET_SINK_INPUT_VOLUME
] = { command_set_volume
},
181 /* structure management */
183 static struct upload_stream
* upload_stream_new(struct connection
*c
, const struct pa_sample_spec
*ss
, const char *name
, size_t length
) {
184 struct upload_stream
*s
;
185 assert(c
&& ss
&& name
&& length
);
187 s
= pa_xmalloc(sizeof(struct upload_stream
));
188 s
->type
= UPLOAD_STREAM
;
190 s
->sample_spec
= *ss
;
191 s
->name
= pa_xstrdup(name
);
193 s
->memchunk
.memblock
= NULL
;
194 s
->memchunk
.index
= 0;
195 s
->memchunk
.length
= 0;
199 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
203 static void upload_stream_free(struct upload_stream
*o
) {
204 assert(o
&& o
->connection
);
206 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
210 if (o
->memchunk
.memblock
)
211 pa_memblock_unref(o
->memchunk
.memblock
);
216 static struct record_stream
* record_stream_new(struct connection
*c
, struct pa_source
*source
, const struct pa_sample_spec
*ss
, const char *name
, size_t maxlength
, size_t fragment_size
) {
217 struct record_stream
*s
;
218 struct pa_source_output
*source_output
;
220 assert(c
&& source
&& ss
&& name
&& maxlength
);
222 if (!(source_output
= pa_source_output_new(source
, name
, ss
)))
225 s
= pa_xmalloc(sizeof(struct record_stream
));
227 s
->source_output
= source_output
;
228 s
->source_output
->push
= source_output_push_cb
;
229 s
->source_output
->kill
= source_output_kill_cb
;
230 s
->source_output
->userdata
= s
;
231 s
->source_output
->owner
= c
->protocol
->module
;
232 s
->source_output
->client
= c
->client
;
234 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0, c
->protocol
->core
->memblock_stat
);
235 assert(s
->memblockq
);
237 s
->fragment_size
= (fragment_size
/base
)*base
;
238 if (!s
->fragment_size
)
239 s
->fragment_size
= base
;
241 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
245 static void record_stream_free(struct record_stream
* r
) {
246 assert(r
&& r
->connection
);
248 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
249 pa_source_output_free(r
->source_output
);
250 pa_memblockq_free(r
->memblockq
);
254 static struct playback_stream
* playback_stream_new(struct connection
*c
, struct pa_sink
*sink
, const struct pa_sample_spec
*ss
, const char *name
,
259 struct playback_stream
*s
;
260 struct pa_sink_input
*sink_input
;
261 assert(c
&& sink
&& ss
&& name
&& maxlength
);
263 if (!(sink_input
= pa_sink_input_new(sink
, name
, ss
)))
266 s
= pa_xmalloc(sizeof(struct playback_stream
));
267 s
->type
= PLAYBACK_STREAM
;
269 s
->sink_input
= sink_input
;
271 s
->sink_input
->peek
= sink_input_peek_cb
;
272 s
->sink_input
->drop
= sink_input_drop_cb
;
273 s
->sink_input
->kill
= sink_input_kill_cb
;
274 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
275 s
->sink_input
->userdata
= s
;
276 s
->sink_input
->owner
= c
->protocol
->module
;
277 s
->sink_input
->client
= c
->client
;
279 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
, c
->protocol
->core
->memblock_stat
);
280 assert(s
->memblockq
);
282 s
->requested_bytes
= 0;
283 s
->drain_request
= 0;
285 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
289 static void playback_stream_free(struct playback_stream
* p
) {
290 assert(p
&& p
->connection
);
292 if (p
->drain_request
)
293 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
295 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
296 pa_sink_input_free(p
->sink_input
);
297 pa_memblockq_free(p
->memblockq
);
301 static void connection_free(struct connection
*c
) {
302 struct record_stream
*r
;
303 struct output_stream
*o
;
304 assert(c
&& c
->protocol
);
306 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
307 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
308 record_stream_free(r
);
309 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
311 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
312 if (o
->type
== PLAYBACK_STREAM
)
313 playback_stream_free((struct playback_stream
*) o
);
315 upload_stream_free((struct upload_stream
*) o
);
316 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
318 pa_pdispatch_unref(c
->pdispatch
);
319 pa_pstream_close(c
->pstream
);
320 pa_pstream_unref(c
->pstream
);
321 pa_client_free(c
->client
);
324 pa_subscription_free(c
->subscription
);
329 static void request_bytes(struct playback_stream
*s
) {
330 struct pa_tagstruct
*t
;
334 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
337 if (l
<= s
->requested_bytes
)
340 l
-= s
->requested_bytes
;
342 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
345 s
->requested_bytes
+= l
;
347 t
= pa_tagstruct_new(NULL
, 0);
349 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
350 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
351 pa_tagstruct_putu32(t
, s
->index
);
352 pa_tagstruct_putu32(t
, l
);
353 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
355 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
358 static void send_memblock(struct connection
*c
) {
360 struct record_stream
*r
;
362 start
= PA_IDXSET_INVALID
;
364 struct pa_memchunk chunk
;
366 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
369 if (start
== PA_IDXSET_INVALID
)
370 start
= c
->rrobin_index
;
371 else if (start
== c
->rrobin_index
)
374 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
375 if (chunk
.length
> r
->fragment_size
)
376 chunk
.length
= r
->fragment_size
;
378 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &chunk
);
379 pa_memblockq_drop(r
->memblockq
, chunk
.length
);
380 pa_memblock_unref(chunk
.memblock
);
387 static void send_playback_stream_killed(struct playback_stream
*p
) {
388 struct pa_tagstruct
*t
;
391 t
= pa_tagstruct_new(NULL
, 0);
393 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
394 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
395 pa_tagstruct_putu32(t
, p
->index
);
396 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
399 static void send_record_stream_killed(struct record_stream
*r
) {
400 struct pa_tagstruct
*t
;
403 t
= pa_tagstruct_new(NULL
, 0);
405 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
406 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
407 pa_tagstruct_putu32(t
, r
->index
);
408 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
412 /*** sinkinput callbacks ***/
414 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
415 struct playback_stream
*s
;
416 assert(i
&& i
->userdata
&& chunk
);
419 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
425 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
) {
426 struct playback_stream
*s
;
427 assert(i
&& i
->userdata
&& length
);
430 pa_memblockq_drop(s
->memblockq
, length
);
433 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
434 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
435 s
->drain_request
= 0;
439 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
440 assert(i
&& i
->userdata
);
441 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
442 playback_stream_free((struct playback_stream
*) i
->userdata
);
445 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
446 struct playback_stream
*s
;
447 assert(i
&& i
->userdata
);
450 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
453 /*** source_output callbacks ***/
455 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
456 struct record_stream
*s
;
457 assert(o
&& o
->userdata
&& chunk
);
460 pa_memblockq_push(s
->memblockq
, chunk
, 0);
461 if (!pa_pstream_is_pending(s
->connection
->pstream
))
462 send_memblock(s
->connection
);
465 static void source_output_kill_cb(struct pa_source_output
*o
) {
466 assert(o
&& o
->userdata
);
467 send_record_stream_killed((struct record_stream
*) o
->userdata
);
468 record_stream_free((struct record_stream
*) o
->userdata
);
471 /*** pdispatch callbacks ***/
473 static void protocol_error(struct connection
*c
) {
474 fprintf(stderr
, __FILE__
": protocol error, kicking client\n");
478 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
479 struct connection
*c
= userdata
;
480 struct playback_stream
*s
;
481 size_t maxlength
, tlength
, prebuf
, minreq
;
483 const char *name
, *sink_name
;
484 struct pa_sample_spec ss
;
485 struct pa_tagstruct
*reply
;
486 struct pa_sink
*sink
;
487 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
489 if (pa_tagstruct_gets(t
, &name
) < 0 ||
490 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
491 pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
492 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
493 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
494 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
495 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
496 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
497 !pa_tagstruct_eof(t
)) {
502 if (!c
->authorized
) {
503 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
507 if (sink_index
!= (uint32_t) -1)
508 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
510 sink
= pa_namereg_get(c
->protocol
->core
, *sink_name
? sink_name
: NULL
, PA_NAMEREG_SINK
, 1);
513 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
517 if (!(s
= playback_stream_new(c
, sink
, &ss
, name
, maxlength
, tlength
, prebuf
, minreq
))) {
518 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
522 reply
= pa_tagstruct_new(NULL
, 0);
524 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
525 pa_tagstruct_putu32(reply
, tag
);
526 pa_tagstruct_putu32(reply
, s
->index
);
527 assert(s
->sink_input
);
528 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
529 pa_pstream_send_tagstruct(c
->pstream
, reply
);
533 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
534 struct connection
*c
= userdata
;
538 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
539 !pa_tagstruct_eof(t
)) {
544 if (!c
->authorized
) {
545 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
549 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
550 struct playback_stream
*s
;
551 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
552 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
556 playback_stream_free(s
);
557 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
558 struct record_stream
*s
;
559 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
560 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
564 record_stream_free(s
);
566 struct upload_stream
*s
;
567 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
568 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
569 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
573 upload_stream_free(s
);
576 pa_pstream_send_simple_ack(c
->pstream
, tag
);
579 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
580 struct connection
*c
= userdata
;
581 struct record_stream
*s
;
582 size_t maxlength
, fragment_size
;
583 uint32_t source_index
;
584 const char *name
, *source_name
;
585 struct pa_sample_spec ss
;
586 struct pa_tagstruct
*reply
;
587 struct pa_source
*source
;
588 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
590 if (pa_tagstruct_gets(t
, &name
) < 0 ||
591 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
592 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
593 pa_tagstruct_gets(t
, &source_name
) < 0 ||
594 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
595 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
596 !pa_tagstruct_eof(t
)) {
601 if (!c
->authorized
) {
602 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
606 if (source_index
!= (uint32_t) -1)
607 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
609 source
= pa_namereg_get(c
->protocol
->core
, *source_name
? source_name
: NULL
, PA_NAMEREG_SOURCE
, 1);
612 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
616 if (!(s
= record_stream_new(c
, source
, &ss
, name
, maxlength
, fragment_size
))) {
617 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
621 reply
= pa_tagstruct_new(NULL
, 0);
623 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
624 pa_tagstruct_putu32(reply
, tag
);
625 pa_tagstruct_putu32(reply
, s
->index
);
626 assert(s
->source_output
);
627 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
628 pa_pstream_send_tagstruct(c
->pstream
, reply
);
631 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
632 struct connection
*c
= userdata
;
635 if (!pa_tagstruct_eof(t
)) {
640 if (!c
->authorized
) {
641 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
645 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
646 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
647 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
651 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
652 struct connection
*c
= userdata
;
656 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
657 !pa_tagstruct_eof(t
)) {
662 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
663 fprintf(stderr
, "protocol-native.c: Denied access to client with invalid authorization key.\n");
664 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
669 pa_pstream_send_simple_ack(c
->pstream
, tag
);
673 static void command_set_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
674 struct connection
*c
= userdata
;
678 if (pa_tagstruct_gets(t
, &name
) < 0 ||
679 !pa_tagstruct_eof(t
)) {
684 pa_client_rename(c
->client
, name
);
685 pa_pstream_send_simple_ack(c
->pstream
, tag
);
689 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
690 struct connection
*c
= userdata
;
692 uint32_t index
= PA_IDXSET_INVALID
;
695 if (pa_tagstruct_gets(t
, &name
) < 0 ||
696 !pa_tagstruct_eof(t
)) {
701 if (!c
->authorized
) {
702 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
706 if (command
== PA_COMMAND_LOOKUP_SINK
) {
707 struct pa_sink
*sink
;
708 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
711 struct pa_source
*source
;
712 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
713 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
714 index
= source
->index
;
717 if (index
== PA_IDXSET_INVALID
)
718 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
720 struct pa_tagstruct
*reply
;
721 reply
= pa_tagstruct_new(NULL
, 0);
723 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
724 pa_tagstruct_putu32(reply
, tag
);
725 pa_tagstruct_putu32(reply
, index
);
726 pa_pstream_send_tagstruct(c
->pstream
, reply
);
730 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
731 struct connection
*c
= userdata
;
733 struct playback_stream
*s
;
736 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
737 !pa_tagstruct_eof(t
)) {
742 if (!c
->authorized
) {
743 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
747 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
748 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
752 s
->drain_request
= 0;
754 pa_memblockq_prebuf_disable(s
->memblockq
);
756 if (!pa_memblockq_is_readable(s
->memblockq
))
757 pa_pstream_send_simple_ack(c
->pstream
, tag
);
759 s
->drain_request
= 1;
762 pa_sink_notify(s
->sink_input
->sink
);
766 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
767 struct connection
*c
= userdata
;
768 struct pa_tagstruct
*reply
;
771 if (!pa_tagstruct_eof(t
)) {
776 if (!c
->authorized
) {
777 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
781 reply
= pa_tagstruct_new(NULL
, 0);
783 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
784 pa_tagstruct_putu32(reply
, tag
);
785 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total
);
786 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->total_size
);
787 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated
);
788 pa_tagstruct_putu32(reply
, c
->protocol
->core
->memblock_stat
->allocated_size
);
789 pa_pstream_send_tagstruct(c
->pstream
, reply
);
792 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
793 struct connection
*c
= userdata
;
794 struct pa_tagstruct
*reply
;
795 struct playback_stream
*s
;
796 uint32_t index
, latency
;
799 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
800 !pa_tagstruct_eof(t
)) {
805 if (!c
->authorized
) {
806 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
810 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
811 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
815 latency
= pa_sink_input_get_latency(s
->sink_input
);
816 reply
= pa_tagstruct_new(NULL
, 0);
818 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
819 pa_tagstruct_putu32(reply
, tag
);
820 pa_tagstruct_putu32(reply
, latency
);
821 pa_pstream_send_tagstruct(c
->pstream
, reply
);
824 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
825 struct connection
*c
= userdata
;
826 struct upload_stream
*s
;
829 struct pa_sample_spec ss
;
830 struct pa_tagstruct
*reply
;
831 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
833 if (pa_tagstruct_gets(t
, &name
) < 0 ||
834 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
835 pa_tagstruct_getu32(t
, &length
) < 0 ||
836 !pa_tagstruct_eof(t
)) {
841 if (!c
->authorized
) {
842 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
846 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
847 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
851 if (!(s
= upload_stream_new(c
, &ss
, name
, length
))) {
852 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
856 reply
= pa_tagstruct_new(NULL
, 0);
858 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
859 pa_tagstruct_putu32(reply
, tag
);
860 pa_tagstruct_putu32(reply
, s
->index
);
861 pa_pstream_send_tagstruct(c
->pstream
, reply
);
863 reply
= pa_tagstruct_new(NULL
, 0);
865 pa_tagstruct_putu32(reply
, PA_COMMAND_REQUEST
);
866 pa_tagstruct_putu32(reply
, (uint32_t) -1); /* tag */
867 pa_tagstruct_putu32(reply
, s
->index
);
868 pa_tagstruct_putu32(reply
, length
);
869 pa_pstream_send_tagstruct(c
->pstream
, reply
);
872 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
873 struct connection
*c
= userdata
;
875 struct upload_stream
*s
;
879 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
880 !pa_tagstruct_eof(t
)) {
885 if (!c
->authorized
) {
886 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
890 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
891 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
895 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->memchunk
, &index
);
896 pa_pstream_send_simple_ack(c
->pstream
, tag
);
897 upload_stream_free(s
);
900 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
901 struct connection
*c
= userdata
;
902 uint32_t sink_index
, volume
;
903 struct pa_sink
*sink
;
904 const char *name
, *sink_name
;
907 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
908 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
909 pa_tagstruct_getu32(t
, &volume
) < 0 ||
910 pa_tagstruct_gets(t
, &name
) < 0 ||
911 !pa_tagstruct_eof(t
)) {
916 if (!c
->authorized
) {
917 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
921 if (sink_index
!= (uint32_t) -1)
922 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
924 sink
= pa_namereg_get(c
->protocol
->core
, *sink_name
? sink_name
: NULL
, PA_NAMEREG_SINK
, 1);
927 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
931 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
932 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
936 pa_pstream_send_simple_ack(c
->pstream
, tag
);
939 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
940 struct connection
*c
= userdata
;
944 if (pa_tagstruct_gets(t
, &name
) < 0 ||
945 !pa_tagstruct_eof(t
)) {
950 if (!c
->authorized
) {
951 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
955 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
956 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
960 pa_pstream_send_simple_ack(c
->pstream
, tag
);
963 static void sink_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink
*sink
) {
965 pa_tagstruct_putu32(t
, sink
->index
);
966 pa_tagstruct_puts(t
, sink
->name
);
967 pa_tagstruct_puts(t
, sink
->description
? sink
->description
: "");
968 pa_tagstruct_put_sample_spec(t
, &sink
->sample_spec
);
969 pa_tagstruct_putu32(t
, sink
->owner
? sink
->owner
->index
: (uint32_t) -1);
970 pa_tagstruct_putu32(t
, sink
->volume
);
971 pa_tagstruct_putu32(t
, sink
->monitor_source
->index
);
972 pa_tagstruct_puts(t
, sink
->monitor_source
->name
);
973 pa_tagstruct_putu32(t
, pa_sink_get_latency(sink
));
976 static void source_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source
*source
) {
978 pa_tagstruct_putu32(t
, source
->index
);
979 pa_tagstruct_puts(t
, source
->name
);
980 pa_tagstruct_puts(t
, source
->description
? source
->description
: "");
981 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
982 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
983 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
984 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: "");
987 static void client_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_client
*client
) {
989 pa_tagstruct_putu32(t
, client
->index
);
990 pa_tagstruct_puts(t
, client
->name
);
991 pa_tagstruct_puts(t
, client
->protocol_name
);
992 pa_tagstruct_putu32(t
, client
->owner
? client
->owner
->index
: (uint32_t) -1);
995 static void module_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_module
*module
) {
997 pa_tagstruct_putu32(t
, module
->index
);
998 pa_tagstruct_puts(t
, module
->name
);
999 pa_tagstruct_puts(t
, module
->argument
? module
->argument
: "");
1000 pa_tagstruct_putu32(t
, module
->n_used
);
1001 pa_tagstruct_putu32(t
, module
->auto_unload
);
1004 static void sink_input_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink_input
*s
) {
1006 pa_tagstruct_putu32(t
, s
->index
);
1007 pa_tagstruct_puts(t
, s
->name
? s
->name
: "");
1008 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1009 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1010 pa_tagstruct_putu32(t
, s
->sink
->index
);
1011 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1012 pa_tagstruct_putu32(t
, s
->volume
);
1013 pa_tagstruct_putu32(t
, pa_sink_input_get_latency(s
));
1016 static void source_output_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source_output
*s
) {
1018 pa_tagstruct_putu32(t
, s
->index
);
1019 pa_tagstruct_puts(t
, s
->name
? s
->name
: "");
1020 pa_tagstruct_putu32(t
, s
->owner
? s
->owner
->index
: (uint32_t) -1);
1021 pa_tagstruct_putu32(t
, s
->client
? s
->client
->index
: (uint32_t) -1);
1022 pa_tagstruct_putu32(t
, s
->source
->index
);
1023 pa_tagstruct_put_sample_spec(t
, &s
->sample_spec
);
1026 static void scache_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_scache_entry
*e
) {
1028 pa_tagstruct_putu32(t
, e
->index
);
1029 pa_tagstruct_puts(t
, e
->name
);
1030 pa_tagstruct_putu32(t
, e
->volume
);
1031 pa_tagstruct_putu32(t
, pa_bytes_to_usec(e
->memchunk
.length
, &e
->sample_spec
));
1032 pa_tagstruct_put_sample_spec(t
, &e
->sample_spec
);
1035 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1036 struct connection
*c
= userdata
;
1038 struct pa_sink
*sink
= NULL
;
1039 struct pa_source
*source
= NULL
;
1040 struct pa_client
*client
= NULL
;
1041 struct pa_module
*module
= NULL
;
1042 struct pa_sink_input
*si
= NULL
;
1043 struct pa_source_output
*so
= NULL
;
1044 struct pa_scache_entry
*sce
= NULL
;
1046 struct pa_tagstruct
*reply
;
1050 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1051 (command
!= PA_COMMAND_GET_CLIENT_INFO
&&
1052 command
!= PA_COMMAND_GET_MODULE_INFO
&&
1053 command
!= PA_COMMAND_GET_SINK_INPUT_INFO
&&
1054 command
!= PA_COMMAND_GET_SOURCE_OUTPUT_INFO
&&
1055 pa_tagstruct_gets(t
, &name
) < 0) ||
1056 !pa_tagstruct_eof(t
)) {
1061 if (!c
->authorized
) {
1062 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1066 if (command
== PA_COMMAND_GET_SINK_INFO
) {
1067 if (index
!= (uint32_t) -1)
1068 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1070 sink
= pa_namereg_get(c
->protocol
->core
, *name
? name
: NULL
, PA_NAMEREG_SINK
, 1);
1071 } else if (command
== PA_COMMAND_GET_SOURCE_INFO
) {
1072 if (index
!= (uint32_t) -1)
1073 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, index
);
1075 source
= pa_namereg_get(c
->protocol
->core
, *name
? name
: NULL
, PA_NAMEREG_SOURCE
, 1);
1076 } else if (command
== PA_COMMAND_GET_CLIENT_INFO
)
1077 client
= pa_idxset_get_by_index(c
->protocol
->core
->clients
, index
);
1078 else if (command
== PA_COMMAND_GET_MODULE_INFO
)
1079 module
= pa_idxset_get_by_index(c
->protocol
->core
->modules
, index
);
1080 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO
)
1081 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1082 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO
)
1083 so
= pa_idxset_get_by_index(c
->protocol
->core
->source_outputs
, index
);
1085 assert(command
== PA_COMMAND_GET_SAMPLE_INFO
&& name
);
1086 if (index
!= (uint32_t) -1)
1087 sce
= pa_idxset_get_by_index(c
->protocol
->core
->scache
, index
);
1089 sce
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SAMPLE
, 0);
1092 if (!sink
&& !source
&& !client
&& !module
&& !si
&& !so
&& !sce
) {
1093 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1097 reply
= pa_tagstruct_new(NULL
, 0);
1099 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1100 pa_tagstruct_putu32(reply
, tag
);
1102 sink_fill_tagstruct(reply
, sink
);
1104 source_fill_tagstruct(reply
, source
);
1106 client_fill_tagstruct(reply
, client
);
1108 module_fill_tagstruct(reply
, module
);
1110 sink_input_fill_tagstruct(reply
, si
);
1112 source_output_fill_tagstruct(reply
, so
);
1114 scache_fill_tagstruct(reply
, sce
);
1115 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1118 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1119 struct connection
*c
= userdata
;
1120 struct pa_idxset
*i
;
1123 struct pa_tagstruct
*reply
;
1126 if (!pa_tagstruct_eof(t
)) {
1131 if (!c
->authorized
) {
1132 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1136 reply
= pa_tagstruct_new(NULL
, 0);
1138 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1139 pa_tagstruct_putu32(reply
, tag
);
1141 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1142 i
= c
->protocol
->core
->sinks
;
1143 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1144 i
= c
->protocol
->core
->sources
;
1145 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1146 i
= c
->protocol
->core
->clients
;
1147 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1148 i
= c
->protocol
->core
->modules
;
1149 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1150 i
= c
->protocol
->core
->sink_inputs
;
1151 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1152 i
= c
->protocol
->core
->source_outputs
;
1154 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1155 i
= c
->protocol
->core
->scache
;
1158 for (p
= pa_idxset_first(i
, &index
); p
; p
= pa_idxset_next(i
, &index
)) {
1159 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1160 sink_fill_tagstruct(reply
, p
);
1161 else if (command
== PA_COMMAND_GET_SOURCE_INFO_LIST
)
1162 source_fill_tagstruct(reply
, p
);
1163 else if (command
== PA_COMMAND_GET_CLIENT_INFO_LIST
)
1164 client_fill_tagstruct(reply
, p
);
1165 else if (command
== PA_COMMAND_GET_MODULE_INFO_LIST
)
1166 module_fill_tagstruct(reply
, p
);
1167 else if (command
== PA_COMMAND_GET_SINK_INPUT_INFO_LIST
)
1168 sink_input_fill_tagstruct(reply
, p
);
1169 else if (command
== PA_COMMAND_GET_SOURCE_OUTPUT_INFO_LIST
)
1170 source_output_fill_tagstruct(reply
, p
);
1172 assert(command
== PA_COMMAND_GET_SAMPLE_INFO_LIST
);
1173 scache_fill_tagstruct(reply
, p
);
1177 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1180 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1181 struct connection
*c
= userdata
;
1182 struct pa_tagstruct
*reply
;
1186 if (!pa_tagstruct_eof(t
)) {
1191 if (!c
->authorized
) {
1192 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1196 reply
= pa_tagstruct_new(NULL
, 0);
1198 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1199 pa_tagstruct_putu32(reply
, tag
);
1200 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1201 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1202 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1203 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
1204 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1205 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1208 static void subscription_cb(struct pa_core
*core
, enum pa_subscription_event_type e
, uint32_t index
, void *userdata
) {
1209 struct pa_tagstruct
*t
;
1210 struct connection
*c
= userdata
;
1213 t
= pa_tagstruct_new(NULL
, 0);
1215 pa_tagstruct_putu32(t
, PA_COMMAND_SUBSCRIBE_EVENT
);
1216 pa_tagstruct_putu32(t
, (uint32_t) -1);
1217 pa_tagstruct_putu32(t
, e
);
1218 pa_tagstruct_putu32(t
, index
);
1219 pa_pstream_send_tagstruct(c
->pstream
, t
);
1222 static void command_subscribe(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1223 struct connection
*c
= userdata
;
1224 enum pa_subscription_mask m
;
1227 if (pa_tagstruct_getu32(t
, &m
) < 0 ||
1228 !pa_tagstruct_eof(t
)) {
1233 if (!c
->authorized
) {
1234 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1238 if (c
->subscription
)
1239 pa_subscription_free(c
->subscription
);
1242 c
->subscription
= pa_subscription_new(c
->protocol
->core
, m
, subscription_cb
, c
);
1243 assert(c
->subscription
);
1245 c
->subscription
= NULL
;
1247 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1250 static void command_set_volume(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1251 struct connection
*c
= userdata
;
1252 uint32_t index
, volume
;
1253 struct pa_sink
*sink
= NULL
;
1254 struct pa_sink_input
*si
= NULL
;
1255 const char *name
= NULL
;
1258 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
1259 (command
== PA_COMMAND_SET_SINK_VOLUME
&& pa_tagstruct_gets(t
, &name
) < 0) ||
1260 pa_tagstruct_getu32(t
, &volume
) ||
1261 !pa_tagstruct_eof(t
)) {
1266 if (!c
->authorized
) {
1267 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1271 if (command
== PA_COMMAND_SET_SINK_VOLUME
) {
1272 if (index
!= (uint32_t) -1)
1273 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
1275 sink
= pa_namereg_get(c
->protocol
->core
, *name
? name
: NULL
, PA_NAMEREG_SINK
, 1);
1277 assert(command
== PA_COMMAND_SET_SINK_INPUT_VOLUME
);
1278 si
= pa_idxset_get_by_index(c
->protocol
->core
->sink_inputs
, index
);
1282 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
1287 pa_sink_set_volume(sink
, volume
);
1289 pa_sink_input_set_volume(si
, volume
);
1291 pa_pstream_send_simple_ack(c
->pstream
, tag
);
1295 /*** pstream callbacks ***/
1297 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
1298 struct connection
*c
= userdata
;
1299 assert(p
&& packet
&& packet
->data
&& c
);
1301 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1302 fprintf(stderr
, "protocol-native: invalid packet.\n");
1307 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, int32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
1308 struct connection
*c
= userdata
;
1309 struct output_stream
*stream
;
1310 assert(p
&& chunk
&& userdata
);
1312 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1313 fprintf(stderr
, "protocol-native: client sent block for invalid stream.\n");
1318 if (stream
->type
== PLAYBACK_STREAM
) {
1319 struct playback_stream
*p
= (struct playback_stream
*) stream
;
1320 if (chunk
->length
>= p
->requested_bytes
)
1321 p
->requested_bytes
= 0;
1323 p
->requested_bytes
-= chunk
->length
;
1325 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
1326 assert(p
->sink_input
);
1327 pa_sink_notify(p
->sink_input
->sink
);
1328 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
1330 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1332 assert(u
->type
== UPLOAD_STREAM
);
1334 if (!u
->memchunk
.memblock
) {
1335 if (u
->length
== chunk
->length
) {
1336 u
->memchunk
= *chunk
;
1337 pa_memblock_ref(u
->memchunk
.memblock
);
1339 fprintf(stderr
, "COPY\n");
1341 u
->memchunk
.memblock
= pa_memblock_new(u
->length
, c
->protocol
->core
->memblock_stat
);
1342 u
->memchunk
.index
= u
->memchunk
.length
= 0;
1346 assert(u
->memchunk
.memblock
);
1349 if (l
> chunk
->length
)
1353 memcpy(u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
, chunk
->memblock
->data
+chunk
->index
, l
);
1354 u
->memchunk
.length
+= l
;
1360 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
1361 struct connection
*c
= userdata
;
1365 fprintf(stderr
, "protocol-native: connection died.\n");
1369 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1370 struct connection
*c
= userdata
;
1376 /*** client callbacks ***/
1378 static void client_kill_cb(struct pa_client
*c
) {
1379 assert(c
&& c
->userdata
);
1380 connection_free(c
->userdata
);
1383 /*** socket server callbacks ***/
1385 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1386 struct pa_protocol_native
*p
= userdata
;
1387 struct connection
*c
;
1388 assert(s
&& io
&& p
);
1390 c
= pa_xmalloc(sizeof(struct connection
));
1391 c
->authorized
= p
->public;
1394 c
->client
= pa_client_new(p
->core
, "NATIVE", "Client");
1396 c
->client
->kill
= client_kill_cb
;
1397 c
->client
->userdata
= c
;
1398 c
->client
->owner
= p
->module
;
1400 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
, p
->core
->memblock_stat
);
1403 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
1404 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
1405 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
1406 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
1408 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
1409 assert(c
->pdispatch
);
1411 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
1412 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
1413 assert(c
->record_streams
&& c
->output_streams
);
1415 c
->rrobin_index
= PA_IDXSET_INVALID
;
1416 c
->subscription
= NULL
;
1418 pa_idxset_put(p
->connections
, c
, NULL
);
1421 /*** module entry points ***/
1423 struct pa_protocol_native
* pa_protocol_native_new(struct pa_core
*core
, struct pa_socket_server
*server
, struct pa_module
*m
, struct pa_modargs
*ma
) {
1424 struct pa_protocol_native
*p
;
1426 assert(core
&& server
&& ma
);
1428 if (pa_modargs_get_value_u32(ma
, "public", &public) < 0) {
1429 fprintf(stderr
, __FILE__
": public= expects numeric argument.\n");
1433 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
1435 if (pa_authkey_load_from_home(pa_modargs_get_value(ma
, "cookie", PA_NATIVE_COOKIE_FILE
), p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0) {
1444 p
->connections
= pa_idxset_new(NULL
, NULL
);
1445 assert(p
->connections
);
1447 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1452 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
1453 struct connection
*c
;
1456 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1458 pa_idxset_free(p
->connections
, NULL
, NULL
);
1459 pa_socket_server_unref(p
->server
);