options.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import typing as t
  2. import module.device.method.scrcpy.const as const
  3. class ScrcpyOptions:
  4. frame_rate = 6
  5. @classmethod
  6. def codec_options(cls) -> str:
  7. """
  8. Custom codec options passing through scrcpy.
  9. https://developer.android.com/reference/android/media/MediaFormat
  10. Returns:
  11. key_profile=1,key_level=4096,...
  12. """
  13. options = dict(
  14. # H.264 profile and level
  15. # https://developer.android.com/reference/android/media/MediaCodecInfo.CodecProfileLevel
  16. # Baseline, which only has I/P frames
  17. key_profile=1,
  18. # Level 4.1, for 1280x720@30fps
  19. key_level=4096,
  20. # Max quality
  21. key_quality=100,
  22. # https://developer.android.com/reference/android/media/MediaCodecInfo.EncoderCapabilities
  23. # Constant quality
  24. key_bitrate_mode=0,
  25. # A zero value means a stream containing all key frames is requested.
  26. key_i_frame_interval=0,
  27. # https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities
  28. # COLOR_Format24bitBGR888
  29. key_color_format=12,
  30. # The same as output frame rate to lower CPU consumption
  31. key_capture_rate=cls.frame_rate,
  32. # 20Mbps, the maximum output bitrate of scrcpy
  33. key_bit_rate=20000000,
  34. )
  35. return ','.join([f'{k}={v}' for k, v in options.items()])
  36. @classmethod
  37. def arguments(cls) -> t.List[str]:
  38. """
  39. https://github.com/Genymobile/scrcpy/blob/master/server/src/main/java/com/genymobile/scrcpy/Server.java
  40. https://github.com/Genymobile/scrcpy/blob/master/server/src/main/java/com/genymobile/scrcpy/Options.java
  41. Returns:
  42. ['log_level=info', 'max_size=1280', ...]
  43. """
  44. options = [
  45. 'log_level=info',
  46. 'max_size=1280',
  47. # 20Mbps, the maximum output bitrate of scrcpy
  48. # If a higher value is set, scrcpy fallback to 8Mbps default.
  49. 'bit_rate=20000000',
  50. # Screenshot time cost <= 300ms is enough for human speed.
  51. f'max_fps={cls.frame_rate}',
  52. # No orientation lock
  53. f'lock_video_orientation={const.LOCK_SCREEN_ORIENTATION_UNLOCKED}',
  54. # Always true
  55. 'tunnel_forward=true',
  56. # Always true for controlling via scrcpy
  57. 'control=true',
  58. # Default to 0
  59. 'display_id=0',
  60. # Useless, always false
  61. 'show_touches=false',
  62. # Not determined, leave it as default
  63. 'stay_awake=false',
  64. # Encoder name
  65. # Should in [
  66. # "OMX.google.h264.encoder",
  67. # "OMX.qcom.video.encoder.avc",
  68. # "c2.qti.avc.encoder",
  69. # "c2.android.avc.encoder",
  70. # ]
  71. # Empty value, let scrcpy to decide
  72. # 'encoder_name=',
  73. # Codec options
  74. f'codec_options={cls.codec_options()}',
  75. # Useless, always false
  76. 'power_off_on_close=false',
  77. 'clipboard_autosync=false',
  78. 'downsize_on_error=false',
  79. ]
  80. return options
  81. @classmethod
  82. def command_v125(cls, jar_path='/data/local/tmp/scrcpy-server.jar') -> t.List[str]:
  83. """
  84. Generate the commands to run scrcpy.
  85. """
  86. commands = [
  87. f'CLASSPATH={jar_path}',
  88. 'app_process',
  89. '/',
  90. 'com.genymobile.scrcpy.Server',
  91. '1.25',
  92. ]
  93. commands += cls.arguments()
  94. return commands
  95. @classmethod
  96. def command_v120(cls, jar_path='/data/local/tmp/scrcpy-server.jar') -> t.List[str]:
  97. commands = [
  98. f"CLASSPATH={jar_path}",
  99. "app_process",
  100. "/",
  101. "com.genymobile.scrcpy.Server",
  102. "1.20", # Scrcpy server version
  103. "info", # Log level: info, verbose...
  104. f"1280", # Max screen width (long side)
  105. f"20000000", # Bitrate of video
  106. f"{cls.frame_rate}", # Max frame per second
  107. f"{const.LOCK_SCREEN_ORIENTATION_UNLOCKED}", # Lock screen orientation: LOCK_SCREEN_ORIENTATION
  108. "true", # Tunnel forward
  109. "-", # Crop screen
  110. "false", # Send frame rate to client
  111. "true", # Control enabled
  112. "0", # Display id
  113. "false", # Show touches
  114. "false", # Stay awake
  115. cls.codec_options(), # Codec (video encoding) options
  116. "-", # Encoder name
  117. "false", # Power off screen after server closed
  118. ]
  119. return commands
  120. if __name__ == '__main__':
  121. print(' '.join(ScrcpyOptions.command_v120()))