From e2e440b68984c8984717c0c10c8bde9a128a6e37 Mon Sep 17 00:00:00 2001 From: saravr Date: Tue, 11 Jun 2024 14:55:33 -0700 Subject: [PATCH] Load control test --- .../playback/ArmadilloAnalyticsListener.kt | 61 +++++++++++++++++++ .../scribd/armadillo/playback/ExoplayerExt.kt | 20 +++++- .../armadillo/playback/PlayerEventListener.kt | 15 ++++- 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 Armadillo/src/main/java/com/scribd/armadillo/playback/ArmadilloAnalyticsListener.kt diff --git a/Armadillo/src/main/java/com/scribd/armadillo/playback/ArmadilloAnalyticsListener.kt b/Armadillo/src/main/java/com/scribd/armadillo/playback/ArmadilloAnalyticsListener.kt new file mode 100644 index 0000000..f1b0c8c --- /dev/null +++ b/Armadillo/src/main/java/com/scribd/armadillo/playback/ArmadilloAnalyticsListener.kt @@ -0,0 +1,61 @@ +package com.scribd.armadillo.playback + +import com.google.android.exoplayer2.analytics.AnalyticsListener +import com.google.android.exoplayer2.analytics.AnalyticsListener.EventTime +import com.google.android.exoplayer2.source.LoadEventInfo +import com.google.android.exoplayer2.source.MediaLoadData +import com.google.android.exoplayer2.util.Log +import java.io.IOException +import java.lang.Exception + +class ArmadilloAnalyticsListener : AnalyticsListener { + override fun onLoadStarted( + eventTime: EventTime, + loadEventInfo: LoadEventInfo, + mediaLoadData: MediaLoadData + ) { + // Called when a media load starts + Log.e(TAG, "Load started: ${loadEventInfo.describe()}") + } + + override fun onLoadCompleted( + eventTime: EventTime, + loadEventInfo: LoadEventInfo, + mediaLoadData: MediaLoadData + ) { + // Called when a media load completes + Log.e(TAG, "Load completed: ${loadEventInfo.describe()}") + } + + override fun onLoadCanceled( + eventTime: EventTime, + loadEventInfo: LoadEventInfo, + mediaLoadData: MediaLoadData + ) { + // Called when a media load is canceled + Log.e(TAG, "Load canceled: ${loadEventInfo.describe()}") + } + + override fun onLoadError( + eventTime: EventTime, + loadEventInfo: LoadEventInfo, + mediaLoadData: MediaLoadData, + error: IOException, + wasCanceled: Boolean + ) { + // Called when a media load encounters an error + Log.e(TAG, "Load error: ${loadEventInfo.describeWithError(error, wasCanceled)}") + } + + companion object { + private const val TAG = "ArmListener" + } +} + +fun LoadEventInfo.describe(): String { + return "${dataSpec.uri} pos: ${dataSpec.position}, len: ${dataSpec.length}" +} + +fun LoadEventInfo.describeWithError(exception: Exception, wasCanceled: Boolean): String { + return "ERR: ${exception.message}: wasCanceled: $wasCanceled, ${dataSpec.uri} pos: ${dataSpec.position}, len: ${dataSpec.length}" +} diff --git a/Armadillo/src/main/java/com/scribd/armadillo/playback/ExoplayerExt.kt b/Armadillo/src/main/java/com/scribd/armadillo/playback/ExoplayerExt.kt index 94f29bc..7ea00ad 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/playback/ExoplayerExt.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/playback/ExoplayerExt.kt @@ -1,7 +1,9 @@ package com.scribd.armadillo.playback import android.content.Context +import android.util.Log import com.google.android.exoplayer2.C +import com.google.android.exoplayer2.DefaultLoadControl import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.RenderersFactory import com.google.android.exoplayer2.audio.AudioAttributes @@ -41,12 +43,24 @@ internal fun ExoPlayer.playerDuration(): Milliseconds? = if (duration == C.TIME_ * * We provide our own renderers factory so that Proguard can remove any non-audio rendering code. */ -internal fun createExoplayerInstance(context: Context, attributes: AudioAttributes): ExoPlayer = - ExoPlayer.Builder(context, createRenderersFactory(context)) +internal fun createExoplayerInstance(context: Context, attributes: AudioAttributes): ExoPlayer { + val loadControl = DefaultLoadControl.Builder() + .setBufferDurationsMs( + 20000, + 120 * 60 * 1000, // 2hrs?? + 5000, + 5000, + ) + .build() + + Log.e("ExoplayerExt", "++++ USING LOAD CONTROL 2hrs") + return ExoPlayer.Builder(context, createRenderersFactory(context)) + .setLoadControl(loadControl) .build().apply { setAudioAttributes(attributes, true) + addAnalyticsListener(ArmadilloAnalyticsListener()) } - +} internal fun createRenderersFactory(context: Context): RenderersFactory = RenderersFactory { eventHandler, _, audioRendererEventListener, _, _ -> // Default audio sink taken from DefaultRenderersFactory. We need to provide it in order to enable offloading diff --git a/Armadillo/src/main/java/com/scribd/armadillo/playback/PlayerEventListener.kt b/Armadillo/src/main/java/com/scribd/armadillo/playback/PlayerEventListener.kt index 5271099..433831b 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/playback/PlayerEventListener.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/playback/PlayerEventListener.kt @@ -34,6 +34,11 @@ internal class PlayerEventListener : Player.Listener { @Inject internal lateinit var stateModifier: StateStore.Modifier + override fun onEvents(player: Player, events: Player.Events) { + super.onEvents(player, events) + Log.e(TAG, "ExoPlayer events: ${events.describe()}") + } + override fun onPlayerError(error: PlaybackException) { val exception = (error as ExoPlaybackException).toArmadilloException() stateModifier.dispatch(ErrorAction(exception)) @@ -80,4 +85,12 @@ internal class PlayerEventListener : Player.Listener { else -> "unknown" } } -} \ No newline at end of file +} + +fun Player.Events.describe(): String { + val flags = mutableListOf() + repeat(size()) { + flags.add(get(it)) + } + return flags.joinToString() +}