scrcpy.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import time
  2. from functools import wraps
  3. import numpy as np
  4. from adbutils.errors import AdbError
  5. import module.device.method.scrcpy.const as const
  6. from module.base.utils import random_rectangle_point
  7. from module.device.method.minitouch import insert_swipe
  8. from module.device.method.scrcpy.core import ScrcpyCore, ScrcpyError
  9. from module.device.method.uiautomator_2 import Uiautomator2
  10. from module.device.method.utils import RETRY_TRIES, retry_sleep, handle_adb_error
  11. from module.exception import RequestHumanTakeover
  12. from module.logger import logger
  13. def retry(func):
  14. @wraps(func)
  15. def retry_wrapper(self, *args, **kwargs):
  16. """
  17. Args:
  18. self (Minitouch):
  19. """
  20. init = None
  21. for _ in range(RETRY_TRIES):
  22. try:
  23. if callable(init):
  24. retry_sleep(_)
  25. init()
  26. return func(self, *args, **kwargs)
  27. # Can't handle
  28. except RequestHumanTakeover:
  29. break
  30. # When adb server was killed
  31. except ConnectionResetError as e:
  32. logger.error(e)
  33. def init():
  34. self.adb_reconnect()
  35. # Emulator closed
  36. except ConnectionAbortedError as e:
  37. logger.error(e)
  38. def init():
  39. self.adb_reconnect()
  40. # ScrcpyError
  41. except ScrcpyError as e:
  42. logger.error(e)
  43. def init():
  44. self.scrcpy_init()
  45. # AdbError
  46. except AdbError as e:
  47. if handle_adb_error(e):
  48. def init():
  49. self.adb_reconnect()
  50. else:
  51. break
  52. # Unknown, probably a trucked image
  53. except Exception as e:
  54. logger.exception(e)
  55. def init():
  56. pass
  57. logger.critical(f'Retry {func.__name__}() failed')
  58. raise RequestHumanTakeover
  59. return retry_wrapper
  60. class Scrcpy(ScrcpyCore, Uiautomator2):
  61. def _scrcpy_resolution_check(self):
  62. if not self._scrcpy_alive:
  63. with self._scrcpy_control_socket_lock:
  64. self.resolution_check_uiautomator2()
  65. @retry
  66. def screenshot_scrcpy(self):
  67. self._scrcpy_resolution_check()
  68. self.scrcpy_ensure_running()
  69. with self._scrcpy_control_socket_lock:
  70. # Wait new frame
  71. now = time.time()
  72. while 1:
  73. time.sleep(0.001)
  74. if self._scrcpy_stream_loop_thread is None or not self._scrcpy_stream_loop_thread.is_alive():
  75. raise ScrcpyError('_scrcpy_stream_loop_thread died')
  76. if self._scrcpy_last_frame_time > now:
  77. screenshot = self._scrcpy_last_frame.copy()
  78. return screenshot
  79. @retry
  80. def click_scrcpy(self, x, y):
  81. self.scrcpy_ensure_running()
  82. with self._scrcpy_control_socket_lock:
  83. self._scrcpy_control.touch(x, y, const.ACTION_DOWN)
  84. self._scrcpy_control.touch(x, y, const.ACTION_UP)
  85. self.sleep(0.05)
  86. @retry
  87. def long_click_scrcpy(self, x, y, duration=1.0):
  88. self.scrcpy_ensure_running()
  89. with self._scrcpy_control_socket_lock:
  90. self._scrcpy_control.touch(x, y, const.ACTION_DOWN)
  91. self.sleep(duration)
  92. self._scrcpy_control.touch(x, y, const.ACTION_UP)
  93. self.sleep(0.05)
  94. @retry
  95. def swipe_scrcpy(self, p1, p2):
  96. self.scrcpy_ensure_running()
  97. with self._scrcpy_control_socket_lock:
  98. # Unlike minitouch, scrcpy swipes needs to be continuous
  99. # So 5 times smother
  100. points = insert_swipe(p0=p1, p3=p2, speed=4, min_distance=2)
  101. self._scrcpy_control.touch(*p1, const.ACTION_DOWN)
  102. for point in points[1:-1]:
  103. self._scrcpy_control.touch(*point, const.ACTION_MOVE)
  104. self.sleep(0.002)
  105. self._scrcpy_control.touch(*p2, const.ACTION_MOVE)
  106. self._scrcpy_control.touch(*p2, const.ACTION_UP)
  107. self.sleep(0.05)
  108. @retry
  109. def drag_scrcpy(self, p1, p2, point_random=(-10, -10, 10, 10)):
  110. self.scrcpy_ensure_running()
  111. with self._scrcpy_control_socket_lock:
  112. p1 = np.array(p1) - random_rectangle_point(point_random)
  113. p2 = np.array(p2) - random_rectangle_point(point_random)
  114. points = insert_swipe(p0=p1, p3=p2, speed=4, min_distance=2)
  115. self._scrcpy_control.touch(*p1, const.ACTION_DOWN)
  116. for point in points[1:-1]:
  117. self._scrcpy_control.touch(*point, const.ACTION_MOVE)
  118. self.sleep(0.002)
  119. # Hold 280ms
  120. for _ in range(int(0.14 // 0.002) * 2):
  121. self._scrcpy_control.touch(*p2, const.ACTION_MOVE)
  122. self.sleep(0.002)
  123. self._scrcpy_control.touch(*p2, const.ACTION_UP)
  124. self.sleep(0.05)