From df3f3920f543515f512b35b956fc81244301831b Mon Sep 17 00:00:00 2001 From: Christian Bernier Date: Mon, 2 Mar 2020 15:13:46 -0500 Subject: [PATCH 1/2] Fix the usage of the MutedAudioDataSource in the TranscoderOptions builder --- .../transcoder/TranscoderOptions.java | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java b/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java index 45cff249..f9481982 100644 --- a/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java +++ b/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java @@ -129,11 +129,7 @@ public static class Builder { @NonNull @SuppressWarnings("WeakerAccess") public Builder addDataSource(@NonNull DataSource dataSource) { - if (dataSource.getTrackFormat(TrackType.AUDIO) == null && dataSource.getTrackFormat(TrackType.VIDEO) != null) { - audioDataSources.add(new MutedAudioDataSource(dataSource.getDurationUs())); - } else { - audioDataSources.add(dataSource); - } + audioDataSources.add(dataSource); videoDataSources.add(dataSource); return this; } @@ -322,6 +318,43 @@ public Builder setAudioResampler(@NonNull AudioResampler audioResampler) { return this; } + /** + * Generates muted audio data sources if needed + * @return The list of audio data sources including the muted sources + */ + private List buildAudioDataSources() + { + // Check if we have a mix of empty and non-empty data sources + // This would cause an error in Engine::computeTrackStatus + boolean hasEmptyAudioDataSources = false; + int i; + for (i = 0; i < audioDataSources.size(); i++) { + DataSource dataSource = audioDataSources.get(i); + if (dataSource.getTrackFormat(TrackType.AUDIO) == null) { + if (i == 0 || hasEmptyAudioDataSources) { + hasEmptyAudioDataSources = true; + } else { + break; + } + } else if (hasEmptyAudioDataSources) { + break; + } + } + if (i == audioDataSources.size()) { + return audioDataSources; + } + // Fix the audioDataSources by replacing the empty data source by muted data source + List result = new ArrayList<>(); + for (DataSource dataSource : audioDataSources) { + if (dataSource.getTrackFormat(TrackType.AUDIO) != null) { + result.add(dataSource); + } else { + result.add(new MutedAudioDataSource(dataSource.getDurationUs())); + } + } + return result; + } + @NonNull public TranscoderOptions build() { if (listener == null) { @@ -358,7 +391,7 @@ public TranscoderOptions build() { } TranscoderOptions options = new TranscoderOptions(); options.listener = listener; - options.audioDataSources = audioDataSources; + options.audioDataSources = buildAudioDataSources(); options.videoDataSources = videoDataSources; options.dataSink = dataSink; options.listenerHandler = listenerHandler; From bef8da43cc0aa06088bbc2eb35abad2596a0595a Mon Sep 17 00:00:00 2001 From: Christian Bernier Date: Mon, 2 Mar 2020 15:52:50 -0500 Subject: [PATCH 2/2] Makes the buildAudioDataSources function clearer --- .../transcoder/TranscoderOptions.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java b/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java index f9481982..172adc27 100644 --- a/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java +++ b/lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java @@ -326,21 +326,21 @@ private List buildAudioDataSources() { // Check if we have a mix of empty and non-empty data sources // This would cause an error in Engine::computeTrackStatus - boolean hasEmptyAudioDataSources = false; - int i; - for (i = 0; i < audioDataSources.size(); i++) { - DataSource dataSource = audioDataSources.get(i); + boolean hasMissingAudioDataSources = false; + boolean hasAudioDataSources = false; + boolean hasValidAudioDataSources = true; + for (DataSource dataSource : audioDataSources) { if (dataSource.getTrackFormat(TrackType.AUDIO) == null) { - if (i == 0 || hasEmptyAudioDataSources) { - hasEmptyAudioDataSources = true; - } else { - break; - } - } else if (hasEmptyAudioDataSources) { + hasMissingAudioDataSources = true; + } else { + hasAudioDataSources = true; + } + if (hasAudioDataSources && hasMissingAudioDataSources) { + hasValidAudioDataSources = false; break; } } - if (i == audioDataSources.size()) { + if (hasValidAudioDataSources) { return audioDataSources; } // Fix the audioDataSources by replacing the empty data source by muted data source