fix: Fix recorder states

This commit is contained in:
Myzel394 2023-12-15 19:40:46 +01:00
parent 3d355df522
commit 4be2fc52e2
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
6 changed files with 51 additions and 40 deletions

View File

@ -38,6 +38,7 @@ abstract class IntervalRecorderService<S : IntervalRecorderService.Settings, I>
} }
override fun start() { override fun start() {
super.start()
batchesFolder.initFolders() batchesFolder.initFolders()
if (!batchesFolder.checkIfFolderIsAccessible()) { if (!batchesFolder.checkIfFolderIsAccessible()) {
onCustomOutputFolderNotAccessible() onCustomOutputFolderNotAccessible()
@ -48,10 +49,12 @@ abstract class IntervalRecorderService<S : IntervalRecorderService.Settings, I>
} }
override fun pause() { override fun pause() {
super.pause()
cycleTimer.shutdown() cycleTimer.shutdown()
} }
override fun resume() { override fun resume() {
super.resume()
createTimer() createTimer()
} }

View File

@ -39,21 +39,27 @@ abstract class RecorderService : LifecycleService() {
var recordingTime = 0L var recordingTime = 0L
private set private set
protected abstract fun start() protected open fun start() {
createRecordingTimeTimer()
protected abstract fun pause()
// TODO: Move pause / recording here
protected abstract fun resume()
protected open suspend fun stop() {
} }
override fun onDestroy() { protected open fun pause() {
isPaused = true
recordingTimeTimer.shutdown()
}
protected open fun resume() {
createRecordingTimeTimer()
}
protected open suspend fun stop() {
recordingTimeTimer.shutdown()
NotificationManagerCompat.from(this) NotificationManagerCompat.from(this)
.cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID) .cancel(NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID)
stopForeground(STOP_FOREGROUND_REMOVE) stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf() stopSelf()
super.onDestroy()
} }
protected abstract fun startForegroundService() protected abstract fun startForegroundService()
@ -141,20 +147,9 @@ abstract class RecorderService : LifecycleService() {
isPaused = false isPaused = false
} }
// `start` is handled by `startRecording` // `start` is handled by `startRecording`
createRecordingTimeTimer()
} }
RecorderState.PAUSED -> { RecorderState.PAUSED -> pause()
isPaused = true
recordingTimeTimer.shutdown()
pause()
}
RecorderState.STOPPED -> {
recordingTimeTimer.shutdown()
}
else -> {} else -> {}
} }

View File

@ -33,8 +33,6 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import kotlin.properties.Delegates import kotlin.properties.Delegates
const val CAMERA_CLOSE_TIMEOUT = 20000L
class VideoRecorderService : class VideoRecorderService :
IntervalRecorderService<VideoRecorderService.Settings, RecordingInformation>() { IntervalRecorderService<VideoRecorderService.Settings, RecordingInformation>() {
override var batchesFolder: BatchesFolder = VideoBatchesFolder.viaInternalFolder(this) override var batchesFolder: BatchesFolder = VideoBatchesFolder.viaInternalFolder(this)
@ -238,7 +236,10 @@ class VideoRecorderService :
type = RecordingInformation.Type.VIDEO, type = RecordingInformation.Type.VIDEO,
) )
// TODO: Save camera selector as it doesn't make sense to change the camera midway companion object {
const val CAMERA_CLOSE_TIMEOUT = 20000L
}
data class Settings( data class Settings(
override val maxDuration: Long, override val maxDuration: Long,
override val intervalDuration: Long, override val intervalDuration: Long,

View File

@ -77,20 +77,22 @@ fun VideoRecordingStatus(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(bottom = 32.dp), modifier = Modifier.padding(bottom = 32.dp),
) { ) {
val cameraControl = videoRecorder.recorderService!!.cameraControl!! if (!videoRecorder.isStartingRecording) {
if (cameraControl.hasTorchAvailable()) { val cameraControl = videoRecorder.recorderService!!.cameraControl!!
val isTorchEnabled = cameraControl.isTorchEnabled() if (cameraControl.hasTorchAvailable()) {
val isTorchEnabled = cameraControl.isTorchEnabled()
TorchStatus( TorchStatus(
enabled = isTorchEnabled, enabled = isTorchEnabled,
onChange = { onChange = {
if (isTorchEnabled) { if (isTorchEnabled) {
cameraControl.disableTorch() cameraControl.disableTorch()
} else { } else {
cameraControl.enableTorch() cameraControl.enableTorch()
} }
}, },
) )
}
} }
Divider() Divider()

View File

@ -77,6 +77,8 @@ abstract class BaseRecorderModel<S : IntervalRecorderService.Settings, I, T : In
} }
override fun onServiceDisconnected(arg0: ComponentName) { override fun onServiceDisconnected(arg0: ComponentName) {
// `onServiceDisconnected` is called when the connection is unexpectedly lost,
// so we need to make sure to manually call `reset` to clean up in other places
reset() reset()
} }
} }
@ -135,8 +137,13 @@ abstract class BaseRecorderModel<S : IntervalRecorderService.Settings, I, T : In
recorderService!!.stopRecording() recorderService!!.stopRecording()
val intent = Intent(context, intentClass) val intent = Intent(context, intentClass)
context.unbindService(connection) runCatching {
context.stopService(intent) context.unbindService(connection)
}
runCatching {
context.stopService(intent)
}
reset()
} }
fun pauseRecording() { fun pauseRecording() {

View File

@ -25,7 +25,10 @@ class VideoRecorderModel :
var cameraID by mutableIntStateOf(CameraInfo.Lens.BACK.androidValue) var cameraID by mutableIntStateOf(CameraInfo.Lens.BACK.androidValue)
override val isInRecording: Boolean override val isInRecording: Boolean
get() = super.isInRecording && recorderService!!.cameraControl != null get() = super.isInRecording
val isStartingRecording: Boolean
get() = recorderService?.cameraControl == null
val cameraSelector: CameraSelector val cameraSelector: CameraSelector
get() = CameraSelector.Builder().requireLensFacing(cameraID).build() get() = CameraSelector.Builder().requireLensFacing(cameraID).build()