Fix recorded video rotation

This commit is contained in:
Matéo Duparc 2021-10-12 14:22:35 +02:00
parent dc89c02b9f
commit 497c22edc1
Signed by untrusted user: hardcoresushi
GPG Key ID: 007F84120107191E
5 changed files with 18 additions and 5 deletions

View File

@ -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);
}

View File

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

View File

@ -464,6 +464,7 @@ public final class VideoCapture extends UseCase {
synchronized (mMuxerLock) {
mMuxer = new MediaMuxer(outputFileOptions.mWriter);
mMuxer.setOrientationHint(getRelativeRotation(attachedCamera));
}
mEndOfVideoStreamSignal.set(false);

View File

@ -1,6 +1,7 @@
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/display.h>
#include <jni.h>
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;
}

View File

@ -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"
}
}