[FFmpeg-devel] [PATCH] Add -timelimit option

Stefano Sabatini stefano.sabatini-lala
Sat Nov 14 22:14:48 CET 2009


On date Saturday 2009-11-14 16:49:22 +0000, Mans Rullgard encoded:
> This option limits the CPU time used by ffmpeg to the number of seconds
> specified.  After this time, the OS sends a SIGXCPU signal, which we
> handle and attempt to exit cleanly.  If the process is stuck, the OS
> will deliver a SIGKILL one second later, forcibly terminating the
> process.
> 
> This functionality is useful in automated setups where a runaway ffmpeg
> process would otherwise go undetected.
> ---
>  ffmpeg.c |   18 ++++++++++++++++++
>  1 files changed, 18 insertions(+), 0 deletions(-)
> 
> diff --git a/ffmpeg.c b/ffmpeg.c
> index 4d50768..6b18ba3 100644
> --- a/ffmpeg.c
> +++ b/ffmpeg.c
> @@ -352,6 +352,10 @@ static void term_init(void)
>  
>      signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).  */
>      signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
> +#ifdef SIGXCPU
> +    signal(SIGXCPU, sigterm_handler);
> +#endif
> +
>      /*
>      register a function to be called at normal program termination
>      */
> @@ -3823,6 +3827,19 @@ static int opt_preset(const char *opt, const char *arg)
>      return 0;
>  }
>  
> +static int opt_timelimit(const char *arg)
> +{
> +#if HAVE_SYS_RESOURCE_H
> +    int lim = strtol(arg, NULL, 0);

Please use parse_number_or_die().

> +    struct rlimit rl = { lim, lim + 1 };
> +    if (setrlimit(RLIMIT_CPU, &rl))
> +        perror("setrlimit");
> +#else
> +    fprintf(stderr, "Warning: -timelimit not implemented on this OS\n");

I prefer to specify const char *opt as param and use it rather than
"-timelimit".

> +#endif
> +    return 0;
> +}
> +
>  static const OptionDef options[] = {
>      /* main options */
>      { "L", OPT_EXIT, {(void*)show_license}, "show license" },
> @@ -3844,6 +3861,7 @@ static const OptionDef options[] = {
>      { "dframes", OPT_INT | HAS_ARG, {(void*)&max_frames[CODEC_TYPE_DATA]}, "set the number of data frames to record", "number" },
>      { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
>        "add timings for benchmarking" },
> +    { "timelimit", HAS_ARG, {(void*)opt_timelimit}, "set max runtime in seconds", "limit" },
>      { "dump", OPT_BOOL | OPT_EXPERT, {(void*)&do_pkt_dump},
>        "dump each input packet" },
>      { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},

Regards.
-- 
FFmpeg = Fantastic Fundamental Marvellous Powerful Encoding/decoding Guide



More information about the ffmpeg-devel mailing list