Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ internal class CarlifeSession(
private var activeContacts: List<TouchContact> = emptyList()
private var lastSummary = createdAt
private var knownFormat: VideoFormat? = null
private var displaySize: Pair<Int, Int>? = null
private data class VideoNegotiation(val requestedSize: Viewport, val frameSize: Viewport)
private var videoNegotiation: VideoNegotiation? = null
@Volatile var audio: CarlifeAudio? = null
private set
val carriesAudio: Boolean get() = connected(3)
Expand Down Expand Up @@ -120,7 +121,7 @@ internal class CarlifeSession(
diagnostic("transport_opened peer=$peer channel=${connection.channel} transport=${if (connection.usb) "usb" else "tcp"}")
if (connection.usb || connection.channel in 3..5) {
if (connection.usb || connection.channel == 3 || connection.channel == 5) {
displaySize?.let { (w, h) -> publishCapabilities(w, h) }
publishCapabilities()
}
wakeAudio()
}
Expand Down Expand Up @@ -227,19 +228,23 @@ internal class CarlifeSession(
val requestedHeight = rawHeight!!.toInt()
val scale = minOf(1.0, config.maxWidth.toDouble() / requestedWidth,
config.maxHeight.toDouble() / requestedHeight)
width = ((requestedWidth * scale).toInt() / 2 * 2).coerceAtLeast(16)
height = ((requestedHeight * scale).toInt() / 2 * 2).coerceAtLeast(16)
sourceFormat()?.takeIf { it.codec == VideoCodec.H264 && it.width <= 1920 && it.height <= 1080 }?.let {
width = it.width
height = it.height
}
val fittedFrame = Viewport(
((requestedWidth * scale).toInt() / 2 * 2).coerceAtLeast(16),
((requestedHeight * scale).toInt() / 2 * 2).coerceAtLeast(16),
)
val source = sourceFormat()?.takeIf { it.codec == VideoCodec.H264 && supportsSourceSize(it) }
width = source?.width ?: fittedFrame.width
height = source?.height ?: fittedFrame.height
val requestedFps = payload.number(3)?.toInt() ?: 0
fps = if (requestedFps > 0) requestedFps.coerceIn(1, config.fps) else config.fps
encoderChannel = message.channel
diagnostic("video_negotiated requested=${requestedWidth}x$requestedHeight selected=${width}x$height fps=$fps codec=H264")
val sizeSource = if (source == null) "headunit_request" else "existing_h264_source"
diagnostic("video_negotiated requested=${requestedWidth}x$requestedHeight selected=${width}x$height fps=$fps codec=H264 " +
"sizeSource=$sizeSource frameSize=${fittedFrame.width}x${fittedFrame.height}")
stage("video_negotiated")
displaySize = requestedWidth to requestedHeight
publishCapabilities(requestedWidth, requestedHeight)
// The relay uses the fitted head-unit request as its target picture size.
videoNegotiation = VideoNegotiation(Viewport(requestedWidth, requestedHeight), fittedFrame)
publishCapabilities()
if (encoderInfo()) establishAudio()
report(CarlifeState.AUTHENTICATED)
}
Expand Down Expand Up @@ -384,19 +389,23 @@ internal class CarlifeSession(
stage("device_sent")
}

private fun publishCapabilities(displayWidth: Int, displayHeight: Int) {
private fun publishCapabilities() {
val (requestedSize, frameSize) = videoNegotiation ?: return
host.capabilities(OutputCapabilities(LatencyClass.INTERACTIVE,
listOf(DecoderSupport("avc1.64002a", true), DecoderSupport("avc1.42e01f", true)),
mapOf(ScreenId.MAIN to DisplayInfo(displayWidth, displayHeight, 160, fps.toDouble(),
frameSize = Viewport(width, height))), touch = 10, keys = KeyCode.entries.toSet(),
mapOf(ScreenId.MAIN to DisplayInfo(requestedSize.width, requestedSize.height, 160, fps.toDouble(),
frameSize = frameSize)), touch = 10, keys = KeyCode.entries.toSet(),
audio = if (carriesAudio) setOf(AudioCodec.PCM_S16LE) else emptySet(),
microphone = carriesAudio && connected(5), prefersH264 = true))
}

private fun supportsSourceSize(format: VideoFormat): Boolean =
maxOf(format.width, format.height) <= 1920 && minOf(format.width, format.height) <= 1080

fun format(format: VideoFormat) {
if (!authenticated || width == 0) return
protocolRequire(format.codec == VideoCodec.H264, "unsupported_source_codec")
protocolRequire(format.width <= 1920 && format.height <= 1080, "unsupported_source_size")
protocolRequire(supportsSourceSize(format), "unsupported_source_size")
if (knownFormat !== format) {
knownFormat = format
diagnostic("video_source codec=${format.codec} size=${format.width}x${format.height} fpsHint=${format.fpsHint ?: 0.0}")
Expand Down Expand Up @@ -537,6 +546,7 @@ internal class CarlifeSession(

fun close(cause: String = "local_close") {
if (active.compareAndSet(true, false)) {
videoNegotiation = null
val root = rootFailure.get()
val endedCause = root?.cause ?: cause
val rootChannel = root?.channel ?: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,37 @@ class CarlifeSessionTest {
assertEquals(480L, info.number(2))
}

@Test fun frameSizeFollowsTheHeadUnitRequestWhileEncoderInfoFollowsTheSource() {
val hex = "01 42 c0 28 ff e1 00 18 67 42 c0 28 d9 00 e4 0f 1e fc 04 40 00 00 03 00 40 00 00 0f 03 c6 0c 92 " +
"01 00 05 68 cb 83 cb 20"
val source = VideoFormat.create(ScreenId.MAIN, VideoCodec.H264,
hex.split(' ').map { it.toInt(16).toByte() }.toByteArray(), 30.0)
val fixture = Fixture(source)
fixture.authenticate()
fixture.negotiate(2322, 1038)

val display = fixture.host.caps.single().displays.getValue(ScreenId.MAIN)
assertEquals(2322, display.widthPx)
assertEquals(1038, display.heightPx)
assertEquals(Viewport(1920, 858), display.frameSize)
assertTrue(fixture.diagnostics.any { it.contains("sizeSource=existing_h264_source frameSize=1920x858") })

fixture.session.format(source)
val initial = Proto.parse(fixture.messages().single { it.id == 65544 }.payload)
assertEquals(908L, initial.number(1))
assertEquals(1920L, initial.number(2))

fixture.session.format(testFormat())
val updated = Proto.parse(fixture.messages().last { it.id == 65544 }.payload)
assertEquals(640L, updated.number(1))
assertEquals(480L, updated.number(2))

fixture.session.attach(CarlifeConnection(ByteArrayInputStream(byteArrayOf()),
ByteArrayOutputStream(), Closeable {}, false, 3))
assertEquals(display, fixture.host.caps.last().displays.getValue(ScreenId.MAIN))
assertEquals(2, fixture.host.caps.size)
}

@Test fun changedActualFormatRequiresReceiverRestartBeforeMoreFrames() {
val fixture = Fixture()
fixture.authenticate()
Expand Down
Loading