X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=alsa_input.h;h=3b98885677832ec6f1763555fbb4f36ada45e429;hb=26e1ec466d4730b6abc0e20201d704cfdf41a6eb;hp=07e8fe52e18dac164cdc6d967e92e3cf48fe1042;hpb=a0e27555696d0c63e3d89e31ca8f4292dcf40179;p=nageru diff --git a/alsa_input.h b/alsa_input.h index 07e8fe5..3b98885 100644 --- a/alsa_input.h +++ b/alsa_input.h @@ -22,6 +22,7 @@ #include "timebase.h" class ALSAPool; +class DeviceSpecProto; class ALSAInput { public: @@ -30,6 +31,11 @@ public: ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback, ALSAPool *parent_pool, unsigned internal_dev_index); ~ALSAInput(); + // If not called before start_capture_thread(), the capture thread + // will call it until it succeeds. + bool open_device(); + + // Not valid before the device has been successfully opened. // NOTE: Might very well be different from the sample rate given to the // constructor, since the card might not support the one you wanted. unsigned get_sample_rate() const { return sample_rate; } @@ -40,7 +46,13 @@ public: private: void capture_thread_func(); int64_t frames_to_pts(uint64_t n) const; - void die_on_error(const char *func_name, int err); + + enum class CaptureEndReason { + REQUESTED_QUIT, + DEVICE_GONE, + OTHER_ERROR + }; + CaptureEndReason do_capture(); std::string device; unsigned sample_rate, num_channels, num_periods; @@ -49,7 +61,7 @@ private: bmusb::AudioFormat audio_format; audio_callback_t audio_callback; - snd_pcm_t *pcm_handle; + snd_pcm_t *pcm_handle = nullptr; std::thread capture_thread; std::atomic should_quit{false}; std::unique_ptr buffer; @@ -108,6 +120,8 @@ public: // 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(); @@ -139,8 +153,14 @@ public: // EMPTY or DEAD state. Only for ALSAInput and for internal use. void free_card(unsigned index); - // TODO: Add accessors and/or callbacks about changed state, so that - // the UI actually stands a chance in using that information. + // 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; @@ -171,9 +191,14 @@ private: }; 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(); + unsigned find_free_device_index(const std::string &name, + const std::string &info, + unsigned num_channels, + const std::string &address); friend class ALSAInput; };