Skip to content

Commit

Permalink
[fix|optimize] Fix missing MPV default font; optimize subtitle track …
Browse files Browse the repository at this point in the history
…display
  • Loading branch information
SkyD666 committed May 17, 2024
1 parent e587d96 commit 70374dd
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 162 deletions.
29 changes: 12 additions & 17 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
minSdk = 24
targetSdk = 34
versionCode = 16
versionName = "1.1-beta25"
versionName = "1.1-beta26"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -92,12 +92,6 @@ android {
)
}
}
androidResources {
ignoreAssetsPatterns += listOf(
"subfont.ttf", // mpv-android
"cacert.pem", // mpv-android
)
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand All @@ -122,13 +116,14 @@ android {
"kotlin-tooling-metadata.json",
"okhttp3/internal/publicsuffix/NOTICE",
)
jniLibs.excludes += mutableSetOf(
"lib/*/libffmpegkit.so", // mpv-android
"lib/*/libffmpegkit_abidetect.so", // mpv-android
)
jniLibs {
excludes += mutableSetOf(
"lib/*/libffmpegkit.so", // mpv-android
"lib/*/libffmpegkit_abidetect.so", // mpv-android
)
useLegacyPackaging = true
}
dex {
// Set to "true" because android:extractNativeLibs
// is set to "true" in AndroidManifest.xml
useLegacyPackaging = true
}
}
Expand Down Expand Up @@ -162,7 +157,7 @@ dependencies {
implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
implementation("androidx.navigation:navigation-ui-ktx:2.7.7")
implementation("androidx.navigation:navigation-compose:2.7.7")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.0")
implementation("androidx.compose.ui:ui:1.6.7")
implementation("androidx.compose.material:material:1.6.7")
implementation("androidx.compose.material3:material3:1.3.0-alpha05")
Expand All @@ -177,8 +172,8 @@ dependencies {
implementation("androidx.datastore:datastore-preferences:1.1.1")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation("androidx.core:core-splashscreen:1.0.1")
implementation("androidx.paging:paging-runtime-ktx:3.2.1")
implementation("androidx.paging:paging-compose:3.3.0-beta01")
implementation("androidx.paging:paging-runtime-ktx:3.3.0")
implementation("androidx.paging:paging-compose:3.3.0")
implementation("androidx.preference:preference-ktx:1.2.1")

implementation("com.google.android.material:material:1.12.0")
Expand All @@ -196,7 +191,7 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.7.3")

implementation("com.github.aniyomiorg:aniyomi-mpv-lib:1.15.n")
implementation("com.github.jmir1:ffmpeg-kit:1.14")
implementation("com.github.jmir1:ffmpeg-kit:1.15")

implementation("io.coil-kt:coil:2.6.0")
implementation("io.coil-kt:coil-compose:2.6.0")
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/com/skyd/anivu/config/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ object Const {
if (!exists()) mkdirs()
}

val MPV_CONFIG_DIR = File("${appContext.filesDir.path}/Mpv", "Config").apply {
if (!exists()) mkdirs()
}

val MPV_CACHE_DIR = File("${appContext.cacheDir.path}/Mpv", "Cache").apply {
if (!exists()) mkdirs()
}
val MPV_CONFIG_DIR = File("${appContext.filesDir.path}/Mpv", "Config")
.apply { if (!exists()) mkdirs() }
val MPV_CACHE_DIR = File("${appContext.cacheDir.path}/Mpv", "Cache")
.apply { if (!exists()) mkdirs() }
val MPV_FONT_DIR = File(MPV_CONFIG_DIR, "Font")
.apply { if (!exists()) mkdirs() }
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/skyd/anivu/ui/activity/PlayActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.skyd.anivu.base.BaseComposeActivity
import com.skyd.anivu.ui.mpv.PlayerView
import com.skyd.anivu.ui.mpv.copyAssetsForMpv


class PlayActivity : BaseComposeActivity() {
Expand All @@ -24,6 +25,8 @@ class PlayActivity : BaseComposeActivity() {
}

override fun onCreate(savedInstanceState: Bundle?) {
copyAssetsForMpv(this)

super.onCreate(savedInstanceState)

val windowInsetsController =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.skyd.anivu.ui.component.dialog

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
Expand All @@ -26,6 +27,7 @@ fun AniVuDialog(
title: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
selectable: Boolean = true,
scrollable: Boolean = true,
confirmButton: @Composable () -> Unit,
dismissButton: @Composable (() -> Unit)? = null,
) {
Expand All @@ -38,11 +40,21 @@ fun AniVuDialog(
title = title,
text = {
if (selectable) {
SelectionContainer(modifier = Modifier.verticalScroll(rememberScrollState())) {
SelectionContainer(
modifier = Modifier.run {
if (scrollable) verticalScroll(rememberScrollState()) else this
},
) {
text?.invoke()
}
} else {
text?.invoke()
if (scrollable) {
Box(modifier = Modifier.verticalScroll(rememberScrollState())) {
text?.invoke()
}
} else {
text?.invoke()
}
}
},
confirmButton = confirmButton,
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/skyd/anivu/ui/mpv/MPVView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ class MPVView(context: Context, attrs: AttributeSet?) : SurfaceView(context, att
SurfaceHolder.Callback {
private var surfaceCreated = false

fun initialize(configDir: String, cacheDir: String, logLvl: String = "v", vo: String = "gpu") {
fun initialize(
configDir: String,
cacheDir: String,
fontDir: String,
logLvl: String = "v",
vo: String = "gpu",
) {
MPVLib.create(this.context, logLvl)
MPVLib.setOptionString("config", "yes")
MPVLib.setOptionString("config-dir", configDir)
Expand All @@ -37,6 +43,8 @@ class MPVView(context: Context, attrs: AttributeSet?) : SurfaceView(context, att
MPVLib.setOptionString("force-window", "no")
// "no" wouldn't work and "yes" is not intended by the UI
MPVLib.setOptionString("idle", "yes")
MPVLib.setPropertyString("sub-fonts-dir", fontDir)
MPVLib.setPropertyString("osd-fonts-dir", fontDir)

holder.addCallback(this)
observeProperties()
Expand Down Expand Up @@ -67,7 +75,7 @@ class MPVView(context: Context, attrs: AttributeSet?) : SurfaceView(context, att
MPVLib.setOptionString("gpu-context", "android")
MPVLib.setOptionString("opengl-es", "yes")
MPVLib.setOptionString("hwdec", hwdec)
MPVLib.setOptionString("hwdec-codecs", "h264,hevc,mpeg4,mpeg2video,vp8,vp9,av1")
// MPVLib.setOptionString("hwdec-codecs", "h264,hevc,mpeg4,mpeg2video,vp8,vp9,av1")
MPVLib.setOptionString("ao", "audiotrack,opensles")
MPVLib.setOptionString("input-default-bindings", "yes")
// Limit demuxer cache since the defaults are too high for mobile devices
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/skyd/anivu/ui/mpv/PlayerUtil.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.skyd.anivu.ui.mpv

import android.content.Context
import android.content.res.AssetManager
import android.net.Uri
import android.os.ParcelFileDescriptor
import android.util.Log
import com.skyd.anivu.config.Const
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

internal fun Uri.resolveUri(context: Context): String? {
Expand Down Expand Up @@ -58,4 +62,30 @@ fun findRealPath(fd: Int): String? {
ins?.close()
}
return null
}

fun copyAssetsForMpv(context: Context) {
val assetManager = context.assets
arrayOf(
"subfont.ttf", "cacert.pem"
).forEach { filename ->
try {
assetManager.open(filename, AssetManager.ACCESS_STREAMING).use { ins ->
val outFile = File("${Const.MPV_CONFIG_DIR.path}/$filename")
// Note that .available() officially returns an *estimated* number of bytes available
// this is only true for generic streams, asset streams return the full file size
if (outFile.length() == ins.available().toLong()) {
Log.v(
"copyAssetsForMpv",
"Skipping copy of asset file (exists same size): $filename"
)
return@forEach
}
FileOutputStream(outFile).use { out -> ins.copyTo(out) }
Log.w("copyAssetsForMpv", "Copied asset file: $filename")
}
} catch (e: IOException) {
Log.e("copyAssetsForMpv", "Failed to copy asset file: $filename", e)
}
}
}
Loading

0 comments on commit 70374dd

Please sign in to comment.