wasapi2: Introduce new WASAPI plugin

Add a new wasapi implementation mainly to support UWP application.
Basically the core logic of this plugin is almost identical to
existing wasapi plugin, but main target is Windows 10 (+ UWP).
Since this plugin uses WinRT APIs, this plugin most likely might not
work Windows 8 or lower.

Compared with existing wasapi plugin, additional features of this plugin are
* Fully compatible with both Windows 10 desktop and UWP application
* Supports automatic stream routing (auto fallback when device was removed)
* Support device level mute/volume control

But some features of existing wasapi plugin are not implemented
in this plugin yet
* Exclusive streaming mode is not supported
* Loopback feature is not implemented
* Cross-compile is not possible with current mingw toolchain
  (meaning that MSVC and Windows 10 SDK are required to build this plugin)

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1264>
This commit is contained in:
Seungha Yang 2020-05-26 05:17:41 +09:00 committed by Nirbheek Chauhan
parent 0f74785b8e
commit 2778457678
13 changed files with 3595 additions and 0 deletions

View file

@ -155,6 +155,7 @@ option('voaacenc', type : 'feature', value : 'auto', description : 'AAC audio en
option('voamrwbenc', type : 'feature', value : 'auto', description : 'AMR-WB audio encoder plugin')
option('vulkan', type : 'feature', value : 'auto', description : 'Vulkan video sink plugin')
option('wasapi', type : 'feature', value : 'auto', description : 'Windows Audio Session API source/sink plugin')
option('wasapi2', type : 'feature', value : 'auto', description : 'Windows Audio Session API source/sink plugin with WinRT API')
option('webp', type : 'feature', value : 'auto', description : 'WebP image codec plugin')
option('webrtc', type : 'feature', value : 'auto', description : 'WebRTC audio/video network bin plugin')
option('webrtcdsp', type : 'feature', value : 'auto', description : 'Plugin with various audio filters provided by the WebRTC audio processing library')

View file

@ -22,6 +22,7 @@ subdir('tinyalsa')
subdir('uvch264')
subdir('v4l2codecs')
subdir('wasapi')
subdir('wasapi2')
subdir('winks')
subdir('winscreencap')

View file

@ -0,0 +1,168 @@
// MIT License
//
// Copyright (c) 2016 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Source taken from https://github.com/microsoft/MixedRealityCompanionKit
#pragma once
#include <wrl.h>
#include <wrl\async.h>
#include <Windows.System.Threading.h>
#include <functional>
template <typename TDelegate, typename TOperation, typename TLambda>
HRESULT StartAsyncThen(_In_ TOperation* pOperation, _In_ TLambda&& tFunc)
{
if (nullptr == pOperation)
{
return E_INVALIDARG;
}
auto spCallback = Microsoft::WRL::Callback<TDelegate>(
[tFunc](_In_ TOperation* pOperation, _In_ AsyncStatus status) -> HRESULT
{
HRESULT hr = S_OK;
// wrap the operation
if (status != AsyncStatus::Completed)
{
Microsoft::WRL::ComPtr<TOperation> spOperation(pOperation);
Microsoft::WRL::ComPtr<IAsyncInfo> spAsyncInfo;
hr = spOperation.As(&spAsyncInfo);
if (SUCCEEDED(hr))
{
spAsyncInfo->get_ErrorCode(&hr);
}
}
return tFunc(hr, pOperation, status);
});
// start
return (nullptr != spCallback) ? pOperation->put_Completed(spCallback.Get()) : E_OUTOFMEMORY;
}
template <typename TLambda>
HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncAction* pOperation, _In_ TLambda&& tFunc)
{
return StartAsyncThen<ABI::Windows::Foundation::IAsyncActionCompletedHandler, ABI::Windows::Foundation::IAsyncAction>(pOperation, static_cast<TLambda&&>(tFunc));
}
template <typename TProgress, typename TLambda>
HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncActionWithProgress<TProgress>* pOperation, _In_ TLambda&& tFunc)
{
return StartAsyncThen<ABI::Windows::Foundation::IAsyncActionWithProgressCompletedHandler<TProgress>, Windows::Foundation::IAsyncActionWithProgress<TProgress>>(pOperation, static_cast<TLambda&&>(tFunc));
}
template <typename TResult, typename TLambda>
HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncOperation<TResult>* pOperation, _In_ TLambda&& tFunc)
{
return StartAsyncThen<ABI::Windows::Foundation::IAsyncOperationCompletedHandler<TResult>, ABI::Windows::Foundation::IAsyncOperation<TResult>>(pOperation, static_cast<TLambda&&>(tFunc));
}
template <typename TResult, typename TProgress, typename TLambda>
HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>* pOperation, _In_ TLambda&& tFunc)
{
return StartAsyncThen<ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<TResult, TProgress>, ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>>(pOperation, static_cast<TLambda&&>(tFunc));
}
// eg. TOperation = IAsyncOperationWithProgress<UINT32, UINT32>
// eg. THandler = IAsyncOperationWithProgressCompletedHandler<UINT, UINT>
template<typename TOperation, typename THandler>
class AsyncEventDelegate
: public Microsoft::WRL::RuntimeClass
< Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::Delegate>
, THandler
, Microsoft::WRL::FtmBase >
{
public:
AsyncEventDelegate()
: _completedEvent(CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS))
{
ComPtr<AsyncEventDelegate> spThis(this);
auto lambda = ([this, spThis](_In_ HRESULT hr, _In_ TOperation* pOperation)
{
SetEvent(_completedEvent.Get());
});
_func = std::move(lambda);
}
STDMETHOD(Invoke)(
_In_ TOperation* pOperation,
_In_ AsyncStatus status)
{
HRESULT hr = S_OK;
// if we completed successfully, then there is no need for getting hresult
if (status != AsyncStatus::Completed)
{
Microsoft::WRL::ComPtr<TOperation> spOperation(pOperation);
Microsoft::WRL::ComPtr<IAsyncInfo> spAsyncInfo;
if (SUCCEEDED(spOperation.As(&spAsyncInfo)))
{
spAsyncInfo->get_ErrorCode(&hr);
}
}
_func(hr, pOperation);
return S_OK;
}
STDMETHOD(SyncWait)(_In_ TOperation* pOperation, _In_ DWORD dwMilliseconds)
{
HRESULT hr = pOperation->put_Completed(this);
if (FAILED(hr))
{
return hr;
}
DWORD dwWait = WaitForSingleObjectEx(_completedEvent.Get(), dwMilliseconds, TRUE);
if (WAIT_IO_COMPLETION == dwWait || WAIT_OBJECT_0 == dwWait)
return S_OK;
return HRESULT_FROM_WIN32(GetLastError());
}
private:
std::function<void(HRESULT, TOperation*)> _func;
Microsoft::WRL::Wrappers::Event _completedEvent;
};
template <typename TOperation, typename THandler>
HRESULT SyncWait(_In_ TOperation* pOperation, _In_ DWORD dwMilliseconds)
{
auto spCallback = Microsoft::WRL::Make<AsyncEventDelegate<TOperation, THandler>>();
return spCallback->SyncWait(pOperation, dwMilliseconds);
}
template <typename TResult>
HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncAction* pOperation, _In_ DWORD dwMilliseconds = INFINITE)
{
return SyncWait<ABI::Windows::Foundation::IAsyncAction, ABI::Windows::Foundation::IAsyncActionCompletedHandler>(pOperation, dwMilliseconds);
}
template <typename TResult>
HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncOperation<TResult>* pOperation, _In_ DWORD dwMilliseconds = INFINITE)
{
return SyncWait<ABI::Windows::Foundation::IAsyncOperation<TResult>, ABI::Windows::Foundation::IAsyncOperationCompletedHandler<TResult>>(pOperation, dwMilliseconds);
}
template <typename TResult, typename TProgress>
HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>* pOperation, _In_ DWORD dwMilliseconds = INFINITE)
{
return SyncWait<ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>, ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<TResult, TProgress>>(pOperation, dwMilliseconds);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,82 @@
/*
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_WASAPI2_CLIENT_H__
#define __GST_WASAPI2_CLIENT_H__
#include <gst/gst.h>
#include <gst/audio/audio.h>
G_BEGIN_DECLS
typedef enum
{
GST_WASAPI2_CLIENT_DEVICE_CLASS_CAPTURE = 0,
GST_WASAPI2_CLIENT_DEVICE_CLASS_RENDER,
} GstWasapi2ClientDeviceClass;
#define GST_TYPE_WASAPI2_CLIENT_DEVICE_CLASS (gst_wasapi2_client_device_class_get_type())
GType gst_wasapi2_client_device_class_get_type (void);
#define GST_TYPE_WASAPI2_CLIENT (gst_wasapi2_client_get_type())
G_DECLARE_FINAL_TYPE (GstWasapi2Client,
gst_wasapi2_client, GST, WASAPI2_CLIENT, GstObject);
GstCaps * gst_wasapi2_client_get_caps (GstWasapi2Client * client);
gboolean gst_wasapi2_client_open (GstWasapi2Client * client,
GstAudioRingBufferSpec * spec,
GstAudioRingBuffer * buf);
gboolean gst_wasapi2_client_start (GstWasapi2Client * client);
gboolean gst_wasapi2_client_stop (GstWasapi2Client * client);
gint gst_wasapi2_client_read (GstWasapi2Client * client,
gpointer data,
guint length);
gint gst_wasapi2_client_write (GstWasapi2Client * client,
gpointer data,
guint length);
guint gst_wasapi2_client_delay (GstWasapi2Client * client);
gboolean gst_wasapi2_client_set_mute (GstWasapi2Client * client,
gboolean mute);
gboolean gst_wasapi2_client_get_mute (GstWasapi2Client * client,
gboolean * mute);
gboolean gst_wasapi2_client_set_volume (GstWasapi2Client * client,
gfloat volume);
gboolean gst_wasapi2_client_get_volume (GstWasapi2Client * client,
gfloat * volume);
GstWasapi2Client * gst_wasapi2_client_new (GstWasapi2ClientDeviceClass device_class,
gboolean low_latency,
gint device_index,
const gchar * device_id);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GstWasapi2Client, gst_object_unref)
G_END_DECLS
#endif /* __GST_WASAPI2_CLIENT_H__ */

View file

@ -0,0 +1,548 @@
/*
* Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
* Copyright (C) 2013 Collabora Ltd.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
* Copyright (C) 2018 Centricular Ltd.
* Author: Nirbheek Chauhan <nirbheek@centricular.com>
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-wasapi2sink
* @title: wasapi2sink
*
* Provides audio playback using the Windows Audio Session API available with
* Windows 10.
*
* ## Example pipelines
* |[
* gst-launch-1.0 -v audiotestsrc samplesperbuffer=160 ! wasapi2sink
* ]| Generate 20 ms buffers and render to the default audio device.
*
* |[
* gst-launch-1.0 -v audiotestsrc samplesperbuffer=160 ! wasapi2sink low-latency=true
* ]| Same as above, but with the minimum possible latency
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gstwasapi2sink.h"
#include "gstwasapi2util.h"
#include "gstwasapi2client.h"
GST_DEBUG_CATEGORY_STATIC (gst_wasapi2_sink_debug);
#define GST_CAT_DEFAULT gst_wasapi2_sink_debug
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_WASAPI2_STATIC_CAPS));
#define DEFAULT_LOW_LATENCY FALSE
#define DEFAULT_MUTE FALSE
#define DEFAULT_VOLUME 1.0
#define GST_WASAPI2_SINK_LOCK(s) g_mutex_lock(&(s)->lock)
#define GST_WASAPI2_SINK_UNLOCK(s) g_mutex_unlock(&(s)->lock)
enum
{
PROP_0,
PROP_DEVICE,
PROP_LOW_LATENCY,
PROP_MUTE,
PROP_VOLUME,
};
struct _GstWasapi2Sink
{
GstAudioSink parent;
GstWasapi2Client *client;
GstCaps *cached_caps;
gboolean started;
/* properties */
gchar *device_id;
gboolean low_latency;
gboolean mute;
gdouble volume;
gboolean mute_changed;
gboolean volume_changed;
/* to protect audioclient from set/get property */
GMutex lock;
};
static void gst_wasapi2_sink_dispose (GObject * object);
static void gst_wasapi2_sink_finalize (GObject * object);
static void gst_wasapi2_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_wasapi2_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static GstCaps *gst_wasapi2_sink_get_caps (GstBaseSink * bsink,
GstCaps * filter);
static gboolean gst_wasapi2_sink_prepare (GstAudioSink * asink,
GstAudioRingBufferSpec * spec);
static gboolean gst_wasapi2_sink_unprepare (GstAudioSink * asink);
static gboolean gst_wasapi2_sink_open (GstAudioSink * asink);
static gboolean gst_wasapi2_sink_close (GstAudioSink * asink);
static gint gst_wasapi2_sink_write (GstAudioSink * asink,
gpointer data, guint length);
static guint gst_wasapi2_sink_delay (GstAudioSink * asink);
static void gst_wasapi2_sink_reset (GstAudioSink * asink);
static void gst_wasapi2_sink_set_mute (GstWasapi2Sink * self, gboolean mute);
static gboolean gst_wasapi2_sink_get_mute (GstWasapi2Sink * self);
static void gst_wasapi2_sink_set_volume (GstWasapi2Sink * self, gdouble volume);
static gdouble gst_wasapi2_sink_get_volume (GstWasapi2Sink * self);
#define gst_wasapi2_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstWasapi2Sink, gst_wasapi2_sink, GST_TYPE_AUDIO_SINK,
G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
static void
gst_wasapi2_sink_class_init (GstWasapi2SinkClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
GstAudioSinkClass *audiosink_class = GST_AUDIO_SINK_CLASS (klass);
gobject_class->dispose = gst_wasapi2_sink_dispose;
gobject_class->finalize = gst_wasapi2_sink_finalize;
gobject_class->set_property = gst_wasapi2_sink_set_property;
gobject_class->get_property = gst_wasapi2_sink_get_property;
g_object_class_install_property (gobject_class, PROP_DEVICE,
g_param_spec_string ("device", "Device",
"WASAPI playback device as a GUID string",
NULL, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
g_param_spec_boolean ("low-latency", "Low latency",
"Optimize all settings for lowest latency. Always safe to enable.",
DEFAULT_LOW_LATENCY, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MUTE,
g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
DEFAULT_MUTE, GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_VOLUME,
g_param_spec_double ("volume", "Volume", "Volume of this stream",
0.0, 1.0, DEFAULT_VOLUME,
GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
gst_element_class_add_static_pad_template (element_class, &sink_template);
gst_element_class_set_static_metadata (element_class, "Wasapi2Sink",
"Sink/Audio/Hardware",
"Stream audio to an audio capture device through WASAPI",
"Nirbheek Chauhan <nirbheek@centricular.com>, "
"Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>, "
"Seungha Yang <seungha@centricular.com>");
basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_get_caps);
audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_prepare);
audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_unprepare);
audiosink_class->open = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_open);
audiosink_class->close = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_close);
audiosink_class->write = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_write);
audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_delay);
audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_reset);
GST_DEBUG_CATEGORY_INIT (gst_wasapi2_sink_debug, "wasapi2sink",
0, "Windows audio session API sink");
}
static void
gst_wasapi2_sink_init (GstWasapi2Sink * self)
{
self->low_latency = DEFAULT_LOW_LATENCY;
self->mute = DEFAULT_MUTE;
self->volume = DEFAULT_VOLUME;
g_mutex_init (&self->lock);
}
static void
gst_wasapi2_sink_dispose (GObject * object)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
GST_WASAPI2_SINK_LOCK (self);
gst_clear_object (&self->client);
gst_clear_caps (&self->cached_caps);
GST_WASAPI2_SINK_UNLOCK (self);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_wasapi2_sink_finalize (GObject * object)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
g_free (self->device_id);
g_mutex_clear (&self->lock);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_wasapi2_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
switch (prop_id) {
case PROP_DEVICE:
g_free (self->device_id);
self->device_id = g_value_dup_string (value);
break;
case PROP_LOW_LATENCY:
self->low_latency = g_value_get_boolean (value);
break;
case PROP_MUTE:
gst_wasapi2_sink_set_mute (self, g_value_get_boolean (value));
break;
case PROP_VOLUME:
gst_wasapi2_sink_set_volume (self, g_value_get_double (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_wasapi2_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
switch (prop_id) {
case PROP_DEVICE:
g_value_set_string (value, self->device_id);
break;
case PROP_LOW_LATENCY:
g_value_set_boolean (value, self->low_latency);
break;
case PROP_MUTE:
g_value_set_boolean (value, gst_wasapi2_sink_get_mute (self));
break;
case PROP_VOLUME:
g_value_set_double (value, gst_wasapi2_sink_get_volume (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstCaps *
gst_wasapi2_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (bsink);
GstCaps *caps = NULL;
/* store one caps here so that we can return device caps even if
* audioclient was closed due to unprepare() */
if (!self->cached_caps && self->client)
self->cached_caps = gst_wasapi2_client_get_caps (self->client);
if (self->client)
caps = gst_wasapi2_client_get_caps (self->client);
if (!caps && self->cached_caps)
caps = gst_caps_ref (self->cached_caps);
if (!caps)
caps = gst_pad_get_pad_template_caps (bsink->sinkpad);
if (filter) {
GstCaps *filtered =
gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref (caps);
caps = filtered;
}
GST_DEBUG_OBJECT (self, "returning caps %" GST_PTR_FORMAT, caps);
return caps;
}
static gboolean
gst_wasapi2_sink_open_unlocked (GstAudioSink * asink)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
self->client =
gst_wasapi2_client_new (GST_WASAPI2_CLIENT_DEVICE_CLASS_RENDER,
self->low_latency, -1, self->device_id);
return ! !self->client;
}
static gboolean
gst_wasapi2_sink_open (GstAudioSink * asink)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
gboolean ret;
GST_DEBUG_OBJECT (self, "Opening device");
GST_WASAPI2_SINK_LOCK (self);
ret = gst_wasapi2_sink_open_unlocked (asink);
GST_WASAPI2_SINK_UNLOCK (self);
if (!ret) {
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE, (NULL),
("Failed to open device"));
return FALSE;
}
return TRUE;
}
static gboolean
gst_wasapi2_sink_close (GstAudioSink * asink)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
GST_WASAPI2_SINK_LOCK (self);
gst_clear_object (&self->client);
gst_clear_caps (&self->cached_caps);
self->started = FALSE;
GST_WASAPI2_SINK_UNLOCK (self);
return TRUE;
}
static gboolean
gst_wasapi2_sink_prepare (GstAudioSink * asink, GstAudioRingBufferSpec * spec)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
GstAudioBaseSink *bsink = GST_AUDIO_BASE_SINK (asink);
gboolean ret = FALSE;
GST_WASAPI2_SINK_LOCK (self);
if (!self->client && !gst_wasapi2_sink_open_unlocked (asink)) {
GST_ERROR_OBJECT (self, "No audio client was configured");
goto done;
}
if (!gst_wasapi2_client_open (self->client, spec, bsink->ringbuffer)) {
GST_ERROR_OBJECT (self, "Couldn't open audio client");
goto done;
}
/* Set mute and volume here again, maybe when "mute" property was set, audioclient
* might not be configured at that moment */
if (self->mute_changed) {
gst_wasapi2_client_set_mute (self->client, self->mute);
self->mute_changed = FALSE;
}
if (self->volume_changed) {
gst_wasapi2_client_set_volume (self->client, self->volume);
self->volume_changed = FALSE;
}
/* Will start IAudioClient on the first write request */
self->started = FALSE;
ret = TRUE;
done:
GST_WASAPI2_SINK_UNLOCK (self);
return ret;
}
static gboolean
gst_wasapi2_sink_unprepare (GstAudioSink * asink)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
self->started = FALSE;
/* Will reopen device later prepare() */
GST_WASAPI2_SINK_LOCK (self);
if (self->client) {
gst_wasapi2_client_stop (self->client);
gst_clear_object (&self->client);
}
GST_WASAPI2_SINK_UNLOCK (self);
return TRUE;
}
static gint
gst_wasapi2_sink_write (GstAudioSink * asink, gpointer data, guint length)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
if (!self->client) {
GST_ERROR_OBJECT (self, "No audio client was configured");
return -1;
}
if (!self->started) {
if (!gst_wasapi2_client_start (self->client)) {
GST_ERROR_OBJECT (self, "Failed to re-start client");
return -1;
}
self->started = TRUE;
}
return gst_wasapi2_client_write (self->client, data, length);
}
static guint
gst_wasapi2_sink_delay (GstAudioSink * asink)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
if (!self->client)
return 0;
return gst_wasapi2_client_delay (self->client);
}
static void
gst_wasapi2_sink_reset (GstAudioSink * asink)
{
GstWasapi2Sink *self = GST_WASAPI2_SINK (asink);
GST_INFO_OBJECT (self, "reset called");
self->started = FALSE;
if (!self->client)
return;
gst_wasapi2_client_stop (self->client);
}
static void
gst_wasapi2_sink_set_mute (GstWasapi2Sink * self, gboolean mute)
{
GST_WASAPI2_SINK_LOCK (self);
self->mute = mute;
self->mute_changed = TRUE;
if (self->client) {
if (!gst_wasapi2_client_set_mute (self->client, mute)) {
GST_INFO_OBJECT (self, "Couldn't set mute");
} else {
self->mute_changed = FALSE;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SINK_UNLOCK (self);
}
static gboolean
gst_wasapi2_sink_get_mute (GstWasapi2Sink * self)
{
gboolean mute;
GST_WASAPI2_SINK_LOCK (self);
mute = self->mute;
if (self->client) {
if (!gst_wasapi2_client_get_mute (self->client, &mute)) {
GST_INFO_OBJECT (self, "Couldn't get mute state");
} else {
self->mute = mute;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SINK_UNLOCK (self);
return mute;
}
static void
gst_wasapi2_sink_set_volume (GstWasapi2Sink * self, gdouble volume)
{
GST_WASAPI2_SINK_LOCK (self);
self->volume = volume;
/* clip volume value */
self->volume = MAX (0.0, self->volume);
self->volume = MIN (1.0, self->volume);
self->volume_changed = TRUE;
if (self->client) {
if (!gst_wasapi2_client_set_volume (self->client, (gfloat) self->volume)) {
GST_INFO_OBJECT (self, "Couldn't set volume");
} else {
self->volume_changed = FALSE;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SINK_UNLOCK (self);
}
static gdouble
gst_wasapi2_sink_get_volume (GstWasapi2Sink * self)
{
gfloat volume;
GST_WASAPI2_SINK_LOCK (self);
volume = (gfloat) self->volume;
if (self->client) {
if (!gst_wasapi2_client_get_volume (self->client, &volume)) {
GST_INFO_OBJECT (self, "Couldn't get volume");
} else {
self->volume = volume;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SINK_UNLOCK (self);
volume = MAX (0.0, volume);
volume = MIN (1.0, volume);
return volume;
}

View file

@ -0,0 +1,34 @@
/* GStreamer
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_WASAPI2_SINK_H__
#define __GST_WASAPI2_SINK_H__
#include <gst/gst.h>
#include <gst/audio/audio.h>
G_BEGIN_DECLS
#define GST_TYPE_WASAPI2_SINK (gst_wasapi2_sink_get_type ())
G_DECLARE_FINAL_TYPE (GstWasapi2Sink,
gst_wasapi2_sink, GST, WASAPI2_SINK, GstAudioSink);
G_END_DECLS
#endif /* __GST_WASAPI2_SINK_H__ */

546
sys/wasapi2/gstwasapi2src.c Normal file
View file

@ -0,0 +1,546 @@
/*
* Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
* Copyright (C) 2018 Centricular Ltd.
* Author: Nirbheek Chauhan <nirbheek@centricular.com>
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-wasapi2src
* @title: wasapi2src
*
* Provides audio capture from the Windows Audio Session API available with
* Windows 10.
*
* ## Example pipelines
* |[
* gst-launch-1.0 -v wasapi2src ! fakesrc
* ]| Capture from the default audio device and render to fakesrc.
*
* |[
* gst-launch-1.0 -v wasapi2src low-latency=true ! fakesrc
* ]| Capture from the default audio device with the minimum possible latency and render to fakesrc.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gstwasapi2src.h"
#include "gstwasapi2util.h"
#include "gstwasapi2client.h"
GST_DEBUG_CATEGORY_STATIC (gst_wasapi2_src_debug);
#define GST_CAT_DEFAULT gst_wasapi2_src_debug
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_WASAPI2_STATIC_CAPS));
#define DEFAULT_LOW_LATENCY FALSE
#define DEFAULT_MUTE FALSE
#define DEFAULT_VOLUME 1.0
#define GST_WASAPI2_SRC_LOCK(s) g_mutex_lock(&(s)->lock)
#define GST_WASAPI2_SRC_UNLOCK(s) g_mutex_unlock(&(s)->lock)
enum
{
PROP_0,
PROP_DEVICE,
PROP_LOW_LATENCY,
PROP_MUTE,
PROP_VOLUME,
};
struct _GstWasapi2Src
{
GstAudioSrc parent;
GstWasapi2Client *client;
GstCaps *cached_caps;
gboolean started;
/* properties */
gchar *device_id;
gboolean low_latency;
gboolean mute;
gdouble volume;
gboolean mute_changed;
gboolean volume_changed;
/* to protect audioclient from set/get property */
GMutex lock;
};
static void gst_wasapi2_src_dispose (GObject * object);
static void gst_wasapi2_src_finalize (GObject * object);
static void gst_wasapi2_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_wasapi2_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static GstCaps *gst_wasapi2_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter);
static gboolean gst_wasapi2_src_open (GstAudioSrc * asrc);
static gboolean gst_wasapi2_src_close (GstAudioSrc * asrc);
static gboolean gst_wasapi2_src_prepare (GstAudioSrc * asrc,
GstAudioRingBufferSpec * spec);
static gboolean gst_wasapi2_src_unprepare (GstAudioSrc * asrc);
static guint gst_wasapi2_src_read (GstAudioSrc * asrc, gpointer data,
guint length, GstClockTime * timestamp);
static guint gst_wasapi2_src_delay (GstAudioSrc * asrc);
static void gst_wasapi2_src_reset (GstAudioSrc * asrc);
static void gst_wasapi2_src_set_mute (GstWasapi2Src * self, gboolean mute);
static gboolean gst_wasapi2_src_get_mute (GstWasapi2Src * self);
static void gst_wasapi2_src_set_volume (GstWasapi2Src * self, gdouble volume);
static gdouble gst_wasapi2_src_get_volume (GstWasapi2Src * self);
#define gst_wasapi2_src_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstWasapi2Src, gst_wasapi2_src, GST_TYPE_AUDIO_SRC,
G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
static void
gst_wasapi2_src_class_init (GstWasapi2SrcClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstBaseSrcClass *basesrc_class = GST_BASE_SRC_CLASS (klass);
GstAudioSrcClass *audiosrc_class = GST_AUDIO_SRC_CLASS (klass);
gobject_class->dispose = gst_wasapi2_src_dispose;
gobject_class->finalize = gst_wasapi2_src_finalize;
gobject_class->set_property = gst_wasapi2_src_set_property;
gobject_class->get_property = gst_wasapi2_src_get_property;
g_object_class_install_property (gobject_class, PROP_DEVICE,
g_param_spec_string ("device", "Device",
"WASAPI playback device as a GUID string",
NULL, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
g_param_spec_boolean ("low-latency", "Low latency",
"Optimize all settings for lowest latency. Always safe to enable.",
DEFAULT_LOW_LATENCY, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MUTE,
g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
DEFAULT_MUTE, GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_VOLUME,
g_param_spec_double ("volume", "Volume", "Volume of this stream",
0.0, 1.0, DEFAULT_VOLUME,
GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
gst_element_class_add_static_pad_template (element_class, &src_template);
gst_element_class_set_static_metadata (element_class, "Wasapi2Src",
"Source/Audio/Hardware",
"Stream audio from an audio capture device through WASAPI",
"Nirbheek Chauhan <nirbheek@centricular.com>, "
"Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>, "
"Seungha Yang <seungha@centricular.com>");
basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_wasapi2_src_get_caps);
audiosrc_class->open = GST_DEBUG_FUNCPTR (gst_wasapi2_src_open);
audiosrc_class->close = GST_DEBUG_FUNCPTR (gst_wasapi2_src_close);
audiosrc_class->read = GST_DEBUG_FUNCPTR (gst_wasapi2_src_read);
audiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_wasapi2_src_prepare);
audiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_wasapi2_src_unprepare);
audiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_wasapi2_src_delay);
audiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_wasapi2_src_reset);
GST_DEBUG_CATEGORY_INIT (gst_wasapi2_src_debug, "wasapi2src",
0, "Windows audio session API source");
}
static void
gst_wasapi2_src_init (GstWasapi2Src * self)
{
self->mute = DEFAULT_MUTE;
self->volume = DEFAULT_VOLUME;
self->low_latency = DEFAULT_LOW_LATENCY;
g_mutex_init (&self->lock);
}
static void
gst_wasapi2_src_dispose (GObject * object)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (object);
GST_WASAPI2_SRC_LOCK (self);
gst_clear_object (&self->client);
gst_clear_caps (&self->cached_caps);
GST_WASAPI2_SRC_UNLOCK (self);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_wasapi2_src_finalize (GObject * object)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (object);
g_free (self->device_id);
g_mutex_clear (&self->lock);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_wasapi2_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (object);
switch (prop_id) {
case PROP_DEVICE:
g_free (self->device_id);
self->device_id = g_value_dup_string (value);
break;
case PROP_LOW_LATENCY:
self->low_latency = g_value_get_boolean (value);
break;
case PROP_MUTE:
gst_wasapi2_src_set_mute (self, g_value_get_boolean (value));
break;
case PROP_VOLUME:
gst_wasapi2_src_set_volume (self, g_value_get_double (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_wasapi2_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (object);
switch (prop_id) {
case PROP_DEVICE:
g_value_set_string (value, self->device_id);
break;
case PROP_LOW_LATENCY:
g_value_set_boolean (value, self->low_latency);
break;
case PROP_MUTE:
g_value_set_boolean (value, gst_wasapi2_src_get_mute (self));
break;
case PROP_VOLUME:
g_value_set_double (value, gst_wasapi2_src_get_volume (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstCaps *
gst_wasapi2_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (bsrc);
GstCaps *caps = NULL;
/* store one caps here so that we can return device caps even if
* audioclient was closed due to unprepare() */
if (!self->cached_caps && self->client)
self->cached_caps = gst_wasapi2_client_get_caps (self->client);
if (self->client)
caps = gst_wasapi2_client_get_caps (self->client);
if (!caps && self->cached_caps)
caps = gst_caps_ref (self->cached_caps);
if (!caps)
caps = gst_pad_get_pad_template_caps (bsrc->srcpad);
if (filter) {
GstCaps *filtered =
gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref (caps);
caps = filtered;
}
GST_DEBUG_OBJECT (self, "returning caps %" GST_PTR_FORMAT, caps);
return caps;
}
static gboolean
gst_wasapi2_src_open_unlocked (GstAudioSrc * asrc)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
self->client =
gst_wasapi2_client_new (GST_WASAPI2_CLIENT_DEVICE_CLASS_CAPTURE,
self->low_latency, -1, self->device_id);
return ! !self->client;
}
static gboolean
gst_wasapi2_src_open (GstAudioSrc * asrc)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
gboolean ret;
GST_DEBUG_OBJECT (self, "Opening device");
GST_WASAPI2_SRC_LOCK (self);
ret = gst_wasapi2_src_open_unlocked (asrc);
GST_WASAPI2_SRC_UNLOCK (self);
if (!ret) {
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
("Failed to open device"));
return FALSE;
}
return TRUE;
}
static gboolean
gst_wasapi2_src_close (GstAudioSrc * asrc)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
GST_WASAPI2_SRC_LOCK (self);
gst_clear_object (&self->client);
gst_clear_caps (&self->cached_caps);
self->started = FALSE;
GST_WASAPI2_SRC_UNLOCK (self);
return TRUE;
}
static gboolean
gst_wasapi2_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
GstAudioBaseSrc *bsrc = GST_AUDIO_BASE_SRC (asrc);
gboolean ret = FALSE;
GST_WASAPI2_SRC_LOCK (self);
if (!self->client && !gst_wasapi2_src_open_unlocked (asrc)) {
GST_ERROR_OBJECT (self, "No audio client was configured");
goto done;
}
if (!gst_wasapi2_client_open (self->client, spec, bsrc->ringbuffer)) {
GST_ERROR_OBJECT (self, "Couldn't open audio client");
goto done;
}
/* Set mute and volume here again, maybe when "mute" property was set, audioclient
* might not be configured at that moment */
if (self->mute_changed) {
gst_wasapi2_client_set_mute (self->client, self->mute);
self->mute_changed = FALSE;
}
if (self->volume_changed) {
gst_wasapi2_client_set_volume (self->client, self->volume);
self->volume_changed = FALSE;
}
/* Will start IAudioClient on the first read request */
self->started = FALSE;
ret = TRUE;
done:
GST_WASAPI2_SRC_UNLOCK (self);
return ret;
}
static gboolean
gst_wasapi2_src_unprepare (GstAudioSrc * asrc)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
self->started = FALSE;
/* Will reopen device later prepare() */
GST_WASAPI2_SRC_LOCK (self);
if (self->client) {
gst_wasapi2_client_stop (self->client);
gst_clear_object (&self->client);
}
GST_WASAPI2_SRC_UNLOCK (self);
return TRUE;
}
static guint
gst_wasapi2_src_read (GstAudioSrc * asrc, gpointer data, guint length,
GstClockTime * timestamp)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
if (!self->client) {
GST_ERROR_OBJECT (self, "No audio client was configured");
return -1;
}
if (!self->started) {
if (!gst_wasapi2_client_start (self->client)) {
GST_ERROR_OBJECT (self, "Failed to re-start client");
return -1;
}
self->started = TRUE;
}
return gst_wasapi2_client_read (self->client, data, length);
}
static guint
gst_wasapi2_src_delay (GstAudioSrc * asrc)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
if (!self->client)
return 0;
return gst_wasapi2_client_delay (self->client);
}
static void
gst_wasapi2_src_reset (GstAudioSrc * asrc)
{
GstWasapi2Src *self = GST_WASAPI2_SRC (asrc);
GST_DEBUG_OBJECT (self, "reset called");
self->started = FALSE;
if (!self->client)
return;
gst_wasapi2_client_stop (self->client);
}
static void
gst_wasapi2_src_set_mute (GstWasapi2Src * self, gboolean mute)
{
GST_WASAPI2_SRC_LOCK (self);
self->mute = mute;
self->mute_changed = TRUE;
if (self->client) {
if (!gst_wasapi2_client_set_mute (self->client, mute)) {
GST_INFO_OBJECT (self, "Couldn't set mute");
} else {
self->mute_changed = FALSE;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SRC_UNLOCK (self);
}
static gboolean
gst_wasapi2_src_get_mute (GstWasapi2Src * self)
{
gboolean mute;
GST_WASAPI2_SRC_LOCK (self);
mute = self->mute;
if (self->client) {
if (!gst_wasapi2_client_get_mute (self->client, &mute)) {
GST_INFO_OBJECT (self, "Couldn't get mute state");
} else {
self->mute = mute;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SRC_UNLOCK (self);
return mute;
}
static void
gst_wasapi2_src_set_volume (GstWasapi2Src * self, gdouble volume)
{
GST_WASAPI2_SRC_LOCK (self);
self->volume = volume;
/* clip volume value */
self->volume = MAX (0.0, self->volume);
self->volume = MIN (1.0, self->volume);
self->volume_changed = TRUE;
if (self->client) {
if (!gst_wasapi2_client_set_volume (self->client, (gfloat) self->volume)) {
GST_INFO_OBJECT (self, "Couldn't set volume");
} else {
self->volume_changed = FALSE;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SRC_UNLOCK (self);
}
static gdouble
gst_wasapi2_src_get_volume (GstWasapi2Src * self)
{
gfloat volume;
GST_WASAPI2_SRC_LOCK (self);
volume = (gfloat) self->volume;
if (self->client) {
if (!gst_wasapi2_client_get_volume (self->client, &volume)) {
GST_INFO_OBJECT (self, "Couldn't get volume");
} else {
self->volume = volume;
}
} else {
GST_DEBUG_OBJECT (self, "audio client is not configured yet");
}
GST_WASAPI2_SRC_UNLOCK (self);
volume = MAX (0.0, volume);
volume = MIN (1.0, volume);
return volume;
}

View file

@ -0,0 +1,34 @@
/* GStreamer
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_WASAPI2_SRC_H__
#define __GST_WASAPI2_SRC_H__
#include <gst/gst.h>
#include <gst/audio/audio.h>
G_BEGIN_DECLS
#define GST_TYPE_WASAPI2_SRC (gst_wasapi2_src_get_type ())
G_DECLARE_FINAL_TYPE (GstWasapi2Src,
gst_wasapi2_src, GST, WASAPI2_SRC, GstAudioSrc);
G_END_DECLS
#endif /* __GST_WASAPI2_SRC_H__ */

View file

@ -0,0 +1,209 @@
/*
* Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
* Copyright (C) 2018 Centricular Ltd.
* Author: Nirbheek Chauhan <nirbheek@centricular.com>
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gstwasapi2util.h"
#include <audioclient.h>
GST_DEBUG_CATEGORY_EXTERN (gst_wasapi_debug);
#define GST_CAT_DEFAULT gst_wasapi_debug
/* *INDENT-OFF* */
static struct
{
guint64 wasapi_pos;
GstAudioChannelPosition gst_pos;
} wasapi_to_gst_pos[] = {
{SPEAKER_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT},
{SPEAKER_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
{SPEAKER_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER},
{SPEAKER_LOW_FREQUENCY, GST_AUDIO_CHANNEL_POSITION_LFE1},
{SPEAKER_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT},
{SPEAKER_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
{SPEAKER_FRONT_LEFT_OF_CENTER,
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER},
{SPEAKER_FRONT_RIGHT_OF_CENTER,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
{SPEAKER_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER},
/* Enum values diverge from this point onwards */
{SPEAKER_SIDE_LEFT, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT},
{SPEAKER_SIDE_RIGHT, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT},
{SPEAKER_TOP_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_CENTER},
{SPEAKER_TOP_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT},
{SPEAKER_TOP_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER},
{SPEAKER_TOP_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT},
{SPEAKER_TOP_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT},
{SPEAKER_TOP_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER},
{SPEAKER_TOP_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT}
};
/* *INDENT-ON* */
static const gchar *
hresult_to_string_fallback (HRESULT hr)
{
const gchar *s = "unknown error";
switch (hr) {
case AUDCLNT_E_NOT_INITIALIZED:
s = "AUDCLNT_E_NOT_INITIALIZED";
break;
case AUDCLNT_E_ALREADY_INITIALIZED:
s = "AUDCLNT_E_ALREADY_INITIALIZED";
break;
case AUDCLNT_E_WRONG_ENDPOINT_TYPE:
s = "AUDCLNT_E_WRONG_ENDPOINT_TYPE";
break;
case AUDCLNT_E_DEVICE_INVALIDATED:
s = "AUDCLNT_E_DEVICE_INVALIDATED";
break;
case AUDCLNT_E_NOT_STOPPED:
s = "AUDCLNT_E_NOT_STOPPED";
break;
case AUDCLNT_E_BUFFER_TOO_LARGE:
s = "AUDCLNT_E_BUFFER_TOO_LARGE";
break;
case AUDCLNT_E_OUT_OF_ORDER:
s = "AUDCLNT_E_OUT_OF_ORDER";
break;
case AUDCLNT_E_UNSUPPORTED_FORMAT:
s = "AUDCLNT_E_UNSUPPORTED_FORMAT";
break;
case AUDCLNT_E_INVALID_DEVICE_PERIOD:
s = "AUDCLNT_E_INVALID_DEVICE_PERIOD";
break;
case AUDCLNT_E_INVALID_SIZE:
s = "AUDCLNT_E_INVALID_SIZE";
break;
case AUDCLNT_E_DEVICE_IN_USE:
s = "AUDCLNT_E_DEVICE_IN_USE";
break;
case AUDCLNT_E_BUFFER_OPERATION_PENDING:
s = "AUDCLNT_E_BUFFER_OPERATION_PENDING";
break;
case AUDCLNT_E_BUFFER_SIZE_ERROR:
s = "AUDCLNT_E_BUFFER_SIZE_ERROR";
break;
case AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED:
s = "AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED";
break;
case AUDCLNT_E_THREAD_NOT_REGISTERED:
s = "AUDCLNT_E_THREAD_NOT_REGISTERED";
break;
case AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED:
s = "AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED";
break;
case AUDCLNT_E_ENDPOINT_CREATE_FAILED:
s = "AUDCLNT_E_ENDPOINT_CREATE_FAILED";
break;
case AUDCLNT_E_SERVICE_NOT_RUNNING:
s = "AUDCLNT_E_SERVICE_NOT_RUNNING";
break;
case AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED:
s = "AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED";
break;
case AUDCLNT_E_EXCLUSIVE_MODE_ONLY:
s = "AUDCLNT_E_EXCLUSIVE_MODE_ONLY";
break;
case AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL:
s = "AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL";
break;
case AUDCLNT_E_EVENTHANDLE_NOT_SET:
s = "AUDCLNT_E_EVENTHANDLE_NOT_SET";
break;
case AUDCLNT_E_INCORRECT_BUFFER_SIZE:
s = "AUDCLNT_E_INCORRECT_BUFFER_SIZE";
break;
case AUDCLNT_E_CPUUSAGE_EXCEEDED:
s = "AUDCLNT_E_CPUUSAGE_EXCEEDED";
break;
case AUDCLNT_S_BUFFER_EMPTY:
s = "AUDCLNT_S_BUFFER_EMPTY";
break;
case AUDCLNT_S_THREAD_ALREADY_REGISTERED:
s = "AUDCLNT_S_THREAD_ALREADY_REGISTERED";
break;
case AUDCLNT_S_POSITION_STALLED:
s = "AUDCLNT_S_POSITION_STALLED";
break;
case E_POINTER:
s = "E_POINTER";
break;
case E_INVALIDARG:
s = "E_INVALIDARG";
break;
}
return s;
}
static gchar *
gst_wasapi2_util_hresult_to_string (HRESULT hr)
{
DWORD flags;
gchar *ret_text;
LPTSTR error_text = NULL;
flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS;
FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) & error_text, 0, NULL);
/* If we couldn't get the error msg, try the fallback switch statement */
if (error_text == NULL)
return g_strdup (hresult_to_string_fallback (hr));
#ifdef UNICODE
/* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
#else
ret_text = g_strdup (error_text);
#endif
LocalFree (error_text);
return ret_text;
}
gboolean
_gst_wasapi2_result (HRESULT hr, GstDebugCategory * cat, const gchar * file,
const gchar * function, gint line)
{
#ifndef GST_DISABLE_GST_DEBUG
gboolean ret = TRUE;
if (FAILED (hr)) {
gchar *error_text = NULL;
error_text = gst_wasapi2_util_hresult_to_string (hr);
gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
NULL, "WASAPI call failed: 0x%x, %s", (guint) hr, error_text);
g_free (error_text);
ret = FALSE;
}
return ret;
#else
return SUCCEEDED (hr);
#endif
}

View file

@ -0,0 +1,47 @@
/* GStreamer
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_WASAPI2_UTIL_H__
#define __GST_WASAPI2_UTIL_H__
#include <gst/gst.h>
#include <gst/audio/audio.h>
#include <windows.h>
G_BEGIN_DECLS
/* Static Caps shared between source, sink, and device provider */
#define GST_WASAPI2_STATIC_CAPS "audio/x-raw, " \
"format = (string) " GST_AUDIO_FORMATS_ALL ", " \
"layout = (string) interleaved, " \
"rate = " GST_AUDIO_RATE_RANGE ", " \
"channels = " GST_AUDIO_CHANNELS_RANGE
gboolean _gst_wasapi2_result (HRESULT hr,
GstDebugCategory * cat,
const gchar * file,
const gchar * function,
gint line);
#define gst_wasapi2_result(result) \
_gst_wasapi2_result (result, GST_CAT_DEFAULT, __FILE__, GST_FUNCTION, __LINE__)
G_END_DECLS
#endif /* __GST_WASAPI_UTIL_H__ */

92
sys/wasapi2/meson.build Normal file
View file

@ -0,0 +1,92 @@
wasapi2_sources = [
'gstwasapi2src.c',
'gstwasapi2sink.c',
'gstwasapi2util.c',
'gstwasapi2client.cpp',
'plugin.c',
]
mmdeviceapi_symbols = [
'ActivateAudioInterfaceAsync',
'DEVINTERFACE_AUDIO_RENDER',
'DEVINTERFACE_AUDIO_CAPTURE',
]
wasapi2_option = get_option('wasapi2')
if host_system != 'windows'
if wasapi2_option.disabled()
subdir_done()
elif wasapi2_option.enabled()
error('Cannot build wasapi2 plugin when not building for Windows')
endif
endif
ole32_dep = cc.find_library('ole32', required : get_option('wasapi2'))
ksuser_dep = cc.find_library('ksuser', required : get_option('wasapi2'))
runtimeobject_dep = cc.find_library('runtimeobject', required : get_option('wasapi2'))
mmdeviceapi_dep = cc.find_library('mmdevapi', required : get_option('wasapi2'))
wasapi2_dep = [ole32_dep, ksuser_dep, runtimeobject_dep, mmdeviceapi_dep]
have_symbols = false
foreach dep: wasapi2_dep
if not dep.found()
if wasapi2_option.enabled()
error('wasapi2 plugin was enabled explicitly, but required dependencies were not found')
else
subdir_done()
endif
endif
endforeach
if not cxx.has_header_symbol ('audioclient.h', 'IAudioClient3', dependencies : wasapi2_dep)
if wasapi2_option.enabled()
error('wasapi2 plugin was enabled explicitly, but IAudioClient3 is unavailable')
else
subdir_done()
endif
endif
foreach symbol: mmdeviceapi_symbols
if not cxx.has_header_symbol ('mmdeviceapi.h', symbol, dependencies : wasapi2_dep)
if wasapi2_option.enabled()
error('wasapi2 plugin was enabled explicitly, but @1@ is unavailable'.format(symbol))
else
subdir_done()
endif
endif
endforeach
winapi_app = cxx.compiles('''#include <winapifamily.h>
#include <windows.applicationmodel.core.h>
#include <wrl.h>
#include <wrl/wrappers/corewrappers.h>
#include <audioclient.h>
#include <mmdeviceapi.h>
#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#error "not winrt"
#endif
int main (int argc, char ** argv) {
IAudioClient3 *client = NULL;
return 0;
} ''',
dependencies: wasapi2_dep,
name: 'checking if building winapi-partiion-app')
if not winapi_app
if wasapi2_option.enabled()
error('wasapi2 plugin was enabled explicitly, but build target is not include WINAPI_PARTITION_APP')
else
subdir_done()
endif
endif
gstwasapi2 = library('gstwasapi2',
wasapi2_sources,
c_args : gst_plugins_bad_args + ['-DCOBJMACROS'],
cpp_args : gst_plugins_bad_args,
include_directories : [configinc],
dependencies : [gstaudio_dep] + wasapi2_dep,
install : true,
install_dir : plugins_install_dir)
pkgconfig.generate(gstwasapi2, install_dir : plugins_pkgconfig_install_dir)
plugins += [gstwasapi2]

56
sys/wasapi2/plugin.c Normal file
View file

@ -0,0 +1,56 @@
/* GStreamer
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <winapifamily.h>
#include "gstwasapi2sink.h"
#include "gstwasapi2src.h"
GST_DEBUG_CATEGORY (gst_wasapi2_debug);
GST_DEBUG_CATEGORY (gst_wasapi2_client_debug);
static gboolean
plugin_init (GstPlugin * plugin)
{
GstRank rank = GST_RANK_SECONDARY;
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
/* If we are building for UWP, wasapi2 plugin should have the highest rank */
rank = GST_RANK_PRIMARY + 1;
#endif
GST_DEBUG_CATEGORY_INIT (gst_wasapi2_debug, "wasapi2", 0, "wasapi2");
GST_DEBUG_CATEGORY_INIT (gst_wasapi2_client_debug, "wasapi2client",
0, "wasapi2client");
gst_element_register (plugin, "wasapi2sink", rank, GST_TYPE_WASAPI2_SINK);
gst_element_register (plugin, "wasapi2src", rank, GST_TYPE_WASAPI2_SRC);
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
wasapi2,
"Windows audio session API plugin",
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)