mirror of
https://github.com/twinksoftware/Gearslip.git
synced 2026-12-01 15:40:32 +01:00
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
This commit is contained in:
1 parent
aad5c449e8
commit
69e15fa2d1
9 files changed
+87
-36
No files matched your search
@@ -65,8 +65,8 @@ class VideoSource(
|
|||||||
fun start() {
|
fun start() {
|
||||||
val level = levelFor(width, height, frameRate)
|
val level = levelFor(width, height, frameRate)
|
||||||
val bitRate = defaultBitRate(width, height, frameRate, level).let {
|
val bitRate = defaultBitRate(width, height, frameRate, level).let {
|
||||||
if (wireless) (it * WIRELESS_SHARE).toInt().coerceAtMost(WIRELESS_MAX_BIT_RATE) else it
|
if (wireless) it.coerceAtMost(WIRELESS_MAX_BIT_RATE) else it
|
||||||
}.let { (it * bitRateShare).toInt() }
|
}.let { (it * bitRateShare).toInt().coerceAtLeast(LIGHT_MIN_BIT_RATE) }
|
||||||
fullBitRate = bitRate
|
fullBitRate = bitRate
|
||||||
this.bitRate = bitRate
|
this.bitRate = bitRate
|
||||||
fun format(tuned: Boolean) = MediaFormat.createVideoFormat(MIME, width, height).apply {
|
fun format(tuned: Boolean) = MediaFormat.createVideoFormat(MIME, width, height).apply {
|
||||||
@@ -277,15 +277,22 @@ class VideoSource(
|
|||||||
private const val STILL_MS = 1_000L
|
private const val STILL_MS = 1_000L
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 800x480 at 30fps gets 8 Mbps: about the most that size can show, enough that a moving
|
* About 0.12 bits per pixel per frame, between [MIN_BIT_RATE] and a cap set by the screen's
|
||||||
* video in the browser stays sharp. Bigger screens get more, but less than in proportion,
|
* short side: 720p30 comes out near 3.3 Mbps and 1080p30 near 7.5 Mbps. A car's screen is
|
||||||
* since large frames compress better: bitrate grows with pixels^0.75. 60fps gets 1.5x,
|
* mostly flat colour and text, which stays sharp at that. Gearslip used to send two to
|
||||||
* because frames that close together are mostly alike.
|
* 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 BITS_PER_PIXEL = 0.12
|
||||||
const val BASE_PIXELS = 800.0 * 480
|
/** The least Light video quality goes to: below this, text smears whenever the screen moves. */
|
||||||
/** No car's decoder is asked for more than this, whatever its level allows. */
|
const val LIGHT_MIN_BIT_RATE = 1_500_000
|
||||||
const val MAX_BIT_RATE = 25_000_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
|
* 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
|
return Level.entries.firstOrNull { perSecond <= it.macroblocksPerSecond } ?: Level.L42
|
||||||
}
|
}
|
||||||
|
|
||||||
fun defaultBitRate(width: Int, height: Int, frameRate: Int, level: Level): Int {
|
fun defaultBitRate(width: Int, height: Int, frameRate: Int, level: Level): Int =
|
||||||
val size = Math.pow(width.toDouble() * height / BASE_PIXELS, 0.75)
|
(width.toDouble() * height * frameRate * BITS_PER_PIXEL).toInt()
|
||||||
val rate = if (frameRate > 30) 1.5 else 1.0
|
.coerceIn(MIN_BIT_RATE, capFor(width, height))
|
||||||
return (BASE_BIT_RATE * size * rate).toInt().coerceAtMost(minOf(level.maxBitRate, MAX_BIT_RATE))
|
.coerceAtMost(level.maxBitRate)
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* A car's Wi-Fi is shared, noisy and far slower than its nominal rate, and the sound rides
|
* 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
|
* the same link behind the video. At the old cable rate (15-20 Mbps for 720p) a laptop
|
||||||
* cable rate (15-20 Mbps for 720p) a laptop hotspot fell seconds behind and the music
|
* hotspot fell seconds behind and the music cut in and out. The rates above already sit
|
||||||
* cut in and out. 720p30 comes out near 5.4 Mbps.
|
* 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 WIRELESS_MAX_BIT_RATE = 8_000_000
|
||||||
const val I_FRAME_INTERVAL_S = 5
|
const val I_FRAME_INTERVAL_S = 5
|
||||||
private const val REPEAT_AFTER_US = 100_000L
|
private const val REPEAT_AFTER_US = 100_000L
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ object CarAssistant {
|
|||||||
fun init(context: Context) {
|
fun init(context: Context) {
|
||||||
appContext = context.applicationContext
|
appContext = context.applicationContext
|
||||||
if (tts != null) return
|
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())
|
private val mainHandler = Handler(Looper.getMainLooper())
|
||||||
|
|||||||
@@ -42,10 +42,14 @@ object CarFullscreen {
|
|||||||
if (revealed) lastTouch = SystemClock.uptimeMillis()
|
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() {
|
fun tick() {
|
||||||
if (revealed && SystemClock.uptimeMillis() - lastTouch >= REVEAL_MS) revealed = false
|
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
|
private const val REVEAL_MS = 4_000L
|
||||||
}
|
}
|
||||||
@@ -97,6 +97,7 @@ import app.seb3thehacker.gearslip.notify.CarNotifications
|
|||||||
import androidx.core.graphics.drawable.toBitmap
|
import androidx.core.graphics.drawable.toBitmap
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.text.DateFormat
|
import java.text.DateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
@@ -133,10 +134,19 @@ fun CarUi() {
|
|||||||
}
|
}
|
||||||
val controlsWake = remember { MapControlsWake() }
|
val controlsWake = remember { MapControlsWake() }
|
||||||
LaunchedEffect(controlsWake) {
|
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) {
|
while (true) {
|
||||||
delay(250)
|
|
||||||
controlsWake.tick()
|
controlsWake.tick()
|
||||||
CarFullscreen.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.
|
// The knob counts as a touch: the map's buttons can't be found while they're faded out.
|
||||||
|
|||||||
@@ -69,9 +69,10 @@ object DirectionsVoice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun start() {
|
private fun start() {
|
||||||
|
val context = appContext ?: return
|
||||||
ttsReady = false
|
ttsReady = false
|
||||||
ttsStartedAt = SystemClock.elapsedRealtime()
|
ttsStartedAt = SystemClock.elapsedRealtime()
|
||||||
tts = TextToSpeech(appContext) { status ->
|
tts = app.seb3thehacker.gearslip.speech.Voices.create(context) { status ->
|
||||||
ttsReady = status == TextToSpeech.SUCCESS
|
ttsReady = status == TextToSpeech.SUCCESS
|
||||||
if (!ttsReady) GearslipLog.w("guidance: text-to-speech didn't start (status $status)")
|
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
|
// The turns are worded in Gearslip's language; the engine's default voice may be
|
||||||
|
|||||||
@@ -30,13 +30,22 @@ class MapControlsWake {
|
|||||||
if (!navBarAwake) navBarAwake = true
|
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() {
|
fun tick() {
|
||||||
val idle = SystemClock.uptimeMillis() - lastTouch
|
val idle = SystemClock.uptimeMillis() - lastTouch
|
||||||
if (awake && idle >= IDLE_MS) awake = false
|
if (awake && idle >= IDLE_MS) awake = false
|
||||||
if (navBarAwake && idle >= NAV_BAR_IDLE_MS) navBarAwake = 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 {
|
companion object {
|
||||||
const val IDLE_MS = 4_000L
|
const val IDLE_MS = 4_000L
|
||||||
const val NAV_BAR_IDLE_MS = 8_000L
|
const val NAV_BAR_IDLE_MS = 8_000L
|
||||||
|
|||||||
@@ -74,10 +74,12 @@ internal fun PlayerProgress(
|
|||||||
if (now.durationMs <= 0) return
|
if (now.durationMs <= 0) return
|
||||||
var position by remember { mutableLongStateOf(now.currentPosition()) }
|
var position by remember { mutableLongStateOf(now.currentPosition()) }
|
||||||
LaunchedEffect(now) {
|
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) {
|
while (true) {
|
||||||
position = now.currentPosition()
|
position = now.currentPosition()
|
||||||
if (!now.playing) break
|
if (!now.playing) break
|
||||||
delay(500)
|
delay(1_000 - position % 1_000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Column(modifier) {
|
Column(modifier) {
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
package app.seb3thehacker.gearslip.car.theme
|
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.Canvas
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
|
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
|
* 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
|
* filled part is a sine wave, then a gap, then a straight track and a stop dot at the end.
|
||||||
* 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
|
@Composable
|
||||||
private fun WavyBar(fraction: Float, color: Color, track: Color, modifier: Modifier) {
|
private fun WavyBar(fraction: Float, color: Color, track: Color, modifier: Modifier) {
|
||||||
val phase by rememberInfiniteTransition(label = "wave").animateFloat(
|
val phase = 0f
|
||||||
0f, 1f, infiniteRepeatable(tween(1_000, easing = LinearEasing), RepeatMode.Restart), label = "phase",
|
|
||||||
)
|
|
||||||
Canvas(modifier) {
|
Canvas(modifier) {
|
||||||
val stroke = 4.dp.toPx()
|
val stroke = 4.dp.toPx()
|
||||||
val amplitude = (size.height / 2 - stroke).coerceIn(0f, 3.dp.toPx())
|
val amplitude = (size.height / 2 - stroke).coerceIn(0f, 3.dp.toPx())
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
Reference in new issue
Block a user