Skip to content

Commit

Permalink
Destroy PCM controller IO watch upon PCM release
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jan 13, 2024
1 parent d9918e4 commit e64ae50
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
23 changes: 13 additions & 10 deletions src/ba-transport-pcm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - ba-transport-pcm.c
* Copyright (c) 2016-2023 Arkadiusz Bokowy
* Copyright (c) 2016-2024 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -437,19 +437,22 @@ void ba_transport_pcm_stop(
int ba_transport_pcm_release(struct ba_transport_pcm *pcm) {

#if DEBUG
if (pcm->t->profile != BA_TRANSPORT_PROFILE_NONE)
/* assert that we were called with the lock held */
g_assert_cmpint(pthread_mutex_trylock(&pcm->mutex), !=, 0);
/* assert that we were called with the lock held */
g_assert_cmpint(pthread_mutex_trylock(&pcm->mutex), !=, 0);
#endif

if (pcm->fd == -1)
goto final;
if (pcm->fd != -1) {
debug("Closing PCM: %d", pcm->fd);
close(pcm->fd);
pcm->fd = -1;
}

debug("Closing PCM: %d", pcm->fd);
close(pcm->fd);
pcm->fd = -1;
if (pcm->controller != NULL) {
g_source_destroy(pcm->controller);
g_source_unref(pcm->controller);
pcm->controller = NULL;
}

final:
return 0;
}

Expand Down
5 changes: 4 additions & 1 deletion src/ba-transport-pcm.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - ba-transport-pcm.h
* Copyright (c) 2016-2023 Arkadiusz Bokowy
* Copyright (c) 2016-2024 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -133,6 +133,9 @@ struct ba_transport_pcm {
/* new PCM client mutex */
pthread_mutex_t client_mtx;

/* source watch for controller socket */
GSource *controller;

/* actual thread ID */
pthread_t tid;

Expand Down
10 changes: 6 additions & 4 deletions src/bluealsa-dbus.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - bluealsa-dbus.c
* Copyright (c) 2016-2023 Arkadiusz Bokowy
* Copyright (c) 2016-2024 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -475,22 +475,24 @@ static void bluealsa_pcm_open(GDBusMethodInvocation *inv, void *userdata) {
}

pthread_mutex_lock(&pcm->mutex);

/* get correct PIPE endpoint - PIPE is unidirectional */
pcm->fd = pcm_fds[is_sink ? 0 : 1];
/* set newly opened PCM as active */
pcm->paused = false;
pthread_mutex_unlock(&pcm->mutex);

GIOChannel *ch = g_io_channel_unix_new(pcm_fds[2]);
g_io_channel_set_close_on_unref(ch, TRUE);
g_io_channel_set_encoding(ch, NULL, NULL);
g_io_channel_set_buffered(ch, FALSE);

g_io_add_watch_full(ch, G_PRIORITY_DEFAULT, G_IO_IN,
bluealsa_pcm_controller, ba_transport_pcm_ref(pcm),
pcm->controller = g_io_create_watch_full(ch, G_PRIORITY_DEFAULT,
G_IO_IN, bluealsa_pcm_controller, ba_transport_pcm_ref(pcm),
(GDestroyNotify)ba_transport_pcm_unref);
g_io_channel_unref(ch);

pthread_mutex_unlock(&pcm->mutex);

/* notify our audio thread that the FIFO is ready */
ba_transport_pcm_signal_send(pcm, BA_TRANSPORT_PCM_SIGNAL_OPEN);

Expand Down
26 changes: 24 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - utils.c
* Copyright (c) 2016-2023 Arkadiusz Bokowy
* Copyright (c) 2016-2024 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand All @@ -9,7 +9,10 @@
*/

#include "utils.h"
/* IWYU pragma: no_include "config.h" */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <ctype.h>
#include <stdbool.h>
Expand Down Expand Up @@ -95,6 +98,25 @@ bool g_variant_validate_value(GVariant *value, const GVariantType *type,
return false;
}

/**
* Create a new watch source for the given I/O channel.
*
* @param channel A pointer to the GIOChannel.
* @param priority The priority of the source.
* @param cond The condition to watch for.
* @param func The function to call when the condition is satisfied.
* @param userdata Data to pass to the function.
* @param notify Function to call when the source is destroyed.
* @return New watch source. */
GSource *g_io_create_watch_full(GIOChannel *channel, int priority,
GIOCondition cond, GIOFunc func, void *userdata, GDestroyNotify notify) {
GSource *watch = g_io_create_watch(channel, cond);
g_source_set_callback(watch, G_SOURCE_FUNC(func), userdata, notify);
g_source_set_priority(watch, priority);
g_source_attach(watch, NULL);
return watch;
}

/**
* Convert a pointer to BT address to a hash value.
*
Expand Down
3 changes: 3 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ char *g_variant_sanitize_object_path(char *path);
bool g_variant_validate_value(GVariant *value, const GVariantType *type,
const char *name);

GSource *g_io_create_watch_full(GIOChannel *channel, int priority,
GIOCondition cond, GIOFunc func, void *userdata, GDestroyNotify notify);

unsigned int g_bdaddr_hash(const void *v);
gboolean g_bdaddr_equal(const void *v1, const void *v2);

Expand Down
4 changes: 2 additions & 2 deletions test/test-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static void *pcm_write_frames_sndfile_async(void *userdata) {
};

if (aging_duration == 0) {
/* If we are not performign aging test, close the PCM right
/* If we are not performing aging test, close the PCM right
* away. The reading loop will not wait for timeout. */
pthread_mutex_lock(&pcm->mutex);
ba_transport_pcm_release(pcm);
Expand Down Expand Up @@ -329,7 +329,7 @@ static void pcm_write_frames(struct ba_transport_pcm *pcm, size_t frames) {
}

if (aging_duration == 0) {
/* If we are not performign aging test, close the PCM right
/* If we are not performing aging test, close the PCM right
* away. The reading loop will not wait for timeout. */
pthread_mutex_lock(&pcm->mutex);
ba_transport_pcm_release(pcm);
Expand Down

0 comments on commit e64ae50

Please sign in to comment.