diff --git a/.github/nightly-notes/2026-11-16-merge-issues-1633-2167.md b/.github/nightly-notes/2026-11-16-merge-issues-1633-2167.md new file mode 100644 index 0000000000..5a04617055 --- /dev/null +++ b/.github/nightly-notes/2026-11-16-merge-issues-1633-2167.md @@ -0,0 +1,6 @@ +## New +- DeArrow thumbnails only: never show the uploader's thumbnail (#1633) + +## Fixed +- With DeArrow, new videos show a frame from the video instead of the original thumbnail (#1633) +- Seeking in a song no longer pauses to load it again (#2167) diff --git a/app/src/main/java/io/github/aedev/flow/data/local/PlayerPreferences.kt b/app/src/main/java/io/github/aedev/flow/data/local/PlayerPreferences.kt index 5ed91ad3e7..c66794e945 100644 --- a/app/src/main/java/io/github/aedev/flow/data/local/PlayerPreferences.kt +++ b/app/src/main/java/io/github/aedev/flow/data/local/PlayerPreferences.kt @@ -273,6 +273,7 @@ class PlayerPreferences( val DEARROW_ENABLED = booleanPreferencesKey("dearrow_enabled") val DEARROW_BADGE_ENABLED = booleanPreferencesKey("dearrow_badge_enabled") val DEARROW_RANDOM_FRAME = booleanPreferencesKey("dearrow_random_frame") + val DEARROW_THUMBNAILS_ONLY = booleanPreferencesKey("dearrow_thumbnails_only") // AI channel list val AI_LIST_BLOCK_ENABLED = booleanPreferencesKey("ai_list_block_enabled") @@ -2502,6 +2503,17 @@ class PlayerPreferences( } } + /** Never load the uploader's thumbnail: DeArrow's, or else a frame YouTube takes from the video (#1633). */ + val deArrowThumbnailsOnly: Flow = + context.playerPreferencesDataStore.data + .map { preferences -> preferences[Keys.DEARROW_THUMBNAILS_ONLY] ?: false } + + suspend fun setDeArrowThumbnailsOnly(enabled: Boolean) { + context.playerPreferencesDataStore.edit { preferences -> + preferences[Keys.DEARROW_THUMBNAILS_ONLY] = enabled + } + } + // AI channel list val aiListBlockEnabled: Flow = context.playerPreferencesDataStore.data diff --git a/app/src/main/java/io/github/aedev/flow/data/model/DeArrowContent.kt b/app/src/main/java/io/github/aedev/flow/data/model/DeArrowContent.kt index 7a423d898f..95b68c8bba 100644 --- a/app/src/main/java/io/github/aedev/flow/data/model/DeArrowContent.kt +++ b/app/src/main/java/io/github/aedev/flow/data/model/DeArrowContent.kt @@ -29,8 +29,11 @@ data class DeArrowContent( /** * The resolved deArrow overrides for a video. Non-null fields should replace the originals. + * [youTubeFrames] means DeArrow wants a frame here: until its own loads (or when it has no time to + * place one), YouTube's automatic frames stand in rather than the uploader's thumbnail (#1633). */ data class DeArrowResult( val title: String? = null, val thumbnailUrl: String? = null, + val youTubeFrames: Boolean = false, ) diff --git a/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicy.kt b/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicy.kt index 7b0d7c2ecb..4f78dfc8e4 100644 --- a/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicy.kt +++ b/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicy.kt @@ -8,7 +8,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl * Turns a branding answer into what a card shows, the way the DeArrow extension does: the server's * first usable submission wins, a winning "original" keeps YouTube's version, and a video with no * thumbnail submission shows the frame at the server's random time for it, so every DeArrow user - * sees the same one. + * sees the same one. When that frame cannot be shown yet, YouTube's own frames stand in. */ internal object DeArrowBrandingPolicy { const val THUMBNAIL_URL = "https://dearrow-thumb.ajay.app/api/v1/getThumbnail" @@ -29,15 +29,19 @@ internal object DeArrowBrandingPolicy { ?.takeIf { it.isNotBlank() } val thumbnail = content.thumbnails.firstOrNull { it.locked || it.votes >= 0 } + val wantsRandomFrame = thumbnail == null && useRandomFrame && !isLiveOrUpcoming val frameTime = when { thumbnail != null -> thumbnail.takeUnless { it.original }?.timestamp - useRandomFrame && !isLiveOrUpcoming -> randomFrameTime(content, fallbackDurationSeconds) + wantsRandomFrame -> randomFrameTime(content, fallbackDurationSeconds) else -> null } val thumbnailUrl = frameTime?.let { frameUrl(videoId, it) } + // A new upload has no length on DeArrow's side and often none on the card (RSS), so the + // random frame can't be placed; it still wants a frame rather than the uploader's thumbnail. + val youTubeFrames = thumbnailUrl != null || wantsRandomFrame - return if (title == null && thumbnailUrl == null) null else DeArrowResult(title, thumbnailUrl) + return if (title == null && !youTubeFrames) null else DeArrowResult(title, thumbnailUrl, youTubeFrames) } private fun randomFrameTime( diff --git a/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowRepository.kt b/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowRepository.kt index ce057c1680..d5427ead26 100644 --- a/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowRepository.kt +++ b/app/src/main/java/io/github/aedev/flow/data/repository/DeArrowRepository.kt @@ -51,6 +51,9 @@ class DeArrowRepository internal constructor( /** What a request already answered, for a card's first frame. */ fun cached(videoId: String): DeArrowContent? = fresh(videoId)?.content + /** Whether a request already answered for [videoId], even when the answer was nothing. */ + fun isCached(videoId: String): Boolean = fresh(videoId) != null + suspend fun branding(videoId: String): DeArrowContent? { fresh(videoId)?.let { return it.content } val mine = CompletableDeferred() diff --git a/app/src/main/java/io/github/aedev/flow/player/config/PlayerConfig.kt b/app/src/main/java/io/github/aedev/flow/player/config/PlayerConfig.kt index 7a3eb81d32..53c0848b92 100644 --- a/app/src/main/java/io/github/aedev/flow/player/config/PlayerConfig.kt +++ b/app/src/main/java/io/github/aedev/flow/player/config/PlayerConfig.kt @@ -68,11 +68,19 @@ object PlayerConfig { const val SHORTS_BUFFER_FOR_REBUFFER_MS = 750 const val SHORTS_BACK_BUFFER_MS = 2_000 - /** Music buffer window. Audio-only, so a long window is cheap and a low start threshold is safe. */ + /** + * Music buffer window. [MUSIC_MAX_BUFFER_MS] is the lean window (crossfade tails, low-RAM devices + * and Data Saver); otherwise a song loads whole and what has played stays, so a seek is in memory (#2167). + */ const val MUSIC_MIN_BUFFER_MS = 2_500 const val MUSIC_MAX_BUFFER_MS = 30_000 const val MUSIC_BUFFER_FOR_PLAYBACK_MS = 1_000 const val MUSIC_BUFFER_FOR_REBUFFER_MS = 1_500 + const val MUSIC_WHOLE_SONG_BUFFER_MS = 10 * 60 * 1000 + const val MUSIC_BACK_BUFFER_MS = 10 * 60 * 1000 + + /** Bounds a long mix and the music player's video mode, which would otherwise size the cap for video. */ + const val MUSIC_TARGET_BUFFER_BYTES = 24 * 1024 * 1024 /** Preferred delay from the true live edge. Keeps YouTube live playback stable. */ const val LIVE_EDGE_GAP_MS = 10_000L diff --git a/app/src/main/java/io/github/aedev/flow/player/factory/LoadControlFactory.kt b/app/src/main/java/io/github/aedev/flow/player/factory/LoadControlFactory.kt index 7c68393d81..69bfdbc536 100644 --- a/app/src/main/java/io/github/aedev/flow/player/factory/LoadControlFactory.kt +++ b/app/src/main/java/io/github/aedev/flow/player/factory/LoadControlFactory.kt @@ -128,19 +128,19 @@ object LoadControlFactory { ) /** - * Music service: audio-only, so a long window costs little memory and a low playback threshold - * gets the first note out quickly. No back buffer, and no byte cap — the duration window is the - * only budget it needs. + * Music: a low playback threshold gets the first note out quickly. With [wholeSong] the song loads + * to its end and what has played is kept, both inside one byte cap, so a seek either way stays in + * memory (#2167). Without it, the lean 30 s window with no back buffer. */ - fun forMusic(): DefaultLoadControl = + fun forMusic(wholeSong: Boolean = false): DefaultLoadControl = build( minMs = PlayerConfig.MUSIC_MIN_BUFFER_MS, - maxMs = PlayerConfig.MUSIC_MAX_BUFFER_MS, + maxMs = if (wholeSong) PlayerConfig.MUSIC_WHOLE_SONG_BUFFER_MS else PlayerConfig.MUSIC_MAX_BUFFER_MS, playbackMs = PlayerConfig.MUSIC_BUFFER_FOR_PLAYBACK_MS, rebufferMs = PlayerConfig.MUSIC_BUFFER_FOR_REBUFFER_MS, - backBufferMs = 0, - retainBackBufferFromKeyframe = false, - targetBufferBytes = C.LENGTH_UNSET, + backBufferMs = if (wholeSong) PlayerConfig.MUSIC_BACK_BUFFER_MS else 0, + retainBackBufferFromKeyframe = wholeSong, + targetBufferBytes = if (wholeSong) PlayerConfig.MUSIC_TARGET_BUFFER_BYTES else C.LENGTH_UNSET, ) /** A skipped song's tail plays for moments, so it starts on little audio and loads no more than it will play. */ diff --git a/app/src/main/java/io/github/aedev/flow/service/Media3MusicService.kt b/app/src/main/java/io/github/aedev/flow/service/Media3MusicService.kt index 18dd32d6e5..4e789e1f84 100644 --- a/app/src/main/java/io/github/aedev/flow/service/Media3MusicService.kt +++ b/app/src/main/java/io/github/aedev/flow/service/Media3MusicService.kt @@ -569,7 +569,8 @@ class Media3MusicService : MediaLibraryService() { val renderersFactory = musicRenderers(equalizer) - val loadControl = LoadControlFactory.forMusic() + val isLowRamDevice = (getSystemService(ACTIVITY_SERVICE) as? ActivityManager)?.isLowRamDevice == true + val loadControl = MusicLoadControl(LoadControlFactory.forMusic(wholeSong = !isLowRamDevice), NetworkState.isDataSaverOn(this)) player = ExoPlayer @@ -600,7 +601,7 @@ class Media3MusicService : MediaLibraryService() { // order, the player's shuffle flag reorders nothing on its own, whichever controller sets it. player.setShuffleOrder(ShuffleOrder.UnshuffledShuffleOrder(0)) musicResume.attach(player) - MusicPreload(this, player).attach(lifecycleScope, prefs.bufferProfile) + MusicPreload(this, player) { loadControl.savesData = it }.attach(lifecycleScope, prefs.bufferProfile) crossfader = MusicCrossfader(player, ::buildCrossfadeTail, ::crossfadeMoment, ::applyLoudnessGain).also { it.attach() } lifecycleScope.launch { combine( diff --git a/app/src/main/java/io/github/aedev/flow/service/MusicLoadControl.kt b/app/src/main/java/io/github/aedev/flow/service/MusicLoadControl.kt new file mode 100644 index 0000000000..f013c2f891 --- /dev/null +++ b/app/src/main/java/io/github/aedev/flow/service/MusicLoadControl.kt @@ -0,0 +1,81 @@ +package io.github.aedev.flow.service + +import androidx.annotation.OptIn +import androidx.media3.common.Timeline +import androidx.media3.common.util.UnstableApi +import androidx.media3.exoplayer.DefaultLoadControl +import androidx.media3.exoplayer.LoadControl +import androidx.media3.exoplayer.analytics.PlayerId +import androidx.media3.exoplayer.source.MediaSource +import androidx.media3.exoplayer.source.TrackGroupArray +import androidx.media3.exoplayer.trackselection.ExoTrackSelection +import androidx.media3.exoplayer.upstream.Allocator +import io.github.aedev.flow.player.config.PlayerConfig + +/** + * The music player's load control: [base] loads the whole song, and while [savesData] is on it stops at + * the lean 30 s window instead. Media3 fixes a player's load control when the player is built, and + * 1.11 ships no forwarding one, so the Data Saver check wraps the stock control rather than rebuilding + * the player. The back buffer stays either way: it costs memory, not data. + * + * Every override is spelled out: Kotlin's `by` delegation skips Java default methods, and the + * interface's defaults throw. These are the ten overloads `ExoPlayerImplInternal` 1.11 calls. + */ +@OptIn(UnstableApi::class) +internal class MusicLoadControl( + private val base: DefaultLoadControl, + initiallySavesData: Boolean, +) : LoadControl { + @Volatile + var savesData: Boolean = initiallySavesData + + private var capped = false + + override fun shouldContinueLoading(parameters: LoadControl.Parameters): Boolean { + val baseWants = base.shouldContinueLoading(parameters) + if (!savesData) { + capped = false + return baseWants + } + // Same hysteresis as the stock window: fill to the cap, then wait until below the minimum, so + // the radio wakes in bursts rather than for every few kilobytes. + if (parameters.bufferedDurationUs >= LEAN_MAX_US) { + capped = true + } else if (parameters.bufferedDurationUs < LEAN_MIN_US) { + capped = false + } + return baseWants && !capped + } + + override fun onPrepared(playerId: PlayerId) = base.onPrepared(playerId) + + override fun onTracksSelected( + parameters: LoadControl.Parameters, + trackGroups: TrackGroupArray, + trackSelections: Array, + ) = base.onTracksSelected(parameters, trackGroups, trackSelections) + + override fun onStopped(playerId: PlayerId) = base.onStopped(playerId) + + override fun onReleased(playerId: PlayerId) = base.onReleased(playerId) + + override fun getAllocator(playerId: PlayerId): Allocator = base.getAllocator(playerId) + + override fun getBackBufferDurationUs(playerId: PlayerId): Long = base.getBackBufferDurationUs(playerId) + + override fun retainBackBufferFromKeyframe(playerId: PlayerId): Boolean = base.retainBackBufferFromKeyframe(playerId) + + override fun shouldStartPlayback(parameters: LoadControl.Parameters): Boolean = base.shouldStartPlayback(parameters) + + override fun shouldContinuePreloading( + playerId: PlayerId, + timeline: Timeline, + mediaPeriodId: MediaSource.MediaPeriodId, + bufferedDurationUs: Long, + ): Boolean = base.shouldContinuePreloading(playerId, timeline, mediaPeriodId, bufferedDurationUs) + + private companion object { + const val LEAN_MAX_US = PlayerConfig.MUSIC_MAX_BUFFER_MS * 1_000L + const val LEAN_MIN_US = PlayerConfig.MUSIC_MIN_BUFFER_MS * 1_000L + } +} diff --git a/app/src/main/java/io/github/aedev/flow/service/MusicPreload.kt b/app/src/main/java/io/github/aedev/flow/service/MusicPreload.kt index 4ab73b214e..f6ba1cfb76 100644 --- a/app/src/main/java/io/github/aedev/flow/service/MusicPreload.kt +++ b/app/src/main/java/io/github/aedev/flow/service/MusicPreload.kt @@ -15,12 +15,14 @@ import kotlinx.coroutines.launch /** * Loads the start of the next song while the current one plays, so a skip has audio to start from * (#2076). Media3 does it only while the current song isn't loading, through the same sources and - * cache. Off while Android's Data Saver or the Data saver buffer profile is on, checked on each new song. + * cache. Off while Android's Data Saver or the Data saver buffer profile is on, checked on each new song; + * [onSavingData] hears the same answer, for the load control's own window. */ @OptIn(UnstableApi::class) internal class MusicPreload( private val context: Context, private val player: ExoPlayer, + private val onSavingData: (Boolean) -> Unit = {}, ) { private var profileSavesData = false @@ -46,8 +48,10 @@ internal class MusicPreload( } private fun refresh() { + val savesData = profileSavesData || NetworkState.isDataSaverOn(context) + onSavingData(savesData) val wanted = - if (profileSavesData || NetworkState.isDataSaverOn(context)) { + if (savesData) { ExoPlayer.PreloadConfiguration.DEFAULT } else { ExoPlayer.PreloadConfiguration(NEXT_SONG_US) diff --git a/app/src/main/java/io/github/aedev/flow/sync/mapping/SettingsMapper.kt b/app/src/main/java/io/github/aedev/flow/sync/mapping/SettingsMapper.kt index e72765f9e0..6fa373bfb3 100644 --- a/app/src/main/java/io/github/aedev/flow/sync/mapping/SettingsMapper.kt +++ b/app/src/main/java/io/github/aedev/flow/sync/mapping/SettingsMapper.kt @@ -55,6 +55,7 @@ object SettingsMapper { Entry("sponsorblock_action_poi_highlight", "sb_action_poi_highlight", Ty.STRING), Entry("dearrow_enabled", "dearrow_enabled", Ty.BOOL), Entry("dearrow_badge_enabled", "dearrow_badge_enabled", Ty.BOOL), + Entry("dearrow_random_frame", "dearrow_random_frame", Ty.BOOL), Entry("subtitles_enabled", "subtitles_enabled", Ty.BOOL), // Another device must not switch this phone's UI language (#1277). Entry("app_language", "app_language", Ty.STRING, applied = false), diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/DeArrowState.kt b/app/src/main/java/io/github/aedev/flow/ui/components/DeArrowState.kt index 0da7a6ebde..bdddb692a1 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/DeArrowState.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/DeArrowState.kt @@ -1,6 +1,7 @@ package io.github.aedev.flow.ui.components import androidx.compose.runtime.Composable +import androidx.compose.runtime.Immutable import androidx.compose.runtime.key import androidx.compose.runtime.produceState import androidx.compose.runtime.staticCompositionLocalOf @@ -14,6 +15,16 @@ import io.github.aedev.flow.data.repository.DeArrowRepository /** The activity's DeArrow lookup, installed by `ProvideVideoCardState`; null in previews and tests. */ val LocalDeArrow = staticCompositionLocalOf { null } +/** + * DeArrow's [result] for a video, and whether the lookup has [answered]: true once the server replied + * or failed, and at once when there is nothing to ask. + */ +@Immutable +internal data class DeArrowLookup( + val result: DeArrowResult?, + val answered: Boolean, +) + /** * What DeArrow replaces on [video], or null to keep YouTube's: switched off, a local file, no lookup * installed, or nothing to replace. An answer already fetched shows on the first frame. @@ -23,7 +34,15 @@ internal fun rememberDeArrowResult( video: Video, enabled: Boolean, useRandomFrame: Boolean, -): DeArrowResult? { +): DeArrowResult? = rememberDeArrowLookup(video, enabled, useRandomFrame).result + +/** [rememberDeArrowResult], also saying whether the answer is in yet. */ +@Composable +internal fun rememberDeArrowLookup( + video: Video, + enabled: Boolean, + useRandomFrame: Boolean, +): DeArrowLookup { val repository = LocalDeArrow.current val active = enabled && repository != null && !LocalMediaIds.isLocal(video.id) val resolve = { content: DeArrowContent? -> @@ -38,8 +57,14 @@ internal fun rememberDeArrowResult( } } return key(video.id, active, useRandomFrame) { - produceState(initialValue = if (active) resolve(repository?.cached(video.id)) else null, video.id, active, useRandomFrame) { - if (active) value = resolve(repository?.branding(video.id)) + val initial = + when { + !active || repository == null -> DeArrowLookup(null, answered = true) + repository.isCached(video.id) -> DeArrowLookup(resolve(repository.cached(video.id)), answered = true) + else -> DeArrowLookup(null, answered = false) + } + produceState(initialValue = initial, video.id, active, useRandomFrame) { + if (active && !initial.answered) value = DeArrowLookup(resolve(repository?.branding(video.id)), answered = true) }.value } } diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/MediaThumbnail.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/MediaThumbnail.kt index f4938fd525..2567b2550b 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/shared/MediaThumbnail.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/MediaThumbnail.kt @@ -44,6 +44,8 @@ fun MediaThumbnail( showWatchProgress: Boolean = false, placeholder: ImageVector? = null, onModelLoaded: ((Any) -> Unit)? = null, + youTubeFrames: Boolean = LocalYouTubeFramesOnly.current, + showImage: Boolean = true, overlay: @Composable BoxScope.() -> Unit = {}, ) { val openOrigin = rememberMediaOpenOrigin(videoId, shape) @@ -70,15 +72,18 @@ fun MediaThumbnail( ) } - VideoThumbnailImage( - videoId = videoId, - model = thumbnailUrl, - contentDescription = null, - modifier = Modifier.fillMaxSize(), - contentScale = ContentScale.Crop, - onImageKey = reportImageKey, - onModelLoaded = onModelLoaded, - ) + if (showImage) { + VideoThumbnailImage( + videoId = videoId, + model = thumbnailUrl, + contentDescription = null, + modifier = Modifier.fillMaxSize(), + contentScale = ContentScale.Crop, + onImageKey = reportImageKey, + onModelLoaded = onModelLoaded, + youTubeFrames = youTubeFrames, + ) + } if (durationSeconds != null && durationSeconds > 0) { DurationBadge( diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/VideoThumbnailImage.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/VideoThumbnailImage.kt index 1de6ee8073..48ba6a8df9 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/shared/VideoThumbnailImage.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/VideoThumbnailImage.kt @@ -31,7 +31,14 @@ import io.github.aedev.flow.utils.ThumbnailUrlResolver */ val LocalThumbnailQuality = staticCompositionLocalOf { ThumbnailQuality.HIGH } -/** [portrait] marks a card that crops the frame to 9:16, which keeps it from the smallest tier. */ +/** DeArrow thumbnails only (#1633): every YouTube video image not decided by its caller shows YouTube's own frame. */ +val LocalYouTubeFramesOnly = staticCompositionLocalOf { false } + +/** + * [portrait] marks a card that crops the frame to 9:16, which keeps it from the smallest tier. + * [youTubeFrames] puts YouTube's automatic frames where the uploader's thumbnail tiers would go; a + * portrait (Shorts) image keeps its own, since a 16:9 frame of a vertical clip is mostly bars. + */ @Composable fun VideoThumbnailImage( videoId: String, @@ -43,17 +50,18 @@ fun VideoThumbnailImage( placeholderKey: MemoryCache.Key? = null, onImageKey: ((MemoryCache.Key?) -> Unit)? = null, onModelLoaded: ((Any) -> Unit)? = null, + youTubeFrames: Boolean = LocalYouTubeFramesOnly.current && !portrait, ) { val quality = LocalThumbnailQuality.current val models = - remember(videoId, model, quality, portrait) { + remember(videoId, model, quality, portrait, youTubeFrames) { when { quality == null -> { emptyList() } model is String || model == null -> { - ThumbnailUrlResolver.resolveVideoThumbnailCandidates(videoId, model as? String, quality, portrait) + ThumbnailUrlResolver.resolveVideoThumbnailCandidates(videoId, model as? String, quality, portrait, youTubeFrames) } else -> { diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/CardThumbnailPolicy.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/CardThumbnailPolicy.kt new file mode 100644 index 0000000000..2d7c00206e --- /dev/null +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/CardThumbnailPolicy.kt @@ -0,0 +1,34 @@ +package io.github.aedev.flow.ui.components.shared.card + +import io.github.aedev.flow.data.localmedia.LocalMediaIds +import io.github.aedev.flow.data.model.Video +import io.github.aedev.flow.ui.components.DeArrowLookup + +/** What a card's thumbnail loads: [url] null holds the empty container, [youTubeFrames] as in [VideoCardState]. */ +internal data class CardThumbnail( + val url: String?, + val youTubeFrames: Boolean, +) + +/** + * The card's image under DeArrow. With [thumbnailsOnly] the uploader's thumbnail is never requested + * (#1633): the card waits for DeArrow's answer, then shows DeArrow's frame or YouTube's own. Shorts, + * live and upcoming videos and device files keep their own image, since YouTube has no frame for them. + */ +internal object CardThumbnailPolicy { + fun resolve( + video: Video, + lookup: DeArrowLookup, + thumbnailsOnly: Boolean, + ): CardThumbnail { + val framesOnly = thumbnailsOnly && keepsNoOwnImage(video) + if (framesOnly && !lookup.answered) return CardThumbnail(url = null, youTubeFrames = true) + return CardThumbnail( + url = lookup.result?.thumbnailUrl ?: video.thumbnailUrl, + youTubeFrames = framesOnly || lookup.result?.youTubeFrames == true, + ) + } + + private fun keepsNoOwnImage(video: Video): Boolean = + !video.isShort && !video.isLive && !video.isUpcoming && !LocalMediaIds.isLocal(video.id) +} diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardSheets.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardSheets.kt index 47cb4070fe..907f65ff93 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardSheets.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardSheets.kt @@ -15,7 +15,8 @@ internal fun VideoCardSheets( VideoQuickActionsBottomSheet( video = state.video, title = state.title, - thumbnailUrl = state.thumbnailUrl, + thumbnailUrl = state.thumbnailUrl ?: state.video.thumbnailUrl, + youTubeFrames = state.youTubeFrames, showChannel = showChannel, whyShown = whyShown, onDismiss = { sheets.showQuickActions = false }, diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardState.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardState.kt index f96bd88b02..7a4c2b0df0 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardState.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardState.kt @@ -27,9 +27,10 @@ import io.github.aedev.flow.data.repository.DeArrowRepository import io.github.aedev.flow.data.repository.OriginalTitleRepository import io.github.aedev.flow.ui.components.LocalDeArrow import io.github.aedev.flow.ui.components.layout.navigation.MediaNavigator -import io.github.aedev.flow.ui.components.rememberDeArrowResult +import io.github.aedev.flow.ui.components.rememberDeArrowLookup import io.github.aedev.flow.ui.components.shared.LocalArtworkColors import io.github.aedev.flow.ui.components.shared.LocalThumbnailQuality +import io.github.aedev.flow.ui.components.shared.LocalYouTubeFramesOnly import io.github.aedev.flow.ui.components.shared.quickactions.QuickActionsViewModel import io.github.aedev.flow.ui.components.shared.quickactions.sharedQuickActionsViewModel import io.github.aedev.flow.ui.utils.rememberIsOnWifi @@ -54,6 +55,7 @@ data class VideoCardPreferences( val showWatchProgress: Boolean = true, val originalTitlesEnabled: Boolean = false, val deArrowRandomFrame: Boolean = true, + val deArrowThumbnailsOnly: Boolean = false, ) /** @@ -160,6 +162,8 @@ fun ProvideVideoCardState( cardPreferences.copy(originalTitlesEnabled = originalTitles) }.combine(playerPreferences.deArrowRandomFrame) { cardPreferences, randomFrame -> cardPreferences.copy(deArrowRandomFrame = randomFrame) + }.combine(playerPreferences.deArrowThumbnailsOnly) { cardPreferences, thumbnailsOnly -> + cardPreferences.copy(deArrowThumbnailsOnly = thumbnailsOnly) }.distinctUntilChanged() } val preferences by preferencesFlow.collectAsStateWithLifecycle(VideoCardPreferences()) @@ -193,6 +197,7 @@ fun ProvideVideoCardState( LocalVideoWatchProgress provides progressStore, LocalVideoCardActions provides actions, LocalThumbnailQuality provides thumbnailQuality, + LocalYouTubeFramesOnly provides (preferences.deArrowEnabled && preferences.deArrowThumbnailsOnly), LocalArtworkColors provides artworkColors, LocalOriginalTitles provides originalTitles, LocalDeArrow provides deArrow, @@ -233,9 +238,10 @@ internal class VideoCardSheetState { internal class VideoCardState( val video: Video, val title: String, - val thumbnailUrl: String, + val thumbnailUrl: String?, val deArrowTitle: Boolean, val deArrowFrameUrl: String?, + val youTubeFrames: Boolean, val deArrowBadgeEnabled: Boolean, val channelName: String, val collaborators: List, @@ -260,7 +266,10 @@ internal class VideoCardState( @Composable internal fun rememberVideoCardState(video: Video): VideoCardState { val preferences = LocalVideoCardPreferences.current - val deArrow = rememberDeArrowResult(video, preferences.deArrowEnabled, preferences.deArrowRandomFrame) + val thumbnailsOnly = preferences.deArrowEnabled && preferences.deArrowThumbnailsOnly + val lookup = rememberDeArrowLookup(video, preferences.deArrowEnabled, preferences.deArrowRandomFrame || thumbnailsOnly) + val deArrow = lookup.result + val thumbnail = CardThumbnailPolicy.resolve(video, lookup, thumbnailsOnly) val originalTitle = rememberOriginalTitle(video.id, preferences.originalTitlesEnabled && deArrow?.title == null) val collaborators = rememberCollaboratorItems(video) val channelName = rememberCollaboratorChannelDisplayName(video.channelName, collaborators) @@ -269,9 +278,10 @@ internal fun rememberVideoCardState(video: Video): VideoCardState { return VideoCardState( video = video, title = deArrow?.title ?: originalTitle ?: video.title, - thumbnailUrl = deArrow?.thumbnailUrl ?: video.thumbnailUrl, + thumbnailUrl = thumbnail.url, deArrowTitle = deArrow?.title != null, deArrowFrameUrl = deArrow?.thumbnailUrl, + youTubeFrames = thumbnail.youTubeFrames, deArrowBadgeEnabled = preferences.deArrowBadgeEnabled, channelName = channelName, collaborators = collaborators, diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardThumbnail.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardThumbnail.kt index 03dc3b8b02..8525118897 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardThumbnail.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/card/VideoCardThumbnail.kt @@ -35,7 +35,8 @@ internal fun VideoCardThumbnail( width: Dp = Dp.Unspecified, ) { val video = state.video - // DeArrow answers 204 for a frame it has not rendered yet, and the image falls back to YouTube's. + // DeArrow answers 204 for a frame it has not rendered yet, and the image falls back to YouTube's + // own frame, never the uploader's thumbnail. var deArrowFrameShown by remember(state.deArrowFrameUrl) { mutableStateOf(false) } MediaThumbnail( videoId = video.id, @@ -44,6 +45,8 @@ internal fun VideoCardThumbnail( shape = shape, showWatchProgress = LocalVideoCardPreferences.current.showWatchProgress, onModelLoaded = state.deArrowFrameUrl?.let { frame -> { loaded -> deArrowFrameShown = loaded == frame } }, + youTubeFrames = state.youTubeFrames, + showImage = state.thumbnailUrl != null, modifier = modifier, ) { val badgeModifier = Modifier.padding(MediaThumbnailDefaults.BadgePadding) diff --git a/app/src/main/java/io/github/aedev/flow/ui/components/shared/quickactions/VideoQuickActionsSheet.kt b/app/src/main/java/io/github/aedev/flow/ui/components/shared/quickactions/VideoQuickActionsSheet.kt index 270b4be650..cea413b2a4 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/components/shared/quickactions/VideoQuickActionsSheet.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/components/shared/quickactions/VideoQuickActionsSheet.kt @@ -50,6 +50,7 @@ import io.github.aedev.flow.ui.components.shared.ChannelOrganizeTarget import io.github.aedev.flow.ui.components.shared.FlowNavRow import io.github.aedev.flow.ui.components.shared.FlowSubscribeButton import io.github.aedev.flow.ui.components.shared.FlowSubscribeButtonSize +import io.github.aedev.flow.ui.components.shared.LocalYouTubeFramesOnly import io.github.aedev.flow.ui.components.shared.MediaThumbnail import io.github.aedev.flow.ui.components.shared.SaveVideoSheet import io.github.aedev.flow.ui.components.shared.card.LocalVideoCardPreferences @@ -69,7 +70,7 @@ import kotlinx.coroutines.launch private enum class VideoMenuPage { Actions, Details, Collaborators, Save, ReportAi } /** - * A video's menu. [title] and [thumbnailUrl] are what the card shows, so a DeArrow title stays the + * A video's menu. [title], [thumbnailUrl] and [youTubeFrames] are what the card shows, so a DeArrow title stays the * same in the menu. [showChannel] is false where the screen already is the channel. A playlist that * can drop the video passes [onRemoveFromCollection] with its [removeFromCollectionLabel] and icon; * it is the last row of the Options group. [whyShown] says why a feed shows @@ -82,6 +83,7 @@ fun VideoQuickActionsBottomSheet( onDismiss: () -> Unit, title: String = rememberCardTitle(video), thumbnailUrl: String = video.thumbnailUrl, + youTubeFrames: Boolean = LocalYouTubeFramesOnly.current && !video.isShort, showChannel: Boolean = true, whyShown: String? = null, onRemoveFromCollection: (() -> Unit)? = null, @@ -133,6 +135,7 @@ fun VideoQuickActionsBottomSheet( width = QuickActionsDefaults.VideoArtworkWidth, shape = MaterialTheme.shapes.medium, showWatchProgress = LocalVideoCardPreferences.current.showWatchProgress, + youTubeFrames = youTubeFrames, ) } VideoPrimaryActions(video, viewModel, onSave = { sheet.hideThen { page = VideoMenuPage.Save } }, onDismiss = close) diff --git a/app/src/main/java/io/github/aedev/flow/ui/screens/settings/index/IntegrationsIndex.kt b/app/src/main/java/io/github/aedev/flow/ui/screens/settings/index/IntegrationsIndex.kt index 8cd9fddc1a..25cff0be4d 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/screens/settings/index/IntegrationsIndex.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/screens/settings/index/IntegrationsIndex.kt @@ -74,6 +74,14 @@ internal object IntegrationsIndex { section = R.string.player_settings_dearrow, destination = page, ) + val deArrowThumbnailsOnly = + SettingEntry( + key = "integrations.dearrow.thumbnails_only", + title = R.string.dearrow_thumbnails_only, + summary = R.string.dearrow_thumbnails_only_subtitle, + section = R.string.player_settings_dearrow, + destination = page, + ) val deArrowBadge = SettingEntry( key = "integrations.dearrow.badge", @@ -198,6 +206,7 @@ internal object IntegrationsIndex { userId, deArrow, deArrowRandomFrame, + deArrowThumbnailsOnly, deArrowBadge, aiListBlock, aiListWarn, diff --git a/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsScreen.kt b/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsScreen.kt index 8cdc53482f..9b3709fb5a 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsScreen.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsScreen.kt @@ -44,6 +44,8 @@ internal fun IntegrationsScreen( val segments by viewModel.segments.collectAsStateWithLifecycle() val skipButtonStyle by viewModel.skipButtonStyle.collectAsStateWithLifecycle() val deArrow by viewModel.deArrow.collectAsStateWithLifecycle() + val deArrowRandomFrame by viewModel.deArrowRandomFrame.collectAsStateWithLifecycle() + val deArrowThumbnailsOnly by viewModel.deArrowThumbnailsOnly.collectAsStateWithLifecycle() val discordState by viewModel.discordState.collectAsStateWithLifecycle() val aiListBlock by viewModel.aiListBlock.collectAsStateWithLifecycle() val aiListWarn by viewModel.aiListWarn.collectAsStateWithLifecycle() @@ -92,7 +94,19 @@ internal fun IntegrationsScreen( } group(key = "integrations.dearrow.group", header = R.string.player_settings_dearrow) { switch(IntegrationsIndex.deArrow, viewModel.deArrow, viewModel::setDeArrow, icon = Icons.Outlined.AutoFixHigh) - switch(IntegrationsIndex.deArrowRandomFrame, viewModel.deArrowRandomFrame, viewModel::setDeArrowRandomFrame, enabled = deArrow) + // With thumbnails only, a frame is the only fallback left, so the row reads on; the stored value is kept. + switch( + IntegrationsIndex.deArrowRandomFrame, + checked = deArrowRandomFrame || deArrowThumbnailsOnly, + onCheckedChange = viewModel::setDeArrowRandomFrame, + enabled = deArrow && !deArrowThumbnailsOnly, + ) + switch( + IntegrationsIndex.deArrowThumbnailsOnly, + viewModel.deArrowThumbnailsOnly, + viewModel::setDeArrowThumbnailsOnly, + enabled = deArrow, + ) switch(IntegrationsIndex.deArrowBadge, viewModel.deArrowBadge, viewModel::setDeArrowBadge, enabled = deArrow) } aiListSection( diff --git a/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsViewModel.kt b/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsViewModel.kt index 1f4dc5cb6a..35c2ee4f56 100644 --- a/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsViewModel.kt +++ b/app/src/main/java/io/github/aedev/flow/ui/screens/settings/integrations/IntegrationsViewModel.kt @@ -40,6 +40,7 @@ class IntegrationsViewModel val deArrow = preferences.deArrowEnabled.asState(false) val deArrowBadge = preferences.deArrowBadgeEnabled.asState(false) val deArrowRandomFrame = preferences.deArrowRandomFrame.asState(true) + val deArrowThumbnailsOnly = preferences.deArrowThumbnailsOnly.asState(false) val dislikes = preferences.rytdEnabled.asState(true) val aiListBlock = aiList.blockEnabled.asState(false) val aiListWarn = aiList.warnEnabled.asState(false) @@ -116,6 +117,8 @@ class IntegrationsViewModel fun setDeArrowRandomFrame(value: Boolean) = write { preferences.setDeArrowRandomFrame(value) } + fun setDeArrowThumbnailsOnly(value: Boolean) = write { preferences.setDeArrowThumbnailsOnly(value) } + fun setDislikes(value: Boolean) = write { preferences.setRytdEnabled(value) } fun setDiscordEnabled(value: Boolean) = write { discord.setEnabled(value) } diff --git a/app/src/main/java/io/github/aedev/flow/utils/ThumbnailUrlResolver.kt b/app/src/main/java/io/github/aedev/flow/utils/ThumbnailUrlResolver.kt index 0b51023d63..98b324645b 100644 --- a/app/src/main/java/io/github/aedev/flow/utils/ThumbnailUrlResolver.kt +++ b/app/src/main/java/io/github/aedev/flow/utils/ThumbnailUrlResolver.kt @@ -58,6 +58,27 @@ object ThumbnailUrlResolver { ThumbnailQuality.OFF -> emptyList() } + /** + * YouTube's automatic middle frame (`2.jpg`, about half way in) in the viewer's tier, best first: + * a still from the video, never the uploader's thumbnail. Every video has `hq2`. + */ + fun youtubeFrameCandidates( + videoId: String, + quality: ThumbnailQuality = ThumbnailQuality.HIGH, + portrait: Boolean = false, + ): List { + val id = videoId.trim() + if (id.isEmpty() || LocalMediaIds.isLocal(id)) return emptyList() + val tiers = + when (quality) { + ThumbnailQuality.HIGH -> listOf("maxres2", "sd2", "hq2") + ThumbnailQuality.MEDIUM -> listOf("sd2", "hq2") + ThumbnailQuality.LOW -> if (portrait) listOf("hq2") else listOf("mq2", "hq2") + ThumbnailQuality.OFF -> emptyList() + } + return tiers.map { tier -> "https://i.ytimg.com/vi/$id/$tier.jpg" } + } + /** A cover stored on the device: a download's saved thumbnail, or a MediaStore item. */ fun isDeviceUri(url: String): Boolean = url.startsWith("file:") || url.startsWith("content:") || url.startsWith("/") @@ -66,10 +87,18 @@ object ThumbnailUrlResolver { rawUrl: String?, quality: ThumbnailQuality = ThumbnailQuality.HIGH, portrait: Boolean = false, + youTubeFrames: Boolean = false, ): List { val raw = rawUrl?.trim().orEmpty() val resolvedVideoId = resolveYoutubeThumbnailVideoId(videoId, raw) - val youtubeCandidates = youtubeThumbnailCandidates(resolvedVideoId, quality, portrait) + val youtubeCandidates = + if (youTubeFrames) { + // Live and upcoming videos have no frames yet; only they ever reach the last entry. + youtubeFrameCandidates(resolvedVideoId, quality, portrait) + + youtubeThumbnailCandidates(resolvedVideoId, quality, portrait).takeLast(1) + } else { + youtubeThumbnailCandidates(resolvedVideoId, quality, portrait) + } val candidates = when { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index be53f3f7f3..057a997432 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1670,6 +1670,8 @@ Show DeArrow Badge Use a video frame When nothing is submitted, show a frame from the video + DeArrow thumbnails only + Never show the uploader\'s thumbnail Display a badge on thumbnails corrected by DeArrow Global Video Loop Loop all videos by default diff --git a/app/src/test/java/io/github/aedev/flow/data/local/DevicePreferencesSplitTest.kt b/app/src/test/java/io/github/aedev/flow/data/local/DevicePreferencesSplitTest.kt index a5ec7ccf1b..47c68c5014 100644 --- a/app/src/test/java/io/github/aedev/flow/data/local/DevicePreferencesSplitTest.kt +++ b/app/src/test/java/io/github/aedev/flow/data/local/DevicePreferencesSplitTest.kt @@ -25,7 +25,7 @@ class DevicePreferencesSplitTest { val device = keyNames("DeviceKeys") assertThat(device).hasSize(133) - assertThat(profile).hasSize(279) + assertThat(profile).hasSize(280) assertThat(device.toSet()).hasSize(device.size) assertThat(profile.toSet()).hasSize(profile.size) assertThat(device.intersect(profile.toSet())).isEmpty() diff --git a/app/src/test/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicyTest.kt b/app/src/test/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicyTest.kt index 0ead98ca35..470025d8bd 100644 --- a/app/src/test/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicyTest.kt +++ b/app/src/test/java/io/github/aedev/flow/data/repository/DeArrowBrandingPolicyTest.kt @@ -34,8 +34,24 @@ class DeArrowBrandingPolicyTest { } @Test - fun `no duration anywhere means no frame`() { - assertThat(resolve(DeArrowContent(randomTime = 0.5), duration = 0)).isNull() + fun `no duration anywhere still asks for youtube's frame instead of the uploader's thumbnail`() { + val result = resolve(DeArrowContent(randomTime = 0.5), duration = 0) + + assertThat(result?.thumbnailUrl).isNull() + assertThat(result?.youTubeFrames).isTrue() + } + + @Test + fun `a frame from DeArrow falls back to youtube's frames`() { + assertThat(resolve(DeArrowContent(randomTime = 0.5, videoDuration = 100.0))?.youTubeFrames).isTrue() + assertThat(resolve(DeArrowContent(thumbnails = listOf(DeArrowThumbnail(timestamp = 4.0, votes = 1))))?.youTubeFrames).isTrue() + } + + @Test + fun `a title alone keeps the uploader's thumbnail`() { + val result = resolve(DeArrowContent(titles = listOf(DeArrowTitle("Calm title", votes = 1))), useRandomFrame = false) + + assertThat(result?.youTubeFrames).isFalse() } @Test diff --git a/app/src/test/java/io/github/aedev/flow/player/factory/LoadControlFactoryTest.kt b/app/src/test/java/io/github/aedev/flow/player/factory/LoadControlFactoryTest.kt index 539b94c5bc..865ea513c2 100644 --- a/app/src/test/java/io/github/aedev/flow/player/factory/LoadControlFactoryTest.kt +++ b/app/src/test/java/io/github/aedev/flow/player/factory/LoadControlFactoryTest.kt @@ -2,6 +2,7 @@ package io.github.aedev.flow.player.factory import androidx.media3.common.util.UnstableApi import androidx.media3.exoplayer.DefaultLoadControl +import androidx.media3.exoplayer.analytics.PlayerId import io.github.aedev.flow.player.config.PlayerConfig import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -26,6 +27,33 @@ class LoadControlFactoryTest { LoadControlFactory.forMusic() } + @Test + fun `whole-song music profile is accepted by the load control`() { + LoadControlFactory.forMusic(wholeSong = true) + } + + @Test + fun `whole-song music keeps what has played and the lean profile keeps nothing`() { + val playerId = PlayerId("test") + + assertEquals( + PlayerConfig.MUSIC_BACK_BUFFER_MS * 1_000L, + LoadControlFactory.forMusic(wholeSong = true).getBackBufferDurationUs(playerId), + ) + assertEquals(0L, LoadControlFactory.forMusic().getBackBufferDurationUs(playerId)) + } + + @Test + fun `whole-song music constants satisfy the load control contract before any coercion`() { + assertContractHolds( + profile = "whole-song music", + minMs = PlayerConfig.MUSIC_MIN_BUFFER_MS, + maxMs = PlayerConfig.MUSIC_WHOLE_SONG_BUFFER_MS, + playbackMs = PlayerConfig.MUSIC_BUFFER_FOR_PLAYBACK_MS, + rebufferMs = PlayerConfig.MUSIC_BUFFER_FOR_REBUFFER_MS, + ) + } + @Test fun `shorts constants satisfy the load control contract before any coercion`() { assertContractHolds( diff --git a/app/src/test/java/io/github/aedev/flow/service/MusicLoadControlTest.kt b/app/src/test/java/io/github/aedev/flow/service/MusicLoadControlTest.kt new file mode 100644 index 0000000000..80e6f7933d --- /dev/null +++ b/app/src/test/java/io/github/aedev/flow/service/MusicLoadControlTest.kt @@ -0,0 +1,103 @@ +package io.github.aedev.flow.service + +import androidx.annotation.OptIn +import androidx.media3.common.MediaItem +import androidx.media3.common.util.UnstableApi +import androidx.media3.exoplayer.DefaultLoadControl +import androidx.media3.exoplayer.LoadControl +import androidx.media3.exoplayer.analytics.PlayerId +import androidx.media3.exoplayer.source.MediaSource +import androidx.media3.exoplayer.source.SinglePeriodTimeline +import io.github.aedev.flow.player.factory.LoadControlFactory +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test + +@OptIn(UnstableApi::class) +class MusicLoadControlTest { + private val playerId = PlayerId("music") + private val timeline = SinglePeriodTimeline(300_000_000L, true, false, false, null, MediaItem.Builder().setMediaId("song").build()) + private val periodId = MediaSource.MediaPeriodId(timeline.getUidOfPeriod(0)) + private lateinit var control: MusicLoadControl + + @Before + fun setUp() { + control = MusicLoadControl(LoadControlFactory.forMusic(wholeSong = true), initiallySavesData = false) + control.onPrepared(playerId) + } + + @Test + fun `without data saving the song keeps loading past the lean window`() { + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 0))) + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 45))) + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 240))) + } + + @Test + fun `data saving stops at the lean window and waits until the buffer runs low`() { + control.savesData = true + + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 0))) + assertFalse(control.shouldContinueLoading(params(bufferedSeconds = 30))) + assertFalse(control.shouldContinueLoading(params(bufferedSeconds = 20))) + assertFalse(control.shouldContinueLoading(params(bufferedSeconds = 3))) + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 2))) + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 20))) + } + + @Test + fun `turning data saving off lets a capped song load to the end`() { + control.savesData = true + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 0))) + assertFalse(control.shouldContinueLoading(params(bufferedSeconds = 30))) + + control.savesData = false + + assertTrue(control.shouldContinueLoading(params(bufferedSeconds = 31))) + } + + @Test + fun `the back buffer stays while saving data`() { + control.savesData = true + + assertEquals( + LoadControlFactory.forMusic(wholeSong = true).getBackBufferDurationUs(playerId), + control.getBackBufferDurationUs(playerId), + ) + assertTrue(control.retainBackBufferFromKeyframe(playerId)) + } + + @Test + fun `every method the stock control implements is forwarded, not left to a throwing default`() { + val forwarded = + MusicLoadControl::class.java.declaredMethods + .map { it.name to it.parameterTypes.toList() } + .toSet() + val missing = + DefaultLoadControl::class.java.declaredMethods + .filter { + java.lang.reflect.Modifier + .isPublic(it.modifiers) && + LoadControl::class.java.methods.any { m -> m.name == it.name } + }.map { it.name to it.parameterTypes.toList() } + .filterNot { it in forwarded } + + assertEquals(emptyList(), missing) + } + + private fun params(bufferedSeconds: Long) = + LoadControl.Parameters( + playerId, + timeline, + periodId, + 0L, + bufferedSeconds * 1_000_000L, + 1f, + true, + false, + 0L, + 0L, + ) +} diff --git a/app/src/test/java/io/github/aedev/flow/ui/components/shared/card/CardThumbnailPolicyTest.kt b/app/src/test/java/io/github/aedev/flow/ui/components/shared/card/CardThumbnailPolicyTest.kt new file mode 100644 index 0000000000..9f6df1264b --- /dev/null +++ b/app/src/test/java/io/github/aedev/flow/ui/components/shared/card/CardThumbnailPolicyTest.kt @@ -0,0 +1,83 @@ +package io.github.aedev.flow.ui.components.shared.card + +import com.google.common.truth.Truth.assertThat +import io.github.aedev.flow.data.model.DeArrowResult +import io.github.aedev.flow.data.model.Video +import io.github.aedev.flow.ui.components.DeArrowLookup +import org.junit.Test + +class CardThumbnailPolicyTest { + private val uploaders = "https://i.ytimg.com/vi/abc123def45/hq720.jpg" + private val frame = "https://dearrow-thumb.ajay.app/api/v1/getThumbnail?videoID=abc123def45&time=12.5" + + private fun video( + id: String = "abc123def45", + isShort: Boolean = false, + isLive: Boolean = false, + isUpcoming: Boolean = false, + ) = Video( + id = id, + title = "Title", + channelName = "Channel", + channelId = "UC1", + thumbnailUrl = uploaders, + duration = 600, + viewCount = 1, + uploadDate = "", + isShort = isShort, + isLive = isLive, + isUpcoming = isUpcoming, + ) + + private val pending = DeArrowLookup(result = null, answered = false) + private val nothing = DeArrowLookup(result = null, answered = true) + + @Test + fun `thumbnails only holds the card empty until DeArrow answers`() { + assertThat( + CardThumbnailPolicy.resolve(video(), pending, thumbnailsOnly = true), + ).isEqualTo(CardThumbnail(url = null, youTubeFrames = true)) + } + + @Test + fun `thumbnails only shows DeArrow's frame once it answers`() { + val answered = DeArrowLookup(DeArrowResult(thumbnailUrl = frame, youTubeFrames = true), answered = true) + + assertThat( + CardThumbnailPolicy.resolve(video(), answered, thumbnailsOnly = true), + ).isEqualTo(CardThumbnail(frame, youTubeFrames = true)) + } + + @Test + fun `thumbnails only turns no answer, a failure or a kept original into youtube's frame`() { + val keptTitle = DeArrowLookup(DeArrowResult(title = "Calm title"), answered = true) + + assertThat(CardThumbnailPolicy.resolve(video(), nothing, thumbnailsOnly = true).youTubeFrames).isTrue() + assertThat(CardThumbnailPolicy.resolve(video(), keptTitle, thumbnailsOnly = true).youTubeFrames).isTrue() + } + + @Test + fun `shorts, live, upcoming and device files keep their own image`() { + listOf(video(isShort = true), video(isLive = true), video(isUpcoming = true), video(id = "local_42")).forEach { kept -> + assertThat( + CardThumbnailPolicy.resolve(kept, pending, thumbnailsOnly = true), + ).isEqualTo(CardThumbnail(uploaders, youTubeFrames = false)) + } + } + + @Test + fun `with the switch off the card shows the uploader's thumbnail while DeArrow is asked`() { + assertThat( + CardThumbnailPolicy.resolve(video(), pending, thumbnailsOnly = false), + ).isEqualTo(CardThumbnail(uploaders, youTubeFrames = false)) + } + + @Test + fun `with the switch off a DeArrow frame still falls back to youtube's frames`() { + val answered = DeArrowLookup(DeArrowResult(thumbnailUrl = frame, youTubeFrames = true), answered = true) + + assertThat( + CardThumbnailPolicy.resolve(video(), answered, thumbnailsOnly = false), + ).isEqualTo(CardThumbnail(frame, youTubeFrames = true)) + } +} diff --git a/app/src/test/java/io/github/aedev/flow/utils/ThumbnailUrlResolverTest.kt b/app/src/test/java/io/github/aedev/flow/utils/ThumbnailUrlResolverTest.kt index 64fde0cd6c..bfe5779be2 100644 --- a/app/src/test/java/io/github/aedev/flow/utils/ThumbnailUrlResolverTest.kt +++ b/app/src/test/java/io/github/aedev/flow/utils/ThumbnailUrlResolverTest.kt @@ -1,6 +1,7 @@ package io.github.aedev.flow.utils import com.google.common.truth.Truth.assertThat +import io.github.aedev.flow.data.local.ThumbnailQuality import org.junit.Test class ThumbnailUrlResolverTest { @@ -158,4 +159,60 @@ class ThumbnailUrlResolverTest { assertThat(ThumbnailUrlResolver.resolveCommunityPostImage("//yt3.ggpht.com/abc", uncropped = true)) .isEqualTo("https://yt3.ggpht.com/abc=w2048") } + + @Test + fun `youtube frames replace the uploader's thumbnail tiers at each quality`() { + fun frames(quality: ThumbnailQuality) = + ThumbnailUrlResolver.youtubeFrameCandidates("abc123", quality).map { it.substringAfterLast('/') } + + assertThat(frames(ThumbnailQuality.HIGH)).containsExactly("maxres2.jpg", "sd2.jpg", "hq2.jpg").inOrder() + assertThat(frames(ThumbnailQuality.MEDIUM)).containsExactly("sd2.jpg", "hq2.jpg").inOrder() + assertThat(frames(ThumbnailQuality.LOW)).containsExactly("mq2.jpg", "hq2.jpg").inOrder() + assertThat(frames(ThumbnailQuality.OFF)).isEmpty() + } + + @Test + fun `a DeArrow frame falls back to youtube frames, never the uploader's thumbnail`() { + val dearrow = "https://dearrow-thumb.ajay.app/api/v1/getThumbnail?videoID=abc123&time=12.5" + + val candidates = + ThumbnailUrlResolver.resolveVideoThumbnailCandidates( + "abc123", + dearrow, + ThumbnailQuality.HIGH, + youTubeFrames = true, + ) + + assertThat(candidates) + .containsExactly( + dearrow, + "https://i.ytimg.com/vi/abc123/maxres2.jpg", + "https://i.ytimg.com/vi/abc123/sd2.jpg", + "https://i.ytimg.com/vi/abc123/hq2.jpg", + "https://i.ytimg.com/vi/abc123/hqdefault.jpg", + ).inOrder() + } + + @Test + fun `the uploader's thumbnail is only the last resort when youtube frames are wanted`() { + val candidates = + ThumbnailUrlResolver.resolveVideoThumbnailCandidates( + "abc123", + "https://i.ytimg.com/vi/abc123/hq720.jpg", + ThumbnailQuality.MEDIUM, + youTubeFrames = true, + ) + + assertThat(candidates) + .containsExactly( + "https://i.ytimg.com/vi/abc123/sd2.jpg", + "https://i.ytimg.com/vi/abc123/hq2.jpg", + "https://i.ytimg.com/vi/abc123/hqdefault.jpg", + ).inOrder() + } + + @Test + fun `local videos get no youtube frames`() { + assertThat(ThumbnailUrlResolver.youtubeFrameCandidates("local_42")).isEmpty() + } }