2 #define _ALSA_INPUT_H 1
4 // ALSA sound input, running in a separate thread and sending audio back
7 // Note: “frame” here generally refers to the ALSA definition of frame,
8 // which is a set of samples, exactly one for each channel. The only exception
9 // is in frame_length, where it means the TIMEBASE length of the buffer
10 // as a whole, since that's what AudioMixer::add_audio() wants.
12 #include <alsa/asoundlib.h>
18 #include <unordered_map>
21 #include "bmusb/bmusb.h"
28 typedef std::function<bool(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length)> audio_callback_t;
30 ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback, ALSAPool *parent_pool, unsigned internal_dev_index);
33 // NOTE: Might very well be different from the sample rate given to the
34 // constructor, since the card might not support the one you wanted.
35 unsigned get_sample_rate() const { return sample_rate; }
37 void start_capture_thread();
38 void stop_capture_thread();
41 void capture_thread_func();
42 int64_t frames_to_pts(uint64_t n) const;
43 void die_on_error(const char *func_name, int err);
46 unsigned sample_rate, num_channels, num_periods;
47 snd_pcm_uframes_t period_size;
48 snd_pcm_uframes_t buffer_frames;
49 bmusb::AudioFormat audio_format;
50 audio_callback_t audio_callback;
52 snd_pcm_t *pcm_handle;
53 std::thread capture_thread;
54 std::atomic<bool> should_quit{false};
55 std::unique_ptr<uint8_t[]> buffer;
56 ALSAPool *parent_pool;
57 unsigned internal_dev_index;
60 // The class dealing with the collective of all ALSA cards in the system.
61 // In particular, it deals with enumeration of cards, and hotplug of new ones.
68 // There is no card here. (There probably used to be one,
69 // but it got removed.) We don't insert a card before
70 // we've actually probed it, ie., we know whether it
71 // can be captured from at all, and what its name is.
74 // This card is ready for capture, as far as we know.
75 // (It could still be used by someone else; we don't know
76 // until we try to open it.)
79 // We are trying to start capture from this card, but we are not
80 // streaming yet. Note that this could in theory go on forever,
81 // if the card is in use by some other process; in the UI,
82 // we will show this state as “(busy)”.
85 // The card is capturing and sending data. If there's a fatal error,
86 // it could go back to STARTING, or it could go to DEAD
87 // (depending on the error).
90 // The card is gone (e.g., unplugged). However, since there's
91 // still a bus using it, we can't just remove the entry.
92 // If the card comes back (ie., a new card is plugged in,
93 // and we believe it has the same configuration), it could be
94 // installed in place of this card, and then presumably be put
95 // back into STARTING or RUNNING.
97 } state = State::EMPTY;
99 std::string address; // E.g. “hw:0,0”.
100 std::string name, info;
101 unsigned num_channels;
102 ALSAInput *input = nullptr; // nullptr iff EMPTY or DEAD.
104 // Whether the AudioMixer is interested in this card or not.
105 // “Interested” could mean either of two things: Either it is part of
106 // a bus mapping, or it is in the process of enumerating devices
107 // (to show to the user). A card that is _not_ held can disappear
108 // at any given time as a result of an error or hotplug event;
109 // a card that is held will go to the DEAD state instead.
115 // Get the list of all current devices. Note that this will implicitly mark
116 // all of the returned devices as held, since the input mapping UI needs
117 // some kind of stability when the user is to choose. Thus, when you are done
118 // with the list and have set a new mapping, you must go through all the devices
119 // you don't want and release them using release_device().
120 std::vector<Device> get_devices();
122 void hold_device(unsigned index);
123 void release_device(unsigned index); // Note: index is allowed to go out of bounds.
125 // If device is held, start or restart capture. If device is not held,
126 // stop capture if it isn't already.
127 void reset_device(unsigned index);
129 // Note: The card must be held. Returns OUTPUT_FREQUENCY if the card is in EMPTY or DEAD.
130 unsigned get_capture_frequency(unsigned index);
132 // Note: The card must be held.
133 Device::State get_card_state(unsigned index);
135 // Only for ALSAInput.
136 void set_card_state(unsigned index, Device::State state);
138 // Just a short form for taking <mu> and then moving the card to
139 // EMPTY or DEAD state. Only for ALSAInput and for internal use.
140 void free_card(unsigned index);
142 // TODO: Add accessors and/or callbacks about changed state, so that
143 // the UI actually stands a chance in using that information.
146 mutable std::mutex mu;
147 std::vector<Device> devices; // Under mu.
148 std::vector<std::unique_ptr<ALSAInput>> inputs; // Under mu, corresponds 1:1 to devices.
150 // Keyed on device address (e.g. “hw:0,0”). If there's an entry here,
151 // it means we already have a thread doing retries, so we shouldn't
153 std::unordered_map<std::string, unsigned> add_device_tries_left; // Under add_device_mutex.
154 std::mutex add_device_mutex;
156 static constexpr int num_retries = 10;
158 void inotify_thread_func();
159 void enumerate_devices();
161 // Try to add an input at the given card/device. If it succeeds, return
162 // synchronously. If not, fire off a background thread to try up to
163 // <num_retries> times.
164 void probe_device_with_retry(unsigned card_index, unsigned dev_index);
165 void probe_device_retry_thread_func(unsigned card_index, unsigned dev_index);
167 enum class ProbeResult {
172 ProbeResult probe_device_once(unsigned card_index, unsigned dev_index);
174 // Must be called with <mu> held. Will allocate a new entry if needed.
175 // The returned entry will be set to READY state.
176 unsigned find_free_device_index();
178 friend class ALSAInput;
181 #endif // !defined(_ALSA_INPUT_H)