Skip to content

Commit

Permalink
3rdparty/cubeb: Sync to 19fcbef
Browse files Browse the repository at this point in the history
Backport of stenzek/duckstation@872cee9

And apply PR #740 (Re-enable and polish IAudioClient3 to achieve lower
latencies).

`*latency_frames = min_period;` in wasapi_get_min_latency was changed to
`*latency_frames = hns_to_frames(params.rate, min_period_rt);`, as
otherwise it reports in mixer frames, not stream frames.
  • Loading branch information
stenzek committed May 17, 2024
1 parent f084e76 commit c573c00
Show file tree
Hide file tree
Showing 14 changed files with 502 additions and 156 deletions.
123 changes: 63 additions & 60 deletions 3rdparty/cubeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ target_include_directories(cubeb
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
)

add_library(speex OBJECT subprojects/speex/resample.c)
set_target_properties(speex PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
target_include_directories(speex INTERFACE subprojects)
target_compile_definitions(speex PUBLIC
OUTSIDE_SPEEX
FLOATING_POINT
EXPORT=
RANDOM_PREFIX=speex
)
add_library(speex OBJECT subprojects/speex/resample.c)
set_target_properties(speex PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
target_include_directories(speex INTERFACE subprojects)
target_compile_definitions(speex PUBLIC
OUTSIDE_SPEEX
FLOATING_POINT
EXPORT=
RANDOM_PREFIX=speex
)

# $<BUILD_INTERFACE:> required because of https://gitlab.kitware.com/cmake/cmake/-/issues/15415
target_link_libraries(cubeb PRIVATE $<BUILD_INTERFACE:speex>)
Expand All @@ -58,17 +58,20 @@ find_package(Threads)
target_link_libraries(cubeb PRIVATE Threads::Threads)

if(LAZY_LOAD_LIBS)
if(NOT APPLE AND NOT WIN32)
# Skip checks on MacOS because it takes ages in XCode.
check_include_files(pulse/pulseaudio.h USE_PULSE)
check_include_files(alsa/asoundlib.h USE_ALSA)
check_include_files(jack/jack.h USE_JACK)
check_include_files(sndio.h USE_SNDIO)

if(USE_PULSE OR USE_ALSA OR USE_JACK OR USE_SNDIO)
target_link_libraries(cubeb PRIVATE ${CMAKE_DL_LIBS})
if(NOT APPLE AND NOT WIN32) # Skip checks on MacOS because it takes ages in XCode.
check_include_files(pulse/pulseaudio.h USE_PULSE)
check_include_files(alsa/asoundlib.h USE_ALSA)
check_include_files(jack/jack.h USE_JACK)
check_include_files(sndio.h USE_SNDIO)

if(USE_PULSE OR USE_ALSA OR USE_JACK OR USE_SNDIO OR USE_AAUDIO)
target_link_libraries(cubeb PRIVATE ${CMAKE_DL_LIBS})

if(ANDROID)
target_compile_definitions(cubeb PRIVATE __ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__)
endif()
endif()
endif()

elseif(NOT APPLE AND NOT WIN32)

Expand Down Expand Up @@ -123,56 +126,56 @@ if(USE_SNDIO)
endif()

if(APPLE)
check_include_files(AudioUnit/AudioUnit.h USE_AUDIOUNIT)
if(USE_AUDIOUNIT)
target_sources(cubeb PRIVATE
src/cubeb_audiounit.cpp
src/cubeb_osx_run_loop.cpp)
target_compile_definitions(cubeb PRIVATE USE_AUDIOUNIT)
target_link_libraries(cubeb PRIVATE "-framework AudioUnit" "-framework CoreAudio" "-framework CoreServices")
endif()
check_include_files(AudioUnit/AudioUnit.h USE_AUDIOUNIT)
if(USE_AUDIOUNIT)
target_sources(cubeb PRIVATE
src/cubeb_audiounit.cpp
src/cubeb_osx_run_loop.cpp)
target_compile_definitions(cubeb PRIVATE USE_AUDIOUNIT)
target_link_libraries(cubeb PRIVATE "-framework AudioUnit" "-framework CoreAudio" "-framework CoreServices")
endif()
endif()

if(WIN32)
check_include_files(audioclient.h USE_WASAPI)
if(USE_WASAPI)
target_sources(cubeb PRIVATE
src/cubeb_wasapi.cpp)
target_compile_definitions(cubeb PRIVATE USE_WASAPI)
target_link_libraries(cubeb PRIVATE avrt ole32 ksuser)
endif()
check_include_files(audioclient.h USE_WASAPI)
if(USE_WASAPI)
target_sources(cubeb PRIVATE
src/cubeb_wasapi.cpp)
target_compile_definitions(cubeb PRIVATE USE_WASAPI)
target_link_libraries(cubeb PRIVATE ole32 ksuser)
endif()

check_include_files("windows.h;mmsystem.h" USE_WINMM)
if(USE_WINMM)
target_sources(cubeb PRIVATE
src/cubeb_winmm.c)
target_compile_definitions(cubeb PRIVATE USE_WINMM)
target_link_libraries(cubeb PRIVATE winmm)
endif()
check_include_files("windows.h;mmsystem.h" USE_WINMM)
if(USE_WINMM)
target_sources(cubeb PRIVATE
src/cubeb_winmm.c)
target_compile_definitions(cubeb PRIVATE USE_WINMM)
target_link_libraries(cubeb PRIVATE winmm)
endif()
endif()

if(NOT WIN32 AND NOT APPLE)
check_include_files(sys/soundcard.h HAVE_SYS_SOUNDCARD_H)
if(HAVE_SYS_SOUNDCARD_H)
try_compile(USE_OSS "${PROJECT_BINARY_DIR}/compile_tests"
${PROJECT_SOURCE_DIR}/cmake/compile_tests/oss_is_v4.c)
if(USE_OSS)
# strlcpy is not available on BSD systems that use glibc,
# like Debian kfreebsd, so try using libbsd if available
include(CheckSymbolExists)
check_symbol_exists(strlcpy string.h HAVE_STRLCPY)
if(NOT HAVE_STRLCPY)
pkg_check_modules(libbsd-overlay IMPORTED_TARGET libbsd-overlay)
if(libbsd-overlay_FOUND)
target_link_libraries(cubeb PRIVATE PkgConfig::libbsd-overlay)
set(HAVE_STRLCPY true)
endif()
endif()
if (HAVE_STRLCPY)
target_sources(cubeb PRIVATE
src/cubeb_oss.c)
target_compile_definitions(cubeb PRIVATE USE_OSS)
check_include_files(sys/soundcard.h HAVE_SYS_SOUNDCARD_H)
if(HAVE_SYS_SOUNDCARD_H)
try_compile(USE_OSS "${PROJECT_BINARY_DIR}/compile_tests"
${PROJECT_SOURCE_DIR}/cmake/compile_tests/oss_is_v4.c)
if(USE_OSS)
# strlcpy is not available on BSD systems that use glibc,
# like Debian kfreebsd, so try using libbsd if available
include(CheckSymbolExists)
check_symbol_exists(strlcpy string.h HAVE_STRLCPY)
if(NOT HAVE_STRLCPY)
pkg_check_modules(libbsd-overlay IMPORTED_TARGET libbsd-overlay)
if(libbsd-overlay_FOUND)
target_link_libraries(cubeb PRIVATE PkgConfig::libbsd-overlay)
set(HAVE_STRLCPY true)
endif()
endif()
if (HAVE_STRLCPY)
target_sources(cubeb PRIVATE
src/cubeb_oss.c)
target_compile_definitions(cubeb PRIVATE USE_OSS)
endif()
endif()
endif()
endif()
50 changes: 49 additions & 1 deletion 3rdparty/cubeb/include/cubeb/cubeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,23 @@ typedef enum {
the jack backend. */
} cubeb_stream_prefs;

/**
* Input stream audio processing parameters. Only applicable with
* CUBEB_STREAM_PREF_VOICE.
*/
typedef enum {
CUBEB_INPUT_PROCESSING_PARAM_NONE = 0x00,
CUBEB_INPUT_PROCESSING_PARAM_ECHO_CANCELLATION = 0x01,
CUBEB_INPUT_PROCESSING_PARAM_NOISE_SUPPRESSION = 0x02,
CUBEB_INPUT_PROCESSING_PARAM_AUTOMATIC_GAIN_CONTROL = 0x04,
CUBEB_INPUT_PROCESSING_PARAM_VOICE_ISOLATION = 0x08,
} cubeb_input_processing_params;

/** Stream format initialization parameters. */
typedef struct {
cubeb_sample_format format; /**< Requested sample format. One of
#cubeb_sample_format. */
uint32_t rate; /**< Requested sample rate. Valid range is [1000, 192000]. */
uint32_t rate; /**< Requested sample rate. Valid range is [1000, 384000]. */
uint32_t channels; /**< Requested channel count. Valid range is [1, 8]. */
cubeb_channel_layout
layout; /**< Requested channel layout. This must be consistent with the
Expand Down Expand Up @@ -519,6 +531,18 @@ cubeb_get_min_latency(cubeb * context, cubeb_stream_params * params,
CUBEB_EXPORT int
cubeb_get_preferred_sample_rate(cubeb * context, uint32_t * rate);

/** Get the supported input processing features for this backend. See
cubeb_stream_set_input_processing for how to set them for a particular input
stream.
@param context A pointer to the cubeb context.
@param params Out parameter for the input processing params supported by
this backend.
@retval CUBEB_OK
@retval CUBEB_ERROR_NOT_SUPPORTED */
CUBEB_EXPORT int
cubeb_get_supported_input_processing_params(
cubeb * context, cubeb_input_processing_params * params);

/** Destroy an application context. This must be called after all stream have
* been destroyed.
@param context A pointer to the cubeb context.*/
Expand Down Expand Up @@ -646,6 +670,30 @@ CUBEB_EXPORT int
cubeb_stream_get_current_device(cubeb_stream * stm,
cubeb_device ** const device);

/** Set input mute state for this stream. Some platforms notify the user when an
application is accessing audio input. When all inputs are muted they can
prove to the user that the application is not actively capturing any input.
@param stream the stream for which to set input mute state
@param muted whether the input should mute or not
@retval CUBEB_OK
@retval CUBEB_ERROR_INVALID_PARAMETER if this stream does not have an input
device
@retval CUBEB_ERROR_NOT_SUPPORTED */
CUBEB_EXPORT int
cubeb_stream_set_input_mute(cubeb_stream * stream, int mute);

/** Set what input processing features to enable for this stream.
@param stream the stream for which to set input processing features.
@param params what input processing features to use
@retval CUBEB_OK
@retval CUBEB_ERROR if params could not be applied
@retval CUBEB_ERROR_INVALID_PARAMETER if a given param is not supported by
this backend, or if this stream does not have an input device
@retval CUBEB_ERROR_NOT_SUPPORTED */
CUBEB_EXPORT int
cubeb_stream_set_input_processing_params(cubeb_stream * stream,
cubeb_input_processing_params params);

/** Destroy a cubeb_device structure.
@param stream the stream passed in cubeb_stream_get_current_device
@param devices the devices to destroy
Expand Down
5 changes: 5 additions & 0 deletions 3rdparty/cubeb/src/cubeb-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct cubeb_ops {
int (*get_min_latency)(cubeb * context, cubeb_stream_params params,
uint32_t * latency_ms);
int (*get_preferred_sample_rate)(cubeb * context, uint32_t * rate);
int (*get_supported_input_processing_params)(
cubeb * context, cubeb_input_processing_params * params);
int (*enumerate_devices)(cubeb * context, cubeb_device_type type,
cubeb_device_collection * collection);
int (*device_collection_destroy)(cubeb * context,
Expand All @@ -62,6 +64,9 @@ struct cubeb_ops {
int (*stream_set_name)(cubeb_stream * stream, char const * stream_name);
int (*stream_get_current_device)(cubeb_stream * stream,
cubeb_device ** const device);
int (*stream_set_input_mute)(cubeb_stream * stream, int mute);
int (*stream_set_input_processing_params)(
cubeb_stream * stream, cubeb_input_processing_params params);
int (*stream_device_destroy)(cubeb_stream * stream, cubeb_device * device);
int (*stream_register_device_changed_callback)(
cubeb_stream * stream,
Expand Down
52 changes: 47 additions & 5 deletions 3rdparty/cubeb/src/cubeb.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ validate_stream_params(cubeb_stream_params * input_stream_params,
XASSERT(input_stream_params || output_stream_params);
if (output_stream_params) {
if (output_stream_params->rate < 1000 ||
output_stream_params->rate > 192000 ||
output_stream_params->rate > 768000 ||
output_stream_params->channels < 1 ||
output_stream_params->channels > UINT8_MAX) {
return CUBEB_ERROR_INVALID_FORMAT;
}
}
if (input_stream_params) {
if (input_stream_params->rate < 1000 ||
input_stream_params->rate > 192000 ||
input_stream_params->rate > 768000 ||
input_stream_params->channels < 1 ||
input_stream_params->channels > UINT8_MAX) {
return CUBEB_ERROR_INVALID_FORMAT;
Expand Down Expand Up @@ -239,9 +239,6 @@ cubeb_get_backend_names()
#if defined(USE_SNDIO)
"sndio",
#endif
#if defined(USE_SUN)
"sun",
#endif
#if defined(USE_OSS)
"oss",
#endif
Expand Down Expand Up @@ -304,6 +301,21 @@ cubeb_get_preferred_sample_rate(cubeb * context, uint32_t * rate)
return context->ops->get_preferred_sample_rate(context, rate);
}

int
cubeb_get_supported_input_processing_params(
cubeb * context, cubeb_input_processing_params * params)
{
if (!context || !params) {
return CUBEB_ERROR_INVALID_PARAMETER;
}

if (!context->ops->get_supported_input_processing_params) {
return CUBEB_ERROR_NOT_SUPPORTED;
}

return context->ops->get_supported_input_processing_params(context, params);
}

void
cubeb_destroy(cubeb * context)
{
Expand Down Expand Up @@ -463,6 +475,36 @@ cubeb_stream_get_current_device(cubeb_stream * stream,
return stream->context->ops->stream_get_current_device(stream, device);
}

int
cubeb_stream_set_input_mute(cubeb_stream * stream, int mute)
{
if (!stream) {
return CUBEB_ERROR_INVALID_PARAMETER;
}

if (!stream->context->ops->stream_set_input_mute) {
return CUBEB_ERROR_NOT_SUPPORTED;
}

return stream->context->ops->stream_set_input_mute(stream, mute);
}

int
cubeb_stream_set_input_processing_params(cubeb_stream * stream,
cubeb_input_processing_params params)
{
if (!stream) {
return CUBEB_ERROR_INVALID_PARAMETER;
}

if (!stream->context->ops->stream_set_input_processing_params) {
return CUBEB_ERROR_NOT_SUPPORTED;
}

return stream->context->ops->stream_set_input_processing_params(stream,
params);
}

int
cubeb_stream_device_destroy(cubeb_stream * stream, cubeb_device * device)
{
Expand Down
3 changes: 3 additions & 0 deletions 3rdparty/cubeb/src/cubeb_alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ static struct cubeb_ops const alsa_ops = {
.get_max_channel_count = alsa_get_max_channel_count,
.get_min_latency = alsa_get_min_latency,
.get_preferred_sample_rate = alsa_get_preferred_sample_rate,
.get_supported_input_processing_params = NULL,
.enumerate_devices = alsa_enumerate_devices,
.device_collection_destroy = alsa_device_collection_destroy,
.destroy = alsa_destroy,
Expand All @@ -1485,6 +1486,8 @@ static struct cubeb_ops const alsa_ops = {
.stream_set_volume = alsa_stream_set_volume,
.stream_set_name = NULL,
.stream_get_current_device = NULL,
.stream_set_input_mute = NULL,
.stream_set_input_processing_params = NULL,
.stream_device_destroy = NULL,
.stream_register_device_changed_callback = NULL,
.register_device_collection_changed = NULL};
Loading

0 comments on commit c573c00

Please sign in to comment.