Fixed PlaybackTracker slowness.

This commit is contained in:
Koen 2023-09-29 17:13:50 +02:00
parent 14ed45e833
commit 25cbdcb504
3 changed files with 45 additions and 3 deletions

View file

@ -5,6 +5,16 @@ import com.google.android.exoplayer2.util.Log
class Stopwatch {
var startTime = System.nanoTime()
val elapsedMs: Double get() {
val now = System.nanoTime()
val diff = now - startTime
return diff / 1000000.0
}
fun reset() {
startTime = System.nanoTime()
}
fun logAndNext(tag: String, message: String): Long {
val now = System.nanoTime()
val diff = now - startTime

View file

@ -94,6 +94,7 @@ import com.google.android.exoplayer2.Format
import com.google.android.exoplayer2.ui.PlayerControlView
import com.google.android.exoplayer2.ui.TimeBar
import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException
import com.google.common.base.Stopwatch
import com.google.protobuf.ByteString
import kotlinx.coroutines.*
import userpackage.Protocol
@ -930,12 +931,21 @@ class VideoDetailView : ConstraintLayout {
val me = this;
fragment.lifecycleScope.launch(Dispatchers.IO) {
try {
val tracker = video.getPlaybackTracker() ?: StatePlatform.instance.getPlaybackTracker(video.url);
val stopwatch = com.futo.platformplayer.debug.Stopwatch()
var tracker = video.getPlaybackTracker()
Logger.i(TAG, "video.getPlaybackTracker took ${stopwatch.elapsedMs}ms")
if (tracker == null) {
stopwatch.reset()
tracker = StatePlatform.instance.getPlaybackTracker(video.url);
Logger.i(TAG, "StatePlatform.instance.getPlaybackTracker took ${stopwatch.elapsedMs}ms")
}
if(me.video == video)
me._playbackTracker = tracker;
}
catch(ex: Throwable) {
fragment.lifecycleScope.launch(Dispatchers.Main) {
withContext(Dispatchers.Main) {
UIDialogs.showGeneralErrorDialog(context, "Failed to get Playback Tracker", ex);
};
}

View file

@ -62,6 +62,7 @@ class StatePlatform {
private val _enabledClients : ArrayList<IPlatformClient> = ArrayList();
private val _clientPools: HashMap<IPlatformClient, PlatformClientPool> = hashMapOf();
private val _trackerClientPools: HashMap<IPlatformClient, PlatformClientPool> = hashMapOf();
private val _primaryClientPersistent = FragmentedStorage.get<StringStorage>("primaryClient");
private var _primaryClientObj : IPlatformClient? = null;
@ -246,6 +247,22 @@ class StatePlatform {
};
return pool.getClient(capacity);
}
fun getTrackerClientPooled(parentClient: IPlatformClient, capacity: Int): IPlatformClient {
val pool = synchronized(_trackerClientPools) {
if(!_trackerClientPools.containsKey(parentClient))
_trackerClientPools[parentClient] = PlatformClientPool(parentClient).apply {
this.onDead.subscribe { client, pool ->
synchronized(_trackerClientPools) {
if(_trackerClientPools[parentClient] == pool)
_trackerClientPools.remove(parentClient);
}
}
}
_trackerClientPools[parentClient]!!;
};
return pool.getClient(capacity);
}
fun getClientsByClaimType(claimType: Int): List<IPlatformClient> {
return getEnabledClients().filter { it.isClaimTypeSupported(claimType) };
}
@ -605,7 +622,12 @@ class StatePlatform {
}
fun getPlaybackTracker(url: String): IPlaybackTracker? {
return getContentClientOrNull(url)?.getPlaybackTracker(url);
val baseClient = getContentClientOrNull(url) ?: return null;
if (baseClient !is JSClient) {
return baseClient.getPlaybackTracker(url);
}
val client = getTrackerClientPooled(baseClient, 1);
return client.getPlaybackTracker(url);
}
fun hasEnabledChannelClient(url : String) : Boolean = getEnabledClients().any { it.isChannelUrl(url) };