]> git.sesse.net Git - nageru/blob - alsa_pool.h
Shut down the ALSA inotify thread using an eventfd instead of busypolling every 100 ms.
[nageru] / alsa_pool.h
1 #ifndef _ALSA_POOL_H
2 #define _ALSA_POOL_H 1
3
4 #include <alsa/asoundlib.h>
5 #include <atomic>
6 #include <functional>
7 #include <mutex>
8 #include <string>
9 #include <thread>
10 #include <unordered_map>
11 #include <vector>
12
13 #include "alsa_input.h"
14 #include "bmusb/bmusb.h"
15 #include "timebase.h"
16
17 class ALSAInput;
18 class DeviceSpecProto;
19
20 // The class dealing with the collective of all ALSA cards in the system.
21 // In particular, it deals with enumeration of cards, and hotplug of new ones.
22 class ALSAPool {
23 public:
24         ALSAPool();
25         ~ALSAPool();
26
27         struct Device {
28                 enum class State {
29                         // There is no card here. (There probably used to be one,
30                         // but it got removed.) We don't insert a card before
31                         // we've actually probed it, ie., we know whether it
32                         // can be captured from at all, and what its name is.
33                         EMPTY,
34
35                         // This card is ready for capture, as far as we know.
36                         // (It could still be used by someone else; we don't know
37                         // until we try to open it.)
38                         READY,
39
40                         // We are trying to start capture from this card, but we are not
41                         // streaming yet. Note that this could in theory go on forever,
42                         // if the card is in use by some other process; in the UI,
43                         // we will show this state as “(busy)”.
44                         STARTING,
45
46                         // The card is capturing and sending data. If there's a fatal error,
47                         // it could go back to STARTING, or it could go to DEAD
48                         // (depending on the error).
49                         RUNNING,
50
51                         // The card is gone (e.g., unplugged). However, since there's
52                         // still a bus using it, we can't just remove the entry.
53                         // If the card comes back (ie., a new card is plugged in,
54                         // and we believe it has the same configuration), it could be
55                         // installed in place of this card, and then presumably be put
56                         // back into STARTING or RUNNING.
57                         DEAD
58                 } state = State::EMPTY;
59
60                 std::string address;  // E.g. “hw:0,0”.
61                 std::string name, info;
62                 unsigned num_channels;
63                 ALSAInput *input = nullptr;  // nullptr iff EMPTY or DEAD.
64
65                 // Whether the AudioMixer is interested in this card or not.
66                 // “Interested” could mean either of two things: Either it is part of
67                 // a bus mapping, or it is in the process of enumerating devices
68                 // (to show to the user). A card that is _not_ held can disappear
69                 // at any given time as a result of an error or hotplug event;
70                 // a card that is held will go to the DEAD state instead.
71                 bool held = false;
72
73                 std::string display_name() const { return name + " (" + info + ")"; }
74         };
75
76         void init();
77
78         // Get the list of all current devices. Note that this will implicitly mark
79         // all of the returned devices as held, since the input mapping UI needs
80         // some kind of stability when the user is to choose. Thus, when you are done
81         // with the list and have set a new mapping, you must go through all the devices
82         // you don't want and release them using release_device().
83         std::vector<Device> get_devices();
84
85         void hold_device(unsigned index);
86         void release_device(unsigned index);  // Note: index is allowed to go out of bounds.
87
88         // If device is held, start or restart capture. If device is not held,
89         // stop capture if it isn't already.
90         void reset_device(unsigned index);
91
92         // Note: The card must be held. Returns OUTPUT_FREQUENCY if the card is in EMPTY or DEAD.
93         unsigned get_capture_frequency(unsigned index);
94
95         // Note: The card must be held.
96         Device::State get_card_state(unsigned index);
97
98         // Only for ALSAInput.
99         void set_card_state(unsigned index, Device::State state);
100
101         // Just a short form for taking <mu> and then moving the card to
102         // EMPTY or DEAD state. Only for ALSAInput and for internal use.
103         void free_card(unsigned index);
104
105         // Create a new card, mark it immediately as DEAD and hold it.
106         // Returns the new index.
107         unsigned create_dead_card(const std::string &name, const std::string &info, unsigned num_channels);
108
109         // Make a protobuf representation of the given card, so that it can be
110         // matched against at a later stage. For AudioMixer only.
111         // The given card must be held.
112         void serialize_device(unsigned index, DeviceSpecProto *serialized);
113
114 private:
115         mutable std::mutex mu;
116         std::vector<Device> devices;  // Under mu.
117         std::vector<std::unique_ptr<ALSAInput>> inputs;  // Under mu, corresponds 1:1 to devices.
118
119         // Keyed on device address (e.g. “hw:0,0”). If there's an entry here,
120         // it means we already have a thread doing retries, so we shouldn't
121         // start a new one.
122         std::unordered_map<std::string, unsigned> add_device_tries_left;  // Under add_device_mutex.
123         std::mutex add_device_mutex;
124
125         static constexpr int num_retries = 10;
126
127         void inotify_thread_func();
128         void enumerate_devices();
129
130         // Try to add an input at the given card/device. If it succeeds, return
131         // synchronously. If not, fire off a background thread to try up to
132         // <num_retries> times.
133         void probe_device_with_retry(unsigned card_index, unsigned dev_index);
134         void probe_device_retry_thread_func(unsigned card_index, unsigned dev_index);
135
136         enum class ProbeResult {
137                 SUCCESS,
138                 DEFER,
139                 FAILURE
140         };
141         ProbeResult probe_device_once(unsigned card_index, unsigned dev_index);
142
143         void unplug_device(unsigned card_index, unsigned dev_index);
144
145         // Must be called with <mu> held. Will allocate a new entry if needed.
146         // The returned entry will be set to READY state.
147         unsigned find_free_device_index(const std::string &name,
148                                         const std::string &info,
149                                         unsigned num_channels,
150                                         const std::string &address);
151
152         std::atomic<bool> should_quit{false};
153         int should_quit_fd;
154         std::thread inotify_thread;
155         std::atomic<int> retry_threads_running{0};
156
157         friend class ALSAInput;
158 };
159
160 #endif  // !defined(_ALSA_POOL_H)