exit
authoryu.dongliang <18588496441@163.com>
Tue, 11 Apr 2023 12:35:22 +0000 (20:35 +0800)
committeryu.dongliang <18588496441@163.com>
Tue, 11 Apr 2023 12:35:22 +0000 (20:35 +0800)
main.c
simp.c
simp.h
simp_ffmpeg_input.c
simp_ffmpeg_output.c

diff --git a/main.c b/main.c
index 97f438e3494a7fbe087f58b1d964a8deb82e21b5..f498219e0f2915c9680f26e852cb5fa8fd111c82 100644 (file)
--- 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 df17b3b402898d7df1977cc1f6c08f8d636280a7..e114bfe57824fa794b7563c309289dc8ddd99b5b 100644 (file)
--- 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 7b571bd97e6c8360af6b6148455b1cf118b7bdad..6aa2bfa485bbffbda2efd73f522813c6ee2550e0 100644 (file)
--- 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);
index 28b3bea548d9b5c8331c75f426a5ac8efe0047ab..496326e803152ccf6a114bbb61e0d781eb7b3ab9 100644 (file)
@@ -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,
 };
 
index 8e3c1b2e9af8b8534dd91818aa06a6b5f1f6f450..549226d1cc1a249e41ecca6fed8ebe58c5368a6b 100644 (file)
@@ -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;