window-basic-stats.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <QPushButton>
  7. #include <QScrollArea>
  8. #include <QVBoxLayout>
  9. #include <QHBoxLayout>
  10. #include <QGridLayout>
  11. #include <string>
  12. #define TIMER_INTERVAL 2000
  13. static void setThemeID(QWidget *widget, const QString &themeID)
  14. {
  15. if (widget->property("themeID").toString() != themeID) {
  16. widget->setProperty("themeID", themeID);
  17. /* force style sheet recalculation */
  18. QString qss = widget->styleSheet();
  19. widget->setStyleSheet("/* */");
  20. widget->setStyleSheet(qss);
  21. }
  22. }
  23. OBSBasicStats::OBSBasicStats(QWidget *parent)
  24. : QDialog (parent),
  25. cpu_info (os_cpu_usage_info_start()),
  26. timer (this)
  27. {
  28. QVBoxLayout *mainLayout = new QVBoxLayout();
  29. QGridLayout *topLayout = new QGridLayout();
  30. outputLayout = new QGridLayout();
  31. int row = 0;
  32. auto newStatBare = [&] (QString name, QWidget *label, int col)
  33. {
  34. QLabel *typeLabel = new QLabel(name, this);
  35. topLayout->addWidget(typeLabel, row, col);
  36. topLayout->addWidget(label, row++, col + 1);
  37. };
  38. auto newStat = [&] (const char *strLoc, QWidget *label, int col)
  39. {
  40. std::string str = "Basic.Stats.";
  41. str += strLoc;
  42. newStatBare(QTStr(str.c_str()), label, col);
  43. };
  44. /* --------------------------------------------- */
  45. cpuUsage = new QLabel(this);
  46. hddSpace = new QLabel(this);
  47. #ifdef _WIN32
  48. memUsage = new QLabel(this);
  49. #endif
  50. newStat("CPUUsage", cpuUsage, 0);
  51. newStat("HDDSpaceAvailable", hddSpace, 0);
  52. #ifdef _WIN32
  53. newStat("MemoryUsage", memUsage, 0);
  54. #endif
  55. fps = new QLabel(this);
  56. renderTime = new QLabel(this);
  57. skippedFrames = new QLabel(this);
  58. missedFrames = new QLabel(this);
  59. row = 0;
  60. newStatBare("FPS", fps, 2);
  61. newStat("AverageTimeToRender", renderTime, 2);
  62. newStat("SkipppedFrames", skippedFrames, 2);
  63. newStat("MissedFrames", missedFrames, 2);
  64. /* --------------------------------------------- */
  65. QPushButton *closeButton = new QPushButton(QTStr("Close"));
  66. QPushButton *resetButton = new QPushButton(QTStr("Reset"));
  67. QHBoxLayout *buttonLayout = new QHBoxLayout;
  68. buttonLayout->addStretch();
  69. buttonLayout->addWidget(resetButton);
  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. connect(closeButton, &QPushButton::clicked, [this] () {close();});
  103. connect(resetButton, &QPushButton::clicked, [this] () {Reset();});
  104. installEventFilter(CreateShortcutFilter());
  105. resize(800, 280);
  106. setWindowTitle(QTStr("Basic.Stats"));
  107. setSizeGripEnabled(true);
  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. }
  115. OBSBasicStats::~OBSBasicStats()
  116. {
  117. os_cpu_usage_info_destroy(cpu_info);
  118. }
  119. void OBSBasicStats::AddOutputLabels(QString name)
  120. {
  121. OutputLabels ol;
  122. ol.name = new QLabel(name, this);
  123. ol.status = new QLabel(this);
  124. ol.droppedFrames = new QLabel(this);
  125. ol.megabytesSent = new QLabel(this);
  126. ol.bitrate = new QLabel(this);
  127. int newPointSize = ol.status->font().pointSize();
  128. newPointSize *= 13;
  129. newPointSize /= 10;
  130. QString qss =
  131. QString("font-size: %1pt").arg(QString::number(newPointSize));
  132. ol.status->setStyleSheet(qss);
  133. int col = 0;
  134. int row = outputLabels.size() + 1;
  135. outputLayout->addWidget(ol.name, row, col++);
  136. outputLayout->addWidget(ol.status, row, col++);
  137. outputLayout->addWidget(ol.droppedFrames, row, col++);
  138. outputLayout->addWidget(ol.megabytesSent, row, col++);
  139. outputLayout->addWidget(ol.bitrate, row, col++);
  140. outputLabels.push_back(ol);
  141. }
  142. static uint32_t first_encoded = 0xFFFFFFFF;
  143. static uint32_t first_skipped = 0xFFFFFFFF;
  144. static uint32_t first_rendered = 0xFFFFFFFF;
  145. static uint32_t first_lagged = 0xFFFFFFFF;
  146. void OBSBasicStats::Update()
  147. {
  148. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  149. /* TODO: Un-hardcode */
  150. struct obs_video_info ovi = {};
  151. obs_get_video_info(&ovi);
  152. OBSOutput strOutput = obs_frontend_get_streaming_output();
  153. OBSOutput recOutput = obs_frontend_get_recording_output();
  154. obs_output_release(strOutput);
  155. obs_output_release(recOutput);
  156. /* ------------------------------------------- */
  157. /* general usage */
  158. double curFPS = obs_get_active_fps();
  159. double obsFPS = (double)ovi.fps_num / (double)ovi.fps_den;
  160. QString str = QString::number(curFPS, 'f', 2);
  161. fps->setText(str);
  162. if (curFPS < (obsFPS * 0.8))
  163. setThemeID(fps, "error");
  164. else if (curFPS < (obsFPS * 0.95))
  165. setThemeID(fps, "warning");
  166. else
  167. setThemeID(fps, "");
  168. /* ------------------ */
  169. double usage = os_cpu_usage_info_query(cpu_info);
  170. str = QString::number(usage, 'g', 2) + QStringLiteral("%");
  171. cpuUsage->setText(str);
  172. /* ------------------ */
  173. const char *mode = config_get_string(main->Config(), "Output", "Mode");
  174. const char *path = strcmp(mode, "Advanced") ?
  175. config_get_string(main->Config(), "SimpleOutput", "FilePath") :
  176. config_get_string(main->Config(), "AdvOut", "RecFilePath");
  177. #define MBYTE (1024ULL * 1024ULL)
  178. #define GBYTE (1024ULL * 1024ULL * 1024ULL)
  179. #define TBYTE (1024ULL * 1024ULL * 1024ULL * 1024ULL)
  180. uint64_t num_bytes = os_get_free_disk_space(path);
  181. QString abrv = QStringLiteral(" MB");
  182. long double num;
  183. num = (long double)num_bytes / (1024.0l * 1024.0l);
  184. if (num_bytes > TBYTE) {
  185. num /= 1024.0l * 1024.0l;
  186. abrv = QStringLiteral(" TB");
  187. } else if (num_bytes > GBYTE) {
  188. num /= 1024.0l;
  189. abrv = QStringLiteral(" GB");
  190. }
  191. str = QString::number(num, 'f', 1) + abrv;
  192. hddSpace->setText(str);
  193. if (num_bytes < GBYTE)
  194. setThemeID(hddSpace, "error");
  195. else if (num_bytes < (5 * GBYTE))
  196. setThemeID(hddSpace, "warning");
  197. else
  198. setThemeID(hddSpace, "");
  199. /* ------------------ */
  200. #ifdef _WIN32
  201. num = (long double)CurrentMemoryUsage() / (1024.0l * 1024.0l);
  202. str = QString::number(num, 'f', 1) + QStringLiteral(" MB");
  203. memUsage->setText(str);
  204. #endif
  205. /* ------------------ */
  206. num = (long double)obs_get_average_frame_time_ns() / 1000000.0l;
  207. str = QString::number(num, 'f', 1) + QStringLiteral(" ms");
  208. renderTime->setText(str);
  209. long double fpsFrameTime =
  210. (long double)ovi.fps_den * 1000.0l / (long double)ovi.fps_num;
  211. if (num > fpsFrameTime)
  212. setThemeID(renderTime, "error");
  213. else if (num > fpsFrameTime * 0.75l)
  214. setThemeID(renderTime, "warning");
  215. else
  216. setThemeID(renderTime, "");
  217. /* ------------------ */
  218. video_t *video = obs_get_video();
  219. uint32_t total_encoded = video_output_get_total_frames(video);
  220. uint32_t total_skipped = video_output_get_skipped_frames(video);
  221. if (total_encoded < first_encoded || total_skipped < first_skipped) {
  222. first_encoded = total_encoded;
  223. first_skipped = total_skipped;
  224. }
  225. total_encoded -= first_encoded;
  226. total_skipped -= first_skipped;
  227. num = total_encoded
  228. ? (long double)total_skipped / (long double)total_encoded
  229. : 0.0l;
  230. num *= 100.0l;
  231. str = QString("%1 / %2 (%3%)").arg(
  232. QString::number(total_skipped),
  233. QString::number(total_encoded),
  234. QString::number(num, 'f', 1));
  235. skippedFrames->setText(str);
  236. if (num > 5.0l)
  237. setThemeID(skippedFrames, "error");
  238. else if (num > 1.0l)
  239. setThemeID(skippedFrames, "warning");
  240. else
  241. setThemeID(skippedFrames, "");
  242. /* ------------------ */
  243. uint32_t total_rendered = obs_get_total_frames();
  244. uint32_t total_lagged = obs_get_lagged_frames();
  245. if (total_rendered < first_rendered || total_lagged < first_lagged) {
  246. first_rendered = total_rendered;
  247. first_lagged = total_lagged;
  248. }
  249. total_rendered -= first_rendered;
  250. total_lagged -= first_lagged;
  251. num = total_rendered
  252. ? (long double)total_lagged / (long double)total_rendered
  253. : 0.0l;
  254. num *= 100.0l;
  255. str = QString("%1 / %2 (%3%)").arg(
  256. QString::number(total_lagged),
  257. QString::number(total_rendered),
  258. QString::number(num, 'f', 1));
  259. missedFrames->setText(str);
  260. if (num > 5.0l)
  261. setThemeID(missedFrames, "error");
  262. else if (num > 1.0l)
  263. setThemeID(missedFrames, "warning");
  264. else
  265. setThemeID(missedFrames, "");
  266. /* ------------------------------------------- */
  267. /* recording/streaming stats */
  268. outputLabels[0].Update(strOutput);
  269. outputLabels[1].Update(recOutput);
  270. }
  271. void OBSBasicStats::Reset()
  272. {
  273. timer.start();
  274. first_encoded = 0xFFFFFFFF;
  275. first_skipped = 0xFFFFFFFF;
  276. first_rendered = 0xFFFFFFFF;
  277. first_lagged = 0xFFFFFFFF;
  278. OBSOutput strOutput = obs_frontend_get_streaming_output();
  279. OBSOutput recOutput = obs_frontend_get_recording_output();
  280. obs_output_release(strOutput);
  281. obs_output_release(recOutput);
  282. outputLabels[0].Reset(strOutput);
  283. outputLabels[1].Reset(recOutput);
  284. Update();
  285. }
  286. void OBSBasicStats::OutputLabels::Update(obs_output_t *output)
  287. {
  288. const char *id = obs_obj_get_id(output);
  289. bool rec = strcmp(id, "rtmp_output") != 0;
  290. uint64_t totalBytes = obs_output_get_total_bytes(output);
  291. uint64_t curTime = os_gettime_ns();
  292. uint64_t bytesSent = totalBytes;
  293. if (bytesSent < lastBytesSent)
  294. bytesSent = 0;
  295. if (bytesSent == 0)
  296. lastBytesSent = 0;
  297. uint64_t bitsBetween = (bytesSent - lastBytesSent) * 8;
  298. long double timePassed = (long double)(curTime - lastBytesSentTime) /
  299. 1000000000.0l;
  300. long double kbps = (long double)bitsBetween /
  301. timePassed / 1000.0l;
  302. if (timePassed < 0.01l)
  303. kbps = 0.0l;
  304. QString str = QTStr("Basic.Stats.Status.Inactive");
  305. QString themeID;
  306. if (rec) {
  307. if (obs_output_active(output))
  308. str = QTStr("Basic.Stats.Status.Recording");
  309. } else {
  310. if (obs_output_active(output)) {
  311. if (obs_output_reconnecting(output)) {
  312. str = QTStr("Basic.Stats.Status.Reconnecting");
  313. themeID = "error";
  314. } else {
  315. str = QTStr("Basic.Stats.Status.Live");
  316. themeID = "good";
  317. }
  318. }
  319. }
  320. status->setText(str);
  321. setThemeID(status, themeID);
  322. long double num = (long double)totalBytes / (1024.0l * 1024.0l);
  323. megabytesSent->setText(
  324. QString("%1 MB").arg(QString::number(num, 'f', 1)));
  325. bitrate->setText(
  326. QString("%1 kb/s").arg(QString::number(kbps, 'f', 0)));
  327. if (!rec) {
  328. int total = obs_output_get_total_frames(output);
  329. int dropped = obs_output_get_frames_dropped(output);
  330. if (total < first_total || dropped < first_dropped) {
  331. first_total = 0;
  332. first_dropped = 0;
  333. }
  334. total -= first_total;
  335. dropped -= first_dropped;
  336. num = total
  337. ? (long double)dropped / (long double)total * 100.0l
  338. : 0.0l;
  339. str = QString("%1 / %2 (%3%)").arg(
  340. QString::number(dropped),
  341. QString::number(total),
  342. QString::number(num, 'f', 1));
  343. droppedFrames->setText(str);
  344. if (num > 5.0l)
  345. setThemeID(droppedFrames, "error");
  346. else if (num > 1.0l)
  347. setThemeID(droppedFrames, "warning");
  348. else
  349. setThemeID(droppedFrames, "");
  350. }
  351. lastBytesSent = bytesSent;
  352. lastBytesSentTime = curTime;
  353. }
  354. void OBSBasicStats::OutputLabels::Reset(obs_output_t *output)
  355. {
  356. first_total = obs_output_get_total_frames(output);
  357. first_dropped = obs_output_get_frames_dropped(output);
  358. }