fix android server null ptr crash / orientation
This commit is contained in:
		
							parent
							
								
									811265554a
								
							
						
					
					
						commit
						49dc99f170
					
				| @ -13,10 +13,10 @@ import android.content.Context | |||||||
| import android.content.Intent | import android.content.Intent | ||||||
| import android.content.pm.PackageManager | import android.content.pm.PackageManager | ||||||
| import android.content.res.Configuration | import android.content.res.Configuration | ||||||
|  | import android.content.res.Configuration.ORIENTATION_LANDSCAPE | ||||||
| import android.graphics.Color | import android.graphics.Color | ||||||
| import android.graphics.PixelFormat | import android.graphics.PixelFormat | ||||||
| import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR | import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR | ||||||
| import android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |  | ||||||
| import android.hardware.display.VirtualDisplay | import android.hardware.display.VirtualDisplay | ||||||
| import android.media.* | import android.media.* | ||||||
| import android.media.projection.MediaProjection | import android.media.projection.MediaProjection | ||||||
| @ -37,6 +37,8 @@ import kotlin.concurrent.thread | |||||||
| import org.json.JSONException | import org.json.JSONException | ||||||
| import org.json.JSONObject | import org.json.JSONObject | ||||||
| import java.nio.ByteBuffer | import java.nio.ByteBuffer | ||||||
|  | import kotlin.math.max | ||||||
|  | import kotlin.math.min | ||||||
| 
 | 
 | ||||||
| const val EXTRA_MP_DATA = "mp_intent" | const val EXTRA_MP_DATA = "mp_intent" | ||||||
| const val INIT_SERVICE = "init_service" | const val INIT_SERVICE = "init_service" | ||||||
| @ -132,10 +134,10 @@ class MainService : Service() { | |||||||
|     private external fun init(ctx: Context) |     private external fun init(ctx: Context) | ||||||
|     private external fun startServer() |     private external fun startServer() | ||||||
|     private external fun onVideoFrameUpdate(buf: ByteBuffer) |     private external fun onVideoFrameUpdate(buf: ByteBuffer) | ||||||
|     private external fun releaseVideoFrame() |  | ||||||
|     private external fun onAudioFrameUpdate(buf: ByteBuffer) |     private external fun onAudioFrameUpdate(buf: ByteBuffer) | ||||||
|     private external fun translateLocale(localeName: String, input: String): String |     private external fun translateLocale(localeName: String, input: String): String | ||||||
|     private external fun refreshScreen() |     private external fun refreshScreen() | ||||||
|  |     private external fun setFrameRawEnable(name: String, value: Boolean) | ||||||
|     // private external fun sendVp9(data: ByteArray) |     // private external fun sendVp9(data: ByteArray) | ||||||
| 
 | 
 | ||||||
|     private fun translate(input: String): String { |     private fun translate(input: String): String { | ||||||
| @ -144,8 +146,8 @@ class MainService : Service() { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     companion object { |     companion object { | ||||||
|         private var _isReady = false |         private var _isReady = false // media permission ready status | ||||||
|         private var _isStart = false |         private var _isStart = false // screen capture start status | ||||||
|         val isReady: Boolean |         val isReady: Boolean | ||||||
|             get() = _isReady |             get() = _isReady | ||||||
|         val isStart: Boolean |         val isStart: Boolean | ||||||
| @ -183,7 +185,7 @@ class MainService : Service() { | |||||||
|             serviceLooper = looper |             serviceLooper = looper | ||||||
|             serviceHandler = Handler(looper) |             serviceHandler = Handler(looper) | ||||||
|         } |         } | ||||||
|         updateScreenInfo() |         updateScreenInfo(resources.configuration.orientation) | ||||||
|         initNotification() |         initNotification() | ||||||
|         startServer() |         startServer() | ||||||
|     } |     } | ||||||
| @ -197,7 +199,7 @@ class MainService : Service() { | |||||||
|         super.onDestroy() |         super.onDestroy() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private fun updateScreenInfo() { |     private fun updateScreenInfo(orientation: Int) { | ||||||
|         var w: Int |         var w: Int | ||||||
|         var h: Int |         var h: Int | ||||||
|         var dpi: Int |         var dpi: Int | ||||||
| @ -217,6 +219,16 @@ class MainService : Service() { | |||||||
|             dpi = dm.densityDpi |             dpi = dm.densityDpi | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         val max = max(w,h) | ||||||
|  |         val min = min(w,h) | ||||||
|  |         if (orientation == ORIENTATION_LANDSCAPE) { | ||||||
|  |             w = max | ||||||
|  |             h = min | ||||||
|  |         } else { | ||||||
|  |             w = min | ||||||
|  |             h = max | ||||||
|  |         } | ||||||
|  |         Log.d(logTag,"updateScreenInfo:w:$w,h:$h") | ||||||
|         var scale = 1 |         var scale = 1 | ||||||
|         if (w != 0 && h != 0) { |         if (w != 0 && h != 0) { | ||||||
|             if (w > MAX_SCREEN_SIZE || h > MAX_SCREEN_SIZE) { |             if (w > MAX_SCREEN_SIZE || h > MAX_SCREEN_SIZE) { | ||||||
| @ -274,7 +286,7 @@ class MainService : Service() { | |||||||
| 
 | 
 | ||||||
|     override fun onConfigurationChanged(newConfig: Configuration) { |     override fun onConfigurationChanged(newConfig: Configuration) { | ||||||
|         super.onConfigurationChanged(newConfig) |         super.onConfigurationChanged(newConfig) | ||||||
|         updateScreenInfo() |         updateScreenInfo(newConfig.orientation) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @SuppressLint("WrongConstant") |     @SuppressLint("WrongConstant") | ||||||
| @ -317,6 +329,7 @@ class MainService : Service() { | |||||||
|             Log.w(logTag, "startCapture fail,mediaProjection is null") |             Log.w(logTag, "startCapture fail,mediaProjection is null") | ||||||
|             return false |             return false | ||||||
|         } |         } | ||||||
|  |         updateScreenInfo(resources.configuration.orientation) | ||||||
|         Log.d(logTag, "Start Capture") |         Log.d(logTag, "Start Capture") | ||||||
|         surface = createSurface() |         surface = createSurface() | ||||||
| 
 | 
 | ||||||
| @ -331,17 +344,20 @@ class MainService : Service() { | |||||||
|         } |         } | ||||||
|         checkMediaPermission() |         checkMediaPermission() | ||||||
|         _isStart = true |         _isStart = true | ||||||
|  |         setFrameRawEnable("video",true) | ||||||
|  |         setFrameRawEnable("audio",true) | ||||||
|         return true |         return true | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun stopCapture() { |     fun stopCapture() { | ||||||
|         Log.d(logTag, "Stop Capture") |         Log.d(logTag, "Stop Capture") | ||||||
|  |         setFrameRawEnable("video",false) | ||||||
|  |         setFrameRawEnable("audio",false) | ||||||
|         _isStart = false |         _isStart = false | ||||||
|         // release video |         // release video | ||||||
|         imageReader?.close() |  | ||||||
|         releaseVideoFrame() |  | ||||||
|         surface?.release() |  | ||||||
|         virtualDisplay?.release() |         virtualDisplay?.release() | ||||||
|  |         surface?.release() | ||||||
|  |         imageReader?.close() | ||||||
|         videoEncoder?.let { |         videoEncoder?.let { | ||||||
|             it.signalEndOfInputStream() |             it.signalEndOfInputStream() | ||||||
|             it.stop() |             it.stop() | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user