summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2008-03-15 15:21:41 +0000
committerLennart Poettering <lennart@poettering.net>2008-03-15 15:21:41 +0000
commitd6bd152b4d5072f8af22644b7e2cdfa427a7fb86 (patch)
tree7f86a84e44d8e9d7772c8604740d396061fee331 /src
parentebecf3d3e19ac56f507ce771d5c455198280a80c (diff)
commit glitch-free work
git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/glitch-free@2124 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'src')
-rw-r--r--src/modules/alsa-util.c133
-rw-r--r--src/modules/module-alsa-sink.c610
-rw-r--r--src/modules/module-alsa-source.c321
-rw-r--r--src/modules/module-combine.c66
-rw-r--r--src/modules/module-esound-sink.c14
-rw-r--r--src/modules/module-hal-detect.c8
-rw-r--r--src/modules/module-jack-sink.c25
-rw-r--r--src/modules/module-jack-source.c25
-rw-r--r--src/modules/module-ladspa-sink.c194
-rw-r--r--src/modules/module-match.c7
-rw-r--r--src/modules/module-null-sink.c16
-rw-r--r--src/modules/module-oss.c62
-rw-r--r--src/modules/module-pipe-sink.c20
-rw-r--r--src/modules/module-pipe-source.c20
-rw-r--r--src/modules/module-remap-sink.c132
-rw-r--r--src/modules/module-rescue-streams.c8
-rw-r--r--src/modules/module-sine.c46
-rw-r--r--src/modules/module-suspend-on-idle.c4
-rw-r--r--src/modules/module-tunnel.c35
-rw-r--r--src/modules/module-volume-restore.c6
-rw-r--r--src/modules/module-x11-bell.c2
-rw-r--r--src/modules/module-zeroconf-publish.c16
22 files changed, 1266 insertions, 504 deletions
diff --git a/src/modules/alsa-util.c b/src/modules/alsa-util.c
index 6afec3bc..d899eaec 100644
--- a/src/modules/alsa-util.c
+++ b/src/modules/alsa-util.c
@@ -27,6 +27,7 @@
#endif
#include <sys/types.h>
+#include <limits.h>
#include <asoundlib.h>
#include <pulse/sample.h>
@@ -290,16 +291,22 @@ int pa_alsa_set_hw_params(
pa_sample_spec *ss,
uint32_t *periods,
snd_pcm_uframes_t *period_size,
+ snd_pcm_uframes_t tsched_size,
pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched,
pa_bool_t require_exact_channel_number) {
int ret = -1;
+ snd_pcm_uframes_t _period_size = *period_size;
+ unsigned int _periods = *periods;
snd_pcm_uframes_t buffer_size;
unsigned int r = ss->rate;
unsigned int c = ss->channels;
pa_sample_format_t f = ss->format;
snd_pcm_hw_params_t *hwparams;
pa_bool_t _use_mmap = use_mmap && *use_mmap;
+ pa_bool_t _use_tsched = use_tsched && *use_tsched;
+ int dir;
pa_assert(pcm_handle);
pa_assert(ss);
@@ -308,8 +315,6 @@ int pa_alsa_set_hw_params(
snd_pcm_hw_params_alloca(&hwparams);
- buffer_size = *periods * *period_size;
-
if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
goto finish;
@@ -330,12 +335,19 @@ int pa_alsa_set_hw_params(
} else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
goto finish;
+ if (!_use_mmap)
+ _use_tsched = FALSE;
+
if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
goto finish;
if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
goto finish;
+ /* Adjust the buffer sizes, if we didn't get the rate we were asking for */
+ _period_size = (snd_pcm_uframes_t) (((uint64_t) _period_size * r) / ss->rate);
+ tsched_size = (snd_pcm_uframes_t) (((uint64_t) tsched_size * r) / ss->rate);
+
if (require_exact_channel_number) {
if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
goto finish;
@@ -344,10 +356,32 @@ int pa_alsa_set_hw_params(
goto finish;
}
- if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
- (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
+ if (_use_tsched) {
+ _period_size = tsched_size;
+ _periods = 1;
+
+ pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size) == 0);
+ pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size * 1000 / r);
+ }
+
+ buffer_size = _periods * _period_size;
+
+ if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0)
goto finish;
+ if (_periods > 0) {
+ dir = 1;
+ if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
+ dir = -1;
+ if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0)
+ goto finish;
+ }
+ }
+
+ if (_period_size > 0)
+ if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0)
+ goto finish;
+
if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
goto finish;
@@ -363,8 +397,8 @@ int pa_alsa_set_hw_params(
if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
goto finish;
- if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
- (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
+ if ((ret = snd_pcm_hw_params_get_period_size(hwparams, &_period_size, &dir)) < 0 ||
+ (ret = snd_pcm_hw_params_get_periods(hwparams, &_periods, &dir)) < 0)
goto finish;
/* If the sample rate deviates too much, we need to resample */
@@ -373,14 +407,18 @@ int pa_alsa_set_hw_params(
ss->channels = c;
ss->format = f;
- pa_assert(buffer_size > 0);
- pa_assert(*period_size > 0);
- *periods = buffer_size / *period_size;
- pa_assert(*periods > 0);
+ pa_assert(_periods > 0);
+ pa_assert(_period_size > 0);
+
+ *periods = _periods;
+ *period_size = _period_size;
if (use_mmap)
*use_mmap = _use_mmap;
+ if (use_tsched)
+ *use_tsched = _use_tsched;
+
ret = 0;
finish:
@@ -388,7 +426,7 @@ finish:
return ret;
}
-int pa_alsa_set_sw_params(snd_pcm_t *pcm) {
+int pa_alsa_set_sw_params(snd_pcm_t *pcm, size_t avail_min) {
snd_pcm_sw_params_t *swparams;
int err;
@@ -411,6 +449,11 @@ int pa_alsa_set_sw_params(snd_pcm_t *pcm) {
return err;
}
+ if ((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0) {
+ pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err));
+ return err;
+ }
+
if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
return err;
@@ -477,7 +520,9 @@ snd_pcm_t *pa_alsa_open_by_device_id(
int mode,
uint32_t *nfrags,
snd_pcm_uframes_t *period_size,
- pa_bool_t *use_mmap) {
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched) {
int i;
int direction = 1;
@@ -536,7 +581,7 @@ snd_pcm_t *pa_alsa_open_by_device_id(
try_ss.rate = ss->rate;
try_ss.format = ss->format;
- if ((err = pa_alsa_set_hw_params(pcm_handle, &try_ss, nfrags, period_size, use_mmap, TRUE)) < 0) {
+ if ((err = pa_alsa_set_hw_params(pcm_handle, &try_ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, TRUE)) < 0) {
pa_log_info("PCM device %s refused our hw parameters: %s", d, snd_strerror(err));
pa_xfree(d);
snd_pcm_close(pcm_handle);
@@ -554,7 +599,7 @@ snd_pcm_t *pa_alsa_open_by_device_id(
d = pa_sprintf_malloc("hw:%s", dev_id);
pa_log_debug("Trying %s as last resort...", d);
- pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, use_mmap);
+ pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, tsched_size, use_mmap, use_tsched);
pa_xfree(d);
return pcm_handle;
@@ -568,7 +613,9 @@ snd_pcm_t *pa_alsa_open_by_device_string(
int mode,
uint32_t *nfrags,
snd_pcm_uframes_t *period_size,
- pa_bool_t *use_mmap) {
+ snd_pcm_uframes_t tsched_size,
+ pa_bool_t *use_mmap,
+ pa_bool_t *use_tsched) {
int err;
char *d;
@@ -591,7 +638,7 @@ snd_pcm_t *pa_alsa_open_by_device_string(
return NULL;
}
- if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, use_mmap, FALSE)) < 0) {
+ if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, FALSE)) < 0) {
if (err == -EPERM) {
/* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
@@ -773,7 +820,7 @@ int pa_alsa_calc_mixer_map(snd_mixer_elem_t *elem, const pa_channel_map *channel
}
if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
- pa_log_info("Channel map has duplicate channel '%s', failling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
+ pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
return -1;
}
@@ -793,7 +840,57 @@ int pa_alsa_calc_mixer_map(snd_mixer_elem_t *elem, const pa_channel_map *channel
}
}
- pa_log_info("All %u channels can be mapped to mixer channels. Using hardware volume control.", channel_map->channels);
+ pa_log_info("All %u channels can be mapped to mixer channels.", channel_map->channels);
return 0;
}
+
+void pa_alsa_0dB_playback(snd_mixer_elem_t *elem) {
+ long min, max, v;
+
+ pa_assert(elem);
+
+ /* Try to enable 0 dB if possible. If ALSA cannot do dB, then use
+ * raw volume levels and fix them to 75% */
+
+ if (snd_mixer_selem_set_playback_dB_all(elem, 0, -1) >= 0)
+ return;
+
+ if (snd_mixer_selem_set_playback_dB_all(elem, 0, 1) >= 0)
+ return;
+
+ if (snd_mixer_selem_get_playback_volume_range(elem, &min, &max) < 0)
+ return;
+
+ v = min + ((max - min) * 3) / 4; /* 75% */
+
+ if (v <= min)
+ v = max;
+
+ snd_mixer_selem_set_playback_volume_all(elem, v);
+}
+
+void pa_alsa_0dB_capture(snd_mixer_elem_t *elem) {
+ long min, max, v;
+
+ pa_assert(elem);
+
+ /* Try to enable 0 dB if possible. If ALSA cannot do dB, then use
+ * raw volume levels and fix them to 75% */
+
+ if (snd_mixer_selem_set_capture_dB_all(elem, 0, -1) >= 0)
+ return;
+
+ if (snd_mixer_selem_set_capture_dB_all(elem, 0, 1) >= 0)
+ return;
+
+ if (snd_mixer_selem_get_capture_volume_range(elem, &min, &max) < 0)
+ return;
+
+ v = min + ((max - min) * 3) / 4; /* 75% */
+
+ if (v <= min)
+ v = max;
+
+ snd_mixer_selem_set_capture_volume_all(elem, v);
+}
diff --git a/src/modules/module-alsa-sink.c b/src/modules/module-alsa-sink.c
index 14aef7c9..7e60cead 100644
--- a/src/modules/module-alsa-sink.c
+++ b/src/modules/module-alsa-sink.c
@@ -32,6 +32,7 @@
#include <pulse/xmalloc.h>
#include <pulse/util.h>
+#include <pulse/timeval.h>
#include <pulsecore/core.h>
#include <pulsecore/module.h>
@@ -46,6 +47,8 @@
#include <pulsecore/core-error.h>
#include <pulsecore/thread-mq.h>
#include <pulsecore/rtpoll.h>
+#include <pulsecore/rtclock.h>
+#include <pulsecore/time-smoother.h>
#include "alsa-util.h"
#include "module-alsa-sink-symdef.h"
@@ -59,14 +62,19 @@ PA_MODULE_USAGE(
"device=<ALSA device> "
"device_id=<ALSA device id> "
"format=<sample format> "
- "channels=<number of channels> "
"rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
"fragments=<number of fragments> "
"fragment_size=<fragment size> "
- "channel_map=<channel map> "
- "mmap=<enable memory mapping?>");
+ "mmap=<enable memory mapping?> "
+ "tsched=<enable system timer based scheduling mode?> "
+ "tsched_buffer_size=<buffer size when using timer based scheduling> "
+ "tsched_buffer_watermark=<lower fill watermark>");
#define DEFAULT_DEVICE "default"
+#define DEFAULT_TSCHED_BUFFER_USEC (3*PA_USEC_PER_SEC)
+#define DEFAULT_TSCHED_WATERMARK_USEC (20*PA_USEC_PER_MSEC)
struct userdata {
pa_core *core;
@@ -83,33 +91,43 @@ struct userdata {
snd_mixer_t *mixer_handle;
snd_mixer_elem_t *mixer_elem;
long hw_volume_max, hw_volume_min;
+ long hw_dB_max, hw_dB_min;
+ pa_bool_t hw_dB_supported;
- size_t frame_size, fragment_size, hwbuf_size;
+ size_t frame_size, fragment_size, hwbuf_size, tsched_watermark;
unsigned nfragments;
pa_memchunk memchunk;
char *device_name;
- pa_bool_t use_mmap;
+ pa_bool_t use_mmap, use_tsched;
pa_bool_t first;
pa_rtpoll_item *alsa_rtpoll_item;
snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
+
+ pa_smoother *smoother;
+ int64_t frame_index;
+
+ snd_pcm_sframes_t hwbuf_unused_frames;
};
static const char* const valid_modargs[] = {
+ "sink_name",
"device",
"device_id",
- "sink_name",
"format",
- "channels",
"rate",
+ "channels",
+ "channel_map",
"fragments",
"fragment_size",
- "channel_map",
"mmap",
+ "tsched",
+ "tsched_buffer_size",
+ "tsched_buffer_watermark",
NULL
};
@@ -127,7 +145,12 @@ static int mmap_write(struct userdata *u) {
const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t offset, frames;
- if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
+ /* First we determine how many samples are missing to fill the
+ * buffer up to 100% */
+
+ if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
+
+ pa_log_debug("snd_pcm_avail_update: %s", snd_strerror(n));
if (n == -EPIPE) {
pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
@@ -144,14 +167,17 @@ static int mmap_write(struct userdata *u) {
return -1;
}
-/* pa_log("Got request for %i samples", (int) n); */
+ /* We only use part of the buffer that matches our
+ * dynamically requested latency */
- if (n <= 0)
+ if (PA_UNLIKELY(n <= u->hwbuf_unused_frames))
return work_done;
- frames = n;
+ frames = n = n - u->hwbuf_unused_frames;
+
+ if (PA_UNLIKELY((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0)) {
- if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
+ pa_log_debug("snd_pcm_mmap_begin: %s", snd_strerror(err));
if (err == -EPIPE) {
pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
@@ -188,7 +214,9 @@ static int mmap_write(struct userdata *u) {
* a little bit longer around? */
pa_memblock_unref_fixed(chunk.memblock);
- if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
+ if (PA_UNLIKELY((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
+
+ pa_log_debug("snd_pcm_mmap_commit: %s", snd_strerror(err));
if (err == -EPIPE) {
pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
@@ -207,10 +235,12 @@ static int mmap_write(struct userdata *u) {
work_done = 1;
- if (frames >= (snd_pcm_uframes_t) n)
- return work_done;
+ u->frame_index += frames;
-/* pa_log("wrote %i samples", (int) frames); */
+ pa_log_debug("wrote %llu frames", (unsigned long long) frames);
+
+ if (PA_LIKELY(frames >= (snd_pcm_uframes_t) n))
+ return work_done;
}
}
@@ -225,54 +255,60 @@ static int unix_write(struct userdata *u) {
for (;;) {
void *p;
- snd_pcm_sframes_t t;
- ssize_t l;
+ snd_pcm_sframes_t n, frames;
int err;
- if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
+ if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0)) {
pa_log("Failed to query DSP status data: %s", snd_strerror(err));
return -1;
}
- if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
+ if (PA_UNLIKELY(snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size))
pa_log_debug("Buffer underrun!");
- l = snd_pcm_status_get_avail(status) * u->frame_size;
+ n = snd_pcm_status_get_avail(status);
-/* pa_log("%u bytes to write", l); */
+ /* We only use part of the buffer that matches our
+ * dynamically requested latency */
- if (l <= 0)
+ if (PA_UNLIKELY(n <= u->hwbuf_unused_frames))
return work_done;
+ n -= u->hwbuf_unused_frames;
+
if (u->memchunk.length <= 0)
- pa_sink_render(u->sink, l, &u->memchunk);
+ pa_sink_render(u->sink, n * u->frame_size, &u->memchunk);
pa_assert(u->memchunk.length > 0);
+ frames = u->memchunk.length / u->frame_size;
+
+ if (frames > n)
+ frames = n;
+
p = pa_memblock_acquire(u->memchunk.memblock);
- t = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, u->memchunk.length / u->frame_size);
+ frames = snd_pcm_writei(u->pcm_handle, (const uint8_t*) p + u->memchunk.index, frames);
pa_memblock_release(u->memchunk.memblock);
-/* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
+ pa_assert(frames != 0);
- pa_assert(t != 0);
+ if (PA_UNLIKELY(frames < 0)) {
- if (t < 0) {
+ if (frames == -EPIPE)
+ pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
- if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
+ if ((frames = snd_pcm_recover(u->pcm_handle, frames, 1)) == 0)
continue;
- if (t == -EAGAIN) {
- pa_log_debug("EAGAIN");
+ if (frames == -EAGAIN)
return work_done;
- } else {
- pa_log("Failed to write data to DSP: %s", snd_strerror(t));
- return -1;
- }
+
+ pa_log("Failed to write data to DSP: %s", snd_strerror(frames));
+ return -1;
}
- u->memchunk.index += t * u->frame_size;
- u->memchunk.length -= t * u->frame_size;
+ u->memchunk.index += frames * u->frame_size;
+ u->memchunk.length -= frames * u->frame_size;
if (u->memchunk.length <= 0) {
pa_memblock_unref(u->memchunk.memblock);
@@ -281,29 +317,51 @@ static int unix_write(struct userdata *u) {
work_done = 1;
- if (t * u->frame_size >= (unsigned) l)
+ u->frame_index += frames;
+
+ if (PA_LIKELY(frames >= n))
return work_done;
}
}
-static pa_usec_t sink_get_latency(struct userdata *u) {
- pa_usec_t r = 0;
- snd_pcm_status_t *status;
- snd_pcm_sframes_t frames = 0;
+static int update_smoother(struct userdata *u) {
+ snd_pcm_sframes_t delay;
+ int64_t frames;
int err;
-
- snd_pcm_status_alloca(&status);
+ pa_usec_t now1, now2;
pa_assert(u);
pa_assert(u->pcm_handle);
- if ((err = snd_pcm_status(u->pcm_handle, status)) < 0)
+ /* Let's update the time smoother */
+ snd_pcm_avail_update(u->pcm_handle);
+
+ if (PA_UNLIKELY((err = snd_pcm_delay(u->pcm_handle, &delay)) < 0)) {
pa_log("Failed to get delay: %s", snd_strerror(err));
- else
- frames = snd_pcm_status_get_delay(status);
+ return -1;
+ }
+
+ frames = u->frame_index - delay;
+
+ pa_log_debug("frame_index = %llu, delay = %llu, p = %llu", (unsigned long long) u->frame_index, (unsigned long long) delay, (unsigned long long) frames);
- if (frames > 0)
- r = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
+ now1 = pa_rtclock_usec();
+ now2 = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
+ pa_smoother_put(u->smoother, now1, now2);
+
+ return 0;
+}
+
+static pa_usec_t sink_get_latency(struct userdata *u) {
+ pa_usec_t r = 0;
+ int64_t delay;
+
+ pa_assert(u);
+
+ delay = u->frame_index - pa_smoother_get(u->smoother, pa_rtclock_usec());
+
+ if (delay > 0)
+ r = pa_bytes_to_usec(delay * u->frame_size, &u->sink->sample_spec);
if (u->memchunk.memblock)
r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
@@ -342,6 +400,8 @@ static int suspend(struct userdata *u) {
pa_assert(u);
pa_assert(u->pcm_handle);
+ pa_smoother_pause(u->smoother, pa_rtclock_usec());
+
/* Let's suspend */
snd_pcm_drain(u->pcm_handle);
snd_pcm_close(u->pcm_handle);
@@ -357,10 +417,61 @@ static int suspend(struct userdata *u) {
return 0;
}
+static pa_usec_t hw_sleep_time(struct userdata *u) {
+ pa_usec_t usec, wm;
+
+ pa_assert(u);
+
+ usec = pa_sink_get_requested_latency(u->sink);
+
+ if (usec <= 0)
+ usec = pa_bytes_to_usec(u->hwbuf_size, &u->sink->sample_spec);
+
+ pa_log_debug("hw buffer time: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC));
+
+ wm = pa_bytes_to_usec(u->tsched_watermark, &u->sink->sample_spec);
+
+ if (usec >= wm)
+ usec -= wm;
+ else
+ usec /= 2;
+
+ pa_log_debug("after watermark: %u ms", (unsigned) (usec / PA_USEC_PER_MSEC));
+
+ return usec;
+}
+
+static int update_sw_params(struct userdata *u) {
+ size_t avail_min;
+ int err;
+
+ pa_assert(u);
+
+ if (u->use_tsched) {
+ pa_usec_t usec;
+
+ usec = hw_sleep_time(u);
+
+ avail_min = pa_usec_to_bytes(usec, &u->sink->sample_spec);
+
+ if (avail_min <= 0)
+ avail_min = 1;
+
+ } else
+ avail_min = 1;
+
+ if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) {
+ pa_log("Failed to set software parameters: %s", snd_strerror(err));
+ return err;
+ }
+
+ return 0;
+}
+
static int unsuspend(struct userdata *u) {
pa_sample_spec ss;
int err;
- pa_bool_t b;
+ pa_bool_t b, d;
unsigned nfrags;
snd_pcm_uframes_t period_size;
@@ -379,13 +490,14 @@ static int unsuspend(struct userdata *u) {
nfrags = u->nfragments;
period_size = u->fragment_size / u->frame_size;
b = u->use_mmap;
+ d = u->use_tsched;
- if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b, TRUE)) < 0) {
+ if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) {
pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
goto fail;
}
- if (b != u->use_mmap) {
+ if (b != u->use_mmap || d != u->use_tsched) {
pa_log_warn("Resume failed, couldn't get original access mode.");
goto fail;
}
@@ -400,10 +512,8 @@ static int unsuspend(struct userdata *u) {
goto fail;
}
- if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
- pa_log("Failed to set software parameters: %s", snd_strerror(err));
+ if (update_sw_params(u) < 0)
goto fail;
- }
if (build_pollfd(u) < 0)
goto fail;
@@ -425,6 +535,29 @@ fail:
return -1;
}
+static void update_hwbuf_unused_frames(struct userdata *u) {
+ pa_usec_t usec;
+ size_t b;
+
+ pa_assert(u);
+
+ if ((usec = pa_sink_get_requested_latency(u->sink)) <= 0) {
+ /* Use the full buffer if noone asked us for anything
+ * specific */
+ u->hwbuf_unused_frames = 0;
+ return;
+ }
+
+ b = pa_usec_to_bytes(usec, &u->sink->sample_spec);
+
+ /* We need at least one sample in our buffer */
+
+ if (PA_UNLIKELY(b < u->frame_size))
+ b = u->frame_size;
+
+ u->hwbuf_unused_frames = PA_LIKELY(b < u->hwbuf_size) ? ((u->hwbuf_size - b) / u->frame_size) : 0;
+}
+
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
struct userdata *u = PA_SINK(o)->userdata;
@@ -474,6 +607,14 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
}
break;
+
+ case PA_SINK_MESSAGE_ADD_INPUT:
+ case PA_SINK_MESSAGE_REMOVE_INPUT:
+ case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER: {
+ int r = pa_sink_process_msg(o, code, data, offset, chunk);
+ update_hwbuf_unused_frames(u);
+ return r;
+ }
}
return pa_sink_process_msg(o, code, data, offset, chunk);
@@ -505,18 +646,24 @@ static int sink_get_volume_cb(pa_sink *s) {
pa_assert(u->mixer_elem);
for (i = 0; i < s->sample_spec.channels; i++) {
- long set_vol, vol;
+ long alsa_vol;
pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
- if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &vol)) < 0)
- goto fail;
+ if (u->hw_dB_supported) {
+
+ if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) >= 0) {
+ s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
+ continue;
+ }
+
+ u->hw_dB_supported = FALSE;
+ }
- set_vol = (long) roundf(((float) s->volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
+ if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0)
+ goto fail;
- /* Try to avoid superfluous volume changes */
- if (set_vol != vol)
- s->volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
+ s->volume.values[i] = (pa_volume_t) roundf(((float) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
}
return 0;
@@ -524,8 +671,6 @@ static int sink_get_volume_cb(pa_sink *s) {
fail:
pa_log_error("Unable to read volume: %s", snd_strerror(err));
- s->get_volume = NULL;
- s->set_volume = NULL;
return -1;
}
@@ -543,15 +688,31 @@ static int sink_set_volume_cb(pa_sink *s) {
pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, u->mixer_map[i]));
- vol = s->volume.values[i];
+ vol = PA_MIN(s->volume.values[i], PA_VOLUME_NORM);
+
+ if (u->hw_dB_supported) {
+ alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100);
+ alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max);
+
+ if ((err = snd_mixer_selem_set_playback_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, -1)) >= 0) {
- if (vol > PA_VOLUME_NORM)
- vol = PA_VOLUME_NORM;
+ if (snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
+ s->volume.values[i] = pa_sw_volume_from_dB(alsa_vol / 100.0);
+
+ continue;
+ }
+
+ u->hw_dB_supported = FALSE;
+ }
alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
+ alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max);
if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0)
goto fail;
+
+ if (snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol) >= 0)
+ s->volume.values[i] = (pa_volume_t) roundf(((float) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
}
return 0;
@@ -559,8 +720,6 @@ static int sink_set_volume_cb(pa_sink *s) {
fail:
pa_log_error("Unable to set volume: %s", snd_strerror(err));
- s->get_volume = NULL;
- s->set_volume = NULL;
return -1;
}
@@ -573,9 +732,6 @@ static int sink_get_mute_cb(pa_sink *s) {
if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) {
pa_log_error("Unable to get switch: %s", snd_strerror(err));
-
- s->get_mute = NULL;
- s->set_mute = NULL;
return -1;
}
@@ -593,15 +749,20 @@ static int sink_set_mute_cb(pa_sink *s) {
if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) {
pa_log_error("Unable to set switch: %s", snd_strerror(err));
-
- s->get_mute = NULL;
- s->set_mute = NULL;
return -1;
}
return 0;
}
+static void sink_update_requested_latency_cb(pa_sink *s) {
+ struct userdata *u = s->userdata;
+
+ pa_assert(u);
+
+ update_sw_params(u);
+}
+
static void thread_func(void *userdata) {
struct userdata *u = userdata;
@@ -615,13 +776,50 @@ static void thread_func(void *userdata) {
pa_thread_mq_install(&u->thread_mq);
pa_rtpoll_install(u->rtpoll);
+ update_hwbuf_unused_frames(u);
+
for (;;) {
int ret;
+ pa_log_debug("loop");
+
/* Render some data and write it to the dsp */
if (PA_SINK_OPENED(u->sink->thread_info.state)) {
int work_done = 0;
+ if (u->sink->thread_info.rewind_nbytes > 0 && u->use_tsched) {
+ snd_pcm_sframes_t frames, limit, unused;
+
+ pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) u->sink->thread_info.rewind_nbytes);
+
+ frames = u->sink->thread_info.rewind_nbytes / u->frame_size;
+
+ if ((unused = snd_pcm_avail_update(u->pcm_handle)) < 0) {
+ pa_log("snd_pcm_avail_update() failed: %s", snd_strerror(unused));
+ goto fail;
+ }
+
+ limit = (u->hwbuf_size / u->frame_size) - unused;
+
+ if (frames > limit)
+ frames = limit;
+
+ pa_log_debug("Limited to %lu bytes.", (unsigned long) frames * u->frame_size);
+
+ if ((frames = snd_pcm_rewind(u->pcm_handle, frames)) < 0) {
+ pa_log("snd_pcm_rewind() failed: %s", snd_strerror(frames));
+ goto fail;
+ }
+
+ if ((u->sink->thread_info.rewind_nbytes = frames * u->frame_size) <= 0)
+ pa_log_info("Tried rewind, but was apparently not possible.");
+ else {
+ u->frame_index -= frames;
+ pa_log_debug("Rewound %lu bytes.", (unsigned long) u->sink->thread_info.rewind_nbytes);
+ pa_sink_process_rewind(u->sink);
+ }
+ }
+
if (u->use_mmap) {
if ((work_done = mmap_write(u)) < 0)
goto fail;
@@ -630,12 +828,46 @@ static void thread_func(void *userdata) {
goto fail;
}
- if (work_done && u->first) {
- pa_log_info("Starting playback.");
- snd_pcm_start(u->pcm_handle);
- u->first = FALSE;
- continue;
+ pa_log_debug("work_done = %i", work_done);
+
+ if (work_done) {
+
+ if (u->first) {
+ pa_log_info("Starting playback.");
+ snd_pcm_start(u->pcm_handle);
+ u->first = FALSE;
+
+ pa_smoother_resume(u->smoother, pa_rtclock_usec());
+ }
+
+ if (update_smoother(u) < 0)
+ goto fail;
}
+
+ if (u->use_tsched) {
+ pa_usec_t usec, cusec;
+
+ /* OK, the playback buffer is now full, let's
+ * calculate when to wake up next */
+
+ usec = hw_sleep_time(u);
+
+ pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec / PA_USEC_PER_MSEC);
+
+ /* Convert from the sound card time domain to the
+ * system time domain */
+ cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), usec);
+
+ pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC);
+
+ /* We don't trust the conversion, so we wake up whatever comes first */
+ pa_rtpoll_set_timer_relative(u->rtpoll, PA_MIN(usec, cusec));
+ }
+
+ } else if (u->use_tsched) {
+
+ /* OK, we're in an invalid state, let's disable our timers */
+ pa_rtpoll_set_timer_disabled(u->rtpoll);
}
/* Hmm, nothing to do. Let's sleep */
@@ -697,6 +929,8 @@ static void thread_func(void *userdata) {
break;
}
}
+
+ pa_log_debug("alsa revents = %i", revents);
}
}
@@ -717,16 +951,23 @@ int pa__init(pa_module*m) {
const char *dev_id;
pa_sample_spec ss;
pa_channel_map map;
- uint32_t nfrags, frag_size;
- snd_pcm_uframes_t period_size;
+ uint32_t nfrags, hwbuf_size, frag_size, tsched_size, tsched_watermark;
+ snd_pcm_uframes_t period_frames, tsched_frames;
size_t frame_size;
snd_pcm_info_t *pcm_info = NULL;
int err;
- char *t;
const char *name;
char *name_buf = NULL;
- int namereg_fail;
- pa_bool_t use_mmap = TRUE, b;
+ pa_bool_t namereg_fail;
+ pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d;
+ pa_usec_t usec;
+ pa_sink_new_data data;
+ static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
+ [SND_PCM_CLASS_GENERIC] = "sound",
+ [SND_PCM_CLASS_MULTI] = NULL,
+ [SND_PCM_CLASS_MODEM] = "modem",
+ [SND_PCM_CLASS_DIGITIZER] = NULL
+ };
snd_pcm_info_alloca(&pcm_info);
@@ -746,35 +987,60 @@ int pa__init(pa_module*m) {
frame_size = pa_frame_size(&ss);
nfrags = m->core->default_n_fragments;
- frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
+ frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
if (frag_size <= 0)
frag_size = frame_size;
+ tsched_size = pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
+ tsched_watermark = pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
- if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
+ if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
+ pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
pa_log("Failed to parse buffer metrics");
goto fail;
}
- period_size = frag_size/frame_size;
+
+ hwbuf_size = frag_size * nfrags;
+ period_frames = frag_size/frame_size;
+ tsched_frames = tsched_size/frame_size;
if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
pa_log("Failed to parse mmap argument.");
goto fail;
}
+ if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
+ pa_log("Failed to parse timer_scheduling argument.");
+ goto fail;
+ }
+
+ if (use_tsched && !pa_rtclock_hrtimer()) {
+ pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
+ use_tsched = FALSE;
+ }
+
u = pa_xnew0(struct userdata, 1);
u->core = m->core;
u->module = m;
m->userdata = u;
u->use_mmap = use_mmap;
+ u->use_tsched = use_tsched;
u->first = TRUE;
pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
u->rtpoll = pa_rtpoll_new();
u->alsa_rtpoll_item = NULL;
pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
+ u->smoother = pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC*2, DEFAULT_TSCHED_BUFFER_USEC*2, TRUE);
+ usec = pa_rtclock_usec();
+ pa_smoother_set_time_offset(u->smoother, usec);
+ pa_smoother_pause(u->smoother, usec);
+
snd_config_update_free_global();
b = use_mmap;
+ d = use_tsched;
if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
@@ -783,8 +1049,8 @@ int pa__init(pa_module*m) {
&u->device_name,
&ss, &map,
SND_PCM_STREAM_PLAYBACK,
- &nfrags, &period_size,
- &b)))
+ &nfrags, &period_frames, tsched_frames,
+ &b, &d)))
goto fail;
@@ -795,8 +1061,8 @@ int pa__init(pa_module*m) {
&u->device_name,
&ss, &map,
SND_PCM_STREAM_PLAYBACK,
- &nfrags, &period_size,
- &b)))
+ &nfrags, &period_frames, tsched_frames,
+ &b, &d)))
goto fail;
}
@@ -806,22 +1072,25 @@ int pa__init(pa_module*m) {
if (use_mmap && !b) {
pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
- u->use_mmap = use_mmap = b;
+ u->use_mmap = use_mmap = FALSE;
+ }
+
+ if (use_tsched && (!b || !d)) {
+ pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
+ u->use_tsched = use_tsched = FALSE;
}
if (u->use_mmap)
pa_log_info("Successfully enabled mmap() mode.");
+ if (u->use_tsched)
+ pa_log_info("Successfully enabled timer-based scheduling mode.");
+
if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
pa_log("Error fetching PCM info: %s", snd_strerror(err));
goto fail;
}
- if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
- pa_log("Failed to set software parameters: %s", snd_strerror(err));
- goto fail;
- }
-
/* ALSA might tweak the sample spec, so recalculate the frame size */
frame_size = pa_frame_size(&ss);
@@ -853,13 +1122,31 @@ int pa__init(pa_module*m) {
}
if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
- namereg_fail = 1;
+ namereg_fail = TRUE;
else {
name = name_buf = pa_sprintf_malloc("alsa_output.%s", u->device_name);
- namereg_fail = 0;
+ namereg_fail = FALSE;
}
- u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, name);
+ data.namereg_fail = namereg_fail;
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "alsa");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, snd_pcm_info_get_name(pcm_info));
+
+ if (class_table[snd_pcm_info_get_class(pcm_info)])
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, class_table[snd_pcm_info_get_class(pcm_info)]);
+
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap_rewrite" : (u->use_mmap ? "mmap" : "serial"));
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
pa_xfree(name_buf);
if (!u->sink) {
@@ -868,26 +1155,35 @@ int pa__init(pa_module*m) {
}
u->sink->parent.process_msg = sink_process_msg;
+ u->sink->update_requested_latency = sink_update_requested_latency_cb;
u->sink->userdata = u;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
- "ALSA PCM on %s (%s)%s",
- u->device_name,
- snd_pcm_info_get_name(pcm_info),
- use_mmap ? " via DMA" : ""));
- pa_xfree(t);
-
- u->sink->flags = PA_SINK_HARDWARE|PA_SINK_LATENCY;
u->frame_size = frame_size;
- u->fragment_size = frag_size = period_size * frame_size;
+ u->fragment_size = frag_size = period_frames * frame_size;
u->nfragments = nfrags;
u->hwbuf_size = u->fragment_size * nfrags;
+ u->hwbuf_unused_frames = 0;
+ u->tsched_watermark = tsched_watermark;
+ u->frame_index = 0;
+ u->hw_dB_supported = FALSE;
+ u->hw_dB_min = u->hw_dB_max = 0;
+ u->hw_volume_min = u->hw_volume_max = 0;
+
+ u->sink->thread_info.max_rewind = use_tsched ? u->hwbuf_size : 0;
+
+ pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms",
+ nfrags, (long unsigned) u->fragment_size,
+ (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC);
- pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
+ if (use_tsched)
+ pa_log_info("Time scheduling watermark is %0.2fms",
+ (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC);
+
+ if (update_sw_params(u) < 0)
+ goto fail;
pa_memchunk_reset(&u->memchunk);
@@ -895,17 +1191,71 @@ int pa__init(pa_module*m) {
pa_assert(u->mixer_elem);
if (snd_mixer_selem_has_playback_volume(u->mixer_elem))
- if (pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, TRUE) >= 0) {
- u->sink->get_volume = sink_get_volume_cb;
- u->sink->set_volume = sink_set_volume_cb;
- snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
- u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
+
+ if (pa_alsa_calc_mixer_map(u->mixer_elem, &map, u->mixer_map, TRUE) >= 0 &&
+ snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) >= 0) {
+
+ pa_bool_t suitable = TRUE;
+
+ pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max);
+
+ if (u->hw_volume_min > u->hw_volume_max) {
+
+ pa_log_info("Minimal volume %li larger than maximum volume %li. Strange stuff Falling back to software volume control.", u->hw_volume_min, u->hw_volume_max);
+ suitable = FALSE;
+
+ } else if (u->hw_volume_max - u->hw_volume_min < 3) {
+
+ pa_log_info("Device has less than 4 volume levels. Falling back to software volume control.");
+ suitable = FALSE;
+
+ } else if (snd_mixer_selem_get_playback_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) >= 0) {
+
+ pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", u->hw_dB_min/100.0, u->hw_dB_max/100.0);
+
+ /* Let's see if this thing actually is useful for muting */
+ if (u->hw_dB_min > -6000) {
+ pa_log_info("Device cannot attenuate for more than -60 dB (only %0.2f dB supported), falling back to software volume control.", ((double) u->hw_dB_min) / 100);
+
+ suitable = FALSE;
+ } else if (u->hw_dB_max < 0) {
+
+ pa_log_info("Device is still attenuated at maximum volume setting (%0.2f dB is maximum). Strange stuff. Falling back to software volume control.", ((double) u->hw_dB_max) / 100);
+ suitable = FALSE;
+
+ } else if (u->hw_dB_min >= u->hw_dB_max) {
+
+ pa_log_info("Minimal dB (%0.2f) larger or equal to maximum dB (%0.2f). Strange stuff. Falling back to software volume control.", ((double) u->hw_dB_min) / 100, ((double) u->hw_dB_max) / 100);
+ suitable = FALSE;
+
+ } else {
+
+ if (u->hw_dB_max > 0) {
+ /* dB > 0 means overamplification, and clipping, we don't want that here */
+ pa_log_info("Device can do overamplification for %0.2f dB. Limiting to 0 db", ((double) u->hw_dB_max) / 100);
+ u->hw_dB_max = 0;
+ }
+
+ u->hw_dB_supported = TRUE;
+ }
+ }
+
+ if (suitable) {
+ u->sink->get_volume = sink_get_volume_cb;
+ u->sink->set_volume = sink_set_volume_cb;
+ u->sink->flags |= PA_SINK_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SINK_DECIBEL_VOLUME : 0);
+ pa_log_info("Using hardware volume control. %s dB scale.", u->hw_dB_supported ? "Using" : "Not using");
+
+ } else {
+ pa_log_info("Using software volume control. Trying to reset sound card to 0 dB.");
+ pa_alsa_0dB_playback(u->mixer_elem);
+ }
}
if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
u->sink->get_mute = sink_get_mute_cb;
u->sink->set_mute = sink_set_mute_cb;
- u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
+ u->sink->flags |= PA_SINK_HW_MUTE_CTRL;
}
u->mixer_fdl = pa_alsa_fdlist_new();
@@ -926,10 +1276,21 @@ int pa__init(pa_module*m) {
}
/* Get initial mixer settings */
- if (u->sink->get_volume)
- u->sink->get_volume(u->sink);
- if (u->sink->get_mute)
- u->sink->get_mute(u->sink);
+ if (data.volume_is_set) {
+ if (u->sink->set_volume)
+ u->sink->set_volume(u->sink);
+ } else {
+ if (u->sink->get_volume)
+ u->sink->get_volume(u->sink);
+ }
+
+ if (data.muted_is_set) {
+ if (u->sink->set_mute)
+ u->sink->set_mute(u->sink);
+ } else {
+ if (u->sink->get_mute)
+ u->sink->get_mute(u->sink);
+ }
pa_sink_put(u->sink);
@@ -988,6 +1349,9 @@ void pa__done(pa_module*m) {
snd_pcm_close(u->pcm_handle);
}
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
pa_xfree(u->device_name);
pa_xfree(u);
diff --git a/src/modules/module-alsa-source.c b/src/modules/module-alsa-source.c
index 23a2f921..b2d0d43d 100644
--- a/src/modules/module-alsa-source.c
+++ b/src/modules/module-alsa-source.c
@@ -32,6 +32,7 @@
#include <pulse/xmalloc.h>
#include <pulse/util.h>
+#include <pulse/timeval.h>
#include <pulsecore/core-error.h>
#include <pulsecore/core.h>
@@ -47,6 +48,8 @@
#include <pulsecore/core-error.h>
#include <pulsecore/thread-mq.h>
#include <pulsecore/rtpoll.h>
+#include <pulsecore/time-smoother.h>
+#include <pulsecore/rtclock.h>
#include "alsa-util.h"
#include "module-alsa-source-symdef.h"
@@ -60,14 +63,19 @@ PA_MODULE_USAGE(
"device=<ALSA device> "
"device_id=<ALSA device id> "
"format=<sample format> "
- "channels=<number of channels> "
"rate=<sample rate> "
+ "channels=<number of channels> "
+ "channel_map=<channel map> "
"fragments=<number of fragments> "
"fragment_size=<fragment size> "
- "channel_map=<channel map> "
- "mmap=<enable memory mapping?>");
+ "mmap=<enable memory mapping?> "
+ "tsched=<enable system timer based scheduling mode?> "
+ "tsched_buffer_size=<buffer size when using timer based scheduling> "
+ "tsched_buffer_watermark=<upper fill watermark>");
#define DEFAULT_DEVICE "default"
+#define DEFAULT_TSCHED_BUFFER_USEC (2*PA_USEC_PER_SEC)
+#define DEFAULT_TSCHED_WATERMARK_USEC (10*PA_USEC_PER_MSEC)
struct userdata {
pa_core *core;
@@ -85,29 +93,35 @@ struct userdata {
snd_mixer_elem_t *mixer_elem;
long hw_volume_max, hw_volume_min;
- size_t frame_size, fragment_size, hwbuf_size;
+ size_t frame_size, fragment_size, hwbuf_size, tsched_watermark;
unsigned nfragments;
char *device_name;
- pa_bool_t use_mmap;
+ pa_bool_t use_mmap, use_tsched;
pa_rtpoll_item *alsa_rtpoll_item;
snd_mixer_selem_channel_id_t mixer_map[SND_MIXER_SCHN_LAST];
+
+ pa_smoother *smoother;
+ int64_t frame_index;
};
static const char* const valid_modargs[] = {
+ "source_name",
"device",
"device_id",
- "source_name",
- "channels",
- "rate",
"format",
+ "rate",
+ "channels",
+ "channel_map",
"fragments",
"fragment_size",
- "channel_map",
"mmap",
+ "tsched",
+ "tsched_buffer_size",
+ "tsched_buffer_watermark",
NULL
};
@@ -125,7 +139,7 @@ static int mmap_read(struct userdata *u) {
pa_memchunk chunk;
void *p;
- if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
+ if (PA_UNLIKELY((n = snd_pcm_avail_update(u->pcm_handle)) < 0)) {
if (n == -EPIPE)
pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
@@ -140,14 +154,12 @@ static int mmap_read(struct userdata *u) {
return -1;
}
-/* pa_log("Got request for %i samples", (int) n); */
-
- if (n <= 0)
+ if (PA_UNLIKELY(n <= 0))
return work_done;
frames = n;
- if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
+ if (PA_UNLIKELY((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0)) {
if (err == -EPIPE)
pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
@@ -182,7 +194,7 @@ static int mmap_read(struct userdata *u) {
* a little bit longer around? */
pa_memblock_unref_fixed(chunk.memblock);
- if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
+ if (PA_UNLIKELY((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0)) {
if (err == -EPIPE)
pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
@@ -199,7 +211,10 @@ static int mmap_read(struct userdata *u) {
work_done = 1;
-/* pa_log("wrote %i samples", (int) frames); */
+ u->frame_index += frames;
+
+ if (PA_LIKELY(frames >= (snd_pcm_uframes_t) n))
+ return work_done;
}
}
@@ -214,87 +229,98 @@ static int unix_read(struct userdata *u) {
for (;;) {
void *p;
- snd_pcm_sframes_t t, k;
- ssize_t l;
+ snd_pcm_sframes_t n, frames;
int err;
pa_memchunk chunk;
- if ((err = snd_pcm_status(u->pcm_handle, status)) < 0) {
+ if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0)) {
pa_log("Failed to query DSP status data: %s", snd_strerror(err));
return -1;
}
- if (snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size)
+ if (PA_UNLIKELY(snd_pcm_status_get_avail_max(status)*u->frame_size >= u->hwbuf_size))
pa_log_debug("Buffer overrun!");
- l = snd_pcm_status_get_avail(status) * u->frame_size;
+ n = snd_pcm_status_get_avail(status);
- if (l <= 0)
+ if (PA_UNLIKELY(n <= 0))
return work_done;
chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
- k = pa_memblock_get_length(chunk.memblock);
-
- if (k > l)
- k = l;
+ frames = pa_memblock_get_length(chunk.memblock) / u->frame_size;
- k = (k/u->frame_size)*u->frame_size;
+ if (frames > n)
+ frames = n;
p = pa_memblock_acquire(chunk.memblock);
- t = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, k / u->frame_size);
+ frames = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, frames);
pa_memblock_release(chunk.memblock);
-/* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
+ pa_assert(frames != 0);
- pa_assert(t != 0);
-
- if (t < 0) {
+ if (PA_UNLIKELY(frames < 0)) {
pa_memblock_unref(chunk.memblock);
- if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
+ if ((frames = snd_pcm_recover(u->pcm_handle, frames, 1)) == 0)
continue;
- if (t == -EAGAIN) {
- pa_log_debug("EAGAIN");
+ if (frames == -EAGAIN)
return work_done;
- } else {
- pa_log("Failed to read data from DSP: %s", snd_strerror(t));
- return -1;
- }
+
+ pa_log("Failed to read data from DSP: %s", snd_strerror(frames));
+ return -1;
}
chunk.index = 0;
- chunk.length = t * u->frame_size;
+ chunk.length = frames * u->frame_size;
pa_source_post(u->source, &chunk);
pa_memblock_unref(chunk.memblock);
work_done = 1;
- if (t * u->frame_size >= (unsigned) l)
+ u->frame_index += frames;
+
+ if (PA_LIKELY(frames >= n))
return work_done;
}
}
-static pa_usec_t source_get_latency(struct userdata *u) {
- pa_usec_t r = 0;
+static int update_smoother(struct userdata *u) {
snd_pcm_status_t *status;
- snd_pcm_sframes_t frames = 0;
+ int64_t frames;
int err;
- snd_pcm_status_alloca(&status);
-
pa_assert(u);
pa_assert(u->pcm_handle);
- if ((err = snd_pcm_status(u->pcm_handle, status)) < 0)
+ snd_pcm_status_alloca(&status);
+
+ /* Let's update the time smoother */
+
+ if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0)) {
pa_log("Failed to get delay: %s", snd_strerror(err));
- else
- frames = snd_pcm_status_get_delay(status);
+ return -1;
+ }
+
+ frames = u->frame_index + snd_pcm_status_get_delay(status);
+
+ pa_smoother_put(u->smoother, pa_rtclock_usec(), pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_spec));
+
+ return 0;
+}
+
+static pa_usec_t source_get_latency(struct userdata *u) {
+ pa_usec_t r = 0;
+ int64_t delay;
+
+ pa_assert(u);
- if (frames > 0)
- r = pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_spec);
+ delay = pa_smoother_get(u->smoother, pa_rtclock_usec()) - u->frame_index;
+
+ if (delay > 0)
+ r = pa_bytes_to_usec(delay * u->frame_size, &u->source->sample_spec);
return r;
}
@@ -330,6 +356,8 @@ static int suspend(struct userdata *u) {
pa_assert(u);
pa_assert(u->pcm_handle);
+ pa_smoother_pause(u->smoother, pa_rtclock_usec());
+
/* Let's suspend */
snd_pcm_close(u->pcm_handle);
u->pcm_handle = NULL;
@@ -344,10 +372,55 @@ static int suspend(struct userdata *u) {
return 0;
}
+static pa_usec_t hw_sleep_time(struct userdata *u) {
+ pa_usec_t usec;
+
+ pa_assert(u);
+
+ usec = pa_source_get_requested_latency(u->source);
+
+ if (usec <= 0)
+ usec = pa_bytes_to_usec(u->hwbuf_size, &u->source->sample_spec);
+
+ if (usec >= u->tsched_watermark)
+ usec -= u->tsched_watermark;
+ else
+ usec /= 2;
+
+ return usec;
+}
+
+static int update_sw_params(struct userdata *u) {
+ size_t avail_min;
+ int err;
+
+ pa_assert(u);
+
+ if (u->use_tsched) {
+ pa_usec_t usec;
+
+ usec = hw_sleep_time(u);
+
+ avail_min = pa_usec_to_bytes(usec, &u->source->sample_spec);
+
+ if (avail_min <= 0)
+ avail_min = 1;
+
+ } else
+ avail_min = 1;
+
+ if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) {
+ pa_log("Failed to set software parameters: %s", snd_strerror(err));
+ return err;
+ }
+
+ return 0;
+}
+
static int unsuspend(struct userdata *u) {
pa_sample_spec ss;
int err;
- pa_bool_t b;
+ pa_bool_t b, d;
unsigned nfrags;
snd_pcm_uframes_t period_size;
@@ -366,13 +439,14 @@ static int unsuspend(struct userdata *u) {
nfrags = u->nfragments;
period_size = u->fragment_size / u->frame_size;
b = u->use_mmap;
+ d = u->use_tsched;
- if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b, TRUE)) < 0) {
+ if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) {
pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
goto fail;
}
- if (b != u->use_mmap) {
+ if (b != u->use_mmap || d != u->use_tsched) {
pa_log_warn("Resume failed, couldn't get original access mode.");
goto fail;
}
@@ -387,17 +461,17 @@ static int unsuspend(struct userdata *u) {
goto fail;
}
- if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
- pa_log("Failed to set software parameters: %s", snd_strerror(err));
+ if (update_sw_params(u) < 0)
goto fail;
- }
if (build_pollfd(u) < 0)
goto fail;
+ /* FIXME: We need to reload the volume somehow */
+
snd_pcm_start(u->pcm_handle);
- /* FIXME: We need to reload the volume somehow */
+ pa_smoother_resume(u->smoother, pa_rtclock_usec());
pa_log_info("Resumed successfully...");
@@ -609,15 +683,39 @@ static void thread_func(void *userdata) {
/* Read some data and pass it to the sources */
if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
+ int work_done = 0;
if (u->use_mmap) {
- if (mmap_read(u) < 0)
+ if ((work_done = mmap_read(u)) < 0)
goto fail;
} else {
- if (unix_read(u) < 0)
+ if ((work_done = unix_read(u) < 0))
goto fail;
}
+
+ if (update_smoother(u) < 0)
+ goto fail;
+
+ if (u->use_tsched && work_done) {
+ pa_usec_t usec, cusec;
+
+ /* OK, the capture buffer is now empty, let's
+ * calculate when to wake up next */
+
+ usec = hw_sleep_time(u);
+
+ pa_log_debug("Waking up in %0.2fms (sound card clock).", (double) usec / PA_USEC_PER_MSEC);
+
+ /* Convert from the sound card time domain to the
+ * system time domain */
+ cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), usec);
+
+ pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC);
+
+ /* We don't trust the conversion, so we wake up whatever comes first */
+ pa_rtpoll_set_timer_relative(u->rtpoll, PA_MIN(usec, cusec));
+ }
}
/* Hmm, nothing to do. Let's sleep */
@@ -699,16 +797,22 @@ int pa__init(pa_module*m) {
const char *dev_id;
pa_sample_spec ss;
pa_channel_map map;
- uint32_t nfrags, frag_size;
- snd_pcm_uframes_t period_size;
+ uint32_t nfrags, frag_size, tsched_size, tsched_watermark;
+ snd_pcm_uframes_t period_frames, tsched_frames;
size_t frame_size;
snd_pcm_info_t *pcm_info = NULL;
int err;
- char *t;
const char *name;
char *name_buf = NULL;
- int namereg_fail;
- pa_bool_t use_mmap = TRUE, b;
+ pa_bool_t namereg_fail;
+ pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d;
+ pa_source_new_data data;
+ static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
+ [SND_PCM_CLASS_GENERIC] = "sound",
+ [SND_PCM_CLASS_MULTI] = NULL,
+ [SND_PCM_CLASS_MODEM] = "modem",
+ [SND_PCM_CLASS_DIGITIZER] = NULL
+ };
snd_pcm_info_alloca(&pcm_info);
@@ -728,34 +832,55 @@ int pa__init(pa_module*m) {
frame_size = pa_frame_size(&ss);
nfrags = m->core->default_n_fragments;
- frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
+ frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
if (frag_size <= 0)
frag_size = frame_size;
+ tsched_size = pa_usec_to_bytes(DEFAULT_TSCHED_BUFFER_USEC, &ss);
+ tsched_watermark = pa_usec_to_bytes(DEFAULT_TSCHED_WATERMARK_USEC, &ss);
- if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
+ if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 ||
+ pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_size", &tsched_size) < 0 ||
+ pa_modargs_get_value_u32(ma, "tsched_buffer_watermark", &tsched_watermark) < 0) {
pa_log("Failed to parse buffer metrics");
goto fail;
}
- period_size = frag_size/frame_size;
+
+ period_frames = frag_size/frame_size;
+ tsched_frames = tsched_size/frame_size;
if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
pa_log("Failed to parse mmap argument.");
goto fail;
}
+ if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
+ pa_log("Failed to parse timer_scheduling argument.");
+ goto fail;
+ }
+
+ if (use_tsched && !pa_rtclock_hrtimer()) {
+ pa_log("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
+ use_tsched = FALSE;
+ }
+
u = pa_xnew0(struct userdata, 1);
u->core = m->core;
u->module = m;
m->userdata = u;
u->use_mmap = use_mmap;
+ u->use_tsched = use_tsched;
pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
u->rtpoll = pa_rtpoll_new();
u->alsa_rtpoll_item = NULL;
pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
+ u->smoother = pa_smoother_new(DEFAULT_TSCHED_WATERMARK_USEC, DEFAULT_TSCHED_WATERMARK_USEC, TRUE);
+ pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec());
snd_config_update_free_global();
b = use_mmap;
+ d = use_tsched;
if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) {
@@ -764,8 +889,8 @@ int pa__init(pa_module*m) {
&u->device_name,
&ss, &map,
SND_PCM_STREAM_CAPTURE,
- &nfrags, &period_size,
- &b)))
+ &nfrags, &period_frames, tsched_frames,
+ &b, &d)))
goto fail;
} else {
@@ -775,8 +900,8 @@ int pa__init(pa_module*m) {
&u->device_name,
&ss, &map,
SND_PCM_STREAM_CAPTURE,
- &nfrags, &period_size,
- &b)))
+ &nfrags, &period_frames, tsched_frames,
+ &b, &d)))
goto fail;
}
@@ -788,18 +913,24 @@ int pa__init(pa_module*m) {
u->use_mmap = use_mmap = b;
}
+ if (use_tsched && (!b || !d)) {
+ pa_log_info("Cannot enabled timer-based scheduling, falling back to sound IRQ scheduling.");
+ u->use_tsched = use_tsched = FALSE;
+ }
+
if (u->use_mmap)
pa_log_info("Successfully enabled mmap() mode.");
+ if (u->use_tsched)
+ pa_log_info("Successfully enabled timer-based scheduling mode.");
+
if ((err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
pa_log("Error fetching PCM info: %s", snd_strerror(err));
goto fail;
}
- if ((err = pa_alsa_set_sw_params(u->pcm_handle)) < 0) {
- pa_log("Failed to set software parameters: %s", snd_strerror(err));
+ if (update_sw_params(u) < 0)
goto fail;
- }
/* ALSA might tweak the sample spec, so recalculate the frame size */
frame_size = pa_frame_size(&ss);
@@ -832,13 +963,31 @@ int pa__init(pa_module*m) {
}
if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
- namereg_fail = 1;
+ namereg_fail = TRUE;
else {
name = name_buf = pa_sprintf_malloc("alsa_input.%s", u->device_name);
- namereg_fail = 0;
+ namereg_fail = FALSE;
}
- u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, name);
+ data.namereg_fail = namereg_fail;
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "alsa");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, snd_pcm_info_get_name(pcm_info));
+
+ if (class_table[snd_pcm_info_get_class(pcm_info)])
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, class_table[snd_pcm_info_get_class(pcm_info)]);
+
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap_rewrite" : (u->use_mmap ? "mmap" : "serial"));
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
pa_xfree(name_buf);
if (!u->source) {
@@ -849,22 +998,15 @@ int pa__init(pa_module*m) {
u->source->parent.process_msg = source_process_msg;
u->source->userdata = u;
- pa_source_set_module(u->source, m);
pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
pa_source_set_rtpoll(u->source, u->rtpoll);
- pa_source_set_description(u->source, t = pa_sprintf_malloc(
- "ALSA PCM on %s (%s)%s",
- u->device_name,
- snd_pcm_info_get_name(pcm_info),
- use_mmap ? " via DMA" : ""));
- pa_xfree(t);
-
- u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY;
u->frame_size = frame_size;
- u->fragment_size = frag_size = period_size * frame_size;
+ u->fragment_size = frag_size = period_frames * frame_size;
u->nfragments = nfrags;
u->hwbuf_size = u->fragment_size * nfrags;
+ u->tsched_watermark = tsched_watermark;
+ u->frame_index = 0;
pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
@@ -961,6 +1103,9 @@ void pa__done(pa_module*m) {
snd_pcm_close(u->pcm_handle);
}
+ if (u->smoother)
+ pa_smoother_free(u->smoother);
+
pa_xfree(u->device_name);
pa_xfree(u);
diff --git a/src/modules/module-combine.c b/src/modules/module-combine.c
index 996cd4f6..0b17bc5a 100644
--- a/src/modules/module-combine.c
+++ b/src/modules/module-combine.c
@@ -66,7 +66,7 @@ PA_MODULE_USAGE(
"channel_map=<channel map>");
#define DEFAULT_SINK_NAME "combined"
-#define MEMBLOCKQ_MAXLENGTH (1024*170)
+#define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
#define DEFAULT_ADJUST_TIME 10
@@ -139,7 +139,7 @@ enum {
};
enum {
- SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX
+ SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
};
static void output_free(struct output *o);
@@ -203,10 +203,10 @@ static void adjust_rates(struct userdata *u) {
r += (uint32_t) (((((double) o->total_latency - target_latency))/u->adjust_time)*r/PA_USEC_PER_SEC);
if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
- pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->name, base_rate, r);
+ pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), base_rate, r);
pa_sink_input_set_rate(o->sink_input, base_rate);
} else {
- pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o->sink_input->name, r, (double) r / base_rate, (float) o->total_latency);
+ pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), r, (double) r / base_rate, (float) o->total_latency);
pa_sink_input_set_rate(o->sink_input, r);
}
}
@@ -250,6 +250,10 @@ static void thread_func(void *userdata) {
if (u->sink->thread_info.state == PA_SINK_RUNNING && !u->thread_info.active_outputs) {
struct timeval now;
+ /* Just rewind if necessary, since we are in NULL mode, we
+ * don't have to pass this on */
+ pa_sink_process_rewind(u->sink);
+
pa_rtclock_get(&now);
if (!u->thread_info.in_null_mode || pa_timeval_cmp(&u->thread_info.timestamp, &now) <= 0) {
@@ -354,27 +358,20 @@ static void request_memblock(struct output *o, size_t length) {
}
/* Called from I/O thread context */
-static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
struct output *o;
pa_sink_input_assert_ref(i);
pa_assert_se(o = i->userdata);
/* If necessary, get some new data */
- request_memblock(o, length);
-
- return pa_memblockq_peek(o->memblockq, chunk);
-}
-
-/* Called from I/O thread context */
-static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
- struct output *o;
+ request_memblock(o, nbytes);
- pa_sink_input_assert_ref(i);
- pa_assert(length > 0);
- pa_assert_se(o = i->userdata);
+ if (pa_memblockq_peek(o->memblockq, chunk) < 0)
+ return -1;
- pa_memblockq_drop(o->memblockq, length);
+ pa_memblockq_drop(o->memblockq, chunk->length);
+ return 0;
}
/* Called from I/O thread context */
@@ -440,6 +437,7 @@ static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64
pa_memblockq_flush(o->memblockq);
break;
+
}
return pa_sink_input_process_msg(obj, code, data, offset, chunk);
@@ -665,10 +663,10 @@ static void update_description(struct userdata *u) {
char *e;
if (first) {
- e = pa_sprintf_malloc("%s %s", t, o->sink->description);
+ e = pa_sprintf_malloc("%s %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
first = 0;
} else
- e = pa_sprintf_malloc("%s, %s", t, o->sink->description);
+ e = pa_sprintf_malloc("%s, %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
pa_xfree(t);
t = e;
@@ -723,12 +721,12 @@ static int output_create_sink_input(struct output *o) {
if (o->sink_input)
return 0;
- t = pa_sprintf_malloc("Simultaneous output on %s", o->sink->description);
+ t = pa_sprintf_malloc("Simultaneous output on %s", pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
pa_sink_input_new_data_init(&data);
data.sink = o->sink;
data.driver = __FILE__;
- data.name = t;
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, t);
pa_sink_input_new_data_set_sample_spec(&data, &o->userdata->sink->sample_spec);
pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map);
data.module = o->userdata->module;
@@ -736,14 +734,15 @@ static int output_create_sink_input(struct output *o) {
o->sink_input = pa_sink_input_new(o->userdata->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE);
+ pa_sink_input_new_data_done(&data);
+
pa_xfree(t);
if (!o->sink_input)
return -1;
o->sink_input->parent.process_msg = sink_input_process_msg;
- o->sink_input->peek = sink_input_peek_cb;
- o->sink_input->drop = sink_input_drop_cb;
+ o->sink_input->pop = sink_input_pop_cb;
o->sink_input->attach = sink_input_attach_cb;
o->sink_input->detach = sink_input_detach_cb;
o->sink_input->kill = sink_input_kill_cb;
@@ -775,6 +774,7 @@ static struct output *output_new(struct userdata *u, pa_sink *sink) {
pa_frame_size(&u->sink->sample_spec),
1,
0,
+ 0,
NULL);
pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
@@ -920,6 +920,7 @@ int pa__init(pa_module*m) {
pa_channel_map map;
struct output *o;
uint32_t idx;
+ pa_sink_new_data data;
pa_assert(m);
@@ -1003,7 +1004,19 @@ int pa__init(pa_module*m) {
goto fail;
}
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
+ pa_sink_new_data_init(&data);
+ data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ data.namereg_fail = FALSE;
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output");
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
pa_log("Failed to create sink");
goto fail;
}
@@ -1013,9 +1026,6 @@ int pa__init(pa_module*m) {
u->sink->set_state = sink_set_state;
u->sink->userdata = u;
- u->sink->flags = PA_SINK_LATENCY;
- pa_sink_set_module(u->sink, m);
- pa_sink_set_description(u->sink, "Simultaneous output");
pa_sink_set_rtpoll(u->sink, u->rtpoll);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
@@ -1075,7 +1085,7 @@ int pa__init(pa_module*m) {
}
}
- u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], (pa_hook_cb_t) sink_new_hook_cb, u);
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], (pa_hook_cb_t) sink_new_hook_cb, u);
}
u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], (pa_hook_cb_t) sink_unlink_hook_cb, u);
diff --git a/src/modules/module-esound-sink.c b/src/modules/module-esound-sink.c
index f9bea63d..51c76dcb 100644
--- a/src/modules/module-esound-sink.c
+++ b/src/modules/module-esound-sink.c
@@ -508,6 +508,7 @@ int pa__init(pa_module*m) {
char *t;
const char *espeaker;
uint32_t key;
+ pa_sink_new_data data;
pa_assert(m);
@@ -554,16 +555,23 @@ int pa__init(pa_module*m) {
u->state = STATE_AUTH;
u->latency = 0;
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL))) {
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
pa_log("Failed to create sink.");
goto fail;
}
u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- u->sink->flags = PA_SINK_LATENCY|PA_SINK_NETWORK;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
diff --git a/src/modules/module-hal-detect.c b/src/modules/module-hal-detect.c
index 832bc73e..44b31a59 100644
--- a/src/modules/module-hal-detect.c
+++ b/src/modules/module-hal-detect.c
@@ -372,7 +372,7 @@ static int hal_device_add_all(struct userdata *u, const char *capability) {
pa_log_debug("Not loaded device %s", udis[i]);
else {
if (d->sink_name)
- pa_scache_play_item_by_name(u->core, "pulse-coldplug", d->sink_name, PA_VOLUME_NORM, 0);
+ pa_scache_play_item_by_name(u->core, "pulse-coldplug", d->sink_name, FALSE, PA_VOLUME_NORM, NULL, NULL);
count++;
}
}
@@ -412,7 +412,7 @@ static void device_added_time_cb(pa_mainloop_api *ea, pa_time_event *ev, const s
pa_log_debug("Not loaded device %s", td->udi);
else {
if (d->sink_name)
- pa_scache_play_item_by_name(td->u->core, "pulse-hotplug", d->sink_name, PA_VOLUME_NORM, 0);
+ pa_scache_play_item_by_name(td->u->core, "pulse-hotplug", d->sink_name, FALSE, PA_VOLUME_NORM, NULL, NULL);
}
}
}
@@ -575,7 +575,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo
if (prev_suspended && !suspend) {
/* resume */
if (pa_sink_suspend(sink, 0) >= 0)
- pa_scache_play_item_by_name(u->core, "pulse-access", d->sink_name, PA_VOLUME_NORM, 0);
+ pa_scache_play_item_by_name(u->core, "pulse-access", d->sink_name, FALSE, PA_VOLUME_NORM, NULL, NULL);
else
d->acl_race_fix = 1;
@@ -643,7 +643,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo
if (prev_suspended) {
/* resume */
if (pa_sink_suspend(sink, 0) >= 0)
- pa_scache_play_item_by_name(u->core, "pulse-access", d->sink_name, PA_VOLUME_NORM, 0);
+ pa_scache_play_item_by_name(u->core, "pulse-access", d->sink_name, FALSE, PA_VOLUME_NORM, NULL, NULL);
}
}
}
diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c
index a42aa9ef..80a14a6f 100644
--- a/src/modules/module-jack-sink.c
+++ b/src/modules/module-jack-sink.c
@@ -277,6 +277,7 @@ int pa__init(pa_module*m) {
unsigned i;
const char **ports = NULL, **p;
char *t;
+ pa_sink_new_data data;
pa_assert(m);
@@ -355,20 +356,32 @@ int pa__init(pa_module*m) {
}
}
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
- pa_log("failed to create sink.");
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack");
+ if (server_name)
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client)));
+ pa_xfree(t);
+ pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client));
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log("Failed to create sink.");
goto fail;
}
u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- u->sink->flags = PA_SINK_LATENCY;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client)));
- pa_xfree(t);
jack_set_process_callback(u->client, jack_process, u);
jack_on_shutdown(u->client, jack_shutdown, u);
diff --git a/src/modules/module-jack-source.c b/src/modules/module-jack-source.c
index 4ee08bf1..a687563d 100644
--- a/src/modules/module-jack-source.c
+++ b/src/modules/module-jack-source.c
@@ -254,6 +254,7 @@ int pa__init(pa_module*m) {
unsigned i;
const char **ports = NULL, **p;
char *t;
+ pa_source_new_data data;
pa_assert(m);
@@ -326,20 +327,32 @@ int pa__init(pa_module*m) {
}
}
- if (!(u->source = pa_source_new(m->core, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
- pa_log("failed to create source.");
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack");
+ if (server_name)
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client)));
+ pa_xfree(t);
+ pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client));
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log("Failed to create source.");
goto fail;
}
u->source->parent.process_msg = source_process_msg;
u->source->userdata = u;
- u->source->flags = PA_SOURCE_LATENCY;
- pa_source_set_module(u->source, m);
pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
pa_source_set_rtpoll(u->source, u->rtpoll);
- pa_source_set_description(u->source, t = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client)));
- pa_xfree(t);
jack_set_process_callback(u->client, jack_process, u);
jack_on_shutdown(u->client, jack_shutdown, u);
diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c
index b31037b6..696b6ea6 100644
--- a/src/modules/module-ladspa-sink.c
+++ b/src/modules/module-ladspa-sink.c
@@ -78,8 +78,6 @@ struct userdata {
/* This is a dummy buffer. Every port must be connected, but we don't care
about control out ports. We connect them all to this single buffer. */
LADSPA_Data control_out;
-
- pa_memchunk memchunk;
};
static const char* const valid_modargs[] = {
@@ -107,7 +105,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
usec = 0;
- *((pa_usec_t*) data) = usec + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+ *((pa_usec_t*) data) = usec /* + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec) */;
return 0;
}
}
@@ -129,12 +127,35 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
}
/* Called from I/O thread context */
+static void sink_request_rewind(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ /* Just hand this one over to the master sink */
+ pa_sink_input_request_rewrite(u->sink_input, s->thread_info.rewind_nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ /* Just hand this one over to the master sink */
+ u->sink_input->thread_info.requested_sink_latency = pa_sink_get_requested_latency(s);
+ pa_sink_invalidate_requested_latency(u->master);
+}
+
+/* Called from I/O thread context */
static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
struct userdata *u = PA_SINK_INPUT(o)->userdata;
switch (code) {
case PA_SINK_INPUT_MESSAGE_GET_LATENCY:
- *((pa_usec_t*) data) = pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec);
+ *((pa_usec_t*) data) = 0 /*pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec)*/;
/* Fall through, the default handler will add in the extra
* latency added by the resampler */
@@ -145,87 +166,76 @@ static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t
}
/* Called from I/O thread context */
-static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
struct userdata *u;
+ float *src, *dst;
+ size_t fs;
+ unsigned n, c;
+ pa_memchunk tchunk;
pa_sink_input_assert_ref(i);
+ pa_assert(chunk);
pa_assert_se(u = i->userdata);
- if (!u->memchunk.memblock) {
- pa_memchunk tchunk;
- float *src, *dst;
- size_t fs;
- unsigned n, c;
+ pa_sink_render(u->sink, nbytes, &tchunk);
- pa_sink_render(u->sink, length, &tchunk);
+ fs = pa_frame_size(&i->sample_spec);
+ n = tchunk.length / fs;
- fs = pa_frame_size(&i->sample_spec);
- n = tchunk.length / fs;
+ pa_assert(n > 0);
- pa_assert(n > 0);
+ chunk->memblock = pa_memblock_new(i->sink->core->mempool, tchunk.length);
+ chunk->index = 0;
+ chunk->length = tchunk.length;
- u->memchunk.memblock = pa_memblock_new(i->sink->core->mempool, tchunk.length);
- u->memchunk.index = 0;
- u->memchunk.length = tchunk.length;
+ src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
+ dst = (float*) pa_memblock_acquire(chunk->memblock);
- src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
- dst = (float*) pa_memblock_acquire(u->memchunk.memblock);
+ for (c = 0; c < u->channels; c++) {
+ unsigned j;
+ float *p, *q;
- for (c = 0; c < u->channels; c++) {
- unsigned j;
- float *p, *q;
+ p = src + c;
+ q = u->input;
+ for (j = 0; j < n; j++, p += u->channels, q++)
+ *q = PA_CLAMP_UNLIKELY(*p, -1.0, 1.0);
- p = src + c;
- q = u->input;
- for (j = 0; j < n; j++, p += u->channels, q++)
- *q = PA_CLAMP_UNLIKELY(*p, -1.0, 1.0);
+ u->descriptor->run(u->handle[c], n);
- u->descriptor->run(u->handle[c], n);
-
- q = u->output;
- p = dst + c;
- for (j = 0; j < n; j++, q++, p += u->channels)
- *p = PA_CLAMP_UNLIKELY(*q, -1.0, 1.0);
- }
-
- pa_memblock_release(tchunk.memblock);
- pa_memblock_release(u->memchunk.memblock);
-
- pa_memblock_unref(tchunk.memblock);
+ q = u->output;
+ p = dst + c;
+ for (j = 0; j < n; j++, q++, p += u->channels)
+ *p = PA_CLAMP_UNLIKELY(*q, -1.0, 1.0);
}
- pa_assert(u->memchunk.length > 0);
- pa_assert(u->memchunk.memblock);
+ pa_memblock_release(tchunk.memblock);
+ pa_memblock_release(chunk->memblock);
- *chunk = u->memchunk;
- pa_memblock_ref(chunk->memblock);
+ pa_memblock_unref(tchunk.memblock);
return 0;
}
/* Called from I/O thread context */
-static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
+static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) {
struct userdata *u;
pa_sink_input_assert_ref(i);
pa_assert_se(u = i->userdata);
- pa_assert(length > 0);
+ pa_assert(nbytes > 0);
- if (u->memchunk.memblock) {
+ u->sink->thread_info.rewind_nbytes = nbytes;
+ pa_sink_process_rewind(u->sink);
+}
- if (length < u->memchunk.length) {
- u->memchunk.index += length;
- u->memchunk.length -= length;
- return;
- }
+/* Called from I/O thread context */
+static void sink_input_set_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
- pa_memblock_unref(u->memchunk.memblock);
- length -= u->memchunk.length;
- pa_memchunk_reset(&u->memchunk);
- }
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
- if (length > 0)
- pa_sink_skip(u->sink, length);
+ pa_sink_set_max_rewind(u->sink, nbytes);
}
/* Called from I/O thread context */
@@ -275,8 +285,10 @@ int pa__init(pa_module*m) {
pa_channel_map map;
pa_modargs *ma;
char *t;
+ const char *z;
pa_sink *master;
- pa_sink_input_new_data data;
+ pa_sink_input_new_data sink_input_data;
+ pa_sink_new_data sink_data;
const char *plugin, *label;
LADSPA_Descriptor_Function descriptor_func;
const char *e, *cdata;
@@ -284,7 +296,6 @@ int pa__init(pa_module*m) {
unsigned long input_port, output_port, p, j, n_control;
unsigned c;
pa_bool_t *use_default = NULL;
- char *default_sink_name = NULL;
pa_assert(m);
@@ -325,7 +336,8 @@ int pa__init(pa_module*m) {
u->module = m;
m->userdata = u;
u->master = master;
- pa_memchunk_reset(&u->memchunk);
+ u->sink = NULL;
+ u->sink_input = NULL;
if (!(e = getenv("LADSPA_PATH")))
e = LADSPA_PATH;
@@ -583,40 +595,65 @@ int pa__init(pa_module*m) {
for (c = 0; c < u->channels; c++)
d->activate(u->handle[c]);
- default_sink_name = pa_sprintf_malloc("%s.ladspa", master->name);
-
/* Create sink */
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", default_sink_name), 0, &ss, &map))) {
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
+ sink_data.namereg_fail = FALSE;
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &map);
+ z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("LADSPA Plugin %s on %s", label, z ? z : master->name));
+ pa_xfree(t);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
+ pa_proplist_sets(sink_data.proplist, "device.ladspa.unique_id", t = pa_sprintf_malloc("%lu", (unsigned long) d->UniqueID));
+ pa_xfree(t);
+
+ u->sink = pa_sink_new(m->core, &sink_data, 0);
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
pa_log("Failed to create sink.");
goto fail;
}
u->sink->parent.process_msg = sink_process_msg;
u->sink->set_state = sink_set_state;
+ u->sink->update_requested_latency = sink_update_requested_latency;
+ u->sink->request_rewind = sink_request_rewind;
u->sink->userdata = u;
u->sink->flags = PA_SINK_LATENCY;
- pa_sink_set_module(u->sink, m);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc("LADSPA plugin '%s' on '%s'", label, master->description));
- pa_xfree(t);
pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
pa_sink_set_rtpoll(u->sink, master->rtpoll);
/* Create sink input */
- pa_sink_input_new_data_init(&data);
- data.sink = u->master;
- data.driver = __FILE__;
- data.name = "LADSPA Stream";
- pa_sink_input_new_data_set_sample_spec(&data, &ss);
- pa_sink_input_new_data_set_channel_map(&data, &map);
- data.module = m;
-
- if (!(u->sink_input = pa_sink_input_new(m->core, &data, PA_SINK_INPUT_DONT_MOVE)))
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ sink_input_data.sink = u->master;
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "routing");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
+
+ u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
goto fail;
u->sink_input->parent.process_msg = sink_input_process_msg;
- u->sink_input->peek = sink_input_peek_cb;
- u->sink_input->drop = sink_input_drop_cb;
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->rewind = sink_input_rewind_cb;
+ u->sink_input->set_max_rewind = sink_input_set_max_rewind_cb;
u->sink_input->kill = sink_input_kill_cb;
u->sink_input->attach = sink_input_attach_cb;
u->sink_input->detach = sink_input_detach_cb;
@@ -628,7 +665,6 @@ int pa__init(pa_module*m) {
pa_modargs_free(ma);
pa_xfree(use_default);
- pa_xfree(default_sink_name);
return 0;
@@ -637,7 +673,6 @@ fail:
pa_modargs_free(ma);
pa_xfree(use_default);
- pa_xfree(default_sink_name);
pa__done(m);
@@ -663,9 +698,6 @@ void pa__done(pa_module*m) {
pa_sink_unref(u->sink);
}
- if (u->memchunk.memblock)
- pa_memblock_unref(u->memchunk.memblock);
-
for (c = 0; c < u->channels; c++)
if (u->handle[c]) {
if (u->descriptor->deactivate)
diff --git a/src/modules/module-match.c b/src/modules/module-match.c
index ed5f3076..0411dcdc 100644
--- a/src/modules/module-match.c
+++ b/src/modules/module-match.c
@@ -166,6 +166,7 @@ static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, v
struct userdata *u = userdata;
pa_sink_input *si;
struct rule *r;
+ const char *n;
pa_assert(c);
pa_assert(u);
@@ -176,13 +177,13 @@ static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, v
if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
return;
- if (!si->name)
+ if (!(n = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_NAME)))
return;
for (r = u->rules; r; r = r->next) {
- if (!regexec(&r->regex, si->name, 0, NULL, 0)) {
+ if (!regexec(&r->regex, n, 0, NULL, 0)) {
pa_cvolume cv;
- pa_log_debug("changing volume of sink input '%s' to 0x%03x", si->name, r->volume);
+ pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume);
pa_cvolume_set(&cv, si->sample_spec.channels, r->volume);
pa_sink_input_set_volume(si, &cv);
}
diff --git a/src/modules/module-null-sink.c b/src/modules/module-null-sink.c
index de35fff9..6e59c62c 100644
--- a/src/modules/module-null-sink.c
+++ b/src/modules/module-null-sink.c
@@ -169,6 +169,7 @@ int pa__init(pa_module*m) {
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
+ pa_sink_new_data data;
pa_assert(m);
@@ -191,7 +192,18 @@ int pa__init(pa_module*m) {
u->rtpoll = pa_rtpoll_new();
pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "NULL sink"));
+
+ u->sink = pa_sink_new(m->core, &data, 0);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
pa_log("Failed to create sink.");
goto fail;
}
@@ -200,10 +212,8 @@ int pa__init(pa_module*m) {
u->sink->userdata = u;
u->sink->flags = PA_SINK_LATENCY;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
- pa_sink_set_description(u->sink, pa_modargs_get_value(ma, "description", "NULL sink"));
u->block_size = pa_bytes_per_second(&ss) / 20; /* 50 ms */
if (u->block_size <= 0)
diff --git a/src/modules/module-oss.c b/src/modules/module-oss.c
index a7df8a0c..149c70d5 100644
--- a/src/modules/module-oss.c
+++ b/src/modules/module-oss.c
@@ -1143,9 +1143,9 @@ int pa__init(pa_module*m) {
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
- char hwdesc[64], *t;
+ char hwdesc[64];
const char *name;
- int namereg_fail;
+ pa_bool_t namereg_fail;
pa_assert(m);
@@ -1258,6 +1258,7 @@ int pa__init(pa_module*m) {
u->out_hwbuf_size = u->out_nfrags * u->out_fragment_size;
if (mode != O_WRONLY) {
+ pa_source_new_data data;
char *name_buf = NULL;
if (use_mmap) {
@@ -1270,14 +1271,28 @@ int pa__init(pa_module*m) {
}
if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
- namereg_fail = 1;
+ namereg_fail = TRUE;
else {
name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
- namereg_fail = 0;
+ namereg_fail = FALSE;
}
- u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, name);
+ data.namereg_fail = namereg_fail;
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, dev);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "oss");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
pa_xfree(name_buf);
+
if (!u->source) {
pa_log("Failed to create source object");
goto fail;
@@ -1286,18 +1301,8 @@ int pa__init(pa_module*m) {
u->source->parent.process_msg = source_process_msg;
u->source->userdata = u;
- pa_source_set_module(u->source, m);
pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
pa_source_set_rtpoll(u->source, u->rtpoll);
- pa_source_set_description(u->source, t = pa_sprintf_malloc(
- "OSS PCM on %s%s%s%s%s",
- dev,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : "",
- use_mmap ? " via DMA" : ""));
- pa_xfree(t);
- u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY;
u->source->refresh_volume = TRUE;
if (use_mmap)
@@ -1305,6 +1310,7 @@ int pa__init(pa_module*m) {
}
if (mode != O_RDONLY) {
+ pa_sink_new_data data;
char *name_buf = NULL;
if (use_mmap) {
@@ -1331,8 +1337,22 @@ int pa__init(pa_module*m) {
namereg_fail = 0;
}
- u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, name);
+ data.namereg_fail = namereg_fail;
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, dev);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "oss");
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
pa_xfree(name_buf);
+
if (!u->sink) {
pa_log("Failed to create sink object");
goto fail;
@@ -1341,18 +1361,8 @@ int pa__init(pa_module*m) {
u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
- "OSS PCM on %s%s%s%s%s",
- dev,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : "",
- use_mmap ? " via DMA" : ""));
- pa_xfree(t);
- u->sink->flags = PA_SINK_HARDWARE|PA_SINK_LATENCY;
u->sink->refresh_volume = TRUE;
if (use_mmap)
diff --git a/src/modules/module-pipe-sink.c b/src/modules/module-pipe-sink.c
index e720c8ad..73a52112 100644
--- a/src/modules/module-pipe-sink.c
+++ b/src/modules/module-pipe-sink.c
@@ -207,6 +207,7 @@ int pa__init(pa_module*m) {
pa_modargs *ma;
char *t;
struct pollfd *pollfd;
+ pa_sink_new_data data;
pa_assert(m);
@@ -251,20 +252,29 @@ int pa__init(pa_module*m) {
goto fail;
}
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("Unix FIFO sink %s", u->filename));
+ pa_xfree(t);
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
pa_log("Failed to create sink.");
goto fail;
}
u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- u->sink->flags = PA_SINK_LATENCY;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Unix FIFO sink '%s'", u->filename));
- pa_xfree(t);
u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
diff --git a/src/modules/module-pipe-source.c b/src/modules/module-pipe-source.c
index 02935649..cf88c823 100644
--- a/src/modules/module-pipe-source.c
+++ b/src/modules/module-pipe-source.c
@@ -184,6 +184,7 @@ int pa__init(pa_module*m) {
pa_modargs *ma;
char *t;
struct pollfd *pollfd;
+ pa_source_new_data data;
pa_assert(m);
@@ -228,19 +229,28 @@ int pa__init(pa_module*m) {
goto fail;
}
- if (!(u->source = pa_source_new(m->core, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("Unix FIFO source %s", u->filename));
+ pa_xfree(t);
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+
+ u->source = pa_source_new(m->core, &data, 0);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
pa_log("Failed to create source.");
goto fail;
}
u->source->userdata = u;
- u->source->flags = 0;
- pa_source_set_module(u->source, m);
pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
pa_source_set_rtpoll(u->source, u->rtpoll);
- pa_source_set_description(u->source, t = pa_sprintf_malloc("Unix FIFO source '%s'", u->filename));
- pa_xfree(t);
u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
diff --git a/src/modules/module-remap-sink.c b/src/modules/module-remap-sink.c
index 39a9245d..1c97a824 100644
--- a/src/modules/module-remap-sink.c
+++ b/src/modules/module-remap-sink.c
@@ -59,8 +59,6 @@ struct userdata {
pa_sink *sink, *master;
pa_sink_input *sink_input;
-
- pa_memchunk memchunk;
};
static const char* const valid_modargs[] = {
@@ -86,7 +84,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
usec = 0;
- *((pa_usec_t*) data) = usec + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+ *((pa_usec_t*) data) = usec/* + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec)*/;
return 0;
}
}
@@ -108,12 +106,34 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
}
/* Called from I/O thread context */
+static void sink_request_rewind(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ pa_sink_input_request_rewrite(u->sink_input, s->thread_info.rewind_nbytes);
+}
+
+/* Called from I/O thread context */
+static void sink_update_requested_latency(pa_sink *s) {
+ struct userdata *u;
+
+ pa_sink_assert_ref(s);
+ pa_assert_se(u = s->userdata);
+
+ /* Just hand this one over to the master sink */
+ u->sink_input->thread_info.requested_sink_latency = pa_sink_get_requested_latency(s);
+ pa_sink_invalidate_requested_latency(u->master);
+}
+
+/* Called from I/O thread context */
static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
struct userdata *u = PA_SINK_INPUT(o)->userdata;
switch (code) {
case PA_SINK_INPUT_MESSAGE_GET_LATENCY:
- *((pa_usec_t*) data) = pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec);
+ *((pa_usec_t*) data) = 0; /*pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec);*/
/* Fall through, the default handler will add in the extra
* latency added by the resampler */
@@ -123,45 +143,41 @@ static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t
return pa_sink_input_process_msg(o, code, data, offset, chunk);
}
+static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes);
+
/* Called from I/O thread context */
-static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
struct userdata *u;
pa_sink_input_assert_ref(i);
+ pa_assert(chunk);
pa_assert_se(u = i->userdata);
- if (!u->memchunk.memblock)
- pa_sink_render(u->sink, length, &u->memchunk);
+ pa_sink_render(u->sink, nbytes, chunk);
- pa_assert(u->memchunk.memblock);
- *chunk = u->memchunk;
- pa_memblock_ref(chunk->memblock);
return 0;
}
/* Called from I/O thread context */
-static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
+static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) {
struct userdata *u;
pa_sink_input_assert_ref(i);
pa_assert_se(u = i->userdata);
- pa_assert(length > 0);
+ pa_assert(nbytes > 0);
- if (u->memchunk.memblock) {
+ u->sink->thread_info.rewind_nbytes = nbytes;
+ pa_sink_process_rewind(u->sink);
+}
- if (length < u->memchunk.length) {
- u->memchunk.index += length;
- u->memchunk.length -= length;
- return;
- }
+/* Called from I/O thread context */
+static void sink_input_set_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+ struct userdata *u;
- pa_memblock_unref(u->memchunk.memblock);
- length -= u->memchunk.length;
- pa_memchunk_reset(&u->memchunk);
- }
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
- if (length > 0)
- pa_sink_skip(u->sink, length);
+ pa_sink_set_max_rewind(u->sink, nbytes);
}
/* Called from I/O thread context */
@@ -211,9 +227,10 @@ int pa__init(pa_module*m) {
pa_channel_map sink_map, stream_map;
pa_modargs *ma;
char *t;
+ const char *k;
pa_sink *master;
- pa_sink_input_new_data data;
- char *default_sink_name = NULL;
+ pa_sink_input_new_data sink_input_data;
+ pa_sink_new_data sink_data;
pa_assert(m);
@@ -245,47 +262,68 @@ int pa__init(pa_module*m) {
goto fail;
}
+ if (pa_channel_map_equal(&stream_map, &master->channel_map))
+ pa_log_warn("No remapping configured, proceeding nonetheless!");
+
u = pa_xnew0(struct userdata, 1);
u->core = m->core;
u->module = m;
m->userdata = u;
u->master = master;
- pa_memchunk_reset(&u->memchunk);
-
- default_sink_name = pa_sprintf_malloc("%s.remapped", master->name);
+ u->sink = NULL;
+ u->sink_input = NULL;
/* Create sink */
- if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", default_sink_name), 0, &ss, &sink_map))) {
+ pa_sink_new_data_init(&sink_data);
+ sink_data.driver = __FILE__;
+ sink_data.module = m;
+ if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
+ sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
+ pa_sink_new_data_set_sample_spec(&sink_data, &ss);
+ pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
+ k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("Remapped %s", k ? k : master->name));
+ pa_xfree(t);
+ pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
+
+ u->sink = pa_sink_new(m->core, &sink_data, 0);
+ pa_sink_new_data_done(&sink_data);
+
+ if (!u->sink) {
pa_log("Failed to create sink.");
goto fail;
}
u->sink->parent.process_msg = sink_process_msg;
u->sink->set_state = sink_set_state;
+ u->sink->update_requested_latency = sink_update_requested_latency;
+ u->sink->request_rewind = sink_request_rewind;
u->sink->userdata = u;
u->sink->flags = PA_SINK_LATENCY;
- pa_sink_set_module(u->sink, m);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Remapped %s", master->description));
- pa_xfree(t);
pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
pa_sink_set_rtpoll(u->sink, master->rtpoll);
/* Create sink input */
- pa_sink_input_new_data_init(&data);
- data.sink = u->master;
- data.driver = __FILE__;
- data.name = "Remapped Stream";
- pa_sink_input_new_data_set_sample_spec(&data, &ss);
- pa_sink_input_new_data_set_channel_map(&data, &stream_map);
- data.module = m;
-
- if (!(u->sink_input = pa_sink_input_new(m->core, &data, PA_SINK_INPUT_DONT_MOVE)))
+ pa_sink_input_new_data_init(&sink_input_data);
+ sink_input_data.driver = __FILE__;
+ sink_input_data.module = m;
+ sink_input_data.sink = u->master;
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
+ pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "routing");
+ pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
+ pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
+
+ u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
+ pa_sink_input_new_data_done(&sink_input_data);
+
+ if (!u->sink_input)
goto fail;
u->sink_input->parent.process_msg = sink_input_process_msg;
- u->sink_input->peek = sink_input_peek_cb;
- u->sink_input->drop = sink_input_drop_cb;
+ u->sink_input->pop = sink_input_pop_cb;
+ u->sink_input->rewind = sink_input_rewind_cb;
+ u->sink_input->set_max_rewind = sink_input_set_max_rewind_cb;
u->sink_input->kill = sink_input_kill_cb;
u->sink_input->attach = sink_input_attach_cb;
u->sink_input->detach = sink_input_detach_cb;
@@ -295,7 +333,6 @@ int pa__init(pa_module*m) {
pa_sink_input_put(u->sink_input);
pa_modargs_free(ma);
- pa_xfree(default_sink_name);
return 0;
@@ -305,8 +342,6 @@ fail:
pa__done(m);
- pa_xfree(default_sink_name);
-
return -1;
}
@@ -328,8 +363,5 @@ void pa__done(pa_module*m) {
pa_sink_unref(u->sink);
}
- if (u->memchunk.memblock)
- pa_memblock_unref(u->memchunk.memblock);
-
pa_xfree(u);
}
diff --git a/src/modules/module-rescue-streams.c b/src/modules/module-rescue-streams.c
index 12957c9d..dda54735 100644
--- a/src/modules/module-rescue-streams.c
+++ b/src/modules/module-rescue-streams.c
@@ -76,11 +76,11 @@ static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* user
while ((i = pa_idxset_first(sink->inputs, NULL))) {
if (pa_sink_input_move_to(i, target, 1) < 0) {
- pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i->index, i->name, target->name);
+ pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i->index, pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME), target->name);
return PA_HOOK_OK;
}
- pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, i->name, target->name);
+ pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME), target->name);
}
@@ -116,11 +116,11 @@ static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void
while ((o = pa_idxset_first(source->outputs, NULL))) {
if (pa_source_output_move_to(o, target) < 0) {
- pa_log_warn("Failed to move source output %u \"%s\" to %s.", o->index, o->name, target->name);
+ pa_log_warn("Failed to move source output %u \"%s\" to %s.", o->index, pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME), target->name);
return PA_HOOK_OK;
}
- pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, o->name, target->name);
+ pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME), target->name);
}
diff --git a/src/modules/module-sine.c b/src/modules/module-sine.c
index 41d9a51c..f6718fd8 100644
--- a/src/modules/module-sine.c
+++ b/src/modules/module-sine.c
@@ -59,44 +59,25 @@ static const char* const valid_modargs[] = {
NULL,
};
-static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
struct userdata *u;
- pa_assert(i);
- u = i->userdata;
- pa_assert(u);
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
pa_assert(chunk);
chunk->memblock = pa_memblock_ref(u->memblock);
- chunk->index = u->peek_index;
- chunk->length = pa_memblock_get_length(u->memblock) - u->peek_index;
+ chunk->length = pa_memblock_get_length(chunk->memblock);
+ chunk->index = 0;
return 0;
}
-static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
- struct userdata *u;
- size_t l;
-
- pa_assert(i);
- u = i->userdata;
- pa_assert(u);
- pa_assert(length > 0);
-
- u->peek_index += length;
-
- l = pa_memblock_get_length(u->memblock);
-
- while (u->peek_index >= l)
- u->peek_index -= l;
-}
-
static void sink_input_kill_cb(pa_sink_input *i) {
struct userdata *u;
- pa_assert(i);
- u = i->userdata;
- pa_assert(u);
+ pa_sink_input_assert_ref(i);
+ pa_assert_se(u = i->userdata);
pa_sink_input_unlink(u->sink_input);
pa_sink_input_unref(u->sink_input);
@@ -156,20 +137,23 @@ int pa__init(pa_module*m) {
calc_sine(p, pa_memblock_get_length(u->memblock), frequency);
pa_memblock_release(u->memblock);
- pa_snprintf(t, sizeof(t), "Sine Generator at %u Hz", frequency);
+ pa_snprintf(t, sizeof(t), "%u Hz Sine", frequency);
pa_sink_input_new_data_init(&data);
data.sink = sink;
data.driver = __FILE__;
- data.name = t;
+ pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, t);
+ pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "abstract");
pa_sink_input_new_data_set_sample_spec(&data, &ss);
data.module = m;
- if (!(u->sink_input = pa_sink_input_new(m->core, &data, 0)))
+ u->sink_input = pa_sink_input_new(m->core, &data, 0);
+ pa_sink_input_new_data_done(&data);
+
+ if (!u->sink_input)
goto fail;
- u->sink_input->peek = sink_input_peek_cb;
- u->sink_input->drop = sink_input_drop_cb;
+ u->sink_input->pop = sink_input_pop_cb;
u->sink_input->kill = sink_input_kill_cb;
u->sink_input->userdata = u;
diff --git a/src/modules/module-suspend-on-idle.c b/src/modules/module-suspend-on-idle.c
index 4c260d76..ef8239d0 100644
--- a/src/modules/module-suspend-on-idle.c
+++ b/src/modules/module-suspend-on-idle.c
@@ -367,8 +367,8 @@ int pa__init(pa_module*m) {
for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
device_new_hook_cb(m->core, PA_OBJECT(source), u);
- u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], (pa_hook_cb_t) device_new_hook_cb, u);
- u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], (pa_hook_cb_t) device_new_hook_cb, u);
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], (pa_hook_cb_t) device_new_hook_cb, u);
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], (pa_hook_cb_t) device_new_hook_cb, u);
u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], (pa_hook_cb_t) device_unlink_hook_cb, u);
u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], (pa_hook_cb_t) device_unlink_hook_cb, u);
u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], (pa_hook_cb_t) device_state_changed_hook_cb, u);
diff --git a/src/modules/module-tunnel.c b/src/modules/module-tunnel.c
index a53e3932..2da2d134 100644
--- a/src/modules/module-tunnel.c
+++ b/src/modules/module-tunnel.c
@@ -1294,6 +1294,11 @@ int pa__init(pa_module*m) {
pa_sample_spec ss;
pa_channel_map map;
char *t, *dn = NULL;
+#ifdef TUNNEL_SINK
+ pa_sink_new_data data;
+#else
+ pa_source_new_data data;
+#endif
pa_assert(m);
@@ -1354,7 +1359,18 @@ int pa__init(pa_module*m) {
if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
- if (!(u->sink = pa_sink_new(m->core, __FILE__, dn, 1, &ss, &map))) {
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ data.namereg_fail = TRUE;
+ pa_sink_new_data_set_name(&data, dn);
+ pa_sink_new_data_set_sample_spec(&data, &ss);
+ pa_sink_new_data_set_channel_map(&data, &map);
+
+ u->sink = pa_sink_new(m->core, &data, PA_SINK_NETWORK|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
pa_log("Failed to create sink.");
goto fail;
}
@@ -1367,9 +1383,7 @@ int pa__init(pa_module*m) {
u->sink->get_mute = sink_get_mute;
u->sink->set_volume = sink_set_volume;
u->sink->set_mute = sink_set_mute;
- u->sink->flags = PA_SINK_NETWORK|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL;
- pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
pa_sink_set_description(u->sink, t = pa_sprintf_malloc("%s%s%s", u->sink_name ? u->sink_name : "", u->sink_name ? " on " : "", u->server_name));
@@ -1380,7 +1394,18 @@ int pa__init(pa_module*m) {
if (!(dn = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
dn = pa_sprintf_malloc("tunnel.%s", u->server_name);
- if (!(u->source = pa_source_new(m->core, __FILE__, dn, 1, &ss, &map))) {
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ data.namereg_fail = TRUE;
+ pa_source_new_data_set_name(&data, dn);
+ pa_source_new_data_set_sample_spec(&data, &ss);
+ pa_source_new_data_set_channel_map(&data, &map);
+
+ u->source = pa_source_new(m->core, &data, PA_SOURCE_NETWORK|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
pa_log("Failed to create source.");
goto fail;
}
@@ -1389,9 +1414,7 @@ int pa__init(pa_module*m) {
u->source->userdata = u;
u->source->set_state = source_set_state;
u->source->get_latency = source_get_latency;
- u->source->flags = PA_SOURCE_NETWORK|PA_SOURCE_LATENCY;
- pa_source_set_module(u->source, m);
pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
pa_source_set_rtpoll(u->source, u->rtpoll);
pa_source_set_description(u->source, t = pa_sprintf_malloc("%s%s%s", u->source_name ? u->source_name : "", u->source_name ? " on " : "", u->server_name));
diff --git a/src/modules/module-volume-restore.c b/src/modules/module-volume-restore.c
index 192a2a78..0dc8dcfd 100644
--- a/src/modules/module-volume-restore.c
+++ b/src/modules/module-volume-restore.c
@@ -115,7 +115,7 @@ static pa_cvolume* parse_volume(const char *s, pa_cvolume *v) {
k = strtol(p, &p, 0);
- if (k < PA_VOLUME_MUTED)
+ if (k < (long) PA_VOLUME_MUTED)
return NULL;
v->values[i] = (pa_volume_t) k;
@@ -280,10 +280,10 @@ finish:
static char* client_name(pa_client *c) {
char *t, *e;
- if (!c->name || !c->driver)
+ if (!pa_proplist_gets(c->proplist, PA_PROP_APPLICATION_NAME) || !c->driver)
return NULL;
- t = pa_sprintf_malloc("%s$%s", c->driver, c->name);
+ t = pa_sprintf_malloc("%s$%s", c->driver, pa_proplist_gets(c->proplist, PA_PROP_APPLICATION_NAME));
t[strcspn(t, "\n\r#")] = 0;
if (!*t) {
diff --git a/src/modules/module-x11-bell.c b/src/modules/module-x11-bell.c
index 87c6849d..761b82a9 100644
--- a/src/modules/module-x11-bell.c
+++ b/src/modules/module-x11-bell.c
@@ -81,7 +81,7 @@ static int x11_event_callback(pa_x11_wrapper *w, XEvent *e, void *userdata) {
bne = (XkbBellNotifyEvent*) e;
- if (pa_scache_play_item_by_name(u->core, u->scache_item, u->sink_name, (bne->percent*PA_VOLUME_NORM)/100, 1) < 0) {
+ if (pa_scache_play_item_by_name(u->core, u->scache_item, u->sink_name, TRUE, (bne->percent*PA_VOLUME_NORM)/100, NULL, NULL) < 0) {
pa_log_info("Ringing bell failed, reverting to X11 device bell.");
XkbForceDeviceBell(pa_x11_wrapper_get_display(w), bne->device, bne->bell_class, bne->bell_id, bne->percent);
}
diff --git a/src/modules/module-zeroconf-publish.c b/src/modules/module-zeroconf-publish.c
index 46969a24..6ed8e3d9 100644
--- a/src/modules/module-zeroconf-publish.c
+++ b/src/modules/module-zeroconf-publish.c
@@ -115,7 +115,7 @@ static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_chann
*ret_ss = sink->sample_spec;
*ret_map = sink->channel_map;
*ret_name = sink->name;
- *ret_description = sink->description;
+ *ret_description = pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
*ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
} else if (pa_source_isinstance(s->device)) {
@@ -124,7 +124,7 @@ static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_chann
*ret_ss = source->sample_spec;
*ret_map = source->channel_map;
*ret_name = source->name;
- *ret_description = source->description;
+ *ret_description = pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION));
*ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
} else
@@ -304,10 +304,10 @@ static struct service *get_service(struct userdata *u, pa_object *device) {
s->device = device;
if (pa_sink_isinstance(device)) {
- if (!(n = PA_SINK(device)->description))
+ if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
n = PA_SINK(device)->name;
} else {
- if (!(n = PA_SOURCE(device)->description))
+ if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
n = PA_SOURCE(device)->name;
}
@@ -578,11 +578,11 @@ int pa__init(pa_module*m) {
u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
- u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], (pa_hook_cb_t) device_new_or_changed_cb, u);
- u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_DESCRIPTION_CHANGED], (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], (pa_hook_cb_t) device_new_or_changed_cb, u);
u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], (pa_hook_cb_t) device_unlink_cb, u);
- u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], (pa_hook_cb_t) device_new_or_changed_cb, u);
- u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_DESCRIPTION_CHANGED], (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], (pa_hook_cb_t) device_new_or_changed_cb, u);
u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], (pa_hook_cb_t) device_unlink_cb, u);
u->main_entry_group = NULL;