]> git.sesse.net Git - nageru/commitdiff
Don't reset an ALSA device when the only thing that changes is which channels to...
authorSteinar H. Gunderson <steinar+nageru@gunderson.no>
Sun, 11 Aug 2019 17:28:17 +0000 (19:28 +0200)
committerSteinar H. Gunderson <steinar+nageru@gunderson.no>
Sun, 25 Aug 2019 22:39:08 +0000 (00:39 +0200)
nageru/alsa_pool.cpp
nageru/alsa_pool.h
nageru/audio_mixer.cpp
nageru/audio_mixer.h

index c74ed26613a5a31449d7e5c9938a38dea45ec86d..4e12bb75349750d57d2e2ff1b72e43a1def841ce 100644 (file)
@@ -79,6 +79,16 @@ void ALSAPool::release_device(unsigned index)
        }
 }
 
+bool ALSAPool::device_is_held(unsigned index)
+{
+       lock_guard<mutex> lock(mu);
+       if (index < devices.size()) {
+               return devices[index].held;
+       } else {
+               return false;
+       }
+}
+
 void ALSAPool::enumerate_devices()
 {
        // Enumerate all cards.
index 9c1715c56bb0bd2eb049de94c92cad588e93c8f0..d42101971ccd41d6cd15bdf7e99689bea2283633 100644 (file)
@@ -80,6 +80,7 @@ public:
 
        void hold_device(unsigned index);
        void release_device(unsigned index);  // Note: index is allowed to go out of bounds.
+       bool device_is_held(unsigned index);  // Same here.
 
        // If device is held, start or restart capture. If device is not held,
        // stop capture if it isn't already.
index 07d10dad3650a9ca037263d661b4d5deca4adbf2..e441aac19ff9bd1bb73c3c2b5a7094270ed0a31f 100644 (file)
@@ -1256,21 +1256,16 @@ void AudioMixer::set_input_mapping_lock_held(const InputMapping &new_input_mappi
                const DeviceSpec device_spec{InputSourceType::ALSA_INPUT, card_index};
                AudioDevice *device = find_audio_device(device_spec);
                double extra_delay_ms = new_extra_delay_ms[device_spec];
-               if (interesting_channels[device_spec].empty()) {
-                       alsa_pool.release_device(card_index);
-               } else {
-                       alsa_pool.hold_device(card_index);
-               }
                if (device->interesting_channels != interesting_channels[device_spec]) {
                        device->interesting_channels = interesting_channels[device_spec];
                        device->extra_delay_ms = extra_delay_ms;
-                       alsa_pool.reset_device(device_spec.index);
                        reset_resampler_mutex_held(device_spec);
                } else if (device->extra_delay_ms != extra_delay_ms &&
                           device->resampling_queue != nullptr) {
                        device->extra_delay_ms = extra_delay_ms;
                        device->resampling_queue->change_expected_delay(get_delay_seconds(extra_delay_ms));
                }
+               start_or_stop_alsa_capture(device_spec);
        }
        for (unsigned card_index = 0; card_index < num_ffmpeg_inputs; ++card_index) {
                const DeviceSpec device_spec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index};
@@ -1329,4 +1324,20 @@ bool AudioMixer::is_mono(unsigned bus_index)
        }
 }
 
+void AudioMixer::start_or_stop_alsa_capture(DeviceSpec device_spec)
+{
+       assert(device_spec.type == InputSourceType::ALSA_INPUT);
+       AudioDevice *device = find_audio_device(device_spec);
+       bool previously_held = alsa_pool.device_is_held(device_spec.index);
+       bool should_be_held = !device->interesting_channels.empty();
+       if (should_be_held) {
+               alsa_pool.hold_device(device_spec.index);
+       } else {
+               alsa_pool.release_device(device_spec.index);
+       }
+       if (previously_held != should_be_held) {
+               alsa_pool.reset_device(device_spec.index);
+       }
+}
+
 AudioMixer *global_audio_mixer = nullptr;
index 0beaf103ab08dcfcd03ecef99eaed32ff83292df..2b9aa4b9f77d412dc9a6d93950f4596cfff7ad00 100644 (file)
@@ -365,6 +365,7 @@ private:
        void send_audio_level_callback();
        std::vector<DeviceSpec> get_active_devices() const;
        void set_input_mapping_lock_held(const InputMapping &input_mapping);
+       void start_or_stop_alsa_capture(DeviceSpec device_spec);
 
        unsigned num_capture_cards, num_ffmpeg_inputs;