ffmpeg_pusher改为jni宏定义调用

ffmpeg_pusher改为jni宏定义调用
pull/107/head
xufulong 6 years ago
parent fa9e2d5461
commit eaf1f3778a
  1. 4
      app/src/main/cpp/ffmpeg_jni_define.h
  2. 30
      app/src/main/cpp/ffmpeg_pusher.cpp
  3. 8
      app/src/main/res/layout/activity_push.xml

@ -27,10 +27,6 @@
(JNIEnv *env, jobject thiz, ##__VA_ARGS__)\ (JNIEnv *env, jobject thiz, ##__VA_ARGS__)\
#define PUSHER_FUNC(RETURN_TYPE, FUNC_NAME, ...) \ #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 \ JNIEXPORT RETURN_TYPE JNICALL Java_com_frank_ffmpeg_Pusher_ ## FUNC_NAME \
(JNIEnv *env, jobject thiz, ##__VA_ARGS__)\ (JNIEnv *env, jobject thiz, ##__VA_ARGS__)\

@ -5,10 +5,9 @@
#include <jni.h> #include <jni.h>
#include <string> #include <string>
#include <android/log.h> #include <android/log.h>
#include "ffmpeg_jni_define.h"
#define LOG_TAG "FFmpegLive" #define TAG "FFmpegPusher"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -18,8 +17,7 @@ extern "C" {
#include <libavfilter/avfilter.h> #include <libavfilter/avfilter.h>
#include <libavutil/time.h> #include <libavutil/time.h>
JNIEXPORT jint JNICALL PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
Java_com_frank_ffmpeg_Pusher_pushStream(JNIEnv *env, jobject, jstring filePath, jstring liveUrl) {
AVOutputFormat *output_format = NULL; AVOutputFormat *output_format = NULL;
AVFormatContext *in_format = NULL, *out_format = NULL; AVFormatContext *in_format = NULL, *out_format = NULL;
@ -33,8 +31,8 @@ extern "C" {
file_path = env->GetStringUTFChars(filePath, NULL); file_path = env->GetStringUTFChars(filePath, NULL);
live_url = env->GetStringUTFChars(liveUrl, NULL); live_url = env->GetStringUTFChars(liveUrl, NULL);
LOGI("file_path=%s", file_path); LOGE(TAG, "file_path=%s", file_path);
LOGI("live_url=%s", live_url); LOGE(TAG, "live_url=%s", live_url);
//注册所有组件 //注册所有组件
av_register_all(); av_register_all();
@ -42,12 +40,12 @@ extern "C" {
avformat_network_init(); avformat_network_init();
//打开输入文件 //打开输入文件
if((ret = avformat_open_input(&in_format, file_path, 0, 0)) < 0){ 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; goto end;
} }
//寻找流信息 //寻找流信息
if((ret = avformat_find_stream_info(in_format, 0)) < 0){ 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; goto end;
} }
// //
@ -62,7 +60,7 @@ extern "C" {
//分配输出封装格式上下文, rtmp协议支持格式为flv //分配输出封装格式上下文, rtmp协议支持格式为flv
avformat_alloc_output_context2(&out_format, NULL, "flv", live_url); avformat_alloc_output_context2(&out_format, NULL, "flv", live_url);
if(!out_format){ if(!out_format){
LOGE("could not alloc output context..."); LOGE(TAG, "could not alloc output context...");
ret = AVERROR_UNKNOWN; ret = AVERROR_UNKNOWN;
goto end; goto end;
} }
@ -71,14 +69,14 @@ extern "C" {
AVStream *in_stream = in_format->streams[i]; AVStream *in_stream = in_format->streams[i];
AVStream *out_stream = avformat_new_stream(out_format, in_stream->codec->codec); AVStream *out_stream = avformat_new_stream(out_format, in_stream->codec->codec);
if(!out_stream){ if(!out_stream){
LOGE("could not alloc output stream..."); LOGE(TAG, "could not alloc output stream...");
ret = AVERROR_UNKNOWN; ret = AVERROR_UNKNOWN;
goto end; goto end;
} }
//复制封装格式上下文 //复制封装格式上下文
ret = avcodec_copy_context(out_stream->codec, in_stream->codec); ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if(ret < 0){ if(ret < 0){
LOGE("could not copy context..."); LOGE(TAG, "could not copy context...");
goto end; goto end;
} }
out_stream->codec->codec_tag = 0; out_stream->codec->codec_tag = 0;
@ -92,14 +90,14 @@ extern "C" {
if(!(output_format->flags & AVFMT_NOFILE)){ if(!(output_format->flags & AVFMT_NOFILE)){
ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE); ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE);
if(ret < 0){ if(ret < 0){
LOGE("could not open output url '%s'", live_url); LOGE(TAG, "could not open output url '%s'", live_url);
goto end; goto end;
} }
} }
//写文件头 //写文件头
ret = avformat_write_header(out_format, NULL); ret = avformat_write_header(out_format, NULL);
if(ret < 0){ if(ret < 0){
LOGE("could not write header..."); LOGE(TAG, "could not write header...");
goto end; goto end;
} }
//获取开始时间 //获取开始时间
@ -144,12 +142,12 @@ extern "C" {
//视频帧计数 //视频帧计数
if(packet.stream_index == video_index){ if(packet.stream_index == video_index){
frame_index ++; frame_index ++;
LOGI("write frame = %d", frame_index); LOGI(TAG, "write frame = %d", frame_index);
} }
//写一帧数据 //写一帧数据
ret = av_interleaved_write_frame(out_format, &packet); ret = av_interleaved_write_frame(out_format, &packet);
if(ret < 0){ if(ret < 0){
LOGE("could not write frame..."); LOGE(TAG, "could not write frame...");
break; break;
} }
//释放包数据内存 //释放包数据内存

@ -9,7 +9,7 @@
android:id="@+id/edit_file_path" android:id="@+id/edit_file_path"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:maxLines="1" android:maxLines="3"
android:minWidth="200dp" android:minWidth="200dp"
android:layout_centerInParent="true"/> android:layout_centerInParent="true"/>
@ -18,7 +18,7 @@
android:id="@+id/edit_live_url" android:id="@+id/edit_live_url"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:maxLines="1" android:maxLines="3"
android:minWidth="200dp" android:minWidth="200dp"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:layout_below="@+id/edit_file_path"/> android:layout_below="@+id/edit_file_path"/>
@ -27,8 +27,8 @@
android:id="@+id/btn_push_stream" android:id="@+id/btn_push_stream"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_alignParentBottom="true"
android:layout_below="@+id/edit_live_url" android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:text="@string/video_push"/> android:text="@string/video_push"/>

Loading…
Cancel
Save