Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

Distinguish decoder startup failures from invalid media (#4)

Cinna Davis4913fc446683
diff --git a/docs/AUDIO_INTAKE.md b/docs/AUDIO_INTAKE.md
index 32c6dcc..d17b1d2 100644
--- a/docs/AUDIO_INTAKE.md
+++ b/docs/AUDIO_INTAKE.md
@@ -37,6 +37,14 @@ no_audio_stream, decoder_unavailable, decode_failed, invalid_samples, empty_audi
and source_changed. Existing output raises FileExistsError. Input parameter and
shared-contract validation raise ValueError. ffmpeg and ffprobe must be on PATH.
+decoder_unavailable also covers executable launch errors and POSIX startup
+statuses126/127, including unavailable runtime dependencies. A valid recording
+must not be labelled corrupt merely because the decoder cannot start. Ordinary
+probe rejection remains invalid_media; a decoder that starts but rejects the
+audio remains decode_failed. Private executable-stub tests cover these paths
+without changing installed binaries, preserve input bytes and leave no finalized
+cache on failure. Historical namespace failures retain their original outcomes.
+
Verification: `PYTHONPATH=src .venv/bin/python -m pytest -q tests/test_intake.py`.
The tests encode a real MP3 with libmp3lame, compare WAV/MP3 burst timings at three
positions to <=1 ms and duration to one 4000 Hz sample, check 48 kHz stereo channel
diff --git a/src/aircraft_geoloc/intake.py b/src/aircraft_geoloc/intake.py
index bdcf7f3..83937f6 100644
--- a/src/aircraft_geoloc/intake.py
+++ b/src/aircraft_geoloc/intake.py
@@ -40,9 +40,11 @@ def probe_media(source):
["ffprobe", "-v", "error", "-show_streams", "-show_format", "-of", "json", str(path)],
capture_output=True, check=True, text=True)
probe = json.loads(result.stdout)
- except FileNotFoundError as exc:
- raise AudioIntakeError("decoder_unavailable", "ffprobe is required for audio intake") from exc
+ except OSError as exc:
+ raise AudioIntakeError("decoder_unavailable", "ffprobe could not start; check executable and runtime dependencies") from exc
except (subprocess.CalledProcessError, json.JSONDecodeError) as exc:
+ if isinstance(exc, subprocess.CalledProcessError) and exc.returncode in (126, 127):
+ raise AudioIntakeError("decoder_unavailable", "ffprobe could not start; check runtime dependencies") from exc
raise AudioIntakeError("invalid_media", "Media probe failed; file is unsupported or corrupt") from exc
audio = [s for s in probe.get("streams", []) if s.get("codec_type") == "audio"]
if not audio:
@@ -128,8 +130,8 @@ def intake_recording(source, output_dir, *, time_window=None, sample_rate_hz=400
temporary = Path(cache.name)
try:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=errors)
- except FileNotFoundError as exc:
- raise AudioIntakeError("decoder_unavailable", "ffmpeg is required for audio intake") from exc
+ except OSError as exc:
+ raise AudioIntakeError("decoder_unavailable", "ffmpeg could not start; check executable and runtime dependencies") from exc
count, peak, remainder = 0, 0.0, b""
try:
while block := process.stdout.read(256 * 1024):
@@ -144,6 +146,8 @@ def intake_recording(source, output_dir, *, time_window=None, sample_rate_hz=400
cache.write(block[:usable])
remainder = block[usable:]
code = process.wait()
+ if code in (126, 127):
+ raise AudioIntakeError("decoder_unavailable", "ffmpeg could not start; check runtime dependencies")
if code or remainder:
raise AudioIntakeError("decode_failed", "Audio decode failed; media may be truncated or corrupt")
finally:
diff --git a/tests/test_intake.py b/tests/test_intake.py
index 5bfc985..56c02fa 100644
--- a/tests/test_intake.py
+++ b/tests/test_intake.py
@@ -1,4 +1,5 @@
import json
+import shutil
import subprocess
import sys
@@ -10,6 +11,34 @@ from aircraft_geoloc.contracts import validate_recording
from aircraft_geoloc.intake import AudioIntakeError, intake_recording, source_hash
[email protected](sys.platform == "win32", reason="POSIX executable startup failures")
[email protected]("decoder", ["ffprobe", "ffmpeg"])
[email protected]("failure", ["loader_exit", "not_executable", "media_rejection"])
+def test_decoder_startup_and_media_failures_are_distinct(tmp_path, monkeypatch, decoder, failure):
+ source = tmp_path / "valid.wav"
+ sf.write(source, np.zeros(8000), 8000)
+ digest = source_hash(source)
+ real_probe = shutil.which("ffprobe")
+ assert real_probe
+ binaries = tmp_path / "bin"
+ binaries.mkdir()
+ if decoder == "ffmpeg":
+ (binaries / "ffprobe").symlink_to(real_probe)
+ executable = binaries / decoder
+ code = 1 if failure == "media_rejection" else 127
+ executable.write_text(f"#!/bin/sh\nprintf 'controlled decoder failure\\n' >&2\nexit {code}\n")
+ executable.chmod(0o644 if failure == "not_executable" else 0o755)
+ monkeypatch.setenv("PATH", str(binaries))
+ with pytest.raises(AudioIntakeError) as error:
+ intake_recording(source, tmp_path / "cache")
+ expected = "decoder_unavailable"
+ if failure == "media_rejection":
+ expected = "invalid_media" if decoder == "ffprobe" else "decode_failed"
+ assert error.value.code == expected
+ assert source_hash(source) == digest
+ assert not (tmp_path / "cache" / "recording.f32").exists()
+
+
@pytest.mark.skipif(sys.platform != "linux", reason="Linux /proc resident-memory accounting")
def test_chunk_iterator_does_not_retain_recording_sized_rss(tmp_path):
# A synthetic raw-cache stress fixture, not a captured audio recording.