public static ResourceDecoder<ParcelFileDescriptor, Bitmap> parcel(BitmapPool bitmapPool) { return new VideoDecoder<>(bitmapPool, new ParcelFileDescriptorInitializer()); }
@Nullable private static Bitmap decodeFrame( MediaMetadataRetriever mediaMetadataRetriever, long frameTimeMicros, int frameOption, int outWidth, int outHeight, DownsampleStrategy strategy) { Bitmap result = null; // Arguably we should handle the case where just width or just height is set to // Target.SIZE_ORIGINAL. Up to and including OMR1, MediaMetadataRetriever defaults to setting // the dimensions to the display width and height if they aren't specified (ie // getScaledFrameAtTime is not used). Given that this is an optimization only if // Target.SIZE_ORIGINAL is not used and not using getScaledFrameAtTime ever would match the // behavior of Glide in all versions of Android prior to OMR1, it's probably fine for now. if (Build.VERSION.SDK_INT >= VERSION_CODES.O_MR1 && outWidth != Target.SIZE_ORIGINAL && outHeight != Target.SIZE_ORIGINAL && strategy != DownsampleStrategy.NONE) { result = decodeScaledFrame( mediaMetadataRetriever, frameTimeMicros, frameOption, outWidth, outHeight, strategy); } if (result == null) { result = decodeOriginalFrame(mediaMetadataRetriever, frameTimeMicros, frameOption); } return result; }
@Test(expected = IllegalArgumentException.class) public void testThrowsExceptionIfCalledWithInvalidFrame() throws IOException { Util.setSdkVersionInt(19); options.set(VideoDecoder.TARGET_FRAME, -5L); new VideoDecoder<>(bitmapPool, initializer, factory).decode(resource, 100, 100, options); }
@Test public void decodeFrame_withTargetSizeOriginalWidthOnly_onApi27_doesNotThrow() throws IOException { Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(retriever.getFrameAtTime(-1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)) .thenReturn(expected); verify(retriever, never()).getScaledFrameAtTime(anyLong(), anyInt(), anyInt(), anyInt()); assertThat(decoder.decode(resource, Target.SIZE_ORIGINAL, 100, options).get()) .isSameAs(expected); }
new ByteBufferGifDecoder(context, imageHeaderParsers, bitmapPool, arrayPool); ResourceDecoder<ParcelFileDescriptor, Bitmap> parcelFileDescriptorVideoDecoder = VideoDecoder.parcel(bitmapPool); ByteBufferBitmapDecoder byteBufferBitmapDecoder = new ByteBufferBitmapDecoder(downsampler); StreamBitmapDecoder streamBitmapDecoder = new StreamBitmapDecoder(downsampler, arrayPool); AssetFileDescriptor.class, Bitmap.class, VideoDecoder.asset(bitmapPool)) .append(Bitmap.class, Bitmap.class, UnitModelLoader.Factory.<Bitmap>getInstance()) .append(
initializer.initialize(mediaMetadataRetriever, resource); result = decodeFrame( mediaMetadataRetriever, frameTimeMicros,
@Test public void testDoesNotSpecifyThumbnailFrameIfCalledWithoutFrameNumber() throws IOException { Util.setSdkVersionInt(19); decoder = new VideoDecoder<>(bitmapPool, initializer, factory); decoder.decode(resource, 100, 100, options); verify(retriever).getFrameAtTime(VideoDecoder.DEFAULT_FRAME, VideoDecoder.DEFAULT_FRAME_OPTION); }
@Test public void decodeFrame_withTargetSizeOriginal_onApi27_doesNotThrow() throws IOException { Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(retriever.getFrameAtTime(-1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)) .thenReturn(expected); verify(retriever, never()).getScaledFrameAtTime(anyLong(), anyInt(), anyInt(), anyInt()); assertThat(decoder.decode(resource, Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL, options).get()) .isSameAs(expected); }
@Test public void testSpecifiesThumbnailFrameIfICalledWithFrameNumber() throws IOException { Util.setSdkVersionInt(19); long frame = 5; options.set(VideoDecoder.TARGET_FRAME, frame); decoder = new VideoDecoder<>(bitmapPool, initializer, factory); decoder.decode(resource, 100, 100, options); verify(retriever).getFrameAtTime(frame, VideoDecoder.DEFAULT_FRAME_OPTION); }
public static ResourceDecoder<AssetFileDescriptor, Bitmap> asset(BitmapPool bitmapPool) { return new VideoDecoder<>(bitmapPool, new AssetFileDescriptorInitializer()); }
@Test public void decodeFrame_withTargetSizeOriginalHeightOnly_onApi27_doesNotThrow() throws IOException { Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(retriever.getFrameAtTime(-1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)) .thenReturn(expected); verify(retriever, never()).getScaledFrameAtTime(anyLong(), anyInt(), anyInt(), anyInt()); assertThat(decoder.decode(resource, 100, Target.SIZE_ORIGINAL, options).get()) .isSameAs(expected); } }
@Before public void setup() { MockitoAnnotations.initMocks(this); when(factory.build()).thenReturn(retriever); decoder = new VideoDecoder<>(bitmapPool, initializer, factory); options = new Options(); initialSdkVersion = Build.VERSION.SDK_INT; }
@Test public void testReleasesMediaMetadataRetriever() throws IOException { Util.setSdkVersionInt(19); decoder.decode(resource, 1, 2, options); verify(retriever).release(); }
@Test public void getScaledFrameAtTime() throws IOException { // Anything other than NONE. options.set(DownsampleStrategy.OPTION, DownsampleStrategy.AT_LEAST); Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)) .thenReturn("100"); when(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)) .thenReturn("100"); when(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)) .thenReturn("0"); when(retriever.getScaledFrameAtTime(-1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC, 100, 100)) .thenReturn(expected); assertThat(decoder.decode(resource, 100, 100, options).get()).isSameAs(expected); }
@Test public void testReturnsRetrievedFrameForResource() throws IOException { Util.setSdkVersionInt(19); Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(retriever.getFrameAtTime(VideoDecoder.DEFAULT_FRAME, VideoDecoder.DEFAULT_FRAME_OPTION)) .thenReturn(expected); Resource<Bitmap> result = Preconditions.checkNotNull(decoder.decode(resource, 100, 100, options)); verify(initializer).initialize(retriever, resource); assertEquals(expected, result.get()); }