window-basic-stats.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "obs-frontend-api/obs-frontend-api.h"
  2. #include "window-basic-stats.hpp"
  3. #include "window-basic-main.hpp"
  4. #include "platform.hpp"
  5. #include "obs-app.hpp"
  6. #include <QDesktopWidget>
  7. #include <QPushButton>
  8. #include <QScrollArea>
  9. #include <QVBoxLayout>
  10. #include <QHBoxLayout>
  11. #include <QGridLayout>
  12. #include <string>
  13. #define TIMER_INTERVAL 2000
  14. static void setThemeID(QWidget *widget, const QString &themeID)
  15. {
  16. if (widget->property("themeID").toString() != themeID) {
  17. widget->setProperty("themeID", themeID);
  18. /* force style sheet recalculation */
  19. QString qss = widget->styleSheet();
  20. widget->setStyleSheet("/* */");
  21. widget->setStyleSheet(qss);
  22. }
  23. }
  24. OBSBasicStats::OBSBasicStats(QWidget *parent, bool closeable)
  25. : QWidget (parent),
  26. cpu_info (os_cpu_usage_info_start()),
  27. timer (this)
  28. {
  29. QVBoxLayout *mainLayout = new QVBoxLayout();
  30. QGridLayout *topLayout = new QGridLayout();
  31. outputLayout = new QGridLayout();
  32. int row = 0;
  33. auto newStatBare = [&] (QString name, QWidget *label, int col)
  34. {
  35. QLabel *typeLabel = new QLabel(name, this);
  36. topLayout->addWidget(typeLabel, row, col);
  37. topLayout->addWidget(label, row++, col + 1);
  38. };
  39. auto newStat = [&] (const char *strLoc, QWidget *label, int col)
  40. {
  41. std::string str = "Basic.Stats.";
  42. str += strLoc;
  43. newStatBare(QTStr(str.c_str()), label, col);
  44. };
  45. /* --------------------------------------------- */
  46. cpuUsage = new QLabel(this);
  47. hddSpace = new QLabel(this);
  48. memUsage = new QLabel(this);
  49. newStat("CPUUsage", cpuUsage, 0);
  50. newStat("HDDSpaceAvailable", hddSpace, 0);
  51. newStat("MemoryUsage", memUsage, 0);
  52. fps = new QLabel(this);
  53. renderTime = new QLabel(this);
  54. skippedFrames = new QLabel(this);
  55. missedFrames = new QLabel(this);
  56. row = 0;
  57. newStatBare("FPS", fps, 2);
  58. newStat("AverageTimeToRender", renderTime, 2);
  59. newStat("MissedFrames", missedFrames, 2);
  60. newStat("SkippedFrames", skippedFrames, 2);
  61. /* --------------------------------------------- */
  62. QPushButton *closeButton = nullptr;
  63. if(closeable)
  64. closeButton = new QPushButton(QTStr("Close"));
  65. QPushButton *resetButton = new QPushButton(QTStr("Reset"));
  66. QHBoxLayout *buttonLayout = new QHBoxLayout;
  67. buttonLayout->addStretch();
  68. buttonLayout->addWidget(resetButton);
  69. if(closeable)
  70. buttonLayout->addWidget(closeButton);
  71. /* --------------------------------------------- */
  72. int col = 0;
  73. auto addOutputCol = [&] (const char *loc)
  74. {
  75. QLabel *label = new QLabel(QTStr(loc), this);
  76. label->setStyleSheet("font-weight: bold");
  77. outputLayout->addWidget(label, 0, col++);
  78. };
  79. addOutputCol("Basic.Settings.Output");
  80. addOutputCol("Basic.Stats.Status");
  81. addOutputCol("Basic.Stats.DroppedFrames");
  82. addOutputCol("Basic.Stats.MegabytesSent");
  83. addOutputCol("Basic.Stats.Bitrate");
  84. /* --------------------------------------------- */
  85. AddOutputLabels(QTStr("Basic.Stats.Output.Stream"));
  86. AddOutputLabels(QTStr("Basic.Stats.Output.Recording"));
  87. /* --------------------------------------------- */
  88. QVBoxLayout *outputContainerLayout = new QVBoxLayout();
  89. outputContainerLayout->addLayout(outputLayout);
  90. outputContainerLayout->addStretch();
  91. QWidget *widget = new QWidget(this);
  92. widget->setLayout(outputContainerLayout);
  93. QScrollArea *scrollArea = new QScrollArea(this);
  94. scrollArea->setWidget(widget);
  95. scrollArea->setWidgetResizable(true);
  96. /* --------------------------------------------- */
  97. mainLayout->addLayout(topLayout);
  98. mainLayout->addWidget(scrollArea);
  99. mainLayout->addLayout(buttonLayout);
  100. setLayout(mainLayout);
  101. /* --------------------------------------------- */
  102. if(closeable)
  103. connect(closeButton, &QPushButton::clicked,
  104. [this] () {close();});
  105. connect(resetButton, &QPushButton::clicked, [this] () {Reset();});
  106. delete shortcutFilter;
  107. shortcutFilter = CreateShortcutFilter();
  108. installEventFilter(shortcutFilter);
  109. resize(800, 280);
  110. setWindowTitle(QTStr("Basic.Stats"));
  111. setWindowIcon(QIcon::fromTheme("obs", QIcon(":/res/images/obs.png")));
  112. setWindowModality(Qt::NonModal);
  113. setAttribute(Qt::WA_DeleteOnClose, true);
  114. QObject::connect(&timer, &QTimer::timeout, this, &OBSBasicStats::Update);
  115. timer.setInterval(TIMER_INTERVAL);
  116. if (isVisible())
  117. timer.start();
  118. Update();
  119. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  120. const char *geometry = config_get_string(main->Config(),
  121. "Stats", "geometry");
  122. if (geometry != NULL) {
  123. QByteArray byteArray = QByteArray::fromBase64(
  124. QByteArray(geometry));
  125. restoreGeometry(byteArray);
  126. QRect windowGeometry = normalGeometry();
  127. if (!WindowPositionValid(windowGeometry)) {
  128. QRect rect = App()->desktop()->geometry();
  129. setGeometry(QStyle::alignedRect(
  130. Qt::LeftToRight,
  131. Qt::AlignCenter,
  132. size(), rect));
  133. }
  134. }
  135. }
  136. void OBSBasicStats::closeEvent(QCloseEvent *event)
  137. {
  138. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  139. if (isVisible()) {
  140. config_set_string(main->Config(),
  141. "Stats", "geometry",
  142. saveGeometry().toBase64().constData());
  143. config_save_safe(main->Config(), "tmp", nullptr);
  144. }
  145. QWidget::closeEvent(event);
  146. }
  147. OBSBasicStats::~OBSBasicStats()
  148. {
  149. delete shortcutFilter;
  150. os_cpu_usage_info_destroy(cpu_info);
  151. }
  152. void OBSBasicStats::AddOutputLabels(QString name)
  153. {
  154. OutputLabels ol;
  155. ol.name = new QLabel(name, this);
  156. ol.status = new QLabel(this);
  157. ol.droppedFrames = new QLabel(this);
  158. ol.megabytesSent = new QLabel(this);
  159. ol.bitrate = new QLabel(this);
  160. int newPointSize = ol.status->font().pointSize();
  161. newPointSize *= 13;
  162. newPointSize /= 10;
  163. QString qss =
  164. QString("font-size: %1pt").arg(QString::number(newPointSize));
  165. ol.status->setStyleSheet(qss);
  166. int col = 0;
  167. int row = outputLabels.size() + 1;
  168. outputLayout->addWidget(ol.name, row, col++);
  169. outputLayout->addWidget(ol.status, row, col++);
  170. outputLayout->addWidget(ol.droppedFrames, row, col++);
  171. outputLayout->addWidget(ol.megabytesSent, row, col++);
  172. outputLayout->addWidget(ol.bitrate, row, col++);
  173. outputLabels.push_back(ol);
  174. }
  175. static uint32_t first_encoded = 0xFFFFFFFF;
  176. static uint32_t first_skipped = 0xFFFFFFFF;
  177. static uint32_t first_rendered = 0xFFFFFFFF;
  178. static uint32_t first_lagged = 0xFFFFFFFF;
  179. void OBSBasicStats::InitializeValues()
  180. {
  181. video_t *video = obs_get_video();
  182. first_encoded = video_output_get_total_frames(video);
  183. first_skipped = video_output_get_skipped_frames(video);
  184. first_rendered = obs_get_total_frames();
  185. first_lagged = obs_get_lagged_frames();
  186. }
  187. void OBSBasicStats::Update()
  188. {
  189. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  190. /* TODO: Un-hardcode */
  191. struct obs_video_info ovi = {};
  192. obs_get_video_info(&ovi);
  193. OBSOutput strOutput = obs_frontend_get_streaming_output();
  194. OBSOutput recOutput = obs_frontend_get_recording_output();
  195. obs_output_release(strOutput);
  196. obs_output_release(recOutput);
  197. if (!strOutput && !recOutput)
  198. return;
  199. /* ------------------------------------------- */
  200. /* general usage */
  201. double curFPS = obs_get_active_fps();
  202. double obsFPS = (double)ovi.fps_num / (double)ovi.fps_den;
  203. QString str = QString::number(curFPS, 'f', 2);
  204. fps->setText(str);
  205. if (curFPS < (obsFPS * 0.8))
  206. setThemeID(fps, "error");
  207. else if (curFPS < (obsFPS * 0.95))
  208. setThemeID(fps, "warning");
  209. else
  210. setThemeID(fps, "");
  211. /* ------------------ */
  212. double usage = os_cpu_usage_info_query(cpu_info);
  213. str = QString::number(usage, 'g', 2) + QStringLiteral("%");
  214. cpuUsage->setText(str);
  215. /* ------------------ */
  216. const char *mode = config_get_string(main->Config(), "Output", "Mode");
  217. const char *path = strcmp(mode, "Advanced") ?
  218. config_get_string(main->Config(), "SimpleOutput", "FilePath") :
  219. config_get_string(main->Config(), "AdvOut", "RecFilePath");
  220. #define MBYTE (1024ULL * 1024ULL)
  221. #define GBYTE (1024ULL * 1024ULL * 1024ULL)
  222. #define TBYTE (1024ULL * 1024ULL * 1024ULL * 1024ULL)
  223. uint64_t num_bytes = os_get_free_disk_space(path);
  224. QString abrv = QStringLiteral(" MB");
  225. long double num;
  226. num = (long double)num_bytes / (1024.0l * 1024.0l);
  227. if (num_bytes > TBYTE) {
  228. num /= 1024.0l * 1024.0l;
  229. abrv = QStringLiteral(" TB");
  230. } else if (num_bytes > GBYTE) {
  231. num /= 1024.0l;
  232. abrv = QStringLiteral(" GB");
  233. }
  234. str = QString::number(num, 'f', 1) + abrv;
  235. hddSpace->setText(str);
  236. if (num_bytes < GBYTE)
  237. setThemeID(hddSpace, "error");
  238. else if (num_bytes < (5 * GBYTE))
  239. setThemeID(hddSpace, "warning");
  240. else
  241. setThemeID(hddSpace, "");
  242. /* ------------------ */
  243. num = (long double)os_get_proc_resident_size() / (1024.0l * 1024.0l);
  244. str = QString::number(num, 'f', 1) + QStringLiteral(" MB");
  245. memUsage->setText(str);
  246. /* ------------------ */
  247. num = (long double)obs_get_average_frame_time_ns() / 1000000.0l;
  248. str = QString::number(num, 'f', 1) + QStringLiteral(" ms");
  249. renderTime->setText(str);
  250. long double fpsFrameTime =
  251. (long double)ovi.fps_den * 1000.0l / (long double)ovi.fps_num;
  252. if (num > fpsFrameTime)
  253. setThemeID(renderTime, "error");
  254. else if (num > fpsFrameTime * 0.75l)
  255. setThemeID(renderTime, "warning");
  256. else
  257. setThemeID(renderTime, "");
  258. /* ------------------ */
  259. video_t *video = obs_get_video();
  260. uint32_t total_encoded = video_output_get_total_frames(video);
  261. uint32_t total_skipped = video_output_get_skipped_frames(video);
  262. if (total_encoded < first_encoded || total_skipped < first_skipped) {
  263. first_encoded = total_encoded;
  264. first_skipped = total_skipped;
  265. }
  266. total_encoded -= first_encoded;
  267. total_skipped -= first_skipped;
  268. num = total_encoded
  269. ? (long double)total_skipped / (long double)total_encoded
  270. : 0.0l;
  271. num *= 100.0l;
  272. str = QString("%1 / %2 (%3%)").arg(
  273. QString::number(total_skipped),
  274. QString::number(total_encoded),
  275. QString::number(num, 'f', 1));
  276. skippedFrames->setText(str);
  277. if (num > 5.0l)
  278. setThemeID(skippedFrames, "error");
  279. else if (num > 1.0l)
  280. setThemeID(skippedFrames, "warning");
  281. else
  282. setThemeID(skippedFrames, "");
  283. /* ------------------ */
  284. uint32_t total_rendered = obs_get_total_frames();
  285. uint32_t total_lagged = obs_get_lagged_frames();
  286. if (total_rendered < first_rendered || total_lagged < first_lagged) {
  287. first_rendered = total_rendered;
  288. first_lagged = total_lagged;
  289. }
  290. total_rendered -= first_rendered;
  291. total_lagged -= first_lagged;
  292. num = total_rendered
  293. ? (long double)total_lagged / (long double)total_rendered
  294. : 0.0l;
  295. num *= 100.0l;
  296. str = QString("%1 / %2 (%3%)").arg(
  297. QString::number(total_lagged),
  298. QString::number(total_rendered),
  299. QString::number(num, 'f', 1));
  300. missedFrames->setText(str);
  301. if (num > 5.0l)
  302. setThemeID(missedFrames, "error");
  303. else if (num > 1.0l)
  304. setThemeID(missedFrames, "warning");
  305. else
  306. setThemeID(missedFrames, "");
  307. /* ------------------------------------------- */
  308. /* recording/streaming stats */
  309. outputLabels[0].Update(strOutput, false);
  310. outputLabels[1].Update(recOutput, true);
  311. }
  312. void OBSBasicStats::Reset()
  313. {
  314. timer.start();
  315. first_encoded = 0xFFFFFFFF;
  316. first_skipped = 0xFFFFFFFF;
  317. first_rendered = 0xFFFFFFFF;
  318. first_lagged = 0xFFFFFFFF;
  319. OBSOutput strOutput = obs_frontend_get_streaming_output();
  320. OBSOutput recOutput = obs_frontend_get_recording_output();
  321. obs_output_release(strOutput);
  322. obs_output_release(recOutput);
  323. outputLabels[0].Reset(strOutput);
  324. outputLabels[1].Reset(recOutput);
  325. Update();
  326. }
  327. void OBSBasicStats::OutputLabels::Update(obs_output_t *output, bool rec)
  328. {
  329. uint64_t totalBytes = output ? obs_output_get_total_bytes(output) : 0;
  330. uint64_t curTime = os_gettime_ns();
  331. uint64_t bytesSent = totalBytes;
  332. if (bytesSent < lastBytesSent)
  333. bytesSent = 0;
  334. if (bytesSent == 0)
  335. lastBytesSent = 0;
  336. uint64_t bitsBetween = (bytesSent - lastBytesSent) * 8;
  337. long double timePassed = (long double)(curTime - lastBytesSentTime) /
  338. 1000000000.0l;
  339. long double kbps = (long double)bitsBetween /
  340. timePassed / 1000.0l;
  341. if (timePassed < 0.01l)
  342. kbps = 0.0l;
  343. QString str = QTStr("Basic.Stats.Status.Inactive");
  344. QString themeID;
  345. bool active = output ? obs_output_active(output) : false;
  346. if (rec) {
  347. if (active)
  348. str = QTStr("Basic.Stats.Status.Recording");
  349. } else {
  350. if (active) {
  351. bool reconnecting = output
  352. ? obs_output_reconnecting(output)
  353. : false;
  354. if (reconnecting) {
  355. str = QTStr("Basic.Stats.Status.Reconnecting");
  356. themeID = "error";
  357. } else {
  358. str = QTStr("Basic.Stats.Status.Live");
  359. themeID = "good";
  360. }
  361. }
  362. }
  363. status->setText(str);
  364. setThemeID(status, themeID);
  365. long double num = (long double)totalBytes / (1024.0l * 1024.0l);
  366. megabytesSent->setText(
  367. QString("%1 MB").arg(QString::number(num, 'f', 1)));
  368. bitrate->setText(
  369. QString("%1 kb/s").arg(QString::number(kbps, 'f', 0)));
  370. if (!rec) {
  371. int total = output ? obs_output_get_total_frames(output) : 0;
  372. int dropped = output ? obs_output_get_frames_dropped(output) : 0;
  373. if (total < first_total || dropped < first_dropped) {
  374. first_total = 0;
  375. first_dropped = 0;
  376. }
  377. total -= first_total;
  378. dropped -= first_dropped;
  379. num = total
  380. ? (long double)dropped / (long double)total * 100.0l
  381. : 0.0l;
  382. str = QString("%1 / %2 (%3%)").arg(
  383. QString::number(dropped),
  384. QString::number(total),
  385. QString::number(num, 'f', 1));
  386. droppedFrames->setText(str);
  387. if (num > 5.0l)
  388. setThemeID(droppedFrames, "error");
  389. else if (num > 1.0l)
  390. setThemeID(droppedFrames, "warning");
  391. else
  392. setThemeID(droppedFrames, "");
  393. }
  394. lastBytesSent = bytesSent;
  395. lastBytesSentTime = curTime;
  396. }
  397. void OBSBasicStats::OutputLabels::Reset(obs_output_t *output)
  398. {
  399. if (!output)
  400. return;
  401. first_total = obs_output_get_total_frames(output);
  402. first_dropped = obs_output_get_frames_dropped(output);
  403. }
  404. void OBSBasicStats::showEvent(QShowEvent *)
  405. {
  406. timer.start(TIMER_INTERVAL);
  407. }
  408. void OBSBasicStats::hideEvent(QHideEvent *)
  409. {
  410. timer.stop();
  411. }