feat: boot movie settings for 30/60 fps, sound and custom imports (#969)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-16 15:07:46 -04:00
1 parent 23479d15f5
commit 6bc6edafea
43 files changed
+1504 -20

No files matched your search

@@ -209,6 +209,9 @@ class MainActivity : ComponentActivity() {
private val pickDeckyPluginZip = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedFile(r.data)?.let { decky.importPluginZip(it) }
}
private val pickBootMovie = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedFile(r.data)?.let { importBootMovie(it) }
}
private val pickAnyDriver = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedUri(r.data)?.let { drivers.importDriver(it, linux = null) }
}
@@ -253,6 +256,38 @@ class MainActivity : ComponentActivity() {
}, "lossless-sync").start()
}
private fun importBootMovie(file: File) {
bootMovieImporting = true
bootMovieProgress = -1
Thread({
val result = com.droiddeck.launcher.session.BootMovie.import(
this, file,
onStart = { ui.post { android.widget.Toast.makeText(this, getString(R.string.boot_movie_converting_started, file.name), android.widget.Toast.LENGTH_LONG).show() } },
onProgress = { percent -> ui.post { bootMovieProgress = percent } },
)
val message = when (result) {
is com.droiddeck.launcher.session.BootMovie.Import.Done -> getString(
if (result.audioDropped) R.string.boot_movie_imported_silent else R.string.boot_movie_imported, result.name)
com.droiddeck.launcher.session.BootMovie.Import.TooLong ->
getString(R.string.boot_movie_too_long, com.droiddeck.launcher.session.BootMovie.MAX_CONVERT_SECONDS)
is com.droiddeck.launcher.session.BootMovie.Import.Unsupported -> getString(R.string.boot_movie_unsupported)
com.droiddeck.launcher.session.BootMovie.Import.Failed -> getString(R.string.boot_movie_import_failed)
}
ui.post {
bootMovieImporting = false
bootMovieProgress = -1
loadBootMovie()
android.widget.Toast.makeText(this, message, android.widget.Toast.LENGTH_LONG).show()
}
}, "boot-movie-import").start()
}
private fun loadBootMovie() {
bootMovie = com.droiddeck.launcher.session.BootMovie.choice(this)
bootMovieSound = SessionPrefs.bootMovieSound(this)
bootMovieCustomName = com.droiddeck.launcher.session.BootMovie.customName(this)
}
private fun importLossless(dll: File) {
Thread({
val message = Lossless.message(this, Lossless.import(this, dll))
@@ -329,6 +364,11 @@ class MainActivity : ComponentActivity() {
private var fexPreset by mutableStateOf("")
private var forceSsbs by mutableStateOf(false)
private var runSteamAtStartup by mutableStateOf(false)
private var bootMovie by mutableStateOf(com.droiddeck.launcher.session.BootMovie.DROIDDECK_30)
private var bootMovieSound by mutableStateOf(true)
private var bootMovieCustomName by mutableStateOf<String?>(null)
private var bootMovieImporting by mutableStateOf(false)
private var bootMovieProgress by mutableStateOf(-1)
private var theme by mutableStateOf("graphite")
private var appScale by mutableStateOf(com.droiddeck.launcher.core.AppUiPrefs.DEFAULT_SCALE)
private var hdrOn by mutableStateOf(false)
@@ -1318,6 +1358,11 @@ class MainActivity : ComponentActivity() {
mangoapp = mangoapp,
steamController = if (mode == SessionService.MODE_STEAM) steamController else null,
runSteamAtStartup = mode == SessionService.MODE_STEAM && runSteamAtStartup,
bootMovie = if (mode == SessionService.MODE_STEAM) bootMovie else null,
bootMovieSound = bootMovieSound,
bootMovieCustomName = bootMovieCustomName,
bootMovieImporting = bootMovieImporting,
bootMovieProgress = bootMovieProgress,
wifiDiscovery = if (mode == SessionService.MODE_STEAM) wifiDiscovery else null,
wifiDiscoveryPermission = wifiDiscoveryPermission,
wifiDiscoveryLocation = wifiDiscoveryLocation,
@@ -1398,6 +1443,12 @@ class MainActivity : ComponentActivity() {
SessionPrefs.setRunSteamAtStartup(this, on)
runSteamAtStartup = on
},
onBootMovie = { id -> SessionPrefs.setBootMovie(this, id); loadBootMovie() },
onBootMovieSound = { on -> SessionPrefs.setBootMovieSound(this, on); bootMovieSound = on },
onImportBootMovie = {
pickBootMovie.launch(InAppFilePicker.buildIntent(this, com.droiddeck.launcher.session.BootMovie.PICKER_EXTENSIONS, getString(R.string.boot_movie_pick)))
},
onRemoveBootMovie = { com.droiddeck.launcher.session.BootMovie.removeCustom(this); loadBootMovie() },
onDeckyInstall = { release -> decky.installDecky(release) },
onDeckyCheck = { decky.refreshDecky() },
onDeckyEnabled = { enabled -> DeckyManager.setSupervisorEnabled(this, enabled); decky.deckySupervisor = enabled },
@@ -1522,6 +1573,7 @@ class MainActivity : ComponentActivity() {
mangoapp = SessionPrefs.mangoapp(this)
steamController = SessionPrefs.steamController(this)
runSteamAtStartup = SessionPrefs.runSteamAtStartup(this)
loadBootMovie()
hdrOn = SessionPrefs.hdr(this, mode)
fpsLimit = SessionPrefs.fpsLimit(this, mode)
upscaler = SessionPrefs.upscaler(this)
@@ -0,0 +1,208 @@
package com.droiddeck.launcher.session
import android.content.Context
import android.media.MediaCodec
import android.media.MediaExtractor
import android.media.MediaFormat
import android.media.MediaMuxer
import android.util.Log
import com.droiddeck.launcher.core.FileUtils
import java.io.File
import java.nio.ByteBuffer
object BootMovie {
private const val TAG = "BootMovie"
const val DROIDDECK_30 = "dd30"
const val DROIDDECK_60 = "dd60"
const val CUSTOM = "custom"
const val MAX_BYTES = 50L shl 20
const val MAX_CONVERT_SECONDS = 60
val PICKER_EXTENSIONS = listOf("webm", "mkv", "mp4", "mov", "m4v", "3gp")
private val ASSETS = mapOf(
DROIDDECK_30 to "droiddeck-startup.webm",
DROIDDECK_60 to "droiddeck-startup-60.webm",
)
// Steam's CEF player only takes WebM codecs; MediaMuxer's WebM writer cannot take AV1.
internal val VIDEO_CODECS = setOf(MediaFormat.MIMETYPE_VIDEO_VP8, MediaFormat.MIMETYPE_VIDEO_VP9, MediaFormat.MIMETYPE_VIDEO_AV1)
private val REMUX_CODECS = setOf(MediaFormat.MIMETYPE_VIDEO_VP8, MediaFormat.MIMETYPE_VIDEO_VP9)
internal val AUDIO_CODECS = setOf(MediaFormat.MIMETYPE_AUDIO_OPUS, MediaFormat.MIMETYPE_AUDIO_VORBIS)
sealed class Import {
data class Done(val name: String, val audioDropped: Boolean) : Import()
object TooLong : Import()
data class Unsupported(val codec: String?) : Import()
object Failed : Import()
}
private fun dir(context: Context) = File(context.filesDir, "boot-movie")
private fun customFile(context: Context) = File(dir(context), "custom.webm")
fun customName(context: Context): String? =
if (customFile(context).isFile) SessionPrefs.bootMovieCustomName(context) ?: "custom.webm" else null
fun choice(context: Context): String {
val chosen = SessionPrefs.bootMovie(context)
return when {
chosen == CUSTOM && customFile(context).isFile -> CUSTOM
chosen in ASSETS -> chosen
else -> DROIDDECK_30
}
}
fun prepare(context: Context): File? {
val choice = choice(context)
val source = if (choice == CUSTOM) customFile(context) else extractAsset(context, ASSETS.getValue(choice)) ?: return null
if (SessionPrefs.bootMovieSound(context)) return source
val muted = File(source.parentFile, source.nameWithoutExtension + "-muted.webm")
if (muted.isFile && muted.lastModified() >= source.lastModified()) return muted
if (remux(source, muted, audio = false)) return muted
if (BootMovieTranscoder.transcode(source, muted, keepAudio = false) {} != null) return muted
Log.e(TAG, "could not strip audio from ${source.name}; not staging it")
return null
}
private fun extractAsset(context: Context, name: String): File? {
val target = File(dir(context), name)
val updated = runCatching { context.packageManager.getPackageInfo(context.packageName, 0).lastUpdateTime }.getOrDefault(Long.MAX_VALUE)
if (target.isFile && target.lastModified() >= updated) return target
val staged = File(target.parentFile, "$name.staged")
return try {
target.parentFile?.mkdirs()
context.assets.open("steam-startup/$name").use { input ->
staged.outputStream().use { output -> FileUtils.copy(input, output) }
}
if (staged.renameTo(target)) target else null
} catch (e: Exception) {
Log.w(TAG, "could not extract $name", e)
null
} finally {
staged.delete()
}
}
fun import(context: Context, file: File, onStart: () -> Unit = {}, onProgress: (Int) -> Unit = {}): Import {
val (format, audio) = probe(file) ?: return Import.Unsupported(null)
val video = format.getString(MediaFormat.KEY_MIME)
val audioPlayable = audio == null || audio in AUDIO_CODECS
val playable = video in VIDEO_CODECS && file.length() <= MAX_BYTES
val copy = playable && audioPlayable && isWebm(file)
val remux = !copy && playable && video in REMUX_CODECS
if (!copy && !remux) {
if (!BootMovieTranscoder.canDecode(format)) return Import.Unsupported(video)
val durationUs = if (format.containsKey(MediaFormat.KEY_DURATION)) format.getLong(MediaFormat.KEY_DURATION) else 0L
if (durationUs > MAX_CONVERT_SECONDS * 1_000_000L) return Import.TooLong
onStart()
}
val target = customFile(context)
val staged = File(dir(context), "custom.webm.staged")
var audioDropped = remux && !audioPlayable
return try {
target.parentFile?.mkdirs()
val ok = when {
copy -> {
file.inputStream().use { input -> staged.outputStream().use { output -> FileUtils.copy(input, output) } }
true
}
remux -> remux(file, staged, audio = true)
else -> BootMovieTranscoder.transcode(file, staged, onProgress = onProgress)?.also { audioDropped = it.audioDropped } != null
}
if (!ok || !staged.renameTo(target)) return Import.Failed
File(target.parentFile, "custom-muted.webm").delete()
SessionPrefs.setBootMovieCustomName(context, file.name)
SessionPrefs.setBootMovie(context, CUSTOM)
Import.Done(file.name, audioDropped)
} catch (e: Exception) {
Log.w(TAG, "could not import ${file.name}", e)
Import.Failed
} finally {
staged.delete()
}
}
fun removeCustom(context: Context) {
customFile(context).delete()
File(dir(context), "custom-muted.webm").delete()
SessionPrefs.setBootMovieCustomName(context, null)
if (SessionPrefs.bootMovie(context) == CUSTOM) SessionPrefs.setBootMovie(context, DROIDDECK_30)
}
private fun isWebm(file: File): Boolean {
if (!file.name.endsWith(".webm", ignoreCase = true)) return false
val magic = ByteArray(4)
val read = runCatching { file.inputStream().use { it.read(magic) } }.getOrDefault(0)
return read == 4 && magic.contentEquals(byteArrayOf(0x1A, 0x45, 0xDF.toByte(), 0xA3.toByte()))
}
private fun probe(file: File): Pair<MediaFormat, String?>? {
val extractor = MediaExtractor()
return try {
extractor.setDataSource(file.path)
var video: MediaFormat? = null
var audio: String? = null
for (i in 0 until extractor.trackCount) {
val format = extractor.getTrackFormat(i)
val mime = format.getString(MediaFormat.KEY_MIME) ?: continue
if (video == null && mime.startsWith("video/")) video = format
if (audio == null && mime.startsWith("audio/")) audio = mime
}
video?.let { it to audio }
} catch (e: Exception) {
Log.w(TAG, "could not read ${file.name}", e)
null
} finally {
extractor.release()
}
}
private fun remux(source: File, target: File, audio: Boolean): Boolean {
val staged = File(target.parentFile, target.name + ".remux")
val extractor = MediaExtractor()
var muxer: MediaMuxer? = null
return try {
extractor.setDataSource(source.path)
val output = MediaMuxer(staged.path, MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM).also { muxer = it }
val tracks = HashMap<Int, Int>()
var hasVideo = false
var hasAudio = false
var bufferSize = 8 shl 20
for (i in 0 until extractor.trackCount) {
val format = extractor.getTrackFormat(i)
val mime = format.getString(MediaFormat.KEY_MIME) ?: continue
val keep = mime in VIDEO_CODECS && !hasVideo || audio && mime in AUDIO_CODECS && !hasAudio
if (!keep) continue
if (mime in VIDEO_CODECS) hasVideo = true else hasAudio = true
extractor.selectTrack(i)
tracks[i] = output.addTrack(format)
if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) bufferSize = maxOf(bufferSize, format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE))
}
if (!hasVideo) return false
output.start()
val buffer = ByteBuffer.allocate(bufferSize)
val info = MediaCodec.BufferInfo()
while (true) {
val size = extractor.readSampleData(buffer, 0)
if (size < 0) break
val track = tracks[extractor.sampleTrackIndex]
if (track != null) {
val key = extractor.sampleFlags and MediaExtractor.SAMPLE_FLAG_SYNC != 0
info.set(0, size, extractor.sampleTime, if (key) MediaCodec.BUFFER_FLAG_KEY_FRAME else 0)
output.writeSampleData(track, buffer, info)
}
extractor.advance()
}
output.stop()
output.release()
muxer = null
staged.renameTo(target)
} catch (e: Exception) {
Log.w(TAG, "could not remux ${source.name}", e)
false
} finally {
runCatching { muxer?.release() }
extractor.release()
staged.delete()
}
}
}
@@ -0,0 +1,499 @@
package com.droiddeck.launcher.session
import android.graphics.SurfaceTexture
import android.media.MediaCodec
import android.media.MediaCodecInfo
import android.media.MediaCodecList
import android.media.MediaExtractor
import android.media.MediaFormat
import android.media.MediaMuxer
import android.opengl.EGL14
import android.opengl.EGLConfig
import android.opengl.EGLExt
import android.opengl.GLES11Ext
import android.opengl.GLES20
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.view.Surface
import java.io.ByteArrayOutputStream
import java.io.File
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
internal object BootMovieTranscoder {
private const val TAG = "BootMovieTranscoder"
private const val TIMEOUT_US = 10_000L
private const val MAX_FPS = 60
private const val OPUS_RATE = 48_000
class Result(val audioDropped: Boolean)
private class Sample(val data: ByteArray, val timeUs: Long)
private class Audio(val format: MediaFormat, val samples: List<Sample>)
fun canDecode(format: MediaFormat): Boolean = runCatching {
MediaCodecList(MediaCodecList.REGULAR_CODECS).findDecoderForFormat(format) != null
}.getOrDefault(false)
fun transcode(source: File, target: File, keepAudio: Boolean = true, onProgress: (Int) -> Unit): Result? {
val hadAudio = keepAudio && trackFormat(source, "audio/") != null
val audio = if (hadAudio) runCatching { encodeAudio(source) }
.onFailure { Log.w(TAG, "could not convert the audio of ${source.name}", it) }.getOrNull() else null
val muxedAudio = runCatching { encodeVideo(source, target, audio, onProgress) }
.onFailure { Log.w(TAG, "could not convert ${source.name}", it) }.getOrNull() ?: return null
return Result(audioDropped = hadAudio && !muxedAudio)
}
private fun trackFormat(source: File, prefix: String): Pair<Int, MediaFormat>? {
val extractor = MediaExtractor()
return try {
extractor.setDataSource(source.path)
(0 until extractor.trackCount).map { it to extractor.getTrackFormat(it) }
.firstOrNull { it.second.getString(MediaFormat.KEY_MIME)?.startsWith(prefix) == true }
} finally {
extractor.release()
}
}
internal class Layout(val width: Int, val height: Int, val x: Int, val y: Int, val contentWidth: Int, val contentHeight: Int)
// Steam fills the screen with its startup movie, so portrait clips are pillarboxed into 16:9 instead of cropped.
internal fun layout(width: Int, height: Int, rotation: Int): Layout {
val (w, h) = if (rotation % 180 != 0) height to width else width to height
fun even(v: Double) = maxOf(2, (v.toInt() / 2) * 2)
if (w >= h) {
val scale = minOf(1.0, 1280.0 / w, 720.0 / h)
val cw = even(w * scale)
val ch = even(h * scale)
return Layout(cw, ch, 0, 0, cw, ch)
}
val ch = even(minOf(h, 720).toDouble())
val cw = even(w * ch.toDouble() / h)
val canvas = even(ch * 16 / 9.0)
return Layout(canvas, ch, (canvas - cw) / 2, 0, cw, ch)
}
internal fun resample(pcm: ShortArray, channels: Int, rate: Int): ShortArray {
if (rate == OPUS_RATE) return pcm
val frames = pcm.size / channels
val outFrames = (frames.toLong() * OPUS_RATE / rate).toInt()
val out = ShortArray(outFrames * channels)
for (i in 0 until outFrames) {
val pos = i.toDouble() * rate / OPUS_RATE
val i0 = pos.toInt().coerceAtMost(frames - 1)
val i1 = (i0 + 1).coerceAtMost(frames - 1)
val frac = pos - i0
for (c in 0 until channels) {
val a = pcm[i0 * channels + c]
val b = pcm[i1 * channels + c]
out[i * channels + c] = (a + (b - a) * frac).toInt().toShort()
}
}
return out
}
private fun encodeAudio(source: File): Audio? {
val (track, inFormat) = trackFormat(source, "audio/") ?: return null
val extractor = MediaExtractor()
var decoder: MediaCodec? = null
var encoder: MediaCodec? = null
try {
extractor.setDataSource(source.path)
extractor.selectTrack(track)
var rate = inFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE)
var channels = inFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT)
val dec = MediaCodec.createDecoderByType(inFormat.getString(MediaFormat.KEY_MIME)!!).also { decoder = it }
dec.configure(inFormat, null, null, 0)
dec.start()
val pcm = ByteArrayOutputStream()
val info = MediaCodec.BufferInfo()
var inputDone = false
while (true) {
if (!inputDone) {
val index = dec.dequeueInputBuffer(TIMEOUT_US)
if (index >= 0) {
val size = extractor.readSampleData(dec.getInputBuffer(index)!!, 0)
if (size < 0) {
dec.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
inputDone = true
} else {
dec.queueInputBuffer(index, 0, size, extractor.sampleTime, 0)
extractor.advance()
}
}
}
val index = dec.dequeueOutputBuffer(info, TIMEOUT_US)
if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
val format = dec.outputFormat
rate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE)
channels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT)
if (format.containsKey(MediaFormat.KEY_PCM_ENCODING) &&
format.getInteger(MediaFormat.KEY_PCM_ENCODING) != android.media.AudioFormat.ENCODING_PCM_16BIT) return null
} else if (index >= 0) {
if (info.size > 0) {
val bytes = ByteArray(info.size)
dec.getOutputBuffer(index)!!.apply { position(info.offset); get(bytes) }
pcm.write(bytes)
}
dec.releaseOutputBuffer(index, false)
if (info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) break
}
}
if (channels !in 1..2) return null
val raw = pcm.toByteArray()
val shorts = ShortArray(raw.size / 2)
ByteBuffer.wrap(raw).order(ByteOrder.nativeOrder()).asShortBuffer().get(shorts)
val samples = resample(shorts, channels, rate)
val format = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_OPUS, OPUS_RATE, channels)
format.setInteger(MediaFormat.KEY_BIT_RATE, 128_000)
val name = MediaCodecList(MediaCodecList.REGULAR_CODECS).findEncoderForFormat(format) ?: return null
val enc = MediaCodec.createByCodecName(name).also { encoder = it }
enc.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
enc.start()
val packets = ArrayList<Sample>()
var outFormat: MediaFormat? = null
var offset = 0
var encodeInputDone = false
while (true) {
if (!encodeInputDone) {
val index = enc.dequeueInputBuffer(TIMEOUT_US)
if (index >= 0) {
val buffer = enc.getInputBuffer(index)!!
buffer.clear()
val count = minOf(buffer.remaining() / 2, samples.size - offset) / channels * channels
val timeUs = offset.toLong() / channels * 1_000_000L / OPUS_RATE
if (count == 0) {
enc.queueInputBuffer(index, 0, 0, timeUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
encodeInputDone = true
} else {
buffer.order(ByteOrder.nativeOrder()).asShortBuffer().put(samples, offset, count)
enc.queueInputBuffer(index, 0, count * 2, timeUs, 0)
offset += count
}
}
}
val index = enc.dequeueOutputBuffer(info, TIMEOUT_US)
if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
outFormat = enc.outputFormat
} else if (index >= 0) {
if (info.size > 0 && info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG == 0) {
val bytes = ByteArray(info.size)
enc.getOutputBuffer(index)!!.apply { position(info.offset); get(bytes) }
packets.add(Sample(bytes, info.presentationTimeUs))
}
enc.releaseOutputBuffer(index, false)
if (info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) break
}
}
return outFormat?.let { Audio(it, packets) }
} finally {
runCatching { decoder?.stop() }
decoder?.release()
runCatching { encoder?.stop() }
encoder?.release()
extractor.release()
}
}
private fun MediaFormat.intOrNull(key: String): Int? = if (!containsKey(key)) null else
runCatching { getInteger(key) }.getOrElse { runCatching { getFloat(key).toInt() }.getOrNull() }
private fun encodeVideo(source: File, target: File, audio: Audio?, onProgress: (Int) -> Unit): Boolean {
val (track, inFormat) = trackFormat(source, "video/") ?: throw IllegalStateException("no video track")
val rotation = (inFormat.intOrNull(MediaFormat.KEY_ROTATION) ?: 0).mod(360)
val layout = layout(inFormat.getInteger(MediaFormat.KEY_WIDTH), inFormat.getInteger(MediaFormat.KEY_HEIGHT), rotation)
val width = layout.width
val height = layout.height
val fps = (inFormat.intOrNull(MediaFormat.KEY_FRAME_RATE) ?: 30).coerceIn(1, MAX_FPS)
val durationUs = if (inFormat.containsKey(MediaFormat.KEY_DURATION)) inFormat.getLong(MediaFormat.KEY_DURATION) else 0L
val minIntervalUs = 1_000_000L / MAX_FPS - 2_000
val encFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_VP8, width, height).apply {
setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
setInteger(MediaFormat.KEY_BIT_RATE, (width.toLong() * height * fps / 10).coerceIn(1_000_000, 8_000_000).toInt())
setInteger(MediaFormat.KEY_FRAME_RATE, fps)
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
}
val encoderName = MediaCodecList(MediaCodecList.REGULAR_CODECS).findEncoderForFormat(encFormat)
?: throw IllegalStateException("no VP8 encoder")
val extractor = MediaExtractor()
var encoder: MediaCodec? = null
var decoder: MediaCodec? = null
var egl: EglWindow? = null
var renderer: FrameRenderer? = null
var muxer: MediaMuxer? = null
val staged = File(target.parentFile, target.name + ".convert")
try {
extractor.setDataSource(source.path)
extractor.selectTrack(track)
val enc = MediaCodec.createByCodecName(encoderName).also { encoder = it }
enc.configure(encFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
val window = EglWindow(enc.createInputSurface()).also { egl = it }
val frames = FrameRenderer(rotation).also { renderer = it }
enc.start()
inFormat.setInteger(MediaFormat.KEY_ROTATION, 0)
val dec = MediaCodec.createDecoderByType(inFormat.getString(MediaFormat.KEY_MIME)!!).also { decoder = it }
dec.configure(inFormat, frames.surface, null, 0)
dec.start()
val mux = MediaMuxer(staged.path, MediaMuxer.OutputFormat.MUXER_OUTPUT_WEBM).also { muxer = it }
var videoTrack = -1
var audioTrack = -1
var audioIndex = 0
fun writeAudioUpTo(timeUs: Long) {
if (audioTrack < 0 || audio == null) return
val info = MediaCodec.BufferInfo()
while (audioIndex < audio.samples.size && audio.samples[audioIndex].timeUs <= timeUs) {
val sample = audio.samples[audioIndex++]
info.set(0, sample.data.size, sample.timeUs, 0)
mux.writeSampleData(audioTrack, ByteBuffer.wrap(sample.data), info)
}
}
val decInfo = MediaCodec.BufferInfo()
val encInfo = MediaCodec.BufferInfo()
var inputDone = false
var decoderDone = false
var lastKept = Long.MIN_VALUE
var lastProgress = -1
while (true) {
if (!inputDone) {
val index = dec.dequeueInputBuffer(TIMEOUT_US)
if (index >= 0) {
val size = extractor.readSampleData(dec.getInputBuffer(index)!!, 0)
if (size < 0) {
dec.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)
inputDone = true
} else {
dec.queueInputBuffer(index, 0, size, extractor.sampleTime, 0)
extractor.advance()
}
}
}
if (!decoderDone) {
val index = dec.dequeueOutputBuffer(decInfo, TIMEOUT_US)
if (index >= 0) {
val timeUs = decInfo.presentationTimeUs
val keep = decInfo.size > 0 && (lastKept == Long.MIN_VALUE || timeUs - lastKept >= minIntervalUs)
dec.releaseOutputBuffer(index, keep)
if (keep) {
if (!frames.awaitFrame()) throw IllegalStateException("decoder frame timed out")
frames.draw(layout)
window.swap(timeUs * 1000)
lastKept = timeUs
if (durationUs > 0) {
val percent = (timeUs * 100 / durationUs).toInt().coerceIn(0, 99)
if (percent != lastProgress) { lastProgress = percent; onProgress(percent) }
}
}
if (decInfo.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) {
enc.signalEndOfInputStream()
decoderDone = true
}
}
}
var encoderDone = false
while (true) {
val index = enc.dequeueOutputBuffer(encInfo, if (decoderDone) TIMEOUT_US else 0)
if (index == MediaCodec.INFO_TRY_AGAIN_LATER) break
if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
videoTrack = mux.addTrack(enc.outputFormat)
if (audio != null) audioTrack = runCatching { mux.addTrack(audio.format) }
.onFailure { Log.w(TAG, "WebM muxer refused the Opus track", it) }.getOrDefault(-1)
mux.start()
} else if (index >= 0) {
if (encInfo.size > 0 && encInfo.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG == 0) {
check(videoTrack >= 0) { "encoder produced data before its format" }
writeAudioUpTo(encInfo.presentationTimeUs)
mux.writeSampleData(videoTrack, enc.getOutputBuffer(index)!!, encInfo)
}
enc.releaseOutputBuffer(index, false)
if (encInfo.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) { encoderDone = true; break }
}
}
if (encoderDone) break
}
writeAudioUpTo(Long.MAX_VALUE)
mux.stop()
mux.release()
muxer = null
if (!staged.renameTo(target)) throw IllegalStateException("could not move the converted movie")
onProgress(100)
return audioTrack >= 0
} finally {
runCatching { muxer?.release() }
runCatching { decoder?.stop() }
decoder?.release()
runCatching { encoder?.stop() }
encoder?.release()
renderer?.release()
egl?.release()
extractor.release()
staged.delete()
}
}
private class EglWindow(private val surface: Surface) {
private val display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)
private val context: android.opengl.EGLContext
private val window: android.opengl.EGLSurface
init {
val version = IntArray(2)
check(EGL14.eglInitialize(display, version, 0, version, 1)) { "eglInitialize failed" }
val attributes = intArrayOf(
EGL14.EGL_RED_SIZE, 8, EGL14.EGL_GREEN_SIZE, 8, EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, EGL_RECORDABLE_ANDROID, 1, EGL14.EGL_NONE,
)
val configs = arrayOfNulls<EGLConfig>(1)
val count = IntArray(1)
check(EGL14.eglChooseConfig(display, attributes, 0, configs, 0, 1, count, 0) && count[0] > 0) { "no recordable EGL config" }
context = EGL14.eglCreateContext(display, configs[0], EGL14.EGL_NO_CONTEXT,
intArrayOf(EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE), 0)
window = EGL14.eglCreateWindowSurface(display, configs[0], surface, intArrayOf(EGL14.EGL_NONE), 0)
check(EGL14.eglMakeCurrent(display, window, window, context)) { "eglMakeCurrent failed" }
}
fun swap(timeNs: Long) {
EGLExt.eglPresentationTimeANDROID(display, window, timeNs)
EGL14.eglSwapBuffers(display, window)
}
fun release() {
EGL14.eglMakeCurrent(display, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)
EGL14.eglDestroySurface(display, window)
EGL14.eglDestroyContext(display, context)
EGL14.eglReleaseThread()
EGL14.eglTerminate(display)
surface.release()
}
companion object {
private const val EGL_RECORDABLE_ANDROID = 0x3142
}
}
private class FrameRenderer(rotation: Int) {
private val thread = HandlerThread("boot-movie-frames").apply { start() }
private val lock = Object()
private var available = false
private val texture = IntArray(1).also { GLES20.glGenTextures(1, it, 0) }[0]
private val surfaceTexture: SurfaceTexture
val surface: Surface
private val program: Int
private val transform = FloatArray(16)
private val positions: FloatBuffer
private val coords: FloatBuffer = floats(0f, 0f, 1f, 0f, 0f, 1f, 1f, 1f)
init {
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture)
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR)
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR)
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
surfaceTexture = SurfaceTexture(texture)
surfaceTexture.setOnFrameAvailableListener({
synchronized(lock) { available = true; lock.notifyAll() }
}, Handler(thread.looper))
surface = Surface(surfaceTexture)
program = link(VERTEX, FRAGMENT)
val radians = Math.toRadians(rotation.toDouble())
val cos = Math.cos(radians).toFloat()
val sin = Math.sin(radians).toFloat()
val corners = floatArrayOf(-1f, -1f, 1f, -1f, -1f, 1f, 1f, 1f)
positions = floats(*FloatArray(8) { i ->
val x = corners[i - i % 2]
val y = corners[i - i % 2 + 1]
if (i % 2 == 0) x * cos + y * sin else -x * sin + y * cos
})
}
fun awaitFrame(): Boolean {
synchronized(lock) {
val deadline = System.currentTimeMillis() + 2_500
while (!available) {
val wait = deadline - System.currentTimeMillis()
if (wait <= 0) return false
lock.wait(wait)
}
available = false
}
surfaceTexture.updateTexImage()
return true
}
fun draw(layout: Layout) {
surfaceTexture.getTransformMatrix(transform)
GLES20.glViewport(0, 0, layout.width, layout.height)
GLES20.glClearColor(0f, 0f, 0f, 1f)
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
GLES20.glViewport(layout.x, layout.y, layout.contentWidth, layout.contentHeight)
GLES20.glUseProgram(program)
GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture)
val position = GLES20.glGetAttribLocation(program, "aPosition")
val coord = GLES20.glGetAttribLocation(program, "aCoord")
GLES20.glEnableVertexAttribArray(position)
GLES20.glVertexAttribPointer(position, 2, GLES20.GL_FLOAT, false, 0, positions)
GLES20.glEnableVertexAttribArray(coord)
GLES20.glVertexAttribPointer(coord, 2, GLES20.GL_FLOAT, false, 0, coords)
GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(program, "uTransform"), 1, false, transform, 0)
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
}
fun release() {
surface.release()
surfaceTexture.release()
GLES20.glDeleteProgram(program)
GLES20.glDeleteTextures(1, intArrayOf(texture), 0)
thread.quitSafely()
}
private fun floats(vararg values: Float): FloatBuffer =
ByteBuffer.allocateDirect(values.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer().apply { put(values); position(0) }
private fun link(vertex: String, fragment: String): Int {
fun compile(type: Int, code: String): Int = GLES20.glCreateShader(type).also {
GLES20.glShaderSource(it, code)
GLES20.glCompileShader(it)
val ok = IntArray(1)
GLES20.glGetShaderiv(it, GLES20.GL_COMPILE_STATUS, ok, 0)
check(ok[0] != 0) { GLES20.glGetShaderInfoLog(it) }
}
return GLES20.glCreateProgram().also {
GLES20.glAttachShader(it, compile(GLES20.GL_VERTEX_SHADER, vertex))
GLES20.glAttachShader(it, compile(GLES20.GL_FRAGMENT_SHADER, fragment))
GLES20.glLinkProgram(it)
val ok = IntArray(1)
GLES20.glGetProgramiv(it, GLES20.GL_LINK_STATUS, ok, 0)
check(ok[0] != 0) { GLES20.glGetProgramInfoLog(it) }
}
}
companion object {
private val VERTEX = """
attribute vec4 aPosition;
attribute vec4 aCoord;
uniform mat4 uTransform;
varying vec2 vCoord;
void main() {
gl_Position = aPosition;
vCoord = (uTransform * aCoord).xy;
}
""".trimIndent()
private val FRAGMENT = """
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vCoord;
uniform samplerExternalOES sTexture;
void main() {
gl_FragColor = texture2D(sTexture, vCoord);
}
""".trimIndent()
}
}
}
@@ -188,27 +188,29 @@ object SessionFiles {
}
val startupMovieDir = File(root, "root/.local/share/Steam/config/uioverrides/movies")
val bigPictureMovieInstalled = stageStartupMovie(
context,
startupMovieDir,
"bigpicture_startup.webm",
)
stageStartupMovie(context, startupMovieDir, "steam_os_startup.webm")
val startupMovie = BootMovie.prepare(context)
val bigPictureMovieInstalled = startupMovie != null &&
stageStartupMovie(startupMovie, startupMovieDir, "bigpicture_startup.webm")
if (startupMovie != null) stageStartupMovie(startupMovie, startupMovieDir, "steam_os_startup.webm")
if (bigPictureMovieInstalled) {
ensureStartupMovieDefault(File(root, "root/.local/share/Steam/config/config.vdf"))
}
}
private fun stageStartupMovie(context: Context, directory: File, name: String): Boolean {
private fun stageStartupMovie(source: File, directory: File, name: String): Boolean {
val movie = File(directory, name)
val stamp = File(directory, ".$name.source")
val identity = "${source.absolutePath}\n${source.length()}\n${source.lastModified()}"
if (movie.length() == source.length() && runCatching { stamp.readText() }.getOrNull() == identity) return true
val staged = File(directory, "$name.staged")
var installed = false
try {
directory.mkdirs()
context.assets.open("steam-startup/droiddeck-startup.webm").use { input ->
source.inputStream().use { input ->
staged.outputStream().use { output -> FileUtils.copy(input, output) }
}
installed = staged.setReadable(true, false) && staged.renameTo(movie)
if (installed) FileUtils.writeString(stamp, identity)
} catch (e: Exception) {
Log.w(TAG, "could not stage Steam startup movie $name", e)
} finally {
@@ -499,6 +499,16 @@ object SessionPrefs {
.onFailure { android.util.Log.e("GameEnvironment", "Could not update game environment", it) }
}
fun bootMovie(context: Context): String =
prefs(context).getString("bootMovie", BootMovie.DROIDDECK_30) ?: BootMovie.DROIDDECK_30
fun setBootMovie(context: Context, id: String) { prefs(context).edit().putString("bootMovie", id).apply() }
fun bootMovieSound(context: Context): Boolean = prefs(context).getBoolean("bootMovieSound", true)
fun setBootMovieSound(context: Context, on: Boolean) { prefs(context).edit().putBoolean("bootMovieSound", on).apply() }
fun bootMovieCustomName(context: Context): String? = prefs(context).getString("bootMovieCustomName", null)
fun setBootMovieCustomName(context: Context, name: String?) {
prefs(context).edit().apply { if (name == null) remove("bootMovieCustomName") else putString("bootMovieCustomName", name) }.apply()
}
fun runSteamAtStartup(context: Context): Boolean = prefs(context).getBoolean("runSteamAtStartup", false)
fun setRunSteamAtStartup(context: Context, on: Boolean) {
@@ -21,7 +21,7 @@ object SettingsReset {
"noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath",
"guestHostname", "fexPreset", "forceSsbs", "logs", "storageDiagnostics",
"upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias",
"steamDownloadsInBackground", "steam_offline",
"steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound",
"questDefaultsV1",
)
@@ -27,6 +27,7 @@ import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.onGloballyPositioned
import com.droiddeck.launcher.core.FexPreset
import com.droiddeck.launcher.runtime.DeckyManager
import com.droiddeck.launcher.session.BootMovie
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionService
@@ -84,6 +85,11 @@ class ModeSettings(
val steamController: String? = null,
val unfoldedControls: Boolean? = null,
val runSteamAtStartup: Boolean = false,
val bootMovie: String? = null,
val bootMovieSound: Boolean = true,
val bootMovieCustomName: String? = null,
val bootMovieImporting: Boolean = false,
val bootMovieProgress: Int = -1,
val wifiDiscovery: Boolean? = null,
val wifiDiscoveryPermission: Boolean = false,
val wifiDiscoveryLocation: Boolean = false,
@@ -128,6 +134,10 @@ class ModeSettingsActions(
val onSteamController: (String) -> Unit = {},
val onUnfoldedControls: (Boolean) -> Unit = {},
val onRunSteamAtStartup: (Boolean) -> Unit = {},
val onBootMovie: (String) -> Unit = {},
val onBootMovieSound: (Boolean) -> Unit = {},
val onImportBootMovie: () -> Unit = {},
val onRemoveBootMovie: () -> Unit = {},
val onWifiDiscovery: (Boolean) -> Unit = {},
val onWifiDiscoverySettings: () -> Unit = {},
val onDeckyInstall: (DeckyManager.Release) -> Unit = {},
@@ -417,6 +427,41 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
SecondaryButton(stringResource(R.string.mode_steam_repair_button), enabled = !s.steamRepairQueued, onClick = a.onSteamRepair)
}
}
if (s.bootMovie != null) SettingsGroup(stringResource(R.string.boot_movie)) {
ChoiceRow(
host, "boot-movie", stringResource(R.string.boot_movie_video), stringResource(R.string.common_applies_next_session),
buildList {
add(BootMovie.DROIDDECK_30 to stringResource(R.string.boot_movie_droiddeck, 30))
add(BootMovie.DROIDDECK_60 to stringResource(R.string.boot_movie_droiddeck, 60))
s.bootMovieCustomName?.let { add(BootMovie.CUSTOM to it) }
}, s.bootMovie,
note = stringResource(R.string.boot_movie_note), onPick = a.onBootMovie,
)
ToggleRow(
host, "boot-movie-sound", stringResource(R.string.boot_movie_sound),
stringResource(R.string.common_applies_next_session), s.bootMovieSound, onChange = a.onBootMovieSound,
)
SettingsRow(
stringResource(R.string.boot_movie_custom),
stringResource(R.string.boot_movie_custom_hint, (BootMovie.MAX_BYTES shr 20).toInt(), BootMovie.MAX_CONVERT_SECONDS),
) {
Row(horizontalArrangement = androidx.compose.foundation.layout.Arrangement.spacedBy(6.dp)) {
SecondaryButton(
when {
s.bootMovieProgress >= 0 -> stringResource(R.string.boot_movie_converting, s.bootMovieProgress)
s.bootMovieImporting -> stringResource(R.string.common_working)
else -> stringResource(R.string.boot_movie_import)
},
enabled = !s.bootMovieImporting,
progress = s.bootMovieProgress.takeIf { it >= 0 }?.let { it / 100f },
onClick = a.onImportBootMovie,
)
if (s.bootMovieCustomName != null) SecondaryButton(
stringResource(R.string.boot_movie_remove), enabled = !s.bootMovieImporting, onClick = a.onRemoveBootMovie,
)
}
}
}
if (s.wifiDiscovery != null) SettingsGroup(stringResource(R.string.mode_network)) {
val hint = when {
s.wifiDiscovery && !s.wifiDiscoveryLocation -> stringResource(R.string.mode_wifi_location_off)