Преглед изворни кода

Merge pull request #246 from xbj110825/dev

Fix: [OAS] DroidCast image orientation on MuMu12
huangrunheng пре 2 година
родитељ
комит
936e6e5ce0
3 измењених фајлова са 92 додато и 6 уклоњено
  1. 27 0
      module/device/connection.py
  2. 60 2
      module/device/method/droidcast.py
  3. 5 4
      module/device/method/uiautomator_2.py

+ 27 - 0
module/device/connection.py

@@ -218,6 +218,18 @@ class Connection(ConnectionAttr):
             result = remove_shell_warning(result)
             # str
             return result
+    
+    def adb_getprop(self, name):
+        """
+        Get system property in Android, same as `getprop <name>`
+
+        Args:
+            name (str): Property name
+
+        Returns:
+            str:
+        """
+        return self.adb_shell(['getprop', name]).strip()
 
     @cached_property
     def cpu_abi(self) -> str:
@@ -252,6 +264,21 @@ class Connection(ConnectionAttr):
         if 'goldfish' in self.adb_shell(['getprop', 'ro.hardware.audio.primary']):
             return True
         return False
+    
+    @cached_property
+    def nemud_app_keep_alive(self) -> str:
+        res = self.adb_getprop('nemud.app_keep_alive')
+        logger.attr('nemud.app_keep_alive', res)
+        return res
+    
+    @cached_property
+    def is_mumu_over_version_356(self) -> bool:
+        """
+        Returns:
+            bool: If MuMu12 version >= 3.5.6,
+                which has nemud.app_keep_alive and always be a vertical device
+        """
+        return self.nemud_app_keep_alive != ''
 
     @cached_property
     def _nc_server_host_port(self):

+ 60 - 2
module/device/method/droidcast.py

@@ -95,6 +95,8 @@ class DroidCast(Uiautomator2):
     """
 
     _droidcast_port: int = 0
+    droidcast_width: int = 0
+    droidcast_height: int = 0
 
     @cached_property
     def droidcast_session(self):
@@ -120,12 +122,37 @@ class DroidCast(Uiautomator2):
 
         Note that /screenshot?format=jpg is unavailable.
         """
+    
+        if self.is_mumu_over_version_356:
+            w, h = self.droidcast_width, self.droidcast_height
+            if self.orientation == 0:
+                return f'http://127.0.0.1:{self._droidcast_port}{url}?width={w}&height={h}'
+            elif self.orientation == 1:
+                return f'http://127.0.0.1:{self._droidcast_port}{url}?width={h}&height={w}'
+            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'):
+        if self.is_mumu_over_version_356:
+            w, h = self.droidcast_width, self.droidcast_height
+            if self.orientation == 0:
+                return f'http://127.0.0.1:{self._droidcast_port}{url}?width={w}&height={h}'
+            elif self.orientation == 1:
+                return f'http://127.0.0.1:{self._droidcast_port}{url}?width={h}&height={w}'
+            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')
         self.droidcast_stop()
+        self._droidcast_update_resolution()
 
         logger.info('Pushing DroidCast apk')
         self.adb_push(self.config.DROIDCAST_FILEPATH_LOCAL, self.config.DROIDCAST_FILEPATH_REMOTE)
@@ -147,6 +174,16 @@ class DroidCast(Uiautomator2):
         logger.attr('DroidCast', self.droidcast_url())
         self.droidcast_wait_startup()
 
+    def _droidcast_update_resolution(self):
+        if self.is_mumu_over_version_356:
+            logger.info('Update droidcast resolution')
+            w, h = self.resolution_uiautomator2(cal_rotation=False)
+            self.get_orientation()
+            # 720, 1280
+            # mumu12 > 3.5.6 is always a vertical device
+            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')
@@ -177,6 +214,11 @@ class DroidCast(Uiautomator2):
     @retry
     def screenshot_droidcast(self):
         self.config.DROIDCAST_VERSION = 'DroidCast'
+
+        if self.is_mumu_over_version_356:
+            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
         image = np.frombuffer(image, np.uint8)
         if image is None:
@@ -192,16 +234,28 @@ class DroidCast(Uiautomator2):
         if image is None:
             raise ImageTruncated('Empty image after cv2.cvtColor')
 
+        if self.is_mumu_over_version_356:
+            if self.orientation == 1:
+                image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
+
         return image
 
     @retry
     def screenshot_droidcast_raw(self):
         self.config.DROIDCAST_VERSION = 'DroidCast_raw'
-        image = self.droidcast_session.get(self.droidcast_url(), timeout=3).content
+
+        shape = (720, 1280)
+        if self.is_mumu_over_version_356:
+            if not self.droidcast_width or not self.droidcast_height:
+                self._droidcast_update_resolution()
+            if self.droidcast_height and self.droidcast_width:
+                shape = (self.droidcast_height, self.droidcast_width)
+
+        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((720, 1280))
+            arr = np.frombuffer(image, dtype=np.uint16).reshape(shape)
         except ValueError as e:
             # Try to load as `DroidCast`
             image = np.frombuffer(image, np.uint8)
@@ -236,6 +290,10 @@ class DroidCast(Uiautomator2):
         b = cv2.add(b, cv2.multiply(b, 0.03125))
         image = cv2.merge([r, g, b])
 
+        if self.is_mumu_over_version_356:
+            if self.orientation == 1:
+                image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
+
         return image
 
     def droidcast_wait_startup(self):

+ 5 - 4
module/device/method/uiautomator_2.py

@@ -245,7 +245,7 @@ class Uiautomator2(Connection):
         return hierarchy
 
     @retry
-    def resolution_uiautomator2(self) -> t.Tuple[int, int]:
+    def resolution_uiautomator2(self, cal_rotation=True) -> t.Tuple[int, int]:
         """
         Faster u2.window_size(), cause that calls `dumpsys display` twice.
 
@@ -254,9 +254,10 @@ class Uiautomator2(Connection):
         """
         info = self.u2.http.get('/info').json()
         w, h = info['display']['width'], info['display']['height']
-        rotation = self.get_orientation()
-        if (w > h) != (rotation % 2 == 1):
-            w, h = h, w
+        if cal_rotation:
+            rotation = self.get_orientation()
+            if (w > h) != (rotation % 2 == 1):
+                w, h = h, w
         return w, h
 
     def resolution_check_uiautomator2(self):