+static void thread_func(void *userdata) {
+ enum {
+ POLLFD_ASYNCQ,
+ POLLFD_ALSA_BASE
+ };
+
+ struct userdata *u = userdata;
+ struct pollfd *pollfd = NULL;
+ int n_alsa_fds, err;
+ unsigned short revents = 0;
+ snd_pcm_status_t *status;
+
+ pa_assert(u);
+ snd_pcm_status_alloca(&status);
+
+ pa_log_debug("Thread starting up");
+
+ if ((n_alsa_fds = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n_alsa_fds));
+ goto fail;
+ }
+
+ pollfd = pa_xnew0(struct pollfd, POLLFD_ALSA_BASE + n_alsa_fds);
+
+ pollfd[POLLFD_ASYNCQ].fd = pa_asyncmsgq_get_fd(u->asyncmsgq);
+ pollfd[POLLFD_ASYNCQ].events = POLLIN;
+
+ if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd+POLLFD_ALSA_BASE, n_alsa_fds)) < 0) {
+ pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
+ goto fail;
+ }
+
+ for (;;) {
+ pa_msgobject *object;
+ int code;
+ void *data;
+ int r;
+ pa_memchunk chunk;
+
+/* pa_log("loop"); */
+
+ /* Check whether there is a message for us to process */
+ if (pa_asyncmsgq_get(u->asyncmsgq, &object, &code, &data, &chunk, 0) == 0) {
+ int ret;
+
+/* pa_log("processing msg"); */
+
+ if (!object && code == PA_MESSAGE_SHUTDOWN) {
+ pa_asyncmsgq_done(u->asyncmsgq, 0);
+ goto finish;
+ }
+
+ ret = pa_asyncmsgq_dispatch(object, code, data, &chunk);
+ pa_asyncmsgq_done(u->asyncmsgq, ret);
+ continue;
+ }
+
+/* pa_log("loop2"); */
+
+ /* Render some data and write it to the dsp */
+
+ if (PA_SOURCE_OPENED(u->source->thread_info.state) && (revents & POLLIN)) {
+ int work_done = 0;
+ pa_assert(u->pcm_handle);
+
+ if (u->use_mmap) {
+
+ if ((work_done = mmap_read(u)) < 0)
+ goto fail;
+
+ } else {
+ ssize_t l;
+
+ snd_pcm_hwsync(u->pcm_handle);
+ if ((err = snd_pcm_status(u->pcm_handle, status)) >= 0)
+ l = snd_pcm_status_get_avail(status) * u->frame_size;
+ else
+ l = u->fragment_size;
+
+ while (l > 0) {
+ void *p;
+ snd_pcm_sframes_t t;
+
+ pa_assert(l > 0);
+
+ chunk.memblock = pa_memblock_new(u->core->mempool, l);
+
+ p = pa_memblock_acquire(chunk.memblock);
+ t = snd_pcm_readi(u->pcm_handle, (uint8_t*) p, l / u->frame_size);
+ pa_memblock_release(chunk.memblock);
+
+/* pa_log("wrote %i bytes of %u (%u)", t*u->frame_size, u->memchunk.length, l); */
+
+ pa_assert(t != 0);
+
+ if (t < 0) {
+ pa_memblock_unref(chunk.memblock);
+
+ if (t == -EPIPE)
+ pa_log_debug("Buffer underrun!");
+
+ if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
+ continue;
+
+ if (t == -EAGAIN) {
+ pa_log_debug("EAGAIN");
+ break;
+ } else {
+ pa_log("Failed to read data from DSP: %s", snd_strerror(t));
+ goto fail;
+ }
+
+ } else {
+
+ chunk.index = 0;
+ chunk.length = t * u->frame_size;
+
+ pa_source_post(u->source, &chunk);
+ pa_memblock_unref(chunk.memblock);
+
+ l -= t * u->frame_size;
+
+ work_done = 1;
+ }
+ }
+ }
+
+ revents &= ~POLLIN;
+
+ if (work_done)
+ continue;
+ }
+
+ /* Hmm, nothing to do. Let's sleep */
+ if (pa_asyncmsgq_before_poll(u->asyncmsgq) < 0)
+ continue;
+
+/* pa_log("polling for %i", POLLFD_ALSA_BASE + (PA_SOURCE_OPENED(u->source->thread_info.state) ? n_alsa_fds : 0)); */
+ r = poll(pollfd, POLLFD_ALSA_BASE + (PA_SOURCE_OPENED(u->source->thread_info.state) ? n_alsa_fds : 0), -1);
+ /*pa_log("polling got dsp=%i amq=%i (%i)", r > 0 ? pollfd[POLLFD_DSP].revents : 0, r > 0 ? pollfd[POLLFD_ASYNCQ].revents : 0, r); */
+/* pa_log("poll end"); */
+
+ pa_asyncmsgq_after_poll(u->asyncmsgq);
+
+ if (r < 0) {
+ if (errno == EINTR) {
+ pollfd[POLLFD_ASYNCQ].revents = 0;
+ revents = 0;
+ continue;
+ }
+
+ pa_log("poll() failed: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ pa_assert(r > 0);
+
+ if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
+ if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd + POLLFD_ALSA_BASE, n_alsa_fds, &revents)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
+ goto fail;
+ }
+
+/* pa_log("got alsa event"); */
+ } else
+ revents = 0;
+
+ pa_assert((pollfd[POLLFD_ASYNCQ].revents & ~POLLIN) == 0);
+ }
+
+fail:
+ /* We have to continue processing messages until we receive the
+ * SHUTDOWN message */
+ pa_asyncmsgq_post(u->core->asyncmsgq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->asyncmsgq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+
+ pa_xfree(pollfd);
+}
+
+