Skip to content

Commit

Permalink
feat: add mp4 header description
Browse files Browse the repository at this point in the history
  • Loading branch information
wlanjie committed May 13, 2020
1 parent dc262ed commit 2aaeb30
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 28 deletions.
6 changes: 4 additions & 2 deletions library/src/main/cpp/camera/camera_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ void CameraRecord::StartEncoding(const char* path,
bool use_hard_encode,
int audio_sample_rate,
int audio_channel,
int audio_bit_rate) {
int audio_bit_rate,
const char* tag) {
LOGI("StartEncoding enter width: %d height: %d videoBitRate: %d frameRate: %f useHard: %d audio_sample_rate: %d audio_channel: %d audio_bit_rate: %d",
width, height, video_bit_rate, frame_rate, use_hard_encode, audio_sample_rate, audio_channel, audio_bit_rate);

Expand All @@ -676,8 +677,9 @@ void CameraRecord::StartEncoding(const char* path,
PacketPool::GetInstance()->InitAudioPacketQueue(44100);
AudioPacketPool::GetInstance()->InitAudioPacketQueue();
std::string audio_codec_name("libfdk_aac");
std::string tag_name(tag);
int ret = packet_thread_->Init(path, video_width_, video_height_, frame_rate,
video_bit_rate * 1000, audio_sample_rate, audio_channel, audio_bit_rate * 1000, audio_codec_name);
video_bit_rate * 1000, audio_sample_rate, audio_channel, audio_bit_rate * 1000, audio_codec_name, tag_name);
if (ret >= 0) {
packet_thread_->StartAsync();
}
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/cpp/camera/camera_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class CameraRecord : public Handler, public FaceDetection {
bool use_hard_encode,
int audio_sample_rate,
int audio_channel,
int audio_bit_rate);
int audio_bit_rate,
const char* tag);

void StopEncoding();

Expand Down
6 changes: 4 additions & 2 deletions library/src/main/cpp/editor/video_export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ void VideoExport::CreateEncode(bool media_codec_encode) {
int VideoExport::Export(const char *export_config, const char *path,
int width, int height, int frame_rate, int video_bit_rate,
int sample_rate, int channel_count, int audio_bit_rate,
bool media_codec_decode, bool media_codec_encode) {
bool media_codec_decode, bool media_codec_encode,
const char* tag_name) {
LOGI("enter %s path: %s width: %d height: %d frame_rate: %d video_bit_rate: %d sample_rate: %d channel_count: %d audio_bit_rate: %d media_codec_decode: %d media_codec_encode: %d",
__func__, path, width, height, frame_rate, video_bit_rate, sample_rate, channel_count, audio_bit_rate, media_codec_decode, media_codec_encode);
FILE* file = fopen(export_config, "r");
Expand Down Expand Up @@ -439,7 +440,8 @@ int VideoExport::Export(const char *export_config, const char *path,
vocal_sample_rate_ = sample_rate;
packet_thread_ = new VideoConsumerThread();
std::string audio_codec_name("libfdk_aac");
int ret = packet_thread_->Init(path, width, height, frame_rate, video_bit_rate * 1000, sample_rate, channel_count, audio_bit_rate * 1000, audio_codec_name);
std::string tag(tag_name);
int ret = packet_thread_->Init(path, width, height, frame_rate, video_bit_rate * 1000, sample_rate, channel_count, audio_bit_rate * 1000, audio_codec_name, tag);
if (ret < 0) {
return ret;
}
Expand Down
9 changes: 2 additions & 7 deletions library/src/main/cpp/editor/video_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ extern "C" {

namespace trinity {

enum {
kStartNextExport
};

class VideoExportHandler;

class VideoExport {
public:
VideoExport(JNIEnv* env, jobject object);
Expand All @@ -60,7 +54,8 @@ class VideoExport {
int Export(const char* export_config, const char* path,
int width, int height, int frame_rate, int video_bit_rate,
int sample_rate, int channel_count, int audio_bit_rate,
bool media_codec_decode, bool media_codec_encode);
bool media_codec_decode, bool media_codec_encode,
const char* tag_name);

void Cancel();
private:
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/cpp/mp4/mp4_muxer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Mp4Muxer::Mp4Muxer()
Mp4Muxer::~Mp4Muxer() {}

int Mp4Muxer::Init(const char *path, int video_width, int video_height, int frame_rate, int video_bit_rate,
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name) {
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name,
std::string& tag_name) {
duration_ = 0;
format_context_ = nullptr;
video_stream_ = nullptr;
Expand All @@ -77,6 +78,7 @@ int Mp4Muxer::Init(const char *path, int video_width, int video_height, int fram
if (format_context_ == nullptr) {
return 0;
}
av_dict_set(&format_context_->metadata, "description", tag_name.c_str(), 0);
ret = BuildVideoStream();
if (ret != 0) {
LOGE("add video stream error: %d", ret);
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/cpp/mp4/mp4_muxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Mp4Muxer {
Mp4Muxer();
virtual ~Mp4Muxer();
virtual int Init(const char* path, int video_width, int video_height, int frame_rate, int video_bit_rate,
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name);
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name,
std::string& tag_name);

virtual void RegisterAudioPacketCallback(int (*audio_packet)(AudioPacket**, void* context), void* context);
virtual void RegisterVideoPacketCallback(int (*video_packet)(VideoPacket**, void* context), void* context);
Expand Down
7 changes: 5 additions & 2 deletions library/src/main/cpp/thread/video_consumer_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ int VideoConsumerThread::GetAudioPacket(AudioPacket** packet) {
}

int VideoConsumerThread::Init(const char* path, int video_width, int video_height, int frame_rate, int video_bit_Rate,
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name) {
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name,
std::string& tag_name) {
Init();
if (nullptr == mp4_muxer_) {
mp4_muxer_ = new Mp4Muxer();
int ret = mp4_muxer_->Init(path, video_width, video_height, frame_rate, video_bit_Rate, audio_sample_rate, audio_channels, audio_bit_rate, audio_codec_name);
int ret = mp4_muxer_->Init(path, video_width, video_height, frame_rate, video_bit_Rate,
audio_sample_rate, audio_channels, audio_bit_rate, audio_codec_name,
tag_name);
if (ret < 0) {
Release();
return ret;
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/cpp/thread/video_consumer_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class VideoConsumerThread {
~VideoConsumerThread();

int Init(const char* path, int video_width, int video_height, int frame_rate, int video_bit_Rate,
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name);
int audio_sample_rate, int audio_channels, int audio_bit_rate, std::string& audio_codec_name,
std::string& tag_name);

void Start();
void StartAsync();
Expand Down
18 changes: 12 additions & 6 deletions library/src/main/cpp/trinity_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,21 @@ static void
Android_JNI_record_start(JNIEnv *env, jobject object, jlong handle,
jstring path, jint width, jint height, jint video_bit_rate,
jint frame_rate, jboolean use_hard_encode,
jint audio_sample_rate, jint audio_channel, jint audio_bit_rate) {
jint audio_sample_rate, jint audio_channel, jint audio_bit_rate,
jstring tag) {
if (handle <= 0) {
return;
}
const char* output = env->GetStringUTFChars(path, JNI_FALSE);
const char* tag_name = env->GetStringUTFChars(tag, JNI_FALSE);
auto *record = reinterpret_cast<CameraRecord *>(handle);
record->StartEncoding(
output,
width, height, video_bit_rate, frame_rate,
use_hard_encode,
audio_sample_rate, audio_channel, audio_bit_rate);
audio_sample_rate, audio_channel, audio_bit_rate, tag_name);
env->ReleaseStringUTFChars(path, output);
env->ReleaseStringUTFChars(tag, tag_name);
}

static void Android_JNI_record_stop(JNIEnv *env, jobject object, jlong handle) {
Expand Down Expand Up @@ -684,19 +687,22 @@ static jint Android_JNI_video_export_export(JNIEnv* env, jobject object,
jstring export_path, jint width, jint height,
jint frame_rate, jint video_bit_rate,
jint sample_rate, jint channel_count, jint audio_bit_rate,
jboolean media_codec_decode, jboolean media_codec_encode) {
jboolean media_codec_decode, jboolean media_codec_encode,
jstring tag) {
if (handle <= 0) {
return 0;
}
auto* video_export = reinterpret_cast<VideoExport*>(handle);
const char* config = env->GetStringUTFChars(export_config, JNI_FALSE);
const char* path = env->GetStringUTFChars(export_path, JNI_FALSE);
const char* tag_name = env->GetStringUTFChars(tag, JNI_FALSE);
int result = video_export->Export(config, path,
width, height, frame_rate, video_bit_rate,
sample_rate, channel_count, audio_bit_rate,
media_codec_decode, media_codec_encode);
media_codec_decode, media_codec_encode, tag_name);
env->ReleaseStringUTFChars(export_path, path);
env->ReleaseStringUTFChars(export_config, config);
env->ReleaseStringUTFChars(tag, tag_name);
return result;
}

Expand Down Expand Up @@ -729,7 +735,7 @@ static JNINativeMethod recordMethods[] = {
{"updateTextureMatrix", "(J[F)V", (void **) Android_JNI_updateTextureMatrix},
{"destroyWindowSurface", "(J)V", (void **) Android_JNI_destroyWindowSurface},
{"destroyEGLContext", "(J)V", (void **) Android_JNI_destroyEGLContext},
{"startEncode", "(JLjava/lang/String;IIIIZIII)V", (void **) Android_JNI_record_start},
{"startEncode", "(JLjava/lang/String;IIIIZIIILjava/lang/String;)V", (void **) Android_JNI_record_start },
{"stopEncode", "(J)V", (void **) Android_JNI_record_stop},
{"addFilter", "(JLjava/lang/String;)I", (void **) Android_JNI_record_addFilter},
{"updateFilter", "(JLjava/lang/String;III)V", (void **) Android_JNI_record_updateFilter },
Expand Down Expand Up @@ -794,7 +800,7 @@ static JNINativeMethod videoEditorMethods[] = {

static JNINativeMethod videoExportMethods[] = {
{"create", "()J", (void **) Android_JNI_video_export_create },
{"export", "(JLjava/lang/String;Ljava/lang/String;IIIIIIIZZ)I", (void **) Android_JNI_video_export_export },
{"export", "(JLjava/lang/String;Ljava/lang/String;IIIIIIIZZLjava/lang/String;)I", (void **) Android_JNI_video_export_export },
{"cancel", "(J)V", (void **) Android_JNI_video_export_cancel },
{"release", "(J)V", (void **) Android_JNI_video_export_release }
};
Expand Down
22 changes: 20 additions & 2 deletions library/src/main/java/com/trinity/editor/VideoExport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ package com.trinity.editor

import android.content.Context
import android.view.Surface
import com.trinity.BuildConfig
import com.trinity.encoder.MediaCodecSurfaceEncoder
import com.trinity.listener.OnExportListener
import com.trinity.util.Trinity
import java.text.SimpleDateFormat
import java.util.*

/**
* Created by wlanjie on 2019-07-30
Expand All @@ -36,13 +39,27 @@ class TrinityVideoExport(private val context: Context) : VideoExport {
private var mSurface: Surface ?= null
private var mListener: OnExportListener ?= null

private fun getNow(): String {
return if (android.os.Build.VERSION.SDK_INT >= 24) {
SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.US).format(Date())
} else {
val tms = Calendar.getInstance()
tms.get(Calendar.YEAR).toString() + "-" +
tms.get(Calendar.MONTH).toString() + "-" +
tms.get(Calendar.DAY_OF_MONTH).toString() + "-" +
tms.get(Calendar.HOUR_OF_DAY).toString() + "-" +
tms.get(Calendar.MINUTE).toString()
}
}

override fun export(info: VideoExportInfo, l: OnExportListener): Int {
mListener = l
val tag = "trinity-export-" + BuildConfig.VERSION_NAME + "-" + getNow()
val resourcePath = context.externalCacheDir?.absolutePath + "/resource.json"
return export(mHandle, resourcePath, info.path,
info.width, info.height, info.frameRate, info.videoBitRate,
info.sampleRate, info.channelCount, info.audioBitRate,
info.mediaCodecDecode, info.mediaCodecEncode)
info.mediaCodecDecode, info.mediaCodecEncode, tag)
}

override fun cancel() {
Expand All @@ -65,7 +82,8 @@ class TrinityVideoExport(private val context: Context) : VideoExport {
private external fun export(handle: Long, resourcePath: String, path: String,
width: Int, height: Int, frameRate: Int,
videoBitRate: Int, sampleRate: Int, channelCount: Int,
audioBitRate: Int, mediaCodecDecode: Boolean, mediaCodecEncode: Boolean): Int
audioBitRate: Int, mediaCodecDecode: Boolean, mediaCodecEncode: Boolean,
tag: String): Int

private external fun cancel(handle: Long)

Expand Down
24 changes: 21 additions & 3 deletions library/src/main/java/com/trinity/record/TrinityRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.provider.Settings.SettingNotFoundException
import android.view.OrientationEventListener
import android.view.Surface
import com.tencent.mars.xlog.Log
import com.trinity.BuildConfig
import com.trinity.Constants
import com.trinity.ErrorCode
import com.trinity.OnRecordingListener
Expand All @@ -47,6 +48,8 @@ import com.trinity.record.service.RecorderService
import com.trinity.record.service.impl.AudioRecordRecorderServiceImpl
import com.trinity.util.Timer
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

/**
* Create by wlanjie on 2019/3/14-下午8:45
Expand Down Expand Up @@ -535,6 +538,19 @@ class TrinityRecord(

}

private fun getNow(): String {
return if (android.os.Build.VERSION.SDK_INT >= 24){
SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.US).format(Date())
} else {
val tms = Calendar.getInstance()
tms.get(Calendar.YEAR).toString() + "-" +
tms.get(Calendar.MONTH).toString() + "-" +
tms.get(Calendar.DAY_OF_MONTH).toString() + "-" +
tms.get(Calendar.HOUR_OF_DAY).toString() + "-" +
tms.get(Calendar.MINUTE).toString()
}
}

/**
* 开始录制一段视频
* @param path 录制的视频保存的地址
Expand Down Expand Up @@ -584,11 +600,12 @@ class TrinityRecord(

mAudioRecordService.start()
setSpeed(mHandle, 1.0f / mSpeed.value)
val tag = "trinity-" + BuildConfig.VERSION_NAME + "-" + getNow()
startEncode(
mHandle, path, width, height, videoBitRate, frameRate,
useHardWareEncode,
audioSampleRate, audioChannel, audioBitRate
)
audioSampleRate, audioChannel, audioBitRate,
tag)
mRecording = true
}
return ErrorCode.SUCCESS
Expand Down Expand Up @@ -897,7 +914,8 @@ class TrinityRecord(
useHardWareEncode: Boolean,
audioSampleRate: Int,
audioChannel: Int,
audioBitRate: Int)
audioBitRate: Int,
tag: String)

/**
* 停止录制
Expand Down

0 comments on commit 2aaeb30

Please sign in to comment.