window-basic-stats.cpp 13 KB

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