MPVPlayer: Support for software decoding + clean up

This commit is contained in:
jarnedemeulemeester 2021-09-18 15:17:20 +02:00
parent 0baef5ec2e
commit 64b86f2b40
No known key found for this signature in database
GPG key ID: B61B7B150DB6A6D2
5 changed files with 40 additions and 27 deletions

View file

@ -1,14 +1,11 @@
#vo=mediacodec_embed ### hwdec: try to use hardware decoding
# hwdec: try to use hardware decoding # hwdec=mediacodec-copy
hwdec=mediacodec-copy # hwdec-codecs="h264,hevc,mpeg4,mpeg2video,vp8,vp9,av1"
hwdec-codecs="h264,hevc,mpeg4,mpeg2video,vp8,vp9,av1"
gpu-dumb-mode=auto ### tls: allow self signed certificate
# tls: allow self signed certificate # tls-verify=no
tls-verify=no # tls-ca-file=""
tls-ca-file=""
# demuxer: limit cache to 32 MiB, the default is too high for mobile devices ### sub: scale subtitles with video
demuxer-max-bytes=32MiB # sub-scale-with-window=no
demuxer-max-back-bytes=32MiB # sub-use-margins=no
# sub: scale subtitles with video
sub-scale-with-window=no
sub-use-margins=no

Binary file not shown.

View file

@ -40,7 +40,8 @@ import java.util.concurrent.CopyOnWriteArraySet
class MPVPlayer( class MPVPlayer(
context: Context, context: Context,
requestAudioFocus: Boolean, requestAudioFocus: Boolean,
preferredLanguages: Map<String, String> preferredLanguages: Map<String, String>,
disableHardwareDecoding: Boolean
) : BasePlayer(), MPVLib.EventObserver, AudioManager.OnAudioFocusChangeListener { ) : BasePlayer(), MPVLib.EventObserver, AudioManager.OnAudioFocusChangeListener {
private val audioManager: AudioManager by lazy { context.getSystemService()!! } private val audioManager: AudioManager by lazy { context.getSystemService()!! }
@ -50,13 +51,11 @@ class MPVPlayer(
init { init {
require(context is Application) require(context is Application)
@Suppress("DEPRECATION")
val mpvDir = File(context.getExternalFilesDir(null) ?: context.filesDir, "mpv") val mpvDir = File(context.getExternalFilesDir(null) ?: context.filesDir, "mpv")
if (!mpvDir.exists()) { if (!mpvDir.exists()) {
mpvDir.mkdirs() mpvDir.mkdirs()
} }
// https://github.com/mpv-android/mpv-android/commit/12d4d78 arrayOf("mpv.conf", "subfont.ttf").forEach { fileName ->
arrayOf("mpv.conf", "subfont.ttf"/*, "cacert.pem"*/).forEach { fileName ->
val file = File(mpvDir, fileName) val file = File(mpvDir, fileName)
Log.i("mpv", "File ${file.absolutePath}") Log.i("mpv", "File ${file.absolutePath}")
if (!file.exists()) { if (!file.exists()) {
@ -64,24 +63,43 @@ class MPVPlayer(
} }
} }
MPVLib.create(context) MPVLib.create(context)
MPVLib.setOptionString("config", "yes")
// General
MPVLib.setOptionString("config-dir", mpvDir.path) MPVLib.setOptionString("config-dir", mpvDir.path)
// vo: set display fps as reported by android
MPVLib.setOptionString("vo", "gpu") MPVLib.setOptionString("vo", "gpu")
MPVLib.setOptionString("gpu-context", "android") MPVLib.setOptionString("gpu-context", "android")
MPVLib.setOptionString("ao", "audiotrack,opensles") MPVLib.setOptionString("ao", "audiotrack,opensles")
MPVLib.init() // Hardware video decoding
if (disableHardwareDecoding) {
MPVLib.setOptionString("hwdec", "no")
} else {
MPVLib.setOptionString("hwdec", "mediacodec-copy")
}
MPVLib.setOptionString("hwdec-codecs", "h264,hevc,mpeg4,mpeg2video,vp8,vp9,av1")
// hardcoded options // TLS
MPVLib.setOptionString("tls-verify", "no")
// Cache
MPVLib.setOptionString("cache", "yes") MPVLib.setOptionString("cache", "yes")
MPVLib.setOptionString("cache-pause-initial", "yes") MPVLib.setOptionString("cache-pause-initial", "yes")
MPVLib.setOptionString("demuxer-max-bytes", "32MiB")
MPVLib.setOptionString("demuxer-max-back-bytes", "32MiB")
// Subs
MPVLib.setOptionString("sub-scale-with-window", "no")
MPVLib.setOptionString("sub-use-margins", "no")
// Other options
MPVLib.setOptionString("force-window", "no") MPVLib.setOptionString("force-window", "no")
MPVLib.setOptionString("keep-open", "always") MPVLib.setOptionString("keep-open", "always")
MPVLib.setOptionString("save-position-on-quit", "no") MPVLib.setOptionString("save-position-on-quit", "no")
MPVLib.setOptionString("sub-font-provider", "none") MPVLib.setOptionString("sub-font-provider", "none")
MPVLib.setOptionString("ytdl", "no") MPVLib.setOptionString("ytdl", "no")
MPVLib.init()
for (preferredLanguage in preferredLanguages) { for (preferredLanguage in preferredLanguages) {
when (preferredLanguage.key) { when (preferredLanguage.key) {
TrackType.AUDIO -> { TrackType.AUDIO -> {
@ -1281,7 +1299,6 @@ class MPVPlayer(
COMMAND_PREPARE_STOP, COMMAND_PREPARE_STOP,
COMMAND_SET_SPEED_AND_PITCH, COMMAND_SET_SPEED_AND_PITCH,
COMMAND_GET_CURRENT_MEDIA_ITEM, COMMAND_GET_CURRENT_MEDIA_ITEM,
//COMMAND_GET_MEDIA_ITEMS,
COMMAND_GET_MEDIA_ITEMS_METADATA, COMMAND_GET_MEDIA_ITEMS_METADATA,
COMMAND_CHANGE_MEDIA_ITEMS, COMMAND_CHANGE_MEDIA_ITEMS,
COMMAND_SET_VIDEO_SURFACE, COMMAND_SET_VIDEO_SURFACE,
@ -1455,8 +1472,6 @@ class MPVPlayer(
return Triple(tracks, trackGroupArray, trackSelectionArray) return Triple(tracks, trackGroupArray, trackSelectionArray)
} }
const val PLAYER_NAME = "MPV Player"
/** /**
* Merges multiple [subtitleSources] into a single [videoSource] * Merges multiple [subtitleSources] into a single [videoSource]
*/ */

View file

@ -59,7 +59,7 @@ constructor(
if (useMpv) { if (useMpv) {
val preferredLanguages = mapOf(TrackType.AUDIO to preferredAudioLanguage, TrackType.SUBTITLE to preferredSubtitleLanguage) val preferredLanguages = mapOf(TrackType.AUDIO to preferredAudioLanguage, TrackType.SUBTITLE to preferredSubtitleLanguage)
player = MPVPlayer(application, false, preferredLanguages) player = MPVPlayer(application, false, preferredLanguages, sp.getBoolean("mpv_disable_hwdec", false))
} else { } else {
val renderersFactory = val renderersFactory =
DefaultRenderersFactory(application).setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON) DefaultRenderersFactory(application).setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
@ -101,7 +101,7 @@ constructor(
when (player) { when (player) {
is MPVPlayer -> { is MPVPlayer -> {
player.setMediaItem(mediaItems[0]) player.setMediaItems(mediaItems)
player.prepare() player.prepare()
player.play() player.play()
} }

View file

@ -45,6 +45,7 @@
app:title="MPV Player" app:title="MPV Player"
app:summary="Use the experimental MPV Player to play videos. MPV has support for more video, audio and subtitle codecs."/> app:summary="Use the experimental MPV Player to play videos. MPV has support for more video, audio and subtitle codecs."/>
<SwitchPreference <SwitchPreference
app:key="mpv_disable_hwdec"
app:dependency="mpv_player" app:dependency="mpv_player"
app:title="Force software decoding" app:title="Force software decoding"
app:summary="Disable hardware decoding and use software decoding. Can be useful if hardware decoding gives weird artifacts."/> app:summary="Disable hardware decoding and use software decoding. Can be useful if hardware decoding gives weird artifacts."/>