Add some main thread checks, change incorrect cache size

This commit is contained in:
Kelvin 2023-10-09 21:26:27 +02:00
parent 1768d73c01
commit 7ebd8f13c2
8 changed files with 32 additions and 4 deletions

View file

@ -17,6 +17,7 @@ import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import com.futo.platformplayer.api.http.ManagedHttpClient
import com.futo.platformplayer.api.media.models.video.IPlatformVideo
import com.futo.platformplayer.engine.V8Plugin
import com.futo.platformplayer.logging.Logger
import com.futo.platformplayer.models.PlatformVideoWithTime
import com.futo.platformplayer.others.PlatformLinkMovementMethod
@ -51,6 +52,11 @@ fun findNonRuntimeException(ex: Throwable?): Throwable? {
return ex;
}
fun warnIfMainThread(context: String) {
if(BuildConfig.DEBUG && Looper.myLooper() == Looper.getMainLooper())
Logger.w(V8Plugin.TAG, "JAVASCRIPT ON MAIN THREAD\nAt: ${context}\n" + Thread.currentThread().stackTrace);
}
fun ensureNotMainThread() {
if (Looper.myLooper() == Looper.getMainLooper()) {
Logger.e("Utility", "Throwing exception because a function that should not be called on main thread, is called on main thread")

View file

@ -1,11 +1,14 @@
package com.futo.platformplayer.api.media.platforms.js.models
import android.os.Looper
import com.caoccao.javet.values.reference.V8ValueArray
import com.caoccao.javet.values.reference.V8ValueObject
import com.futo.platformplayer.BuildConfig
import com.futo.platformplayer.api.media.platforms.js.SourcePluginConfig
import com.futo.platformplayer.api.media.structures.IPager
import com.futo.platformplayer.engine.V8Plugin
import com.futo.platformplayer.getOrThrow
import com.futo.platformplayer.warnIfMainThread
abstract class JSPager<T> : IPager<T> {
protected val plugin: V8Plugin;
@ -37,6 +40,8 @@ abstract class JSPager<T> : IPager<T> {
}
override fun nextPage() {
warnIfMainThread("JSPager.nextPage");
pager = plugin.catchScriptErrors("[${plugin.config.name}] JSPager", "pager.nextPage()") {
pager.invoke("nextPage", arrayOf<Any>());
};
@ -53,6 +58,8 @@ abstract class JSPager<T> : IPager<T> {
}
override fun getResults(): List<T> {
warnIfMainThread("JSPager.getResults");
val previousResults = _lastResults?.let {
if(!_resultChanged)
return@let it;

View file

@ -6,6 +6,7 @@ import com.futo.platformplayer.engine.IV8PluginConfig
import com.futo.platformplayer.engine.exceptions.ScriptImplementationException
import com.futo.platformplayer.getOrThrow
import com.futo.platformplayer.logging.Logger
import com.futo.platformplayer.warnIfMainThread
class JSPlaybackTracker: IPlaybackTracker {
private val _config: IV8PluginConfig;
@ -20,6 +21,7 @@ class JSPlaybackTracker: IPlaybackTracker {
private set;
constructor(config: IV8PluginConfig, obj: V8ValueObject) {
warnIfMainThread("JSPlaybackTracker.constructor");
if(!obj.has("onProgress"))
throw ScriptImplementationException(config, "Missing onProgress on PlaybackTracker");
if(!obj.has("nextRequest"))
@ -31,6 +33,7 @@ class JSPlaybackTracker: IPlaybackTracker {
}
override fun onInit(seconds: Double) {
warnIfMainThread("JSPlaybackTracker.onInit");
synchronized(_obj) {
if(_hasCalledInit)
return;
@ -44,6 +47,7 @@ class JSPlaybackTracker: IPlaybackTracker {
}
override fun onProgress(seconds: Double, isPlaying: Boolean) {
warnIfMainThread("JSPlaybackTracker.onProgress");
synchronized(_obj) {
if(!_hasCalledInit && _hasInit)
onInit(seconds);

View file

@ -1,5 +1,6 @@
package com.futo.platformplayer.constructs
import android.provider.Settings.Global
import com.futo.platformplayer.states.StateApp
import kotlinx.coroutines.*
@ -39,8 +40,7 @@ class BatchedTaskHandler<TParameter, TResult> {
//Cached
if(result != null)
//TODO: Replace with some kind of constant Deferred<IPlatformStreamVideo>
return _scope.async { result as TResult }
return CompletableDeferred(result as TResult);
//Already requesting
if(taskResult != null)
return taskResult as Deferred<TResult>;

View file

@ -1,6 +1,7 @@
package com.futo.platformplayer.engine
import android.content.Context
import android.os.Looper
import com.caoccao.javet.exceptions.JavetCompilationException
import com.caoccao.javet.exceptions.JavetExecutionException
import com.caoccao.javet.interop.V8Host
@ -17,6 +18,7 @@ import com.futo.platformplayer.engine.exceptions.*
import com.futo.platformplayer.engine.internal.V8Converter
import com.futo.platformplayer.engine.packages.*
import com.futo.platformplayer.logging.Logger
import com.futo.platformplayer.states.StateApp
import com.futo.platformplayer.states.StateAssets
import kotlinx.coroutines.*
@ -25,6 +27,7 @@ class V8Plugin {
private val _client: ManagedHttpClient;
private val _clientAuth: ManagedHttpClient;
val httpClient: ManagedHttpClient get() = _client;
val httpClientAuth: ManagedHttpClient get() = _clientAuth;
@ -137,6 +140,8 @@ class V8Plugin {
return executeTyped<V8Value>(js);
}
fun <T : V8Value> executeTyped(js: String) : T {
warnIfMainThread("V8Plugin.executeTyped");
val runtime = _runtime ?: throw IllegalStateException("JSPlugin not started yet");
return catchScriptErrors("Plugin[${config.name}]", js) { runtime.getExecutor(js).execute() };
}

View file

@ -927,6 +927,7 @@ class VideoDetailView : ConstraintLayout {
StateDownloads.instance.getCachedVideo(videoDetail.id) ?: videoDetail;
this.video = video;
this._playbackTracker = null;
if(video is JSVideoDetails) {
val me = this;
fragment.lifecycleScope.launch(Dispatchers.IO) {
@ -1001,6 +1002,7 @@ class VideoDetailView : ConstraintLayout {
_subTitle.text = subTitleSegments.joinToString("");
_rating.onLikeDislikeUpdated.remove(this);
if (ref != null) {
_rating.visibility = View.GONE;
@ -1085,12 +1087,14 @@ class VideoDetailView : ConstraintLayout {
_layoutRating.visibility = View.GONE;
}
//Overlay
updateQualitySourcesOverlay(video);
setLoading(false);
//Set Mediasource
val toResume = _videoResumePositionMilliseconds;
_videoResumePositionMilliseconds = 0;
loadCurrentVideo(toResume);
@ -1118,6 +1122,7 @@ class VideoDetailView : ConstraintLayout {
_textResume.text = "";
}
StatePlayer.instance.startOrUpdateMediaSession(context, video);
StatePlayer.instance.setCurrentlyPlaying(video);

View file

@ -45,7 +45,7 @@ import kotlin.streams.toList
*/
class StatePlatform {
private val TAG = "StatePlatform";
private val VIDEO_CACHE = 1024 * 1024 * 10;
private val VIDEO_CACHE = 100;
private val _scope = CoroutineScope(Dispatchers.IO);
@ -92,6 +92,7 @@ class StatePlatform {
return@BatchedTaskHandler null;
else {
val cached = synchronized(_cache) { _cache.get(it); } ?: return@BatchedTaskHandler null;
Logger.i(TAG, "Video Cache Hit [${cached.video.name}]");
if (cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) {
Logger.i(TAG, "Invalidated cache for [${it}]");
synchronized(_cache) {

@ -1 +1 @@
Subproject commit 123960682a286232963b5ed456598b1922dbe559
Subproject commit 2061a75ec1a9428b83f8cef5b84738c6f9cac290