Add module-tizenaudio-sink2/source2 57/262057/3
authorJaechul Lee <jcsing.lee@samsung.com>
Mon, 2 Aug 2021 05:47:18 +0000 (14:47 +0900)
committerJaechul Lee <jcsing.lee@samsung.com>
Tue, 3 Aug 2021 00:20:24 +0000 (09:20 +0900)
module-tizenaudio-sink2/source2 that are copies of
tizenaudio-sink/source without AEC parts were added for a experimental
functionality such as mmap, echo cancel, and non-interleave.

Change-Id: I09588a5d90737da6e2ee9c8b3fd574db012e71f5
Signed-off-by: Jaechul Lee <jcsing.lee@samsung.com>
Makefile.am
packaging/pulseaudio-modules-tizen.spec
src/module-tizenaudio-sink2.c [new file with mode: 0644]
src/module-tizenaudio-source2.c [new file with mode: 0644]

index 495578e128a66b85c1ab184e9ec6c420a4deeabc..6869eaa9a188c1e243a8b77919baf18f2c831fce 100644 (file)
@@ -39,6 +39,8 @@ pulsemodlibexec_LTLIBRARIES = \
                libcommunicator.la \
                module-tizenaudio-sink.la \
                module-tizenaudio-source.la \
+               module-tizenaudio-sink2.la \
+               module-tizenaudio-source2.la \
                module-tizenaudio-policy.la \
                module-tizenaudio-discover.la \
                module-tizenaudio-publish.la \
@@ -76,6 +78,16 @@ module_tizenaudio_source_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_tizenaudio_source_la_LIBADD = $(MODULE_LIBADD) libhal-interface.la
 module_tizenaudio_source_la_CFLAGS = $(MODULE_CFLAGS) -DPA_MODULE_NAME=module_tizenaudio_source
 
+module_tizenaudio_sink2_la_SOURCES = src/module-tizenaudio-sink2.c
+module_tizenaudio_sink2_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_tizenaudio_sink2_la_LIBADD = $(MODULE_LIBADD) libhal-interface.la
+module_tizenaudio_sink2_la_CFLAGS = $(MODULE_CFLAGS) -DPA_MODULE_NAME=module_tizenaudio_sink2
+
+module_tizenaudio_source2_la_SOURCES = src/module-tizenaudio-source2.c
+module_tizenaudio_source2_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_tizenaudio_source2_la_LIBADD = $(MODULE_LIBADD) libhal-interface.la
+module_tizenaudio_source2_la_CFLAGS = $(MODULE_CFLAGS) -DPA_MODULE_NAME=module_tizenaudio_source2
+
 module_sound_player_la_SOURCES = src/module-sound-player.c
 module_sound_player_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_sound_player_la_LIBADD = $(MODULE_LIBADD) $(DBUS_LIBS)
index 2d54e446c8ccaceb72adf20cec8d1f137188a3ff..df5e515bf747e76e7af8cc4849de8e8ae098daf6 100644 (file)
@@ -80,6 +80,8 @@ install -m 0644 %SOURCE1 %{buildroot}%{_tmpfilesdir}/pulseaudio.conf
 %{_libdir}/pulse-13.0/modules/module-tizenaudio-policy.so
 %{_libdir}/pulse-13.0/modules/module-tizenaudio-sink.so
 %{_libdir}/pulse-13.0/modules/module-tizenaudio-source.so
+%{_libdir}/pulse-13.0/modules/module-tizenaudio-sink2.so
+%{_libdir}/pulse-13.0/modules/module-tizenaudio-source2.so
 %{_libdir}/pulse-13.0/modules/module-tizenaudio-discover.so
 %{_libdir}/pulse-13.0/modules/module-tizenaudio-publish.so
 %{_libdir}/pulse-13.0/modules/libhal-interface.so
diff --git a/src/module-tizenaudio-sink2.c b/src/module-tizenaudio-sink2.c
new file mode 100644 (file)
index 0000000..f8a6059
--- /dev/null
@@ -0,0 +1,704 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+#include <pulse/util.h>
+
+#include <pulsecore/i18n.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/sink.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
+
+#include "hal-interface.h"
+
+PA_MODULE_AUTHOR("Tizen");
+PA_MODULE_DESCRIPTION("Tizen Audio Sink");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(false);
+PA_MODULE_USAGE(
+        "sink_name=<name of sink> "
+        "sink_properties=<properties for the sink> "
+        "device=<device to use, card comma device (e.g. 0,0)> "
+        "format=<sample format> "
+        "rate=<sample rate> "
+        "channels=<number of channels> "
+        "channel_map=<channel map>"
+        "fragments=<number of fragments> "
+        "fragment_size=<fragment size> "
+        "block_msec=<sink keep> "
+        "max_request_msec=<maximum buffer request of device> ");
+
+#define DEFAULT_SINK_NAME "tizenaudio-sink"
+
+/* block_usec should be less than amount of fragment_size * fragments */
+#define DEFAULT_BLOCK_USEC (PA_USEC_PER_SEC * 0.05)
+
+/* Sink device consumes that amount of maximum buffer at every request */
+#define DEFAULT_MAX_REQUEST_USEC (PA_USEC_PER_SEC * 0.032)
+
+#define DEVICE_NAME_MAX                     30
+
+struct userdata {
+    pa_core *core;
+    pa_module *module;
+    pa_sink *sink;
+
+    pa_thread *thread;
+    pa_thread_mq thread_mq;
+    pa_rtpoll *rtpoll;
+
+    void *pcm_handle;
+    uint32_t nfrags;
+    uint32_t frag_size;
+
+    pa_usec_t block_usec;
+    pa_usec_t max_request_usec;
+    pa_usec_t timestamp;
+    pa_usec_t timestamp_written;
+
+    char* card;
+    char* device;
+    bool first;
+
+    pa_rtpoll_item *rtpoll_item;
+
+    uint64_t write_count;
+    pa_hal_interface *hal_interface;
+};
+
+static const char* const valid_modargs[] = {
+    "sink_name",
+    "sink_properties",
+    "device",
+    "format",
+    "rate",
+    "channels",
+    "channel_map",
+    "fragments",
+    "fragment_size",
+    "block_msec",
+    "max_request_msec",
+    NULL
+};
+
+static int build_pollfd(struct userdata *u) {
+    int32_t ret;
+    struct pollfd *pollfd;
+    int fd = -1;
+
+    pa_assert(u);
+    pa_assert(u->pcm_handle);
+    pa_assert(u->rtpoll);
+
+    if (u->rtpoll_item)
+        pa_rtpoll_item_free(u->rtpoll_item);
+
+    u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+    pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+    ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
+    if (ret < 0 || fd < 0) {
+        pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
+        return -1;
+    }
+    pollfd->fd = fd;
+    pollfd->events = /* POLLOUT | */ POLLERR | POLLNVAL;
+
+    return 0;
+}
+
+/* Called from IO context */
+static int suspend(struct userdata *u) {
+    int32_t ret;
+    pa_assert(u);
+    pa_assert(u->pcm_handle);
+
+    ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
+    if (ret) {
+        pa_log_error("Error closing PCM device %x", ret);
+    }
+    u->pcm_handle = NULL;
+
+    if (u->rtpoll_item) {
+        pa_rtpoll_item_free(u->rtpoll_item);
+        u->rtpoll_item = NULL;
+    }
+
+    pa_log_info("Device suspended...");
+
+    return 0;
+}
+
+/* Called from IO context */
+static int unsuspend(struct userdata *u) {
+    pa_sample_spec sample_spec;
+    int32_t ret;
+    size_t frame_size;
+
+    pa_assert(u);
+    pa_assert(!u->pcm_handle);
+
+    pa_log_info("Trying resume...");
+
+    sample_spec = u->sink->sample_spec;
+    frame_size = pa_frame_size(&sample_spec);
+    if (frame_size == 0) {
+        pa_log_error("Unexpected frame size zero!");
+        goto fail;
+    }
+
+    ret = pa_hal_interface_pcm_open(u->hal_interface,
+              u->card,
+              u->device,
+              DIRECTION_OUT,
+              &sample_spec,
+              u->frag_size / frame_size,
+              u->nfrags,
+              (void **)&u->pcm_handle);
+    if (ret) {
+        pa_log_error("Error opening PCM device %x", ret);
+        goto fail;
+    }
+
+    if (build_pollfd(u) < 0)
+        goto fail;
+
+    u->write_count = 0;
+    u->first = true;
+
+    pa_log_info("Resumed successfully...");
+
+    return 0;
+
+fail:
+    if (u->pcm_handle) {
+        pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
+        u->pcm_handle = NULL;
+    }
+    return -PA_ERR_IO;
+}
+
+/* Called from the IO thread. */
+static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
+    struct userdata *u;
+    int r;
+
+    pa_assert(s);
+    pa_assert_se(u = s->userdata);
+
+    /* It may be that only the suspend cause is changing, in which case there's
+     * nothing to do. */
+    if (new_state == s->thread_info.state)
+        return 0;
+
+    switch (new_state) {
+        case PA_SINK_SUSPENDED: {
+            pa_assert(PA_SINK_IS_OPENED(s->thread_info.state));
+            if ((r = suspend(u)) < 0)
+                return r;
+            break;
+        }
+
+        case PA_SINK_IDLE:
+        case PA_SINK_RUNNING: {
+            if (s->thread_info.state == PA_SINK_INIT) {
+                if (build_pollfd(u) < 0)
+                    return -PA_ERR_IO;
+            }
+
+            if (s->thread_info.state == PA_SINK_SUSPENDED) {
+                if ((r = unsuspend(u)) < 0)
+                    return r;
+            }
+            break;
+        }
+
+        case PA_SINK_UNLINKED:
+        case PA_SINK_INIT:
+        case PA_SINK_INVALID_STATE:
+            break;
+    }
+
+    return 0;
+}
+
+static int sink_process_msg(
+        pa_msgobject *o,
+        int code,
+        void *data,
+        int64_t offset,
+        pa_memchunk *chunk) {
+
+    struct userdata *u = PA_SINK(o)->userdata;
+
+    switch (code) {
+        case PA_SINK_MESSAGE_GET_LATENCY: {
+            pa_usec_t now = pa_rtclock_now();
+            pa_usec_t latency = 0ULL;
+            if (u->timestamp > now) {
+                if ((u->timestamp - now) > (now - u->timestamp_written)) {
+                    latency = (u->timestamp - now) + (u->timestamp_written - now);
+                }
+            }
+            *((pa_usec_t*)data) = latency;
+            return 0;
+        }
+    }
+
+    return pa_sink_process_msg(o, code, data, offset, chunk);
+}
+
+static void sink_update_requested_latency_cb(pa_sink *s) {
+    struct userdata *u;
+
+    pa_sink_assert_ref(s);
+    pa_assert_se((u = s->userdata));
+
+    u->block_usec = pa_sink_get_requested_latency_within_thread(s);
+
+    if (u->block_usec == (pa_usec_t)-1)
+        u->block_usec = s->thread_info.max_latency;
+
+    pa_sink_set_max_rewind_within_thread(s, pa_usec_to_bytes(u->block_usec, &s->sample_spec));
+    pa_sink_set_max_request_within_thread(s, pa_usec_to_bytes(u->max_request_usec, &s->sample_spec));
+}
+
+static void process_rewind(struct userdata *u, pa_usec_t now) {
+#if 1
+    /* Rewind not supported */
+    pa_sink_process_rewind(u->sink, 0);
+#else
+    size_t rewind_nbytes, in_buffer;
+    pa_usec_t delay;
+
+    pa_assert(u);
+
+    rewind_nbytes = u->sink->thread_info.rewind_nbytes;
+
+    if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
+        goto do_nothing;
+
+    pa_log_debug("Requested to rewind %lu bytes.", (unsigned long)rewind_nbytes);
+
+    if (u->timestamp <= now)
+        goto do_nothing;
+
+    delay = u->timestamp - now;
+    in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
+
+    if (in_buffer <= 0)
+        goto do_nothing;
+
+    if (rewind_nbytes > in_buffer)
+        rewind_nbytes = in_buffer;
+
+    pa_sink_process_rewind(u->sink, rewind_nbytes);
+    u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
+
+    pa_log_debug("Rewound %lu bytes.", (unsigned long)rewind_nbytes);
+    return;
+
+do_nothing:
+    pa_sink_process_rewind(u->sink, 0);
+#endif
+}
+
+static int process_render(struct userdata *u, pa_usec_t now) {
+    int work_done = 0;
+    size_t ate = 0;
+    void *p;
+    size_t frames_to_write, frame_size;
+    uint32_t avail = 0;
+
+    pa_assert(u);
+
+    /* Fill the buffer up the latency size */
+    while (u->timestamp < now + u->block_usec) {
+        pa_memchunk chunk;
+        frame_size = pa_frame_size(&u->sink->sample_spec);
+        if (frame_size == 0) {
+            pa_log_error("Unexpected frame size zero!");
+            break;
+        }
+
+        pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
+        if ((avail == 0) && !(u->first)) {
+            break;
+        }
+
+        if (u->first) {
+            pa_log_debug("Fill initial buffer");
+
+            frames_to_write = u->frag_size / frame_size;
+            u->timestamp = u->timestamp_written = now;
+        } else {
+            frames_to_write = (u->frag_size / u->nfrags);
+
+            if (frames_to_write > avail)
+                break;
+        }
+
+        pa_sink_render_full(u->sink, frames_to_write * frame_size, &chunk);
+        p = pa_memblock_acquire(chunk.memblock);
+
+        pa_hal_interface_pcm_write(u->hal_interface, u->pcm_handle, (const char*)p + chunk.index, (uint32_t)frames_to_write);
+
+        pa_memblock_release(chunk.memblock);
+        pa_memblock_unref(chunk.memblock);
+        u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
+        u->timestamp_written = pa_rtclock_now();
+
+        work_done = 1;
+
+        ate += chunk.length;
+        if (ate >= u->sink->thread_info.max_request) {
+            break;
+        }
+    }
+
+    return work_done;
+}
+
+static void thread_func(void *userdata) {
+    struct userdata *u = userdata;
+    unsigned short revents = 0;
+
+    pa_assert(u);
+
+    pa_log_debug("Thread starting up");
+
+    if (u->core->realtime_scheduling)
+        pa_thread_make_realtime(u->core->realtime_priority);
+
+    pa_thread_mq_install(&u->thread_mq);
+
+    for (;;) {
+        pa_usec_t now = 0;
+        int ret;
+
+        if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+            now = pa_rtclock_now();
+
+        if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
+            process_rewind(u, now);
+
+        /* Render some data and drop it immediately */
+        if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+            int work_done = process_render(u, now);
+
+            if (work_done < 0)
+                goto fail;
+
+            if (work_done == 0) {
+                pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
+            } else {
+                if (u->first) {
+                    pa_log_info("Starting playback.");
+                    pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
+                    u->first = false;
+                }
+                pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
+            }
+        } else {
+            pa_rtpoll_set_timer_disabled(u->rtpoll);
+        }
+
+        /* Hmm, nothing to do. Let's sleep */
+        if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
+            goto fail;
+
+        if (ret == 0)
+            goto finish;
+
+        if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
+            struct pollfd *pollfd;
+            if (u->rtpoll_item) {
+                pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+                revents = pollfd->revents;
+                if (revents & ~POLLOUT) {
+                    pa_log_debug("Poll error 0x%x occured, try recover.", revents);
+                    pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
+                    u->first = true;
+                    revents = 0;
+                } else {
+                    //pa_log_debug("Poll wakeup.", revents);
+                }
+            }
+        }
+    }
+
+fail:
+    /* If this was no regular exit from the loop we have to continue
+     * processing messages until we received PA_MESSAGE_SHUTDOWN */
+    pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+    pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+    pa_log_debug("Thread shutting down");
+}
+
+static int parse_to_get_card(const char *modarg_device, char *card) {
+    const char *name_p;
+    char *card_p;
+
+    if (!strchr(modarg_device, ',')) {
+        pa_log_error("Failed to parse device argument : no comma");
+        return -1;
+    }
+
+    name_p = modarg_device;
+    card_p = card;
+    while (*name_p != ',')
+        *(card_p++) = *(name_p++);
+    *card_p = '\0';
+
+    return 0;
+}
+
+static int parse_to_get_device(const char *modarg_device, char *device) {
+    const char *comma_p;
+    char *device_p;
+
+    if (!(comma_p = strchr(modarg_device, ','))) {
+        pa_log_error("Failed to parse device argument : no comma");
+        return -1;
+    }
+
+    comma_p++;
+    device_p = device;
+    while (*comma_p != '\0')
+        *(device_p++) = *(comma_p++);
+    *device_p = '\0';
+
+    return 0;
+}
+
+int pa__init(pa_module*m) {
+    struct userdata *u = NULL;
+    pa_sample_spec ss;
+    pa_channel_map map;
+    pa_modargs *ma = NULL;
+    pa_sink_new_data data;
+    uint32_t alternate_sample_rate;
+    uint32_t block_msec;
+    uint32_t max_request_msec;
+    const char *modarg_device;
+    char card[DEVICE_NAME_MAX];
+    char device[DEVICE_NAME_MAX];
+    size_t frame_size, buffer_size, period_frames, buffer_frames;
+
+    pa_assert(m);
+
+    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+        pa_log_error("Failed to parse module arguments.");
+        goto fail;
+    }
+
+    ss = m->core->default_sample_spec;
+    map = m->core->default_channel_map;
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+        pa_log_error("Invalid sample format specification or channel map");
+        goto fail;
+    }
+
+    alternate_sample_rate = m->core->alternate_sample_rate;
+    if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
+        pa_log_error("Failed to parse alternate sample rate");
+        goto fail;
+    }
+
+    m->userdata = u = pa_xnew0(struct userdata, 1);
+    u->core = m->core;
+    u->module = m;
+    u->first = true;
+    u->hal_interface = pa_hal_interface_get(u->core);
+    u->rtpoll = pa_rtpoll_new();
+    pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+    if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
+        pa_log_error("device is invalid");
+        goto fail;
+    }
+
+    if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
+        pa_log_error("failed to parse device module argument, %s", modarg_device);
+        goto fail;
+    }
+
+    u->card = pa_xstrdup(card);
+    u->device = pa_xstrdup(device);
+
+    u->frag_size = (uint32_t) pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
+    u->nfrags = m->core->default_n_fragments;
+    if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
+        pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
+        pa_log_error("fragment_size or fragments are invalid.");
+        goto fail;
+    }
+    pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
+
+    /* Optional */
+    block_msec = DEFAULT_BLOCK_USEC / PA_USEC_PER_MSEC;
+    pa_modargs_get_value_u32(ma, "block_msec", &block_msec);
+    u->block_usec = (pa_usec_t) block_msec * PA_USEC_PER_MSEC;
+
+    /* Optional */
+    max_request_msec = DEFAULT_MAX_REQUEST_USEC / PA_USEC_PER_MSEC;
+    pa_modargs_get_value_u32(ma, "max_request_msec", &max_request_msec);
+    u->max_request_usec = (pa_usec_t) max_request_msec * PA_USEC_PER_MSEC;
+
+    u->timestamp = 0ULL;
+    u->timestamp_written = 0ULL;
+
+    pa_sink_new_data_init(&data);
+    data.driver = __FILE__;
+    data.module = m;
+    pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
+    pa_sink_new_data_set_sample_spec(&data, &ss);
+    pa_sink_new_data_set_channel_map(&data, &map);
+    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio sink"));
+    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
+    pa_proplist_sets(data.proplist, "tizen.card", u->card);
+    pa_proplist_sets(data.proplist, "tizen.device", u->device);
+    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
+
+    frame_size = pa_frame_size(&ss);
+    buffer_size = u->frag_size * u->nfrags;
+    buffer_frames = buffer_size / frame_size;
+    period_frames = u->frag_size / frame_size;
+    pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
+    pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
+
+    if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+        pa_log_error("Invalid properties.");
+        pa_sink_new_data_done(&data);
+        goto fail;
+    }
+
+    u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
+    pa_sink_new_data_done(&data);
+
+    if (!u->sink) {
+        pa_log_error("Failed to create sink object.");
+        goto fail;
+    }
+
+    u->sink->parent.process_msg = sink_process_msg;
+    u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
+    u->sink->update_requested_latency = sink_update_requested_latency_cb;
+    u->sink->userdata = u;
+
+    if (pa_hal_interface_pcm_open(u->hal_interface,
+              u->card,
+              u->device,
+              DIRECTION_OUT,
+              &u->sink->sample_spec,
+              u->frag_size / pa_frame_size(&u->sink->sample_spec),
+              u->nfrags,
+              (void **)&u->pcm_handle)) {
+        pa_log_error("Error opening PCM device");
+        goto fail;
+    }
+
+    pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+    pa_sink_set_rtpoll(u->sink, u->rtpoll);
+
+    pa_sink_set_max_rewind(u->sink, 0);
+    pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->max_request_usec, &u->sink->sample_spec));
+
+    if (!(u->thread = pa_thread_new("tizenaudio-sink", thread_func, u))) {
+        pa_log_error("Failed to create thread.");
+        goto fail;
+    }
+    pa_sink_set_fixed_latency(u->sink, u->block_usec);
+    pa_sink_put(u->sink);
+    pa_modargs_free(ma);
+
+    return 0;
+
+fail:
+    if (ma)
+        pa_modargs_free(ma);
+
+    pa__done(m);
+    return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+    struct userdata *u;
+
+    pa_assert(m);
+    pa_assert_se((u = m->userdata));
+
+    return pa_sink_linked_by(u->sink);
+}
+
+void pa__done(pa_module*m) {
+    struct userdata *u;
+
+    pa_assert(m);
+
+    if (!(u = m->userdata))
+        return;
+
+    if (u->sink)
+        pa_sink_unlink(u->sink);
+
+    if (u->thread) {
+        pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+        pa_thread_free(u->thread);
+    }
+
+    pa_thread_mq_done(&u->thread_mq);
+
+    if (u->sink)
+        pa_sink_unref(u->sink);
+
+    pa_xfree(u->card);
+    pa_xfree(u->device);
+
+    if (u->rtpoll)
+        pa_rtpoll_free(u->rtpoll);
+
+    if (u->pcm_handle) {
+        pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
+        pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
+    }
+
+    pa_xfree(u);
+}
diff --git a/src/module-tizenaudio-source2.c b/src/module-tizenaudio-source2.c
new file mode 100644 (file)
index 0000000..790e2b2
--- /dev/null
@@ -0,0 +1,636 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio 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
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <pulse/rtclock.h>
+#include <pulse/timeval.h>
+#include <pulse/xmalloc.h>
+#include <pulse/util.h>
+
+#include <pulsecore/i18n.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/source.h>
+#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
+
+#include "hal-interface.h"
+
+PA_MODULE_AUTHOR("Tizen");
+PA_MODULE_DESCRIPTION("Tizen Audio Source");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(false);
+PA_MODULE_USAGE(
+        "source_name=<name of source> "
+        "source_properties=<properties for the source> "
+        "device=<device to use, card comma device (e.g. 0,0)> "
+        "format=<sample format> "
+        "rate=<sample rate> "
+        "channels=<number of channels> "
+        "channel_map=<channel map>"
+        "fragments=<number of fragments> "
+        "fragment_size=<fragment size> ");
+
+
+#define DEFAULT_SOURCE_NAME "tizenaudio-source"
+
+/* BLOCK_USEC should be less than amount of fragment_size * fragments */
+#define BLOCK_USEC (PA_USEC_PER_SEC * 0.032)
+
+#define DEVICE_NAME_MAX                     30
+
+struct userdata {
+    pa_core *core;
+    pa_module *module;
+    pa_source *source;
+
+    pa_thread *thread;
+    pa_thread_mq thread_mq;
+    pa_rtpoll *rtpoll;
+
+    void *pcm_handle;
+    uint32_t nfrags;
+    uint32_t frag_size;
+
+    pa_usec_t block_usec;
+    pa_usec_t timestamp;
+
+    char* card;
+    char* device;
+    bool first;
+
+    pa_rtpoll_item *rtpoll_item;
+
+    uint64_t read_count;
+    pa_usec_t latency_time;
+    pa_hal_interface *hal_interface;
+};
+
+static const char* const valid_modargs[] = {
+    "source_name",
+    "source_properties",
+    "device",
+    "format",
+    "rate",
+    "channels",
+    "channel_map",
+    "fragments",
+    "fragment_size",
+    NULL
+};
+
+static int build_pollfd(struct userdata *u) {
+    int32_t ret;
+    struct pollfd *pollfd;
+    int fd = -1;
+
+    pa_assert(u);
+    pa_assert(u->pcm_handle);
+    pa_assert(u->rtpoll);
+
+    if (u->rtpoll_item)
+        pa_rtpoll_item_free(u->rtpoll_item);
+
+    u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+    pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+    ret = pa_hal_interface_pcm_get_fd(u->hal_interface, u->pcm_handle, &fd);
+    if (ret < 0 || fd < 0) {
+        pa_log_error("Failed to get fd(%d) of PCM device %d", fd, ret);
+        return -1;
+    }
+    pollfd->fd = fd;
+    pollfd->events = /* POLLIN | */ POLLERR | POLLNVAL;
+
+    return 0;
+}
+
+/* Called from IO context */
+static int suspend(struct userdata *u) {
+    int32_t ret;
+    pa_assert(u);
+    pa_assert(u->pcm_handle);
+
+    ret = pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
+    if (ret) {
+        pa_log_error("Error closing PCM device %x", ret);
+    }
+    u->pcm_handle = NULL;
+
+    if (u->rtpoll_item) {
+        pa_rtpoll_item_free(u->rtpoll_item);
+        u->rtpoll_item = NULL;
+    }
+
+    pa_log_info("Device suspended...");
+
+    return 0;
+}
+
+/* Called from IO context */
+static int unsuspend(struct userdata *u) {
+    pa_sample_spec sample_spec;
+    int32_t ret;
+    size_t frame_size;
+
+    pa_assert(u);
+    pa_assert(!u->pcm_handle);
+
+    pa_log_info("Trying resume...");
+
+    sample_spec = u->source->sample_spec;
+    frame_size = pa_frame_size(&sample_spec);
+    if (frame_size == 0) {
+        pa_log_error("Unexpected frame size zero!");
+        goto fail;
+    }
+
+    ret = pa_hal_interface_pcm_open(u->hal_interface,
+              u->card,
+              u->device,
+              DIRECTION_IN,
+              &sample_spec,
+              u->frag_size / frame_size,
+              u->nfrags,
+              (void **)&u->pcm_handle);
+    if (ret) {
+        pa_log_error("Error opening PCM device %x", ret);
+        goto fail;
+    }
+
+    if (build_pollfd(u) < 0)
+        goto fail;
+
+    u->read_count = 0;
+    u->first = true;
+
+    pa_log_info("Resumed successfully...");
+
+    return 0;
+
+fail:
+    if (u->pcm_handle) {
+        pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
+        u->pcm_handle = NULL;
+    }
+    return -PA_ERR_IO;
+}
+
+/* Called from the IO thread. */
+static int source_set_state_in_io_thread_cb(pa_source *s, pa_source_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
+    struct userdata *u;
+    int r;
+
+    pa_assert(s);
+    pa_assert_se(u = s->userdata);
+
+    /* It may be that only the suspend cause is changing, in which case there's
+     * nothing more to do. */
+    if (new_state == s->thread_info.state)
+        return 0;
+
+    switch (new_state) {
+        case PA_SOURCE_SUSPENDED: {
+            pa_assert(PA_SOURCE_IS_OPENED(s->thread_info.state));
+            if ((r = suspend(u)) < 0)
+                return r;
+            break;
+        }
+
+        case PA_SOURCE_IDLE:
+        case PA_SOURCE_RUNNING: {
+            if (s->thread_info.state == PA_SOURCE_INIT) {
+                if (build_pollfd(u) < 0)
+                    return -PA_ERR_IO;
+            }
+
+            if (s->thread_info.state == PA_SOURCE_SUSPENDED) {
+                if ((r = unsuspend(u)) < 0)
+                    return r;
+            }
+            break;
+        }
+
+        case PA_SOURCE_UNLINKED:
+        case PA_SOURCE_INIT:
+        case PA_SOURCE_INVALID_STATE:
+            break;
+    }
+
+    return 0;
+}
+
+static int source_process_msg(
+        pa_msgobject *o,
+        int code,
+        void *data,
+        int64_t offset,
+        pa_memchunk *chunk) {
+
+    struct userdata *u = PA_SOURCE(o)->userdata;
+
+    switch (code) {
+        case PA_SOURCE_MESSAGE_GET_LATENCY: {
+            pa_usec_t now = pa_rtclock_now();
+            *((pa_usec_t*)data) = u->timestamp > now ? 0ULL : now - u->timestamp;
+            return 0;
+        }
+    }
+
+    return pa_source_process_msg(o, code, data, offset, chunk);
+}
+
+static void source_update_requested_latency_cb(pa_source *s) {
+    struct userdata *u;
+    size_t nbytes;
+
+    pa_source_assert_ref(s);
+    pa_assert_se((u = s->userdata));
+
+    u->block_usec = pa_source_get_requested_latency_within_thread(s);
+
+    if (u->block_usec == (pa_usec_t)-1)
+        u->block_usec = s->thread_info.max_latency;
+
+    nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
+    pa_source_set_max_rewind_within_thread(s, nbytes);
+}
+
+static int process_render(struct userdata *u, pa_usec_t now) {
+    int work_done = 0;
+    size_t ate = 0;
+    void *p;
+    size_t frames_to_read, frame_size;
+    uint32_t avail = 0;
+
+    pa_assert(u);
+
+    /* Fill the buffer up the latency size */
+    while (u->timestamp < now + u->block_usec) {
+        pa_memchunk chunk;
+        frame_size = pa_frame_size(&u->source->sample_spec);
+        if (frame_size == 0) {
+            pa_log_error("Unexpected frame size zero!");
+            break;
+        }
+
+        pa_hal_interface_pcm_available(u->hal_interface, u->pcm_handle, &avail);
+        if (avail == 0) {
+            break;
+        }
+
+        frames_to_read = (u->frag_size / u->nfrags);
+        chunk.length = frames_to_read * frame_size;
+        chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
+
+        if (frames_to_read > (size_t)avail)
+            frames_to_read = (size_t)avail;
+
+        p = pa_memblock_acquire(chunk.memblock);
+        pa_hal_interface_pcm_read(u->hal_interface, u->pcm_handle, p, (uint32_t)frames_to_read);
+        pa_memblock_release(chunk.memblock);
+
+        chunk.index = 0;
+        chunk.length = (size_t)frames_to_read * frame_size;
+        pa_source_post(u->source, &chunk);
+        pa_memblock_unref(chunk.memblock);
+
+        u->timestamp += pa_bytes_to_usec(chunk.length, &u->source->sample_spec);
+
+        work_done = 1;
+
+        ate += chunk.length;
+        if (ate >= pa_usec_to_bytes(u->block_usec, &u->source->sample_spec)) {
+            break;
+        }
+    }
+
+    return work_done;
+}
+
+static void thread_func(void *userdata) {
+    struct userdata *u = userdata;
+    unsigned short revents = 0;
+
+    pa_assert(u);
+    pa_log_debug("Thread starting up");
+
+    if (u->core->realtime_scheduling)
+        pa_thread_make_realtime(u->core->realtime_priority);
+
+    pa_thread_mq_install(&u->thread_mq);
+
+    for (;;) {
+        pa_usec_t now = 0;
+        int ret;
+
+        if (PA_SOURCE_IS_OPENED(u->source->thread_info.state))
+            now = pa_rtclock_now();
+
+        /* Render some data and drop it immediately */
+        if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+            int work_done;
+
+            if (u->first) {
+                pa_log_info("Starting capture.");
+                pa_hal_interface_pcm_start(u->hal_interface, u->pcm_handle);
+                u->first = false;
+                u->timestamp = now;
+            }
+
+            work_done = process_render(u, now);
+
+            if (work_done < 0)
+                goto fail;
+
+            if (work_done == 0) {
+                pa_rtpoll_set_timer_relative(u->rtpoll, (20 * PA_USEC_PER_MSEC));
+            } else {
+                pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
+            }
+        } else {
+            pa_rtpoll_set_timer_disabled(u->rtpoll);
+        }
+
+        /* Hmm, nothing to do. Let's sleep */
+        if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
+            goto fail;
+
+        if (ret == 0)
+            goto finish;
+
+        if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
+            struct pollfd *pollfd;
+            if (u->rtpoll_item) {
+                pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+                revents = pollfd->revents;
+                if (revents & ~POLLIN) {
+                    pa_log_debug("Poll error 0x%x occured, try recover.", revents);
+                    pa_hal_interface_pcm_recover(u->hal_interface, u->pcm_handle, revents);
+                    u->first = true;
+                    revents = 0;
+                } else {
+                    //pa_log_debug("Poll wakeup.", revents);
+                }
+            }
+        }
+    }
+
+fail:
+    /* If this was no regular exit from the loop we have to continue
+     * processing messages until we received PA_MESSAGE_SHUTDOWN */
+    pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
+    pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+    pa_log_debug("Thread shutting down");
+}
+
+static int parse_to_get_card(const char *modarg_device, char *card) {
+    const char *name_p;
+    char *card_p;
+
+    if (!strchr(modarg_device, ',')) {
+        pa_log_error("Failed to parse device argument : no comma");
+        return -1;
+    }
+
+    name_p = modarg_device;
+    card_p = card;
+    while (*name_p != ',')
+        *(card_p++) = *(name_p++);
+    *card_p = '\0';
+
+    return 0;
+}
+
+static int parse_to_get_device(const char *modarg_device, char *device) {
+    const char *comma_p;
+    char *device_p;
+
+    if (!(comma_p = strchr(modarg_device, ','))) {
+        pa_log_error("Failed to parse device argument : no comma");
+        return -1;
+    }
+
+    comma_p++;
+    device_p = device;
+    while (*comma_p != '\0')
+        *(device_p++) = *(comma_p++);
+    *device_p = '\0';
+
+    return 0;
+}
+
+int pa__init(pa_module*m) {
+    struct userdata *u = NULL;
+    pa_sample_spec ss;
+    pa_channel_map map;
+    pa_modargs *ma = NULL;
+    pa_source_new_data data;
+    uint32_t alternate_sample_rate;
+    const char *modarg_device;
+    char card[DEVICE_NAME_MAX];
+    char device[DEVICE_NAME_MAX];
+    size_t frame_size, buffer_size, period_frames, buffer_frames;
+
+    pa_assert(m);
+
+    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+        pa_log_error("Failed to parse module arguments.");
+        goto fail;
+    }
+
+    ss = m->core->default_sample_spec;
+    map = m->core->default_channel_map;
+    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
+        pa_log_error("Invalid sample format specification or channel map");
+        goto fail;
+    }
+
+    alternate_sample_rate = m->core->alternate_sample_rate;
+    if (pa_modargs_get_alternate_sample_rate(ma, &alternate_sample_rate) < 0) {
+        pa_log_error("Failed to parse alternate sample rate");
+        goto fail;
+    }
+
+    m->userdata = u = pa_xnew0(struct userdata, 1);
+    u->core = m->core;
+    u->module = m;
+    u->first = true;
+    u->hal_interface = pa_hal_interface_get(u->core);
+    u->rtpoll = pa_rtpoll_new();
+    pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
+
+    if (!(modarg_device = pa_modargs_get_value(ma, "device", NULL))) {
+        pa_log_error("device is invalid");
+        goto fail;
+    }
+
+    if (parse_to_get_card(modarg_device, card) || parse_to_get_device(modarg_device, device)) {
+        pa_log_error("failed to parse device module argument, %s", modarg_device);
+        goto fail;
+    }
+
+    u->card = pa_xstrdup(card);
+    u->device = pa_xstrdup(device);
+
+    u->frag_size = (uint32_t) pa_usec_to_bytes(m->core->default_fragment_size_msec*PA_USEC_PER_MSEC, &ss);
+    u->nfrags = m->core->default_n_fragments;
+    if (pa_modargs_get_value_u32(ma, "fragment_size", &u->frag_size) < 0 ||
+        pa_modargs_get_value_u32(ma, "fragments", &u->nfrags) < 0) {
+        pa_log_error("fragment_size or fragments are invalid.");
+        goto fail;
+    }
+    pa_log_info("card(%s) device(%s) fragment_size(%u), fragments(%u)", u->card, u->device, u->frag_size, u->nfrags);
+
+    pa_source_new_data_init(&data);
+    data.driver = __FILE__;
+    data.module = m;
+    pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
+    pa_source_new_data_set_sample_spec(&data, &ss);
+    pa_source_new_data_set_channel_map(&data, &map);
+    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Tizen audio source"));
+    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
+    pa_proplist_sets(data.proplist, "tizen.card", u->card);
+    pa_proplist_sets(data.proplist, "tizen.device", u->device);
+    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "tizen");
+
+    frame_size = pa_frame_size(&ss);
+    buffer_size = u->frag_size * u->nfrags;
+    buffer_frames = buffer_size / frame_size;
+    period_frames = u->frag_size / frame_size;
+    pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%zu", buffer_frames * frame_size);
+    pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%zu", period_frames * frame_size);
+
+    if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+        pa_log_error("Invalid properties.");
+        pa_source_new_data_done(&data);
+        goto fail;
+    }
+
+    u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
+    pa_source_new_data_done(&data);
+
+    if (!u->source) {
+        pa_log_error("Failed to create source object.");
+        goto fail;
+    }
+
+    u->source->parent.process_msg = source_process_msg;
+    u->source->set_state_in_io_thread = source_set_state_in_io_thread_cb;
+    u->source->update_requested_latency = source_update_requested_latency_cb;
+    u->source->userdata = u;
+
+    if (pa_hal_interface_pcm_open(u->hal_interface,
+              u->card,
+              u->device,
+              DIRECTION_IN,
+              &u->source->sample_spec,
+              u->frag_size / pa_frame_size(&u->source->sample_spec),
+              u->nfrags,
+              (void **)&u->pcm_handle)) {
+        pa_log_error("Error opening PCM device");
+        goto fail;
+    }
+
+    pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+    pa_source_set_rtpoll(u->source, u->rtpoll);
+
+    u->block_usec = BLOCK_USEC;
+    u->latency_time = u->block_usec;
+    u->timestamp = 0ULL;
+
+    pa_source_set_max_rewind(u->source, 0);
+
+    if (!(u->thread = pa_thread_new("tizenaudio-source", thread_func, u))) {
+        pa_log_error("Failed to create thread.");
+        goto fail;
+    }
+    pa_source_set_fixed_latency(u->source, u->block_usec);
+    pa_source_put(u->source);
+    pa_modargs_free(ma);
+
+    return 0;
+
+fail:
+    if (ma)
+        pa_modargs_free(ma);
+
+    pa__done(m);
+    return -1;
+}
+
+int pa__get_n_used(pa_module *m) {
+    struct userdata *u;
+
+    pa_assert(m);
+    pa_assert_se((u = m->userdata));
+
+    return pa_source_linked_by(u->source);
+}
+
+void pa__done(pa_module*m) {
+    struct userdata *u;
+
+    pa_assert(m);
+
+    if (!(u = m->userdata))
+        return;
+
+    if (u->source)
+        pa_source_unlink(u->source);
+
+    if (u->thread) {
+        pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
+        pa_thread_free(u->thread);
+    }
+
+    pa_thread_mq_done(&u->thread_mq);
+
+    if (u->source)
+        pa_source_unref(u->source);
+
+    pa_xfree(u->card);
+    pa_xfree(u->device);
+
+    if (u->rtpoll)
+        pa_rtpoll_free(u->rtpoll);
+
+    if (u->pcm_handle) {
+        pa_hal_interface_pcm_stop(u->hal_interface, u->pcm_handle);
+        pa_hal_interface_pcm_close(u->hal_interface, u->pcm_handle);
+    }
+
+    pa_xfree(u);
+}