Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions modules/audio_device/audio_engine_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef SDK_OBJC_NATIVE_SRC_AUDIO_AUDIO_DEVICE_AUDIOENGINE_H_
#define SDK_OBJC_NATIVE_SRC_AUDIO_AUDIO_DEVICE_AUDIOENGINE_H_

#include <atomic>
#include <memory>

#include "api/scoped_refptr.h"
Expand Down Expand Up @@ -137,6 +136,7 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
bool output_available = true;
bool input_available = true;

bool output_running_persistent_mode = false;
bool input_enabled_persistent_mode = false;

bool input_muted = true;
Expand All @@ -161,6 +161,7 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
return input_enabled == rhs.input_enabled && input_running == rhs.input_running &&
output_enabled == rhs.output_enabled && output_running == rhs.output_running &&
input_available == rhs.input_available && output_available == rhs.output_available &&
output_running_persistent_mode == rhs.output_running_persistent_mode &&
input_enabled_persistent_mode == rhs.input_enabled_persistent_mode &&
input_muted == rhs.input_muted && is_interrupted == rhs.is_interrupted &&
render_mode == rhs.render_mode && mute_mode == rhs.mute_mode &&
Expand All @@ -178,9 +179,8 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
// AUDIO STATE LOGIC
//
// Device Mode:
// - Output follows input only when voice_processing_enabled=true (for AEC)
// - Output follows input to keep AVAudioEngine IO active for capture.
// - Input respects mute mode restrictions (RestartEngine + input_muted)
// - Independent operation when voice processing is disabled
//
// Manual Mode:
// - Bidirectional coupling: if ANY component is enabled/running, BOTH are considered
Expand All @@ -192,21 +192,23 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver

bool IsOutputEnabled() const {
if (!output_available) return false;
if (output_running_persistent_mode) return true;

switch (render_mode) {
case RenderMode::Device:
return voice_processing_enabled ? (IsInputEnabled() || output_enabled) : output_enabled;
return IsInputEnabled() || output_enabled;
case RenderMode::Manual:
return output_enabled || input_enabled || input_enabled_persistent_mode;
}
}

bool IsOutputRunning() const {
if (!output_available) return false;
if (output_running_persistent_mode) return true;

switch (render_mode) {
case RenderMode::Device:
return voice_processing_enabled ? (IsInputRunning() || output_running) : output_running;
return IsInputRunning() || output_running;
case RenderMode::Manual:
return output_running || input_running;
}
Expand All @@ -220,7 +222,8 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
return !(mute_mode == MuteMode::RestartEngine && input_muted) &&
(input_enabled || input_enabled_persistent_mode);
case RenderMode::Manual:
return input_enabled || input_enabled_persistent_mode || output_enabled;
return (input_enabled || input_enabled_persistent_mode) ||
(output_enabled || output_running_persistent_mode);
}
}

Expand All @@ -231,7 +234,7 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
case RenderMode::Device:
return !(mute_mode == MuteMode::RestartEngine && input_muted) && input_running;
case RenderMode::Manual:
return input_running || output_running;
return input_running || (output_running || output_running_persistent_mode);
}
}

Expand Down Expand Up @@ -367,6 +370,9 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
int32_t SetDuckingLevel(AudioDuckingLevel level);
int32_t DuckingLevel(AudioDuckingLevel* level);

int32_t SetOutputRunningPersistentMode(bool enable);
int32_t OutputRunningPersistentMode(bool* enabled);

int32_t SetInitRecordingPersistentMode(bool enable);
int32_t InitRecordingPersistentMode(bool* enabled);

Expand Down Expand Up @@ -429,11 +435,7 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver

bool DidUpdateMuteMode() const { return prev.mute_mode != next.mute_mode; }

bool IsEngineRestartRequired() const {
return DidUpdateAudioGraph() ||
// Voice processing enable state updates
DidUpdateVoiceProcessingEnabled();
}
bool IsEngineRestartRequired() const { return DidUpdateAudioGraph(); }

bool IsEngineRecreateRequired() const {
// Device id specified
Expand All @@ -448,7 +450,11 @@ class AudioEngineDevice : public AudioDeviceModule, public AudioSessionObserver
bool special_case = (prev.IsOutputEnabled() && next.IsOutputEnabled()) &&
(prev.IsInputEnabled() && !next.IsInputEnabled());

return device || default_device || special_case;
// Toggling voice processing requires a full engine recreate to ensure
// a clean audio hardware state.
bool voice_processing = DidUpdateVoiceProcessingEnabled();

return device || default_device || special_case || voice_processing;
}

bool DidEnableManualRenderingMode() const {
Expand Down
Loading