|
| 1 | +import importlib |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from aiortc.codecs.h264 import H264Encoder |
| 6 | +from aiortc.codecs.vpx import Vp8Encoder |
| 7 | + |
| 8 | +from getstream.video.rtc.encoders_patches import ( |
| 9 | + STREAM_VIDEO_DEFAULT_BITRATE, |
| 10 | + STREAM_VIDEO_MAX_BITRATE, |
| 11 | + STREAM_VIDEO_MIN_BITRATE, |
| 12 | + StreamH264Encoder, |
| 13 | + StreamVp8Encoder, |
| 14 | + patch_sender_encoder, |
| 15 | +) |
| 16 | + |
| 17 | +# Original upstream values |
| 18 | +ORIGINAL_VPX = { |
| 19 | + "DEFAULT_BITRATE": 500_000, |
| 20 | + "MIN_BITRATE": 250_000, |
| 21 | + "MAX_BITRATE": 1_500_000, |
| 22 | +} |
| 23 | +ORIGINAL_H264 = { |
| 24 | + "DEFAULT_BITRATE": 1_000_000, |
| 25 | + "MIN_BITRATE": 500_000, |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +class TestStreamVp8Encoder: |
| 30 | + def test_default_bitrate(self): |
| 31 | + enc = StreamVp8Encoder() |
| 32 | + assert enc.target_bitrate == STREAM_VIDEO_DEFAULT_BITRATE |
| 33 | + |
| 34 | + def test_clamps_above_max(self): |
| 35 | + enc = StreamVp8Encoder() |
| 36 | + enc.target_bitrate = 999_999_999 |
| 37 | + assert enc.target_bitrate == STREAM_VIDEO_MAX_BITRATE |
| 38 | + |
| 39 | + def test_clamps_below_min(self): |
| 40 | + enc = StreamVp8Encoder() |
| 41 | + enc.target_bitrate = 1 |
| 42 | + assert enc.target_bitrate == STREAM_VIDEO_MIN_BITRATE |
| 43 | + |
| 44 | + def test_accepts_in_range(self): |
| 45 | + enc = StreamVp8Encoder() |
| 46 | + enc.target_bitrate = 2_000_000 |
| 47 | + assert enc.target_bitrate == 2_000_000 |
| 48 | + |
| 49 | + |
| 50 | +class TestStreamH264Encoder: |
| 51 | + def test_default_bitrate(self): |
| 52 | + enc = StreamH264Encoder() |
| 53 | + assert enc.target_bitrate == STREAM_VIDEO_DEFAULT_BITRATE |
| 54 | + |
| 55 | + def test_clamps_above_max(self): |
| 56 | + enc = StreamH264Encoder() |
| 57 | + enc.target_bitrate = 999_999_999 |
| 58 | + assert enc.target_bitrate == STREAM_VIDEO_MAX_BITRATE |
| 59 | + |
| 60 | + def test_clamps_below_min(self): |
| 61 | + enc = StreamH264Encoder() |
| 62 | + enc.target_bitrate = 1 |
| 63 | + assert enc.target_bitrate == STREAM_VIDEO_MIN_BITRATE |
| 64 | + |
| 65 | + def test_accepts_in_range(self): |
| 66 | + enc = StreamH264Encoder() |
| 67 | + enc.target_bitrate = 2_000_000 |
| 68 | + assert enc.target_bitrate == 2_000_000 |
| 69 | + |
| 70 | + |
| 71 | +class TestBitratePatchDisabled: |
| 72 | + @pytest.mark.parametrize("env_val", [None, "", "1", "true", "yes", "on"]) |
| 73 | + def test_enabled_by_default(self, monkeypatch, env_val): |
| 74 | + """BITRATE_PATCH_DISABLED is False when env var is unset or truthy.""" |
| 75 | + if env_val is None: |
| 76 | + monkeypatch.delenv("STREAM_PATCH_AIORTC_BITRATES", raising=False) |
| 77 | + else: |
| 78 | + monkeypatch.setenv("STREAM_PATCH_AIORTC_BITRATES", env_val) |
| 79 | + |
| 80 | + import getstream.video.rtc.encoders_patches as mod |
| 81 | + |
| 82 | + importlib.reload(mod) |
| 83 | + assert mod.BITRATE_PATCH_DISABLED is False |
| 84 | + |
| 85 | + @pytest.mark.parametrize("env_val", ["0", "false", "no", "off"]) |
| 86 | + def test_disabled_via_env(self, monkeypatch, env_val): |
| 87 | + """BITRATE_PATCH_DISABLED is True when env var is a falsy value.""" |
| 88 | + monkeypatch.setenv("STREAM_PATCH_AIORTC_BITRATES", env_val) |
| 89 | + |
| 90 | + import getstream.video.rtc.encoders_patches as mod |
| 91 | + |
| 92 | + importlib.reload(mod) |
| 93 | + assert mod.BITRATE_PATCH_DISABLED is True |
| 94 | + |
| 95 | + |
| 96 | +class TestPatchSenderEncoder: |
| 97 | + @pytest.mark.asyncio |
| 98 | + async def test_installs_vp8_encoder(self): |
| 99 | + sender = MagicMock() |
| 100 | + sender._RTCRtpSender__encoder = None |
| 101 | + |
| 102 | + async def _orig_coro(codec): |
| 103 | + return None |
| 104 | + |
| 105 | + sender._next_encoded_frame = _orig_coro |
| 106 | + patch_sender_encoder(sender) |
| 107 | + |
| 108 | + codec = MagicMock() |
| 109 | + codec.mimeType = "video/VP8" |
| 110 | + await sender._next_encoded_frame(codec) |
| 111 | + |
| 112 | + from getstream.video.rtc.encoders_patches import StreamVp8Encoder as Vp8Cls |
| 113 | + |
| 114 | + assert isinstance(sender._RTCRtpSender__encoder, Vp8Cls) |
| 115 | + |
| 116 | + @pytest.mark.asyncio |
| 117 | + async def test_installs_h264_encoder(self): |
| 118 | + sender = MagicMock() |
| 119 | + sender._RTCRtpSender__encoder = None |
| 120 | + |
| 121 | + async def _orig_coro(codec): |
| 122 | + return None |
| 123 | + |
| 124 | + sender._next_encoded_frame = _orig_coro |
| 125 | + patch_sender_encoder(sender) |
| 126 | + |
| 127 | + codec = MagicMock() |
| 128 | + codec.mimeType = "video/H264" |
| 129 | + await sender._next_encoded_frame(codec) |
| 130 | + |
| 131 | + from getstream.video.rtc.encoders_patches import StreamH264Encoder as H264Cls |
| 132 | + |
| 133 | + assert isinstance(sender._RTCRtpSender__encoder, H264Cls) |
| 134 | + |
| 135 | + @pytest.mark.asyncio |
| 136 | + async def test_does_not_replace_existing_encoder(self): |
| 137 | + """Already-set encoder is not replaced.""" |
| 138 | + sender = MagicMock() |
| 139 | + existing_encoder = MagicMock() |
| 140 | + sender._RTCRtpSender__encoder = existing_encoder |
| 141 | + |
| 142 | + async def _orig_coro(codec): |
| 143 | + return None |
| 144 | + |
| 145 | + sender._next_encoded_frame = _orig_coro |
| 146 | + patch_sender_encoder(sender) |
| 147 | + |
| 148 | + codec = MagicMock() |
| 149 | + codec.mimeType = "video/H264" |
| 150 | + await sender._next_encoded_frame(codec) |
| 151 | + assert sender._RTCRtpSender__encoder is existing_encoder |
| 152 | + |
| 153 | + def test_noop_when_encoders_none(self, monkeypatch): |
| 154 | + """patch_sender_encoder is a no-op when encoder classes failed to load.""" |
| 155 | + import getstream.video.rtc.encoders_patches as mod |
| 156 | + |
| 157 | + monkeypatch.setattr(mod, "StreamVp8Encoder", None) |
| 158 | + sender = MagicMock() |
| 159 | + orig = sender._next_encoded_frame |
| 160 | + mod.patch_sender_encoder(sender) |
| 161 | + assert sender._next_encoded_frame is orig |
| 162 | + |
| 163 | + |
| 164 | +class TestUpstreamAssumptions: |
| 165 | + def test_upstream_assumptions_hold(self): |
| 166 | + """Canary: fails if aiortc changes its internal API. |
| 167 | +
|
| 168 | + Our per-sender patching relies on name-mangled attributes and |
| 169 | + the _next_encoded_frame method. If this breaks after an aiortc |
| 170 | + upgrade, review the upstream code and update accordingly. |
| 171 | + """ |
| 172 | + vp8 = Vp8Encoder() |
| 173 | + assert hasattr(vp8, "target_bitrate"), "Vp8Encoder lost target_bitrate" |
| 174 | + |
| 175 | + h264 = H264Encoder() |
| 176 | + assert hasattr(h264, "target_bitrate"), "H264Encoder lost target_bitrate" |
| 177 | + |
| 178 | + # Name-mangled __target_bitrate is accessible from subclasses |
| 179 | + assert hasattr(vp8, "_Vp8Encoder__target_bitrate"), ( |
| 180 | + "Vp8Encoder name-mangled __target_bitrate changed" |
| 181 | + ) |
| 182 | + assert hasattr(h264, "_H264Encoder__target_bitrate"), ( |
| 183 | + "H264Encoder name-mangled __target_bitrate changed" |
| 184 | + ) |
| 185 | + |
| 186 | + # RTCRtpSender has _next_encoded_frame and uses __encoder |
| 187 | + import inspect |
| 188 | + |
| 189 | + from aiortc.rtcrtpsender import RTCRtpSender |
| 190 | + |
| 191 | + assert hasattr(RTCRtpSender, "_next_encoded_frame"), ( |
| 192 | + "RTCRtpSender lost _next_encoded_frame" |
| 193 | + ) |
| 194 | + source = inspect.getsource(RTCRtpSender._next_encoded_frame) |
| 195 | + assert "self.__encoder" in source, ( |
| 196 | + "RTCRtpSender._next_encoded_frame no longer uses self.__encoder" |
| 197 | + ) |
0 commit comments