window-basic-status-bar.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include <QLabel>
  2. #include <QHBoxLayout>
  3. #include <QPainter>
  4. #include <QPixmap>
  5. #include "obs-app.hpp"
  6. #include "window-basic-main.hpp"
  7. #include "window-basic-status-bar.hpp"
  8. #include "window-basic-main-outputs.hpp"
  9. OBSBasicStatusBar::OBSBasicStatusBar(QWidget *parent)
  10. : QStatusBar (parent),
  11. delayInfo (new QLabel),
  12. droppedFrames (new QLabel),
  13. streamTime (new QLabel),
  14. recordTime (new QLabel),
  15. cpuUsage (new QLabel),
  16. transparentPixmap (20, 20),
  17. greenPixmap (20, 20),
  18. grayPixmap (20, 20),
  19. redPixmap (20, 20)
  20. {
  21. streamTime->setText(QString("LIVE: 00:00:00"));
  22. recordTime->setText(QString("REC: 00:00:00"));
  23. cpuUsage->setText(QString("CPU: 0.0%, 0.00 fps"));
  24. QWidget *brWidget = new QWidget(this);
  25. QHBoxLayout *brLayout = new QHBoxLayout(brWidget);
  26. brLayout->setContentsMargins(0, 0, 0, 0);
  27. statusSquare = new QLabel(brWidget);
  28. brLayout->addWidget(statusSquare);
  29. kbps = new QLabel(brWidget);
  30. brLayout->addWidget(kbps);
  31. brWidget->setLayout(brLayout);
  32. delayInfo->setAlignment(Qt::AlignRight);
  33. delayInfo->setAlignment(Qt::AlignVCenter);
  34. droppedFrames->setAlignment(Qt::AlignRight);
  35. droppedFrames->setAlignment(Qt::AlignVCenter);
  36. streamTime->setAlignment(Qt::AlignRight);
  37. streamTime->setAlignment(Qt::AlignVCenter);
  38. recordTime->setAlignment(Qt::AlignRight);
  39. recordTime->setAlignment(Qt::AlignVCenter);
  40. cpuUsage->setAlignment(Qt::AlignRight);
  41. cpuUsage->setAlignment(Qt::AlignVCenter);
  42. kbps->setAlignment(Qt::AlignRight);
  43. kbps->setAlignment(Qt::AlignVCenter);
  44. delayInfo->setIndent(20);
  45. droppedFrames->setIndent(20);
  46. streamTime->setIndent(20);
  47. recordTime->setIndent(20);
  48. cpuUsage->setIndent(20);
  49. kbps->setIndent(10);
  50. addPermanentWidget(droppedFrames);
  51. addPermanentWidget(streamTime);
  52. addPermanentWidget(recordTime);
  53. addPermanentWidget(cpuUsage);
  54. addPermanentWidget(delayInfo);
  55. addPermanentWidget(brWidget);
  56. transparentPixmap.fill(QColor(0, 0, 0, 0));
  57. greenPixmap.fill(QColor(0, 255, 0));
  58. grayPixmap.fill(QColor(72, 72, 72));
  59. redPixmap.fill(QColor(255, 0, 0));
  60. statusSquare->setPixmap(transparentPixmap);
  61. }
  62. void OBSBasicStatusBar::Activate()
  63. {
  64. if (!active) {
  65. refreshTimer = new QTimer(this);
  66. connect(refreshTimer, SIGNAL(timeout()),
  67. this, SLOT(UpdateStatusBar()));
  68. int skipped = video_output_get_skipped_frames(obs_get_video());
  69. int total = video_output_get_total_frames(obs_get_video());
  70. totalStreamSeconds = 0;
  71. totalRecordSeconds = 0;
  72. lastSkippedFrameCount = 0;
  73. startSkippedFrameCount = skipped;
  74. startTotalFrameCount = total;
  75. refreshTimer->start(1000);
  76. active = true;
  77. if (streamOutput) {
  78. statusSquare->setPixmap(grayPixmap);
  79. }
  80. }
  81. }
  82. void OBSBasicStatusBar::Deactivate()
  83. {
  84. OBSBasic *main = qobject_cast<OBSBasic*>(parent());
  85. if (!main)
  86. return;
  87. if (!streamOutput) {
  88. streamTime->setText(QString("LIVE: 00:00:00"));
  89. totalStreamSeconds = 0;
  90. }
  91. if (!recordOutput) {
  92. recordTime->setText(QString("REC: 00:00:00"));
  93. totalRecordSeconds = 0;
  94. }
  95. if (!main->outputHandler->Active()) {
  96. delete refreshTimer;
  97. delayInfo->setText("");
  98. droppedFrames->setText("");
  99. kbps->setText("");
  100. delaySecTotal = 0;
  101. delaySecStarting = 0;
  102. delaySecStopping = 0;
  103. reconnectTimeout = 0;
  104. active = false;
  105. overloadedNotify = true;
  106. statusSquare->setPixmap(transparentPixmap);
  107. }
  108. }
  109. void OBSBasicStatusBar::UpdateDelayMsg()
  110. {
  111. QString msg;
  112. if (delaySecTotal) {
  113. if (delaySecStarting && !delaySecStopping) {
  114. msg = QTStr("Basic.StatusBar.DelayStartingIn");
  115. msg = msg.arg(QString::number(delaySecStarting));
  116. } else if (!delaySecStarting && delaySecStopping) {
  117. msg = QTStr("Basic.StatusBar.DelayStoppingIn");
  118. msg = msg.arg(QString::number(delaySecStopping));
  119. } else if (delaySecStarting && delaySecStopping) {
  120. msg = QTStr("Basic.StatusBar.DelayStartingStoppingIn");
  121. msg = msg.arg(QString::number(delaySecStopping),
  122. QString::number(delaySecStarting));
  123. } else {
  124. msg = QTStr("Basic.StatusBar.Delay");
  125. msg = msg.arg(QString::number(delaySecTotal));
  126. }
  127. }
  128. delayInfo->setText(msg);
  129. }
  130. #define BITRATE_UPDATE_SECONDS 2
  131. void OBSBasicStatusBar::UpdateBandwidth()
  132. {
  133. if (!streamOutput)
  134. return;
  135. if (++bitrateUpdateSeconds < BITRATE_UPDATE_SECONDS)
  136. return;
  137. uint64_t bytesSent = obs_output_get_total_bytes(streamOutput);
  138. uint64_t bytesSentTime = os_gettime_ns();
  139. if (bytesSent < lastBytesSent)
  140. bytesSent = 0;
  141. if (bytesSent == 0)
  142. lastBytesSent = 0;
  143. uint64_t bitsBetween = (bytesSent - lastBytesSent) * 8;
  144. double timePassed = double(bytesSentTime - lastBytesSentTime) /
  145. 1000000000.0;
  146. double kbitsPerSec = double(bitsBetween) / timePassed / 1000.0;
  147. QString text;
  148. text += QString("kb/s: ") +
  149. QString::number(kbitsPerSec, 'f', 0);
  150. kbps->setText(text);
  151. kbps->setMinimumWidth(kbps->width());
  152. lastBytesSent = bytesSent;
  153. lastBytesSentTime = bytesSentTime;
  154. bitrateUpdateSeconds = 0;
  155. }
  156. void OBSBasicStatusBar::UpdateCPUUsage()
  157. {
  158. OBSBasic *main = qobject_cast<OBSBasic*>(parent());
  159. if (!main)
  160. return;
  161. QString text;
  162. text += QString("CPU: ") +
  163. QString::number(main->GetCPUUsage(), 'f', 1) + QString("%, ") +
  164. QString::number(obs_get_active_fps(), 'f', 2) + QString(" fps");
  165. cpuUsage->setText(text);
  166. cpuUsage->setMinimumWidth(cpuUsage->width());
  167. }
  168. void OBSBasicStatusBar::UpdateStreamTime()
  169. {
  170. totalStreamSeconds++;
  171. int seconds = totalStreamSeconds % 60;
  172. int totalMinutes = totalStreamSeconds / 60;
  173. int minutes = totalMinutes % 60;
  174. int hours = totalMinutes / 60;
  175. QString text;
  176. text.sprintf("LIVE: %02d:%02d:%02d", hours, minutes, seconds);
  177. streamTime->setText(text);
  178. streamTime->setMinimumWidth(streamTime->width());
  179. if (reconnectTimeout > 0) {
  180. QString msg = QTStr("Basic.StatusBar.Reconnecting")
  181. .arg(QString::number(retries),
  182. QString::number(reconnectTimeout));
  183. showMessage(msg);
  184. reconnectTimeout--;
  185. } else if (retries > 0) {
  186. QString msg = QTStr("Basic.StatusBar.AttemptingReconnect");
  187. showMessage(msg.arg(QString::number(retries)));
  188. }
  189. if (delaySecStopping > 0 || delaySecStarting > 0) {
  190. if (delaySecStopping > 0)
  191. --delaySecStopping;
  192. if (delaySecStarting > 0)
  193. --delaySecStarting;
  194. UpdateDelayMsg();
  195. }
  196. }
  197. void OBSBasicStatusBar::UpdateRecordTime()
  198. {
  199. totalRecordSeconds++;
  200. int seconds = totalRecordSeconds % 60;
  201. int totalMinutes = totalRecordSeconds / 60;
  202. int minutes = totalMinutes % 60;
  203. int hours = totalMinutes / 60;
  204. QString text;
  205. text.sprintf("REC: %02d:%02d:%02d", hours, minutes, seconds);
  206. recordTime->setText(text);
  207. recordTime->setMinimumWidth(recordTime->width());
  208. }
  209. void OBSBasicStatusBar::UpdateDroppedFrames()
  210. {
  211. if (!streamOutput)
  212. return;
  213. int totalDropped = obs_output_get_frames_dropped(streamOutput);
  214. int totalFrames = obs_output_get_total_frames(streamOutput);
  215. double percent = (double)totalDropped / (double)totalFrames * 100.0;
  216. if (!totalFrames)
  217. return;
  218. QString text = QTStr("DroppedFrames");
  219. text = text.arg(QString::number(totalDropped),
  220. QString::number(percent, 'f', 1));
  221. droppedFrames->setText(text);
  222. droppedFrames->setMinimumWidth(droppedFrames->width());
  223. /* ----------------------------------- *
  224. * calculate congestion color */
  225. float congestion = obs_output_get_congestion(streamOutput);
  226. float avgCongestion = (congestion + lastCongestion) * 0.5f;
  227. if (avgCongestion < congestion)
  228. avgCongestion = congestion;
  229. if (avgCongestion > 1.0f)
  230. avgCongestion = 1.0f;
  231. if (avgCongestion < EPSILON) {
  232. statusSquare->setPixmap(greenPixmap);
  233. } else if (fabsf(avgCongestion - 1.0f) < EPSILON) {
  234. statusSquare->setPixmap(redPixmap);
  235. } else {
  236. QPixmap pixmap(20, 20);
  237. float red = avgCongestion * 2.0f;
  238. if (red > 1.0f) red = 1.0f;
  239. red *= 255.0;
  240. float green = (1.0f - avgCongestion) * 2.0f;
  241. if (green > 1.0f) green = 1.0f;
  242. green *= 255.0;
  243. pixmap.fill(QColor(int(red), int(green), 0));
  244. statusSquare->setPixmap(pixmap);
  245. }
  246. lastCongestion = congestion;
  247. }
  248. void OBSBasicStatusBar::OBSOutputReconnect(void *data, calldata_t *params)
  249. {
  250. OBSBasicStatusBar *statusBar =
  251. reinterpret_cast<OBSBasicStatusBar*>(data);
  252. int seconds = (int)calldata_int(params, "timeout_sec");
  253. QMetaObject::invokeMethod(statusBar, "Reconnect", Q_ARG(int, seconds));
  254. UNUSED_PARAMETER(params);
  255. }
  256. void OBSBasicStatusBar::OBSOutputReconnectSuccess(void *data, calldata_t *params)
  257. {
  258. OBSBasicStatusBar *statusBar =
  259. reinterpret_cast<OBSBasicStatusBar*>(data);
  260. QMetaObject::invokeMethod(statusBar, "ReconnectSuccess");
  261. UNUSED_PARAMETER(params);
  262. }
  263. void OBSBasicStatusBar::Reconnect(int seconds)
  264. {
  265. OBSBasic *main = qobject_cast<OBSBasic*>(parent());
  266. if (!retries)
  267. main->SysTrayNotify(
  268. QTStr("Basic.SystemTray.Message.Reconnecting"),
  269. QSystemTrayIcon::Warning);
  270. reconnectTimeout = seconds;
  271. if (streamOutput) {
  272. delaySecTotal = obs_output_get_active_delay(streamOutput);
  273. UpdateDelayMsg();
  274. retries++;
  275. }
  276. }
  277. void OBSBasicStatusBar::ReconnectClear()
  278. {
  279. retries = 0;
  280. reconnectTimeout = 0;
  281. bitrateUpdateSeconds = -1;
  282. lastBytesSent = 0;
  283. lastBytesSentTime = os_gettime_ns();
  284. delaySecTotal = 0;
  285. UpdateDelayMsg();
  286. }
  287. void OBSBasicStatusBar::ReconnectSuccess()
  288. {
  289. OBSBasic *main = qobject_cast<OBSBasic*>(parent());
  290. QString msg = QTStr("Basic.StatusBar.ReconnectSuccessful");
  291. showMessage(msg, 4000);
  292. main->SysTrayNotify(msg, QSystemTrayIcon::Information);
  293. ReconnectClear();
  294. if (streamOutput) {
  295. delaySecTotal = obs_output_get_active_delay(streamOutput);
  296. UpdateDelayMsg();
  297. }
  298. }
  299. void OBSBasicStatusBar::UpdateStatusBar()
  300. {
  301. OBSBasic *main = qobject_cast<OBSBasic*>(parent());
  302. UpdateBandwidth();
  303. if (streamOutput)
  304. UpdateStreamTime();
  305. if (recordOutput)
  306. UpdateRecordTime();
  307. UpdateDroppedFrames();
  308. int skipped = video_output_get_skipped_frames(obs_get_video());
  309. int total = video_output_get_total_frames(obs_get_video());
  310. skipped -= startSkippedFrameCount;
  311. total -= startTotalFrameCount;
  312. int diff = skipped - lastSkippedFrameCount;
  313. double percentage = double(skipped) / double(total) * 100.0;
  314. if (diff > 10 && percentage >= 0.1f) {
  315. showMessage(QTStr("HighResourceUsage"), 4000);
  316. if (!main->isVisible() && overloadedNotify) {
  317. main->SysTrayNotify(QTStr("HighResourceUsage"),
  318. QSystemTrayIcon::Warning);
  319. overloadedNotify = false;
  320. }
  321. }
  322. lastSkippedFrameCount = skipped;
  323. }
  324. void OBSBasicStatusBar::StreamDelayStarting(int sec)
  325. {
  326. OBSBasic *main = qobject_cast<OBSBasic*>(parent());
  327. if (!main || !main->outputHandler)
  328. return;
  329. streamOutput = main->outputHandler->streamOutput;
  330. delaySecTotal = delaySecStarting = sec;
  331. UpdateDelayMsg();
  332. Activate();
  333. }
  334. void OBSBasicStatusBar::StreamDelayStopping(int sec)
  335. {
  336. delaySecTotal = delaySecStopping = sec;
  337. UpdateDelayMsg();
  338. }
  339. void OBSBasicStatusBar::StreamStarted(obs_output_t *output)
  340. {
  341. streamOutput = output;
  342. signal_handler_connect(obs_output_get_signal_handler(streamOutput),
  343. "reconnect", OBSOutputReconnect, this);
  344. signal_handler_connect(obs_output_get_signal_handler(streamOutput),
  345. "reconnect_success", OBSOutputReconnectSuccess, this);
  346. retries = 0;
  347. lastBytesSent = 0;
  348. lastBytesSentTime = os_gettime_ns();
  349. Activate();
  350. }
  351. void OBSBasicStatusBar::StreamStopped()
  352. {
  353. if (streamOutput) {
  354. signal_handler_disconnect(
  355. obs_output_get_signal_handler(streamOutput),
  356. "reconnect", OBSOutputReconnect, this);
  357. signal_handler_disconnect(
  358. obs_output_get_signal_handler(streamOutput),
  359. "reconnect_success", OBSOutputReconnectSuccess,
  360. this);
  361. ReconnectClear();
  362. streamOutput = nullptr;
  363. clearMessage();
  364. Deactivate();
  365. }
  366. }
  367. void OBSBasicStatusBar::RecordingStarted(obs_output_t *output)
  368. {
  369. recordOutput = output;
  370. Activate();
  371. }
  372. void OBSBasicStatusBar::RecordingStopped()
  373. {
  374. recordOutput = nullptr;
  375. Deactivate();
  376. }