tmp
authoryu.dongliang <18588496441@163.com>
Fri, 14 Apr 2023 13:18:39 +0000 (21:18 +0800)
committeryu.dongliang <18588496441@163.com>
Fri, 14 Apr 2023 13:18:39 +0000 (21:18 +0800)
simp_ffmpeg_alsa.c

index 91bca41bb7a8d3b28e222183e28d9bed65723a3a..214c379d0f99bb0793f9b54f986d53fd9d251ed9 100644 (file)
@@ -78,7 +78,7 @@ static int _audio_output_init(simp_audio_t* priv, const char* path)
                return -ENOMEM;
        }
 
-       c = avcodec_find_encoder(priv->codec_in->codec_id);
+       c = avcodec_find_encoder(out->audio_codec);
        if (!c)
                return -EINVAL;
 
@@ -94,9 +94,9 @@ static int _audio_output_init(simp_audio_t* priv, const char* path)
                return -ENOMEM;
 
        priv->codec_out->codec_id       = c->id;
-       priv->codec_out->bit_rate       = 64 * 1024;
+//     priv->codec_out->bit_rate       = 64 * 1024;
        priv->codec_out->sample_rate    = 44100;
-       priv->codec_out->sample_fmt     = priv->codec_in->sample_fmt;
+       priv->codec_out->sample_fmt     = c->sample_fmts[0];
        priv->codec_out->channels       = 2;
        priv->codec_out->channel_layout = av_get_default_channel_layout(priv->codec_out->channels);
        priv->codec_out->time_base      = (AVRational){1, priv->codec_out->sample_rate};
@@ -202,7 +202,8 @@ static int _ffmpeg_alsa_open(simp_avio_t* io, const char* in, const char* out)
                        goto error;
        }
 
-       io->priv = priv;
+       io->aopen = 1;
+       io->priv  = priv;
        return 0;
 
 error:
@@ -378,26 +379,51 @@ static void* __ffmpeg_alsa_run(void* arg)
 {
        simp_avio_t*    io   = arg;
        simp_audio_t*   priv = io->priv;
-
        scf_list_t      ih;
 
+       int ret;
+
        scf_list_init(&ih);
 
-       while (!io->exit) {
+       if (priv->ctx_out) {
+               av_dump_format(priv->ctx_out, 0, priv->ctx_out->url, 1);
+
+               if (!(priv->ctx_out->oformat->flags & AVFMT_NOFILE)) {
 
-               int ret = __output_audio(io, priv->codec_out, priv->opkt, priv->oframe);
+                       ret = avio_open(&priv->ctx_out->pb, priv->ctx_out->url, AVIO_FLAG_WRITE);
+                       if (ret < 0) {
+                               scf_loge("Could not open output file '%s'", priv->ctx_out->url);
+                               io->error = ret;
+                               goto end;
+                       }
+               }
+
+               ret = avformat_write_header(priv->ctx_out, NULL);
                if (ret < 0) {
+                       scf_loge("avformat_write_header error, ret: %s\n", av_err2str(ret));
                        io->error = ret;
                        goto end;
                }
+       }
 
-               ret = __input_audio(io, priv->codec_in, priv->ipkt, priv->iframe, &ih);
+       while (!io->exit) {
 
-               av_packet_unref(priv->ipkt);
+               if (priv->ctx_out) {
+                       ret = __output_audio(io, priv->codec_out, priv->opkt, priv->oframe);
+                       if (ret < 0) {
+                               io->error = ret;
+                               goto end;
+                       }
+               }
 
-               if (ret < 0) {
-                       io->error = ret;
-                       goto end;
+               if (priv->ctx_in) {
+                       ret = __input_audio(io, priv->codec_in, priv->ipkt, priv->iframe, &ih);
+
+                       av_packet_unref(priv->ipkt);
+                       if (ret < 0) {
+                               io->error = ret;
+                               goto end;
+                       }
                }
 
                // AEC: audio echo cancel