Closes#2212
Behind Redis Sentinel, rtpengine stays on the address it was given:
after a failover its writes go to the old master, which refuses them
until it is demoted, then serves them as a replica.
New options --redis-sentinel, --redis-sentinel-master and
--redis-sentinel-interval; the keyword "sentinel" replaces IP:PORT in
--redis, --redis-write or --redis-subscribe. A "redis sentinel" thread
asks every Sentinel for the master, takes the highest config epoch
(never an older one than in use), checks the node with ROLE and
publishes it with a generation number. It also runs at once when a
connection meets a replica. redis_check_conn() compares the generation
and reconnects on a change; these connections require the master role.
Without "sentinel", none of this runs.
Tested by auto-daemon-tests-redis-sentinel.pl and -sentinel-start.pl.
Change-Id: I7b31b50968fd6a255b9933fbf2c95fc95a8b8408
Closes#2212
--redis-write accepts any role, so after a failover that demotes its
node, rtpengine reconnects to the same address, accepts the replica,
and every later write is refused with READONLY. --redis alone requires
the master at connect, but keeps the connection when its writes are
refused.
The write connection now requires the master role, as --redis alone
does, and a connection that requires it is dropped on a write refused
with READONLY or MASTERDOWN, so that the next operation reconnects and
checks the role. Other refusals keep the connection. The read database
of --redis, when --redis-write is given, still accepts any role.
Tested by auto-daemon-tests-redis-master-only.pl; the READONLY cases
of auto-daemon-tests-redis-errors.pl, which expected the connection to be
kept, now use NOREPLICAS.
Change-Id: Ie164c2c3495c6c8b77553caf9a449e40f8e13699
Cache timestamps from the actual output encoder rather than the DTX decoder context, and retain elapsed time when transcoding resumes after playback or floor blocking. Add repeated-burst coverage for PCMA, Opus and G.722, including timestamp wraparound, and update DTX restart expectations.
Validation: 712 repeated-burst checks, 19 null-packet/DTMF checks and 87 playback checks passed. Extended DTX retains the same 56 baseline failures with no additional failures.
Closes#2191
Change-Id: I3b142a5ea377b94c2a1d16e97dee85569683af86
A fix-up for 4fbef394
Since that commit, the printer adds the `mid` from the egress media
extmap even when the input had none and the older bug (with the missing
room in `codec_add_dtmf_packet()`) started corrupting packets.
Just reserve the extension room in the same way the `codec_add_raw_packet_dup()`
and the `packet_encoded_packetize()` already do.
An example of bug triggered: chrome sending the DTMF with no extensions.
Change-Id: I87a61e1f87e896609e76bbdf87b4c7827027f39d
Expose locally measured RTP loss and jitter in NG query/delete output
even without RTCP, merge kernel-measured per-SSRC loss/jitter from shm
into userspace consumers (NG, MQTT, RTCP RRs, call summary), start the
kernel loss tracker at -1 like userspace, and skip jitter metrics when
no RTP clock rate is known.
Closes#2201Closes#934
Change-Id: Id7a6b1b87ae04def15fd81968d3be07fdf385395
Replace spinning for websocket_service_lock with a short mutex-protected
writable queue. All lws_callback_on_writable() calls are now marshalled
onto the LWS service thread and only run after lws_service() returns,
instead of being issued directly from worker threads while the service
thread may be holding the same lock. The callback queue mutex is
released before calling into libwebsockets, and the amount of queued
work drained per service cycle is bounded to avoid starving other
connections under a connection-abort storm. Pending callbacks are
dropped on teardown before the LWS session storage is freed.
Add an HTTP disconnect-storm regression test (two worker threads
hammering /metrics with aborted connections while a liveness /ping
request keeps proving the service thread is not stuck) to
t/auto-daemon-tests-websocket.py, plus a standalone stress script. The
stress test waits for the listening socket to come up before firing,
avoiding a FileNotFoundError race on CI.
Closes#2206
Change-Id: I81e03c1d7f1a0b5650aa719d00d60be3d3d0213b
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
4.3 seems to change the default behaviour regarding dual v4/v6 socket
setup with port-only bindings.
Change it to be explicit about it: Always request v6-only sockets and
open a separate v4 socket.
Keep the previous code only for <2.1 versions, which don't know about
the required options.
Change-Id: I6a437bac601e9de49ced26e993d04cdbf5325796
Add a codec module for Codec 2, using libcodec2 for decoding and
encoding.
There is no RFC payload format for Codec 2, so this follows the
convention Asterisk uses: encoding name "codec2" at 8000 Hz, mono, with
one or more complete frames per RTP packet, each padded to whole bytes.
The mode is selected with the format parameter "mode=" (3200, 2400,
1600, 1400, 1300, 1200 or 700C) and defaults to 2400, the only mode
Asterisk supports. With the transcode option it can also be given as
the bit rate, e.g. "codec2/8000/1/3200".
Different modes are different bit streams, so payload types only match
when they use the same mode. Modes 1600 and below use 40 ms frames, so
their ptime is rounded up to a multiple of 40 ms. Modes not known to
rtpengine (e.g. 450) are passed through unchanged when both sides
agree, but can't be transcoded.
libcodec2 is optional. It is detected with pkg-config (or from
/usr/include/codec2) by utils/gen-codec2-flags. Without it, Codec 2 is
still recognised for SDP handling and passthrough. In a Debian build
the dependency can be dropped with the pkg.ngcp-rtpengine.nocodec2
build profile.
test-transcode gains Codec 2 tests for both directions in modes 2400,
3200 and 1300, plus passthrough. check_encoder() is no longer limited
to the extended AMR tests, as these use it too.
Closes#2226
Change-Id: I2cad021f48e2043f36c30f5e6290cb7d6eba8c1f
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
remove_oldest_packets() took entries out of jb->ttq.entries and freed
them without holding jb->ttq.lock. It even dropped jb->lock first, but
that lock never protected the tree: the jitter buffer thread pops,
runs and re-inserts entries in timerthread_queue_run() under
ttq.lock only.
So when a burst of packets pushed the buffer over its target capacity,
the media thread could modify the GTree at the same time as the jitter
buffer thread. Both threads can also pick the same first entry, which
is then freed by the discard and sent and freed again by the timer
thread. Either way this corrupts the heap and crashes the daemon.
Take ttq.lock around the removal, as timerthread_queue_flush() does,
and keep jb->lock held. The lock order jb->lock -> ttq.lock is the one
timerthread_queue_push() already uses from queue_packet(), and
jb_packet_free() takes neither lock.
Closes#2225
Change-Id: Ibab99b0258c6a573018f76c1c58ee5518c04dac0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The decoder output frame was hard-coded to 960 samples, which is 20 ms
at 48 kHz. libopus refuses to decode a packet that does not fit the
given frame size, so every 40 ms and 60 ms packet (both ptimes accepted
by opus_init) failed with "buffer too small" and was dropped.
Size the buffer for 120 ms, the longest duration an Opus packet can
carry, at the decoder's clock rate.
Closes#2224
Change-Id: Ic2fcb32977ad0e262d42b74948103b0f68511d95
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the negotiated fmtp has a bitrate range (br=A-B), the encoder's
lower bound check compared the bitrate against max_br instead of
min_br. Any bitrate below the upper bound was therefore forced down
to the minimum, e.g. br=9.6-24.4 with the default 16.4 kbit/s ended
up encoding at 9.6 kbit/s instead of staying at 16.4.
Compare against min_br so the bitrate is only raised when it is
actually below the lower bound.
Closes#2223
Change-Id: I8bfaff332476e351e0b7a6a375a9c55bb3fa772f
The egress sequence offset of a stream counts the packets rtpengine sends in
place of the sender's: the decoder charges one per packet it consumes and the
encoder pays one back per packet it sends, so the output stays contiguous
through transcoding, DTX and DTMF. The audio player's decoder charges the same
way, but its frames go into the mix, which leaves under the media player's own
SSRC, so nothing pays the charge back: after an injection of N packets the
stream's offset is -N. The kernel module applied it to every packet while the
plain userspace passthrough handler applied it only under a fixed egress SSRC,
so after `inject stop` the receiver saw the sender's numbers from userspace
and then, from the kernel, numbers N lower; under a fixed egress SSRC a number
could repeat after the injection. SRTP derives the packet index from the
sequence number and discards packets behind the replay window: a WebRTC
client, whose window is 1024 packets, went silent for 20 seconds after a
40-second injection.
A decode-only handler, whose output never leaves under the input's egress
SSRC, no longer moves that offset: not for the packets it decodes, not for a
DTX discard and not for a sequence reset of the sender. An injection then
leaves the offset where it was, and the sender's packets leave with the
sender's own numbers from userspace and from the kernel alike, plain RTP or
SRTP, whatever the keys.
The receiver sees one forward gap per injection, the length of the injected
stretch.
Closes#2209
Change-Id: I409b4e51fe85168ec2449cd2602f24333dec539e
A sink that substitutes the SSRC rewrites the header the sender authenticated,
so it re-encrypts the packet. A fixed egress SSRC on a stream forwarded under
the sender's key used to reach the receiver with the sender's authentication
tag over the rewritten header, and the receiver dropped every packet. The
predicate is the one the kernel module already uses for ssrc_subst: echo,
transcoding or a fixed egress SSRC.
The handlers rewrite the packet in place while every sink of the stream is
handed the same received buffer, so a fixed egress SSRC, the sequence offset
and an injected DTMF event used to reach the next sink too, and after an event
the next sink read a header on the handler's stack. The sink loop now starts
every sink from the packet as received and gives each one its own copy unless
it is the last, as it already did for RTCP.
Closes#2209
Change-Id: I265d6b2d3eeac4effcbc76dcd65bd1d3fe8d2ba1
stats->ext_seq has been the SRTP receive index since srtp_index moved into
shm (08c7f5853d). The transcode sequencer overwrote it after every
emitted packet with its own roc<<16|seq, which lags the received packets
and, whenever the sequencer emits a late or replayed packet with a lower
sequence number, carries a roc that was incremented without a real
wrap-around. packet_index() then estimated the wrong index, and once the
sender's real roc reached 1 none of the fallback guesses matched, so every
packet of the stream failed authentication until the call ended.
packet_index() already maintains the index for every received packet, so
drop the write. Also make the sequencer's own roc increment only on a
forward wrap-around, never on a backwards step.
Closes#2221
Change-Id: Ia38bf8cbc63dd3435717927f1adfd81ac94ea347
test6: lost / received / lost with forced emissions; the second forced
emission must return the lower queued packet, not the one that arrived
last. test7: a packet exactly one ring size ahead across the 65535->0
wrap-around must be handled as a seq reset.
Both tests fail on the previous sequencer code.
Change-Id: I47c5d0b961856255cb204fbcf2e656cf01b49d7d
Treat a packet exactly one ring size ahead across the 65535->0
wrap-around as a seq reset, like the non-wrapped case, instead of
accepting it into the head slot.
Fixes#2220
Change-Id: I94e2fdd3d50d5c8241cfed5da06829097566356e
The decision to stop is taken in the `media_player_run()` with the call
read-locked and the player locked, the same way as the `run_func()` decides
that the playback is finished, so the stop can share the write-locked
part of the finished case, including the check that the player wasn't
stopped or restarted in-between.
That replaces the separate re-check of the `stop-on-ingress` state
and the rescheduling needed when it failed.
Stopping goes through the `call_stop_media()` taking the player directly,
so there is no need to look it up as `MP_DEFAULT` again.
Change-Id: Iaed17c10a0dc6d2b34cdac51fb7f81b7c3a71bb5
When the `run_func()` reports the end of the playback,
then the cleanup is done after dropping the call's read lock and
taking the write lock.
If the `play media` command restarts the player, or the `stop media` stops it
in the meanwhile, the cleanup then clears the `next_run` of the new playback,
which silently stops it and also lifts its `block egress`.
Simplest solution is to count every stop and start/rester of the player
under the write lock and then skip the cleanup if the count has changed
since `run_func()` was called, means the stop has done its own cleanup
and the restart has set up the new playback.
Change-Id: I030b7d32c1b15aaaf81b2f56492bc3299bf6c231