Repository navigation
Expand file tree
/
Copy pathplayback_session_test.py
More file actions
191 lines (172 loc) · 8.21 KB
/
Copy pathplayback_session_test.py
File metadata and controls
191 lines (172 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Verify the fallback changes the actual encoder configuration."""
from unittest.mock import AsyncMock, patch
import pytest
from ffmpeg_session_test import FakeProcess
import ffmpeg_session
@pytest.mark.asyncio
@pytest.mark.parametrize(
"configured_resolution,expected_resolution", [("4k", "720p"), ("480p", "480p")]
)
async def test_fallback_command_and_cleanup(tmp_path, configured_resolution, expected_resolution):
settings = {"max_resolution": configured_resolution, "quality": "high", "sr_model": "nomos"}
with (
patch("ffmpeg_session.get_settings", return_value=settings),
patch("ffmpeg_session.get_transcode_dir", return_value=tmp_path),
patch("ffmpeg_session.resolve_hls_master_playlist", side_effect=lambda url: url),
patch(
"ffmpeg_session.build_hls_ffmpeg_cmd", return_value=["ffmpeg", "-i", "stream"]
) as command,
patch(
"ffmpeg_session.asyncio.create_subprocess_exec",
new_callable=AsyncMock,
return_value=FakeProcess(),
),
patch(
"ffmpeg_session._spawn_background_task", side_effect=lambda coroutine: coroutine.close()
),
patch("ffmpeg_session._wait_for_playlist", new_callable=AsyncMock, return_value=True),
):
result = await ffmpeg_session.start_transcode(
"http://example.com/live.ts", bandwidth_saver=True
)
try:
assert command.call_args.args[6:8] == (expected_resolution, "low")
assert command.call_args.kwargs["allow_upscale"] is False
assert settings == {
"max_resolution": configured_resolution,
"quality": "high",
"sr_model": "nomos",
}
session = ffmpeg_session.get_session(result["session_id"])
assert session is not None
assert session["bandwidth_saver"] is True
finally:
ffmpeg_session.stop_session(result["session_id"], force=True)
assert not list(tmp_path.iterdir())
@pytest.mark.asyncio
async def test_fallback_does_not_reuse_high_quality_stream():
with (
patch("ffmpeg_session._get_existing_session", return_value=("old", True, 0)),
patch("ffmpeg_session.get_session", return_value={"username": "viewer"}),
patch("ffmpeg_session.stop_session") as stop,
patch("ffmpeg_session.enforce_stream_limits", return_value=None),
patch("ffmpeg_session._try_reuse_session", new_callable=AsyncMock) as reuse,
patch(
"ffmpeg_session._do_start_transcode",
new_callable=AsyncMock,
return_value={"session_id": "new"},
) as start,
):
result = await ffmpeg_session.start_transcode(
"stream", username="viewer", bandwidth_saver=True
)
assert result == {"session_id": "new"}
stop.assert_called_once_with("old", force=True)
reuse.assert_not_called()
assert start.call_args.args[-1] is True
@pytest.mark.asyncio
async def test_legacy_saver_session_can_request_recovery(tmp_path):
from playback_policy import PlaybackHealth
with (
patch("ffmpeg_session.get_settings", return_value={"max_resolution": "1080p"}),
patch("ffmpeg_session.get_transcode_dir", return_value=tmp_path),
patch("ffmpeg_session.resolve_hls_master_playlist", side_effect=lambda url: url),
patch("ffmpeg_session.build_hls_ffmpeg_cmd", return_value=["ffmpeg", "-i", "stream"]),
patch("ffmpeg_session.asyncio.create_subprocess_exec", new=AsyncMock(return_value=FakeProcess())),
patch("ffmpeg_session._spawn_background_task", side_effect=lambda coro: coro.close()),
patch("ffmpeg_session._wait_for_playlist", new=AsyncMock(return_value=True)),
):
result = await ffmpeg_session.start_transcode("http://example.com/live.ts", bandwidth_saver=True)
session_id = result["session_id"]
try:
good = PlaybackHealth(buffer_seconds=12, waiting=False, observed_bitrate=20_000_000)
with patch("ffmpeg_session.time.monotonic") as clock:
for now in range(0, 60, 2):
clock.return_value = now
assert ffmpeg_session.report_playback_health(session_id, "", good) == {"bandwidth_saver": True}
clock.return_value = 60
assert ffmpeg_session.report_playback_health(session_id, "", good) == {"bandwidth_saver": False}
finally:
ffmpeg_session.stop_session(session_id, force=True)
@pytest.mark.asyncio
async def test_aac_client_does_not_reuse_dolby_session():
"""A client without Dolby decoding replaces its own passthrough session."""
with (
patch("ffmpeg_session._get_existing_session", return_value=("old", True, 12.0)),
patch(
"ffmpeg_session.get_session",
return_value={"username": "viewer", "audio_passthrough": True},
),
patch("ffmpeg_session.stop_session") as stop,
patch("ffmpeg_session.enforce_stream_limits", return_value=None),
patch("ffmpeg_session._try_reuse_session", new_callable=AsyncMock) as reuse,
patch(
"ffmpeg_session._do_start_transcode",
new_callable=AsyncMock,
return_value={"session_id": "new"},
) as start,
):
result = await ffmpeg_session.start_transcode("stream", "movie", username="viewer")
assert result == {"session_id": "new"}
stop.assert_called_once_with("old", force=True)
reuse.assert_not_called()
assert start.call_args.args[4] == 12.0 # VOD resume position survives the restart
assert start.call_args.kwargs["audio_passthrough"] is False
@pytest.mark.asyncio
async def test_dolby_session_of_another_user_is_not_stopped():
with (
patch("ffmpeg_session._get_existing_session", return_value=("old", True, 0)),
patch(
"ffmpeg_session.get_session",
return_value={"username": "other", "audio_passthrough": True},
),
patch("ffmpeg_session.stop_session") as stop,
patch("ffmpeg_session.enforce_stream_limits", return_value=None),
pytest.raises(ffmpeg_session.HTTPException) as error,
):
await ffmpeg_session.start_transcode("stream", username="viewer")
assert error.value.status_code == 409
stop.assert_not_called()
@pytest.mark.asyncio
async def test_passthrough_client_reuses_dolby_session():
session = {"username": "viewer", "audio_passthrough": True}
with (
patch("ffmpeg_session._get_existing_session", return_value=("old", True, 0)),
patch("ffmpeg_session.get_session", return_value=session),
patch("ffmpeg_session.stop_session") as stop,
patch("ffmpeg_session.enforce_stream_limits", return_value=None),
patch(
"ffmpeg_session._try_reuse_session",
new_callable=AsyncMock,
return_value={"session_id": "old"},
),
):
result = await ffmpeg_session.start_transcode(
"stream", username="viewer", audio_passthrough=True
)
assert result == {"session_id": "old"}
stop.assert_not_called()
@pytest.mark.asyncio
async def test_starting_passthrough_session_is_not_reused_by_aac_client():
"""While the audio probe is pending, a passthrough request's session counts as Dolby."""
ffmpeg_session._transcode_sessions["pending"] = {
"username": "viewer", "fast_start": True, "audio_passthrough": True, "url": "stream",
"process": None, "dir": "/nonexistent", "last_access": 0, "started": 0,
} # fmt: skip
ffmpeg_session._url_to_session["stream"] = "pending"
try:
with (
patch("ffmpeg_session._get_existing_session", return_value=("pending", True, 0)),
patch("ffmpeg_session.stop_session") as stop,
patch("ffmpeg_session.enforce_stream_limits", return_value=None),
patch(
"ffmpeg_session._do_start_transcode",
new_callable=AsyncMock,
return_value={"session_id": "new"},
),
):
await ffmpeg_session.start_transcode("stream", username="viewer")
stop.assert_called_once_with("pending", force=True)
finally:
ffmpeg_session._transcode_sessions.pop("pending", None)
ffmpeg_session._url_to_session.pop("stream", None)