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"
47 struct pa_protocol_native
;
49 struct record_stream
{
50 struct connection
*connection
;
52 struct pa_source_output
*source_output
;
53 struct pa_memblockq
*memblockq
;
57 struct playback_stream
{
59 struct connection
*connection
;
61 struct pa_sink_input
*sink_input
;
62 struct pa_memblockq
*memblockq
;
63 size_t requested_bytes
;
68 struct upload_stream
{
70 struct connection
*connection
;
72 struct pa_memchunk memchunk
;
75 struct pa_sample_spec sample_spec
;
78 struct output_stream
{
89 struct pa_protocol_native
*protocol
;
90 struct pa_client
*client
;
91 struct pa_pstream
*pstream
;
92 struct pa_pdispatch
*pdispatch
;
93 struct pa_idxset
*record_streams
, *output_streams
;
94 uint32_t rrobin_index
;
97 struct pa_protocol_native
{
98 struct pa_module
*module
;
100 struct pa_core
*core
;
101 struct pa_socket_server
*server
;
102 struct pa_idxset
*connections
;
103 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
106 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
107 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
);
108 static void sink_input_kill_cb(struct pa_sink_input
*i
);
109 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
);
111 static void request_bytes(struct playback_stream
*s
);
113 static void source_output_kill_cb(struct pa_source_output
*o
);
114 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
116 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
117 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
118 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
119 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
120 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
121 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
122 static void command_set_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
123 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
124 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
125 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
126 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
127 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
128 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
129 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
131 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
132 [PA_COMMAND_ERROR
] = { NULL
},
133 [PA_COMMAND_TIMEOUT
] = { NULL
},
134 [PA_COMMAND_REPLY
] = { NULL
},
135 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = { command_create_playback_stream
},
136 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = { command_delete_stream
},
137 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = { command_drain_playback_stream
},
138 [PA_COMMAND_CREATE_RECORD_STREAM
] = { command_create_record_stream
},
139 [PA_COMMAND_DELETE_RECORD_STREAM
] = { command_delete_stream
},
140 [PA_COMMAND_AUTH
] = { command_auth
},
141 [PA_COMMAND_REQUEST
] = { NULL
},
142 [PA_COMMAND_EXIT
] = { command_exit
},
143 [PA_COMMAND_SET_NAME
] = { command_set_name
},
144 [PA_COMMAND_LOOKUP_SINK
] = { command_lookup
},
145 [PA_COMMAND_LOOKUP_SOURCE
] = { command_lookup
},
146 [PA_COMMAND_STAT
] = { command_stat
},
147 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = { command_get_playback_latency
},
148 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = { command_create_upload_stream
},
149 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = { command_delete_stream
},
150 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = { command_finish_upload_stream
},
151 [PA_COMMAND_PLAY_SAMPLE
] = { command_play_sample
},
152 [PA_COMMAND_REMOVE_SAMPLE
] = { command_remove_sample
},
155 /* structure management */
157 static struct upload_stream
* upload_stream_new(struct connection
*c
, const struct pa_sample_spec
*ss
, const char *name
, size_t length
) {
158 struct upload_stream
*s
;
159 assert(c
&& ss
&& name
&& length
);
161 s
= pa_xmalloc(sizeof(struct upload_stream
));
162 s
->type
= UPLOAD_STREAM
;
164 s
->sample_spec
= *ss
;
165 s
->name
= pa_xstrdup(name
);
167 s
->memchunk
.memblock
= NULL
;
168 s
->memchunk
.index
= 0;
169 s
->memchunk
.length
= 0;
173 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
177 static void upload_stream_free(struct upload_stream
*o
) {
178 assert(o
&& o
->connection
);
180 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
184 if (o
->memchunk
.memblock
)
185 pa_memblock_unref(o
->memchunk
.memblock
);
190 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
) {
191 struct record_stream
*s
;
192 struct pa_source_output
*source_output
;
194 assert(c
&& source
&& ss
&& name
&& maxlength
);
196 if (!(source_output
= pa_source_output_new(source
, name
, ss
)))
199 s
= pa_xmalloc(sizeof(struct record_stream
));
201 s
->source_output
= source_output
;
202 s
->source_output
->push
= source_output_push_cb
;
203 s
->source_output
->kill
= source_output_kill_cb
;
204 s
->source_output
->userdata
= s
;
205 s
->source_output
->owner
= c
->protocol
->module
;
206 s
->source_output
->client
= c
->client
;
208 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0);
209 assert(s
->memblockq
);
211 s
->fragment_size
= (fragment_size
/base
)*base
;
212 if (!s
->fragment_size
)
213 s
->fragment_size
= base
;
215 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
219 static void record_stream_free(struct record_stream
* r
) {
220 assert(r
&& r
->connection
);
222 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
223 pa_source_output_free(r
->source_output
);
224 pa_memblockq_free(r
->memblockq
);
228 static struct playback_stream
* playback_stream_new(struct connection
*c
, struct pa_sink
*sink
, const struct pa_sample_spec
*ss
, const char *name
,
233 struct playback_stream
*s
;
234 struct pa_sink_input
*sink_input
;
235 assert(c
&& sink
&& ss
&& name
&& maxlength
);
237 if (!(sink_input
= pa_sink_input_new(sink
, name
, ss
)))
240 s
= pa_xmalloc(sizeof(struct playback_stream
));
241 s
->type
= PLAYBACK_STREAM
;
243 s
->sink_input
= sink_input
;
245 s
->sink_input
->peek
= sink_input_peek_cb
;
246 s
->sink_input
->drop
= sink_input_drop_cb
;
247 s
->sink_input
->kill
= sink_input_kill_cb
;
248 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
249 s
->sink_input
->userdata
= s
;
250 s
->sink_input
->owner
= c
->protocol
->module
;
251 s
->sink_input
->client
= c
->client
;
253 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
);
254 assert(s
->memblockq
);
256 s
->requested_bytes
= 0;
257 s
->drain_request
= 0;
259 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
263 static void playback_stream_free(struct playback_stream
* p
) {
264 assert(p
&& p
->connection
);
266 if (p
->drain_request
)
267 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
269 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
270 pa_sink_input_free(p
->sink_input
);
271 pa_memblockq_free(p
->memblockq
);
275 static void connection_free(struct connection
*c
) {
276 struct record_stream
*r
;
277 struct output_stream
*o
;
278 assert(c
&& c
->protocol
);
280 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
281 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
282 record_stream_free(r
);
283 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
285 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
286 if (o
->type
== PLAYBACK_STREAM
)
287 playback_stream_free((struct playback_stream
*) o
);
289 upload_stream_free((struct upload_stream
*) o
);
290 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
292 pa_pdispatch_free(c
->pdispatch
);
293 pa_pstream_free(c
->pstream
);
294 pa_client_free(c
->client
);
298 static void request_bytes(struct playback_stream
*s
) {
299 struct pa_tagstruct
*t
;
303 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
306 if (l
<= s
->requested_bytes
)
309 l
-= s
->requested_bytes
;
311 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
314 s
->requested_bytes
+= l
;
316 t
= pa_tagstruct_new(NULL
, 0);
318 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
319 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
320 pa_tagstruct_putu32(t
, s
->index
);
321 pa_tagstruct_putu32(t
, l
);
322 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
324 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
327 static void send_memblock(struct connection
*c
) {
329 struct record_stream
*r
;
331 start
= PA_IDXSET_INVALID
;
333 struct pa_memchunk chunk
;
335 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
338 if (start
== PA_IDXSET_INVALID
)
339 start
= c
->rrobin_index
;
340 else if (start
== c
->rrobin_index
)
343 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
344 if (chunk
.length
> r
->fragment_size
)
345 chunk
.length
= r
->fragment_size
;
347 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &chunk
);
348 pa_memblockq_drop(r
->memblockq
, chunk
.length
);
349 pa_memblock_unref(chunk
.memblock
);
356 static void send_playback_stream_killed(struct playback_stream
*p
) {
357 struct pa_tagstruct
*t
;
360 t
= pa_tagstruct_new(NULL
, 0);
362 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
363 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
364 pa_tagstruct_putu32(t
, p
->index
);
365 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
368 static void send_record_stream_killed(struct record_stream
*r
) {
369 struct pa_tagstruct
*t
;
372 t
= pa_tagstruct_new(NULL
, 0);
374 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
375 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
376 pa_tagstruct_putu32(t
, r
->index
);
377 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
381 /*** sinkinput callbacks ***/
383 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
384 struct playback_stream
*s
;
385 assert(i
&& i
->userdata
&& chunk
);
388 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
394 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
) {
395 struct playback_stream
*s
;
396 assert(i
&& i
->userdata
&& length
);
399 pa_memblockq_drop(s
->memblockq
, length
);
402 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
403 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
404 s
->drain_request
= 0;
408 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
409 assert(i
&& i
->userdata
);
410 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
411 playback_stream_free((struct playback_stream
*) i
->userdata
);
414 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
415 struct playback_stream
*s
;
416 assert(i
&& i
->userdata
);
419 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
422 /*** source_output callbacks ***/
424 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
425 struct record_stream
*s
;
426 assert(o
&& o
->userdata
&& chunk
);
429 pa_memblockq_push(s
->memblockq
, chunk
, 0);
430 if (!pa_pstream_is_pending(s
->connection
->pstream
))
431 send_memblock(s
->connection
);
434 static void source_output_kill_cb(struct pa_source_output
*o
) {
435 assert(o
&& o
->userdata
);
436 send_record_stream_killed((struct record_stream
*) o
->userdata
);
437 record_stream_free((struct record_stream
*) o
->userdata
);
440 /*** pdispatch callbacks ***/
442 static void protocol_error(struct connection
*c
) {
443 fprintf(stderr
, __FILE__
": protocol error, kicking client\n");
447 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
448 struct connection
*c
= userdata
;
449 struct playback_stream
*s
;
450 size_t maxlength
, tlength
, prebuf
, minreq
;
452 const char *name
, *sink_name
;
453 struct pa_sample_spec ss
;
454 struct pa_tagstruct
*reply
;
455 struct pa_sink
*sink
;
456 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
458 if (pa_tagstruct_gets(t
, &name
) < 0 ||
459 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
460 pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
461 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
462 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
463 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
464 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
465 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
466 !pa_tagstruct_eof(t
)) {
471 if (!c
->authorized
) {
472 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
476 if (sink_index
!= (uint32_t) -1)
477 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
479 sink
= pa_namereg_get(c
->protocol
->core
, *sink_name
? sink_name
: NULL
, PA_NAMEREG_SINK
, 1);
482 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
486 if (!(s
= playback_stream_new(c
, sink
, &ss
, name
, maxlength
, tlength
, prebuf
, minreq
))) {
487 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
491 reply
= pa_tagstruct_new(NULL
, 0);
493 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
494 pa_tagstruct_putu32(reply
, tag
);
495 pa_tagstruct_putu32(reply
, s
->index
);
496 assert(s
->sink_input
);
497 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
498 pa_pstream_send_tagstruct(c
->pstream
, reply
);
502 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
503 struct connection
*c
= userdata
;
507 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
508 !pa_tagstruct_eof(t
)) {
513 if (!c
->authorized
) {
514 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
518 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
519 struct playback_stream
*s
;
520 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
521 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
525 playback_stream_free(s
);
526 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
527 struct record_stream
*s
;
528 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
529 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
533 record_stream_free(s
);
535 struct upload_stream
*s
;
536 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
537 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
538 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
542 upload_stream_free(s
);
545 pa_pstream_send_simple_ack(c
->pstream
, tag
);
548 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
549 struct connection
*c
= userdata
;
550 struct record_stream
*s
;
551 size_t maxlength
, fragment_size
;
552 uint32_t source_index
;
553 const char *name
, *source_name
;
554 struct pa_sample_spec ss
;
555 struct pa_tagstruct
*reply
;
556 struct pa_source
*source
;
557 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
559 if (pa_tagstruct_gets(t
, &name
) < 0 ||
560 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
561 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
562 pa_tagstruct_gets(t
, &source_name
) < 0 ||
563 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
564 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
565 !pa_tagstruct_eof(t
)) {
570 if (!c
->authorized
) {
571 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
575 if (source_index
!= (uint32_t) -1)
576 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
578 source
= pa_namereg_get(c
->protocol
->core
, *source_name
? source_name
: NULL
, PA_NAMEREG_SOURCE
, 1);
581 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
585 if (!(s
= record_stream_new(c
, source
, &ss
, name
, maxlength
, fragment_size
))) {
586 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
590 reply
= pa_tagstruct_new(NULL
, 0);
592 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
593 pa_tagstruct_putu32(reply
, tag
);
594 pa_tagstruct_putu32(reply
, s
->index
);
595 assert(s
->source_output
);
596 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
597 pa_pstream_send_tagstruct(c
->pstream
, reply
);
600 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
601 struct connection
*c
= userdata
;
604 if (!pa_tagstruct_eof(t
)) {
609 if (!c
->authorized
) {
610 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
614 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
615 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
616 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
620 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
621 struct connection
*c
= userdata
;
625 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
626 !pa_tagstruct_eof(t
)) {
631 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
632 fprintf(stderr
, "protocol-native.c: Denied access to client with invalid authorization key.\n");
633 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
638 pa_pstream_send_simple_ack(c
->pstream
, tag
);
642 static void command_set_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
643 struct connection
*c
= userdata
;
647 if (pa_tagstruct_gets(t
, &name
) < 0 ||
648 !pa_tagstruct_eof(t
)) {
653 pa_client_rename(c
->client
, name
);
654 pa_pstream_send_simple_ack(c
->pstream
, tag
);
658 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
659 struct connection
*c
= userdata
;
661 uint32_t index
= PA_IDXSET_INVALID
;
664 if (pa_tagstruct_gets(t
, &name
) < 0 ||
665 !pa_tagstruct_eof(t
)) {
670 if (!c
->authorized
) {
671 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
675 if (command
== PA_COMMAND_LOOKUP_SINK
) {
676 struct pa_sink
*sink
;
677 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
680 struct pa_source
*source
;
681 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
682 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
683 index
= source
->index
;
686 if (index
== PA_IDXSET_INVALID
)
687 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
689 struct pa_tagstruct
*reply
;
690 reply
= pa_tagstruct_new(NULL
, 0);
692 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
693 pa_tagstruct_putu32(reply
, tag
);
694 pa_tagstruct_putu32(reply
, index
);
695 pa_pstream_send_tagstruct(c
->pstream
, reply
);
699 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
700 struct connection
*c
= userdata
;
702 struct playback_stream
*s
;
705 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
706 !pa_tagstruct_eof(t
)) {
711 if (!c
->authorized
) {
712 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
716 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
717 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
721 s
->drain_request
= 0;
723 if (!pa_memblockq_is_readable(s
->memblockq
))
724 pa_pstream_send_simple_ack(c
->pstream
, tag
);
726 s
->drain_request
= 1;
731 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
732 struct connection
*c
= userdata
;
734 struct pa_tagstruct
*reply
;
736 if (!pa_tagstruct_eof(t
)) {
741 if (!c
->authorized
) {
742 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
746 reply
= pa_tagstruct_new(NULL
, 0);
748 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
749 pa_tagstruct_putu32(reply
, tag
);
750 pa_tagstruct_putu32(reply
, pa_memblock_get_count());
751 pa_tagstruct_putu32(reply
, pa_memblock_get_total());
752 pa_pstream_send_tagstruct(c
->pstream
, reply
);
755 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
756 struct connection
*c
= userdata
;
758 struct pa_tagstruct
*reply
;
759 struct playback_stream
*s
;
760 uint32_t index
, latency
;
762 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
763 !pa_tagstruct_eof(t
)) {
768 if (!c
->authorized
) {
769 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
773 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
774 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
778 latency
= pa_sink_input_get_latency(s
->sink_input
);
779 reply
= pa_tagstruct_new(NULL
, 0);
781 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
782 pa_tagstruct_putu32(reply
, tag
);
783 pa_tagstruct_putu32(reply
, latency
);
784 pa_pstream_send_tagstruct(c
->pstream
, reply
);
787 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
788 struct connection
*c
= userdata
;
789 struct upload_stream
*s
;
792 struct pa_sample_spec ss
;
793 struct pa_tagstruct
*reply
;
794 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
796 if (pa_tagstruct_gets(t
, &name
) < 0 ||
797 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
798 pa_tagstruct_getu32(t
, &length
) < 0 ||
799 !pa_tagstruct_eof(t
)) {
804 if (!c
->authorized
) {
805 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
809 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
810 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
814 if (!(s
= upload_stream_new(c
, &ss
, name
, length
))) {
815 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
819 reply
= pa_tagstruct_new(NULL
, 0);
821 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
822 pa_tagstruct_putu32(reply
, tag
);
823 pa_tagstruct_putu32(reply
, s
->index
);
824 pa_pstream_send_tagstruct(c
->pstream
, reply
);
826 reply
= pa_tagstruct_new(NULL
, 0);
828 pa_tagstruct_putu32(reply
, PA_COMMAND_REQUEST
);
829 pa_tagstruct_putu32(reply
, (uint32_t) -1); /* tag */
830 pa_tagstruct_putu32(reply
, s
->index
);
831 pa_tagstruct_putu32(reply
, length
);
832 pa_pstream_send_tagstruct(c
->pstream
, reply
);
835 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
836 struct connection
*c
= userdata
;
838 struct upload_stream
*s
;
842 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
843 !pa_tagstruct_eof(t
)) {
848 if (!c
->authorized
) {
849 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
853 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
854 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
858 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->memchunk
, &index
);
859 pa_pstream_send_simple_ack(c
->pstream
, tag
);
860 upload_stream_free(s
);
863 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
864 struct connection
*c
= userdata
;
865 uint32_t sink_index
, volume
;
866 struct pa_sink
*sink
;
867 const char *name
, *sink_name
;
870 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
871 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
872 pa_tagstruct_getu32(t
, &volume
) < 0 ||
873 pa_tagstruct_gets(t
, &name
) < 0 ||
874 !pa_tagstruct_eof(t
)) {
879 if (!c
->authorized
) {
880 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
884 if (sink_index
!= (uint32_t) -1)
885 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
887 sink
= pa_namereg_get(c
->protocol
->core
, *sink_name
? sink_name
: NULL
, PA_NAMEREG_SINK
, 1);
890 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
894 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
895 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
899 pa_pstream_send_simple_ack(c
->pstream
, tag
);
902 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
903 struct connection
*c
= userdata
;
907 if (pa_tagstruct_gets(t
, &name
) < 0 ||
908 !pa_tagstruct_eof(t
)) {
913 if (!c
->authorized
) {
914 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
918 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
919 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
923 pa_pstream_send_simple_ack(c
->pstream
, tag
);
926 /*** pstream callbacks ***/
928 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
929 struct connection
*c
= userdata
;
930 assert(p
&& packet
&& packet
->data
&& c
);
932 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
933 fprintf(stderr
, "protocol-native: invalid packet.\n");
938 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, int32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
939 struct connection
*c
= userdata
;
940 struct output_stream
*stream
;
941 assert(p
&& chunk
&& userdata
);
943 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
944 fprintf(stderr
, "protocol-native: client sent block for invalid stream.\n");
949 if (stream
->type
== PLAYBACK_STREAM
) {
950 struct playback_stream
*p
= (struct playback_stream
*) stream
;
951 if (chunk
->length
>= p
->requested_bytes
)
952 p
->requested_bytes
= 0;
954 p
->requested_bytes
-= chunk
->length
;
956 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
957 assert(p
->sink_input
);
958 pa_sink_notify(p
->sink_input
->sink
);
959 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
961 struct upload_stream
*u
= (struct upload_stream
*) stream
;
963 assert(u
->type
== UPLOAD_STREAM
);
965 if (!u
->memchunk
.memblock
) {
966 if (u
->length
== chunk
->length
) {
967 u
->memchunk
= *chunk
;
968 pa_memblock_ref(u
->memchunk
.memblock
);
970 fprintf(stderr
, "COPY\n");
972 u
->memchunk
.memblock
= pa_memblock_new(u
->length
);
973 u
->memchunk
.index
= u
->memchunk
.length
= 0;
977 assert(u
->memchunk
.memblock
);
980 if (l
> chunk
->length
)
984 memcpy(u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
, chunk
->memblock
->data
+chunk
->index
, l
);
985 u
->memchunk
.length
+= l
;
991 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
992 struct connection
*c
= userdata
;
996 fprintf(stderr
, "protocol-native: connection died.\n");
1000 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1001 struct connection
*c
= userdata
;
1007 /*** client callbacks ***/
1009 static void client_kill_cb(struct pa_client
*c
) {
1010 assert(c
&& c
->userdata
);
1011 connection_free(c
->userdata
);
1014 /*** socket server callbacks ***/
1016 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1017 struct pa_protocol_native
*p
= userdata
;
1018 struct connection
*c
;
1019 assert(s
&& io
&& p
);
1021 c
= pa_xmalloc(sizeof(struct connection
));
1022 c
->authorized
= p
->public;
1025 c
->client
= pa_client_new(p
->core
, "NATIVE", "Client");
1027 c
->client
->kill
= client_kill_cb
;
1028 c
->client
->userdata
= c
;
1029 c
->client
->owner
= p
->module
;
1031 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
);
1034 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
1035 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
1036 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
1037 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
1039 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
1040 assert(c
->pdispatch
);
1042 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
1043 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
1044 assert(c
->record_streams
&& c
->output_streams
);
1046 c
->rrobin_index
= PA_IDXSET_INVALID
;
1048 pa_idxset_put(p
->connections
, c
, NULL
);
1051 /*** module entry points ***/
1053 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
) {
1054 struct pa_protocol_native
*p
;
1056 assert(core
&& server
&& ma
);
1058 if (pa_modargs_get_value_u32(ma
, "public", &public) < 0) {
1059 fprintf(stderr
, __FILE__
": public= expects numeric argument.\n");
1063 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
1065 if (pa_authkey_load_from_home(pa_modargs_get_value(ma
, "cookie", PA_NATIVE_COOKIE_FILE
), p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0) {
1074 p
->connections
= pa_idxset_new(NULL
, NULL
);
1075 assert(p
->connections
);
1077 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1082 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
1083 struct connection
*c
;
1086 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1088 pa_idxset_free(p
->connections
, NULL
, NULL
);
1089 pa_socket_server_free(p
->server
);