Does appium UiAutomator2 mjpeg server work on Android 5?

UiAutomator2 mjpeg server (port 7810) works on Android 6 real device as my topic but not on Android 5.1.1 real device.

As ask in the title, does appium UiAutomator2 mjpeg server work on Android 5?

from appium import webdriver
from datetime import datetime
deviceName = 'r3'
caps = dict(
            platformName = 'Android',
            automationName = 'uiautomator2',
            udid = deviceName + ':5555',
            mjpegScreenshotUrl = f'http://{deviceName}:7810',
            newCommandTimeout = 600,
            noReset = True
        )
driver = webdriver.Remote('http://localhost:4000/wd/hub', caps)
2021-03-22 22:18:10:712 [UiAutomator2] Starting MJPEG stream reading URL: 'http://r3:7810'
2021-03-22 22:18:10:714 [Support] Loading local package 'mjpeg-consumer'
2021-03-22 22:18:10:733 [Support] Error getting MJpeg screenshot chunk: connect ECONNREFUSED 192.168.31.130:7810

Full appium server debug log

Environment

  • Appium version (or git revision) that exhibits the issue: 1.20.2
  • Last Appium version that did not exhibit the issue (if applicable):
  • Desktop OS/version used to run Appium: Manjaro 20.2.1 Nibia, x86_64 Linux 5.10.7-3-MANJARO
  • Node.js version (unless using Appium.app|exe): v14.8.0
  • Npm or Yarn package manager: npm 7.5.6
  • Mobile platform/version under test: Android 5.1.1
  • Real device or emulator/simulator: Real device
  • Appium CLI or Appium.app|exe: Appium CLI

UiAutomator2 is known to be flaky on Android below v6. Unfortunately there is nothing that could be done there from Appium side. Also Google won’t be fixing any issues there, as this OS version is too ancient.

The answer is yes. Appium UiAutomator2 mjpeg server works on Android 5.

Thanks to mykola-mokhnach for the explanation in Does appium UiAutomator2 mjpeg server always start on port 7810?.
Capability mjpegServerPort specifies the PC port instead of the Android port.
The UiAutomator2 mjpeg server always starts on Android port 7810.
In below Python code, mjpegServerPort is set to 12345, which causes Android port 7810 forwarded to PC port 12345.

from appium import webdriver
from datetime import datetime
deviceName = 'm4'
caps = dict(
            platformName = 'Android',
            automationName = 'uiautomator2',
            udid = deviceName + ':5555',
            mjpegServerPort = 12345,
            mjpegScreenshotUrl = 'http://localhost:12345',
            newCommandTimeout = 600,
            noReset = True
        )
driver = webdriver.Remote('http://localhost:4723/wd/hub', caps)

The appium server log should contains

[debug] [ADB] Forwarding system: 12345 to device: 7810

One difference below Android 5 and Android >= 6 is Android port 7810 is not exposed on Android 5. That means,
For Android 5, mjpegScreenshotUrl should be set to http://localhost:12345.
For Android >= 6, mjpegScreenshotUrl can be set to http://localhost:12345 or http://m4:7810, where m4 is the hostname of my Andriod phone.