Skip to content

Commit 9190698

Browse files
committedJan 3, 2020
Add a VideoDecoder for ByteBuffers
Fixes #4021
1 parent 100ac4a commit 9190698

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
 

‎library/src/main/java/com/bumptech/glide/Glide.java

+10
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,16 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
560560
bitmapPool, bitmapBytesTranscoder, gifDrawableBytesTranscoder))
561561
.register(GifDrawable.class, byte[].class, gifDrawableBytesTranscoder);
562562

563+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
564+
ResourceDecoder<ByteBuffer, Bitmap> byteBufferVideoDecoder =
565+
VideoDecoder.byteBuffer(bitmapPool);
566+
registry.append(ByteBuffer.class, Bitmap.class, byteBufferVideoDecoder);
567+
registry.append(
568+
ByteBuffer.class,
569+
BitmapDrawable.class,
570+
new BitmapDrawableDecoder<>(resources, byteBufferVideoDecoder));
571+
}
572+
563573
ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
564574
glideContext =
565575
new GlideContext(

‎library/src/main/java/com/bumptech/glide/load/resource/bitmap/VideoDecoder.java

+37
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import android.annotation.TargetApi;
44
import android.content.res.AssetFileDescriptor;
55
import android.graphics.Bitmap;
6+
import android.media.MediaDataSource;
67
import android.media.MediaMetadataRetriever;
78
import android.os.Build;
89
import android.os.Build.VERSION_CODES;
910
import android.os.ParcelFileDescriptor;
1011
import android.util.Log;
1112
import androidx.annotation.NonNull;
1213
import androidx.annotation.Nullable;
14+
import androidx.annotation.RequiresApi;
1315
import androidx.annotation.VisibleForTesting;
1416
import com.bumptech.glide.load.Option;
1517
import com.bumptech.glide.load.Options;
@@ -120,6 +122,11 @@ public static ResourceDecoder<ParcelFileDescriptor, Bitmap> parcel(BitmapPool bi
120122
return new VideoDecoder<>(bitmapPool, new ParcelFileDescriptorInitializer());
121123
}
122124

125+
@RequiresApi(api = VERSION_CODES.M)
126+
public static ResourceDecoder<ByteBuffer, Bitmap> byteBuffer(BitmapPool bitmapPool) {
127+
return new VideoDecoder<>(bitmapPool, new ByteBufferInitializer());
128+
}
129+
123130
VideoDecoder(BitmapPool bitmapPool, MediaMetadataRetrieverInitializer<T> initializer) {
124131
this(bitmapPool, initializer, DEFAULT_FACTORY);
125132
}
@@ -299,4 +306,34 @@ public void initialize(MediaMetadataRetriever retriever, ParcelFileDescriptor da
299306
retriever.setDataSource(data.getFileDescriptor());
300307
}
301308
}
309+
310+
@RequiresApi(Build.VERSION_CODES.M)
311+
static final class ByteBufferInitializer
312+
implements MediaMetadataRetrieverInitializer<ByteBuffer> {
313+
314+
@Override
315+
public void initialize(MediaMetadataRetriever retriever, final ByteBuffer data) {
316+
retriever.setDataSource(
317+
new MediaDataSource() {
318+
@Override
319+
public int readAt(long position, byte[] buffer, int offset, int size) {
320+
if (position >= data.limit()) {
321+
return -1;
322+
}
323+
data.position((int) position);
324+
int numBytesRead = Math.min(size, data.remaining());
325+
data.get(buffer, offset, numBytesRead);
326+
return numBytesRead;
327+
}
328+
329+
@Override
330+
public long getSize() {
331+
return data.limit();
332+
}
333+
334+
@Override
335+
public void close() {}
336+
});
337+
}
338+
}
302339
}

0 commit comments

Comments
 (0)
Please sign in to comment.