diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt
index 03ad430..67e66f0 100644
--- a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt
+++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt
@@ -232,6 +232,8 @@ class GearslipRunner(
CarFocus.request = null
CarFocus.set(true)
synchronized(ackLock) { ackLock.notifyAll() } // free an encoder waiting on an ack
+ hotPhone?.stop()
+ if (hotPhone?.ranHot == true) log.i("phone heat: this drive ran hot - Video quality Light would spare the phone")
videoSource?.stop()
videoSource = null
stretcher?.release()
@@ -1463,8 +1465,17 @@ class GearslipRunner(
}
}.onFailure { e -> log.e("encoder start failed", e) }
}
+ if (hotPhone == null) {
+ hotPhone = app.seb3thehacker.gearslip.car.CarEnvironment.appContext
+ ?.let { HotPhone(it) { videoSource } }
+ ?.also { it.start() }
+ }
+ hotPhone?.apply()
}
+ /** Lowers the bitrate while the phone is hot: see [HotPhone]. */
+ @Volatile private var hotPhone: HotPhone? = null
+
/**
* Flow control: the head unit allows max_unacked frames in flight (LIVI allows one). When
* the window is full this waits for an ack instead of dropping the frame. A dropped frame
@@ -1892,7 +1903,7 @@ class GearslipRunner(
log.i("video: car falling behind - bitrate ${before / 1000} -> ${source.bitRate / 1000} kbps")
}
}
- easy && source.bitRate < source.fullBitRate -> {
+ easy && source.bitRate < minOf(source.fullBitRate, hotPhone?.cap ?: Int.MAX_VALUE) -> {
lastCut = null
if (++smoothWindows >= 2) {
smoothWindows = 0
diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/HotPhone.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/HotPhone.kt
new file mode 100644
index 0000000..3040812
--- /dev/null
+++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/HotPhone.kt
@@ -0,0 +1,79 @@
+package app.seb3thehacker.gearslip
+
+import android.content.Context
+import android.os.PowerManager
+import androidx.car.app.CarToast
+
+/**
+ * Eases the video off when the phone gets hot. A phone in a dash mount, charging, in the sun, with
+ * the encoder running, heats up, and once Android throttles it the picture stutters with no clue
+ * why. Android reports how hot it is, so from Severe the bitrate drops to Light quality's share,
+ * and from Critical lower still; the car screen says so once. Once the phone has cooled to Light
+ * or below, the bitrate goes back. Resolution and frame rate stay as the drive started: changing
+ * them means starting the video over.
+ */
+internal class HotPhone(
+ private val context: Context,
+ private val source: () -> VideoSource?,
+) {
+ private val log = GearslipLog.tagged("PROTO")
+ private val power = context.getSystemService(PowerManager::class.java)
+ private val listener = PowerManager.OnThermalStatusChangedListener(::onStatus)
+
+ /** The most the bitrate may be while the phone is hot, null when it's cool enough. */
+ @Volatile var cap: Int? = null
+ private set
+
+ /** Whether this drive ran hot at all, for the session report. */
+ @Volatile var ranHot = false
+ private set
+
+ private var toldDriver = false
+
+ fun start() {
+ runCatching { power?.addThermalStatusListener(context.mainExecutor, listener) }
+ .onFailure { log.w("phone heat: can't watch it - ${it.message}") }
+ }
+
+ fun stop() {
+ runCatching { power?.removeThermalStatusListener(listener) }
+ }
+
+ /** A new video source started: apply the cap to it too. */
+ fun apply() {
+ val c = cap ?: return
+ source()?.let { if (it.bitRate > c) it.setBitRate(c) }
+ }
+
+ private fun onStatus(status: Int) {
+ val video = source() ?: return
+ val full = video.fullBitRate
+ val share = when {
+ status >= PowerManager.THERMAL_STATUS_CRITICAL -> CRITICAL_SHARE
+ status >= PowerManager.THERMAL_STATUS_SEVERE -> GearslipRunner.LIGHT_BIT_RATE_SHARE
+ status <= PowerManager.THERMAL_STATUS_LIGHT -> null
+ else -> return // Moderate: keep whatever is set, so it doesn't flap at the edge.
+ }
+ if (share == null) {
+ if (cap == null) return
+ cap = null
+ video.setBitRate(full)
+ log.i("phone heat: cooled down (status $status) - bitrate back to ${full / 1000} kbps")
+ return
+ }
+ val newCap = (full * share).toInt()
+ if (cap == newCap) return
+ cap = newCap
+ ranHot = true
+ if (video.bitRate > newCap) video.setBitRate(newCap)
+ log.w("phone heat: status $status - bitrate capped at ${newCap / 1000} kbps")
+ if (!toldDriver) {
+ toldDriver = true
+ app.seb3thehacker.gearslip.car.CarToasts.show(this, context.getString(R.string.hot_phone_toast), CarToast.LENGTH_LONG)
+ }
+ }
+
+ private companion object {
+ const val CRITICAL_SHARE = 0.4f
+ }
+}
diff --git a/gearslip/src/main/res/values/strings.xml b/gearslip/src/main/res/values/strings.xml
index 83b4493..3a4450d 100644
--- a/gearslip/src/main/res/values/strings.xml
+++ b/gearslip/src/main/res/values/strings.xml
@@ -986,6 +986,8 @@
Car won\'t connect? Some cars from around 2020 on reject the aasdk certificate, so import your own instead. Changes take effect on the next connection.
+
+ The phone is hot, so the picture is lighter until it cools
Turn card on the cluster screen
For a car with a second screen in the instrument cluster: Gearslip shows the next turn there. Applies on the next connection.