]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
fix a few more copy/paste errors
[pulseaudio] / src / pulsecore / source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
34
35 #include <pulsecore/source-output.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/sample-util.h>
40
41 #include "source.h"
42
43 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
44
45 static PA_DEFINE_CHECK_TYPE(pa_source, pa_msgobject);
46
47 static void source_free(pa_object *o);
48
49 pa_source_new_data* pa_source_new_data_init(pa_source_new_data *data) {
50 pa_assert(data);
51
52 memset(data, 0, sizeof(*data));
53 data->proplist = pa_proplist_new();
54
55 return data;
56 }
57
58 void pa_source_new_data_set_name(pa_source_new_data *data, const char *name) {
59 pa_assert(data);
60
61 pa_xfree(data->name);
62 data->name = pa_xstrdup(name);
63 }
64
65 void pa_source_new_data_set_sample_spec(pa_source_new_data *data, const pa_sample_spec *spec) {
66 pa_assert(data);
67
68 if ((data->sample_spec_is_set = !!spec))
69 data->sample_spec = *spec;
70 }
71
72 void pa_source_new_data_set_channel_map(pa_source_new_data *data, const pa_channel_map *map) {
73 pa_assert(data);
74
75 if ((data->channel_map_is_set = !!map))
76 data->channel_map = *map;
77 }
78
79 void pa_source_new_data_set_volume(pa_source_new_data *data, const pa_cvolume *volume) {
80 pa_assert(data);
81
82 if ((data->volume_is_set = !!volume))
83 data->volume = *volume;
84 }
85
86 void pa_source_new_data_set_muted(pa_source_new_data *data, pa_bool_t mute) {
87 pa_assert(data);
88
89 data->muted_is_set = TRUE;
90 data->muted = !!mute;
91 }
92
93 void pa_source_new_data_done(pa_source_new_data *data) {
94 pa_assert(data);
95
96 pa_xfree(data->name);
97 pa_proplist_free(data->proplist);
98 }
99
100 /* Called from main context */
101 static void reset_callbacks(pa_source *s) {
102 pa_assert(s);
103
104 s->set_state = NULL;
105 s->get_volume = NULL;
106 s->set_volume = NULL;
107 s->get_mute = NULL;
108 s->set_mute = NULL;
109 s->update_requested_latency = NULL;
110 }
111
112 /* Called from main context */
113 pa_source* pa_source_new(
114 pa_core *core,
115 pa_source_new_data *data,
116 pa_source_flags_t flags) {
117
118 pa_source *s;
119 const char *name;
120 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
121
122 pa_assert(core);
123 pa_assert(data);
124 pa_assert(data->name);
125
126 s = pa_msgobject_new(pa_source);
127
128 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SOURCE, s, data->namereg_fail))) {
129 pa_xfree(s);
130 return NULL;
131 }
132
133 pa_source_new_data_set_name(data, name);
134
135 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_NEW], data) < 0) {
136 pa_xfree(s);
137 pa_namereg_unregister(core, name);
138 return NULL;
139 }
140
141 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
142 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
143
144 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
145
146 if (!data->channel_map_is_set)
147 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
148
149 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
150 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
151
152 if (!data->volume_is_set)
153 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
154
155 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
156 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
157
158 if (!data->muted_is_set)
159 data->muted = FALSE;
160
161 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], data) < 0) {
162 pa_xfree(s);
163 pa_namereg_unregister(core, name);
164 return NULL;
165 }
166
167 s->parent.parent.free = source_free;
168 s->parent.process_msg = pa_source_process_msg;
169
170 s->core = core;
171 s->state = PA_SOURCE_INIT;
172 s->flags = flags;
173 s->name = pa_xstrdup(name);
174 s->proplist = pa_proplist_copy(data->proplist);
175 s->driver = pa_xstrdup(data->driver);
176 s->module = data->module;
177
178 s->sample_spec = data->sample_spec;
179 s->channel_map = data->channel_map;
180
181 s->outputs = pa_idxset_new(NULL, NULL);
182 s->n_corked = 0;
183 s->monitor_of = NULL;
184
185 s->volume = data->volume;
186 s->muted = data->muted;
187 s->refresh_volume = s->refresh_muted = FALSE;
188
189 reset_callbacks(s);
190 s->userdata = NULL;
191
192 s->asyncmsgq = NULL;
193 s->rtpoll = NULL;
194
195 pa_silence_memchunk_get(
196 &core->silence_cache,
197 core->mempool,
198 &s->silence,
199 &s->sample_spec,
200 0);
201
202 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
203 pa_cvolume_reset(&s->thread_info.soft_volume, s->sample_spec.channels);
204 s->thread_info.soft_muted = FALSE;
205 s->thread_info.state = s->state;
206 s->thread_info.max_rewind = 0;
207 s->thread_info.requested_latency_valid = FALSE;
208 s->thread_info.requested_latency = 0;
209 s->thread_info.min_latency = DEFAULT_MIN_LATENCY;
210 s->thread_info.max_latency = 0;
211
212 pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
213
214 pa_log_info("Created source %u \"%s\" with sample spec %s and channel map %s",
215 s->index,
216 s->name,
217 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
218 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map));
219
220 return s;
221 }
222
223 /* Called from main context */
224 static int source_set_state(pa_source *s, pa_source_state_t state) {
225 int ret;
226 pa_bool_t suspend_change;
227 pa_source_state_t original_state;
228
229 pa_assert(s);
230
231 if (s->state == state)
232 return 0;
233
234 original_state = s->state;
235
236 suspend_change =
237 (original_state == PA_SOURCE_SUSPENDED && PA_SOURCE_IS_OPENED(state)) ||
238 (PA_SOURCE_IS_OPENED(original_state) && state == PA_SOURCE_SUSPENDED);
239
240 if (s->set_state)
241 if ((ret = s->set_state(s, state)) < 0)
242 return ret;
243
244 if (s->asyncmsgq)
245 if ((ret = pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL)) < 0) {
246
247 if (s->set_state)
248 s->set_state(s, original_state);
249
250 return ret;
251 }
252
253 s->state = state;
254
255 if (suspend_change) {
256 pa_source_output *o;
257 uint32_t idx;
258
259 /* We're suspending or resuming, tell everyone about it */
260
261 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
262 if (o->suspend)
263 o->suspend(o, state == PA_SOURCE_SUSPENDED);
264 }
265
266 if (state != PA_SOURCE_UNLINKED) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
267 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
268
269 return 0;
270 }
271
272 /* Called from main context */
273 void pa_source_put(pa_source *s) {
274 pa_source_assert_ref(s);
275
276 pa_assert(s->state == PA_SOURCE_INIT);
277
278 /* The following fields must be initialized properly when calling _put() */
279 pa_assert(s->asyncmsgq);
280 pa_assert(s->rtpoll);
281 pa_assert(!s->thread_info.min_latency || !s->thread_info.max_latency ||
282 s->thread_info.min_latency <= s->thread_info.max_latency);
283
284 if (!(s->flags & PA_SOURCE_HW_VOLUME_CTRL)) {
285 s->flags |= PA_SOURCE_DECIBEL_VOLUME;
286
287 s->thread_info.soft_volume = s->volume;
288 s->thread_info.soft_muted = s->muted;
289 }
290
291 pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
292
293 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
294 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PUT], s);
295 }
296
297 /* Called from main context */
298 void pa_source_unlink(pa_source *s) {
299 pa_bool_t linked;
300 pa_source_output *o, *j = NULL;
301
302 pa_assert(s);
303
304 /* See pa_sink_unlink() for a couple of comments how this function
305 * works. */
306
307 linked = PA_SOURCE_IS_LINKED(s->state);
308
309 if (linked)
310 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], s);
311
312 if (s->state != PA_SOURCE_UNLINKED)
313 pa_namereg_unregister(s->core, s->name);
314 pa_idxset_remove_by_data(s->core->sources, s, NULL);
315
316 while ((o = pa_idxset_first(s->outputs, NULL))) {
317 pa_assert(o != j);
318 pa_source_output_kill(o);
319 j = o;
320 }
321
322 if (linked)
323 source_set_state(s, PA_SOURCE_UNLINKED);
324 else
325 s->state = PA_SOURCE_UNLINKED;
326
327 reset_callbacks(s);
328
329 if (linked) {
330 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
331 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], s);
332 }
333 }
334
335 /* Called from main context */
336 static void source_free(pa_object *o) {
337 pa_source_output *so;
338 pa_source *s = PA_SOURCE(o);
339
340 pa_assert(s);
341 pa_assert(pa_source_refcnt(s) == 0);
342
343 if (PA_SOURCE_IS_LINKED(s->state))
344 pa_source_unlink(s);
345
346 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
347
348 pa_idxset_free(s->outputs, NULL, NULL);
349
350 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
351 pa_source_output_unref(so);
352
353 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
354
355 if (s->silence.memblock)
356 pa_memblock_unref(s->silence.memblock);
357
358 pa_xfree(s->name);
359 pa_xfree(s->driver);
360
361 if (s->proplist)
362 pa_proplist_free(s->proplist);
363
364 pa_xfree(s);
365 }
366
367 /* Called from main context */
368 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
369 pa_source_assert_ref(s);
370
371 s->asyncmsgq = q;
372 }
373
374 /* Called from main context */
375 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
376 pa_source_assert_ref(s);
377
378 s->rtpoll = p;
379 }
380
381 /* Called from main context */
382 int pa_source_update_status(pa_source*s) {
383 pa_source_assert_ref(s);
384 pa_assert(PA_SOURCE_IS_LINKED(s->state));
385
386 if (s->state == PA_SOURCE_SUSPENDED)
387 return 0;
388
389 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
390 }
391
392 /* Called from main context */
393 int pa_source_suspend(pa_source *s, pa_bool_t suspend) {
394 pa_source_assert_ref(s);
395 pa_assert(PA_SOURCE_IS_LINKED(s->state));
396
397 if (suspend)
398 return source_set_state(s, PA_SOURCE_SUSPENDED);
399 else
400 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
401 }
402
403 /* Called from IO thread context */
404 void pa_source_process_rewind(pa_source *s, size_t nbytes) {
405 pa_source_output *o;
406 void *state = NULL;
407
408 pa_source_assert_ref(s);
409 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
410
411 if (nbytes <= 0)
412 return;
413
414 pa_log_debug("Processing rewind...");
415
416 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
417 pa_source_output_assert_ref(o);
418 pa_source_output_process_rewind(o, nbytes);
419 }
420 }
421
422 /* Called from IO thread context */
423 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
424 pa_source_output *o;
425 void *state = NULL;
426
427 pa_source_assert_ref(s);
428 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
429 pa_assert(chunk);
430
431 if (s->thread_info.state != PA_SOURCE_RUNNING)
432 return;
433
434 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
435 pa_memchunk vchunk = *chunk;
436
437 pa_memblock_ref(vchunk.memblock);
438 pa_memchunk_make_writable(&vchunk, 0);
439
440 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
441 pa_silence_memchunk(&vchunk, &s->sample_spec);
442 else
443 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
444
445 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
446 pa_source_output_assert_ref(o);
447
448 if (!o->thread_info.direct_on_input)
449 pa_source_output_push(o, &vchunk);
450 }
451
452 pa_memblock_unref(vchunk.memblock);
453 } else {
454
455 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
456 pa_source_output_assert_ref(o);
457
458 if (!o->thread_info.direct_on_input)
459 pa_source_output_push(o, chunk);
460 }
461 }
462 }
463
464 /* Called from IO thread context */
465 void pa_source_post_direct(pa_source*s, pa_source_output *o, const pa_memchunk *chunk) {
466 pa_source_assert_ref(s);
467 pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
468 pa_source_output_assert_ref(o);
469 pa_assert(o->thread_info.direct_on_input);
470 pa_assert(chunk);
471
472 if (s->thread_info.state != PA_SOURCE_RUNNING)
473 return;
474
475 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
476 pa_memchunk vchunk = *chunk;
477
478 pa_memblock_ref(vchunk.memblock);
479 pa_memchunk_make_writable(&vchunk, 0);
480
481 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
482 pa_silence_memchunk(&vchunk, &s->sample_spec);
483 else
484 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
485
486 pa_source_output_push(o, &vchunk);
487
488 pa_memblock_unref(vchunk.memblock);
489 } else
490 pa_source_output_push(o, chunk);
491 }
492
493 /* Called from main thread */
494 pa_usec_t pa_source_get_latency(pa_source *s) {
495 pa_usec_t usec;
496
497 pa_source_assert_ref(s);
498 pa_assert(PA_SOURCE_IS_LINKED(s->state));
499
500 if (!PA_SOURCE_IS_OPENED(s->state))
501 return 0;
502
503 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
504
505 return usec;
506 }
507
508 /* Called from main thread */
509 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
510 pa_bool_t changed;
511
512 pa_source_assert_ref(s);
513 pa_assert(PA_SOURCE_IS_LINKED(s->state));
514 pa_assert(volume);
515
516 changed = !pa_cvolume_equal(volume, &s->volume);
517 s->volume = *volume;
518
519 if (s->set_volume && s->set_volume(s) < 0)
520 s->set_volume = NULL;
521
522 if (!s->set_volume)
523 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, volume, 0, NULL);
524
525 if (changed)
526 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
527 }
528
529 /* Called from main thread */
530 const pa_cvolume *pa_source_get_volume(pa_source *s) {
531 pa_source_assert_ref(s);
532 pa_assert(PA_SOURCE_IS_LINKED(s->state));
533
534 if (s->refresh_volume) {
535 pa_cvolume old_volume = s->volume;
536
537 if (s->get_volume && s->get_volume(s) < 0)
538 s->get_volume = NULL;
539
540 if (!s->get_volume)
541 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
542
543 if (!pa_cvolume_equal(&old_volume, &s->volume))
544 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
545 }
546
547 return &s->volume;
548 }
549
550 /* Called from main thread */
551 void pa_source_set_mute(pa_source *s, pa_bool_t mute) {
552 pa_bool_t changed;
553
554 pa_source_assert_ref(s);
555 pa_assert(PA_SOURCE_IS_LINKED(s->state));
556
557 changed = s->muted != mute;
558 s->muted = mute;
559
560 if (s->set_mute && s->set_mute(s) < 0)
561 s->set_mute = NULL;
562
563 if (!s->set_mute)
564 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
565
566 if (changed)
567 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
568 }
569
570 /* Called from main thread */
571 pa_bool_t pa_source_get_mute(pa_source *s) {
572
573 pa_source_assert_ref(s);
574 pa_assert(PA_SOURCE_IS_LINKED(s->state));
575
576 if (s->refresh_muted) {
577 pa_bool_t old_muted = s->muted;
578
579 if (s->get_mute && s->get_mute(s) < 0)
580 s->get_mute = NULL;
581
582 if (!s->get_mute)
583 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
584
585 if (old_muted != s->muted)
586 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
587 }
588
589 return s->muted;
590 }
591
592 /* Called from main thread */
593 void pa_source_set_description(pa_source *s, const char *description) {
594 const char *old;
595 pa_source_assert_ref(s);
596
597 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
598 return;
599
600 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
601
602 if (old && description && !strcmp(old, description))
603 return;
604
605 if (description)
606 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
607 else
608 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
609
610 if (PA_SOURCE_IS_LINKED(s->state)) {
611 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
612 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], s);
613 }
614 }
615
616 /* Called from main thread */
617 unsigned pa_source_linked_by(pa_source *s) {
618 pa_source_assert_ref(s);
619 pa_assert(PA_SOURCE_IS_LINKED(s->state));
620
621 return pa_idxset_size(s->outputs);
622 }
623
624 /* Called from main thread */
625 unsigned pa_source_used_by(pa_source *s) {
626 unsigned ret;
627
628 pa_source_assert_ref(s);
629 pa_assert(PA_SOURCE_IS_LINKED(s->state));
630
631 ret = pa_idxset_size(s->outputs);
632 pa_assert(ret >= s->n_corked);
633
634 return ret - s->n_corked;
635 }
636
637 /* Called from IO thread, except when it is not */
638 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
639 pa_source *s = PA_SOURCE(object);
640 pa_source_assert_ref(s);
641
642 switch ((pa_source_message_t) code) {
643
644 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
645 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
646
647 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
648
649 if (o->direct_on_input) {
650 o->thread_info.direct_on_input = o->direct_on_input;
651 pa_hashmap_put(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index), o);
652 }
653
654 pa_assert(!o->thread_info.attached);
655 o->thread_info.attached = TRUE;
656
657 if (o->attach)
658 o->attach(o);
659
660 pa_source_output_set_state_within_thread(o, o->state);
661
662 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
663
664 /* We don't just invalidate the requested latency here,
665 * because if we are in a move we might need to fix up the
666 * requested latency. */
667 pa_source_output_set_requested_latency_within_thread(o, o->thread_info.requested_source_latency);
668
669 return 0;
670 }
671
672 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
673 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
674
675 pa_source_output_set_state_within_thread(o, o->state);
676
677 if (o->detach)
678 o->detach(o);
679
680 pa_assert(o->thread_info.attached);
681 o->thread_info.attached = FALSE;
682
683 if (o->thread_info.direct_on_input) {
684 pa_hashmap_remove(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index));
685 o->thread_info.direct_on_input = NULL;
686 }
687
688 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
689 pa_source_output_unref(o);
690
691 pa_source_invalidate_requested_latency(s);
692
693 return 0;
694 }
695
696 case PA_SOURCE_MESSAGE_SET_VOLUME:
697 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
698 return 0;
699
700 case PA_SOURCE_MESSAGE_SET_MUTE:
701 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
702 return 0;
703
704 case PA_SOURCE_MESSAGE_GET_VOLUME:
705 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
706 return 0;
707
708 case PA_SOURCE_MESSAGE_GET_MUTE:
709 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
710 return 0;
711
712 case PA_SOURCE_MESSAGE_SET_STATE:
713 s->thread_info.state = PA_PTR_TO_UINT(userdata);
714 return 0;
715
716 case PA_SOURCE_MESSAGE_DETACH:
717
718 /* Detach all streams */
719 pa_source_detach_within_thread(s);
720 return 0;
721
722 case PA_SOURCE_MESSAGE_ATTACH:
723
724 /* Reattach all streams */
725 pa_source_attach_within_thread(s);
726 return 0;
727
728 case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY: {
729
730 pa_usec_t *usec = userdata;
731 *usec = pa_source_get_requested_latency_within_thread(s);
732
733 if (*usec == (pa_usec_t) -1)
734 *usec = s->thread_info.max_latency;
735
736 return 0;
737 }
738
739 case PA_SOURCE_MESSAGE_SET_LATENCY_RANGE: {
740 pa_usec_t *r = userdata;
741
742 pa_source_update_latency_range(s, r[0], r[1]);
743
744 return 0;
745 }
746
747 case PA_SOURCE_MESSAGE_GET_LATENCY_RANGE: {
748 pa_usec_t *r = userdata;
749
750 r[0] = s->thread_info.min_latency;
751 r[1] = s->thread_info.max_latency;
752
753 return 0;
754 }
755
756 case PA_SOURCE_MESSAGE_GET_MAX_REWIND:
757
758 *((size_t*) userdata) = s->thread_info.max_rewind;
759 return 0;
760
761 case PA_SOURCE_MESSAGE_GET_LATENCY:
762
763 if (s->monitor_of) {
764 *((pa_usec_t*) userdata) = 0;
765 return 0;
766 }
767
768 /* Implementors need to overwrite this implementation! */
769 return -1;
770
771 case PA_SOURCE_MESSAGE_MAX:
772 ;
773 }
774
775 return -1;
776 }
777
778 /* Called from main thread */
779 int pa_source_suspend_all(pa_core *c, pa_bool_t suspend) {
780 uint32_t idx;
781 pa_source *source;
782 int ret = 0;
783
784 pa_core_assert_ref(c);
785
786 for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
787 ret -= pa_source_suspend(source, suspend) < 0;
788
789 return ret;
790 }
791
792 /* Called from main thread */
793 void pa_source_detach(pa_source *s) {
794 pa_source_assert_ref(s);
795 pa_assert(PA_SOURCE_IS_LINKED(s->state));
796
797 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL) == 0);
798 }
799
800 /* Called from main thread */
801 void pa_source_attach(pa_source *s) {
802 pa_source_assert_ref(s);
803 pa_assert(PA_SOURCE_IS_LINKED(s->state));
804
805 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
806 }
807
808 /* Called from IO thread */
809 void pa_source_detach_within_thread(pa_source *s) {
810 pa_source_output *o;
811 void *state = NULL;
812
813 pa_source_assert_ref(s);
814 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
815
816 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
817 if (o->detach)
818 o->detach(o);
819 }
820
821 /* Called from IO thread */
822 void pa_source_attach_within_thread(pa_source *s) {
823 pa_source_output *o;
824 void *state = NULL;
825
826 pa_source_assert_ref(s);
827 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
828
829 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
830 if (o->attach)
831 o->attach(o);
832 }
833
834 /* Called from IO thread */
835 pa_usec_t pa_source_get_requested_latency_within_thread(pa_source *s) {
836 pa_usec_t result = (pa_usec_t) -1;
837 pa_source_output *o;
838 void *state = NULL;
839
840 pa_source_assert_ref(s);
841
842 if (s->thread_info.requested_latency_valid)
843 return s->thread_info.requested_latency;
844
845 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
846
847 if (o->thread_info.requested_source_latency != (pa_usec_t) -1 &&
848 (result == (pa_usec_t) -1 || result > o->thread_info.requested_source_latency))
849 result = o->thread_info.requested_source_latency;
850
851 if (result != (pa_usec_t) -1) {
852 if (s->thread_info.max_latency > 0 && result > s->thread_info.max_latency)
853 result = s->thread_info.max_latency;
854
855 if (s->thread_info.min_latency > 0 && result < s->thread_info.min_latency)
856 result = s->thread_info.min_latency;
857 }
858
859 s->thread_info.requested_latency = result;
860 s->thread_info.requested_latency_valid = TRUE;
861
862 return result;
863 }
864
865 /* Called from main thread */
866 pa_usec_t pa_source_get_requested_latency(pa_source *s) {
867 pa_usec_t usec;
868
869 pa_source_assert_ref(s);
870 pa_assert(PA_SOURCE_IS_LINKED(s->state));
871
872 if (!PA_SOURCE_IS_OPENED(s->state))
873 return 0;
874
875 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
876
877 return usec;
878 }
879
880 /* Called from IO thread */
881 void pa_source_set_max_rewind(pa_source *s, size_t max_rewind) {
882 pa_source_output *o;
883 void *state = NULL;
884
885 pa_source_assert_ref(s);
886
887 if (max_rewind == s->thread_info.max_rewind)
888 return;
889
890 s->thread_info.max_rewind = max_rewind;
891
892 if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
893 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
894 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
895 }
896 }
897
898 void pa_source_invalidate_requested_latency(pa_source *s) {
899 pa_source_output *o;
900 void *state = NULL;
901
902 pa_source_assert_ref(s);
903
904 s->thread_info.requested_latency_valid = FALSE;
905
906 if (s->update_requested_latency)
907 s->update_requested_latency(s);
908
909 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
910 if (o->update_source_requested_latency)
911 o->update_source_requested_latency(o);
912
913 if (s->monitor_of)
914 pa_sink_invalidate_requested_latency(s->monitor_of);
915 }
916
917 void pa_source_set_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
918 pa_source_assert_ref(s);
919
920 /* min_latency == 0: no limit
921 * min_latency == (size_t) -1: default limit
922 * min_latency anything else: specified limit
923 *
924 * Similar for max_latency */
925
926 if (min_latency == (pa_usec_t) -1)
927 min_latency = DEFAULT_MIN_LATENCY;
928
929 if (max_latency == (pa_usec_t) -1)
930 max_latency = min_latency;
931
932 pa_assert(!min_latency || !max_latency ||
933 min_latency <= max_latency);
934
935 if (PA_SOURCE_IS_LINKED(s->state)) {
936 pa_usec_t r[2];
937
938 r[0] = min_latency;
939 r[1] = max_latency;
940
941 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
942 } else {
943 s->thread_info.min_latency = min_latency;
944 s->thread_info.max_latency = max_latency;
945
946 s->thread_info.requested_latency_valid = FALSE;
947 }
948 }
949
950 void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
951 pa_source_assert_ref(s);
952 pa_assert(min_latency);
953 pa_assert(max_latency);
954
955 if (PA_SOURCE_IS_LINKED(s->state)) {
956 pa_usec_t r[2] = { 0, 0 };
957
958 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
959
960 *min_latency = r[0];
961 *max_latency = r[1];
962 } else {
963 *min_latency = s->thread_info.min_latency;
964 *max_latency = s->thread_info.max_latency;
965 }
966 }
967
968 /* Called from IO thread */
969 void pa_source_update_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
970 pa_source_output *o;
971 void *state = NULL;
972
973 pa_source_assert_ref(s);
974
975 s->thread_info.min_latency = min_latency;
976 s->thread_info.max_latency = max_latency;
977
978 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
979 if (o->update_source_latency_range)
980 o->update_source_latency_range(o);
981
982 pa_source_invalidate_requested_latency(s);
983 }
984
985 size_t pa_source_get_max_rewind(pa_source *s) {
986 size_t r;
987 pa_source_assert_ref(s);
988
989 if (!PA_SOURCE_IS_LINKED(s->state))
990 return s->thread_info.max_rewind;
991
992 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
993
994 return r;
995 }