From d2cbf1126450b37dad13bca5f2d6162793e611dc Mon Sep 17 00:00:00 2001 From: Nicholas Tinsley Date: Thu, 25 Jan 2024 13:32:03 -0500 Subject: [PATCH] Don't fail video send on postprocess failure. --- .../jobs/AttachmentCompressionJob.java | 18 +++++++++++++++++- .../securesms/video/InMemoryTranscoder.java | 2 +- .../Mp4FaststartPostProcessor.kt | 8 +++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentCompressionJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentCompressionJob.java index 5da1398c4e..b5750762a7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentCompressionJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentCompressionJob.java @@ -40,6 +40,7 @@ import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.MemoryFileDescriptor.MemoryFileException; import org.thoughtcrime.securesms.video.InMemoryTranscoder; import org.thoughtcrime.securesms.video.StreamingTranscoder; +import org.thoughtcrime.securesms.video.exceptions.VideoPostProcessingException; import org.thoughtcrime.securesms.video.interfaces.TranscoderCancelationSignal; import org.thoughtcrime.securesms.video.TranscoderOptions; import org.thoughtcrime.securesms.video.exceptions.VideoSourceException; @@ -287,7 +288,8 @@ public final class AttachmentCompressionJob extends BaseJob { try { return ModernDecryptingPartInputStream.createFor(attachmentSecret, file, 0); } catch (IOException e) { - throw new RuntimeException(e); + Log.w(TAG, "IOException thrown while creating CipherInputStream.", e); + throw new VideoPostProcessingException("Exception while opening InputStream!", e); } }); @@ -295,6 +297,20 @@ public final class AttachmentCompressionJob extends BaseJob { try (MediaStream mediaStream = new MediaStream(postProcessor.process(plaintextLength), MimeTypes.VIDEO_MP4, 0, 0, true)) { attachmentDatabase.updateAttachmentData(attachment, mediaStream, true); faststart = true; + } catch (VideoPostProcessingException e) { + Log.w(TAG, "Exception thrown during post processing.", e); + final Throwable cause = e.getCause(); + if (cause instanceof IOException) { + throw (IOException) cause; + } else if (cause instanceof EncodingException) { + throw (EncodingException) cause; + } + } + + if (!faststart) { + try (MediaStream mediaStream = new MediaStream(ModernDecryptingPartInputStream.createFor(attachmentSecret, file, 0), MimeTypes.VIDEO_MP4, 0, 0, false)) { + attachmentDatabase.updateAttachmentData(attachment, mediaStream, true); + } } } finally { if (!file.delete()) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/video/InMemoryTranscoder.java b/app/src/main/java/org/thoughtcrime/securesms/video/InMemoryTranscoder.java index dbb2226eac..438d426670 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/video/InMemoryTranscoder.java +++ b/app/src/main/java/org/thoughtcrime/securesms/video/InMemoryTranscoder.java @@ -185,7 +185,7 @@ public final class InMemoryTranscoder implements Closeable { final Throwable cause = e.getCause(); if (cause instanceof IOException) { throw (IOException) cause; - } else if ( cause instanceof EncodingException) { + } else if (cause instanceof EncodingException) { throw (EncodingException) cause; } } diff --git a/video/lib/src/main/java/org/thoughtcrime/securesms/video/postprocessing/Mp4FaststartPostProcessor.kt b/video/lib/src/main/java/org/thoughtcrime/securesms/video/postprocessing/Mp4FaststartPostProcessor.kt index a9b1353d65..127be2921d 100644 --- a/video/lib/src/main/java/org/thoughtcrime/securesms/video/postprocessing/Mp4FaststartPostProcessor.kt +++ b/video/lib/src/main/java/org/thoughtcrime/securesms/video/postprocessing/Mp4FaststartPostProcessor.kt @@ -8,6 +8,7 @@ package org.thoughtcrime.securesms.video.postprocessing import org.signal.core.util.readLength import org.signal.libsignal.media.Mp4Sanitizer import org.signal.libsignal.media.SanitizedMetadata +import org.thoughtcrime.securesms.video.exceptions.VideoPostProcessingException import java.io.ByteArrayInputStream import java.io.FilterInputStream import java.io.IOException @@ -26,7 +27,12 @@ class Mp4FaststartPostProcessor(private val inputStreamFactory: InputStreamFacto * It is the responsibility of the caller to close the resulting [InputStream]. */ fun process(inputLength: Long = calculateStreamLength(inputStreamFactory.create())): SequenceInputStream { - val metadata = sanitizeMetadata(inputStreamFactory.create(), inputLength) + val metadata = inputStreamFactory.create().use { inputStream -> + sanitizeMetadata(inputStream, inputLength) + } + if (metadata.sanitizedMetadata == null) { + throw VideoPostProcessingException("Sanitized metadata was null!") + } val inputStream = inputStreamFactory.create() inputStream.skip(metadata.dataOffset) return SequenceInputStream(ByteArrayInputStream(metadata.sanitizedMetadata), LimitedInputStream(inputStream, metadata.dataLength))