scrcpy.py 5.2 KB

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