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