]> git.sesse.net Git - nageru/blobdiff - alsa_pool.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / alsa_pool.cpp
index 5983446e37ab4f687cfaa7a9e4db11d497eff711..59ae595f029f8ba6cfef0d7aff12f88a97f61799 100644 (file)
@@ -1,15 +1,22 @@
 #include "alsa_pool.h"
 
-#include <alsa/control.h>
-#include <alsa/error.h>
-#include <alsa/pcm.h>
+#include <alsa/asoundlib.h>
 #include <assert.h>
+#include <errno.h>
 #include <limits.h>
+#include <pthread.h>
+#include <poll.h>
+#include <stdint.h>
 #include <stdio.h>
+#include <sys/eventfd.h>
 #include <sys/inotify.h>
 #include <unistd.h>
 #include <algorithm>
+#include <chrono>
+#include <functional>
+#include <iterator>
 #include <memory>
+#include <ratio>
 
 #include "alsa_input.h"
 #include "audio_mixer.h"
 using namespace std;
 using namespace std::placeholders;
 
+ALSAPool::ALSAPool()
+{
+       should_quit_fd = eventfd(/*initval=*/0, /*flags=*/0);
+       assert(should_quit_fd != -1);
+}
+
 ALSAPool::~ALSAPool()
 {
        for (Device &device : devices) {
@@ -28,6 +41,11 @@ ALSAPool::~ALSAPool()
                }
        }
        should_quit = true;
+       const uint64_t one = 1;
+       if (write(should_quit_fd, &one, sizeof(one)) != sizeof(one)) {
+               perror("write(should_quit_fd)");
+               exit(1);
+       }
        inotify_thread.join();
 
        while (retry_threads_running > 0) {
@@ -117,6 +135,10 @@ void ALSAPool::probe_device_retry_thread_func(unsigned card_index, unsigned dev_
        char address[256];
        snprintf(address, sizeof(address), "hw:%d,%d", card_index, dev_index);
 
+       char thread_name[16];
+       snprintf(thread_name, sizeof(thread_name), "Reprobe_hw:%d,%d", card_index, dev_index);
+       pthread_setname_np(pthread_self(), thread_name);
+
        for ( ;; ) {  // Termination condition within the loop.
                sleep(1);
 
@@ -163,19 +185,24 @@ ALSAPool::ProbeResult ALSAPool::probe_device_once(unsigned card_index, unsigned
        }
        unique_ptr<snd_ctl_t, decltype(snd_ctl_close)*> ctl_closer(ctl, snd_ctl_close);
 
+       snprintf(address, sizeof(address), "hw:%d,%d", card_index, dev_index);
+
        snd_pcm_info_t *pcm_info;
        snd_pcm_info_alloca(&pcm_info);
        snd_pcm_info_set_device(pcm_info, dev_index);
        snd_pcm_info_set_subdevice(pcm_info, 0);
        snd_pcm_info_set_stream(pcm_info, SND_PCM_STREAM_CAPTURE);
-       if (snd_ctl_pcm_info(ctl, pcm_info) < 0) {
+       err = snd_ctl_pcm_info(ctl, pcm_info);
+       if (err == -ENOENT) {
+               // Not a capture card.
+               return ALSAPool::ProbeResult::FAILURE;
+       }
+       if (err < 0) {
                // Not available for capture.
                printf("%s: Not available for capture.\n", address);
                return ALSAPool::ProbeResult::DEFER;
        }
 
-       snprintf(address, sizeof(address), "hw:%d,%d", card_index, dev_index);
-
        unsigned num_channels = 0;
 
        // Find all channel maps for this device, and pick out the one
@@ -271,6 +298,8 @@ void ALSAPool::init()
 
 void ALSAPool::inotify_thread_func()
 {
+       pthread_setname_np(pthread_self(), "ALSA_Hotplug");
+
        int inotify_fd = inotify_init();
        if (inotify_fd == -1) {
                perror("inotify_init()");
@@ -289,12 +318,15 @@ void ALSAPool::inotify_thread_func()
        int size = sizeof(inotify_event) + NAME_MAX + 1;
        unique_ptr<char[]> buf(new char[size]);
        while (!should_quit) {
-               pollfd fds;
-               fds.fd = inotify_fd;
-               fds.events = POLLIN;
-               fds.revents = 0;
-
-               int ret = poll(&fds, 1, 100);
+               pollfd fds[2];
+               fds[0].fd = inotify_fd;
+               fds[0].events = POLLIN;
+               fds[0].revents = 0;
+               fds[1].fd = should_quit_fd;
+               fds[1].events = POLLIN;
+               fds[1].revents = 0;
+
+               int ret = poll(fds, 2, -1);
                if (ret == -1) {
                        if (errno == EINTR) {
                                continue;
@@ -307,6 +339,8 @@ void ALSAPool::inotify_thread_func()
                        continue;
                }
 
+               if (fds[1].revents) break;  // should_quit_fd asserted.
+
                ret = read(inotify_fd, buf.get(), size);
                if (ret == -1) {
                        if (errno == EINTR) {
@@ -350,6 +384,7 @@ void ALSAPool::inotify_thread_func()
        }
        close(watch_fd);
        close(inotify_fd);
+       close(should_quit_fd);
 }
 
 void ALSAPool::reset_device(unsigned index)
@@ -363,7 +398,7 @@ void ALSAPool::reset_device(unsigned index)
                inputs[index].reset();
        } else {
                // TODO: Put on a background thread instead of locking?
-               auto callback = bind(&AudioMixer::add_audio, global_audio_mixer, DeviceSpec{InputSourceType::ALSA_INPUT, index}, _1, _2, _3, _4);
+               auto callback = bind(&AudioMixer::add_audio, global_audio_mixer, DeviceSpec{InputSourceType::ALSA_INPUT, index}, _1, _2, _3, _4, _5);
                inputs[index].reset(new ALSAInput(device->address.c_str(), OUTPUT_FREQUENCY, device->num_channels, callback, this, index));
                inputs[index]->start_capture_thread();
        }