print jni log to log file
authorPeng Li <seudut@gmail.com>
Thu, 17 May 2018 20:11:08 +0000 (04:11 +0800)
committerPeng Li <seudut@gmail.com>
Thu, 17 May 2018 20:11:08 +0000 (04:11 +0800)
app/src/main/java/ai/suanzi/rtmpclient/Ffmpeg.java
app/src/main/java/ai/suanzi/rtmpclient/MyService.java
app/src/main/jni/ai_suanzi_rtmpclient_Ffmpeg.cpp

index 7141810..4d61c9d 100644 (file)
@@ -2,6 +2,8 @@ package ai.suanzi.rtmpclient;
 
 import android.util.Log;
 
+import org.apache.log4j.Logger;
+
 public class Ffmpeg {
 
     static {
@@ -33,6 +35,10 @@ public class Ffmpeg {
         //getPerfectDevice();
     }
 
+    private Logger gLogger = Logger.getLogger("FFmpeg");
+    public void  print(String str){
+        gLogger.error(str);
+    }
 
     public native String getVersion();
     public native void init();
index 2637d60..0d0136d 100644 (file)
@@ -34,12 +34,9 @@ public class MyService extends Service  implements Camera.PreviewCallback {
 
     private Ffmpeg ffmpeg = Ffmpeg.getInstance();
     private  Boolean isRunning = false;
-    //private FfmpegRunnable  runnable;
     private Camera mCamera = null;
     IBinder mBinder = new LocalBinder();
     private String rtmpUrl;
-    //private WindowManager mWindowManager;
-    //private SurfaceView mOutComeVideoView;
     private long frameCount = 0;
 
 
@@ -215,27 +212,5 @@ public class MyService extends Service  implements Camera.PreviewCallback {
         gLogger.error("Preview Format: " + paras.getPreviewFormat() + ". Size: " + paras.getPreviewSize().width + "x" + paras.getPreviewSize().height);
         gLogger.error("Picture Format: " + paras.getPictureFormat() + ". Size: " + paras.getPictureSize().width + "x" + paras.getPictureSize().height);
         camera.setPreviewCallback(this);
-        //camera.startPreview();
     }
 }
-
-//    private class FfmpegRunnable implements Runnable {
-//        private String url;
-//        private Camera.PreviewCallback cb;
-//        public FfmpegRunnable(String _url, Camera.PreviewCallback _cb){
-//            this.url = _url;
-//            this.cb = _cb;
-//        }
-//        @Override
-//        public void run(){
-//            gLogger.error("Run Ffmpeg url: " + url);
-//            isRunning = true;
-//            gLogger.error("Open camera");
-//            mCamera = getCameraInstance();
-//            if(mCamera == null) {
-//                gLogger.error("Open camera, camera is null");
-//            }
-//            configCamera(mCamera);
-//            mCamera.setPreviewCallback(this.cb);
-//        }
-//    }
index decd9b4..6bfea3a 100644 (file)
@@ -28,6 +28,31 @@ AVCodec* pCodec;
 AVPacket enc_pkt;
 AVFrame *pFrameYUV;
 
+void javaPrint(JNIEnv *env, jobject obj, const char* str)
+{
+    jclass clazz = (*env).GetObjectClass(obj);
+    jobject mobj = env->NewGlobalRef(obj);
+    jmethodID mmid = env->GetMethodID(clazz, "print", "(Ljava/lang/String;)V");
+    jstring jstr = env->NewStringUTF(str);
+    env->CallVoidMethod(mobj, mmid, jstr);
+    env->DeleteLocalRef(jstr);
+}
+
+JNIEnv *g_env;
+jobject g_obj;
+
+void custom_log222 (void *ptr, int level, const char* fmt, va_list vl){
+    static int print_prefix = 1;
+    char line[1024];
+    av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
+    if (level <= AV_LOG_WARNING){
+        LOGE("%s", line);
+        javaPrint(g_env, g_obj, line);
+    } else {
+        //LOGE("%s", line);
+        //javaPrint(g_env, g_obj, line);
+    }
+}
 
 void custom_log(void *ptr, int level, const char* fmt, va_list vl){
     //To TXT file
@@ -50,8 +75,10 @@ void custom_log(void *ptr, int level, const char* fmt, va_list vl){
 
     if (level <= AV_LOG_WARNING){
         LOGE("%s", line);
+
+
     } else {
-        LOGE("%s", line);
+//        LOGE("%s", line);
     }
 }
 
@@ -117,10 +144,17 @@ JNIEXPORT void JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_setRtmpUrl (JNIEnv *env,
     //out_path  = env->GetStringUTFChars(url, 0);
 
 }
+
+
+
+//#defind JLOGE(s) javaPrint(env, obj, (s));
+
+
 JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jobject obj, jint width, jint height, jstring url)
 {
     const char * out_path= env->GetStringUTFChars(url, 0);
     LOGE("Ffmpeg init, width=%d, heigh=%d, url=%s", width, height, out_path);
+    javaPrint(env, obj, "Ffmpeg init");
 
        yuv_width=width;
        yuv_height=height;
@@ -129,6 +163,11 @@ JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jo
 
 
        av_register_all();
+       avformat_network_init();
+       g_env = env;
+       g_obj = obj;
+       av_log_set_callback(custom_log222);
+
 
        //output initialize
        avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_path);
@@ -136,6 +175,7 @@ JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jo
        pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
        if (!pCodec){
                LOGE("Can not find encoder!\n");
+               javaPrint(env, obj, "Can not find encoder!");
                return -1;
        }
        pCodecCtx = avcodec_alloc_context3(pCodec);
@@ -165,6 +205,7 @@ JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jo
 
        if (avcodec_open2(pCodecCtx, pCodec, &param) < 0){
                LOGE("Failed to open encoder!\n");
+               javaPrint(env, obj, "Failed to open encoder!");
                return -1;
        }
 
@@ -181,6 +222,7 @@ JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jo
        jint ret = 0;
        if (( ret = avio_open(&ofmt_ctx->pb, out_path, AVIO_FLAG_READ_WRITE)) < 0){
                LOGE("Failed to open output file! return :%s(%d)\n", av_err2str(ret),ret);
+               javaPrint(env, obj, "Failed to open output file! return!");
                return -1;
        }
 
@@ -369,6 +411,8 @@ JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_process (JNIEnv *env, jo
        if (enc_got_frame == 1){
         if (framecnt % (15 * 60) == 0){
                    LOGE("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, enc_pkt.size);
+                   javaPrint(env, obj, "Succeed to encode frame:");
+
         }
                framecnt++;
                enc_pkt.stream_index = video_st->index;