]> git.sesse.net Git - nageru/commitdiff
Fix a thread race that would sometimes cause x264 streaming to go awry.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 22 Nov 2016 23:47:27 +0000 (00:47 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 22 Nov 2016 23:53:10 +0000 (00:53 +0100)
x264_encoder.cpp
x264_encoder.h

index f339e8c8b27e09e7b7f059d3d6d1108daccfd37d..f909f55e555bea9ee3369b62ad8509f207c324ee 100644 (file)
@@ -194,6 +194,7 @@ void X264Encoder::encoder_thread_func()
                // No exit; it's not fatal.
        }
        init_x264();
+       x264_init_done = true;
 
        bool frames_left;
 
index c71cba2f373e7bc3864a23224a734a17ffeb9ead..f607a91df8ab4786f1d27a7df25910ceffe6795f 100644 (file)
@@ -16,6 +16,7 @@
 #ifndef _X264ENCODE_H
 #define _X264ENCODE_H 1
 
+#include <sched.h>
 #include <stdint.h>
 #include <x264.h>
 #include <atomic>
@@ -48,7 +49,12 @@ public:
        // Does not block.
        void add_frame(int64_t pts, int64_t duration, const uint8_t *data);
 
-       std::string get_global_headers() const { return global_headers; }
+       std::string get_global_headers() const {
+               while (!x264_init_done) {
+                       sched_yield();
+               }
+               return global_headers;
+       }
 
        void change_bitrate(unsigned rate_kbit) {
                new_bitrate_kbit = rate_kbit;
@@ -75,6 +81,7 @@ private:
        std::string buffered_sei;  // Will be output before first frame, if any.
 
        std::thread encoder_thread;
+       std::atomic<bool> x264_init_done{false};
        std::atomic<bool> should_quit{false};
        x264_t *x264;
        std::unique_ptr<X264SpeedControl> speed_control;