X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=alsa_input.h;h=060b9212690fce317f9dfac701af37d597af908a;hp=3b98885677832ec6f1763555fbb4f36ada45e429;hb=refs%2Fheads%2Fffmpeg-audio-only;hpb=5cb4274907d32fb8946558988461224196c2be59 diff --git a/alsa_input.h b/alsa_input.h index 3b98885..060b921 100644 --- a/alsa_input.h +++ b/alsa_input.h @@ -10,23 +10,23 @@ // as a whole, since that's what AudioMixer::add_audio() wants. #include - +#include +#include #include +#include #include +#include #include #include -#include -#include #include "bmusb/bmusb.h" -#include "timebase.h" +#include "quittable_sleeper.h" class ALSAPool; -class DeviceSpecProto; class ALSAInput { public: - typedef std::function audio_callback_t; + typedef std::function audio_callback_t; ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback, ALSAPool *parent_pool, unsigned internal_dev_index); ~ALSAInput(); @@ -43,6 +43,12 @@ public: void start_capture_thread(); void stop_capture_thread(); + // Set access, sample rate and format parameters on the given ALSA PCM handle. + // Returns the computed parameter set and the chosen sample rate. Note that + // sample_rate is an in/out parameter; you send in the desired rate, + // and ALSA picks one as close to that as possible. + static bool set_base_params(const char *device_name, snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hw_params, unsigned *sample_rate); + private: void capture_thread_func(); int64_t frames_to_pts(uint64_t n) const; @@ -63,144 +69,10 @@ private: snd_pcm_t *pcm_handle = nullptr; std::thread capture_thread; - std::atomic should_quit{false}; + QuittableSleeper should_quit; std::unique_ptr buffer; ALSAPool *parent_pool; unsigned internal_dev_index; }; -// The class dealing with the collective of all ALSA cards in the system. -// In particular, it deals with enumeration of cards, and hotplug of new ones. -class ALSAPool { -public: - ~ALSAPool(); - - struct Device { - enum class State { - // There is no card here. (There probably used to be one, - // but it got removed.) We don't insert a card before - // we've actually probed it, ie., we know whether it - // can be captured from at all, and what its name is. - EMPTY, - - // This card is ready for capture, as far as we know. - // (It could still be used by someone else; we don't know - // until we try to open it.) - READY, - - // We are trying to start capture from this card, but we are not - // streaming yet. Note that this could in theory go on forever, - // if the card is in use by some other process; in the UI, - // we will show this state as “(busy)”. - STARTING, - - // The card is capturing and sending data. If there's a fatal error, - // it could go back to STARTING, or it could go to DEAD - // (depending on the error). - RUNNING, - - // The card is gone (e.g., unplugged). However, since there's - // still a bus using it, we can't just remove the entry. - // If the card comes back (ie., a new card is plugged in, - // and we believe it has the same configuration), it could be - // installed in place of this card, and then presumably be put - // back into STARTING or RUNNING. - DEAD - } state = State::EMPTY; - - std::string address; // E.g. “hw:0,0”. - std::string name, info; - unsigned num_channels; - ALSAInput *input = nullptr; // nullptr iff EMPTY or DEAD. - - // Whether the AudioMixer is interested in this card or not. - // “Interested” could mean either of two things: Either it is part of - // a bus mapping, or it is in the process of enumerating devices - // (to show to the user). A card that is _not_ held can disappear - // at any given time as a result of an error or hotplug event; - // a card that is held will go to the DEAD state instead. - bool held = false; - - std::string display_name() const { return name + " (" + info + ")"; } - }; - - void init(); - - // Get the list of all current devices. Note that this will implicitly mark - // all of the returned devices as held, since the input mapping UI needs - // some kind of stability when the user is to choose. Thus, when you are done - // with the list and have set a new mapping, you must go through all the devices - // you don't want and release them using release_device(). - std::vector get_devices(); - - void hold_device(unsigned index); - void release_device(unsigned index); // Note: index is allowed to go out of bounds. - - // If device is held, start or restart capture. If device is not held, - // stop capture if it isn't already. - void reset_device(unsigned index); - - // Note: The card must be held. Returns OUTPUT_FREQUENCY if the card is in EMPTY or DEAD. - unsigned get_capture_frequency(unsigned index); - - // Note: The card must be held. - Device::State get_card_state(unsigned index); - - // Only for ALSAInput. - void set_card_state(unsigned index, Device::State state); - - // Just a short form for taking and then moving the card to - // EMPTY or DEAD state. Only for ALSAInput and for internal use. - void free_card(unsigned index); - - // Create a new card, mark it immediately as DEAD and hold it. - // Returns the new index. - unsigned create_dead_card(const std::string &name, const std::string &info, unsigned num_channels); - - // Make a protobuf representation of the given card, so that it can be - // matched against at a later stage. For AudioMixer only. - // The given card must be held. - void serialize_device(unsigned index, DeviceSpecProto *serialized); - -private: - mutable std::mutex mu; - std::vector devices; // Under mu. - std::vector> inputs; // Under mu, corresponds 1:1 to devices. - - // Keyed on device address (e.g. “hw:0,0”). If there's an entry here, - // it means we already have a thread doing retries, so we shouldn't - // start a new one. - std::unordered_map add_device_tries_left; // Under add_device_mutex. - std::mutex add_device_mutex; - - static constexpr int num_retries = 10; - - void inotify_thread_func(); - void enumerate_devices(); - - // Try to add an input at the given card/device. If it succeeds, return - // synchronously. If not, fire off a background thread to try up to - // times. - void probe_device_with_retry(unsigned card_index, unsigned dev_index); - void probe_device_retry_thread_func(unsigned card_index, unsigned dev_index); - - enum class ProbeResult { - SUCCESS, - DEFER, - FAILURE - }; - ProbeResult probe_device_once(unsigned card_index, unsigned dev_index); - - void unplug_device(unsigned card_index, unsigned dev_index); - - // Must be called with held. Will allocate a new entry if needed. - // The returned entry will be set to READY state. - unsigned find_free_device_index(const std::string &name, - const std::string &info, - unsigned num_channels, - const std::string &address); - - friend class ALSAInput; -}; - #endif // !defined(_ALSA_INPUT_H)