Replace ImageReader capture with MediaCodec encode/decode for Amlogic.
Android CI / build (push) Canceled after 0s
Android CI / build (push) Canceled after 0s
Fixes black frames on Ugoos X5M Pro, stabilizes Home/resume, settings framerate crash, and ships a debug APK under dist/apk. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b9cbbfa13d
commit
063a068620
17 files changed
+942
-453
No files matched your search
@@ -6,12 +6,16 @@ import android.content.Intent;
|
||||
import android.media.projection.MediaProjectionManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.hyperion.grabber.common.util.MediaProjectionHelper;
|
||||
|
||||
public class BootActivity extends AppCompatActivity {
|
||||
public static final int REQUEST_MEDIA_PROJECTION = 1;
|
||||
private static final String TAG = "BootActivity";
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
@Override
|
||||
@@ -20,7 +24,9 @@ public class BootActivity extends AppCompatActivity {
|
||||
MediaProjectionManager manager = (MediaProjectionManager)
|
||||
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
|
||||
if (manager != null) {
|
||||
startActivityForResult(manager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
|
||||
Log.i(TAG, "Requesting MediaProjection permission");
|
||||
startActivityForResult(MediaProjectionHelper.createCaptureIntent(manager),
|
||||
REQUEST_MEDIA_PROJECTION);
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
@@ -31,7 +37,10 @@ public class BootActivity extends AppCompatActivity {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_MEDIA_PROJECTION) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
Log.i(TAG, "MediaProjection granted, starting service");
|
||||
startScreenRecorder(this, resultCode, data);
|
||||
} else {
|
||||
Log.w(TAG, "MediaProjection denied resultCode=" + resultCode);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -35,7 +35,7 @@ public class HyperionScreenService extends Service {
|
||||
public static final String BROADCAST_ERROR = "SERVICE_ERROR";
|
||||
public static final String BROADCAST_TAG = "SERVICE_STATUS";
|
||||
public static final String BROADCAST_FILTER = "SERVICE_FILTER";
|
||||
private static final boolean DEBUG = false;
|
||||
private static final boolean DEBUG = true;
|
||||
private static final String TAG = "HyperionScreenService";
|
||||
|
||||
private boolean mForegroundFailed = false;
|
||||
@@ -405,10 +405,15 @@ public class HyperionScreenService extends Service {
|
||||
final DisplayMetrics metrics = new DisplayMetrics();
|
||||
window.getDefaultDisplay().getRealMetrics(metrics);
|
||||
|
||||
Log.i(TAG, "MediaProjection OK screen=" + metrics.widthPixels + "x" + metrics.heightPixels
|
||||
+ " dpi=" + metrics.densityDpi
|
||||
+ " leds=" + mHorizontalLEDCount + "x" + mVerticalLEDCount
|
||||
+ " fps=" + mFrameRate);
|
||||
|
||||
HyperionGrabberOptions options = new HyperionGrabberOptions(
|
||||
mHorizontalLEDCount, mVerticalLEDCount, mFrameRate, mSendAverageColor);
|
||||
|
||||
if (DEBUG) Log.v(TAG, "Creating encoder: " + metrics.widthPixels + "x" + metrics.heightPixels);
|
||||
Log.i(TAG, "Creating MediaCodec screen encoder");
|
||||
mHyperionEncoder = new HyperionScreenEncoder(
|
||||
mHyperionThread.getReceiver(),
|
||||
projection,
|
||||
|
||||
@@ -7,12 +7,16 @@ import android.content.Intent;
|
||||
import android.media.projection.MediaProjectionManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.hyperion.grabber.common.util.MediaProjectionHelper;
|
||||
|
||||
public class ToggleActivity extends AppCompatActivity {
|
||||
public static final int REQUEST_MEDIA_PROJECTION = 1;
|
||||
private static final String TAG = "ToggleActivity";
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
@Override
|
||||
@@ -27,7 +31,6 @@ public class ToggleActivity extends AppCompatActivity {
|
||||
} else {
|
||||
requestPermission();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -35,11 +38,12 @@ public class ToggleActivity extends AppCompatActivity {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_MEDIA_PROJECTION) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
Log.i(TAG, "MediaProjection granted");
|
||||
startScreenRecorder(this, resultCode, data);
|
||||
} else {
|
||||
Log.w(TAG, "MediaProjection denied");
|
||||
}
|
||||
|
||||
finish();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,17 +83,17 @@ public class ToggleActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
private void requestPermission(){
|
||||
private void requestPermission() {
|
||||
MediaProjectionManager manager = (MediaProjectionManager)
|
||||
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
|
||||
if (manager != null) {
|
||||
startActivityForResult(manager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
|
||||
Log.i(TAG, "Requesting MediaProjection permission");
|
||||
startActivityForResult(MediaProjectionHelper.createCaptureIntent(manager),
|
||||
REQUEST_MEDIA_PROJECTION);
|
||||
}
|
||||
}
|
||||
|
||||
/** stop recording & stop service */
|
||||
|
||||
private void stopService() {
|
||||
private void stopService() {
|
||||
Intent stopIntent = new Intent(ToggleActivity.this, HyperionScreenService.class);
|
||||
stopIntent.setAction(HyperionScreenService.ACTION_EXIT);
|
||||
startService(stopIntent);
|
||||
|
||||
@@ -36,8 +36,10 @@ class HyperionThread(
|
||||
val client = clientRef.get()
|
||||
if (client == null || !client.isConnected()) return
|
||||
|
||||
// Copy — encoder reuses the ring buffer; async send must not share it.
|
||||
val copy = data.copyOf()
|
||||
++lastFrameNumber
|
||||
latestFrame.set(FrameData(data, width, height, lastFrameNumber))
|
||||
latestFrame.set(FrameData(copy, width, height, lastFrameNumber))
|
||||
|
||||
if (sendTaskScheduled.compareAndSet(false, true)) {
|
||||
networkExecutor.submit { sendLatestFrame() }
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.hyperion.grabber.common.util;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.media.projection.MediaProjectionConfig;
|
||||
import android.media.projection.MediaProjectionManager;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Creates MediaProjection consent intents. On Android 14+ prefers
|
||||
* {@link MediaProjectionConfig#createConfigForDefaultDisplay()} so the entire
|
||||
* display is captured (not only an app window).
|
||||
*/
|
||||
public final class MediaProjectionHelper {
|
||||
private static final String TAG = "MediaProjectionHelper";
|
||||
|
||||
private MediaProjectionHelper() {}
|
||||
|
||||
public static Intent createCaptureIntent(MediaProjectionManager manager) {
|
||||
if (manager == null) {
|
||||
throw new IllegalArgumentException("MediaProjectionManager is null");
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
try {
|
||||
MediaProjectionConfig config =
|
||||
MediaProjectionConfig.createConfigForDefaultDisplay();
|
||||
Log.i(TAG, "Using MediaProjectionConfig.createConfigForDefaultDisplay()");
|
||||
return manager.createScreenCaptureIntent(config);
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Default-display config failed, falling back: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
Log.i(TAG, "Using default createScreenCaptureIntent()");
|
||||
return manager.createScreenCaptureIntent();
|
||||
}
|
||||
}
|
||||
@@ -7,59 +7,54 @@ import androidx.annotation.StringRes
|
||||
import android.util.Log
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
/** Wrapper around SharedPreferences which allows for default values defined in Resources
|
||||
* Main purpose is that defaults are defined in a centralized location and that preferences are
|
||||
* accessed through a unified interface
|
||||
* Now includes in-memory caching to reduce disk I/O */
|
||||
/** Wrapper around SharedPreferences with defaults from Resources and an in-memory cache. */
|
||||
class Preferences(context: Context) {
|
||||
|
||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
private val resources = context.resources
|
||||
|
||||
// In-memory cache to reduce SharedPreferences disk I/O
|
||||
private val stringCache = ConcurrentHashMap<String, String?>()
|
||||
|
||||
// ConcurrentHashMap forbids null values — never store nulls.
|
||||
private val stringCache = ConcurrentHashMap<String, String>()
|
||||
private val intCache = ConcurrentHashMap<String, Int>()
|
||||
private val boolCache = ConcurrentHashMap<String, Boolean>()
|
||||
private val cacheHits = ConcurrentHashMap<String, Boolean>()
|
||||
private val nullStringKeys = ConcurrentHashMap.newKeySet<String>()
|
||||
|
||||
fun contains(@StringRes keyResourceId: Int): Boolean = preferences.contains(key(keyResourceId))
|
||||
|
||||
fun getString(@StringRes keyResourceId: Int, default: String? = null): String? {
|
||||
val keyStr = key(keyResourceId)
|
||||
|
||||
// Return from cache if available
|
||||
if (stringCache.containsKey(keyStr)) {
|
||||
return stringCache[keyStr]
|
||||
|
||||
if (nullStringKeys.contains(keyStr)) {
|
||||
return default
|
||||
}
|
||||
|
||||
// Fetch from SharedPreferences and cache
|
||||
stringCache[keyStr]?.let { return it }
|
||||
|
||||
val value = preferences.getString(keyStr, default)
|
||||
stringCache[keyStr] = value
|
||||
if (value != null) {
|
||||
stringCache[keyStr] = value
|
||||
nullStringKeys.remove(keyStr)
|
||||
} else {
|
||||
stringCache.remove(keyStr)
|
||||
nullStringKeys.add(keyStr)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
fun putString(@StringRes keyResourceId: Int, value: String){
|
||||
fun putString(@StringRes keyResourceId: Int, value: String) {
|
||||
val keyStr = key(keyResourceId)
|
||||
val edit = preferences.edit()
|
||||
edit.putString(keyStr, value)
|
||||
edit.apply()
|
||||
|
||||
// Update cache
|
||||
preferences.edit().putString(keyStr, value).apply()
|
||||
stringCache[keyStr] = value
|
||||
nullStringKeys.remove(keyStr)
|
||||
intCache.remove(keyStr)
|
||||
boolCache.remove(keyStr)
|
||||
}
|
||||
|
||||
fun getInt(@StringRes keyResourceId: Int): Int {
|
||||
val keyStr = key(keyResourceId)
|
||||
|
||||
// Return from cache if available
|
||||
if (intCache.containsKey(keyStr)) {
|
||||
return intCache[keyStr]!!
|
||||
}
|
||||
|
||||
intCache[keyStr]?.let { return it }
|
||||
|
||||
val default = defaultKey(keyResourceId, "integer").let {
|
||||
if (it == 0){
|
||||
if (it == 0) {
|
||||
0
|
||||
} else {
|
||||
try {
|
||||
@@ -75,33 +70,30 @@ class Preferences(context: Context) {
|
||||
|
||||
fun getInt(@StringRes keyResourceId: Int, default: Int = 0): Int {
|
||||
val keyStr = key(keyResourceId)
|
||||
|
||||
// Return from cache if available
|
||||
if (intCache.containsKey(keyStr)) {
|
||||
return intCache[keyStr]!!
|
||||
intCache[keyStr]?.let { return it }
|
||||
|
||||
val raw = getString(keyResourceId)
|
||||
val value = try {
|
||||
if (raw.isNullOrBlank()) default else Integer.parseInt(raw.trim())
|
||||
} catch (e: NumberFormatException) {
|
||||
Log.w("Preferences", "Invalid int for $keyStr: '$raw', using default $default")
|
||||
default
|
||||
}
|
||||
|
||||
val value = getString(keyResourceId)?.let { Integer.parseInt(it) } ?: default
|
||||
intCache[keyStr] = value
|
||||
return value
|
||||
}
|
||||
|
||||
fun putInt(@StringRes keyResourceId: Int, value: Int){
|
||||
fun putInt(@StringRes keyResourceId: Int, value: Int) {
|
||||
putString(keyResourceId, value.toString())
|
||||
val keyStr = key(keyResourceId)
|
||||
intCache[keyStr] = value
|
||||
intCache[key(keyResourceId)] = value
|
||||
}
|
||||
|
||||
fun getBoolean(@StringRes keyResourceId: Int): Boolean {
|
||||
val keyStr = key(keyResourceId)
|
||||
|
||||
// Return from cache if available
|
||||
if (boolCache.containsKey(keyStr)) {
|
||||
return boolCache[keyStr]!!
|
||||
}
|
||||
|
||||
boolCache[keyStr]?.let { return it }
|
||||
|
||||
val default = defaultKey(keyResourceId, "bool").let {
|
||||
if (it == 0){
|
||||
if (it == 0) {
|
||||
false
|
||||
} else {
|
||||
try {
|
||||
@@ -119,46 +111,39 @@ class Preferences(context: Context) {
|
||||
|
||||
fun getBoolean(@StringRes keyResourceId: Int, default: Boolean): Boolean {
|
||||
val keyStr = key(keyResourceId)
|
||||
|
||||
// Return from cache if available
|
||||
if (boolCache.containsKey(keyStr)) {
|
||||
return boolCache[keyStr]!!
|
||||
}
|
||||
|
||||
boolCache[keyStr]?.let { return it }
|
||||
|
||||
val value = preferences.getBoolean(keyStr, default)
|
||||
boolCache[keyStr] = value
|
||||
return value
|
||||
}
|
||||
|
||||
fun putBoolean(@StringRes keyResourceId: Int, value: Boolean){
|
||||
fun putBoolean(@StringRes keyResourceId: Int, value: Boolean) {
|
||||
val keyStr = key(keyResourceId)
|
||||
val edit = preferences.edit()
|
||||
edit.putBoolean(keyStr, value)
|
||||
edit.apply()
|
||||
|
||||
// Update cache
|
||||
preferences.edit().putBoolean(keyStr, value).apply()
|
||||
boolCache[keyStr] = value
|
||||
stringCache.remove(keyStr)
|
||||
nullStringKeys.remove(keyStr)
|
||||
intCache.remove(keyStr)
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the in-memory cache but NOT the SharedPreferences
|
||||
* Call this when you know preferences have been updated externally
|
||||
*/
|
||||
|
||||
fun clearCache() {
|
||||
stringCache.clear()
|
||||
intCache.clear()
|
||||
boolCache.clear()
|
||||
nullStringKeys.clear()
|
||||
Log.d("Preferences", "Cache cleared")
|
||||
}
|
||||
|
||||
private fun key(keyResourceId: Int) = resources.getString(keyResourceId)
|
||||
|
||||
/** @return 0 if not found, resource id otherwise */
|
||||
private fun defaultKey(keyResourceId: Int, type: String): Int {
|
||||
val defaultKeyName = resources.getResourceEntryName(keyResourceId).replace("pref_key_", "pref_default_")
|
||||
return resources.getIdentifier(defaultKeyName, type, resources.getResourcePackageName(keyResourceId))
|
||||
val defaultKeyName = resources.getResourceEntryName(keyResourceId)
|
||||
.replace("pref_key_", "pref_default_")
|
||||
return resources.getIdentifier(
|
||||
defaultKeyName,
|
||||
type,
|
||||
resources.getResourcePackageName(keyResourceId)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user