Skip to content

Commit

Permalink
Add a VideoDecoder for ByteBuffers
Browse files Browse the repository at this point in the history
Fixes #4021
  • Loading branch information
sjudd committed Jan 3, 2020
1 parent 100ac4a commit 9190698
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
bitmapPool, bitmapBytesTranscoder, gifDrawableBytesTranscoder))
.register(GifDrawable.class, byte[].class, gifDrawableBytesTranscoder);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ResourceDecoder<ByteBuffer, Bitmap> byteBufferVideoDecoder =
VideoDecoder.byteBuffer(bitmapPool);
registry.append(ByteBuffer.class, Bitmap.class, byteBufferVideoDecoder);
registry.append(
ByteBuffer.class,
BitmapDrawable.class,
new BitmapDrawableDecoder<>(resources, byteBufferVideoDecoder));
}

ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
glideContext =
new GlideContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import android.annotation.TargetApi;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.media.MediaDataSource;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import com.bumptech.glide.load.Option;
import com.bumptech.glide.load.Options;
Expand Down Expand Up @@ -120,6 +122,11 @@ public static ResourceDecoder<ParcelFileDescriptor, Bitmap> parcel(BitmapPool bi
return new VideoDecoder<>(bitmapPool, new ParcelFileDescriptorInitializer());
}

@RequiresApi(api = VERSION_CODES.M)
public static ResourceDecoder<ByteBuffer, Bitmap> byteBuffer(BitmapPool bitmapPool) {
return new VideoDecoder<>(bitmapPool, new ByteBufferInitializer());
}

VideoDecoder(BitmapPool bitmapPool, MediaMetadataRetrieverInitializer<T> initializer) {
this(bitmapPool, initializer, DEFAULT_FACTORY);
}
Expand Down Expand Up @@ -299,4 +306,34 @@ public void initialize(MediaMetadataRetriever retriever, ParcelFileDescriptor da
retriever.setDataSource(data.getFileDescriptor());
}
}

@RequiresApi(Build.VERSION_CODES.M)
static final class ByteBufferInitializer
implements MediaMetadataRetrieverInitializer<ByteBuffer> {

@Override
public void initialize(MediaMetadataRetriever retriever, final ByteBuffer data) {
retriever.setDataSource(
new MediaDataSource() {
@Override
public int readAt(long position, byte[] buffer, int offset, int size) {
if (position >= data.limit()) {
return -1;
}
data.position((int) position);
int numBytesRead = Math.min(size, data.remaining());
data.get(buffer, offset, numBytesRead);
return numBytesRead;
}

@Override
public long getSize() {
return data.limit();
}

@Override
public void close() {}
});
}
}
}

0 comments on commit 9190698

Please sign in to comment.