+
+ // 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(qs_mu, qs_audio_mu);
+ lock_guard<mutex> lock1(qs_mu, adopt_lock), lock2(qs_audio_mu, adopt_lock);
+ QuickSyncEncoder *old_encoder = quicksync_encoder.release(); // When we go C++14, we can use move capture instead.
+ X264Encoder *old_x264_encoder = nullptr;
+ if (global_flags.x264_video_to_disk) {
+ old_x264_encoder = x264_encoder.release();
+ }
+ thread([old_encoder, old_x264_encoder, this]{
+ old_encoder->shutdown();
+ delete old_x264_encoder;
+ old_encoder->close_file();
+ 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();
+
+ if (global_flags.x264_video_to_disk) {
+ x264_encoder.reset(new X264Encoder(oformat));
+ if (global_flags.x264_video_to_http) {
+ x264_encoder->add_mux(stream_mux.get());
+ }
+ if (overriding_bitrate != 0) {
+ x264_encoder->change_bitrate(overriding_bitrate);
+ }
+ }
+
+ quicksync_encoder.reset(new QuickSyncEncoder(filename, resource_pool, surface, va_display, width, height, oformat, x264_encoder.get(), disk_space_estimator));