-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathTranscoderOptions.java
More file actions
447 lines (401 loc) · 16 KB
/
TranscoderOptions.java
File metadata and controls
447 lines (401 loc) · 16 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package com.otaliastudios.transcoder;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import com.otaliastudios.transcoder.engine.TrackType;
import com.otaliastudios.transcoder.io_factory.DecoderIOFactory;
import com.otaliastudios.transcoder.io_factory.DefaultDecoderIOFactory;
import com.otaliastudios.transcoder.resample.AudioResampler;
import com.otaliastudios.transcoder.resample.DefaultAudioResampler;
import com.otaliastudios.transcoder.sink.DataSink;
import com.otaliastudios.transcoder.sink.DefaultDataSink;
import com.otaliastudios.transcoder.source.DataSource;
import com.otaliastudios.transcoder.source.FileDescriptorDataSource;
import com.otaliastudios.transcoder.source.FilePathDataSource;
import com.otaliastudios.transcoder.source.BlankAudioDataSource;
import com.otaliastudios.transcoder.source.UriDataSource;
import com.otaliastudios.transcoder.strategy.DefaultAudioStrategy;
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategies;
import com.otaliastudios.transcoder.strategy.TrackStrategy;
import com.otaliastudios.transcoder.stretch.AudioStretcher;
import com.otaliastudios.transcoder.stretch.DefaultAudioStretcher;
import com.otaliastudios.transcoder.time.DefaultTimeInterpolator;
import com.otaliastudios.transcoder.time.SpeedTimeInterpolator;
import com.otaliastudios.transcoder.time.TimeInterpolator;
import com.otaliastudios.transcoder.validator.DefaultValidator;
import com.otaliastudios.transcoder.validator.Validator;
import java.io.FileDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
/**
* Collects transcoding options consumed by {@link Transcoder}.
*/
public class TranscoderOptions {
private TranscoderOptions() {}
private DataSink dataSink;
private DecoderIOFactory decoderIOFactory;
private List<DataSource> videoDataSources;
private List<DataSource> audioDataSources;
private TrackStrategy audioTrackStrategy;
private TrackStrategy videoTrackStrategy;
private Validator validator;
private int rotation;
private TimeInterpolator timeInterpolator;
private AudioStretcher audioStretcher;
private AudioResampler audioResampler;
TranscoderListener listener;
Handler listenerHandler;
@NonNull
public DataSink getDataSink() {
return dataSink;
}
@NonNull
public DecoderIOFactory getDecoderIOFactory() {
return decoderIOFactory;
}
@NonNull
public List<DataSource> getAudioDataSources() {
return audioDataSources;
}
@NonNull
public List<DataSource> getVideoDataSources() {
return videoDataSources;
}
@NonNull
public TrackStrategy getAudioTrackStrategy() {
return audioTrackStrategy;
}
@NonNull
public TrackStrategy getVideoTrackStrategy() {
return videoTrackStrategy;
}
@NonNull
public Validator getValidator() {
return validator;
}
public int getVideoRotation() {
return rotation;
}
@NonNull
public TimeInterpolator getTimeInterpolator() {
return timeInterpolator;
}
@NonNull
public AudioStretcher getAudioStretcher() {
return audioStretcher;
}
@NonNull
public AudioResampler getAudioResampler() {
return audioResampler;
}
public static class Builder {
private DataSink dataSink;
private DecoderIOFactory decoderIOFactory;
private final List<DataSource> audioDataSources = new ArrayList<>();
private final List<DataSource> videoDataSources = new ArrayList<>();
private TranscoderListener listener;
private Handler listenerHandler;
private TrackStrategy audioTrackStrategy;
private TrackStrategy videoTrackStrategy;
private Validator validator;
private int rotation;
private TimeInterpolator timeInterpolator;
private AudioStretcher audioStretcher;
private AudioResampler audioResampler;
Builder(@NonNull String outPath) {
this.dataSink = new DefaultDataSink(outPath);
}
@RequiresApi(api = Build.VERSION_CODES.O)
Builder(@NonNull FileDescriptor fileDescriptor) {
this.dataSink = new DefaultDataSink(fileDescriptor);
}
Builder(@NonNull DataSink dataSink) {
this.dataSink = dataSink;
}
@NonNull
@SuppressWarnings("WeakerAccess")
public Builder addDataSource(@NonNull DataSource dataSource) {
audioDataSources.add(dataSource);
videoDataSources.add(dataSource);
return this;
}
@NonNull
@SuppressWarnings("WeakerAccess")
public Builder addDataSource(@NonNull TrackType type, @NonNull DataSource dataSource) {
if (type == TrackType.AUDIO) {
audioDataSources.add(dataSource);
} else if (type == TrackType.VIDEO) {
videoDataSources.add(dataSource);
}
return this;
}
@NonNull
@SuppressWarnings("unused")
public Builder addDataSource(@NonNull FileDescriptor fileDescriptor) {
return addDataSource(new FileDescriptorDataSource(fileDescriptor));
}
@NonNull
@SuppressWarnings("unused")
public Builder addDataSource(@NonNull TrackType type, @NonNull FileDescriptor fileDescriptor) {
return addDataSource(type, new FileDescriptorDataSource(fileDescriptor));
}
@NonNull
@SuppressWarnings("unused")
public Builder addDataSource(@NonNull String inPath) {
return addDataSource(new FilePathDataSource(inPath));
}
@NonNull
@SuppressWarnings("unused")
public Builder addDataSource(@NonNull TrackType type, @NonNull String inPath) {
return addDataSource(type, new FilePathDataSource(inPath));
}
@NonNull
@SuppressWarnings({"unused", "UnusedReturnValue"})
public Builder addDataSource(@NonNull Context context, @NonNull Uri uri) {
return addDataSource(new UriDataSource(context, uri));
}
@NonNull
@SuppressWarnings({"unused", "UnusedReturnValue"})
public Builder addDataSource(@NonNull TrackType type, @NonNull Context context, @NonNull Uri uri) {
return addDataSource(type, new UriDataSource(context, uri));
}
/**
* Sets the audio output strategy. If absent, this defaults to
* {@link com.otaliastudios.transcoder.strategy.DefaultAudioStrategy}.
*
* @param trackStrategy the desired strategy
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setAudioTrackStrategy(@Nullable TrackStrategy trackStrategy) {
this.audioTrackStrategy = trackStrategy;
return this;
}
/**
* Sets the video output strategy. If absent, this defaults to the 16:9
* strategy returned by {@link DefaultVideoStrategies#for720x1280()}.
*
* @param trackStrategy the desired strategy
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setVideoTrackStrategy(@Nullable TrackStrategy trackStrategy) {
this.videoTrackStrategy = trackStrategy;
return this;
}
@NonNull
public Builder setListener(@NonNull TranscoderListener listener) {
this.listener = listener;
return this;
}
/**
* Sets an handler for {@link TranscoderListener} callbacks.
* If null, this will default to the thread that starts the transcoding, if it
* has a looper, or the UI thread otherwise.
*
* @param listenerHandler the thread to receive callbacks
* @return this for chaining
*/
@NonNull
@SuppressWarnings("WeakerAccess")
public Builder setListenerHandler(@Nullable Handler listenerHandler) {
this.listenerHandler = listenerHandler;
return this;
}
/**
* Sets a validator to understand whether the transcoding process should
* stop before being started, based on the tracks status. Will default to
* {@link com.otaliastudios.transcoder.validator.DefaultValidator}.
*
* @param validator the validator
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setValidator(@Nullable Validator validator) {
this.validator = validator;
return this;
}
/**
* The clockwise rotation to be applied to the input video frames.
* Defaults to 0, which leaves the input rotation unchanged.
*
* @param rotation either 0, 90, 180 or 270
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setVideoRotation(int rotation) {
this.rotation = rotation;
return this;
}
/**
* Sets a {@link TimeInterpolator} to change the frames timestamps - either video or
* audio or both - before they are written into the output file.
* Defaults to {@link com.otaliastudios.transcoder.time.DefaultTimeInterpolator}.
*
* @param timeInterpolator a time interpolator
* @return this for chaining
*/
@NonNull
@SuppressWarnings("WeakerAccess")
public Builder setTimeInterpolator(@NonNull TimeInterpolator timeInterpolator) {
this.timeInterpolator = timeInterpolator;
return this;
}
/**
* Shorthand for calling {@link #setTimeInterpolator(TimeInterpolator)}
* and passing a {@link com.otaliastudios.transcoder.time.SpeedTimeInterpolator}.
* This interpolator can modify the video speed by the given factor.
*
* @param speedFactor a factor, greather than 0
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setSpeed(float speedFactor) {
return setTimeInterpolator(new SpeedTimeInterpolator(speedFactor));
}
/**
* Sets an {@link AudioStretcher} to perform stretching of audio samples
* as a consequence of speed and time interpolator changes.
* Defaults to {@link DefaultAudioStretcher}.
*
* @param audioStretcher an audio stretcher
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setAudioStretcher(@NonNull AudioStretcher audioStretcher) {
this.audioStretcher = audioStretcher;
return this;
}
/**
* Sets an {@link AudioResampler} to change the sample rate of audio
* frames when sample rate conversion is needed. Upsampling is discouraged.
* Defaults to {@link DefaultAudioResampler}.
*
* @param audioResampler an audio resampler
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setAudioResampler(@NonNull AudioResampler audioResampler) {
this.audioResampler = audioResampler;
return this;
}
/**
* Sets an {@link DecoderIOFactory} to provide decoder input and output.
* Can be use to implement custom video postprocessing
* Defaults to {@link DefaultAudioResampler}.
*
* @param decoderIOFactory a decoder io factory
* @return this for chaining
*/
public Builder setDecoderIOFactory(DecoderIOFactory decoderIOFactory) {
this.decoderIOFactory = decoderIOFactory;
return this;
}
/**
* Generates muted audio data sources if needed
* @return The list of audio data sources including the muted sources
*/
private List<DataSource> buildAudioDataSources()
{
// Check if we have a mix of empty and non-empty data sources
// This would cause an error in Engine::computeTrackStatus
boolean hasMissingAudioDataSources = false;
boolean hasAudioDataSources = false;
boolean hasValidAudioDataSources = true;
for (DataSource dataSource : audioDataSources) {
if (dataSource.getTrackFormat(TrackType.AUDIO) == null) {
hasMissingAudioDataSources = true;
} else {
hasAudioDataSources = true;
}
if (hasAudioDataSources && hasMissingAudioDataSources) {
hasValidAudioDataSources = false;
break;
}
}
if (hasValidAudioDataSources) {
return audioDataSources;
}
// Fix the audioDataSources by replacing the empty data source by muted data source
List<DataSource> result = new ArrayList<>();
for (DataSource dataSource : audioDataSources) {
if (dataSource.getTrackFormat(TrackType.AUDIO) != null) {
result.add(dataSource);
} else {
result.add(new BlankAudioDataSource(dataSource.getDurationUs()));
}
}
return result;
}
@NonNull
public TranscoderOptions build() {
if (listener == null) {
throw new IllegalStateException("listener can't be null");
}
if (audioDataSources.isEmpty() && videoDataSources.isEmpty()) {
throw new IllegalStateException("we need at least one data source");
}
if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270) {
throw new IllegalArgumentException("Accepted values for rotation are 0, 90, 180, 270");
}
if (listenerHandler == null) {
Looper looper = Looper.myLooper();
if (looper == null) looper = Looper.getMainLooper();
listenerHandler = new Handler(looper);
}
if (audioTrackStrategy == null) {
audioTrackStrategy = DefaultAudioStrategy.builder().build();
}
if (videoTrackStrategy == null) {
videoTrackStrategy = DefaultVideoStrategies.for720x1280();
}
if (validator == null) {
validator = new DefaultValidator();
}
if (timeInterpolator == null) {
timeInterpolator = new DefaultTimeInterpolator();
}
if (audioStretcher == null) {
audioStretcher = new DefaultAudioStretcher();
}
if (audioResampler == null) {
audioResampler = new DefaultAudioResampler();
}
if (decoderIOFactory == null) {
decoderIOFactory = new DefaultDecoderIOFactory();
}
TranscoderOptions options = new TranscoderOptions();
options.listener = listener;
options.audioDataSources = buildAudioDataSources();
options.videoDataSources = videoDataSources;
options.dataSink = dataSink;
options.decoderIOFactory = decoderIOFactory;
options.listenerHandler = listenerHandler;
options.audioTrackStrategy = audioTrackStrategy;
options.videoTrackStrategy = videoTrackStrategy;
options.validator = validator;
options.rotation = rotation;
options.timeInterpolator = timeInterpolator;
options.audioStretcher = audioStretcher;
options.audioResampler = audioResampler;
return options;
}
@NonNull
public Future<Void> transcode() {
return Transcoder.getInstance().transcode(build());
}
}
}