Skip to content

Commit

Permalink
feat(gui): run comput. & thumbnail vid. creation concurrently
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kriese <[email protected]>
  • Loading branch information
akriese committed Feb 6, 2024
1 parent 883f5f5 commit 9f6f161
Showing 1 changed file with 61 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ fun RowScope.ComputeDifferencesButton(
onClick = {
try {
if (referenceIsOlderThanCurrent(state)) {
createThumbnailVideos(state)
calculateVideoDifferences(scope, state, errorDialogText, showDialog)
scope.launch {
runComputation(scope, state, errorDialogText, showDialog)
}
} else {
showConfirmDialog.value = true
}
Expand Down Expand Up @@ -80,8 +81,9 @@ fun RowScope.ComputeDifferencesButton(
text = "The reference video is newer than the current video. Are you sure you want to continue?",
showDialog = showConfirmDialog.value,
onConfirm = {
createThumbnailVideos(state)
calculateVideoDifferences(scope, state, errorDialogText, showDialog)
scope.launch {
runComputation(scope, state, errorDialogText, showDialog)
}
showConfirmDialog.value = false
},
onCancel = {
Expand All @@ -90,6 +92,31 @@ fun RowScope.ComputeDifferencesButton(
)
}

suspend fun runComputation(
scope: CoroutineScope,
state: MutableState<AppState>,
errorDialogText: MutableState<String?>,
isLoading: MutableState<Boolean>,
) {
isLoading.value = true
val computeJob =
scope.launch(Dispatchers.Default) {
calculateVideoDifferences(state, errorDialogText)
}

computeJob.invokeOnCompletion { isLoading.value = false }

val videoScaleJob =
scope.launch(Dispatchers.Default) {
createThumbnailVideos(state)
}

// wait for both jobs to finish before transitioning to the diff screen
listOf(computeJob, videoScaleJob).joinAll()

state.value = state.value.copy(screen = Screen.DiffScreen, hasUnsavedChanges = true)
}

fun createThumbnailVideos(state: MutableState<AppState>) {
// create the thumbnail videos
val tempReference = createThumbnailVideo(state.value.videoReferencePath!!, 0.25f)
Expand All @@ -99,49 +126,42 @@ fun createThumbnailVideos(state: MutableState<AppState>) {
}

private fun calculateVideoDifferences(
scope: CoroutineScope,
state: MutableState<AppState>,
errorDialogText: MutableState<String?>,
isLoading: MutableState<Boolean>,
) {
scope.launch(Dispatchers.Default) {
isLoading.value = true
AlgorithmExecutionState.getInstance().reset()

// generate the differences
lateinit var generator: DifferenceGeneratorWrapper
try {
generator = DifferenceGeneratorWrapper(state)
} catch (e: DifferenceGeneratorException) {
errorDialogText.value = e.toString()
return@launch
} catch (e: Exception) {
errorDialogText.value = "An unexpected exception was thrown when creating" +
"the DifferenceGenerator instance:\n\n${e.message}"
return@launch
}

try {
generator.getDifferences(state.value.outputPath!!)
} catch (e: DifferenceGeneratorStoppedException) {
println("stopped by canceling...")
return@launch
} catch (e: Exception) {
errorDialogText.value = "An unexpected exception was thrown when running" +
"the difference computation:\n\n${e.message}"
return@launch
}
AlgorithmExecutionState.getInstance().reset()

// generate the differences
lateinit var generator: DifferenceGeneratorWrapper
try {
generator = DifferenceGeneratorWrapper(state)
} catch (e: DifferenceGeneratorException) {
errorDialogText.value = e.toString()
return
} catch (e: Exception) {
errorDialogText.value = "An unexpected exception was thrown when creating" +
"the DifferenceGenerator instance:\n\n${e.message}"
return
}

// check for cancellation one last time before switching to the diff screen
if (!AlgorithmExecutionState.getInstance().isAlive()) {
return@launch
}
try {
generator.getDifferences(state.value.outputPath!!)
} catch (e: DifferenceGeneratorStoppedException) {
println("stopped by canceling...")
return
} catch (e: Exception) {
errorDialogText.value = "An unexpected exception was thrown when running" +
"the difference computation:\n\n${e.message}"
return
}

// set the sequence and screen
state.value = state.value.copy(sequenceObj = generator.getSequence(), screen = Screen.DiffScreen, hasUnsavedChanges = true)
}.invokeOnCompletion {
isLoading.value = false
// check for cancellation one last time before switching to the diff screen
if (!AlgorithmExecutionState.getInstance().isAlive()) {
return
}

// set the sequence
state.value = state.value.copy(sequenceObj = generator.getSequence())
}

fun getVideoCreationDate(videoPath: String): Long {
Expand Down

0 comments on commit 9f6f161

Please sign in to comment.