]> git.sesse.net Git - nageru/blobdiff - mux.cpp
Make the VideoEncoder own the stream audio encoder (and make it unconditionally)...
[nageru] / mux.cpp
diff --git a/mux.cpp b/mux.cpp
index eff336f5799d01706fd7f9c4a2a0fc99356303f6..161194ed3d98b65364ad1fa3fbb5c0ca508ca678 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -1,5 +1,6 @@
 #include <assert.h>
 
+#include <mutex>
 #include <string>
 #include <vector>
 
@@ -86,29 +87,35 @@ Mux::~Mux()
 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
 {
        AVPacket pkt_copy;
-       av_copy_packet(&pkt_copy, &pkt);
+       if (av_copy_packet(&pkt_copy, &pkt) < 0) {
+               fprintf(stderr, "av_copy_packet() failed\n");
+               exit(1);
+       }
        if (pkt.stream_index == 0) {
                pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
                pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
+               pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_video->time_base);
        } else if (pkt.stream_index == 1) {
                pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
                pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
+               pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_audio->time_base);
        } else {
                assert(false);
        }
 
        if (keyframe_signal_receiver) {
                if (pkt.flags & AV_PKT_FLAG_KEY) {
-                       if (avctx->oformat->flags & AVFMT_ALLOW_FLUSH) {
-                               av_write_frame(avctx, nullptr);
-                       }
+                       av_write_frame(avctx, nullptr);
                        keyframe_signal_receiver->signal_keyframe();
                }
        }
 
-       if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
-               fprintf(stderr, "av_interleaved_write_frame() failed\n");
-               exit(1);
+       {
+               lock_guard<mutex> lock(ctx_mu);
+               if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
+                       fprintf(stderr, "av_interleaved_write_frame() failed\n");
+                       exit(1);
+               }
        }
 
        av_packet_unref(&pkt_copy);