window-basic-auto-config-test.cpp 37 KB

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