-
-
Notifications
You must be signed in to change notification settings - Fork 723
Expand file tree
/
Copy pathtest_media_player.py
More file actions
691 lines (563 loc) · 22.6 KB
/
Copy pathtest_media_player.py
File metadata and controls
691 lines (563 loc) · 22.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
import logging
import subprocess
from collections.abc import Iterator
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from anthias_viewer.media_player import (
MPVMediaPlayer,
MediaPlayerProxy,
VLCMediaPlayer,
get_alsa_audio_device,
)
logging.disable(logging.CRITICAL)
class _MPVFixtures:
player: MPVMediaPlayer
mock_settings: Any
@pytest.fixture
def mpv() -> Iterator[_MPVFixtures]:
fixtures = _MPVFixtures()
fixtures.player = MPVMediaPlayer()
patch_settings = patch('anthias_viewer.media_player.settings')
patch_device_type = patch(
'anthias_viewer.media_player.get_device_type', return_value='pi4'
)
fixtures.mock_settings = patch_settings.start()
fixtures.mock_settings.__getitem__.return_value = 'hdmi'
patch_device_type.start()
try:
yield fixtures
finally:
patch_settings.stop()
patch_device_type.stop()
@patch(
'anthias_viewer.media_player._detect_hdmi_audio_device',
return_value='sysdefault:CARD=vc4hdmi0',
)
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_invokes_popen_with_expected_args_on_pi4_64(
mock_popen: Any, _mock_detect: Any, mpv: _MPVFixtures
) -> None:
mpv.player.set_asset('file:///test/video.mp4', 30)
with patch.dict('os.environ', {'DEVICE_TYPE': 'pi4-64'}):
mpv.player.play()
mock_popen.assert_called_once_with(
[
'mpv',
'--no-terminal',
'--vo=gpu',
'--gpu-context=drm',
'--hwdec=auto-safe',
'--vd-lavc-threads=4',
'--audio-device=alsa/sysdefault:CARD=vc4hdmi0',
'--',
'file:///test/video.mp4',
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_tunes_decoder_threads_on_pi4_64(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mpv.player.set_asset('file:///test/video.mp4', 30)
with patch.dict('os.environ', {'DEVICE_TYPE': 'pi4-64'}):
mpv.player.play()
args, _ = mock_popen.call_args
assert '--vd-lavc-threads=4' in args[0]
assert '--hwdec=auto-safe' in args[0]
assert '--hwdec=v4l2m2m-copy' not in args[0]
# --drm-mode pinning was used on the legacy --vo=drm path to dodge
# CPU zimg upscale at 4K; under --gpu-context=drm the V3D handles
# scaling, the pin is no longer needed, and it actively hurts
# throughput when combined with GBM (verified on Pi4 hardware).
assert '--drm-mode=1920x1080@60' not in args[0]
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_tunes_decoder_threads_on_pi5(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mpv.player.set_asset('file:///test/video.mp4', 30)
with patch.dict('os.environ', {'DEVICE_TYPE': 'pi5'}):
mpv.player.play()
args, _ = mock_popen.call_args
assert '--vd-lavc-threads=4' in args[0]
assert '--drm-mode=1920x1080@60' not in args[0]
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_omits_pi_tuning_on_x86(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mpv.player.set_asset('file:///test/video.mp4', 30)
with patch.dict('os.environ', {'DEVICE_TYPE': 'x86'}):
mpv.player.play()
args, _ = mock_popen.call_args
assert '--drm-mode=1920x1080@60' not in args[0]
assert '--vd-lavc-threads=4' not in args[0]
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_uses_wayland_vo_on_x86(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mpv.player.set_asset('file:///test/video.mp4', 30)
with patch.dict('os.environ', {'DEVICE_TYPE': 'x86'}):
mpv.player.play()
args, _ = mock_popen.call_args
assert '--vo=gpu' in args[0]
assert '--gpu-context=wayland' in args[0]
assert '--vo=drm' not in args[0]
@pytest.mark.parametrize('device_type', ['pi4-64', 'pi5'])
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_uses_drm_gpu_context_on_pi4_64_and_pi5(
mock_popen: Any, mpv: _MPVFixtures, device_type: str
) -> None:
# Pi4-64 and Pi5 own the framebuffer directly (no compositor)
# and use mpv's GL VO with --gpu-context=drm. This offloads the
# 1080p->4K upscale to the V3D instead of the A72/A76 CPU zimg
# path that --vo=drm took.
mpv.player.set_asset('file:///test/video.mp4', 30)
with patch.dict('os.environ', {'DEVICE_TYPE': device_type}):
mpv.player.play()
args, _ = mock_popen.call_args
assert '--vo=gpu' in args[0]
assert '--gpu-context=drm' in args[0]
assert '--vo=drm' not in args[0]
assert '--gpu-context=wayland' not in args[0]
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_uses_local_audio_device_when_configured(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mpv.mock_settings.__getitem__.return_value = 'local'
mpv.player.set_asset('file:///test/video.mp4', 30)
mpv.player.play()
args, _ = mock_popen.call_args
assert '--audio-device=alsa/plughw:CARD=Headphones' in args[0]
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_play_reloads_settings_each_call(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mpv.player.set_asset('file:///test/video.mp4', 30)
mpv.player.play()
mpv.mock_settings.load.assert_called_once()
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_is_playing_returns_true_when_process_running(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mock_process = MagicMock()
mock_process.poll.return_value = None
mock_popen.return_value = mock_process
mpv.player.set_asset('file:///test/video.mp4', 30)
mpv.player.play()
assert mpv.player.is_playing()
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_is_playing_returns_false_when_process_finished(
mock_popen: Any, mpv: _MPVFixtures
) -> None:
mock_process = MagicMock()
mock_process.poll.return_value = 0
mock_popen.return_value = mock_process
mpv.player.set_asset('file:///test/video.mp4', 30)
mpv.player.play()
assert not mpv.player.is_playing()
def test_is_playing_returns_false_when_no_process(mpv: _MPVFixtures) -> None:
assert not mpv.player.is_playing()
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_stop_terminates_process(mock_popen: Any, mpv: _MPVFixtures) -> None:
mock_process = MagicMock()
mock_popen.return_value = mock_process
mpv.player.set_asset('file:///test/video.mp4', 30)
mpv.player.play()
mpv.player.stop()
mock_process.terminate.assert_called_once()
assert mpv.player.process is None
@pytest.fixture
def alsa_settings() -> Iterator[Any]:
patch_settings = patch('anthias_viewer.media_player.settings')
mock_settings = patch_settings.start()
try:
yield mock_settings
finally:
patch_settings.stop()
def test_local_on_pi5_uses_detected_hdmi_device(alsa_settings: Any) -> None:
alsa_settings.__getitem__.return_value = 'local'
with (
patch(
'anthias_viewer.media_player.get_device_type', return_value='pi5'
),
patch(
'anthias_viewer.media_player._detect_hdmi_audio_device',
return_value='sysdefault:CARD=vc4hdmi1',
) as mock_detect,
):
assert get_alsa_audio_device() == 'sysdefault:CARD=vc4hdmi1'
mock_detect.assert_called_once()
@pytest.mark.parametrize('device_type', ['pi1', 'pi2', 'pi3', 'pi4'])
def test_local_on_other_pi_uses_headphones(
alsa_settings: Any, device_type: str
) -> None:
alsa_settings.__getitem__.return_value = 'local'
with patch(
'anthias_viewer.media_player.get_device_type',
return_value=device_type,
):
assert get_alsa_audio_device() == 'plughw:CARD=Headphones'
@pytest.mark.parametrize('device_type', ['pi4', 'pi5'])
def test_hdmi_on_pi4_pi5_uses_detected_device(
alsa_settings: Any, device_type: str
) -> None:
alsa_settings.__getitem__.return_value = 'hdmi'
with (
patch(
'anthias_viewer.media_player.get_device_type',
return_value=device_type,
),
patch(
'anthias_viewer.media_player._detect_hdmi_audio_device',
return_value='sysdefault:CARD=vc4hdmi1',
) as mock_detect,
):
assert get_alsa_audio_device() == 'sysdefault:CARD=vc4hdmi1'
mock_detect.assert_called_once()
@pytest.mark.parametrize('device_type', ['pi1', 'pi2', 'pi3'])
def test_hdmi_on_pi1_pi2_pi3_uses_vc4hdmi(
alsa_settings: Any, device_type: str
) -> None:
alsa_settings.__getitem__.return_value = 'hdmi'
with patch(
'anthias_viewer.media_player.get_device_type',
return_value=device_type,
):
assert get_alsa_audio_device() == 'sysdefault:CARD=vc4hdmi'
def test_hdmi_on_x86_falls_back_to_hid(alsa_settings: Any) -> None:
alsa_settings.__getitem__.return_value = 'hdmi'
with patch(
'anthias_viewer.media_player.get_device_type', return_value='x86'
):
assert get_alsa_audio_device() == 'sysdefault:CARD=HID'
class _FakeDirEntry:
"""Minimal os.DirEntry stand-in for scandir tests."""
def __init__(self, name: str, base: str = '/sys/class/drm') -> None:
self.name = name
self.path = f'{base}/{name}'
def _patch_drm(entries: list[str], statuses: dict[str, str]) -> Any:
"""Build patches that fake os.scandir() + open() for status reads."""
from io import StringIO
fake_entries = [_FakeDirEntry(n) for n in entries]
def fake_open(path: str, *args: Any, **kwargs: Any) -> Any:
if path in statuses:
return StringIO(statuses[path])
raise FileNotFoundError(path)
return (
patch(
'anthias_viewer.media_player.os.scandir', return_value=fake_entries
),
patch('builtins.open', side_effect=fake_open),
)
def test_detect_hdmi_returns_first_connected_port() -> None:
from anthias_viewer.media_player import _detect_hdmi_audio_device
scandir_patch, open_patch = _patch_drm(
entries=['card0', 'card1', 'card1-HDMI-A-1', 'card1-HDMI-A-2'],
statuses={
'/sys/class/drm/card1-HDMI-A-1/status': 'connected\n',
'/sys/class/drm/card1-HDMI-A-2/status': 'disconnected\n',
},
)
with scandir_patch, open_patch:
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi0'
def test_detect_hdmi_prefers_first_port_when_both_connected() -> None:
from anthias_viewer.media_player import _detect_hdmi_audio_device
scandir_patch, open_patch = _patch_drm(
entries=['card1-HDMI-A-1', 'card1-HDMI-A-2'],
statuses={
'/sys/class/drm/card1-HDMI-A-1/status': 'connected\n',
'/sys/class/drm/card1-HDMI-A-2/status': 'connected\n',
},
)
with scandir_patch, open_patch:
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi0'
def test_detect_hdmi_prefers_hdmi_a_1_across_mixed_card_indices() -> None:
"""If card0 hosts HDMI-A-2 and card1 hosts HDMI-A-1, HDMI-A-1 still wins.
Guards against accidentally sorting on the full entry name
(card0-... < card1-...) instead of the HDMI-A-N suffix.
"""
from anthias_viewer.media_player import _detect_hdmi_audio_device
scandir_patch, open_patch = _patch_drm(
entries=['card0-HDMI-A-2', 'card1-HDMI-A-1'],
statuses={
'/sys/class/drm/card0-HDMI-A-2/status': 'connected\n',
'/sys/class/drm/card1-HDMI-A-1/status': 'connected\n',
},
)
with scandir_patch, open_patch:
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi0'
def test_detect_hdmi_returns_second_port_when_only_it_is_connected() -> None:
from anthias_viewer.media_player import _detect_hdmi_audio_device
scandir_patch, open_patch = _patch_drm(
entries=['card1-HDMI-A-1', 'card1-HDMI-A-2'],
statuses={
'/sys/class/drm/card1-HDMI-A-1/status': 'disconnected\n',
'/sys/class/drm/card1-HDMI-A-2/status': 'connected\n',
},
)
with scandir_patch, open_patch:
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi1'
def test_detect_hdmi_discovers_non_card1_layouts() -> None:
"""DRM card index is probe-order-dependent; HDMI-A-N mapping is stable."""
from anthias_viewer.media_player import _detect_hdmi_audio_device
scandir_patch, open_patch = _patch_drm(
entries=['card2-HDMI-A-1', 'card2-HDMI-A-2'],
statuses={
'/sys/class/drm/card2-HDMI-A-1/status': 'disconnected\n',
'/sys/class/drm/card2-HDMI-A-2/status': 'connected\n',
},
)
with scandir_patch, open_patch:
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi1'
def test_detect_hdmi_falls_back_when_no_status_files() -> None:
from anthias_viewer.media_player import _detect_hdmi_audio_device
with patch('anthias_viewer.media_player.os.scandir', return_value=[]):
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi0'
def test_detect_hdmi_logs_only_on_transitions() -> None:
"""Repeated identical results log at DEBUG; transitions re-log loudly.
Guards against log spam since the helper runs on every play()/
set_asset(). Manipulates the module-level cache directly so the
test is independent of state left by other tests.
"""
import anthias_viewer.media_player as mp
saved = mp._last_detected_device
mp._last_detected_device = None
try:
# 1. Three identical "no HDMI" calls — WARN once, DEBUG twice.
with (
patch('anthias_viewer.media_player.os.scandir', return_value=[]),
patch('anthias_viewer.media_player.logging.warning') as mock_warn,
patch('anthias_viewer.media_player.logging.debug') as mock_debug,
):
for _ in range(3):
assert (
mp._detect_hdmi_audio_device()
== 'sysdefault:CARD=vc4hdmi0'
)
assert mock_warn.call_count == 1
fallback_debugs = [
c
for c in mock_debug.call_args_list
if 'falling back' in (c.args[0] if c.args else '')
]
assert len(fallback_debugs) == 2
# 2. Transition: HDMI-A-2 comes online — should re-log at INFO.
scandir_patch, open_patch = _patch_drm(
entries=['card1-HDMI-A-1', 'card1-HDMI-A-2'],
statuses={
'/sys/class/drm/card1-HDMI-A-1/status': 'disconnected\n',
'/sys/class/drm/card1-HDMI-A-2/status': 'connected\n',
},
)
with (
scandir_patch,
open_patch,
patch('anthias_viewer.media_player.logging.info') as mock_info,
patch('anthias_viewer.media_player.logging.debug') as mock_debug,
):
assert mp._detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi1'
assert mp._detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi1'
assert mock_info.call_count == 1
success_debugs = [
c
for c in mock_debug.call_args_list
if 'Detected connected HDMI' in (c.args[0] if c.args else '')
]
assert len(success_debugs) == 1
# 3. Cable yanked: back to fallback — WARN re-fires on transition.
with (
patch('anthias_viewer.media_player.os.scandir', return_value=[]),
patch('anthias_viewer.media_player.logging.warning') as mock_warn,
):
assert mp._detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi0'
assert mock_warn.call_count == 1
finally:
mp._last_detected_device = saved
def test_detect_hdmi_falls_back_on_oserror() -> None:
from anthias_viewer.media_player import _detect_hdmi_audio_device
scandir_patch, _ = _patch_drm(
entries=['card1-HDMI-A-1', 'card1-HDMI-A-2'],
statuses={},
)
with (
scandir_patch,
patch('builtins.open', side_effect=OSError('boom')),
):
assert _detect_hdmi_audio_device() == 'sysdefault:CARD=vc4hdmi0'
class _VLCFixtures:
player: VLCMediaPlayer
mock_media: Any
mock_vlc_player: Any
mock_settings: Any
@pytest.fixture
def vlc() -> Iterator[_VLCFixtures]:
fixtures = _VLCFixtures()
with patch.object(VLCMediaPlayer, '__init__', return_value=None):
fixtures.player = VLCMediaPlayer()
fixtures.mock_media = MagicMock()
fixtures.mock_vlc_player = MagicMock()
fixtures.mock_vlc_player.get_media.return_value = fixtures.mock_media
fixtures.player.player = fixtures.mock_vlc_player
patch_settings = patch('anthias_viewer.media_player.settings')
patch_device_type = patch(
'anthias_viewer.media_player.get_device_type', return_value='pi4'
)
fixtures.mock_settings = patch_settings.start()
fixtures.mock_settings.__getitem__.return_value = 'hdmi'
patch_device_type.start()
try:
yield fixtures
finally:
patch_settings.stop()
patch_device_type.stop()
def test_set_asset_invokes_parse(vlc: _VLCFixtures) -> None:
vlc.player.set_asset('file:///test/video.mp4', 30)
vlc.mock_vlc_player.get_media.assert_called_once()
vlc.mock_media.parse.assert_called_once()
@pytest.fixture
def reset_media_proxy() -> Iterator[None]:
MediaPlayerProxy.INSTANCE = None
try:
yield
finally:
MediaPlayerProxy.INSTANCE = None
@pytest.mark.parametrize('device_type', ['pi1', 'pi2', 'pi3', 'pi4'])
def test_get_instance_returns_vlc_for_pi_devices(
reset_media_proxy: None, device_type: str
) -> None:
MediaPlayerProxy.INSTANCE = None
with (
patch(
'anthias_viewer.media_player.get_device_type',
return_value=device_type,
),
patch.dict('os.environ', {'DEVICE_TYPE': device_type}),
):
with patch.object(VLCMediaPlayer, '__init__', return_value=None):
instance = MediaPlayerProxy.get_instance()
assert isinstance(instance, VLCMediaPlayer)
@pytest.mark.parametrize('device_type', ['pi5', 'x86'])
def test_get_instance_returns_mpv_for_pi5_and_x86(
reset_media_proxy: None, device_type: str
) -> None:
MediaPlayerProxy.INSTANCE = None
with patch(
'anthias_viewer.media_player.get_device_type',
return_value=device_type,
):
instance = MediaPlayerProxy.get_instance()
assert isinstance(instance, MPVMediaPlayer)
def test_get_instance_returns_mpv_for_pi4_64(reset_media_proxy: None) -> None:
MediaPlayerProxy.INSTANCE = None
with (
patch(
'anthias_viewer.media_player.get_device_type', return_value='pi4'
),
patch.dict('os.environ', {'DEVICE_TYPE': 'pi4-64'}),
):
instance = MediaPlayerProxy.get_instance()
assert isinstance(instance, MPVMediaPlayer)
@patch('anthias_viewer.media_player.get_device_type', return_value='pi5')
def test_get_instance_returns_same_instance(
_: Any, reset_media_proxy: None
) -> None:
instance1 = MediaPlayerProxy.get_instance()
instance2 = MediaPlayerProxy.get_instance()
assert instance1 is instance2
# ---------------------------------------------------------------------------
# Screen rotation (issue #2856)
def _rotated_mpv_settings(rotation: int) -> Any:
"""Build a settings mock that answers `audio_output` like the
default fixture but also surfaces `screen_rotation`. Used by the
mpv rotation tests below."""
table = {'audio_output': 'hdmi', 'screen_rotation': rotation}
mock = MagicMock()
mock.__getitem__.side_effect = lambda key: table[key]
return mock
@patch(
'anthias_viewer.media_player._detect_hdmi_audio_device',
return_value='sysdefault:CARD=vc4hdmi0',
)
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_mpv_passes_video_rotate_on_pi(
mock_popen: Any, _mock_detect: Any
) -> None:
"""On --vo=drm the framebuffer is written direct; rotation has to
happen inside mpv."""
player = MPVMediaPlayer()
with (
patch(
'anthias_viewer.media_player.settings',
_rotated_mpv_settings(180),
),
patch(
'anthias_viewer.media_player.get_device_type',
return_value='pi5',
),
patch.dict('os.environ', {'DEVICE_TYPE': 'pi5'}),
):
player.set_asset('file:///test/video.mp4', 30)
player.play()
args, _ = mock_popen.call_args
assert '--video-rotate=180' in args[0]
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_mpv_skips_video_rotate_on_x86(mock_popen: Any) -> None:
"""x86 inherits the compositor transform via wayland; double-
rotating in mpv would undo wlr-randr's work."""
player = MPVMediaPlayer()
# Patch get_device_type to 'x86' so get_alsa_audio_device() takes
# the HID-card fallback branch — patching it to 'pi5' while
# DEVICE_TYPE=x86 would route through _detect_hdmi_audio_device()
# and stat /sys/class/drm, making the test depend on the host.
with (
patch(
'anthias_viewer.media_player.settings',
_rotated_mpv_settings(90),
),
patch(
'anthias_viewer.media_player.get_device_type',
return_value='x86',
),
patch.dict('os.environ', {'DEVICE_TYPE': 'x86'}),
):
player.set_asset('file:///test/video.mp4', 30)
player.play()
args, _ = mock_popen.call_args
assert not any(arg.startswith('--video-rotate') for arg in args[0])
@patch(
'anthias_viewer.media_player._detect_hdmi_audio_device',
return_value='sysdefault:CARD=vc4hdmi0',
)
@patch('anthias_viewer.media_player.subprocess.Popen')
def test_mpv_no_video_rotate_at_zero(
mock_popen: Any, _mock_detect: Any
) -> None:
"""The default-orientation case must NOT add --video-rotate=0 —
keeps the CLI surface unchanged for the 99% of operators who never
touch the dropdown, matching the existing arg-list test."""
player = MPVMediaPlayer()
with (
patch(
'anthias_viewer.media_player.settings',
_rotated_mpv_settings(0),
),
patch(
'anthias_viewer.media_player.get_device_type',
return_value='pi5',
),
patch.dict('os.environ', {'DEVICE_TYPE': 'pi5'}),
):
player.set_asset('file:///test/video.mp4', 30)
player.play()
args, _ = mock_popen.call_args
assert not any(arg.startswith('--video-rotate') for arg in args[0])
def test_proxy_reset_clears_cached_instance(reset_media_proxy: None) -> None:
"""When the operator changes rotation in Settings, the viewer
calls MediaPlayerProxy.reset() so the next play() rebuilds VLC
with the new transform-filter options."""
fake = MagicMock()
MediaPlayerProxy.INSTANCE = fake
MediaPlayerProxy.reset()
assert MediaPlayerProxy.INSTANCE is None
fake.stop.assert_called_once()