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