From: Steinar H. Gunderson Date: Sun, 11 Aug 2019 17:28:17 +0000 (+0200) Subject: Don't reset an ALSA device when the only thing that changes is which channels to... X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=9768e380472e6ad68c8fc06efde3fa20f18bfc00 Don't reset an ALSA device when the only thing that changes is which channels to pick out. --- diff --git a/nageru/alsa_pool.cpp b/nageru/alsa_pool.cpp index c74ed26..4e12bb7 100644 --- a/nageru/alsa_pool.cpp +++ b/nageru/alsa_pool.cpp @@ -79,6 +79,16 @@ void ALSAPool::release_device(unsigned index) } } +bool ALSAPool::device_is_held(unsigned index) +{ + lock_guard lock(mu); + if (index < devices.size()) { + return devices[index].held; + } else { + return false; + } +} + void ALSAPool::enumerate_devices() { // Enumerate all cards. diff --git a/nageru/alsa_pool.h b/nageru/alsa_pool.h index 9c1715c..d421019 100644 --- a/nageru/alsa_pool.h +++ b/nageru/alsa_pool.h @@ -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. diff --git a/nageru/audio_mixer.cpp b/nageru/audio_mixer.cpp index 07d10da..e441aac 100644 --- a/nageru/audio_mixer.cpp +++ b/nageru/audio_mixer.cpp @@ -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; diff --git a/nageru/audio_mixer.h b/nageru/audio_mixer.h index 0beaf10..2b9aa4b 100644 --- a/nageru/audio_mixer.h +++ b/nageru/audio_mixer.h @@ -365,6 +365,7 @@ private: void send_audio_level_callback(); std::vector 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;