]> git.sesse.net Git - nageru/blobdiff - x264_encoder.h
Release Nageru 1.4.2.
[nageru] / x264_encoder.h
index ef118c6f4410d7e3554aad2a4e8354d6875e1c0b..f607a91df8ab4786f1d27a7df25910ceffe6795f 100644 (file)
 #ifndef _X264ENCODE_H
 #define _X264ENCODE_H 1
 
+#include <sched.h>
 #include <stdint.h>
-
+#include <x264.h>
 #include <atomic>
 #include <condition_variable>
 #include <memory>
 #include <mutex>
-#include <thread>
 #include <queue>
+#include <string>
+#include <thread>
 
 extern "C" {
-#include "x264.h"
 #include <libavformat/avformat.h>
 }
 
@@ -48,7 +49,16 @@ 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;
+       }
 
 private:
        struct QueuedFrame {
@@ -71,10 +81,13 @@ 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;
 
+       std::atomic<unsigned> new_bitrate_kbit{0};  // 0 for no change.
+
        // Protects everything below it.
        std::mutex mu;