|
|
@@ -91,7 +91,7 @@ def retry(func):
|
|
|
class DroidCast(Uiautomator2):
|
|
|
"""
|
|
|
DroidCast, another screenshot method, https://github.com/rayworks/DroidCast
|
|
|
- DroidCast_raw, a modified version of DroidCast sending raw bitmap https://github.com/Torther/DroidCastS
|
|
|
+ DroidCast_raw, a modified version of DroidCast sending raw bitmap and png, https://github.com/Torther/DroidCastS
|
|
|
"""
|
|
|
|
|
|
_droidcast_port: int = 0
|
|
|
@@ -105,24 +105,16 @@ class DroidCast(Uiautomator2):
|
|
|
self._droidcast_port = self.adb_forward('tcp:53516')
|
|
|
return session
|
|
|
|
|
|
- def droidcast_url(self, url='/screenshot?format=png'):
|
|
|
- """
|
|
|
- Check APIs from source code:
|
|
|
- https://github.com/rayworks/DroidCast/blob/master/app/src/main/java/com/rayworks/droidcast/Main.java
|
|
|
-
|
|
|
- Available APIs:
|
|
|
- - /screenshot
|
|
|
- To get JPG screenshots.
|
|
|
- - /screenshot?format=png
|
|
|
- To get PNG screenshots.
|
|
|
- - /screenshot?format=webp
|
|
|
- To get WEBP screenshots.
|
|
|
- - /src
|
|
|
- Websocket to get JPG screenshots.
|
|
|
-
|
|
|
- Note that /screenshot?format=jpg is unavailable.
|
|
|
- """
|
|
|
-
|
|
|
+ """
|
|
|
+ Check APIs from source code:
|
|
|
+ https://github.com/Torther/DroidCast_raw/blob/DroidCast_raw/app/src/main/java/ink/mol/droidcast_raw/KtMain.kt
|
|
|
+ Available APIs:
|
|
|
+ - /screenshot
|
|
|
+ To get a RGB565 bitmap
|
|
|
+ - /preview
|
|
|
+ To get PNG screenshots.
|
|
|
+ """
|
|
|
+ def droidcast_url(self, url='/preview'):
|
|
|
if self.is_mumu_over_version_356:
|
|
|
w, h = self.droidcast_width, self.droidcast_height
|
|
|
if self.orientation == 0:
|
|
|
@@ -132,7 +124,6 @@ class DroidCast(Uiautomator2):
|
|
|
else:
|
|
|
# logger.warning('DroidCast receives invalid device orientation')
|
|
|
pass
|
|
|
-
|
|
|
return f'http://127.0.0.1:{self._droidcast_port}{url}'
|
|
|
|
|
|
def droidcast_raw_url(self, url='/screenshot'):
|
|
|
@@ -145,12 +136,11 @@ class DroidCast(Uiautomator2):
|
|
|
else:
|
|
|
# logger.warning('DroidCast receives invalid device orientation')
|
|
|
pass
|
|
|
-
|
|
|
return f'http://127.0.0.1:{self._droidcast_port}{url}'
|
|
|
|
|
|
- @Config.when(DROIDCAST_VERSION='DroidCast')
|
|
|
+
|
|
|
def droidcast_init(self):
|
|
|
- logger.hr('Droidcast init')
|
|
|
+ logger.hr('DroidCast init')
|
|
|
self.droidcast_stop()
|
|
|
self._droidcast_update_resolution()
|
|
|
|
|
|
@@ -158,21 +148,29 @@ class DroidCast(Uiautomator2):
|
|
|
self.adb_push(self.config.DROIDCAST_FILEPATH_LOCAL, self.config.DROIDCAST_FILEPATH_REMOTE)
|
|
|
|
|
|
logger.info('Starting DroidCast apk')
|
|
|
- # CLASSPATH=/data/local/tmp/DroidCast.apk app_process / com.rayworks.droidcast.Main > /dev/null
|
|
|
+ # DroidCast_raw-release-1.0.apk
|
|
|
+ # CLASSPATH=/data/local/tmp/DroidCast_raw.apk app_process / ink.mol.droidcast_raw.Main > /dev/null
|
|
|
+ # adb shell CLASSPATH=/data/local/tmp/DroidCast_raw.apk app_process / ink.mol.droidcast_raw.Main
|
|
|
resp = self.u2_shell_background([
|
|
|
- 'CLASSPATH=/data/local/tmp/DroidCast.apk',
|
|
|
+ 'CLASSPATH=/data/local/tmp/DroidCast_raw.apk',
|
|
|
'app_process',
|
|
|
'/',
|
|
|
- 'com.rayworks.droidcast.Main',
|
|
|
+ 'ink.mol.droidcast_raw.Main',
|
|
|
'>',
|
|
|
'/dev/null'
|
|
|
])
|
|
|
logger.info(resp)
|
|
|
-
|
|
|
del_cached_property(self, 'droidcast_session')
|
|
|
_ = self.droidcast_session
|
|
|
- logger.attr('DroidCast', self.droidcast_url())
|
|
|
- self.droidcast_wait_startup()
|
|
|
+
|
|
|
+ if self.config.DROIDCAST_VERSION == 'DroidCast':
|
|
|
+ logger.attr('DroidCast', self.droidcast_url())
|
|
|
+ self.droidcast_wait_startup()
|
|
|
+ elif self.config.DROIDCAST_VERSION == 'DroidCast_raw':
|
|
|
+ logger.attr('DroidCast_raw', self.droidcast_raw_url())
|
|
|
+ self.droidcast_wait_startup()
|
|
|
+ else:
|
|
|
+ logger.error(f'Unknown DROIDCAST_VERSION: {self.config.DROIDCAST_VERSION}')
|
|
|
|
|
|
def _droidcast_update_resolution(self):
|
|
|
if self.is_mumu_over_version_356:
|
|
|
@@ -184,32 +182,6 @@ class DroidCast(Uiautomator2):
|
|
|
self.droidcast_width, self.droidcast_height = w, h
|
|
|
logger.info(f'Droicast resolution: {(w, h)}')
|
|
|
|
|
|
- @Config.when(DROIDCAST_VERSION='DroidCast_raw')
|
|
|
- def droidcast_init(self):
|
|
|
- logger.hr('Droidcast init')
|
|
|
- self.resolution_check_uiautomator2()
|
|
|
- self.droidcast_stop()
|
|
|
-
|
|
|
- logger.info('Pushing DroidCast apk')
|
|
|
- self.adb_push(self.config.DROIDCAST_RAW_FILEPATH_LOCAL, self.config.DROIDCAST_RAW_FILEPATH_REMOTE)
|
|
|
-
|
|
|
- logger.info('Starting DroidCast apk')
|
|
|
- # DroidCastS-release-1.1.5.apk
|
|
|
- # CLASSPATH=/data/local/tmp/DroidCastS-release-1.1.5.apk app_process / com.torther.droidcasts.Main > /dev/null
|
|
|
- resp = self.u2_shell_background([
|
|
|
- 'CLASSPATH=/data/local/tmp/DroidCastS.apk',
|
|
|
- 'app_process',
|
|
|
- '/',
|
|
|
- 'com.torther.droidcasts.Main',
|
|
|
- '>',
|
|
|
- '/dev/null'
|
|
|
- ])
|
|
|
- logger.info(resp)
|
|
|
-
|
|
|
- del_cached_property(self, 'droidcast_session')
|
|
|
- _ = self.droidcast_session
|
|
|
- logger.attr('DroidCast', self.droidcast_url())
|
|
|
- self.droidcast_wait_startup()
|
|
|
|
|
|
@retry
|
|
|
def screenshot_droidcast(self):
|
|
|
@@ -219,7 +191,11 @@ class DroidCast(Uiautomator2):
|
|
|
if not self.droidcast_width or not self.droidcast_height:
|
|
|
self._droidcast_update_resolution()
|
|
|
|
|
|
- image = self.droidcast_session.get(self.droidcast_url(), timeout=3).content
|
|
|
+ resp = self.droidcast_session.get(self.droidcast_url(), timeout=3)
|
|
|
+
|
|
|
+ if resp.status_code == 404:
|
|
|
+ raise DroidCastVersionIncompatible('DroidCast server does not have /preview')
|
|
|
+ image = resp.content
|
|
|
image = np.frombuffer(image, np.uint8)
|
|
|
if image is None:
|
|
|
raise ImageTruncated('Empty image after reading from buffer')
|
|
|
@@ -251,12 +227,24 @@ class DroidCast(Uiautomator2):
|
|
|
if self.droidcast_height and self.droidcast_width:
|
|
|
shape = (self.droidcast_height, self.droidcast_width)
|
|
|
|
|
|
+ rotate = self.is_mumu_over_version_356 and self.orientation == 1
|
|
|
+
|
|
|
image = self.droidcast_session.get(self.droidcast_raw_url(), timeout=3).content
|
|
|
# DroidCast_raw returns a RGB565 bitmap
|
|
|
|
|
|
try:
|
|
|
- arr = np.frombuffer(image, dtype=np.uint16).reshape(shape)
|
|
|
+ arr = np.frombuffer(image, dtype=np.uint16)
|
|
|
+ if rotate:
|
|
|
+ arr = arr.reshape(shape)
|
|
|
+ # arr = cv2.rotate(arr, cv2.ROTATE_90_CLOCKWISE)
|
|
|
+ # A little bit faster?
|
|
|
+ arr = cv2.transpose(arr)
|
|
|
+ cv2.flip(arr, 1, dst=arr)
|
|
|
+ else:
|
|
|
+ arr = arr.reshape(shape)
|
|
|
except ValueError as e:
|
|
|
+ if len(image) < 500:
|
|
|
+ logger.warning(f'Unexpected screenshot: {image}')
|
|
|
# Try to load as `DroidCast`
|
|
|
image = np.frombuffer(image, np.uint8)
|
|
|
if image is not None:
|
|
|
@@ -267,32 +255,39 @@ class DroidCast(Uiautomator2):
|
|
|
# ValueError: cannot reshape array of size 0 into shape (720,1280)
|
|
|
raise ImageTruncated(str(e))
|
|
|
|
|
|
- # Convert RGB565 to RGB888
|
|
|
- # https://blog.csdn.net/happy08god/article/details/10516871
|
|
|
-
|
|
|
- # r = (arr & 0b1111100000000000) >> (11 - 3)
|
|
|
- # g = (arr & 0b0000011111100000) >> (5 - 2)
|
|
|
- # b = (arr & 0b0000000000011111) << 3
|
|
|
- # r |= (r & 0b11100000) >> 5
|
|
|
- # g |= (g & 0b11000000) >> 6
|
|
|
- # b |= (b & 0b11100000) >> 5
|
|
|
- # r = r.astype(np.uint8)
|
|
|
- # g = g.astype(np.uint8)
|
|
|
- # b = b.astype(np.uint8)
|
|
|
- # image = cv2.merge([r, g, b])
|
|
|
-
|
|
|
- # The same as the code above but costs about 5ms instead of 10ms.
|
|
|
- r = cv2.multiply(arr & 0b1111100000000000, 0.00390625).astype(np.uint8)
|
|
|
- g = cv2.multiply(arr & 0b0000011111100000, 0.125).astype(np.uint8)
|
|
|
- b = cv2.multiply(arr & 0b0000000000011111, 8).astype(np.uint8)
|
|
|
- r = cv2.add(r, cv2.multiply(r, 0.03125))
|
|
|
- g = cv2.add(g, cv2.multiply(g, 0.015625))
|
|
|
- b = cv2.add(b, cv2.multiply(b, 0.03125))
|
|
|
- image = cv2.merge([r, g, b])
|
|
|
+ # Convert RGB565 to RGB888
|
|
|
+ # https://blog.csdn.net/happy08god/article/details/10516871
|
|
|
+
|
|
|
+ # r = (arr & 0b1111100000000000) >> (11 - 3)
|
|
|
+ # g = (arr & 0b0000011111100000) >> (5 - 2)
|
|
|
+ # b = (arr & 0b0000000000011111) << 3
|
|
|
+ # r |= (r & 0b11100000) >> 5
|
|
|
+ # g |= (g & 0b11000000) >> 6
|
|
|
+ # b |= (b & 0b11100000) >> 5
|
|
|
+ # r = r.astype(np.uint8)
|
|
|
+ # g = g.astype(np.uint8)
|
|
|
+ # b = b.astype(np.uint8)
|
|
|
+ # image = cv2.merge([r, g, b])
|
|
|
+
|
|
|
+ # The same as the code above but costs about 3~4ms instead of 10ms.
|
|
|
+ # Note that cv2.convertScaleAbs is 5x fast as cv2.multiply, cv2.add is 8x fast as cv2.convertScaleAbs
|
|
|
+ # Note that cv2.convertScaleAbs includes rounding
|
|
|
+ r = cv2.bitwise_and(arr, 0b1111100000000000)
|
|
|
+ r = cv2.convertScaleAbs(r, alpha=0.00390625)
|
|
|
+ m = cv2.convertScaleAbs(r, alpha=0.03125)
|
|
|
+ cv2.add(r, m, dst=r)
|
|
|
+
|
|
|
+ g = cv2.bitwise_and(arr, 0b0000011111100000)
|
|
|
+ g = cv2.convertScaleAbs(g, alpha=0.125)
|
|
|
+ m = cv2.convertScaleAbs(g, alpha=0.015625, dst=m)
|
|
|
+ cv2.add(g, m, dst=g)
|
|
|
+
|
|
|
+ b = cv2.bitwise_and(arr, 0b0000000000011111)
|
|
|
+ b = cv2.convertScaleAbs(b, alpha=8)
|
|
|
+ m = cv2.convertScaleAbs(b, alpha=0.03125, dst=m)
|
|
|
+ cv2.add(b, m, dst=b)
|
|
|
|
|
|
- if self.is_mumu_over_version_356:
|
|
|
- if self.orientation == 1:
|
|
|
- image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
|
|
+ image = cv2.merge([r, g, b])
|
|
|
|
|
|
return image
|
|
|
|
|
|
@@ -320,13 +315,13 @@ class DroidCast(Uiautomator2):
|
|
|
|
|
|
def droidcast_uninstall(self):
|
|
|
"""
|
|
|
- Stop all DroidCast processes and remove DroidCast APK.
|
|
|
- DroidCast has't been installed but a JAVA class call, uninstall is a file delete.
|
|
|
+ Stop DroidCast processes and remove DroidCast APK.
|
|
|
+ DroidCast hasn't been installed but a JAVA class call, uninstall is a file delete.
|
|
|
"""
|
|
|
self.droidcast_stop()
|
|
|
logger.info('Removing DroidCast')
|
|
|
self.adb_shell(["rm", self.config.DROIDCAST_FILEPATH_REMOTE])
|
|
|
- self.adb_shell(["rm", self.config.DROIDCAST_RAW_FILEPATH_REMOTE])
|
|
|
+
|
|
|
|
|
|
def _iter_droidcast_proc(self) -> t.Iterable[ProcessInfo]:
|
|
|
"""
|
|
|
@@ -338,17 +333,21 @@ class DroidCast(Uiautomator2):
|
|
|
yield proc
|
|
|
if 'com.torther.droidcasts.Main' in proc.cmdline:
|
|
|
yield proc
|
|
|
+ if 'ink.mol.droidcast_raw.Main' in proc.cmdline:
|
|
|
+ yield proc
|
|
|
|
|
|
def droidcast_stop(self):
|
|
|
"""
|
|
|
- Stop all DroidCast processes.
|
|
|
+ Stop DroidCast processes.
|
|
|
"""
|
|
|
logger.info('Stopping DroidCast')
|
|
|
for proc in self._iter_droidcast_proc():
|
|
|
logger.info(f'Kill pid={proc.pid}')
|
|
|
self.adb_shell(['kill', '-s', 9, proc.pid])
|
|
|
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
dc = DroidCast(config='oas1')
|
|
|
- cv2.imshow("iiii", dc.screenshot_droidcast())
|
|
|
+ cv2.imshow("iiii", dc.screenshot_droidcast_raw())
|
|
|
cv2.waitKey(0)
|