AutoConfigTestPage.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. #include <chrono>
  2. #include <QFormLayout>
  3. #include <obs.hpp>
  4. #include <util/platform.h>
  5. #include <util/util_uint64.h>
  6. #include <graphics/vec4.h>
  7. #include <graphics/graphics.h>
  8. #include <graphics/math-extra.h>
  9. #include <qt-wrappers.hpp>
  10. #include "window-basic-auto-config.hpp"
  11. #include "window-basic-main.hpp"
  12. #include "obs-app.hpp"
  13. #include "ui_AutoConfigTestPage.h"
  14. #define wiz reinterpret_cast<AutoConfig *>(wizard())
  15. using namespace std;
  16. /* ------------------------------------------------------------------------- */
  17. class TestMode {
  18. obs_video_info ovi;
  19. OBSSource source[6];
  20. static void render_rand(void *, uint32_t cx, uint32_t cy)
  21. {
  22. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  23. gs_eparam_t *randomvals[3] = {gs_effect_get_param_by_name(solid, "randomvals1"),
  24. gs_effect_get_param_by_name(solid, "randomvals2"),
  25. gs_effect_get_param_by_name(solid, "randomvals3")};
  26. struct vec4 r;
  27. for (int i = 0; i < 3; i++) {
  28. vec4_set(&r, rand_float(true) * 100.0f, rand_float(true) * 100.0f,
  29. rand_float(true) * 50000.0f + 10000.0f, 0.0f);
  30. gs_effect_set_vec4(randomvals[i], &r);
  31. }
  32. while (gs_effect_loop(solid, "Random"))
  33. gs_draw_sprite(nullptr, 0, cx, cy);
  34. }
  35. public:
  36. inline TestMode()
  37. {
  38. obs_get_video_info(&ovi);
  39. obs_add_main_render_callback(render_rand, this);
  40. for (uint32_t i = 0; i < 6; i++) {
  41. source[i] = obs_get_output_source(i);
  42. obs_source_release(source[i]);
  43. obs_set_output_source(i, nullptr);
  44. }
  45. }
  46. inline ~TestMode()
  47. {
  48. for (uint32_t i = 0; i < 6; i++)
  49. obs_set_output_source(i, source[i]);
  50. obs_remove_main_render_callback(render_rand, this);
  51. obs_reset_video(&ovi);
  52. }
  53. inline void SetVideo(int cx, int cy, int fps_num, int fps_den)
  54. {
  55. obs_video_info newOVI = ovi;
  56. newOVI.output_width = (uint32_t)cx;
  57. newOVI.output_height = (uint32_t)cy;
  58. newOVI.fps_num = (uint32_t)fps_num;
  59. newOVI.fps_den = (uint32_t)fps_den;
  60. obs_reset_video(&newOVI);
  61. }
  62. };
  63. /* ------------------------------------------------------------------------- */
  64. #define TEST_STR(x) "Basic.AutoConfig.TestPage." x
  65. #define SUBTITLE_TESTING TEST_STR("SubTitle.Testing")
  66. #define SUBTITLE_COMPLETE TEST_STR("SubTitle.Complete")
  67. #define TEST_BW TEST_STR("TestingBandwidth")
  68. #define TEST_BW_NO_OUTPUT TEST_STR("TestingBandwidth.NoOutput")
  69. #define TEST_BW_CONNECTING TEST_STR("TestingBandwidth.Connecting")
  70. #define TEST_BW_CONNECT_FAIL TEST_STR("TestingBandwidth.ConnectFailed")
  71. #define TEST_BW_SERVER TEST_STR("TestingBandwidth.Server")
  72. #define TEST_RES_VAL TEST_STR("TestingRes.Resolution")
  73. #define TEST_RES_FAIL TEST_STR("TestingRes.Fail")
  74. #define TEST_SE TEST_STR("TestingStreamEncoder")
  75. #define TEST_RE TEST_STR("TestingRecordingEncoder")
  76. #define TEST_RESULT_SE TEST_STR("Result.StreamingEncoder")
  77. #define TEST_RESULT_RE TEST_STR("Result.RecordingEncoder")
  78. void AutoConfigTestPage::StartBandwidthStage()
  79. {
  80. ui->progressLabel->setText(QTStr(TEST_BW));
  81. testThread = std::thread([this]() { TestBandwidthThread(); });
  82. }
  83. void AutoConfigTestPage::StartStreamEncoderStage()
  84. {
  85. ui->progressLabel->setText(QTStr(TEST_SE));
  86. testThread = std::thread([this]() { TestStreamEncoderThread(); });
  87. }
  88. void AutoConfigTestPage::StartRecordingEncoderStage()
  89. {
  90. ui->progressLabel->setText(QTStr(TEST_RE));
  91. testThread = std::thread([this]() { TestRecordingEncoderThread(); });
  92. }
  93. void AutoConfigTestPage::GetServers(std::vector<ServerInfo> &servers)
  94. {
  95. OBSDataAutoRelease settings = obs_data_create();
  96. obs_data_set_string(settings, "service", wiz->serviceName.c_str());
  97. obs_properties_t *ppts = obs_get_service_properties("rtmp_common");
  98. obs_property_t *p = obs_properties_get(ppts, "service");
  99. obs_property_modified(p, settings);
  100. p = obs_properties_get(ppts, "server");
  101. size_t count = obs_property_list_item_count(p);
  102. servers.reserve(count);
  103. for (size_t i = 0; i < count; i++) {
  104. const char *name = obs_property_list_item_name(p, i);
  105. const char *server = obs_property_list_item_string(p, i);
  106. if (wiz->CanTestServer(name)) {
  107. ServerInfo info(name, server);
  108. servers.push_back(info);
  109. }
  110. }
  111. obs_properties_destroy(ppts);
  112. }
  113. static inline void string_depad_key(string &key)
  114. {
  115. while (!key.empty()) {
  116. char ch = key.back();
  117. if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
  118. key.pop_back();
  119. else
  120. break;
  121. }
  122. }
  123. const char *FindAudioEncoderFromCodec(const char *type);
  124. static inline bool can_use_output(const char *prot, const char *output, const char *prot_test1,
  125. const char *prot_test2 = nullptr)
  126. {
  127. return (strcmp(prot, prot_test1) == 0 || (prot_test2 && strcmp(prot, prot_test2) == 0)) &&
  128. (obs_get_output_flags(output) & OBS_OUTPUT_SERVICE) != 0;
  129. }
  130. static bool return_first_id(void *data, const char *id)
  131. {
  132. const char **output = (const char **)data;
  133. *output = id;
  134. return false;
  135. }
  136. void AutoConfigTestPage::TestBandwidthThread()
  137. {
  138. bool connected = false;
  139. bool stopped = false;
  140. TestMode testMode;
  141. testMode.SetVideo(128, 128, 60, 1);
  142. QMetaObject::invokeMethod(this, "Progress", Q_ARG(int, 0));
  143. /*
  144. * create encoders
  145. * create output
  146. * test for 10 seconds
  147. */
  148. QMetaObject::invokeMethod(this, "UpdateMessage", Q_ARG(QString, QStringLiteral("")));
  149. /* -----------------------------------*/
  150. /* create obs objects */
  151. const char *serverType = wiz->customServer ? "rtmp_custom" : "rtmp_common";
  152. OBSEncoderAutoRelease vencoder = obs_video_encoder_create("obs_x264", "test_x264", nullptr, nullptr);
  153. OBSEncoderAutoRelease aencoder = obs_audio_encoder_create("ffmpeg_aac", "test_aac", nullptr, 0, nullptr);
  154. OBSServiceAutoRelease service = obs_service_create(serverType, "test_service", nullptr, nullptr);
  155. /* -----------------------------------*/
  156. /* configure settings */
  157. // service: "service", "server", "key"
  158. // vencoder: "bitrate", "rate_control",
  159. // obs_service_apply_encoder_settings
  160. // aencoder: "bitrate"
  161. // output: "bind_ip" via main config -> "Output", "BindIP"
  162. // obs_output_set_service
  163. OBSDataAutoRelease service_settings = obs_data_create();
  164. OBSDataAutoRelease vencoder_settings = obs_data_create();
  165. OBSDataAutoRelease aencoder_settings = obs_data_create();
  166. OBSDataAutoRelease output_settings = obs_data_create();
  167. std::string key = wiz->key;
  168. if (wiz->service == AutoConfig::Service::Twitch || wiz->service == AutoConfig::Service::AmazonIVS) {
  169. string_depad_key(key);
  170. key += "?bandwidthtest";
  171. } else if (wiz->serviceName == "Restream.io" || wiz->serviceName == "Restream.io - RTMP") {
  172. string_depad_key(key);
  173. key += "?test=true";
  174. }
  175. obs_data_set_string(service_settings, "service", wiz->serviceName.c_str());
  176. obs_data_set_string(service_settings, "key", key.c_str());
  177. obs_data_set_int(vencoder_settings, "bitrate", wiz->startingBitrate);
  178. obs_data_set_string(vencoder_settings, "rate_control", "CBR");
  179. obs_data_set_string(vencoder_settings, "preset", "veryfast");
  180. obs_data_set_int(vencoder_settings, "keyint_sec", 2);
  181. obs_data_set_int(aencoder_settings, "bitrate", 32);
  182. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  183. const char *bind_ip = config_get_string(main->Config(), "Output", "BindIP");
  184. obs_data_set_string(output_settings, "bind_ip", bind_ip);
  185. const char *ip_family = config_get_string(main->Config(), "Output", "IPFamily");
  186. obs_data_set_string(output_settings, "ip_family", ip_family);
  187. /* -----------------------------------*/
  188. /* determine which servers to test */
  189. std::vector<ServerInfo> servers;
  190. if (wiz->customServer)
  191. servers.emplace_back(wiz->server.c_str(), wiz->server.c_str());
  192. else
  193. GetServers(servers);
  194. /* just use the first server if it only has one alternate server,
  195. * or if using Restream or Nimo TV due to their "auto" servers */
  196. if (servers.size() < 3 || wiz->serviceName.substr(0, 11) == "Restream.io" || wiz->serviceName == "Nimo TV") {
  197. servers.resize(1);
  198. } else if ((wiz->service == AutoConfig::Service::Twitch && wiz->twitchAuto) ||
  199. (wiz->service == AutoConfig::Service::AmazonIVS && wiz->amazonIVSAuto)) {
  200. /* if using Twitch and "Auto" is available, test 3 closest
  201. * server */
  202. servers.erase(servers.begin() + 1);
  203. servers.resize(3);
  204. } else if (wiz->service == AutoConfig::Service::YouTube) {
  205. /* Only test first set of primary + backup servers */
  206. servers.resize(2);
  207. }
  208. if (!wiz->serviceConfigServers.empty()) {
  209. if (wiz->service == AutoConfig::Service::Twitch && wiz->twitchAuto) {
  210. // servers from Twitch service config replace the "auto" entry
  211. servers.erase(servers.begin());
  212. }
  213. for (auto it = std::rbegin(wiz->serviceConfigServers); it != std::rend(wiz->serviceConfigServers);
  214. it++) {
  215. auto same_server =
  216. std::find_if(std::begin(servers), std::end(servers),
  217. [&](const ServerInfo &si) { return si.address == it->address; });
  218. if (same_server != std::end(servers))
  219. servers.erase(same_server);
  220. servers.emplace(std::begin(servers), it->name.c_str(), it->address.c_str());
  221. }
  222. if (wiz->service == AutoConfig::Service::Twitch && wiz->twitchAuto) {
  223. // see above, only test 3 servers
  224. // rtmps urls are currently counted as separate servers
  225. servers.resize(3);
  226. }
  227. }
  228. /* -----------------------------------*/
  229. /* apply service settings */
  230. obs_service_update(service, service_settings);
  231. obs_service_apply_encoder_settings(service, vencoder_settings, aencoder_settings);
  232. if (wiz->multitrackVideo.testSuccessful) {
  233. obs_data_set_int(vencoder_settings, "bitrate", wiz->startingBitrate);
  234. }
  235. /* -----------------------------------*/
  236. /* create output */
  237. /* Check if the service has a preferred output type */
  238. const char *output_type = obs_service_get_preferred_output_type(service);
  239. if (!output_type || (obs_get_output_flags(output_type) & OBS_OUTPUT_SERVICE) == 0) {
  240. /* Otherwise, prefer first-party output types */
  241. const char *protocol = obs_service_get_protocol(service);
  242. if (can_use_output(protocol, "rtmp_output", "RTMP", "RTMPS")) {
  243. output_type = "rtmp_output";
  244. } else if (can_use_output(protocol, "ffmpeg_hls_muxer", "HLS")) {
  245. output_type = "ffmpeg_hls_muxer";
  246. } else if (can_use_output(protocol, "ffmpeg_mpegts_muxer", "SRT", "RIST")) {
  247. output_type = "ffmpeg_mpegts_muxer";
  248. }
  249. /* If third-party protocol, use the first enumerated type */
  250. if (!output_type)
  251. obs_enum_output_types_with_protocol(protocol, &output_type, return_first_id);
  252. /* If none, fail */
  253. if (!output_type) {
  254. QMetaObject::invokeMethod(this, "Failure", Q_ARG(QString, QTStr(TEST_BW_NO_OUTPUT)));
  255. return;
  256. }
  257. }
  258. OBSOutputAutoRelease output = obs_output_create(output_type, "test_stream", nullptr, nullptr);
  259. obs_output_update(output, output_settings);
  260. const char *audio_codec = obs_output_get_supported_audio_codecs(output);
  261. if (strcmp(audio_codec, "aac") != 0) {
  262. const char *id = FindAudioEncoderFromCodec(audio_codec);
  263. aencoder = obs_audio_encoder_create(id, "test_audio", nullptr, 0, nullptr);
  264. }
  265. /* -----------------------------------*/
  266. /* connect encoders/services/outputs */
  267. obs_encoder_update(vencoder, vencoder_settings);
  268. obs_encoder_update(aencoder, aencoder_settings);
  269. obs_encoder_set_video(vencoder, obs_get_video());
  270. obs_encoder_set_audio(aencoder, obs_get_audio());
  271. obs_output_set_video_encoder(output, vencoder);
  272. obs_output_set_audio_encoder(output, aencoder, 0);
  273. obs_output_set_reconnect_settings(output, 0, 0);
  274. obs_output_set_service(output, service);
  275. /* -----------------------------------*/
  276. /* connect signals */
  277. auto on_started = [&]() {
  278. unique_lock<mutex> lock(m);
  279. connected = true;
  280. stopped = false;
  281. cv.notify_one();
  282. };
  283. auto on_stopped = [&]() {
  284. unique_lock<mutex> lock(m);
  285. connected = false;
  286. stopped = true;
  287. cv.notify_one();
  288. };
  289. using on_started_t = decltype(on_started);
  290. using on_stopped_t = decltype(on_stopped);
  291. auto pre_on_started = [](void *data, calldata_t *) {
  292. on_started_t &on_started = *reinterpret_cast<on_started_t *>(data);
  293. on_started();
  294. };
  295. auto pre_on_stopped = [](void *data, calldata_t *) {
  296. on_stopped_t &on_stopped = *reinterpret_cast<on_stopped_t *>(data);
  297. on_stopped();
  298. };
  299. signal_handler *sh = obs_output_get_signal_handler(output);
  300. signal_handler_connect(sh, "start", pre_on_started, &on_started);
  301. signal_handler_connect(sh, "stop", pre_on_stopped, &on_stopped);
  302. /* -----------------------------------*/
  303. /* test servers */
  304. bool success = false;
  305. for (size_t i = 0; i < servers.size(); i++) {
  306. auto &server = servers[i];
  307. connected = false;
  308. stopped = false;
  309. int per = int((i + 1) * 100 / servers.size());
  310. QMetaObject::invokeMethod(this, "Progress", Q_ARG(int, per));
  311. QMetaObject::invokeMethod(this, "UpdateMessage",
  312. Q_ARG(QString, QTStr(TEST_BW_CONNECTING).arg(server.name.c_str())));
  313. obs_data_set_string(service_settings, "server", server.address.c_str());
  314. obs_service_update(service, service_settings);
  315. if (!obs_output_start(output))
  316. continue;
  317. unique_lock<mutex> ul(m);
  318. if (cancel) {
  319. ul.unlock();
  320. obs_output_force_stop(output);
  321. return;
  322. }
  323. if (!stopped && !connected)
  324. cv.wait(ul);
  325. if (cancel) {
  326. ul.unlock();
  327. obs_output_force_stop(output);
  328. return;
  329. }
  330. if (!connected)
  331. continue;
  332. QMetaObject::invokeMethod(this, "UpdateMessage",
  333. Q_ARG(QString, QTStr(TEST_BW_SERVER).arg(server.name.c_str())));
  334. /* ignore first 2.5 seconds due to possible buffering skewing
  335. * the result */
  336. cv.wait_for(ul, chrono::milliseconds(2500));
  337. if (stopped)
  338. continue;
  339. if (cancel) {
  340. ul.unlock();
  341. obs_output_force_stop(output);
  342. return;
  343. }
  344. /* continue test */
  345. int start_bytes = (int)obs_output_get_total_bytes(output);
  346. uint64_t t_start = os_gettime_ns();
  347. cv.wait_for(ul, chrono::seconds(10));
  348. if (stopped)
  349. continue;
  350. if (cancel) {
  351. ul.unlock();
  352. obs_output_force_stop(output);
  353. return;
  354. }
  355. obs_output_stop(output);
  356. cv.wait(ul);
  357. uint64_t total_time = os_gettime_ns() - t_start;
  358. if (total_time == 0)
  359. total_time = 1;
  360. int total_bytes = (int)obs_output_get_total_bytes(output) - start_bytes;
  361. uint64_t bitrate = util_mul_div64(total_bytes, 8ULL * 1000000000ULL / 1000ULL, total_time);
  362. if (obs_output_get_frames_dropped(output) || (int)bitrate < (wiz->startingBitrate * 75 / 100)) {
  363. server.bitrate = (int)bitrate * 70 / 100;
  364. } else {
  365. server.bitrate = wiz->startingBitrate;
  366. }
  367. server.ms = obs_output_get_connect_time_ms(output);
  368. success = true;
  369. }
  370. if (!success) {
  371. QMetaObject::invokeMethod(this, "Failure", Q_ARG(QString, QTStr(TEST_BW_CONNECT_FAIL)));
  372. return;
  373. }
  374. int bestBitrate = 0;
  375. int bestMS = 0x7FFFFFFF;
  376. string bestServer;
  377. string bestServerName;
  378. for (auto &server : servers) {
  379. bool close = abs(server.bitrate - bestBitrate) < 400;
  380. if ((!close && server.bitrate > bestBitrate) || (close && server.ms < bestMS)) {
  381. bestServer = server.address;
  382. bestServerName = server.name;
  383. bestBitrate = server.bitrate;
  384. bestMS = server.ms;
  385. }
  386. }
  387. wiz->server = std::move(bestServer);
  388. wiz->serverName = std::move(bestServerName);
  389. wiz->idealBitrate = bestBitrate;
  390. QMetaObject::invokeMethod(this, "NextStage");
  391. }
  392. /* this is used to estimate the lower bitrate limit for a given
  393. * resolution/fps. yes, it is a totally arbitrary equation that gets
  394. * the closest to the expected values */
  395. static long double EstimateBitrateVal(int cx, int cy, int fps_num, int fps_den)
  396. {
  397. long fps = (long double)fps_num / (long double)fps_den;
  398. long double areaVal = pow((long double)(cx * cy), 0.85l);
  399. return areaVal * sqrt(pow(fps, 1.1l));
  400. }
  401. static long double EstimateMinBitrate(int cx, int cy, int fps_num, int fps_den)
  402. {
  403. long double val = EstimateBitrateVal(1920, 1080, 60, 1) / 5800.0l;
  404. return EstimateBitrateVal(cx, cy, fps_num, fps_den) / val;
  405. }
  406. static long double EstimateUpperBitrate(int cx, int cy, int fps_num, int fps_den)
  407. {
  408. long double val = EstimateBitrateVal(1280, 720, 30, 1) / 3000.0l;
  409. return EstimateBitrateVal(cx, cy, fps_num, fps_den) / val;
  410. }
  411. struct Result {
  412. int cx;
  413. int cy;
  414. int fps_num;
  415. int fps_den;
  416. inline Result(int cx_, int cy_, int fps_num_, int fps_den_)
  417. : cx(cx_),
  418. cy(cy_),
  419. fps_num(fps_num_),
  420. fps_den(fps_den_)
  421. {
  422. }
  423. };
  424. static void CalcBaseRes(int &baseCX, int &baseCY)
  425. {
  426. const int maxBaseArea = 1920 * 1200;
  427. const int clipResArea = 1920 * 1080;
  428. /* if base resolution unusually high, recalculate to a more reasonable
  429. * value to start the downscaling at, based upon 1920x1080's area.
  430. *
  431. * for 16:9 resolutions this will always change the starting value to
  432. * 1920x1080 */
  433. if ((baseCX * baseCY) > maxBaseArea) {
  434. long double xyAspect = (long double)baseCX / (long double)baseCY;
  435. baseCY = (int)sqrt((long double)clipResArea / xyAspect);
  436. baseCX = (int)((long double)baseCY * xyAspect);
  437. }
  438. }
  439. bool AutoConfigTestPage::TestSoftwareEncoding()
  440. {
  441. TestMode testMode;
  442. QMetaObject::invokeMethod(this, "UpdateMessage", Q_ARG(QString, QStringLiteral("")));
  443. /* -----------------------------------*/
  444. /* create obs objects */
  445. OBSEncoderAutoRelease vencoder = obs_video_encoder_create("obs_x264", "test_x264", nullptr, nullptr);
  446. OBSEncoderAutoRelease aencoder = obs_audio_encoder_create("ffmpeg_aac", "test_aac", nullptr, 0, nullptr);
  447. OBSOutputAutoRelease output = obs_output_create("null_output", "null", nullptr, nullptr);
  448. /* -----------------------------------*/
  449. /* configure settings */
  450. OBSDataAutoRelease aencoder_settings = obs_data_create();
  451. OBSDataAutoRelease vencoder_settings = obs_data_create();
  452. obs_data_set_int(aencoder_settings, "bitrate", 32);
  453. if (wiz->type != AutoConfig::Type::Recording) {
  454. obs_data_set_int(vencoder_settings, "keyint_sec", 2);
  455. obs_data_set_int(vencoder_settings, "bitrate", wiz->idealBitrate);
  456. obs_data_set_string(vencoder_settings, "rate_control", "CBR");
  457. obs_data_set_string(vencoder_settings, "profile", "main");
  458. obs_data_set_string(vencoder_settings, "preset", "veryfast");
  459. } else {
  460. obs_data_set_int(vencoder_settings, "crf", 20);
  461. obs_data_set_string(vencoder_settings, "rate_control", "CRF");
  462. obs_data_set_string(vencoder_settings, "profile", "high");
  463. obs_data_set_string(vencoder_settings, "preset", "veryfast");
  464. }
  465. /* -----------------------------------*/
  466. /* apply settings */
  467. obs_encoder_update(vencoder, vencoder_settings);
  468. obs_encoder_update(aencoder, aencoder_settings);
  469. /* -----------------------------------*/
  470. /* connect encoders/services/outputs */
  471. obs_output_set_video_encoder(output, vencoder);
  472. obs_output_set_audio_encoder(output, aencoder, 0);
  473. /* -----------------------------------*/
  474. /* connect signals */
  475. auto on_stopped = [&]() {
  476. unique_lock<mutex> lock(m);
  477. cv.notify_one();
  478. };
  479. using on_stopped_t = decltype(on_stopped);
  480. auto pre_on_stopped = [](void *data, calldata_t *) {
  481. on_stopped_t &on_stopped = *reinterpret_cast<on_stopped_t *>(data);
  482. on_stopped();
  483. };
  484. signal_handler *sh = obs_output_get_signal_handler(output);
  485. signal_handler_connect(sh, "deactivate", pre_on_stopped, &on_stopped);
  486. /* -----------------------------------*/
  487. /* calculate starting resolution */
  488. int baseCX = wiz->baseResolutionCX;
  489. int baseCY = wiz->baseResolutionCY;
  490. CalcBaseRes(baseCX, baseCY);
  491. /* -----------------------------------*/
  492. /* calculate starting test rates */
  493. int pcores = os_get_physical_cores();
  494. int lcores = os_get_logical_cores();
  495. int maxDataRate;
  496. if (lcores > 8 || pcores > 4) {
  497. /* superb */
  498. maxDataRate = 1920 * 1200 * 60 + 1000;
  499. } else if (lcores > 4 && pcores == 4) {
  500. /* great */
  501. maxDataRate = 1920 * 1080 * 60 + 1000;
  502. } else if (pcores == 4) {
  503. /* okay */
  504. maxDataRate = 1920 * 1080 * 30 + 1000;
  505. } else {
  506. /* toaster */
  507. maxDataRate = 960 * 540 * 30 + 1000;
  508. }
  509. /* -----------------------------------*/
  510. /* perform tests */
  511. vector<Result> results;
  512. int i = 0;
  513. int count = 1;
  514. auto testRes = [&](int cy, int fps_num, int fps_den, bool force) {
  515. int per = ++i * 100 / count;
  516. QMetaObject::invokeMethod(this, "Progress", Q_ARG(int, per));
  517. if (cy > baseCY)
  518. return true;
  519. /* no need for more than 3 tests max */
  520. if (results.size() >= 3)
  521. return true;
  522. if (!fps_num || !fps_den) {
  523. fps_num = wiz->specificFPSNum;
  524. fps_den = wiz->specificFPSDen;
  525. }
  526. long double fps = ((long double)fps_num / (long double)fps_den);
  527. int cx = int(((long double)baseCX / (long double)baseCY) * (long double)cy);
  528. if (!force && wiz->type != AutoConfig::Type::Recording) {
  529. int est = EstimateMinBitrate(cx, cy, fps_num, fps_den);
  530. if (est > wiz->idealBitrate)
  531. return true;
  532. }
  533. long double rate = (long double)cx * (long double)cy * fps;
  534. if (!force && rate > maxDataRate)
  535. return true;
  536. testMode.SetVideo(cx, cy, fps_num, fps_den);
  537. obs_encoder_set_video(vencoder, obs_get_video());
  538. obs_encoder_set_audio(aencoder, obs_get_audio());
  539. obs_encoder_update(vencoder, vencoder_settings);
  540. obs_output_set_media(output, obs_get_video(), obs_get_audio());
  541. QString cxStr = QString::number(cx);
  542. QString cyStr = QString::number(cy);
  543. QString fpsStr = (fps_den > 1) ? QString::number(fps, 'f', 2) : QString::number(fps, 'g', 2);
  544. QMetaObject::invokeMethod(this, "UpdateMessage",
  545. Q_ARG(QString, QTStr(TEST_RES_VAL).arg(cxStr, cyStr, fpsStr)));
  546. unique_lock<mutex> ul(m);
  547. if (cancel)
  548. return false;
  549. if (!obs_output_start(output)) {
  550. QMetaObject::invokeMethod(this, "Failure", Q_ARG(QString, QTStr(TEST_RES_FAIL)));
  551. return false;
  552. }
  553. cv.wait_for(ul, chrono::seconds(5));
  554. obs_output_stop(output);
  555. cv.wait(ul);
  556. int skipped = (int)video_output_get_skipped_frames(obs_get_video());
  557. if (force || skipped <= 10)
  558. results.emplace_back(cx, cy, fps_num, fps_den);
  559. return !cancel;
  560. };
  561. if (wiz->specificFPSNum && wiz->specificFPSDen) {
  562. count = 7;
  563. if (!testRes(2160, 0, 0, false))
  564. return false;
  565. if (!testRes(1440, 0, 0, false))
  566. return false;
  567. if (!testRes(1080, 0, 0, false))
  568. return false;
  569. if (!testRes(720, 0, 0, false))
  570. return false;
  571. if (!testRes(480, 0, 0, false))
  572. return false;
  573. if (!testRes(360, 0, 0, false))
  574. return false;
  575. if (!testRes(240, 0, 0, true))
  576. return false;
  577. } else {
  578. count = 14;
  579. if (!testRes(2160, 60, 1, false))
  580. return false;
  581. if (!testRes(2160, 30, 1, false))
  582. return false;
  583. if (!testRes(1440, 60, 1, false))
  584. return false;
  585. if (!testRes(1440, 30, 1, false))
  586. return false;
  587. if (!testRes(1080, 60, 1, false))
  588. return false;
  589. if (!testRes(1080, 30, 1, false))
  590. return false;
  591. if (!testRes(720, 60, 1, false))
  592. return false;
  593. if (!testRes(720, 30, 1, false))
  594. return false;
  595. if (!testRes(480, 60, 1, false))
  596. return false;
  597. if (!testRes(480, 30, 1, false))
  598. return false;
  599. if (!testRes(360, 60, 1, false))
  600. return false;
  601. if (!testRes(360, 30, 1, false))
  602. return false;
  603. if (!testRes(240, 60, 1, false))
  604. return false;
  605. if (!testRes(240, 30, 1, true))
  606. return false;
  607. }
  608. /* -----------------------------------*/
  609. /* find preferred settings */
  610. int minArea = 960 * 540 + 1000;
  611. if (!wiz->specificFPSNum && wiz->preferHighFPS && results.size() > 1) {
  612. Result &result1 = results[0];
  613. Result &result2 = results[1];
  614. if (result1.fps_num == 30 && result2.fps_num == 60) {
  615. int nextArea = result2.cx * result2.cy;
  616. if (nextArea >= minArea)
  617. results.erase(results.begin());
  618. }
  619. }
  620. Result result = results.front();
  621. wiz->idealResolutionCX = result.cx;
  622. wiz->idealResolutionCY = result.cy;
  623. wiz->idealFPSNum = result.fps_num;
  624. wiz->idealFPSDen = result.fps_den;
  625. long double fUpperBitrate = EstimateUpperBitrate(result.cx, result.cy, result.fps_num, result.fps_den);
  626. int upperBitrate = int(floor(fUpperBitrate / 50.0l) * 50.0l);
  627. if (wiz->streamingEncoder != AutoConfig::Encoder::x264) {
  628. upperBitrate *= 114;
  629. upperBitrate /= 100;
  630. }
  631. if (wiz->testMultitrackVideo && wiz->multitrackVideo.testSuccessful &&
  632. !wiz->multitrackVideo.bitrate.has_value())
  633. wiz->multitrackVideo.bitrate = wiz->idealBitrate;
  634. if (wiz->idealBitrate > upperBitrate)
  635. wiz->idealBitrate = upperBitrate;
  636. softwareTested = true;
  637. return true;
  638. }
  639. void AutoConfigTestPage::FindIdealHardwareResolution()
  640. {
  641. int baseCX = wiz->baseResolutionCX;
  642. int baseCY = wiz->baseResolutionCY;
  643. CalcBaseRes(baseCX, baseCY);
  644. vector<Result> results;
  645. int pcores = os_get_physical_cores();
  646. int maxDataRate;
  647. if (pcores >= 4) {
  648. maxDataRate = 1920 * 1200 * 60 + 1000;
  649. } else {
  650. maxDataRate = 1280 * 720 * 30 + 1000;
  651. }
  652. auto testRes = [&](int cy, int fps_num, int fps_den, bool force) {
  653. if (cy > baseCY)
  654. return;
  655. if (results.size() >= 3)
  656. return;
  657. if (!fps_num || !fps_den) {
  658. fps_num = wiz->specificFPSNum;
  659. fps_den = wiz->specificFPSDen;
  660. }
  661. long double fps = ((long double)fps_num / (long double)fps_den);
  662. int cx = int(((long double)baseCX / (long double)baseCY) * (long double)cy);
  663. long double rate = (long double)cx * (long double)cy * fps;
  664. if (!force && rate > maxDataRate)
  665. return;
  666. AutoConfig::Encoder encType = wiz->streamingEncoder;
  667. bool nvenc = encType == AutoConfig::Encoder::NVENC;
  668. int minBitrate = EstimateMinBitrate(cx, cy, fps_num, fps_den);
  669. /* most hardware encoders don't have a good quality to bitrate
  670. * ratio, so increase the minimum bitrate estimate for them.
  671. * NVENC currently is the exception because of the improvements
  672. * its made to its quality in recent generations. */
  673. if (!nvenc)
  674. minBitrate = minBitrate * 114 / 100;
  675. if (wiz->type == AutoConfig::Type::Recording)
  676. force = true;
  677. if (force || wiz->idealBitrate >= minBitrate)
  678. results.emplace_back(cx, cy, fps_num, fps_den);
  679. };
  680. if (wiz->specificFPSNum && wiz->specificFPSDen) {
  681. testRes(2160, 0, 0, false);
  682. testRes(1440, 0, 0, false);
  683. testRes(1080, 0, 0, false);
  684. testRes(720, 0, 0, false);
  685. testRes(480, 0, 0, false);
  686. testRes(360, 0, 0, false);
  687. testRes(240, 0, 0, true);
  688. } else {
  689. testRes(2160, 60, 1, false);
  690. testRes(2160, 30, 1, false);
  691. testRes(1440, 60, 1, false);
  692. testRes(1440, 30, 1, false);
  693. testRes(1080, 60, 1, false);
  694. testRes(1080, 30, 1, false);
  695. testRes(720, 60, 1, false);
  696. testRes(720, 30, 1, false);
  697. testRes(480, 60, 1, false);
  698. testRes(480, 30, 1, false);
  699. testRes(360, 60, 1, false);
  700. testRes(360, 30, 1, false);
  701. testRes(240, 60, 1, false);
  702. testRes(240, 30, 1, true);
  703. }
  704. int minArea = 960 * 540 + 1000;
  705. if (!wiz->specificFPSNum && wiz->preferHighFPS && results.size() > 1) {
  706. Result &result1 = results[0];
  707. Result &result2 = results[1];
  708. if (result1.fps_num == 30 && result2.fps_num == 60) {
  709. int nextArea = result2.cx * result2.cy;
  710. if (nextArea >= minArea)
  711. results.erase(results.begin());
  712. }
  713. }
  714. Result result = results.front();
  715. wiz->idealResolutionCX = result.cx;
  716. wiz->idealResolutionCY = result.cy;
  717. wiz->idealFPSNum = result.fps_num;
  718. wiz->idealFPSDen = result.fps_den;
  719. }
  720. void AutoConfigTestPage::TestStreamEncoderThread()
  721. {
  722. bool preferHardware = wiz->preferHardware;
  723. if (!softwareTested) {
  724. if (!preferHardware || !wiz->hardwareEncodingAvailable) {
  725. if (!TestSoftwareEncoding()) {
  726. return;
  727. }
  728. }
  729. }
  730. if (!softwareTested) {
  731. if (wiz->nvencAvailable)
  732. wiz->streamingEncoder = AutoConfig::Encoder::NVENC;
  733. else if (wiz->qsvAvailable)
  734. wiz->streamingEncoder = AutoConfig::Encoder::QSV;
  735. else if (wiz->appleAvailable)
  736. wiz->streamingEncoder = AutoConfig::Encoder::Apple;
  737. else
  738. wiz->streamingEncoder = AutoConfig::Encoder::AMD;
  739. } else {
  740. wiz->streamingEncoder = AutoConfig::Encoder::x264;
  741. }
  742. #ifdef __linux__
  743. // On linux CBR rate control is not guaranteed so fallback to x264.
  744. if (wiz->streamingEncoder == AutoConfig::Encoder::QSV) {
  745. wiz->streamingEncoder = AutoConfig::Encoder::x264;
  746. if (!TestSoftwareEncoding()) {
  747. return;
  748. }
  749. }
  750. #endif
  751. if (preferHardware && !softwareTested && wiz->hardwareEncodingAvailable)
  752. FindIdealHardwareResolution();
  753. QMetaObject::invokeMethod(this, "NextStage");
  754. }
  755. void AutoConfigTestPage::TestRecordingEncoderThread()
  756. {
  757. if (!wiz->hardwareEncodingAvailable && !softwareTested) {
  758. if (!TestSoftwareEncoding()) {
  759. return;
  760. }
  761. }
  762. if (wiz->type == AutoConfig::Type::Recording && wiz->hardwareEncodingAvailable)
  763. FindIdealHardwareResolution();
  764. wiz->recordingQuality = AutoConfig::Quality::High;
  765. bool recordingOnly = wiz->type == AutoConfig::Type::Recording;
  766. if (wiz->hardwareEncodingAvailable) {
  767. if (wiz->nvencAvailable)
  768. wiz->recordingEncoder = AutoConfig::Encoder::NVENC;
  769. else if (wiz->qsvAvailable)
  770. wiz->recordingEncoder = AutoConfig::Encoder::QSV;
  771. else if (wiz->appleAvailable)
  772. wiz->recordingEncoder = AutoConfig::Encoder::Apple;
  773. else
  774. wiz->recordingEncoder = AutoConfig::Encoder::AMD;
  775. } else {
  776. wiz->recordingEncoder = AutoConfig::Encoder::x264;
  777. }
  778. if (wiz->recordingEncoder != AutoConfig::Encoder::NVENC) {
  779. if (!recordingOnly) {
  780. wiz->recordingEncoder = AutoConfig::Encoder::Stream;
  781. wiz->recordingQuality = AutoConfig::Quality::Stream;
  782. }
  783. }
  784. QMetaObject::invokeMethod(this, "NextStage");
  785. }
  786. #define ENCODER_TEXT(x) "Basic.Settings.Output.Simple.Encoder." x
  787. #define ENCODER_SOFTWARE ENCODER_TEXT("Software")
  788. #define ENCODER_NVENC ENCODER_TEXT("Hardware.NVENC.H264")
  789. #define ENCODER_QSV ENCODER_TEXT("Hardware.QSV.H264")
  790. #define ENCODER_AMD ENCODER_TEXT("Hardware.AMD.H264")
  791. #define ENCODER_APPLE ENCODER_TEXT("Hardware.Apple.H264")
  792. #define QUALITY_SAME "Basic.Settings.Output.Simple.RecordingQuality.Stream"
  793. #define QUALITY_HIGH "Basic.Settings.Output.Simple.RecordingQuality.Small"
  794. void set_closest_res(int &cx, int &cy, struct obs_service_resolution *res_list, size_t count)
  795. {
  796. int best_pixel_diff = 0x7FFFFFFF;
  797. int start_cx = cx;
  798. int start_cy = cy;
  799. for (size_t i = 0; i < count; i++) {
  800. struct obs_service_resolution &res = res_list[i];
  801. int pixel_cx_diff = abs(start_cx - res.cx);
  802. int pixel_cy_diff = abs(start_cy - res.cy);
  803. int pixel_diff = pixel_cx_diff + pixel_cy_diff;
  804. if (pixel_diff < best_pixel_diff) {
  805. best_pixel_diff = pixel_diff;
  806. cx = res.cx;
  807. cy = res.cy;
  808. }
  809. }
  810. }
  811. void AutoConfigTestPage::FinalizeResults()
  812. {
  813. ui->stackedWidget->setCurrentIndex(1);
  814. setSubTitle(QTStr(SUBTITLE_COMPLETE));
  815. QFormLayout *form = results;
  816. auto encName = [](AutoConfig::Encoder enc) -> QString {
  817. switch (enc) {
  818. case AutoConfig::Encoder::x264:
  819. return QTStr(ENCODER_SOFTWARE);
  820. case AutoConfig::Encoder::NVENC:
  821. return QTStr(ENCODER_NVENC);
  822. case AutoConfig::Encoder::QSV:
  823. return QTStr(ENCODER_QSV);
  824. case AutoConfig::Encoder::AMD:
  825. return QTStr(ENCODER_AMD);
  826. case AutoConfig::Encoder::Apple:
  827. return QTStr(ENCODER_APPLE);
  828. case AutoConfig::Encoder::Stream:
  829. return QTStr(QUALITY_SAME);
  830. }
  831. return QTStr(ENCODER_SOFTWARE);
  832. };
  833. auto newLabel = [this](const char *str) -> QLabel * {
  834. return new QLabel(QTStr(str), this);
  835. };
  836. if (wiz->type == AutoConfig::Type::Streaming) {
  837. const char *serverType = wiz->customServer ? "rtmp_custom" : "rtmp_common";
  838. OBSServiceAutoRelease service = obs_service_create(serverType, "temp_service", nullptr, nullptr);
  839. OBSDataAutoRelease service_settings = obs_data_create();
  840. OBSDataAutoRelease vencoder_settings = obs_data_create();
  841. if (wiz->testMultitrackVideo && wiz->multitrackVideo.testSuccessful &&
  842. !wiz->multitrackVideo.bitrate.has_value())
  843. wiz->multitrackVideo.bitrate = wiz->idealBitrate;
  844. obs_data_set_int(vencoder_settings, "bitrate", wiz->idealBitrate);
  845. obs_data_set_string(service_settings, "service", wiz->serviceName.c_str());
  846. obs_service_update(service, service_settings);
  847. obs_service_apply_encoder_settings(service, vencoder_settings, nullptr);
  848. BPtr<obs_service_resolution> res_list;
  849. size_t res_count;
  850. int maxFPS;
  851. obs_service_get_supported_resolutions(service, &res_list, &res_count);
  852. obs_service_get_max_fps(service, &maxFPS);
  853. if (res_list) {
  854. set_closest_res(wiz->idealResolutionCX, wiz->idealResolutionCY, res_list, res_count);
  855. }
  856. if (maxFPS) {
  857. double idealFPS = (double)wiz->idealFPSNum / (double)wiz->idealFPSDen;
  858. if (idealFPS > (double)maxFPS) {
  859. wiz->idealFPSNum = maxFPS;
  860. wiz->idealFPSDen = 1;
  861. }
  862. }
  863. wiz->idealBitrate = (int)obs_data_get_int(vencoder_settings, "bitrate");
  864. if (!wiz->customServer)
  865. form->addRow(newLabel("Basic.AutoConfig.StreamPage.Service"),
  866. new QLabel(wiz->serviceName.c_str(), ui->finishPage));
  867. form->addRow(newLabel("Basic.AutoConfig.StreamPage.Server"),
  868. new QLabel(wiz->serverName.c_str(), ui->finishPage));
  869. form->addRow(newLabel("Basic.Settings.Stream.MultitrackVideoLabel"),
  870. newLabel(wiz->multitrackVideo.testSuccessful ? "Yes" : "No"));
  871. if (wiz->multitrackVideo.testSuccessful) {
  872. form->addRow(newLabel("Basic.Settings.Output.VideoBitrate"), newLabel("Automatic"));
  873. form->addRow(newLabel(TEST_RESULT_SE), newLabel("Automatic"));
  874. form->addRow(newLabel("Basic.AutoConfig.TestPage.Result.StreamingResolution"),
  875. newLabel("Automatic"));
  876. } else {
  877. form->addRow(newLabel("Basic.Settings.Output.VideoBitrate"),
  878. new QLabel(QString::number(wiz->idealBitrate), ui->finishPage));
  879. form->addRow(newLabel(TEST_RESULT_SE),
  880. new QLabel(encName(wiz->streamingEncoder), ui->finishPage));
  881. }
  882. }
  883. QString baseRes =
  884. QString("%1x%2").arg(QString::number(wiz->baseResolutionCX), QString::number(wiz->baseResolutionCY));
  885. QString scaleRes =
  886. QString("%1x%2").arg(QString::number(wiz->idealResolutionCX), QString::number(wiz->idealResolutionCY));
  887. if (wiz->recordingEncoder != AutoConfig::Encoder::Stream ||
  888. wiz->recordingQuality != AutoConfig::Quality::Stream)
  889. form->addRow(newLabel(TEST_RESULT_RE), new QLabel(encName(wiz->recordingEncoder), ui->finishPage));
  890. QString recQuality;
  891. switch (wiz->recordingQuality) {
  892. case AutoConfig::Quality::High:
  893. recQuality = QTStr(QUALITY_HIGH);
  894. break;
  895. case AutoConfig::Quality::Stream:
  896. recQuality = QTStr(QUALITY_SAME);
  897. break;
  898. }
  899. form->addRow(newLabel("Basic.Settings.Output.Simple.RecordingQuality"), new QLabel(recQuality, ui->finishPage));
  900. long double fps = (long double)wiz->idealFPSNum / (long double)wiz->idealFPSDen;
  901. QString fpsStr = (wiz->idealFPSDen > 1) ? QString::number(fps, 'f', 2) : QString::number(fps, 'g', 2);
  902. form->addRow(newLabel("Basic.Settings.Video.BaseResolution"), new QLabel(baseRes, ui->finishPage));
  903. form->addRow(newLabel("Basic.Settings.Video.ScaledResolution"), new QLabel(scaleRes, ui->finishPage));
  904. form->addRow(newLabel("Basic.Settings.Video.FPS"), new QLabel(fpsStr, ui->finishPage));
  905. // FIXME: form layout is super squished, probably need to set proper sizepolicy on all widgets?
  906. }
  907. #define STARTING_SEPARATOR "\n==== Auto-config wizard testing commencing ======\n"
  908. #define STOPPING_SEPARATOR "\n==== Auto-config wizard testing stopping ========\n"
  909. void AutoConfigTestPage::NextStage()
  910. {
  911. if (testThread.joinable())
  912. testThread.join();
  913. if (cancel)
  914. return;
  915. ui->subProgressLabel->setText(QString());
  916. /* make it skip to bandwidth stage if only set to config recording */
  917. if (stage == Stage::Starting) {
  918. if (!started) {
  919. blog(LOG_INFO, STARTING_SEPARATOR);
  920. started = true;
  921. }
  922. if (wiz->type != AutoConfig::Type::Streaming) {
  923. stage = Stage::StreamEncoder;
  924. } else if (!wiz->bandwidthTest) {
  925. stage = Stage::BandwidthTest;
  926. }
  927. }
  928. if (stage == Stage::Starting) {
  929. stage = Stage::BandwidthTest;
  930. StartBandwidthStage();
  931. } else if (stage == Stage::BandwidthTest) {
  932. stage = Stage::StreamEncoder;
  933. StartStreamEncoderStage();
  934. } else if (stage == Stage::StreamEncoder) {
  935. stage = Stage::RecordingEncoder;
  936. StartRecordingEncoderStage();
  937. } else {
  938. stage = Stage::Finished;
  939. FinalizeResults();
  940. emit completeChanged();
  941. }
  942. }
  943. void AutoConfigTestPage::UpdateMessage(QString message)
  944. {
  945. ui->subProgressLabel->setText(message);
  946. }
  947. void AutoConfigTestPage::Failure(QString message)
  948. {
  949. ui->errorLabel->setText(message);
  950. ui->stackedWidget->setCurrentIndex(2);
  951. }
  952. void AutoConfigTestPage::Progress(int percentage)
  953. {
  954. ui->progressBar->setValue(percentage);
  955. }
  956. AutoConfigTestPage::AutoConfigTestPage(QWidget *parent) : QWizardPage(parent), ui(new Ui_AutoConfigTestPage)
  957. {
  958. ui->setupUi(this);
  959. setTitle(QTStr("Basic.AutoConfig.TestPage"));
  960. setSubTitle(QTStr(SUBTITLE_TESTING));
  961. setCommitPage(true);
  962. }
  963. AutoConfigTestPage::~AutoConfigTestPage()
  964. {
  965. if (testThread.joinable()) {
  966. {
  967. unique_lock<mutex> ul(m);
  968. cancel = true;
  969. cv.notify_one();
  970. }
  971. testThread.join();
  972. }
  973. if (started)
  974. blog(LOG_INFO, STOPPING_SEPARATOR);
  975. }
  976. void AutoConfigTestPage::initializePage()
  977. {
  978. if (wiz->type == AutoConfig::Type::VirtualCam) {
  979. wiz->idealResolutionCX = wiz->baseResolutionCX;
  980. wiz->idealResolutionCY = wiz->baseResolutionCY;
  981. wiz->idealFPSNum = 30;
  982. wiz->idealFPSDen = 1;
  983. stage = Stage::Finished;
  984. } else {
  985. stage = Stage::Starting;
  986. }
  987. setSubTitle(QTStr(SUBTITLE_TESTING));
  988. softwareTested = false;
  989. cancel = false;
  990. DeleteLayout(results);
  991. results = new QFormLayout();
  992. results->setContentsMargins(0, 0, 0, 0);
  993. ui->finishPageLayout->insertLayout(1, results);
  994. ui->stackedWidget->setCurrentIndex(0);
  995. NextStage();
  996. }
  997. void AutoConfigTestPage::cleanupPage()
  998. {
  999. if (testThread.joinable()) {
  1000. {
  1001. unique_lock<mutex> ul(m);
  1002. cancel = true;
  1003. cv.notify_one();
  1004. }
  1005. testThread.join();
  1006. }
  1007. }
  1008. bool AutoConfigTestPage::isComplete() const
  1009. {
  1010. return stage == Stage::Finished;
  1011. }
  1012. int AutoConfigTestPage::nextId() const
  1013. {
  1014. return -1;
  1015. }