diff --git a/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/ClinkOutput.kt b/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/ClinkOutput.kt index fc2b2f2..7395f80 100644 --- a/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/ClinkOutput.kt +++ b/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/ClinkOutput.kt @@ -15,6 +15,7 @@ import org.openxplay.stream.clink.media.RtpSender import org.openxplay.stream.core.* import org.openxplay.stream.core.audio.Adts import org.openxplay.stream.core.audio.AudioSpecificConfig +import org.openxplay.stream.core.audio.opensVoiceChannel import org.openxplay.stream.core.codec.HevcConfig import org.openxplay.stream.core.codec.AvcConfig import org.openxplay.stream.core.codec.Nal @@ -81,10 +82,12 @@ public class ClinkOutput( @Volatile private var lastAudioPts = -1L /** - * Only voice and phone calls open the head unit's voice channel: system sounds such as keyboard - * clicks sent here made Carlink duck or pause media (2026-10-19). Other roles are not delivered here. + * Only voice and phone calls open the head unit's voice channel. A navigation prompt must not: + * on the G6 the voice channel pauses media playback, and the 2026-11-11 trace shows it does not + * always resume afterwards — one prompt left the car silent for the remaining 147 s while music + * kept arriving at 47 access units a second. The relay mixes the prompt into the music instead, + * so it never reaches this side. See [opensVoiceChannel]. */ - private fun voiceRole(role: AudioRole): Boolean = role == AudioRole.VOICE || role == AudioRole.TELEPHONY private val opened = AtomicBoolean(false) private fun endWithoutSession(run: Run, event: String, reason: ClinkEndReason) { if (run.endReported.compareAndSet(false, true)) runCatching { @@ -222,37 +225,50 @@ public class ClinkOutput( audioOffset = null; lastAudioPts = -1 } run.voiceStreams.remove(stream) - if (format != null && voiceRole(format.role) && format.codec == AudioCodec.PCM_S16LE && - format.sampleRate in 8000..48000 && format.channels in 1..2) run.voiceStreams.add(stream) - run.voice.format(stream, format?.takeIf { voiceRole(it.role) }) + val incoming = format + val voiced = incoming != null && opensVoiceChannel(incoming.role) && + incoming.codec == AudioCodec.PCM_S16LE && incoming.sampleRate in 8000..48000 && + incoming.channels in 1..2 + if (incoming != null && voiced) run.voiceStreams.add(stream) + run.voice.format(stream, incoming?.takeIf { opensVoiceChannel(it.role) }) + if (run.diagnostics.size < 31) run.diagnostics.addLast( + "audio_in stream=$stream role=${incoming?.role} codec=${incoming?.codec} " + + "rate=${incoming?.sampleRate} ch=${incoming?.channels} voiced=$voiced") + if (trace !== ClinkTrace.NONE) trace.emit(ClinkTraceEvent.Audio(System.nanoTime(), run.traceSession, + "format", stream, "voiced=$voiced media=${run.mediaStream}", role = incoming?.role?.name.orEmpty(), + codec = incoming?.codec?.name.orEmpty(), rate = incoming?.sampleRate ?: 0, + channels = incoming?.channels ?: 0, voiced = voiced)) } } override fun onAudioPacket(packet: AudioPacket) { val run = current ?: return synchronized(run.lock) { - if (!run.active.get()) { audioReject("inactive"); return } - if (packet.session != run.inputSession) { audioReject("session=${packet.session}/${run.inputSession}"); return } + if (!run.active.get()) { audioReject(run, "inactive", packet.stream); return } + if (packet.session != run.inputSession) { audioReject(run, "session=${packet.session}/${run.inputSession}", packet.stream); return } if (packet.stream == run.mediaStream) { - if (packet.data.size !in 1..8184) { audioReject("size=${packet.data.size}"); return } + if (packet.data.size !in 1..8184) { audioReject(run, "size=${packet.data.size}", packet.stream); return } while (run.audio.isNotEmpty() && mediaQueueFull(run.audio.size, run.audioBytes + packet.data.size, mediaQueueSpanUs(packet.ptsUs, run.audio.first.ptsUs))) { run.audioBytes -= run.audio.removeFirst().data.size; run.audioDrops++ } run.audio.addLast(packet); run.audioBytes += packet.data.size } else if (packet.stream in run.voiceStreams) run.voice.packet(packet) - else audioReject("stream=${packet.stream}/media=${run.mediaStream}/voice=${run.voiceStreams}") + else audioReject(run, "stream=${packet.stream}/media=${run.mediaStream}/voice=${run.voiceStreams}", packet.stream) } } + // Diagnostic: why an audio packet never reached the queue. Rate-limited to one line a second. @Volatile private var audioRejectAt = 0L @Volatile private var audioRejectReason = "" - private fun audioReject(reason: String) { + private fun audioReject(run: Run, reason: String, stream: Int) { val now = System.currentTimeMillis() if (now - audioRejectAt < 1_000) return audioRejectAt = now audioRejectReason = reason // clink is a plain JVM module: no android.util.Log here. println lands in logcat as System.out. println("OXP-Audio packet_rejected $reason") + if (trace !== ClinkTrace.NONE) trace.emit(ClinkTraceEvent.Audio(System.nanoTime(), run.traceSession, + "reject", stream, reason)) } override fun onMicrophone(stream: Int, request: MicRequest?) { val run = current ?: return @@ -658,6 +674,15 @@ public class ClinkOutput( if (trace !== ClinkTrace.NONE) trace.emit(ClinkTraceEvent.Media(now, session.traceSession, liveFrames, placeholderFrames, dropped + failedVideo, keyframes, videoBytes, run.rtpPackets.get(), run.rtpBytes.get(), sendStalls, audioFrames, overflows)) + // Audio counters every second: the slot the head unit is fed, its backlog, and the + // voice streams that are open. Without this a silent car cannot be told apart from + // a dead audio path (2026-11-11). + if (trace !== ClinkTrace.NONE) { + val queued = synchronized(run.lock) { run.audio.size } + val voice = synchronized(run.lock) { run.voiceStreams.toList() } + trace.emit(ClinkTraceEvent.Audio(now, session.traceSession, "stats", run.mediaStream ?: -1, + "media=${session.mediaAudio} queued=$queued voice=$voice", packets = audioFrames)) + } listener.statistics(ClinkStatistics(liveFrames - liveBase, placeholderFrames - placeholderBase, dropped - queueDropBase + failedVideo - failedBase + codecMismatchDrops, liveBytes, sendStalls - stallBase, statsFormat?.codec, statsFormat?.width ?: 0, statsFormat?.height ?: 0)) withHost(run) { it.stats(OutputStats(origin, ScreenId.MAIN, null, 0, dropped, 0, null)) } diff --git a/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/VoiceDelivery.kt b/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/VoiceDelivery.kt index 72c2bba..294382e 100644 --- a/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/VoiceDelivery.kt +++ b/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/VoiceDelivery.kt @@ -7,6 +7,7 @@ package org.openxplay.stream.clink import java.util.ArrayDeque import org.openxplay.stream.core.* import org.openxplay.stream.core.audio.PcmMixer +import org.openxplay.stream.core.audio.opensVoiceChannel /** Dedicated, paced voice sender. Callback work only retains commands in a bounded queue. */ internal class VoiceDelivery { @@ -84,7 +85,7 @@ internal class VoiceDelivery { streams.remove(command.stream) formats.remove(command.stream) val f = command.format - if (f != null && (f.role == AudioRole.VOICE || f.role == AudioRole.TELEPHONY) && + if (f != null && opensVoiceChannel(f.role) && f.codec == AudioCodec.PCM_S16LE && f.sampleRate in 8000..48000 && f.channels in 1..2) { mixer.removeStream(command.stream) diff --git a/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/trace/ClinkTraceEvent.kt b/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/trace/ClinkTraceEvent.kt index 89a2e3e..240a98b 100644 --- a/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/trace/ClinkTraceEvent.kt +++ b/kotlin/clink/src/main/kotlin/org/openxplay/stream/clink/trace/ClinkTraceEvent.kt @@ -10,6 +10,7 @@ public sealed interface ClinkTraceEvent { public val type: String get() = when (this) { is Rtsp -> "Rtsp"; is Frame -> "Frame"; is Socket -> "Socket"; is Negotiation -> "Negotiation" is Media -> "Media"; is Video -> "Video"; is Input -> "Input"; is Error -> "Error"; is Link -> "Link" + is Audio -> "Audio" } public val atNanos: Long public val session: Long @@ -36,6 +37,15 @@ public sealed interface ClinkTraceEvent { val exceptionClass: String, val location: String) : ClinkTraceEvent public data class Link(override val atNanos: Long, override val session: Long, val stage: String, val fields: Map = emptyMap()) : ClinkTraceEvent + /** + * Audio slot and voice-channel state: "format" when a slot is (re)declared, "reject" when a packet + * reached no queue, "stats" once a second with the gate counters. + */ + public data class Audio(override val atNanos: Long, override val session: Long, val event: String, + val stream: Int, val detail: String = "", val role: String = "", val codec: String = "", + val rate: Int = 0, val channels: Int = 0, val voiced: Boolean = false, val packets: Long = 0, + val audible: Long = 0, val silent: Long = 0, val dropped: Long = 0, + val voiceOpen: Boolean = false) : ClinkTraceEvent } public sealed interface ClinkTraceField { diff --git a/kotlin/clink/src/test/kotlin/org/openxplay/stream/clink/ClinkAudioLoopbackTest.kt b/kotlin/clink/src/test/kotlin/org/openxplay/stream/clink/ClinkAudioLoopbackTest.kt index 257900b..ff02eed 100644 --- a/kotlin/clink/src/test/kotlin/org/openxplay/stream/clink/ClinkAudioLoopbackTest.kt +++ b/kotlin/clink/src/test/kotlin/org/openxplay/stream/clink/ClinkAudioLoopbackTest.kt @@ -161,6 +161,21 @@ class ClinkAudioLoopbackTest { assertTrue(loop.vehicle.voiceFrames.filter { it.kind == 0 }.drop(1).all { it.data.size == 640 }) } + @Test fun `a navigation prompt never opens the voice channel`() = Loop().use { loop -> + loop.output.onAudioFormat(0, AudioFormat(0, AudioRole.MEDIA, AudioCodec.AAC_LC, 48000, 2, + byteArrayOf(0x11, 0x90.toByte()))) + loop.output.onAudioFormat(2, AudioFormat(2, AudioRole.OTHER, AudioCodec.PCM_S16LE, 16000, 1)) + loop.packet(0, byteArrayOf(1, 2, 3), 8_000_000_000L) + loop.vehicle.await("music keeps its AAC track") { loop.vehicle.audioUnits.size == 1 } + // The relay mixes a prompt into the music and never sends it here: opening the voice channel + // makes the G6 pause media playback, and one such prompt left it silent for good (2026-11-11). + repeat(8) { loop.packet(2, loop.pcm(4000, 320)); Thread.sleep(20) } + assertTrue(loop.vehicle.audioCommands.none { it.method == 8 }, + "a prompt must never open the voice channel") + assertTrue(loop.vehicle.voiceFrames.isEmpty()) + assertEquals(1, loop.vehicle.audioUnits.size) + } + @Test fun `microphone stops on input session end and output close`() = Loop().use { loop -> loop.output.onMicrophone(2, MicRequest(2, AudioCodec.PCM_S16LE, 16000, 1, 20)) loop.mic() diff --git a/kotlin/core/src/main/kotlin/org/openxplay/stream/core/audio/VoiceChannel.kt b/kotlin/core/src/main/kotlin/org/openxplay/stream/core/audio/VoiceChannel.kt new file mode 100644 index 0000000..efa7cc7 --- /dev/null +++ b/kotlin/core/src/main/kotlin/org/openxplay/stream/core/audio/VoiceChannel.kt @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2026 OpenXPlay.org +// SPDX-License-Identifier: AGPL-3.0-only + +package org.openxplay.stream.core.audio + +import org.openxplay.stream.core.AudioRole + +/** CarPlay type 101 lands in this slot; the relay's AudioPolicy maps 101 -> 20 + role offset. */ +public const val ALT_AUDIO_STREAM: Int = 2 + +/** + * Whether a stream may open the head unit's voice channel. + * + * Only voice and phone calls may. Everything else — the navigation prompt above all — is mixed into + * the music by the relay instead, because on the G6 the voice channel pauses media playback and does + * not always resume when it closes: the 2026-11-11 trace shows one prompt that left the car silent + * for the remaining 147 s while the music kept arriving at 47 access units a second. System sounds + * such as keyboard clicks were already refused here for the same reason (2026-10-19). + */ +public fun opensVoiceChannel(role: AudioRole): Boolean = role == AudioRole.VOICE || role == AudioRole.TELEPHONY