Skip to content

Commit

Permalink
[feature] Support configure hardware decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed May 19, 2024
1 parent 2b08882 commit a50eebb
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion 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-beta29"
versionName = "1.1-beta30"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/PreferenceExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.skyd.anivu.model.preference.behavior.article.ArticleSwipeLeftActionPr
import com.skyd.anivu.model.preference.behavior.article.ArticleTapActionPreference
import com.skyd.anivu.model.preference.behavior.article.DeduplicateTitleInDescPreference
import com.skyd.anivu.model.preference.behavior.feed.HideEmptyDefaultPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
import com.skyd.anivu.model.preference.player.PlayerShow85sButtonPreference
import com.skyd.anivu.model.preference.player.PlayerShowScreenshotButtonPreference
Expand Down Expand Up @@ -40,5 +41,6 @@ fun Preferences.toSettings(): Settings {
playerDoubleTap = PlayerDoubleTapPreference.fromPreferences(this),
playerShow85sButton = PlayerShow85sButtonPreference.fromPreferences(this),
playerShowScreenshotButton = PlayerShowScreenshotButtonPreference.fromPreferences(this),
hardwareDecode = HardwareDecodePreference.fromPreferences(this),
)
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/skyd/anivu/model/preference/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.skyd.anivu.model.preference.behavior.article.ArticleSwipeLeftActionPr
import com.skyd.anivu.model.preference.behavior.article.ArticleTapActionPreference
import com.skyd.anivu.model.preference.behavior.article.DeduplicateTitleInDescPreference
import com.skyd.anivu.model.preference.behavior.feed.HideEmptyDefaultPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
import com.skyd.anivu.model.preference.player.PlayerShow85sButtonPreference
import com.skyd.anivu.model.preference.player.PlayerShowScreenshotButtonPreference
Expand All @@ -27,6 +28,7 @@ import com.skyd.anivu.ui.local.LocalDarkMode
import com.skyd.anivu.ui.local.LocalDateStyle
import com.skyd.anivu.ui.local.LocalDeduplicateTitleInDesc
import com.skyd.anivu.ui.local.LocalFeedGroupExpand
import com.skyd.anivu.ui.local.LocalHardwareDecode
import com.skyd.anivu.ui.local.LocalHideEmptyDefault
import com.skyd.anivu.ui.local.LocalIgnoreUpdateVersion
import com.skyd.anivu.ui.local.LocalNavigationBarLabel
Expand Down Expand Up @@ -57,6 +59,7 @@ data class Settings(
val playerDoubleTap: String = PlayerDoubleTapPreference.default,
val playerShow85sButton: Boolean = PlayerShow85sButtonPreference.default,
val playerShowScreenshotButton: Boolean = PlayerShowScreenshotButtonPreference.default,
val hardwareDecode: Boolean = HardwareDecodePreference.default,
)

@Composable
Expand Down Expand Up @@ -86,6 +89,7 @@ fun SettingsProvider(
LocalPlayerDoubleTap provides settings.playerDoubleTap,
LocalPlayerShow85sButton provides settings.playerShow85sButton,
LocalPlayerShowScreenshotButton provides settings.playerShowScreenshotButton,
LocalHardwareDecode provides settings.hardwareDecode,
) {
content()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.skyd.anivu.model.preference.player

import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import com.skyd.anivu.base.BasePreference
import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.put
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object HardwareDecodePreference : BasePreference<Boolean> {
private const val HARDWARE_DECODE = "hardwareDecode"
override val default = true

val key = booleanPreferencesKey(HARDWARE_DECODE)

fun put(context: Context, scope: CoroutineScope, value: Boolean) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(key, value)
}
}

override fun fromPreferences(preferences: Preferences): Boolean = preferences[key] ?: default
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.PlayCircle
import androidx.compose.material.icons.rounded.DeveloperBoard
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
Expand All @@ -20,14 +21,18 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.skyd.anivu.R
import com.skyd.anivu.base.BaseComposeFragment
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.MpvConfigPreference
import com.skyd.anivu.ui.component.AniVuTopBar
import com.skyd.anivu.ui.component.AniVuTopBarStyle
import com.skyd.anivu.ui.component.BaseSettingsItem
import com.skyd.anivu.ui.component.SwitchSettingsItem
import com.skyd.anivu.ui.component.dialog.TextFieldDialog
import com.skyd.anivu.ui.local.LocalHardwareDecode
import dagger.hilt.android.AndroidEntryPoint


Expand All @@ -43,6 +48,7 @@ class PlayerConfigAdvancedFragment : BaseComposeFragment() {
@Composable
fun PlayerConfigAdvancedScreen() {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
val context = LocalContext.current
val scope = rememberCoroutineScope()
var mpvConfEditDialogValue by rememberSaveable { mutableStateOf("") }
var openMpvConfEditDialog by rememberSaveable { mutableStateOf(false) }
Expand All @@ -62,6 +68,21 @@ fun PlayerConfigAdvancedScreen() {
.nestedScroll(scrollBehavior.nestedScrollConnection),
contentPadding = paddingValues,
) {
item {
SwitchSettingsItem(
imageVector = Icons.Rounded.DeveloperBoard,
text = stringResource(id = R.string.player_config_advanced_screen_hardware_decode),
description = stringResource(id = R.string.player_config_advanced_screen_hardware_decode_description),
checked = LocalHardwareDecode.current,
onCheckedChange = {
HardwareDecodePreference.put(
context = context,
scope = scope,
value = it,
)
}
)
}
item {
BaseSettingsItem(
icon = rememberVectorPainter(Icons.Outlined.PlayCircle),
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/skyd/anivu/ui/local/LocalValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.skyd.anivu.model.preference.behavior.article.ArticleSwipeLeftActionPr
import com.skyd.anivu.model.preference.behavior.article.ArticleTapActionPreference
import com.skyd.anivu.model.preference.behavior.article.DeduplicateTitleInDescPreference
import com.skyd.anivu.model.preference.behavior.feed.HideEmptyDefaultPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
import com.skyd.anivu.model.preference.player.PlayerShow85sButtonPreference
import com.skyd.anivu.model.preference.player.PlayerShowScreenshotButtonPreference
Expand Down Expand Up @@ -46,4 +47,5 @@ val LocalHideEmptyDefault = compositionLocalOf { HideEmptyDefaultPreference.defa
// Player
val LocalPlayerDoubleTap = compositionLocalOf { PlayerDoubleTapPreference.default }
val LocalPlayerShow85sButton = compositionLocalOf { PlayerShow85sButtonPreference.default }
val LocalPlayerShowScreenshotButton = compositionLocalOf { PlayerShowScreenshotButtonPreference.default }
val LocalPlayerShowScreenshotButton = compositionLocalOf { PlayerShowScreenshotButtonPreference.default }
val LocalHardwareDecode = compositionLocalOf { HardwareDecodePreference.default }
13 changes: 7 additions & 6 deletions app/src/main/java/com/skyd/anivu/ui/mpv/MPVView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.WindowManager
import com.skyd.anivu.config.Const
import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.getOrDefault
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import `is`.xyz.mpv.MPVLib
import `is`.xyz.mpv.MPVLib.mpvFormat.MPV_FORMAT_DOUBLE
import `is`.xyz.mpv.MPVLib.mpvFormat.MPV_FORMAT_FLAG
Expand Down Expand Up @@ -69,9 +72,6 @@ class MPVView(context: Context, attrs: AttributeSet?) : SurfaceView(context, att
// vo
voInUse = vo

// hwdec
val hwdec = "auto"//"no"

// vo: set display fps as reported by android
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val disp = wm.defaultDisplay
Expand All @@ -84,8 +84,10 @@ class MPVView(context: Context, attrs: AttributeSet?) : SurfaceView(context, att
MPVLib.setOptionString("vo", vo)
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",
if (context.dataStore.getOrDefault(HardwareDecodePreference)) "auto" else "no"
)
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 Expand Up @@ -121,7 +123,6 @@ class MPVView(context: Context, attrs: AttributeSet?) : SurfaceView(context, att
Property("eof-reached", MPV_FORMAT_FLAG),
Property("paused-for-cache", MPV_FORMAT_FLAG),
Property("track-list"),
Property("sub-add"),
// observing double properties is not hooked up in the JNI code, but doing this
// will restrict updates to when it actually changes
Property("video-zoom", MPV_FORMAT_DOUBLE),
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
<string name="player_screenshot">截图</string>
<string name="player_no_permission_cannot_save_screenshot">无权限。无法保存截图。</string>
<string name="player_config_fragment_show_screenshot_button">截图按钮</string>
<string name="player_add_external_subtitle">添加字幕文件</string>
<string name="player_config_advanced_screen_hardware_decode">硬件解码</string>
<string name="player_config_advanced_screen_hardware_decode_description">优先使用硬件解码,失败时改用软件解码。</string>
<plurals name="rss_sync_frequency_minute">
<item quantity="other">每 %d 分钟</item>
</plurals>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@
<string name="player_no_permission_cannot_save_screenshot">No permission. Can\'t save screenshot.</string>
<string name="player_config_fragment_show_screenshot_button">Screenshot button</string>
<string name="player_add_external_subtitle">Add subtitle file</string>
<string name="player_config_advanced_screen_hardware_decode">Hardware decoding</string>
<string name="player_config_advanced_screen_hardware_decode_description">Try hardware decoding first; fallback to software if it fails</string>
<plurals name="rss_sync_frequency_minute">
<item quantity="one">Every %d minute</item>
<item quantity="other">Every %d minutes</item>
Expand Down

0 comments on commit a50eebb

Please sign in to comment.