X-Git-Url: http://git.ieval.ro/?p=fdkaac.git;a=blobdiff_plain;f=src%2Fmain.c;h=ecd104e65f82a5ef62fb2c8e35f48ab6b4d44c24;hp=4397c86bfd150dad098cd4e065dc705b7c8b5efa;hb=bd3b4b343aeb3efce8bab3c35449edcd005ccca5;hpb=406f0e0f4435c8e15e90e78e0367e9c58306cf60 diff --git a/src/main.c b/src/main.c index 4397c86..ecd104e 100644 --- a/src/main.c +++ b/src/main.c @@ -24,8 +24,13 @@ #if HAVE_UNISTD_H #include #endif +#if HAVE_SIGACTION +#include +#endif #ifdef _WIN32 #include +#define WIN32_LEAN_AND_MEAN +#include #endif #include "compat.h" #include "wav_reader.h" @@ -36,6 +41,40 @@ #define PROGNAME "fdkaac" +static volatile g_interrupted = 0; + +#if HAVE_SIGACTION +static void signal_handler(int signum) +{ + g_interrupted = 1; +} +static void handle_signals(void) +{ + int i, sigs[] = { SIGINT, SIGHUP, SIGTERM }; + for (i = 0; i < sizeof(sigs)/sizeof(sigs[0]); ++i) { + struct sigaction sa = { 0 }; + sa.sa_handler = signal_handler; + sa.sa_flags |= SA_RESTART; + sigaction(sigs[i], &sa, 0); + } +} +#elif defined(_WIN32) +static BOOL WINAPI signal_handler(DWORD type) +{ + g_interrupted = 1; + return TRUE; +} + +static void handle_signals(void) +{ + SetConsoleCtrlHandler(signal_handler, TRUE); +} +#else +static void handle_signals(void) +{ +} +#endif + static int read_callback(void *cookie, void *data, uint32_t size) { @@ -462,7 +501,9 @@ int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder, ibuf = malloc(frame_length * format->bytes_per_frame); aacenc_progress_init(&progress, wav_get_length(wavf), format->sample_rate); do { - if (nread) { + if (g_interrupted) + nread = 0; + else if (nread) { if ((nread = wav_read_frames(wavf, ibuf, frame_length)) < 0) { fprintf(stderr, "ERROR: read failed\n"); goto END; @@ -821,6 +862,7 @@ int main(int argc, char **argv) strerror(errno)); goto END; } + handle_signals(); if (!params.transport_format) { uint32_t scale; unsigned framelen = aacinfo.frameLength;