From f0e6b2944a20144945e33f14324ef17733a019e8 Mon Sep 17 00:00:00 2001 From: Milan Stevanovic Date: Wed, 19 Mar 2025 09:29:04 -0700 Subject: [PATCH] Eliminate zero-sized samples from contributing to the audio track. Eliminating unnecessary and potentially counter-productive zero-sized samples from the audio trak. The Android MP4 multiplexer tends to add them at the very end of the audio stream. Their presence may negatively affect the declared audio stream duration, and pose further complications down the road. The changes are verified on Samsung A54 (Android 14) device. --- .../video/videoconverter/muxer/Mp4Writer.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/video/lib/src/main/java/org/thoughtcrime/securesms/video/videoconverter/muxer/Mp4Writer.java b/video/lib/src/main/java/org/thoughtcrime/securesms/video/videoconverter/muxer/Mp4Writer.java index 980cafdd3d..088f9facc5 100644 --- a/video/lib/src/main/java/org/thoughtcrime/securesms/video/videoconverter/muxer/Mp4Writer.java +++ b/video/lib/src/main/java/org/thoughtcrime/securesms/video/videoconverter/muxer/Mp4Writer.java @@ -277,7 +277,33 @@ final class Mp4Writer extends DefaultBoxes implements SampleSink { final @NonNull StreamingSample streamingSample, final @NonNull StreamingTrack streamingTrack) throws IOException { - + if (streamingSample.getContent().limit() == 0) { + // + // For currently unknown reason, the STSZ table of AAC audio stream + // related to the very last chunk comes with the extra table elements + // whose value is zero. + // + // The ISO MP4 spec does not absolutely prohibit such a case, but strongly + // stipulates that the stream has to have the inner logic to support + // the zero length audio frames (QCELP happens to be one such example). + // + // Spec excerpt: + // ---------------------------------------------------------------------- + // 8.7.3 Sample Size Boxes + // 8.7.3.1 Definition + // ... + // NOTE A sample size of zero is not prohibited in general, but it + // must be valid and defined for the coding system, as defined by + // the sample entry, that the sample belongs to + // ---------------------------------------------------------------------- + // + // In all other cases, having zero STSZ table values is very illogical + // and may pose the problems down the road. Here we will eliminate such + // samples from all the related bookkeeping + // + Log.d(TAG, "skipping zero-sized sample"); + return; + } TrackBox tb = trackBoxes.get(streamingTrack); if (tb == null) { tb = new TrackBox();