[FFmpeg-devel] [PATCH 18/18] fftools/ffmpeg: move sending filtergraph commands to a separate function

Anton Khirnov anton at khirnov.net
Sat Aug 26 18:11:44 EEST 2023


Stop accessing filtergraph internals from keyboard reading code.
---
 fftools/ffmpeg.c        | 26 ++++----------------------
 fftools/ffmpeg.h        |  3 +++
 fftools/ffmpeg_filter.c | 24 ++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 45844b489f..f1c5f40caf 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -97,10 +97,6 @@
 
 #include "libswresample/swresample.h"
 
-#include "libavfilter/avfilter.h"
-#include "libavfilter/buffersrc.h"
-#include "libavfilter/buffersink.h"
-
 #include "cmdutils.h"
 #include "ffmpeg.h"
 #include "sync_queue.h"
@@ -977,7 +973,7 @@ static void set_tty_echo(int on)
 
 static int check_keyboard_interaction(int64_t cur_time)
 {
-    int i, ret, key;
+    int i, key;
     static int64_t last_time;
     if (received_nb_signals)
         return AVERROR_EXIT;
@@ -1010,23 +1006,9 @@ static int check_keyboard_interaction(int64_t cur_time)
             (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
             av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
                    target, time, command, arg);
-            for (i = 0; i < nb_filtergraphs; i++) {
-                FilterGraph *fg = filtergraphs[i];
-                if (fg->graph) {
-                    if (time < 0) {
-                        ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
-                                                          key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
-                        fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
-                    } else if (key == 'c') {
-                        fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
-                        ret = AVERROR_PATCHWELCOME;
-                    } else {
-                        ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
-                        if (ret < 0)
-                            fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
-                    }
-                }
-            }
+            for (i = 0; i < nb_filtergraphs; i++)
+                fg_send_command(filtergraphs[i], time, target, command, arg,
+                                key == 'C');
         } else {
             av_log(NULL, AV_LOG_ERROR,
                    "Parse error, at least 3 arguments were expected, "
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 9c85df5bdf..b146f1f2dc 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -759,6 +759,9 @@ void fg_free(FilterGraph **pfg);
  */
 int fg_transcode_step(FilterGraph *graph, InputStream **best_ist);
 
+void fg_send_command(FilterGraph *fg, double time, const char *target,
+                     const char *command, const char *arg, int all_filters);
+
 /**
  * Get and encode new output from specified filtergraph, without causing
  * activity.
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index e65e7cb64d..4ba20342a9 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1741,6 +1741,30 @@ int filtergraph_is_simple(const FilterGraph *fg)
     return fgp->is_simple;
 }
 
+void fg_send_command(FilterGraph *fg, double time, const char *target,
+                     const char *command, const char *arg, int all_filters)
+{
+    int ret;
+
+    if (!fg->graph)
+        return;
+
+    if (time < 0) {
+        char response[4096];
+        ret = avfilter_graph_send_command(fg->graph, target, command, arg,
+                                          response, sizeof(response),
+                                          all_filters ? 0 : AVFILTER_CMD_FLAG_ONE);
+        fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s",
+                fg->index, ret, response);
+    } else if (!all_filters) {
+        fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
+    } else {
+        ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
+        if (ret < 0)
+            fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
+    }
+}
+
 static int fg_output_step(OutputFilterPriv *ofp, int flush)
 {
     FilterGraphPriv    *fgp = fgp_from_fg(ofp->ofilter.graph);
-- 
2.40.1



More information about the ffmpeg-devel mailing list