Commit e20ad13f authored by Martin Tobias Holmedahl Sandsmark's avatar Martin Tobias Holmedahl Sandsmark Committed by Jonathan Marten
Browse files

Use QtPaMainLoop instead of a full GLib event loop just for PulseAudio

Replaces Qt's GLib event loop with a simple wrapper that maps
PulseAudio timers, events etc. to the corresponding Qt stuff.

Does not stop the Glib library being loaded (because if Qt is built to
use it then it becomes a dependency of qtcore), but allows it to work
with QT_NO_GLIB set, removes the Glib dependency from KMix itself, and
integrates better with Qt in general (prioritization of events etc.).

Differential Revision: https://phabricator.kde.org/D28982
parent 1651d9b8
......@@ -58,11 +58,6 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
#
find_package(PulseAudio "${PA_MIN_VERSION}")
# PulseAudio requires GLib2
if (PulseAudio_FOUND)
find_package(GLIB2 REQUIRED)
endif(PulseAudio_FOUND)
# Canberra is an optional dependency
find_package(Canberra)
......@@ -99,7 +94,6 @@ endif (ALSA_FOUND)
if (PulseAudio_FOUND)
add_definitions(-DHAVE_PULSE)
include_directories(${PulseAudio_INCLUDE_DIRS})
include_directories(${GLIB2_INCLUDE_DIR})
endif (PulseAudio_FOUND)
if (CANBERRA_FOUND)
......@@ -213,7 +207,7 @@ if (HAVE_LIBASOUND2)
endif (HAVE_LIBASOUND2)
if (PulseAudio_FOUND)
target_link_libraries(kmixcore PRIVATE ${PulseAudio_LIBRARIES} ${PulseAudio_MAINLOOP_LIBRARY} ${GLIB2_LIBRARIES})
target_link_libraries(kmixcore PRIVATE ${PulseAudio_LIBRARIES} ${PulseAudio_MAINLOOP_LIBRARY})
endif (PulseAudio_FOUND)
if (CANBERRA_FOUND)
......
......@@ -21,6 +21,8 @@
#include "mixer_pulse.h"
#include "qtpamainloop.h"
#include <cstdlib>
#include <QAbstractEventDispatcher>
#include <QTimer>
......@@ -32,7 +34,6 @@
#include "core/ControlManager.h"
#include "core/GlobalConfig.h"
#include <pulse/glib-mainloop.h>
#include <pulse/ext-stream-restore.h>
#if defined(HAVE_CANBERRA)
# include <canberra.h>
......@@ -55,7 +56,6 @@
#define KMIXPA_EVENT_KEY "sink-input-by-media-role:event"
static unsigned int refcount = 0;
static pa_glib_mainloop *s_mainloop = NULL;
static pa_context *s_context = NULL;
static enum { UNKNOWN, ACTIVE, INACTIVE } s_pulseActive = UNKNOWN;
static int s_outstandingRequests = 0;
......@@ -689,8 +689,6 @@ static void context_state_callback(pa_context *c, void *)
if (s_context != c) {
pa_context_disconnect(c);
} else {
// If we're not probing, it means we've been disconnected from our
// GLib context
pa_context_unref(s_context);
s_context = NULL;
......@@ -956,10 +954,14 @@ bool Mixer_PULSE::connectToDaemon()
Q_ASSERT(NULL == s_context);
qCDebug(KMIX_LOG) << "Attempting connection to PulseAudio sound daemon";
pa_mainloop_api *api = pa_glib_mainloop_get_api(s_mainloop);
Q_ASSERT(api);
s_context = pa_context_new(api, "KMix");
// No need to create this until necessary
if (!m_mainloop) {
// When bumping c++ requirement to c++14,
// replace with `= std::make_unique<QtPaMainLoop>();`
m_mainloop.reset(new QtPaMainLoop);
}
s_context = pa_context_new(&m_mainloop->pa_vtable, "KMix");
Q_ASSERT(s_context);
if (pa_context_connect(s_context, NULL, PA_CONTEXT_NOFAIL, 0) < 0) {
......@@ -981,13 +983,6 @@ Mixer_PULSE::Mixer_PULSE(Mixer *mixer, int devnum) : Mixer_Backend(mixer, devnum
if (pulseenv.toInt())
s_pulseActive = INACTIVE;
// We require a glib event loop
if (!QByteArray(QAbstractEventDispatcher::instance()->metaObject()->className()).contains("EventDispatcherGlib") &&
!QByteArray(QAbstractEventDispatcher::instance()->metaObject()->className()).contains("GlibEventDispatcher")) {
qCDebug(KMIX_LOG) << "Disabling PulseAudio integration for lack of GLib event loop";
s_pulseActive = INACTIVE;
}
++refcount;
if (INACTIVE != s_pulseActive && 1 == refcount)
{
......@@ -1040,9 +1035,6 @@ Mixer_PULSE::Mixer_PULSE(Mixer *mixer, int devnum) : Mixer_Backend(mixer, devnum
if (INACTIVE != s_pulseActive)
{
// Reconnect via integrated mainloop
s_mainloop = pa_glib_mainloop_new(NULL);
Q_ASSERT(s_mainloop);
connectToDaemon();
#if defined(HAVE_CANBERRA)
......@@ -1082,11 +1074,6 @@ Mixer_PULSE::~Mixer_PULSE()
pa_context_unref(s_context);
s_context = NULL;
}
if (s_mainloop) {
pa_glib_mainloop_free(s_mainloop);
s_mainloop = NULL;
}
}
}
......
......@@ -27,6 +27,8 @@
#include "mixer_backend.h"
#include <pulse/pulseaudio.h>
struct QtPaMainLoop;
typedef QMap<uint8_t,Volume::ChannelID> chanIDMap;
typedef struct {
int index;
......@@ -77,6 +79,7 @@ class Mixer_PULSE : public Mixer_Backend
int fd;
QString _id;
std::unique_ptr<QtPaMainLoop> m_mainloop;
private:
bool addDevice(devinfo& dev, bool isAppStream = false);
......
......@@ -31,6 +31,6 @@ if (HAVE_LIBASOUND2)
endif (HAVE_LIBASOUND2)
if (PULSEAUDIO_FOUND)
target_link_libraries(profiletest ${PULSEAUDIO_LIBRARY} ${PULSEAUDIO_MAINLOOP_LIBRARY} ${GLIB2_LIBRARIES})
target_link_libraries(profiletest ${PULSEAUDIO_LIBRARY} ${PULSEAUDIO_MAINLOOP_LIBRARY})
endif (PULSEAUDIO_FOUND)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment