sip: integrate PJSIP pjmedia G.722 and G.711 codecs and direct aplay ringbuffer playout
This commit is contained in:
1 parent
0af39d9fa0
commit
83dd79fe76
6 files changed
+619
-353
No files matched your search
@@ -13,6 +13,7 @@ It uses a pure **X11 (`Xlibre` + `sxmo-de-dwm`)** stack (no Wayland) with **Adre
|
|||||||
- `theme.h`: header forwarding to `../neoui/neoui.h` for unified `.Xresources` / `~/.xthemes/*.xth` parsing, proportional font sizing (`theme.baseFontSize`), automatic shade derivation, and live Xft/Pango theme reloading (`PluginTheme`).
|
- `theme.h`: header forwarding to `../neoui/neoui.h` for unified `.Xresources` / `~/.xthemes/*.xth` parsing, proportional font sizing (`theme.baseFontSize`), automatic shade derivation, and live Xft/Pango theme reloading (`PluginTheme`).
|
||||||
- `phone_data.h`: shared X11 geometry (`240x296` below the 24px bar), FIFO IPC (`/tmp/dwm_plugin_<name>.fifo`), softkey chord detection, Sxmo TSV contact/call/SMS parsers, and `libjpeg`/`libpng` helpers.
|
- `phone_data.h`: shared X11 geometry (`240x296` below the 24px bar), FIFO IPC (`/tmp/dwm_plugin_<name>.fifo`), softkey chord detection, Sxmo TSV contact/call/SMS parsers, and `libjpeg`/`libpng` helpers.
|
||||||
- `net_tls.h`: shared C++20 TCP + OpenSSL TLS + STARTTLS socket class (`NetStream`), HTTP/HTTPS `GET` client (`httpGet`), MD5/CSPRNG helpers, and RFC 2047 / Base64 / Quoted-Printable MIME decoders.
|
- `net_tls.h`: shared C++20 TCP + OpenSSL TLS + STARTTLS socket class (`NetStream`), HTTP/HTTPS `GET` client (`httpGet`), MD5/CSPRNG helpers, and RFC 2047 / Base64 / Quoted-Printable MIME decoders.
|
||||||
|
- `pjsip_media.h`: PJSIP (`pjproject` / `pjmedia-codec/g722` & `pjmedia/alaw_ulaw`) ITU-T G.722 wideband 16 kHz SB-ADPCM and G.711 PCMU/PCMA codec implementation for `sip.so`.
|
||||||
- `monotone.h` + `bananarchy_melody.cpp` (`/usr/bin/bananarchy-melody`): Nokia RTTTL (`.nrt`, `.rtttl`, `.rtx`), Ericsson eMelody (`.emy`), and IrDA iMelody (`.imy`) monophonic ringtone parser and 48 kHz WAV synthesizer.
|
- `monotone.h` + `bananarchy_melody.cpp` (`/usr/bin/bananarchy-melody`): Nokia RTTTL (`.nrt`, `.rtttl`, `.rtx`), Ericsson eMelody (`.emy`), and IrDA iMelody (`.imy`) monophonic ringtone parser and 48 kHz WAV synthesizer.
|
||||||
- `locker_plugin.cpp` (`locker.so` / `xlocker`) + `verify_passcode.c` (`/usr/bin/bananarchy-verify-passcode`), `passcode_plugin.cpp` (`passcode.so` / `xpasscode`), `dialer_plugin.cpp` (`dialer.so` / `xdialer`), `calllog_plugin.cpp` (`calllog.so` / `xcalllog`), `callscreen_plugin.cpp` (`callscreen.so` / `xcallscreen`), `contacts_plugin.cpp` (`contacts.so` / `xcontacts`), `sms_plugin.cpp` (`sms.so` / `xsms`), `files_plugin.cpp` (`files.so` / `xfiles`), `camera_plugin.cpp` (`camera.so` / `xcamera`), `themes_plugin.cpp` (`themes.so` / `xthemes`), `modem_plugin.cpp` (`modem.so` / `xmodem`), `networks_plugin.cpp` (`networks.so` / `xnetworks` / `xbluetooth`), `ringtones_plugin.cpp` (`ringtones.so` / `xringtones`), `wallpapers_plugin.cpp` (`wallpapers.so` / `xwallpapers` / `xbackground`), `music_plugin.cpp` (`music.so` / `xmusic` / `xplayer`), `sip_plugin.cpp` (`sip.so` / `xsip`), `email_plugin.cpp` (`email.so` / `xemail` / `xmail`), `irc_plugin.cpp` (`irc.so` / `xirc`), `tox_plugin.cpp` (`tox.so` / `xtox`), `smallweb_plugin.cpp` (`smallweb.so` / `xkopher` / `kopher` / `xsmallweb`), `webarchy_plugin.cpp` (`webarchy.so` / `webarchy` / `xwebarchy`), `gps_plugin.cpp` (`gps.so` / `xgps` / `xmaps` / `xnav`).
|
- `locker_plugin.cpp` (`locker.so` / `xlocker`) + `verify_passcode.c` (`/usr/bin/bananarchy-verify-passcode`), `passcode_plugin.cpp` (`passcode.so` / `xpasscode`), `dialer_plugin.cpp` (`dialer.so` / `xdialer`), `calllog_plugin.cpp` (`calllog.so` / `xcalllog`), `callscreen_plugin.cpp` (`callscreen.so` / `xcallscreen`), `contacts_plugin.cpp` (`contacts.so` / `xcontacts`), `sms_plugin.cpp` (`sms.so` / `xsms`), `files_plugin.cpp` (`files.so` / `xfiles`), `camera_plugin.cpp` (`camera.so` / `xcamera`), `themes_plugin.cpp` (`themes.so` / `xthemes`), `modem_plugin.cpp` (`modem.so` / `xmodem`), `networks_plugin.cpp` (`networks.so` / `xnetworks` / `xbluetooth`), `ringtones_plugin.cpp` (`ringtones.so` / `xringtones`), `wallpapers_plugin.cpp` (`wallpapers.so` / `xwallpapers` / `xbackground`), `music_plugin.cpp` (`music.so` / `xmusic` / `xplayer`), `sip_plugin.cpp` (`sip.so` / `xsip`), `email_plugin.cpp` (`email.so` / `xemail` / `xmail`), `irc_plugin.cpp` (`irc.so` / `xirc`), `tox_plugin.cpp` (`tox.so` / `xtox`), `smallweb_plugin.cpp` (`smallweb.so` / `xkopher` / `kopher` / `xsmallweb`), `webarchy_plugin.cpp` (`webarchy.so` / `webarchy` / `xwebarchy`), `gps_plugin.cpp` (`gps.so` / `xgps` / `xmaps` / `xnav`).
|
||||||
- `dwm_plugin_runner.c`: lightweight `/usr/bin/dwm-plugin` launcher symlinked by `xlocker`, `xpasscode`, `xdrawer`, `xdialer`, `xcalllog`, `xcallscreen`, `xcontacts`, `xsms`, `xfiles`, `xcamera`, `xthemes`, `xmodem`, `xnetworks`, `xbluetooth`, `xringtones`, `xwallpapers`, `xbackground`, `xmusic`, `xplayer`, `xsip`, `xemail`, `xmail`, `xirc`, `xtox`, `xkopher`, `kopher`, `xsmallweb`, `webarchy`, `xwebarchy`, `xgps`, `xmaps`, `xnav`, `dmenu`, and `xdmenu`.
|
- `dwm_plugin_runner.c`: lightweight `/usr/bin/dwm-plugin` launcher symlinked by `xlocker`, `xpasscode`, `xdrawer`, `xdialer`, `xcalllog`, `xcallscreen`, `xcontacts`, `xsms`, `xfiles`, `xcamera`, `xthemes`, `xmodem`, `xnetworks`, `xbluetooth`, `xringtones`, `xwallpapers`, `xbackground`, `xmusic`, `xplayer`, `xsip`, `xemail`, `xmail`, `xirc`, `xtox`, `xkopher`, `kopher`, `xsmallweb`, `webarchy`, `xwebarchy`, `xgps`, `xmaps`, `xnav`, `dmenu`, and `xdmenu`.
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ Release versions follow the `<year-letter><MM><DD>` format (where `2026` is `a`,
|
|||||||
|
|
||||||
### New features and improvements
|
### New features and improvements
|
||||||
- **Native NeoUI communication and internet suite (`xsip`, `xemail`, `xirc`, `xtox`, `xkopher`, `webarchy`)**:
|
- **Native NeoUI communication and internet suite (`xsip`, `xemail`, `xirc`, `xtox`, `xkopher`, `webarchy`)**:
|
||||||
- **SIP VoIP client (`sip.so` / `xsip`)**: implemented an RFC 3261 SIP softphone (`Dialer`, `Accounts`, `History` tabs) supporting both account-registered calling (with alphanumeric username/password multi-tap entry, Digest MD5 authentication, and background standby `REGISTER` keepalive) and direct accountless P2P/federated SIP URI calling (`user@host[:port]` or `sip:user@host[:port]`) with RFC 3263 DNS `SRV` (`_sip._udp.<domain>`) proxy discovery, RFC 3261 Timer A `INVITE` retransmission with multi-`A` proxy failover, `Record-Route`/`Contact` dialog routing, RFC 6189 **ZRTP** media-path key agreement (`DH3k` 3072-bit / `DH2k` 2048-bit Diffie-Hellman, `H0..H3` SHA-256 hash chain, Castagnoli CRC-32C framing, `a=zrtp-hash` SDP signaling, and 4-character Base32 `SAS` verification code) with RFC 3711 **SRTP** (`AES-128-CM` + `HMAC-SHA1-80`/`32`) encrypted voice media, incoming `INVITE` standby wakeup and call handling integrated with `xcallscreen` (`[SIP/<codec>]` / `[SIP/<codec>:<sas>]`), wideband and narrowband multi-codec RTP/SRTP audio (`G.711 PCMU`/`PCMA` 8 kHz prioritized by default in `AUTO` mode, `GSM 06.10` Full Rate 8 kHz, `Opus` 16 kHz / 48 kHz RTP clock with in-band FEC, `ITU-T G.722` 16 kHz wideband SB-ADPCM, `ITU-T G.726-32` 8 kHz ADPCM, `IMA ADPCM DVI4` 8/16 kHz, and `L16` uncompressed 16-bit PCM 8/16 kHz) with per-account and direct-mode codec preference selection, seamless early-media (`183 Session Progress` -> `200 OK`) `arecord`/`aplay` stream continuity, and RFC 2833 / RFC 4733 DTMF tones.
|
- **SIP VoIP client (`sip.so` / `xsip`)**: implemented an RFC 3261 SIP softphone (`Dialer`, `Accounts`, `History` tabs) supporting both account-registered calling (with alphanumeric username/password multi-tap entry, Digest MD5 authentication, and background standby `REGISTER` keepalive) and direct accountless P2P/federated SIP URI calling (`user@host[:port]` or `sip:user@host[:port]`) with RFC 3263 DNS `SRV` (`_sip._udp.<domain>`) proxy discovery, RFC 3261 Timer A `INVITE` retransmission with multi-`A` proxy failover, `Record-Route`/`Contact` dialog routing, RFC 6189 **ZRTP** media-path key agreement (`DH3k` 3072-bit / `DH2k` 2048-bit Diffie-Hellman, `H0..H3` SHA-256 hash chain, Castagnoli CRC-32C framing, `a=zrtp-hash` SDP signaling, and 4-character Base32 `SAS` verification code) with RFC 3711 **SRTP** (`AES-128-CM` + `HMAC-SHA1-80`/`32`) encrypted voice media, incoming `INVITE` standby wakeup and call handling integrated with `xcallscreen` (`[SIP/<codec>]` / `[SIP/<codec>:<sas>]`), wideband and narrowband multi-codec RTP/SRTP audio (`G.711 PCMU`/`PCMA` 8 kHz via PJSIP `pjmedia`, `GSM 06.10` Full Rate 8 kHz, `Opus` 16 kHz / 48 kHz RTP clock with in-band FEC, `ITU-T G.722` 16 kHz wideband SB-ADPCM via PJSIP `pjmedia-codec/g722`, `ITU-T G.726-32` 8 kHz ADPCM, `IMA ADPCM DVI4` 8/16 kHz, and `L16` uncompressed 16-bit PCM 8/16 kHz) with per-account and direct-mode codec preference selection, seamless early-media (`183 Session Progress` -> `200 OK`) `arecord`/`aplay` stream continuity, and RFC 2833 / RFC 4733 DTMF tones.
|
||||||
- **Email client (`email.so` / `xemail` / `xmail`)**: implemented a multi-account `IMAP4rev1`, `POP3`, and `SMTP` client (`Inbox`, `Compose`, `Accounts` tabs) supporting implicit TLS (`993`/`995`/`465`), `STARTTLS` (`143`/`110`/`587`), and plain TCP, SASL `PLAIN`/`LOGIN` authentication, RFC 2047 encoded-word header decoding, Base64/Quoted-Printable MIME body decoding, HTML tag stripping, offline inbox cache (`~/.local/share/sxmo/email/`), full-screen reader, and message composer/reply.
|
- **Email client (`email.so` / `xemail` / `xmail`)**: implemented a multi-account `IMAP4rev1`, `POP3`, and `SMTP` client (`Inbox`, `Compose`, `Accounts` tabs) supporting implicit TLS (`993`/`995`/`465`), `STARTTLS` (`143`/`110`/`587`), and plain TCP, SASL `PLAIN`/`LOGIN` authentication, RFC 2047 encoded-word header decoding, Base64/Quoted-Printable MIME body decoding, HTML tag stripping, offline inbox cache (`~/.local/share/sxmo/email/`), full-screen reader, and message composer/reply.
|
||||||
- **IRCv3 client (`irc.so` / `xirc`)**: implemented a multi-server IRC client (`Chat`, `Buffers`, `Servers` tabs) with OpenSSL TLS, IRCv3 capability negotiation (`CAP LS 302`: `sasl`, `server-time`, `message-tags`, `multi-prefix`, `away-notify`, `account-notify`, `chghost`, `cap-notify`), `SASL PLAIN` authentication, `433` nick-in-use fallback and `470` channel forwarding tracking, slash commands (`/join`, `/part`, `/close`, `/msg`, `/nick`, `/me`, `/clear`, `/raw`, `/quit`), targeted buffer closing (`Menu -> Close Buffer` or `BackSpace` in `Buffers`), and a resident background mode (`BG Mode`) that keeps sockets connected when the window is closed and triggers notification hooks on nick highlights or private messages.
|
- **IRCv3 client (`irc.so` / `xirc`)**: implemented a multi-server IRC client (`Chat`, `Buffers`, `Servers` tabs) with OpenSSL TLS, IRCv3 capability negotiation (`CAP LS 302`: `sasl`, `server-time`, `message-tags`, `multi-prefix`, `away-notify`, `account-notify`, `chghost`, `cap-notify`), `SASL PLAIN` authentication, `433` nick-in-use fallback and `470` channel forwarding tracking, slash commands (`/join`, `/part`, `/close`, `/msg`, `/nick`, `/me`, `/clear`, `/raw`, `/quit`), targeted buffer closing (`Menu -> Close Buffer` or `BackSpace` in `Buffers`), and a resident background mode (`BG Mode`) that keeps sockets connected when the window is closed and triggers notification hooks on nick highlights or private messages.
|
||||||
- **Tox / Trifa P2P messenger and audio call client (`tox.so` / `xtox`)**: built an encrypted P2P messenger and voice call client (`Friends`, `Chat`, `My Tox ID` tabs) on `c-toxcore` (`libtoxcore.so.2`), featuring persistent profile storage (`~/.local/share/sxmo/tox/profile.tox`), non-blocking IPv4 DHT + TCP relay bootstrap, friend request handling, persistent chat logs, and standby incoming `ToxAV` 48 kHz Opus voice calls integrated with `xcallscreen` (`[TOX]`).
|
- **Tox / Trifa P2P messenger and audio call client (`tox.so` / `xtox`)**: built an encrypted P2P messenger and voice call client (`Friends`, `Chat`, `My Tox ID` tabs) on `c-toxcore` (`libtoxcore.so.2`), featuring persistent profile storage (`~/.local/share/sxmo/tox/profile.tox`), non-blocking IPv4 DHT + TCP relay bootstrap, friend request handling, persistent chat logs, and standby incoming `ToxAV` 48 kHz Opus voice calls integrated with `xcallscreen` (`[TOX]`).
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,435 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0-only
|
||||||
|
// PJSIP (pjmedia / pjmedia-codec) G.722 and G.711 audio codec implementation
|
||||||
|
// Adapted from Teluu pjproject (pjmedia/src/pjmedia-codec/g722 & pjmedia/alaw_ulaw.c)
|
||||||
|
|
||||||
|
#ifndef BANANARCHY_PJSIP_MEDIA_H
|
||||||
|
#define BANANARCHY_PJSIP_MEDIA_H
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace bananarchy {
|
||||||
|
namespace pjsip {
|
||||||
|
|
||||||
|
inline int saturate(int v, int maxVal, int minVal) {
|
||||||
|
if (v > maxVal) return maxVal;
|
||||||
|
if (v < minVal) return minVal;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// PJSIP ITU-T G.711 u-law / A-law (pjmedia/src/pjmedia/alaw_ulaw.c)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
inline int g711Search(int val, const int16_t* table, int size) {
|
||||||
|
for (int i = 0; i < size; ++i) {
|
||||||
|
if (val <= table[i]) return i;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint8_t linear2alaw(int pcmVal) {
|
||||||
|
static constexpr int16_t kSegEnd[8] = {
|
||||||
|
0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
|
||||||
|
};
|
||||||
|
int mask;
|
||||||
|
if (pcmVal >= 0) {
|
||||||
|
mask = 0xD5;
|
||||||
|
} else {
|
||||||
|
mask = 0x55;
|
||||||
|
pcmVal = -pcmVal - 1;
|
||||||
|
if (pcmVal < 0) pcmVal = 32767;
|
||||||
|
}
|
||||||
|
int seg = g711Search(pcmVal, kSegEnd, 8);
|
||||||
|
if (seg >= 8) {
|
||||||
|
return static_cast<uint8_t>(0x7F ^ mask);
|
||||||
|
}
|
||||||
|
uint8_t aval = static_cast<uint8_t>(seg << 4);
|
||||||
|
if (seg < 2) {
|
||||||
|
aval |= (pcmVal >> 4) & 0x0F;
|
||||||
|
} else {
|
||||||
|
aval |= (pcmVal >> (seg + 3)) & 0x0F;
|
||||||
|
}
|
||||||
|
return static_cast<uint8_t>(aval ^ mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int16_t alaw2linear(uint8_t aVal) {
|
||||||
|
aVal ^= 0x55;
|
||||||
|
int t = (aVal & 0x0F) << 4;
|
||||||
|
int seg = (static_cast<unsigned>(aVal) & 0x70) >> 4;
|
||||||
|
switch (seg) {
|
||||||
|
case 0:
|
||||||
|
t += 8;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
t += 0x108;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
t += 0x108;
|
||||||
|
t <<= seg - 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return static_cast<int16_t>((aVal & 0x80) ? t : -t);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint8_t linear2ulaw(int pcmVal) {
|
||||||
|
static constexpr int16_t kSegEnd[8] = {
|
||||||
|
0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
|
||||||
|
};
|
||||||
|
constexpr int kBias = 0x84;
|
||||||
|
int mask;
|
||||||
|
if (pcmVal < 0) {
|
||||||
|
pcmVal = kBias - pcmVal;
|
||||||
|
mask = 0x7F;
|
||||||
|
} else {
|
||||||
|
pcmVal += kBias;
|
||||||
|
mask = 0xFF;
|
||||||
|
}
|
||||||
|
int seg = g711Search(pcmVal, kSegEnd, 8);
|
||||||
|
if (seg >= 8) {
|
||||||
|
return static_cast<uint8_t>(0x7F ^ mask);
|
||||||
|
}
|
||||||
|
uint8_t uval = static_cast<uint8_t>((seg << 4) | ((pcmVal >> (seg + 3)) & 0x0F));
|
||||||
|
return static_cast<uint8_t>(uval ^ mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int16_t ulaw2linear(uint8_t uVal) {
|
||||||
|
constexpr int kBias = 0x84;
|
||||||
|
uVal = static_cast<uint8_t>(~uVal);
|
||||||
|
int t = ((uVal & 0x0F) << 3) + kBias;
|
||||||
|
t <<= (static_cast<unsigned>(uVal) & 0x70) >> 4;
|
||||||
|
return static_cast<int16_t>((uVal & 0x80) ? (kBias - t) : (t - kBias));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// PJSIP ITU-T G.722 Wideband 16 kHz Codec (pjmedia-codec/g722)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
inline constexpr int kG722QmfCoeff[24] = {
|
||||||
|
3, -11, -11, 53, 12, -156,
|
||||||
|
32, 362, -210, -805, 951, 3876,
|
||||||
|
3876, 951, -805, -210, 362, 32,
|
||||||
|
-156, 12, 53, -11, -11, 3
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr int kG722Qm4[16] = {
|
||||||
|
0, -20456, -12896, -8968,
|
||||||
|
-6288, -4240, -2584, -1200,
|
||||||
|
20456, 12896, 8968, 6288,
|
||||||
|
4240, 2584, 1200, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr int kG722Ilb[32] = {
|
||||||
|
2048, 2093, 2139, 2186, 2233, 2282, 2332,
|
||||||
|
2383, 2435, 2489, 2543, 2599, 2656, 2714,
|
||||||
|
2774, 2834, 2896, 2960, 3025, 3091, 3158,
|
||||||
|
3228, 3298, 3371, 3444, 3520, 3597, 3676,
|
||||||
|
3756, 3838, 3922, 4008
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr int kG722Wl[8] = {
|
||||||
|
-60, -30, 58, 172, 334, 538, 1198, 3042
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr int kG722Rl42[16] = {
|
||||||
|
0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr int kG722Qm2[4] = { -7408, -1616, 7408, 1616 };
|
||||||
|
inline constexpr int kG722Wh[3] = { 0, -214, 798 };
|
||||||
|
inline constexpr int kG722Rh2[4] = { 2, 1, 2, 1 };
|
||||||
|
|
||||||
|
struct G722BandState {
|
||||||
|
int s = 0;
|
||||||
|
int det = 32;
|
||||||
|
int sp = 0;
|
||||||
|
int sz = 0;
|
||||||
|
int r[3] {};
|
||||||
|
int a[3] {};
|
||||||
|
int ap[3] {};
|
||||||
|
int p[3] {};
|
||||||
|
int d[7] {};
|
||||||
|
int b[7] {};
|
||||||
|
int bp[7] {};
|
||||||
|
int sg[7] {};
|
||||||
|
int nb = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline int g722Block2l(int il, int detl) {
|
||||||
|
int ril = il >> 2;
|
||||||
|
int wd2 = kG722Qm4[ril];
|
||||||
|
return (detl * wd2) >> 15;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int g722Block3l(G722BandState& band, int il) {
|
||||||
|
int ril = il >> 2;
|
||||||
|
int il4 = kG722Rl42[ril];
|
||||||
|
int wd = (band.nb * 32512) >> 15;
|
||||||
|
int nbpl = saturate(wd + kG722Wl[il4], 18432, 0);
|
||||||
|
int wd1 = (nbpl >> 6) & 31;
|
||||||
|
int wd2 = nbpl >> 11;
|
||||||
|
int wd3 = ((8 - wd2) < 0) ? (kG722Ilb[wd1] << (wd2 - 8))
|
||||||
|
: (kG722Ilb[wd1] >> (8 - wd2));
|
||||||
|
band.nb = nbpl;
|
||||||
|
return wd3 << 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int g722Block2h(int ih, int deth) {
|
||||||
|
return (deth * kG722Qm2[ih & 3]) >> 15;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int g722Block3h(G722BandState& band, int ih) {
|
||||||
|
int ih2 = kG722Rh2[ih & 3];
|
||||||
|
int wd = (band.nb * 32512) >> 15;
|
||||||
|
int nbph = saturate(wd + kG722Wh[ih2], 22528, 0);
|
||||||
|
int wd1 = (nbph >> 6) & 31;
|
||||||
|
int wd2 = nbph >> 11;
|
||||||
|
int wd3 = ((10 - wd2) < 0) ? (kG722Ilb[wd1] << (wd2 - 10))
|
||||||
|
: (kG722Ilb[wd1] >> (10 - wd2));
|
||||||
|
band.nb = nbph;
|
||||||
|
return wd3 << 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int g722Block4(G722BandState& band, int dl) {
|
||||||
|
int sl = band.s;
|
||||||
|
band.d[0] = dl;
|
||||||
|
|
||||||
|
// RECONS
|
||||||
|
band.r[0] = saturate(sl + band.d[0], 32767, -32768);
|
||||||
|
|
||||||
|
// PARREC
|
||||||
|
band.p[0] = saturate(band.d[0] + band.sz, 32767, -32768);
|
||||||
|
|
||||||
|
// UPPOL2
|
||||||
|
band.sg[0] = band.p[0] >> 15;
|
||||||
|
band.sg[1] = band.p[1] >> 15;
|
||||||
|
band.sg[2] = band.p[2] >> 15;
|
||||||
|
|
||||||
|
int wd1 = saturate(band.a[1] * 4, 32767, -32768);
|
||||||
|
int wd2 = (band.sg[0] == band.sg[1]) ? -wd1 : wd1;
|
||||||
|
if (wd2 > 32767) wd2 = 32767;
|
||||||
|
wd2 = wd2 >> 7;
|
||||||
|
|
||||||
|
int wd3 = (band.sg[0] == band.sg[2]) ? 128 : -128;
|
||||||
|
int wd4 = wd2 + wd3;
|
||||||
|
int wd5 = (band.a[2] * 32512) >> 15;
|
||||||
|
|
||||||
|
band.ap[2] = saturate(wd4 + wd5, 12288, -12288);
|
||||||
|
|
||||||
|
// UPPOL1
|
||||||
|
band.sg[0] = band.p[0] >> 15;
|
||||||
|
band.sg[1] = band.p[1] >> 15;
|
||||||
|
|
||||||
|
wd1 = (band.sg[0] == band.sg[1]) ? 192 : -192;
|
||||||
|
wd2 = (band.a[1] * 32640) >> 15;
|
||||||
|
|
||||||
|
band.ap[1] = saturate(wd1 + wd2, 32767, -32768);
|
||||||
|
wd3 = saturate(15360 - band.ap[2], 32767, -32768);
|
||||||
|
if (band.ap[1] > wd3) band.ap[1] = wd3;
|
||||||
|
else if (band.ap[1] < -wd3) band.ap[1] = -wd3;
|
||||||
|
|
||||||
|
// UPZERO
|
||||||
|
wd1 = (band.d[0] == 0) ? 0 : 128;
|
||||||
|
band.sg[0] = band.d[0] >> 15;
|
||||||
|
|
||||||
|
for (int i = 1; i < 7; ++i) {
|
||||||
|
band.sg[i] = band.d[i] >> 15;
|
||||||
|
wd2 = (band.sg[i] == band.sg[0]) ? wd1 : -wd1;
|
||||||
|
wd3 = (band.b[i] * 32640) >> 15;
|
||||||
|
band.bp[i] = saturate(wd2 + wd3, 32767, -32768);
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELAYA
|
||||||
|
for (int i = 6; i > 0; --i) {
|
||||||
|
band.d[i] = band.d[i - 1];
|
||||||
|
band.b[i] = band.bp[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 2; i > 0; --i) {
|
||||||
|
band.r[i] = band.r[i - 1];
|
||||||
|
band.p[i] = band.p[i - 1];
|
||||||
|
band.a[i] = band.ap[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// FILTEP
|
||||||
|
wd1 = saturate(band.r[1] + band.r[1], 32767, -32768);
|
||||||
|
wd1 = (band.a[1] * wd1) >> 15;
|
||||||
|
|
||||||
|
wd2 = saturate(band.r[2] + band.r[2], 32767, -32768);
|
||||||
|
wd2 = (band.a[2] * wd2) >> 15;
|
||||||
|
|
||||||
|
band.sp = saturate(wd1 + wd2, 32767, -32768);
|
||||||
|
|
||||||
|
// FILTEZ
|
||||||
|
band.sz = 0;
|
||||||
|
for (int i = 6; i > 0; --i) {
|
||||||
|
int wd = saturate(band.d[i] + band.d[i], 32767, -32768);
|
||||||
|
band.sz += (band.b[i] * wd) >> 15;
|
||||||
|
band.sz = saturate(band.sz, 32767, -32768);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PREDIC
|
||||||
|
sl = saturate(band.sp + band.sz, 32767, -32768);
|
||||||
|
return sl;
|
||||||
|
}
|
||||||
|
|
||||||
|
class G722Codec {
|
||||||
|
public:
|
||||||
|
G722Codec() { reset(); }
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
encLow_ = G722BandState{};
|
||||||
|
encHigh_ = G722BandState{};
|
||||||
|
encLow_.det = 32;
|
||||||
|
encHigh_.det = 8;
|
||||||
|
std::memset(txX_, 0, sizeof(txX_));
|
||||||
|
|
||||||
|
decLow_ = G722BandState{};
|
||||||
|
decHigh_ = G722BandState{};
|
||||||
|
decLow_.det = 32;
|
||||||
|
decHigh_.det = 8;
|
||||||
|
std::memset(rxXd_, 0, sizeof(rxXd_));
|
||||||
|
std::memset(rxXs_, 0, sizeof(rxXs_));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t encodeFrame(const int16_t* pcm16k, size_t numSamples, uint8_t* outBuf) {
|
||||||
|
static constexpr int kQ6[32] = {
|
||||||
|
0, 35, 72, 110, 150, 190, 233, 276, 323,
|
||||||
|
370, 422, 473, 530, 587, 650, 714, 786,
|
||||||
|
858, 940, 1023, 1121, 1219, 1339, 1458,
|
||||||
|
1612, 1765, 1980, 2195, 2557, 2919, 0, 0
|
||||||
|
};
|
||||||
|
static constexpr int kIln[32] = {
|
||||||
|
0, 63, 62, 31, 30, 29, 28, 27, 26, 25,
|
||||||
|
24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14,
|
||||||
|
13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 0
|
||||||
|
};
|
||||||
|
static constexpr int kIlp[32] = {
|
||||||
|
0, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52,
|
||||||
|
51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41,
|
||||||
|
40, 39, 38, 37, 36, 35, 34, 33, 32, 0
|
||||||
|
};
|
||||||
|
static constexpr int kIhn[3] = { 0, 1, 0 };
|
||||||
|
static constexpr int kIhp[3] = { 0, 3, 2 };
|
||||||
|
|
||||||
|
size_t outIdx = 0;
|
||||||
|
for (size_t i = 0; i + 1 < numSamples; i += 2) {
|
||||||
|
// tx_qmf (16-bit PCM input -> 14-bit sub-band scale via >> 14)
|
||||||
|
std::memmove(&txX_[2], txX_, 22 * sizeof(txX_[0]));
|
||||||
|
txX_[1] = pcm16k[i];
|
||||||
|
txX_[0] = pcm16k[i + 1];
|
||||||
|
|
||||||
|
int sumOdd = 0;
|
||||||
|
for (int j = 1; j < 24; j += 2) sumOdd += txX_[j] * kG722QmfCoeff[j];
|
||||||
|
int sumEven = 0;
|
||||||
|
for (int j = 0; j < 24; j += 2) sumEven += txX_[j] * kG722QmfCoeff[j];
|
||||||
|
|
||||||
|
int xlow = saturate((sumEven + sumOdd) >> 14, 16383, -16384);
|
||||||
|
int xhigh = saturate((sumEven - sumOdd) >> 14, 16383, -16384);
|
||||||
|
|
||||||
|
// block1l
|
||||||
|
int el = saturate(xlow - encLow_.s, 32767, -32768);
|
||||||
|
int sil = el >> 15;
|
||||||
|
int wd = (sil == 0) ? el : ((32767 - el) & 32767);
|
||||||
|
int mil = 1;
|
||||||
|
for (int j = 1; j < 30; ++j) {
|
||||||
|
int hdu = (kQ6[j] << 3) * encLow_.det;
|
||||||
|
int wd1 = hdu >> 15;
|
||||||
|
if (wd >= wd1) mil = j + 1;
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
int ilow = (sil == -1) ? kIln[mil] : kIlp[mil];
|
||||||
|
int dlowt = g722Block2l(ilow, encLow_.det);
|
||||||
|
encLow_.det = g722Block3l(encLow_, ilow);
|
||||||
|
encLow_.s = g722Block4(encLow_, dlowt);
|
||||||
|
|
||||||
|
// block1h
|
||||||
|
int eh = saturate(xhigh - encHigh_.s, 32767, -32768);
|
||||||
|
int sih = eh >> 15;
|
||||||
|
int wdh = (sih == 0) ? eh : ((32767 - eh) & 32767);
|
||||||
|
int hduh = (564 << 3) * encHigh_.det;
|
||||||
|
int mih = (wdh >= (hduh >> 15)) ? 2 : 1;
|
||||||
|
int ihigh = (sih == -1) ? kIhn[mih] : kIhp[mih];
|
||||||
|
int dhigh = g722Block2h(ihigh, encHigh_.det);
|
||||||
|
encHigh_.det = g722Block3h(encHigh_, ihigh);
|
||||||
|
encHigh_.s = g722Block4(encHigh_, dhigh);
|
||||||
|
|
||||||
|
outBuf[outIdx++] = static_cast<uint8_t>(((ihigh & 0x03) << 6) | (ilow & 0x3F));
|
||||||
|
}
|
||||||
|
return outIdx;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t decodeFrame(const uint8_t* inBuf, size_t inLen, int16_t* outPcm16k, size_t maxSamples) {
|
||||||
|
static constexpr int kQm6[64] = {
|
||||||
|
-136, -136, -136, -136,
|
||||||
|
-24808, -21904, -19008, -16704,
|
||||||
|
-14984, -13512, -12280, -11192,
|
||||||
|
-10232, -9360, -8576, -7856,
|
||||||
|
-7192, -6576, -6000, -5456,
|
||||||
|
-4944, -4464, -4008, -3576,
|
||||||
|
-3168, -2776, -2400, -2032,
|
||||||
|
-1688, -1360, -1040, -728,
|
||||||
|
24808, 21904, 19008, 16704,
|
||||||
|
14984, 13512, 12280, 11192,
|
||||||
|
10232, 9360, 8576, 7856,
|
||||||
|
7192, 6576, 6000, 5456,
|
||||||
|
4944, 4464, 4008, 3576,
|
||||||
|
3168, 2776, 2400, 2032,
|
||||||
|
1688, 1360, 1040, 728,
|
||||||
|
432, 136, -432, -136
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t outCount = 0;
|
||||||
|
for (size_t i = 0; i < inLen && outCount + 1 < maxSamples; ++i) {
|
||||||
|
int ilowr = inBuf[i] & 63;
|
||||||
|
int ihigh = (inBuf[i] >> 6) & 3;
|
||||||
|
|
||||||
|
// Low band decoder (block5l + block6l + block2l + block3l + block4l)
|
||||||
|
int dl = (decLow_.det * kQm6[ilowr]) >> 15;
|
||||||
|
int ylow = saturate(decLow_.s + dl, 32767, -32768);
|
||||||
|
int rlow = saturate(ylow, 16383, -16384);
|
||||||
|
int dlowt = g722Block2l(ilowr, decLow_.det);
|
||||||
|
decLow_.det = g722Block3l(decLow_, ilowr);
|
||||||
|
decLow_.s = g722Block4(decLow_, dlowt);
|
||||||
|
|
||||||
|
// High band decoder (block2h + block5h + block3h + block4h)
|
||||||
|
int dhigh = g722Block2h(ihigh, decHigh_.det);
|
||||||
|
int rhigh = saturate(dhigh + decHigh_.s, 16383, -16384);
|
||||||
|
decHigh_.det = g722Block3h(decHigh_, ihigh);
|
||||||
|
decHigh_.s = g722Block4(decHigh_, dhigh);
|
||||||
|
|
||||||
|
// rx_qmf (14-bit sub-band -> 16-bit full-scale PCM via >> 11)
|
||||||
|
std::memmove(&rxXd_[1], rxXd_, 11 * sizeof(rxXd_[0]));
|
||||||
|
std::memmove(&rxXs_[1], rxXs_, 11 * sizeof(rxXs_[0]));
|
||||||
|
rxXd_[0] = saturate(rlow - rhigh, 16383, -16384);
|
||||||
|
rxXs_[0] = saturate(rlow + rhigh, 16383, -16384);
|
||||||
|
|
||||||
|
int xout1 = 0;
|
||||||
|
int xout2 = 0;
|
||||||
|
for (int j = 0; j < 12; ++j) {
|
||||||
|
xout1 += rxXd_[j] * kG722QmfCoeff[2 * j];
|
||||||
|
xout2 += rxXs_[j] * kG722QmfCoeff[2 * j + 1];
|
||||||
|
}
|
||||||
|
outPcm16k[outCount++] = static_cast<int16_t>(saturate(xout1 >> 11, 32767, -32768));
|
||||||
|
outPcm16k[outCount++] = static_cast<int16_t>(saturate(xout2 >> 11, 32767, -32768));
|
||||||
|
}
|
||||||
|
return outCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
G722BandState encLow_;
|
||||||
|
G722BandState encHigh_;
|
||||||
|
int txX_[24] {};
|
||||||
|
|
||||||
|
G722BandState decLow_;
|
||||||
|
G722BandState decHigh_;
|
||||||
|
int rxXd_[12] {};
|
||||||
|
int rxXs_[12] {};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace pjsip
|
||||||
|
} // namespace bananarchy
|
||||||
|
|
||||||
|
#endif // BANANARCHY_PJSIP_MEDIA_H
|
||||||
+182
-352
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
#include "../sxmo-dwm/dwm_plugin.h"
|
#include "../sxmo-dwm/dwm_plugin.h"
|
||||||
#include "net_tls.h"
|
#include "net_tls.h"
|
||||||
|
#include "pjsip_media.h"
|
||||||
|
|
||||||
#include <openssl/aes.h>
|
#include <openssl/aes.h>
|
||||||
#include <openssl/bn.h>
|
#include <openssl/bn.h>
|
||||||
@@ -26,7 +27,10 @@
|
|||||||
#include <gsm.h>
|
#include <gsm.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <sys/timerfd.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <deque>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -148,63 +152,21 @@ std::string cyclePreferredCodec(const std::string& cur) {
|
|||||||
return "AUTO";
|
return "AUTO";
|
||||||
}
|
}
|
||||||
|
|
||||||
// G.711 u-law and A-law codecs (ITU-T G.711)
|
// G.711 u-law and A-law codecs (via PJSIP / pjmedia alaw_ulaw)
|
||||||
uint8_t linearToUlaw(int16_t pcmVal) {
|
uint8_t linearToUlaw(int16_t pcmVal) {
|
||||||
constexpr int kBias = 0x84;
|
return pjsip::linear2ulaw(pcmVal);
|
||||||
constexpr int kClip = 32635;
|
|
||||||
int pcm = pcmVal;
|
|
||||||
int mask = (pcm < 0) ? 0x7f : 0xff;
|
|
||||||
if (pcm < 0) pcm = -pcm;
|
|
||||||
if (pcm > kClip) pcm = kClip;
|
|
||||||
pcm += kBias;
|
|
||||||
int seg = 0;
|
|
||||||
for (int thr = 0xFF; seg < 7 && pcm > thr; ++seg, thr = (thr << 1) | 1) {
|
|
||||||
}
|
|
||||||
uint8_t uval = static_cast<uint8_t>((seg << 4) | ((pcm >> (seg + 3)) & 0x0f));
|
|
||||||
return static_cast<uint8_t>(uval ^ mask);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ulawToLinear(uint8_t uval) {
|
int16_t ulawToLinear(uint8_t uval) {
|
||||||
uval = static_cast<uint8_t>(~uval);
|
return pjsip::ulaw2linear(uval);
|
||||||
int t = (((uval & 0x0f) << 3) + 0x84) << ((uval & 0x70) >> 4);
|
|
||||||
return static_cast<int16_t>((uval & 0x80) ? (0x84 - t) : (t - 0x84));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t linearToAlaw(int16_t pcmVal) {
|
uint8_t linearToAlaw(int16_t pcmVal) {
|
||||||
int pcm = pcmVal;
|
return pjsip::linear2alaw(pcmVal);
|
||||||
int mask = 0x55;
|
|
||||||
if (pcm >= 0) {
|
|
||||||
mask |= 0x80;
|
|
||||||
} else {
|
|
||||||
pcm = -pcm - 1;
|
|
||||||
}
|
|
||||||
if (pcm > 32767) pcm = 32767;
|
|
||||||
int seg = 0;
|
|
||||||
for (int thr = 0xFF; seg < 7 && pcm > thr; ++seg, thr = (thr << 1) | 1) {
|
|
||||||
}
|
|
||||||
uint8_t aval = (seg == 0)
|
|
||||||
? static_cast<uint8_t>((pcm >> 4) & 0x0f)
|
|
||||||
: static_cast<uint8_t>((seg << 4) | ((pcm >> (seg + 3)) & 0x0f));
|
|
||||||
return static_cast<uint8_t>(aval ^ mask);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t alawToLinear(uint8_t aval) {
|
int16_t alawToLinear(uint8_t aval) {
|
||||||
aval ^= 0x55;
|
return pjsip::alaw2linear(aval);
|
||||||
int t = (aval & 0x0f) << 4;
|
|
||||||
int seg = (static_cast<unsigned>(aval) & 0x70) >> 4;
|
|
||||||
switch (seg) {
|
|
||||||
case 0:
|
|
||||||
t += 8;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
t += 0x108;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
t += 0x108;
|
|
||||||
t <<= seg - 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return static_cast<int16_t>((aval & 0x80) ? t : -t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -381,229 +343,10 @@ struct G726_32State {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// ITU-T G.722 Wideband 16 kHz (64 kbps SB-ADPCM) Codec (RFC 3551 PT 9)
|
// ITU-T G.722 Wideband 16 kHz (64 kbps SB-ADPCM) Codec (via PJSIP / pjmedia)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
class G722State {
|
using G722State = pjsip::G722Codec;
|
||||||
public:
|
|
||||||
G722State() { reset(); }
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
band_[0] = Band{};
|
|
||||||
band_[1] = Band{};
|
|
||||||
band_[0].det = 32;
|
|
||||||
band_[1].det = 8;
|
|
||||||
std::memset(txQmf_, 0, sizeof(txQmf_));
|
|
||||||
std::memset(rxQmf_, 0, sizeof(rxQmf_));
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t encodeFrame(const int16_t* pcm16k, size_t numSamples, uint8_t* outBuf) {
|
|
||||||
size_t outIdx = 0;
|
|
||||||
for (size_t i = 0; i + 1 < numSamples; i += 2) {
|
|
||||||
// 24-tap QMF analysis filter
|
|
||||||
for (int j = 23; j >= 2; --j) {
|
|
||||||
txQmf_[j] = txQmf_[j - 2];
|
|
||||||
}
|
|
||||||
txQmf_[1] = pcm16k[i];
|
|
||||||
txQmf_[0] = pcm16k[i + 1];
|
|
||||||
|
|
||||||
int sumEven = 0;
|
|
||||||
int sumOdd = 0;
|
|
||||||
for (int j = 0; j < 12; ++j) {
|
|
||||||
sumEven += txQmf_[2 * j] * kQmfCoeffs[j];
|
|
||||||
sumOdd += txQmf_[2 * j + 1] * kQmfCoeffs[11 - j];
|
|
||||||
}
|
|
||||||
int xlow = std::clamp((sumEven + sumOdd) >> 14, -16384, 16383);
|
|
||||||
int xhigh = std::clamp((sumEven - sumOdd) >> 14, -16384, 16383);
|
|
||||||
|
|
||||||
// Lower sub-band (6-bit quantizer)
|
|
||||||
int el = std::clamp(xlow - band_[0].s, -16384, 16383);
|
|
||||||
int wd = (el >= 0) ? el : -(el + 1);
|
|
||||||
int mila = 0;
|
|
||||||
for (int m = 1; m < 30; ++m) {
|
|
||||||
if (wd < ((kQ6[m] * band_[0].det) >> 12)) break;
|
|
||||||
mila = m;
|
|
||||||
}
|
|
||||||
int il = (el < 0) ? kMisn[mila] : kMisp[mila];
|
|
||||||
int ril = il >> 2;
|
|
||||||
int dlt = (band_[0].det * kQ6m4[ril]) >> 15;
|
|
||||||
updateBand(0, dlt, ril, true);
|
|
||||||
|
|
||||||
// Higher sub-band (2-bit quantizer)
|
|
||||||
int eh = std::clamp(xhigh - band_[1].s, -16384, 16383);
|
|
||||||
int wdh = (eh >= 0) ? eh : -(eh + 1);
|
|
||||||
int mih = (wdh >= ((564 * band_[1].det) >> 12)) ? 2 : 1;
|
|
||||||
int ih = (eh < 0) ? ((mih == 1) ? 1 : 0) : ((mih == 1) ? 3 : 2);
|
|
||||||
int dhy = (band_[1].det * kQm2[ih]) >> 15;
|
|
||||||
updateBand(1, dhy, ih, false);
|
|
||||||
|
|
||||||
outBuf[outIdx++] = static_cast<uint8_t>(((ih & 0x03) << 6) | (il & 0x3f));
|
|
||||||
}
|
|
||||||
return outIdx;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t decodeFrame(const uint8_t* inBuf, size_t inLen, int16_t* outPcm16k, size_t maxSamples) {
|
|
||||||
size_t outCount = 0;
|
|
||||||
for (size_t i = 0; i < inLen && outCount + 1 < maxSamples; ++i) {
|
|
||||||
uint8_t code = inBuf[i];
|
|
||||||
int il = code & 0x3f;
|
|
||||||
int ih = (code >> 6) & 0x03;
|
|
||||||
|
|
||||||
// Lower sub-band reconstruction (6-bit)
|
|
||||||
int ril = il >> 2;
|
|
||||||
int dlt = (band_[0].det * kQ6b[il]) >> 15;
|
|
||||||
int rlow = std::clamp(band_[0].s + dlt, -16384, 16383);
|
|
||||||
updateBand(0, (band_[0].det * kQ6m4[ril]) >> 15, ril, true);
|
|
||||||
|
|
||||||
// Higher sub-band reconstruction (2-bit)
|
|
||||||
int dhy = (band_[1].det * kQm2[ih]) >> 15;
|
|
||||||
int rhigh = std::clamp(band_[1].s + dhy, -16384, 16383);
|
|
||||||
updateBand(1, dhy, ih, false);
|
|
||||||
|
|
||||||
// 24-tap QMF synthesis filter
|
|
||||||
for (int j = 23; j >= 2; --j) {
|
|
||||||
rxQmf_[j] = rxQmf_[j - 2];
|
|
||||||
}
|
|
||||||
rxQmf_[1] = rlow - rhigh;
|
|
||||||
rxQmf_[0] = rlow + rhigh;
|
|
||||||
|
|
||||||
int sumA = 0;
|
|
||||||
int sumB = 0;
|
|
||||||
for (int j = 0; j < 12; ++j) {
|
|
||||||
sumA += rxQmf_[2 * j] * kQmfCoeffs[j];
|
|
||||||
sumB += rxQmf_[2 * j + 1] * kQmfCoeffs[11 - j];
|
|
||||||
}
|
|
||||||
outPcm16k[outCount++] = static_cast<int16_t>(std::clamp(sumB >> 13, -32768, 32767));
|
|
||||||
outPcm16k[outCount++] = static_cast<int16_t>(std::clamp(sumA >> 13, -32768, 32767));
|
|
||||||
}
|
|
||||||
return outCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct Band {
|
|
||||||
int s = 0;
|
|
||||||
int sp = 0;
|
|
||||||
int sz = 0;
|
|
||||||
int r[3] {};
|
|
||||||
int a[3] {};
|
|
||||||
int ap[3] {};
|
|
||||||
int p[3] {};
|
|
||||||
int d[7] {};
|
|
||||||
int b[7] {};
|
|
||||||
int bp[7] {};
|
|
||||||
int sg[7] {};
|
|
||||||
int nb = 0;
|
|
||||||
int det = 32;
|
|
||||||
};
|
|
||||||
|
|
||||||
void updateBand(int bIdx, int dlt, int idx, bool isLow) {
|
|
||||||
Band& b = band_[bIdx];
|
|
||||||
b.d[0] = dlt;
|
|
||||||
b.r[0] = std::clamp(b.s + dlt, -16384, 16383);
|
|
||||||
b.p[0] = std::clamp(b.sz + dlt, -16384, 16383);
|
|
||||||
|
|
||||||
// Adaptive zero section
|
|
||||||
for (int i = 1; i <= 6; ++i) {
|
|
||||||
int wd1 = (dlt == 0) ? 0 : (((dlt ^ b.d[i]) >= 0) ? 128 : -128);
|
|
||||||
int wd2 = (b.b[i] * 32640) >> 15;
|
|
||||||
b.bp[i] = std::clamp(wd1 + wd2, -32768, 32767);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adaptive pole section
|
|
||||||
b.sg[0] = (b.p[0] >= 0) ? 0 : 1;
|
|
||||||
b.sg[1] = (b.p[1] >= 0) ? 0 : 1;
|
|
||||||
b.sg[2] = (b.p[2] >= 0) ? 0 : 1;
|
|
||||||
|
|
||||||
int wd1 = std::clamp(b.a[1], -8191, 8191);
|
|
||||||
int wd2 = (b.sg[0] == b.sg[1]) ? -wd1 : wd1;
|
|
||||||
if (wd2 > 32767) wd2 = 32767;
|
|
||||||
int wd3 = (b.sg[0] == b.sg[2]) ? 128 : -128;
|
|
||||||
wd3 += (wd2 >> 7);
|
|
||||||
wd3 += (b.a[2] * 32512) >> 15;
|
|
||||||
b.ap[2] = std::clamp(wd3, -12288, 12288);
|
|
||||||
|
|
||||||
int wd4 = (b.p[0] == 0) ? 0 : ((b.sg[0] == b.sg[1]) ? 192 : -192);
|
|
||||||
int wd5 = (b.a[1] * 32640) >> 15;
|
|
||||||
int limit = 15360 - b.ap[2];
|
|
||||||
b.ap[1] = std::clamp(wd4 + wd5, -limit, limit);
|
|
||||||
|
|
||||||
// Log scale factor adaptation
|
|
||||||
int wl = isLow ? kWl[idx] : kWh[idx];
|
|
||||||
b.nb = std::clamp(((b.nb * 127) >> 7) + wl, 0, isLow ? 18432 : 22528);
|
|
||||||
int shift = (b.nb >> 11) + (isLow ? 0 : 2);
|
|
||||||
int ilb = kIlb[(b.nb & 0x7ff) >> 6];
|
|
||||||
b.det = std::max(1, (shift >= 9) ? (ilb << (shift - 9)) : (ilb >> (9 - shift)));
|
|
||||||
|
|
||||||
// Shift delay lines and compute new predictor outputs
|
|
||||||
for (int i = 6; i >= 1; --i) {
|
|
||||||
b.d[i] = b.d[i - 1];
|
|
||||||
b.b[i] = b.bp[i];
|
|
||||||
}
|
|
||||||
b.r[2] = b.r[1];
|
|
||||||
b.r[1] = b.r[0];
|
|
||||||
b.p[2] = b.p[1];
|
|
||||||
b.p[1] = b.p[0];
|
|
||||||
b.a[2] = b.ap[2];
|
|
||||||
b.a[1] = b.ap[1];
|
|
||||||
|
|
||||||
b.sz = 0;
|
|
||||||
for (int i = 1; i <= 6; ++i) {
|
|
||||||
b.sz += (b.b[i] * b.d[i]) >> 14;
|
|
||||||
}
|
|
||||||
b.sz = std::clamp(b.sz, -16384, 16383);
|
|
||||||
b.sp = std::clamp(((b.a[1] * b.r[1]) + (b.a[2] * b.r[2])) >> 14, -16384, 16383);
|
|
||||||
b.s = std::clamp(b.sp + b.sz, -16384, 16383);
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr int kQmfCoeffs[12] = {
|
|
||||||
3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11
|
|
||||||
};
|
|
||||||
static constexpr int kQ6[30] = {
|
|
||||||
0, 35, 72, 110, 150, 190, 233, 276, 323, 370,
|
|
||||||
422, 473, 530, 587, 650, 714, 786, 858, 940, 1023,
|
|
||||||
1121, 1219, 1339, 1458, 1612, 1765, 1980, 2195, 2557, 2919
|
|
||||||
};
|
|
||||||
static constexpr int kMisn[30] = {
|
|
||||||
63, 62, 31, 30, 29, 28, 27, 26, 25, 24,
|
|
||||||
23, 22, 21, 20, 19, 18, 17, 16, 15, 14,
|
|
||||||
13, 12, 11, 10, 9, 8, 7, 6, 5, 4
|
|
||||||
};
|
|
||||||
static constexpr int kMisp[30] = {
|
|
||||||
61, 60, 59, 58, 57, 56, 55, 54, 53, 52,
|
|
||||||
51, 50, 49, 48, 47, 46, 45, 44, 43, 42,
|
|
||||||
41, 40, 39, 38, 37, 36, 35, 34, 33, 32
|
|
||||||
};
|
|
||||||
static constexpr int kQ6m4[16] = {
|
|
||||||
0, -20456, -12896, -8968, -6288, -4240, -2584, -1200,
|
|
||||||
20456, 12896, 8968, 6288, 4240, 2584, 1200, 0
|
|
||||||
};
|
|
||||||
static constexpr int kQ6b[64] = {
|
|
||||||
-136, -136, -136, -136, -24808, -21904, -19008, -16704,
|
|
||||||
-14984, -13512, -12280, -11192, -10232, -9360, -8576, -7856,
|
|
||||||
-7192, -6576, -6000, -5456, -4944, -4464, -4008, -3576,
|
|
||||||
-3168, -2776, -2400, -2032, -1688, -1360, -1040, -728,
|
|
||||||
24808, 21904, 19008, 16704, 14984, 13512, 12280, 11192,
|
|
||||||
10232, 9360, 8576, 7856, 7192, 6576, 6000, 5456,
|
|
||||||
4944, 4464, 4008, 3576, 3168, 2776, 2400, 2032,
|
|
||||||
1688, 1360, 1040, 728, 432, 136, -432, -136
|
|
||||||
};
|
|
||||||
static constexpr int kQm2[4] = { -7408, -1616, 7408, 1616 };
|
|
||||||
static constexpr int kWl[16] = {
|
|
||||||
-60, 3042, 1198, 538, 334, 172, 58, -30,
|
|
||||||
3042, 1198, 538, 334, 172, 58, -30, -60
|
|
||||||
};
|
|
||||||
static constexpr int kWh[4] = { 798, -214, 798, -214 };
|
|
||||||
static constexpr int kIlb[32] = {
|
|
||||||
2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383,
|
|
||||||
2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834,
|
|
||||||
2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371,
|
|
||||||
3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008
|
|
||||||
};
|
|
||||||
|
|
||||||
Band band_[2];
|
|
||||||
int txQmf_[24] {};
|
|
||||||
int rxQmf_[24] {};
|
|
||||||
};
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// RFC 6189 ZRTP & RFC 3711 SRTP Cryptographic Helpers
|
// RFC 6189 ZRTP & RFC 3711 SRTP Cryptographic Helpers
|
||||||
@@ -1404,11 +1147,11 @@ bool codecMatchesPreference(SipCodecId id, const std::string& pref) {
|
|||||||
|
|
||||||
std::vector<SipCodecId> orderedCodecsForPreference(const std::string& pref) {
|
std::vector<SipCodecId> orderedCodecsForPreference(const std::string& pref) {
|
||||||
static const SipCodecId kDefaultOrder[] = {
|
static const SipCodecId kDefaultOrder[] = {
|
||||||
|
SipCodecId::G722,
|
||||||
|
SipCodecId::Opus,
|
||||||
SipCodecId::Pcmu,
|
SipCodecId::Pcmu,
|
||||||
SipCodecId::Pcma,
|
SipCodecId::Pcma,
|
||||||
SipCodecId::Gsm,
|
SipCodecId::Gsm,
|
||||||
SipCodecId::Opus,
|
|
||||||
SipCodecId::G722,
|
|
||||||
SipCodecId::G726_32,
|
SipCodecId::G726_32,
|
||||||
SipCodecId::Dvi4_8k,
|
SipCodecId::Dvi4_8k,
|
||||||
SipCodecId::Dvi4_16k,
|
SipCodecId::Dvi4_16k,
|
||||||
@@ -1582,20 +1325,9 @@ void parseSdpMedia(const std::string& msg, const std::string& preferredCodec,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. In AUTO mode, prefer our ordered priority list (G.711 PCMU/PCMA first)
|
// 2. Auto-negotiate per RFC 3264: pick the first mutually supported codec in
|
||||||
// among all codecs offered by the peer.
|
// the remote peer's m=audio line (offeredPts[0] is the codec the remote
|
||||||
for (SipCodecId prefId : orderedCodecsForPreference("AUTO")) {
|
// peer selected and will transmit).
|
||||||
for (int pt : offeredPts) {
|
|
||||||
auto it = outPtMap.find(pt);
|
|
||||||
if (it != outPtMap.end() && it->second == prefId) {
|
|
||||||
outCodec = it->second;
|
|
||||||
outPayloadType = pt;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Otherwise pick the first mutually supported codec in peer's m=audio line
|
|
||||||
for (int pt : offeredPts) {
|
for (int pt : offeredPts) {
|
||||||
auto it = outPtMap.find(pt);
|
auto it = outPtMap.find(pt);
|
||||||
if (it != outPtMap.end()) {
|
if (it != outPtMap.end()) {
|
||||||
@@ -1640,8 +1372,9 @@ class RtpAudioSession {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (running_.load() && sock_ >= 0 && remoteIp == activeRemoteIp_ &&
|
if (running_.load() && sock_ >= 0 && remoteIp == activeRemoteIp_ &&
|
||||||
remotePort == activeRemotePort_ && codec == codec_ &&
|
remotePort == activeRemotePort_) {
|
||||||
payloadType == payloadType_) {
|
codec_.store(codec);
|
||||||
|
payloadType_.store(payloadType);
|
||||||
dtmfPt_ = (dtmfPt > 0 && dtmfPt <= 127) ? dtmfPt : 101;
|
dtmfPt_ = (dtmfPt > 0 && dtmfPt <= 127) ? dtmfPt : 101;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1694,8 +1427,9 @@ class RtpAudioSession {
|
|||||||
remAddr_ = remAddr;
|
remAddr_ = remAddr;
|
||||||
activeRemoteIp_ = remoteIp;
|
activeRemoteIp_ = remoteIp;
|
||||||
activeRemotePort_ = remotePort;
|
activeRemotePort_ = remotePort;
|
||||||
codec_ = codec;
|
codec_.store(codec);
|
||||||
payloadType_ = payloadType;
|
payloadType_.store(payloadType);
|
||||||
|
codecChanged_.store(false);
|
||||||
dtmfPt_ = (dtmfPt > 0 && dtmfPt <= 127) ? dtmfPt : 101;
|
dtmfPt_ = (dtmfPt > 0 && dtmfPt <= 127) ? dtmfPt : 101;
|
||||||
rxPtMap_ = ptMap;
|
rxPtMap_ = ptMap;
|
||||||
rxPtMap_[payloadType] = codec;
|
rxPtMap_[payloadType] = codec;
|
||||||
@@ -1731,8 +1465,14 @@ class RtpAudioSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isZrtpSecure() const { return zrtpSecure_.load(); }
|
bool isZrtpSecure() const { return zrtpSecure_.load(); }
|
||||||
SipCodecId activeCodec() const { return codec_; }
|
SipCodecId activeCodec() const { return codec_.load(); }
|
||||||
const char* activeCodecName() const { return codecShortName(codec_); }
|
const char* activeCodecName() const { return codecShortName(codec_.load()); }
|
||||||
|
|
||||||
|
bool consumeCodecNotification(SipCodecId& outCodec) {
|
||||||
|
if (!codecChanged_.exchange(false)) return false;
|
||||||
|
outCodec = codec_.load();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool consumeZrtpNotification(std::string& outSas) {
|
bool consumeZrtpNotification(std::string& outSas) {
|
||||||
if (!zrtpSecure_.load() || zrtpNotified_.load()) return false;
|
if (!zrtpSecure_.load() || zrtpNotified_.load()) return false;
|
||||||
@@ -2350,10 +2090,13 @@ class RtpAudioSession {
|
|||||||
void runLoop() {
|
void runLoop() {
|
||||||
::system("amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
|
::system("amixer -c 0 cset \"name='PRI_MI2S_RX Audio Mixer MultiMedia1'\" 1 >/dev/null 2>&1; "
|
||||||
"amixer -c 0 cset \"name='MultiMedia2 Mixer TERT_MI2S_TX'\" 1 >/dev/null 2>&1");
|
"amixer -c 0 cset \"name='MultiMedia2 Mixer TERT_MI2S_TX'\" 1 >/dev/null 2>&1");
|
||||||
int audioRate = codecAudioSampleRate(codec_);
|
|
||||||
size_t frameSamples = static_cast<size_t>(audioRate / 50); // 160 @ 8k, 320 @ 16k
|
// Always run ALSA/PipeWire capture & playback at 16000 Hz so wideband
|
||||||
size_t frameBytes = frameSamples * sizeof(int16_t);
|
// (G.722, Opus, DVI4-16k, L16-16k) and narrowband (G.711 PCMU/PCMA, GSM,
|
||||||
std::string rateStr = std::to_string(audioRate);
|
// G.726-32, DVI4-8k, L16-8k) codecs can switch dynamically without
|
||||||
|
// reopening audio pipes.
|
||||||
|
constexpr size_t kFrameSamples16k = 320; // 20 ms @ 16000 Hz
|
||||||
|
constexpr size_t kFrameBytes16k = kFrameSamples16k * sizeof(int16_t);
|
||||||
|
|
||||||
int capPipe[2] = {-1, -1};
|
int capPipe[2] = {-1, -1};
|
||||||
int capFd = -1;
|
int capFd = -1;
|
||||||
@@ -2371,7 +2114,7 @@ class RtpAudioSession {
|
|||||||
if (nullFd > 2) ::close(nullFd);
|
if (nullFd > 2) ::close(nullFd);
|
||||||
}
|
}
|
||||||
::execlp("arecord", "arecord", "-q", "--buffer-time=160000",
|
::execlp("arecord", "arecord", "-q", "--buffer-time=160000",
|
||||||
"--period-time=20000", "-f", "S16_LE", "-r", rateStr.c_str(),
|
"--period-time=20000", "-f", "S16_LE", "-r", "16000",
|
||||||
"-c", "1", "-t", "raw", static_cast<char*>(nullptr));
|
"-c", "1", "-t", "raw", static_cast<char*>(nullptr));
|
||||||
::_exit(1);
|
::_exit(1);
|
||||||
} else if (capPid > 0) {
|
} else if (capPid > 0) {
|
||||||
@@ -2401,8 +2144,9 @@ class RtpAudioSession {
|
|||||||
::dup2(nullFd, STDERR_FILENO);
|
::dup2(nullFd, STDERR_FILENO);
|
||||||
if (nullFd > 2) ::close(nullFd);
|
if (nullFd > 2) ::close(nullFd);
|
||||||
}
|
}
|
||||||
::execlp("aplay", "aplay", "-q", "--buffer-time=200000",
|
::execlp("aplay", "aplay", "-q", "--buffer-time=160000",
|
||||||
"--period-time=20000", "-f", "S16_LE", "-r", rateStr.c_str(),
|
"--period-time=20000", "--start-delay=60000",
|
||||||
|
"-f", "S16_LE", "-r", "16000",
|
||||||
"-c", "1", "-t", "raw", static_cast<char*>(nullptr));
|
"-c", "1", "-t", "raw", static_cast<char*>(nullptr));
|
||||||
::_exit(1);
|
::_exit(1);
|
||||||
} else if (playPid > 0) {
|
} else if (playPid > 0) {
|
||||||
@@ -2417,6 +2161,14 @@ class RtpAudioSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int timerFd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
|
||||||
|
if (timerFd >= 0) {
|
||||||
|
struct itimerspec its {};
|
||||||
|
its.it_value.tv_nsec = 20000000;
|
||||||
|
its.it_interval.tv_nsec = 20000000;
|
||||||
|
::timerfd_settime(timerFd, 0, &its, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
int opusErr = OPUS_OK;
|
int opusErr = OPUS_OK;
|
||||||
OpusEncoder* opusEnc = opus_encoder_create(16000, 1, OPUS_APPLICATION_VOIP, &opusErr);
|
OpusEncoder* opusEnc = opus_encoder_create(16000, 1, OPUS_APPLICATION_VOIP, &opusErr);
|
||||||
if (opusEnc) {
|
if (opusEnc) {
|
||||||
@@ -2440,16 +2192,29 @@ class RtpAudioSession {
|
|||||||
uint32_t ts = static_cast<uint32_t>(nowMs() * 8);
|
uint32_t ts = static_cast<uint32_t>(nowMs() * 8);
|
||||||
uint32_t ssrc = 0x51504241u ^ static_cast<uint32_t>(::getpid());
|
uint32_t ssrc = 0x51504241u ^ static_cast<uint32_t>(::getpid());
|
||||||
int64_t nextTxMs = nowMs() + 20;
|
int64_t nextTxMs = nowMs() + 20;
|
||||||
bool playPrimed = false;
|
|
||||||
uint8_t capRing[2560];
|
uint8_t capRing[3840];
|
||||||
size_t capAvail = 0;
|
size_t capAvail = 0;
|
||||||
|
|
||||||
|
bool playPrimed = false;
|
||||||
|
int64_t lastRxAudioMs = 0;
|
||||||
|
int16_t lastRx8k = 0;
|
||||||
|
bool hasLastRx8k = false;
|
||||||
|
|
||||||
while (running_.load()) {
|
while (running_.load()) {
|
||||||
// Drain available microphone capture bytes into capRing without blocking
|
// Drain available microphone capture bytes into capRing without blocking
|
||||||
if (capFd >= 0 && capAvail < sizeof(capRing)) {
|
if (capFd >= 0 && capAvail < sizeof(capRing)) {
|
||||||
ssize_t r = ::read(capFd, capRing + capAvail, sizeof(capRing) - capAvail);
|
ssize_t r = ::read(capFd, capRing + capAvail, sizeof(capRing) - capAvail);
|
||||||
if (r > 0) {
|
if (r > 0) {
|
||||||
capAvail += static_cast<size_t>(r);
|
capAvail += static_cast<size_t>(r);
|
||||||
|
// Cap capture latency at 120 ms (1920 samples = 3840 bytes)
|
||||||
|
if (capAvail > 2560) {
|
||||||
|
size_t drop = ((capAvail - 1280) / kFrameBytes16k) * kFrameBytes16k;
|
||||||
|
if (drop > 0 && drop <= capAvail) {
|
||||||
|
capAvail -= drop;
|
||||||
|
std::memmove(capRing, capRing + drop, capAvail);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2475,12 +2240,20 @@ class RtpAudioSession {
|
|||||||
if (!unprotectRtpPacket(rxPkt, pktLen, hdrLen)) {
|
if (!unprotectRtpPacket(rxPkt, pktLen, hdrLen)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (pt == dtmfPt_) continue;
|
if (pt == dtmfPt_ || pt == 13 || pt == 19) continue;
|
||||||
|
|
||||||
auto itCodec = rxPtMap_.find(pt);
|
auto itCodec = rxPtMap_.find(pt);
|
||||||
if (itCodec == rxPtMap_.end()) continue;
|
if (itCodec == rxPtMap_.end()) continue;
|
||||||
SipCodecId rxCodec = itCodec->second;
|
SipCodecId rxCodec = itCodec->second;
|
||||||
|
|
||||||
|
// Auto-negotiate / adapt TX codec if peer transmits a different
|
||||||
|
// supported audio payload type than initially assumed.
|
||||||
|
if (rxCodec != codec_.load() || pt != payloadType_.load()) {
|
||||||
|
codec_.store(rxCodec);
|
||||||
|
payloadType_.store(pt);
|
||||||
|
codecChanged_.store(true);
|
||||||
|
}
|
||||||
|
|
||||||
const uint8_t* payload = rxPkt + hdrLen;
|
const uint8_t* payload = rxPkt + hdrLen;
|
||||||
size_t payLen = pktLen - hdrLen;
|
size_t payLen = pktLen - hdrLen;
|
||||||
if (playFd < 0 || payLen == 0) continue;
|
if (playFd < 0 || payLen == 0) continue;
|
||||||
@@ -2540,45 +2313,59 @@ class RtpAudioSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (decodedSamples > 0) {
|
if (decodedSamples > 0) {
|
||||||
if (!playPrimed) {
|
int64_t rxNow = nowMs();
|
||||||
|
if (!playPrimed || (rxNow - lastRxAudioMs > 220)) {
|
||||||
|
int16_t prefill[640] = {0};
|
||||||
|
(void)::write(playFd, prefill, sizeof(prefill));
|
||||||
playPrimed = true;
|
playPrimed = true;
|
||||||
int16_t silence[960] = {0};
|
|
||||||
(void)::write(playFd, silence, frameSamples * 3 * sizeof(int16_t));
|
|
||||||
}
|
}
|
||||||
|
lastRxAudioMs = rxNow;
|
||||||
|
|
||||||
int rxRate = codecAudioSampleRate(rxCodec);
|
int rxRate = codecAudioSampleRate(rxCodec);
|
||||||
if (rxRate == audioRate) {
|
if (rxRate == 8000) {
|
||||||
|
int16_t pcm16kOut[3840];
|
||||||
|
size_t outCount = 0;
|
||||||
|
for (size_t i = 0; i < decodedSamples && outCount + 1 < 3840; ++i) {
|
||||||
|
int16_t s = pcmDecoded[i];
|
||||||
|
int16_t prev = hasLastRx8k ? lastRx8k : s;
|
||||||
|
pcm16kOut[outCount++] =
|
||||||
|
static_cast<int16_t>((static_cast<int>(prev) + s) / 2);
|
||||||
|
pcm16kOut[outCount++] = s;
|
||||||
|
lastRx8k = s;
|
||||||
|
hasLastRx8k = true;
|
||||||
|
}
|
||||||
|
(void)::write(playFd, pcm16kOut, outCount * sizeof(int16_t));
|
||||||
|
} else {
|
||||||
(void)::write(playFd, pcmDecoded, decodedSamples * sizeof(int16_t));
|
(void)::write(playFd, pcmDecoded, decodedSamples * sizeof(int16_t));
|
||||||
} else if (rxRate == 8000 && audioRate == 16000) {
|
|
||||||
int16_t pcmUp[1920];
|
|
||||||
size_t upCount = 0;
|
|
||||||
for (size_t i = 0; i < decodedSamples && upCount + 1 < 1920; ++i) {
|
|
||||||
int16_t s0 = pcmDecoded[i];
|
|
||||||
int16_t s1 = (i + 1 < decodedSamples) ? pcmDecoded[i + 1] : s0;
|
|
||||||
pcmUp[upCount++] = s0;
|
|
||||||
pcmUp[upCount++] = static_cast<int16_t>((static_cast<int>(s0) + s1) / 2);
|
|
||||||
}
|
|
||||||
(void)::write(playFd, pcmUp, upCount * sizeof(int16_t));
|
|
||||||
} else if (rxRate == 16000 && audioRate == 8000) {
|
|
||||||
int16_t pcmDown[960];
|
|
||||||
size_t downCount = 0;
|
|
||||||
for (size_t i = 0; i + 1 < decodedSamples && downCount < 960; i += 2) {
|
|
||||||
pcmDown[downCount++] = static_cast<int16_t>(
|
|
||||||
(static_cast<int>(pcmDecoded[i]) + pcmDecoded[i + 1]) / 2);
|
|
||||||
}
|
|
||||||
(void)::write(playFd, pcmDown, downCount * sizeof(int16_t));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Transmit 20ms audio frame or RFC 2833 DTMF event + ZRTP tick
|
// 2. Process 20 ms transmit ticks
|
||||||
int64_t now = nowMs();
|
uint64_t ticks = 0;
|
||||||
periodicZrtpTick(ssrc, now);
|
if (timerFd >= 0) {
|
||||||
|
uint64_t exp = 0;
|
||||||
if (now >= nextTxMs) {
|
if (::read(timerFd, &exp, sizeof(exp)) == sizeof(exp) && exp > 0) {
|
||||||
nextTxMs += 20;
|
ticks = std::min<uint64_t>(exp, 3);
|
||||||
if (now - nextTxMs > 60) {
|
|
||||||
nextTxMs = now + 20;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
int64_t now = nowMs();
|
||||||
|
if (now >= nextTxMs) {
|
||||||
|
ticks = 1;
|
||||||
|
nextTxMs += 20;
|
||||||
|
if (now - nextTxMs > 60) nextTxMs = now + 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticks > 0) {
|
||||||
|
int64_t now = nowMs();
|
||||||
|
periodicZrtpTick(ssrc, now);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint64_t t = 0; t < ticks; ++t) {
|
||||||
|
// Transmit 20 ms RTP audio frame or RFC 2833 DTMF event
|
||||||
|
SipCodecId txCodec = codec_.load();
|
||||||
|
int txPt = payloadType_.load();
|
||||||
char dtmfCh = '\0';
|
char dtmfCh = '\0';
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lk(dtmfMtx_);
|
std::lock_guard<std::mutex> lk(dtmfMtx_);
|
||||||
@@ -2612,20 +2399,33 @@ class RtpAudioSession {
|
|||||||
reinterpret_cast<const struct sockaddr*>(&remAddr_),
|
reinterpret_cast<const struct sockaddr*>(&remAddr_),
|
||||||
sizeof(remAddr_));
|
sizeof(remAddr_));
|
||||||
}
|
}
|
||||||
ts += codecRtpTimestampStep20ms(codec_) * 3;
|
ts += codecRtpTimestampStep20ms(txCodec) * 3;
|
||||||
} else {
|
} else {
|
||||||
int16_t pcmIn[320] = {0};
|
int16_t pcm16k[kFrameSamples16k] = {0};
|
||||||
if (capAvail >= frameBytes) {
|
if (capAvail >= kFrameBytes16k) {
|
||||||
std::memcpy(pcmIn, capRing, frameBytes);
|
std::memcpy(pcm16k, capRing, kFrameBytes16k);
|
||||||
capAvail -= frameBytes;
|
capAvail -= kFrameBytes16k;
|
||||||
if (capAvail > 0) {
|
if (capAvail > 0) {
|
||||||
std::memmove(capRing, capRing + frameBytes, capAvail);
|
std::memmove(capRing, capRing + kFrameBytes16k, capAvail);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int16_t pcm8k[160];
|
||||||
|
const int16_t* pcmIn = pcm16k;
|
||||||
|
size_t frameSamples = kFrameSamples16k;
|
||||||
|
if (codecAudioSampleRate(txCodec) == 8000) {
|
||||||
|
for (size_t i = 0; i < 160; ++i) {
|
||||||
|
pcm8k[i] = static_cast<int16_t>(
|
||||||
|
(static_cast<int>(pcm16k[2 * i]) + pcm16k[2 * i + 1]) / 2);
|
||||||
|
}
|
||||||
|
pcmIn = pcm8k;
|
||||||
|
frameSamples = 160;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t pkt[12 + 768 + 16];
|
uint8_t pkt[12 + 768 + 16];
|
||||||
uint16_t curSeq = seq++;
|
uint16_t curSeq = seq++;
|
||||||
pkt[0] = 0x80;
|
pkt[0] = 0x80;
|
||||||
pkt[1] = static_cast<uint8_t>(payloadType_ & 0x7f);
|
pkt[1] = static_cast<uint8_t>(txPt & 0x7f);
|
||||||
uint16_t netSeq = htons(curSeq);
|
uint16_t netSeq = htons(curSeq);
|
||||||
uint32_t netTs = htonl(ts);
|
uint32_t netTs = htonl(ts);
|
||||||
uint32_t netSsrc = htonl(ssrc);
|
uint32_t netSsrc = htonl(ssrc);
|
||||||
@@ -2634,7 +2434,7 @@ class RtpAudioSession {
|
|||||||
std::memcpy(pkt + 8, &netSsrc, 4);
|
std::memcpy(pkt + 8, &netSsrc, 4);
|
||||||
|
|
||||||
size_t encBytes = 0;
|
size_t encBytes = 0;
|
||||||
switch (codec_) {
|
switch (txCodec) {
|
||||||
case SipCodecId::Opus:
|
case SipCodecId::Opus:
|
||||||
if (opusEnc) {
|
if (opusEnc) {
|
||||||
int r = opus_encode(opusEnc, pcmIn, static_cast<int>(frameSamples),
|
int r = opus_encode(opusEnc, pcmIn, static_cast<int>(frameSamples),
|
||||||
@@ -2659,7 +2459,8 @@ class RtpAudioSession {
|
|||||||
break;
|
break;
|
||||||
case SipCodecId::Gsm:
|
case SipCodecId::Gsm:
|
||||||
if (gsmEnc) {
|
if (gsmEnc) {
|
||||||
gsm_encode(gsmEnc, pcmIn, reinterpret_cast<gsm_byte*>(pkt + 12));
|
gsm_encode(gsmEnc, const_cast<gsm_signal*>(pcmIn),
|
||||||
|
reinterpret_cast<gsm_byte*>(pkt + 12));
|
||||||
encBytes = 33;
|
encBytes = 33;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -2688,15 +2489,21 @@ class RtpAudioSession {
|
|||||||
sizeof(remAddr_));
|
sizeof(remAddr_));
|
||||||
++txAudioFrames_;
|
++txAudioFrames_;
|
||||||
}
|
}
|
||||||
ts += codecRtpTimestampStep20ms(codec_);
|
ts += codecRtpTimestampStep20ms(txCodec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct pollfd pfd { sock_, POLLIN, 0 };
|
struct pollfd pfds[3] {};
|
||||||
int waitMs = static_cast<int>(std::max<int64_t>(2, nextTxMs - nowMs()));
|
int nfds = 0;
|
||||||
::poll(&pfd, 1, std::min(waitMs, 20));
|
pfds[nfds++] = { sock_, POLLIN, 0 };
|
||||||
|
if (timerFd >= 0) pfds[nfds++] = { timerFd, POLLIN, 0 };
|
||||||
|
if (capFd >= 0 && capAvail < sizeof(capRing)) {
|
||||||
|
pfds[nfds++] = { capFd, POLLIN, 0 };
|
||||||
|
}
|
||||||
|
::poll(pfds, nfds, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (timerFd >= 0) ::close(timerFd);
|
||||||
if (opusEnc) opus_encoder_destroy(opusEnc);
|
if (opusEnc) opus_encoder_destroy(opusEnc);
|
||||||
if (opusDec) opus_decoder_destroy(opusDec);
|
if (opusDec) opus_decoder_destroy(opusDec);
|
||||||
if (gsmEnc) gsm_destroy(gsmEnc);
|
if (gsmEnc) gsm_destroy(gsmEnc);
|
||||||
@@ -2719,8 +2526,9 @@ class RtpAudioSession {
|
|||||||
struct sockaddr_in remAddr_ {};
|
struct sockaddr_in remAddr_ {};
|
||||||
std::string activeRemoteIp_;
|
std::string activeRemoteIp_;
|
||||||
int activeRemotePort_ = 0;
|
int activeRemotePort_ = 0;
|
||||||
SipCodecId codec_ = SipCodecId::Pcmu;
|
std::atomic<SipCodecId> codec_{SipCodecId::Pcmu};
|
||||||
int payloadType_ = 0;
|
std::atomic<int> payloadType_{0};
|
||||||
|
std::atomic<bool> codecChanged_{false};
|
||||||
int dtmfPt_ = 101;
|
int dtmfPt_ = 101;
|
||||||
std::map<int, SipCodecId> rxPtMap_;
|
std::map<int, SipCodecId> rxPtMap_;
|
||||||
std::atomic<bool> running_{false};
|
std::atomic<bool> running_{false};
|
||||||
@@ -2783,6 +2591,13 @@ class RtpAudioSession {
|
|||||||
|
|
||||||
int sip_run_main(int argc, char** argv) {
|
int sip_run_main(int argc, char** argv) {
|
||||||
gRunning = 1;
|
gRunning = 1;
|
||||||
|
const char* xdgDir = ::getenv("XDG_RUNTIME_DIR");
|
||||||
|
if (!xdgDir || !xdgDir[0] || ::access(xdgDir, F_OK) != 0) {
|
||||||
|
std::string fallbackDir = "/run/user/" + std::to_string(::getuid());
|
||||||
|
if (::access(fallbackDir.c_str(), F_OK) == 0) {
|
||||||
|
::setenv("XDG_RUNTIME_DIR", fallbackDir.c_str(), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
bool isPreload = false;
|
bool isPreload = false;
|
||||||
std::string initCmd;
|
std::string initCmd;
|
||||||
for (int i = 1; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
@@ -2962,14 +2777,18 @@ int sip_run_main(int argc, char** argv) {
|
|||||||
int answerPt = -1,
|
int answerPt = -1,
|
||||||
int dtmfPt = 101) -> std::string {
|
int dtmfPt = 101) -> std::string {
|
||||||
std::vector<std::pair<int, SipCodecId>> mediaCodecs;
|
std::vector<std::pair<int, SipCodecId>> mediaCodecs;
|
||||||
|
std::string normPref = normalizePreferredCodec(preferredCodec);
|
||||||
if (answerCodec && answerPt >= 0) {
|
if (answerCodec && answerPt >= 0) {
|
||||||
mediaCodecs.push_back({answerPt, *answerCodec});
|
mediaCodecs.push_back({answerPt, *answerCodec});
|
||||||
}
|
} else {
|
||||||
for (SipCodecId c : orderedCodecsForPreference(preferredCodec)) {
|
for (SipCodecId c : orderedCodecsForPreference(normPref)) {
|
||||||
if (answerCodec && c == *answerCodec) continue;
|
if (normPref != "AUTO" && !codecMatchesPreference(c, normPref)) {
|
||||||
int pt = codecDefaultPt(c);
|
continue;
|
||||||
if (answerPt == pt || dtmfPt == pt) continue;
|
}
|
||||||
mediaCodecs.push_back({pt, c});
|
int pt = codecDefaultPt(c);
|
||||||
|
if (dtmfPt == pt) continue;
|
||||||
|
mediaCodecs.push_back({pt, c});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostringstream sdp;
|
std::ostringstream sdp;
|
||||||
@@ -3816,8 +3635,19 @@ int sip_run_main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2b. Check if ZRTP completed handshake on active call
|
// 2b. Check if RTP codec auto-adapted or ZRTP completed handshake on active call
|
||||||
if (callState == SipCallState::Active) {
|
if (callState == SipCallState::Active) {
|
||||||
|
SipCodecId newCodec = SipCodecId::Pcmu;
|
||||||
|
if (rtpSession.consumeCodecNotification(newCodec)) {
|
||||||
|
remoteCodec = newCodec;
|
||||||
|
std::string badge = std::string("SIP/") + rtpSession.activeCodecName();
|
||||||
|
if (rtpSession.isZrtpSecure()) {
|
||||||
|
badge += ":" + rtpSession.getZrtpSas();
|
||||||
|
}
|
||||||
|
sendPluginCommand("callscreen",
|
||||||
|
"ACTIVE sip:1 " + activePeerUri + " " + badge);
|
||||||
|
if (visible) render();
|
||||||
|
}
|
||||||
std::string sas;
|
std::string sas;
|
||||||
if (rtpSession.consumeZrtpNotification(sas)) {
|
if (rtpSession.consumeZrtpNotification(sas)) {
|
||||||
sendPluginCommand("callscreen",
|
sendPluginCommand("callscreen",
|
||||||
|
|||||||
Reference in new issue
Block a user