print jni log to log file
[rtmpclient.git] / app / src / main / jni / ai_suanzi_rtmpclient_Ffmpeg.cpp
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;