fix(video): recover encoder capacity, pace RTP and isolate preview sessions

This commit is contained in:
DCCONSTRUCTIONS
2026-11-21 20:58:25 +03:00
parent 3d04fa45d3
commit bed1db3508
29 changed files with 1534 additions and 141 deletions
+46 -32
View File
@@ -16,10 +16,24 @@ class Peer:
self.sent.append(data)
self.ice = type("Ice", (), {})()
self.ice._send = send
self.dtls = type("Dtls", (), {})()
self.dtls.transport = self.ice
async def encrypt(data):
await self.ice._send(data)
self.dtls._send_rtp = encrypt
from aiortc.rtp import HeaderExtensionsMap
self.sender = SimpleNamespace(transport=self.dtls, _ssrc=int.from_bytes(b"ssrc", "big"),
_rtx_ssrc=int.from_bytes(b"rtxs", "big"),
_RTCRtpSender__rtp_header_extensions_map=HeaderExtensionsMap())
async def send(self, data):
if len(data) >= 12 and 128 <= data[0] <= 191:
await self.dtls._send_rtp(data)
else:
await self.ice._send(data)
def getTransceivers(self):
transport = SimpleNamespace(transport=self.ice)
return [SimpleNamespace(sender=SimpleNamespace(transport=transport))]
return [SimpleNamespace(sender=self.sender)]
def on(self, event):
return lambda handler: self.handlers.update({event: handler})
@@ -48,13 +62,13 @@ def test_existing_transport_accounts_rtp_sctp_and_retransmits():
async def run():
pc, client = Peer(), Client()
budget_peer(pc, "video", client=client)
rtp = b"\x80\x60" + b"x" * 100
rtp = b"\x80\x60" + b"x" * 6 + b"ssrc" + b"x" * 90
sctp = b"\x17" + b"x" * 100
rtcp = b"\x80\xc8" + b"x" * 20
for packet in (rtp, rtp, sctp, rtcp):
await pc.ice._send(packet)
await pc.send(packet)
await asyncio.sleep(.02)
assert client.sizes == [102, 102, 101]
assert client.sizes == [118, 118, 101]
assert pc.sent[:2] == [rtp, rtp]
assert sctp in pc.sent and rtcp in pc.sent and len(pc.sent) == 4
await pc.close()
@@ -67,7 +81,7 @@ def test_budget_failure_closes_media_without_sending_or_self_deadlock():
pc, client = Peer(), Client()
client.fail = True
budget_peer(pc, "cloud", client=client)
await pc.ice._send(b"\x17data")
await pc.send(b"\x17data")
await asyncio.sleep(.02)
assert pc.connectionState == "closed" and not pc.sent
asyncio.run(run())
@@ -93,11 +107,11 @@ def test_frame_age_bounds_all_fragments_and_keeps_next_frame_live():
budget_peer(pc, "video", client=client, clock=lambda: now[0])
first = b"\x80\x60\x00\x01" + (1).to_bytes(4, "big") + b"ssrc" + b"x" * 100
second = first[:4] + (2).to_bytes(4, "big") + first[8:]
await pc.ice._send(first)
await pc.send(first)
now[0] += .6
await pc.ice._send(first)
await pc.ice._send(first)
await pc.ice._send(second)
await pc.send(first)
await pc.send(first)
await pc.send(second)
assert pc.sent == [first, second]
assert pc._missioncore_egress_metrics["expired_frames"] == 1
assert pc.connectionState != "closed"
@@ -127,15 +141,15 @@ def test_policy_pause_drops_stale_media_without_retiring_peer(monkeypatch, cycle
rtp = b'\x80\x60\x00\x01' + (1 + cycle * 3000).to_bytes(4,'big') + b'ssrc' + b'x'*100
fresh = rtp[:4] + (2 + cycle * 3000).to_bytes(4,'big') + rtp[8:]
try:
await pc.ice._send(rtp)
await pc.ice._send(b'\x17stale-sensor')
await pc.send(rtp)
await pc.send(b'\x17stale-sensor')
await asyncio.sleep(.58)
assert pc.connectionState != 'closed' and not pc.sent
assert keyframes and pc._missioncore_pending_media() == 0
client.paused = False
await pc.ice._send(rtp) # No replay of expired fragments.
await pc.ice._send(fresh)
await pc.ice._send(b'\x17fresh-sensor')
await pc.send(rtp) # No replay of expired fragments.
await pc.send(fresh)
await pc.send(b'\x17fresh-sensor')
await asyncio.sleep(.03)
assert pc.sent == [fresh, b'\x17fresh-sensor']
assert pc._missioncore_egress_metrics['expired_frames'] == 1
@@ -155,9 +169,9 @@ def test_expired_fifo_never_waits_again_and_protocol_bypasses_backlog():
raise BudgetDeferred()
pc=Peer();budget_peer(pc,'cloud',client=Paused(),clock=lambda:now[0])
try:
for _ in range(3):await pc.ice._send(b'\x17queued')
for _ in range(3):await pc.send(b'\x17queued')
await blocked.wait()
await pc.ice._send(b'\x16handshake')
await pc.send(b'\x16handshake')
assert pc.sent==[b'\x16handshake']
release.set();await asyncio.sleep(.01)
assert calls==[(0.0,.5)]
@@ -180,7 +194,7 @@ def test_frame_fragments_share_one_deadline():
pc=Peer();budget_peer(pc,'video',client=Late(),clock=lambda:now[0])
frame=b'\x80\x60\x00\x01'+(1).to_bytes(4,'big')+b'ssrc'+b'x'*100
try:
await pc.ice._send(frame);await pc.ice._send(frame)
await pc.send(frame);await pc.send(frame)
assert deadlines==[.5,.5] and now[0]==.5
assert pc.sent==[frame] and pc.connectionState!='closed'
finally:await pc.close()
@@ -251,14 +265,14 @@ def test_rtx_cannot_renew_original_deadline_or_replay_before_new_frame():
return b"\x80\x60\x00\x01" + stamp.to_bytes(4,"big") + (11 if retry else 10).to_bytes(4,"big") + b"x"*100
try:
first, retry = packet(0xfffffff0), packet(0xfffffff0, True)
await pc.ice._send(first)
await pc.send(first)
now[0] += .2
await pc.ice._send(retry) # Same deadline, legitimate early retry.
await pc.send(retry) # Same deadline, legitimate early retry.
now[0] += .31
await pc.ice._send(retry)
await pc.send(retry)
fresh = packet(16) # Timestamp wrap is a new frame.
await pc.ice._send(fresh)
await pc.ice._send(retry)
await pc.send(fresh)
await pc.send(retry)
assert pc.sent == [first, retry, fresh]
assert len(client.sizes) == 3
assert pc._missioncore_egress_metrics["expired_frames"] == 1
@@ -284,17 +298,17 @@ def test_rtx_of_previous_frame_retains_original_unexpired_deadline():
try:
first, second, retry = packet(1000), packet(7000), packet(1000, True)
await pc.ice._send(first)
await pc.send(first)
now[0] += .067
await pc.ice._send(second)
await pc.send(second)
now[0] += .1
await pc.ice._send(retry)
await pc.send(retry)
assert pc.sent == [first, second, retry]
# Neither a retry nor a newer frame can extend the original lease.
now[0] = 100.501
await pc.ice._send(retry)
await pc.ice._send(first) # Old primary payload remains forbidden.
await pc.ice._send(packet(9000, True)) # Unknown RTX has no lease.
await pc.send(retry)
await pc.send(first) # Old primary payload remains forbidden.
await pc.send(packet(9000, True)) # Unknown RTX has no lease.
assert pc.sent == [first, second, retry]
assert len(client.sizes) == 3
assert pc.connectionState != "closed"
@@ -318,11 +332,11 @@ def test_rtx_deadline_history_is_bounded_without_retaining_payload():
try:
for stamp in range(300):
await pc.ice._send(packet(stamp))
await pc.send(packet(stamp))
assert pc._missioncore_egress_metrics["retained_frame_deadlines"] == 256
await pc.ice._send(packet(0, True)) # Evicted metadata cannot grant a lease.
await pc.send(packet(0, True)) # Evicted metadata cannot grant a lease.
assert len(pc.sent) == 300
await pc.ice._send(packet(250, True))
await pc.send(packet(250, True))
assert pc.sent[-1] == packet(250, True) and len(pc.sent) == 301
assert pc._missioncore_egress_metrics["retransmitted_packets"] == 1
finally: