# mayamatakeshi/sip-lab architecture

> Architecture diagram of the GitHub repository mayamatakeshi/sip-lab, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mayamatakeshi/sip-lab
- Repository: https://github.com/mayamatakeshi/sip-lab

## Overview

#### Purpose and public entry points
`sip-lab` is a Linux Node.js native module for functional/integration testing of SIP systems, including RTP/media operations. JavaScript consumers load the package through `index.js` or `index_napi.js`; the native addon boundary is implemented by `src/addon.cpp` (legacy) and the Rust N-API implementation in `rust/src/napi_addon.rs`.

#### Binding and runtime boundary
Node scripts issue SIP/test actions and receive asynchronous events through a N-API/native binding rather than a web service. The package has two native implementation tracks: C/C++ PJPROJECT-facing sources under `src/`, and a Rust crate that exposes N-API and FFI adapters under `rust/src/`. Build metadata includes `binding.napi.gyp`, `rust/Cargo.toml`, and `rust/build.rs`.

#### SIP core and call control
The central SIP lifecycle layer creates and manages SIP endpoints, dialogs, registrations, requests, and call/media negotiation on PJPROJECT. Native orchestration resides in `src/sip.cpp` and `src/sip.hpp`; the Rust-side core/FFI split is `rust/src/sip_core.rs` and `rust/src/sip_ffi.rs`. SIP traffic can use standard transports plus the repository’s WebSocket transport extension in `src/pjsip/src/pjsip/sip_transport_ws.c`.

#### Event delivery and identity management
Native SIP/media callbacks are normalized into event payloads for JavaScript, with stable identifiers used to correlate calls, streams, and operations. Both implementation tracks contain this architectural pair: `src/event_templates.cpp` / `src/idmanager.cpp` and `rust/src/event_templates.rs` / `rust/src/id_manager.rs`. This is the key asynchronous boundary between PJPROJECT callback threads and Node-facing test logic.

#### Media graph and signal-processing extensions
The project extends PJMEDIA with custom ports and chain-link components used to compose test media paths. `src/pjmedia/src/chainlink/` provides wiring, WAV I/O, tone generation, DTMF detection, fax, and cross-correlation stages; `src/pjmedia/include/chainlink/chainlink.h` defines the extension surface. Custom PJMEDIA ports add BFSK/DTMF detection, fax, Flite TTS, PocketSphinx recognition, and WebSocket speech integration under `src/pjmedia/src/pjmedia/`.

#### External protocols and services
The native stack depends on PJPROJECT for SIP/SDP/RTP/media processing and system codec/TLS/media libraries supplied on Debian/Ubuntu. It supports SIP over WebSocket, TLS/SRTP-oriented scenarios, and optional speech processing. `src/pjmedia/src/pjmedia/ws_speech_port.cpp` connects media to an external WebSocket speech service; `samples_extra/` demonstrates use with the separately deployed `ws_speech_server` and, indirectly, Google speech credentials/services.

#### Execution model and packaging
Consumers execute Node test scripts that drive the module, e.g. SIP calls, registration, reinvites, codecs, fax, and media assertions represented by `samples/`. The native server/bootstrap area is `src/server.cpp`, with logging in `src/log.cpp`. Distribution targets Debian/Ubuntu-compatible prebuilt native binaries, produced through `gen_prebuilds.sh` and containerized build environments in `docker-images/debian11/` and `docker-images/jammy/`.

Main entry points: [`src/addon.cpp`](https://github.com/mayamatakeshi/sip-lab/blob/master/src/addon.cpp) (N-API bindings), [`src/event_templates.cpp`](https://github.com/mayamatakeshi/sip-lab/blob/master/src/event_templates.cpp) (Event normalization), [`rust/src/napi_addon.rs`](https://github.com/mayamatakeshi/sip-lab/blob/master/rust/src/napi_addon.rs) (Rust N-API addon), [`index.js`](https://github.com/mayamatakeshi/sip-lab/blob/master/index.js) (Package entry points)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_node["Node package"]
  node_consumer["Node test scripts<br/>consumer"]
  node_entry["Package entry points<br/>JavaScript API<br/>[index.js]"]
  node_napi{{"N-API bindings<br/>native boundary<br/>[addon.cpp]"}}
  node_rust_napi{{"Rust N-API addon<br/>native boundary<br/>[napi_addon.rs]"}}
end

subgraph group_native["Native SIP runtime"]
  node_sip_orchestration["SIP lifecycle orchestration<br/>call control<br/>[sip.cpp]"]
  node_rust_sip_core["Rust SIP core &amp; FFI<br/>core adapter<br/>[sip_core.rs]"]
  node_pjproject[("PJPROJECT<br/>SIP/media stack")]
  node_ws_transport["SIP WebSocket transport<br/>transport extension<br/>[sip_transport_ws.c]"]
  node_events["Event normalization<br/>callback adapter"]
  node_identity["Stable ID manager<br/>identity service<br/>[idmanager.cpp]"]
  node_server["Native bootstrap &amp; logging<br/>runtime bootstrap<br/>[server.cpp]"]
end

subgraph group_media["PJMEDIA extensions"]
  node_chainlink["Chainlink media graph<br/>media pipeline"]
  node_chainlink_api["Chainlink extension API<br/>media interface<br/>[chainlink.h]"]
  node_custom_ports["Custom media ports<br/>signal processing"]
  node_ws_speech{{"WebSocket speech port<br/>external media adapter<br/>[ws_speech_port.cpp]"}}
end

subgraph group_build["Build &amp; distribution"]
  node_build_metadata["Native build metadata<br/>build definitions<br/>[binding.napi.gyp]"]
  node_prebuilds["Prebuilt binary pipeline<br/>distribution build<br/>[gen_prebuilds.sh]"]
  node_containers["Ubuntu build containers<br/>build environment"]
end

node_speech_service[("External speech service<br/>deployed service")]

node_consumer -->|"loads"| node_entry
node_entry -->|"invokes"| node_napi
node_entry -.->|"loads alternate addon"| node_rust_napi
node_napi -->|"issues SIP actions"| node_sip_orchestration
node_rust_napi -->|"calls"| node_rust_sip_core
node_sip_orchestration -->|"controls"| node_pjproject
node_rust_sip_core -->|"FFI access"| node_pjproject
node_pjproject -->|"extends with"| node_ws_transport
node_pjproject -->|"emits callbacks"| node_events
node_events -->|"correlates with"| node_identity
node_events -->|"asynchronous events"| node_napi
node_events -.->|"asynchronous events"| node_rust_napi
node_server -->|"initializes"| node_sip_orchestration
node_pjproject -->|"hosts media paths"| node_chainlink
node_chainlink_api -->|"defines extension surface"| node_chainlink
node_chainlink -->|"connects stages"| node_custom_ports
node_custom_ports -->|"includes"| node_ws_speech
node_ws_speech -->|"WebSocket audio"| node_speech_service
node_build_metadata -->|"builds legacy addon"| node_napi
node_build_metadata -->|"builds Rust addon"| node_rust_napi
node_containers -->|"provide build environment"| node_prebuilds
node_prebuilds -->|"packages binaries"| node_napi

click node_entry "https://github.com/mayamatakeshi/sip-lab/blob/master/index.js"
click node_napi "https://github.com/mayamatakeshi/sip-lab/blob/master/src/addon.cpp"
click node_rust_napi "https://github.com/mayamatakeshi/sip-lab/blob/master/rust/src/napi_addon.rs"
click node_sip_orchestration "https://github.com/mayamatakeshi/sip-lab/blob/master/src/sip.cpp"
click node_rust_sip_core "https://github.com/mayamatakeshi/sip-lab/blob/master/rust/src/sip_core.rs"
click node_ws_transport "https://github.com/mayamatakeshi/sip-lab/blob/master/src/pjsip/src/pjsip/sip_transport_ws.c"
click node_events "https://github.com/mayamatakeshi/sip-lab/blob/master/src/event_templates.cpp"
click node_identity "https://github.com/mayamatakeshi/sip-lab/blob/master/src/idmanager.cpp"
click node_server "https://github.com/mayamatakeshi/sip-lab/blob/master/src/server.cpp"
click node_chainlink_api "https://github.com/mayamatakeshi/sip-lab/blob/master/src/pjmedia/include/chainlink/chainlink.h"
click node_ws_speech "https://github.com/mayamatakeshi/sip-lab/blob/master/src/pjmedia/src/pjmedia/ws_speech_port.cpp"
click node_build_metadata "https://github.com/mayamatakeshi/sip-lab/blob/master/binding.napi.gyp"
click node_prebuilds "https://github.com/mayamatakeshi/sip-lab/blob/master/gen_prebuilds.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_consumer,node_entry,node_napi,node_rust_napi toneBlue
class node_sip_orchestration,node_rust_sip_core,node_pjproject,node_ws_transport,node_events,node_identity,node_server toneAmber
class node_chainlink,node_chainlink_api,node_custom_ports,node_ws_speech toneMint
class node_build_metadata,node_prebuilds,node_containers toneRose
class node_speech_service toneNeutral
```

## Components

### Node package

- **Node test scripts**: consumer · SIP, registration, codec, fax, and media test scenarios.
- [Package entry points](https://github.com/mayamatakeshi/sip-lab/blob/master/index.js): JavaScript API · `index.js`
- [N-API bindings](https://github.com/mayamatakeshi/sip-lab/blob/master/src/addon.cpp): native boundary · Delivers commands and asynchronous native events to Node. · `src/addon.cpp`
- [Rust N-API addon](https://github.com/mayamatakeshi/sip-lab/blob/master/rust/src/napi_addon.rs): native boundary · `rust/src/napi_addon.rs`

### Native SIP runtime

- [SIP lifecycle orchestration](https://github.com/mayamatakeshi/sip-lab/blob/master/src/sip.cpp): call control · Manages endpoints, dialogs, registrations, requests, calls, and negotiation. · `src/sip.cpp`
- [Rust SIP core & FFI](https://github.com/mayamatakeshi/sip-lab/blob/master/rust/src/sip_core.rs): core adapter · `rust/src/sip_core.rs`
- **PJPROJECT**: SIP/media stack · Provides SIP, SDP, RTP, and base media processing.
- [SIP WebSocket transport](https://github.com/mayamatakeshi/sip-lab/blob/master/src/pjsip/src/pjsip/sip_transport_ws.c): transport extension · `src/pjsip/src/pjsip/sip_transport_ws.c`
- [Event normalization](https://github.com/mayamatakeshi/sip-lab/blob/master/src/event_templates.cpp): callback adapter · Converts SIP and media callbacks into Node-facing event payloads. · `src/event_templates.cpp`
- [Stable ID manager](https://github.com/mayamatakeshi/sip-lab/blob/master/src/idmanager.cpp): identity service · Correlates calls, streams, and operations across asynchronous events. · `src/idmanager.cpp`
- [Native bootstrap & logging](https://github.com/mayamatakeshi/sip-lab/blob/master/src/server.cpp): runtime bootstrap · `src/server.cpp`

### PJMEDIA extensions

- **Chainlink media graph**: media pipeline · Composes test media paths with WAV, tones, DTMF, fax, and correlation stages.
- [Chainlink extension API](https://github.com/mayamatakeshi/sip-lab/blob/master/src/pjmedia/include/chainlink/chainlink.h): media interface · `src/pjmedia/include/chainlink/chainlink.h`
- **Custom media ports**: signal processing · Adds BFSK/DTMF, fax, TTS, and speech recognition ports.
- [WebSocket speech port](https://github.com/mayamatakeshi/sip-lab/blob/master/src/pjmedia/src/pjmedia/ws_speech_port.cpp): external media adapter · `src/pjmedia/src/pjmedia/ws_speech_port.cpp`

### Build & distribution

- [Native build metadata](https://github.com/mayamatakeshi/sip-lab/blob/master/binding.napi.gyp): build definitions · `binding.napi.gyp`
- [Prebuilt binary pipeline](https://github.com/mayamatakeshi/sip-lab/blob/master/gen_prebuilds.sh): distribution build · Produces Debian/Ubuntu-compatible native binaries. · `gen_prebuilds.sh`
- **Ubuntu build containers**: build environment

### Other components

- **External speech service**: deployed service · Optional WebSocket speech server and Google-backed speech integration.

## Connections

- Node test scripts → Package entry points: loads
- Package entry points → N-API bindings: invokes
- Package entry points → Rust N-API addon: loads alternate addon
- N-API bindings → SIP lifecycle orchestration: issues SIP actions
- Rust N-API addon → Rust SIP core & FFI: calls
- SIP lifecycle orchestration → PJPROJECT: controls
- Rust SIP core & FFI → PJPROJECT: FFI access
- PJPROJECT → SIP WebSocket transport: extends with
- PJPROJECT → Event normalization: emits callbacks
- Event normalization → Stable ID manager: correlates with
- Event normalization → N-API bindings: asynchronous events
- Event normalization → Rust N-API addon: asynchronous events
- Native bootstrap & logging → SIP lifecycle orchestration: initializes
- PJPROJECT → Chainlink media graph: hosts media paths
- Chainlink extension API → Chainlink media graph: defines extension surface
- Chainlink media graph → Custom media ports: connects stages
- Custom media ports → WebSocket speech port: includes
- WebSocket speech port → External speech service: WebSocket audio
- Native build metadata → N-API bindings: builds legacy addon
- Native build metadata → Rust N-API addon: builds Rust addon
- Ubuntu build containers → Prebuilt binary pipeline: provide build environment
- Prebuilt binary pipeline → N-API bindings: packages binaries
