From ada818890000e8b03cd5b5e2173ae2efd78524b2 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Wed, 12 Aug 2020 21:09:55 +0800 Subject: [PATCH] cancel the running task cancel the running task --- app/src/main/cpp/ffmpeg/ffmpeg.c | 10 ++++++++++ app/src/main/cpp/ffmpeg/ffmpeg.h | 1 + app/src/main/cpp/ffmpeg_cmd.c | 4 ++++ app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java | 6 ++++++ .../java/com/frank/ffmpeg/handler/FFmpegHandler.java | 8 ++++++++ 5 files changed, 29 insertions(+) diff --git a/app/src/main/cpp/ffmpeg/ffmpeg.c b/app/src/main/cpp/ffmpeg/ffmpeg.c index 1ab4b35..cb987fe 100644 --- a/app/src/main/cpp/ffmpeg/ffmpeg.c +++ b/app/src/main/cpp/ffmpeg/ffmpeg.c @@ -159,6 +159,7 @@ int nb_output_files = 0; FilterGraph **filtergraphs; int nb_filtergraphs; +int cancel_execute = 0; #if HAVE_TERMIOS_H /* init terminal so that we can grab keys */ @@ -4709,6 +4710,11 @@ static int transcode(void) break; } + if (cancel_execute) { + av_log(NULL, AV_LOG_ERROR, "cancel task by user..."); + break; + } + ret = transcode_step(); if (ret < 0 && ret != AVERROR_EOF) { av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret)); @@ -4940,3 +4946,7 @@ end: ffmpeg_cleanup(0); return main_return_code; } + +void cancel_task(int cancel) { + cancel_execute = cancel; +} diff --git a/app/src/main/cpp/ffmpeg/ffmpeg.h b/app/src/main/cpp/ffmpeg/ffmpeg.h index 5204ea3..5c93cca 100644 --- a/app/src/main/cpp/ffmpeg/ffmpeg.h +++ b/app/src/main/cpp/ffmpeg/ffmpeg.h @@ -675,5 +675,6 @@ enum ProgressState { }; void progress_callback(int position, int duration, int state); +void cancel_task(int cancel); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/app/src/main/cpp/ffmpeg_cmd.c b/app/src/main/cpp/ffmpeg_cmd.c index 7ae7d8d..e3fb8e5 100644 --- a/app/src/main/cpp/ffmpeg_cmd.c +++ b/app/src/main/cpp/ffmpeg_cmd.c @@ -49,6 +49,10 @@ FFMPEG_FUNC(jint, handle, jobjectArray commands) { return result; } +FFMPEG_FUNC(void, cancelTaskJni, jint cancel) { + cancel_task(cancel); +} + void log_callback(void* ptr, int level, const char* format, va_list args) { switch (level) { case AV_LOG_DEBUG: diff --git a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java index 632f653..def89bc 100644 --- a/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java +++ b/app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java @@ -93,6 +93,10 @@ public class FFmpegCmd { }).start(); } + public static void cancelTask(boolean cancel) { + cancelTaskJni(cancel ? 1 : 0); + } + /** * Using FastStart to moov box in front of mdat box * @@ -142,6 +146,8 @@ public class FFmpegCmd { private native static int handle(String[] commands); + private native static void cancelTaskJni(int cancel); + private native static int fastStart(String inputFile, String outputFile); private native static String handleProbe(String[] commands); diff --git a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java index ae9956f..7e4871a 100644 --- a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java +++ b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java @@ -73,6 +73,14 @@ public class FFmpegHandler { }); } + /** + * cancel the running task, and exit quietly + * @param cancel cancel the task when flag is true + */ + public void cancelExecute(boolean cancel) { + FFmpegCmd.cancelTask(cancel); + } + /** * execute multi commands of FFmpeg *