window-basic-stats.cpp 13 KB

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