Skip to content

Commit

Permalink
jni部分
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjie10930 committed May 4, 2018
1 parent 771ee38 commit 891935b
Show file tree
Hide file tree
Showing 122 changed files with 29,417 additions and 0 deletions.
64 changes: 64 additions & 0 deletions app/jnicode/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libavutil
LOCAL_SRC_FILES := prebuilt/libavutil.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libswresample
LOCAL_SRC_FILES := prebuilt/libswresample.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libswscale
LOCAL_SRC_FILES := prebuilt/libswscale.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libavcodec
LOCAL_SRC_FILES := prebuilt/libavcodec.so
include $(PREBUILT_SHARED_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := libavformat
LOCAL_SRC_FILES := prebuilt/libavformat.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libavfilter
LOCAL_SRC_FILES := prebuilt/libavfilter.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libavdevice
LOCAL_SRC_FILES := prebuilt/libavdevice.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libpostproc
LOCAL_SRC_FILES := prebuilt/libpostproc.so
include $(PREBUILT_SHARED_LIBRARY)


include $(CLEAR_VARS)

LOCAL_ARM_MODE := arm
LOCAL_MODULE := ffmpeg


LOCAL_SRC_FILES := Jni_FFmpegCmd.c\
cmdutils.c \
ffmpeg.c \
ffmpeg_opt.c \
ffmpeg_filter.c \
ffmpeg_thread.c

LOCAL_C_INCLUDES := E:\ijkPlayer\EpMedia\ffmpeg-3.3.4

LOCAL_LDLIBS := -llog -ljnigraphics -lz -landroid -lm -pthread -L$(SYSROOT)/usr/lib -latomic
LOCAL_SHARED_LIBRARIES := libavcodec libavfilter libavformat libavutil libswresample libswscale libavdevice libpostproc
LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS -Wno-sign-compare -Wno-switch -Wno-pointer-sign -DHAVE_NEON=1 -fPIC -DANDROI

include $(BUILD_SHARED_LIBRARY)
2 changes: 2 additions & 0 deletions app/jnicode/jni/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM=android-19
115 changes: 115 additions & 0 deletions app/jnicode/jni/Jni_FFmpegCmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "Jni_FFmpegCmd.h"
#include <string.h>
#include "ffmpeg_thread.h"
#include "android_log.h"
#include "cmdutils.h"

static JavaVM *jvm = NULL;
//java虚拟机
static jclass m_clazz = NULL;//当前类(面向java)

/**
* 回调执行Java方法
* 参看 Jni反射+Java反射
*/
void callJavaMethod(JNIEnv *env, jclass clazz,int ret) {
if (clazz == NULL) {
LOGE("---------------clazz isNULL---------------");
return;
}
//获取方法ID (I)V指的是方法签名 通过javap -s -public FFmpegCmd 命令生成
jmethodID methodID = (*env)->GetStaticMethodID(env, clazz, "onExecuted", "(I)V");
if (methodID == NULL) {
LOGE("---------------methodID isNULL---------------");
return;
}
//调用该java方法
(*env)->CallStaticVoidMethod(env, clazz, methodID,ret);
}
void callJavaMethodProgress(JNIEnv *env, jclass clazz,float ret) {
if (clazz == NULL) {
LOGE("---------------clazz isNULL---------------");
return;
}
//获取方法ID (I)V指的是方法签名 通过javap -s -public FFmpegCmd 命令生成
jmethodID methodID = (*env)->GetStaticMethodID(env, clazz, "onProgress", "(F)V");
if (methodID == NULL) {
LOGE("---------------methodID isNULL---------------");
return;
}
//调用该java方法
(*env)->CallStaticVoidMethod(env, clazz, methodID,ret);
}

/**
* c语言-线程回调
*/
static void ffmpeg_callback(int ret) {
JNIEnv *env;
//附加到当前线程从JVM中取出JNIEnv, C/C++从子线程中直接回到Java里的方法时 必须经过这个步骤
(*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
callJavaMethod(env, m_clazz,ret);

//完毕-脱离当前线程
(*jvm)->DetachCurrentThread(jvm);
}

void ffmpeg_progress(float progress) {
JNIEnv *env;
(*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
callJavaMethodProgress(env, m_clazz,progress);
(*jvm)->DetachCurrentThread(jvm);
}

/*
* Class: JniTest_FFmpegCmd
* Method: exec
* Signature: (I[Ljava/lang/String;)I
*/
JNIEXPORT jint

JNICALL Java_Jni_FFmpegCmd_exec
(JNIEnv *env, jclass clazz, jint cmdnum, jobjectArray cmdline) {
//---------------------------------C语言 反射Java 相关----------------------------------------
//在jni的c线程中不允许使用共用的env环境变量 但JavaVM在整个jvm中是共用的 可通过保存JavaVM指针,到时候再通过JavaVM指针取出JNIEnv *env;
//ICS之前(你可把NDK sdk版本改成低于11) 可以直接写m_clazz = clazz;直接赋值, 然而ICS(sdk11) 后便改变了这一机制,在线程中回调java时 不能直接共用变量 必须使用NewGlobalRef创建全局对象
//官方文档正在拼命的解释这一原因,参看:http://android-developers.blogspot.jp/2011/11/jni-local-reference-changes-in-ics.html
(*env)->GetJavaVM(env, &jvm);
m_clazz = (*env)->NewGlobalRef(env, clazz);
//---------------------------------C语言 反射Java 相关----------------------------------------
//---------------------------------java 数组转C语言数组----------------------------------------
int i = 0;//满足NDK所需的C99标准
char **argv = NULL;//命令集 二维指针
jstring *strr = NULL;

if (cmdline != NULL) {
argv = (char **) malloc(sizeof(char *) * cmdnum);
strr = (jstring *) malloc(sizeof(jstring) * cmdnum);

for (i = 0; i < cmdnum; ++i) {//转换
strr[i] = (jstring)(*env)->GetObjectArrayElement(env, cmdline, i);
argv[i] = (char *) (*env)->GetStringUTFChars(env, strr[i], 0);
}

}
//---------------------------------java 数组转C语言数组----------------------------------------
//---------------------------------执行FFmpeg命令相关----------------------------------------
//新建线程 执行ffmpeg 命令
ffmpeg_thread_run_cmd(cmdnum, argv);
//注册ffmpeg命令执行完毕时的回调
ffmpeg_thread_callback(ffmpeg_callback);

free(strr);
return 0;
}

JNIEXPORT void

/*
JNICALL Java_Jni_FFmpegCmd_exit
(JNIEnv *env, jclass clazz) {
(*env)->GetJavaVM(env, &jvm);
m_clazz = (*env)->NewGlobalRef(env, clazz);
ffmpeg_thread_cancel();
}
*/
26 changes: 26 additions & 0 deletions app/jnicode/jni/Jni_FFmpegCmd.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions app/jnicode/jni/android_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//#include <android/log.h>
//#define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, "ffmpeg_vdtest", format, ##__VA_ARGS__)
//#define LOGI(format, ...) __android_log_print(ANDROID_LOG_INFO, "ffmpeg_vdtest", format, ##__VA_ARGS__)
#include <android/log.h>
static int use_log_report = 0;


#define FF_LOG_TAG "FFmpeg_EpMedia"


#define FF_LOG_UNKNOWN ANDROID_LOG_UNKNOWN
#define FF_LOG_DEFAULT ANDROID_LOG_DEFAULT


#define FF_LOG_VERBOSE ANDROID_LOG_VERBOSE
#define FF_LOG_DEBUG ANDROID_LOG_DEBUG
#define FF_LOG_INFO ANDROID_LOG_INFO
#define FF_LOG_WARN ANDROID_LOG_WARN
#define FF_LOG_ERROR ANDROID_LOG_ERROR
#define FF_LOG_FATAL ANDROID_LOG_FATAL
#define FF_LOG_SILENT ANDROID_LOG_SILENT


#define VLOG(level, TAG, ...) ((void)__android_log_vprint(level, TAG, __VA_ARGS__))
#define VLOGV(...) VLOG(FF_LOG_VERBOSE, FF_LOG_TAG, __VA_ARGS__)
#define VLOGD(...) VLOG(FF_LOG_DEBUG, FF_LOG_TAG, __VA_ARGS__)
#define VLOGI(...) VLOG(FF_LOG_INFO, FF_LOG_TAG, __VA_ARGS__)
#define VLOGW(...) VLOG(FF_LOG_WARN, FF_LOG_TAG, __VA_ARGS__)
#define VLOGE(...) VLOG(FF_LOG_ERROR, FF_LOG_TAG, __VA_ARGS__)


#define ALOG(level, TAG, ...) ((void)__android_log_print(level, TAG, __VA_ARGS__))
#define ALOGV(...) ALOG(FF_LOG_VERBOSE, FF_LOG_TAG, __VA_ARGS__)
#define ALOGD(...) ALOG(FF_LOG_DEBUG, FF_LOG_TAG, __VA_ARGS__)
#define ALOGI(...) ALOG(FF_LOG_INFO, FF_LOG_TAG, __VA_ARGS__)
#define ALOGW(...) ALOG(FF_LOG_WARN, FF_LOG_TAG, __VA_ARGS__)
#define ALOGE(...) ALOG(FF_LOG_ERROR, FF_LOG_TAG, __VA_ARGS__)

#define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, FF_LOG_TAG, format, ##__VA_ARGS__)
#define LOGI(format, ...) __android_log_print(ANDROID_LOG_INFO, FF_LOG_TAG, format, ##__VA_ARGS__)


/*belown printf info*/
static void ffp_log_callback_brief(void *ptr, int level, const char *fmt, va_list vl)
{
int ffplv = FF_LOG_VERBOSE;
if (level <= AV_LOG_ERROR)
ffplv = FF_LOG_ERROR;
else if (level <= AV_LOG_WARNING)
ffplv = FF_LOG_WARN;
else if (level <= AV_LOG_INFO)
ffplv = FF_LOG_INFO;
else if (level <= AV_LOG_VERBOSE)
ffplv = FF_LOG_VERBOSE;
else
ffplv = FF_LOG_DEBUG;


if (level <= AV_LOG_INFO)
VLOG(ffplv, FF_LOG_TAG, fmt, vl);
}


static void ffp_log_callback_report(void *ptr, int level, const char *fmt, va_list vl)
{
int ffplv = FF_LOG_VERBOSE;
if (level <= AV_LOG_ERROR)
ffplv = FF_LOG_ERROR;
else if (level <= AV_LOG_WARNING)
ffplv = FF_LOG_WARN;
else if (level <= AV_LOG_INFO)
ffplv = FF_LOG_INFO;
else if (level <= AV_LOG_VERBOSE)
ffplv = FF_LOG_VERBOSE;
else
ffplv = FF_LOG_DEBUG;


va_list vl2;
char line[1024];
static int print_prefix = 1;


va_copy(vl2, vl);
// av_log_default_callback(ptr, level, fmt, vl);
av_log_format_line(ptr, level, fmt, vl2, line, sizeof(line), &print_prefix);
va_end(vl2);


ALOG(ffplv, FF_LOG_TAG, "%s", line);
}

56 changes: 56 additions & 0 deletions app/jnicode/jni/ffmpeg_thread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "libavcodec/avcodec.h"
#include "ffmpeg_thread.h"
#include "android_log.h"

pthread_t ntid;
char **argvs = NULL;
int num=0;


void *thread(void *arg)
{ //执行
int result = ffmpeg_exec(num, argvs);
return ((void *)0);
}
/**
* 新建子线程执行ffmpeg命令
*/
int ffmpeg_thread_run_cmd(int cmdnum,char **argv){
num=cmdnum;
argvs=argv;

int temp =pthread_create(&ntid,NULL,thread,NULL);
if(temp!=0)
{
LOGE("can't create thread: %s ",strerror(temp));
return 1;
}
LOGI("create thread succes: %s ",strerror(temp));
return 0;
}

static void (*ffmpeg_callback)(int ret);
/**
* 注册线程回调
*/
void ffmpeg_thread_callback(void (*cb)(int ret)){
ffmpeg_callback = cb;
}

/**
* 退出线程
*/
void ffmpeg_thread_exit(int ret){
if (ffmpeg_callback) {
ffmpeg_callback(ret);
}
pthread_exit("ffmpeg_thread_exit");
}

/**
* 取消线程
*/
void ffmpeg_thread_cancel(){
void *ret=NULL;
pthread_join(ntid, &ret);
}
14 changes: 14 additions & 0 deletions app/jnicode/jni/ffmpeg_thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "ffmpeg.h"
#include <pthread.h>
#include <string.h>

int ffmpeg_thread_run_cmd(int cmdnum,char **argv);

void ffmpeg_thread_exit(int ret);

void ffmpeg_thread_callback(void (*cb)(int ret));

void ffmpeg_thread_cancel();
Loading

0 comments on commit 891935b

Please sign in to comment.