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