fix: audio frame size, rotation rebuild, background disconnect reason
- AudioCapture: short reads buffered and concatenated, only send when exactly 640B - AndroidManifest: configChanges prevents rotation from recreating Activity - onStop shows explicit disconnect reason instead of silent disconnect - Remote debug log now works (HTTPS endpoint fixed) Version 0.3.0
This commit is contained in:
+2
-2
@@ -45,8 +45,8 @@ android {
|
||||
applicationId 'com.gorios.lanvoice'
|
||||
minSdk 26
|
||||
targetSdk 34
|
||||
versionCode 3
|
||||
versionName '0.2.7'
|
||||
versionCode 4
|
||||
versionName '0.3.0'
|
||||
ndk { abiFilters 'arm64-v8a' }
|
||||
}
|
||||
compileOptions { sourceCompatibility JavaVersion.VERSION_17; targetCompatibility JavaVersion.VERSION_17 }
|
||||
|
||||
@@ -12,7 +12,10 @@
|
||||
android:supportsRtl="true"
|
||||
android:usesCleartextTraffic="false"
|
||||
android:networkSecurityConfig="@xml/network_security_config">
|
||||
<activity android:name=".MainActivity" android:exported="true">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
|
||||
@@ -133,7 +133,10 @@ class MainActivity : ComponentActivity() {
|
||||
pendingCamera = false
|
||||
expressions.stop()
|
||||
if (!permissionInFlight) pendingStart = false
|
||||
session.background(keepConnectionForPermission = permissionInFlight)
|
||||
session.background(
|
||||
keepConnectionForPermission = permissionInFlight,
|
||||
showDisconnectReason = !permissionInFlight,
|
||||
)
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
|
||||
@@ -59,11 +59,11 @@ class AudioCapture {
|
||||
onDiagnostic("AudioRecord initialization started")
|
||||
Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO)
|
||||
val minimum = AudioRecord.getMinBufferSize(
|
||||
16_000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT
|
||||
SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT
|
||||
)
|
||||
check(minimum > 0) { "16k mono PCM16 recording is unavailable" }
|
||||
val device = AudioRecord(
|
||||
MediaRecorder.AudioSource.VOICE_COMMUNICATION, 16_000,
|
||||
MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE,
|
||||
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
|
||||
maxOf(minimum * 2, FRAME_BYTES * 2)
|
||||
)
|
||||
@@ -89,19 +89,25 @@ class AudioCapture {
|
||||
val frame = ByteArray(FRAME_BYTES)
|
||||
var filled = 0
|
||||
while (run.active) {
|
||||
val read = device.read(frame, filled, frame.size - filled, AudioRecord.READ_BLOCKING)
|
||||
// READ_BLOCKING can still return a short read. Keep it until a full
|
||||
// 320-sample frame is ready; never pad or send a partial frame.
|
||||
val remaining = FRAME_BYTES - filled
|
||||
val read = device.read(frame, filled, remaining, AudioRecord.READ_BLOCKING)
|
||||
if (!run.active) break
|
||||
check(read >= 0) { "AudioRecord read failed: $read" }
|
||||
check(read <= remaining) { "AudioRecord read exceeded requested bytes" }
|
||||
if (read == 0) {
|
||||
Thread.sleep(2)
|
||||
continue
|
||||
}
|
||||
filled += read
|
||||
if (filled == frame.size) {
|
||||
// The callback may queue this asynchronously; do not reuse its bytes.
|
||||
onFrame(frame.copyOf())
|
||||
filled = 0
|
||||
}
|
||||
}
|
||||
// Any unfinished frame belongs to this run and is discarded on stop.
|
||||
} catch (error: Exception) {
|
||||
if (run.active) {
|
||||
onDiagnostic("AudioRecord $stage failed: ${error.javaClass.simpleName}")
|
||||
@@ -121,5 +127,10 @@ class AudioCapture {
|
||||
}
|
||||
}
|
||||
|
||||
companion object { const val FRAME_BYTES = 640 }
|
||||
companion object {
|
||||
private const val SAMPLE_RATE = 16_000
|
||||
private const val FRAME_DURATION_MS = 20
|
||||
private const val BYTES_PER_SAMPLE = 2
|
||||
const val FRAME_BYTES = SAMPLE_RATE * FRAME_DURATION_MS / 1_000 * BYTES_PER_SAMPLE // 640B
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ class VoiceSession(
|
||||
else DebugLogUpload.log("connect", "foreground reused active connection")
|
||||
}
|
||||
|
||||
fun background(keepConnectionForPermission: Boolean = false) {
|
||||
fun background(keepConnectionForPermission: Boolean = false, showDisconnectReason: Boolean = false) {
|
||||
checkMainThread()
|
||||
foreground = false
|
||||
stopVoice(sendControls = false)
|
||||
@@ -95,7 +95,13 @@ class VoiceSession(
|
||||
backgroundTimeout = null
|
||||
if (!foreground && !released) dialogue.disconnect("permission_background_timeout")
|
||||
}.also { main.postDelayed(it, 30_000L) }
|
||||
} else dialogue.disconnect("session_background")
|
||||
} else {
|
||||
if (showDisconnectReason) {
|
||||
update(state.copy(guidance = BACKGROUND_DISCONNECT_GUIDANCE))
|
||||
onConnectionLog(BACKGROUND_DISCONNECT_GUIDANCE)
|
||||
}
|
||||
dialogue.disconnect("session_background")
|
||||
}
|
||||
}
|
||||
|
||||
fun release() {
|
||||
@@ -157,7 +163,12 @@ class VoiceSession(
|
||||
blockedTurn = null
|
||||
sequence = 0
|
||||
update(state.copy(connection = connection, voiceActive = false, phase = VoicePhase.IDLE))
|
||||
} else update(state.copy(connection = connection, guidance = null))
|
||||
} else update(state.copy(
|
||||
connection = connection,
|
||||
// onStop runs while the screen is hidden. Keep the reason visible on
|
||||
// return, including after reconnect; startVoice clears it explicitly.
|
||||
guidance = state.guidance?.takeIf { it == BACKGROUND_DISCONNECT_GUIDANCE },
|
||||
))
|
||||
}
|
||||
|
||||
private fun startCapture() {
|
||||
@@ -314,6 +325,7 @@ class VoiceSession(
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val BACKGROUND_DISCONNECT_GUIDANCE = "App 退到后台,连接已断开"
|
||||
private const val TAG = "LanVoice"
|
||||
private const val UINT32_MASK = 0xffff_ffffL
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user