]> git.sesse.net Git - nageru/blobdiff - quicksync_encoder.cpp
Be much slower about reducing queue length; this was a bit too spammy.
[nageru] / quicksync_encoder.cpp
index b2316dab550235b8ac9c6f5a2ec944b94d9c1823..03bc7065ef1fe9d7e4f56a3480d04b2c2f3576b6 100644 (file)
@@ -193,14 +193,12 @@ FrameReorderer::Frame FrameReorderer::get_first_frame()
 
 class QuickSyncEncoderImpl {
 public:
-       QuickSyncEncoderImpl(QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux);
+       QuickSyncEncoderImpl(const std::string &filename, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux, AudioEncoder *stream_audio_encoder, X264Encoder *x264_encoder);
        ~QuickSyncEncoderImpl();
        void add_audio(int64_t pts, vector<float> audio);
        bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
        RefCountedGLsync end_frame(int64_t pts, int64_t duration, const vector<RefCountedFrame> &input_frames);
        void shutdown();
-       void open_output_file(const std::string &filename);
-       void close_output_file();
 
 private:
        struct storage_task {
@@ -220,6 +218,7 @@ private:
                return int64_t(ip_period - 1) * (TIMEBASE / MAX_FPS);
        }
 
+       void open_output_file(const std::string &filename);
        void encode_thread_func();
        void encode_remaining_frames_as_p(int encoding_frame_num, int gop_start_display_frame_num, int64_t last_dts);
        void add_packet_for_uncompressed_frame(int64_t pts, int64_t duration, const uint8_t *data);
@@ -274,14 +273,14 @@ private:
        QSurface *surface;
 
        unique_ptr<AudioEncoder> file_audio_encoder;
-       unique_ptr<AudioEncoder> stream_audio_encoder;
+       AudioEncoder *stream_audio_encoder;
+
+       unique_ptr<FrameReorderer> reorderer;
+       X264Encoder *x264_encoder;  // nullptr if not using x264.
 
        Mux* stream_mux;  // To HTTP.
        unique_ptr<Mux> file_mux;  // To local disk.
 
-       unique_ptr<FrameReorderer> reorderer;
-       unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
-
        Display *x11_display = nullptr;
 
        // Encoder parameters
@@ -1150,8 +1149,8 @@ int QuickSyncEncoderImpl::setup_encode()
     VAStatus va_status;
     VASurfaceID *tmp_surfaceid;
     int codedbuf_size, i;
-    static VASurfaceID src_surface[SURFACE_NUM];
-    static VASurfaceID ref_surface[SURFACE_NUM];
+    VASurfaceID src_surface[SURFACE_NUM];
+    VASurfaceID ref_surface[SURFACE_NUM];
     
     va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
             &config_attrib[0], config_attrib_num, &config_id);
@@ -1652,9 +1651,7 @@ void QuickSyncEncoderImpl::save_codeddata(storage_task task)
                }
 
                file_audio_encoder->encode_audio(audio, audio_pts + global_delay());
-               if (stream_audio_encoder) {
-                       stream_audio_encoder->encode_audio(audio, audio_pts + global_delay());
-               }
+               stream_audio_encoder->encode_audio(audio, audio_pts + global_delay());
 
                if (audio_pts == task.pts) break;
        }
@@ -1733,15 +1730,12 @@ namespace {
 
 }  // namespace
 
-QuickSyncEncoderImpl::QuickSyncEncoderImpl(QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux)
-       : current_storage_frame(0), surface(surface), stream_mux(stream_mux), frame_width(width), frame_height(height)
+QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux, AudioEncoder *stream_audio_encoder, X264Encoder *x264_encoder)
+       : current_storage_frame(0), surface(surface), stream_audio_encoder(stream_audio_encoder), x264_encoder(x264_encoder), stream_mux(stream_mux), frame_width(width), frame_height(height)
 {
-       if (global_flags.stream_audio_codec_name.empty()) {
-               file_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, { file_mux.get(), stream_mux }));
-       } else {
-               file_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE, { file_mux.get() }));
-               stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate, { stream_mux }));
-       }
+       file_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE));
+       open_output_file(filename);
+       file_audio_encoder->add_mux(file_mux.get());
 
        frame_width_mbaligned = (frame_width + 15) & (~15);
        frame_height_mbaligned = (frame_height + 15) & (~15);
@@ -1753,7 +1747,9 @@ QuickSyncEncoderImpl::QuickSyncEncoderImpl(QSurface *surface, const string &va_d
                reorderer.reset(new FrameReorderer(ip_period - 1, frame_width, frame_height));
        }
        if (global_flags.x264_video_to_http) {
-               x264_encoder.reset(new X264Encoder(stream_mux));
+               assert(x264_encoder != nullptr);
+       } else {
+               assert(x264_encoder == nullptr);
        }
 
        init_va(va_display);
@@ -1778,6 +1774,7 @@ QuickSyncEncoderImpl::QuickSyncEncoderImpl(QSurface *surface, const string &va_d
                        exit(1);
                }
                encode_thread_func();
+               delete_context(context);
        });
 }
 
@@ -1925,7 +1922,6 @@ void QuickSyncEncoderImpl::shutdown()
                frame_queue_nonempty.notify_all();
        }
        encode_thread.join();
-       x264_encoder.reset();
        {
                unique_lock<mutex> lock(storage_task_queue_mutex);
                storage_thread_should_quit = true;
@@ -1937,6 +1933,7 @@ void QuickSyncEncoderImpl::shutdown()
 
        release_encode();
        deinit_va();
+       file_mux.reset();
        is_shutdown = true;
 }
 
@@ -1958,11 +1955,6 @@ void QuickSyncEncoderImpl::open_output_file(const std::string &filename)
        file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, file_audio_encoder->get_codec(), TIMEBASE, DEFAULT_AUDIO_OUTPUT_BIT_RATE, nullptr));
 }
 
-void QuickSyncEncoderImpl::close_output_file()
-{
-        file_mux.reset();
-}
-
 void QuickSyncEncoderImpl::encode_thread_func()
 {
        int64_t last_dts = -1;
@@ -2058,10 +2050,8 @@ void QuickSyncEncoderImpl::encode_remaining_audio()
        pending_audio_frames.clear();
 
        // Encode any leftover audio in the queues, and also any delayed frames.
+       // Note: stream_audio_encoder is not owned by us, so don't call encode_last_audio().
        file_audio_encoder->encode_last_audio();
-       if (stream_audio_encoder) {
-               stream_audio_encoder->encode_last_audio();
-       }
 }
 
 void QuickSyncEncoderImpl::add_packet_for_uncompressed_frame(int64_t pts, int64_t duration, const uint8_t *data)
@@ -2183,8 +2173,8 @@ void QuickSyncEncoderImpl::encode_frame(QuickSyncEncoderImpl::PendingFrame frame
 }
 
 // Proxy object.
-QuickSyncEncoder::QuickSyncEncoder(QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux)
-       : impl(new QuickSyncEncoderImpl(surface, va_display, width, height, stream_mux)) {}
+QuickSyncEncoder::QuickSyncEncoder(const std::string &filename, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux, AudioEncoder *stream_audio_encoder, X264Encoder *x264_encoder)
+       : impl(new QuickSyncEncoderImpl(filename, surface, va_display, width, height, stream_mux, stream_audio_encoder, x264_encoder)) {}
 
 // Must be defined here because unique_ptr<> destructor needs to know the impl.
 QuickSyncEncoder::~QuickSyncEncoder() {}
@@ -2208,13 +2198,3 @@ void QuickSyncEncoder::shutdown()
 {
        impl->shutdown();
 }
-
-void QuickSyncEncoder::open_output_file(const std::string &filename)
-{
-       impl->open_output_file(filename);
-}
-
-void QuickSyncEncoder::close_output_file()
-{
-       impl->close_output_file();
-}