From 19cdf982c4206c1dc3df09168d74ad87c4975869 Mon Sep 17 00:00:00 2001 From: bleack Date: Sat, 28 Nov 2026 00:58:55 +0100 Subject: [PATCH] perf(android): prefer Qualcomm c2.qti.*.low_latency decoder, picture-order output, and immediate VSync render --- .../steamframe/fpv/LowLatencyVideoDecoder.kt | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/android/app/src/main/kotlin/com/steamframe/fpv/LowLatencyVideoDecoder.kt b/android/app/src/main/kotlin/com/steamframe/fpv/LowLatencyVideoDecoder.kt index 2e2609b..ce2dc5c 100644 --- a/android/app/src/main/kotlin/com/steamframe/fpv/LowLatencyVideoDecoder.kt +++ b/android/app/src/main/kotlin/com/steamframe/fpv/LowLatencyVideoDecoder.kt @@ -3,6 +3,8 @@ package com.steamframe.fpv import android.content.ContentValues import android.content.Context import android.media.MediaCodec +import android.media.MediaCodecInfo +import android.media.MediaCodecList import android.media.MediaFormat import android.media.MediaMuxer import android.os.Environment @@ -67,15 +69,45 @@ class LowLatencyVideoDecoder(private val context: Context) { } } + private fun createBestLowLatencyDecoder(mime: String): MediaCodec { + val list = MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos + var featureMatch: String? = null + for (info in list) { + if (info.isEncoder) continue + if (!info.supportedTypes.any { it.equals(mime, ignoreCase = true) }) continue + val nameLower = info.name.lowercase(Locale.US) + if (nameLower.contains("low_latency")) { + Log.i(TAG, "Sélection décodeur matériel dédié low_latency : ${info.name}") + return MediaCodec.createByCodecName(info.name) + } + if (featureMatch == null && !nameLower.startsWith("c2.android.") && !nameLower.startsWith("omx.google.")) { + val caps = try { + info.getCapabilitiesForType(mime) + } catch (_: Exception) { + null + } + if (caps?.isFeatureSupported(MediaCodecInfo.CodecCapabilities.FEATURE_LowLatency) == true) { + featureMatch = info.name + } + } + } + if (featureMatch != null) { + Log.i(TAG, "Sélection décodeur matériel FEATURE_LowLatency : $featureMatch") + return MediaCodec.createByCodecName(featureMatch) + } + return MediaCodec.createDecoderByType(mime) + } + private fun initCodecLocked(targetSurface: Surface) { try { - val mc = MediaCodec.createDecoderByType(mimeType) - val format = MediaFormat.createVideoFormat(mimeType, 1920, 1080).apply { + val mc = createBestLowLatencyDecoder(mimeType) + val format = MediaFormat.createVideoFormat(mimeType, 1280, 720).apply { setInteger(MediaFormat.KEY_LOW_LATENCY, 1) setInteger(MediaFormat.KEY_PRIORITY, 0) // Temps réel setInteger(MediaFormat.KEY_OPERATING_RATE, Short.MAX_VALUE.toInt()) - // Clé matérielle Qualcomm (Snapdragon 8 Gen 1 / Tab S8 Ultra) pour zéro frame buffering + // Clés matérielles Qualcomm (Snapdragon 8 Gen 1 / Tab S8 Ultra) : zéro buffering + sortie en ordre de décodage setInteger("vendor.qti-ext-dec-low-latency.enable", 1) + setInteger("vendor.qti-ext-dec-picture-order.enable", 1) } mc.configure(format, targetSurface, null, 0) mc.start() @@ -93,7 +125,7 @@ class LowLatencyVideoDecoder(private val context: Context) { priority = Thread.MAX_PRIORITY start() } - Log.i(TAG, "MediaCodec démarré ($mimeType, Low-Latency Snapdragon actif)") + Log.i(TAG, "MediaCodec démarré (${mc.name}, $mimeType, Low-Latency + Picture-Order actif)") } catch (e: Exception) { Log.e(TAG, "Échec initialisation MediaCodec ($mimeType): ${e.message}") releaseCodecLocked() @@ -159,8 +191,8 @@ class LowLatencyVideoDecoder(private val context: Context) { try { val outIdx = mc.dequeueOutputBuffer(info, 10_000) if (outIdx >= 0) { - // render = true -> affichage immédiat sur SurfaceView sans attente de PTS (latence minimale) - mc.releaseOutputBuffer(outIdx, true) + // Présentation immédiate sur le tout prochain VSync 120 Hz (zéro attente PTS) + mc.releaseOutputBuffer(outIdx, System.nanoTime()) } } catch (_: IllegalStateException) { break