window-basic-stats.cpp 13 KB

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