Feature: Support voice monitoring on Android hosts

The master's voice monitoring menu sends COMMAND_AUDIO; the client opens a
ConnAuth sub-connection with TOKEN_AUDIO_START, the server opens CAudioDlg
and answers COMMAND_NEXT, then audio flows both ways. The protocol matches
the Windows client, so the server needs no changes.

The server's ACM format was read from Audio.cpp and AudioDlg.cpp first:
WAVE_FORMAT_GSM610, 8000 Hz mono, 65-byte blocks of 320 samples (40 ms),
about 1625 B/s. libgsm 1.0.13 (AOSP external/libgsm mirror) is vendored
under cpp/gsm and built with -DSASR -DWAV49 so its MS-GSM packing is
byte-identical to ACM GSM610. Encode and decode use separate gsm states
because frame_index/frame_chain live in the state.

Uplink packets are always exactly 1000 bytes: CAudio::PlayBuffer copies
1000 bytes from the buffer start regardless of packet length, so a short
packet would over-read. Since 1000 is not a multiple of 65, blocks
straddle packets; the stream is continuous and codec state carries over.

AudioMonitor.kt owns AudioRecord (8 kHz/16-bit/mono, 320 samples a read)
and AudioTrack playback of decoded downlink PCM. RECORD_AUDIO is requested
with POST_NOTIFICATIONS before the projection dialog, and CaptureService
adds the microphone FGS type only when held, falling back on failure. An
8-second dialog wait or missing microphone reports a tip to the master
instead of failing silently.

Verified on an Android 15 emulator against the Debug server: dialog
opened, uplink started, server receive counter grew at ~1.6 KB/s.
Downlink playback was not exercised (the test machine has no recording
device); that path is code-reviewed only.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:54 +01:00
1 parent 3ab5154db1
commit 4aedca8ee0
31 files changed
+4979 -22

No files matched your search

@@ -1,5 +1,6 @@
package com.yama.client
import android.Manifest
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
@@ -8,6 +9,8 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.hardware.display.DisplayManager
import android.hardware.display.VirtualDisplay
import android.media.MediaCodec
@@ -201,12 +204,7 @@ class CaptureService : Service() {
// kill the service after a ~5-second grace period — the root cause of the
// 3-9 s disconnect seen on Google TV when no screen-capture permission
// was granted.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIF_ID, buildNotification(),
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else {
startForeground(NOTIF_ID, buildNotification())
}
startForegroundTyped(ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -254,10 +252,7 @@ class CaptureService : Service() {
if (projResult == android.app.Activity.RESULT_OK && projData != null) {
// Android 14+: upgrade foreground type to MEDIA_PROJECTION in the same
// start command that calls getMediaProjection(), as required by API 34+.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIF_ID, buildNotification(),
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
}
startForegroundTyped(ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
val pm = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
val mp = pm.getMediaProjection(projResult, projData)
mediaProjection = mp
@@ -354,10 +349,7 @@ class CaptureService : Service() {
stopCapture()
// Android 14+: system auto-kills a foreground service whose mediaProjection
// is revoked. Re-declare as dataSync-only to survive without screen capture.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIF_ID, buildNotification(),
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
}
startForegroundTyped(ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
}
}, null)
@@ -481,6 +473,34 @@ class CaptureService : Service() {
}
}
/**
* 带类型的前台服务声明。语音监听(COMMAND_AUDIO)要在后台用麦克风,按 Android 11+
* 的要求前台服务必须带 microphone 类型,所以只要 RECORD_AUDIO 已授权就把这一位加上。
* 没授权时绝不能带:Android 14+ 会在 startForeground 时校验 while-in-use 权限,
* 直接抛 SecurityException 把服务打死。加了之后仍可能被系统拒绝(例如进程已在
* 后台且权限不是「使用中」),因此失败时退化回原始类型,保证远程控制本身不受影响。
*/
private fun startForegroundTyped(base: Int) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
startForeground(NOTIF_ID, buildNotification())
return
}
var types = base
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R &&
checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
types = types or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
}
try {
startForeground(NOTIF_ID, buildNotification(), types)
} catch (e: Exception) {
Log.e(TAG, "startForeground(types=0x${types.toString(16)}) failed: $e")
if (types != base) {
runCatching { startForeground(NOTIF_ID, buildNotification(), base) }
.onFailure { Log.e(TAG, "startForeground(base=0x${base.toString(16)}) also failed: $it") }
}
}
}
private fun buildNotification(): Notification {
val b = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(this, CHANNEL_ID)