AMD Worker
The AMD Worker is a Java/Vert.x 5 microservice that performs real-time Answering Machine Detection (AMD). It receives live audio streams from Cloudonix via WebSocket, analyzes audio for voicemail beep tones, and posts detection results to the Laravel backend.
Purpose
The worker handles:
- Receiving WebSocket audio streams from Cloudonix
- Checking each stream's session with Laravel before acting on it (
/api/voice/amd-session) - Decoding mu-law audio to PCM and resampling to 16 kHz
- Detecting voicemail beeps with ML and energy-based detectors
- Posting results to Laravel's
/api/voice/amd-actionendpoint - Closing the WebSocket after a result or timeout
When It Is Triggered
The AMD worker is triggered when:
- An outbound auto-dialer campaign has AMD enabled
- A call is answered
- Laravel returns CXML with a
<Start><Stream url="wss://.../ws/amd/detect">verb. The URL is built from the organization's webhook base URL (orAPP_URL), withhttpsturned intowss - Cloudonix opens a WebSocket to Nginx at
/ws/amd/detect; Nginx proxies it to the worker's/ws/detecton port 8082
Streams whose session is not an active auto-dialer call are closed (see Session admission).
Architecture
Build and Run
Docker Compose (Recommended)
The worker starts automatically with the full OPBX stack:
docker compose up -d
Local Build
cd amd-worker
# Compile
mvn compile
# Run tests
mvn test
# Build shaded JAR
mvn package -DskipTests -B
# Run locally
java -jar target/amd-worker-1.0.0.jar
Docker Build
cd amd-worker
docker build -t amd-worker:latest .
docker run -p 8082:8082 -p 8083:8083 \
-e AMD_WORKER_API_TOKEN=your-token \
-e AMD_ACTION_CALLBACK_URL=http://host.docker.internal/api/voice/amd-action \
amd-worker:latest
The image (eclipse-temurin:21-jre) runs as a non-root user (uid 10001). In the compose stack it publishes no ports; Cloudonix reaches it through Nginx at /ws/amd/.
Environment Variables
The compose file passes only some of these to the container: AMD_WEBSOCKET_PORT, AMD_HTTP_PORT, AMD_WORKER_API_TOKEN, AMD_MODEL_PATH (/app/models/beep_detector.onnx), AMD_MAX_CONCURRENT_STREAMS, AMD_MIN_BEEP_START_MS, AMD_LOG_LEVEL, AMD_DUMP_AUDIO and AMD_DUMP_AUDIO_PATH. The others (AMD_DEFAULT_TIMEOUT_SECONDS, AMD_DETECTORS, AMD_ACTION_CALLBACK_URL, AMD_SESSION_CHECK_URL) keep their defaults unless you add them to the amd-worker environment in the compose file; setting them only in .env has no effect.
| Variable | Default | Required | Description |
|---|---|---|---|
AMD_WEBSOCKET_PORT | 8082 | No | WebSocket listener port |
AMD_HTTP_PORT | 8083 | No | Health/metrics HTTP port |
AMD_MODEL_PATH | ./models/beep_detector.onnx | No | ONNX model file path |
AMD_MAX_CONCURRENT_STREAMS | 100 | No | Max simultaneous streams |
AMD_DEFAULT_TIMEOUT_SECONDS | 30 | No | Detection timeout per stream |
AMD_DETECTORS | beep_ml,tone_energy | No | Enabled detectors |
AMD_MIN_BEEP_START_MS | 2000 | No | Earliest point (ms after the stream starts) a beep may be accepted |
AMD_LOG_LEVEL | info | No | SLF4J log level |
AMD_DUMP_AUDIO | false | No | Dump streams to WAV for debugging |
AMD_DUMP_AUDIO_PATH | /tmp/amd-dumps | No | Dump directory. The compose file mounts ./volumes/amd-dumps here; it must be writable by uid 10001 |
AMD_WORKER_API_TOKEN | — | Yes | Bearer token for callback auth |
AMD_ACTION_CALLBACK_URL | http://nginx/api/voice/amd-action | No | Laravel callback URL |
AMD_SESSION_CHECK_URL | callback URL with /amd-action replaced by /amd-session | No | Session admission URL. If it cannot be derived, every stream fails the check |
AMD_WORKER_API_TOKEN must match the value configured in OPBX (AMD_WORKER_API_TOKEN in .env). Laravel compares it in constant time and answers 401 on a mismatch, or 503 when its own token is empty. The worker refuses to start when the token is empty or a shipped placeholder.
Session Admission
A stream must belong to a live auto-dialer call:
- Cloudonix must send the
startevent within 5 seconds of connecting, or the socket is closed. - On
start, the worker posts the stream's session token to/api/voice/amd-session. Laravel answers 200 when it is an active auto-dialer call session, 404 otherwise. - Audio keeps flowing while the check runs. A result found before the answer is held until it arrives.
- An unknown session closes the stream ("Unknown session"), and any result for it is dropped.
Only 8 kHz audio is accepted; another sample rate closes the stream with code 1003. A second connection with the same stream ID is rejected, connections above AMD_MAX_CONCURRENT_STREAMS are closed with code 1013, and a connection to any path other than /ws/detect is closed.
Detection Pipeline
- Cloudonix sends a
connectedevent - Cloudonix sends a
startevent with custom parameters - The worker checks the session (see above)
- Cloudonix sends
mediaevents every 20 ms with Base64 mu-law audio - The worker decodes to PCM16 and resamples to 16 kHz
- Energy VAD cuts the audio into segments; a segment is handed to the detectors when speech ends, so a short tone followed by silence reaches them
- Every 500 ms, once the stream is at least
AMD_MIN_BEEP_START_MSold, the tone detector also scans the last 1.5 seconds of audio - The first detector to find a beep resolves the call as
voicemail. If nothing is found before the timeout, the result isunknown - The worker POSTs the result to
/api/voice/amd-action(5-second timeout, one retry after 1 second on a connection error or 5xx) and closes the WebSocket - Laravel stores the result on the call session, updates the Cloudonix session profile, and runs the campaign's action
Detectors
| Detector | Name in AMD_DETECTORS | How it works |
|---|---|---|
| ML beep model | beep_ml | MFCC features into an ONNX classifier (AMD_MODEL_PATH). Skipped if the model does not load |
| Tone energy | tone_energy | Looks for a steady tone with a clear onset. A tone that starts before AMD_MIN_BEEP_START_MS (default 2 seconds) is ignored, because a beep follows a greeting, and a tone already running at answer is ringback or early media |
The detectors report voicemail only. The worker never decides human; a call with no beep before the timeout is unknown.
Actions
The action is configured on the campaign (action_voicemail, action_human, action_unknown). Laravel also passes the values as <Parameter> elements on the <Stream> verb:
<Stream url="wss://.../ws/amd/detect">
<Parameter name="action_voicemail" value="HANGUP"/>
<Parameter name="action_human" value="CONTINUE"/>
<Parameter name="action_unknown" value="CONTINUE"/>
</Stream>
The worker echoes the action in its callback, but Laravel ignores it and uses the campaign's setting, so whoever opens a stream cannot control the call.
| Action Value | Behavior |
|---|---|
HANGUP (or empty) | Disconnect the session |
CONTINUE | Take no further action |
Laravel treats an empty campaign action as HANGUP. The worker's own echoed default (voicemail → HANGUP; human or unknown → CONTINUE) is only logged and has no effect.
Health Endpoint
curl http://localhost:8083/health
Returns JSON with status, model_loaded, active_streams, max_streams, total_detections, detection_breakdown (voicemail, human, unknown), avg_detection_time_ms, errors_total and uptime_seconds. The compose healthcheck calls it every 30 seconds, and the Platform Status page probes it every minute (PLATFORM_STATUS_AMD_URL, default http://amd-worker:8083/health).
Troubleshooting
| Issue | Solution |
|---|---|
| AMD results not appearing | Verify AMD_WORKER_API_TOKEN matches OPBX; check docker compose logs -f amd-worker for "Unknown session" or callback errors |
| Voicemail reported as unknown | The beep came before AMD_MIN_BEEP_START_MS, or after the timeout; review a dumped stream. To raise the timeout, add AMD_DEFAULT_TIMEOUT_SECONDS to the amd-worker environment in the compose file |
| High false-positive rate | Review dumped audio with OfflineToneTest and tune detector parameters |
| Model fails to load | Ensure AMD_MODEL_PATH points to a valid ONNX file (the compose file mounts ./amd-worker/models); the worker falls back to tone energy detection |
| Streams rejected | Check concurrent stream count against AMD_MAX_CONCURRENT_STREAMS |
Related Modules
- Campaign Manager — Campaigns that trigger AMD
- Call Logs — CDR includes AMD results
- Answering Machine Detection — turning detection on for a campaign