]> git.sesse.net Git - nageru/blobdiff - h264encode.cpp
Release Nageru 1.2.1.
[nageru] / h264encode.cpp
index fbeeba707949284bb0ba982cdd86d46a4b0c01b6..523eae9a47a2ff299807e6444070e259b397354b 100644 (file)
@@ -196,7 +196,7 @@ public:
        ~H264EncoderImpl();
        void add_audio(int64_t pts, vector<float> audio);
        bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
-       void end_frame(RefCountedGLsync fence, int64_t pts, const vector<RefCountedFrame> &input_frames);
+       RefCountedGLsync end_frame(int64_t pts, const vector<RefCountedFrame> &input_frames);
        void shutdown();
 
 private:
@@ -1605,7 +1605,7 @@ void H264EncoderImpl::save_codeddata(storage_task task)
         pkt.data = reinterpret_cast<uint8_t *>(&data[0]);
         pkt.size = data.size();
         pkt.stream_index = 0;
-        if (task.frame_type == FRAME_IDR || task.frame_type == FRAME_I) {
+        if (task.frame_type == FRAME_IDR) {
             pkt.flags = AV_PKT_FLAG_KEY;
         } else {
             pkt.flags = 0;
@@ -1657,6 +1657,7 @@ void H264EncoderImpl::save_codeddata(storage_task task)
         avcodec_encode_audio2(context_audio, &pkt, audio_frame, &got_output);
         if (got_output) {
             pkt.stream_index = 1;
+            pkt.flags = AV_PKT_FLAG_KEY;
             httpd->add_packet(pkt, audio_pts + global_delay, audio_pts + global_delay, HTTPD::DESTINATION_FILE_AND_HTTP);
         }
         // TODO: Delayed frames.
@@ -1878,7 +1879,7 @@ void H264EncoderImpl::add_audio(int64_t pts, vector<float> audio)
        frame_queue_nonempty.notify_all();
 }
 
-void H264EncoderImpl::end_frame(RefCountedGLsync fence, int64_t pts, const vector<RefCountedFrame> &input_frames)
+RefCountedGLsync H264EncoderImpl::end_frame(int64_t pts, const vector<RefCountedFrame> &input_frames)
 {
        assert(!is_shutdown);
 
@@ -1908,16 +1909,20 @@ void H264EncoderImpl::end_frame(RefCountedGLsync fence, int64_t pts, const vecto
 
                glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
                check_error();
-               fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
-               check_error();
        }
 
+       RefCountedGLsync fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
+       check_error();
+       glFlush();  // Make the H.264 thread see the fence as soon as possible.
+       check_error();
+
        {
                unique_lock<mutex> lock(frame_queue_mutex);
                pending_video_frames[current_storage_frame] = PendingFrame{ fence, input_frames, pts };
                ++current_storage_frame;
        }
        frame_queue_nonempty.notify_all();
+       return fence;
 }
 
 void H264EncoderImpl::shutdown()
@@ -2146,9 +2151,9 @@ bool H264Encoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
        return impl->begin_frame(y_tex, cbcr_tex);
 }
 
-void H264Encoder::end_frame(RefCountedGLsync fence, int64_t pts, const vector<RefCountedFrame> &input_frames)
+RefCountedGLsync H264Encoder::end_frame(int64_t pts, const vector<RefCountedFrame> &input_frames)
 {
-       impl->end_frame(fence, pts, input_frames);
+       return impl->end_frame(pts, input_frames);
 }
 
 void H264Encoder::shutdown()