X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjni%2Fai_suanzi_rtmpclient_Ffmpeg.cpp;h=32f4b670687ecc66b6363849a6344b7e54526c0d;hb=410441c06aa4257d7d9dcb1b581abb4017c67c50;hp=decd9b4bca703210f32ca38c95c78cb62eae77c7;hpb=8af8da070650175d3a35a840fbd339ee572d144f;p=rtmpclient.git diff --git a/app/src/main/jni/ai_suanzi_rtmpclient_Ffmpeg.cpp b/app/src/main/jni/ai_suanzi_rtmpclient_Ffmpeg.cpp index decd9b4..32f4b67 100644 --- a/app/src/main/jni/ai_suanzi_rtmpclient_Ffmpeg.cpp +++ b/app/src/main/jni/ai_suanzi_rtmpclient_Ffmpeg.cpp @@ -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,9 @@ 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); } } @@ -115,12 +141,18 @@ JNIEXPORT void JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_setRtmpUrl (JNIEnv *env, LOGE("set rtmpurl"); //const char* out_path = "rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid"; //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 +161,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 +173,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 +203,7 @@ JNIEXPORT jint JNICALL Java_ai_suanzi_rtmpclient_Ffmpeg_initnew (JNIEnv *env, jo if (avcodec_open2(pCodecCtx, pCodec, ¶m) < 0){ LOGE("Failed to open encoder!\n"); + javaPrint(env, obj, "Failed to open encoder!"); return -1; } @@ -181,6 +220,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 +409,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;