Store independent capture profiles in the application settings queue

This commit is contained in:
evgeny committed 2026-10-28 12:08:07 +01:00
1 parent 244a8257fc
commit f8e91f6d4b
3 files changed
+100 -3

No files matched your search

@@ -0,0 +1,30 @@
package com.utun.chat.data
/** Независимый выбор фото: пустая камера означает обычную заднюю, нулевые размеры — максимальное разрешение. */
data class PhotoCaptureSettings(val camera: String = "", val width: Int = 0, val height: Int = 0, val jpegQuality: Int = 85) {
init {
require((width == 0 && height == 0) || (width in 1..16384 && height in 1..16384))
require(jpegQuality in QUALITIES)
}
fun resolveCamera(lenses: List<LensOption>): LensOption? = lenses.firstOrNull { it.cameraId == camera }
?: lenses.firstOrNull { it.type == LensType.MAIN } ?: lenses.firstOrNull()
fun resolveResolution(sizes: List<PhotoResolution>): PhotoResolution? =
sizes.firstOrNull { it.width == width && it.height == height } ?: sizes.maxByOrNull { it.area }
companion object { val QUALITIES = listOf(50, 75, 85, 95, 100) }
}
/** Выбор записи видео хранится отдельно от фото, звонков и трансляций; height — метка 480p/720p/1080p. */
data class VideoCaptureSettings(val camera: String = "", val height: Int = 720, val codec: Int = 1, val bitrate: Int = 2_000_000) {
init { require(height in HEIGHTS); require(codec in 1..2); require(bitrate in BITRATES) }
fun resolveCamera(lenses: List<LensOption>): LensOption? = lenses.firstOrNull { it.cameraId == camera }
?: lenses.firstOrNull { it.type == LensType.SELFIE } ?: lenses.firstOrNull()
companion object {
val HEIGHTS = listOf(480, 720, 1080)
val BITRATES = listOf(256_000, 512_000, 1_000_000, 2_000_000, 4_500_000, 8_000_000)
}
}
@@ -69,6 +69,19 @@ class ConfigProvider(private val context: Context) {
private val streamRotation = intPreferencesKey("stream_capture_rotation")
private val streamBitrate = intPreferencesKey("stream_capture_bitrate")
private val streamAudio = booleanPreferencesKey("stream_capture_audio")
private val photoCamera = stringPreferencesKey("photo_capture_camera")
private val photoWidth = intPreferencesKey("photo_capture_width")
private val photoHeight = intPreferencesKey("photo_capture_height")
private val photoJpegQuality = intPreferencesKey("photo_capture_jpeg_quality")
private val videoCamera = stringPreferencesKey("video_capture_camera")
private val videoHeight = intPreferencesKey("video_capture_height")
private val videoCodec = intPreferencesKey("video_capture_codec")
private val videoBitrate = intPreferencesKey("video_capture_bitrate")
private val callVideoCamera = stringPreferencesKey("call_video_camera")
private val callVideoWidth = intPreferencesKey("call_video_width")
private val callVideoHeight = intPreferencesKey("call_video_height")
private val callVideoFps = intPreferencesKey("call_video_fps")
private val callVideoCodec = intPreferencesKey("call_video_codec")
private val callJitterMaxReserveMs = intPreferencesKey("call_jitter_max_reserve_ms")
private val callVideoBitrateRange = stringPreferencesKey("call_video_bitrate_range_kbps")
private val chatCompressorEnabled = intPreferencesKey("chat_compressor_enabled")
@@ -177,7 +190,7 @@ class ConfigProvider(private val context: Context) {
}
fun getCallVideoBitrateRange(): CallVideoBitrateRange = runBlocking {
videoBitrateRange(context.dataStore.data.first())
configFiles.submit("load call video bitrate") { videoBitrateRange(context.dataStore.data.first()) }.await()
}
private fun videoBitrateRange(prefs: Preferences): CallVideoBitrateRange {
@@ -188,11 +201,65 @@ class ConfigProvider(private val context: Context) {
}
}
suspend fun saveCallVideoBitrateRange(range: CallVideoBitrateRange) {
fun saveCallVideoBitrateRange(range: CallVideoBitrateRange): Deferred<Unit> = configFiles.submit("save call video bitrate") {
context.dataStore.edit { it[callVideoBitrateRange] = range.encode() }
LogManager.addLog("INFO", "CallSettings", "Video bitrate range saved: ${range.encode()} kbps")
}
/** Очередь Application сохраняет выбор после закрытия экрана и завершает записи перед последующим чтением. */
suspend fun getPhotoCaptureSettings(): PhotoCaptureSettings = configFiles.submit("load photo capture") {
val prefs = context.dataStore.data.first()
try { PhotoCaptureSettings(prefs[photoCamera] ?: "", prefs[photoWidth] ?: 0, prefs[photoHeight] ?: 0, prefs[photoJpegQuality] ?: 85) }
catch (e: IllegalArgumentException) {
LogManager.addLog("WARN", "Capture", "Invalid photo settings camera=${prefs[photoCamera]} " +
"size=${prefs[photoWidth]}x${prefs[photoHeight]} jpegQuality=${prefs[photoJpegQuality]}; using defaults")
PhotoCaptureSettings()
}
}.await()
fun savePhotoCaptureSettings(settings: PhotoCaptureSettings): Deferred<Unit> = configFiles.submit("save photo capture") {
context.dataStore.edit { prefs ->
prefs[photoCamera] = settings.camera; prefs[photoWidth] = settings.width; prefs[photoHeight] = settings.height
prefs[photoJpegQuality] = settings.jpegQuality
}
LogManager.addLog("INFO", "Capture", "Photo settings saved: $settings")
}
suspend fun getVideoCaptureSettings(): VideoCaptureSettings = configFiles.submit("load video capture") {
val prefs = context.dataStore.data.first()
try { VideoCaptureSettings(prefs[videoCamera] ?: "", prefs[videoHeight] ?: 720, prefs[videoCodec] ?: 1, prefs[videoBitrate] ?: 2_000_000) }
catch (e: IllegalArgumentException) {
LogManager.addLog("WARN", "Capture", "Invalid video settings camera=${prefs[videoCamera]} " +
"height=${prefs[videoHeight]} codec=${prefs[videoCodec]} bitrate=${prefs[videoBitrate]}; using defaults")
VideoCaptureSettings()
}
}.await()
fun saveVideoCaptureSettings(settings: VideoCaptureSettings): Deferred<Unit> = configFiles.submit("save video capture") {
context.dataStore.edit { prefs ->
prefs[videoCamera] = settings.camera; prefs[videoHeight] = settings.height
prefs[videoCodec] = settings.codec; prefs[videoBitrate] = settings.bitrate
}
LogManager.addLog("INFO", "Capture", "Video settings saved: $settings")
}
/** Синхронное чтение только в worker подготовки видеозвонка; enabled остаётся состоянием текущего звонка. */
fun getCallVideoCaptureSettings(): CallVideoSettings = runBlocking {
configFiles.submit("load call video capture") {
val prefs = context.dataStore.data.first()
CallVideoSettings(camera = prefs[callVideoCamera] ?: "", width = prefs[callVideoWidth] ?: 640,
height = prefs[callVideoHeight] ?: 480, codec = prefs[callVideoCodec] ?: 0, fps = prefs[callVideoFps] ?: 30)
}.await()
}
fun saveCallVideoCaptureSettings(settings: CallVideoSettings): Deferred<Unit> = configFiles.submit("save call video capture") {
context.dataStore.edit { prefs ->
prefs[callVideoCamera] = settings.camera; prefs[callVideoWidth] = settings.width; prefs[callVideoHeight] = settings.height
prefs[callVideoFps] = settings.fps; prefs[callVideoCodec] = settings.codec
}
LogManager.addLog("INFO", "CallSettings", "Video capture settings saved: ${settings.copy(enabled = false)}")
}
suspend fun getMediaAudioProfile(): MediaAudioProfile {
val value = context.dataStore.data.first()[mediaAudioProfileKey]
return MediaAudioProfile.entries.firstOrNull { it.name == value } ?: MediaAudioProfile.AUTO
@@ -1280,7 +1280,7 @@ private fun AudioSettingsTab(vm: ChatViewModel) {
videoRangeSaveFailed = false
vm.viewModelScope.launch {
try {
provider.saveCallVideoBitrateRange(range)
provider.saveCallVideoBitrateRange(range).await()
NativeLib.setChatSetting("call_video_bitrate_range_kbps", range.encode())
} catch (e: IOException) {
videoRangeSaveFailed = true