File: window-basic-auto-config-test.cpp

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,852 kB
  • sloc: ansic: 202,137; cpp: 112,402; makefile: 868; python: 599; sh: 275; javascript: 19
file content (1366 lines) | stat: -rw-r--r-- 37,777 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
#include <chrono>

#include <QFormLayout>

#include <obs.hpp>
#include <util/platform.h>
#include <util/util_uint64.h>
#include <graphics/vec4.h>
#include <graphics/graphics.h>
#include <graphics/math-extra.h>

#include "window-basic-auto-config.hpp"
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"
#include "obs-app.hpp"

#include "ui_AutoConfigTestPage.h"

#define wiz reinterpret_cast<AutoConfig *>(wizard())

using namespace std;

/* ------------------------------------------------------------------------- */

class TestMode {
	obs_video_info ovi;
	OBSSource source[6];

	static void render_rand(void *, uint32_t cx, uint32_t cy)
	{
		gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
		gs_eparam_t *randomvals[3] = {
			gs_effect_get_param_by_name(solid, "randomvals1"),
			gs_effect_get_param_by_name(solid, "randomvals2"),
			gs_effect_get_param_by_name(solid, "randomvals3")};

		struct vec4 r;

		for (int i = 0; i < 3; i++) {
			vec4_set(&r, rand_float(true) * 100.0f,
				 rand_float(true) * 100.0f,
				 rand_float(true) * 50000.0f + 10000.0f, 0.0f);
			gs_effect_set_vec4(randomvals[i], &r);
		}

		while (gs_effect_loop(solid, "Random"))
			gs_draw_sprite(nullptr, 0, cx, cy);
	}

public:
	inline TestMode()
	{
		obs_get_video_info(&ovi);
		obs_add_main_render_callback(render_rand, this);

		for (uint32_t i = 0; i < 6; i++) {
			source[i] = obs_get_output_source(i);
			obs_source_release(source[i]);
			obs_set_output_source(i, nullptr);
		}
	}

	inline ~TestMode()
	{
		for (uint32_t i = 0; i < 6; i++)
			obs_set_output_source(i, source[i]);

		obs_remove_main_render_callback(render_rand, this);
		obs_reset_video(&ovi);
	}

	inline void SetVideo(int cx, int cy, int fps_num, int fps_den)
	{
		obs_video_info newOVI = ovi;

		newOVI.output_width = (uint32_t)cx;
		newOVI.output_height = (uint32_t)cy;
		newOVI.fps_num = (uint32_t)fps_num;
		newOVI.fps_den = (uint32_t)fps_den;

		obs_reset_video(&newOVI);
	}
};

/* ------------------------------------------------------------------------- */

#define TEST_STR(x) "Basic.AutoConfig.TestPage." x
#define SUBTITLE_TESTING TEST_STR("SubTitle.Testing")
#define SUBTITLE_COMPLETE TEST_STR("SubTitle.Complete")
#define TEST_BW TEST_STR("TestingBandwidth")
#define TEST_BW_NO_OUTPUT TEST_STR("TestingBandwidth.NoOutput")
#define TEST_BW_CONNECTING TEST_STR("TestingBandwidth.Connecting")
#define TEST_BW_CONNECT_FAIL TEST_STR("TestingBandwidth.ConnectFailed")
#define TEST_BW_SERVER TEST_STR("TestingBandwidth.Server")
#define TEST_RES_VAL TEST_STR("TestingRes.Resolution")
#define TEST_RES_FAIL TEST_STR("TestingRes.Fail")
#define TEST_SE TEST_STR("TestingStreamEncoder")
#define TEST_RE TEST_STR("TestingRecordingEncoder")
#define TEST_RESULT_SE TEST_STR("Result.StreamingEncoder")
#define TEST_RESULT_RE TEST_STR("Result.RecordingEncoder")

void AutoConfigTestPage::StartBandwidthStage()
{
	ui->progressLabel->setText(QTStr(TEST_BW));
	testThread = std::thread([this]() { TestBandwidthThread(); });
}

void AutoConfigTestPage::StartStreamEncoderStage()
{
	ui->progressLabel->setText(QTStr(TEST_SE));
	testThread = std::thread([this]() { TestStreamEncoderThread(); });
}

void AutoConfigTestPage::StartRecordingEncoderStage()
{
	ui->progressLabel->setText(QTStr(TEST_RE));
	testThread = std::thread([this]() { TestRecordingEncoderThread(); });
}

void AutoConfigTestPage::GetServers(std::vector<ServerInfo> &servers)
{
	OBSDataAutoRelease settings = obs_data_create();
	obs_data_set_string(settings, "service", wiz->serviceName.c_str());

	obs_properties_t *ppts = obs_get_service_properties("rtmp_common");
	obs_property_t *p = obs_properties_get(ppts, "service");
	obs_property_modified(p, settings);

	p = obs_properties_get(ppts, "server");
	size_t count = obs_property_list_item_count(p);
	servers.reserve(count);

	for (size_t i = 0; i < count; i++) {
		const char *name = obs_property_list_item_name(p, i);
		const char *server = obs_property_list_item_string(p, i);

		if (wiz->CanTestServer(name)) {
			ServerInfo info(name, server);
			servers.push_back(info);
		}
	}

	obs_properties_destroy(ppts);
}

static inline void string_depad_key(string &key)
{
	while (!key.empty()) {
		char ch = key.back();
		if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
			key.pop_back();
		else
			break;
	}
}

const char *FindAudioEncoderFromCodec(const char *type);

static inline bool can_use_output(const char *prot, const char *output,
				  const char *prot_test1,
				  const char *prot_test2 = nullptr)
{
	return (strcmp(prot, prot_test1) == 0 ||
		(prot_test2 && strcmp(prot, prot_test2) == 0)) &&
	       (obs_get_output_flags(output) & OBS_OUTPUT_SERVICE) != 0;
}

static bool return_first_id(void *data, const char *id)
{
	const char **output = (const char **)data;

	*output = id;
	return false;
}

void AutoConfigTestPage::TestBandwidthThread()
{
	bool connected = false;
	bool stopped = false;

	TestMode testMode;
	testMode.SetVideo(128, 128, 60, 1);

	QMetaObject::invokeMethod(this, "Progress", Q_ARG(int, 0));

	/*
	 * create encoders
	 * create output
	 * test for 10 seconds
	 */

	QMetaObject::invokeMethod(this, "UpdateMessage",
				  Q_ARG(QString, QStringLiteral("")));

	/* -----------------------------------*/
	/* create obs objects                 */

	const char *serverType = wiz->customServer ? "rtmp_custom"
						   : "rtmp_common";

	OBSEncoderAutoRelease vencoder = obs_video_encoder_create(
		"obs_x264", "test_x264", nullptr, nullptr);
	OBSEncoderAutoRelease aencoder = obs_audio_encoder_create(
		"ffmpeg_aac", "test_aac", nullptr, 0, nullptr);
	OBSServiceAutoRelease service = obs_service_create(
		serverType, "test_service", nullptr, nullptr);

	/* -----------------------------------*/
	/* configure settings                 */

	// service: "service", "server", "key"
	// vencoder: "bitrate", "rate_control",
	//           obs_service_apply_encoder_settings
	// aencoder: "bitrate"
	// output: "bind_ip" via main config -> "Output", "BindIP"
	//         obs_output_set_service

	OBSDataAutoRelease service_settings = obs_data_create();
	OBSDataAutoRelease vencoder_settings = obs_data_create();
	OBSDataAutoRelease aencoder_settings = obs_data_create();
	OBSDataAutoRelease output_settings = obs_data_create();

	std::string key = wiz->key;
	if (wiz->service == AutoConfig::Service::Twitch) {
		string_depad_key(key);
		key += "?bandwidthtest";
	} else if (wiz->serviceName == "Restream.io" ||
		   wiz->serviceName == "Restream.io - RTMP") {
		string_depad_key(key);
		key += "?test=true";
	} else if (wiz->serviceName == "Restream.io - FTL") {
		string_depad_key(key);
		key += "?test";
	}

	obs_data_set_string(service_settings, "service",
			    wiz->serviceName.c_str());
	obs_data_set_string(service_settings, "key", key.c_str());

	obs_data_set_int(vencoder_settings, "bitrate", wiz->startingBitrate);
	obs_data_set_string(vencoder_settings, "rate_control", "CBR");
	obs_data_set_string(vencoder_settings, "preset", "veryfast");
	obs_data_set_int(vencoder_settings, "keyint_sec", 2);

	obs_data_set_int(aencoder_settings, "bitrate", 32);

	OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
	const char *bind_ip =
		config_get_string(main->Config(), "Output", "BindIP");
	obs_data_set_string(output_settings, "bind_ip", bind_ip);

	const char *ip_family =
		config_get_string(main->Config(), "Output", "IPFamily");
	obs_data_set_string(output_settings, "ip_family", ip_family);

	/* -----------------------------------*/
	/* determine which servers to test    */

	std::vector<ServerInfo> servers;
	if (wiz->customServer)
		servers.emplace_back(wiz->server.c_str(), wiz->server.c_str());
	else
		GetServers(servers);

	/* just use the first server if it only has one alternate server,
	 * or if using Restream or Nimo TV due to their "auto" servers */
	if (servers.size() < 3 ||
	    wiz->serviceName.substr(0, 11) == "Restream.io" ||
	    wiz->serviceName == "Nimo TV") {
		servers.resize(1);

	} else if (wiz->service == AutoConfig::Service::Twitch &&
		   wiz->twitchAuto) {
		/* if using Twitch and "Auto" is available, test 3 closest
		 * server */
		servers.erase(servers.begin() + 1);
		servers.resize(3);
	} else if (wiz->service == AutoConfig::Service::YouTube) {
		/* Only test first set of primary + backup servers */
		servers.resize(2);
	}

	if (!wiz->serviceConfigServers.empty()) {
		if (wiz->service == AutoConfig::Service::Twitch &&
		    wiz->twitchAuto) {
			// servers from Twitch service config replace the "auto" entry
			servers.erase(servers.begin());
		}

		for (auto it = std::rbegin(wiz->serviceConfigServers);
		     it != std::rend(wiz->serviceConfigServers); it++) {
			auto same_server = std::find_if(
				std::begin(servers), std::end(servers),
				[&](const ServerInfo &si) {
					return si.address == it->address;
				});
			if (same_server != std::end(servers))
				servers.erase(same_server);
			servers.emplace(std::begin(servers), it->name.c_str(),
					it->address.c_str());
		}

		if (wiz->service == AutoConfig::Service::Twitch &&
		    wiz->twitchAuto) {
			// see above, only test 3 servers
			// rtmps urls are currently counted as separate servers
			servers.resize(3);
		}
	}

	/* -----------------------------------*/
	/* apply service settings             */

	obs_service_update(service, service_settings);
	obs_service_apply_encoder_settings(service, vencoder_settings,
					   aencoder_settings);

	if (wiz->multitrackVideo.testSuccessful) {
		obs_data_set_int(vencoder_settings, "bitrate",
				 wiz->startingBitrate);
	}

	/* -----------------------------------*/
	/* create output                      */

	/* Check if the service has a preferred output type */
	const char *output_type =
		obs_service_get_preferred_output_type(service);
	if (!output_type ||
	    (obs_get_output_flags(output_type) & OBS_OUTPUT_SERVICE) == 0) {
		/* Otherwise, prefer first-party output types */
		const char *protocol = obs_service_get_protocol(service);

		if (can_use_output(protocol, "rtmp_output", "RTMP", "RTMPS")) {
			output_type = "rtmp_output";
		} else if (can_use_output(protocol, "ffmpeg_hls_muxer",
					  "HLS")) {
			output_type = "ffmpeg_hls_muxer";
		} else if (can_use_output(protocol, "ffmpeg_mpegts_muxer",
					  "SRT", "RIST")) {
			output_type = "ffmpeg_mpegts_muxer";
		}

		/* If third-party protocol, use the first enumerated type */
		if (!output_type)
			obs_enum_output_types_with_protocol(
				protocol, &output_type, return_first_id);

		/* If none, fail */
		if (!output_type) {
			QMetaObject::invokeMethod(
				this, "Failure",
				Q_ARG(QString, QTStr(TEST_BW_NO_OUTPUT)));
			return;
		}
	}

	OBSOutputAutoRelease output =
		obs_output_create(output_type, "test_stream", nullptr, nullptr);
	obs_output_update(output, output_settings);

	const char *audio_codec = obs_output_get_supported_audio_codecs(output);

	if (strcmp(audio_codec, "aac") != 0) {
		const char *id = FindAudioEncoderFromCodec(audio_codec);
		aencoder = obs_audio_encoder_create(id, "test_audio", nullptr,
						    0, nullptr);
	}

	/* -----------------------------------*/
	/* connect encoders/services/outputs  */

	obs_encoder_update(vencoder, vencoder_settings);
	obs_encoder_update(aencoder, aencoder_settings);
	obs_encoder_set_video(vencoder, obs_get_video());
	obs_encoder_set_audio(aencoder, obs_get_audio());

	obs_output_set_video_encoder(output, vencoder);
	obs_output_set_audio_encoder(output, aencoder, 0);
	obs_output_set_reconnect_settings(output, 0, 0);

	obs_output_set_service(output, service);

	/* -----------------------------------*/
	/* connect signals                    */

	auto on_started = [&]() {
		unique_lock<mutex> lock(m);
		connected = true;
		stopped = false;
		cv.notify_one();
	};

	auto on_stopped = [&]() {
		unique_lock<mutex> lock(m);
		connected = false;
		stopped = true;
		cv.notify_one();
	};

	using on_started_t = decltype(on_started);
	using on_stopped_t = decltype(on_stopped);

	auto pre_on_started = [](void *data, calldata_t *) {
		on_started_t &on_started =
			*reinterpret_cast<on_started_t *>(data);
		on_started();
	};

	auto pre_on_stopped = [](void *data, calldata_t *) {
		on_stopped_t &on_stopped =
			*reinterpret_cast<on_stopped_t *>(data);
		on_stopped();
	};

	signal_handler *sh = obs_output_get_signal_handler(output);
	signal_handler_connect(sh, "start", pre_on_started, &on_started);
	signal_handler_connect(sh, "stop", pre_on_stopped, &on_stopped);

	/* -----------------------------------*/
	/* test servers                       */

	bool success = false;

	for (size_t i = 0; i < servers.size(); i++) {
		auto &server = servers[i];

		connected = false;
		stopped = false;

		int per = int((i + 1) * 100 / servers.size());
		QMetaObject::invokeMethod(this, "Progress", Q_ARG(int, per));
		QMetaObject::invokeMethod(
			this, "UpdateMessage",
			Q_ARG(QString, QTStr(TEST_BW_CONNECTING)
					       .arg(server.name.c_str())));

		obs_data_set_string(service_settings, "server",
				    server.address.c_str());
		obs_service_update(service, service_settings);

		if (!obs_output_start(output))
			continue;

		unique_lock<mutex> ul(m);
		if (cancel) {
			ul.unlock();
			obs_output_force_stop(output);
			return;
		}
		if (!stopped && !connected)
			cv.wait(ul);
		if (cancel) {
			ul.unlock();
			obs_output_force_stop(output);
			return;
		}
		if (!connected)
			continue;

		QMetaObject::invokeMethod(
			this, "UpdateMessage",
			Q_ARG(QString,
			      QTStr(TEST_BW_SERVER).arg(server.name.c_str())));

		/* ignore first 2.5 seconds due to possible buffering skewing
		 * the result */
		cv.wait_for(ul, chrono::milliseconds(2500));
		if (stopped)
			continue;
		if (cancel) {
			ul.unlock();
			obs_output_force_stop(output);
			return;
		}

		/* continue test */
		int start_bytes = (int)obs_output_get_total_bytes(output);
		uint64_t t_start = os_gettime_ns();

		cv.wait_for(ul, chrono::seconds(10));
		if (stopped)
			continue;
		if (cancel) {
			ul.unlock();
			obs_output_force_stop(output);
			return;
		}

		obs_output_stop(output);
		cv.wait(ul);

		uint64_t total_time = os_gettime_ns() - t_start;
		if (total_time == 0)
			total_time = 1;

		int total_bytes =
			(int)obs_output_get_total_bytes(output) - start_bytes;
		uint64_t bitrate = util_mul_div64(
			total_bytes, 8ULL * 1000000000ULL / 1000ULL,
			total_time);
		if (obs_output_get_frames_dropped(output) ||
		    (int)bitrate < (wiz->startingBitrate * 75 / 100)) {
			server.bitrate = (int)bitrate * 70 / 100;
		} else {
			server.bitrate = wiz->startingBitrate;
		}

		server.ms = obs_output_get_connect_time_ms(output);
		success = true;
	}

	if (!success) {
		QMetaObject::invokeMethod(this, "Failure",
					  Q_ARG(QString,
						QTStr(TEST_BW_CONNECT_FAIL)));
		return;
	}

	int bestBitrate = 0;
	int bestMS = 0x7FFFFFFF;
	string bestServer;
	string bestServerName;

	for (auto &server : servers) {
		bool close = abs(server.bitrate - bestBitrate) < 400;

		if ((!close && server.bitrate > bestBitrate) ||
		    (close && server.ms < bestMS)) {
			bestServer = server.address;
			bestServerName = server.name;
			bestBitrate = server.bitrate;
			bestMS = server.ms;
		}
	}

	wiz->server = std::move(bestServer);
	wiz->serverName = std::move(bestServerName);
	wiz->idealBitrate = bestBitrate;

	QMetaObject::invokeMethod(this, "NextStage");
}

/* this is used to estimate the lower bitrate limit for a given
 * resolution/fps.  yes, it is a totally arbitrary equation that gets
 * the closest to the expected values */
static long double EstimateBitrateVal(int cx, int cy, int fps_num, int fps_den)
{
	long fps = (long double)fps_num / (long double)fps_den;
	long double areaVal = pow((long double)(cx * cy), 0.85l);
	return areaVal * sqrt(pow(fps, 1.1l));
}

static long double EstimateMinBitrate(int cx, int cy, int fps_num, int fps_den)
{
	long double val = EstimateBitrateVal(1920, 1080, 60, 1) / 5800.0l;
	return EstimateBitrateVal(cx, cy, fps_num, fps_den) / val;
}

static long double EstimateUpperBitrate(int cx, int cy, int fps_num,
					int fps_den)
{
	long double val = EstimateBitrateVal(1280, 720, 30, 1) / 3000.0l;
	return EstimateBitrateVal(cx, cy, fps_num, fps_den) / val;
}

struct Result {
	int cx;
	int cy;
	int fps_num;
	int fps_den;

	inline Result(int cx_, int cy_, int fps_num_, int fps_den_)
		: cx(cx_),
		  cy(cy_),
		  fps_num(fps_num_),
		  fps_den(fps_den_)
	{
	}
};

static void CalcBaseRes(int &baseCX, int &baseCY)
{
	const int maxBaseArea = 1920 * 1200;
	const int clipResArea = 1920 * 1080;

	/* if base resolution unusually high, recalculate to a more reasonable
	 * value to start the downscaling at, based upon 1920x1080's area.
	 *
	 * for 16:9 resolutions this will always change the starting value to
	 * 1920x1080 */
	if ((baseCX * baseCY) > maxBaseArea) {
		long double xyAspect =
			(long double)baseCX / (long double)baseCY;
		baseCY = (int)sqrt((long double)clipResArea / xyAspect);
		baseCX = (int)((long double)baseCY * xyAspect);
	}
}

bool AutoConfigTestPage::TestSoftwareEncoding()
{
	TestMode testMode;
	QMetaObject::invokeMethod(this, "UpdateMessage",
				  Q_ARG(QString, QStringLiteral("")));

	/* -----------------------------------*/
	/* create obs objects                 */

	OBSEncoderAutoRelease vencoder = obs_video_encoder_create(
		"obs_x264", "test_x264", nullptr, nullptr);
	OBSEncoderAutoRelease aencoder = obs_audio_encoder_create(
		"ffmpeg_aac", "test_aac", nullptr, 0, nullptr);
	OBSOutputAutoRelease output =
		obs_output_create("null_output", "null", nullptr, nullptr);

	/* -----------------------------------*/
	/* configure settings                 */

	OBSDataAutoRelease aencoder_settings = obs_data_create();
	OBSDataAutoRelease vencoder_settings = obs_data_create();
	obs_data_set_int(aencoder_settings, "bitrate", 32);

	if (wiz->type != AutoConfig::Type::Recording) {
		obs_data_set_int(vencoder_settings, "keyint_sec", 2);
		obs_data_set_int(vencoder_settings, "bitrate",
				 wiz->idealBitrate);
		obs_data_set_string(vencoder_settings, "rate_control", "CBR");
		obs_data_set_string(vencoder_settings, "profile", "main");
		obs_data_set_string(vencoder_settings, "preset", "veryfast");
	} else {
		obs_data_set_int(vencoder_settings, "crf", 20);
		obs_data_set_string(vencoder_settings, "rate_control", "CRF");
		obs_data_set_string(vencoder_settings, "profile", "high");
		obs_data_set_string(vencoder_settings, "preset", "veryfast");
	}

	/* -----------------------------------*/
	/* apply settings                     */

	obs_encoder_update(vencoder, vencoder_settings);
	obs_encoder_update(aencoder, aencoder_settings);

	/* -----------------------------------*/
	/* connect encoders/services/outputs  */

	obs_output_set_video_encoder(output, vencoder);
	obs_output_set_audio_encoder(output, aencoder, 0);

	/* -----------------------------------*/
	/* connect signals                    */

	auto on_stopped = [&]() {
		unique_lock<mutex> lock(m);
		cv.notify_one();
	};

	using on_stopped_t = decltype(on_stopped);

	auto pre_on_stopped = [](void *data, calldata_t *) {
		on_stopped_t &on_stopped =
			*reinterpret_cast<on_stopped_t *>(data);
		on_stopped();
	};

	signal_handler *sh = obs_output_get_signal_handler(output);
	signal_handler_connect(sh, "deactivate", pre_on_stopped, &on_stopped);

	/* -----------------------------------*/
	/* calculate starting resolution      */

	int baseCX = wiz->baseResolutionCX;
	int baseCY = wiz->baseResolutionCY;
	CalcBaseRes(baseCX, baseCY);

	/* -----------------------------------*/
	/* calculate starting test rates      */

	int pcores = os_get_physical_cores();
	int lcores = os_get_logical_cores();
	int maxDataRate;
	if (lcores > 8 || pcores > 4) {
		/* superb */
		maxDataRate = 1920 * 1200 * 60 + 1000;

	} else if (lcores > 4 && pcores == 4) {
		/* great */
		maxDataRate = 1920 * 1080 * 60 + 1000;

	} else if (pcores == 4) {
		/* okay */
		maxDataRate = 1920 * 1080 * 30 + 1000;

	} else {
		/* toaster */
		maxDataRate = 960 * 540 * 30 + 1000;
	}

	/* -----------------------------------*/
	/* perform tests                      */

	vector<Result> results;
	int i = 0;
	int count = 1;

	auto testRes = [&](int cy, int fps_num, int fps_den, bool force) {
		int per = ++i * 100 / count;
		QMetaObject::invokeMethod(this, "Progress", Q_ARG(int, per));

		if (cy > baseCY)
			return true;

		/* no need for more than 3 tests max */
		if (results.size() >= 3)
			return true;

		if (!fps_num || !fps_den) {
			fps_num = wiz->specificFPSNum;
			fps_den = wiz->specificFPSDen;
		}

		long double fps = ((long double)fps_num / (long double)fps_den);

		int cx = int(((long double)baseCX / (long double)baseCY) *
			     (long double)cy);

		if (!force && wiz->type != AutoConfig::Type::Recording) {
			int est = EstimateMinBitrate(cx, cy, fps_num, fps_den);
			if (est > wiz->idealBitrate)
				return true;
		}

		long double rate = (long double)cx * (long double)cy * fps;
		if (!force && rate > maxDataRate)
			return true;

		testMode.SetVideo(cx, cy, fps_num, fps_den);

		obs_encoder_set_video(vencoder, obs_get_video());
		obs_encoder_set_audio(aencoder, obs_get_audio());
		obs_encoder_update(vencoder, vencoder_settings);

		obs_output_set_media(output, obs_get_video(), obs_get_audio());

		QString cxStr = QString::number(cx);
		QString cyStr = QString::number(cy);

		QString fpsStr = (fps_den > 1) ? QString::number(fps, 'f', 2)
					       : QString::number(fps, 'g', 2);

		QMetaObject::invokeMethod(
			this, "UpdateMessage",
			Q_ARG(QString,
			      QTStr(TEST_RES_VAL).arg(cxStr, cyStr, fpsStr)));

		unique_lock<mutex> ul(m);
		if (cancel)
			return false;

		if (!obs_output_start(output)) {
			QMetaObject::invokeMethod(this, "Failure",
						  Q_ARG(QString,
							QTStr(TEST_RES_FAIL)));
			return false;
		}

		cv.wait_for(ul, chrono::seconds(5));

		obs_output_stop(output);
		cv.wait(ul);

		int skipped =
			(int)video_output_get_skipped_frames(obs_get_video());
		if (force || skipped <= 10)
			results.emplace_back(cx, cy, fps_num, fps_den);

		return !cancel;
	};

	if (wiz->specificFPSNum && wiz->specificFPSDen) {
		count = 7;
		if (!testRes(2160, 0, 0, false))
			return false;
		if (!testRes(1440, 0, 0, false))
			return false;
		if (!testRes(1080, 0, 0, false))
			return false;
		if (!testRes(720, 0, 0, false))
			return false;
		if (!testRes(480, 0, 0, false))
			return false;
		if (!testRes(360, 0, 0, false))
			return false;
		if (!testRes(240, 0, 0, true))
			return false;
	} else {
		count = 14;
		if (!testRes(2160, 60, 1, false))
			return false;
		if (!testRes(2160, 30, 1, false))
			return false;
		if (!testRes(1440, 60, 1, false))
			return false;
		if (!testRes(1440, 30, 1, false))
			return false;
		if (!testRes(1080, 60, 1, false))
			return false;
		if (!testRes(1080, 30, 1, false))
			return false;
		if (!testRes(720, 60, 1, false))
			return false;
		if (!testRes(720, 30, 1, false))
			return false;
		if (!testRes(480, 60, 1, false))
			return false;
		if (!testRes(480, 30, 1, false))
			return false;
		if (!testRes(360, 60, 1, false))
			return false;
		if (!testRes(360, 30, 1, false))
			return false;
		if (!testRes(240, 60, 1, false))
			return false;
		if (!testRes(240, 30, 1, true))
			return false;
	}

	/* -----------------------------------*/
	/* find preferred settings            */

	int minArea = 960 * 540 + 1000;

	if (!wiz->specificFPSNum && wiz->preferHighFPS && results.size() > 1) {
		Result &result1 = results[0];
		Result &result2 = results[1];

		if (result1.fps_num == 30 && result2.fps_num == 60) {
			int nextArea = result2.cx * result2.cy;
			if (nextArea >= minArea)
				results.erase(results.begin());
		}
	}

	Result result = results.front();
	wiz->idealResolutionCX = result.cx;
	wiz->idealResolutionCY = result.cy;
	wiz->idealFPSNum = result.fps_num;
	wiz->idealFPSDen = result.fps_den;

	long double fUpperBitrate = EstimateUpperBitrate(
		result.cx, result.cy, result.fps_num, result.fps_den);

	int upperBitrate = int(floor(fUpperBitrate / 50.0l) * 50.0l);

	if (wiz->streamingEncoder != AutoConfig::Encoder::x264) {
		upperBitrate *= 114;
		upperBitrate /= 100;
	}

	if (wiz->testMultitrackVideo && wiz->multitrackVideo.testSuccessful &&
	    !wiz->multitrackVideo.bitrate.has_value())
		wiz->multitrackVideo.bitrate = wiz->idealBitrate;

	if (wiz->idealBitrate > upperBitrate)
		wiz->idealBitrate = upperBitrate;

	softwareTested = true;
	return true;
}

void AutoConfigTestPage::FindIdealHardwareResolution()
{
	int baseCX = wiz->baseResolutionCX;
	int baseCY = wiz->baseResolutionCY;
	CalcBaseRes(baseCX, baseCY);

	vector<Result> results;

	int pcores = os_get_physical_cores();
	int maxDataRate;
	if (pcores >= 4) {
		maxDataRate = 1920 * 1200 * 60 + 1000;
	} else {
		maxDataRate = 1280 * 720 * 30 + 1000;
	}

	auto testRes = [&](int cy, int fps_num, int fps_den, bool force) {
		if (cy > baseCY)
			return;

		if (results.size() >= 3)
			return;

		if (!fps_num || !fps_den) {
			fps_num = wiz->specificFPSNum;
			fps_den = wiz->specificFPSDen;
		}

		long double fps = ((long double)fps_num / (long double)fps_den);

		int cx = int(((long double)baseCX / (long double)baseCY) *
			     (long double)cy);

		long double rate = (long double)cx * (long double)cy * fps;
		if (!force && rate > maxDataRate)
			return;

		AutoConfig::Encoder encType = wiz->streamingEncoder;
		bool nvenc = encType == AutoConfig::Encoder::NVENC;

		int minBitrate = EstimateMinBitrate(cx, cy, fps_num, fps_den);

		/* most hardware encoders don't have a good quality to bitrate
		 * ratio, so increase the minimum bitrate estimate for them.
		 * NVENC currently is the exception because of the improvements
		 * its made to its quality in recent generations. */
		if (!nvenc)
			minBitrate = minBitrate * 114 / 100;

		if (wiz->type == AutoConfig::Type::Recording)
			force = true;
		if (force || wiz->idealBitrate >= minBitrate)
			results.emplace_back(cx, cy, fps_num, fps_den);
	};

	if (wiz->specificFPSNum && wiz->specificFPSDen) {
		testRes(2160, 0, 0, false);
		testRes(1440, 0, 0, false);
		testRes(1080, 0, 0, false);
		testRes(720, 0, 0, false);
		testRes(480, 0, 0, false);
		testRes(360, 0, 0, false);
		testRes(240, 0, 0, true);
	} else {
		testRes(2160, 60, 1, false);
		testRes(2160, 30, 1, false);
		testRes(1440, 60, 1, false);
		testRes(1440, 30, 1, false);
		testRes(1080, 60, 1, false);
		testRes(1080, 30, 1, false);
		testRes(720, 60, 1, false);
		testRes(720, 30, 1, false);
		testRes(480, 60, 1, false);
		testRes(480, 30, 1, false);
		testRes(360, 60, 1, false);
		testRes(360, 30, 1, false);
		testRes(240, 60, 1, false);
		testRes(240, 30, 1, true);
	}

	int minArea = 960 * 540 + 1000;

	if (!wiz->specificFPSNum && wiz->preferHighFPS && results.size() > 1) {
		Result &result1 = results[0];
		Result &result2 = results[1];

		if (result1.fps_num == 30 && result2.fps_num == 60) {
			int nextArea = result2.cx * result2.cy;
			if (nextArea >= minArea)
				results.erase(results.begin());
		}
	}

	Result result = results.front();
	wiz->idealResolutionCX = result.cx;
	wiz->idealResolutionCY = result.cy;
	wiz->idealFPSNum = result.fps_num;
	wiz->idealFPSDen = result.fps_den;
}

void AutoConfigTestPage::TestStreamEncoderThread()
{
	bool preferHardware = wiz->preferHardware;
	if (!softwareTested) {
		if (!preferHardware || !wiz->hardwareEncodingAvailable) {
			if (!TestSoftwareEncoding()) {
				return;
			}
		}
	}

	if (!softwareTested) {
		if (wiz->nvencAvailable)
			wiz->streamingEncoder = AutoConfig::Encoder::NVENC;
		else if (wiz->qsvAvailable)
			wiz->streamingEncoder = AutoConfig::Encoder::QSV;
		else if (wiz->appleAvailable)
			wiz->streamingEncoder = AutoConfig::Encoder::Apple;
		else
			wiz->streamingEncoder = AutoConfig::Encoder::AMD;
	} else {
		wiz->streamingEncoder = AutoConfig::Encoder::x264;
	}

#ifdef __linux__
	// On linux CBR rate control is not guaranteed so fallback to x264.
	if (wiz->streamingEncoder == AutoConfig::Encoder::QSV) {
		wiz->streamingEncoder = AutoConfig::Encoder::x264;
		if (!TestSoftwareEncoding()) {
			return;
		}
	}
#endif

	if (preferHardware && !softwareTested && wiz->hardwareEncodingAvailable)
		FindIdealHardwareResolution();

	QMetaObject::invokeMethod(this, "NextStage");
}

void AutoConfigTestPage::TestRecordingEncoderThread()
{
	if (!wiz->hardwareEncodingAvailable && !softwareTested) {
		if (!TestSoftwareEncoding()) {
			return;
		}
	}

	if (wiz->type == AutoConfig::Type::Recording &&
	    wiz->hardwareEncodingAvailable)
		FindIdealHardwareResolution();

	wiz->recordingQuality = AutoConfig::Quality::High;

	bool recordingOnly = wiz->type == AutoConfig::Type::Recording;

	if (wiz->hardwareEncodingAvailable) {
		if (wiz->nvencAvailable)
			wiz->recordingEncoder = AutoConfig::Encoder::NVENC;
		else if (wiz->qsvAvailable)
			wiz->recordingEncoder = AutoConfig::Encoder::QSV;
		else if (wiz->appleAvailable)
			wiz->recordingEncoder = AutoConfig::Encoder::Apple;
		else
			wiz->recordingEncoder = AutoConfig::Encoder::AMD;
	} else {
		wiz->recordingEncoder = AutoConfig::Encoder::x264;
	}

	if (wiz->recordingEncoder != AutoConfig::Encoder::NVENC) {
		if (!recordingOnly) {
			wiz->recordingEncoder = AutoConfig::Encoder::Stream;
			wiz->recordingQuality = AutoConfig::Quality::Stream;
		}
	}

	QMetaObject::invokeMethod(this, "NextStage");
}

#define ENCODER_TEXT(x) "Basic.Settings.Output.Simple.Encoder." x
#define ENCODER_SOFTWARE ENCODER_TEXT("Software")
#define ENCODER_NVENC ENCODER_TEXT("Hardware.NVENC.H264")
#define ENCODER_QSV ENCODER_TEXT("Hardware.QSV.H264")
#define ENCODER_AMD ENCODER_TEXT("Hardware.AMD.H264")
#define ENCODER_APPLE ENCODER_TEXT("Hardware.Apple.H264")

#define QUALITY_SAME "Basic.Settings.Output.Simple.RecordingQuality.Stream"
#define QUALITY_HIGH "Basic.Settings.Output.Simple.RecordingQuality.Small"

void set_closest_res(int &cx, int &cy, struct obs_service_resolution *res_list,
		     size_t count)
{
	int best_pixel_diff = 0x7FFFFFFF;
	int start_cx = cx;
	int start_cy = cy;

	for (size_t i = 0; i < count; i++) {
		struct obs_service_resolution &res = res_list[i];
		int pixel_cx_diff = abs(start_cx - res.cx);
		int pixel_cy_diff = abs(start_cy - res.cy);
		int pixel_diff = pixel_cx_diff + pixel_cy_diff;

		if (pixel_diff < best_pixel_diff) {
			best_pixel_diff = pixel_diff;
			cx = res.cx;
			cy = res.cy;
		}
	}
}

void AutoConfigTestPage::FinalizeResults()
{
	ui->stackedWidget->setCurrentIndex(1);
	setSubTitle(QTStr(SUBTITLE_COMPLETE));

	QFormLayout *form = results;

	auto encName = [](AutoConfig::Encoder enc) -> QString {
		switch (enc) {
		case AutoConfig::Encoder::x264:
			return QTStr(ENCODER_SOFTWARE);
		case AutoConfig::Encoder::NVENC:
			return QTStr(ENCODER_NVENC);
		case AutoConfig::Encoder::QSV:
			return QTStr(ENCODER_QSV);
		case AutoConfig::Encoder::AMD:
			return QTStr(ENCODER_AMD);
		case AutoConfig::Encoder::Apple:
			return QTStr(ENCODER_APPLE);
		case AutoConfig::Encoder::Stream:
			return QTStr(QUALITY_SAME);
		}

		return QTStr(ENCODER_SOFTWARE);
	};

	auto newLabel = [this](const char *str) -> QLabel * {
		return new QLabel(QTStr(str), this);
	};

	if (wiz->type == AutoConfig::Type::Streaming) {
		const char *serverType = wiz->customServer ? "rtmp_custom"
							   : "rtmp_common";

		OBSServiceAutoRelease service = obs_service_create(
			serverType, "temp_service", nullptr, nullptr);

		OBSDataAutoRelease service_settings = obs_data_create();
		OBSDataAutoRelease vencoder_settings = obs_data_create();

		if (wiz->testMultitrackVideo &&
		    wiz->multitrackVideo.testSuccessful &&
		    !wiz->multitrackVideo.bitrate.has_value())
			wiz->multitrackVideo.bitrate = wiz->idealBitrate;

		obs_data_set_int(vencoder_settings, "bitrate",
				 wiz->idealBitrate);

		obs_data_set_string(service_settings, "service",
				    wiz->serviceName.c_str());
		obs_service_update(service, service_settings);
		obs_service_apply_encoder_settings(service, vencoder_settings,
						   nullptr);

		BPtr<obs_service_resolution> res_list;
		size_t res_count;
		int maxFPS;
		obs_service_get_supported_resolutions(service, &res_list,
						      &res_count);
		obs_service_get_max_fps(service, &maxFPS);

		if (res_list) {
			set_closest_res(wiz->idealResolutionCX,
					wiz->idealResolutionCY, res_list,
					res_count);
		}
		if (maxFPS) {
			double idealFPS = (double)wiz->idealFPSNum /
					  (double)wiz->idealFPSDen;
			if (idealFPS > (double)maxFPS) {
				wiz->idealFPSNum = maxFPS;
				wiz->idealFPSDen = 1;
			}
		}

		wiz->idealBitrate =
			(int)obs_data_get_int(vencoder_settings, "bitrate");

		if (!wiz->customServer)
			form->addRow(
				newLabel("Basic.AutoConfig.StreamPage.Service"),
				new QLabel(wiz->serviceName.c_str(),
					   ui->finishPage));
		form->addRow(newLabel("Basic.AutoConfig.StreamPage.Server"),
			     new QLabel(wiz->serverName.c_str(),
					ui->finishPage));
		form->addRow(
			newLabel("Basic.Settings.Stream.MultitrackVideoLabel"),
			newLabel(wiz->multitrackVideo.testSuccessful ? "Yes"
								     : "No"));

		if (wiz->multitrackVideo.testSuccessful) {
			form->addRow(
				newLabel("Basic.Settings.Output.VideoBitrate"),
				newLabel("Automatic"));
			form->addRow(newLabel(TEST_RESULT_SE),
				     newLabel("Automatic"));
			form->addRow(
				newLabel(
					"Basic.AutoConfig.TestPage.Result.StreamingResolution"),
				newLabel("Automatic"));
		} else {
			form->addRow(
				newLabel("Basic.Settings.Output.VideoBitrate"),
				new QLabel(QString::number(wiz->idealBitrate),
					   ui->finishPage));
			form->addRow(newLabel(TEST_RESULT_SE),
				     new QLabel(encName(wiz->streamingEncoder),
						ui->finishPage));
		}
	}

	QString baseRes =
		QString("%1x%2").arg(QString::number(wiz->baseResolutionCX),
				     QString::number(wiz->baseResolutionCY));
	QString scaleRes =
		QString("%1x%2").arg(QString::number(wiz->idealResolutionCX),
				     QString::number(wiz->idealResolutionCY));

	if (wiz->recordingEncoder != AutoConfig::Encoder::Stream ||
	    wiz->recordingQuality != AutoConfig::Quality::Stream)
		form->addRow(newLabel(TEST_RESULT_RE),
			     new QLabel(encName(wiz->recordingEncoder),
					ui->finishPage));

	QString recQuality;

	switch (wiz->recordingQuality) {
	case AutoConfig::Quality::High:
		recQuality = QTStr(QUALITY_HIGH);
		break;
	case AutoConfig::Quality::Stream:
		recQuality = QTStr(QUALITY_SAME);
		break;
	}

	form->addRow(newLabel("Basic.Settings.Output.Simple.RecordingQuality"),
		     new QLabel(recQuality, ui->finishPage));

	long double fps =
		(long double)wiz->idealFPSNum / (long double)wiz->idealFPSDen;

	QString fpsStr = (wiz->idealFPSDen > 1) ? QString::number(fps, 'f', 2)
						: QString::number(fps, 'g', 2);

	form->addRow(newLabel("Basic.Settings.Video.BaseResolution"),
		     new QLabel(baseRes, ui->finishPage));
	form->addRow(newLabel("Basic.Settings.Video.ScaledResolution"),
		     new QLabel(scaleRes, ui->finishPage));
	form->addRow(newLabel("Basic.Settings.Video.FPS"),
		     new QLabel(fpsStr, ui->finishPage));

	// FIXME: form layout is super squished, probably need to set proper sizepolicy on all widgets?
}

#define STARTING_SEPARATOR \
	"\n==== Auto-config wizard testing commencing ======\n"
#define STOPPING_SEPARATOR \
	"\n==== Auto-config wizard testing stopping ========\n"

void AutoConfigTestPage::NextStage()
{
	if (testThread.joinable())
		testThread.join();
	if (cancel)
		return;

	ui->subProgressLabel->setText(QString());

	/* make it skip to bandwidth stage if only set to config recording */
	if (stage == Stage::Starting) {
		if (!started) {
			blog(LOG_INFO, STARTING_SEPARATOR);
			started = true;
		}

		if (wiz->type != AutoConfig::Type::Streaming) {
			stage = Stage::StreamEncoder;
		} else if (!wiz->bandwidthTest) {
			stage = Stage::BandwidthTest;
		}
	}

	if (stage == Stage::Starting) {
		stage = Stage::BandwidthTest;
		StartBandwidthStage();

	} else if (stage == Stage::BandwidthTest) {
		stage = Stage::StreamEncoder;
		StartStreamEncoderStage();

	} else if (stage == Stage::StreamEncoder) {
		stage = Stage::RecordingEncoder;
		StartRecordingEncoderStage();

	} else {
		stage = Stage::Finished;
		FinalizeResults();
		emit completeChanged();
	}
}

void AutoConfigTestPage::UpdateMessage(QString message)
{
	ui->subProgressLabel->setText(message);
}

void AutoConfigTestPage::Failure(QString message)
{
	ui->errorLabel->setText(message);
	ui->stackedWidget->setCurrentIndex(2);
}

void AutoConfigTestPage::Progress(int percentage)
{
	ui->progressBar->setValue(percentage);
}

AutoConfigTestPage::AutoConfigTestPage(QWidget *parent)
	: QWizardPage(parent),
	  ui(new Ui_AutoConfigTestPage)
{
	ui->setupUi(this);
	setTitle(QTStr("Basic.AutoConfig.TestPage"));
	setSubTitle(QTStr(SUBTITLE_TESTING));
	setCommitPage(true);
}

AutoConfigTestPage::~AutoConfigTestPage()
{
	if (testThread.joinable()) {
		{
			unique_lock<mutex> ul(m);
			cancel = true;
			cv.notify_one();
		}
		testThread.join();
	}

	if (started)
		blog(LOG_INFO, STOPPING_SEPARATOR);
}

void AutoConfigTestPage::initializePage()
{
	if (wiz->type == AutoConfig::Type::VirtualCam) {
		wiz->idealResolutionCX = wiz->baseResolutionCX;
		wiz->idealResolutionCY = wiz->baseResolutionCY;
		wiz->idealFPSNum = 30;
		wiz->idealFPSDen = 1;
		stage = Stage::Finished;
	} else {
		stage = Stage::Starting;
	}

	setSubTitle(QTStr(SUBTITLE_TESTING));
	softwareTested = false;
	cancel = false;
	DeleteLayout(results);
	results = new QFormLayout();
	results->setContentsMargins(0, 0, 0, 0);
	ui->finishPageLayout->insertLayout(1, results);
	ui->stackedWidget->setCurrentIndex(0);
	NextStage();
}

void AutoConfigTestPage::cleanupPage()
{
	if (testThread.joinable()) {
		{
			unique_lock<mutex> ul(m);
			cancel = true;
			cv.notify_one();
		}
		testThread.join();
	}
}

bool AutoConfigTestPage::isComplete() const
{
	return stage == Stage::Finished;
}

int AutoConfigTestPage::nextId() const
{
	return -1;
}