]> code.delx.au - pulseaudio/blob - src/pulsecore/source.c
f7825931a0e55c14636c46ac0392fd41601456f2
[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 if (pa_hook_fire(&core->hook_sink_new, NULL) < 0) /* FIXME */
77 return NULL;
78
79 s = pa_msgobject_new(pa_source);
80
81 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
82 pa_xfree(s);
83 return NULL;
84 }
85
86 s->parent.parent.free = source_free;
87 s->parent.process_msg = pa_source_process_msg;
88
89 s->core = core;
90 s->state = PA_SOURCE_IDLE;
91 s->name = pa_xstrdup(name);
92 s->description = NULL;
93 s->driver = pa_xstrdup(driver);
94 s->module = NULL;
95
96 s->sample_spec = *spec;
97 s->channel_map = *map;
98
99 s->outputs = pa_idxset_new(NULL, NULL);
100 s->monitor_of = NULL;
101
102 pa_cvolume_reset(&s->volume, spec->channels);
103 s->muted = 0;
104 s->refresh_volume = s->refresh_muted = 0;
105
106 s->is_hardware = 0;
107
108 s->get_latency = NULL;
109 s->set_volume = NULL;
110 s->get_volume = NULL;
111 s->set_mute = NULL;
112 s->get_mute = NULL;
113 s->set_state = NULL;
114 s->userdata = NULL;
115
116 s->asyncmsgq = NULL;
117
118 r = pa_idxset_put(core->sources, s, &s->index);
119 assert(s->index != PA_IDXSET_INVALID && r >= 0);
120
121 pa_sample_spec_snprint(st, sizeof(st), spec);
122 pa_log_info("Created source %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
123
124 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
125 s->thread_info.soft_volume = s->volume;
126 s->thread_info.soft_muted = s->muted;
127 s->thread_info.state = s->state;
128
129 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
130
131 pa_hook_fire(&core->hook_source_new_post, s);
132
133 return s;
134 }
135
136 static int source_set_state(pa_source *s, pa_source_state_t state) {
137 int ret;
138
139 pa_assert(s);
140
141 if (s->state == state)
142 return 0;
143
144 if (s->set_state)
145 if ((ret = s->set_state(s, state)) < 0)
146 return -1;
147
148 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL) < 0)
149 return -1;
150
151 s->state = state;
152 return 0;
153 }
154
155 void pa_source_disconnect(pa_source *s) {
156 pa_source_output *o, *j = NULL;
157
158 pa_assert(s);
159 pa_return_if_fail(s->state != PA_SOURCE_DISCONNECTED);
160
161 pa_hook_fire(&s->core->hook_source_disconnect, s);
162
163 pa_namereg_unregister(s->core, s->name);
164 pa_idxset_remove_by_data(s->core->sources, s, NULL);
165
166 while ((o = pa_idxset_first(s->outputs, NULL))) {
167 pa_assert(o != j);
168 pa_source_output_kill(o);
169 j = o;
170 }
171
172 source_set_state(s, PA_SOURCE_DISCONNECTED);
173
174 s->get_latency = NULL;
175 s->get_volume = NULL;
176 s->set_volume = NULL;
177 s->set_mute = NULL;
178 s->get_mute = NULL;
179 s->set_state = NULL;
180
181 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
182
183 pa_hook_fire(&s->core->hook_source_disconnect_post, s);
184 }
185
186 static void source_free(pa_object *o) {
187 pa_source_output *so;
188 pa_source *s = PA_SOURCE(o);
189
190 pa_assert(s);
191 pa_assert(pa_source_refcnt(s) == 0);
192
193 if (s->state != PA_SOURCE_DISCONNECTED)
194 pa_source_disconnect(s);
195
196 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
197
198 pa_idxset_free(s->outputs, NULL, NULL);
199
200 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
201 pa_source_output_unref(so);
202
203 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
204
205 pa_xfree(s->name);
206 pa_xfree(s->description);
207 pa_xfree(s->driver);
208 pa_xfree(s);
209 }
210
211 int pa_source_update_status(pa_source*s) {
212 pa_source_assert_ref(s);
213
214 if (s->state == PA_SOURCE_SUSPENDED)
215 return 0;
216
217 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
218 }
219
220 int pa_source_suspend(pa_source *s, int suspend) {
221 pa_source_assert_ref(s);
222
223 if (suspend)
224 return source_set_state(s, PA_SOURCE_SUSPENDED);
225 else
226 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
227 }
228
229 void pa_source_ping(pa_source *s) {
230 pa_source_assert_ref(s);
231
232 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_PING, NULL, 0, NULL, NULL);
233 }
234
235 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
236 pa_source_output *o;
237 void *state = NULL;
238
239 pa_source_assert_ref(s);
240 pa_assert(chunk);
241
242 if (s->thread_info.state != PA_SOURCE_RUNNING)
243 return;
244
245 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
246 pa_memchunk vchunk = *chunk;
247
248 pa_memblock_ref(vchunk.memblock);
249 pa_memchunk_make_writable(&vchunk, 0);
250
251 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
252 pa_silence_memchunk(&vchunk, &s->sample_spec);
253 else
254 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
255
256 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
257 pa_source_output_push(o, &vchunk);
258
259 pa_memblock_unref(vchunk.memblock);
260 } else {
261
262 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
263 pa_source_output_push(o, chunk);
264
265 }
266 }
267
268 pa_usec_t pa_source_get_latency(pa_source *s) {
269 pa_usec_t usec;
270
271 pa_source_assert_ref(s);
272
273 if (s->get_latency)
274 return s->get_latency(s);
275
276 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
277 return 0;
278
279 return usec;
280 }
281
282 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
283 int changed;
284
285 pa_source_assert_ref(s);
286 pa_assert(volume);
287
288 changed = !pa_cvolume_equal(volume, &s->volume);
289 s->volume = *volume;
290
291 if (s->set_volume && s->set_volume(s) < 0)
292 s->set_volume = NULL;
293
294 if (!s->set_volume)
295 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, volume, 1), 0, NULL, pa_xfree);
296
297 if (changed)
298 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
299 }
300
301 const pa_cvolume *pa_source_get_volume(pa_source *s) {
302 pa_cvolume old_volume;
303 pa_source_assert_ref(s);
304
305 old_volume = s->volume;
306
307 if (s->get_volume && s->get_volume(s) < 0)
308 s->get_volume = NULL;
309
310 if (!s->get_volume && s->refresh_volume)
311 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, &s->volume, 0, NULL);
312
313 if (!pa_cvolume_equal(&old_volume, &s->volume))
314 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
315
316 return &s->volume;
317 }
318
319 void pa_source_set_mute(pa_source *s, int mute) {
320 int changed;
321
322 pa_source_assert_ref(s);
323
324 changed = s->muted != mute;
325
326 if (s->set_mute && s->set_mute(s) < 0)
327 s->set_mute = NULL;
328
329 if (!s->set_mute)
330 pa_asyncmsgq_post(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, PA_UINT_TO_PTR(mute), 0, NULL, NULL);
331
332 if (changed)
333 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
334 }
335
336 int pa_source_get_mute(pa_source *s) {
337 int old_muted;
338
339 pa_source_assert_ref(s);
340
341 old_muted = s->muted;
342
343 if (s->get_mute && s->get_mute(s) < 0)
344 s->get_mute = NULL;
345
346 if (!s->get_mute && s->refresh_muted)
347 pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, &s->muted, 0, NULL);
348
349 if (old_muted != s->muted)
350 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
351
352 return s->muted;
353 }
354
355 void pa_source_set_module(pa_source *s, pa_module *m) {
356 pa_source_assert_ref(s);
357
358 if (m == s->module)
359 return;
360
361 s->module = m;
362
363 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
364 }
365
366 void pa_source_set_description(pa_source *s, const char *description) {
367 pa_source_assert_ref(s);
368
369 if (!description && !s->description)
370 return;
371
372 if (description && s->description && !strcmp(description, s->description))
373 return;
374
375 pa_xfree(s->description);
376 s->description = pa_xstrdup(description);
377
378 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
379 }
380
381 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
382 pa_source_assert_ref(s);
383 pa_assert(q);
384
385 s->asyncmsgq = q;
386 }
387
388 unsigned pa_source_used_by(pa_source *s) {
389 pa_source_assert_ref(s);
390
391 return pa_idxset_size(s->outputs);
392 }
393
394 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
395 pa_source *s = PA_SOURCE(object);
396 pa_source_assert_ref(s);
397
398 switch ((pa_source_message_t) code) {
399 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
400 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
401 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
402 return 0;
403 }
404
405 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
406 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
407 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
408 pa_source_output_unref(o);
409
410 return 0;
411 }
412
413 case PA_SOURCE_MESSAGE_SET_VOLUME:
414 s->thread_info.soft_volume = *((pa_cvolume*) userdata);
415 return 0;
416
417 case PA_SOURCE_MESSAGE_SET_MUTE:
418 s->thread_info.soft_muted = PA_PTR_TO_UINT(userdata);
419 return 0;
420
421 case PA_SOURCE_MESSAGE_GET_VOLUME:
422 *((pa_cvolume*) userdata) = s->thread_info.soft_volume;
423 return 0;
424
425 case PA_SOURCE_MESSAGE_GET_MUTE:
426 *((int*) userdata) = s->thread_info.soft_muted;
427 return 0;
428
429 case PA_SOURCE_MESSAGE_PING:
430 return 0;
431
432 case PA_SOURCE_MESSAGE_SET_STATE:
433 s->thread_info.state = PA_PTR_TO_UINT(userdata);
434 return 0;
435
436 case PA_SOURCE_MESSAGE_GET_LATENCY:
437 case PA_SOURCE_MESSAGE_MAX:
438 ;
439 }
440
441 return -1;
442 }