2008-09-30 11:19:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
|
2018-01-21 03:32:30 +00:00
|
|
|
* Copyright (C) 2018 Centricular Ltd.
|
|
|
|
* Author: Nirbheek Chauhan <nirbheek@centricular.com>
|
2008-09-30 11:19:10 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2008-09-30 11:19:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-wasapisrc
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: wasapisrc
|
2008-09-30 11:19:10 +00:00
|
|
|
*
|
|
|
|
* Provides audio capture from the Windows Audio Session API available with
|
|
|
|
* Vista and newer.
|
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example pipelines
|
2008-09-30 11:19:10 +00:00
|
|
|
* |[
|
2013-03-26 14:22:16 +00:00
|
|
|
* gst-launch-1.0 -v wasapisrc ! fakesink
|
2008-09-30 11:19:10 +00:00
|
|
|
* ]| Capture from the default audio device and render to fakesink.
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2018-02-26 10:38:28 +00:00
|
|
|
* |[
|
|
|
|
* gst-launch-1.0 -v wasapisrc low-latency=true ! fakesink
|
|
|
|
* ]| Capture from the default audio device with the minimum possible latency and render to fakesink.
|
|
|
|
*
|
2008-09-30 11:19:10 +00:00
|
|
|
*/
|
2013-03-26 14:01:08 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
#include "gstwasapisrc.h"
|
|
|
|
|
2018-02-06 18:15:02 +00:00
|
|
|
#include <avrt.h>
|
|
|
|
|
2008-09-30 11:19:10 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_wasapi_src_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_wasapi_src_debug
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2018-01-24 02:50:38 +00:00
|
|
|
GST_STATIC_CAPS (GST_WASAPI_STATIC_CAPS));
|
2018-01-21 03:32:30 +00:00
|
|
|
|
2018-02-06 23:18:58 +00:00
|
|
|
#define DEFAULT_ROLE GST_WASAPI_DEVICE_ROLE_CONSOLE
|
2018-04-03 19:37:14 +00:00
|
|
|
#define DEFAULT_LOOPBACK FALSE
|
2018-02-06 23:18:58 +00:00
|
|
|
#define DEFAULT_EXCLUSIVE FALSE
|
|
|
|
#define DEFAULT_LOW_LATENCY FALSE
|
2018-02-26 10:25:19 +00:00
|
|
|
#define DEFAULT_AUDIOCLIENT3 FALSE
|
2018-04-18 09:33:19 +00:00
|
|
|
/* The clock provided by WASAPI is always off and causes buffers to be late
|
|
|
|
* very quickly on the sink. Disable pending further investigation. */
|
|
|
|
#define DEFAULT_PROVIDE_CLOCK FALSE
|
2018-01-21 03:32:30 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ROLE,
|
2018-02-06 18:10:49 +00:00
|
|
|
PROP_DEVICE,
|
2018-04-03 19:37:14 +00:00
|
|
|
PROP_LOOPBACK,
|
2018-02-06 23:18:58 +00:00
|
|
|
PROP_EXCLUSIVE,
|
2018-02-26 10:25:19 +00:00
|
|
|
PROP_LOW_LATENCY,
|
|
|
|
PROP_AUDIOCLIENT3
|
2018-01-21 03:32:30 +00:00
|
|
|
};
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
static void gst_wasapi_src_dispose (GObject * object);
|
|
|
|
static void gst_wasapi_src_finalize (GObject * object);
|
2018-01-21 03:32:30 +00:00
|
|
|
static void gst_wasapi_src_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_wasapi_src_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
static GstCaps *gst_wasapi_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
static gboolean gst_wasapi_src_open (GstAudioSrc * asrc);
|
|
|
|
static gboolean gst_wasapi_src_close (GstAudioSrc * asrc);
|
2016-03-04 06:50:26 +00:00
|
|
|
static gboolean gst_wasapi_src_prepare (GstAudioSrc * asrc,
|
|
|
|
GstAudioRingBufferSpec * spec);
|
2013-03-28 15:52:26 +00:00
|
|
|
static gboolean gst_wasapi_src_unprepare (GstAudioSrc * asrc);
|
2016-03-04 06:50:26 +00:00
|
|
|
static guint gst_wasapi_src_read (GstAudioSrc * asrc, gpointer data,
|
|
|
|
guint length, GstClockTime * timestamp);
|
2013-03-28 15:52:26 +00:00
|
|
|
static guint gst_wasapi_src_delay (GstAudioSrc * asrc);
|
|
|
|
static void gst_wasapi_src_reset (GstAudioSrc * asrc);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-08-02 12:54:02 +00:00
|
|
|
#if DEFAULT_PROVIDE_CLOCK
|
2008-09-30 11:19:10 +00:00
|
|
|
static GstClockTime gst_wasapi_src_get_time (GstClock * clock,
|
|
|
|
gpointer user_data);
|
2018-04-18 09:33:19 +00:00
|
|
|
#endif
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
#define gst_wasapi_src_parent_class parent_class
|
2013-03-28 15:52:26 +00:00
|
|
|
G_DEFINE_TYPE (GstWasapiSrc, gst_wasapi_src, GST_TYPE_AUDIO_SRC);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wasapi_src_class_init (GstWasapiSrcClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
|
2013-03-28 15:52:26 +00:00
|
|
|
GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
gobject_class->dispose = gst_wasapi_src_dispose;
|
|
|
|
gobject_class->finalize = gst_wasapi_src_finalize;
|
2018-01-21 03:32:30 +00:00
|
|
|
gobject_class->set_property = gst_wasapi_src_set_property;
|
|
|
|
gobject_class->get_property = gst_wasapi_src_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_ROLE,
|
|
|
|
g_param_spec_enum ("role", "Role",
|
|
|
|
"Role of the device: communications, multimedia, etc",
|
|
|
|
GST_WASAPI_DEVICE_TYPE_ROLE, DEFAULT_ROLE, G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DEVICE,
|
|
|
|
g_param_spec_string ("device", "Device",
|
|
|
|
"WASAPI playback device as a GUID string",
|
|
|
|
NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-04-03 19:37:14 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_LOOPBACK,
|
|
|
|
g_param_spec_boolean ("loopback", "Loopback recording",
|
|
|
|
"Open the sink device for loopback recording",
|
|
|
|
DEFAULT_LOOPBACK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2018-02-06 18:10:49 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_EXCLUSIVE,
|
|
|
|
g_param_spec_boolean ("exclusive", "Exclusive mode",
|
|
|
|
"Open the device in exclusive mode",
|
|
|
|
DEFAULT_EXCLUSIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2018-02-06 23:18:58 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_LOW_LATENCY,
|
|
|
|
g_param_spec_boolean ("low-latency", "Low latency",
|
2018-02-26 10:38:28 +00:00
|
|
|
"Optimize all settings for lowest latency. Always safe to enable.",
|
2018-02-06 23:18:58 +00:00
|
|
|
DEFAULT_LOW_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2018-02-26 10:25:19 +00:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_AUDIOCLIENT3,
|
|
|
|
g_param_spec_boolean ("use-audioclient3", "Use the AudioClient3 API",
|
|
|
|
"Whether to use the Windows 10 AudioClient3 API when available",
|
|
|
|
DEFAULT_AUDIOCLIENT3, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &src_template);
|
2013-03-26 14:22:16 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "WasapiSrc",
|
2019-02-14 10:54:25 +00:00
|
|
|
"Source/Audio/Hardware",
|
2013-03-26 14:22:16 +00:00
|
|
|
"Stream audio from an audio capture device through WASAPI",
|
2018-02-26 10:38:28 +00:00
|
|
|
"Nirbheek Chauhan <nirbheek@centricular.com>, "
|
2013-03-26 14:22:16 +00:00
|
|
|
"Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>");
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_wasapi_src_get_caps);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_wasapi_src_open);
|
|
|
|
gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_wasapi_src_close);
|
|
|
|
gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_wasapi_src_read);
|
|
|
|
gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_wasapi_src_prepare);
|
2016-03-04 06:50:26 +00:00
|
|
|
gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_wasapi_src_unprepare);
|
2013-03-28 15:52:26 +00:00
|
|
|
gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_wasapi_src_delay);
|
|
|
|
gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_wasapi_src_reset);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_wasapi_src_debug, "wasapisrc",
|
|
|
|
0, "Windows audio session API source");
|
2020-06-08 15:20:08 +00:00
|
|
|
|
|
|
|
gst_type_mark_as_plugin_api (GST_WASAPI_DEVICE_TYPE_ROLE, 0);
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-03-26 14:22:16 +00:00
|
|
|
gst_wasapi_src_init (GstWasapiSrc * self)
|
2008-09-30 11:19:10 +00:00
|
|
|
{
|
2018-08-02 12:54:02 +00:00
|
|
|
#if DEFAULT_PROVIDE_CLOCK
|
2013-03-28 15:52:26 +00:00
|
|
|
/* override with a custom clock */
|
|
|
|
if (GST_AUDIO_BASE_SRC (self)->clock)
|
|
|
|
gst_object_unref (GST_AUDIO_BASE_SRC (self)->clock);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
GST_AUDIO_BASE_SRC (self)->clock = gst_audio_clock_new ("GstWasapiSrcClock",
|
2010-07-08 07:56:43 +00:00
|
|
|
gst_wasapi_src_get_time, gst_object_ref (self),
|
|
|
|
(GDestroyNotify) gst_object_unref);
|
2018-04-18 09:33:19 +00:00
|
|
|
#endif
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-02-26 10:25:19 +00:00
|
|
|
self->role = DEFAULT_ROLE;
|
|
|
|
self->sharemode = AUDCLNT_SHAREMODE_SHARED;
|
2018-04-03 19:37:14 +00:00
|
|
|
self->loopback = DEFAULT_LOOPBACK;
|
2018-02-26 10:25:19 +00:00
|
|
|
self->low_latency = DEFAULT_LOW_LATENCY;
|
|
|
|
self->try_audioclient3 = DEFAULT_AUDIOCLIENT3;
|
2013-03-28 15:52:26 +00:00
|
|
|
self->event_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
|
2020-06-09 13:38:28 +00:00
|
|
|
self->cancellable = CreateEvent (NULL, TRUE, FALSE, NULL);
|
2018-04-09 23:39:42 +00:00
|
|
|
self->client_needs_restart = FALSE;
|
2019-11-25 15:49:59 +00:00
|
|
|
self->adapter = gst_adapter_new ();
|
2013-03-28 15:52:26 +00:00
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
/* Extra event handles used for loopback */
|
|
|
|
self->loopback_event_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
|
|
|
|
self->loopback_cancellable = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
|
|
|
2019-08-13 17:24:42 +00:00
|
|
|
CoInitializeEx (NULL, COINIT_MULTITHREADED);
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wasapi_src_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (object);
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
if (self->event_handle != NULL) {
|
|
|
|
CloseHandle (self->event_handle);
|
|
|
|
self->event_handle = NULL;
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 13:38:28 +00:00
|
|
|
if (self->cancellable != NULL) {
|
|
|
|
CloseHandle (self->cancellable);
|
|
|
|
self->cancellable = NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-24 02:50:13 +00:00
|
|
|
if (self->client_clock != NULL) {
|
|
|
|
IUnknown_Release (self->client_clock);
|
|
|
|
self->client_clock = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->client != NULL) {
|
|
|
|
IUnknown_Release (self->client);
|
|
|
|
self->client = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->capture_client != NULL) {
|
|
|
|
IUnknown_Release (self->capture_client);
|
|
|
|
self->capture_client = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
if (self->loopback_client != NULL) {
|
|
|
|
IUnknown_Release (self->loopback_client);
|
|
|
|
self->loopback_client = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->loopback_event_handle != NULL) {
|
|
|
|
CloseHandle (self->loopback_event_handle);
|
|
|
|
self->loopback_event_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->loopback_cancellable != NULL) {
|
|
|
|
CloseHandle (self->loopback_cancellable);
|
|
|
|
self->loopback_cancellable = NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wasapi_src_finalize (GObject * object)
|
|
|
|
{
|
2018-01-24 02:50:13 +00:00
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (object);
|
|
|
|
|
2018-05-22 20:12:34 +00:00
|
|
|
CoTaskMemFree (self->mix_format);
|
|
|
|
self->mix_format = NULL;
|
2018-01-24 02:50:13 +00:00
|
|
|
|
2008-09-30 11:19:10 +00:00
|
|
|
CoUninitialize ();
|
|
|
|
|
2018-01-24 02:50:13 +00:00
|
|
|
g_clear_pointer (&self->cached_caps, gst_caps_unref);
|
2018-01-24 02:50:38 +00:00
|
|
|
g_clear_pointer (&self->positions, g_free);
|
2018-02-06 18:07:19 +00:00
|
|
|
g_clear_pointer (&self->device_strid, g_free);
|
2018-01-24 02:50:13 +00:00
|
|
|
|
2019-11-25 15:49:59 +00:00
|
|
|
g_object_unref (self->adapter);
|
|
|
|
self->adapter = NULL;
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wasapi_src_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ROLE:
|
|
|
|
self->role = gst_wasapi_device_role_to_erole (g_value_get_enum (value));
|
|
|
|
break;
|
|
|
|
case PROP_DEVICE:
|
|
|
|
{
|
2018-01-24 02:50:13 +00:00
|
|
|
const gchar *device = g_value_get_string (value);
|
2018-02-06 18:07:19 +00:00
|
|
|
g_free (self->device_strid);
|
|
|
|
self->device_strid =
|
2018-01-24 02:50:13 +00:00
|
|
|
device ? g_utf8_to_utf16 (device, -1, NULL, NULL, NULL) : NULL;
|
2018-01-21 03:32:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-04-03 19:37:14 +00:00
|
|
|
case PROP_LOOPBACK:
|
|
|
|
self->loopback = g_value_get_boolean (value);
|
|
|
|
break;
|
2018-02-06 18:10:49 +00:00
|
|
|
case PROP_EXCLUSIVE:
|
|
|
|
self->sharemode = g_value_get_boolean (value)
|
|
|
|
? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED;
|
|
|
|
break;
|
2018-02-06 23:18:58 +00:00
|
|
|
case PROP_LOW_LATENCY:
|
|
|
|
self->low_latency = g_value_get_boolean (value);
|
|
|
|
break;
|
2018-02-26 10:25:19 +00:00
|
|
|
case PROP_AUDIOCLIENT3:
|
|
|
|
self->try_audioclient3 = g_value_get_boolean (value);
|
|
|
|
break;
|
2018-01-21 03:32:30 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wasapi_src_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ROLE:
|
|
|
|
g_value_set_enum (value, gst_wasapi_erole_to_device_role (self->role));
|
|
|
|
break;
|
|
|
|
case PROP_DEVICE:
|
2018-02-06 18:07:19 +00:00
|
|
|
g_value_take_string (value, self->device_strid ?
|
|
|
|
g_utf16_to_utf8 (self->device_strid, -1, NULL, NULL, NULL) : NULL);
|
2018-01-21 03:32:30 +00:00
|
|
|
break;
|
2018-04-03 19:37:14 +00:00
|
|
|
case PROP_LOOPBACK:
|
|
|
|
g_value_set_boolean (value, self->loopback);
|
|
|
|
break;
|
2018-02-06 18:10:49 +00:00
|
|
|
case PROP_EXCLUSIVE:
|
|
|
|
g_value_set_boolean (value,
|
|
|
|
self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE);
|
|
|
|
break;
|
2018-02-06 23:18:58 +00:00
|
|
|
case PROP_LOW_LATENCY:
|
|
|
|
g_value_set_boolean (value, self->low_latency);
|
|
|
|
break;
|
2018-02-26 10:25:19 +00:00
|
|
|
case PROP_AUDIOCLIENT3:
|
|
|
|
g_value_set_boolean (value, self->try_audioclient3);
|
|
|
|
break;
|
2018-01-21 03:32:30 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 10:25:19 +00:00
|
|
|
static gboolean
|
|
|
|
gst_wasapi_src_can_audioclient3 (GstWasapiSrc * self)
|
|
|
|
{
|
2019-11-02 15:29:02 +00:00
|
|
|
return (self->sharemode == AUDCLNT_SHAREMODE_SHARED &&
|
|
|
|
self->try_audioclient3 && gst_wasapi_util_have_audioclient3 ());
|
2018-02-26 10:25:19 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_wasapi_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
|
2008-09-30 11:19:10 +00:00
|
|
|
{
|
2018-01-21 03:32:30 +00:00
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (bsrc);
|
|
|
|
WAVEFORMATEX *format = NULL;
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "entering get caps");
|
|
|
|
|
|
|
|
if (self->cached_caps) {
|
|
|
|
caps = gst_caps_ref (self->cached_caps);
|
|
|
|
} else {
|
|
|
|
GstCaps *template_caps;
|
2018-02-06 18:10:49 +00:00
|
|
|
gboolean ret;
|
2018-01-21 03:32:30 +00:00
|
|
|
|
|
|
|
template_caps = gst_pad_get_pad_template_caps (bsrc->srcpad);
|
|
|
|
|
2018-04-09 11:46:38 +00:00
|
|
|
if (!self->client) {
|
|
|
|
caps = template_caps;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-01-21 03:32:30 +00:00
|
|
|
|
2018-02-06 18:10:49 +00:00
|
|
|
ret = gst_wasapi_util_get_device_format (GST_ELEMENT (self),
|
|
|
|
self->sharemode, self->device, self->client, &format);
|
|
|
|
if (!ret) {
|
2018-01-21 03:32:30 +00:00
|
|
|
GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL),
|
2018-02-06 18:10:49 +00:00
|
|
|
("failed to detect format"));
|
2018-04-09 11:46:38 +00:00
|
|
|
gst_caps_unref (template_caps);
|
|
|
|
return NULL;
|
2018-01-21 03:32:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 02:50:38 +00:00
|
|
|
gst_wasapi_util_parse_waveformatex ((WAVEFORMATEXTENSIBLE *) format,
|
|
|
|
template_caps, &caps, &self->positions);
|
2018-01-21 03:32:30 +00:00
|
|
|
if (caps == NULL) {
|
|
|
|
GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL), ("unknown format"));
|
2018-04-09 11:46:38 +00:00
|
|
|
gst_caps_unref (template_caps);
|
|
|
|
return NULL;
|
2018-01-21 03:32:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 02:50:38 +00:00
|
|
|
{
|
|
|
|
gchar *pos_str = gst_audio_channel_positions_to_string (self->positions,
|
|
|
|
format->nChannels);
|
|
|
|
GST_INFO_OBJECT (self, "positions are: %s", pos_str);
|
|
|
|
g_free (pos_str);
|
|
|
|
}
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
self->mix_format = format;
|
|
|
|
gst_caps_replace (&self->cached_caps, caps);
|
|
|
|
gst_caps_unref (template_caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
GstCaps *filtered =
|
|
|
|
gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = filtered;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2018-04-09 11:46:38 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "returning caps %" GST_PTR_FORMAT, caps);
|
2018-01-21 03:32:30 +00:00
|
|
|
return caps;
|
2013-03-28 15:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_wasapi_src_open (GstAudioSrc * asrc)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
|
|
|
gboolean res = FALSE;
|
2016-03-04 06:50:26 +00:00
|
|
|
IAudioClient *client = NULL;
|
2018-02-06 18:10:49 +00:00
|
|
|
IMMDevice *device = NULL;
|
2020-09-18 15:26:35 +00:00
|
|
|
IMMDevice *loopback_device = NULL;
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
if (self->client)
|
|
|
|
return TRUE;
|
|
|
|
|
2018-01-24 02:50:13 +00:00
|
|
|
/* FIXME: Switching the default device does not switch the stream to it,
|
|
|
|
* even if the old device was unplugged. We need to handle this somehow.
|
|
|
|
* For example, perhaps we should automatically switch to the new device if
|
|
|
|
* the default device is changed and a device isn't explicitly selected. */
|
2018-04-03 19:37:14 +00:00
|
|
|
if (!gst_wasapi_util_get_device_client (GST_ELEMENT (self),
|
|
|
|
self->loopback ? eRender : eCapture, self->role, self->device_strid,
|
|
|
|
&device, &client)) {
|
2018-02-06 18:07:19 +00:00
|
|
|
if (!self->device_strid)
|
2018-01-21 03:32:30 +00:00
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
|
|
|
|
("Failed to get default device"));
|
|
|
|
else
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
|
2018-02-06 18:07:19 +00:00
|
|
|
("Failed to open device %S", self->device_strid));
|
2013-03-28 15:52:26 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
/* An oddness of wasapi loopback feature is that capture client will not
|
|
|
|
* provide any audio data if there is no outputting sound.
|
|
|
|
* To workaround this problem, probably we can add timeout around loop
|
|
|
|
* in this case but it's glitch prone. So, instead of timeout,
|
|
|
|
* we will keep pusing silence data to into wasapi client so that make audio
|
|
|
|
* client report audio data in any case
|
|
|
|
*/
|
|
|
|
if (!gst_wasapi_util_get_device_client (GST_ELEMENT (self),
|
|
|
|
eRender, self->role, self->device_strid,
|
|
|
|
&loopback_device, &self->loopback_client)) {
|
|
|
|
if (!self->device_strid)
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
|
|
|
|
("Failed to get default device for loopback"));
|
|
|
|
else
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
|
|
|
|
("Failed to open device %S", self->device_strid));
|
|
|
|
goto beach;
|
|
|
|
|
|
|
|
/* no need to hold this object */
|
|
|
|
IUnknown_Release (loopback_device);
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
self->client = client;
|
2018-02-06 18:10:49 +00:00
|
|
|
self->device = device;
|
2013-03-28 15:52:26 +00:00
|
|
|
res = TRUE;
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
beach:
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
return res;
|
|
|
|
}
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
static gboolean
|
|
|
|
gst_wasapi_src_close (GstAudioSrc * asrc)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
|
|
|
|
2018-02-06 18:10:49 +00:00
|
|
|
if (self->device != NULL) {
|
|
|
|
IUnknown_Release (self->device);
|
|
|
|
self->device = NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
if (self->client != NULL) {
|
|
|
|
IUnknown_Release (self->client);
|
|
|
|
self->client = NULL;
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
2013-03-28 15:52:26 +00:00
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
if (self->loopback_client != NULL) {
|
|
|
|
IUnknown_Release (self->loopback_client);
|
|
|
|
self->loopback_client = NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
return TRUE;
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
static gpointer
|
|
|
|
gst_wasapi_src_loopback_silence_feeding_thread (GstWasapiSrc * self)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
UINT32 buffer_frames;
|
|
|
|
gboolean res G_GNUC_UNUSED = FALSE;
|
|
|
|
BYTE *data;
|
|
|
|
DWORD dwWaitResult;
|
|
|
|
HANDLE event_handle[2];
|
|
|
|
UINT32 padding;
|
|
|
|
UINT32 n_frames;
|
|
|
|
|
|
|
|
/* NOTE: if this task cause glitch, we need to consider thread priority
|
|
|
|
* adjusing. See gstaudioutilsprivate.c (e.g., AvSetMmThreadCharacteristics)
|
|
|
|
* for this context */
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "Run loopback silence feeding thread");
|
|
|
|
|
|
|
|
event_handle[0] = self->loopback_event_handle;
|
|
|
|
event_handle[1] = self->loopback_cancellable;
|
|
|
|
|
|
|
|
hr = IAudioClient_GetBufferSize (self->loopback_client, &buffer_frames);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioClient::GetBufferSize, beach);
|
|
|
|
|
|
|
|
hr = IAudioClient_SetEventHandle (self->loopback_client,
|
|
|
|
self->loopback_event_handle);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioClient::SetEventHandle, beach);
|
|
|
|
|
|
|
|
/* To avoid start-up glitches, before starting the streaming, we fill the
|
|
|
|
* buffer with silence as recommended by the documentation:
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/desktop/dd370879%28v=vs.85%29.aspx */
|
|
|
|
hr = IAudioRenderClient_GetBuffer (self->loopback_render_client,
|
|
|
|
buffer_frames, &data);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioRenderClient::GetBuffer, beach);
|
|
|
|
|
|
|
|
hr = IAudioRenderClient_ReleaseBuffer (self->loopback_render_client,
|
|
|
|
buffer_frames, AUDCLNT_BUFFERFLAGS_SILENT);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioRenderClient::ReleaseBuffer, beach);
|
|
|
|
|
|
|
|
hr = IAudioClient_Start (self->loopback_client);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioClock::Start, beach);
|
|
|
|
|
|
|
|
/* There is an OS bug prior to Windows 10, that is loopback capture client
|
|
|
|
* will not receive event (in case of event-driven mode).
|
|
|
|
* A guide for workaround this case is that signal it whenever render client
|
|
|
|
* writes data.
|
|
|
|
* See https://docs.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient-initialize
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Signal for read thread to wakeup */
|
|
|
|
SetEvent (self->event_handle);
|
|
|
|
|
|
|
|
/* Ok, now we are ready for running for feeding silence data */
|
|
|
|
while (1) {
|
|
|
|
dwWaitResult = WaitForMultipleObjects (2, event_handle, FALSE, INFINITE);
|
|
|
|
if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_OBJECT_0 + 1) {
|
|
|
|
GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
|
|
|
|
(guint) dwWaitResult);
|
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stopping was requested from unprepare() */
|
|
|
|
if (dwWaitResult == WAIT_OBJECT_0 + 1) {
|
|
|
|
GST_DEBUG_OBJECT (self, "operation was cancelled");
|
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = IAudioClient_GetCurrentPadding (self->loopback_client, &padding);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioClock::Start, stop);
|
|
|
|
|
|
|
|
if (buffer_frames < padding) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Current padding %d is too large (buffer size %d)",
|
|
|
|
padding, buffer_frames);
|
|
|
|
n_frames = 0;
|
|
|
|
} else {
|
|
|
|
n_frames = buffer_frames - padding;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = IAudioRenderClient_GetBuffer (self->loopback_render_client, n_frames,
|
|
|
|
&data);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioRenderClient::GetBuffer, stop);
|
|
|
|
|
|
|
|
hr = IAudioRenderClient_ReleaseBuffer (self->loopback_render_client,
|
|
|
|
n_frames, AUDCLNT_BUFFERFLAGS_SILENT);
|
|
|
|
HR_FAILED_GOTO (hr, IAudioRenderClient::ReleaseBuffer, stop);
|
|
|
|
|
|
|
|
/* Signal for read thread to wakeup */
|
|
|
|
SetEvent (self->event_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
stop:
|
|
|
|
IAudioClient_Stop (self->loopback_client);
|
|
|
|
|
|
|
|
beach:
|
|
|
|
GST_INFO_OBJECT (self, "Terminate loopback silence feeding thread");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-30 11:19:10 +00:00
|
|
|
static gboolean
|
2013-03-28 15:52:26 +00:00
|
|
|
gst_wasapi_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
|
2008-09-30 11:19:10 +00:00
|
|
|
{
|
2013-03-28 15:52:26 +00:00
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
2008-09-30 11:19:10 +00:00
|
|
|
gboolean res = FALSE;
|
2018-02-14 06:43:36 +00:00
|
|
|
REFERENCE_TIME latency_rt;
|
|
|
|
guint bpf, rate, devicep_frames, buffer_frames;
|
2008-09-30 11:19:10 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
2019-08-13 17:24:42 +00:00
|
|
|
CoInitializeEx (NULL, COINIT_MULTITHREADED);
|
2018-04-16 14:05:07 +00:00
|
|
|
|
2018-02-26 10:25:19 +00:00
|
|
|
if (gst_wasapi_src_can_audioclient3 (self)) {
|
2018-02-14 06:43:36 +00:00
|
|
|
if (!gst_wasapi_util_initialize_audioclient3 (GST_ELEMENT (self), spec,
|
|
|
|
(IAudioClient3 *) self->client, self->mix_format, self->low_latency,
|
2018-04-03 19:37:14 +00:00
|
|
|
self->loopback, &devicep_frames))
|
2018-02-14 06:43:36 +00:00
|
|
|
goto beach;
|
2018-02-06 18:10:49 +00:00
|
|
|
} else {
|
2018-02-14 06:43:36 +00:00
|
|
|
if (!gst_wasapi_util_initialize_audioclient (GST_ELEMENT (self), spec,
|
|
|
|
self->client, self->mix_format, self->sharemode, self->low_latency,
|
2018-04-03 19:37:14 +00:00
|
|
|
self->loopback, &devicep_frames))
|
2018-02-14 06:43:36 +00:00
|
|
|
goto beach;
|
2018-02-06 18:10:49 +00:00
|
|
|
}
|
2018-02-06 23:18:58 +00:00
|
|
|
|
2018-02-14 06:43:36 +00:00
|
|
|
bpf = GST_AUDIO_INFO_BPF (&spec->info);
|
|
|
|
rate = GST_AUDIO_INFO_RATE (&spec->info);
|
2013-03-28 15:52:26 +00:00
|
|
|
|
2018-01-30 22:21:47 +00:00
|
|
|
/* Total size in frames of the allocated buffer that we will read from */
|
|
|
|
hr = IAudioClient_GetBufferSize (self->client, &buffer_frames);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_GOTO (hr, IAudioClient::GetBufferSize, beach);
|
2018-01-30 22:21:47 +00:00
|
|
|
|
2018-02-14 06:43:36 +00:00
|
|
|
GST_INFO_OBJECT (self, "buffer size is %i frames, device period is %i "
|
|
|
|
"frames, bpf is %i bytes, rate is %i Hz", buffer_frames,
|
|
|
|
devicep_frames, bpf, rate);
|
2018-01-30 22:21:47 +00:00
|
|
|
|
2018-02-14 06:43:36 +00:00
|
|
|
/* Actual latency-time/buffer-time will be different now */
|
|
|
|
spec->segsize = devicep_frames * bpf;
|
2018-02-06 18:10:49 +00:00
|
|
|
|
|
|
|
/* We need a minimum of 2 segments to ensure glitch-free playback */
|
2019-05-01 00:25:12 +00:00
|
|
|
spec->segtotal = MAX (buffer_frames * bpf / spec->segsize, 2);
|
2018-01-30 22:21:47 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (self, "segsize is %i, segtotal is %i", spec->segsize,
|
|
|
|
spec->segtotal);
|
|
|
|
|
|
|
|
/* Get WASAPI latency for logging */
|
2013-03-28 15:52:26 +00:00
|
|
|
hr = IAudioClient_GetStreamLatency (self->client, &latency_rt);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_GOTO (hr, IAudioClient::GetStreamLatency, beach);
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
GST_INFO_OBJECT (self, "wasapi stream latency: %" G_GINT64_FORMAT " (%"
|
|
|
|
G_GINT64_FORMAT " ms)", latency_rt, latency_rt / 10000);
|
2013-03-28 15:52:26 +00:00
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
/* Set the event handler which will trigger reads */
|
2013-03-28 15:52:26 +00:00
|
|
|
hr = IAudioClient_SetEventHandle (self->client, self->event_handle);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_GOTO (hr, IAudioClient::SetEventHandle, beach);
|
2013-03-28 15:52:26 +00:00
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
/* Get the clock and the clock freq */
|
2013-03-28 15:52:26 +00:00
|
|
|
if (!gst_wasapi_util_get_clock (GST_ELEMENT (self), self->client,
|
2018-02-14 03:57:31 +00:00
|
|
|
&self->client_clock))
|
2008-09-30 11:19:10 +00:00
|
|
|
goto beach;
|
|
|
|
|
2018-02-08 08:57:43 +00:00
|
|
|
hr = IAudioClock_GetFrequency (self->client_clock, &self->client_clock_freq);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_GOTO (hr, IAudioClock::GetFrequency, beach);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-04-18 09:33:19 +00:00
|
|
|
GST_INFO_OBJECT (self, "wasapi clock freq is %" G_GUINT64_FORMAT,
|
|
|
|
self->client_clock_freq);
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
/* Get capture source client and start it up */
|
2013-03-28 15:52:26 +00:00
|
|
|
if (!gst_wasapi_util_get_capture_client (GST_ELEMENT (self), self->client,
|
2018-02-08 08:57:43 +00:00
|
|
|
&self->capture_client)) {
|
2008-09-30 11:19:10 +00:00
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
/* In case loopback, spawn another dedicated thread for feeding silence data
|
|
|
|
* into wasapi render client */
|
|
|
|
if (self->loopback) {
|
|
|
|
/* don't need to be audioclient3 or low-latency since we will keep pushing
|
|
|
|
* silence data which is not varying over entire playback */
|
|
|
|
if (!gst_wasapi_util_initialize_audioclient (GST_ELEMENT (self), spec,
|
|
|
|
self->loopback_client, self->mix_format, self->sharemode,
|
|
|
|
FALSE, FALSE, &devicep_frames))
|
|
|
|
goto beach;
|
|
|
|
|
|
|
|
if (!gst_wasapi_util_get_render_client (GST_ELEMENT (self),
|
|
|
|
self->loopback_client, &self->loopback_render_client)) {
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->loopback_thread = g_thread_new ("wasapi-loopback",
|
|
|
|
(GThreadFunc) gst_wasapi_src_loopback_silence_feeding_thread, self);
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
hr = IAudioClient_Start (self->client);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_GOTO (hr, IAudioClock::Start, beach);
|
2019-01-04 13:06:43 +00:00
|
|
|
self->client_needs_restart = FALSE;
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-02-08 06:02:32 +00:00
|
|
|
gst_audio_ring_buffer_set_channel_positions (GST_AUDIO_BASE_SRC
|
|
|
|
(self)->ringbuffer, self->positions);
|
2018-01-24 02:50:38 +00:00
|
|
|
|
2008-09-30 11:19:10 +00:00
|
|
|
res = TRUE;
|
2020-06-09 13:38:28 +00:00
|
|
|
|
|
|
|
/* reset cancellable event handle */
|
|
|
|
ResetEvent (self->cancellable);
|
|
|
|
|
2008-09-30 11:19:10 +00:00
|
|
|
beach:
|
2020-06-09 13:38:28 +00:00
|
|
|
|
2018-02-08 08:57:43 +00:00
|
|
|
/* unprepare() is not called if prepare() fails, but we want it to be, so call
|
|
|
|
* it manually when needed */
|
|
|
|
if (!res)
|
|
|
|
gst_wasapi_src_unprepare (asrc);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-03-28 15:52:26 +00:00
|
|
|
gst_wasapi_src_unprepare (GstAudioSrc * asrc)
|
2008-09-30 11:19:10 +00:00
|
|
|
{
|
2013-03-28 15:52:26 +00:00
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
if (self->client != NULL) {
|
|
|
|
IAudioClient_Stop (self->client);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->capture_client != NULL) {
|
|
|
|
IUnknown_Release (self->capture_client);
|
|
|
|
self->capture_client = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->client_clock != NULL) {
|
|
|
|
IUnknown_Release (self->client_clock);
|
|
|
|
self->client_clock = NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:26:35 +00:00
|
|
|
if (self->loopback_thread) {
|
|
|
|
GST_DEBUG_OBJECT (self, "loopback task thread is stopping");
|
|
|
|
|
|
|
|
SetEvent (self->loopback_cancellable);
|
|
|
|
|
|
|
|
g_thread_join (self->loopback_thread);
|
|
|
|
self->loopback_thread = NULL;
|
|
|
|
ResetEvent (self->loopback_cancellable);
|
|
|
|
GST_DEBUG_OBJECT (self, "loopback task thread has been stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->loopback_render_client != NULL) {
|
|
|
|
IUnknown_Release (self->loopback_render_client);
|
|
|
|
self->loopback_render_client = NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:57:43 +00:00
|
|
|
self->client_clock_freq = 0;
|
|
|
|
|
2018-04-16 14:05:07 +00:00
|
|
|
CoUninitialize ();
|
|
|
|
|
2008-09-30 11:19:10 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:26 +00:00
|
|
|
static guint
|
|
|
|
gst_wasapi_src_read (GstAudioSrc * asrc, gpointer data, guint length,
|
|
|
|
GstClockTime * timestamp)
|
2008-09-30 11:19:10 +00:00
|
|
|
{
|
2013-03-28 15:52:26 +00:00
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
2008-09-30 11:19:10 +00:00
|
|
|
HRESULT hr;
|
2018-01-21 03:32:30 +00:00
|
|
|
gint16 *from = NULL;
|
|
|
|
guint wanted = length;
|
2019-11-25 15:46:05 +00:00
|
|
|
guint bpf;
|
2018-01-21 03:32:30 +00:00
|
|
|
DWORD flags;
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-04-09 23:39:42 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
if (self->client_needs_restart) {
|
|
|
|
hr = IAudioClient_Start (self->client);
|
2019-01-14 20:33:23 +00:00
|
|
|
HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioClient::Start, self,
|
|
|
|
GST_OBJECT_UNLOCK (self); goto err);
|
2018-04-09 23:39:42 +00:00
|
|
|
self->client_needs_restart = FALSE;
|
2020-06-09 13:38:28 +00:00
|
|
|
ResetEvent (self->cancellable);
|
2019-11-25 15:49:59 +00:00
|
|
|
gst_adapter_clear (self->adapter);
|
2018-04-09 23:39:42 +00:00
|
|
|
}
|
2019-11-25 15:46:05 +00:00
|
|
|
|
|
|
|
bpf = self->mix_format->nBlockAlign;
|
2018-04-09 23:39:42 +00:00
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
2019-11-25 15:49:59 +00:00
|
|
|
/* If we've accumulated enough data, return it immediately */
|
|
|
|
if (gst_adapter_available (self->adapter) >= wanted) {
|
|
|
|
memcpy (data, gst_adapter_map (self->adapter, wanted), wanted);
|
|
|
|
gst_adapter_flush (self->adapter, wanted);
|
|
|
|
GST_DEBUG_OBJECT (self, "Adapter has enough data, returning %i", wanted);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
while (wanted > 0) {
|
2018-04-09 23:43:17 +00:00
|
|
|
DWORD dwWaitResult;
|
2019-11-25 15:49:59 +00:00
|
|
|
guint got_frames, avail_frames, n_frames, want_frames, read_len;
|
2020-06-09 13:38:28 +00:00
|
|
|
HANDLE event_handle[2];
|
|
|
|
|
|
|
|
event_handle[0] = self->event_handle;
|
|
|
|
event_handle[1] = self->cancellable;
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
/* Wait for data to become available */
|
2020-06-09 13:38:28 +00:00
|
|
|
dwWaitResult = WaitForMultipleObjects (2, event_handle, FALSE, INFINITE);
|
|
|
|
if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_OBJECT_0 + 1) {
|
2018-04-09 23:43:17 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
|
|
|
|
(guint) dwWaitResult);
|
2019-01-14 20:33:23 +00:00
|
|
|
goto err;
|
2018-04-09 23:43:17 +00:00
|
|
|
}
|
2008-09-30 11:19:10 +00:00
|
|
|
|
2020-06-09 13:38:28 +00:00
|
|
|
/* ::reset was requested */
|
|
|
|
if (dwWaitResult == WAIT_OBJECT_0 + 1) {
|
|
|
|
GST_DEBUG_OBJECT (self, "operation was cancelled");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-21 03:32:30 +00:00
|
|
|
hr = IAudioCaptureClient_GetBuffer (self->capture_client,
|
2019-11-25 15:49:59 +00:00
|
|
|
(BYTE **) & from, &got_frames, &flags, NULL, NULL);
|
2018-01-21 03:32:30 +00:00
|
|
|
if (hr != S_OK) {
|
2019-01-14 20:33:23 +00:00
|
|
|
if (hr == AUDCLNT_S_BUFFER_EMPTY) {
|
|
|
|
gchar *msg = gst_wasapi_util_hresult_to_string (hr);
|
2018-01-24 02:50:13 +00:00
|
|
|
GST_WARNING_OBJECT (self, "IAudioCaptureClient::GetBuffer failed: %s"
|
2018-02-06 18:26:41 +00:00
|
|
|
", retrying", msg);
|
2019-01-14 20:33:23 +00:00
|
|
|
g_free (msg);
|
|
|
|
length = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioCaptureClient::GetBuffer, self,
|
|
|
|
goto err);
|
2018-01-21 03:32:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 15:55:43 +00:00
|
|
|
if (G_UNLIKELY (flags != 0)) {
|
|
|
|
/* https://docs.microsoft.com/en-us/windows/win32/api/audioclient/ne-audioclient-_audclnt_bufferflags */
|
|
|
|
if (flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
|
|
|
|
GST_DEBUG_OBJECT (self, "WASAPI reported discontinuity (glitch?)");
|
|
|
|
if (flags & AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR)
|
|
|
|
GST_DEBUG_OBJECT (self, "WASAPI reported a timestamp error");
|
|
|
|
}
|
2018-01-21 03:32:30 +00:00
|
|
|
|
2019-11-25 15:49:59 +00:00
|
|
|
/* Copy all the frames we got into the adapter, and then extract at most
|
|
|
|
* @wanted size of frames from it. This helps when ::GetBuffer returns more
|
2019-11-26 06:09:32 +00:00
|
|
|
* data than we can handle right now. */
|
2019-11-25 15:49:59 +00:00
|
|
|
{
|
|
|
|
GstBuffer *tmp = gst_buffer_new_allocate (NULL, got_frames * bpf, NULL);
|
2019-11-26 06:09:32 +00:00
|
|
|
/* If flags has AUDCLNT_BUFFERFLAGS_SILENT, we will ignore the actual
|
|
|
|
* data and write out silence, see:
|
|
|
|
* https://docs.microsoft.com/en-us/windows/win32/api/audioclient/ne-audioclient-_audclnt_bufferflags */
|
|
|
|
if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
|
|
|
|
memset (from, 0, got_frames * bpf);
|
2019-11-25 15:49:59 +00:00
|
|
|
gst_buffer_fill (tmp, 0, from, got_frames * bpf);
|
|
|
|
gst_adapter_push (self->adapter, tmp);
|
|
|
|
}
|
2018-01-21 03:32:30 +00:00
|
|
|
|
2019-11-25 15:49:59 +00:00
|
|
|
/* Release all captured buffers; we copied them above */
|
|
|
|
hr = IAudioCaptureClient_ReleaseBuffer (self->capture_client, got_frames);
|
|
|
|
from = NULL;
|
|
|
|
HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioCaptureClient::ReleaseBuffer, self,
|
|
|
|
goto err);
|
|
|
|
|
|
|
|
want_frames = wanted / bpf;
|
|
|
|
avail_frames = gst_adapter_available (self->adapter) / bpf;
|
2018-01-21 03:32:30 +00:00
|
|
|
|
|
|
|
/* Only copy data that will fit into the allocated buffer of size @length */
|
2019-11-25 15:49:59 +00:00
|
|
|
n_frames = MIN (avail_frames, want_frames);
|
2019-11-25 15:46:05 +00:00
|
|
|
read_len = n_frames * bpf;
|
2018-01-21 03:32:30 +00:00
|
|
|
|
2019-11-25 15:49:59 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "frames captured: %i (%i bytes), "
|
|
|
|
"can read: %i (%i bytes), will read: %i (%i bytes), "
|
|
|
|
"adapter has: %i (%i bytes)", got_frames, got_frames * bpf, want_frames,
|
|
|
|
wanted, n_frames, read_len, avail_frames, avail_frames * bpf);
|
2018-01-21 03:32:30 +00:00
|
|
|
|
2019-11-25 15:49:59 +00:00
|
|
|
memcpy (data, gst_adapter_map (self->adapter, read_len), read_len);
|
|
|
|
gst_adapter_flush (self->adapter, read_len);
|
2018-01-21 03:32:30 +00:00
|
|
|
wanted -= read_len;
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-14 20:33:23 +00:00
|
|
|
out:
|
2013-03-28 15:52:26 +00:00
|
|
|
return length;
|
2019-01-14 20:33:23 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
length = -1;
|
|
|
|
goto out;
|
2013-03-28 15:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gst_wasapi_src_delay (GstAudioSrc * asrc)
|
|
|
|
{
|
2018-01-21 03:32:30 +00:00
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
|
|
|
guint delay = 0;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
hr = IAudioClient_GetCurrentPadding (self->client, &delay);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_RET (hr, IAudioClock::GetCurrentPadding, 0);
|
2018-01-21 03:32:30 +00:00
|
|
|
|
|
|
|
return delay;
|
2013-03-28 15:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wasapi_src_reset (GstAudioSrc * asrc)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
|
|
|
|
HRESULT hr;
|
|
|
|
|
2018-02-14 03:57:31 +00:00
|
|
|
if (!self->client)
|
|
|
|
return;
|
2013-03-28 15:52:26 +00:00
|
|
|
|
2020-06-09 13:38:28 +00:00
|
|
|
SetEvent (self->cancellable);
|
|
|
|
|
2018-04-09 23:39:42 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
2018-02-14 03:57:31 +00:00
|
|
|
hr = IAudioClient_Stop (self->client);
|
2020-07-15 08:39:33 +00:00
|
|
|
HR_FAILED_AND (hr, IAudioClock::Stop, goto err);
|
2018-02-14 03:57:31 +00:00
|
|
|
|
|
|
|
hr = IAudioClient_Reset (self->client);
|
2020-07-15 08:39:33 +00:00
|
|
|
HR_FAILED_AND (hr, IAudioClock::Reset, goto err);
|
2018-04-09 23:39:42 +00:00
|
|
|
|
2020-07-15 08:39:33 +00:00
|
|
|
err:
|
2018-04-09 23:39:42 +00:00
|
|
|
self->client_needs_restart = TRUE;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
2008-09-30 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 12:54:02 +00:00
|
|
|
#if DEFAULT_PROVIDE_CLOCK
|
2008-09-30 11:19:10 +00:00
|
|
|
static GstClockTime
|
|
|
|
gst_wasapi_src_get_time (GstClock * clock, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstWasapiSrc *self = GST_WASAPI_SRC (user_data);
|
|
|
|
HRESULT hr;
|
|
|
|
guint64 devpos;
|
|
|
|
GstClockTime result;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (self->client_clock == NULL))
|
|
|
|
return GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
hr = IAudioClock_GetPosition (self->client_clock, &devpos, NULL);
|
2018-02-14 03:57:31 +00:00
|
|
|
HR_FAILED_RET (hr, IAudioClock::GetPosition, GST_CLOCK_TIME_NONE);
|
2008-09-30 11:19:10 +00:00
|
|
|
|
|
|
|
result = gst_util_uint64_scale_int (devpos, GST_SECOND,
|
|
|
|
self->client_clock_freq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
GST_DEBUG_OBJECT (self, "devpos = %" G_GUINT64_FORMAT
|
|
|
|
" frequency = %" G_GUINT64_FORMAT
|
|
|
|
" result = %" G_GUINT64_FORMAT " ms",
|
|
|
|
devpos, self->client_clock_freq, GST_TIME_AS_MSECONDS (result));
|
|
|
|
*/
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-04-18 09:33:19 +00:00
|
|
|
#endif
|