Skip to content

Commit 2ba7bda

Browse files
authored
Merge pull request #1 from cbernier2/feature/muted-audio-builder
Fix the usage of the MutedAudioDataSource in the TranscoderOptions bu…
2 parents dd56264 + bef8da4 commit 2ba7bda

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,7 @@ public static class Builder {
129129
@NonNull
130130
@SuppressWarnings("WeakerAccess")
131131
public Builder addDataSource(@NonNull DataSource dataSource) {
132-
if (dataSource.getTrackFormat(TrackType.AUDIO) == null && dataSource.getTrackFormat(TrackType.VIDEO) != null) {
133-
audioDataSources.add(new MutedAudioDataSource(dataSource.getDurationUs()));
134-
} else {
135-
audioDataSources.add(dataSource);
136-
}
132+
audioDataSources.add(dataSource);
137133
videoDataSources.add(dataSource);
138134
return this;
139135
}
@@ -322,6 +318,43 @@ public Builder setAudioResampler(@NonNull AudioResampler audioResampler) {
322318
return this;
323319
}
324320

321+
/**
322+
* Generates muted audio data sources if needed
323+
* @return The list of audio data sources including the muted sources
324+
*/
325+
private List<DataSource> buildAudioDataSources()
326+
{
327+
// Check if we have a mix of empty and non-empty data sources
328+
// This would cause an error in Engine::computeTrackStatus
329+
boolean hasMissingAudioDataSources = false;
330+
boolean hasAudioDataSources = false;
331+
boolean hasValidAudioDataSources = true;
332+
for (DataSource dataSource : audioDataSources) {
333+
if (dataSource.getTrackFormat(TrackType.AUDIO) == null) {
334+
hasMissingAudioDataSources = true;
335+
} else {
336+
hasAudioDataSources = true;
337+
}
338+
if (hasAudioDataSources && hasMissingAudioDataSources) {
339+
hasValidAudioDataSources = false;
340+
break;
341+
}
342+
}
343+
if (hasValidAudioDataSources) {
344+
return audioDataSources;
345+
}
346+
// Fix the audioDataSources by replacing the empty data source by muted data source
347+
List<DataSource> result = new ArrayList<>();
348+
for (DataSource dataSource : audioDataSources) {
349+
if (dataSource.getTrackFormat(TrackType.AUDIO) != null) {
350+
result.add(dataSource);
351+
} else {
352+
result.add(new MutedAudioDataSource(dataSource.getDurationUs()));
353+
}
354+
}
355+
return result;
356+
}
357+
325358
@NonNull
326359
public TranscoderOptions build() {
327360
if (listener == null) {
@@ -358,7 +391,7 @@ public TranscoderOptions build() {
358391
}
359392
TranscoderOptions options = new TranscoderOptions();
360393
options.listener = listener;
361-
options.audioDataSources = audioDataSources;
394+
options.audioDataSources = buildAudioDataSources();
362395
options.videoDataSources = videoDataSources;
363396
options.dataSink = dataSink;
364397
options.listenerHandler = listenerHandler;

0 commit comments

Comments
 (0)