From 968ecc89fc5028777f1f9e6456d68b81544c1494 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Tue, 11 Apr 2023 20:35:22 +0800 Subject: [PATCH] exit --- main.c | 9 +++++- simp.c | 8 ++++++ simp.h | 3 ++ simp_ffmpeg_input.c | 18 +++++++++++- simp_ffmpeg_output.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 97f438e..f498219 100644 --- a/main.c +++ b/main.c @@ -49,7 +49,14 @@ int main(int argc, char* argv[]) return -1; } - while (1) + int c; + + while ('q' != (c = getchar())) { sleep(1); + } + + simp_filter_close(f); + + printf("main quit ok\n"); return 0; } diff --git a/simp.c b/simp.c index df17b3b..e114bfe 100644 --- a/simp.c +++ b/simp.c @@ -106,3 +106,11 @@ int simp_avio_run(simp_avio_t* io) return -EINVAL; } +int simp_avio_stop(simp_avio_t* io) +{ + if (io && io->ops && io->ops->stop) + return io->ops->stop(io); + + return -EINVAL; +} + diff --git a/simp.h b/simp.h index 7b571bd..6aa2bfa 100644 --- a/simp.h +++ b/simp.h @@ -59,6 +59,7 @@ struct simp_avio_s int error; int exit; + int flush; pthread_t tid; pthread_mutex_t mutex; pthread_cond_t cond; @@ -94,6 +95,7 @@ struct simp_avio_ops_s int (*open )(simp_avio_t* io, const char* path); int (*close)(simp_avio_t* io); int (*run )(simp_avio_t* io); + int (*stop )(simp_avio_t* io); }; simp_frame_t* simp_frame_alloc(); @@ -102,6 +104,7 @@ void simp_frame_free (simp_frame_t* f); int simp_avio_open (simp_avio_t** pio, const char* type, const char* path); int simp_avio_close (simp_avio_t* io); int simp_avio_run (simp_avio_t* io); +int simp_avio_stop (simp_avio_t* io); int simp_filter_open (simp_filter_t** pf); int simp_filter_close (simp_filter_t* f); diff --git a/simp_ffmpeg_input.c b/simp_ffmpeg_input.c index 28b3bea..496326e 100644 --- a/simp_ffmpeg_input.c +++ b/simp_ffmpeg_input.c @@ -179,7 +179,7 @@ static int _ffmpeg_input_close(simp_avio_t* io) io->exit = 1; while (-1 != io->tid) { pthread_cond_signal(&io->cond); - pthread_cond_signal(&io->cond); + pthread_cond_wait(&io->cond, &io->mutex); } pthread_mutex_unlock(&io->mutex); @@ -346,6 +346,21 @@ end: return NULL; } +static int _ffmpeg_input_stop(simp_avio_t* io) +{ + if (io) { + pthread_mutex_lock(&io->mutex); + io->exit = 1; + while (-1 != io->tid) { + pthread_cond_signal(&io->cond); + pthread_cond_wait(&io->cond, &io->mutex); + } + pthread_mutex_unlock(&io->mutex); + } + + return 0; +} + static int _ffmpeg_input_run(simp_avio_t* io) { if (pthread_create(&io->tid, NULL, __ffmpeg_input_run, io)) { @@ -362,5 +377,6 @@ simp_avio_ops_t simp_avio_ffmpeg_input = .open = _ffmpeg_input_open, .close = _ffmpeg_input_close, .run = _ffmpeg_input_run, + .stop = _ffmpeg_input_stop, }; diff --git a/simp_ffmpeg_output.c b/simp_ffmpeg_output.c index 8e3c1b2..549226d 100644 --- a/simp_ffmpeg_output.c +++ b/simp_ffmpeg_output.c @@ -380,6 +380,74 @@ static void* __ffmpeg_output_run(void* arg) } } + + if (io->flush) { + pthread_mutex_lock(&io->mutex); + while(!scf_list_empty(&io->vout)) { + l = scf_list_head(&io->vout); + f = scf_list_data(l, simp_frame_t, list); + + scf_list_del(&f->list); + + scf_logd("priv->vidx: %d, frame->pts: %ld\n", priv->vidx, f->frame->pts); + + if (0 == io->error) { + + ret = __ffmpeg_encode(io, priv->vcodec_ctx, priv->vpkt, f->frame, priv->vidx); + if (ret < 0) + scf_loge("ret: %d, frame->pts: %ld\n", ret, f->frame->pts); + + io->error = ret; + } + + simp_frame_free(f); + f = NULL; + } + + while (!scf_list_empty(&io->aout)) { + l = scf_list_head(&io->aout); + f = scf_list_data(l, simp_frame_t, list); + + scf_list_del(&f->list); + + if (0 == io->error) { + ret = av_audio_fifo_write(priv->afifo, (void**)f->frame->data, f->frame->nb_samples); + if (ret < 0) + scf_loge("ret: %d\n", ret); + + io->error = ret; + } + + simp_frame_free(f); + f = NULL; + } + + while (av_audio_fifo_size(priv->afifo) > 0) { + + if (0 == io->error) { + ret = av_audio_fifo_read(priv->afifo, (void**)priv->aframe->data, 1024); + if (ret < 0) { + scf_loge("ret: %d\n", ret); + io->error = ret; + break; + } + + priv->nb_samples += priv->aframe->nb_samples; + priv->aframe->pts = priv->nb_samples; + + scf_logd("aframe->pts: %ld\n", priv->aframe->pts); + + ret = __ffmpeg_encode(io, priv->acodec_ctx, priv->apkt, priv->aframe, priv->aidx); + if (ret < 0) { + scf_loge("ret: %d\n", ret); + io->error = ret; + break; + } + } + } + pthread_mutex_unlock(&io->mutex); + } + end: pthread_mutex_lock(&io->mutex); io->tid = -1; -- 2.25.1