2020-12-28 16:15:37 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2013 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* This program is made available under an ISC-style license. See the
|
|
|
|
* accompanying file LICENSE for details.
|
|
|
|
*/
|
|
|
|
#define _WIN32_WINNT 0x0603
|
|
|
|
#define NOMINMAX
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
2020-12-28 16:15:37 +01:00
|
|
|
#include <audioclient.h>
|
2021-12-08 07:33:31 +01:00
|
|
|
#include <avrt.h>
|
|
|
|
#include <cmath>
|
2020-12-28 16:15:37 +01:00
|
|
|
#include <devicetopology.h>
|
2021-12-08 07:33:31 +01:00
|
|
|
#include <initguid.h>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <mmdeviceapi.h>
|
2020-12-28 16:15:37 +01:00
|
|
|
#include <process.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <vector>
|
2021-12-08 07:33:31 +01:00
|
|
|
#include <windef.h>
|
|
|
|
#include <windows.h>
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
#include "cubeb-internal.h"
|
2021-12-08 07:33:31 +01:00
|
|
|
#include "cubeb/cubeb.h"
|
2020-12-28 16:15:37 +01:00
|
|
|
#include "cubeb_mixer.h"
|
|
|
|
#include "cubeb_resampler.h"
|
|
|
|
#include "cubeb_strings.h"
|
|
|
|
#include "cubeb_utils.h"
|
|
|
|
|
|
|
|
// Windows 10 exposes the IAudioClient3 interface to create low-latency streams.
|
2021-12-08 07:33:31 +01:00
|
|
|
// Copy the interface definition from audioclient.h here to make the code
|
|
|
|
// simpler and so that we can still access IAudioClient3 via COM if cubeb was
|
|
|
|
// compiled against an older SDK.
|
2020-12-28 16:15:37 +01:00
|
|
|
#ifndef __IAudioClient3_INTERFACE_DEFINED__
|
|
|
|
#define __IAudioClient3_INTERFACE_DEFINED__
|
|
|
|
MIDL_INTERFACE("7ED4EE07-8E67-4CD4-8C1A-2B7A5987AD42")
|
|
|
|
IAudioClient3 : public IAudioClient
|
|
|
|
{
|
|
|
|
public:
|
2021-12-08 07:33:31 +01:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE GetSharedModeEnginePeriod(
|
|
|
|
/* [annotation][in] */
|
|
|
|
_In_ const WAVEFORMATEX * pFormat,
|
|
|
|
/* [annotation][out] */
|
|
|
|
_Out_ UINT32 * pDefaultPeriodInFrames,
|
|
|
|
/* [annotation][out] */
|
|
|
|
_Out_ UINT32 * pFundamentalPeriodInFrames,
|
|
|
|
/* [annotation][out] */
|
|
|
|
_Out_ UINT32 * pMinPeriodInFrames,
|
|
|
|
/* [annotation][out] */
|
|
|
|
_Out_ UINT32 * pMaxPeriodInFrames) = 0;
|
|
|
|
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE GetCurrentSharedModeEnginePeriod(
|
|
|
|
/* [unique][annotation][out] */
|
|
|
|
_Out_ WAVEFORMATEX * *ppFormat,
|
|
|
|
/* [annotation][out] */
|
|
|
|
_Out_ UINT32 * pCurrentPeriodInFrames) = 0;
|
|
|
|
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE InitializeSharedAudioStream(
|
|
|
|
/* [annotation][in] */
|
|
|
|
_In_ DWORD StreamFlags,
|
|
|
|
/* [annotation][in] */
|
|
|
|
_In_ UINT32 PeriodInFrames,
|
|
|
|
/* [annotation][in] */
|
|
|
|
_In_ const WAVEFORMATEX * pFormat,
|
|
|
|
/* [annotation][in] */
|
|
|
|
_In_opt_ LPCGUID AudioSessionGuid) = 0;
|
2020-12-28 16:15:37 +01:00
|
|
|
};
|
|
|
|
#ifdef __CRT_UUID_DECL
|
|
|
|
// Required for MinGW
|
2021-12-08 07:33:31 +01:00
|
|
|
__CRT_UUID_DECL(IAudioClient3, 0x7ED4EE07, 0x8E67, 0x4CD4, 0x8C, 0x1A, 0x2B,
|
|
|
|
0x7A, 0x59, 0x87, 0xAD, 0x42)
|
2020-12-28 16:15:37 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
// Copied from audioclient.h in the Windows 10 SDK
|
|
|
|
#ifndef AUDCLNT_E_ENGINE_PERIODICITY_LOCKED
|
2021-12-08 07:33:31 +01:00
|
|
|
#define AUDCLNT_E_ENGINE_PERIODICITY_LOCKED AUDCLNT_ERR(0x028)
|
2020-12-28 16:15:37 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PKEY_Device_FriendlyName
|
2021-12-08 07:33:31 +01:00
|
|
|
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,
|
|
|
|
0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0,
|
|
|
|
14); // DEVPROP_TYPE_STRING
|
2020-12-28 16:15:37 +01:00
|
|
|
#endif
|
|
|
|
#ifndef PKEY_Device_InstanceId
|
2021-12-08 07:33:31 +01:00
|
|
|
DEFINE_PROPERTYKEY(PKEY_Device_InstanceId, 0x78c34fc8, 0x104a, 0x4aca, 0x9e,
|
|
|
|
0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57,
|
|
|
|
0x00000100); // VT_LPWSTR
|
2020-12-28 16:15:37 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int64_t LATENCY_NOT_AVAILABLE_YET = -1;
|
|
|
|
|
|
|
|
struct com_heap_ptr_deleter {
|
2021-12-08 07:33:31 +01:00
|
|
|
void operator()(void * ptr) const noexcept { CoTaskMemFree(ptr); }
|
2020-12-28 16:15:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using com_heap_ptr = std::unique_ptr<T, com_heap_ptr_deleter>;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
template <typename T, size_t N> constexpr size_t ARRAY_LENGTH(T (&)[N])
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
template <typename T> class no_addref_release : public T {
|
2020-12-28 16:15:37 +01:00
|
|
|
ULONG STDMETHODCALLTYPE AddRef() = 0;
|
|
|
|
ULONG STDMETHODCALLTYPE Release() = 0;
|
|
|
|
};
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
template <typename T> class com_ptr {
|
2020-12-28 16:15:37 +01:00
|
|
|
public:
|
|
|
|
com_ptr() noexcept = default;
|
|
|
|
|
|
|
|
com_ptr(com_ptr const & other) noexcept = delete;
|
|
|
|
com_ptr & operator=(com_ptr const & other) noexcept = delete;
|
|
|
|
T ** operator&() const noexcept = delete;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
~com_ptr() noexcept { release(); }
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
com_ptr(com_ptr && other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
com_ptr & operator=(com_ptr && other) noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
if (ptr != other.ptr) {
|
|
|
|
release();
|
|
|
|
ptr = other.ptr;
|
|
|
|
other.ptr = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
explicit operator bool() const noexcept { return nullptr != ptr; }
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
no_addref_release<T> * operator->() const noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
return static_cast<no_addref_release<T> *>(ptr);
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
T * get() const noexcept { return ptr; }
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
T ** receive() noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
XASSERT(ptr == nullptr);
|
|
|
|
return &ptr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void ** receive_vpp() noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
return reinterpret_cast<void **>(receive());
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
com_ptr & operator=(std::nullptr_t) noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
release();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void reset(T * p = nullptr) noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
release();
|
|
|
|
ptr = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-12-08 07:33:31 +01:00
|
|
|
void release() noexcept
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
T * temp = ptr;
|
|
|
|
|
|
|
|
if (temp) {
|
|
|
|
ptr = nullptr;
|
|
|
|
temp->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T * ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern cubeb_ops const wasapi_ops;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_stop(cubeb_stream * stm);
|
|
|
|
int
|
|
|
|
wasapi_stream_start(cubeb_stream * stm);
|
|
|
|
void
|
|
|
|
close_wasapi_stream(cubeb_stream * stm);
|
|
|
|
int
|
|
|
|
setup_wasapi_stream(cubeb_stream * stm);
|
|
|
|
ERole
|
|
|
|
pref_to_role(cubeb_stream_prefs param);
|
|
|
|
int
|
|
|
|
wasapi_create_device(cubeb * ctx, cubeb_device_info & ret,
|
|
|
|
IMMDeviceEnumerator * enumerator, IMMDevice * dev);
|
|
|
|
void
|
|
|
|
wasapi_destroy_device(cubeb_device_info * device_info);
|
|
|
|
static int
|
|
|
|
wasapi_enumerate_devices(cubeb * context, cubeb_device_type type,
|
|
|
|
cubeb_device_collection * out);
|
|
|
|
static int
|
|
|
|
wasapi_device_collection_destroy(cubeb * ctx,
|
|
|
|
cubeb_device_collection * collection);
|
|
|
|
static char const *
|
|
|
|
wstr_to_utf8(wchar_t const * str);
|
|
|
|
static std::unique_ptr<wchar_t const[]>
|
|
|
|
utf8_to_wstr(char const * str);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
} // namespace
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
class wasapi_collection_notification_client;
|
|
|
|
class monitor_device_notifications;
|
|
|
|
|
|
|
|
struct cubeb {
|
|
|
|
cubeb_ops const * ops = &wasapi_ops;
|
|
|
|
cubeb_strings * device_ids;
|
|
|
|
/* Device enumerator to get notifications when the
|
|
|
|
device collection change. */
|
|
|
|
com_ptr<IMMDeviceEnumerator> device_collection_enumerator;
|
|
|
|
com_ptr<wasapi_collection_notification_client> collection_notification_client;
|
|
|
|
/* Collection changed for input (capture) devices. */
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_device_collection_changed_callback input_collection_changed_callback =
|
|
|
|
nullptr;
|
2020-12-28 16:15:37 +01:00
|
|
|
void * input_collection_changed_user_ptr = nullptr;
|
|
|
|
/* Collection changed for output (render) devices. */
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_device_collection_changed_callback output_collection_changed_callback =
|
|
|
|
nullptr;
|
2020-12-28 16:15:37 +01:00
|
|
|
void * output_collection_changed_user_ptr = nullptr;
|
|
|
|
UINT64 performance_counter_frequency;
|
|
|
|
};
|
|
|
|
|
|
|
|
class wasapi_endpoint_notification_client;
|
|
|
|
|
|
|
|
/* We have three possible callbacks we can use with a stream:
|
|
|
|
* - input only
|
|
|
|
* - output only
|
|
|
|
* - synchronized input and output
|
|
|
|
*
|
|
|
|
* Returns true when we should continue to play, false otherwise.
|
|
|
|
*/
|
|
|
|
typedef bool (*wasapi_refill_callback)(cubeb_stream * stm);
|
|
|
|
|
|
|
|
struct cubeb_stream {
|
|
|
|
/* Note: Must match cubeb_stream layout in cubeb.c. */
|
|
|
|
cubeb * context = nullptr;
|
|
|
|
void * user_ptr = nullptr;
|
|
|
|
/**/
|
|
|
|
|
|
|
|
/* Mixer pameters. We need to convert the input stream to this
|
|
|
|
samplerate/channel layout, as WASAPI does not resample nor upmix
|
|
|
|
itself. */
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_stream_params input_mix_params = {CUBEB_SAMPLE_FLOAT32NE, 0, 0,
|
|
|
|
CUBEB_LAYOUT_UNDEFINED,
|
|
|
|
CUBEB_STREAM_PREF_NONE};
|
|
|
|
cubeb_stream_params output_mix_params = {CUBEB_SAMPLE_FLOAT32NE, 0, 0,
|
|
|
|
CUBEB_LAYOUT_UNDEFINED,
|
|
|
|
CUBEB_STREAM_PREF_NONE};
|
2020-12-28 16:15:37 +01:00
|
|
|
/* Stream parameters. This is what the client requested,
|
|
|
|
* and what will be presented in the callback. */
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_stream_params input_stream_params = {CUBEB_SAMPLE_FLOAT32NE, 0, 0,
|
|
|
|
CUBEB_LAYOUT_UNDEFINED,
|
|
|
|
CUBEB_STREAM_PREF_NONE};
|
|
|
|
cubeb_stream_params output_stream_params = {CUBEB_SAMPLE_FLOAT32NE, 0, 0,
|
|
|
|
CUBEB_LAYOUT_UNDEFINED,
|
|
|
|
CUBEB_STREAM_PREF_NONE};
|
2020-12-28 16:15:37 +01:00
|
|
|
/* A MMDevice role for this stream: either communication or console here. */
|
|
|
|
ERole role;
|
|
|
|
/* True if this stream will transport voice-data. */
|
|
|
|
bool voice;
|
|
|
|
/* True if the input device of this stream is using bluetooth handsfree. */
|
|
|
|
bool input_bluetooth_handsfree;
|
|
|
|
/* The input and output device, or NULL for default. */
|
|
|
|
std::unique_ptr<const wchar_t[]> input_device_id;
|
|
|
|
std::unique_ptr<const wchar_t[]> output_device_id;
|
|
|
|
com_ptr<IMMDevice> input_device;
|
|
|
|
com_ptr<IMMDevice> output_device;
|
|
|
|
/* The latency initially requested for this stream, in frames. */
|
|
|
|
unsigned latency = 0;
|
|
|
|
cubeb_state_callback state_callback = nullptr;
|
|
|
|
cubeb_data_callback data_callback = nullptr;
|
|
|
|
wasapi_refill_callback refill_callback = nullptr;
|
|
|
|
/* True when a loopback device is requested with no output device. In this
|
|
|
|
case a dummy output device is opened to drive the loopback, but should not
|
|
|
|
be exposed. */
|
|
|
|
bool has_dummy_output = false;
|
|
|
|
/* Lifetime considerations:
|
|
|
|
- client, render_client, audio_clock and audio_stream_volume are interface
|
|
|
|
pointer to the IAudioClient.
|
|
|
|
- The lifetime for device_enumerator and notification_client, resampler,
|
|
|
|
mix_buffer are the same as the cubeb_stream instance. */
|
|
|
|
|
|
|
|
/* Main handle on the WASAPI stream. */
|
|
|
|
com_ptr<IAudioClient> output_client;
|
|
|
|
/* Interface pointer to use the event-driven interface. */
|
|
|
|
com_ptr<IAudioRenderClient> render_client;
|
2021-12-08 07:33:31 +01:00
|
|
|
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
2020-12-28 16:15:37 +01:00
|
|
|
/* Interface pointer to use the volume facilities. */
|
|
|
|
com_ptr<IAudioStreamVolume> audio_stream_volume;
|
2021-12-08 07:33:31 +01:00
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
/* Interface pointer to use the stream audio clock. */
|
|
|
|
com_ptr<IAudioClock> audio_clock;
|
|
|
|
/* Frames written to the stream since it was opened. Reset on device
|
|
|
|
change. Uses mix_params.rate. */
|
|
|
|
UINT64 frames_written = 0;
|
|
|
|
/* Frames written to the (logical) stream since it was first
|
|
|
|
created. Updated on device change. Uses stream_params.rate. */
|
|
|
|
UINT64 total_frames_written = 0;
|
|
|
|
/* Last valid reported stream position. Used to ensure the position
|
|
|
|
reported by stream_get_position increases monotonically. */
|
|
|
|
UINT64 prev_position = 0;
|
|
|
|
/* Device enumerator to be able to be notified when the default
|
|
|
|
device change. */
|
|
|
|
com_ptr<IMMDeviceEnumerator> device_enumerator;
|
|
|
|
/* Device notification client, to be able to be notified when the default
|
|
|
|
audio device changes and route the audio to the new default audio output
|
|
|
|
device */
|
|
|
|
com_ptr<wasapi_endpoint_notification_client> notification_client;
|
|
|
|
/* Main andle to the WASAPI capture stream. */
|
|
|
|
com_ptr<IAudioClient> input_client;
|
|
|
|
/* Interface to use the event driven capture interface */
|
|
|
|
com_ptr<IAudioCaptureClient> capture_client;
|
|
|
|
/* This event is set by the stream_stop and stream_destroy
|
|
|
|
function, so the render loop can exit properly. */
|
|
|
|
HANDLE shutdown_event = 0;
|
|
|
|
/* Set by OnDefaultDeviceChanged when a stream reconfiguration is required.
|
|
|
|
The reconfiguration is handled by the render loop thread. */
|
|
|
|
HANDLE reconfigure_event = 0;
|
|
|
|
/* This is set by WASAPI when we should refill the stream. */
|
|
|
|
HANDLE refill_event = 0;
|
|
|
|
/* This is set by WASAPI when we should read from the input stream. In
|
|
|
|
* practice, we read from the input stream in the output callback, so
|
|
|
|
* this is not used, but it is necessary to start getting input data. */
|
|
|
|
HANDLE input_available_event = 0;
|
|
|
|
/* Each cubeb_stream has its own thread. */
|
|
|
|
HANDLE thread = 0;
|
|
|
|
/* The lock protects all members that are touched by the render thread or
|
|
|
|
change during a device reset, including: audio_clock, audio_stream_volume,
|
|
|
|
client, frames_written, mix_params, total_frames_written, prev_position. */
|
|
|
|
owned_critical_section stream_reset_lock;
|
|
|
|
/* Maximum number of frames that can be passed down in a callback. */
|
|
|
|
uint32_t input_buffer_frame_count = 0;
|
|
|
|
/* Maximum number of frames that can be requested in a callback. */
|
|
|
|
uint32_t output_buffer_frame_count = 0;
|
|
|
|
/* Resampler instance. Resampling will only happen if necessary. */
|
2021-12-08 07:33:31 +01:00
|
|
|
std::unique_ptr<cubeb_resampler, decltype(&cubeb_resampler_destroy)>
|
|
|
|
resampler = {nullptr, cubeb_resampler_destroy};
|
2020-12-28 16:15:37 +01:00
|
|
|
/* Mixer interfaces */
|
2021-12-08 07:33:31 +01:00
|
|
|
std::unique_ptr<cubeb_mixer, decltype(&cubeb_mixer_destroy)> output_mixer = {
|
|
|
|
nullptr, cubeb_mixer_destroy};
|
|
|
|
std::unique_ptr<cubeb_mixer, decltype(&cubeb_mixer_destroy)> input_mixer = {
|
|
|
|
nullptr, cubeb_mixer_destroy};
|
2020-12-28 16:15:37 +01:00
|
|
|
/* A buffer for up/down mixing multi-channel audio output. */
|
|
|
|
std::vector<BYTE> mix_buffer;
|
|
|
|
/* WASAPI input works in "packets". We re-linearize the audio packets
|
|
|
|
* into this buffer before handing it to the resampler. */
|
|
|
|
std::unique_ptr<auto_array_wrapper> linear_input_buffer;
|
|
|
|
/* Bytes per sample. This multiplied by the number of channels is the number
|
|
|
|
* of bytes per frame. */
|
|
|
|
size_t bytes_per_sample = 0;
|
|
|
|
/* WAVEFORMATEXTENSIBLE sub-format: either PCM or float. */
|
|
|
|
GUID waveformatextensible_sub_format = GUID_NULL;
|
|
|
|
/* Stream volume. Set via stream_set_volume and used to reset volume on
|
|
|
|
device changes. */
|
|
|
|
float volume = 1.0;
|
|
|
|
/* True if the stream is draining. */
|
|
|
|
bool draining = false;
|
|
|
|
/* True when we've destroyed the stream. This pointer is leaked on stream
|
|
|
|
* destruction if we could not join the thread. */
|
2021-12-08 07:33:31 +01:00
|
|
|
std::atomic<std::atomic<bool> *> emergency_bailout{nullptr};
|
|
|
|
/* Synchronizes render thread start to ensure safe access to
|
|
|
|
* emergency_bailout. */
|
2020-12-28 16:15:37 +01:00
|
|
|
HANDLE thread_ready_event = 0;
|
|
|
|
/* This needs an active audio input stream to be known, and is updated in the
|
|
|
|
* first audio input callback. */
|
2021-12-08 07:33:31 +01:00
|
|
|
std::atomic<int64_t> input_latency_hns{LATENCY_NOT_AVAILABLE_YET};
|
|
|
|
|
|
|
|
/* Those attributes count the number of frames requested (resp. received) by
|
|
|
|
the OS, to be able to detect drifts. This is only used for logging for now. */
|
|
|
|
size_t total_input_frames = 0;
|
|
|
|
size_t total_output_frames = 0;
|
2020-12-28 16:15:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class monitor_device_notifications {
|
|
|
|
public:
|
2021-12-08 07:33:31 +01:00
|
|
|
monitor_device_notifications(cubeb * context) : cubeb_context(context)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
create_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
~monitor_device_notifications()
|
|
|
|
{
|
|
|
|
SetEvent(begin_shutdown);
|
|
|
|
WaitForSingleObject(shutdown_complete, INFINITE);
|
|
|
|
CloseHandle(thread);
|
|
|
|
|
|
|
|
CloseHandle(input_changed);
|
|
|
|
CloseHandle(output_changed);
|
|
|
|
CloseHandle(begin_shutdown);
|
|
|
|
CloseHandle(shutdown_complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify(EDataFlow flow)
|
|
|
|
{
|
|
|
|
XASSERT(cubeb_context);
|
|
|
|
if (flow == eCapture && cubeb_context->input_collection_changed_callback) {
|
|
|
|
bool res = SetEvent(input_changed);
|
|
|
|
if (!res) {
|
|
|
|
LOG("Failed to set input changed event");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (flow == eRender && cubeb_context->output_collection_changed_callback) {
|
|
|
|
bool res = SetEvent(output_changed);
|
|
|
|
if (!res) {
|
|
|
|
LOG("Failed to set output changed event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
private:
|
2021-12-08 07:33:31 +01:00
|
|
|
static unsigned int __stdcall thread_proc(LPVOID args)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(args);
|
2021-12-08 07:33:31 +01:00
|
|
|
auto mdn = static_cast<monitor_device_notifications *>(args);
|
2020-12-28 16:15:37 +01:00
|
|
|
mdn->notification_thread_loop();
|
|
|
|
SetEvent(mdn->shutdown_complete);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void notification_thread_loop()
|
|
|
|
{
|
|
|
|
struct auto_com {
|
2021-12-08 07:33:31 +01:00
|
|
|
auto_com()
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
|
|
XASSERT(SUCCEEDED(hr));
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
~auto_com() { CoUninitialize(); }
|
2020-12-28 16:15:37 +01:00
|
|
|
} com;
|
|
|
|
|
|
|
|
HANDLE wait_array[3] = {
|
2021-12-08 07:33:31 +01:00
|
|
|
input_changed,
|
|
|
|
output_changed,
|
|
|
|
begin_shutdown,
|
2020-12-28 16:15:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Sleep(200);
|
|
|
|
|
|
|
|
DWORD wait_result = WaitForMultipleObjects(ARRAY_LENGTH(wait_array),
|
2021-12-08 07:33:31 +01:00
|
|
|
wait_array, FALSE, INFINITE);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (wait_result == WAIT_OBJECT_0) { // input changed
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_context->input_collection_changed_callback(
|
|
|
|
cubeb_context, cubeb_context->input_collection_changed_user_ptr);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else if (wait_result == WAIT_OBJECT_0 + 1) { // output changed
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_context->output_collection_changed_callback(
|
|
|
|
cubeb_context, cubeb_context->output_collection_changed_user_ptr);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else if (wait_result == WAIT_OBJECT_0 + 2) { // shutdown
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
LOG("Unexpected result %lu", wait_result);
|
|
|
|
}
|
|
|
|
} // loop
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_thread()
|
|
|
|
{
|
|
|
|
output_changed = CreateEvent(nullptr, 0, 0, nullptr);
|
|
|
|
if (!output_changed) {
|
|
|
|
LOG("Failed to create output changed event.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_changed = CreateEvent(nullptr, 0, 0, nullptr);
|
|
|
|
if (!input_changed) {
|
|
|
|
LOG("Failed to create input changed event.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
begin_shutdown = CreateEvent(nullptr, 0, 0, nullptr);
|
|
|
|
if (!begin_shutdown) {
|
|
|
|
LOG("Failed to create begin_shutdown event.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown_complete = CreateEvent(nullptr, 0, 0, nullptr);
|
|
|
|
if (!shutdown_complete) {
|
|
|
|
LOG("Failed to create shutdown_complete event.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
thread = (HANDLE)_beginthreadex(nullptr, 256 * 1024, thread_proc, this,
|
|
|
|
STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (!thread) {
|
|
|
|
LOG("Failed to create thread.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE thread = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE output_changed = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE input_changed = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE begin_shutdown = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE shutdown_complete = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
cubeb * cubeb_context = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
class wasapi_collection_notification_client : public IMMNotificationClient {
|
2020-12-28 16:15:37 +01:00
|
|
|
public:
|
|
|
|
/* The implementation of MSCOM was copied from MSDN. */
|
2021-12-08 07:33:31 +01:00
|
|
|
ULONG STDMETHODCALLTYPE AddRef() { return InterlockedIncrement(&ref_count); }
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
ULONG STDMETHODCALLTYPE Release()
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
ULONG ulRef = InterlockedDecrement(&ref_count);
|
|
|
|
if (0 == ulRef) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return ulRef;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID ** ppvInterface)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
if (__uuidof(IUnknown) == riid) {
|
|
|
|
AddRef();
|
2021-12-08 07:33:31 +01:00
|
|
|
*ppvInterface = (IUnknown *)this;
|
2020-12-28 16:15:37 +01:00
|
|
|
} else if (__uuidof(IMMNotificationClient) == riid) {
|
|
|
|
AddRef();
|
2021-12-08 07:33:31 +01:00
|
|
|
*ppvInterface = (IMMNotificationClient *)this;
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
|
|
|
*ppvInterface = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
wasapi_collection_notification_client(cubeb * context)
|
2021-12-08 07:33:31 +01:00
|
|
|
: ref_count(1), cubeb_context(context), monitor_notifications(context)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(cubeb_context);
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
virtual ~wasapi_collection_notification_client() {}
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role,
|
|
|
|
LPCWSTR device_id)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
LOG("collection: Audio device default changed, id = %S.", device_id);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The remaining methods are not implemented, they simply log when called (if
|
|
|
|
log is enabled), for debugging. */
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR device_id)
|
|
|
|
{
|
|
|
|
LOG("collection: Audio device added.");
|
|
|
|
return S_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR device_id)
|
|
|
|
{
|
|
|
|
LOG("collection: Audio device removed.");
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR device_id,
|
|
|
|
DWORD new_state)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(cubeb_context->output_collection_changed_callback ||
|
|
|
|
cubeb_context->input_collection_changed_callback);
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("collection: Audio device state changed, id = %S, state = %lu.",
|
|
|
|
device_id, new_state);
|
2020-12-28 16:15:37 +01:00
|
|
|
EDataFlow flow;
|
|
|
|
HRESULT hr = GetDataFlow(device_id, &flow);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
monitor_notifications.notify(flow);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR device_id,
|
|
|
|
const PROPERTYKEY key)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
// Audio device property value changed.
|
2020-12-28 16:15:37 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
HRESULT GetDataFlow(LPCWSTR device_id, EDataFlow * flow)
|
|
|
|
{
|
|
|
|
com_ptr<IMMDevice> device;
|
|
|
|
com_ptr<IMMEndpoint> endpoint;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr = cubeb_context->device_collection_enumerator->GetDevice(
|
|
|
|
device_id, device.receive());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("collection: Could not get device: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = device->QueryInterface(IID_PPV_ARGS(endpoint.receive()));
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("collection: Could not get endpoint: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoint->GetDataFlow(flow);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* refcount for this instance, necessary to implement MSCOM semantics. */
|
|
|
|
LONG ref_count;
|
|
|
|
|
|
|
|
cubeb * cubeb_context = nullptr;
|
|
|
|
monitor_device_notifications monitor_notifications;
|
|
|
|
};
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
class wasapi_endpoint_notification_client : public IMMNotificationClient {
|
2020-12-28 16:15:37 +01:00
|
|
|
public:
|
|
|
|
/* The implementation of MSCOM was copied from MSDN. */
|
2021-12-08 07:33:31 +01:00
|
|
|
ULONG STDMETHODCALLTYPE AddRef() { return InterlockedIncrement(&ref_count); }
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
ULONG STDMETHODCALLTYPE Release()
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
ULONG ulRef = InterlockedDecrement(&ref_count);
|
|
|
|
if (0 == ulRef) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return ulRef;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID ** ppvInterface)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
if (__uuidof(IUnknown) == riid) {
|
|
|
|
AddRef();
|
2021-12-08 07:33:31 +01:00
|
|
|
*ppvInterface = (IUnknown *)this;
|
2020-12-28 16:15:37 +01:00
|
|
|
} else if (__uuidof(IMMNotificationClient) == riid) {
|
|
|
|
AddRef();
|
2021-12-08 07:33:31 +01:00
|
|
|
*ppvInterface = (IMMNotificationClient *)this;
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
|
|
|
*ppvInterface = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
wasapi_endpoint_notification_client(HANDLE event, ERole role)
|
2021-12-08 07:33:31 +01:00
|
|
|
: ref_count(1), reconfigure_event(event), role(role)
|
|
|
|
{
|
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
virtual ~wasapi_endpoint_notification_client() {}
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role,
|
|
|
|
LPCWSTR device_id)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
LOG("endpoint: Audio device default changed.");
|
|
|
|
|
|
|
|
/* we only support a single stream type for now. */
|
|
|
|
if (flow != eRender && role != this->role) {
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ok = SetEvent(reconfigure_event);
|
|
|
|
if (!ok) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("endpoint: SetEvent on reconfigure_event failed: %lx",
|
|
|
|
GetLastError());
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The remaining methods are not implemented, they simply log when called (if
|
|
|
|
log is enabled), for debugging. */
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR device_id)
|
|
|
|
{
|
|
|
|
LOG("endpoint: Audio device added.");
|
|
|
|
return S_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR device_id)
|
|
|
|
{
|
|
|
|
LOG("endpoint: Audio device removed.");
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR device_id,
|
|
|
|
DWORD new_state)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
LOG("endpoint: Audio device state changed.");
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR device_id,
|
|
|
|
const PROPERTYKEY key)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
// Audio device property value changed.
|
2020-12-28 16:15:37 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
private:
|
|
|
|
/* refcount for this instance, necessary to implement MSCOM semantics. */
|
|
|
|
LONG ref_count;
|
|
|
|
HANDLE reconfigure_event;
|
|
|
|
ERole role;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
char const *
|
|
|
|
intern_device_id(cubeb * ctx, wchar_t const * id)
|
|
|
|
{
|
|
|
|
XASSERT(id);
|
|
|
|
|
|
|
|
char const * tmp = wstr_to_utf8(id);
|
|
|
|
if (!tmp) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const * interned = cubeb_strings_intern(ctx->device_ids, tmp);
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
free((void *)tmp);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
return interned;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
bool
|
|
|
|
has_input(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
return stm->input_stream_params.rate != 0;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
bool
|
|
|
|
has_output(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
return stm->output_stream_params.rate != 0;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
double
|
|
|
|
stream_to_mix_samplerate_ratio(cubeb_stream_params & stream,
|
|
|
|
cubeb_stream_params & mixer)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
return double(stream.rate) / mixer.rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the channel layout into the corresponding KSAUDIO_CHANNEL_CONFIG.
|
2021-12-08 07:33:31 +01:00
|
|
|
See more:
|
|
|
|
https://msdn.microsoft.com/en-us/library/windows/hardware/ff537083(v=vs.85).aspx
|
|
|
|
*/
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
cubeb_channel_layout
|
|
|
|
mask_to_channel_layout(WAVEFORMATEX const * fmt)
|
|
|
|
{
|
|
|
|
cubeb_channel_layout mask = 0;
|
|
|
|
|
|
|
|
if (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEXTENSIBLE const * ext =
|
|
|
|
reinterpret_cast<WAVEFORMATEXTENSIBLE const *>(fmt);
|
2020-12-28 16:15:37 +01:00
|
|
|
mask = ext->dwChannelMask;
|
|
|
|
} else if (fmt->wFormatTag == WAVE_FORMAT_PCM ||
|
|
|
|
fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
|
|
|
|
if (fmt->nChannels == 1) {
|
|
|
|
mask = CHANNEL_FRONT_CENTER;
|
|
|
|
} else if (fmt->nChannels == 2) {
|
|
|
|
mask = CHANNEL_FRONT_LEFT | CHANNEL_FRONT_RIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
get_rate(cubeb_stream * stm)
|
|
|
|
{
|
|
|
|
return has_input(stm) ? stm->input_stream_params.rate
|
|
|
|
: stm->output_stream_params.rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
hns_to_frames(uint32_t rate, REFERENCE_TIME hns)
|
|
|
|
{
|
|
|
|
return std::ceil((hns - 1) / 10000000.0 * rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
hns_to_frames(cubeb_stream * stm, REFERENCE_TIME hns)
|
|
|
|
{
|
|
|
|
return hns_to_frames(get_rate(stm), hns);
|
|
|
|
}
|
|
|
|
|
|
|
|
REFERENCE_TIME
|
|
|
|
frames_to_hns(uint32_t rate, uint32_t frames)
|
|
|
|
{
|
|
|
|
return std::ceil(frames * 10000000.0 / rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This returns the size of a frame in the stream, before the eventual upmix
|
|
|
|
occurs. */
|
|
|
|
static size_t
|
|
|
|
frames_to_bytes_before_mix(cubeb_stream * stm, size_t frames)
|
|
|
|
{
|
|
|
|
// This is called only when we has a output client.
|
|
|
|
XASSERT(has_output(stm));
|
|
|
|
return stm->output_stream_params.channels * stm->bytes_per_sample * frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function handles the processing of the input and output audio,
|
|
|
|
* converting it to rate and channel layout specified at initialization.
|
|
|
|
* It then calls the data callback, via the resampler. */
|
|
|
|
long
|
|
|
|
refill(cubeb_stream * stm, void * input_buffer, long input_frames_count,
|
|
|
|
void * output_buffer, long output_frames_needed)
|
|
|
|
{
|
|
|
|
XASSERT(!stm->draining);
|
|
|
|
/* If we need to upmix after resampling, resample into the mix buffer to
|
|
|
|
avoid a copy. Avoid exposing output if it is a dummy stream. */
|
|
|
|
void * dest = nullptr;
|
|
|
|
if (has_output(stm) && !stm->has_dummy_output) {
|
|
|
|
if (stm->output_mixer) {
|
|
|
|
dest = stm->mix_buffer.data();
|
|
|
|
} else {
|
|
|
|
dest = output_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
long out_frames =
|
|
|
|
cubeb_resampler_fill(stm->resampler.get(), input_buffer,
|
|
|
|
&input_frames_count, dest, output_frames_needed);
|
2020-12-28 16:15:37 +01:00
|
|
|
/* TODO: Report out_frames < 0 as an error via the API. */
|
|
|
|
XASSERT(out_frames >= 0);
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
float volume = 1.0;
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
stm->frames_written += out_frames;
|
2021-12-08 07:33:31 +01:00
|
|
|
volume = stm->volume;
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Go in draining mode if we got fewer frames than requested. If the stream
|
|
|
|
has no output we still expect the callback to return number of frames read
|
|
|
|
from input, otherwise we stop. */
|
|
|
|
if ((out_frames < output_frames_needed) ||
|
|
|
|
(!has_output(stm) && out_frames < input_frames_count)) {
|
|
|
|
LOG("start draining.");
|
|
|
|
stm->draining = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is not true, there will be glitches.
|
|
|
|
It is alright to have produced less frames if we are draining, though. */
|
2021-12-08 07:33:31 +01:00
|
|
|
XASSERT(out_frames == output_frames_needed || stm->draining ||
|
|
|
|
!has_output(stm) || stm->has_dummy_output);
|
|
|
|
|
|
|
|
#ifndef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
|
|
|
if (has_output(stm) && !stm->has_dummy_output && volume != 1.0) {
|
|
|
|
// Adjust the output volume.
|
|
|
|
// Note: This could be integrated with the remixing below.
|
|
|
|
long out_samples = out_frames * stm->output_stream_params.channels;
|
|
|
|
if (volume == 0.0) {
|
|
|
|
memset(dest, 0, out_samples * stm->bytes_per_sample);
|
|
|
|
} else {
|
|
|
|
switch (stm->output_stream_params.format) {
|
|
|
|
case CUBEB_SAMPLE_FLOAT32NE: {
|
|
|
|
float * buf = static_cast<float *>(dest);
|
|
|
|
for (long i = 0; i < out_samples; ++i) {
|
|
|
|
buf[i] *= volume;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CUBEB_SAMPLE_S16NE: {
|
|
|
|
short * buf = static_cast<short *>(dest);
|
|
|
|
for (long i = 0; i < out_samples; ++i) {
|
|
|
|
buf[i] = static_cast<short>(static_cast<float>(buf[i]) * volume);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
XASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
// We don't bother mixing dummy output as it will be silenced, otherwise mix
|
|
|
|
// output if needed
|
2020-12-28 16:15:37 +01:00
|
|
|
if (!stm->has_dummy_output && has_output(stm) && stm->output_mixer) {
|
|
|
|
XASSERT(dest == stm->mix_buffer.data());
|
|
|
|
size_t dest_size =
|
2021-12-08 07:33:31 +01:00
|
|
|
out_frames * stm->output_stream_params.channels * stm->bytes_per_sample;
|
2020-12-28 16:15:37 +01:00
|
|
|
XASSERT(dest_size <= stm->mix_buffer.size());
|
|
|
|
size_t output_buffer_size =
|
2021-12-08 07:33:31 +01:00
|
|
|
out_frames * stm->output_mix_params.channels * stm->bytes_per_sample;
|
|
|
|
int ret = cubeb_mixer_mix(stm->output_mixer.get(), out_frames, dest,
|
|
|
|
dest_size, output_buffer, output_buffer_size);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
LOG("Error remixing content (%d)", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_frames;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
trigger_async_reconfigure(cubeb_stream * stm)
|
|
|
|
{
|
|
|
|
XASSERT(stm && stm->reconfigure_event);
|
|
|
|
BOOL ok = SetEvent(stm->reconfigure_event);
|
|
|
|
if (!ok) {
|
|
|
|
LOG("SetEvent on reconfigure_event failed: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
/* This helper grabs all the frames available from a capture client, put them in
|
2021-12-08 07:33:31 +01:00
|
|
|
* the linear_input_buffer. This helper does not work with exclusive mode
|
|
|
|
* streams. */
|
|
|
|
bool
|
|
|
|
get_input_buffer(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(has_input(stm));
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
BYTE * input_packet = NULL;
|
|
|
|
DWORD flags;
|
|
|
|
UINT64 dev_pos;
|
|
|
|
UINT64 pc_position;
|
|
|
|
UINT32 next;
|
|
|
|
/* Get input packets until we have captured enough frames, and put them in a
|
|
|
|
* contiguous buffer. */
|
|
|
|
uint32_t offset = 0;
|
|
|
|
// If the input stream is event driven we should only ever expect to read a
|
|
|
|
// single packet each time. However, if we're pulling from the stream we may
|
|
|
|
// need to grab multiple packets worth of frames that have accumulated (so
|
|
|
|
// need a loop).
|
2021-12-08 07:33:31 +01:00
|
|
|
for (hr = stm->capture_client->GetNextPacketSize(&next); next > 0;
|
2020-12-28 16:15:37 +01:00
|
|
|
hr = stm->capture_client->GetNextPacketSize(&next)) {
|
|
|
|
if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
|
|
|
|
// Application can recover from this error. More info
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd316605(v=vs.85).aspx
|
|
|
|
LOG("Device invalidated error, reset default device");
|
2021-12-08 07:33:31 +01:00
|
|
|
trigger_async_reconfigure(stm);
|
2020-12-28 16:15:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("cannot get next packet size: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 frames;
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = stm->capture_client->GetBuffer(&input_packet, &frames, &flags,
|
|
|
|
&dev_pos, &pc_position);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("GetBuffer failed for capture: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
XASSERT(frames == next);
|
|
|
|
|
|
|
|
if (stm->context->performance_counter_frequency) {
|
|
|
|
LARGE_INTEGER now;
|
|
|
|
UINT64 now_hns;
|
2021-12-08 07:33:31 +01:00
|
|
|
// See
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudiocaptureclient-getbuffer,
|
|
|
|
// section "Remarks".
|
2020-12-28 16:15:37 +01:00
|
|
|
QueryPerformanceCounter(&now);
|
2021-12-08 07:33:31 +01:00
|
|
|
now_hns =
|
|
|
|
10000000 * now.QuadPart / stm->context->performance_counter_frequency;
|
2020-12-28 16:15:37 +01:00
|
|
|
if (now_hns >= pc_position) {
|
|
|
|
stm->input_latency_hns = now_hns - pc_position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->total_input_frames += frames;
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
UINT32 input_stream_samples = frames * stm->input_stream_params.channels;
|
|
|
|
// We do not explicitly handle the AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY
|
|
|
|
// flag. There a two primary (non exhaustive) scenarios we anticipate this
|
|
|
|
// flag being set in:
|
|
|
|
// - The first GetBuffer after Start has this flag undefined. In this
|
|
|
|
// case the flag may be set but is meaningless and can be ignored.
|
|
|
|
// - If a glitch is introduced into the input. This should not happen
|
|
|
|
// for event based inputs, and should be mitigated by using a dummy
|
|
|
|
// stream to drive input in the case of input only loopback. Without
|
|
|
|
// a dummy output, input only loopback would glitch on silence. However,
|
|
|
|
// the dummy input should push silence to the loopback and prevent
|
2021-12-08 07:33:31 +01:00
|
|
|
// discontinuities. See
|
|
|
|
// https://blogs.msdn.microsoft.com/matthew_van_eerde/2008/12/16/sample-wasapi-loopback-capture-record-what-you-hear/
|
2020-12-28 16:15:37 +01:00
|
|
|
// As the first scenario can be ignored, and we anticipate the second
|
|
|
|
// scenario is mitigated, we ignore the flag.
|
2021-12-08 07:33:31 +01:00
|
|
|
// For more info:
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd370859(v=vs.85).aspx,
|
2020-12-28 16:15:37 +01:00
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd371458(v=vs.85).aspx
|
|
|
|
if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
|
|
|
|
LOG("insert silence: ps=%u", frames);
|
|
|
|
stm->linear_input_buffer->push_silence(input_stream_samples);
|
|
|
|
} else {
|
|
|
|
if (stm->input_mixer) {
|
|
|
|
bool ok = stm->linear_input_buffer->reserve(
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->linear_input_buffer->length() + input_stream_samples);
|
2020-12-28 16:15:37 +01:00
|
|
|
XASSERT(ok);
|
|
|
|
size_t input_packet_size =
|
2021-12-08 07:33:31 +01:00
|
|
|
frames * stm->input_mix_params.channels *
|
|
|
|
cubeb_sample_size(stm->input_mix_params.format);
|
2020-12-28 16:15:37 +01:00
|
|
|
size_t linear_input_buffer_size =
|
2021-12-08 07:33:31 +01:00
|
|
|
input_stream_samples *
|
|
|
|
cubeb_sample_size(stm->input_stream_params.format);
|
|
|
|
cubeb_mixer_mix(stm->input_mixer.get(), frames, input_packet,
|
|
|
|
input_packet_size, stm->linear_input_buffer->end(),
|
2020-12-28 16:15:37 +01:00
|
|
|
linear_input_buffer_size);
|
|
|
|
stm->linear_input_buffer->set_length(
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->linear_input_buffer->length() + input_stream_samples);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->linear_input_buffer->push(input_packet, input_stream_samples);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hr = stm->capture_client->ReleaseBuffer(frames);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("FAILED to release intput buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
offset += input_stream_samples;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
ALOGV("get_input_buffer: got %d frames", offset);
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
XASSERT(stm->linear_input_buffer->length() >= offset);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an output buffer from the render_client. It has to be released before
|
|
|
|
* exiting the callback. */
|
2021-12-08 07:33:31 +01:00
|
|
|
bool
|
|
|
|
get_output_buffer(cubeb_stream * stm, void *& buffer, size_t & frame_count)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
UINT32 padding_out;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
XASSERT(has_output(stm));
|
|
|
|
|
|
|
|
hr = stm->output_client->GetCurrentPadding(&padding_out);
|
|
|
|
if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
|
2021-12-08 07:33:31 +01:00
|
|
|
// Application can recover from this error. More info
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd316605(v=vs.85).aspx
|
|
|
|
LOG("Device invalidated error, reset default device");
|
|
|
|
trigger_async_reconfigure(stm);
|
|
|
|
return true;
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Failed to get padding: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
XASSERT(padding_out <= stm->output_buffer_frame_count);
|
|
|
|
|
|
|
|
if (stm->draining) {
|
|
|
|
if (padding_out == 0) {
|
|
|
|
LOG("Draining finished.");
|
|
|
|
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_DRAINED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG("Draining.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_count = stm->output_buffer_frame_count - padding_out;
|
|
|
|
BYTE * output_buffer;
|
|
|
|
|
|
|
|
hr = stm->render_client->GetBuffer(frame_count, &output_buffer);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("cannot get render buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = output_buffer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function gets input data from a input device, and pass it along with an
|
|
|
|
* output buffer to the resamplers. */
|
|
|
|
bool
|
|
|
|
refill_callback_duplex(cubeb_stream * stm)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
void * output_buffer = nullptr;
|
|
|
|
size_t output_frames = 0;
|
|
|
|
size_t input_frames;
|
|
|
|
bool rv;
|
|
|
|
|
|
|
|
XASSERT(has_input(stm) && has_output(stm));
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
if (stm->input_stream_params.prefs & CUBEB_STREAM_PREF_LOOPBACK) {
|
|
|
|
HRESULT rv = get_input_buffer(stm);
|
|
|
|
if (FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
input_frames =
|
|
|
|
stm->linear_input_buffer->length() / stm->input_stream_params.channels;
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
rv = get_output_buffer(stm, output_buffer, output_frames);
|
|
|
|
if (!rv) {
|
|
|
|
hr = stm->render_client->ReleaseBuffer(output_frames, 0);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This can only happen when debugging, and having breakpoints set in the
|
|
|
|
* callback in a way that it makes the stream underrun. */
|
|
|
|
if (output_frames == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for draining is not important on duplex. */
|
|
|
|
if (stm->draining) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->total_output_frames += output_frames;
|
|
|
|
|
|
|
|
ALOGV("in: %zu, out: %zu, missing: %ld, ratio: %f", stm->total_input_frames,
|
|
|
|
stm->total_output_frames,
|
|
|
|
static_cast<long>(stm->total_output_frames) - stm->total_input_frames,
|
|
|
|
static_cast<float>(stm->total_output_frames) / stm->total_input_frames);
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
if (stm->has_dummy_output) {
|
2021-12-08 07:33:31 +01:00
|
|
|
ALOGV(
|
|
|
|
"Duplex callback (dummy output): input frames: %Iu, output frames: %Iu",
|
|
|
|
input_frames, output_frames);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
// We don't want to expose the dummy output to the callback so don't pass
|
|
|
|
// the output buffer (it will be released later with silence in it)
|
2021-12-08 07:33:31 +01:00
|
|
|
refill(stm, stm->linear_input_buffer->data(), input_frames, nullptr, 0);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
|
|
|
ALOGV("Duplex callback: input frames: %Iu, output frames: %Iu",
|
|
|
|
input_frames, output_frames);
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
refill(stm, stm->linear_input_buffer->data(), input_frames, output_buffer,
|
2020-12-28 16:15:37 +01:00
|
|
|
output_frames);
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->linear_input_buffer->clear();
|
|
|
|
|
|
|
|
if (stm->has_dummy_output) {
|
|
|
|
// If output is a dummy output, make sure it's silent
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = stm->render_client->ReleaseBuffer(output_frames,
|
|
|
|
AUDCLNT_BUFFERFLAGS_SILENT);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
|
|
|
hr = stm->render_client->ReleaseBuffer(output_frames, 0);
|
|
|
|
}
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("failed to release buffer: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
refill_callback_input(cubeb_stream * stm)
|
|
|
|
{
|
|
|
|
bool rv;
|
|
|
|
size_t input_frames;
|
|
|
|
|
|
|
|
XASSERT(has_input(stm) && !has_output(stm));
|
|
|
|
|
|
|
|
rv = get_input_buffer(stm);
|
|
|
|
if (!rv) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
input_frames =
|
|
|
|
stm->linear_input_buffer->length() / stm->input_stream_params.channels;
|
2020-12-28 16:15:37 +01:00
|
|
|
if (!input_frames) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALOGV("Input callback: input frames: %Iu", input_frames);
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
long read =
|
|
|
|
refill(stm, stm->linear_input_buffer->data(), input_frames, nullptr, 0);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
XASSERT(read >= 0);
|
|
|
|
|
|
|
|
stm->linear_input_buffer->clear();
|
|
|
|
|
|
|
|
return !stm->draining;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
refill_callback_output(cubeb_stream * stm)
|
|
|
|
{
|
|
|
|
bool rv;
|
|
|
|
HRESULT hr;
|
|
|
|
void * output_buffer = nullptr;
|
|
|
|
size_t output_frames = 0;
|
|
|
|
|
|
|
|
XASSERT(!has_input(stm) && has_output(stm));
|
|
|
|
|
|
|
|
rv = get_output_buffer(stm, output_buffer, output_frames);
|
|
|
|
if (!rv) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stm->draining || output_frames == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
long got = refill(stm, nullptr, 0, output_buffer, output_frames);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
ALOGV("Output callback: output frames requested: %Iu, got %ld", output_frames,
|
|
|
|
got);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
XASSERT(got >= 0);
|
|
|
|
XASSERT(size_t(got) == output_frames || stm->draining);
|
|
|
|
|
|
|
|
hr = stm->render_client->ReleaseBuffer(got, 0);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("failed to release buffer: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size_t(got) == output_frames || stm->draining;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
static unsigned int __stdcall wasapi_stream_render_loop(LPVOID stream)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
cubeb_stream * stm = static_cast<cubeb_stream *>(stream);
|
|
|
|
std::atomic<bool> * emergency_bailout = stm->emergency_bailout;
|
|
|
|
|
|
|
|
// Signal wasapi_stream_start that we've copied emergency_bailout.
|
|
|
|
BOOL ok = SetEvent(stm->thread_ready_event);
|
|
|
|
if (!ok) {
|
|
|
|
LOG("thread_ready SetEvent failed: %lx", GetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_playing = true;
|
2021-12-08 07:33:31 +01:00
|
|
|
HANDLE wait_array[4] = {stm->shutdown_event, stm->reconfigure_event,
|
|
|
|
stm->refill_event, stm->input_available_event};
|
2020-12-28 16:15:37 +01:00
|
|
|
HANDLE mmcss_handle = NULL;
|
|
|
|
HRESULT hr = 0;
|
|
|
|
DWORD mmcss_task_index = 0;
|
|
|
|
struct auto_com {
|
2021-12-08 07:33:31 +01:00
|
|
|
auto_com()
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
|
|
XASSERT(SUCCEEDED(hr));
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
~auto_com() { CoUninitialize(); }
|
2020-12-28 16:15:37 +01:00
|
|
|
} com;
|
|
|
|
|
|
|
|
/* We could consider using "Pro Audio" here for WebAudio and
|
|
|
|
maybe WebRTC. */
|
|
|
|
mmcss_handle = AvSetMmThreadCharacteristicsA("Audio", &mmcss_task_index);
|
|
|
|
if (!mmcss_handle) {
|
|
|
|
/* This is not fatal, but we might glitch under heavy load. */
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Unable to use mmcss to bump the render thread priority: %lx",
|
|
|
|
GetLastError());
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* WaitForMultipleObjects timeout can trigger in cases where we don't want to
|
|
|
|
treat it as a timeout, such as across a system sleep/wake cycle. Trigger
|
|
|
|
the timeout error handling only when the timeout_limit is reached, which is
|
|
|
|
reset on each successful loop. */
|
|
|
|
unsigned timeout_count = 0;
|
|
|
|
const unsigned timeout_limit = 3;
|
|
|
|
while (is_playing) {
|
|
|
|
// We want to check the emergency bailout variable before a
|
2021-12-08 07:33:31 +01:00
|
|
|
// and after the WaitForMultipleObject, because the handles
|
|
|
|
// WaitForMultipleObjects is going to wait on might have been closed
|
|
|
|
// already.
|
2020-12-28 16:15:37 +01:00
|
|
|
if (*emergency_bailout) {
|
|
|
|
delete emergency_bailout;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DWORD waitResult = WaitForMultipleObjects(ARRAY_LENGTH(wait_array),
|
2021-12-08 07:33:31 +01:00
|
|
|
wait_array, FALSE, 1000);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (*emergency_bailout) {
|
|
|
|
delete emergency_bailout;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (waitResult != WAIT_TIMEOUT) {
|
|
|
|
timeout_count = 0;
|
|
|
|
}
|
|
|
|
switch (waitResult) {
|
|
|
|
case WAIT_OBJECT_0: { /* shutdown */
|
|
|
|
is_playing = false;
|
|
|
|
/* We don't check if the drain is actually finished here, we just want to
|
|
|
|
shutdown. */
|
|
|
|
if (stm->draining) {
|
|
|
|
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_DRAINED);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case WAIT_OBJECT_0 + 1: { /* reconfigure */
|
|
|
|
XASSERT(stm->output_client || stm->input_client);
|
|
|
|
LOG("Reconfiguring the stream");
|
|
|
|
/* Close the stream */
|
|
|
|
if (stm->output_client) {
|
|
|
|
stm->output_client->Stop();
|
|
|
|
LOG("Output stopped.");
|
|
|
|
}
|
|
|
|
if (stm->input_client) {
|
|
|
|
stm->input_client->Stop();
|
|
|
|
LOG("Input stopped.");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
close_wasapi_stream(stm);
|
|
|
|
LOG("Stream closed.");
|
2021-12-08 07:33:31 +01:00
|
|
|
/* Reopen a stream and start it immediately. This will automatically
|
|
|
|
pick the new default device for this role. */
|
2020-12-28 16:15:37 +01:00
|
|
|
int r = setup_wasapi_stream(stm);
|
|
|
|
if (r != CUBEB_OK) {
|
|
|
|
LOG("Error setting up the stream during reconfigure.");
|
|
|
|
/* Don't destroy the stream here, since we expect the caller to do
|
|
|
|
so after the error has propagated via the state callback. */
|
|
|
|
is_playing = false;
|
|
|
|
hr = E_FAIL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LOG("Stream setup successfuly.");
|
|
|
|
}
|
|
|
|
XASSERT(stm->output_client || stm->input_client);
|
|
|
|
if (stm->output_client) {
|
|
|
|
hr = stm->output_client->Start();
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Error starting output after reconfigure, error: %lx", hr);
|
|
|
|
is_playing = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LOG("Output started after reconfigure.");
|
|
|
|
}
|
|
|
|
if (stm->input_client) {
|
|
|
|
hr = stm->input_client->Start();
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Error starting input after reconfiguring, error: %lx", hr);
|
|
|
|
is_playing = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LOG("Input started after reconfigure.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
case WAIT_OBJECT_0 + 2: /* refill */
|
2020-12-28 16:15:37 +01:00
|
|
|
XASSERT((has_input(stm) && has_output(stm)) ||
|
|
|
|
(!has_input(stm) && has_output(stm)));
|
|
|
|
is_playing = stm->refill_callback(stm);
|
|
|
|
break;
|
2021-12-08 07:33:31 +01:00
|
|
|
case WAIT_OBJECT_0 + 3: { /* input available */
|
|
|
|
HRESULT rv = get_input_buffer(stm);
|
|
|
|
if (FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_output(stm)) {
|
|
|
|
is_playing = stm->refill_callback(stm);
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
break;
|
2021-12-08 07:33:31 +01:00
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
XASSERT(stm->shutdown_event == wait_array[0]);
|
|
|
|
if (++timeout_count >= timeout_limit) {
|
|
|
|
LOG("Render loop reached the timeout limit.");
|
|
|
|
is_playing = false;
|
|
|
|
hr = E_FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG("case %lu not handled in render loop.", waitResult);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mmcss_handle) {
|
|
|
|
AvRevertMmThreadCharacteristics(mmcss_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void
|
|
|
|
wasapi_destroy(cubeb * context);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT
|
|
|
|
register_notification_client(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
XASSERT(stm->device_enumerator);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->notification_client.reset(new wasapi_endpoint_notification_client(
|
|
|
|
stm->reconfigure_event, stm->role));
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr = stm->device_enumerator->RegisterEndpointNotificationCallback(
|
|
|
|
stm->notification_client.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not register endpoint notification callback: %lx", hr);
|
|
|
|
stm->notification_client = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT
|
|
|
|
unregister_notification_client(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
XASSERT(stm->device_enumerator);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr = stm->device_enumerator->UnregisterEndpointNotificationCallback(
|
|
|
|
stm->notification_client.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
// We can't really do anything here, we'll probably leak the
|
2021-12-08 07:33:31 +01:00
|
|
|
// notification client.
|
2020-12-28 16:15:37 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->notification_client = nullptr;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT
|
|
|
|
get_endpoint(com_ptr<IMMDevice> & device, LPCWSTR devid)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
com_ptr<IMMDeviceEnumerator> enumerator;
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr =
|
|
|
|
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_PPV_ARGS(enumerator.receive()));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device enumerator: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = enumerator->GetDevice(devid, device.receive());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT
|
|
|
|
register_collection_notification_client(cubeb * context)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr = CoCreateInstance(
|
|
|
|
__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_PPV_ARGS(context->device_collection_enumerator.receive()));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device enumerator: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
context->collection_notification_client.reset(
|
|
|
|
new wasapi_collection_notification_client(context));
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = context->device_collection_enumerator
|
|
|
|
->RegisterEndpointNotificationCallback(
|
|
|
|
context->collection_notification_client.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not register endpoint notification callback: %lx", hr);
|
|
|
|
context->collection_notification_client.reset();
|
|
|
|
context->device_collection_enumerator.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT
|
|
|
|
unregister_collection_notification_client(cubeb * context)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr = context->device_collection_enumerator
|
|
|
|
->UnregisterEndpointNotificationCallback(
|
|
|
|
context->collection_notification_client.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->collection_notification_client = nullptr;
|
|
|
|
context->device_collection_enumerator = nullptr;
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT
|
|
|
|
get_default_endpoint(com_ptr<IMMDevice> & device, EDataFlow direction,
|
|
|
|
ERole role)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
com_ptr<IMMDeviceEnumerator> enumerator;
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr =
|
|
|
|
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_PPV_ARGS(enumerator.receive()));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device enumerator: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
hr = enumerator->GetDefaultAudioEndpoint(direction, role, device.receive());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get default audio endpoint: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
current_stream_delay(cubeb_stream * stm)
|
|
|
|
{
|
|
|
|
stm->stream_reset_lock.assert_current_thread_owns();
|
|
|
|
|
|
|
|
/* If the default audio endpoint went away during playback and we weren't
|
|
|
|
able to configure a new one, it's possible the caller may call this
|
|
|
|
before the error callback has propogated back. */
|
|
|
|
if (!stm->audio_clock) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT64 freq;
|
|
|
|
HRESULT hr = stm->audio_clock->GetFrequency(&freq);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("GetFrequency failed: %lx", hr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT64 pos;
|
|
|
|
hr = stm->audio_clock->GetPosition(&pos, NULL);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("GetPosition failed: %lx", hr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double cur_pos = static_cast<double>(pos) / freq;
|
2021-12-08 07:33:31 +01:00
|
|
|
double max_pos =
|
|
|
|
static_cast<double>(stm->frames_written) / stm->output_mix_params.rate;
|
2020-12-28 16:15:37 +01:00
|
|
|
double delay = std::max(max_pos - cur_pos, 0.0);
|
|
|
|
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
2020-12-28 16:15:37 +01:00
|
|
|
int
|
|
|
|
stream_set_volume(cubeb_stream * stm, float volume)
|
|
|
|
{
|
|
|
|
stm->stream_reset_lock.assert_current_thread_owns();
|
|
|
|
|
|
|
|
if (!stm->audio_stream_volume) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t channels;
|
|
|
|
HRESULT hr = stm->audio_stream_volume->GetChannelCount(&channels);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("could not get the channel count: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* up to 9.1 for now */
|
|
|
|
if (channels > 10) {
|
|
|
|
return CUBEB_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
float volumes[10];
|
|
|
|
for (uint32_t i = 0; i < channels; i++) {
|
|
|
|
volumes[i] = volume;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = stm->audio_stream_volume->SetAllVolumes(channels, volumes);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("could not set the channels volume: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
#endif
|
|
|
|
} // namespace
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_init(cubeb ** context, char const * context_name)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
/* We don't use the device yet, but need to make sure we can initialize one
|
|
|
|
so that this backend is not incorrectly enabled on platforms that don't
|
|
|
|
support WASAPI. */
|
|
|
|
com_ptr<IMMDevice> device;
|
|
|
|
HRESULT hr = get_default_endpoint(device, eRender, eConsole);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
XASSERT(hr != CO_E_NOTINITIALIZED);
|
|
|
|
LOG("It wasn't able to find a default rendering device: %lx", hr);
|
|
|
|
hr = get_default_endpoint(device, eCapture, eConsole);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("It wasn't able to find a default capture device: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb * ctx = new cubeb();
|
|
|
|
|
|
|
|
ctx->ops = &wasapi_ops;
|
|
|
|
if (cubeb_strings_init(&ctx->device_ids) != CUBEB_OK) {
|
|
|
|
delete ctx;
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER frequency;
|
|
|
|
if (QueryPerformanceFrequency(&frequency)) {
|
|
|
|
ctx->performance_counter_frequency = frequency.QuadPart;
|
|
|
|
} else {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Failed getting performance counter frequency, latency reporting will "
|
|
|
|
"be inacurate");
|
2020-12-28 16:15:37 +01:00
|
|
|
ctx->performance_counter_frequency = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*context = ctx;
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2021-12-08 07:33:31 +01:00
|
|
|
bool
|
|
|
|
stop_and_join_render_thread(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
bool rv = true;
|
|
|
|
LOG("Stop and join render thread.");
|
|
|
|
if (!stm->thread) {
|
|
|
|
LOG("No thread present.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already leaked the thread, just return,
|
|
|
|
// there is not much we can do.
|
|
|
|
if (!stm->emergency_bailout.load()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ok = SetEvent(stm->shutdown_event);
|
|
|
|
if (!ok) {
|
|
|
|
LOG("Destroy SetEvent failed: %lx", GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait five seconds for the rendering thread to return. It's supposed to
|
|
|
|
* check its event loop very often, five seconds is rather conservative. */
|
|
|
|
DWORD r = WaitForSingleObject(stm->thread, 5000);
|
|
|
|
if (r != WAIT_OBJECT_0) {
|
|
|
|
/* Something weird happened, leak the thread and continue the shutdown
|
|
|
|
* process. */
|
|
|
|
*(stm->emergency_bailout) = true;
|
|
|
|
// We give the ownership to the rendering thread.
|
|
|
|
stm->emergency_bailout = nullptr;
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Destroy WaitForSingleObject on thread failed: %lx, %lx", r,
|
|
|
|
GetLastError());
|
2020-12-28 16:15:37 +01:00
|
|
|
rv = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only attempts to close and null out the thread and event if the
|
|
|
|
// WaitForSingleObject above succeeded, so that calling this function again
|
|
|
|
// attemps to clean up the thread and event each time.
|
|
|
|
if (rv) {
|
|
|
|
LOG("Closing thread.");
|
|
|
|
CloseHandle(stm->thread);
|
|
|
|
stm->thread = NULL;
|
|
|
|
|
|
|
|
CloseHandle(stm->shutdown_event);
|
|
|
|
stm->shutdown_event = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void
|
|
|
|
wasapi_destroy(cubeb * context)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
if (context->device_ids) {
|
|
|
|
cubeb_strings_destroy(context->device_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete context;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
char const *
|
|
|
|
wasapi_get_backend_id(cubeb * context)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
return "wasapi";
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wasapi_get_max_channel_count(cubeb * ctx, uint32_t * max_channels)
|
|
|
|
{
|
|
|
|
XASSERT(ctx && max_channels);
|
|
|
|
|
|
|
|
com_ptr<IMMDevice> device;
|
|
|
|
HRESULT hr = get_default_endpoint(device, eRender, eConsole);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
com_ptr<IAudioClient> client;
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = device->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL,
|
|
|
|
client.receive_vpp());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
WAVEFORMATEX * tmp = nullptr;
|
|
|
|
hr = client->GetMixFormat(&tmp);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
com_heap_ptr<WAVEFORMATEX> mix_format(tmp);
|
|
|
|
|
|
|
|
*max_channels = mix_format->nChannels;
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-12-08 07:33:31 +01:00
|
|
|
wasapi_get_min_latency(cubeb * ctx, cubeb_stream_params params,
|
|
|
|
uint32_t * latency_frames)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
if (params.format != CUBEB_SAMPLE_FLOAT32NE &&
|
|
|
|
params.format != CUBEB_SAMPLE_S16NE) {
|
2020-12-28 16:15:37 +01:00
|
|
|
return CUBEB_ERROR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERole role = pref_to_role(params.prefs);
|
|
|
|
|
|
|
|
com_ptr<IMMDevice> device;
|
|
|
|
HRESULT hr = get_default_endpoint(device, eRender, role);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get default endpoint: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
com_ptr<IAudioClient> client;
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = device->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL,
|
|
|
|
client.receive_vpp());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not activate device for latency: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
REFERENCE_TIME minimum_period;
|
|
|
|
REFERENCE_TIME default_period;
|
|
|
|
hr = client->GetDevicePeriod(&default_period, &minimum_period);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device period: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("default device period: %I64d, minimum device period: %I64d",
|
|
|
|
default_period, minimum_period);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
/* If we're on Windows 10, we can use IAudioClient3 to get minimal latency.
|
|
|
|
Otherwise, according to the docs, the best latency we can achieve is by
|
|
|
|
synchronizing the stream and the engine.
|
2021-12-08 07:33:31 +01:00
|
|
|
http://msdn.microsoft.com/en-us/library/windows/desktop/dd370871%28v=vs.85%29.aspx
|
|
|
|
*/
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
// #ifdef _WIN32_WINNT_WIN10
|
|
|
|
#if 0
|
|
|
|
*latency_frames = hns_to_frames(params.rate, minimum_period);
|
|
|
|
#else
|
|
|
|
*latency_frames = hns_to_frames(params.rate, default_period);
|
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
LOG("Minimum latency in frames: %u", *latency_frames);
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wasapi_get_preferred_sample_rate(cubeb * ctx, uint32_t * rate)
|
|
|
|
{
|
|
|
|
com_ptr<IMMDevice> device;
|
|
|
|
HRESULT hr = get_default_endpoint(device, eRender, eConsole);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
com_ptr<IAudioClient> client;
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = device->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL,
|
|
|
|
client.receive_vpp());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
WAVEFORMATEX * tmp = nullptr;
|
|
|
|
hr = client->GetMixFormat(&tmp);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
com_heap_ptr<WAVEFORMATEX> mix_format(tmp);
|
|
|
|
|
|
|
|
*rate = mix_format->nSamplesPerSec;
|
|
|
|
|
|
|
|
LOG("Preferred sample rate for output: %u", *rate);
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void
|
|
|
|
wasapi_stream_destroy(cubeb_stream * stm);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
waveformatex_update_derived_properties(WAVEFORMATEX * format)
|
|
|
|
{
|
|
|
|
format->nBlockAlign = format->wBitsPerSample * format->nChannels / 8;
|
|
|
|
format->nAvgBytesPerSec = format->nSamplesPerSec * format->nBlockAlign;
|
|
|
|
if (format->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEXTENSIBLE * format_pcm =
|
|
|
|
reinterpret_cast<WAVEFORMATEXTENSIBLE *>(format);
|
2020-12-28 16:15:37 +01:00
|
|
|
format_pcm->Samples.wValidBitsPerSample = format->wBitsPerSample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Based on the mix format and the stream format, try to find a way to play
|
|
|
|
what the user requested. */
|
|
|
|
static void
|
2021-12-08 07:33:31 +01:00
|
|
|
handle_channel_layout(cubeb_stream * stm, EDataFlow direction,
|
|
|
|
com_heap_ptr<WAVEFORMATEX> & mix_format,
|
|
|
|
const cubeb_stream_params * stream_params)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
com_ptr<IAudioClient> & audio_client =
|
|
|
|
(direction == eRender) ? stm->output_client : stm->input_client;
|
2020-12-28 16:15:37 +01:00
|
|
|
XASSERT(audio_client);
|
|
|
|
/* The docs say that GetMixFormat is always of type WAVEFORMATEXTENSIBLE [1],
|
|
|
|
so the reinterpret_cast below should be safe. In practice, this is not
|
|
|
|
true, and we just want to bail out and let the rest of the code find a good
|
|
|
|
conversion path instead of trying to make WASAPI do it by itself.
|
2021-12-08 07:33:31 +01:00
|
|
|
[1]:
|
|
|
|
http://msdn.microsoft.com/en-us/library/windows/desktop/dd370811%28v=vs.85%29.aspx*/
|
2020-12-28 16:15:37 +01:00
|
|
|
if (mix_format->wFormatTag != WAVE_FORMAT_EXTENSIBLE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEXTENSIBLE * format_pcm =
|
|
|
|
reinterpret_cast<WAVEFORMATEXTENSIBLE *>(mix_format.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
/* Stash a copy of the original mix format in case we need to restore it
|
|
|
|
* later. */
|
2020-12-28 16:15:37 +01:00
|
|
|
WAVEFORMATEXTENSIBLE hw_mix_format = *format_pcm;
|
|
|
|
|
|
|
|
/* Get the channel mask by the channel layout.
|
|
|
|
If the layout is not supported, we will get a closest settings below. */
|
|
|
|
format_pcm->dwChannelMask = stream_params->layout;
|
|
|
|
mix_format->nChannels = stream_params->channels;
|
|
|
|
waveformatex_update_derived_properties(mix_format.get());
|
|
|
|
|
|
|
|
/* Check if wasapi will accept our channel layout request. */
|
|
|
|
WAVEFORMATEX * tmp = nullptr;
|
|
|
|
HRESULT hr = audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED,
|
2021-12-08 07:33:31 +01:00
|
|
|
mix_format.get(), &tmp);
|
2020-12-28 16:15:37 +01:00
|
|
|
com_heap_ptr<WAVEFORMATEX> closest(tmp);
|
|
|
|
if (hr == S_FALSE) {
|
|
|
|
/* Channel layout not supported, but WASAPI gives us a suggestion. Use it,
|
|
|
|
and handle the eventual upmix/downmix ourselves. Ignore the subformat of
|
|
|
|
the suggestion, since it seems to always be IEEE_FLOAT. */
|
|
|
|
LOG("Using WASAPI suggested format: channels: %d", closest->nChannels);
|
|
|
|
XASSERT(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE);
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEXTENSIBLE * closest_pcm =
|
|
|
|
reinterpret_cast<WAVEFORMATEXTENSIBLE *>(closest.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
format_pcm->dwChannelMask = closest_pcm->dwChannelMask;
|
|
|
|
mix_format->nChannels = closest->nChannels;
|
|
|
|
waveformatex_update_derived_properties(mix_format.get());
|
|
|
|
} else if (hr == AUDCLNT_E_UNSUPPORTED_FORMAT) {
|
|
|
|
/* Not supported, no suggestion. This should not happen, but it does in the
|
|
|
|
field with some sound cards. We restore the mix format, and let the rest
|
|
|
|
of the code figure out the right conversion path. */
|
|
|
|
XASSERT(mix_format->wFormatTag == WAVE_FORMAT_EXTENSIBLE);
|
|
|
|
*reinterpret_cast<WAVEFORMATEXTENSIBLE *>(mix_format.get()) = hw_mix_format;
|
|
|
|
} else if (hr == S_OK) {
|
|
|
|
LOG("Requested format accepted by WASAPI.");
|
|
|
|
} else {
|
|
|
|
LOG("IsFormatSupported unhandled error: %lx", hr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
initialize_iaudioclient2(com_ptr<IAudioClient> & audio_client)
|
|
|
|
{
|
|
|
|
com_ptr<IAudioClient2> audio_client2;
|
|
|
|
audio_client->QueryInterface<IAudioClient2>(audio_client2.receive());
|
|
|
|
if (!audio_client2) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Could not get IAudioClient2 interface, not setting "
|
|
|
|
"AUDCLNT_STREAMOPTIONS_RAW.");
|
2020-12-28 16:15:37 +01:00
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
AudioClientProperties properties = {0};
|
2020-12-28 16:15:37 +01:00
|
|
|
properties.cbSize = sizeof(AudioClientProperties);
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
properties.Options |= AUDCLNT_STREAMOPTIONS_RAW;
|
|
|
|
#endif
|
|
|
|
HRESULT hr = audio_client2->SetClientProperties(&properties);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("IAudioClient2::SetClientProperties error: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
// Not static to suppress a warning.
|
|
|
|
/* static */ bool
|
2020-12-28 16:15:37 +01:00
|
|
|
initialize_iaudioclient3(com_ptr<IAudioClient> & audio_client,
|
|
|
|
cubeb_stream * stm,
|
|
|
|
const com_heap_ptr<WAVEFORMATEX> & mix_format,
|
2021-12-08 07:33:31 +01:00
|
|
|
DWORD flags, EDataFlow direction)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
com_ptr<IAudioClient3> audio_client3;
|
|
|
|
audio_client->QueryInterface<IAudioClient3>(audio_client3.receive());
|
|
|
|
if (!audio_client3) {
|
|
|
|
LOG("Could not get IAudioClient3 interface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & AUDCLNT_STREAMFLAGS_LOOPBACK) {
|
|
|
|
// IAudioClient3 doesn't work with loopback streams, and will return error
|
|
|
|
// 88890021: AUDCLNT_E_INVALID_STREAM_FLAG
|
|
|
|
LOG("Audio stream is loopback, not using IAudioClient3");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some people have reported glitches with capture streams:
|
|
|
|
// http://blog.nirbheek.in/2018/03/low-latency-audio-on-windows-with.html
|
|
|
|
if (direction == eCapture) {
|
|
|
|
LOG("Audio stream is capture, not using IAudioClient3");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possibly initialize a shared-mode stream using IAudioClient3. Initializing
|
2021-12-08 07:33:31 +01:00
|
|
|
// a stream this way lets you request lower latencies, but also locks the
|
|
|
|
// global WASAPI engine at that latency.
|
|
|
|
// - If we request a shared-mode stream, streams created with IAudioClient
|
|
|
|
// will
|
2020-12-28 16:15:37 +01:00
|
|
|
// have their latency adjusted to match. When the shared-mode stream is
|
|
|
|
// closed, they'll go back to normal.
|
|
|
|
// - If there's already a shared-mode stream running, then we cannot request
|
|
|
|
// the engine change to a different latency - we have to match it.
|
|
|
|
// - It's antisocial to lock the WASAPI engine at its default latency. If we
|
|
|
|
// would do this, then stop and use IAudioClient instead.
|
|
|
|
|
|
|
|
HRESULT hr;
|
2021-12-08 07:33:31 +01:00
|
|
|
uint32_t default_period = 0, fundamental_period = 0, min_period = 0,
|
|
|
|
max_period = 0;
|
|
|
|
hr = audio_client3->GetSharedModeEnginePeriod(
|
|
|
|
mix_format.get(), &default_period, &fundamental_period, &min_period,
|
|
|
|
&max_period);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get shared mode engine period: error: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t requested_latency = stm->latency;
|
|
|
|
if (requested_latency >= default_period) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Requested latency %i greater than default latency %i, not using "
|
|
|
|
"IAudioClient3",
|
|
|
|
requested_latency, default_period);
|
2020-12-28 16:15:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Got shared mode engine period: default=%i fundamental=%i min=%i max=%i",
|
|
|
|
default_period, fundamental_period, min_period, max_period);
|
2020-12-28 16:15:37 +01:00
|
|
|
// Snap requested latency to a valid value
|
|
|
|
uint32_t old_requested_latency = requested_latency;
|
|
|
|
if (requested_latency < min_period) {
|
|
|
|
requested_latency = min_period;
|
|
|
|
}
|
|
|
|
requested_latency -= (requested_latency - min_period) % fundamental_period;
|
|
|
|
if (requested_latency != old_requested_latency) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Requested latency %i was adjusted to %i", old_requested_latency,
|
|
|
|
requested_latency);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = audio_client3->InitializeSharedAudioStream(flags, requested_latency,
|
|
|
|
mix_format.get(), NULL);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
return true;
|
2021-12-08 07:33:31 +01:00
|
|
|
} else if (hr == AUDCLNT_E_ENGINE_PERIODICITY_LOCKED) {
|
2020-12-28 16:15:37 +01:00
|
|
|
LOG("Got AUDCLNT_E_ENGINE_PERIODICITY_LOCKED, adjusting latency request");
|
|
|
|
} else {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Could not initialize shared stream with IAudioClient3: error: %lx",
|
|
|
|
hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t current_period = 0;
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEX * current_format = nullptr;
|
2020-12-28 16:15:37 +01:00
|
|
|
// We have to pass a valid WAVEFORMATEX** and not nullptr, otherwise
|
|
|
|
// GetCurrentSharedModeEnginePeriod will return E_POINTER
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = audio_client3->GetCurrentSharedModeEnginePeriod(¤t_format,
|
|
|
|
¤t_period);
|
2020-12-28 16:15:37 +01:00
|
|
|
CoTaskMemFree(current_format);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get current shared mode engine period: error: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_period >= default_period) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Current shared mode engine period %i too high, not using IAudioClient",
|
|
|
|
current_period);
|
2020-12-28 16:15:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = audio_client3->InitializeSharedAudioStream(flags, current_period,
|
|
|
|
mix_format.get(), NULL);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (SUCCEEDED(hr)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Current shared mode engine period is %i instead of requested %i",
|
|
|
|
current_period, requested_latency);
|
2020-12-28 16:15:37 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Could not initialize shared stream with IAudioClient3: error: %lx", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DIRECTION_NAME (direction == eCapture ? "capture" : "render")
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
template <typename T>
|
|
|
|
int
|
|
|
|
setup_wasapi_stream_one_side(cubeb_stream * stm,
|
|
|
|
cubeb_stream_params * stream_params,
|
|
|
|
wchar_t const * devid, EDataFlow direction,
|
|
|
|
REFIID riid, com_ptr<IAudioClient> & audio_client,
|
|
|
|
uint32_t * buffer_frame_count, HANDLE & event,
|
|
|
|
T & render_or_capture_client,
|
|
|
|
cubeb_stream_params * mix_params,
|
|
|
|
com_ptr<IMMDevice> & device)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
bool is_loopback = stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK;
|
|
|
|
if (is_loopback && direction != eCapture) {
|
|
|
|
LOG("Loopback pref can only be used with capture streams!\n");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->stream_reset_lock.assert_current_thread_owns();
|
|
|
|
bool try_again = false;
|
|
|
|
// This loops until we find a device that works, or we've exhausted all
|
|
|
|
// possibilities.
|
|
|
|
do {
|
|
|
|
if (devid) {
|
|
|
|
hr = get_endpoint(device, devid);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get %s endpoint, error: %lx\n", DIRECTION_NAME, hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If caller has requested loopback but not specified a device, look for
|
|
|
|
// the default render device. Otherwise look for the default device
|
|
|
|
// appropriate to the direction.
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = get_default_endpoint(device, is_loopback ? eRender : direction,
|
|
|
|
pref_to_role(stream_params->prefs));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
if (is_loopback) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Could not get default render endpoint for loopback, error: "
|
|
|
|
"%lx\n",
|
|
|
|
hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Could not get default %s endpoint, error: %lx\n", DIRECTION_NAME,
|
|
|
|
hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a client. We will get all other interfaces we need from
|
|
|
|
* this pointer. */
|
|
|
|
#if 0 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1590902
|
|
|
|
hr = device->Activate(__uuidof(IAudioClient3),
|
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
NULL, audio_client.receive_vpp());
|
|
|
|
if (hr == E_NOINTERFACE) {
|
|
|
|
#endif
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = device->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL,
|
|
|
|
audio_client.receive_vpp());
|
2020-12-28 16:15:37 +01:00
|
|
|
#if 0
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not activate the device to get an audio"
|
2021-12-08 07:33:31 +01:00
|
|
|
" client for %s: error: %lx\n",
|
|
|
|
DIRECTION_NAME, hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
// A particular device can't be activated because it has been
|
|
|
|
// unplugged, try fall back to the default audio device.
|
|
|
|
if (devid && hr == AUDCLNT_E_DEVICE_INVALIDATED) {
|
|
|
|
LOG("Trying again with the default %s audio device.", DIRECTION_NAME);
|
|
|
|
devid = nullptr;
|
|
|
|
device = nullptr;
|
|
|
|
try_again = true;
|
|
|
|
} else {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try_again = false;
|
|
|
|
}
|
|
|
|
} while (try_again);
|
|
|
|
|
|
|
|
/* We have to distinguish between the format the mixer uses,
|
|
|
|
* and the format the stream we want to play uses. */
|
|
|
|
WAVEFORMATEX * tmp = nullptr;
|
|
|
|
hr = audio_client->GetMixFormat(&tmp);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not fetch current mix format from the audio"
|
2021-12-08 07:33:31 +01:00
|
|
|
" client for %s: error: %lx",
|
|
|
|
DIRECTION_NAME, hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
com_heap_ptr<WAVEFORMATEX> mix_format(tmp);
|
|
|
|
|
|
|
|
mix_format->wBitsPerSample = stm->bytes_per_sample * 8;
|
2021-12-08 07:33:31 +01:00
|
|
|
if (mix_format->wFormatTag == WAVE_FORMAT_PCM ||
|
|
|
|
mix_format->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
|
|
|
|
switch (mix_format->wBitsPerSample) {
|
|
|
|
case 8:
|
|
|
|
case 16:
|
|
|
|
mix_format->wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
mix_format->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG("%u bits per sample is incompatible with PCM wave formats",
|
|
|
|
mix_format->wBitsPerSample);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
if (mix_format->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEXTENSIBLE * format_pcm =
|
|
|
|
reinterpret_cast<WAVEFORMATEXTENSIBLE *>(mix_format.get());
|
2020-12-28 16:15:37 +01:00
|
|
|
format_pcm->SubFormat = stm->waveformatextensible_sub_format;
|
|
|
|
}
|
|
|
|
waveformatex_update_derived_properties(mix_format.get());
|
|
|
|
|
|
|
|
/* Set channel layout only when there're more than two channels. Otherwise,
|
|
|
|
* use the default setting retrieved from the stream format of the audio
|
|
|
|
* engine's internal processing by GetMixFormat. */
|
|
|
|
if (mix_format->nChannels > 2) {
|
|
|
|
handle_channel_layout(stm, direction, mix_format, stream_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
mix_params->format = stream_params->format;
|
|
|
|
mix_params->rate = mix_format->nSamplesPerSec;
|
|
|
|
mix_params->channels = mix_format->nChannels;
|
|
|
|
mix_params->layout = mask_to_channel_layout(mix_format.get());
|
|
|
|
|
|
|
|
LOG("Setup requested=[f=%d r=%u c=%u l=%u] mix=[f=%d r=%u c=%u l=%u]",
|
|
|
|
stream_params->format, stream_params->rate, stream_params->channels,
|
2021-12-08 07:33:31 +01:00
|
|
|
stream_params->layout, mix_params->format, mix_params->rate,
|
|
|
|
mix_params->channels, mix_params->layout);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
DWORD flags = 0;
|
|
|
|
|
|
|
|
// Check if a loopback device should be requested. Note that event callbacks
|
|
|
|
// do not work with loopback devices, so only request these if not looping.
|
|
|
|
if (is_loopback) {
|
|
|
|
flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
|
|
|
|
} else {
|
|
|
|
flags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check the latency, it may be that the device doesn't support it.
|
|
|
|
REFERENCE_TIME minimum_period;
|
|
|
|
REFERENCE_TIME default_period;
|
|
|
|
hr = audio_client->GetDevicePeriod(&default_period, &minimum_period);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device period: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
REFERENCE_TIME latency_hns = frames_to_hns(stream_params->rate, stm->latency);
|
|
|
|
stm->input_bluetooth_handsfree = false;
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
cubeb_device_info device_info;
|
2021-12-08 07:33:31 +01:00
|
|
|
if (wasapi_create_device(stm->context, device_info,
|
|
|
|
stm->device_enumerator.get(),
|
|
|
|
device.get()) == CUBEB_OK) {
|
|
|
|
const char * HANDSFREE_TAG = "BTHHFENUM";
|
2020-12-28 16:15:37 +01:00
|
|
|
size_t len = sizeof(HANDSFREE_TAG);
|
2021-12-08 07:33:31 +01:00
|
|
|
if (direction == eCapture) {
|
|
|
|
uint32_t default_period_frames =
|
|
|
|
hns_to_frames(device_info.default_rate, default_period);
|
|
|
|
if (strlen(device_info.group_id) >= len &&
|
|
|
|
strncmp(device_info.group_id, HANDSFREE_TAG, len) == 0) {
|
|
|
|
stm->input_bluetooth_handsfree = true;
|
|
|
|
}
|
|
|
|
// This multiplicator has been found empirically.
|
|
|
|
uint32_t latency_frames = default_period_frames * 8;
|
|
|
|
LOG("Input: latency increased to %u frames from a default of %u",
|
|
|
|
latency_frames, default_period_frames);
|
|
|
|
latency_hns = frames_to_hns(device_info.default_rate, latency_frames);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
wasapi_destroy_device(&device_info);
|
|
|
|
} else {
|
|
|
|
LOG("Could not get cubeb_device_info.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_params->prefs & CUBEB_STREAM_PREF_RAW) {
|
|
|
|
if (initialize_iaudioclient2(audio_client) != CUBEB_OK) {
|
|
|
|
LOG("Can't initialize an IAudioClient2, error: %lx", GetLastError());
|
|
|
|
// This is not fatal.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1590902
|
|
|
|
if (initialize_iaudioclient3(audio_client, stm, mix_format, flags, direction)) {
|
|
|
|
LOG("Initialized with IAudioClient3");
|
|
|
|
} else {
|
|
|
|
#endif
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, flags, latency_hns, 0,
|
|
|
|
mix_format.get(), NULL);
|
2020-12-28 16:15:37 +01:00
|
|
|
#if 0
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Unable to initialize audio client for %s: %lx.", DIRECTION_NAME, hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = audio_client->GetBufferSize(buffer_frame_count);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get the buffer size from the client"
|
2021-12-08 07:33:31 +01:00
|
|
|
" for %s %lx.",
|
|
|
|
DIRECTION_NAME, hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Buffer size is: %d for %s\n", *buffer_frame_count, DIRECTION_NAME);
|
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
// Events are used if not looping back
|
|
|
|
if (!is_loopback) {
|
|
|
|
hr = audio_client->SetEventHandle(event);
|
|
|
|
if (FAILED(hr)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Could set the event handle for the %s client %lx.", DIRECTION_NAME,
|
|
|
|
hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = audio_client->GetService(riid, render_or_capture_client.receive_vpp());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get the %s client %lx.", DIRECTION_NAME, hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef DIRECTION_NAME
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void
|
|
|
|
wasapi_find_matching_output_device(cubeb_stream * stm)
|
|
|
|
{
|
2020-12-28 16:15:37 +01:00
|
|
|
HRESULT hr;
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_device_info * input_device = nullptr;
|
2020-12-28 16:15:37 +01:00
|
|
|
cubeb_device_collection collection;
|
|
|
|
|
|
|
|
// Only try to match to an output device if the input device is a bluetooth
|
|
|
|
// device that is using the handsfree protocol
|
|
|
|
if (!stm->input_bluetooth_handsfree) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t * tmp = nullptr;
|
|
|
|
hr = stm->input_device->GetId(&tmp);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Couldn't get input device id in wasapi_find_matching_output_device");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
com_heap_ptr<wchar_t> device_id(tmp);
|
|
|
|
cubeb_devid input_device_id = intern_device_id(stm->context, device_id.get());
|
|
|
|
if (!input_device_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int rv = wasapi_enumerate_devices(
|
|
|
|
stm->context,
|
|
|
|
(cubeb_device_type)(CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT),
|
|
|
|
&collection);
|
|
|
|
if (rv != CUBEB_OK) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
// Find the input device, and then find the output device with the same group
|
|
|
|
// id and the same rate.
|
|
|
|
for (uint32_t i = 0; i < collection.count; i++) {
|
2021-12-08 07:33:31 +01:00
|
|
|
if (collection.device[i].devid == input_device_id) {
|
|
|
|
input_device = &collection.device[i];
|
2020-12-28 16:15:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < collection.count; i++) {
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_device_info & dev = collection.device[i];
|
|
|
|
if (dev.type == CUBEB_DEVICE_TYPE_OUTPUT && dev.group_id && input_device &&
|
|
|
|
!strcmp(dev.group_id, input_device->group_id) &&
|
2020-12-28 16:15:37 +01:00
|
|
|
dev.default_rate == input_device->default_rate) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Found matching device for %s: %s", input_device->friendly_name,
|
|
|
|
dev.friendly_name);
|
|
|
|
stm->output_device_id =
|
|
|
|
utf8_to_wstr(reinterpret_cast<char const *>(dev.devid));
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wasapi_device_collection_destroy(stm->context, &collection);
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
setup_wasapi_stream(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
stm->stream_reset_lock.assert_current_thread_owns();
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
XASSERT((!stm->output_client || !stm->input_client) &&
|
|
|
|
"WASAPI stream already setup, close it first.");
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
if (has_input(stm)) {
|
|
|
|
LOG("(%p) Setup capture: device=%p", stm, stm->input_device_id.get());
|
2021-12-08 07:33:31 +01:00
|
|
|
rv = setup_wasapi_stream_one_side(
|
|
|
|
stm, &stm->input_stream_params, stm->input_device_id.get(), eCapture,
|
|
|
|
__uuidof(IAudioCaptureClient), stm->input_client,
|
|
|
|
&stm->input_buffer_frame_count, stm->input_available_event,
|
|
|
|
stm->capture_client, &stm->input_mix_params, stm->input_device);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (rv != CUBEB_OK) {
|
|
|
|
LOG("Failure to open the input side.");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
// We initializing an input stream, buffer ahead two buffers worth of
|
|
|
|
// silence. This delays the input side slightly, but allow to not glitch
|
|
|
|
// when no input is available when calling into the resampler to call the
|
|
|
|
// callback: the input refill event will be set shortly after to compensate
|
|
|
|
// for this lack of data. In debug, four buffers are used, to avoid tripping
|
|
|
|
// up assertions down the line.
|
2020-12-28 16:15:37 +01:00
|
|
|
#if !defined(DEBUG)
|
|
|
|
const int silent_buffer_count = 2;
|
|
|
|
#else
|
|
|
|
const int silent_buffer_count = 6;
|
|
|
|
#endif
|
|
|
|
stm->linear_input_buffer->push_silence(stm->input_buffer_frame_count *
|
|
|
|
stm->input_stream_params.channels *
|
|
|
|
silent_buffer_count);
|
|
|
|
|
|
|
|
// If this is a bluetooth device, and the output device is the default
|
|
|
|
// device, and the default device is the same bluetooth device, pick the
|
|
|
|
// right output device, running at the same rate and with the same protocol
|
|
|
|
// as the input.
|
|
|
|
if (!stm->output_device_id) {
|
|
|
|
wasapi_find_matching_output_device(stm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have an output device but are requesting a loopback device,
|
|
|
|
// we attempt to open that same device in output mode in order to drive the
|
|
|
|
// loopback via the output events.
|
|
|
|
stm->has_dummy_output = false;
|
2021-12-08 07:33:31 +01:00
|
|
|
if (!has_output(stm) &&
|
|
|
|
stm->input_stream_params.prefs & CUBEB_STREAM_PREF_LOOPBACK) {
|
2020-12-28 16:15:37 +01:00
|
|
|
stm->output_stream_params.rate = stm->input_stream_params.rate;
|
|
|
|
stm->output_stream_params.channels = stm->input_stream_params.channels;
|
|
|
|
stm->output_stream_params.layout = stm->input_stream_params.layout;
|
|
|
|
if (stm->input_device_id) {
|
|
|
|
size_t len = wcslen(stm->input_device_id.get());
|
|
|
|
std::unique_ptr<wchar_t[]> tmp(new wchar_t[len + 1]);
|
|
|
|
if (wcsncpy_s(tmp.get(), len + 1, stm->input_device_id.get(), len) != 0) {
|
|
|
|
LOG("Failed to copy device identifier while copying input stream"
|
|
|
|
" configuration to output stream configuration to drive loopback.");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
stm->output_device_id = move(tmp);
|
|
|
|
}
|
|
|
|
stm->has_dummy_output = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_output(stm)) {
|
|
|
|
LOG("(%p) Setup render: device=%p", stm, stm->output_device_id.get());
|
2021-12-08 07:33:31 +01:00
|
|
|
rv = setup_wasapi_stream_one_side(
|
|
|
|
stm, &stm->output_stream_params, stm->output_device_id.get(), eRender,
|
|
|
|
__uuidof(IAudioRenderClient), stm->output_client,
|
|
|
|
&stm->output_buffer_frame_count, stm->refill_event, stm->render_client,
|
|
|
|
&stm->output_mix_params, stm->output_device);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (rv != CUBEB_OK) {
|
|
|
|
LOG("Failure to open the output side.");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr = 0;
|
|
|
|
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
|
|
|
hr = stm->output_client->GetService(__uuidof(IAudioStreamVolume),
|
|
|
|
stm->audio_stream_volume.receive_vpp());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get the IAudioStreamVolume: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
XASSERT(stm->frames_written == 0);
|
|
|
|
hr = stm->output_client->GetService(__uuidof(IAudioClock),
|
|
|
|
stm->audio_clock.receive_vpp());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get the IAudioClock: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
2020-12-28 16:15:37 +01:00
|
|
|
/* Restore the stream volume over a device change. */
|
|
|
|
if (stream_set_volume(stm, stm->volume) != CUBEB_OK) {
|
|
|
|
LOG("Could not set the volume.");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have both input and output, we resample to
|
|
|
|
* the highest sample rate available. */
|
|
|
|
int32_t target_sample_rate;
|
|
|
|
if (has_input(stm) && has_output(stm)) {
|
|
|
|
XASSERT(stm->input_stream_params.rate == stm->output_stream_params.rate);
|
|
|
|
target_sample_rate = stm->input_stream_params.rate;
|
|
|
|
} else if (has_input(stm)) {
|
|
|
|
target_sample_rate = stm->input_stream_params.rate;
|
|
|
|
} else {
|
|
|
|
XASSERT(has_output(stm));
|
|
|
|
target_sample_rate = stm->output_stream_params.rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Target sample rate: %d", target_sample_rate);
|
|
|
|
|
|
|
|
/* If we are playing/capturing a mono stream, we only resample one channel,
|
|
|
|
and copy it over, so we are always resampling the number
|
|
|
|
of channels of the stream, not the number of channels
|
|
|
|
that WASAPI wants. */
|
|
|
|
cubeb_stream_params input_params = stm->input_mix_params;
|
|
|
|
input_params.channels = stm->input_stream_params.channels;
|
|
|
|
cubeb_stream_params output_params = stm->output_mix_params;
|
|
|
|
output_params.channels = stm->output_stream_params.channels;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->resampler.reset(cubeb_resampler_create(
|
|
|
|
stm, has_input(stm) ? &input_params : nullptr,
|
|
|
|
has_output(stm) ? &output_params : nullptr, target_sample_rate,
|
|
|
|
stm->data_callback, stm->user_ptr,
|
|
|
|
stm->voice ? CUBEB_RESAMPLER_QUALITY_VOIP
|
|
|
|
: CUBEB_RESAMPLER_QUALITY_DESKTOP));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (!stm->resampler) {
|
|
|
|
LOG("Could not get a resampler");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
XASSERT(has_input(stm) || has_output(stm));
|
|
|
|
|
|
|
|
if (has_input(stm) && has_output(stm)) {
|
|
|
|
stm->refill_callback = refill_callback_duplex;
|
|
|
|
} else if (has_input(stm)) {
|
|
|
|
stm->refill_callback = refill_callback_input;
|
|
|
|
} else if (has_output(stm)) {
|
|
|
|
stm->refill_callback = refill_callback_output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create input mixer.
|
|
|
|
if (has_input(stm) &&
|
|
|
|
((stm->input_mix_params.layout != CUBEB_LAYOUT_UNDEFINED &&
|
|
|
|
stm->input_mix_params.layout != stm->input_stream_params.layout) ||
|
|
|
|
(stm->input_mix_params.channels != stm->input_stream_params.channels))) {
|
|
|
|
if (stm->input_mix_params.layout == CUBEB_LAYOUT_UNDEFINED) {
|
|
|
|
LOG("Input stream using undefined layout! Any mixing may be "
|
|
|
|
"unpredictable!\n");
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->input_mixer.reset(cubeb_mixer_create(
|
|
|
|
stm->input_stream_params.format, stm->input_mix_params.channels,
|
|
|
|
stm->input_mix_params.layout, stm->input_stream_params.channels,
|
|
|
|
stm->input_stream_params.layout));
|
2020-12-28 16:15:37 +01:00
|
|
|
assert(stm->input_mixer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create output mixer.
|
2021-12-08 07:33:31 +01:00
|
|
|
if (has_output(stm) &&
|
|
|
|
stm->output_mix_params.layout != stm->output_stream_params.layout) {
|
2020-12-28 16:15:37 +01:00
|
|
|
if (stm->output_mix_params.layout == CUBEB_LAYOUT_UNDEFINED) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("Output stream using undefined layout! Any mixing may be "
|
|
|
|
"unpredictable!\n");
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->output_mixer.reset(cubeb_mixer_create(
|
|
|
|
stm->output_stream_params.format, stm->output_stream_params.channels,
|
|
|
|
stm->output_stream_params.layout, stm->output_mix_params.channels,
|
|
|
|
stm->output_mix_params.layout));
|
2020-12-28 16:15:37 +01:00
|
|
|
assert(stm->output_mixer);
|
|
|
|
// Input is up/down mixed when depacketized in get_input_buffer.
|
|
|
|
stm->mix_buffer.resize(
|
2021-12-08 07:33:31 +01:00
|
|
|
frames_to_bytes_before_mix(stm, stm->output_buffer_frame_count));
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERole
|
|
|
|
pref_to_role(cubeb_stream_prefs prefs)
|
|
|
|
{
|
|
|
|
if (prefs & CUBEB_STREAM_PREF_VOICE) {
|
|
|
|
return eCommunications;
|
|
|
|
}
|
|
|
|
|
|
|
|
return eConsole;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
wasapi_stream_init(cubeb * context, cubeb_stream ** stream,
|
2021-12-08 07:33:31 +01:00
|
|
|
char const * stream_name, cubeb_devid input_device,
|
2020-12-28 16:15:37 +01:00
|
|
|
cubeb_stream_params * input_stream_params,
|
|
|
|
cubeb_devid output_device,
|
|
|
|
cubeb_stream_params * output_stream_params,
|
2021-12-08 07:33:31 +01:00
|
|
|
unsigned int latency_frames,
|
|
|
|
cubeb_data_callback data_callback,
|
2020-12-28 16:15:37 +01:00
|
|
|
cubeb_state_callback state_callback, void * user_ptr)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
XASSERT(context && stream && (input_stream_params || output_stream_params));
|
|
|
|
|
|
|
|
if (output_stream_params && input_stream_params &&
|
|
|
|
output_stream_params->format != input_stream_params->format) {
|
|
|
|
return CUBEB_ERROR_INVALID_FORMAT;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
std::unique_ptr<cubeb_stream, decltype(&wasapi_stream_destroy)> stm(
|
|
|
|
new cubeb_stream(), wasapi_stream_destroy);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
stm->context = context;
|
|
|
|
stm->data_callback = data_callback;
|
|
|
|
stm->state_callback = state_callback;
|
|
|
|
stm->user_ptr = user_ptr;
|
|
|
|
stm->role = eConsole;
|
|
|
|
stm->input_bluetooth_handsfree = false;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr =
|
|
|
|
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_PPV_ARGS(stm->device_enumerator.receive()));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device enumerator: %lx", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_stream_params) {
|
|
|
|
stm->input_stream_params = *input_stream_params;
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->input_device_id =
|
|
|
|
utf8_to_wstr(reinterpret_cast<char const *>(input_device));
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
if (output_stream_params) {
|
|
|
|
stm->output_stream_params = *output_stream_params;
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->output_device_id =
|
|
|
|
utf8_to_wstr(reinterpret_cast<char const *>(output_device));
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stm->output_stream_params.prefs & CUBEB_STREAM_PREF_VOICE ||
|
|
|
|
stm->input_stream_params.prefs & CUBEB_STREAM_PREF_VOICE) {
|
|
|
|
stm->voice = true;
|
|
|
|
} else {
|
|
|
|
stm->voice = false;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
switch (output_stream_params ? output_stream_params->format
|
|
|
|
: input_stream_params->format) {
|
|
|
|
case CUBEB_SAMPLE_S16NE:
|
|
|
|
stm->bytes_per_sample = sizeof(short);
|
|
|
|
stm->waveformatextensible_sub_format = KSDATAFORMAT_SUBTYPE_PCM;
|
|
|
|
stm->linear_input_buffer.reset(new auto_array_wrapper_impl<short>);
|
|
|
|
break;
|
|
|
|
case CUBEB_SAMPLE_FLOAT32NE:
|
|
|
|
stm->bytes_per_sample = sizeof(float);
|
|
|
|
stm->waveformatextensible_sub_format = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
|
|
|
|
stm->linear_input_buffer.reset(new auto_array_wrapper_impl<float>);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CUBEB_ERROR_INVALID_FORMAT;
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stm->latency = latency_frames;
|
|
|
|
|
|
|
|
stm->reconfigure_event = CreateEvent(NULL, 0, 0, NULL);
|
|
|
|
if (!stm->reconfigure_event) {
|
|
|
|
LOG("Can't create the reconfigure event, error: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unconditionally create the two events so that the wait logic is simpler. */
|
|
|
|
stm->refill_event = CreateEvent(NULL, 0, 0, NULL);
|
|
|
|
if (!stm->refill_event) {
|
|
|
|
LOG("Can't create the refill event, error: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->input_available_event = CreateEvent(NULL, 0, 0, NULL);
|
|
|
|
if (!stm->input_available_event) {
|
|
|
|
LOG("Can't create the input available event , error: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Locking here is not strictly necessary, because we don't have a
|
|
|
|
notification client that can reset the stream yet, but it lets us
|
|
|
|
assert that the lock is held in the function. */
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
rv = setup_wasapi_stream(stm.get());
|
|
|
|
}
|
|
|
|
if (rv != CUBEB_OK) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
if (!((input_stream_params ? (input_stream_params->prefs &
|
|
|
|
CUBEB_STREAM_PREF_DISABLE_DEVICE_SWITCHING)
|
|
|
|
: 0) ||
|
|
|
|
(output_stream_params ? (output_stream_params->prefs &
|
|
|
|
CUBEB_STREAM_PREF_DISABLE_DEVICE_SWITCHING)
|
|
|
|
: 0))) {
|
2020-12-28 16:15:37 +01:00
|
|
|
HRESULT hr = register_notification_client(stm.get());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
/* this is not fatal, we can still play audio, but we won't be able
|
|
|
|
to keep using the default audio endpoint if it changes. */
|
|
|
|
LOG("failed to register notification client, %lx", hr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*stream = stm.release();
|
|
|
|
|
|
|
|
LOG("Stream init succesfull (%p)", *stream);
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void
|
|
|
|
close_wasapi_stream(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(stm);
|
|
|
|
|
|
|
|
stm->stream_reset_lock.assert_current_thread_owns();
|
|
|
|
|
|
|
|
stm->output_client = nullptr;
|
|
|
|
stm->render_client = nullptr;
|
|
|
|
|
|
|
|
stm->input_client = nullptr;
|
|
|
|
stm->capture_client = nullptr;
|
|
|
|
|
|
|
|
stm->output_device = nullptr;
|
|
|
|
stm->input_device = nullptr;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
2020-12-28 16:15:37 +01:00
|
|
|
stm->audio_stream_volume = nullptr;
|
2021-12-08 07:33:31 +01:00
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
stm->audio_clock = nullptr;
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->total_frames_written += static_cast<UINT64>(
|
|
|
|
round(stm->frames_written *
|
|
|
|
stream_to_mix_samplerate_ratio(stm->output_stream_params,
|
|
|
|
stm->output_mix_params)));
|
2020-12-28 16:15:37 +01:00
|
|
|
stm->frames_written = 0;
|
|
|
|
|
|
|
|
stm->resampler.reset();
|
|
|
|
stm->output_mixer.reset();
|
|
|
|
stm->input_mixer.reset();
|
|
|
|
stm->mix_buffer.clear();
|
2021-12-08 07:33:31 +01:00
|
|
|
if (stm->linear_input_buffer) {
|
|
|
|
stm->linear_input_buffer->clear();
|
|
|
|
}
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
void
|
|
|
|
wasapi_stream_destroy(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(stm);
|
|
|
|
LOG("Stream destroy (%p)", stm);
|
|
|
|
|
|
|
|
// Only free stm->emergency_bailout if we could join the thread.
|
|
|
|
// If we could not join the thread, stm->emergency_bailout is true
|
|
|
|
// and is still alive until the thread wakes up and exits cleanly.
|
|
|
|
if (stop_and_join_render_thread(stm)) {
|
|
|
|
delete stm->emergency_bailout.load();
|
|
|
|
stm->emergency_bailout = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stm->notification_client) {
|
|
|
|
unregister_notification_client(stm);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(stm->reconfigure_event);
|
|
|
|
CloseHandle(stm->refill_event);
|
|
|
|
CloseHandle(stm->input_available_event);
|
|
|
|
|
|
|
|
// The variables intialized in wasapi_stream_init,
|
|
|
|
// must be destroyed in wasapi_stream_destroy.
|
|
|
|
stm->linear_input_buffer.reset();
|
|
|
|
|
|
|
|
stm->device_enumerator = nullptr;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
close_wasapi_stream(stm);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete stm;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
enum StreamDirection { OUTPUT, INPUT };
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
stream_start_one_side(cubeb_stream * stm, StreamDirection dir)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT((dir == OUTPUT && stm->output_client) ||
|
|
|
|
(dir == INPUT && stm->input_client));
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr =
|
|
|
|
dir == OUTPUT ? stm->output_client->Start() : stm->input_client->Start();
|
2020-12-28 16:15:37 +01:00
|
|
|
if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
|
|
|
|
LOG("audioclient invalidated for %s device, reconfiguring",
|
|
|
|
dir == OUTPUT ? "output" : "input");
|
|
|
|
|
|
|
|
BOOL ok = ResetEvent(stm->reconfigure_event);
|
|
|
|
if (!ok) {
|
|
|
|
LOG("resetting reconfig event failed for %s stream: %lx",
|
|
|
|
dir == OUTPUT ? "output" : "input", GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
close_wasapi_stream(stm);
|
|
|
|
int r = setup_wasapi_stream(stm);
|
|
|
|
if (r != CUBEB_OK) {
|
|
|
|
LOG("reconfigure failed");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
HRESULT hr2 = dir == OUTPUT ? stm->output_client->Start()
|
|
|
|
: stm->input_client->Start();
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr2)) {
|
|
|
|
LOG("could not start the %s stream after reconfig: %lx",
|
|
|
|
dir == OUTPUT ? "output" : "input", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
} else if (FAILED(hr)) {
|
|
|
|
LOG("could not start the %s stream: %lx.",
|
|
|
|
dir == OUTPUT ? "output" : "input", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_start(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
|
|
|
|
XASSERT(stm && !stm->thread && !stm->shutdown_event);
|
|
|
|
XASSERT(stm->output_client || stm->input_client);
|
|
|
|
|
|
|
|
stm->emergency_bailout = new std::atomic<bool>(false);
|
|
|
|
|
|
|
|
if (stm->output_client) {
|
|
|
|
int rv = stream_start_one_side(stm, OUTPUT);
|
|
|
|
if (rv != CUBEB_OK) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stm->input_client) {
|
|
|
|
int rv = stream_start_one_side(stm, INPUT);
|
|
|
|
if (rv != CUBEB_OK) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->shutdown_event = CreateEvent(NULL, 0, 0, NULL);
|
|
|
|
if (!stm->shutdown_event) {
|
|
|
|
LOG("Can't create the shutdown event, error: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->thread_ready_event = CreateEvent(NULL, 0, 0, NULL);
|
|
|
|
if (!stm->thread_ready_event) {
|
|
|
|
LOG("Can't create the thread_ready event, error: %lx", GetLastError());
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_async_log_reset_threads();
|
2021-12-08 07:33:31 +01:00
|
|
|
stm->thread =
|
|
|
|
(HANDLE)_beginthreadex(NULL, 512 * 1024, wasapi_stream_render_loop, stm,
|
|
|
|
STACK_SIZE_PARAM_IS_A_RESERVATION, NULL);
|
2020-12-28 16:15:37 +01:00
|
|
|
if (stm->thread == NULL) {
|
|
|
|
LOG("could not create WASAPI render thread.");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for wasapi_stream_render_loop to signal that emergency_bailout has
|
|
|
|
// been read, avoiding a bailout situation where we could free `stm`
|
|
|
|
// before wasapi_stream_render_loop had a chance to run.
|
|
|
|
HRESULT hr = WaitForSingleObject(stm->thread_ready_event, INFINITE);
|
|
|
|
XASSERT(hr == WAIT_OBJECT_0);
|
|
|
|
CloseHandle(stm->thread_ready_event);
|
|
|
|
stm->thread_ready_event = 0;
|
|
|
|
|
|
|
|
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_STARTED);
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_stop(cubeb_stream * stm)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(stm);
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
|
|
|
|
if (stm->output_client) {
|
|
|
|
hr = stm->output_client->Stop();
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("could not stop AudioClient (output)");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stm->input_client) {
|
|
|
|
hr = stm->input_client->Stop();
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("could not stop AudioClient (input)");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_STOPPED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_and_join_render_thread(stm)) {
|
|
|
|
delete stm->emergency_bailout.load();
|
|
|
|
stm->emergency_bailout = nullptr;
|
|
|
|
} else {
|
|
|
|
// If we could not join the thread, put the stream in error.
|
|
|
|
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_get_position(cubeb_stream * stm, uint64_t * position)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(stm && position);
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
|
|
|
|
if (!has_output(stm)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate how far behind the current stream head the playback cursor is. */
|
2021-12-08 07:33:31 +01:00
|
|
|
uint64_t stream_delay = static_cast<uint64_t>(current_stream_delay(stm) *
|
|
|
|
stm->output_stream_params.rate);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
/* Calculate the logical stream head in frames at the stream sample rate. */
|
2021-12-08 07:33:31 +01:00
|
|
|
uint64_t max_pos =
|
|
|
|
stm->total_frames_written +
|
|
|
|
static_cast<uint64_t>(
|
|
|
|
round(stm->frames_written *
|
|
|
|
stream_to_mix_samplerate_ratio(stm->output_stream_params,
|
|
|
|
stm->output_mix_params)));
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
*position = max_pos;
|
|
|
|
if (stream_delay <= *position) {
|
|
|
|
*position -= stream_delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*position < stm->prev_position) {
|
|
|
|
*position = stm->prev_position;
|
|
|
|
}
|
|
|
|
stm->prev_position = *position;
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_get_latency(cubeb_stream * stm, uint32_t * latency)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(stm && latency);
|
|
|
|
|
|
|
|
if (!has_output(stm)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
|
|
|
|
/* The GetStreamLatency method only works if the
|
|
|
|
AudioClient has been initialized. */
|
|
|
|
if (!stm->output_client) {
|
|
|
|
LOG("get_latency: No output_client.");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
REFERENCE_TIME latency_hns;
|
|
|
|
HRESULT hr = stm->output_client->GetStreamLatency(&latency_hns);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("GetStreamLatency failed %lx.", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
// This happens on windows 10: no error, but always 0 for latency.
|
|
|
|
if (latency_hns == 0) {
|
|
|
|
LOG("GetStreamLatency returned 0, using workaround.");
|
2021-12-08 07:33:31 +01:00
|
|
|
double delay_s = current_stream_delay(stm);
|
|
|
|
// convert to sample-frames
|
|
|
|
*latency = delay_s * stm->output_stream_params.rate;
|
2020-12-28 16:15:37 +01:00
|
|
|
} else {
|
2021-12-08 07:33:31 +01:00
|
|
|
*latency = hns_to_frames(stm, latency_hns);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Output latency %u frames.", *latency);
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_get_input_latency(cubeb_stream * stm, uint32_t * latency)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(stm && latency);
|
|
|
|
|
|
|
|
if (!has_input(stm)) {
|
|
|
|
LOG("Input latency queried on an output-only stream.");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
|
|
|
|
if (stm->input_latency_hns == LATENCY_NOT_AVAILABLE_YET) {
|
|
|
|
LOG("Input latency not available yet.");
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*latency = hns_to_frames(stm, stm->input_latency_hns);
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
int
|
|
|
|
wasapi_stream_set_volume(cubeb_stream * stm, float volume)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
auto_lock lock(stm->stream_reset_lock);
|
|
|
|
|
|
|
|
if (!has_output(stm)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
|
2020-12-28 16:15:37 +01:00
|
|
|
if (stream_set_volume(stm, volume) != CUBEB_OK) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
#endif
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
stm->volume = volume;
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char const *
|
|
|
|
wstr_to_utf8(LPCWSTR str)
|
|
|
|
{
|
|
|
|
int size = ::WideCharToMultiByte(CP_UTF8, 0, str, -1, nullptr, 0, NULL, NULL);
|
|
|
|
if (size <= 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * ret = static_cast<char *>(malloc(size));
|
|
|
|
::WideCharToMultiByte(CP_UTF8, 0, str, -1, ret, size, NULL, NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::unique_ptr<wchar_t const []>
|
2021-12-08 07:33:31 +01:00
|
|
|
utf8_to_wstr(char const * str) {
|
2020-12-28 16:15:37 +01:00
|
|
|
int size = ::MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
|
|
|
|
if (size <= 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
std::unique_ptr<wchar_t[]> ret(new wchar_t[size]);
|
2020-12-28 16:15:37 +01:00
|
|
|
::MultiByteToWideChar(CP_UTF8, 0, str, -1, ret.get(), size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
static com_ptr<IMMDevice> wasapi_get_device_node(
|
|
|
|
IMMDeviceEnumerator * enumerator, IMMDevice * dev)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
com_ptr<IMMDevice> ret;
|
|
|
|
com_ptr<IDeviceTopology> devtopo;
|
|
|
|
com_ptr<IConnector> connector;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
if (SUCCEEDED(dev->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL,
|
|
|
|
devtopo.receive_vpp())) &&
|
2020-12-28 16:15:37 +01:00
|
|
|
SUCCEEDED(devtopo->GetConnector(0, connector.receive()))) {
|
|
|
|
wchar_t * tmp = nullptr;
|
|
|
|
if (SUCCEEDED(connector->GetDeviceIdConnectedTo(&tmp))) {
|
|
|
|
com_heap_ptr<wchar_t> filterid(tmp);
|
|
|
|
if (FAILED(enumerator->GetDevice(filterid.get(), ret.receive())))
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
wasapi_is_default_device(EDataFlow flow, ERole role, LPCWSTR device_id,
|
|
|
|
IMMDeviceEnumerator * enumerator)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
com_ptr<IMMDevice> dev;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
hr = enumerator->GetDefaultAudioEndpoint(flow, role, dev.receive());
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
wchar_t * tmp = nullptr;
|
|
|
|
if (SUCCEEDED(dev->GetId(&tmp))) {
|
|
|
|
com_heap_ptr<wchar_t> defdevid(tmp);
|
|
|
|
ret = (wcscmp(defdevid.get(), device_id) == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* `ret` must be deallocated with `wasapi_destroy_device`, iff the return value
|
|
|
|
* of this function is `CUBEB_OK`. */
|
|
|
|
int
|
2021-12-08 07:33:31 +01:00
|
|
|
wasapi_create_device(cubeb * ctx, cubeb_device_info & ret,
|
|
|
|
IMMDeviceEnumerator * enumerator, IMMDevice * dev)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
com_ptr<IMMEndpoint> endpoint;
|
|
|
|
com_ptr<IMMDevice> devnode;
|
|
|
|
com_ptr<IAudioClient> client;
|
|
|
|
EDataFlow flow;
|
|
|
|
DWORD state = DEVICE_STATE_NOTPRESENT;
|
|
|
|
com_ptr<IPropertyStore> propstore;
|
|
|
|
REFERENCE_TIME def_period, min_period;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
// zero-out to be able to safely delete the pointers to friendly_name and
|
|
|
|
// group_id at all time in this function.
|
|
|
|
PodZero(&ret, 1);
|
|
|
|
|
|
|
|
struct prop_variant : public PROPVARIANT {
|
|
|
|
prop_variant() { PropVariantInit(this); }
|
|
|
|
~prop_variant() { PropVariantClear(this); }
|
|
|
|
prop_variant(prop_variant const &) = delete;
|
|
|
|
prop_variant & operator=(prop_variant const &) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
hr = dev->QueryInterface(IID_PPV_ARGS(endpoint.receive()));
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = endpoint->GetDataFlow(&flow);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t * tmp = nullptr;
|
|
|
|
hr = dev->GetId(&tmp);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
com_heap_ptr<wchar_t> device_id(tmp);
|
|
|
|
|
|
|
|
char const * device_id_intern = intern_device_id(ctx, device_id.get());
|
|
|
|
if (!device_id_intern) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = dev->OpenPropertyStore(STGM_READ, propstore.receive());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = dev->GetState(&state);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.device_id = device_id_intern;
|
|
|
|
ret.devid = reinterpret_cast<cubeb_devid>(ret.device_id);
|
|
|
|
prop_variant namevar;
|
|
|
|
hr = propstore->GetValue(PKEY_Device_FriendlyName, &namevar);
|
|
|
|
if (SUCCEEDED(hr) && namevar.vt == VT_LPWSTR) {
|
|
|
|
ret.friendly_name = wstr_to_utf8(namevar.pwszVal);
|
|
|
|
}
|
|
|
|
if (!ret.friendly_name) {
|
|
|
|
// This is not fatal, but a valid string is expected in all cases.
|
2021-12-08 07:33:31 +01:00
|
|
|
char * empty = new char[1];
|
2020-12-28 16:15:37 +01:00
|
|
|
empty[0] = '\0';
|
|
|
|
ret.friendly_name = empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
devnode = wasapi_get_device_node(enumerator, dev);
|
|
|
|
if (devnode) {
|
|
|
|
com_ptr<IPropertyStore> ps;
|
|
|
|
hr = devnode->OpenPropertyStore(STGM_READ, ps.receive());
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
wasapi_destroy_device(&ret);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
prop_variant instancevar;
|
|
|
|
hr = ps->GetValue(PKEY_Device_InstanceId, &instancevar);
|
|
|
|
if (SUCCEEDED(hr) && instancevar.vt == VT_LPWSTR) {
|
|
|
|
ret.group_id = wstr_to_utf8(instancevar.pwszVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret.group_id) {
|
|
|
|
// This is not fatal, but a valid string is expected in all cases.
|
2021-12-08 07:33:31 +01:00
|
|
|
char * empty = new char[1];
|
2020-12-28 16:15:37 +01:00
|
|
|
empty[0] = '\0';
|
|
|
|
ret.group_id = empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.preferred = CUBEB_DEVICE_PREF_NONE;
|
|
|
|
if (wasapi_is_default_device(flow, eConsole, device_id.get(), enumerator)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
ret.preferred =
|
|
|
|
(cubeb_device_pref)(ret.preferred | CUBEB_DEVICE_PREF_MULTIMEDIA);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
if (wasapi_is_default_device(flow, eCommunications, device_id.get(),
|
|
|
|
enumerator)) {
|
|
|
|
ret.preferred =
|
|
|
|
(cubeb_device_pref)(ret.preferred | CUBEB_DEVICE_PREF_VOICE);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
if (wasapi_is_default_device(flow, eConsole, device_id.get(), enumerator)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
ret.preferred =
|
|
|
|
(cubeb_device_pref)(ret.preferred | CUBEB_DEVICE_PREF_NOTIFICATION);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flow == eRender) {
|
|
|
|
ret.type = CUBEB_DEVICE_TYPE_OUTPUT;
|
|
|
|
} else if (flow == eCapture) {
|
|
|
|
ret.type = CUBEB_DEVICE_TYPE_INPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
2021-12-08 07:33:31 +01:00
|
|
|
case DEVICE_STATE_ACTIVE:
|
|
|
|
ret.state = CUBEB_DEVICE_STATE_ENABLED;
|
|
|
|
break;
|
|
|
|
case DEVICE_STATE_UNPLUGGED:
|
|
|
|
ret.state = CUBEB_DEVICE_STATE_UNPLUGGED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret.state = CUBEB_DEVICE_STATE_DISABLED;
|
|
|
|
break;
|
2020-12-28 16:15:37 +01:00
|
|
|
};
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
ret.format = static_cast<cubeb_device_fmt>(CUBEB_DEVICE_FMT_F32NE |
|
|
|
|
CUBEB_DEVICE_FMT_S16NE);
|
2020-12-28 16:15:37 +01:00
|
|
|
ret.default_format = CUBEB_DEVICE_FMT_F32NE;
|
|
|
|
prop_variant fmtvar;
|
|
|
|
hr = propstore->GetValue(PKEY_AudioEngine_DeviceFormat, &fmtvar);
|
|
|
|
if (SUCCEEDED(hr) && fmtvar.vt == VT_BLOB) {
|
|
|
|
if (fmtvar.blob.cbSize == sizeof(PCMWAVEFORMAT)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
const PCMWAVEFORMAT * pcm =
|
|
|
|
reinterpret_cast<const PCMWAVEFORMAT *>(fmtvar.blob.pBlobData);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
ret.max_rate = ret.min_rate = ret.default_rate = pcm->wf.nSamplesPerSec;
|
|
|
|
ret.max_channels = pcm->wf.nChannels;
|
|
|
|
} else if (fmtvar.blob.cbSize >= sizeof(WAVEFORMATEX)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
WAVEFORMATEX * wfx =
|
|
|
|
reinterpret_cast<WAVEFORMATEX *>(fmtvar.blob.pBlobData);
|
2020-12-28 16:15:37 +01:00
|
|
|
|
|
|
|
if (fmtvar.blob.cbSize >= sizeof(WAVEFORMATEX) + wfx->cbSize ||
|
|
|
|
wfx->wFormatTag == WAVE_FORMAT_PCM) {
|
|
|
|
ret.max_rate = ret.min_rate = ret.default_rate = wfx->nSamplesPerSec;
|
|
|
|
ret.max_channels = wfx->nChannels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
if (SUCCEEDED(dev->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER,
|
|
|
|
NULL, client.receive_vpp())) &&
|
2020-12-28 16:15:37 +01:00
|
|
|
SUCCEEDED(client->GetDevicePeriod(&def_period, &min_period))) {
|
|
|
|
ret.latency_lo = hns_to_frames(ret.default_rate, min_period);
|
|
|
|
ret.latency_hi = hns_to_frames(ret.default_rate, def_period);
|
|
|
|
} else {
|
|
|
|
ret.latency_lo = 0;
|
|
|
|
ret.latency_hi = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XASSERT(ret.friendly_name && ret.group_id);
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wasapi_destroy_device(cubeb_device_info * device)
|
|
|
|
{
|
2021-12-08 07:33:31 +01:00
|
|
|
delete[] device->friendly_name;
|
|
|
|
delete[] device->group_id;
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
wasapi_enumerate_devices(cubeb * context, cubeb_device_type type,
|
|
|
|
cubeb_device_collection * out)
|
|
|
|
{
|
|
|
|
com_ptr<IMMDeviceEnumerator> enumerator;
|
|
|
|
com_ptr<IMMDeviceCollection> collection;
|
|
|
|
HRESULT hr;
|
|
|
|
UINT cc, i;
|
|
|
|
EDataFlow flow;
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
hr =
|
|
|
|
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_PPV_ARGS(enumerator.receive()));
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not get device enumerator: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
if (type == CUBEB_DEVICE_TYPE_OUTPUT)
|
|
|
|
flow = eRender;
|
|
|
|
else if (type == CUBEB_DEVICE_TYPE_INPUT)
|
|
|
|
flow = eCapture;
|
|
|
|
else if (type & (CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT))
|
|
|
|
flow = eAll;
|
|
|
|
else
|
|
|
|
return CUBEB_ERROR;
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
hr = enumerator->EnumAudioEndpoints(flow, DEVICE_STATEMASK_ALL,
|
|
|
|
collection.receive());
|
2020-12-28 16:15:37 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("Could not enumerate audio endpoints: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = collection->GetCount(&cc);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
LOG("IMMDeviceCollection::GetCount() failed: %lx", hr);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
cubeb_device_info * devices = new cubeb_device_info[cc];
|
|
|
|
if (!devices)
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
|
|
|
|
PodZero(devices, cc);
|
|
|
|
out->count = 0;
|
|
|
|
for (i = 0; i < cc; i++) {
|
|
|
|
com_ptr<IMMDevice> dev;
|
|
|
|
hr = collection->Item(i, dev.receive());
|
|
|
|
if (FAILED(hr)) {
|
2021-12-08 07:33:31 +01:00
|
|
|
LOG("IMMDeviceCollection::Item(%u) failed: %lx", i - 1, hr);
|
2020-12-28 16:15:37 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-08 07:33:31 +01:00
|
|
|
if (wasapi_create_device(context, devices[out->count], enumerator.get(),
|
|
|
|
dev.get()) == CUBEB_OK) {
|
2020-12-28 16:15:37 +01:00
|
|
|
out->count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out->device = devices;
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-08 07:33:31 +01:00
|
|
|
wasapi_device_collection_destroy(cubeb * /*ctx*/,
|
|
|
|
cubeb_device_collection * collection)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
XASSERT(collection);
|
|
|
|
|
|
|
|
for (size_t n = 0; n < collection->count; n++) {
|
2021-12-08 07:33:31 +01:00
|
|
|
cubeb_device_info & dev = collection->device[n];
|
2020-12-28 16:15:37 +01:00
|
|
|
wasapi_destroy_device(&dev);
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:33:31 +01:00
|
|
|
delete[] collection->device;
|
2020-12-28 16:15:37 +01:00
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-08 07:33:31 +01:00
|
|
|
wasapi_register_device_collection_changed(
|
|
|
|
cubeb * context, cubeb_device_type devtype,
|
|
|
|
cubeb_device_collection_changed_callback collection_changed_callback,
|
|
|
|
void * user_ptr)
|
2020-12-28 16:15:37 +01:00
|
|
|
{
|
|
|
|
if (devtype == CUBEB_DEVICE_TYPE_UNKNOWN) {
|
|
|
|
return CUBEB_ERROR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection_changed_callback) {
|
|
|
|
// Make sure it has been unregistered first.
|
|
|
|
XASSERT(((devtype & CUBEB_DEVICE_TYPE_INPUT) &&
|
|
|
|
!context->input_collection_changed_callback) ||
|
|
|
|
((devtype & CUBEB_DEVICE_TYPE_OUTPUT) &&
|
|
|
|
!context->output_collection_changed_callback));
|
|
|
|
|
|
|
|
// Stop the notification client. Notifications arrive on
|
|
|
|
// a separate thread. We stop them here to avoid
|
|
|
|
// synchronization issues during the update.
|
|
|
|
if (context->device_collection_enumerator.get()) {
|
|
|
|
HRESULT hr = unregister_collection_notification_client(context);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devtype & CUBEB_DEVICE_TYPE_INPUT) {
|
|
|
|
context->input_collection_changed_callback = collection_changed_callback;
|
|
|
|
context->input_collection_changed_user_ptr = user_ptr;
|
|
|
|
}
|
|
|
|
if (devtype & CUBEB_DEVICE_TYPE_OUTPUT) {
|
|
|
|
context->output_collection_changed_callback = collection_changed_callback;
|
|
|
|
context->output_collection_changed_user_ptr = user_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = register_collection_notification_client(context);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!context->device_collection_enumerator.get()) {
|
|
|
|
// Already unregistered, ignore it.
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = unregister_collection_notification_client(context);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
if (devtype & CUBEB_DEVICE_TYPE_INPUT) {
|
|
|
|
context->input_collection_changed_callback = nullptr;
|
|
|
|
context->input_collection_changed_user_ptr = nullptr;
|
|
|
|
}
|
|
|
|
if (devtype & CUBEB_DEVICE_TYPE_OUTPUT) {
|
|
|
|
context->output_collection_changed_callback = nullptr;
|
|
|
|
context->output_collection_changed_user_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If after the updates we still have registered
|
|
|
|
// callbacks restart the notification client.
|
|
|
|
if (context->input_collection_changed_callback ||
|
|
|
|
context->output_collection_changed_callback) {
|
|
|
|
hr = register_collection_notification_client(context);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_ops const wasapi_ops = {
|
2021-12-08 07:33:31 +01:00
|
|
|
/*.init =*/wasapi_init,
|
|
|
|
/*.get_backend_id =*/wasapi_get_backend_id,
|
|
|
|
/*.get_max_channel_count =*/wasapi_get_max_channel_count,
|
|
|
|
/*.get_min_latency =*/wasapi_get_min_latency,
|
|
|
|
/*.get_preferred_sample_rate =*/wasapi_get_preferred_sample_rate,
|
|
|
|
/*.enumerate_devices =*/wasapi_enumerate_devices,
|
|
|
|
/*.device_collection_destroy =*/wasapi_device_collection_destroy,
|
|
|
|
/*.destroy =*/wasapi_destroy,
|
|
|
|
/*.stream_init =*/wasapi_stream_init,
|
|
|
|
/*.stream_destroy =*/wasapi_stream_destroy,
|
|
|
|
/*.stream_start =*/wasapi_stream_start,
|
|
|
|
/*.stream_stop =*/wasapi_stream_stop,
|
|
|
|
/*.stream_get_position =*/wasapi_stream_get_position,
|
|
|
|
/*.stream_get_latency =*/wasapi_stream_get_latency,
|
|
|
|
/*.stream_get_input_latency =*/wasapi_stream_get_input_latency,
|
|
|
|
/*.stream_set_volume =*/wasapi_stream_set_volume,
|
|
|
|
/*.stream_set_name =*/NULL,
|
|
|
|
/*.stream_get_current_device =*/NULL,
|
|
|
|
/*.stream_device_destroy =*/NULL,
|
|
|
|
/*.stream_register_device_changed_callback =*/NULL,
|
|
|
|
/*.register_device_collection_changed =*/
|
|
|
|
wasapi_register_device_collection_changed,
|
2020-12-28 16:15:37 +01:00
|
|
|
};
|
2021-12-08 07:33:31 +01:00
|
|
|
} // namespace
|