IMMDevice *dev; /**< Selected output device, NULL if none */
IAudioSessionManager *manager; /**< Session for the output device */
struct IAudioSessionEvents session_events;
+ ISimpleAudioVolume *volume; /**< Volume setter */
LONG refs;
- CRITICAL_SECTION lock; /**< Lock to protect Core Audio API state */
HANDLE device_changed; /**< Event to reset thread */
+ HANDLE device_ready; /**< Event when thread is reset */
vlc_thread_t thread; /**< Thread for audio session control */
};
/* NOTE: The Core Audio API documentation totally fails to specify the thread
- * safety (or lack thereof) of the interfaces. This code is most pessimistic
- * and assumes that the API is not thread-safe at all.
+ * safety (or lack thereof) of the interfaces. This code takes the most
+ * restrictive assumption, no thread safety: The background thread (MMThread)
+ * only runs at specified times, namely between the device_ready and
+ * device_changed events (effectively, a thread barrier but only Windows 8
+ * provides thread barriers natively).
*
* The audio output owner (i.e. the audio output core) is responsible for
* serializing callbacks. This code only needs to be concerned with
- * synchronization between the set of audio output callbacks, the thread
+ * synchronization between the set of audio output callbacks, MMThread()
* and (trivially) the device and session notifications. */
static int vlc_FromHR(audio_output_t *aout, HRESULT hr)
{
/* Restart on unplug */
if (unlikely(hr == AUDCLNT_E_DEVICE_INVALIDATED))
- var_TriggerCallback(aout, "audio-device");
+ aout_RestartRequest(aout, AOUT_RESTART_OUTPUT);
return SUCCEEDED(hr) ? 0 : -1;
}
HRESULT hr;
EnterMTA();
- EnterCriticalSection(&sys->lock);
hr = aout_stream_TimeGet(sys->stream, delay);
- LeaveCriticalSection(&sys->lock);
LeaveMTA();
return SUCCEEDED(hr) ? 0 : -1;
HRESULT hr;
EnterMTA();
- EnterCriticalSection(&sys->lock);
hr = aout_stream_Play(sys->stream, block);
- LeaveCriticalSection(&sys->lock);
LeaveMTA();
vlc_FromHR(aout, hr);
HRESULT hr;
EnterMTA();
- EnterCriticalSection(&sys->lock);
hr = aout_stream_Pause(sys->stream, paused);
- LeaveCriticalSection(&sys->lock);
LeaveMTA();
vlc_FromHR(aout, hr);
static void Flush(audio_output_t *aout, bool wait)
{
aout_sys_t *sys = aout->sys;
- mtime_t delay = VLC_TS_INVALID;
EnterMTA();
- EnterCriticalSection(&sys->lock);
if (wait)
{ /* Loosy drain emulation */
- if (FAILED(aout_stream_TimeGet(sys->stream, &delay)))
- delay = VLC_TS_INVALID;
+ mtime_t delay;
+
+ if (SUCCEEDED(aout_stream_TimeGet(sys->stream, &delay)))
+ Sleep((delay / (CLOCK_FREQ / 1000)) + 1);
}
else
aout_stream_Flush(sys->stream);
- LeaveCriticalSection(&sys->lock);
LeaveMTA();
- if (delay != VLC_TS_INVALID)
- Sleep((delay / (CLOCK_FREQ / 1000)) + 1);
-}
-
-static ISimpleAudioVolume *GetSimpleVolume(audio_output_t *aout)
-{
- aout_sys_t *sys = aout->sys;
- ISimpleAudioVolume *volume;
- HRESULT hr;
-
- if (sys->manager == NULL)
- return NULL;
-
- if (TryEnterMTA(aout))
- return NULL;
- EnterCriticalSection(&sys->lock);
- hr = IAudioSessionManager_GetSimpleAudioVolume(sys->manager,
- &GUID_VLC_AUD_OUT,
- FALSE, &volume);
- if (FAILED(hr))
- {
- LeaveCriticalSection(&sys->lock);
- LeaveMTA();
- msg_Err(aout, "cannot get simple volume (error 0x%lx)", hr);
- return NULL;
- }
- return volume;
-}
-
-static void PutSimpleVolume(audio_output_t *aout, ISimpleAudioVolume *volume)
-{
- aout_sys_t *sys = aout->sys;
-
- ISimpleAudioVolume_Release(volume);
- LeaveCriticalSection(&sys->lock);
- LeaveMTA();
}
static int VolumeSet(audio_output_t *aout, float vol)
{
- ISimpleAudioVolume *volume = GetSimpleVolume(aout);
+ ISimpleAudioVolume *volume = aout->sys->volume;
if (volume == NULL)
return -1;
+ if (TryEnterMTA(aout))
+ return -1;
+
HRESULT hr = ISimpleAudioVolume_SetMasterVolume(volume, vol, NULL);
if (FAILED(hr))
msg_Err(aout, "cannot set volume (error 0x%lx)", hr);
- PutSimpleVolume(aout, volume);
+ LeaveMTA();
return FAILED(hr) ? -1 : 0;
}
static int MuteSet(audio_output_t *aout, bool mute)
{
- ISimpleAudioVolume *volume = GetSimpleVolume(aout);
+ ISimpleAudioVolume *volume = aout->sys->volume;
if (volume == NULL)
return -1;
+ if (TryEnterMTA(aout))
+ return -1;
+
HRESULT hr = ISimpleAudioVolume_SetMute(volume, mute ? TRUE : FALSE, NULL);
if (FAILED(hr))
msg_Err(aout, "cannot set volume (error 0x%lx)", hr);
- PutSimpleVolume(aout, volume);
+ LeaveMTA();
return FAILED(hr) ? -1 : 0;
}
-/*** Audio devices ***/
-static int DeviceChanged(vlc_object_t *obj, const char *varname,
- vlc_value_t prev, vlc_value_t cur, void *data)
-{
- /* FIXME: This does not work. sys->dev, ->manager and ->stream must be
- * recreated. Those pointers are protected by the aout lock, which
- * serializes accesses to the audio_output_t. Unfortunately,
- * aout lock cannot be taken from a variable callback.
- * Solution: add device_change callback to audio_output_t. */
- aout_ChannelsRestart(obj, varname, prev, cur, data);
- return VLC_SUCCESS;
-}
-
-static void GetDevices(vlc_object_t *obj, IMMDeviceEnumerator *it)
-{
- HRESULT hr;
- vlc_value_t val, text;
-
- var_Create (obj, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
- text.psz_string = _("Audio Device");
- var_Change (obj, "audio-device", VLC_VAR_SETTEXT, &text, NULL);
-
- /* TODO: implement IMMNotificationClient for hotplug devices */
- IMMDeviceCollection *devs;
- hr = IMMDeviceEnumerator_EnumAudioEndpoints(it, eRender,
- DEVICE_STATE_ACTIVE, &devs);
- if (FAILED(hr))
- {
- msg_Warn (obj, "cannot enumerate audio endpoints (error 0x%lx)", hr);
- return;
- }
-
- UINT n;
- hr = IMMDeviceCollection_GetCount(devs, &n);
- if (FAILED(hr))
- {
- msg_Warn (obj, "cannot count audio endpoints (error 0x%lx)", hr);
- n = 0;
- }
- else
- msg_Dbg(obj, "Available Windows Audio devices:");
-
- while (n > 0)
- {
- IMMDevice *dev;
-
- hr = IMMDeviceCollection_Item(devs, --n, &dev);
- if (FAILED(hr))
- continue;
-
- /* Unique device ID */
- LPWSTR devid;
- hr = IMMDevice_GetId(dev, &devid);
- if (FAILED(hr))
- {
- IMMDevice_Release(dev);
- continue;
- }
- val.psz_string = FromWide(devid);
- CoTaskMemFree(devid);
- text.psz_string = val.psz_string;
-
- /* User-readable device name */
- IPropertyStore *props;
- hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, &props);
- if (SUCCEEDED(hr))
- {
- PROPVARIANT v;
-
- PropVariantInit(&v);
- hr = IPropertyStore_GetValue(props, &PKEY_Device_FriendlyName, &v);
- if (SUCCEEDED(hr))
- text.psz_string = FromWide(v.pwszVal);
- PropVariantClear(&v);
- IPropertyStore_Release(props);
- }
- IMMDevice_Release(dev);
-
- msg_Dbg(obj, "%s (%s)", val.psz_string, text.psz_string);
- var_Change(obj, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
- if (likely(text.psz_string != val.psz_string))
- free(text.psz_string);
- free(val.psz_string);
- }
- IMMDeviceCollection_Release(devs);
-}
-
/*** Audio session events ***/
static inline aout_sys_t *vlc_AudioSessionEvents_sys(IAudioSessionEvents *this)
{
static STDMETHODIMP
vlc_AudioSessionEvents_OnSessionDisconnected(IAudioSessionEvents *this,
- AudioSessionDisconnectReason reason)
+ AudioSessionDisconnectReason reason)
{
aout_sys_t *sys = vlc_AudioSessionEvents_sys(this);
audio_output_t *aout = sys->aout;
- msg_Dbg(aout, "session disconnected: reason %d", reason);
+ switch (reason)
+ {
+ case DisconnectReasonDeviceRemoval:
+ msg_Warn(aout, "session disconnected: %s", "device removed");
+ break;
+ case DisconnectReasonServerShutdown:
+ msg_Err(aout, "session disconnected: %s", "service stopped");
+ return S_OK;
+ case DisconnectReasonFormatChanged:
+ msg_Warn(aout, "session disconnected: %s", "format changed");
+ break;
+ case DisconnectReasonSessionLogoff:
+ msg_Err(aout, "session disconnected: %s", "user logged off");
+ return S_OK;
+ case DisconnectReasonSessionDisconnected:
+ msg_Err(aout, "session disconnected: %s", "session disconnected");
+ return S_OK;
+ case DisconnectReasonExclusiveModeOverride:
+ msg_Err(aout, "session disconnected: %s", "stream overriden");
+ return S_OK;
+ default:
+ msg_Warn(aout, "session disconnected: unknown reason %d", reason);
+ return S_OK;
+ }
+ /* NOTE: audio decoder thread should get invalidated device and restart */
return S_OK;
}
HRESULT hr;
/* Register session control */
- msg_Err(aout, "HERE: %p", sys->manager);
if (sys->manager != NULL)
{
+ hr = IAudioSessionManager_GetSimpleAudioVolume(sys->manager,
+ &GUID_VLC_AUD_OUT,
+ FALSE, &sys->volume);
+ if (FAILED(hr))
+ msg_Err(aout, "cannot get simple volume (error 0x%lx)", hr);
+
hr = IAudioSessionManager_GetAudioSessionControl(sys->manager,
&GUID_VLC_AUD_OUT, 0,
&control);
msg_Err(aout, "cannot get session control (error 0x%lx)", hr);
}
else
+ {
+ sys->volume = NULL;
control = NULL;
+ }
- msg_Err(aout, "THERE");
if (control != NULL)
{
wchar_t *ua = var_InheritWide(aout, "user-agent");
&sys->session_events);
}
- LeaveCriticalSection(&sys->lock);
+ if (sys->volume != NULL)
+ { /* Get current values (_after_ changes notification registration) */
+ BOOL mute;
+ float level;
+
+ hr = ISimpleAudioVolume_GetMute(sys->volume, &mute);
+ if (FAILED(hr))
+ msg_Err(aout, "cannot get mute (error 0x%lx)", hr);
+ else
+ aout_MuteReport(aout, mute != FALSE);
+
+ hr = ISimpleAudioVolume_GetMasterVolume(sys->volume, &level);
+ if (FAILED(hr))
+ msg_Err(aout, "cannot get mute (error 0x%lx)", hr);
+ else
+ aout_VolumeReport(aout, level);
+ }
+
+ SetEvent(sys->device_ready);
+ /* Wait until device change or exit */
WaitForSingleObject(sys->device_changed, INFINITE);
- EnterCriticalSection(&sys->lock);
/* Deregister session control */
if (control != NULL)
&sys->session_events);
IAudioSessionControl_Release(control);
}
+
+ if (sys->volume != NULL)
+ ISimpleAudioVolume_Release(sys->volume);
}
/** MMDevice audio output thread.
aout_sys_t *sys = aout->sys;
EnterMTA();
- EnterCriticalSection(&sys->lock);
while (sys->it != NULL)
MMSession(aout, sys);
- LeaveCriticalSection(&sys->lock);
LeaveMTA();
return NULL;
}
+/*** Audio devices ***/
+static int DevicesEnum(audio_output_t *aout, char ***idp, char ***namep)
+{
+ aout_sys_t *sys = aout->sys;
+ HRESULT hr;
+ IMMDeviceCollection *devs;
+
+ hr = IMMDeviceEnumerator_EnumAudioEndpoints(sys->it, eRender,
+ DEVICE_STATE_ACTIVE, &devs);
+ if (FAILED(hr))
+ {
+ msg_Warn(aout, "cannot enumerate audio endpoints (error 0x%lx)", hr);
+ return -1;
+ }
+
+ UINT count;
+ hr = IMMDeviceCollection_GetCount(devs, &count);
+ if (FAILED(hr))
+ {
+ msg_Warn(aout, "cannot count audio endpoints (error 0x%lx)", hr);
+ count = 0;
+ }
+ else
+ msg_Dbg(aout, "Available Windows Audio devices:");
+
+ char **ids = xmalloc (count * sizeof (*ids));
+ char **names = xmalloc (count * sizeof (*names));
+ unsigned n = 0;
+
+ for (UINT i = 0; i < count; i++)
+ {
+ IMMDevice *dev;
+
+ hr = IMMDeviceCollection_Item(devs, i, &dev);
+ if (FAILED(hr))
+ continue;
+
+ /* Unique device ID */
+ LPWSTR devid;
+ hr = IMMDevice_GetId(dev, &devid);
+ if (FAILED(hr))
+ {
+ IMMDevice_Release(dev);
+ continue;
+ }
+ ids[n] = FromWide(devid);
+ names[n] = NULL;
+ CoTaskMemFree(devid);
+
+ /* User-readable device name */
+ IPropertyStore *props;
+ hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, &props);
+ if (SUCCEEDED(hr))
+ {
+ PROPVARIANT v;
+
+ PropVariantInit(&v);
+ hr = IPropertyStore_GetValue(props, &PKEY_Device_FriendlyName, &v);
+ if (SUCCEEDED(hr))
+ names[n] = FromWide(v.pwszVal);
+ PropVariantClear(&v);
+ IPropertyStore_Release(props);
+ }
+ IMMDevice_Release(dev);
+ if (names[n] == NULL)
+ names[n] = xstrdup(ids[n]);
+
+ msg_Dbg(aout, "%s (%s)", ids[n], names[n]);
+ n++;
+ }
+ IMMDeviceCollection_Release(devs);
+
+ *idp = ids;
+ *namep = names;
+ return n;
+}
+
/**
* Opens the selected audio output device.
*/
aout_sys_t *sys = aout->sys;
HRESULT hr;
- EnterCriticalSection(&sys->lock);
+ assert(sys->dev == NULL);
+
if (devid != NULL) /* Device selected explicitly */
{
msg_Dbg(aout, "using selected device %s", devid);
hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(sys->it, eRender,
eConsole, &sys->dev);
}
+ assert(sys->manager == NULL);
+ if (FAILED(hr))
+ {
+ msg_Err(aout, "cannot get device (error 0x%lx)", hr);
+ goto out;
+ }
/* Create session manager (for controls even w/o active audio client) */
+ void *pv;
+ hr = IMMDevice_Activate(sys->dev, &IID_IAudioSessionManager,
+ CLSCTX_ALL, NULL, &pv);
if (FAILED(hr))
- msg_Err(aout, "cannot get device (error 0x%lx)", hr);
+ msg_Err(aout, "cannot activate session manager (error 0x%lx)", hr);
else
+ sys->manager = pv;
+
+ /* Report actual device */
+ LPWSTR wdevid;
+ hr = IMMDevice_GetId(sys->dev, &wdevid);
+ if (SUCCEEDED(hr))
{
- void *pv;
- hr = IMMDevice_Activate(sys->dev, &IID_IAudioSessionManager,
- CLSCTX_ALL, NULL, &pv);
- if (FAILED(hr))
- msg_Err(aout, "cannot activate session manager (error 0x%lx)", hr);
- else
- sys->manager = pv;
+ char *id = FromWide(wdevid);
+ CoTaskMemFree(wdevid);
+ if (likely(id != NULL))
+ {
+ aout_DeviceReport(aout, id);
+ free(id);
+ }
}
- LeaveCriticalSection(&sys->lock);
-
+out:
SetEvent(sys->device_changed);
+ WaitForSingleObject(sys->device_ready, INFINITE);
return hr;
}
assert(sys->dev != NULL);
- EnterCriticalSection(&sys->lock);
if (sys->manager != NULL)
{
IAudioSessionManager_Release(sys->manager);
IMMDevice_Release(sys->dev);
sys->dev = NULL;
- LeaveCriticalSection(&sys->lock);
+}
+
+static int DeviceSelect(audio_output_t *aout, const char *id)
+{
+ aout_sys_t *sys = aout->sys;
+ HRESULT hr;
+
+ if (TryEnterMTA(aout))
+ return -1;
+
+ if (sys->dev != NULL)
+ CloseDevice(aout);
+
+ hr = OpenDevice(aout, id);
+ while (hr == AUDCLNT_E_DEVICE_INVALIDATED)
+ hr = OpenDevice(aout, NULL); /* Fallback to default device */
+ LeaveMTA();
+
+ if (sys->stream != NULL)
+ /* Request restart of stream with the new device */
+ aout_RestartRequest(aout, AOUT_RESTART_OUTPUT);
+ return FAILED(hr) ? -1 : 0;
+}
+
+/**
+ * Callback for aout_stream_t to create a stream on the device.
+ * This can instantiate an IAudioClient or IDirectSound(8) object.
+ */
+static HRESULT ActivateDevice(void *opaque, REFIID iid, PROPVARIANT *actparms,
+ void **restrict pv)
+{
+ IMMDevice *dev = opaque;
+
+ return IMMDevice_Activate(dev, iid, CLSCTX_ALL, actparms, pv);
}
static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
{
aout_sys_t *sys = aout->sys;
+ HRESULT hr;
assert (sys->stream == NULL);
- if (sys->dev == NULL)
+ /* Open the default device if required (to deal with restarts) */
+ if (sys->dev == NULL && FAILED(DeviceSelect(aout, NULL)))
return -1;
+ aout_stream_t *s = vlc_object_create(aout, sizeof (*s));
+ if (unlikely(s == NULL))
+ return -1;
+
+ s->owner.device = sys->dev;
+ s->owner.activate = ActivateDevice;
+
EnterMTA();
- EnterCriticalSection(&sys->lock);
- sys->stream = aout_stream_Start(aout, fmt, sys->dev, &GUID_VLC_AUD_OUT);
- LeaveCriticalSection(&sys->lock);
+ hr = aout_stream_Start(s, fmt, &GUID_VLC_AUD_OUT);
+ if (SUCCEEDED(hr))
+ sys->stream = s;
+ else
+ vlc_object_release(s);
LeaveMTA();
- return (sys->stream != NULL) ? 0 : -1;
+ return vlc_FromHR(aout, hr);
}
static void Stop(audio_output_t *aout)
assert (sys->stream != NULL);
EnterMTA();
- EnterCriticalSection(&sys->lock);
aout_stream_Stop(sys->stream);
- LeaveCriticalSection(&sys->lock);
LeaveMTA();
+ vlc_object_release(sys->stream);
sys->stream = NULL;
}
sys->aout = aout;
sys->stream = NULL;
sys->it = NULL;
+ sys->dev = NULL;
+ sys->manager = NULL;
sys->session_events.lpVtbl = &vlc_AudioSessionEvents;
sys->refs = 1;
- InitializeCriticalSection(&sys->lock);
sys->device_changed = CreateEvent(NULL, FALSE, FALSE, NULL);
- if (unlikely(sys->device_changed == NULL))
+ sys->device_ready = CreateEvent(NULL, FALSE, FALSE, NULL);
+ if (unlikely(sys->device_changed == NULL || sys->device_ready == NULL))
goto error;
/* Initialize MMDevice API */
goto error;
}
sys->it = pv;
- GetDevices(obj, sys->it);
if (vlc_clone(&sys->thread, MMThread, aout, VLC_THREAD_PRIORITY_LOW))
goto error;
+ WaitForSingleObject(sys->device_ready, INFINITE);
- /* Get a device to start with */
- do
- hr = OpenDevice(aout, NULL);
- while (hr == AUDCLNT_E_DEVICE_INVALIDATED);
+ DeviceSelect(aout, NULL); /* Get a device to start with */
LeaveMTA(); /* leave MTA after thread has entered MTA */
aout->start = Start;
aout->flush = Flush;
aout->volume_set = VolumeSet;
aout->mute_set = MuteSet;
- var_AddCallback (aout, "audio-device", DeviceChanged, NULL);
+ aout->device_enum = DevicesEnum;
+ aout->device_select = DeviceSelect;
return VLC_SUCCESS;
error:
IMMDeviceEnumerator_Release(sys->it);
LeaveMTA();
}
+ if (sys->device_ready != NULL)
+ CloseHandle(sys->device_ready);
if (sys->device_changed != NULL)
CloseHandle(sys->device_changed);
- DeleteCriticalSection(&sys->lock);
free(sys);
return VLC_EGENERIC;
}
audio_output_t *aout = (audio_output_t *)obj;
aout_sys_t *sys = aout->sys;
- var_DelCallback (aout, "audio-device", DeviceChanged, NULL);
- var_Destroy (aout, "audio-device");
-
EnterMTA(); /* enter MTA before thread leaves MTA */
- EnterCriticalSection(&sys->lock);
if (sys->dev != NULL)
CloseDevice(aout);
IMMDeviceEnumerator_Release(sys->it);
sys->it = NULL;
- LeaveCriticalSection(&sys->lock);
SetEvent(sys->device_changed);
vlc_join(sys->thread, NULL);
LeaveMTA();
+ CloseHandle(sys->device_ready);
CloseHandle(sys->device_changed);
- DeleteCriticalSection(&sys->lock);
free(sys);
}