feat(sdk): add bounded media admission, diagnostics and H264 recovery
Introduce deadline-bound admission and common diagnostics for constrained links. Add H264 packetization, recovery and the measured two-thread veryfast encoder path.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""Real codec regression: moving detail, paced timestamps, bounded IDR bursts."""
|
||||
|
||||
from fractions import Fraction
|
||||
|
||||
import av
|
||||
import numpy as np
|
||||
import pytest
|
||||
from aiortc.codecs.h264 import H264Encoder, h264_depayload
|
||||
from missioncore_plugin_sdk.preview_h264 import encode_preview_frame
|
||||
|
||||
|
||||
class PreviewEncoder(H264Encoder):
|
||||
fps = 4
|
||||
|
||||
def _encode_frame(self, frame, force_keyframe):
|
||||
return encode_preview_frame(self, frame, force_keyframe, self.fps)
|
||||
|
||||
|
||||
def moving_detail(encoder, fps):
|
||||
# Small fixed input, no camera/network, no retained frames or benchmark load.
|
||||
source = np.random.default_rng(28).integers(0, 256, (160, 320), dtype=np.uint8)
|
||||
decoder = av.CodecContext.create("h264", "r")
|
||||
errors, sizes, timestamps = [], [], []
|
||||
for index in range(24):
|
||||
pixels = np.roll(source, index * 7, axis=1)
|
||||
frame = av.VideoFrame.from_ndarray(pixels, format="gray").reformat(format="yuv420p")
|
||||
frame.pts, frame.time_base = index * 90000 // fps, Fraction(1, 90000)
|
||||
payloads, stamp = encoder.encode(frame)
|
||||
timestamps.append(stamp)
|
||||
sizes.append(sum(map(len, payloads)))
|
||||
decoded = decoder.decode(av.Packet(b"".join(h264_depayload(p) for p in payloads)))
|
||||
assert len(decoded) == 1
|
||||
delta = pixels.astype(float) - decoded[0].to_ndarray(format="gray")
|
||||
errors.append(float(np.mean(delta ** 2)))
|
||||
return float(np.mean(errors)), max(sizes), timestamps
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fps", [4, 10, 15])
|
||||
def test_paced_h264_retains_more_moving_detail_without_oversized_start(fps):
|
||||
old, new = H264Encoder(), PreviewEncoder()
|
||||
old.target_bitrate = new.target_bitrate = 500_000
|
||||
new.fps = fps
|
||||
old_error, _, old_pts = moving_detail(old, fps)
|
||||
error, largest, pts = moving_detail(new, fps)
|
||||
assert error < old_error * .8
|
||||
assert pts == old_pts == [i * 90000 // fps for i in range(24)]
|
||||
# Payload headroom below half a second at 500 kbps; transport accounting,
|
||||
# Node's separate cap and deadline are tested by test_link_budget.py.
|
||||
assert largest < 31_250
|
||||
|
||||
|
||||
def test_paced_h264_reconfigures_for_cadence_and_rate_but_keeps_capture_clock():
|
||||
encoder = PreviewEncoder()
|
||||
encoder.target_bitrate = 600_000
|
||||
frames = []
|
||||
settings = [(4, 600_000, 0), (4, 600_000, 22500),
|
||||
(15, 600_000, 31500), (15, 500_000, 450000)]
|
||||
for fps, rate, pts in settings:
|
||||
encoder.fps, encoder.target_bitrate = fps, rate
|
||||
frame = av.VideoFrame(64, 32, "yuv420p")
|
||||
for plane in frame.planes:
|
||||
plane.update(bytes(plane.buffer_size))
|
||||
frame.pts, frame.time_base = pts, Fraction(1, 90000)
|
||||
payloads, stamp = encoder.encode(frame)
|
||||
assert payloads and stamp == pts
|
||||
frames.append(encoder.codec)
|
||||
assert encoder.codec.framerate == fps and encoder.codec.bit_rate == rate
|
||||
assert frames[0] is frames[1]
|
||||
assert frames[1] is not frames[2] and frames[2] is not frames[3]
|
||||
|
||||
|
||||
def test_preview_encodes_immediately_with_bounded_threads_and_no_reordering():
|
||||
encoder = PreviewEncoder()
|
||||
encoder.fps = 15
|
||||
encoder.target_bitrate = 781_000
|
||||
for index in range(3):
|
||||
frame = av.VideoFrame(128, 64, "yuv420p")
|
||||
for plane in frame.planes:
|
||||
plane.update(bytes([96]) * plane.buffer_size)
|
||||
frame.pts, frame.time_base = index * 6000, Fraction(1, 90000)
|
||||
payloads, pts = encoder.encode(frame, force_keyframe=index == 2)
|
||||
assert payloads and pts == index * 6000
|
||||
assert encoder.codec.thread_count == 2
|
||||
assert encoder.codec.has_b_frames is False
|
||||
Reference in New Issue
Block a user