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