Source code
Revision control
Copy as Markdown
Other Tools
/*
* 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
#include <algorithm>
#include <atomic>
#include <audioclient.h>
#include <avrt.h>
#include <cmath>
#include <devicetopology.h>
#include <initguid.h>
#include <limits>
#include <memory>
#include <mmdeviceapi.h>
#include <process.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <windef.h>
#include <windows.h>
/* clang-format off */
/* These need to be included after windows.h */
#include <mmsystem.h>
/* clang-format on */
#include "cubeb-internal.h"
#include "cubeb/cubeb.h"
#include "cubeb_mixer.h"
#include "cubeb_resampler.h"
#include "cubeb_strings.h"
#include "cubeb_tracing.h"
#include "cubeb_utils.h"
// Windows 10 exposes the IAudioClient3 interface to create low-latency streams.
// 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.
#ifndef __IAudioClient3_INTERFACE_DEFINED__
#define __IAudioClient3_INTERFACE_DEFINED__
MIDL_INTERFACE("7ED4EE07-8E67-4CD4-8C1A-2B7A5987AD42")
IAudioClient3 : public IAudioClient
{
public:
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;
};
#ifdef __CRT_UUID_DECL
// Required for MinGW
__CRT_UUID_DECL(IAudioClient3, 0x7ED4EE07, 0x8E67, 0x4CD4, 0x8C, 0x1A, 0x2B,
0x7A, 0x59, 0x87, 0xAD, 0x42)
#endif
#endif
// Copied from audioclient.h in the Windows 10 SDK
#ifndef AUDCLNT_E_ENGINE_PERIODICITY_LOCKED
#define AUDCLNT_E_ENGINE_PERIODICITY_LOCKED AUDCLNT_ERR(0x028)
#endif
#ifndef PKEY_Device_FriendlyName
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,
0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0,
14); // DEVPROP_TYPE_STRING
#endif
#ifndef PKEY_Device_InstanceId
DEFINE_PROPERTYKEY(PKEY_Device_InstanceId, 0x78c34fc8, 0x104a, 0x4aca, 0x9e,
0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57,
0x00000100); // VT_LPWSTR
#endif
namespace {
const int64_t LATENCY_NOT_AVAILABLE_YET = -1;
const DWORD DEVICE_CHANGE_DEBOUNCE_MS = 250;
struct com_heap_ptr_deleter {
void operator()(void * ptr) const noexcept { CoTaskMemFree(ptr); }
};
template <typename T>
using com_heap_ptr = std::unique_ptr<T, com_heap_ptr_deleter>;
template <typename T, size_t N>
constexpr size_t
ARRAY_LENGTH(T (&)[N])
{
return N;
}
template <typename T> class no_addref_release : public T {
ULONG STDMETHODCALLTYPE AddRef() = 0;
ULONG STDMETHODCALLTYPE Release() = 0;
};
template <typename T> class com_ptr {
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;
~com_ptr() noexcept { release(); }
com_ptr(com_ptr && other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
com_ptr & operator=(com_ptr && other) noexcept
{
if (ptr != other.ptr) {
release();
ptr = other.ptr;
other.ptr = nullptr;
}
return *this;
}
explicit operator bool() const noexcept { return nullptr != ptr; }
no_addref_release<T> * operator->() const noexcept
{
return static_cast<no_addref_release<T> *>(ptr);
}
T * get() const noexcept { return ptr; }
T ** receive() noexcept
{
XASSERT(ptr == nullptr);
return &ptr;
}
void ** receive_vpp() noexcept
{
return reinterpret_cast<void **>(receive());
}
com_ptr & operator=(std::nullptr_t) noexcept
{
release();
return *this;
}
void reset(T * p = nullptr) noexcept
{
release();
ptr = p;
}
private:
void release() noexcept
{
T * temp = ptr;
if (temp) {
ptr = nullptr;
temp->Release();
}
}
T * ptr = nullptr;
};
LONG
wasapi_stream_add_ref(cubeb_stream * stm);
LONG
wasapi_stream_release(cubeb_stream * stm);
struct auto_stream_ref {
auto_stream_ref(cubeb_stream * stm_) : stm(stm_)
{
wasapi_stream_add_ref(stm);
}
~auto_stream_ref() { wasapi_stream_release(stm); }
cubeb_stream * stm;
};
extern cubeb_ops const wasapi_ops;
static com_heap_ptr<wchar_t>
wasapi_get_default_device_id(EDataFlow flow, ERole role,
IMMDeviceEnumerator * enumerator);
struct wasapi_default_devices {
wasapi_default_devices(IMMDeviceEnumerator * enumerator)
: render_console_id(
wasapi_get_default_device_id(eRender, eConsole, enumerator)),
render_comms_id(
wasapi_get_default_device_id(eRender, eCommunications, enumerator)),
capture_console_id(
wasapi_get_default_device_id(eCapture, eConsole, enumerator)),
capture_comms_id(
wasapi_get_default_device_id(eCapture, eCommunications, enumerator))
{
}
bool is_default(EDataFlow flow, ERole role, wchar_t const * id)
{
wchar_t const * default_id = nullptr;
if (flow == eRender && role == eConsole) {
default_id = this->render_console_id.get();
} else if (flow == eRender && role == eCommunications) {
default_id = this->render_comms_id.get();
} else if (flow == eCapture && role == eConsole) {
default_id = this->capture_console_id.get();
} else if (flow == eCapture && role == eCommunications) {
default_id = this->capture_comms_id.get();
}
return default_id && wcscmp(id, default_id) == 0;
}
private:
com_heap_ptr<wchar_t> render_console_id;
com_heap_ptr<wchar_t> render_comms_id;
com_heap_ptr<wchar_t> capture_console_id;
com_heap_ptr<wchar_t> capture_comms_id;
};
struct AutoRegisterThread {
AutoRegisterThread(const char * name) { CUBEB_REGISTER_THREAD(name); }
~AutoRegisterThread() { CUBEB_UNREGISTER_THREAD(); }
};
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,
wasapi_default_devices * defaults);
void
wasapi_destroy_device(cubeb_device_info * device_info);
static int
wasapi_enumerate_devices_internal(cubeb * context, cubeb_device_type type,
cubeb_device_collection * out,
DWORD state_mask);
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);
} // namespace
class wasapi_collection_notification_client;
class monitor_device_notifications;
struct cubeb {
cubeb_ops const * ops = &wasapi_ops;
owned_critical_section lock;
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. */
cubeb_device_collection_changed_callback input_collection_changed_callback =
nullptr;
void * input_collection_changed_user_ptr = nullptr;
/* Collection changed for output (render) devices. */
cubeb_device_collection_changed_callback output_collection_changed_callback =
nullptr;
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. */
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};
/* Stream parameters. This is what the client requested,
* and what will be presented in the callback. */
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};
/* 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;
#ifdef CUBEB_WASAPI_USE_IAUDIOSTREAMVOLUME
/* Interface pointer to use the volume facilities. */
com_ptr<IAudioStreamVolume> audio_stream_volume;
#endif
/* 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_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. */
std::unique_ptr<cubeb_resampler, decltype(&cubeb_resampler_destroy)>
resampler = {nullptr, cubeb_resampler_destroy};
/* Mixer interfaces */
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};
/* 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;
/* This needs an active audio input stream to be known, and is updated in the
* first audio input callback. */
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;
/* This is set by the render loop thread once it has obtained a reference to
* COM and this stream object. */
HANDLE thread_ready_event = 0;
/* Keep a ref count on this stream object. After both stream_destroy has been
* called and the render loop thread has exited, destroy this stream object.
*/
LONG ref_count = 0;
/* True if the stream is active, false if inactive. */
bool active = false;
};
class monitor_device_notifications {
public:
monitor_device_notifications(cubeb * context) : cubeb_context(context)
{
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");
}
}
}
private:
static unsigned int __stdcall thread_proc(LPVOID args)
{
AutoRegisterThread raii("WASAPI device notification thread");
XASSERT(args);
auto mdn = static_cast<monitor_device_notifications *>(args);
mdn->notification_thread_loop();
SetEvent(mdn->shutdown_complete);
return 0;
}
void notification_thread_loop()
{
struct auto_com {
auto_com()
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
XASSERT(SUCCEEDED(hr));
}
~auto_com() { CoUninitialize(); }
} com;
HANDLE wait_array[3] = {
input_changed,
output_changed,
begin_shutdown,
};
while (true) {
Sleep(200);
DWORD wait_result = WaitForMultipleObjects(ARRAY_LENGTH(wait_array),
wait_array, FALSE, INFINITE);
if (wait_result == WAIT_OBJECT_0) { // input changed
cubeb_context->input_collection_changed_callback(
cubeb_context, cubeb_context->input_collection_changed_user_ptr);
} else if (wait_result == WAIT_OBJECT_0 + 1) { // output changed
cubeb_context->output_collection_changed_callback(
cubeb_context, cubeb_context->output_collection_changed_user_ptr);
} 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;
}
thread = (HANDLE)_beginthreadex(nullptr, 256 * 1024, thread_proc, this,
STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
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;
};
class wasapi_collection_notification_client : public IMMNotificationClient {
public:
/* The implementation of MSCOM was copied from MSDN. */
ULONG STDMETHODCALLTYPE AddRef() { return InterlockedIncrement(&ref_count); }
ULONG STDMETHODCALLTYPE Release()
{
ULONG ulRef = InterlockedDecrement(&ref_count);
if (0 == ulRef) {
delete this;
}
return ulRef;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID ** ppvInterface)
{
if (__uuidof(IUnknown) == riid) {
AddRef();
*ppvInterface = (IUnknown *)this;
} else if (__uuidof(IMMNotificationClient) == riid) {
AddRef();
*ppvInterface = (IMMNotificationClient *)this;
} else {
*ppvInterface = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
wasapi_collection_notification_client(cubeb * context)
: ref_count(1), cubeb_context(context), monitor_notifications(context)
{
XASSERT(cubeb_context);
}
virtual ~wasapi_collection_notification_client() {}
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role,
LPCWSTR device_id)
{
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;
}
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR device_id,
DWORD new_state)
{
XASSERT(cubeb_context->output_collection_changed_callback ||
cubeb_context->input_collection_changed_callback);
LOG("collection: Audio device state changed, id = %S, state = %lu.",
device_id, new_state);
EDataFlow flow;
HRESULT hr = GetDataFlow(device_id, &flow);
if (FAILED(hr)) {
return hr;
}
monitor_notifications.notify(flow);
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR device_id,
const PROPERTYKEY key)
{
// Audio device property value changed.
return S_OK;
}
private:
HRESULT GetDataFlow(LPCWSTR device_id, EDataFlow * flow)
{
com_ptr<IMMDevice> device;
com_ptr<IMMEndpoint> endpoint;
HRESULT hr = cubeb_context->device_collection_enumerator->GetDevice(
device_id, device.receive());
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;
};
class wasapi_endpoint_notification_client : public IMMNotificationClient {
public:
/* The implementation of MSCOM was copied from MSDN. */
ULONG STDMETHODCALLTYPE AddRef() { return InterlockedIncrement(&ref_count); }
ULONG STDMETHODCALLTYPE Release()
{
ULONG ulRef = InterlockedDecrement(&ref_count);
if (0 == ulRef) {
delete this;
}
return ulRef;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID ** ppvInterface)
{
if (__uuidof(IUnknown) == riid) {
AddRef();
*ppvInterface = (IUnknown *)this;
} else if (__uuidof(IMMNotificationClient) == riid) {
AddRef();
*ppvInterface = (IMMNotificationClient *)this;
} else {
*ppvInterface = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
wasapi_endpoint_notification_client(HANDLE event, ERole role)
: ref_count(1), reconfigure_event(event), role(role),
last_device_change(timeGetTime())
{
}
virtual ~wasapi_endpoint_notification_client() {}
HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role,
LPCWSTR device_id)
{
LOG("endpoint: Audio device default changed flow=%d role=%d "
"new_device_id=%ws.",
flow, role, device_id);
/* we only support a single stream type for now. */
if (flow != eRender || role != this->role) {
return S_OK;
}
DWORD last_change_ms = timeGetTime() - last_device_change;
bool same_device = default_device_id && device_id &&
wcscmp(default_device_id.get(), device_id) == 0;
LOG("endpoint: Audio device default changed last_change=%u same_device=%d",
last_change_ms, same_device);
if (last_change_ms > DEVICE_CHANGE_DEBOUNCE_MS || !same_device) {
if (device_id) {
default_device_id.reset(_wcsdup(device_id));
} else {
default_device_id.reset();
}
BOOL ok = SetEvent(reconfigure_event);
LOG("endpoint: Audio device default changed: trigger reconfig");
if (!ok) {
LOG("endpoint: SetEvent on reconfigure_event failed: %lx",
GetLastError());
}
}
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;
}
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR device_id,
DWORD new_state)
{
LOG("endpoint: Audio device state changed.");
return S_OK;
}
HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR device_id,
const PROPERTYKEY key)
{
// Audio device property value changed.
return S_OK;
}
private:
/* refcount for this instance, necessary to implement MSCOM semantics. */
LONG ref_count;
HANDLE reconfigure_event;
ERole role;
std::unique_ptr<const wchar_t[]> default_device_id;
DWORD last_device_change;
};
namespace {
long
wasapi_data_callback(cubeb_stream * stm, void * user_ptr,
void const * input_buffer, void * output_buffer,
long nframes)
{
return stm->data_callback(stm, user_ptr, input_buffer, output_buffer,
nframes);
}
void
wasapi_state_callback(cubeb_stream * stm, void * user_ptr, cubeb_state state)
{
return stm->state_callback(stm, user_ptr, state);
}
char const *
intern_device_id(cubeb * ctx, wchar_t const * id)
{
XASSERT(id);
auto_lock lock(ctx->lock);
char const * tmp = wstr_to_utf8(id);
if (!tmp) {
return nullptr;
}
char const * interned = cubeb_strings_intern(ctx->device_ids, tmp);
free((void *)tmp);
return interned;
}
bool
has_input(cubeb_stream * stm)
{
return stm->input_stream_params.rate != 0;
}
bool
has_output(cubeb_stream * stm)
{
return stm->output_stream_params.rate != 0;
}
double
stream_to_mix_samplerate_ratio(cubeb_stream_params & stream,
cubeb_stream_params & mixer)
{
return double(stream.rate) / mixer.rate;
}
/* Convert the channel layout into the corresponding KSAUDIO_CHANNEL_CONFIG.
See more:
*/
cubeb_channel_layout
mask_to_channel_layout(WAVEFORMATEX const * fmt)
{
cubeb_channel_layout mask = 0;
if (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
WAVEFORMATEXTENSIBLE const * ext =
reinterpret_cast<WAVEFORMATEXTENSIBLE const *>(fmt);
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;
}
}
long out_frames =
cubeb_resampler_fill(stm->resampler.get(), input_buffer,
&input_frames_count, dest, output_frames_needed);
if (out_frames < 0) {
ALOGV("Callback refill error: %d", out_frames);
wasapi_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
return out_frames;
}
float volume = 1.0;
{
auto_lock lock(stm->stream_reset_lock);
stm->frames_written += out_frames;
volume = stm->volume;
}
/* 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. */
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
// We don't bother mixing dummy output as it will be silenced, otherwise mix
// output if needed
if (!stm->has_dummy_output && has_output(stm) && stm->output_mixer) {
XASSERT(dest == stm->mix_buffer.data());
size_t dest_size =
out_frames * stm->output_stream_params.channels * stm->bytes_per_sample;
XASSERT(dest_size <= stm->mix_buffer.size());
size_t output_buffer_size =
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);
if (ret < 0) {
LOG("Error remixing content (%d)", ret);
}
}
return out_frames;
}
bool
trigger_async_reconfigure(cubeb_stream * stm)
{
XASSERT(stm && stm->reconfigure_event);
LOG("Try reconfiguring the stream");
BOOL ok = SetEvent(stm->reconfigure_event);
if (!ok) {
LOG("SetEvent on reconfigure_event failed: %lx", GetLastError());
}
return static_cast<bool>(ok);
}
/* This helper grabs all the frames available from a capture client, put them in
* the linear_input_buffer. This helper does not work with exclusive mode
* streams. */
bool
get_input_buffer(cubeb_stream * stm)
{
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).
for (hr = stm->capture_client->GetNextPacketSize(&next); next > 0;
hr = stm->capture_client->GetNextPacketSize(&next)) {
if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
// Application can recover from this error. More info
LOG("Input device invalidated error");
// No need to reset device if user asks to use particular device, or
// switching is disabled.
if (stm->input_device_id ||
(stm->input_stream_params.prefs &
CUBEB_STREAM_PREF_DISABLE_DEVICE_SWITCHING) ||
!trigger_async_reconfigure(stm)) {
wasapi_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
return false;
}
return true;
}
if (FAILED(hr)) {
LOG("cannot get next packet size: %lx", hr);
return false;
}
UINT32 frames;
hr = stm->capture_client->GetBuffer(&input_packet, &frames, &flags,
&dev_pos, &pc_position);
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;
// See
// section "Remarks".
QueryPerformanceCounter(&now);
now_hns =
10000000 * now.QuadPart / stm->context->performance_counter_frequency;
if (now_hns >= pc_position) {
stm->input_latency_hns = now_hns - pc_position;
}
}
stm->total_input_frames += frames;
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
// discontinuities. See
// As the first scenario can be ignored, and we anticipate the second
// scenario is mitigated, we ignore the flag.
// For more info:
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(
stm->linear_input_buffer->length() + input_stream_samples);
XASSERT(ok);
size_t input_packet_size =
frames * stm->input_mix_params.channels *
cubeb_sample_size(stm->input_mix_params.format);
size_t linear_input_buffer_size =
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(),
linear_input_buffer_size);
stm->linear_input_buffer->set_length(
stm->linear_input_buffer->length() + input_stream_samples);
} else {
stm->linear_input_buffer->push(input_packet, input_stream_samples);
}
}
hr = stm->capture_client->ReleaseBuffer(frames);
if (FAILED(hr)) {
LOG("FAILED to release intput buffer");
return false;
}
offset += input_stream_samples;
}
ALOGV("get_input_buffer: got %d frames", offset);
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. */
bool
get_output_buffer(cubeb_stream * stm, void *& buffer, size_t & frame_count)
{
UINT32 padding_out;
HRESULT hr;
XASSERT(has_output(stm));
hr = stm->output_client->GetCurrentPadding(&padding_out);
if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
// Application can recover from this error. More info
LOG("Output device invalidated error");
// No need to reset device if user asks to use particular device, or
// switching is disabled.
if (stm->output_device_id ||
(stm->output_stream_params.prefs &
CUBEB_STREAM_PREF_DISABLE_DEVICE_SWITCHING) ||
!trigger_async_reconfigure(stm)) {
wasapi_state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
return false;
}
return true;
}
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.");
wasapi_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));
if (stm->input_stream_params.prefs & CUBEB_STREAM_PREF_LOOPBACK) {
HRESULT rv = get_input_buffer(stm);
if (FAILED(rv)) {
return rv;
}
}
input_frames =
stm->linear_input_buffer->length() / stm->input_stream_params.channels;
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;
}
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);
long got;
if (stm->has_dummy_output) {
ALOGV(
"Duplex callback (dummy output): input frames: %Iu, output frames: %Iu",
input_frames, output_frames);
// 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)
got =
refill(stm, stm->linear_input_buffer->data(), input_frames, nullptr, 0);
} else {
ALOGV("Duplex callback: input frames: %Iu, output frames: %Iu",
input_frames, output_frames);
got = refill(stm, stm->linear_input_buffer->data(), input_frames,
output_buffer, output_frames);
}
stm->linear_input_buffer->clear();
if (stm->has_dummy_output) {
// If output is a dummy output, make sure it's silent
hr = stm->render_client->ReleaseBuffer(output_frames,
AUDCLNT_BUFFERFLAGS_SILENT);
} else {
hr = stm->render_client->ReleaseBuffer(output_frames, 0);
}
if (FAILED(hr)) {
LOG("failed to release buffer: %lx", hr);
return false;
}
if (got < 0) {
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;
}
input_frames =
stm->linear_input_buffer->length() / stm->input_stream_params.channels;
if (!input_frames) {
return true;
}
ALOGV("Input callback: input frames: %Iu", input_frames);
long read =
refill(stm, stm->linear_input_buffer->data(), input_frames, nullptr, 0);
if (read < 0) {
return false;
}
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;
}
long got = refill(stm, nullptr, 0, output_buffer, output_frames);
ALOGV("Output callback: output frames requested: %Iu, got %ld", output_frames,
got);
if (got < 0) {
return false;
}
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;
}
void
wasapi_stream_destroy(cubeb_stream * stm);
static unsigned int __stdcall wasapi_stream_render_loop(LPVOID stream)
{
AutoRegisterThread raii("cubeb rendering thread");
cubeb_stream * stm = static_cast<cubeb_stream *>(stream);
auto_stream_ref stream_ref(stm);
struct auto_com {
auto_com()
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);