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"
48 struct pa_protocol_native
;
50 struct record_stream
{
51 struct connection
*connection
;
53 struct pa_source_output
*source_output
;
54 struct pa_memblockq
*memblockq
;
58 struct playback_stream
{
60 struct connection
*connection
;
62 struct pa_sink_input
*sink_input
;
63 struct pa_memblockq
*memblockq
;
64 size_t requested_bytes
;
69 struct upload_stream
{
71 struct connection
*connection
;
73 struct pa_memchunk memchunk
;
76 struct pa_sample_spec sample_spec
;
79 struct output_stream
{
90 struct pa_protocol_native
*protocol
;
91 struct pa_client
*client
;
92 struct pa_pstream
*pstream
;
93 struct pa_pdispatch
*pdispatch
;
94 struct pa_idxset
*record_streams
, *output_streams
;
95 uint32_t rrobin_index
;
98 struct pa_protocol_native
{
99 struct pa_module
*module
;
101 struct pa_core
*core
;
102 struct pa_socket_server
*server
;
103 struct pa_idxset
*connections
;
104 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
107 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
);
108 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
);
109 static void sink_input_kill_cb(struct pa_sink_input
*i
);
110 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
);
112 static void request_bytes(struct playback_stream
*s
);
114 static void source_output_kill_cb(struct pa_source_output
*o
);
115 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
);
117 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
118 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
119 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
120 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
121 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
122 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
123 static void command_set_name(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
124 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
125 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
126 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
127 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
128 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
129 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
130 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
131 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
132 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
133 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
135 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
136 [PA_COMMAND_ERROR
] = { NULL
},
137 [PA_COMMAND_TIMEOUT
] = { NULL
},
138 [PA_COMMAND_REPLY
] = { NULL
},
139 [PA_COMMAND_CREATE_PLAYBACK_STREAM
] = { command_create_playback_stream
},
140 [PA_COMMAND_DELETE_PLAYBACK_STREAM
] = { command_delete_stream
},
141 [PA_COMMAND_DRAIN_PLAYBACK_STREAM
] = { command_drain_playback_stream
},
142 [PA_COMMAND_CREATE_RECORD_STREAM
] = { command_create_record_stream
},
143 [PA_COMMAND_DELETE_RECORD_STREAM
] = { command_delete_stream
},
144 [PA_COMMAND_AUTH
] = { command_auth
},
145 [PA_COMMAND_REQUEST
] = { NULL
},
146 [PA_COMMAND_EXIT
] = { command_exit
},
147 [PA_COMMAND_SET_NAME
] = { command_set_name
},
148 [PA_COMMAND_LOOKUP_SINK
] = { command_lookup
},
149 [PA_COMMAND_LOOKUP_SOURCE
] = { command_lookup
},
150 [PA_COMMAND_STAT
] = { command_stat
},
151 [PA_COMMAND_GET_PLAYBACK_LATENCY
] = { command_get_playback_latency
},
152 [PA_COMMAND_CREATE_UPLOAD_STREAM
] = { command_create_upload_stream
},
153 [PA_COMMAND_DELETE_UPLOAD_STREAM
] = { command_delete_stream
},
154 [PA_COMMAND_FINISH_UPLOAD_STREAM
] = { command_finish_upload_stream
},
155 [PA_COMMAND_PLAY_SAMPLE
] = { command_play_sample
},
156 [PA_COMMAND_REMOVE_SAMPLE
] = { command_remove_sample
},
157 [PA_COMMAND_GET_SINK_INFO
] = { command_get_info
},
158 [PA_COMMAND_GET_SOURCE_INFO
] = { command_get_info
},
159 [PA_COMMAND_GET_SINK_INFO_LIST
] = { command_get_info_list
},
160 [PA_COMMAND_GET_SOURCE_INFO_LIST
] = { command_get_info_list
},
161 [PA_COMMAND_GET_SERVER_INFO
] = { command_get_server_info
},
164 /* structure management */
166 static struct upload_stream
* upload_stream_new(struct connection
*c
, const struct pa_sample_spec
*ss
, const char *name
, size_t length
) {
167 struct upload_stream
*s
;
168 assert(c
&& ss
&& name
&& length
);
170 s
= pa_xmalloc(sizeof(struct upload_stream
));
171 s
->type
= UPLOAD_STREAM
;
173 s
->sample_spec
= *ss
;
174 s
->name
= pa_xstrdup(name
);
176 s
->memchunk
.memblock
= NULL
;
177 s
->memchunk
.index
= 0;
178 s
->memchunk
.length
= 0;
182 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
186 static void upload_stream_free(struct upload_stream
*o
) {
187 assert(o
&& o
->connection
);
189 pa_idxset_remove_by_data(o
->connection
->output_streams
, o
, NULL
);
193 if (o
->memchunk
.memblock
)
194 pa_memblock_unref(o
->memchunk
.memblock
);
199 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
) {
200 struct record_stream
*s
;
201 struct pa_source_output
*source_output
;
203 assert(c
&& source
&& ss
&& name
&& maxlength
);
205 if (!(source_output
= pa_source_output_new(source
, name
, ss
)))
208 s
= pa_xmalloc(sizeof(struct record_stream
));
210 s
->source_output
= source_output
;
211 s
->source_output
->push
= source_output_push_cb
;
212 s
->source_output
->kill
= source_output_kill_cb
;
213 s
->source_output
->userdata
= s
;
214 s
->source_output
->owner
= c
->protocol
->module
;
215 s
->source_output
->client
= c
->client
;
217 s
->memblockq
= pa_memblockq_new(maxlength
, 0, base
= pa_frame_size(ss
), 0, 0);
218 assert(s
->memblockq
);
220 s
->fragment_size
= (fragment_size
/base
)*base
;
221 if (!s
->fragment_size
)
222 s
->fragment_size
= base
;
224 pa_idxset_put(c
->record_streams
, s
, &s
->index
);
228 static void record_stream_free(struct record_stream
* r
) {
229 assert(r
&& r
->connection
);
231 pa_idxset_remove_by_data(r
->connection
->record_streams
, r
, NULL
);
232 pa_source_output_free(r
->source_output
);
233 pa_memblockq_free(r
->memblockq
);
237 static struct playback_stream
* playback_stream_new(struct connection
*c
, struct pa_sink
*sink
, const struct pa_sample_spec
*ss
, const char *name
,
242 struct playback_stream
*s
;
243 struct pa_sink_input
*sink_input
;
244 assert(c
&& sink
&& ss
&& name
&& maxlength
);
246 if (!(sink_input
= pa_sink_input_new(sink
, name
, ss
)))
249 s
= pa_xmalloc(sizeof(struct playback_stream
));
250 s
->type
= PLAYBACK_STREAM
;
252 s
->sink_input
= sink_input
;
254 s
->sink_input
->peek
= sink_input_peek_cb
;
255 s
->sink_input
->drop
= sink_input_drop_cb
;
256 s
->sink_input
->kill
= sink_input_kill_cb
;
257 s
->sink_input
->get_latency
= sink_input_get_latency_cb
;
258 s
->sink_input
->userdata
= s
;
259 s
->sink_input
->owner
= c
->protocol
->module
;
260 s
->sink_input
->client
= c
->client
;
262 s
->memblockq
= pa_memblockq_new(maxlength
, tlength
, pa_frame_size(ss
), prebuf
, minreq
);
263 assert(s
->memblockq
);
265 s
->requested_bytes
= 0;
266 s
->drain_request
= 0;
268 pa_idxset_put(c
->output_streams
, s
, &s
->index
);
272 static void playback_stream_free(struct playback_stream
* p
) {
273 assert(p
&& p
->connection
);
275 if (p
->drain_request
)
276 pa_pstream_send_error(p
->connection
->pstream
, p
->drain_tag
, PA_ERROR_NOENTITY
);
278 pa_idxset_remove_by_data(p
->connection
->output_streams
, p
, NULL
);
279 pa_sink_input_free(p
->sink_input
);
280 pa_memblockq_free(p
->memblockq
);
284 static void connection_free(struct connection
*c
) {
285 struct record_stream
*r
;
286 struct output_stream
*o
;
287 assert(c
&& c
->protocol
);
289 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
290 while ((r
= pa_idxset_first(c
->record_streams
, NULL
)))
291 record_stream_free(r
);
292 pa_idxset_free(c
->record_streams
, NULL
, NULL
);
294 while ((o
= pa_idxset_first(c
->output_streams
, NULL
)))
295 if (o
->type
== PLAYBACK_STREAM
)
296 playback_stream_free((struct playback_stream
*) o
);
298 upload_stream_free((struct upload_stream
*) o
);
299 pa_idxset_free(c
->output_streams
, NULL
, NULL
);
301 pa_pdispatch_free(c
->pdispatch
);
302 pa_pstream_free(c
->pstream
);
303 pa_client_free(c
->client
);
307 static void request_bytes(struct playback_stream
*s
) {
308 struct pa_tagstruct
*t
;
312 if (!(l
= pa_memblockq_missing(s
->memblockq
)))
315 if (l
<= s
->requested_bytes
)
318 l
-= s
->requested_bytes
;
320 if (l
< pa_memblockq_get_minreq(s
->memblockq
))
323 s
->requested_bytes
+= l
;
325 t
= pa_tagstruct_new(NULL
, 0);
327 pa_tagstruct_putu32(t
, PA_COMMAND_REQUEST
);
328 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
329 pa_tagstruct_putu32(t
, s
->index
);
330 pa_tagstruct_putu32(t
, l
);
331 pa_pstream_send_tagstruct(s
->connection
->pstream
, t
);
333 /*fprintf(stderr, "Requesting %u bytes\n", l);*/
336 static void send_memblock(struct connection
*c
) {
338 struct record_stream
*r
;
340 start
= PA_IDXSET_INVALID
;
342 struct pa_memchunk chunk
;
344 if (!(r
= pa_idxset_rrobin(c
->record_streams
, &c
->rrobin_index
)))
347 if (start
== PA_IDXSET_INVALID
)
348 start
= c
->rrobin_index
;
349 else if (start
== c
->rrobin_index
)
352 if (pa_memblockq_peek(r
->memblockq
, &chunk
) >= 0) {
353 if (chunk
.length
> r
->fragment_size
)
354 chunk
.length
= r
->fragment_size
;
356 pa_pstream_send_memblock(c
->pstream
, r
->index
, 0, &chunk
);
357 pa_memblockq_drop(r
->memblockq
, chunk
.length
);
358 pa_memblock_unref(chunk
.memblock
);
365 static void send_playback_stream_killed(struct playback_stream
*p
) {
366 struct pa_tagstruct
*t
;
369 t
= pa_tagstruct_new(NULL
, 0);
371 pa_tagstruct_putu32(t
, PA_COMMAND_PLAYBACK_STREAM_KILLED
);
372 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
373 pa_tagstruct_putu32(t
, p
->index
);
374 pa_pstream_send_tagstruct(p
->connection
->pstream
, t
);
377 static void send_record_stream_killed(struct record_stream
*r
) {
378 struct pa_tagstruct
*t
;
381 t
= pa_tagstruct_new(NULL
, 0);
383 pa_tagstruct_putu32(t
, PA_COMMAND_RECORD_STREAM_KILLED
);
384 pa_tagstruct_putu32(t
, (uint32_t) -1); /* tag */
385 pa_tagstruct_putu32(t
, r
->index
);
386 pa_pstream_send_tagstruct(r
->connection
->pstream
, t
);
390 /*** sinkinput callbacks ***/
392 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
393 struct playback_stream
*s
;
394 assert(i
&& i
->userdata
&& chunk
);
397 if (pa_memblockq_peek(s
->memblockq
, chunk
) < 0)
403 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
) {
404 struct playback_stream
*s
;
405 assert(i
&& i
->userdata
&& length
);
408 pa_memblockq_drop(s
->memblockq
, length
);
411 if (s
->drain_request
&& !pa_memblockq_is_readable(s
->memblockq
)) {
412 pa_pstream_send_simple_ack(s
->connection
->pstream
, s
->drain_tag
);
413 s
->drain_request
= 0;
417 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
418 assert(i
&& i
->userdata
);
419 send_playback_stream_killed((struct playback_stream
*) i
->userdata
);
420 playback_stream_free((struct playback_stream
*) i
->userdata
);
423 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
424 struct playback_stream
*s
;
425 assert(i
&& i
->userdata
);
428 return pa_bytes_to_usec(pa_memblockq_get_length(s
->memblockq
), &s
->sink_input
->sample_spec
);
431 /*** source_output callbacks ***/
433 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
434 struct record_stream
*s
;
435 assert(o
&& o
->userdata
&& chunk
);
438 pa_memblockq_push(s
->memblockq
, chunk
, 0);
439 if (!pa_pstream_is_pending(s
->connection
->pstream
))
440 send_memblock(s
->connection
);
443 static void source_output_kill_cb(struct pa_source_output
*o
) {
444 assert(o
&& o
->userdata
);
445 send_record_stream_killed((struct record_stream
*) o
->userdata
);
446 record_stream_free((struct record_stream
*) o
->userdata
);
449 /*** pdispatch callbacks ***/
451 static void protocol_error(struct connection
*c
) {
452 fprintf(stderr
, __FILE__
": protocol error, kicking client\n");
456 static void command_create_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
457 struct connection
*c
= userdata
;
458 struct playback_stream
*s
;
459 size_t maxlength
, tlength
, prebuf
, minreq
;
461 const char *name
, *sink_name
;
462 struct pa_sample_spec ss
;
463 struct pa_tagstruct
*reply
;
464 struct pa_sink
*sink
;
465 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
467 if (pa_tagstruct_gets(t
, &name
) < 0 ||
468 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
469 pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
470 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
471 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
472 pa_tagstruct_getu32(t
, &tlength
) < 0 ||
473 pa_tagstruct_getu32(t
, &prebuf
) < 0 ||
474 pa_tagstruct_getu32(t
, &minreq
) < 0 ||
475 !pa_tagstruct_eof(t
)) {
480 if (!c
->authorized
) {
481 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
485 if (sink_index
!= (uint32_t) -1)
486 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
488 sink
= pa_namereg_get(c
->protocol
->core
, *sink_name
? sink_name
: NULL
, PA_NAMEREG_SINK
, 1);
491 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
495 if (!(s
= playback_stream_new(c
, sink
, &ss
, name
, maxlength
, tlength
, prebuf
, minreq
))) {
496 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
500 reply
= pa_tagstruct_new(NULL
, 0);
502 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
503 pa_tagstruct_putu32(reply
, tag
);
504 pa_tagstruct_putu32(reply
, s
->index
);
505 assert(s
->sink_input
);
506 pa_tagstruct_putu32(reply
, s
->sink_input
->index
);
507 pa_pstream_send_tagstruct(c
->pstream
, reply
);
511 static void command_delete_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
512 struct connection
*c
= userdata
;
516 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
517 !pa_tagstruct_eof(t
)) {
522 if (!c
->authorized
) {
523 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
527 if (command
== PA_COMMAND_DELETE_PLAYBACK_STREAM
) {
528 struct playback_stream
*s
;
529 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= PLAYBACK_STREAM
)) {
530 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
534 playback_stream_free(s
);
535 } else if (command
== PA_COMMAND_DELETE_RECORD_STREAM
) {
536 struct record_stream
*s
;
537 if (!(s
= pa_idxset_get_by_index(c
->record_streams
, channel
))) {
538 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
542 record_stream_free(s
);
544 struct upload_stream
*s
;
545 assert(command
== PA_COMMAND_DELETE_UPLOAD_STREAM
);
546 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
547 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
551 upload_stream_free(s
);
554 pa_pstream_send_simple_ack(c
->pstream
, tag
);
557 static void command_create_record_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
558 struct connection
*c
= userdata
;
559 struct record_stream
*s
;
560 size_t maxlength
, fragment_size
;
561 uint32_t source_index
;
562 const char *name
, *source_name
;
563 struct pa_sample_spec ss
;
564 struct pa_tagstruct
*reply
;
565 struct pa_source
*source
;
566 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
568 if (pa_tagstruct_gets(t
, &name
) < 0 ||
569 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
570 pa_tagstruct_getu32(t
, &source_index
) < 0 ||
571 pa_tagstruct_gets(t
, &source_name
) < 0 ||
572 pa_tagstruct_getu32(t
, &maxlength
) < 0 ||
573 pa_tagstruct_getu32(t
, &fragment_size
) < 0 ||
574 !pa_tagstruct_eof(t
)) {
579 if (!c
->authorized
) {
580 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
584 if (source_index
!= (uint32_t) -1)
585 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, source_index
);
587 source
= pa_namereg_get(c
->protocol
->core
, *source_name
? source_name
: NULL
, PA_NAMEREG_SOURCE
, 1);
590 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
594 if (!(s
= record_stream_new(c
, source
, &ss
, name
, maxlength
, fragment_size
))) {
595 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
599 reply
= pa_tagstruct_new(NULL
, 0);
601 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
602 pa_tagstruct_putu32(reply
, tag
);
603 pa_tagstruct_putu32(reply
, s
->index
);
604 assert(s
->source_output
);
605 pa_tagstruct_putu32(reply
, s
->source_output
->index
);
606 pa_pstream_send_tagstruct(c
->pstream
, reply
);
609 static void command_exit(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
610 struct connection
*c
= userdata
;
613 if (!pa_tagstruct_eof(t
)) {
618 if (!c
->authorized
) {
619 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
623 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
);
624 c
->protocol
->core
->mainloop
->quit(c
->protocol
->core
->mainloop
, 0);
625 pa_pstream_send_simple_ack(c
->pstream
, tag
); /* nonsense */
629 static void command_auth(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
630 struct connection
*c
= userdata
;
634 if (pa_tagstruct_get_arbitrary(t
, &cookie
, PA_NATIVE_COOKIE_LENGTH
) < 0 ||
635 !pa_tagstruct_eof(t
)) {
640 if (memcmp(c
->protocol
->auth_cookie
, cookie
, PA_NATIVE_COOKIE_LENGTH
) != 0) {
641 fprintf(stderr
, "protocol-native.c: Denied access to client with invalid authorization key.\n");
642 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
647 pa_pstream_send_simple_ack(c
->pstream
, tag
);
651 static void command_set_name(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_gets(t
, &name
) < 0 ||
657 !pa_tagstruct_eof(t
)) {
662 pa_client_rename(c
->client
, name
);
663 pa_pstream_send_simple_ack(c
->pstream
, tag
);
667 static void command_lookup(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
668 struct connection
*c
= userdata
;
670 uint32_t index
= PA_IDXSET_INVALID
;
673 if (pa_tagstruct_gets(t
, &name
) < 0 ||
674 !pa_tagstruct_eof(t
)) {
679 if (!c
->authorized
) {
680 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
684 if (command
== PA_COMMAND_LOOKUP_SINK
) {
685 struct pa_sink
*sink
;
686 if ((sink
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SINK
, 1)))
689 struct pa_source
*source
;
690 assert(command
== PA_COMMAND_LOOKUP_SOURCE
);
691 if ((source
= pa_namereg_get(c
->protocol
->core
, name
, PA_NAMEREG_SOURCE
, 1)))
692 index
= source
->index
;
695 if (index
== PA_IDXSET_INVALID
)
696 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
698 struct pa_tagstruct
*reply
;
699 reply
= pa_tagstruct_new(NULL
, 0);
701 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
702 pa_tagstruct_putu32(reply
, tag
);
703 pa_tagstruct_putu32(reply
, index
);
704 pa_pstream_send_tagstruct(c
->pstream
, reply
);
708 static void command_drain_playback_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
709 struct connection
*c
= userdata
;
711 struct playback_stream
*s
;
714 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
715 !pa_tagstruct_eof(t
)) {
720 if (!c
->authorized
) {
721 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
725 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
726 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
730 s
->drain_request
= 0;
732 if (!pa_memblockq_is_readable(s
->memblockq
))
733 pa_pstream_send_simple_ack(c
->pstream
, tag
);
735 s
->drain_request
= 1;
740 static void command_stat(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
741 struct connection
*c
= userdata
;
743 struct pa_tagstruct
*reply
;
745 if (!pa_tagstruct_eof(t
)) {
750 if (!c
->authorized
) {
751 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
755 reply
= pa_tagstruct_new(NULL
, 0);
757 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
758 pa_tagstruct_putu32(reply
, tag
);
759 pa_tagstruct_putu32(reply
, pa_memblock_get_count());
760 pa_tagstruct_putu32(reply
, pa_memblock_get_total());
761 pa_pstream_send_tagstruct(c
->pstream
, reply
);
764 static void command_get_playback_latency(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
765 struct connection
*c
= userdata
;
767 struct pa_tagstruct
*reply
;
768 struct playback_stream
*s
;
769 uint32_t index
, latency
;
771 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
772 !pa_tagstruct_eof(t
)) {
777 if (!c
->authorized
) {
778 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
782 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, index
)) || s
->type
!= PLAYBACK_STREAM
) {
783 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
787 latency
= pa_sink_input_get_latency(s
->sink_input
);
788 reply
= pa_tagstruct_new(NULL
, 0);
790 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
791 pa_tagstruct_putu32(reply
, tag
);
792 pa_tagstruct_putu32(reply
, latency
);
793 pa_pstream_send_tagstruct(c
->pstream
, reply
);
796 static void command_create_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
797 struct connection
*c
= userdata
;
798 struct upload_stream
*s
;
801 struct pa_sample_spec ss
;
802 struct pa_tagstruct
*reply
;
803 assert(c
&& t
&& c
->protocol
&& c
->protocol
->core
);
805 if (pa_tagstruct_gets(t
, &name
) < 0 ||
806 pa_tagstruct_get_sample_spec(t
, &ss
) < 0 ||
807 pa_tagstruct_getu32(t
, &length
) < 0 ||
808 !pa_tagstruct_eof(t
)) {
813 if (!c
->authorized
) {
814 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
818 if ((length
% pa_frame_size(&ss
)) != 0 || length
<= 0 || !*name
) {
819 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
823 if (!(s
= upload_stream_new(c
, &ss
, name
, length
))) {
824 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_INVALID
);
828 reply
= pa_tagstruct_new(NULL
, 0);
830 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
831 pa_tagstruct_putu32(reply
, tag
);
832 pa_tagstruct_putu32(reply
, s
->index
);
833 pa_pstream_send_tagstruct(c
->pstream
, reply
);
835 reply
= pa_tagstruct_new(NULL
, 0);
837 pa_tagstruct_putu32(reply
, PA_COMMAND_REQUEST
);
838 pa_tagstruct_putu32(reply
, (uint32_t) -1); /* tag */
839 pa_tagstruct_putu32(reply
, s
->index
);
840 pa_tagstruct_putu32(reply
, length
);
841 pa_pstream_send_tagstruct(c
->pstream
, reply
);
844 static void command_finish_upload_stream(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
845 struct connection
*c
= userdata
;
847 struct upload_stream
*s
;
851 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
852 !pa_tagstruct_eof(t
)) {
857 if (!c
->authorized
) {
858 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
862 if (!(s
= pa_idxset_get_by_index(c
->output_streams
, channel
)) || (s
->type
!= UPLOAD_STREAM
)) {
863 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_EXIST
);
867 pa_scache_add_item(c
->protocol
->core
, s
->name
, &s
->sample_spec
, &s
->memchunk
, &index
);
868 pa_pstream_send_simple_ack(c
->pstream
, tag
);
869 upload_stream_free(s
);
872 static void command_play_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
873 struct connection
*c
= userdata
;
874 uint32_t sink_index
, volume
;
875 struct pa_sink
*sink
;
876 const char *name
, *sink_name
;
879 if (pa_tagstruct_getu32(t
, &sink_index
) < 0 ||
880 pa_tagstruct_gets(t
, &sink_name
) < 0 ||
881 pa_tagstruct_getu32(t
, &volume
) < 0 ||
882 pa_tagstruct_gets(t
, &name
) < 0 ||
883 !pa_tagstruct_eof(t
)) {
888 if (!c
->authorized
) {
889 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
893 if (sink_index
!= (uint32_t) -1)
894 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, sink_index
);
896 sink
= pa_namereg_get(c
->protocol
->core
, *sink_name
? sink_name
: NULL
, PA_NAMEREG_SINK
, 1);
899 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
903 if (pa_scache_play_item(c
->protocol
->core
, name
, sink
, volume
) < 0) {
904 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
908 pa_pstream_send_simple_ack(c
->pstream
, tag
);
911 static void command_remove_sample(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
912 struct connection
*c
= userdata
;
916 if (pa_tagstruct_gets(t
, &name
) < 0 ||
917 !pa_tagstruct_eof(t
)) {
922 if (!c
->authorized
) {
923 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
927 if (pa_scache_remove_item(c
->protocol
->core
, name
) < 0) {
928 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
932 pa_pstream_send_simple_ack(c
->pstream
, tag
);
935 static void sink_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_sink
*sink
) {
937 pa_tagstruct_putu32(t
, sink
->index
);
938 pa_tagstruct_puts(t
, sink
->name
);
939 pa_tagstruct_puts(t
, sink
->description
? sink
->description
: "");
940 pa_tagstruct_put_sample_spec(t
, &sink
->sample_spec
);
941 pa_tagstruct_putu32(t
, sink
->owner
? sink
->owner
->index
: (uint32_t) -1);
942 pa_tagstruct_putu32(t
, sink
->volume
);
943 pa_tagstruct_putu32(t
, sink
->monitor_source
->index
);
944 pa_tagstruct_puts(t
, sink
->monitor_source
->name
);
945 pa_tagstruct_putu32(t
, pa_sink_get_latency(sink
));
948 static void source_fill_tagstruct(struct pa_tagstruct
*t
, struct pa_source
*source
) {
950 pa_tagstruct_putu32(t
, source
->index
);
951 pa_tagstruct_puts(t
, source
->name
);
952 pa_tagstruct_puts(t
, source
->description
? source
->description
: "");
953 pa_tagstruct_put_sample_spec(t
, &source
->sample_spec
);
954 pa_tagstruct_putu32(t
, source
->owner
? source
->owner
->index
: (uint32_t) -1);
955 pa_tagstruct_putu32(t
, source
->monitor_of
? source
->monitor_of
->index
: (uint32_t) -1);
956 pa_tagstruct_puts(t
, source
->monitor_of
? source
->monitor_of
->name
: "");
959 static void command_get_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
960 struct connection
*c
= userdata
;
962 struct pa_sink
*sink
= NULL
;
963 struct pa_source
*source
= NULL
;
965 struct pa_tagstruct
*reply
;
968 if (pa_tagstruct_getu32(t
, &index
) < 0 ||
969 pa_tagstruct_gets(t
, &name
) < 0 ||
970 !pa_tagstruct_eof(t
)) {
975 if (!c
->authorized
) {
976 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
980 if (command
== PA_COMMAND_GET_SINK_INFO
) {
981 if (index
!= (uint32_t) -1)
982 sink
= pa_idxset_get_by_index(c
->protocol
->core
->sinks
, index
);
984 sink
= pa_namereg_get(c
->protocol
->core
, *name
? name
: NULL
, PA_NAMEREG_SINK
, 1);
986 assert(command
== PA_COMMAND_GET_SOURCE_INFO
);
987 if (index
!= (uint32_t) -1)
988 source
= pa_idxset_get_by_index(c
->protocol
->core
->sources
, index
);
990 source
= pa_namereg_get(c
->protocol
->core
, *name
? name
: NULL
, PA_NAMEREG_SOURCE
, 1);
993 if (!sink
&& !source
) {
994 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_NOENTITY
);
998 reply
= pa_tagstruct_new(NULL
, 0);
1000 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1001 pa_tagstruct_putu32(reply
, tag
);
1003 sink_fill_tagstruct(reply
, sink
);
1005 source_fill_tagstruct(reply
, source
);
1006 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1009 static void command_get_info_list(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1010 struct connection
*c
= userdata
;
1011 struct pa_idxset
*i
;
1014 struct pa_tagstruct
*reply
;
1017 if (!pa_tagstruct_eof(t
)) {
1022 if (!c
->authorized
) {
1023 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1027 reply
= pa_tagstruct_new(NULL
, 0);
1029 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1030 pa_tagstruct_putu32(reply
, tag
);
1032 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1033 i
= c
->protocol
->core
->sinks
;
1035 assert(command
== PA_COMMAND_GET_SOURCE_INFO_LIST
);
1036 i
= c
->protocol
->core
->sources
;
1039 for (p
= pa_idxset_first(i
, &index
); p
; p
= pa_idxset_next(i
, &index
)) {
1040 if (command
== PA_COMMAND_GET_SINK_INFO_LIST
)
1041 sink_fill_tagstruct(reply
, p
);
1043 assert(command
== PA_COMMAND_GET_SOURCE_INFO_LIST
);
1044 source_fill_tagstruct(reply
, p
);
1048 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1051 static void command_get_server_info(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
1052 struct connection
*c
= userdata
;
1053 struct pa_tagstruct
*reply
;
1057 if (!pa_tagstruct_eof(t
)) {
1062 if (!c
->authorized
) {
1063 pa_pstream_send_error(c
->pstream
, tag
, PA_ERROR_ACCESS
);
1067 reply
= pa_tagstruct_new(NULL
, 0);
1069 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
1070 pa_tagstruct_putu32(reply
, tag
);
1071 pa_tagstruct_puts(reply
, PACKAGE_NAME
);
1072 pa_tagstruct_puts(reply
, PACKAGE_VERSION
);
1073 pa_tagstruct_puts(reply
, pa_get_user_name(txt
, sizeof(txt
)));
1074 pa_tagstruct_puts(reply
, pa_get_host_name(txt
, sizeof(txt
)));
1075 pa_tagstruct_put_sample_spec(reply
, &c
->protocol
->core
->default_sample_spec
);
1076 pa_pstream_send_tagstruct(c
->pstream
, reply
);
1079 /*** pstream callbacks ***/
1081 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
1082 struct connection
*c
= userdata
;
1083 assert(p
&& packet
&& packet
->data
&& c
);
1085 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
1086 fprintf(stderr
, "protocol-native: invalid packet.\n");
1091 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, int32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
1092 struct connection
*c
= userdata
;
1093 struct output_stream
*stream
;
1094 assert(p
&& chunk
&& userdata
);
1096 if (!(stream
= pa_idxset_get_by_index(c
->output_streams
, channel
))) {
1097 fprintf(stderr
, "protocol-native: client sent block for invalid stream.\n");
1102 if (stream
->type
== PLAYBACK_STREAM
) {
1103 struct playback_stream
*p
= (struct playback_stream
*) stream
;
1104 if (chunk
->length
>= p
->requested_bytes
)
1105 p
->requested_bytes
= 0;
1107 p
->requested_bytes
-= chunk
->length
;
1109 pa_memblockq_push_align(p
->memblockq
, chunk
, delta
);
1110 assert(p
->sink_input
);
1111 pa_sink_notify(p
->sink_input
->sink
);
1112 /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
1114 struct upload_stream
*u
= (struct upload_stream
*) stream
;
1116 assert(u
->type
== UPLOAD_STREAM
);
1118 if (!u
->memchunk
.memblock
) {
1119 if (u
->length
== chunk
->length
) {
1120 u
->memchunk
= *chunk
;
1121 pa_memblock_ref(u
->memchunk
.memblock
);
1123 fprintf(stderr
, "COPY\n");
1125 u
->memchunk
.memblock
= pa_memblock_new(u
->length
);
1126 u
->memchunk
.index
= u
->memchunk
.length
= 0;
1130 assert(u
->memchunk
.memblock
);
1133 if (l
> chunk
->length
)
1137 memcpy(u
->memchunk
.memblock
->data
+ u
->memchunk
.index
+ u
->memchunk
.length
, chunk
->memblock
->data
+chunk
->index
, l
);
1138 u
->memchunk
.length
+= l
;
1144 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
1145 struct connection
*c
= userdata
;
1149 fprintf(stderr
, "protocol-native: connection died.\n");
1153 static void pstream_drain_callback(struct pa_pstream
*p
, void *userdata
) {
1154 struct connection
*c
= userdata
;
1160 /*** client callbacks ***/
1162 static void client_kill_cb(struct pa_client
*c
) {
1163 assert(c
&& c
->userdata
);
1164 connection_free(c
->userdata
);
1167 /*** socket server callbacks ***/
1169 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
1170 struct pa_protocol_native
*p
= userdata
;
1171 struct connection
*c
;
1172 assert(s
&& io
&& p
);
1174 c
= pa_xmalloc(sizeof(struct connection
));
1175 c
->authorized
= p
->public;
1178 c
->client
= pa_client_new(p
->core
, "NATIVE", "Client");
1180 c
->client
->kill
= client_kill_cb
;
1181 c
->client
->userdata
= c
;
1182 c
->client
->owner
= p
->module
;
1184 c
->pstream
= pa_pstream_new(p
->core
->mainloop
, io
);
1187 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
1188 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
1189 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
1190 pa_pstream_set_drain_callback(c
->pstream
, pstream_drain_callback
, c
);
1192 c
->pdispatch
= pa_pdispatch_new(p
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
1193 assert(c
->pdispatch
);
1195 c
->record_streams
= pa_idxset_new(NULL
, NULL
);
1196 c
->output_streams
= pa_idxset_new(NULL
, NULL
);
1197 assert(c
->record_streams
&& c
->output_streams
);
1199 c
->rrobin_index
= PA_IDXSET_INVALID
;
1201 pa_idxset_put(p
->connections
, c
, NULL
);
1204 /*** module entry points ***/
1206 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
) {
1207 struct pa_protocol_native
*p
;
1209 assert(core
&& server
&& ma
);
1211 if (pa_modargs_get_value_u32(ma
, "public", &public) < 0) {
1212 fprintf(stderr
, __FILE__
": public= expects numeric argument.\n");
1216 p
= pa_xmalloc(sizeof(struct pa_protocol_native
));
1218 if (pa_authkey_load_from_home(pa_modargs_get_value(ma
, "cookie", PA_NATIVE_COOKIE_FILE
), p
->auth_cookie
, sizeof(p
->auth_cookie
)) < 0) {
1227 p
->connections
= pa_idxset_new(NULL
, NULL
);
1228 assert(p
->connections
);
1230 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
1235 void pa_protocol_native_free(struct pa_protocol_native
*p
) {
1236 struct connection
*c
;
1239 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
1241 pa_idxset_free(p
->connections
, NULL
, NULL
);
1242 pa_socket_server_free(p
->server
);