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