aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/backends/pulseaudio.c
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2019-04-07 23:39:04 +0200
committerSven Gothel <[email protected]>2019-04-07 23:39:04 +0200
commit73233ce69919fc19c53ce8663c5b8cc05227f07e (patch)
treef2b6ccc1a14d7c387f33398a44ea4511d7ecb212 /Alc/backends/pulseaudio.c
parent8efa4c7ba5ee8eb399d31a9884e45f743d4625ad (diff)
parent99a55c445211fea77af6ab61cbc6a6ec4fbdc9b9 (diff)
Merge branch 'v1.19' of git://repo.or.cz/openal-soft into v1.19v1.19
Diffstat (limited to 'Alc/backends/pulseaudio.c')
-rw-r--r--Alc/backends/pulseaudio.c414
1 files changed, 262 insertions, 152 deletions
diff --git a/Alc/backends/pulseaudio.c b/Alc/backends/pulseaudio.c
index 9ad04a71..b34d7abc 100644
--- a/Alc/backends/pulseaudio.c
+++ b/Alc/backends/pulseaudio.c
@@ -25,6 +25,7 @@
#include "alMain.h"
#include "alu.h"
+#include "alconfig.h"
#include "threads.h"
#include "compat.h"
@@ -182,6 +183,8 @@ static ALCboolean pulse_load(void)
#ifdef HAVE_DYNLOAD
if(!pa_handle)
{
+ al_string missing_funcs = AL_STRING_INIT_STATIC();
+
#ifdef _WIN32
#define PALIB "libpulse-0.dll"
#elif defined(__APPLE__) && defined(__MACH__)
@@ -191,12 +194,16 @@ static ALCboolean pulse_load(void)
#endif
pa_handle = LoadLib(PALIB);
if(!pa_handle)
+ {
+ WARN("Failed to load %s\n", PALIB);
return ALC_FALSE;
+ }
#define LOAD_FUNC(x) do { \
p##x = GetSymbol(pa_handle, #x); \
if(!(p##x)) { \
ret = ALC_FALSE; \
+ alstr_append_cstr(&missing_funcs, "\n" #x); \
} \
} while(0)
LOAD_FUNC(pa_context_unref);
@@ -270,9 +277,11 @@ static ALCboolean pulse_load(void)
if(ret == ALC_FALSE)
{
+ WARN("Missing expected functions:%s\n", alstr_get_cstr(missing_funcs));
CloseLib(pa_handle);
pa_handle = NULL;
}
+ alstr_reset(&missing_funcs);
}
#endif /* HAVE_DYNLOAD */
return ret;
@@ -325,18 +334,20 @@ static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
{
const char *name = "OpenAL Soft";
- char path_name[PATH_MAX];
+ al_string binname = AL_STRING_INIT_STATIC();
pa_context_state_t state;
pa_context *context;
int err;
- if(pa_get_binary_name(path_name, sizeof(path_name)))
- name = pa_path_get_filename(path_name);
+ GetProcBinary(NULL, &binname);
+ if(!alstr_empty(binname))
+ name = alstr_get_cstr(binname);
context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
if(!context)
{
ERR("pa_context_new() failed\n");
+ alstr_reset(&binname);
return NULL;
}
@@ -363,9 +374,10 @@ static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
if(!silent)
ERR("Context did not connect: %s\n", pa_strerror(err));
pa_context_unref(context);
- return NULL;
+ context = NULL;
}
+ alstr_reset(&binname);
return context;
}
@@ -443,7 +455,7 @@ static void clear_devlist(vector_DevMap *list)
#define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS);
#undef DEINIT_STRS
- VECTOR_RESIZE(*list, 0);
+ VECTOR_RESIZE(*list, 0, 0);
}
@@ -460,7 +472,7 @@ typedef struct ALCpulsePlayback {
pa_stream *stream;
pa_context *context;
- volatile ALboolean killNow;
+ ATOMIC(ALenum) killNow;
althrd_t thread;
} ALCpulsePlayback;
@@ -483,13 +495,12 @@ static int ALCpulsePlayback_mixerProc(void *ptr);
static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
-static void ALCpulsePlayback_close(ALCpulsePlayback *self);
static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
-static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self);
+static ClockLatency ALCpulsePlayback_getClockLatency(ALCpulsePlayback *self);
static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
@@ -502,11 +513,20 @@ static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device
ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
+ self->loop = NULL;
AL_STRING_INIT(self->device_name);
+ ATOMIC_INIT(&self->killNow, AL_TRUE);
}
static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
{
+ if(self->loop)
+ {
+ pulse_close(self->loop, self->context, self->stream);
+ self->loop = NULL;
+ self->context = NULL;
+ self->stream = NULL;
+ }
AL_STRING_DEINIT(self->device_name);
ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
}
@@ -525,35 +545,35 @@ static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const p
return;
}
-#define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
+#define MATCH_INFO_NAME(iter) (alstr_cmp_cstr((iter)->device_name, info->name) == 0)
VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_INFO_NAME);
- if(iter != VECTOR_ITER_END(PlaybackDevices)) return;
+ if(iter != VECTOR_END(PlaybackDevices)) return;
#undef MATCH_INFO_NAME
AL_STRING_INIT(entry.name);
AL_STRING_INIT(entry.device_name);
- al_string_copy_cstr(&entry.device_name, info->name);
+ alstr_copy_cstr(&entry.device_name, info->name);
count = 0;
while(1)
{
- al_string_copy_cstr(&entry.name, info->description);
+ alstr_copy_cstr(&entry.name, info->description);
if(count != 0)
{
char str[64];
snprintf(str, sizeof(str), " #%d", count+1);
- al_string_append_cstr(&entry.name, str);
+ alstr_append_cstr(&entry.name, str);
}
-#define MATCH_ENTRY(i) (al_string_cmp(entry.name, (i)->name) == 0)
+#define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_ENTRY);
- if(iter == VECTOR_ITER_END(PlaybackDevices)) break;
+ if(iter == VECTOR_END(PlaybackDevices)) break;
#undef MATCH_ENTRY
count++;
}
- TRACE("Got device \"%s\", \"%s\"\n", al_string_get_cstr(entry.name), al_string_get_cstr(entry.device_name));
+ TRACE("Got device \"%s\", \"%s\"\n", alstr_get_cstr(entry.name), alstr_get_cstr(entry.device_name));
VECTOR_PUSH_BACK(PlaybackDevices, entry);
}
@@ -618,6 +638,11 @@ static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
self->attr = *pa_stream_get_buffer_attr(stream);
TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
+ /* FIXME: Update the device's UpdateSize (and/or NumUpdates) using the new
+ * buffer attributes? Changing UpdateSize will change the ALC_REFRESH
+ * property, which probably shouldn't change between device resets. But
+ * leaving it alone means ALC_REFRESH will be off.
+ */
}
static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
@@ -626,7 +651,7 @@ static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pda
if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
{
ERR("Received context failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
+ aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Playback state failure");
}
pa_threaded_mainloop_signal(self->loop, 0);
}
@@ -637,7 +662,7 @@ static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
{
ERR("Received stream failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
+ aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Playback stream failure");
}
pa_threaded_mainloop_signal(self->loop, 0);
}
@@ -729,7 +754,7 @@ static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const
return;
}
- al_string_copy_cstr(&device->DeviceName, info->description);
+ alstr_copy_cstr(&device->DeviceName, info->description);
}
@@ -737,9 +762,9 @@ static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
{
ALCpulsePlayback *self = pdata;
- al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
+ alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
- TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
+ TRACE("Stream moved to %s\n", alstr_get_cstr(self->device_name));
}
@@ -751,6 +776,13 @@ static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
pa_stream_state_t state;
pa_stream *stream;
+ if(!device_name)
+ {
+ device_name = getenv("ALSOFT_PULSE_DEFAULT");
+ if(device_name && !device_name[0])
+ device_name = NULL;
+ }
+
stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
if(!stream)
{
@@ -789,7 +821,6 @@ static int ALCpulsePlayback_mixerProc(void *ptr)
ALCpulsePlayback *self = ptr;
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
ALuint buffer_size;
- ALint update_size;
size_t frame_size;
ssize_t len;
@@ -798,18 +829,35 @@ static int ALCpulsePlayback_mixerProc(void *ptr)
pa_threaded_mainloop_lock(self->loop);
frame_size = pa_frame_size(&self->spec);
- update_size = device->UpdateSize * frame_size;
-
- /* Sanitize buffer metrics, in case we actually have less than what we
- * asked for. */
- buffer_size = minu(update_size*device->NumUpdates, self->attr.tlength);
- update_size = minu(update_size, buffer_size/2);
- do {
- len = pa_stream_writable_size(self->stream) - self->attr.tlength +
- buffer_size;
- if(len < update_size)
+
+ while(!ATOMIC_LOAD(&self->killNow, almemory_order_acquire) &&
+ ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
+ {
+ void *buf;
+ int ret;
+
+ len = pa_stream_writable_size(self->stream);
+ if(len < 0)
+ {
+ ERR("Failed to get writable size: %ld", (long)len);
+ aluHandleDisconnect(device, "Failed to get writable size: %ld", (long)len);
+ break;
+ }
+
+ /* Make sure we're going to write at least 2 'periods' (minreqs), in
+ * case the server increased it since starting playback. Also round up
+ * the number of writable periods if it's not an integer count.
+ */
+ buffer_size = maxu((self->attr.tlength + self->attr.minreq/2) / self->attr.minreq, 2) *
+ self->attr.minreq;
+
+ /* NOTE: This assumes pa_stream_writable_size returns between 0 and
+ * tlength, else there will be more latency than intended.
+ */
+ len = mini(len - (ssize_t)self->attr.tlength, 0) + buffer_size;
+ if(len < (int32_t)self->attr.minreq)
{
- if(pa_stream_is_corked(self->stream) == 1)
+ if(pa_stream_is_corked(self->stream))
{
pa_operation *o;
o = pa_stream_cork(self->stream, 0, NULL, NULL);
@@ -818,26 +866,17 @@ static int ALCpulsePlayback_mixerProc(void *ptr)
pa_threaded_mainloop_wait(self->loop);
continue;
}
- len -= len%update_size;
- while(len > 0)
- {
- size_t newlen = len;
- void *buf;
- pa_free_cb_t free_func = NULL;
+ len -= len%self->attr.minreq;
+ len -= len%frame_size;
- if(pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
- {
- buf = pa_xmalloc(newlen);
- free_func = pa_xfree;
- }
+ buf = pa_xmalloc(len);
- aluMixData(device, buf, newlen/frame_size);
+ aluMixData(device, buf, len/frame_size);
- pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
- len -= newlen;
- }
- } while(!self->killNow && device->Connected);
+ ret = pa_stream_write(self->stream, buf, len, pa_xfree, 0, PA_SEEK_RELATIVE);
+ if(ret != PA_OK) ERR("Failed to write to stream: %d, %s\n", ret, pa_strerror(ret));
+ }
pa_threaded_mainloop_unlock(self->loop);
return 0;
@@ -858,12 +897,12 @@ static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name
if(VECTOR_SIZE(PlaybackDevices) == 0)
ALCpulsePlayback_probeDevices();
-#define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
+#define MATCH_NAME(iter) (alstr_cmp_cstr((iter)->name, name) == 0)
VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
#undef MATCH_NAME
- if(iter == VECTOR_ITER_END(PlaybackDevices))
+ if(iter == VECTOR_END(PlaybackDevices))
return ALC_INVALID_VALUE;
- pulse_name = al_string_get_cstr(iter->device_name);
+ pulse_name = alstr_get_cstr(iter->device_name);
dev_name = iter->name;
}
@@ -894,11 +933,11 @@ static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name
}
pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
- al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
- if(al_string_empty(dev_name))
+ alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
+ if(alstr_empty(dev_name))
{
pa_operation *o = pa_context_get_sink_info_by_name(
- self->context, al_string_get_cstr(self->device_name),
+ self->context, alstr_get_cstr(self->device_name),
ALCpulsePlayback_sinkNameCallback, self
);
wait_for_operation(o, self->loop);
@@ -906,7 +945,7 @@ static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name
else
{
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- al_string_copy(&device->DeviceName, dev_name);
+ alstr_copy(&device->DeviceName, dev_name);
}
pa_threaded_mainloop_unlock(self->loop);
@@ -914,16 +953,6 @@ static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name
return ALC_NO_ERROR;
}
-static void ALCpulsePlayback_close(ALCpulsePlayback *self)
-{
- pulse_close(self->loop, self->context, self->stream);
- self->loop = NULL;
- self->context = NULL;
- self->stream = NULL;
-
- al_string_clear(&self->device_name);
-}
-
static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
{
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
@@ -931,7 +960,6 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
const char *mapname = NULL;
pa_channel_map chanmap;
pa_operation *o;
- ALuint len;
pa_threaded_mainloop_lock(self->loop);
@@ -946,11 +974,11 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
self->stream = NULL;
}
- o = pa_context_get_sink_info_by_name(self->context, al_string_get_cstr(self->device_name),
+ o = pa_context_get_sink_info_by_name(self->context, alstr_get_cstr(self->device_name),
ALCpulsePlayback_sinkInfoCallback, self);
wait_for_operation(o, self->loop);
- if(GetConfigValueBool(al_string_get_cstr(device->DeviceName), "pulse", "fix-rate", 0) ||
+ if(GetConfigValueBool(alstr_get_cstr(device->DeviceName), "pulse", "fix-rate", 0) ||
!(device->Flags&DEVICE_FREQUENCY_REQUEST))
flags |= PA_STREAM_FIX_RATE;
flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
@@ -984,7 +1012,7 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
break;
}
self->spec.rate = device->Frequency;
- self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
+ self->spec.channels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
if(pa_sample_spec_valid(&self->spec) == 0)
{
@@ -998,7 +1026,7 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
case DevFmtMono:
mapname = "mono";
break;
- case DevFmtBFormat3D:
+ case DevFmtAmbi3D:
device->FmtChans = DevFmtStereo;
/*fall-through*/
case DevFmtStereo:
@@ -1034,9 +1062,9 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
self->attr.maxlength = -1;
- self->stream = ALCpulsePlayback_connectStream(al_string_get_cstr(self->device_name),
- self->loop, self->context, flags,
- &self->attr, &self->spec, &chanmap);
+ self->stream = ALCpulsePlayback_connectStream(alstr_get_cstr(self->device_name),
+ self->loop, self->context, flags, &self->attr, &self->spec, &chanmap
+ );
if(!self->stream)
{
pa_threaded_mainloop_unlock(self->loop);
@@ -1051,10 +1079,12 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
{
/* Server updated our playback rate, so modify the buffer attribs
* accordingly. */
- device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates / device->Frequency *
- self->spec.rate + 0.5);
+ device->NumUpdates = (ALuint)clampd(
+ (ALdouble)device->NumUpdates/device->Frequency*self->spec.rate + 0.5, 2.0, 16.0
+ );
+
self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
- self->attr.tlength = self->attr.minreq * clampu(device->NumUpdates, 2, 16);
+ self->attr.tlength = self->attr.minreq * device->NumUpdates;
self->attr.maxlength = -1;
self->attr.prebuf = 0;
@@ -1068,10 +1098,30 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
ALCpulsePlayback_bufferAttrCallback(self->stream, self);
- len = self->attr.minreq / pa_frame_size(&self->spec);
- device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates/len*device->UpdateSize + 0.5);
- device->NumUpdates = clampu(device->NumUpdates, 2, 16);
- device->UpdateSize = len;
+ device->NumUpdates = (ALuint)clampu64(
+ (self->attr.tlength + self->attr.minreq/2) / self->attr.minreq, 2, 16
+ );
+ device->UpdateSize = self->attr.minreq / pa_frame_size(&self->spec);
+
+ /* HACK: prebuf should be 0 as that's what we set it to. However on some
+ * systems it comes back as non-0, so we have to make sure the device will
+ * write enough audio to start playback. The lack of manual start control
+ * may have unintended consequences, but it's better than not starting at
+ * all.
+ */
+ if(self->attr.prebuf != 0)
+ {
+ ALuint len = self->attr.prebuf / pa_frame_size(&self->spec);
+ if(len <= device->UpdateSize*device->NumUpdates)
+ ERR("Non-0 prebuf, %u samples (%u bytes), device has %u samples\n",
+ len, self->attr.prebuf, device->UpdateSize*device->NumUpdates);
+ else
+ {
+ ERR("Large prebuf, %u samples (%u bytes), increasing device from %u samples",
+ len, self->attr.prebuf, device->UpdateSize*device->NumUpdates);
+ device->NumUpdates = (len+device->UpdateSize-1) / device->UpdateSize;
+ }
+ }
pa_threaded_mainloop_unlock(self->loop);
return ALC_TRUE;
@@ -1079,7 +1129,7 @@ static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
{
- self->killNow = AL_FALSE;
+ ATOMIC_STORE(&self->killNow, AL_FALSE, almemory_order_release);
if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
return ALC_FALSE;
return ALC_TRUE;
@@ -1090,10 +1140,9 @@ static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
pa_operation *o;
int res;
- if(!self->stream || self->killNow)
+ if(!self->stream || ATOMIC_EXCHANGE(&self->killNow, AL_TRUE, almemory_order_acq_rel))
return;
- self->killNow = AL_TRUE;
/* Signal the main loop in case PulseAudio isn't sending us audio requests
* (e.g. if the device is suspended). We need to lock the mainloop in case
* the mixer is between checking the killNow flag but before waiting for
@@ -1113,12 +1162,18 @@ static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
}
-static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
+static ClockLatency ALCpulsePlayback_getClockLatency(ALCpulsePlayback *self)
{
- pa_usec_t latency = 0;
+ ClockLatency ret;
+ pa_usec_t latency;
int neg, err;
- if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
+ pa_threaded_mainloop_lock(self->loop);
+ ret.ClockTime = GetDeviceClockTime(STATIC_CAST(ALCbackend,self)->mDevice);
+ err = pa_stream_get_latency(self->stream, &latency, &neg);
+ pa_threaded_mainloop_unlock(self->loop);
+
+ if(UNLIKELY(err != 0))
{
/* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
* after starting the stream and no timing info has been received from
@@ -1126,11 +1181,14 @@ static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
* dummy value? Either way, it shouldn't be 0. */
if(err != -PA_ERR_NODATA)
ERR("Failed to get stream latency: 0x%x\n", err);
- return 0;
+ latency = 0;
+ neg = 0;
}
+ else if(UNLIKELY(neg))
+ latency = 0;
+ ret.Latency = (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
- if(neg) latency = 0;
- return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
+ return ret;
}
@@ -1180,13 +1238,12 @@ static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
-static void ALCpulseCapture_close(ALCpulseCapture *self);
static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
static void ALCpulseCapture_stop(ALCpulseCapture *self);
static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
-static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
+static ClockLatency ALCpulseCapture_getClockLatency(ALCpulseCapture *self);
static void ALCpulseCapture_lock(ALCpulseCapture *self);
static void ALCpulseCapture_unlock(ALCpulseCapture *self);
DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
@@ -1199,11 +1256,19 @@ static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
+ self->loop = NULL;
AL_STRING_INIT(self->device_name);
}
static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
{
+ if(self->loop)
+ {
+ pulse_close(self->loop, self->context, self->stream);
+ self->loop = NULL;
+ self->context = NULL;
+ self->stream = NULL;
+ }
AL_STRING_DEINIT(self->device_name);
ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
}
@@ -1222,35 +1287,35 @@ static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa
return;
}
-#define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
+#define MATCH_INFO_NAME(iter) (alstr_cmp_cstr((iter)->device_name, info->name) == 0)
VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
- if(iter != VECTOR_ITER_END(CaptureDevices)) return;
+ if(iter != VECTOR_END(CaptureDevices)) return;
#undef MATCH_INFO_NAME
AL_STRING_INIT(entry.name);
AL_STRING_INIT(entry.device_name);
- al_string_copy_cstr(&entry.device_name, info->name);
+ alstr_copy_cstr(&entry.device_name, info->name);
count = 0;
while(1)
{
- al_string_copy_cstr(&entry.name, info->description);
+ alstr_copy_cstr(&entry.name, info->description);
if(count != 0)
{
char str[64];
snprintf(str, sizeof(str), " #%d", count+1);
- al_string_append_cstr(&entry.name, str);
+ alstr_append_cstr(&entry.name, str);
}
-#define MATCH_ENTRY(i) (al_string_cmp(entry.name, (i)->name) == 0)
+#define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_ENTRY);
- if(iter == VECTOR_ITER_END(CaptureDevices)) break;
+ if(iter == VECTOR_END(CaptureDevices)) break;
#undef MATCH_ENTRY
count++;
}
- TRACE("Got device \"%s\", \"%s\"\n", al_string_get_cstr(entry.name), al_string_get_cstr(entry.device_name));
+ TRACE("Got device \"%s\", \"%s\"\n", alstr_get_cstr(entry.name), alstr_get_cstr(entry.device_name));
VECTOR_PUSH_BACK(CaptureDevices, entry);
}
@@ -1315,7 +1380,7 @@ static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdat
if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
{
ERR("Received context failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
+ aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Capture state failure");
}
pa_threaded_mainloop_signal(self->loop, 0);
}
@@ -1326,7 +1391,7 @@ static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
{
ERR("Received stream failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
+ aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Capture stream failure");
}
pa_threaded_mainloop_signal(self->loop, 0);
}
@@ -1343,7 +1408,7 @@ static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), cons
return;
}
- al_string_copy_cstr(&device->DeviceName, info->description);
+ alstr_copy_cstr(&device->DeviceName, info->description);
}
@@ -1351,9 +1416,9 @@ static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
{
ALCpulseCapture *self = pdata;
- al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
+ alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
- TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
+ TRACE("Stream moved to %s\n", alstr_get_cstr(self->device_name));
}
@@ -1403,6 +1468,7 @@ static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
const char *pulse_name = NULL;
pa_stream_flags_t flags = 0;
+ const char *mapname = NULL;
pa_channel_map chanmap;
ALuint samples;
@@ -1413,13 +1479,13 @@ static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
if(VECTOR_SIZE(CaptureDevices) == 0)
ALCpulseCapture_probeDevices();
-#define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
+#define MATCH_NAME(iter) (alstr_cmp_cstr((iter)->name, name) == 0)
VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
#undef MATCH_NAME
- if(iter == VECTOR_ITER_END(CaptureDevices))
+ if(iter == VECTOR_END(CaptureDevices))
return ALC_INVALID_VALUE;
- pulse_name = al_string_get_cstr(iter->device_name);
- al_string_copy(&device->DeviceName, iter->name);
+ pulse_name = alstr_get_cstr(iter->device_name);
+ alstr_copy(&device->DeviceName, iter->name);
}
if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
@@ -1427,9 +1493,6 @@ static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
pa_threaded_mainloop_lock(self->loop);
- self->spec.rate = device->Frequency;
- self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
-
switch(device->FmtType)
{
case DevFmtUByte:
@@ -1452,6 +1515,44 @@ static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
goto fail;
}
+ switch(device->FmtChans)
+ {
+ case DevFmtMono:
+ mapname = "mono";
+ break;
+ case DevFmtStereo:
+ mapname = "front-left,front-right";
+ break;
+ case DevFmtQuad:
+ mapname = "front-left,front-right,rear-left,rear-right";
+ break;
+ case DevFmtX51:
+ mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
+ break;
+ case DevFmtX51Rear:
+ mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
+ break;
+ case DevFmtX61:
+ mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
+ break;
+ case DevFmtX71:
+ mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
+ break;
+ case DevFmtAmbi3D:
+ ERR("%s capture samples not supported\n", DevFmtChannelsString(device->FmtChans));
+ pa_threaded_mainloop_unlock(self->loop);
+ goto fail;
+ }
+ if(!pa_channel_map_parse(&chanmap, mapname))
+ {
+ ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
+ pa_threaded_mainloop_unlock(self->loop);
+ return ALC_FALSE;
+ }
+
+ self->spec.rate = device->Frequency;
+ self->spec.channels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
+
if(pa_sample_spec_valid(&self->spec) == 0)
{
ERR("Invalid sample format\n");
@@ -1481,9 +1582,9 @@ static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
flags |= PA_STREAM_DONT_MOVE;
TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
- self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
- flags, &self->attr, &self->spec,
- &chanmap);
+ self->stream = ALCpulseCapture_connectStream(pulse_name,
+ self->loop, self->context, flags, &self->attr, &self->spec, &chanmap
+ );
if(!self->stream)
{
pa_threaded_mainloop_unlock(self->loop);
@@ -1492,11 +1593,11 @@ static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
- al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
- if(al_string_empty(device->DeviceName))
+ alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
+ if(alstr_empty(device->DeviceName))
{
pa_operation *o = pa_context_get_source_info_by_name(
- self->context, al_string_get_cstr(self->device_name),
+ self->context, alstr_get_cstr(self->device_name),
ALCpulseCapture_sourceNameCallback, self
);
wait_for_operation(o, self->loop);
@@ -1514,30 +1615,23 @@ fail:
return ALC_INVALID_VALUE;
}
-static void ALCpulseCapture_close(ALCpulseCapture *self)
-{
- pulse_close(self->loop, self->context, self->stream);
- self->loop = NULL;
- self->context = NULL;
- self->stream = NULL;
-
- al_string_clear(&self->device_name);
-}
-
static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
{
pa_operation *o;
+ pa_threaded_mainloop_lock(self->loop);
o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
wait_for_operation(o, self->loop);
-
+ pa_threaded_mainloop_unlock(self->loop);
return ALC_TRUE;
}
static void ALCpulseCapture_stop(ALCpulseCapture *self)
{
pa_operation *o;
+ pa_threaded_mainloop_lock(self->loop);
o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
wait_for_operation(o, self->loop);
+ pa_threaded_mainloop_unlock(self->loop);
}
static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
@@ -1548,6 +1642,7 @@ static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *bu
/* Capture is done in fragment-sized chunks, so we loop until we get all
* that's available */
self->last_readable -= todo;
+ pa_threaded_mainloop_lock(self->loop);
while(todo > 0)
{
size_t rem = todo;
@@ -1559,14 +1654,15 @@ static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *bu
state = pa_stream_get_state(self->stream);
if(!PA_STREAM_IS_GOOD(state))
{
- aluHandleDisconnect(device);
+ aluHandleDisconnect(device, "Bad capture state: %u", state);
break;
}
if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
{
ERR("pa_stream_peek() failed: %s\n",
pa_strerror(pa_context_errno(self->context)));
- aluHandleDisconnect(device);
+ aluHandleDisconnect(device, "Failed retrieving capture samples: %s",
+ pa_strerror(pa_context_errno(self->context)));
break;
}
self->cap_remain = self->cap_len;
@@ -1587,6 +1683,7 @@ static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *bu
self->cap_len = 0;
}
}
+ pa_threaded_mainloop_unlock(self->loop);
if(todo > 0)
memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
@@ -1598,16 +1695,19 @@ static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
size_t readable = self->cap_remain;
- if(device->Connected)
+ if(ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
{
- ssize_t got = pa_stream_readable_size(self->stream);
+ ssize_t got;
+ pa_threaded_mainloop_lock(self->loop);
+ got = pa_stream_readable_size(self->stream);
if(got < 0)
{
ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
- aluHandleDisconnect(device);
+ aluHandleDisconnect(device, "Failed getting readable size: %s", pa_strerror(got));
}
else if((size_t)got > self->cap_len)
readable += got - self->cap_len;
+ pa_threaded_mainloop_unlock(self->loop);
}
if(self->last_readable < readable)
@@ -1616,19 +1716,28 @@ static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
}
-static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
+static ClockLatency ALCpulseCapture_getClockLatency(ALCpulseCapture *self)
{
- pa_usec_t latency = 0;
- int neg;
+ ClockLatency ret;
+ pa_usec_t latency;
+ int neg, err;
+
+ pa_threaded_mainloop_lock(self->loop);
+ ret.ClockTime = GetDeviceClockTime(STATIC_CAST(ALCbackend,self)->mDevice);
+ err = pa_stream_get_latency(self->stream, &latency, &neg);
+ pa_threaded_mainloop_unlock(self->loop);
- if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
+ if(UNLIKELY(err != 0))
{
- ERR("Failed to get stream latency!\n");
- return 0;
+ ERR("Failed to get stream latency: 0x%x\n", err);
+ latency = 0;
+ neg = 0;
}
+ else if(UNLIKELY(neg))
+ latency = 0;
+ ret.Latency = (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
- if(neg) latency = 0;
- return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
+ return ret;
}
@@ -1651,9 +1760,8 @@ typedef struct ALCpulseBackendFactory {
static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
-static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
+static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type, al_string *outnames);
static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
-
DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
@@ -1726,23 +1834,25 @@ static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UN
return ALC_FALSE;
}
-static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
+static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type, al_string *outnames)
{
switch(type)
{
+#define APPEND_OUTNAME(e) do { \
+ if(!alstr_empty((e)->name)) \
+ alstr_append_range(outnames, VECTOR_BEGIN((e)->name), \
+ VECTOR_END((e)->name)+1); \
+} while(0)
case ALL_DEVICE_PROBE:
ALCpulsePlayback_probeDevices();
-#define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(al_string_get_cstr((e)->name))
- VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_ALL_DEVICES_LIST);
-#undef APPEND_ALL_DEVICES_LIST
+ VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_OUTNAME);
break;
case CAPTURE_DEVICE_PROBE:
ALCpulseCapture_probeDevices();
-#define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(al_string_get_cstr((e)->name))
- VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_CAPTURE_DEVICE_LIST);
-#undef APPEND_CAPTURE_DEVICE_LIST
+ VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_OUTNAME);
break;
+#undef APPEND_OUTNAME
}
}
@@ -1790,7 +1900,7 @@ static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UN
return ALC_FALSE;
}
-static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
+static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type), al_string* UNUSED(outnames))
{
}