-
-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1867b2
commit 77ef06d
Showing
9 changed files
with
134 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/src/main/java/org/jellyfin/androidtv/ui/playback/overlay/CustomSeekProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,104 @@ | ||
package org.jellyfin.androidtv.ui.playback.overlay | ||
|
||
import android.content.Context | ||
import androidx.core.graphics.drawable.toBitmap | ||
import androidx.leanback.widget.PlaybackSeekDataProvider | ||
import coil.ImageLoader | ||
import coil.request.Disposable | ||
import coil.request.ImageRequest | ||
import coil.size.Size | ||
import org.jellyfin.androidtv.util.coil.SubsetTransformation | ||
import org.jellyfin.sdk.api.client.ApiClient | ||
import org.jellyfin.sdk.api.client.extensions.trickplayApi | ||
import org.jellyfin.sdk.api.client.util.AuthorizationHeaderBuilder | ||
import org.jellyfin.sdk.model.serializer.toUUIDOrNull | ||
import kotlin.math.ceil | ||
import kotlin.math.min | ||
|
||
class CustomSeekProvider( | ||
private val videoPlayerAdapter: VideoPlayerAdapter, | ||
private val imageLoader: ImageLoader, | ||
private val api: ApiClient, | ||
private val context: Context, | ||
private val trickPlayEnabled: Boolean, | ||
) : PlaybackSeekDataProvider() { | ||
companion object { | ||
private const val SEEK_LENGTH = 10000L | ||
} | ||
|
||
private val imageRequests = mutableMapOf<Int, Disposable>() | ||
|
||
override fun getSeekPositions(): LongArray { | ||
if (!videoPlayerAdapter.canSeek()) return LongArray(0) | ||
|
||
val duration = videoPlayerAdapter.duration | ||
val size = ceil(duration.toDouble() / SEEK_LENGTH.toDouble()).toInt() + 1 | ||
return LongArray(size) { i -> min(i * SEEK_LENGTH, duration) } | ||
} | ||
|
||
override fun getThumbnail(index: Int, callback: ResultCallback) { | ||
if (!trickPlayEnabled) return | ||
|
||
val currentRequest = imageRequests[index] | ||
if (currentRequest?.isDisposed == false) currentRequest.dispose() | ||
|
||
val item = videoPlayerAdapter.currentlyPlayingItem | ||
val mediaSource = videoPlayerAdapter.currentMediaSource | ||
val mediaSourceId = mediaSource?.id?.toUUIDOrNull() | ||
if (item == null || mediaSource == null || mediaSourceId == null) return | ||
|
||
val trickPlayResolutions = item.trickplay?.get(mediaSource.id) | ||
val trickPlayInfo = trickPlayResolutions?.values?.firstOrNull() | ||
if (trickPlayInfo == null) return | ||
|
||
val currentTimeMs = (index * SEEK_LENGTH).coerceIn(0, videoPlayerAdapter.duration) | ||
val currentTile = currentTimeMs.floorDiv(trickPlayInfo.interval).toInt() | ||
|
||
val tileSize = trickPlayInfo.tileWidth * trickPlayInfo.tileHeight | ||
val tileOffset = currentTile % tileSize | ||
val tileIndex = currentTile / tileSize | ||
|
||
val tileOffsetX = tileOffset % trickPlayInfo.tileWidth | ||
val tileOffsetY = tileOffset / trickPlayInfo.tileWidth | ||
val offsetX = tileOffsetX * trickPlayInfo.width | ||
val offsetY = tileOffsetY * trickPlayInfo.height | ||
|
||
val url = api.trickplayApi.getTrickplayTileImageUrl( | ||
itemId = item.id, | ||
width = trickPlayInfo.width, | ||
index = tileIndex, | ||
mediaSourceId = mediaSourceId, | ||
) | ||
|
||
imageRequests[index] = imageLoader.enqueue(ImageRequest.Builder(context).apply { | ||
data(url) | ||
size(Size.ORIGINAL) | ||
addHeader( | ||
"Authorization", | ||
AuthorizationHeaderBuilder.buildHeader( | ||
api.clientInfo.name, | ||
api.clientInfo.version, | ||
api.deviceInfo.id, | ||
api.deviceInfo.name, | ||
api.accessToken | ||
) | ||
) | ||
|
||
transformations(SubsetTransformation(offsetX, offsetY, trickPlayInfo.width, trickPlayInfo.height)) | ||
|
||
target( | ||
onSuccess = { result -> | ||
val bitmap = result.current.toBitmap() | ||
callback.onThumbnailLoaded(bitmap, index) | ||
} | ||
) | ||
}.build()) | ||
} | ||
|
||
override fun reset() { | ||
for (request in imageRequests.values) { | ||
if (!request.isDisposed) request.dispose() | ||
} | ||
imageRequests.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/org/jellyfin/androidtv/util/coil/SubsetTransformation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.jellyfin.androidtv.util.coil | ||
|
||
import android.graphics.Bitmap | ||
import coil.size.Size | ||
import coil.transform.Transformation | ||
|
||
class SubsetTransformation( | ||
private val x: Int, | ||
private val y: Int, | ||
private val width: Int, | ||
private val height: Int, | ||
) : Transformation { | ||
override val cacheKey: String = "$x,$y,$width,$height" | ||
|
||
override suspend fun transform( | ||
input: Bitmap, | ||
size: Size, | ||
): Bitmap = Bitmap.createBitmap(input, x, y, width, height) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters