Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 09/29] fftools/ffmpeg: eliminate the main_return_code global
Date: Sun,  9 Apr 2023 16:08:33 +0200
Message-ID: <20230409140853.28858-9-anton@khirnov.net> (raw)
In-Reply-To: <20230409140853.28858-1-anton@khirnov.net>

Properly pass muxing return codes through the call stack instead.
Slightly changes behavior in case of errors:
* the output IO stream is closed even if writing the trailer returns an
  error, which should be more correct
* all files get properly closed with -xerror, even if one of them fails
---
 fftools/ffmpeg.c     | 18 ++++++++----------
 fftools/ffmpeg.h     | 12 +++++++++++-
 fftools/ffmpeg_mux.c | 12 +++++-------
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 816b6c4b08..7431482acc 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -318,7 +318,6 @@ static volatile int received_sigterm = 0;
 static volatile int received_nb_signals = 0;
 static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0);
 static volatile int ffmpeg_exited = 0;
-int main_return_code = 0;
 static int64_t copy_ts_first_pts = AV_NOPTS_VALUE;
 
 static void
@@ -2951,9 +2950,8 @@ static int transcode(void)
 
     /* write the trailer if needed */
     for (i = 0; i < nb_output_files; i++) {
-        ret = of_write_trailer(output_files[i]);
-        if (ret < 0 && exit_on_error)
-            exit_program(1);
+        int err = of_write_trailer(output_files[i]);
+        ret = err_merge(ret, err);
     }
 
     /* dump report by using the first video and audio streams */
@@ -2975,7 +2973,7 @@ static int transcode(void)
         exit_program(1);
     }
 
-    return 0;
+    return ret;
 }
 
 static BenchmarkTimeStamps get_benchmark_time_stamps(void)
@@ -3061,9 +3059,8 @@ int main(int argc, char **argv)
     }
 
     current_time = ti = get_benchmark_time_stamps();
-    if (transcode() < 0)
-        exit_program(1);
-    if (do_benchmark) {
+    ret = transcode();
+    if (ret >= 0 && do_benchmark) {
         int64_t utime, stime, rtime;
         current_time = get_benchmark_time_stamps();
         utime = current_time.user_usec - ti.user_usec;
@@ -3078,6 +3075,7 @@ int main(int argc, char **argv)
     if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
         exit_program(69);
 
-    exit_program(received_nb_signals ? 255 : main_return_code);
-    return main_return_code;
+    ret = received_nb_signals ? 255 : ret;
+    exit_program(ret);
+    return ret;
 }
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 84ce017320..ddfeeabf75 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -756,7 +756,6 @@ extern const OptionDef options[];
 extern HWDevice *filter_hw_device;
 
 extern unsigned nb_output_dumped;
-extern int main_return_code;
 
 extern int ignore_unknown_streams;
 extern int copy_unknown_streams;
@@ -887,6 +886,17 @@ void close_output_stream(OutputStream *ost);
 int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt);
 void update_benchmark(const char *fmt, ...);
 
+/**
+ * Merge two return codes - return one of the error codes if at least one of
+ * them was negative, 0 otherwise.
+ * Currently just picks the first one, eventually we might want to do something
+ * more sophisticated, like sorting them by priority.
+ */
+static inline int err_merge(int err0, int err1)
+{
+    return (err0 < 0) ? err0 : FFMIN(err1, 0);
+}
+
 #define SPECIFIER_OPT_FMT_str  "%s"
 #define SPECIFIER_OPT_FMT_i    "%i"
 #define SPECIFIER_OPT_FMT_i64  "%"PRId64
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 5663b8f11d..c2b7993e66 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -602,7 +602,7 @@ int of_write_trailer(OutputFile *of)
 {
     Muxer *mux = mux_from_of(of);
     AVFormatContext *fc = mux->fc;
-    int ret;
+    int ret, mux_result = 0;
 
     if (!mux->tq) {
         av_log(mux, AV_LOG_ERROR,
@@ -611,14 +611,12 @@ int of_write_trailer(OutputFile *of)
         return AVERROR(EINVAL);
     }
 
-    ret = thread_stop(mux);
-    if (ret < 0)
-        main_return_code = ret;
+    mux_result = thread_stop(mux);
 
     ret = av_write_trailer(fc);
     if (ret < 0) {
         av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
-        return ret;
+        mux_result = err_merge(mux_result, ret);
     }
 
     mux->last_filesize = filesize(fc->pb);
@@ -627,11 +625,11 @@ int of_write_trailer(OutputFile *of)
         ret = avio_closep(&fc->pb);
         if (ret < 0) {
             av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
-            return ret;
+            mux_result = err_merge(mux_result, ret);
         }
     }
 
-    return 0;
+    return mux_result;
 }
 
 static void ost_free(OutputStream **post)
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2023-04-09 14:13 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-09 14:08 [FFmpeg-devel] [PATCH 01/29] fftools/ffmpeg: move OutputStream.vsync_frame_number to Encoder Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 02/29] fftools/ffmpeg: move OutputStream.next_pts " Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 03/29] fftools/ffmpeg: move OutputStream.sq_frame " Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 04/29] fftools/ffmpeg: move OutputStream.last_nb0_frames " Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 05/29] fftools/ffmpeg: move OutputStream.last_filter_pts to OutputFilter Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 06/29] fftools/ffmpeg_enc: replace abort() with av_assert0(0) Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 07/29] fftools/ffmpeg: drop a useless goto Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 08/29] fftools/ffmpeg: move the hw_device_free_all() call to ffmpeg_cleanup() Anton Khirnov
2023-04-09 14:08 ` Anton Khirnov [this message]
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 10/29] fftools/ffmpeg: factorize checking whether any output was written Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 11/29] fftools/ffmpeg: move printing verbose muxing stats to ffmpeg_mux Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 12/29] fftools/ffmpeg_mux: reindent Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 13/29] fftools/ffmpeg_mux: log final stats to muxer context Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 14/29] fftools/ffmpeg: rewrite printing the final output sizes Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 15/29] fftools/ffmpeg_mux: make data_size_mux private to ffmpeg_mux Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 16/29] fftools/ffmpeg: move printing verbose demuxing stats to ffmpeg_demux Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 17/29] fftools/ffmpeg_demux: reindent Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 18/29] fftools/ffmpeg_demux: log final stats to demuxer context Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 19/29] fftools/ffmpeg: disable and deprecate -qphist Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 20/29] fftools/ffmpeg_filter: stop setting encoder channel layout unnecessarily Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 21/29] fftools/ffmpeg_mux_init: print more meaningful error messages Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 22/29] fftools/ffmpeg: track a list of non-lavfi outputs in InputStream Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 23/29] fftools/ffmpeg: stop calling check_output_constraints() for streamcopy Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 24/29] fftools/ffmpeg: inline check_output_constraints() into its only caller Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 25/29] fftools/ffmpeg_demux: set the timebase on demuxed packets Anton Khirnov
2023-04-12  2:49   ` James Almer
2023-04-12  5:59     ` Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 26/29] fftools/ffmpeg: use AVPacket.time_base to simplify do_streamcopy() Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 27/29] fftools/ffmpeg: only set InputStream.next_pts for decoding Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 28/29] fftools/ffmpeg: move checking for input -t out of do_streamcopy() Anton Khirnov
2023-04-09 14:08 ` [FFmpeg-devel] [PATCH 29/29] fftools/ffmpeg: stop using InputStream.pts for streamcopy Anton Khirnov
2023-04-11  6:35 ` [FFmpeg-devel] [PATCH 01/29] fftools/ffmpeg: move OutputStream.vsync_frame_number to Encoder Anton Khirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230409140853.28858-9-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git