|
| 1 | +import time |
| 2 | +from typing import Any, Dict, Mapping, Optional |
| 3 | + |
| 4 | +from inference.core import logger |
| 5 | + |
| 6 | +H264_BITRATE_LIMITS_BPS = { |
| 7 | + "DEFAULT_BITRATE": 6_000_000, |
| 8 | + "MIN_BITRATE": 2_000_000, |
| 9 | + "MAX_BITRATE": 20_000_000, |
| 10 | +} |
| 11 | + |
| 12 | +VP8_BITRATE_LIMITS_BPS = { |
| 13 | + "DEFAULT_BITRATE": 4_000_000, |
| 14 | + "MIN_BITRATE": 1_500_000, |
| 15 | + "MAX_BITRATE": 8_000_000, |
| 16 | +} |
| 17 | + |
| 18 | +REMB_START_BITRATE_BPS = 6_000_000 |
| 19 | +REMB_MIN_BITRATE_BPS = 2_000_000 |
| 20 | +REMB_MAX_BITRATE_BPS = 20_000_000 |
| 21 | + |
| 22 | +_BITRATE_TUNING_APPLIED = False |
| 23 | + |
| 24 | + |
| 25 | +def apply_aiortc_bitrate_tuning() -> None: |
| 26 | + """Apply WebRTC bitrate defaults before aiortc peer connections are created.""" |
| 27 | + global _BITRATE_TUNING_APPLIED |
| 28 | + |
| 29 | + if _BITRATE_TUNING_APPLIED: |
| 30 | + return |
| 31 | + |
| 32 | + import aiortc.codecs.h264 as h264 |
| 33 | + import aiortc.codecs.vpx as vpx |
| 34 | + |
| 35 | + h264_limits = _apply_codec_bitrate_limits(h264, H264_BITRATE_LIMITS_BPS) |
| 36 | + vp8_limits = _apply_codec_bitrate_limits(vpx, VP8_BITRATE_LIMITS_BPS) |
| 37 | + _patch_receiver_remb_estimator() |
| 38 | + _BITRATE_TUNING_APPLIED = True |
| 39 | + |
| 40 | + logger.info( |
| 41 | + "[WEBRTC_BITRATE] applied aiortc bitrate tuning h264=%s vp8=%s " |
| 42 | + "remb_start_bps=%s remb_min_bps=%s remb_max_bps=%s", |
| 43 | + h264_limits, |
| 44 | + vp8_limits, |
| 45 | + REMB_START_BITRATE_BPS, |
| 46 | + REMB_MIN_BITRATE_BPS, |
| 47 | + REMB_MAX_BITRATE_BPS, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def prefer_h264_for_peer_connection(peer_connection: Any) -> bool: |
| 52 | + """Prefer H.264 when the browser offers it, keeping other codecs as fallback.""" |
| 53 | + from aiortc import RTCRtpSender |
| 54 | + |
| 55 | + codecs = RTCRtpSender.getCapabilities("video").codecs |
| 56 | + h264_codecs = [codec for codec in codecs if codec.mimeType.lower() == "video/h264"] |
| 57 | + if not h264_codecs: |
| 58 | + logger.info("[WEBRTC_BITRATE] H.264 codec preference unavailable") |
| 59 | + return False |
| 60 | + |
| 61 | + preferred_codecs = h264_codecs + [ |
| 62 | + codec for codec in codecs if codec not in h264_codecs |
| 63 | + ] |
| 64 | + applied = False |
| 65 | + |
| 66 | + for transceiver in peer_connection.getTransceivers(): |
| 67 | + if transceiver.kind != "video": |
| 68 | + continue |
| 69 | + transceiver.setCodecPreferences(preferred_codecs) |
| 70 | + applied = True |
| 71 | + |
| 72 | + if applied: |
| 73 | + logger.info("[WEBRTC_BITRATE] preferred H.264 for video transceivers") |
| 74 | + return applied |
| 75 | + |
| 76 | + |
| 77 | +def _apply_codec_bitrate_limits( |
| 78 | + module: Any, |
| 79 | + bitrate_limits: Mapping[str, int], |
| 80 | +) -> Dict[str, int]: |
| 81 | + applied = {} |
| 82 | + for constant_name, value in bitrate_limits.items(): |
| 83 | + setattr(module, constant_name, value) |
| 84 | + applied[constant_name] = value |
| 85 | + return applied |
| 86 | + |
| 87 | + |
| 88 | +def _patch_receiver_remb_estimator(rate_module: Optional[Any] = None) -> None: |
| 89 | + if rate_module is None: |
| 90 | + import aiortc.rate as rate_module |
| 91 | + |
| 92 | + estimator_cls = rate_module.RemoteBitrateEstimator |
| 93 | + if getattr(estimator_cls, "_roboflow_remb_tuning_patched", False): |
| 94 | + return |
| 95 | + |
| 96 | + original_init = estimator_cls.__init__ |
| 97 | + original_add = estimator_cls.add |
| 98 | + |
| 99 | + def __init__(self: Any) -> None: |
| 100 | + original_init(self) |
| 101 | + now_ms = int(time.time() * 1000) |
| 102 | + self.rate_control.set_estimate(REMB_START_BITRATE_BPS, now_ms) |
| 103 | + self.rate_control.latest_estimated_throughput = REMB_START_BITRATE_BPS |
| 104 | + |
| 105 | + def add( |
| 106 | + self: Any, |
| 107 | + arrival_time_ms: int, |
| 108 | + abs_send_time: int, |
| 109 | + payload_size: int, |
| 110 | + ssrc: int, |
| 111 | + ) -> Any: |
| 112 | + remb = original_add( |
| 113 | + self, |
| 114 | + arrival_time_ms, |
| 115 | + abs_send_time, |
| 116 | + payload_size, |
| 117 | + ssrc, |
| 118 | + ) |
| 119 | + if remb is None: |
| 120 | + return None |
| 121 | + |
| 122 | + requested_bitrate, ssrcs = remb |
| 123 | + applied_bitrate = _clamp_bitrate( |
| 124 | + requested_bitrate, |
| 125 | + min_bitrate=REMB_MIN_BITRATE_BPS, |
| 126 | + max_bitrate=REMB_MAX_BITRATE_BPS, |
| 127 | + ) |
| 128 | + if applied_bitrate != requested_bitrate: |
| 129 | + self.rate_control.current_bitrate = applied_bitrate |
| 130 | + return applied_bitrate, ssrcs |
| 131 | + |
| 132 | + estimator_cls.__init__ = __init__ |
| 133 | + estimator_cls.add = add |
| 134 | + estimator_cls._roboflow_remb_tuning_patched = True |
| 135 | + |
| 136 | + |
| 137 | +def _clamp_bitrate( |
| 138 | + bitrate: int, |
| 139 | + min_bitrate: int, |
| 140 | + max_bitrate: int, |
| 141 | +) -> int: |
| 142 | + return min(max(bitrate, min_bitrate), max_bitrate) |
0 commit comments