| 1 | # PJSIP/PJPROJECT |
| 2 | |
| 3 | Open-source multimedia communication library in C (SIP, SDP, RTP, STUN, TURN, ICE). |
| 4 | |
| 5 | ## Build & Test |
| 6 | |
| 7 | - **Build**: `./configure && make -j3` (timeout 120s+, NEVER CANCEL) |
| 8 | - **Skip `make dep`** — it often produces corrupt `.depend` files. `make` generates deps automatically. If build fails with "missing separator" in `.depend`, run `find . -name "*.depend" -delete` and retry. |
| 9 | - **Targeted build**: `cd pjlib/build && make` (or `pjsip/build`, etc.) |
| 10 | - **Full clean**: `make clean` (required when switching SSL backends or configure options) |
| 11 | - Get target arch: `make infotarget` (e.g., `x86_64-pc-linux-gnu`) |
| 12 | - **Three build systems** — when adding/removing source files, update all three: |
| 13 | 1. GNU Makefile (e.g., `pjlib/build/Makefile`) |
| 14 | 2. MSVC project (e.g., `pjlib/build/pjlib.vcxproj`) |
| 15 | 3. CMake (`CMakeLists.txt`) |
| 16 | |
| 17 | ### Running Tests |
| 18 | |
| 19 | Run via Makefile (NEVER CANCEL, 5+ min each, timeout 600s): |
| 20 | ``` |
| 21 | make pjlib-test # core library |
| 22 | make pjsip-test # SIP stack |
| 23 | make pjsua-test # high-level API (Python) |
| 24 | ``` |
| 25 | |
| 26 | Run specific test directly — **must run from the bin/ directory**: |
| 27 | ``` |
| 28 | cd pjlib/bin && ./pjlib-test-x86_64-pc-linux-gnu timer_test |
| 29 | cd pjlib/bin && ./pjlib-test-x86_64-pc-linux-gnu --list |
| 30 | ``` |
| 31 | Options: `-w N` (worker threads), `--shuffle`, `--ci-mode`, `--stop-err` |
| 32 | |
| 33 | Quick smoke test: `cd tests/pjsua && python3 run.py mod_run.py scripts-run/100_simple.py` |
| 34 | |
| 35 | ### Minimum Validation (mandatory after every change) |
| 36 | |
| 37 | - **Always run `make -j3`** — zero errors, zero warnings (`-Wall` enabled). |
| 38 | - For targeted builds: `cd pjlib/build && make` or `cd pjsip/build && make`. |
| 39 | - **Known warning**: `PJ_TODO(id)` macro triggers `-Wunused-label` intentionally. Suppress with `#define PJ_TODO(x)` in `config_site.h`. |
| 40 | |
| 41 | ### Configure Options |
| 42 | |
| 43 | - `--with-gnutls=/usr/` — GnuTLS instead of OpenSSL (requires `make clean` first) |
| 44 | - `--disable-ssl` — disable SSL/TLS |
| 45 | - `--enable-shared` — shared libraries |
| 46 | - Feature toggles: `#define` in `pjlib/include/pj/config_site.h` |
| 47 | |
| 48 | ### Config Site |
| 49 | |
| 50 | - `pjlib/include/pj/config_site.h` — local configuration overrides, NOT tracked by git |
| 51 | - CI uses `config_site_test.h` (copied to `config_site.h` in workflows) |
| 52 | - SSL tests will fail with timeouts in sandboxed environments — expected |
| 53 | |
| 54 | ## Repository Structure |
| 55 | |
| 56 | ``` |
| 57 | pjlib/ # Base framework (OS abstraction, data structures, pool allocator) |
| 58 | pjlib-util/ # Utilities (DNS, STUN, XML, JSON, WebSocket) |
| 59 | pjnath/ # NAT traversal (STUN, TURN, ICE) |
| 60 | pjmedia/ # Media framework (codecs, transport, audio/video) |
| 61 | pjsip/ # SIP stack (transactions, dialogs, UA layer) |
| 62 | pjsip-apps/ # Applications and samples |
| 63 | third_party/ # Bundled third-party libs (srtp, resample, etc.) |
| 64 | tests/pjsua/ # Python-based PJSUA tests |
| 65 | .github/ # CI workflows |
| 66 | ``` |
| 67 | |
| 68 | ### Architecture Layers (top → bottom) |
| 69 | |
| 70 | 1. **PJSUA2** (C++) / **PJSUA** (C high-level) — `pjsip/include/pjsua-lib/` |
| 71 | 2. **PJSIP UA** — dialogs, calls, presence — `pjsip/include/pjsip-ua/` |
| 72 | 3. **PJSIP Core** — transactions, transport, parsing — `pjsip/include/pjsip/` |
| 73 | 4. **PJMEDIA** — media sessions, codecs, RTP — `pjmedia/include/pjmedia/` |
| 74 | 5. **PJNATH** — ICE, STUN, TURN — `pjnath/include/pjnath/` |
| 75 | 6. **PJLIB** — pool allocator, OS abstraction — `pjlib/include/pj/` |
| 76 | |
| 77 | ### Key Files |
| 78 | |
| 79 | - `pjlib/include/pj/config_site.h` — main configuration overrides |
| 80 | - `pjlib/include/pj/config.h` — default configuration values |
| 81 | - `build.mak` — generated build configuration |
| 82 | - ICE/NAT: `pjnath/include/pjnath/ice_strmtp.h` |
| 83 | - Call handling: `pjsip/src/pjsua-lib/pjsua_call.c` |
| 84 | - Transport: `pjsip/include/pjsip/sip_transport.h` |
| 85 | |
| 86 | ## Coding Conventions |
| 87 | |
| 88 | Follow: https://docs.pjsip.org/en/latest/get-started/coding-style.html |
| 89 | |
| 90 | Documentation source (for when the docs site is not directly accessible): |
| 91 | https://github.com/pjsip/pjproject_docs |
| 92 | |
| 93 | ### Essentials |
| 94 | - **ANSI C (C89/C90)** for core modules. No declarations after statements. |
| 95 | - **4 spaces** indentation (no tabs). ~80 char lines. |
| 96 | - **K&R braces**: same line for control statements, next line for functions/structs. |
| 97 | - **`/* */` comments** for C code, `/** */` for Doxygen on public APIs. |
| 98 | - **Module prefixes**: `pj_` (pjlib), `pjsip_` (sip), `pjmedia_` (media), `pjnath_` (nat) |
| 99 | - **Constants**: ALL_CAPS with prefix (`PJSIP_MAX_URL_SIZE`, `PJ_TRUE`) |
| 100 | |
| 101 | ### Memory Management |
| 102 | - Core C modules use **pool-based allocation** (`pj_pool_t`). |
| 103 | - Use `PJ_POOL_ALLOC_T()`, `PJ_POOL_ZALLOC_T()`, `pj_pool_alloc()`. |
| 104 | - Pools freed as a whole — no individual deallocation. |
| 105 | - **No `malloc()`/`free()`** in core C code. |
| 106 | |
| 107 | ### String Handling |
| 108 | - `pj_str_t` (pointer + length), NOT null-terminated. |
| 109 | - Never assume null-termination. Use `pj_strcmp()`, `pj_stricmp()`. |
| 110 | |
| 111 | ### Error Handling |
| 112 | - Functions return `pj_status_t`. `PJ_SUCCESS` (0) = success. |
| 113 | - Always check return values. Use `PJ_ASSERT_RETURN()` for preconditions. |
| 114 | |
| 115 | ### Logging |
| 116 | - `PJ_LOG(level, (sender, format, ...))` — note double parentheses. |
| 117 | - Each file defines `THIS_FILE`. Levels: 0 (fatal) to 6 (trace). |
| 118 | |
| 119 | ## Architecture Notes |
| 120 | |
| 121 | ### Threading Model |
| 122 | - Single-threaded (polling) or multi-threaded. |
| 123 | - Use `pj_mutex_t` / `pj_lock_t` — NOT pthread directly. |
| 124 | - Callbacks may fire from worker threads — be thread-safe. |
| 125 | - **Avoid invoking user callbacks while holding a mutex** — release lock first to prevent deadlock. |
| 126 | |
| 127 | ### Group Lock (`pj_grp_lock_t`) |
| 128 | - Mutual exclusion + reference counting + lock ordering in one primitive. |
| 129 | - `add_ref()` before scheduling timer/callback, `dec_ref()` in the callback. |
| 130 | - Never destroy a lock that other threads may reference — use ref counting. |
| 131 | - Lock ordering: PJSUA_LOCK > dialog grp_lock > transaction grp_lock. |
| 132 | - Debug: enable `PJ_GRP_LOCK_DEBUG` in `config_site.h`. |
| 133 | |
| 134 | ### Platform Differences |
| 135 | - **I/O Queue backends**: epoll (Linux), kqueue (macOS/BSD), select (portable), IOCP (Windows) |
| 136 | - IOCP backend has different threading semantics than epoll/select. |
| 137 | |
| 138 | ## Do's and Don'ts |
| 139 | |
| 140 | ### DO: |
| 141 | - Reference existing patterns before writing new code |
| 142 | - Follow module prefix naming strictly |
| 143 | - Check `pj_status_t` return values everywhere |
| 144 | - Declare all variables at top of block (C89) |
| 145 | - Add Doxygen `/** */` for new public APIs |
| 146 | - Prefer param structs over many positional arguments for extensibility |
| 147 | |
| 148 | ### DON'T: |
| 149 | - Don't use malloc/free in core C modules |
| 150 | - Don't assume null-terminated strings with `pj_str_t` |
| 151 | - Don't add C++ in core C modules |
| 152 | - Don't modify public headers without understanding downstream impact |
| 153 | - Don't destroy objects with pending timers/async callbacks |
| 154 | - Don't declare variables after statements (C89) |
| 155 | |
| 156 | ## CI Integration |
| 157 | |
| 158 | - GitHub Actions: `.github/workflows/` (Linux, Mac, Windows) |
| 159 | - Uses `cirunner` tool for test execution with timeouts |
| 160 | - Tests multiple configurations (SSL/no-SSL, GnuTLS, video codecs, etc.) |
| 161 | - `--ci-mode` widens timing tolerances for slow CI runners |
| 162 | |
| 163 | ### Writing Robust Tests |
| 164 | - **No precise timing** — use generous tolerances or event-driven sync. CI runners can be 5-10x slower. |
| 165 | - Verify ASan compatibility for platform-specific features (sigaltstack, thread stacks). |