|
| 1 | +import abc |
| 2 | +import os |
| 3 | +from collections import deque |
| 4 | +from typing import Tuple |
| 5 | + |
| 6 | +from adbutils import adb, AdbDevice |
| 7 | + |
| 8 | + |
| 9 | +class BaseAgent(abc.ABC): |
| 10 | + """ |
| 11 | + Abstract Base Class for BlueStacks screen capture agents. |
| 12 | + This class defines common configuration and the interface for all capture methods. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, |
| 16 | + adb_path: str = "adb", |
| 17 | + adb_port: int = 5555, |
| 18 | + resolution: Tuple[int, int] = None, |
| 19 | + bitrate: int = 8000000, |
| 20 | + max_fps: int = 30, |
| 21 | + queue_size: int = 3): |
| 22 | + """ |
| 23 | + :param resolution: Tuple (width, height) for the desired resolution, or None for native |
| 24 | + :param bitrate: Bitrate for video encoding (default 8Mbps) |
| 25 | + :param max_fps: Maximum frames per second (default 30) |
| 26 | + :param queue_size: Number of frames to buffer (default 3) |
| 27 | + """ |
| 28 | + self.adb_path = adb_path |
| 29 | + self.adb_port = adb_port |
| 30 | + self.resolution = resolution |
| 31 | + self.bitrate = bitrate |
| 32 | + self.max_fps = max_fps |
| 33 | + self.queue_size = queue_size |
| 34 | + |
| 35 | + # Check if the resolution is valid |
| 36 | + if resolution is not None and not self._is_resolution_valid(): |
| 37 | + raise ValueError(f"Invalid resolution: {self.resolution}") |
| 38 | + |
| 39 | + # Check if the bitrate is valid |
| 40 | + if not self._is_bitrate_valid(): |
| 41 | + raise ValueError(f"Invalid bitrate: {self.bitrate}") |
| 42 | + |
| 43 | + # Check if the max_fps is valid |
| 44 | + if not self._is_max_fps_valid(): |
| 45 | + raise ValueError(f"Invalid max_fps: {self.max_fps}") |
| 46 | + |
| 47 | + # Check if the queue_size is valid |
| 48 | + if not self._is_queue_size_valid(): |
| 49 | + raise ValueError(f"Invalid queue_size: {self.queue_size}") |
| 50 | + |
| 51 | + # Create the frame buffer |
| 52 | + self.frame_buffer = deque(maxlen=self.queue_size) |
| 53 | + |
| 54 | + # Connect to ADB |
| 55 | + self._connect_adb() |
| 56 | + self.adb_device: AdbDevice = adb.device(serial=f"emulator-{self.adb_port}") |
| 57 | + |
| 58 | + self._is_streaming = False |
| 59 | + |
| 60 | + @abc.abstractmethod |
| 61 | + def _start_stream(self): |
| 62 | + """ |
| 63 | + Start the screen capture stream internally, to be handled from the concrete instances. |
| 64 | + """ |
| 65 | + |
| 66 | + def start_stream(self): |
| 67 | + """ |
| 68 | + Start the screen capture stream. |
| 69 | + """ |
| 70 | + self._start_stream() |
| 71 | + self._is_streaming = True |
| 72 | + |
| 73 | + @abc.abstractmethod |
| 74 | + def _stop_stream(self): |
| 75 | + """ |
| 76 | + Stop the screen capture stream internally, to be handled from the concrete instances. |
| 77 | + """ |
| 78 | + |
| 79 | + def stop_stream(self): |
| 80 | + """ |
| 81 | + Stop the screen capture stream. |
| 82 | + """ |
| 83 | + self._is_streaming = False |
| 84 | + self._stop_stream() |
| 85 | + |
| 86 | + def is_streaming(self): |
| 87 | + """ |
| 88 | + Return whether the agent is currently streaming. |
| 89 | + """ |
| 90 | + return self._is_streaming |
| 91 | + |
| 92 | + def _is_resolution_valid(self): |
| 93 | + """ |
| 94 | + Check if the resolution is valid. |
| 95 | + """ |
| 96 | + # Check if the resolution is a tuple of two integers |
| 97 | + return isinstance(self.resolution, tuple) and len(self.resolution) == 2 and all( |
| 98 | + isinstance(x, int) for x in self.resolution) |
| 99 | + |
| 100 | + def _is_bitrate_valid(self): |
| 101 | + """ |
| 102 | + Check if the bitrate is valid. |
| 103 | + """ |
| 104 | + # Check if the bitrate is a positive integer |
| 105 | + return isinstance(self.bitrate, int) and self.bitrate > 0 |
| 106 | + |
| 107 | + def _is_max_fps_valid(self): |
| 108 | + """ |
| 109 | + Check if the max_fps is valid. |
| 110 | + """ |
| 111 | + # Check if the max_fps is a positive integer |
| 112 | + return isinstance(self.max_fps, int) and self.max_fps > 0 |
| 113 | + |
| 114 | + def _is_queue_size_valid(self): |
| 115 | + """ |
| 116 | + Check if the queue_size is valid. |
| 117 | + """ |
| 118 | + # Check if the queue_size is a positive integer |
| 119 | + return isinstance(self.queue_size, int) and self.queue_size > 0 |
| 120 | + |
| 121 | + def _insert_in_frame_buffer(self, frame): |
| 122 | + """ |
| 123 | + Insert a frame into the frame buffer. |
| 124 | + """ |
| 125 | + self.frame_buffer.append(frame) |
| 126 | + |
| 127 | + def _connect_adb(self): |
| 128 | + """ |
| 129 | + Connect to ADB. |
| 130 | + """ |
| 131 | + # Check if ADB is installed |
| 132 | + if not self._is_adb_installed(): |
| 133 | + raise FileNotFoundError("ADB not found. Please install ADB and add it to the system PATH.") |
| 134 | + |
| 135 | + def _is_adb_installed(self): |
| 136 | + """ |
| 137 | + Check if ADB is installed. |
| 138 | + """ |
| 139 | + return os.system(f"{self.adb_path} --version") == 0 |
0 commit comments