]> git.sesse.net Git - nageru/commitdiff
Synchronize the Mux class, since both H264EncoderImpl and X264Encoder are writing...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 18 Apr 2016 17:35:18 +0000 (19:35 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 18 Apr 2016 17:35:18 +0000 (19:35 +0200)
mux.cpp
mux.h

diff --git a/mux.cpp b/mux.cpp
index eff336f5799d01706fd7f9c4a2a0fc99356303f6..534d56abd8e0dfb7fd667d9fb60788b83049f409 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -1,5 +1,6 @@
 #include <assert.h>
 
+#include <mutex>
 #include <string>
 #include <vector>
 
@@ -86,7 +87,10 @@ 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);
@@ -106,9 +110,12 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
                }
        }
 
-       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);
diff --git a/mux.h b/mux.h
index 2eb8eeaa45f1472f471488736b9638ce382b2407..d645916d758c1fa9435cbb39e8fc135b9aaad721 100644 (file)
--- a/mux.h
+++ b/mux.h
@@ -9,6 +9,8 @@ extern "C" {
 #include <libavformat/avio.h>
 }
 
+#include <mutex>
+
 class KeyFrameSignalReceiver {
 public:
        // Needs to automatically turn the flag off again after actually receiving data.
@@ -28,7 +30,8 @@ public:
        void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
 
 private:
-       AVFormatContext *avctx;
+       std::mutex ctx_mu;
+       AVFormatContext *avctx;  // Protected by <ctx_mu>.
        AVStream *avstream_video, *avstream_audio;
        KeyFrameSignalReceiver *keyframe_signal_receiver;
 };