Add video compression selector

licaon-kter created

Change summary

src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java | 21 
src/main/java/eu/siacs/conversations/utils/Android1080pFormatStrategy.java          | 69 
src/main/java/eu/siacs/conversations/utils/Android720pFormatStrategy.java           | 10 
src/main/res/values/arrays.xml                                                      | 12 
src/main/res/values/defaults.xml                                                    |  1 
src/main/res/values/strings.xml                                                     |  5 
src/main/res/xml/preferences.xml                                                    |  7 
7 files changed, 117 insertions(+), 8 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java 🔗

@@ -24,7 +24,8 @@ import eu.siacs.conversations.entities.Message;
 import eu.siacs.conversations.persistance.FileBackend;
 import eu.siacs.conversations.ui.UiCallback;
 import eu.siacs.conversations.utils.Android360pFormatStrategy;
-import eu.siacs.conversations.utils.Android480pFormatStrategy;
+import eu.siacs.conversations.utils.Android720pFormatStrategy;
+import eu.siacs.conversations.utils.Android1080pFormatStrategy;
 import eu.siacs.conversations.utils.MimeUtils;
 
 public class AttachFileToConversationRunnable implements Runnable, MediaTranscoder.Listener {
@@ -91,8 +92,22 @@ public class AttachFileToConversationRunnable implements Runnable, MediaTranscod
 		mXmppConnectionService.startForcingForegroundNotification();
 		message.setRelativeFilePath(message.getUuid() + ".mp4");
 		final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
-		final int runtime = mXmppConnectionService.getFileBackend().getMediaRuntime(uri);
-		final MediaFormatStrategy formatStrategy = runtime >= 20000 ? new Android360pFormatStrategy() : new Android480pFormatStrategy();
+		final MediaFormatStrategy formatStrategy;
+		final String compressVideo = mXmppConnectionService.getResources().getString(R.string.video_compression);
+                    switch (compressVideo) {
+                            case "720":
+                                formatStrategy = new Android720pFormatStrategy();
+								Log.d(Config.LOGTAG,"WOOOMP 720 dar " + compressVideo);
+                                break;
+                            case "1080":
+                                formatStrategy = new Android1080pFormatStrategy();
+								Log.d(Config.LOGTAG,"WOOOMP 1080 dar " + compressVideo);
+                                break;
+                            default:
+                                formatStrategy = new Android360pFormatStrategy();
+								Log.d(Config.LOGTAG,"WOOOMP 360 dar" + compressVideo);
+                                break;
+                    } 
 		file.getParentFile().mkdirs();
 		final ParcelFileDescriptor parcelFileDescriptor = mXmppConnectionService.getContentResolver().openFileDescriptor(uri, "r");
 		if (parcelFileDescriptor == null) {

src/main/java/eu/siacs/conversations/utils/Android1080pFormatStrategy.java 🔗

@@ -0,0 +1,69 @@
+package eu.siacs.conversations.utils;
+
+import android.media.MediaCodecInfo;
+import android.media.MediaFormat;
+import android.util.Log;
+
+import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants;
+import net.ypresto.androidtranscoder.format.MediaFormatStrategy;
+import net.ypresto.androidtranscoder.format.OutputFormatUnavailableException;
+
+import eu.siacs.conversations.Config;
+
+public class Android1080pFormatStrategy implements MediaFormatStrategy {
+
+    private static final int LONGER_LENGTH = 1920;
+    private static final int SHORTER_LENGTH = 1080;
+    private static final int DEFAULT_VIDEO_BITRATE = 4000 * 1000;
+    private static final int DEFAULT_AUDIO_BITRATE = 128 * 1000;
+    private final int mVideoBitrate;
+    private final int mAudioBitrate;
+    private final int mAudioChannels;
+
+    public Android1080pFormatStrategy() {
+        mVideoBitrate = DEFAULT_VIDEO_BITRATE;
+        mAudioBitrate = DEFAULT_AUDIO_BITRATE;
+        mAudioChannels = 2;
+    }
+
+    @Override
+    public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
+        int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
+        int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
+        int longer, shorter, outWidth, outHeight;
+        if (width >= height) {
+            longer = width;
+            shorter = height;
+            outWidth = LONGER_LENGTH;
+            outHeight = SHORTER_LENGTH;
+        } else {
+            shorter = width;
+            longer = height;
+            outWidth = SHORTER_LENGTH;
+            outHeight = LONGER_LENGTH;
+        }
+        if (longer * 9 != shorter * 16) {
+            throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
+        }
+        if (shorter <= SHORTER_LENGTH) {
+            Log.d(Config.LOGTAG, "This video is less or equal to 1080p, pass-through. (" + width + "x" + height + ")");
+            return null;
+        }
+        MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);
+        // From Nexus 4 Camera in 720p
+        format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate);
+        format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
+        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
+        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
+        return format;
+    }
+
+    @Override
+    public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
+        final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC, inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
+        format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
+        format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
+        return format;
+    }
+
+}

src/main/java/eu/siacs/conversations/utils/Android480pFormatStrategy.java → src/main/java/eu/siacs/conversations/utils/Android720pFormatStrategy.java 🔗

@@ -10,17 +10,17 @@ import net.ypresto.androidtranscoder.format.OutputFormatUnavailableException;
 
 import eu.siacs.conversations.Config;
 
-public class Android480pFormatStrategy implements MediaFormatStrategy {
+public class Android720pFormatStrategy implements MediaFormatStrategy {
 
-    private static final int LONGER_LENGTH = 854;
-    private static final int SHORTER_LENGTH = 480;
+    private static final int LONGER_LENGTH = 1280;
+    private static final int SHORTER_LENGTH = 720;
     private static final int DEFAULT_VIDEO_BITRATE = 2000 * 1000; // 2000 kbit/s upper range of what YouTube recommends
     private static final int DEFAULT_AUDIO_BITRATE = 96 * 1000;
     private final int mVideoBitrate;
     private final int mAudioBitrate;
     private final int mAudioChannels;
 
-    public Android480pFormatStrategy() {
+    public Android720pFormatStrategy() {
         mVideoBitrate = DEFAULT_VIDEO_BITRATE;
         mAudioBitrate = DEFAULT_AUDIO_BITRATE;
         mAudioChannels = 2;
@@ -46,7 +46,7 @@ public class Android480pFormatStrategy implements MediaFormatStrategy {
             throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
         }
         if (shorter <= SHORTER_LENGTH) {
-            Log.d(Config.LOGTAG, "This video is less or equal to 360p, pass-through. (" + width + "x" + height + ")");
+            Log.d(Config.LOGTAG, "This video is less or equal to 720p, pass-through. (" + width + "x" + height + ")");
             return null;
         }
         MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);

src/main/res/values/arrays.xml 🔗

@@ -112,4 +112,16 @@
 		<item>@string/medium</item>
 		<item>@string/large</item>
 	</string-array>
+
+	<string-array name="video_compression_values">
+		<item>360</item>
+		<item>720</item>
+		<item>1080</item>
+	</string-array>
+
+	<string-array name="video_compression_entries">
+		<item>@string/video_360p</item>
+		<item>@string/video_720p</item>
+		<item>@string/video_1080p</item>
+	</string-array>
 </resources>

src/main/res/values/defaults.xml 🔗

@@ -42,4 +42,5 @@
     <string name="default_font_size">small</string>
     <bool name="use_share_location_plugin">false</bool>
     <bool name="start_searching">false</bool>
+    <string name="video_compression">360</string>
 </resources>

src/main/res/values/strings.xml 🔗

@@ -740,4 +740,9 @@
     <string name="media_browser">Media browser</string>
     <string name="export_channel_name">History export</string>
     <string name="security_violation_not_attaching_file">File omitted due to security violation.</string>
+    <string name="pref_video_compression">Compress Videos</string>
+    <string name="pref_video_compression_summary">Resize and compress videos</string>
+    <string name="video_360p">SD (360p)</string>
+    <string name="video_720p">HD (720p)</string>
+    <string name="video_1080p">Full HD (1080p)</string>
 </resources>

src/main/res/xml/preferences.xml 🔗

@@ -137,6 +137,13 @@
             android:key="picture_compression"
             android:summary="@string/pref_picture_compression_summary"
             android:title="@string/pref_picture_compression" />
+        <ListPreference
+            android:defaultValue="@string/video_compression"
+            android:entries="@array/video_compression_entries"
+            android:entryValues="@array/video_compression_values"
+            android:key="video_compression"
+            android:summary="@string/pref_video_compression_summary"
+            android:title="@string/pref_video_compression" />
         <CheckBoxPreference
             android:defaultValue="@bool/use_share_location_plugin"
             android:key="use_share_location_plugin"