From 497c22edc176067a51b2a0ede1a0fb2ee6983bdf Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 12 Oct 2021 14:22:35 +0200 Subject: [PATCH] Fix recorded video rotation --- app/proguard-rules.pro | 4 ++++ .../hardcore/droidfs/video_recording/MediaMuxer.kt | 6 ++++-- .../hardcore/droidfs/video_recording/VideoCapture.java | 1 + app/src/main/native/libmux.c | 10 ++++++++-- build.gradle | 2 +- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 3407e44..4c8b01d 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -25,3 +25,7 @@ -keepclassmembers class sushi.hardcore.droidfs.explorers.ExplorerElement { static sushi.hardcore.droidfs.explorers.ExplorerElement new(...); } +-keepclassmembers class sushi.hardcore.droidfs.video_recording.MediaMuxer { + void writePacket(byte[]); + void seek(long); +} \ No newline at end of file diff --git a/app/src/main/java/sushi/hardcore/droidfs/video_recording/MediaMuxer.kt b/app/src/main/java/sushi/hardcore/droidfs/video_recording/MediaMuxer.kt index b82efc7..2cd6d35 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/video_recording/MediaMuxer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/video_recording/MediaMuxer.kt @@ -6,7 +6,7 @@ import java.nio.ByteBuffer class MediaMuxer(val writer: SeekableWriter) { external fun allocContext(): Long - external fun addVideoTrack(formatContext: Long, bitrate: Int, width: Int, height: Int): Int + external fun addVideoTrack(formatContext: Long, bitrate: Int, width: Int, height: Int, orientationHint: Int): Int external fun addAudioTrack(formatContext: Long, bitrate: Int, sampleRate: Int, channelCount: Int): Int external fun writeHeaders(formatContext: Long): Int external fun writePacket(formatContext: Long, buffer: ByteArray, pts: Long, streamIndex: Int, isKeyFrame: Boolean) @@ -20,6 +20,7 @@ class MediaMuxer(val writer: SeekableWriter) { var formatContext: Long? + var orientationHint = 0 var realVideoTrackIndex: Int? = null var audioFrameSize: Int? = null var firstPts: Long? = null @@ -63,7 +64,8 @@ class MediaMuxer(val writer: SeekableWriter) { formatContext!!, bitrate, format.getInteger("width"), - format.getInteger("height") + format.getInteger("height"), + orientationHint ) VIDEO_TRACK_INDEX } diff --git a/app/src/main/java/sushi/hardcore/droidfs/video_recording/VideoCapture.java b/app/src/main/java/sushi/hardcore/droidfs/video_recording/VideoCapture.java index d255f3d..e0d4f96 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/video_recording/VideoCapture.java +++ b/app/src/main/java/sushi/hardcore/droidfs/video_recording/VideoCapture.java @@ -464,6 +464,7 @@ public final class VideoCapture extends UseCase { synchronized (mMuxerLock) { mMuxer = new MediaMuxer(outputFileOptions.mWriter); + mMuxer.setOrientationHint(getRelativeRotation(attachedCamera)); } mEndOfVideoStreamSignal.set(false); diff --git a/app/src/main/native/libmux.c b/app/src/main/native/libmux.c index 56f84da..f83653f 100644 --- a/app/src/main/native/libmux.c +++ b/app/src/main/native/libmux.c @@ -1,6 +1,7 @@ #include #include #include +#include #include const size_t BUFF_SIZE = 4096; @@ -68,14 +69,19 @@ Java_sushi_hardcore_droidfs_video_1recording_MediaMuxer_addAudioTrack(JNIEnv *en } JNIEXPORT jint JNICALL -Java_sushi_hardcore_droidfs_video_1recording_MediaMuxer_addVideoTrack(JNIEnv *env, jobject thiz, jlong format_context, jint bitrate, jint width, - jint height) { +Java_sushi_hardcore_droidfs_video_1recording_MediaMuxer_addVideoTrack(JNIEnv *env, jobject thiz, + jlong format_context, + jint bitrate, jint width, + jint height, + jint orientation_hint) { AVStream* stream = avformat_new_stream((AVFormatContext *) format_context, NULL); stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; stream->codecpar->codec_id = AV_CODEC_ID_H264; stream->codecpar->bit_rate = bitrate; stream->codecpar->width = width; stream->codecpar->height = height; + uint8_t* matrix = av_stream_new_side_data(stream, AV_PKT_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9); + av_display_rotation_set((int32_t *) matrix, orientation_hint); return stream->index; } diff --git a/build.gradle b/build.gradle index 6ea876b..53a9f1d 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.2' + classpath 'com.android.tools.build:gradle:7.0.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }