- quicksync_encoder->shutdown();
- quicksync_encoder.reset(new QuickSyncEncoder(filename, surface, va_display, width, height, stream_mux.get(), stream_audio_encoder.get()));
+
+ // Do the shutdown of the old encoder in a separate thread, since it can
+ // take some time (it needs to wait for all the frames in the queue to be
+ // done encoding, for one) and we are running on the main mixer thread.
+ // However, since this means both encoders could be sending packets at
+ // the same time, it means pts could come out of order to the stream mux,
+ // and we need to plug it until the shutdown is complete.
+ stream_mux->plug();
+ lock_guard<mutex> lock(qs_mu);
+ QuickSyncEncoder *old_encoder = quicksync_encoder.release(); // When we go C++14, we can use move capture instead.
+ thread([old_encoder, this]{
+ old_encoder->shutdown();
+ stream_mux->unplug();
+
+ // We cannot delete the encoder here, as this thread has no OpenGL context.
+ // We'll deal with it in begin_frame().
+ lock_guard<mutex> lock(qs_mu);
+ qs_needing_cleanup.emplace_back(old_encoder);
+ }).detach();
+
+ quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, x264_encoder.get(), disk_space_estimator));
+ quicksync_encoder->set_stream_mux(stream_mux.get());
+}
+
+void VideoEncoder::change_x264_bitrate(unsigned rate_kbit)
+{
+ x264_encoder->change_bitrate(rate_kbit);