2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-15 15:18:33 +00:00
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/utf8.h>
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/source-output.h>
|
|
|
|
|
#include <pulsecore/namereg.h>
|
|
|
|
|
#include <pulsecore/core-subscribe.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/sample-util.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "source.h"
|
|
|
|
|
|
2006-05-16 23:47:38 +00:00
|
|
|
#define CHECK_VALIDITY_RETURN_NULL(condition) \
|
|
|
|
|
do {\
|
|
|
|
|
if (!(condition)) \
|
|
|
|
|
return NULL; \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_source* pa_source_new(
|
2006-05-16 23:47:38 +00:00
|
|
|
pa_core *core,
|
|
|
|
|
const char *driver,
|
|
|
|
|
const char *name,
|
|
|
|
|
int fail,
|
|
|
|
|
const pa_sample_spec *spec,
|
|
|
|
|
const pa_channel_map *map) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_source *s;
|
2004-07-03 23:35:12 +00:00
|
|
|
char st[256];
|
2004-06-08 23:54:24 +00:00
|
|
|
int r;
|
2006-05-16 23:47:38 +00:00
|
|
|
pa_channel_map tmap;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(core);
|
|
|
|
|
assert(name);
|
|
|
|
|
assert(spec);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-05-16 23:47:38 +00:00
|
|
|
CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(spec));
|
|
|
|
|
|
|
|
|
|
if (!map)
|
|
|
|
|
map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
|
|
|
|
|
|
|
|
|
|
CHECK_VALIDITY_RETURN_NULL(map && pa_channel_map_valid(map));
|
|
|
|
|
CHECK_VALIDITY_RETURN_NULL(map->channels == spec->channels);
|
|
|
|
|
CHECK_VALIDITY_RETURN_NULL(!driver || pa_utf8_valid(driver));
|
|
|
|
|
CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name) && *name);
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
s = pa_xnew(pa_source, 1);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(s);
|
2004-06-27 22:42:17 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-14 20:53:25 +00:00
|
|
|
s->ref = 1;
|
2006-01-27 16:25:31 +00:00
|
|
|
s->core = core;
|
2004-09-14 20:53:25 +00:00
|
|
|
s->state = PA_SOURCE_RUNNING;
|
2004-08-04 16:39:30 +00:00
|
|
|
s->name = pa_xstrdup(name);
|
2004-07-10 20:56:38 +00:00
|
|
|
s->description = NULL;
|
2006-01-27 16:25:31 +00:00
|
|
|
s->driver = pa_xstrdup(driver);
|
2004-07-10 20:56:38 +00:00
|
|
|
s->owner = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
s->sample_spec = *spec;
|
2006-05-16 23:47:38 +00:00
|
|
|
s->channel_map = *map;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
s->outputs = pa_idxset_new(NULL, NULL);
|
2004-07-04 17:40:15 +00:00
|
|
|
s->monitor_of = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-22 14:11:23 +00:00
|
|
|
pa_cvolume_reset(&s->sw_volume, spec->channels);
|
|
|
|
|
pa_cvolume_reset(&s->hw_volume, spec->channels);
|
2006-02-23 12:04:31 +00:00
|
|
|
s->sw_muted = 0;
|
|
|
|
|
s->hw_muted = 0;
|
2006-02-22 14:11:23 +00:00
|
|
|
|
2006-07-16 17:26:55 +00:00
|
|
|
s->is_hardware = 0;
|
|
|
|
|
|
2004-09-16 00:05:56 +00:00
|
|
|
s->get_latency = NULL;
|
2004-06-15 17:05:03 +00:00
|
|
|
s->notify = NULL;
|
2006-02-22 14:11:23 +00:00
|
|
|
s->set_hw_volume = NULL;
|
|
|
|
|
s->get_hw_volume = NULL;
|
2006-02-23 12:04:31 +00:00
|
|
|
s->set_hw_mute = NULL;
|
|
|
|
|
s->get_hw_mute = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
s->userdata = NULL;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
r = pa_idxset_put(core->sources, s, &s->index);
|
|
|
|
|
assert(s->index != PA_IDXSET_INVALID && r >= 0);
|
2004-06-15 17:05:03 +00:00
|
|
|
|
2004-08-17 17:56:09 +00:00
|
|
|
pa_sample_spec_snprint(st, sizeof(st), spec);
|
2007-01-04 13:43:45 +00:00
|
|
|
pa_log_info("created %u \"%s\" with sample spec \"%s\"", s->index, s->name, st);
|
2004-08-11 00:11:12 +00:00
|
|
|
|
|
|
|
|
pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_source_disconnect(pa_source *s) {
|
|
|
|
|
pa_source_output *o, *j = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->state == PA_SOURCE_RUNNING);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-08-13 19:53:18 +00:00
|
|
|
s->state = PA_SOURCE_DISCONNECTED;
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_namereg_unregister(s->core, s->name);
|
2006-08-13 19:53:18 +00:00
|
|
|
|
|
|
|
|
pa_hook_fire(&s->core->hook_source_disconnect, s);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
while ((o = pa_idxset_first(s->outputs, NULL))) {
|
2004-06-14 20:30:50 +00:00
|
|
|
assert(o != j);
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_source_output_kill(o);
|
2004-06-14 20:30:50 +00:00
|
|
|
j = o;
|
|
|
|
|
}
|
2004-09-14 20:53:25 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_idxset_remove_by_data(s->core->sources, s, NULL);
|
2004-06-11 21:30:16 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
s->get_latency = NULL;
|
2004-09-14 20:53:25 +00:00
|
|
|
s->notify = NULL;
|
2006-02-22 14:11:23 +00:00
|
|
|
s->get_hw_volume = NULL;
|
|
|
|
|
s->set_hw_volume = NULL;
|
2006-02-23 12:04:31 +00:00
|
|
|
s->set_hw_mute = NULL;
|
|
|
|
|
s->get_hw_mute = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
|
2004-09-14 20:53:25 +00:00
|
|
|
}
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void source_free(pa_source *s) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(!s->ref);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-14 20:53:25 +00:00
|
|
|
if (s->state != PA_SOURCE_DISCONNECTED)
|
|
|
|
|
pa_source_disconnect(s);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
|
|
|
|
pa_log_info("freed %u \"%s\"", s->index, s->name);
|
2004-09-14 20:53:25 +00:00
|
|
|
|
|
|
|
|
pa_idxset_free(s->outputs, NULL, NULL);
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(s->name);
|
|
|
|
|
pa_xfree(s->description);
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_xfree(s->driver);
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(s);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_source_unref(pa_source *s) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
2004-09-14 20:53:25 +00:00
|
|
|
|
|
|
|
|
if (!(--s->ref))
|
|
|
|
|
source_free(s);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_source* pa_source_ref(pa_source *s) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-14 20:53:25 +00:00
|
|
|
s->ref++;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_source_notify(pa_source*s) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
2004-06-15 17:05:03 +00:00
|
|
|
|
|
|
|
|
if (s->notify)
|
|
|
|
|
s->notify(s);
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-18 19:31:50 +00:00
|
|
|
static int do_post(void *p, PA_GCC_UNUSED uint32_t idx, PA_GCC_UNUSED int *del, void*userdata) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_source_output *o = p;
|
2006-01-27 16:25:31 +00:00
|
|
|
const pa_memchunk *chunk = userdata;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(o);
|
|
|
|
|
assert(chunk);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_source_output_push(o, chunk);
|
2004-06-08 23:54:24 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
assert(chunk);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-09-14 20:53:25 +00:00
|
|
|
pa_source_ref(s);
|
2006-02-22 14:11:23 +00:00
|
|
|
|
2006-02-23 12:04:31 +00:00
|
|
|
if (s->sw_muted || !pa_cvolume_is_norm(&s->sw_volume)) {
|
2006-02-23 01:17:54 +00:00
|
|
|
pa_memchunk vchunk = *chunk;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-23 01:17:54 +00:00
|
|
|
pa_memblock_ref(vchunk.memblock);
|
2006-08-18 19:55:18 +00:00
|
|
|
pa_memchunk_make_writable(&vchunk, 0);
|
2006-02-23 12:04:31 +00:00
|
|
|
if (s->sw_muted)
|
|
|
|
|
pa_silence_memchunk(&vchunk, &s->sample_spec);
|
|
|
|
|
else
|
|
|
|
|
pa_volume_memchunk(&vchunk, &s->sample_spec, &s->sw_volume);
|
2006-02-23 01:17:54 +00:00
|
|
|
pa_idxset_foreach(s->outputs, do_post, &vchunk);
|
|
|
|
|
pa_memblock_unref(vchunk.memblock);
|
|
|
|
|
} else
|
|
|
|
|
pa_idxset_foreach(s->outputs, do_post, (void*) chunk);
|
2006-02-22 14:11:23 +00:00
|
|
|
|
2004-09-14 20:53:25 +00:00
|
|
|
pa_source_unref(s);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2004-06-18 00:22:37 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_source_set_owner(pa_source *s, pa_module *m) {
|
2004-07-10 20:56:38 +00:00
|
|
|
assert(s);
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s->ref >= 1);
|
2006-08-12 02:19:36 +00:00
|
|
|
|
|
|
|
|
if (m == s->owner)
|
|
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-10 20:56:38 +00:00
|
|
|
s->owner = m;
|
2006-08-12 02:19:36 +00:00
|
|
|
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
|
2004-07-10 20:56:38 +00:00
|
|
|
}
|
2004-09-16 00:05:56 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_usec_t pa_source_get_latency(pa_source *s) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
2004-09-16 00:05:56 +00:00
|
|
|
|
|
|
|
|
if (!s->get_latency)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return s->get_latency(s);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-22 14:11:23 +00:00
|
|
|
void pa_source_set_volume(pa_source *s, pa_mixer_t m, const pa_cvolume *volume) {
|
|
|
|
|
pa_cvolume *v;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 14:11:23 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
assert(volume);
|
|
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
if (m == PA_MIXER_HARDWARE && s->set_hw_volume)
|
2006-02-22 14:11:23 +00:00
|
|
|
v = &s->hw_volume;
|
|
|
|
|
else
|
|
|
|
|
v = &s->sw_volume;
|
|
|
|
|
|
|
|
|
|
if (pa_cvolume_equal(v, volume))
|
|
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 14:11:23 +00:00
|
|
|
*v = *volume;
|
|
|
|
|
|
|
|
|
|
if (v == &s->hw_volume)
|
|
|
|
|
if (s->set_hw_volume(s) < 0)
|
|
|
|
|
s->sw_volume = *volume;
|
|
|
|
|
|
|
|
|
|
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pa_cvolume *pa_source_get_volume(pa_source *s, pa_mixer_t m) {
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
|
|
|
|
|
if (m == PA_MIXER_HARDWARE && s->set_hw_volume) {
|
|
|
|
|
|
|
|
|
|
if (s->get_hw_volume)
|
|
|
|
|
s->get_hw_volume(s);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 14:11:23 +00:00
|
|
|
return &s->hw_volume;
|
|
|
|
|
} else
|
|
|
|
|
return &s->sw_volume;
|
|
|
|
|
}
|
2006-02-23 12:04:31 +00:00
|
|
|
|
|
|
|
|
void pa_source_set_mute(pa_source *s, pa_mixer_t m, int mute) {
|
|
|
|
|
int *t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-23 12:04:31 +00:00
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
if (m == PA_MIXER_HARDWARE && s->set_hw_mute)
|
2006-02-23 12:04:31 +00:00
|
|
|
t = &s->hw_muted;
|
|
|
|
|
else
|
|
|
|
|
t = &s->sw_muted;
|
|
|
|
|
|
|
|
|
|
if (!!*t == !!mute)
|
|
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-23 12:04:31 +00:00
|
|
|
*t = !!mute;
|
|
|
|
|
|
|
|
|
|
if (t == &s->hw_muted)
|
|
|
|
|
if (s->set_hw_mute(s) < 0)
|
|
|
|
|
s->sw_muted = !!mute;
|
|
|
|
|
|
|
|
|
|
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_source_get_mute(pa_source *s, pa_mixer_t m) {
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
|
|
|
|
|
if (m == PA_MIXER_HARDWARE && s->set_hw_mute) {
|
|
|
|
|
|
|
|
|
|
if (s->get_hw_mute)
|
|
|
|
|
s->get_hw_mute(s);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-23 12:04:31 +00:00
|
|
|
return s->hw_muted;
|
|
|
|
|
} else
|
|
|
|
|
return s->sw_muted;
|
|
|
|
|
}
|
2006-08-11 17:53:34 +00:00
|
|
|
|
|
|
|
|
void pa_source_set_description(pa_source *s, const char *description) {
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
|
|
|
|
|
if (!description && !s->description)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (description && s->description && !strcmp(description, s->description))
|
|
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 17:53:34 +00:00
|
|
|
pa_xfree(s->description);
|
|
|
|
|
s->description = pa_xstrdup(description);
|
|
|
|
|
|
|
|
|
|
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
|
|
|
|
|
}
|
2006-08-12 16:50:58 +00:00
|
|
|
|
|
|
|
|
unsigned pa_source_used_by(pa_source *s) {
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(s->ref >= 1);
|
|
|
|
|
|
|
|
|
|
return pa_idxset_size(s->outputs);
|
|
|
|
|
}
|