fix: resolve relative file paths to absolute paths in audio source configuration

- Added path resolution logic to handle relative file: paths robustly regardless of current working directory
- Searches for files relative to both server directory and package root
- Returns clear error message when demo audio files cannot be found
This commit is contained in:
2025-11-04 11:53:32 +01:00
parent c01bdb8b96
commit 6f1d9876c7

View File

@@ -205,6 +205,27 @@ class StreamerWorker: # TODO: is wraping in this Worker stricly nececcarry ?
for big in conf.bigs:
big.input_format = f"int16le,{capture_rate},{channels}"
# Resolve relative file: paths to absolute paths (robust against varying cwd)
try:
server_dir = os.path.dirname(__file__)
pkg_root = os.path.dirname(os.path.dirname(__file__)) # .../auracast
for big in conf.bigs:
if isinstance(big.audio_source, str) and big.audio_source.startswith('file:'):
rel = big.audio_source.split(':', 1)[1]
if not os.path.isabs(rel):
candidates = [
os.path.normpath(os.path.join(server_dir, rel)),
os.path.normpath(os.path.join(pkg_root, rel)),
]
resolved = next((c for c in candidates if os.path.exists(c)), None)
if resolved:
log.info("Resolved demo file path '%s' -> '%s'", rel, resolved)
big.audio_source = f"file:{resolved}"
else:
raise FileNotFoundError(rel)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Demo audio file not found or unreadable: {e}")
# Coerce QoS: compute max_transport_latency from RTN if qos_config present
if getattr(conf, 'qos_config', None) and getattr(conf.qos_config, 'number_of_retransmissions', None) is not None:
conf.qos_config.max_transport_latency_ms = int(conf.qos_config.number_of_retransmissions) * 10 + 3
@@ -279,6 +300,26 @@ class StreamerWorker: # TODO: is wraping in this Worker stricly nececcarry ?
if device_index is None:
raise HTTPException(status_code=400, detail=f"Audio device '{device_name}' not found.")
big.audio_source = f'device:{device_index}'
# Resolve relative file: paths to absolute paths (robust against varying cwd)
try:
server_dir = os.path.dirname(__file__)
pkg_root = os.path.dirname(os.path.dirname(__file__)) # .../auracast
for big in conf.bigs:
if isinstance(big.audio_source, str) and big.audio_source.startswith('file:'):
rel = big.audio_source.split(':', 1)[1]
if not os.path.isabs(rel):
candidates = [
os.path.normpath(os.path.join(server_dir, rel)),
os.path.normpath(os.path.join(pkg_root, rel)),
]
resolved = next((c for c in candidates if os.path.exists(c)), None)
if resolved:
log.info("Resolved demo file path '%s' -> '%s'", rel, resolved)
big.audio_source = f"file:{resolved}"
else:
raise FileNotFoundError(rel)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Demo audio file not found or unreadable: {e}")
# Coerce QoS: compute max_transport_latency from RTN if qos_config present
if getattr(conf, 'qos_config', None) and getattr(conf.qos_config, 'number_of_retransmissions', None) is not None:
conf.qos_config.max_transport_latency_ms = int(conf.qos_config.number_of_retransmissions) * 10 + 3