Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.git] / src / pulsecore / protocol-simple.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2.1 of the License,
9   or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/source-output.h>
37 #include <pulsecore/client.h>
38 #include <pulsecore/sample-util.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/atomic.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/shared.h>
46
47 #include "protocol-simple.h"
48
49 /* Don't allow more than this many concurrent connections */
50 #define MAX_CONNECTIONS 10
51
52 typedef struct connection {
53     pa_msgobject parent;
54     pa_simple_protocol *protocol;
55     pa_simple_options *options;
56     pa_iochannel *io;
57     pa_sink_input *sink_input;
58     pa_source_output *source_output;
59     pa_client *client;
60     pa_memblockq *input_memblockq, *output_memblockq;
61
62     pa_bool_t dead;
63
64     struct {
65         pa_memblock *current_memblock;
66         size_t memblock_index;
67         pa_atomic_t missing;
68         pa_bool_t underrun;
69     } playback;
70 } connection;
71
72 PA_DECLARE_CLASS(connection);
73 #define CONNECTION(o) (connection_cast(o))
74 static PA_DEFINE_CHECK_TYPE(connection, pa_msgobject);
75
76 struct pa_simple_protocol {
77     PA_REFCNT_DECLARE;
78
79     pa_core *core;
80     pa_idxset *connections;
81 };
82
83 enum {
84     SINK_INPUT_MESSAGE_POST_DATA = PA_SINK_INPUT_MESSAGE_MAX, /* data from main loop to sink input */
85     SINK_INPUT_MESSAGE_DISABLE_PREBUF /* disabled prebuf, get playback started. */
86 };
87
88 enum {
89     CONNECTION_MESSAGE_REQUEST_DATA,      /* data requested from sink input from the main loop */
90     CONNECTION_MESSAGE_POST_DATA,         /* data from source output to main loop */
91     CONNECTION_MESSAGE_UNLINK_CONNECTION    /* Please drop a aconnection now */
92 };
93
94 #define PLAYBACK_BUFFER_SECONDS (.5)
95 #define PLAYBACK_BUFFER_FRAGMENTS (10)
96 #define RECORD_BUFFER_SECONDS (5)
97 #define DEFAULT_SINK_LATENCY (300*PA_USEC_PER_MSEC)
98 #define DEFAULT_SOURCE_LATENCY (300*PA_USEC_PER_MSEC)
99
100 static void connection_unlink(connection *c) {
101     pa_assert(c);
102
103     if (!c->protocol)
104         return;
105
106     if (c->options) {
107         pa_simple_options_unref(c->options);
108         c->options = NULL;
109     }
110
111     if (c->sink_input) {
112         pa_sink_input_unlink(c->sink_input);
113         pa_sink_input_unref(c->sink_input);
114         c->sink_input = NULL;
115     }
116
117     if (c->source_output) {
118         pa_source_output_unlink(c->source_output);
119         pa_source_output_unref(c->source_output);
120         c->source_output = NULL;
121     }
122
123     if (c->client) {
124         pa_client_free(c->client);
125         c->client = NULL;
126     }
127
128     if (c->io) {
129         pa_iochannel_free(c->io);
130         c->io = NULL;
131     }
132
133     pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
134     c->protocol = NULL;
135     connection_unref(c);
136 }
137
138 static void connection_free(pa_object *o) {
139     connection *c = CONNECTION(o);
140     pa_assert(c);
141
142     if (c->playback.current_memblock)
143         pa_memblock_unref(c->playback.current_memblock);
144
145     if (c->input_memblockq)
146         pa_memblockq_free(c->input_memblockq);
147     if (c->output_memblockq)
148         pa_memblockq_free(c->output_memblockq);
149
150     pa_xfree(c);
151 }
152
153 static int do_read(connection *c) {
154     pa_memchunk chunk;
155     ssize_t r;
156     size_t l;
157     void *p;
158     size_t space;
159
160     connection_assert_ref(c);
161
162     if (!c->sink_input || (l = (size_t) pa_atomic_load(&c->playback.missing)) <= 0)
163         return 0;
164
165     if (c->playback.current_memblock) {
166
167         space = pa_memblock_get_length(c->playback.current_memblock) - c->playback.memblock_index;
168
169         if (space <= 0) {
170             pa_memblock_unref(c->playback.current_memblock);
171             c->playback.current_memblock = NULL;
172         }
173     }
174
175     if (!c->playback.current_memblock) {
176         pa_assert_se(c->playback.current_memblock = pa_memblock_new(c->protocol->core->mempool, (size_t) -1));
177         c->playback.memblock_index = 0;
178
179         space = pa_memblock_get_length(c->playback.current_memblock);
180     }
181
182     if (l > space)
183         l = space;
184
185     p = pa_memblock_acquire(c->playback.current_memblock);
186     r = pa_iochannel_read(c->io, (uint8_t*) p + c->playback.memblock_index, l);
187     pa_memblock_release(c->playback.current_memblock);
188
189     if (r <= 0) {
190
191         if (r < 0 && (errno == EINTR || errno == EAGAIN))
192             return 0;
193
194         pa_log_debug("read(): %s", r == 0 ? "EOF" : pa_cstrerror(errno));
195         return -1;
196     }
197
198     chunk.memblock = c->playback.current_memblock;
199     chunk.index = c->playback.memblock_index;
200     chunk.length = (size_t) r;
201
202     c->playback.memblock_index += (size_t) r;
203
204     pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_POST_DATA, NULL, 0, &chunk, NULL);
205     pa_atomic_sub(&c->playback.missing, (int) r);
206
207     return 0;
208 }
209
210 static int do_write(connection *c) {
211     pa_memchunk chunk;
212     ssize_t r;
213     void *p;
214
215     connection_assert_ref(c);
216
217     if (!c->source_output)
218         return 0;
219
220     if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0) {
221 /*         pa_log("peek failed"); */
222         return 0;
223     }
224
225     pa_assert(chunk.memblock);
226     pa_assert(chunk.length);
227
228     p = pa_memblock_acquire(chunk.memblock);
229     r = pa_iochannel_write(c->io, (uint8_t*) p+chunk.index, chunk.length);
230     pa_memblock_release(chunk.memblock);
231
232     pa_memblock_unref(chunk.memblock);
233
234     if (r < 0) {
235
236         if (errno == EINTR || errno == EAGAIN)
237             return 0;
238
239         pa_log("write(): %s", pa_cstrerror(errno));
240         return -1;
241     }
242
243     pa_memblockq_drop(c->output_memblockq, (size_t) r);
244
245     return 0;
246 }
247
248 static void do_work(connection *c) {
249     connection_assert_ref(c);
250
251     if (c->dead)
252         return;
253
254     if (pa_iochannel_is_readable(c->io))
255         if (do_read(c) < 0)
256             goto fail;
257
258     if (!c->sink_input && pa_iochannel_is_hungup(c->io))
259         goto fail;
260
261     if (pa_iochannel_is_writable(c->io))
262         if (do_write(c) < 0)
263             goto fail;
264
265     return;
266
267 fail:
268
269     if (c->sink_input) {
270
271         /* If there is a sink input, we first drain what we already have read before shutting down the connection */
272         c->dead = TRUE;
273
274         pa_iochannel_free(c->io);
275         c->io = NULL;
276
277         pa_asyncmsgq_post(c->sink_input->sink->asyncmsgq, PA_MSGOBJECT(c->sink_input), SINK_INPUT_MESSAGE_DISABLE_PREBUF, NULL, 0, NULL, NULL);
278     } else
279         connection_unlink(c);
280 }
281
282 static int connection_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
283     connection *c = CONNECTION(o);
284     connection_assert_ref(c);
285
286     if (!c->protocol)
287         return -1;
288
289     switch (code) {
290         case CONNECTION_MESSAGE_REQUEST_DATA:
291             do_work(c);
292             break;
293
294         case CONNECTION_MESSAGE_POST_DATA:
295 /*             pa_log("got data %u", chunk->length); */
296             pa_memblockq_push_align(c->output_memblockq, chunk);
297             do_work(c);
298             break;
299
300         case CONNECTION_MESSAGE_UNLINK_CONNECTION:
301             connection_unlink(c);
302             break;
303     }
304
305     return 0;
306 }
307
308 /*** sink_input callbacks ***/
309
310 /* Called from thread context */
311 static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
312     pa_sink_input *i = PA_SINK_INPUT(o);
313     connection*c;
314
315     pa_sink_input_assert_ref(i);
316     c = CONNECTION(i->userdata);
317     connection_assert_ref(c);
318
319     switch (code) {
320
321         case SINK_INPUT_MESSAGE_POST_DATA: {
322             pa_assert(chunk);
323
324             /* New data from the main loop */
325             pa_memblockq_push_align(c->input_memblockq, chunk);
326
327             if (pa_memblockq_is_readable(c->input_memblockq) && c->playback.underrun) {
328                 pa_log_debug("Requesting rewind due to end of underrun.");
329                 pa_sink_input_request_rewind(c->sink_input, 0, FALSE, TRUE, FALSE);
330             }
331
332 /*             pa_log("got data, %u", pa_memblockq_get_length(c->input_memblockq)); */
333
334             return 0;
335         }
336
337         case SINK_INPUT_MESSAGE_DISABLE_PREBUF:
338             pa_memblockq_prebuf_disable(c->input_memblockq);
339             return 0;
340
341         case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
342             pa_usec_t *r = userdata;
343
344             *r = pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
345
346             /* Fall through, the default handler will add in the extra
347              * latency added by the resampler */
348         }
349
350         default:
351             return pa_sink_input_process_msg(o, code, userdata, offset, chunk);
352     }
353 }
354
355 /* Called from thread context */
356 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
357     connection *c;
358
359     pa_sink_input_assert_ref(i);
360     c = CONNECTION(i->userdata);
361     connection_assert_ref(c);
362     pa_assert(chunk);
363
364     if (pa_memblockq_peek(c->input_memblockq, chunk) < 0) {
365
366         c->playback.underrun = TRUE;
367
368         if (c->dead && pa_sink_input_safe_to_remove(i))
369             pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_UNLINK_CONNECTION, NULL, 0, NULL, NULL);
370
371         return -1;
372     } else {
373         size_t m;
374
375         chunk->length = PA_MIN(length, chunk->length);
376
377         c->playback.underrun = FALSE;
378
379         pa_memblockq_drop(c->input_memblockq, chunk->length);
380         m = pa_memblockq_pop_missing(c->input_memblockq);
381
382         if (m > 0)
383             if (pa_atomic_add(&c->playback.missing, (int) m) <= 0)
384                 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
385
386         return 0;
387     }
388 }
389
390 /* Called from thread context */
391 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
392     connection *c;
393
394     pa_sink_input_assert_ref(i);
395     c = CONNECTION(i->userdata);
396     connection_assert_ref(c);
397
398     /* If we are in an underrun, then we don't rewind */
399     if (i->thread_info.underrun_for > 0)
400         return;
401
402     pa_memblockq_rewind(c->input_memblockq, nbytes);
403 }
404
405 /* Called from thread context */
406 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
407     connection *c;
408
409     pa_sink_input_assert_ref(i);
410     c = CONNECTION(i->userdata);
411     connection_assert_ref(c);
412
413     pa_memblockq_set_maxrewind(c->input_memblockq, nbytes);
414 }
415
416 /* Called from main context */
417 static void sink_input_kill_cb(pa_sink_input *i) {
418     pa_sink_input_assert_ref(i);
419
420     connection_unlink(CONNECTION(i->userdata));
421 }
422
423 /*** source_output callbacks ***/
424
425 /* Called from thread context */
426 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
427     connection *c;
428
429     pa_source_output_assert_ref(o);
430     c = CONNECTION(o->userdata);
431     pa_assert(c);
432     pa_assert(chunk);
433
434     pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(c), CONNECTION_MESSAGE_POST_DATA, NULL, 0, chunk, NULL);
435 }
436
437 /* Called from main context */
438 static void source_output_kill_cb(pa_source_output *o) {
439     pa_source_output_assert_ref(o);
440
441     connection_unlink(CONNECTION(o->userdata));
442 }
443
444 /* Called from main context */
445 static pa_usec_t source_output_get_latency_cb(pa_source_output *o) {
446     connection*c;
447
448     pa_source_output_assert_ref(o);
449     c = CONNECTION(o->userdata);
450     pa_assert(c);
451
452     return pa_bytes_to_usec(pa_memblockq_get_length(c->output_memblockq), &c->source_output->sample_spec);
453 }
454
455 /*** client callbacks ***/
456
457 static void client_kill_cb(pa_client *client) {
458     connection*c;
459
460     pa_assert(client);
461     c = CONNECTION(client->userdata);
462     pa_assert(c);
463
464     connection_unlink(c);
465 }
466
467 /*** pa_iochannel callbacks ***/
468
469 static void io_callback(pa_iochannel*io, void *userdata) {
470     connection *c = CONNECTION(userdata);
471
472     connection_assert_ref(c);
473     pa_assert(io);
474
475     do_work(c);
476 }
477
478 /*** socket_server callbacks ***/
479
480 void pa_simple_protocol_connect(pa_simple_protocol *p, pa_iochannel *io, pa_simple_options *o) {
481     connection *c = NULL;
482     char pname[128];
483     pa_client_new_data client_data;
484
485     pa_assert(p);
486     pa_assert(io);
487     pa_assert(o);
488
489     if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
490         pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
491         pa_iochannel_free(io);
492         return;
493     }
494
495     c = pa_msgobject_new(connection);
496     c->parent.parent.free = connection_free;
497     c->parent.process_msg = connection_process_msg;
498     c->io = io;
499     pa_iochannel_set_callback(c->io, io_callback, c);
500
501     c->sink_input = NULL;
502     c->source_output = NULL;
503     c->input_memblockq = c->output_memblockq = NULL;
504     c->protocol = p;
505     c->options = pa_simple_options_ref(o);
506     c->playback.current_memblock = NULL;
507     c->playback.memblock_index = 0;
508     c->dead = FALSE;
509     c->playback.underrun = TRUE;
510     pa_atomic_store(&c->playback.missing, 0);
511
512     pa_client_new_data_init(&client_data);
513     client_data.module = o->module;
514     client_data.driver = __FILE__;
515     pa_iochannel_socket_peer_to_string(io, pname, sizeof(pname));
516     pa_proplist_setf(client_data.proplist, PA_PROP_APPLICATION_NAME, "Simple client (%s)", pname);
517     pa_proplist_sets(client_data.proplist, "simple-protocol.peer", pname);
518     c->client = pa_client_new(p->core, &client_data);
519     pa_client_new_data_done(&client_data);
520
521     if (!c->client)
522         goto fail;
523
524     c->client->kill = client_kill_cb;
525     c->client->userdata = c;
526
527     if (o->playback) {
528         pa_sink_input_new_data data;
529         size_t l;
530         pa_sink *sink;
531
532         if (!(sink = pa_namereg_get(c->protocol->core, o->default_sink, PA_NAMEREG_SINK))) {
533             pa_log("Failed to get sink.");
534             goto fail;
535         }
536
537         pa_sink_input_new_data_init(&data);
538         data.driver = __FILE__;
539         data.module = o->module;
540         data.client = c->client;
541         data.sink = sink;
542         pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
543         pa_sink_input_new_data_set_sample_spec(&data, &o->sample_spec);
544
545         pa_sink_input_new(&c->sink_input, p->core, &data, 0);
546         pa_sink_input_new_data_done(&data);
547
548         if (!c->sink_input) {
549             pa_log("Failed to create sink input.");
550             goto fail;
551         }
552
553         c->sink_input->parent.process_msg = sink_input_process_msg;
554         c->sink_input->pop = sink_input_pop_cb;
555         c->sink_input->process_rewind = sink_input_process_rewind_cb;
556         c->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
557         c->sink_input->kill = sink_input_kill_cb;
558         c->sink_input->userdata = c;
559
560         pa_sink_input_set_requested_latency(c->sink_input, DEFAULT_SINK_LATENCY);
561
562         l = (size_t) ((double) pa_bytes_per_second(&o->sample_spec)*PLAYBACK_BUFFER_SECONDS);
563         c->input_memblockq = pa_memblockq_new(
564                 0,
565                 l,
566                 l,
567                 pa_frame_size(&o->sample_spec),
568                 (size_t) -1,
569                 l/PLAYBACK_BUFFER_FRAGMENTS,
570                 0,
571                 NULL);
572         pa_iochannel_socket_set_rcvbuf(io, l);
573
574         pa_atomic_store(&c->playback.missing, (int) pa_memblockq_missing(c->input_memblockq));
575
576         pa_sink_input_put(c->sink_input);
577     }
578
579     if (o->record) {
580         pa_source_output_new_data data;
581         size_t l;
582         pa_source *source;
583
584         if (!(source = pa_namereg_get(c->protocol->core, o->default_source, PA_NAMEREG_SOURCE))) {
585             pa_log("Failed to get source.");
586             goto fail;
587         }
588
589         pa_source_output_new_data_init(&data);
590         data.driver = __FILE__;
591         data.module = o->module;
592         data.client = c->client;
593         data.source = source;
594         pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist);
595         pa_source_output_new_data_set_sample_spec(&data, &o->sample_spec);
596
597         pa_source_output_new(&c->source_output, p->core, &data, 0);
598         pa_source_output_new_data_done(&data);
599
600         if (!c->source_output) {
601             pa_log("Failed to create source output.");
602             goto fail;
603         }
604         c->source_output->push = source_output_push_cb;
605         c->source_output->kill = source_output_kill_cb;
606         c->source_output->get_latency = source_output_get_latency_cb;
607         c->source_output->userdata = c;
608
609         pa_source_output_set_requested_latency(c->source_output, DEFAULT_SOURCE_LATENCY);
610
611         l = (size_t) (pa_bytes_per_second(&o->sample_spec)*RECORD_BUFFER_SECONDS);
612         c->output_memblockq = pa_memblockq_new(
613                 0,
614                 l,
615                 0,
616                 pa_frame_size(&o->sample_spec),
617                 1,
618                 0,
619                 0,
620                 NULL);
621         pa_iochannel_socket_set_sndbuf(io, l);
622
623         pa_source_output_put(c->source_output);
624     }
625
626     pa_idxset_put(p->connections, c, NULL);
627
628     return;
629
630 fail:
631     if (c)
632         connection_unlink(c);
633 }
634
635 void pa_simple_protocol_disconnect(pa_simple_protocol *p, pa_module *m) {
636     connection *c;
637     void *state = NULL;
638
639     pa_assert(p);
640     pa_assert(m);
641
642     while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
643         if (c->options->module == m)
644             connection_unlink(c);
645 }
646
647 static pa_simple_protocol* simple_protocol_new(pa_core *c) {
648     pa_simple_protocol *p;
649
650     pa_assert(c);
651
652     p = pa_xnew(pa_simple_protocol, 1);
653     PA_REFCNT_INIT(p);
654     p->core = c;
655     p->connections = pa_idxset_new(NULL, NULL);
656
657     pa_assert_se(pa_shared_set(c, "simple-protocol", p) >= 0);
658
659     return p;
660 }
661
662 pa_simple_protocol* pa_simple_protocol_get(pa_core *c) {
663     pa_simple_protocol *p;
664
665     if ((p = pa_shared_get(c, "simple-protocol")))
666         return pa_simple_protocol_ref(p);
667
668     return simple_protocol_new(c);
669 }
670
671 pa_simple_protocol* pa_simple_protocol_ref(pa_simple_protocol *p) {
672     pa_assert(p);
673     pa_assert(PA_REFCNT_VALUE(p) >= 1);
674
675     PA_REFCNT_INC(p);
676
677     return p;
678 }
679
680 void pa_simple_protocol_unref(pa_simple_protocol *p) {
681     connection *c;
682     pa_assert(p);
683     pa_assert(PA_REFCNT_VALUE(p) >= 1);
684
685     if (PA_REFCNT_DEC(p) > 0)
686         return;
687
688     while ((c = pa_idxset_first(p->connections, NULL)))
689         connection_unlink(c);
690
691     pa_idxset_free(p->connections, NULL, NULL);
692
693     pa_assert_se(pa_shared_remove(p->core, "simple-protocol") >= 0);
694
695     pa_xfree(p);
696 }
697
698 pa_simple_options* pa_simple_options_new(void) {
699     pa_simple_options *o;
700
701     o = pa_xnew0(pa_simple_options, 1);
702     PA_REFCNT_INIT(o);
703
704     o->record = FALSE;
705     o->playback = TRUE;
706
707     return o;
708 }
709
710 pa_simple_options* pa_simple_options_ref(pa_simple_options *o) {
711     pa_assert(o);
712     pa_assert(PA_REFCNT_VALUE(o) >= 1);
713
714     PA_REFCNT_INC(o);
715
716     return o;
717 }
718
719 void pa_simple_options_unref(pa_simple_options *o) {
720     pa_assert(o);
721     pa_assert(PA_REFCNT_VALUE(o) >= 1);
722
723     if (PA_REFCNT_DEC(o) > 0)
724         return;
725
726     pa_xfree(o->default_sink);
727     pa_xfree(o->default_source);
728
729     pa_xfree(o);
730 }
731
732 int pa_simple_options_parse(pa_simple_options *o, pa_core *c, pa_modargs *ma) {
733     pa_bool_t enabled;
734
735     pa_assert(o);
736     pa_assert(PA_REFCNT_VALUE(o) >= 1);
737     pa_assert(ma);
738
739     o->sample_spec = c->default_sample_spec;
740     if (pa_modargs_get_sample_spec_and_channel_map(ma, &o->sample_spec, &o->channel_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
741         pa_log("Failed to parse sample type specification.");
742         return -1;
743     }
744
745     pa_xfree(o->default_source);
746     o->default_source = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
747
748     pa_xfree(o->default_sink);
749     o->default_sink = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
750
751     enabled = o->record;
752     if (pa_modargs_get_value_boolean(ma, "record", &enabled) < 0) {
753         pa_log("record= expects a boolean argument.");
754         return -1;
755     }
756     o->record = enabled;
757
758     enabled = o->playback;
759     if (pa_modargs_get_value_boolean(ma, "playback", &enabled) < 0) {
760         pa_log("playback= expects a boolean argument.");
761         return -1;
762     }
763     o->playback = enabled;
764
765     if (!o->playback && !o->record) {
766         pa_log("neither playback nor recording enabled for protocol.");
767         return -1;
768     }
769
770     return 0;
771 }