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