window-basic-stats.cpp 13 KB

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