From 48c786a5b894b1715641994ae1003ae41feb1de1 Mon Sep 17 00:00:00 2001 From: LagradOst Date: Wed, 12 Jan 2022 18:32:03 +0100 Subject: [PATCH] minor fix --- .../cloudstream3/ui/player/CS3IPlayer.kt | 22 +++++++- .../ui/player/CustomSubtitleDecoderFactory.kt | 55 ++++++++++--------- .../cloudstream3/ui/player/GeneratorPlayer.kt | 4 +- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt index c37fc0bc..7d188c15 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt @@ -11,6 +11,7 @@ import com.google.android.exoplayer2.extractor.ExtractorsFactory import com.google.android.exoplayer2.source.DefaultMediaSourceFactory import com.google.android.exoplayer2.source.MergingMediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource +import com.google.android.exoplayer2.text.SubtitleDecoderFactory import com.google.android.exoplayer2.text.SubtitleExtractor import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.trackselection.TrackSelector @@ -598,11 +599,26 @@ class CS3IPlayer : IPlayer { .setSelectionFlags(C.SELECTION_FLAG_DEFAULT) .build() + val ownFactory = CustomSubtitleDecoderFactory() val extractorFactory = ExtractorsFactory { arrayOf( - SubtitleExtractor( - CustomSubtitleDecoderFactory().createDecoder(format), format - ) + if (ownFactory.supportsFormat(format)) { + SubtitleExtractor( + ownFactory.createDecoder(format), format + ) + } else { + if (SubtitleDecoderFactory.DEFAULT.supportsFormat(format)) { + SubtitleExtractor( + SubtitleDecoderFactory.DEFAULT.createDecoder(format), format + ) + } else { + // ye we guess if not found instead of using UnknownSubtitlesExtractor, + // this way you can hopefully load a .txt file that is an srt and it will work + SubtitleExtractor( + ownFactory.createDecoder(format), format + ) + } + } ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CustomSubtitleDecoderFactory.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CustomSubtitleDecoderFactory.kt index c038f9b7..0615a7fa 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CustomSubtitleDecoderFactory.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CustomSubtitleDecoderFactory.kt @@ -11,6 +11,7 @@ import com.google.android.exoplayer2.text.subrip.SubripDecoder import com.google.android.exoplayer2.text.ttml.TtmlDecoder import com.google.android.exoplayer2.text.webvtt.WebvttDecoder import com.google.android.exoplayer2.util.MimeTypes +import com.lagradost.cloudstream3.mvvm.logError class CustomDecoder : SubtitleDecoder { @@ -18,7 +19,7 @@ class CustomDecoder : SubtitleDecoder { private const val TAG = "CustomDecoder" } - var realDecoder: SubtitleDecoder? = null + private var realDecoder: SubtitleDecoder? = null override fun getName(): String { return realDecoder?.name ?: this::class.java.name @@ -31,36 +32,40 @@ class CustomDecoder : SubtitleDecoder { override fun queueInputBuffer(inputBuffer: SubtitleInputBuffer) { Log.i(TAG, "queueInputBuffer") + try { + if (realDecoder == null) { + inputBuffer.data?.let { data -> + // this way we read the subtitle file and decide what decoder to use instead of relying on mimetype - if (realDecoder == null) { - inputBuffer.data?.let { data -> + val pos = data.position() + data.position(0) + val arr = ByteArray(minOf(data.remaining(), 100)) + data.get(arr) + data.position(pos) - val pos = data.position() - data.position(0) - val arr = ByteArray(minOf(data.remaining(), 100)) - data.get(arr) - data.position(pos) + val str = arr.decodeToString().trimStart() + Log.i(TAG, "Got data from queueInputBuffer") + Log.i(TAG, "first string is $str") - val str = arr.decodeToString().trimStart() - Log.i(TAG, "Got data from queueInputBuffer") - Log.i(TAG, "first string is $str") + //https://github.com/LagradOst/CloudStream-2/blob/ddd774ee66810137ff7bd65dae70bcf3ba2d2489/CloudStreamForms/CloudStreamForms/Script/MainChrome.cs#L388 + realDecoder = when { + str.startsWith("WEBVTT") -> WebvttDecoder() + str.startsWith(" TtmlDecoder() + str.startsWith("[Script Info]") || str.startsWith("Title:") -> SsaDecoder() + str.startsWith("1") -> SubripDecoder() + else -> null + } - //https://github.com/LagradOst/CloudStream-2/blob/ddd774ee66810137ff7bd65dae70bcf3ba2d2489/CloudStreamForms/CloudStreamForms/Script/MainChrome.cs#L388 - realDecoder = when { - str.startsWith("WEBVTT") -> WebvttDecoder() - str.startsWith(" TtmlDecoder() - str.startsWith("[Script Info]") || str.startsWith("Title:") -> SsaDecoder() - str.startsWith("1") -> SubripDecoder() - else -> null - } - - realDecoder?.dequeueInputBuffer()?.let { buff -> - buff.data = data - realDecoder?.queueInputBuffer(buff) + realDecoder?.dequeueInputBuffer()?.let { buff -> + buff.data = data + realDecoder?.queueInputBuffer(buff) + } } + } else { + realDecoder?.dequeueInputBuffer() } - } else { - realDecoder?.dequeueInputBuffer() + } catch (e: Exception) { + logError(e) } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt index a272b431..ab1ed075 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt @@ -103,9 +103,9 @@ class GeneratorPlayer : FullScreenPlayer() { val (linkData, _) = it var quality = linkData?.quality ?: Qualities.Unknown.value - // we set all qualities above current max as max -1 + // we set all qualities above current max as reverse if (useQualitySettings && quality > currentPrefQuality) { - quality = currentPrefQuality - 1 + quality = currentPrefQuality - quality - 1 } // negative because we want to sort highest quality first -(quality)