window-basic-status-bar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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()), this,
  67. 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 =
  145. double(bytesSentTime - lastBytesSentTime) / 1000000000.0;
  146. double kbitsPerSec = double(bitsBetween) / timePassed / 1000.0;
  147. QString text;
  148. text += QString("kb/s: ") + QString::number(kbitsPerSec, 'f', 0);
  149. kbps->setText(text);
  150. kbps->setMinimumWidth(kbps->width());
  151. lastBytesSent = bytesSent;
  152. lastBytesSentTime = bytesSentTime;
  153. bitrateUpdateSeconds = 0;
  154. }
  155. void OBSBasicStatusBar::UpdateCPUUsage()
  156. {
  157. OBSBasic *main = qobject_cast<OBSBasic *>(parent());
  158. if (!main)
  159. return;
  160. QString text;
  161. text += QString("CPU: ") +
  162. QString::number(main->GetCPUUsage(), 'f', 1) + QString("%, ") +
  163. QString::number(obs_get_active_fps(), 'f', 2) + QString(" fps");
  164. cpuUsage->setText(text);
  165. cpuUsage->setMinimumWidth(cpuUsage->width());
  166. }
  167. void OBSBasicStatusBar::UpdateStreamTime()
  168. {
  169. totalStreamSeconds++;
  170. int seconds = totalStreamSeconds % 60;
  171. int totalMinutes = totalStreamSeconds / 60;
  172. int minutes = totalMinutes % 60;
  173. int hours = totalMinutes / 60;
  174. QString text;
  175. text.sprintf("LIVE: %02d:%02d:%02d", hours, minutes, seconds);
  176. streamTime->setText(text);
  177. streamTime->setMinimumWidth(streamTime->width());
  178. if (reconnectTimeout > 0) {
  179. QString msg = QTStr("Basic.StatusBar.Reconnecting")
  180. .arg(QString::number(retries),
  181. QString::number(reconnectTimeout));
  182. showMessage(msg);
  183. reconnectTimeout--;
  184. } else if (retries > 0) {
  185. QString msg = QTStr("Basic.StatusBar.AttemptingReconnect");
  186. showMessage(msg.arg(QString::number(retries)));
  187. }
  188. if (delaySecStopping > 0 || delaySecStarting > 0) {
  189. if (delaySecStopping > 0)
  190. --delaySecStopping;
  191. if (delaySecStarting > 0)
  192. --delaySecStarting;
  193. UpdateDelayMsg();
  194. }
  195. }
  196. extern volatile bool recording_paused;
  197. void OBSBasicStatusBar::UpdateRecordTime()
  198. {
  199. bool paused = os_atomic_load_bool(&recording_paused);
  200. if (!paused)
  201. totalRecordSeconds++;
  202. QString text;
  203. if (paused) {
  204. text = QStringLiteral("REC: PAUSED");
  205. } else {
  206. int seconds = totalRecordSeconds % 60;
  207. int totalMinutes = totalRecordSeconds / 60;
  208. int minutes = totalMinutes % 60;
  209. int hours = totalMinutes / 60;
  210. text.sprintf("REC: %02d:%02d:%02d", hours, minutes, seconds);
  211. }
  212. recordTime->setText(text);
  213. recordTime->setMinimumWidth(recordTime->width());
  214. }
  215. void OBSBasicStatusBar::UpdateDroppedFrames()
  216. {
  217. if (!streamOutput)
  218. return;
  219. int totalDropped = obs_output_get_frames_dropped(streamOutput);
  220. int totalFrames = obs_output_get_total_frames(streamOutput);
  221. double percent = (double)totalDropped / (double)totalFrames * 100.0;
  222. if (!totalFrames)
  223. return;
  224. QString text = QTStr("DroppedFrames");
  225. text = text.arg(QString::number(totalDropped),
  226. QString::number(percent, 'f', 1));
  227. droppedFrames->setText(text);
  228. droppedFrames->setMinimumWidth(droppedFrames->width());
  229. /* ----------------------------------- *
  230. * calculate congestion color */
  231. float congestion = obs_output_get_congestion(streamOutput);
  232. float avgCongestion = (congestion + lastCongestion) * 0.5f;
  233. if (avgCongestion < congestion)
  234. avgCongestion = congestion;
  235. if (avgCongestion > 1.0f)
  236. avgCongestion = 1.0f;
  237. if (avgCongestion < EPSILON) {
  238. statusSquare->setPixmap(greenPixmap);
  239. } else if (fabsf(avgCongestion - 1.0f) < EPSILON) {
  240. statusSquare->setPixmap(redPixmap);
  241. } else {
  242. QPixmap pixmap(20, 20);
  243. float red = avgCongestion * 2.0f;
  244. if (red > 1.0f)
  245. red = 1.0f;
  246. red *= 255.0;
  247. float green = (1.0f - avgCongestion) * 2.0f;
  248. if (green > 1.0f)
  249. green = 1.0f;
  250. green *= 255.0;
  251. pixmap.fill(QColor(int(red), int(green), 0));
  252. statusSquare->setPixmap(pixmap);
  253. }
  254. lastCongestion = congestion;
  255. }
  256. void OBSBasicStatusBar::OBSOutputReconnect(void *data, calldata_t *params)
  257. {
  258. OBSBasicStatusBar *statusBar =
  259. reinterpret_cast<OBSBasicStatusBar *>(data);
  260. int seconds = (int)calldata_int(params, "timeout_sec");
  261. QMetaObject::invokeMethod(statusBar, "Reconnect", Q_ARG(int, seconds));
  262. UNUSED_PARAMETER(params);
  263. }
  264. void OBSBasicStatusBar::OBSOutputReconnectSuccess(void *data,
  265. calldata_t *params)
  266. {
  267. OBSBasicStatusBar *statusBar =
  268. reinterpret_cast<OBSBasicStatusBar *>(data);
  269. QMetaObject::invokeMethod(statusBar, "ReconnectSuccess");
  270. UNUSED_PARAMETER(params);
  271. }
  272. void OBSBasicStatusBar::Reconnect(int seconds)
  273. {
  274. OBSBasic *main = qobject_cast<OBSBasic *>(parent());
  275. if (!retries)
  276. main->SysTrayNotify(
  277. QTStr("Basic.SystemTray.Message.Reconnecting"),
  278. QSystemTrayIcon::Warning);
  279. reconnectTimeout = seconds;
  280. if (streamOutput) {
  281. delaySecTotal = obs_output_get_active_delay(streamOutput);
  282. UpdateDelayMsg();
  283. retries++;
  284. }
  285. }
  286. void OBSBasicStatusBar::ReconnectClear()
  287. {
  288. retries = 0;
  289. reconnectTimeout = 0;
  290. bitrateUpdateSeconds = -1;
  291. lastBytesSent = 0;
  292. lastBytesSentTime = os_gettime_ns();
  293. delaySecTotal = 0;
  294. UpdateDelayMsg();
  295. }
  296. void OBSBasicStatusBar::ReconnectSuccess()
  297. {
  298. OBSBasic *main = qobject_cast<OBSBasic *>(parent());
  299. QString msg = QTStr("Basic.StatusBar.ReconnectSuccessful");
  300. showMessage(msg, 4000);
  301. main->SysTrayNotify(msg, QSystemTrayIcon::Information);
  302. ReconnectClear();
  303. if (streamOutput) {
  304. delaySecTotal = obs_output_get_active_delay(streamOutput);
  305. UpdateDelayMsg();
  306. }
  307. }
  308. void OBSBasicStatusBar::UpdateStatusBar()
  309. {
  310. OBSBasic *main = qobject_cast<OBSBasic *>(parent());
  311. UpdateBandwidth();
  312. if (streamOutput)
  313. UpdateStreamTime();
  314. if (recordOutput)
  315. UpdateRecordTime();
  316. UpdateDroppedFrames();
  317. int skipped = video_output_get_skipped_frames(obs_get_video());
  318. int total = video_output_get_total_frames(obs_get_video());
  319. skipped -= startSkippedFrameCount;
  320. total -= startTotalFrameCount;
  321. int diff = skipped - lastSkippedFrameCount;
  322. double percentage = double(skipped) / double(total) * 100.0;
  323. if (diff > 10 && percentage >= 0.1f) {
  324. showMessage(QTStr("HighResourceUsage"), 4000);
  325. if (!main->isVisible() && overloadedNotify) {
  326. main->SysTrayNotify(QTStr("HighResourceUsage"),
  327. QSystemTrayIcon::Warning);
  328. overloadedNotify = false;
  329. }
  330. }
  331. lastSkippedFrameCount = skipped;
  332. }
  333. void OBSBasicStatusBar::StreamDelayStarting(int sec)
  334. {
  335. OBSBasic *main = qobject_cast<OBSBasic *>(parent());
  336. if (!main || !main->outputHandler)
  337. return;
  338. streamOutput = main->outputHandler->streamOutput;
  339. delaySecTotal = delaySecStarting = sec;
  340. UpdateDelayMsg();
  341. Activate();
  342. }
  343. void OBSBasicStatusBar::StreamDelayStopping(int sec)
  344. {
  345. delaySecTotal = delaySecStopping = sec;
  346. UpdateDelayMsg();
  347. }
  348. void OBSBasicStatusBar::StreamStarted(obs_output_t *output)
  349. {
  350. streamOutput = output;
  351. signal_handler_connect(obs_output_get_signal_handler(streamOutput),
  352. "reconnect", OBSOutputReconnect, this);
  353. signal_handler_connect(obs_output_get_signal_handler(streamOutput),
  354. "reconnect_success", OBSOutputReconnectSuccess,
  355. this);
  356. retries = 0;
  357. lastBytesSent = 0;
  358. lastBytesSentTime = os_gettime_ns();
  359. Activate();
  360. }
  361. void OBSBasicStatusBar::StreamStopped()
  362. {
  363. if (streamOutput) {
  364. signal_handler_disconnect(
  365. obs_output_get_signal_handler(streamOutput),
  366. "reconnect", OBSOutputReconnect, this);
  367. signal_handler_disconnect(
  368. obs_output_get_signal_handler(streamOutput),
  369. "reconnect_success", OBSOutputReconnectSuccess, this);
  370. ReconnectClear();
  371. streamOutput = nullptr;
  372. clearMessage();
  373. Deactivate();
  374. }
  375. }
  376. void OBSBasicStatusBar::RecordingStarted(obs_output_t *output)
  377. {
  378. recordOutput = output;
  379. Activate();
  380. }
  381. void OBSBasicStatusBar::RecordingStopped()
  382. {
  383. recordOutput = nullptr;
  384. Deactivate();
  385. }