test_video_screens.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Video wallpapers on virtual screens: the same video on several outputs, different videos on different outputs, and
  2. always a third output that shows a copy of one of the others. A video that several outputs show is decoded once (the
  3. engine logs which outputs share a decoder), whatever the outputs show has to be the right video."""
  4. import json
  5. import shutil
  6. import subprocess
  7. import time
  8. import pytest
  9. from PIL import Image
  10. RED, BLUE = (255, 0, 0), (0, 0, 255)
  11. SIZE = (1280, 720)
  12. @pytest.fixture
  13. def sway(desktop_factory):
  14. return desktop_factory("sway-dual")
  15. def _swaymsg(sway, *command):
  16. result = sway.run(["swaymsg", *command], sway.extra)
  17. assert result.returncode == 0, result.stderr or result.stdout
  18. return result.stdout
  19. @pytest.fixture
  20. def three_outputs(sway):
  21. """HEADLESS-1..3 side by side, the third one made here and removed again"""
  22. names = json.loads(_swaymsg(sway, "-t", "get_outputs", "-r"))
  23. created = len(names) < 3
  24. if created:
  25. _swaymsg(sway, "create_output")
  26. outputs = [output["name"] for output in json.loads(_swaymsg(sway, "-t", "get_outputs", "-r"))]
  27. assert len(outputs) >= 3, outputs
  28. outputs = sorted(outputs)[:3]
  29. for index, name in enumerate(outputs):
  30. _swaymsg(sway, f"output {name} enable mode {SIZE[0]}x{SIZE[1]} pos {index * SIZE[0]} 0 scale 1")
  31. yield outputs
  32. if created:
  33. _swaymsg(sway, f"output {outputs[2]} unplug")
  34. def _video(directory, color):
  35. """a project of one solid colour video that loops"""
  36. if shutil.which("ffmpeg") is None:
  37. pytest.skip("ffmpeg is needed to make the test videos")
  38. directory.mkdir(parents=True, exist_ok=True)
  39. made = subprocess.run(
  40. ["ffmpeg", "-y", "-loglevel", "error", "-f", "lavfi", "-i",
  41. f"color=c=0x{color[0]:02x}{color[1]:02x}{color[2]:02x}:s=320x180:r=30:d=3", "-pix_fmt", "yuv420p",
  42. "-c:v", "libx264", str(directory / "video.mp4")], capture_output=True)
  43. if made.returncode != 0:
  44. pytest.skip("ffmpeg cannot encode H.264 here: " + made.stderr.decode(errors="replace")[:200])
  45. (directory / "project.json").write_text(json.dumps({"file": "video.mp4", "type": "video", "title": "solid"}))
  46. return directory
  47. def _engine(sway, engine, log, outputs, wallpapers):
  48. argv = [engine["binary"], "--assets-dir", engine["assets"], "--silent", "--fps", "30"]
  49. for output, wallpaper in zip(outputs, wallpapers):
  50. argv += ["--screen-root", output, "--bg", str(wallpaper)]
  51. return sway.client(argv, log=log)
  52. def _colors(sway, tmp_path, outputs):
  53. path = tmp_path / "shot.png"
  54. sway.shot(path)
  55. image = Image.open(path).convert("RGB")
  56. rects = {output["name"]: output["rect"] for output in json.loads(_swaymsg(sway, "-t", "get_outputs", "-r"))
  57. if output.get("active", True)}
  58. left = min(rect["x"] for rect in rects.values())
  59. top = min(rect["y"] for rect in rects.values())
  60. return [image.getpixel((rects[name]["x"] - left + SIZE[0] // 2, rects[name]["y"] - top + SIZE[1] // 2))
  61. for name in outputs]
  62. def _near(color, expected, tolerance=48):
  63. return all(abs(a - b) <= tolerance for a, b in zip(color, expected))
  64. def _wait_for(sway, tmp_path, process, outputs, expected):
  65. seen = []
  66. for _ in range(30):
  67. assert process.poll() is None, f"the engine exited with {process.returncode}"
  68. seen = _colors(sway, tmp_path, outputs)
  69. if all(_near(color, want) for color, want in zip(seen, expected)):
  70. return
  71. time.sleep(1)
  72. pytest.fail(f"the outputs show {seen}, expected {expected}")
  73. def _shared(log):
  74. return [line for line in log.read_text(errors="replace").splitlines() if "shares the decoder" in line]
  75. def test_same_video_on_two_outputs_and_a_copy_on_the_third(sway, engine, tmp_path, three_outputs):
  76. red = _video(tmp_path / "red", RED)
  77. log = tmp_path / "engine.log"
  78. process = _engine(sway, engine, log, three_outputs, [red, red, red])
  79. try:
  80. _wait_for(sway, tmp_path, process, three_outputs, [RED, RED, RED])
  81. assert len(_shared(log)) == 2, log.read_text(errors="replace")
  82. time.sleep(3)
  83. _wait_for(sway, tmp_path, process, three_outputs, [RED, RED, RED])
  84. finally:
  85. process.terminate()
  86. process.wait(10)
  87. def test_different_videos_and_a_copy_of_the_second_on_the_third(sway, engine, tmp_path, three_outputs):
  88. red = _video(tmp_path / "red", RED)
  89. blue = _video(tmp_path / "blue", BLUE)
  90. log = tmp_path / "engine.log"
  91. process = _engine(sway, engine, log, three_outputs, [red, blue, blue])
  92. try:
  93. _wait_for(sway, tmp_path, process, three_outputs, [RED, BLUE, BLUE])
  94. assert len(_shared(log)) == 1, log.read_text(errors="replace")
  95. finally:
  96. process.terminate()
  97. process.wait(10)
  98. def test_different_videos_and_a_copy_of_the_first_on_the_third(sway, engine, tmp_path, three_outputs):
  99. red = _video(tmp_path / "red", RED)
  100. blue = _video(tmp_path / "blue", BLUE)
  101. log = tmp_path / "engine.log"
  102. process = _engine(sway, engine, log, three_outputs, [red, blue, red])
  103. try:
  104. _wait_for(sway, tmp_path, process, three_outputs, [RED, BLUE, RED])
  105. assert len(_shared(log)) == 1, log.read_text(errors="replace")
  106. finally:
  107. process.terminate()
  108. process.wait(10)
  109. def test_a_shared_video_survives_its_first_output_going_away(sway, engine, tmp_path, three_outputs):
  110. red = _video(tmp_path / "red", RED)
  111. log = tmp_path / "engine.log"
  112. process = _engine(sway, engine, log, three_outputs, [red, red, red])
  113. try:
  114. _wait_for(sway, tmp_path, process, three_outputs, [RED, RED, RED])
  115. _swaymsg(sway, f"output {three_outputs[0]} disable")
  116. time.sleep(4)
  117. assert process.poll() is None, f"the engine exited with {process.returncode}"
  118. _wait_for(sway, tmp_path, process, three_outputs[1:], [RED, RED])
  119. finally:
  120. process.terminate()
  121. process.wait(10)