From eaf1f3778a3bbc315cfebd2f07111a41e6082df3 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Sat, 9 Nov 2019 15:48:58 +0800 Subject: [PATCH] =?UTF-8?q?ffmpeg=5Fpusher=E6=94=B9=E4=B8=BAjni=E5=AE=8F?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ffmpeg_pusher改为jni宏定义调用 --- app/src/main/cpp/ffmpeg_jni_define.h | 4 --- app/src/main/cpp/ffmpeg_pusher.cpp | 30 +++++++++++------------ app/src/main/res/layout/activity_push.xml | 8 +++--- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/app/src/main/cpp/ffmpeg_jni_define.h b/app/src/main/cpp/ffmpeg_jni_define.h index a866349..2724b1d 100644 --- a/app/src/main/cpp/ffmpeg_jni_define.h +++ b/app/src/main/cpp/ffmpeg_jni_define.h @@ -27,10 +27,6 @@ (JNIEnv *env, jobject thiz, ##__VA_ARGS__)\ #define PUSHER_FUNC(RETURN_TYPE, FUNC_NAME, ...) \ -extern "C" { \ - JNIEXPORT RETURN_TYPE JNICALL Java_com_frank_ffmpeg_Pusher_ ## FUNC_NAME \ - (JNIEnv *env, jobject thiz, ##__VA_ARGS__);\ -} \ JNIEXPORT RETURN_TYPE JNICALL Java_com_frank_ffmpeg_Pusher_ ## FUNC_NAME \ (JNIEnv *env, jobject thiz, ##__VA_ARGS__)\ diff --git a/app/src/main/cpp/ffmpeg_pusher.cpp b/app/src/main/cpp/ffmpeg_pusher.cpp index c36430c..55e20df 100644 --- a/app/src/main/cpp/ffmpeg_pusher.cpp +++ b/app/src/main/cpp/ffmpeg_pusher.cpp @@ -5,10 +5,9 @@ #include #include #include +#include "ffmpeg_jni_define.h" -#define LOG_TAG "FFmpegLive" -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) +#define TAG "FFmpegPusher" #ifdef __cplusplus extern "C" { @@ -18,8 +17,7 @@ extern "C" { #include #include - JNIEXPORT jint JNICALL - Java_com_frank_ffmpeg_Pusher_pushStream(JNIEnv *env, jobject, jstring filePath, jstring liveUrl) { +PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) { AVOutputFormat *output_format = NULL; AVFormatContext *in_format = NULL, *out_format = NULL; @@ -33,8 +31,8 @@ extern "C" { file_path = env->GetStringUTFChars(filePath, NULL); live_url = env->GetStringUTFChars(liveUrl, NULL); - LOGI("file_path=%s", file_path); - LOGI("live_url=%s", live_url); + LOGE(TAG, "file_path=%s", file_path); + LOGE(TAG, "live_url=%s", live_url); //注册所有组件 av_register_all(); @@ -42,12 +40,12 @@ extern "C" { avformat_network_init(); //打开输入文件 if((ret = avformat_open_input(&in_format, file_path, 0, 0)) < 0){ - LOGE("could not open input file..."); + LOGE(TAG, "could not open input file..."); goto end; } //寻找流信息 if((ret = avformat_find_stream_info(in_format, 0)) < 0){ - LOGE("could not find stream info..."); + LOGE(TAG, "could not find stream info..."); goto end; } // @@ -62,7 +60,7 @@ extern "C" { //分配输出封装格式上下文, rtmp协议支持格式为flv avformat_alloc_output_context2(&out_format, NULL, "flv", live_url); if(!out_format){ - LOGE("could not alloc output context..."); + LOGE(TAG, "could not alloc output context..."); ret = AVERROR_UNKNOWN; goto end; } @@ -71,14 +69,14 @@ extern "C" { AVStream *in_stream = in_format->streams[i]; AVStream *out_stream = avformat_new_stream(out_format, in_stream->codec->codec); if(!out_stream){ - LOGE("could not alloc output stream..."); + LOGE(TAG, "could not alloc output stream..."); ret = AVERROR_UNKNOWN; goto end; } //复制封装格式上下文 ret = avcodec_copy_context(out_stream->codec, in_stream->codec); if(ret < 0){ - LOGE("could not copy context..."); + LOGE(TAG, "could not copy context..."); goto end; } out_stream->codec->codec_tag = 0; @@ -92,14 +90,14 @@ extern "C" { if(!(output_format->flags & AVFMT_NOFILE)){ ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE); if(ret < 0){ - LOGE("could not open output url '%s'", live_url); + LOGE(TAG, "could not open output url '%s'", live_url); goto end; } } //写文件头 ret = avformat_write_header(out_format, NULL); if(ret < 0){ - LOGE("could not write header..."); + LOGE(TAG, "could not write header..."); goto end; } //获取开始时间 @@ -144,12 +142,12 @@ extern "C" { //视频帧计数 if(packet.stream_index == video_index){ frame_index ++; - LOGI("write frame = %d", frame_index); + LOGI(TAG, "write frame = %d", frame_index); } //写一帧数据 ret = av_interleaved_write_frame(out_format, &packet); if(ret < 0){ - LOGE("could not write frame..."); + LOGE(TAG, "could not write frame..."); break; } //释放包数据内存 diff --git a/app/src/main/res/layout/activity_push.xml b/app/src/main/res/layout/activity_push.xml index 76718d3..3e1cc89 100644 --- a/app/src/main/res/layout/activity_push.xml +++ b/app/src/main/res/layout/activity_push.xml @@ -9,7 +9,7 @@ android:id="@+id/edit_file_path" android:layout_width="match_parent" android:layout_height="wrap_content" - android:maxLines="1" + android:maxLines="3" android:minWidth="200dp" android:layout_centerInParent="true"/> @@ -18,7 +18,7 @@ android:id="@+id/edit_live_url" android:layout_width="match_parent" android:layout_height="wrap_content" - android:maxLines="1" + android:maxLines="3" android:minWidth="200dp" android:layout_marginTop="10dp" android:layout_below="@+id/edit_file_path"/> @@ -27,8 +27,8 @@ android:id="@+id/btn_push_stream" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginTop="10dp" - android:layout_below="@+id/edit_live_url" + android:layout_alignParentBottom="true" + android:layout_marginBottom="10dp" android:layout_centerHorizontal="true" android:text="@string/video_push"/>