Skip to content

Commit

Permalink
Depend on updated Parameters API.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Sep 27, 2019
1 parent 8930117 commit 652563e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
15 changes: 10 additions & 5 deletions coil-video/src/main/java/coil/decode/VideoFrameDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import android.os.Build.VERSION_CODES.M
import android.os.Build.VERSION_CODES.O_MR1
import androidx.core.graphics.drawable.toDrawable
import coil.bitmappool.BitmapPool
import coil.request.videoFrame
import coil.extension.get
import coil.extension.videoFrameMicros
import coil.extension.videoFrameMillis
import coil.size.PixelSize
import coil.size.Size
import okio.BufferedSource
Expand All @@ -21,12 +23,13 @@ import okio.sink
import java.io.File

/**
* A [Decoder] that decodes a frame from a video. Use [videoFrame] to specify the time of the frame to extract.
* A [Decoder] that decodes a frame from a video.
* Use [videoFrameMillis] or [videoFrameMicros] to specify the time of the frame to extract.
*/
class VideoFrameDecoder(private val context: Context) : Decoder {

companion object {
internal const val VIDEO_FRAME_MILLIS_KEY = "coil.decode.VideoFrameDecoder#video_frame_millis"
internal const val VIDEO_FRAME_MICROS_KEY = "coil.decode.VideoFrameDecoder#video_frame_micros"
}

/** TODO: Check the file headers for any of Android's supported video formats instead of relying on the MIME type. */
Expand All @@ -52,7 +55,7 @@ class VideoFrameDecoder(private val context: Context) : Decoder {
retriever.setDataSource(tempFile.path)
}

val frameMicros = TODO()
val frameMicros = (options.parameters[VIDEO_FRAME_MICROS_KEY] as? Number)?.toLong() ?: 0L

// Frame sampling is only supported on O_MR1 and above.
if (SDK_INT >= O_MR1 && size is PixelSize) {
Expand All @@ -70,7 +73,9 @@ class VideoFrameDecoder(private val context: Context) : Decoder {
}

// Read the frame at its full size.
val bitmap = checkNotNull(retriever.getFrameAtTime(frameMicros, OPTION_CLOSEST_SYNC)) { "Failed to decode frame." }
val bitmap = checkNotNull(retriever.getFrameAtTime(frameMicros, OPTION_CLOSEST_SYNC)) {
"Failed to decode frame at $frameMicros microseconds."
}

return DecodeResult(
drawable = bitmap.toDrawable(context.resources),
Expand Down
24 changes: 24 additions & 0 deletions coil-video/src/main/java/coil/extension/Requests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@file:Suppress("unused")
@file:JvmName("Requests")

package coil.extension

import coil.decode.VideoFrameDecoder
import coil.request.RequestBuilder

/**
* Set the time **in milliseconds** of the frame to extract from a video.
*
* @see VideoFrameDecoder
*/
fun RequestBuilder<*>.videoFrameMillis(frameMillis: Long) = videoFrameMicros(1000 * frameMillis)

/**
* Set the time **in microseconds** of the frame to extract from a video.
*
* @see VideoFrameDecoder
*/
fun RequestBuilder<*>.videoFrameMicros(frameMicros: Long) {
require(frameMicros >= 0) { "frameMicros must be >= 0" }
setParameter(VideoFrameDecoder.VIDEO_FRAME_MICROS_KEY, frameMicros, frameMicros.toString())
}
15 changes: 0 additions & 15 deletions coil-video/src/main/java/coil/request/Requests.kt

This file was deleted.

0 comments on commit 652563e

Please sign in to comment.