kotonebot 0.5.0__py3-none-any.whl → 0.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. kotonebot/__init__.py +39 -39
  2. kotonebot/backend/bot.py +312 -312
  3. kotonebot/backend/color.py +525 -525
  4. kotonebot/backend/context/__init__.py +3 -3
  5. kotonebot/backend/context/context.py +1002 -1002
  6. kotonebot/backend/context/task_action.py +183 -183
  7. kotonebot/backend/core.py +86 -129
  8. kotonebot/backend/debug/entry.py +89 -89
  9. kotonebot/backend/debug/mock.py +78 -78
  10. kotonebot/backend/debug/server.py +222 -222
  11. kotonebot/backend/debug/vars.py +351 -351
  12. kotonebot/backend/dispatch.py +227 -227
  13. kotonebot/backend/flow_controller.py +196 -196
  14. kotonebot/backend/image.py +36 -5
  15. kotonebot/backend/loop.py +222 -208
  16. kotonebot/backend/ocr.py +535 -535
  17. kotonebot/backend/preprocessor.py +103 -103
  18. kotonebot/client/__init__.py +9 -9
  19. kotonebot/client/device.py +369 -529
  20. kotonebot/client/fast_screenshot.py +377 -377
  21. kotonebot/client/host/__init__.py +43 -43
  22. kotonebot/client/host/adb_common.py +101 -107
  23. kotonebot/client/host/custom.py +118 -118
  24. kotonebot/client/host/leidian_host.py +196 -196
  25. kotonebot/client/host/mumu12_host.py +353 -353
  26. kotonebot/client/host/protocol.py +214 -214
  27. kotonebot/client/host/windows_common.py +73 -58
  28. kotonebot/client/implements/__init__.py +65 -70
  29. kotonebot/client/implements/adb.py +89 -89
  30. kotonebot/client/implements/nemu_ipc/__init__.py +11 -11
  31. kotonebot/client/implements/nemu_ipc/external_renderer_ipc.py +284 -284
  32. kotonebot/client/implements/nemu_ipc/nemu_ipc.py +327 -327
  33. kotonebot/client/implements/remote_windows.py +188 -188
  34. kotonebot/client/implements/uiautomator2.py +85 -85
  35. kotonebot/client/implements/windows/__init__.py +1 -0
  36. kotonebot/client/implements/windows/print_window.py +133 -0
  37. kotonebot/client/implements/windows/send_message.py +324 -0
  38. kotonebot/client/implements/{windows.py → windows/windows.py} +175 -176
  39. kotonebot/client/protocol.py +69 -69
  40. kotonebot/client/registration.py +24 -24
  41. kotonebot/client/scaler.py +467 -0
  42. kotonebot/config/base_config.py +103 -96
  43. kotonebot/config/config.py +61 -0
  44. kotonebot/config/manager.py +36 -36
  45. kotonebot/core/__init__.py +13 -0
  46. kotonebot/core/entities/base.py +182 -0
  47. kotonebot/core/entities/compound.py +75 -0
  48. kotonebot/core/entities/ocr.py +117 -0
  49. kotonebot/core/entities/template_match.py +198 -0
  50. kotonebot/devtools/__init__.py +42 -0
  51. kotonebot/devtools/cli/__init__.py +6 -0
  52. kotonebot/devtools/cli/main.py +53 -0
  53. kotonebot/{tools → devtools}/mirror.py +354 -354
  54. kotonebot/devtools/project/project.py +41 -0
  55. kotonebot/devtools/project/scanner.py +202 -0
  56. kotonebot/devtools/project/schema.py +99 -0
  57. kotonebot/devtools/resgen/__init__.py +42 -0
  58. kotonebot/devtools/resgen/codegen.py +331 -0
  59. kotonebot/devtools/resgen/core.py +94 -0
  60. kotonebot/devtools/resgen/parsers.py +360 -0
  61. kotonebot/devtools/resgen/utils.py +158 -0
  62. kotonebot/devtools/resgen/validation.py +115 -0
  63. kotonebot/devtools/web/dist/assets/bootstrap-icons-BOrJxbIo.woff +0 -0
  64. kotonebot/devtools/web/dist/assets/bootstrap-icons-BtvjY1KL.woff2 +0 -0
  65. kotonebot/devtools/web/dist/assets/ext-language_tools-CD021WJ2.js +2577 -0
  66. kotonebot/devtools/web/dist/assets/index-B_m5f2LF.js +2836 -0
  67. kotonebot/devtools/web/dist/assets/index-BlEDyGGa.css +9 -0
  68. kotonebot/devtools/web/dist/assets/language-client-C9muzqaq.js +128 -0
  69. kotonebot/devtools/web/dist/assets/mode-python-CtHp76XS.js +476 -0
  70. kotonebot/devtools/web/dist/icons/symbol-class.svg +3 -0
  71. kotonebot/devtools/web/dist/icons/symbol-file.svg +3 -0
  72. kotonebot/devtools/web/dist/icons/symbol-method.svg +3 -0
  73. kotonebot/devtools/web/dist/index.html +25 -0
  74. kotonebot/devtools/web/server/__init__.py +0 -0
  75. kotonebot/devtools/web/server/rest_api.py +217 -0
  76. kotonebot/devtools/web/server/server.py +85 -0
  77. kotonebot/errors.py +76 -76
  78. kotonebot/interop/win/__init__.py +13 -9
  79. kotonebot/interop/win/_mouse.py +310 -310
  80. kotonebot/interop/win/message_box.py +313 -313
  81. kotonebot/interop/win/reg.py +37 -37
  82. kotonebot/interop/win/shake_mouse.py +224 -0
  83. kotonebot/interop/win/shortcut.py +43 -43
  84. kotonebot/interop/win/task_dialog.py +513 -513
  85. kotonebot/interop/win/window.py +89 -0
  86. kotonebot/logging/__init__.py +2 -2
  87. kotonebot/logging/log.py +17 -17
  88. kotonebot/primitives/__init__.py +19 -17
  89. kotonebot/primitives/geometry.py +1067 -862
  90. kotonebot/primitives/visual.py +143 -63
  91. kotonebot/ui/file_host/sensio.py +36 -36
  92. kotonebot/ui/file_host/tmp_send.py +54 -54
  93. kotonebot/ui/pushkit/__init__.py +3 -3
  94. kotonebot/ui/pushkit/image_host.py +88 -88
  95. kotonebot/ui/pushkit/protocol.py +13 -13
  96. kotonebot/ui/pushkit/wxpusher.py +54 -54
  97. kotonebot/ui/user.py +148 -148
  98. kotonebot/util.py +436 -436
  99. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/METADATA +84 -82
  100. kotonebot-0.7.0.dist-info/RECORD +109 -0
  101. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/WHEEL +1 -1
  102. kotonebot-0.7.0.dist-info/entry_points.txt +2 -0
  103. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/licenses/LICENSE +673 -673
  104. kotonebot/client/implements/adb_raw.py +0 -163
  105. kotonebot-0.5.0.dist-info/RECORD +0 -71
  106. /kotonebot/{tools → devtools/project}/__init__.py +0 -0
  107. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/top_level.txt +0 -0
@@ -1,378 +1,378 @@
1
- # type: ignore
2
- # source: https://github.com/hansalemaos/adbnativeblitz
3
- # license: MIT
4
- # requires: av
5
-
6
- import base64
7
- import ctypes
8
- import os
9
- import platform
10
- import signal
11
- import subprocess
12
- import sys
13
- import threading
14
- from collections import deque
15
- from functools import cache
16
- import av
17
- from time import sleep as sleep_
18
- from math import floor
19
-
20
-
21
- def sleep(secs):
22
- try:
23
- if secs == 0:
24
- return
25
- maxrange = 50 * secs
26
- if isinstance(maxrange, float):
27
- sleeplittle = floor(maxrange)
28
- sleep_((maxrange - sleeplittle) / 50)
29
- maxrange = int(sleeplittle)
30
- if maxrange > 0:
31
- for _ in range(maxrange):
32
- sleep_(0.016)
33
- except KeyboardInterrupt:
34
- return
35
-
36
- iswindows = "win" in platform.platform().lower()
37
- if iswindows:
38
- startupinfo = subprocess.STARTUPINFO()
39
- startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
40
- startupinfo.wShowWindow = subprocess.SW_HIDE
41
- creationflags = subprocess.CREATE_NO_WINDOW
42
- invisibledict = {
43
- "startupinfo": startupinfo,
44
- "creationflags": creationflags,
45
- "start_new_session": True,
46
- }
47
- from ctypes import wintypes
48
-
49
- windll = ctypes.LibraryLoader(ctypes.WinDLL)
50
- kernel32 = windll.kernel32
51
- _GetShortPathNameW = kernel32.GetShortPathNameW
52
- _GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
53
- _GetShortPathNameW.restype = wintypes.DWORD
54
- else:
55
- invisibledict = {}
56
-
57
-
58
- @cache
59
- def get_short_path_name(long_name):
60
- try:
61
- if not iswindows:
62
- return long_name
63
- output_buf_size = 4096
64
- output_buf = ctypes.create_unicode_buffer(output_buf_size)
65
- _ = _GetShortPathNameW(long_name, output_buf, output_buf_size)
66
- return output_buf.value
67
- except Exception as e:
68
- sys.stderr.write(f"{e}\n")
69
- return long_name
70
-
71
-
72
- def killthread(threadobject):
73
- # based on https://pypi.org/project/kthread/
74
- if not threadobject.is_alive():
75
- return True
76
- tid = -1
77
- for tid1, tobj in threading._active.items():
78
- if tobj is threadobject:
79
- tid = tid1
80
- break
81
- if tid == -1:
82
- sys.stderr.write(f"{threadobject} not found")
83
- return False
84
- res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
85
- ctypes.c_long(tid), ctypes.py_object(SystemExit)
86
- )
87
- if res == 0:
88
- return False
89
- elif res != 1:
90
- ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
91
- return False
92
- return True
93
-
94
-
95
- def send_ctrl_commands(pid, command=0):
96
- if iswindows:
97
- commandstring = r"""import ctypes, sys; CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT = 0, 1, 2, 3, 4; kernel32 = ctypes.WinDLL("kernel32", use_last_error=True); (lambda pid, cmdtosend=CTRL_C_EVENT: [kernel32.FreeConsole(), kernel32.AttachConsole(pid), kernel32.SetConsoleCtrlHandler(None, 1), kernel32.GenerateConsoleCtrlEvent(cmdtosend, 0), sys.exit(0) if isinstance(pid, int) else None])(int(sys.argv[1]), int(sys.argv[2]) if len(sys.argv) > 2 else None) if __name__ == '__main__' else None"""
98
- subprocess.Popen(
99
- [sys.executable, "-c", commandstring, str(pid), str(command)],
100
- **invisibledict,
101
- )
102
- else:
103
- os.kill(pid, signal.SIGINT)
104
-
105
-
106
- class StopDescriptor:
107
- def __get__(self, instance, owner):
108
- return instance.__dict__[self.name]
109
-
110
- def __set__(self, instance, value):
111
- if not value:
112
- instance.__dict__[self.name] = False
113
- else:
114
- instance.__dict__[self.name] = True
115
- instance.stop_capture()
116
-
117
- def __delete__(self, instance):
118
- sys.stderr.write("Cannot be deleted!")
119
-
120
- def __set_name__(self, owner, name):
121
- self.name = name
122
-
123
-
124
- class AdbFastScreenshots:
125
- stop_recording = StopDescriptor()
126
-
127
- def __init__(
128
- self,
129
- adb_path,
130
- device_serial,
131
- time_interval=179,
132
- width=1600,
133
- height=900,
134
- bitrate="20M",
135
- use_busybox=False,
136
- connect_to_device=True,
137
- screenshotbuffer=10,
138
- go_idle=0,
139
- ):
140
- r"""Capture Android device screen using ADB's screenrecord with high frame rate.
141
-
142
- This class allows capturing the screen of an Android device using ADB's screenrecord
143
- command with an improved frame rate. It continuously captures frames from the device
144
- and provides them as NumPy arrays to the caller.
145
-
146
- Args:
147
- adb_path (str): The path to the ADB executable.
148
- device_serial (str): The serial number of the target Android device.
149
- time_interval (int): The maximum duration, in seconds, for each screen recording session (up to a maximum of 180 seconds). After reaching this time limit, a new recording session automatically starts without causing interruptions to the user experience.
150
- width (int): The width of the captured screen.
151
- height (int): The height of the captured screen.
152
- bitrate (str): The bitrate for screen recording (e.g., "20M" for 20Mbps).
153
- use_busybox (bool): Whether to use BusyBox for base64 encoding.
154
- connect_to_device (bool): Whether to connect to the device using ADB.
155
- screenshotbuffer (int): The size of the frame buffer to store the last captured frames.
156
- go_idle (float): The idle time (in seconds) when no new frames are available. # higher value -> less fps, but also less CPU usage.
157
-
158
- Attributes:
159
- stop_recording (bool): Control attribute to stop the screen capture.
160
-
161
- Methods:
162
- stop_capture(): Stops the screen capture.
163
-
164
- Usage:
165
- import cv2
166
- from adbnativeblitz import AdbFastScreenshots
167
-
168
- with AdbFastScreenshots(
169
- adb_path=r"C:\Android\android-sdk\platform-tools\adb.exe",
170
- device_serial="127.0.0.1:5555",
171
- time_interval=179,
172
- width=1600,
173
- height=900,
174
- bitrate="20M",
175
- use_busybox=False,
176
- connect_to_device=True,
177
- screenshotbuffer=10,
178
- go_idle=0,
179
- ) as adbscreen:
180
- for image in adbscreen:
181
- cv2.imshow("CV2 WINDOW", image)
182
- if cv2.waitKey(1) & 0xFF == ord("q"):
183
- break
184
- cv2.destroyAllWindows()
185
-
186
-
187
- Note:
188
- - The `AdbFastScreenshots` class should be used in a context manager (`with` statement).
189
- - The `stop_capture()` method can be called to stop the screen capture.
190
- - The frames are continuously captured and provided in the form of NumPy arrays.
191
- - The class aims to achieve a higher frame rate by avoiding slow subprocess creation
192
- for each screen capture session.
193
- """
194
-
195
- self.stop_recording = False
196
- self.size = f"{width}x{height}"
197
- self.width = width
198
- self.height = height
199
- self.timelimit = time_interval
200
- self.bitrate = bitrate
201
- self.use_busybox = use_busybox
202
- self.adb_path = get_short_path_name(adb_path)
203
- self.device_serial = device_serial
204
- if connect_to_device:
205
- subprocess.run(
206
- [self.adb_path, "connect", self.device_serial], **invisibledict
207
- )
208
- self.threadlock = threading.Lock()
209
- self.codec = av.codec.CodecContext.create("h264", "r")
210
- self.lastframes = deque([], screenshotbuffer)
211
- self.command_to_execute = (
212
- f"""#!/bin/bash
213
- startscreenrecord() {{
214
-
215
- screenrecord --output-format=h264 --time-limit "$1" --size "$2" --bit-rate "$3" -
216
- }}
217
-
218
- time_interval={self.timelimit}
219
- size="{self.size}"
220
- bitrate="{self.bitrate}"
221
- #screenrecord --output-format=h264 --time-limit 1 --size "$size" --bit-rate "$bitrate" -
222
- while true; do
223
- startscreenrecord $time_interval "$size" "$bitrate"
224
- done"""
225
- + "\n"
226
- )
227
- self.base64cmd = self.format_adb_command(
228
- self.command_to_execute,
229
- su=False,
230
- exitcommand="",
231
- errors="strict",
232
- )
233
- self.p = None
234
- self.threadstdout = None
235
- self.framecounter = 0
236
- self.go_idle = go_idle
237
-
238
- def format_adb_command(
239
- self,
240
- cmd,
241
- su=False,
242
- exitcommand="DONE",
243
- errors="strict",
244
- ):
245
- if su:
246
- cmd = f"su -- {cmd}"
247
- if exitcommand:
248
- cmd = cmd.rstrip() + f"\necho {exitcommand}\n"
249
- nolimitcommand = []
250
- base64_command = base64.standard_b64encode(cmd.encode("utf-8", errors)).decode(
251
- "utf-8", errors
252
- )
253
- nolimitcommand.extend(["echo", base64_command, "|"])
254
- if self.use_busybox:
255
- nolimitcommand.extend(["busybox"])
256
- nolimitcommand.extend(["base64", "-d", "|", "sh"])
257
-
258
- return " ".join(nolimitcommand) + "\n"
259
-
260
- def _start_capturing(self):
261
- def _execute_stdout_read():
262
- try:
263
- for q in iter(self.p.stdout.readline, b""):
264
- if iswindows:
265
- q = q.replace(b"\r\n", b"\n")
266
- if q:
267
- alldata.append(q)
268
- if alldata:
269
- joineddata = b"".join(alldata)
270
- try:
271
- packets = self.codec.parse(joineddata)
272
- if packets:
273
- for pack in packets:
274
- frames = self.codec.decode(pack)
275
- for frame in frames:
276
- nparray = (
277
- frame.to_rgb()
278
- .reformat(
279
- width=self.width,
280
- height=self.height,
281
- format="bgr24",
282
- )
283
- .to_ndarray()
284
- )
285
- try:
286
- self.threadlock.acquire()
287
- self.lastframes.append(nparray)
288
- self.framecounter += 1
289
-
290
- finally:
291
- try:
292
- self.threadlock.release()
293
- except Exception as e:
294
- sys.stderr.write(f"{e}\n")
295
- alldata.clear()
296
- except Exception as e:
297
- sys.stderr.write(f"{e}\n")
298
- except Exception as e:
299
- sys.stderr.write(f"{e}\n")
300
-
301
- self.p = subprocess.Popen(
302
- [self.adb_path, "-s", self.device_serial, "shell", self.base64cmd],
303
- stderr=subprocess.DEVNULL,
304
- stdout=subprocess.PIPE,
305
- stdin=subprocess.DEVNULL,
306
- bufsize=0,
307
- **invisibledict,
308
- )
309
- alldata = []
310
- self.threadstdout = threading.Thread(target=_execute_stdout_read)
311
- self.threadstdout.daemon = True
312
- self.threadstdout.start()
313
-
314
- def _stop_capture(self):
315
- try:
316
- if iswindows:
317
- subprocess.Popen(f"taskkill /F /PID {self.p.pid} /T", **invisibledict)
318
- except:
319
- pass
320
- try:
321
- self.p.stdout.close()
322
- except:
323
- pass
324
- try:
325
- killthread(self.threadstdout)
326
- except:
327
- pass
328
-
329
- def stop_capture(self):
330
- send_ctrl_commands(self.p.pid, command=0)
331
- try:
332
- sleep(1)
333
- except KeyboardInterrupt:
334
- pass
335
- self._stop_capture()
336
-
337
- def __iter__(self):
338
- oldframecounter = 0
339
-
340
- self._start_capturing()
341
- sleep(0.05)
342
- while not self.stop_recording:
343
- if not self.lastframes:
344
- sleep(0.005)
345
- continue
346
- yield self.lastframes[-1].copy()
347
- if oldframecounter == self.framecounter:
348
- if self.go_idle:
349
- sleep(self.go_idle)
350
- oldframecounter = self.framecounter
351
-
352
- def __enter__(self):
353
- return self
354
-
355
- def __exit__(self, type, value, traceback):
356
- self.stop_recording = True
357
-
358
-
359
- if __name__ == "__main__":
360
- import cv2
361
-
362
- with AdbFastScreenshots(
363
- adb_path=r"D:\SDK\Android\platform-tools\adb.exe",
364
- device_serial="127.0.0.1:16384",
365
- time_interval=179,
366
- width=720,
367
- height=1280,
368
- bitrate="20M",
369
- use_busybox=False,
370
- connect_to_device=True,
371
- screenshotbuffer=10,
372
- go_idle=0,
373
- ) as adbscreen:
374
- for image in adbscreen:
375
- cv2.imshow("CV2 WINDOW", image)
376
- if cv2.waitKey(1) & 0xFF == ord("q"):
377
- break
1
+ # type: ignore
2
+ # source: https://github.com/hansalemaos/adbnativeblitz
3
+ # license: MIT
4
+ # requires: av
5
+
6
+ import base64
7
+ import ctypes
8
+ import os
9
+ import platform
10
+ import signal
11
+ import subprocess
12
+ import sys
13
+ import threading
14
+ from collections import deque
15
+ from functools import cache
16
+ import av
17
+ from time import sleep as sleep_
18
+ from math import floor
19
+
20
+
21
+ def sleep(secs):
22
+ try:
23
+ if secs == 0:
24
+ return
25
+ maxrange = 50 * secs
26
+ if isinstance(maxrange, float):
27
+ sleeplittle = floor(maxrange)
28
+ sleep_((maxrange - sleeplittle) / 50)
29
+ maxrange = int(sleeplittle)
30
+ if maxrange > 0:
31
+ for _ in range(maxrange):
32
+ sleep_(0.016)
33
+ except KeyboardInterrupt:
34
+ return
35
+
36
+ iswindows = "win" in platform.platform().lower()
37
+ if iswindows:
38
+ startupinfo = subprocess.STARTUPINFO()
39
+ startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
40
+ startupinfo.wShowWindow = subprocess.SW_HIDE
41
+ creationflags = subprocess.CREATE_NO_WINDOW
42
+ invisibledict = {
43
+ "startupinfo": startupinfo,
44
+ "creationflags": creationflags,
45
+ "start_new_session": True,
46
+ }
47
+ from ctypes import wintypes
48
+
49
+ windll = ctypes.LibraryLoader(ctypes.WinDLL)
50
+ kernel32 = windll.kernel32
51
+ _GetShortPathNameW = kernel32.GetShortPathNameW
52
+ _GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
53
+ _GetShortPathNameW.restype = wintypes.DWORD
54
+ else:
55
+ invisibledict = {}
56
+
57
+
58
+ @cache
59
+ def get_short_path_name(long_name):
60
+ try:
61
+ if not iswindows:
62
+ return long_name
63
+ output_buf_size = 4096
64
+ output_buf = ctypes.create_unicode_buffer(output_buf_size)
65
+ _ = _GetShortPathNameW(long_name, output_buf, output_buf_size)
66
+ return output_buf.value
67
+ except Exception as e:
68
+ sys.stderr.write(f"{e}\n")
69
+ return long_name
70
+
71
+
72
+ def killthread(threadobject):
73
+ # based on https://pypi.org/project/kthread/
74
+ if not threadobject.is_alive():
75
+ return True
76
+ tid = -1
77
+ for tid1, tobj in threading._active.items():
78
+ if tobj is threadobject:
79
+ tid = tid1
80
+ break
81
+ if tid == -1:
82
+ sys.stderr.write(f"{threadobject} not found")
83
+ return False
84
+ res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
85
+ ctypes.c_long(tid), ctypes.py_object(SystemExit)
86
+ )
87
+ if res == 0:
88
+ return False
89
+ elif res != 1:
90
+ ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
91
+ return False
92
+ return True
93
+
94
+
95
+ def send_ctrl_commands(pid, command=0):
96
+ if iswindows:
97
+ commandstring = r"""import ctypes, sys; CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT = 0, 1, 2, 3, 4; kernel32 = ctypes.WinDLL("kernel32", use_last_error=True); (lambda pid, cmdtosend=CTRL_C_EVENT: [kernel32.FreeConsole(), kernel32.AttachConsole(pid), kernel32.SetConsoleCtrlHandler(None, 1), kernel32.GenerateConsoleCtrlEvent(cmdtosend, 0), sys.exit(0) if isinstance(pid, int) else None])(int(sys.argv[1]), int(sys.argv[2]) if len(sys.argv) > 2 else None) if __name__ == '__main__' else None"""
98
+ subprocess.Popen(
99
+ [sys.executable, "-c", commandstring, str(pid), str(command)],
100
+ **invisibledict,
101
+ )
102
+ else:
103
+ os.kill(pid, signal.SIGINT)
104
+
105
+
106
+ class StopDescriptor:
107
+ def __get__(self, instance, owner):
108
+ return instance.__dict__[self.name]
109
+
110
+ def __set__(self, instance, value):
111
+ if not value:
112
+ instance.__dict__[self.name] = False
113
+ else:
114
+ instance.__dict__[self.name] = True
115
+ instance.stop_capture()
116
+
117
+ def __delete__(self, instance):
118
+ sys.stderr.write("Cannot be deleted!")
119
+
120
+ def __set_name__(self, owner, name):
121
+ self.name = name
122
+
123
+
124
+ class AdbFastScreenshots:
125
+ stop_recording = StopDescriptor()
126
+
127
+ def __init__(
128
+ self,
129
+ adb_path,
130
+ device_serial,
131
+ time_interval=179,
132
+ width=1600,
133
+ height=900,
134
+ bitrate="20M",
135
+ use_busybox=False,
136
+ connect_to_device=True,
137
+ screenshotbuffer=10,
138
+ go_idle=0,
139
+ ):
140
+ r"""Capture Android device screen using ADB's screenrecord with high frame rate.
141
+
142
+ This class allows capturing the screen of an Android device using ADB's screenrecord
143
+ command with an improved frame rate. It continuously captures frames from the device
144
+ and provides them as NumPy arrays to the caller.
145
+
146
+ Args:
147
+ adb_path (str): The path to the ADB executable.
148
+ device_serial (str): The serial number of the target Android device.
149
+ time_interval (int): The maximum duration, in seconds, for each screen recording session (up to a maximum of 180 seconds). After reaching this time limit, a new recording session automatically starts without causing interruptions to the user experience.
150
+ width (int): The width of the captured screen.
151
+ height (int): The height of the captured screen.
152
+ bitrate (str): The bitrate for screen recording (e.g., "20M" for 20Mbps).
153
+ use_busybox (bool): Whether to use BusyBox for base64 encoding.
154
+ connect_to_device (bool): Whether to connect to the device using ADB.
155
+ screenshotbuffer (int): The size of the frame buffer to store the last captured frames.
156
+ go_idle (float): The idle time (in seconds) when no new frames are available. # higher value -> less fps, but also less CPU usage.
157
+
158
+ Attributes:
159
+ stop_recording (bool): Control attribute to stop the screen capture.
160
+
161
+ Methods:
162
+ stop_capture(): Stops the screen capture.
163
+
164
+ Usage:
165
+ import cv2
166
+ from adbnativeblitz import AdbFastScreenshots
167
+
168
+ with AdbFastScreenshots(
169
+ adb_path=r"C:\Android\android-sdk\platform-tools\adb.exe",
170
+ device_serial="127.0.0.1:5555",
171
+ time_interval=179,
172
+ width=1600,
173
+ height=900,
174
+ bitrate="20M",
175
+ use_busybox=False,
176
+ connect_to_device=True,
177
+ screenshotbuffer=10,
178
+ go_idle=0,
179
+ ) as adbscreen:
180
+ for image in adbscreen:
181
+ cv2.imshow("CV2 WINDOW", image)
182
+ if cv2.waitKey(1) & 0xFF == ord("q"):
183
+ break
184
+ cv2.destroyAllWindows()
185
+
186
+
187
+ Note:
188
+ - The `AdbFastScreenshots` class should be used in a context manager (`with` statement).
189
+ - The `stop_capture()` method can be called to stop the screen capture.
190
+ - The frames are continuously captured and provided in the form of NumPy arrays.
191
+ - The class aims to achieve a higher frame rate by avoiding slow subprocess creation
192
+ for each screen capture session.
193
+ """
194
+
195
+ self.stop_recording = False
196
+ self.size = f"{width}x{height}"
197
+ self.width = width
198
+ self.height = height
199
+ self.timelimit = time_interval
200
+ self.bitrate = bitrate
201
+ self.use_busybox = use_busybox
202
+ self.adb_path = get_short_path_name(adb_path)
203
+ self.device_serial = device_serial
204
+ if connect_to_device:
205
+ subprocess.run(
206
+ [self.adb_path, "connect", self.device_serial], **invisibledict
207
+ )
208
+ self.threadlock = threading.Lock()
209
+ self.codec = av.codec.CodecContext.create("h264", "r")
210
+ self.lastframes = deque([], screenshotbuffer)
211
+ self.command_to_execute = (
212
+ f"""#!/bin/bash
213
+ startscreenrecord() {{
214
+
215
+ screenrecord --output-format=h264 --time-limit "$1" --size "$2" --bit-rate "$3" -
216
+ }}
217
+
218
+ time_interval={self.timelimit}
219
+ size="{self.size}"
220
+ bitrate="{self.bitrate}"
221
+ #screenrecord --output-format=h264 --time-limit 1 --size "$size" --bit-rate "$bitrate" -
222
+ while true; do
223
+ startscreenrecord $time_interval "$size" "$bitrate"
224
+ done"""
225
+ + "\n"
226
+ )
227
+ self.base64cmd = self.format_adb_command(
228
+ self.command_to_execute,
229
+ su=False,
230
+ exitcommand="",
231
+ errors="strict",
232
+ )
233
+ self.p = None
234
+ self.threadstdout = None
235
+ self.framecounter = 0
236
+ self.go_idle = go_idle
237
+
238
+ def format_adb_command(
239
+ self,
240
+ cmd,
241
+ su=False,
242
+ exitcommand="DONE",
243
+ errors="strict",
244
+ ):
245
+ if su:
246
+ cmd = f"su -- {cmd}"
247
+ if exitcommand:
248
+ cmd = cmd.rstrip() + f"\necho {exitcommand}\n"
249
+ nolimitcommand = []
250
+ base64_command = base64.standard_b64encode(cmd.encode("utf-8", errors)).decode(
251
+ "utf-8", errors
252
+ )
253
+ nolimitcommand.extend(["echo", base64_command, "|"])
254
+ if self.use_busybox:
255
+ nolimitcommand.extend(["busybox"])
256
+ nolimitcommand.extend(["base64", "-d", "|", "sh"])
257
+
258
+ return " ".join(nolimitcommand) + "\n"
259
+
260
+ def _start_capturing(self):
261
+ def _execute_stdout_read():
262
+ try:
263
+ for q in iter(self.p.stdout.readline, b""):
264
+ if iswindows:
265
+ q = q.replace(b"\r\n", b"\n")
266
+ if q:
267
+ alldata.append(q)
268
+ if alldata:
269
+ joineddata = b"".join(alldata)
270
+ try:
271
+ packets = self.codec.parse(joineddata)
272
+ if packets:
273
+ for pack in packets:
274
+ frames = self.codec.decode(pack)
275
+ for frame in frames:
276
+ nparray = (
277
+ frame.to_rgb()
278
+ .reformat(
279
+ width=self.width,
280
+ height=self.height,
281
+ format="bgr24",
282
+ )
283
+ .to_ndarray()
284
+ )
285
+ try:
286
+ self.threadlock.acquire()
287
+ self.lastframes.append(nparray)
288
+ self.framecounter += 1
289
+
290
+ finally:
291
+ try:
292
+ self.threadlock.release()
293
+ except Exception as e:
294
+ sys.stderr.write(f"{e}\n")
295
+ alldata.clear()
296
+ except Exception as e:
297
+ sys.stderr.write(f"{e}\n")
298
+ except Exception as e:
299
+ sys.stderr.write(f"{e}\n")
300
+
301
+ self.p = subprocess.Popen(
302
+ [self.adb_path, "-s", self.device_serial, "shell", self.base64cmd],
303
+ stderr=subprocess.DEVNULL,
304
+ stdout=subprocess.PIPE,
305
+ stdin=subprocess.DEVNULL,
306
+ bufsize=0,
307
+ **invisibledict,
308
+ )
309
+ alldata = []
310
+ self.threadstdout = threading.Thread(target=_execute_stdout_read)
311
+ self.threadstdout.daemon = True
312
+ self.threadstdout.start()
313
+
314
+ def _stop_capture(self):
315
+ try:
316
+ if iswindows:
317
+ subprocess.Popen(f"taskkill /F /PID {self.p.pid} /T", **invisibledict)
318
+ except:
319
+ pass
320
+ try:
321
+ self.p.stdout.close()
322
+ except:
323
+ pass
324
+ try:
325
+ killthread(self.threadstdout)
326
+ except:
327
+ pass
328
+
329
+ def stop_capture(self):
330
+ send_ctrl_commands(self.p.pid, command=0)
331
+ try:
332
+ sleep(1)
333
+ except KeyboardInterrupt:
334
+ pass
335
+ self._stop_capture()
336
+
337
+ def __iter__(self):
338
+ oldframecounter = 0
339
+
340
+ self._start_capturing()
341
+ sleep(0.05)
342
+ while not self.stop_recording:
343
+ if not self.lastframes:
344
+ sleep(0.005)
345
+ continue
346
+ yield self.lastframes[-1].copy()
347
+ if oldframecounter == self.framecounter:
348
+ if self.go_idle:
349
+ sleep(self.go_idle)
350
+ oldframecounter = self.framecounter
351
+
352
+ def __enter__(self):
353
+ return self
354
+
355
+ def __exit__(self, type, value, traceback):
356
+ self.stop_recording = True
357
+
358
+
359
+ if __name__ == "__main__":
360
+ import cv2
361
+
362
+ with AdbFastScreenshots(
363
+ adb_path=r"D:\SDK\Android\platform-tools\adb.exe",
364
+ device_serial="127.0.0.1:16384",
365
+ time_interval=179,
366
+ width=720,
367
+ height=1280,
368
+ bitrate="20M",
369
+ use_busybox=False,
370
+ connect_to_device=True,
371
+ screenshotbuffer=10,
372
+ go_idle=0,
373
+ ) as adbscreen:
374
+ for image in adbscreen:
375
+ cv2.imshow("CV2 WINDOW", image)
376
+ if cv2.waitKey(1) & 0xFF == ord("q"):
377
+ break
378
378
  cv2.destroyAllWindows()