Commit e8c86e1b by tofunmi Committed by Rohit Singh

Remove effect/SimpleBitmapLoader & replace with DataSourceBitmapLoader

PiperOrigin-RevId: 513824487
parent db540a02
......@@ -68,7 +68,6 @@ import com.google.android.exoplayer2.effect.RgbAdjustment;
import com.google.android.exoplayer2.effect.RgbFilter;
import com.google.android.exoplayer2.effect.RgbMatrix;
import com.google.android.exoplayer2.effect.ScaleAndRotateTransformation;
import com.google.android.exoplayer2.effect.SimpleBitmapLoader;
import com.google.android.exoplayer2.effect.SingleColorLut;
import com.google.android.exoplayer2.effect.TextOverlay;
import com.google.android.exoplayer2.effect.TextureOverlay;
......@@ -85,6 +84,7 @@ import com.google.android.exoplayer2.transformer.TransformationRequest;
import com.google.android.exoplayer2.transformer.Transformer;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.DataSourceBitmapLoader;
import com.google.android.exoplayer2.util.BitmapLoader;
import com.google.android.exoplayer2.util.DebugTextViewHelper;
import com.google.android.exoplayer2.util.DebugViewProvider;
......@@ -691,7 +691,7 @@ public final class TransformerActivity extends AppCompatActivity {
inputImageView.setVisibility(View.VISIBLE);
inputTextView.setText(getString(R.string.input_image));
BitmapLoader bitmapLoader = new SimpleBitmapLoader();
BitmapLoader bitmapLoader = new DataSourceBitmapLoader(getApplicationContext());
ListenableFuture<Bitmap> future = bitmapLoader.loadBitmap(uri);
try {
Bitmap bitmap = future.get();
......
......@@ -39,6 +39,7 @@ android {
dependencies {
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
implementation project(modulePrefix + 'library-common')
implementation project(modulePrefix + 'library-datasource')
compileOnly 'com.google.errorprone:error_prone_annotations:' + errorProneVersion
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkCompatVersion
......
......@@ -16,11 +16,14 @@
package com.google.android.exoplayer2.effect;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull;
import android.graphics.Bitmap;
import android.net.Uri;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import com.google.android.exoplayer2.upstream.DataSourceBitmapLoader;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.util.BitmapLoader;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Size;
......@@ -134,7 +137,10 @@ public abstract class BitmapOverlay extends TextureOverlay {
@Override
public Bitmap getBitmap(long presentationTimeUs) throws VideoFrameProcessingException {
if (lastBitmap == null) {
BitmapLoader bitmapLoader = new SimpleBitmapLoader();
BitmapLoader bitmapLoader =
new DataSourceBitmapLoader(
checkStateNotNull(DataSourceBitmapLoader.DEFAULT_EXECUTOR_SERVICE.get()),
new DefaultHttpDataSource.Factory());
ListenableFuture<Bitmap> future = bitmapLoader.loadBitmap(overlayBitmapUri);
try {
lastBitmap = future.get();
......
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.google.android.exoplayer2.effect;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.BitmapLoader;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// TODO(b/258685047): delete this copy once substitute is created in common
// (b/194284041, b/258658893)
/**
* A {@link SimpleBitmapLoader} that delegates all tasks to an executor and supports fetching images
* from URIs with {@code file}, {@code http} and {@code https} schemes.
*
* <p>Loading tasks are delegated to an {@link ExecutorService} (or {@link
* ListeningExecutorService}) defined during construction. If no executor service is defined, all
* tasks are delegated to a single-thread executor service that is shared between instances of this
* class.
*
* <p>For HTTP(S) transfers, this class reads a resource only when the endpoint responds with an
* {@code HTTP 200} after sending the HTTP request.
*/
public final class SimpleBitmapLoader implements BitmapLoader {
private static final String FILE_URI_EXCEPTION_MESSAGE = "Could not read image from file";
private static final Supplier<ListeningExecutorService> DEFAULT_EXECUTOR_SERVICE =
Suppliers.memoize(
() -> MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
private final ListeningExecutorService executorService;
/**
* Creates an instance that delegates all load tasks to a single-thread executor service shared
* between instances.
*/
public SimpleBitmapLoader() {
this(checkStateNotNull(DEFAULT_EXECUTOR_SERVICE.get()));
}
/** Creates an instance that delegates loading tasks to the {@code executorService}. */
public SimpleBitmapLoader(ExecutorService executorService) {
this.executorService = MoreExecutors.listeningDecorator(executorService);
}
@Override
public ListenableFuture<Bitmap> decodeBitmap(byte[] data) {
return executorService.submit(() -> decode(data));
}
@Override
public ListenableFuture<Bitmap> loadBitmap(Uri uri) {
return executorService.submit(() -> load(uri));
}
private static Bitmap decode(byte[] data) {
@Nullable Bitmap bitmap = BitmapFactory.decodeByteArray(data, /* offset= */ 0, data.length);
checkArgument(bitmap != null, "Could not decode image data");
return bitmap;
}
private static Bitmap load(Uri uri) throws IOException {
if ("file".equals(uri.getScheme())) {
@Nullable String path = uri.getPath();
if (path == null) {
throw new IllegalArgumentException(FILE_URI_EXCEPTION_MESSAGE);
}
@Nullable Bitmap bitmap = BitmapFactory.decodeFile(path);
if (bitmap == null) {
throw new IllegalArgumentException(FILE_URI_EXCEPTION_MESSAGE);
}
return bitmap;
}
URLConnection connection = new URL(uri.toString()).openConnection();
if (!(connection instanceof HttpURLConnection)) {
throw new UnsupportedOperationException("Unsupported scheme: " + uri.getScheme());
}
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.connect();
int responseCode = httpConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Invalid response status code: " + responseCode);
}
try (InputStream inputStream = httpConnection.getInputStream()) {
return decode(ByteStreams.toByteArray(inputStream));
}
}
}
......@@ -37,6 +37,7 @@ android {
dependencies {
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
implementation project(modulePrefix + 'library-datasource')
implementation project(modulePrefix + 'library-core')
implementation project(modulePrefix + 'library-effect')
compileOnly 'com.google.errorprone:error_prone_annotations:' + errorProneVersion
......
......@@ -54,7 +54,7 @@ public final class DefaultAssetLoaderFactory implements AssetLoader.Factory {
Codec.DecoderFactory decoderFactory,
boolean forceInterpretHdrAsSdr,
Clock clock) {
this.context = context;
this.context = context.getApplicationContext();
this.decoderFactory = decoderFactory;
this.forceInterpretHdrAsSdr = forceInterpretHdrAsSdr;
this.clock = clock;
......@@ -80,7 +80,7 @@ public final class DefaultAssetLoaderFactory implements AssetLoader.Factory {
boolean forceInterpretHdrAsSdr,
Clock clock,
MediaSource.Factory mediaSourceFactory) {
this.context = context;
this.context = context.getApplicationContext();
this.decoderFactory = decoderFactory;
this.forceInterpretHdrAsSdr = forceInterpretHdrAsSdr;
this.clock = clock;
......@@ -93,7 +93,7 @@ public final class DefaultAssetLoaderFactory implements AssetLoader.Factory {
MediaItem mediaItem = editedMediaItem.mediaItem;
if (isImage(mediaItem.localConfiguration)) {
if (imageAssetLoaderFactory == null) {
imageAssetLoaderFactory = new ImageAssetLoader.Factory();
imageAssetLoaderFactory = new ImageAssetLoader.Factory(context);
}
return imageAssetLoaderFactory.createAssetLoader(editedMediaItem, looper, listener);
}
......
......@@ -23,12 +23,13 @@ import static com.google.android.exoplayer2.transformer.Transformer.PROGRESS_STA
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Looper;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.effect.SimpleBitmapLoader;
import com.google.android.exoplayer2.upstream.DataSourceBitmapLoader;
import com.google.android.exoplayer2.util.BitmapLoader;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableMap;
......@@ -43,22 +44,30 @@ public final class ImageAssetLoader implements AssetLoader {
/** An {@link AssetLoader.Factory} for {@link ImageAssetLoader} instances. */
public static final class Factory implements AssetLoader.Factory {
private final Context context;
public Factory(Context context) {
this.context = context.getApplicationContext();
}
@Override
public AssetLoader createAssetLoader(
EditedMediaItem editedMediaItem, Looper looper, Listener listener) {
return new ImageAssetLoader(editedMediaItem, listener);
return new ImageAssetLoader(context, editedMediaItem, listener);
}
}
public static final String MIME_TYPE_IMAGE_ALL = MimeTypes.BASE_TYPE_IMAGE + "/*";
private final Context context;
private final EditedMediaItem editedMediaItem;
private final Listener listener;
private @Transformer.ProgressState int progressState;
private int progress;
private ImageAssetLoader(EditedMediaItem editedMediaItem, Listener listener) {
private ImageAssetLoader(Context context, EditedMediaItem editedMediaItem, Listener listener) {
this.context = context;
this.editedMediaItem = editedMediaItem;
this.listener = listener;
......@@ -69,7 +78,7 @@ public final class ImageAssetLoader implements AssetLoader {
public void start() {
progressState = PROGRESS_STATE_AVAILABLE;
listener.onTrackCount(1);
BitmapLoader bitmapLoader = new SimpleBitmapLoader();
BitmapLoader bitmapLoader = new DataSourceBitmapLoader(context);
MediaItem.LocalConfiguration localConfiguration =
checkNotNull(editedMediaItem.mediaItem.localConfiguration);
ListenableFuture<Bitmap> future = bitmapLoader.loadBitmap(localConfiguration.uri);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment