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