ignore http upload file size constraints for videos that will be compressed

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/persistance/FileBackend.java                   | 32 
src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java | 12 
2 files changed, 38 insertions(+), 6 deletions(-)

Detailed changes

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 {
 
 	}
 

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<Void> 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();
 	}