From a4cfd39f95cf67f1dc14f2bc0984b0db76af1343 Mon Sep 17 00:00:00 2001 From: csf Date: Thu, 14 Apr 2022 20:16:42 +0800 Subject: [PATCH] android server landscape --- .../com/carriez/flutter_hbb/InputService.kt | 4 +- .../com/carriez/flutter_hbb/MainActivity.kt | 33 --------- .../com/carriez/flutter_hbb/MainService.kt | 72 ++++++++++++++++--- .../kotlin/com/carriez/flutter_hbb/common.kt | 10 ++- 4 files changed, 69 insertions(+), 50 deletions(-) diff --git a/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt b/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt index b95fa733a..1bbfe748d 100644 --- a/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt +++ b/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt @@ -47,8 +47,8 @@ class InputService : AccessibilityService() { } if (!(mask == 9 || mask == 10)) { - mouseX = x * INFO.scale - mouseY = y * INFO.scale + mouseX = x * SCREEN_INFO.scale + mouseY = y * SCREEN_INFO.scale } // left button down ,was up diff --git a/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt b/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt index fa79800b6..5059eaa09 100644 --- a/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt +++ b/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt @@ -9,7 +9,6 @@ import android.media.projection.MediaProjectionManager import android.os.Build import android.os.IBinder import android.provider.Settings -import android.util.DisplayMetrics import android.util.Log import androidx.annotation.RequiresApi import io.flutter.embedding.android.FlutterActivity @@ -31,7 +30,6 @@ class MainActivity : FlutterActivity() { @RequiresApi(Build.VERSION_CODES.M) override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) - updateMachineInfo() flutterMethodChannel = MethodChannel( flutterEngine.dartExecutor.binaryMessenger, channelTag @@ -190,37 +188,6 @@ class MainActivity : FlutterActivity() { } } - private fun updateMachineInfo() { - val dm = DisplayMetrics() - @Suppress("DEPRECATION") - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - display?.getRealMetrics(dm) - } else { - windowManager.defaultDisplay.getRealMetrics(dm) - } - var w = dm.widthPixels - var h = dm.heightPixels - var scale = 1 - if (w != 0 && h != 0) { - if (w > MAX_SCREEN_SIZE || h > MAX_SCREEN_SIZE) { - scale = 2 - w /= scale - h /= scale - } - - INFO.screenWidth = w - INFO.screenHeight = h - INFO.scale = scale - INFO.username = "test" - INFO.hostname = "hostname" - // TODO username hostname - Log.d(logTag, "INIT INFO:$INFO") - - } else { - Log.e(logTag, "Got Screen Size Fail!") - } - } - override fun onDestroy() { Log.e(logTag, "onDestroy") mainService?.let { diff --git a/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainService.kt b/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainService.kt index 8cbbb4eb4..df888093f 100644 --- a/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainService.kt +++ b/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainService.kt @@ -12,6 +12,7 @@ import android.app.PendingIntent.FLAG_UPDATE_CURRENT import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.content.res.Configuration import android.graphics.Color import android.graphics.PixelFormat import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC @@ -20,9 +21,11 @@ import android.media.* import android.media.projection.MediaProjection import android.media.projection.MediaProjectionManager import android.os.* +import android.util.DisplayMetrics import android.util.Log import android.view.Surface import android.view.Surface.FRAME_RATE_COMPATIBILITY_DEFAULT +import android.view.WindowManager import androidx.annotation.Keep import androidx.annotation.RequiresApi import androidx.core.app.ActivityCompat @@ -68,7 +71,7 @@ class MainService : Service() { @Keep fun rustGetByName(name: String): String { return when (name) { - "screen_size" -> "${INFO.screenWidth}:${INFO.screenHeight}" + "screen_size" -> "${SCREEN_INFO.width}:${SCREEN_INFO.height}" else -> "" } } @@ -127,8 +130,10 @@ class MainService : Service() { private external fun init(ctx: Context) private external fun startServer() private external fun onVideoFrameUpdate(buf: ByteBuffer) + private external fun releaseVideoFrame() private external fun onAudioFrameUpdate(buf: ByteBuffer) private external fun translateLocale(localeName: String, input: String): String + private external fun refreshScreen() // private external fun sendVp9(data: ByteArray) private fun translate(input: String): String { @@ -167,10 +172,50 @@ class MainService : Service() { override fun onCreate() { super.onCreate() + updateScreenInfo() initNotification() startServer() } + private fun updateScreenInfo() { + var w: Int + var h: Int + val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager + + @Suppress("DEPRECATION") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val m = windowManager.maximumWindowMetrics + w = m.bounds.width() + h = m.bounds.height() + } else { + val dm = DisplayMetrics() + windowManager.defaultDisplay.getRealMetrics(dm) + w = dm.widthPixels + h = dm.heightPixels + } + + var scale = 1 + if (w != 0 && h != 0) { + if (w > MAX_SCREEN_SIZE || h > MAX_SCREEN_SIZE) { + scale = 2 + w /= scale + h /= scale + } + if (SCREEN_INFO.width != w) { + SCREEN_INFO.width = w + SCREEN_INFO.height = h + SCREEN_INFO.scale = scale + if (isStart) { + stopCapture() + refreshScreen() + startCapture() + } + + } + + } + } + override fun onBind(intent: Intent): IBinder { Log.d(logTag, "service onBind") return binder @@ -195,7 +240,6 @@ class MainService : Service() { mediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, it) checkMediaPermission() - surface = createSurface() init(this) _isReady = true } ?: let { @@ -206,17 +250,22 @@ class MainService : Service() { return super.onStartCommand(intent, flags, startId) } + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + updateScreenInfo() + } + @SuppressLint("WrongConstant") private fun createSurface(): Surface? { return if (useVP9) { // TODO null } else { - Log.d(logTag, "ImageReader.newInstance:INFO:$INFO") + Log.d(logTag, "ImageReader.newInstance:INFO:$SCREEN_INFO") imageReader = ImageReader.newInstance( - INFO.screenWidth, - INFO.screenHeight, + SCREEN_INFO.width, + SCREEN_INFO.height, PixelFormat.RGBA_8888, 4 ).apply { @@ -247,6 +296,7 @@ class MainService : Service() { return false } Log.d(logTag, "Start Capture") + surface = createSurface() if (useVP9) { startVP9VideoRecorder(mediaProjection!!) @@ -266,6 +316,9 @@ class MainService : Service() { Log.d(logTag, "Stop Capture") _isStart = false // release video + imageReader?.close() + releaseVideoFrame() + surface?.release() virtualDisplay?.release() videoEncoder?.let { it.signalEndOfInputStream() @@ -309,14 +362,14 @@ class MainService : Service() { @SuppressLint("WrongConstant") private fun startRawVideoRecorder(mp: MediaProjection) { - Log.d(logTag, "startRawVideoRecorder,screen info:$INFO") + Log.d(logTag, "startRawVideoRecorder,screen info:$SCREEN_INFO") if (surface == null) { Log.d(logTag, "startRawVideoRecorder failed,surface is null") return } virtualDisplay = mp.createVirtualDisplay( "RustDesk", - INFO.screenWidth, INFO.screenHeight, 200, VIRTUAL_DISPLAY_FLAG_PUBLIC, + SCREEN_INFO.width, SCREEN_INFO.height, 200, VIRTUAL_DISPLAY_FLAG_PUBLIC, surface, null, null ) } @@ -333,7 +386,7 @@ class MainService : Service() { it.start() virtualDisplay = mp.createVirtualDisplay( "RustDeskVD", - INFO.screenWidth, INFO.screenHeight, 200, VIRTUAL_DISPLAY_FLAG_PUBLIC, + SCREEN_INFO.width, SCREEN_INFO.height, 200, VIRTUAL_DISPLAY_FLAG_PUBLIC, surface, null, null ) } @@ -367,7 +420,8 @@ class MainService : Service() { private fun createMediaCodec() { Log.d(logTag, "MediaFormat.MIMETYPE_VIDEO_VP9 :$MIME_TYPE") videoEncoder = MediaCodec.createEncoderByType(MIME_TYPE) - val mFormat = MediaFormat.createVideoFormat(MIME_TYPE, INFO.screenWidth, INFO.screenHeight) + val mFormat = + MediaFormat.createVideoFormat(MIME_TYPE, SCREEN_INFO.width, SCREEN_INFO.height) mFormat.setInteger(MediaFormat.KEY_BIT_RATE, VIDEO_KEY_BIT_RATE) mFormat.setInteger(MediaFormat.KEY_FRAME_RATE, VIDEO_KEY_FRAME_RATE) mFormat.setInteger( diff --git a/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt b/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt index b18824964..895a7c658 100644 --- a/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt +++ b/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt @@ -18,12 +18,10 @@ import java.util.* @SuppressLint("ConstantLocale") val LOCAL_NAME = Locale.getDefault().toString() - -val INFO = Info("", "", 0, 0) +val SCREEN_INFO = Info(0, 0) data class Info( - var username: String, var hostname: String, var screenWidth: Int, var screenHeight: Int, - var scale: Int = 1 + var width: Int, var height: Int, var scale: Int = 1 ) @RequiresApi(Build.VERSION_CODES.LOLLIPOP) @@ -33,8 +31,8 @@ fun testVP9Support(): Boolean { .findEncoderForFormat( MediaFormat.createVideoFormat( MediaFormat.MIMETYPE_VIDEO_VP9, - INFO.screenWidth, - INFO.screenWidth + SCREEN_INFO.width, + SCREEN_INFO.width ) ) return res != null