From 4e864935fe9a0633a749c85a0a4826c2eb96fcf0 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 18 Sep 2017 22:42:25 +0200 Subject: [PATCH] ignore http upload file size constraints for videos that will be compressed --- .../persistance/FileBackend.java | 32 +++++++++++++++++-- .../AttachFileToConversationRunnable.java | 12 +++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index d74487a34..ce2a0f84a 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -152,6 +152,18 @@ public class FileBackend { return true; //exception to be compatible with HTTP Upload < v0.2 } for(Uri uri : uris) { + String mime = context.getContentResolver().getType(uri); + if (mime != null && mime.startsWith("video/")) { + try { + Dimensions dimensions = FileBackend.getVideoDimensions(context,uri); + if (dimensions.getMin() > 720) { + Log.d(Config.LOGTAG,"do not consider video file with min width larger than 720 for size check"); + continue; + } + } catch (NotAVideoFile notAVideoFile) { + //ignore and fall through + } + } if (FileBackend.getFileSize(context, uri) > max) { Log.d(Config.LOGTAG,"not all files are under "+max+" bytes. suggesting falling back to jingle"); return false; @@ -815,6 +827,16 @@ public class FileBackend { } catch (Exception e) { throw new NotAVideoFile(); } + return getVideoDimensions(metadataRetriever); + } + + private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile { + MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); + mediaMetadataRetriever.setDataSource(context,uri); + return getVideoDimensions(mediaMetadataRetriever); + } + + private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever) throws NotAVideoFile { String hasVideo = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO); if (hasVideo == null) { throw new NotAVideoFile(); @@ -840,7 +862,7 @@ public class FileBackend { return rotated ? new Dimensions(width, height) : new Dimensions(height, width); } - private int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) { + private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) { int rotation; if (Build.VERSION.SDK_INT >= 17) { String r = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); @@ -855,7 +877,7 @@ public class FileBackend { return rotation; } - private class Dimensions { + private static class Dimensions { public final int width; public final int height; @@ -863,9 +885,13 @@ public class FileBackend { this.width = width; this.height = height; } + + public int getMin() { + return Math.min(width,height); + } } - private class NotAVideoFile extends Exception { + private static class NotAVideoFile extends Exception { } diff --git a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java index aae41e453..f68056be8 100644 --- a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java +++ b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java @@ -11,6 +11,8 @@ import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets; import java.io.FileDescriptor; import java.io.FileNotFoundException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger; import eu.siacs.conversations.Config; @@ -78,11 +80,15 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); final int runtime = mXmppConnectionService.getFileBackend().getMediaRuntime(uri); MediaFormatStrategy formatStrategy = runtime >= 8000 ? MediaFormatStrategyPresets.createExportPreset960x540Strategy() : MediaFormatStrategyPresets.createAndroid720pStrategy(); - Log.d(Config.LOGTAG,"runtime "+runtime); file.getParentFile().mkdirs(); ParcelFileDescriptor parcelFileDescriptor = mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r"); FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); - MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), formatStrategy, this); + Future future = MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), formatStrategy, this); + try { + future.get(); + } catch (Exception e) { + throw new AssertionError(e); + } } @Override @@ -114,7 +120,7 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod @Override public void onTranscodeFailed(Exception e) { mXmppConnectionService.stopForcingForegroundNotification(); - Log.d(Config.LOGTAG,"video transcoding failed "+e.getMessage()); + Log.d(Config.LOGTAG,"video transcoding failed",e); processAsFile(); }