From 69e15fa2d1d6ac23515ff245da47b0c6a535978d Mon Sep 17 00:00:00 2001 From: Seb3thehacker Date: Wed, 25 Nov 2026 12:51:10 -0800 Subject: [PATCH] Cooler video: about 0.12 bits per pixel (3.3 Mbps at 720p30, 7.5 at 1080p30, never over 8) instead of up to 25 Mbps, the progress wave holds still instead of redrawing the car screen every frame while music plays, the play time updates once a second, the map buttons' hide timer sleeps until it's due, and speech uses Google's text-to-speech whenever it's installed --- .../app/seb3thehacker/gearslip/VideoSource.kt | 44 +++++++++++-------- .../gearslip/car/CarAssistant.kt | 2 +- .../gearslip/car/CarFullscreen.kt | 6 ++- .../app/seb3thehacker/gearslip/car/CarUi.kt | 12 ++++- .../gearslip/car/DirectionsVoice.kt | 3 +- .../gearslip/car/MapControlsWake.kt | 11 ++++- .../seb3thehacker/gearslip/car/PlayerParts.kt | 4 +- .../gearslip/car/theme/GsSeekBar.kt | 17 +++---- .../seb3thehacker/gearslip/speech/Voices.kt | 24 ++++++++++ 9 files changed, 87 insertions(+), 36 deletions(-) create mode 100644 gearslip/src/main/java/app/seb3thehacker/gearslip/speech/Voices.kt diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/VideoSource.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/VideoSource.kt index 00fb570..3330171 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/VideoSource.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/VideoSource.kt @@ -65,8 +65,8 @@ class VideoSource( fun start() { val level = levelFor(width, height, frameRate) val bitRate = defaultBitRate(width, height, frameRate, level).let { - if (wireless) (it * WIRELESS_SHARE).toInt().coerceAtMost(WIRELESS_MAX_BIT_RATE) else it - }.let { (it * bitRateShare).toInt() } + if (wireless) it.coerceAtMost(WIRELESS_MAX_BIT_RATE) else it + }.let { (it * bitRateShare).toInt().coerceAtLeast(LIGHT_MIN_BIT_RATE) } fullBitRate = bitRate this.bitRate = bitRate fun format(tuned: Boolean) = MediaFormat.createVideoFormat(MIME, width, height).apply { @@ -277,15 +277,22 @@ class VideoSource( private const val STILL_MS = 1_000L /** - * 800x480 at 30fps gets 8 Mbps: about the most that size can show, enough that a moving - * video in the browser stays sharp. Bigger screens get more, but less than in proportion, - * since large frames compress better: bitrate grows with pixels^0.75. 60fps gets 1.5x, - * because frames that close together are mostly alike. + * About 0.12 bits per pixel per frame, between [MIN_BIT_RATE] and a cap set by the screen's + * short side: 720p30 comes out near 3.3 Mbps and 1080p30 near 7.5 Mbps. A car's screen is + * mostly flat colour and text, which stays sharp at that. Gearslip used to send two to + * three times as much (20 Mbps at 1080p30), and every byte is encrypted on the phone before + * it goes out, so the extra only made the phone hotter. */ - const val BASE_BIT_RATE = 8_000_000.0 - const val BASE_PIXELS = 800.0 * 480 - /** No car's decoder is asked for more than this, whatever its level allows. */ - const val MAX_BIT_RATE = 25_000_000 + const val BITS_PER_PIXEL = 0.12 + /** The least Light video quality goes to: below this, text smears whenever the screen moves. */ + const val LIGHT_MIN_BIT_RATE = 1_500_000 + const val MIN_BIT_RATE = 2_000_000 + + fun capFor(width: Int, height: Int): Int = when { + minOf(width, height) <= 480 -> 4_000_000 + minOf(width, height) <= 720 -> 6_000_000 + else -> 8_000_000 + } /** * The H.264 levels a car screen can need, smallest first: macroblocks a second each @@ -305,18 +312,17 @@ class VideoSource( return Level.entries.firstOrNull { perSecond <= it.macroblocksPerSecond } ?: Level.L42 } - fun defaultBitRate(width: Int, height: Int, frameRate: Int, level: Level): Int { - val size = Math.pow(width.toDouble() * height / BASE_PIXELS, 0.75) - val rate = if (frameRate > 30) 1.5 else 1.0 - return (BASE_BIT_RATE * size * rate).toInt().coerceAtMost(minOf(level.maxBitRate, MAX_BIT_RATE)) - } + fun defaultBitRate(width: Int, height: Int, frameRate: Int, level: Level): Int = + (width.toDouble() * height * frameRate * BITS_PER_PIXEL).toInt() + .coerceIn(MIN_BIT_RATE, capFor(width, height)) + .coerceAtMost(level.maxBitRate) + /** * A car's Wi-Fi is shared, noisy and far slower than its nominal rate, and the sound rides - * the same link behind the video. Real Android Auto sends a few Mbps wirelessly; at the - * cable rate (15-20 Mbps for 720p) a laptop hotspot fell seconds behind and the music - * cut in and out. 720p30 comes out near 5.4 Mbps. + * the same link behind the video. At the old cable rate (15-20 Mbps for 720p) a laptop + * hotspot fell seconds behind and the music cut in and out. The rates above already sit + * where real Android Auto's wireless ones do, so Wi-Fi only gets this cap. */ - const val WIRELESS_SHARE = 0.35 const val WIRELESS_MAX_BIT_RATE = 8_000_000 const val I_FRAME_INTERVAL_S = 5 private const val REPEAT_AFTER_US = 100_000L diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAssistant.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAssistant.kt index 102b202..4b72504 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAssistant.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarAssistant.kt @@ -77,7 +77,7 @@ object CarAssistant { fun init(context: Context) { appContext = context.applicationContext if (tts != null) return - tts = TextToSpeech(appContext) { status -> ttsReady = status == TextToSpeech.SUCCESS } + tts = app.seb3thehacker.gearslip.speech.Voices.create(context.applicationContext) { status -> ttsReady = status == TextToSpeech.SUCCESS } } private val mainHandler = Handler(Looper.getMainLooper()) diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarFullscreen.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarFullscreen.kt index e9d0924..c724428 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarFullscreen.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarFullscreen.kt @@ -42,10 +42,14 @@ object CarFullscreen { if (revealed) lastTouch = SystemClock.uptimeMillis() } - /** Called on a timer; hides the bar again after [REVEAL_MS] without a touch. */ + /** Hides the bar again after [REVEAL_MS] without a touch. */ fun tick() { if (revealed && SystemClock.uptimeMillis() - lastTouch >= REVEAL_MS) revealed = false } + /** How long until [tick] would hide the bar; null when it isn't showing. */ + fun msUntilChange(): Long? = + if (revealed) (REVEAL_MS - (SystemClock.uptimeMillis() - lastTouch)).coerceAtLeast(0) else null + private const val REVEAL_MS = 4_000L } diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarUi.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarUi.kt index cbe3175..37170ac 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarUi.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/CarUi.kt @@ -97,6 +97,7 @@ import app.seb3thehacker.gearslip.notify.CarNotifications import androidx.core.graphics.drawable.toBitmap import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.first import kotlinx.coroutines.withContext import java.text.DateFormat import java.util.Date @@ -133,10 +134,19 @@ fun CarUi() { } val controlsWake = remember { MapControlsWake() } LaunchedEffect(controlsWake) { + // Sleeps until the next thing is due to hide, rather than waking four times a second all + // drive; with everything hidden, until a touch shows something again. A touch in the + // meantime only pushes the deadline back, which the next pass picks up. while (true) { - delay(250) controlsWake.tick() CarFullscreen.tick() + val wait = listOfNotNull(controlsWake.msUntilChange(), CarFullscreen.msUntilChange()).minOrNull() + if (wait == null) { + androidx.compose.runtime.snapshotFlow { controlsWake.awake || controlsWake.navBarAwake || CarFullscreen.revealed } + .first { it } + } else { + delay(wait + 20) + } } } // The knob counts as a touch: the map's buttons can't be found while they're faded out. diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/DirectionsVoice.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/DirectionsVoice.kt index 5283dda..db6b12f 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/DirectionsVoice.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/DirectionsVoice.kt @@ -69,9 +69,10 @@ object DirectionsVoice { } private fun start() { + val context = appContext ?: return ttsReady = false ttsStartedAt = SystemClock.elapsedRealtime() - tts = TextToSpeech(appContext) { status -> + tts = app.seb3thehacker.gearslip.speech.Voices.create(context) { status -> ttsReady = status == TextToSpeech.SUCCESS if (!ttsReady) GearslipLog.w("guidance: text-to-speech didn't start (status $status)") // The turns are worded in Gearslip's language; the engine's default voice may be diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/MapControlsWake.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/MapControlsWake.kt index eacb9ed..aff128e 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/MapControlsWake.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/MapControlsWake.kt @@ -30,13 +30,22 @@ class MapControlsWake { if (!navBarAwake) navBarAwake = true } - /** Called on a timer; hides the buttons once nothing has touched the screen for [IDLE_MS]. */ + /** Hides the buttons once nothing has touched the screen for [IDLE_MS]. */ fun tick() { val idle = SystemClock.uptimeMillis() - lastTouch if (awake && idle >= IDLE_MS) awake = false if (navBarAwake && idle >= NAV_BAR_IDLE_MS) navBarAwake = false } + /** How long until [tick] would hide something; null when everything's already hidden. */ + fun msUntilChange(): Long? { + val idle = SystemClock.uptimeMillis() - lastTouch + return listOfNotNull( + (IDLE_MS - idle).takeIf { awake }, + (NAV_BAR_IDLE_MS - idle).takeIf { navBarAwake }, + ).minOrNull()?.coerceAtLeast(0) + } + companion object { const val IDLE_MS = 4_000L const val NAV_BAR_IDLE_MS = 8_000L diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/PlayerParts.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/PlayerParts.kt index 5888916..1ad47cd 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/PlayerParts.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/PlayerParts.kt @@ -74,10 +74,12 @@ internal fun PlayerProgress( if (now.durationMs <= 0) return var position by remember { mutableLongStateOf(now.currentPosition()) } LaunchedEffect(now) { + // Once a second, on the second, so the time shown ticks over when it should; the bar + // moves a pixel or two a second anyway, and each change is a frame the car is sent. while (true) { position = now.currentPosition() if (!now.playing) break - delay(500) + delay(1_000 - position % 1_000) } } Column(modifier) { diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/theme/GsSeekBar.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/theme/GsSeekBar.kt index 23abdb3..ef6f7af 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/car/theme/GsSeekBar.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/car/theme/GsSeekBar.kt @@ -1,11 +1,5 @@ package app.seb3thehacker.gearslip.car.theme -import androidx.compose.animation.core.LinearEasing -import androidx.compose.animation.core.RepeatMode -import androidx.compose.animation.core.animateFloat -import androidx.compose.animation.core.infiniteRepeatable -import androidx.compose.animation.core.rememberInfiniteTransition -import androidx.compose.animation.core.tween import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.gestures.detectHorizontalDragGestures @@ -101,14 +95,15 @@ fun GsProgressBar( /** * Material 3's wavy progress bar, drawn here because the stable library doesn't offer it yet: the - * filled part is a sine wave that drifts along, then a gap, then a straight track and a stop dot - * at the end. + * filled part is a sine wave, then a gap, then a straight track and a stop dot at the end. + * + * The wave holds still. Drifting, it changed the car screen every frame for as long as music + * played, so the phone drew, encoded, encrypted and sent 30 to 60 frames a second where a still + * screen needs about 10 small ones, and phones ran warm. */ @Composable private fun WavyBar(fraction: Float, color: Color, track: Color, modifier: Modifier) { - val phase by rememberInfiniteTransition(label = "wave").animateFloat( - 0f, 1f, infiniteRepeatable(tween(1_000, easing = LinearEasing), RepeatMode.Restart), label = "phase", - ) + val phase = 0f Canvas(modifier) { val stroke = 4.dp.toPx() val amplitude = (size.height / 2 - stroke).coerceIn(0f, 3.dp.toPx()) diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/speech/Voices.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/speech/Voices.kt new file mode 100644 index 0000000..489266b --- /dev/null +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/speech/Voices.kt @@ -0,0 +1,24 @@ +package app.seb3thehacker.gearslip.speech + +import android.content.Context +import android.content.Intent +import android.speech.tts.TextToSpeech + +/** + * The text-to-speech engine Gearslip speaks with: Google's when it's installed, since it sounds + * the most natural and reads place names best, otherwise whatever the phone has (GrapheneOS ships + * its own, and some phones another maker's). + */ +object Voices { + + const val GOOGLE = "com.google.android.tts" + + /** Google's engine if the phone has it, else null for the phone's default. */ + fun engine(context: Context): String? { + val engines = context.packageManager.queryIntentServices(Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE), 0) + return GOOGLE.takeIf { google -> engines.any { it.serviceInfo?.packageName == google } } + } + + fun create(context: Context, onInit: TextToSpeech.OnInitListener): TextToSpeech = + TextToSpeech(context, onInit, engine(context)) +}