When the phone gets hot, the video's bitrate drops until it cools, and the car screen says so once

This commit is contained in:
Seb3thehacker committed 2026-11-29 16:29:58 -08:00
1 parent 21652333ec
commit db5025052b
3 files changed
+93 -1

No files matched your search

@@ -232,6 +232,8 @@ class GearslipRunner(
CarFocus.request = null CarFocus.request = null
CarFocus.set(true) CarFocus.set(true)
synchronized(ackLock) { ackLock.notifyAll() } // free an encoder waiting on an ack 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?.stop()
videoSource = null videoSource = null
stretcher?.release() stretcher?.release()
@@ -1463,8 +1465,17 @@ class GearslipRunner(
} }
}.onFailure { e -> log.e("encoder start failed", e) } }.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 * 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 * 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") 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 lastCut = null
if (++smoothWindows >= 2) { if (++smoothWindows >= 2) {
smoothWindows = 0 smoothWindows = 0
@@ -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
}
}
+2
View File
@@ -986,6 +986,8 @@
<!-- --> <!-- -->
<string name="cert_downloaded_footer">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.</string> <string name="cert_downloaded_footer">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.</string>
<!-- Shown on the car screen once a drive, when the phone gets hot and Gearslip lowers the picture's quality to cool it. -->
<string name="hot_phone_toast">The phone is hot, so the picture is lighter until it cools</string>
<!-- Cluster screen --> <!-- Cluster screen -->
<string name="car_cluster_video">Turn card on the cluster screen</string> <string name="car_cluster_video">Turn card on the cluster screen</string>
<string name="car_cluster_video_note">For a car with a second screen in the instrument cluster: Gearslip shows the next turn there. Applies on the next connection.</string> <string name="car_cluster_video_note">For a car with a second screen in the instrument cluster: Gearslip shows the next turn there. Applies on the next connection.</string>