]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
d707ad8626dce042046df022acce62349f9adf07
[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
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 static PA_DEFINE_CHECK_TYPE(pa_source, pa_msgobject);
45
46 static void source_free(pa_object *o);
47
48 pa_source* pa_source_new(
49 pa_core *core,
50 const char *driver,
51 const char *name,
52 int fail,
53 const pa_sample_spec *spec,
54 const pa_channel_map *map) {
55
56 pa_source *s;
57 char st[256];
58 pa_channel_map tmap;
59
60 pa_assert(core);
61 pa_assert(name);
62 pa_assert(spec);
63
64 pa_return_null_if_fail(pa_sample_spec_valid(spec));
65
66 if (!map)
67 pa_return_null_if_fail(map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT));
68
69 pa_return_null_if_fail(map && pa_channel_map_valid(map));
70 pa_return_null_if_fail(map->channels == spec->channels);
71 pa_return_null_if_fail(!driver || pa_utf8_valid(driver));
72 pa_return_null_if_fail(pa_utf8_valid(name) && *name);
73
74 s = pa_msgobject_new(pa_source);
75
76 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
77 pa_xfree(s);
78 return NULL;
79 }
80
81 s->parent.parent.free = source_free;
82 s->parent.process_msg = pa_source_process_msg;
83
84 s->core = core;
85 s->state = PA_SOURCE_INIT;
86 s->flags = 0;
87 s->name = pa_xstrdup(name);
88 s->description = NULL;
89 s->driver = pa_xstrdup(driver);
90 s->module = NULL;
91
92 s->sample_spec = *spec;
93 s->channel_map = *map;
94
95 s->outputs = pa_idxset_new(NULL, NULL);
96 s->n_corked = 0;
97 s->monitor_of = NULL;
98
99 pa_cvolume_reset(&s->volume, spec->channels);
100 s->muted = FALSE;
101 s->refresh_volume = s->refresh_muted = FALSE;
102
103 s->get_latency = NULL;
104 s->set_volume = NULL;
105 s->get_volume = NULL;
106 s->set_mute = NULL;
107 s->get_mute = NULL;
108 s->set_state = NULL;
109 s->userdata = NULL;
110
111 s->asyncmsgq = NULL;
112 s->rtpoll = NULL;
113
114 pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
115
116 pa_sample_spec_snprint(st, sizeof(st), spec);
117 pa_log_info("Created source %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
118
119 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
120 s->thread_info.soft_volume = s->volume;
121 s->thread_info.soft_muted = s->muted;
122 s->thread_info.state = s->state;
123
124 return s;
125 }
126
127 static int source_set_state(pa_source *s, pa_source_state_t state) {
128 int ret;
129 pa_bool_t suspend_change;
130
131 pa_assert(s);
132
133 if (s->state == state)
134 return 0;
135
136 suspend_change =
137 (s->state == PA_SOURCE_SUSPENDED && PA_SOURCE_OPENED(state)) ||
138 (PA_SOURCE_OPENED(s->state) && state == PA_SOURCE_SUSPENDED);
139
140 if (s->set_state)
141 if ((ret = s->set_state(s, state)) < 0)
142 return -1;
143
144 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
145 return -1;
146
147 s->state = state;
148
149 if (suspend_change) {
150 pa_source_output *o;
151 uint32_t idx;
152
153 /* We're suspending or resuming, tell everyone about it */
154
155 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
156 if (o->suspend)
157 o->suspend(o, state == PA_SINK_SUSPENDED);
158 }
159
160 if (state != PA_SOURCE_UNLINKED) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
161 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
162
163 return 0;
164 }
165
166 void pa_source_put(pa_source *s) {
167 pa_source_assert_ref(s);
168
169 pa_assert(s->state == PA_SINK_INIT);
170 pa_assert(s->rtpoll);
171 pa_assert(s->asyncmsgq);
172
173 pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
174
175 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
176 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], s);
177 }
178
179 void pa_source_unlink(pa_source *s) {
180 pa_bool_t linked;
181 pa_source_output *o, *j = NULL;
182
183 pa_assert(s);
184
185 /* See pa_sink_unlink() for a couple of comments how this function
186 * works. */
187
188 linked = PA_SOURCE_LINKED(s->state);
189
190 if (linked)
191 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], s);
192
193 if (s->state != PA_SOURCE_UNLINKED)
194 pa_namereg_unregister(s->core, s->name);
195 pa_idxset_remove_by_data(s->core->sources, s, NULL);
196
197 while ((o = pa_idxset_first(s->outputs, NULL))) {
198 pa_assert(o != j);
199 pa_source_output_kill(o);
200 j = o;
201 }
202
203 if (linked)
204 source_set_state(s, PA_SOURCE_UNLINKED);
205 else
206 s->state = PA_SOURCE_UNLINKED;
207
208 s->get_latency = NULL;
209 s->get_volume = NULL;
210 s->set_volume = NULL;
211 s->set_mute = NULL;
212 s->get_mute = NULL;
213 s->set_state = NULL;
214
215 if (linked) {
216 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
217 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], s);
218 }
219 }
220
221 static void source_free(pa_object *o) {
222 pa_source_output *so;
223 pa_source *s = PA_SOURCE(o);
224
225 pa_assert(s);
226 pa_assert(pa_source_refcnt(s) == 0);
227
228 if (PA_SOURCE_LINKED(s->state))
229 pa_source_unlink(s);
230
231 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
232
233 pa_idxset_free(s->outputs, NULL, NULL);
234
235 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
236 pa_source_output_unref(so);
237
238 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
239
240 pa_xfree(s->name);
241 pa_xfree(s->description);
242 pa_xfree(s->driver);
243 pa_xfree(s);
244 }
245
246 int pa_source_update_status(pa_source*s) {
247 pa_source_assert_ref(s);
248 pa_assert(PA_SOURCE_LINKED(s->state));
249
250 if (s->state == PA_SOURCE_SUSPENDED)
251 return 0;
252
253 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
254 }
255
256 int pa_source_suspend(pa_source *s, pa_bool_t suspend) {
257 pa_source_assert_ref(s);
258 pa_assert(PA_SOURCE_LINKED(s->state));
259
260 if (suspend)
261 return source_set_state(s, PA_SOURCE_SUSPENDED);
262 else
263 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
264 }
265
266 void pa_source_ping(pa_source *s) {
267 pa_source_assert_ref(s);
268 pa_assert(PA_SOURCE_LINKED(s->state));
269
270 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_PING, NULL, 0, NULL, NULL);
271 }
272
273 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
274 pa_source_output *o;
275 void *state = NULL;
276
277 pa_source_assert_ref(s);
278 pa_assert(PA_SOURCE_OPENED(s->thread_info.state));
279 pa_assert(chunk);
280
281 if (s->thread_info.state != PA_SOURCE_RUNNING)
282 return;
283
284 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
285 pa_memchunk vchunk = *chunk;
286
287 pa_memblock_ref(vchunk.memblock);
288 pa_memchunk_make_writable(&vchunk, 0);
289
290 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
291 pa_silence_memchunk(&vchunk, &s->sample_spec);
292 else
293 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
294
295 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
296 pa_source_output_push(o, &vchunk);
297
298 pa_memblock_unref(vchunk.memblock);
299 } else {
300
301 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
302 pa_source_output_push(o, chunk);
303 }
304 }
305
306 pa_usec_t pa_source_get_latency(pa_source *s) {
307 pa_usec_t usec;
308
309 pa_source_assert_ref(s);
310 pa_assert(PA_SOURCE_LINKED(s->state));
311
312 if (!PA_SOURCE_OPENED(s->state))
313 return 0;
314
315 if (s->get_latency)
316 return s->get_latency(s);
317
318 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
319 return 0;
320
321 return usec;
322 }
323
324 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
325 int changed;
326
327 pa_source_assert_ref(s);
328 pa_assert(PA_SOURCE_LINKED(s->state));
329 pa_assert(volume);
330
331 changed = !pa_cvolume_equal(volume, &s->volume);
332 s->volume = *volume;
333
334 if (s->set_volume && s->set_volume(s) < 0)
335 s->set_volume = NULL;
336
337 if (!s->set_volume)
338 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
339
340 if (changed)
341 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
342 }
343
344 const pa_cvolume *pa_source_get_volume(pa_source *s) {
345 pa_cvolume old_volume;
346
347 pa_source_assert_ref(s);
348 pa_assert(PA_SOURCE_LINKED(s->state));
349
350 old_volume = s->volume;
351
352 if (s->get_volume && s->get_volume(s) < 0)
353 s->get_volume = NULL;
354
355 if (!s->get_volume && s->refresh_volume)
356 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
357
358 if (!pa_cvolume_equal(&old_volume, &s->volume))
359 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
360
361 return &s->volume;
362 }
363
364 void pa_source_set_mute(pa_source *s, pa_bool_t mute) {
365 int changed;
366
367 pa_source_assert_ref(s);
368 pa_assert(PA_SOURCE_LINKED(s->state));
369
370 changed = s->muted != mute;
371 s->muted = mute;
372
373 if (s->set_mute && s->set_mute(s) < 0)
374 s->set_mute = NULL;
375
376 if (!s->set_mute)
377 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
378
379 if (changed)
380 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
381 }
382
383 pa_bool_t pa_source_get_mute(pa_source *s) {
384 pa_bool_t old_muted;
385
386 pa_source_assert_ref(s);
387 pa_assert(PA_SOURCE_LINKED(s->state));
388
389 old_muted = s->muted;
390
391 if (s->get_mute && s->get_mute(s) < 0)
392 s->get_mute = NULL;
393
394 if (!s->get_mute && s->refresh_muted)
395 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
396
397 if (old_muted != s->muted)
398 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
399
400 return s->muted;
401 }
402
403 void pa_source_set_module(pa_source *s, pa_module *m) {
404 pa_source_assert_ref(s);
405
406 if (m == s->module)
407 return;
408
409 s->module = m;
410
411 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
412 }
413
414 void pa_source_set_description(pa_source *s, const char *description) {
415 pa_source_assert_ref(s);
416
417 if (!description && !s->description)
418 return;
419
420 if (description && s->description && !strcmp(description, s->description))
421 return;
422
423 pa_xfree(s->description);
424 s->description = pa_xstrdup(description);
425
426 if (PA_SOURCE_LINKED(s->state)) {
427 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_DESCRIPTION_CHANGED], s);
428 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
429 }
430 }
431
432 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
433 pa_source_assert_ref(s);
434 pa_assert(q);
435
436 s->asyncmsgq = q;
437 }
438
439 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
440 pa_source_assert_ref(s);
441 pa_assert(p);
442
443 s->rtpoll = p;
444 }
445
446 unsigned pa_source_linked_by(pa_source *s) {
447 pa_source_assert_ref(s);
448 pa_assert(PA_SOURCE_LINKED(s->state));
449
450 return pa_idxset_size(s->outputs);
451 }
452
453 unsigned pa_source_used_by(pa_source *s) {
454 unsigned ret;
455
456 pa_source_assert_ref(s);
457 pa_assert(PA_SOURCE_LINKED(s->state));
458
459 ret = pa_idxset_size(s->outputs);
460 pa_assert(ret >= s->n_corked);
461
462 return ret - s->n_corked;
463 }
464
465 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
466 pa_source *s = PA_SOURCE(object);
467 pa_source_assert_ref(s);
468 pa_assert(s->thread_info.state != PA_SOURCE_UNLINKED);
469
470 switch ((pa_source_message_t) code) {
471 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
472 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
473 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
474
475 pa_assert(!o->thread_info.attached);
476 o->thread_info.attached = TRUE;
477
478 if (o->attach)
479 o->attach(o);
480
481 return 0;
482 }
483
484 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
485 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
486
487 if (o->detach)
488 o->detach(o);
489
490 pa_assert(o->thread_info.attached);
491 o->thread_info.attached = FALSE;
492
493 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
494 pa_source_output_unref(o);
495
496 return 0;
497 }
498
499 case PA_SOURCE_MESSAGE_SET_VOLUME:
500 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
501 return 0;
502
503 case PA_SOURCE_MESSAGE_SET_MUTE:
504 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
505 return 0;
506
507 case PA_SOURCE_MESSAGE_GET_VOLUME:
508 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
509 return 0;
510
511 case PA_SOURCE_MESSAGE_GET_MUTE:
512 *((pa_bool_t*) userdata) = s->thread_info.soft_muted;
513 return 0;
514
515 case PA_SOURCE_MESSAGE_PING:
516 return 0;
517
518 case PA_SOURCE_MESSAGE_SET_STATE:
519 s->thread_info.state = PA_PTR_TO_UINT(userdata);
520 return 0;
521
522 case PA_SOURCE_MESSAGE_DETACH:
523
524 /* We're detaching all our output streams so that the
525 * asyncmsgq and rtpoll fields can be changed without
526 * problems */
527 pa_source_detach_within_thread(s);
528 break;
529
530 case PA_SOURCE_MESSAGE_ATTACH:
531
532 /* Reattach all streams */
533 pa_source_attach_within_thread(s);
534 break;
535
536 case PA_SOURCE_MESSAGE_GET_LATENCY:
537 case PA_SOURCE_MESSAGE_MAX:
538 ;
539 }
540
541 return -1;
542 }
543
544 int pa_source_suspend_all(pa_core *c, pa_bool_t suspend) {
545 uint32_t idx;
546 pa_source *source;
547 int ret = 0;
548
549 pa_core_assert_ref(c);
550
551 for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
552 ret -= pa_source_suspend(source, suspend) < 0;
553
554 return ret;
555 }
556
557 void pa_source_detach(pa_source *s) {
558 pa_source_assert_ref(s);
559 pa_assert(PA_SOURCE_LINKED(s->state));
560
561 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL);
562 }
563
564 void pa_source_attach(pa_source *s) {
565 pa_source_assert_ref(s);
566 pa_assert(PA_SOURCE_LINKED(s->state));
567
568 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL);
569 }
570
571 void pa_source_detach_within_thread(pa_source *s) {
572 pa_source_output *o;
573 void *state = NULL;
574
575 pa_source_assert_ref(s);
576 pa_assert(PA_SOURCE_LINKED(s->thread_info.state));
577
578 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
579 if (o->detach)
580 o->detach(o);
581 }
582
583 void pa_source_attach_within_thread(pa_source *s) {
584 pa_source_output *o;
585 void *state = NULL;
586
587 pa_source_assert_ref(s);
588 pa_assert(PA_SOURCE_LINKED(s->thread_info.state));
589
590 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
591 if (o->attach)
592 o->attach(o);
593
594 }