X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=alsa_input.h;h=060b9212690fce317f9dfac701af37d597af908a;hb=d3e48df512d9476d3849227067792a3537bb094e;hp=895760fed8fef76737e341e44e188267db7c9692;hpb=01181e6e22e5cfc9d0cb17231f2c1866cc53b04a;p=nageru diff --git a/alsa_input.h b/alsa_input.h index 895760f..060b921 100644 --- a/alsa_input.h +++ b/alsa_input.h @@ -10,23 +10,32 @@ // as a whole, since that's what AudioMixer::add_audio() wants. #include - +#include +#include #include +#include #include +#include #include #include -#include #include "bmusb/bmusb.h" -#include "timebase.h" +#include "quittable_sleeper.h" + +class ALSAPool; 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); + 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; } @@ -34,18 +43,22 @@ public: void start_capture_thread(); void stop_capture_thread(); - // TODO: Worry about hotplug. - struct Device { - std::string address; // E.g. “hw:0,0”. - std::string name, info; - unsigned num_channels; - }; - static std::vector enumerate_devices(); + // 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; - 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; @@ -54,10 +67,12 @@ 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}; + QuittableSleeper should_quit; std::unique_ptr buffer; + ALSAPool *parent_pool; + unsigned internal_dev_index; }; #endif // !defined(_ALSA_INPUT_H)