]> git.sesse.net Git - nageru/blobdiff - audio_mixer.h
Fix a deadlock issue when shutting down ALSA cards.
[nageru] / audio_mixer.h
index f2c77b91c8130cfba4a4edd3aa5200df87178b94..6e1d5a9458fbb1ef3d808d7cae162591a2ae878d 100644 (file)
@@ -20,6 +20,7 @@
 #include <set>
 #include <vector>
 
+#include "alsa_input.h"
 #include "bmusb/bmusb.h"
 #include "db.h"
 #include "defs.h"
@@ -31,7 +32,7 @@ namespace bmusb {
 struct AudioFormat;
 }  // namespace bmusb
 
-enum class InputSourceType { SILENCE, CAPTURE_CARD };
+enum class InputSourceType { SILENCE, CAPTURE_CARD, ALSA_INPUT };
 struct DeviceSpec {
        InputSourceType type;
        unsigned index;
@@ -74,11 +75,17 @@ struct InputMapping {
 class AudioMixer {
 public:
        AudioMixer(unsigned num_cards);
-       void reset_device(DeviceSpec device_spec);
+       ~AudioMixer();
+       void reset_resampler(DeviceSpec device_spec);
+
+       // Add audio (or silence) to the given device's queue. Can return false if
+       // the lock wasn't successfully taken; if so, you should simply try again.
+       // (This is to avoid a deadlock where a card hangs on the mutex in add_audio()
+       // while we are trying to shut it down from another thread that also holds
+       // the mutex.) frame_length is in TIMEBASE units.
+       bool add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length);
+       bool add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length);
 
-       // frame_length is in TIMEBASE units.
-       void add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length);
-       void add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length);
        std::vector<float> get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy);
 
        // See comments inside get_output().
@@ -204,18 +211,26 @@ private:
                unsigned capture_frequency = OUTPUT_FREQUENCY;
                // Which channels we consider interesting (ie., are part of some input_mapping).
                std::set<unsigned> interesting_channels;
+               // Only used for ALSA cards, obviously.
+               std::unique_ptr<ALSAInput> alsa_device;
        };
        AudioDevice *find_audio_device(DeviceSpec device_spec);
 
-       void find_sample_src_from_device(const std::vector<float> *samples_card, DeviceSpec device_spec, int source_channel, const float **srcptr, unsigned *stride);
-       void fill_audio_bus(const std::vector<float> *samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output);
-       void reset_device_mutex_held(DeviceSpec device_spec);
+       void find_sample_src_from_device(const std::map<DeviceSpec, std::vector<float>> &samples_card, DeviceSpec device_spec, int source_channel, const float **srcptr, unsigned *stride);
+       void fill_audio_bus(const std::map<DeviceSpec, std::vector<float>> &samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output);
+       void reset_resampler_mutex_held(DeviceSpec device_spec);
+       void reset_alsa_mutex_held(DeviceSpec device_spec);
+       std::map<DeviceSpec, DeviceInfo> get_devices_mutex_held() const;
 
        unsigned num_cards;
 
-       mutable std::mutex audio_mutex;
+       mutable std::timed_mutex audio_mutex;
+
+       AudioDevice video_cards[MAX_VIDEO_CARDS];  // Under audio_mutex.
 
-       AudioDevice cards[MAX_CARDS];  // Under audio_mutex.
+       // TODO: Figure out a better way to unify these two, as they are sharing indexing.
+       AudioDevice alsa_inputs[MAX_ALSA_CARDS];  // Under audio_mutex.
+       std::vector<ALSAInput::Device> available_alsa_cards;
 
        StereoFilter locut;  // Default cutoff 120 Hz, 24 dB/oct.
        std::atomic<float> locut_cutoff_hz;