Fix global Power menu key handling, SIP G.711 audio routing, Webarchy audio playback, and ReMEdy Call menu key

This commit is contained in:
Luxferre committed 2026-10-31 19:39:28 +02:00
1 parent d7335b5ebe
commit 0af39d9fa0
76 files changed
+506 -104

No files matched your search

+189 -67
View File
@@ -137,7 +137,7 @@ std::string normalizePreferredCodec(const std::string& raw) {
std::string cyclePreferredCodec(const std::string& cur) {
static const char* kList[] = {
"AUTO", "OPUS", "G722", "PCMU", "PCMA", "GSM", "G726-32", "DVI4", "L16"
"AUTO", "PCMU", "PCMA", "GSM", "OPUS", "G722", "G726-32", "DVI4", "L16"
};
std::string norm = normalizePreferredCodec(cur);
for (size_t i = 0; i < sizeof(kList) / sizeof(kList[0]); ++i) {
@@ -152,14 +152,15 @@ std::string cyclePreferredCodec(const std::string& cur) {
uint8_t linearToUlaw(int16_t pcmVal) {
constexpr int kBias = 0x84;
constexpr int kClip = 32635;
int mask = (pcmVal < 0) ? 0x7f : 0xff;
if (pcmVal < 0) pcmVal = static_cast<int16_t>(-pcmVal);
if (pcmVal > kClip) pcmVal = kClip;
pcmVal = static_cast<int16_t>(pcmVal + kBias);
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 && pcmVal > thr; ++seg, thr = (thr << 1) | 1) {
for (int thr = 0xFF; seg < 7 && pcm > thr; ++seg, thr = (thr << 1) | 1) {
}
uint8_t uval = static_cast<uint8_t>((seg << 4) | ((pcmVal >> (seg + 3)) & 0x0f));
uint8_t uval = static_cast<uint8_t>((seg << 4) | ((pcm >> (seg + 3)) & 0x0f));
return static_cast<uint8_t>(uval ^ mask);
}
@@ -170,18 +171,20 @@ int16_t ulawToLinear(uint8_t uval) {
}
uint8_t linearToAlaw(int16_t pcmVal) {
int pcm = pcmVal;
int mask = 0x55;
if (pcmVal >= 0) {
if (pcm >= 0) {
mask |= 0x80;
} else {
pcmVal = static_cast<int16_t>(-pcmVal - 1);
pcm = -pcm - 1;
}
if (pcm > 32767) pcm = 32767;
int seg = 0;
for (int thr = 0xFF; seg < 7 && pcmVal > thr; ++seg, thr = (thr << 1) | 1) {
for (int thr = 0xFF; seg < 7 && pcm > thr; ++seg, thr = (thr << 1) | 1) {
}
uint8_t aval = (seg == 0)
? static_cast<uint8_t>((pcmVal >> 4) & 0x0f)
: static_cast<uint8_t>((seg << 4) | ((pcmVal >> (seg + 3)) & 0x0f));
? static_cast<uint8_t>((pcm >> 4) & 0x0f)
: static_cast<uint8_t>((seg << 4) | ((pcm >> (seg + 3)) & 0x0f));
return static_cast<uint8_t>(aval ^ mask);
}
@@ -424,7 +427,6 @@ class G722State {
int il = (el < 0) ? kMisn[mila] : kMisp[mila];
int ril = il >> 2;
int dlt = (band_[0].det * kQ6m4[ril]) >> 15;
if (il < 32) dlt = -dlt;
updateBand(0, dlt, ril, true);
// Higher sub-band (2-bit quantizer)
@@ -451,8 +453,7 @@ class G722State {
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 * ((il < 32) ? -kQ6m4[ril] : kQ6m4[ril])) >> 15,
ril, true);
updateBand(0, (band_[0].det * kQ6m4[ril]) >> 15, ril, true);
// Higher sub-band reconstruction (2-bit)
int dhy = (band_[1].det * kQm2[ih]) >> 15;
@@ -573,18 +574,18 @@ class G722State {
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, 296
0, -20456, -12896, -8968, -6288, -4240, -2584, -1200,
20456, 12896, 8968, 6288, 4240, 2584, 1200, 0
};
static constexpr int kQ6b[64] = {
-152, -28000, -21984, -18256, -15800, -13864, -12240, -10840,
-9608, -8512, -7528, -6624, -5792, -5032, -4328, -3664,
-3056, -2480, -1936, -1424, -936, -464, -152, -152,
-26400, -20912, -17600, -15288, -13448, -11880, -10520, -9320,
28000, 21984, 18256, 15800, 13864, 12240, 10840, 9608,
8512, 7528, 6624, 5792, 5032, 4328, 3664, 3056,
2480, 1936, 1424, 936, 464, 152, 26400, 20912,
17600, 15288, 13448, 11880, 10520, 9320, -8232, 8232
-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] = {
@@ -1403,16 +1404,16 @@ bool codecMatchesPreference(SipCodecId id, const std::string& pref) {
std::vector<SipCodecId> orderedCodecsForPreference(const std::string& pref) {
static const SipCodecId kDefaultOrder[] = {
SipCodecId::Opus,
SipCodecId::G722,
SipCodecId::Pcmu,
SipCodecId::Pcma,
SipCodecId::Gsm,
SipCodecId::Opus,
SipCodecId::G722,
SipCodecId::G726_32,
SipCodecId::Dvi4_16k,
SipCodecId::Dvi4_8k,
SipCodecId::L16_16k,
SipCodecId::L16_8k
SipCodecId::Dvi4_16k,
SipCodecId::L16_8k,
SipCodecId::L16_16k
};
std::vector<SipCodecId> out;
for (SipCodecId c : kDefaultOrder) {
@@ -1434,7 +1435,9 @@ std::string codecRtpmapLine(int pt, SipCodecId id) {
switch (id) {
case SipCodecId::Opus:
oss << "opus/48000/2\r\n"
<< "a=fmtp:" << pt << " minptime=10;useinbandfec=1;maxaveragebitrate=24000\r\n";
<< "a=fmtp:" << pt
<< " minptime=10;useinbandfec=1;maxaveragebitrate=24000;"
"maxplaybackrate=16000;sprop-maxcapturerate=16000;stereo=0;sprop-stereo=0\r\n";
break;
case SipCodecId::G722:
oss << "G722/8000\r\n";
@@ -1579,7 +1582,20 @@ void parseSdpMedia(const std::string& msg, const std::string& preferredCodec,
}
}
// 2. Otherwise pick the first mutually supported codec in peer's m=audio line
// 2. In AUTO mode, prefer our ordered priority list (G.711 PCMU/PCMA first)
// among all codecs offered by the peer.
for (SipCodecId prefId : orderedCodecsForPreference("AUTO")) {
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) {
auto it = outPtMap.find(pt);
if (it != outPtMap.end()) {
@@ -1619,8 +1635,17 @@ class RtpAudioSession {
SipCodecId codec, int payloadType, int dtmfPt,
const std::map<int, SipCodecId>& ptMap, bool enableZrtp,
bool peerHasZrtpSdp = false) {
if (remoteIp.empty() || remotePort <= 0) {
stop();
return false;
}
if (running_.load() && sock_ >= 0 && remoteIp == activeRemoteIp_ &&
remotePort == activeRemotePort_ && codec == codec_ &&
payloadType == payloadType_) {
dtmfPt_ = (dtmfPt > 0 && dtmfPt <= 127) ? dtmfPt : 101;
return true;
}
stop();
if (remoteIp.empty() || remotePort <= 0) return false;
int sock = ::socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (sock < 0) return false;
@@ -1667,6 +1692,8 @@ class RtpAudioSession {
sock_ = sock;
remAddr_ = remAddr;
activeRemoteIp_ = remoteIp;
activeRemotePort_ = remotePort;
codec_ = codec;
payloadType_ = payloadType;
dtmfPt_ = (dtmfPt > 0 && dtmfPt <= 127) ? dtmfPt : 101;
@@ -1679,6 +1706,10 @@ class RtpAudioSession {
void stop() {
running_.store(false);
pid_t cp = capPid_.load();
if (cp > 1) ::kill(cp, SIGTERM);
pid_t pp = playPid_.load();
if (pp > 1) ::kill(pp, SIGTERM);
if (worker_.joinable()) {
worker_.join();
}
@@ -1686,6 +1717,8 @@ class RtpAudioSession {
::close(sock_);
sock_ = -1;
}
activeRemoteIp_.clear();
activeRemotePort_ = 0;
std::lock_guard<std::mutex> lk(zrtpMtx_);
localHelloMsg_.clear();
srtpActive_.store(false);
@@ -2319,18 +2352,69 @@ class RtpAudioSession {
"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
std::string capCmd = "arecord -q -D plughw:0,1 --buffer-time=80000 --period-time=20000 "
"-f S16_LE -r " + std::to_string(audioRate) +
" -c 1 -t raw 2>/dev/null";
std::string playCmd = "aplay -q --buffer-time=80000 --period-time=20000 "
"-f S16_LE -r " + std::to_string(audioRate) +
" -c 1 -t raw 2>/dev/null";
FILE* capFp = ::popen(capCmd.c_str(), "r");
FILE* playFp = ::popen(playCmd.c_str(), "w");
int capFd = capFp ? ::fileno(capFp) : -1;
if (capFd >= 0) {
int fl = ::fcntl(capFd, F_GETFL, 0);
::fcntl(capFd, F_SETFL, fl | O_NONBLOCK);
size_t frameBytes = frameSamples * sizeof(int16_t);
std::string rateStr = std::to_string(audioRate);
int capPipe[2] = {-1, -1};
int capFd = -1;
pid_t capPid = -1;
if (::pipe(capPipe) == 0) {
capPid = ::fork();
if (capPid == 0) {
::close(capPipe[0]);
::dup2(capPipe[1], STDOUT_FILENO);
::close(capPipe[1]);
int nullFd = ::open("/dev/null", O_RDWR);
if (nullFd >= 0) {
::dup2(nullFd, STDIN_FILENO);
::dup2(nullFd, STDERR_FILENO);
if (nullFd > 2) ::close(nullFd);
}
::execlp("arecord", "arecord", "-q", "--buffer-time=160000",
"--period-time=20000", "-f", "S16_LE", "-r", rateStr.c_str(),
"-c", "1", "-t", "raw", static_cast<char*>(nullptr));
::_exit(1);
} else if (capPid > 0) {
::close(capPipe[1]);
capFd = capPipe[0];
int fl = ::fcntl(capFd, F_GETFL, 0);
::fcntl(capFd, F_SETFL, fl | O_NONBLOCK);
capPid_.store(capPid);
} else {
::close(capPipe[0]);
::close(capPipe[1]);
}
}
int playPipe[2] = {-1, -1};
int playFd = -1;
pid_t playPid = -1;
if (::pipe(playPipe) == 0) {
playPid = ::fork();
if (playPid == 0) {
::close(playPipe[1]);
::dup2(playPipe[0], STDIN_FILENO);
::close(playPipe[0]);
int nullFd = ::open("/dev/null", O_RDWR);
if (nullFd >= 0) {
::dup2(nullFd, STDOUT_FILENO);
::dup2(nullFd, STDERR_FILENO);
if (nullFd > 2) ::close(nullFd);
}
::execlp("aplay", "aplay", "-q", "--buffer-time=200000",
"--period-time=20000", "-f", "S16_LE", "-r", rateStr.c_str(),
"-c", "1", "-t", "raw", static_cast<char*>(nullptr));
::_exit(1);
} else if (playPid > 0) {
::close(playPipe[0]);
playFd = playPipe[1];
int fl = ::fcntl(playFd, F_GETFL, 0);
::fcntl(playFd, F_SETFL, fl | O_NONBLOCK);
playPid_.store(playPid);
} else {
::close(playPipe[0]);
::close(playPipe[1]);
}
}
int opusErr = OPUS_OK;
@@ -2356,8 +2440,19 @@ class RtpAudioSession {
uint32_t ts = static_cast<uint32_t>(nowMs() * 8);
uint32_t ssrc = 0x51504241u ^ static_cast<uint32_t>(::getpid());
int64_t nextTxMs = nowMs() + 20;
bool playPrimed = false;
uint8_t capRing[2560];
size_t capAvail = 0;
while (running_.load()) {
// Drain available microphone capture bytes into capRing without blocking
if (capFd >= 0 && capAvail < sizeof(capRing)) {
ssize_t r = ::read(capFd, capRing + capAvail, sizeof(capRing) - capAvail);
if (r > 0) {
capAvail += static_cast<size_t>(r);
}
}
// 1. Receive incoming RTP / SRTP / ZRTP packets
uint8_t rxPkt[1500];
while (true) {
@@ -2388,9 +2483,9 @@ class RtpAudioSession {
const uint8_t* payload = rxPkt + hdrLen;
size_t payLen = pktLen - hdrLen;
if (!playFp || payLen == 0) continue;
if (playFd < 0 || payLen == 0) continue;
int16_t pcmDecoded[960];
int16_t pcmDecoded[1920];
size_t decodedSamples = 0;
switch (rxCodec) {
@@ -2398,28 +2493,28 @@ class RtpAudioSession {
if (opusDec) {
int r = opus_decode(opusDec, payload,
static_cast<opus_int32>(payLen),
pcmDecoded, 960, 0);
pcmDecoded, 1920, 0);
if (r > 0) decodedSamples = static_cast<size_t>(r);
}
break;
case SipCodecId::G722:
decodedSamples = g722Dec.decodeFrame(payload, payLen, pcmDecoded, 960);
decodedSamples = g722Dec.decodeFrame(payload, payLen, pcmDecoded, 1920);
break;
case SipCodecId::Pcmu:
decodedSamples = std::min<size_t>(payLen, 960);
decodedSamples = std::min<size_t>(payLen, 1920);
for (size_t i = 0; i < decodedSamples; ++i) {
pcmDecoded[i] = ulawToLinear(payload[i]);
}
break;
case SipCodecId::Pcma:
decodedSamples = std::min<size_t>(payLen, 960);
decodedSamples = std::min<size_t>(payLen, 1920);
for (size_t i = 0; i < decodedSamples; ++i) {
pcmDecoded[i] = alawToLinear(payload[i]);
}
break;
case SipCodecId::Gsm:
if (gsmDec) {
for (size_t off = 0; off + 33 <= payLen && decodedSamples + 160 <= 960; off += 33) {
for (size_t off = 0; off + 33 <= payLen && decodedSamples + 160 <= 1920; off += 33) {
gsm_byte frame[33];
std::memcpy(frame, payload + off, 33);
if (gsm_decode(gsmDec, frame, pcmDecoded + decodedSamples) == 0) {
@@ -2429,15 +2524,15 @@ class RtpAudioSession {
}
break;
case SipCodecId::G726_32:
decodedSamples = g726Dec.decodeFrame(payload, payLen, pcmDecoded, 960);
decodedSamples = g726Dec.decodeFrame(payload, payLen, pcmDecoded, 1920);
break;
case SipCodecId::Dvi4_8k:
case SipCodecId::Dvi4_16k:
decodedSamples = dvi4Dec.decodeFrame(payload, payLen, pcmDecoded, 960);
decodedSamples = dvi4Dec.decodeFrame(payload, payLen, pcmDecoded, 1920);
break;
case SipCodecId::L16_8k:
case SipCodecId::L16_16k:
for (size_t i = 0; i + 1 < payLen && decodedSamples < 960; i += 2) {
for (size_t i = 0; i + 1 < payLen && decodedSamples < 1920; i += 2) {
uint16_t be = (static_cast<uint16_t>(payload[i]) << 8) | payload[i + 1];
pcmDecoded[decodedSamples++] = static_cast<int16_t>(be);
}
@@ -2445,9 +2540,14 @@ class RtpAudioSession {
}
if (decodedSamples > 0) {
if (!playPrimed) {
playPrimed = true;
int16_t silence[960] = {0};
(void)::write(playFd, silence, frameSamples * 3 * sizeof(int16_t));
}
int rxRate = codecAudioSampleRate(rxCodec);
if (rxRate == audioRate) {
std::fwrite(pcmDecoded, sizeof(int16_t), decodedSamples, playFp);
(void)::write(playFd, pcmDecoded, decodedSamples * sizeof(int16_t));
} else if (rxRate == 8000 && audioRate == 16000) {
int16_t pcmUp[1920];
size_t upCount = 0;
@@ -2457,17 +2557,16 @@ class RtpAudioSession {
pcmUp[upCount++] = s0;
pcmUp[upCount++] = static_cast<int16_t>((static_cast<int>(s0) + s1) / 2);
}
std::fwrite(pcmUp, sizeof(int16_t), upCount, playFp);
(void)::write(playFd, pcmUp, upCount * sizeof(int16_t));
} else if (rxRate == 16000 && audioRate == 8000) {
int16_t pcmDown[480];
int16_t pcmDown[960];
size_t downCount = 0;
for (size_t i = 0; i + 1 < decodedSamples && downCount < 480; i += 2) {
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);
}
std::fwrite(pcmDown, sizeof(int16_t), downCount, playFp);
(void)::write(playFd, pcmDown, downCount * sizeof(int16_t));
}
std::fflush(playFp);
}
}
@@ -2476,7 +2575,10 @@ class RtpAudioSession {
periodicZrtpTick(ssrc, now);
if (now >= nextTxMs) {
nextTxMs = now + 20;
nextTxMs += 20;
if (now - nextTxMs > 60) {
nextTxMs = now + 20;
}
char dtmfCh = '\0';
{
std::lock_guard<std::mutex> lk(dtmfMtx_);
@@ -2513,9 +2615,12 @@ class RtpAudioSession {
ts += codecRtpTimestampStep20ms(codec_) * 3;
} else {
int16_t pcmIn[320] = {0};
if (capFd >= 0) {
ssize_t r = ::read(capFd, pcmIn, frameSamples * sizeof(int16_t));
(void)r;
if (capAvail >= frameBytes) {
std::memcpy(pcmIn, capRing, frameBytes);
capAvail -= frameBytes;
if (capAvail > 0) {
std::memmove(capRing, capRing + frameBytes, capAvail);
}
}
uint8_t pkt[12 + 768 + 16];
uint16_t curSeq = seq++;
@@ -2596,17 +2701,31 @@ class RtpAudioSession {
if (opusDec) opus_decoder_destroy(opusDec);
if (gsmEnc) gsm_destroy(gsmEnc);
if (gsmDec) gsm_destroy(gsmDec);
if (capFp) ::pclose(capFp);
if (playFp) ::pclose(playFp);
if (capFd >= 0) ::close(capFd);
if (playFd >= 0) ::close(playFd);
capPid_.store(-1);
playPid_.store(-1);
if (capPid > 1) {
::kill(capPid, SIGKILL);
::waitpid(capPid, nullptr, 0);
}
if (playPid > 1) {
::kill(playPid, SIGKILL);
::waitpid(playPid, nullptr, 0);
}
}
int sock_ = -1;
struct sockaddr_in remAddr_ {};
std::string activeRemoteIp_;
int activeRemotePort_ = 0;
SipCodecId codec_ = SipCodecId::Pcmu;
int payloadType_ = 0;
int dtmfPt_ = 101;
std::map<int, SipCodecId> rxPtMap_;
std::atomic<bool> running_{false};
std::atomic<pid_t> capPid_{-1};
std::atomic<pid_t> playPid_{-1};
std::thread worker_;
std::mutex dtmfMtx_;
std::string dtmfQueue_;
@@ -3635,6 +3754,9 @@ int sip_run_main(int argc, char** argv) {
} else if (cmd.rfind("CALL_HANGUP", 0) == 0) {
hangupActiveSipCall(false);
if (visible) render();
} else if (cmd == "HANGUP" || cmd.rfind("HANGUP ", 0) == 0) {
hangupActiveSipCall(true);
if (visible) render();
} else if (cmd.rfind("CALL_MUTE_RING", 0) == 0) {
::unlink("/tmp/sxmo_call_ringing");
runAsyncCmd("sxmo_jobs.sh stop ringing >/dev/null 2>&1; "