-
Notifications
You must be signed in to change notification settings - Fork 337
/
DefaultAssetLoaderFactory.java
177 lines (166 loc) · 6.82 KB
/
DefaultAssetLoaderFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* 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 androidx.media3.transformer;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.transformer.ImageUtil.getCommonImageMimeTypeFromExtension;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.ColorSpace;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.BitmapLoader;
import androidx.media3.common.util.Clock;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSourceBitmapLoader;
import androidx.media3.datasource.DefaultDataSource;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.transformer.AssetLoader.CompositionSettings;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.Objects;
import java.util.concurrent.Executors;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** The default {@link AssetLoader.Factory} implementation. */
@UnstableApi
public final class DefaultAssetLoaderFactory implements AssetLoader.Factory {
private final Context context;
private final Codec.DecoderFactory decoderFactory;
private final Clock clock;
@Nullable private final MediaSource.Factory mediaSourceFactory;
private final BitmapLoader bitmapLoader;
private AssetLoader.@MonotonicNonNull Factory imageAssetLoaderFactory;
private AssetLoader.@MonotonicNonNull Factory exoPlayerAssetLoaderFactory;
/**
* Creates an instance.
*
* <p>Uses {@link DataSourceBitmapLoader} to load images, setting the {@link
* android.graphics.BitmapFactory.Options#inPreferredColorSpace} to {@link
* android.graphics.ColorSpace.Named#SRGB} when possible.
*
* @param context The {@link Context}.
* @param decoderFactory The {@link Codec.DecoderFactory} to use to decode the samples (if
* necessary).
* @param clock The {@link Clock} to use. It should always be {@link Clock#DEFAULT}, except for
* testing.
*/
public DefaultAssetLoaderFactory(
Context context, Codec.DecoderFactory decoderFactory, Clock clock) {
this.context = context.getApplicationContext();
this.decoderFactory = decoderFactory;
this.clock = clock;
this.mediaSourceFactory = null;
@Nullable BitmapFactory.Options options = null;
if (Util.SDK_INT >= 26) {
options = new BitmapFactory.Options();
options.inPreferredColorSpace = ColorSpace.get(ColorSpace.Named.SRGB);
}
this.bitmapLoader =
new DataSourceBitmapLoader(
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()),
new DefaultDataSource.Factory(context),
options);
}
/**
* Creates an instance with the default {@link Clock} and {@link Codec.DecoderFactory}.
*
* <p>For multi-picture formats (e.g. gifs), a single image frame from the container is loaded.
* The frame loaded is determined by the {@link BitmapLoader} implementation.
*
* @param context The {@link Context}.
* @param bitmapLoader The {@link BitmapLoader} to use to load and decode images.
*/
public DefaultAssetLoaderFactory(Context context, BitmapLoader bitmapLoader) {
this.context = context.getApplicationContext();
this.decoderFactory = new DefaultDecoderFactory(context);
this.clock = Clock.DEFAULT;
this.mediaSourceFactory = null;
this.bitmapLoader = bitmapLoader;
}
/**
* Creates an instance.
*
* @param context The {@link Context}.
* @param decoderFactory The {@link Codec.DecoderFactory} to use to decode the samples (if
* necessary).
* @param clock The {@link Clock} to use. It should always be {@link Clock#DEFAULT}, except for
* testing.
* @param mediaSourceFactory The {@link MediaSource.Factory} to use to retrieve the samples to
* transform when an {@link ExoPlayerAssetLoader} is used.
* @param bitmapLoader The {@link BitmapLoader} to use to load and decode images.
*/
public DefaultAssetLoaderFactory(
Context context,
Codec.DecoderFactory decoderFactory,
Clock clock,
MediaSource.Factory mediaSourceFactory,
BitmapLoader bitmapLoader) {
this.context = context.getApplicationContext();
this.decoderFactory = decoderFactory;
this.clock = clock;
this.mediaSourceFactory = mediaSourceFactory;
this.bitmapLoader = bitmapLoader;
}
@Override
public AssetLoader createAssetLoader(
EditedMediaItem editedMediaItem,
Looper looper,
AssetLoader.Listener listener,
CompositionSettings compositionSettings) {
MediaItem mediaItem = editedMediaItem.mediaItem;
if (isImage(mediaItem.localConfiguration)) {
if (imageAssetLoaderFactory == null) {
imageAssetLoaderFactory = new ImageAssetLoader.Factory(bitmapLoader);
}
return imageAssetLoaderFactory.createAssetLoader(
editedMediaItem, looper, listener, compositionSettings);
}
if (exoPlayerAssetLoaderFactory == null) {
exoPlayerAssetLoaderFactory =
mediaSourceFactory != null
? new ExoPlayerAssetLoader.Factory(context, decoderFactory, clock, mediaSourceFactory)
: new ExoPlayerAssetLoader.Factory(context, decoderFactory, clock);
}
return exoPlayerAssetLoaderFactory.createAssetLoader(
editedMediaItem, looper, listener, compositionSettings);
}
private boolean isImage(@Nullable MediaItem.LocalConfiguration localConfiguration) {
if (localConfiguration == null) {
return false;
}
@Nullable String mimeType = localConfiguration.mimeType;
if (mimeType == null) {
if (Objects.equals(localConfiguration.uri.getScheme(), ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(localConfiguration.uri);
} else {
mimeType = getCommonImageMimeTypeFromExtension(localConfiguration.uri);
}
}
if (mimeType == null) {
return false;
}
if (!MimeTypes.isImage(mimeType)) {
return false;
}
checkState(
bitmapLoader.supportsMimeType(mimeType),
"Image format not supported by given bitmapLoader");
return true;
}
}