Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions backend/src/main/kotlin/dev/twango/jetplay/rpc/MediaAccessorImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,24 @@ class MediaAccessorImpl : MediaAccessor {
send(TranscodeEvent.Done)
}

// Shared shell for the extract* RPCs: ffmpeg gate, then file resolve, then a guarded extract.
private suspend fun <T> withFfmpegResolvedFile(
fileId: VirtualFileId,
projectId: ProjectId,
default: T,
extract: (File) -> T,
): T = withContext(Dispatchers.IO) {
if (!FfmpegAvailability.available) return@withContext default
val file = resolveFile(fileId, projectId) ?: return@withContext default
runCatching { extract(file) }.getOrDefault(default)
}

override suspend fun extractWaveform(fileId: VirtualFileId, projectId: ProjectId): List<Double> =
withContext(Dispatchers.IO) {
if (!FfmpegAvailability.available) return@withContext emptyList()
val file = resolveFile(fileId, projectId) ?: return@withContext emptyList()
runCatching { WaveformExtractor.extract(file) }.getOrDefault(emptyList())
}
withFfmpegResolvedFile(fileId, projectId, emptyList()) { WaveformExtractor.extract(it) }

override suspend fun extractMediaInfo(fileId: VirtualFileId, projectId: ProjectId): MediaInfo? =
withContext(Dispatchers.IO) {
if (!FfmpegAvailability.available) return@withContext null
val file = resolveFile(fileId, projectId) ?: return@withContext null
runCatching { MediaInfoExtractor.extract(file) }.getOrNull()
}
withFfmpegResolvedFile(fileId, projectId, null) { MediaInfoExtractor.extract(it) }

override suspend fun extractSpectrogram(fileId: VirtualFileId, projectId: ProjectId): Spectrogram? =
withContext(Dispatchers.IO) {
if (!FfmpegAvailability.available) return@withContext null
val file = resolveFile(fileId, projectId) ?: return@withContext null
runCatching { SpectrogramExtractor.extract(file) }.getOrNull()
}
withFfmpegResolvedFile(fileId, projectId, null) { SpectrogramExtractor.extract(it) }
}
Loading