MainPanel.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "MainPanel.h"
  2. #include <QSplitter>
  3. #include <QVBoxLayout>
  4. #include <QListWidget>
  5. #include <util/jsonmapper.h>
  6. #include <QDebug>
  7. #include <qtpromise/qpromise.h>
  8. #include <qtpromise/qpromisefuture.h>
  9. #include <qtpromise/qpromisehelpers.h>
  10. #include <QtConcurrent>
  11. #include "widgets/bubbletip.h"
  12. #include "widgets/chatView/chatwindow.h"
  13. #include "widgets/maskoverlay.h"
  14. #include "widgets/userprofilewidget.h"
  15. #include "widgets/statswidget.h"
  16. #include "AvPlayer2/PlayWidget.h"
  17. #include "api/roomapi.h"
  18. #include "appevent.h"
  19. #include "ui/av_recorder.h"
  20. MainPanel::MainPanel(QWidget *parent)
  21. : QWidget(parent)
  22. , userProfile(nullptr)
  23. , chatView(nullptr)
  24. {
  25. // 初始化防抖定时器
  26. m_debounceTimer = new QTimer(this);
  27. m_debounceTimer->setSingleShot(true);
  28. m_debounceTimer->setInterval(500); // 500ms防抖延迟
  29. connect(m_debounceTimer, &QTimer::timeout, this, &MainPanel::handleDebouncedPlay);
  30. // setupUI
  31. userProfile = new UserProfileWidget(this);
  32. webSocketClient = new WebSocketClient(this);
  33. chatView = new ChatWindow(webSocketClient);
  34. chatView->setMinimumWidth(400);
  35. statsWidget = new StatsWidget(this);
  36. QWidget *rightWidget = new QWidget;
  37. QVBoxLayout *vbox = new QVBoxLayout(rightWidget);
  38. vbox->setContentsMargins(0, 0, 0, 0);
  39. vbox->addWidget(userProfile, 0);
  40. vbox->addWidget(statsWidget, 0);
  41. vbox->addWidget(chatView, 1);
  42. m_roomListWidget = new QListWidget;
  43. splitter = new QSplitter(Qt::Horizontal, this);
  44. playerContainer = new QWidget(this);
  45. splitter->addWidget(m_roomListWidget);
  46. splitter->addWidget(playerContainer);
  47. splitter->addWidget(rightWidget);
  48. splitter->setStretchFactor(0, 10);
  49. splitter->setStretchFactor(1, 60);
  50. splitter->setStretchFactor(2, 30);
  51. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  52. mainLayout->addWidget(splitter, 1);
  53. mainLayout->setContentsMargins(0, 0, 0, 0);
  54. mainLayout->setSpacing(0);
  55. setLayout(mainLayout);
  56. // initConnect
  57. connect(AppEvent::instance(), &AppEvent::connectionStateChanged, this, [this](bool connected) {
  58. if (userProfile) {
  59. userProfile->setStatus(connected ? "在线" : "离线");
  60. }
  61. });
  62. connect(m_roomListWidget, &QListWidget::itemDoubleClicked, this, &MainPanel::roomItemChanged);
  63. connect(userProfile, &UserProfileWidget::logoutClicked, this, &MainPanel::logoutClicked);
  64. connect(webSocketClient, &WebSocketClient::statsUpdate, statsWidget, &StatsWidget::updateStats);
  65. connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
  66. // 这里可以处理 liveStatus 相关逻辑
  67. QJsonParseError err;
  68. QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &err);
  69. if (err.error != QJsonParseError::NoError || !doc.isObject()) {
  70. qDebug() << "[MainPanel] liveStatus: 解析失败" << err.errorString();
  71. return;
  72. }
  73. QJsonObject obj = doc.object();
  74. int liveStatus = obj.value("liveStatus").toInt(0); // 默认-1
  75. if (liveStatus == 1) {
  76. qDebug() << "[MainPanel] liveStatus: 直播中" << chatView;
  77. if (chatView) {
  78. const QString id = webSocketClient->roomId();
  79. // 使用防抖机制处理频繁的请求
  80. m_pendingRoomId = id;
  81. m_debounceTimer->start(); // 重新开始计时,如果在500ms内再次收到请求,会重置定时器
  82. }
  83. // 你的处理逻辑
  84. } else if (liveStatus == 2) {
  85. qDebug() << "[MainPanel] liveStatus: 未开播";
  86. // 你的处理逻辑
  87. } else {
  88. qDebug() << "[MainPanel] liveStatus: 未知状态" << liveStatus;
  89. }
  90. });
  91. }
  92. MainPanel::~MainPanel()
  93. {
  94. if (userProfile) {
  95. delete userProfile;
  96. userProfile = nullptr;
  97. }
  98. }
  99. void MainPanel::setRole(const QStringList &roleList)
  100. {
  101. QWidget *newPlayer = nullptr;
  102. if (roleList.contains("role.admin")) {
  103. newPlayer = new AvRecorder(this);
  104. } else {
  105. newPlayer = new PlayWidget(this);
  106. }
  107. setPlayerWidget(newPlayer);
  108. m_roomListWidget->hide();
  109. // 设置初始化信息
  110. const QString &name = AppEvent::instance()->userName();
  111. userProfile->setUsername(name);
  112. }
  113. void MainPanel::setPushRoomId(const QString &id)
  114. {
  115. // 推流配置
  116. if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  117. SettingsPage::Param param;
  118. param.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  119. param.liveName = id.toStdString();
  120. avRecorder->setSettings(param);
  121. }
  122. // 重新进入房间
  123. chatView->initWebsocket(id);
  124. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  125. MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
  126. QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
  127. QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
  128. roomListPromise
  129. .then([this, playWidget, id](const HttpResponse &response) {
  130. qDebug() << response.code << response.data << response.message;
  131. if (response.code != 0) {
  132. BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
  133. return;
  134. }
  135. RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
  136. qDebug() << "roomInfo.liveStatus.has_value()" << roomInfo.liveStatus.has_value();
  137. int status = roomInfo.liveStatus.value_or(0);
  138. if (status == 1) {
  139. qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
  140. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  141. }
  142. })
  143. .finally([]() { MaskOverlay::instance()->hide(); });
  144. }
  145. }
  146. void MainPanel::setPlayerWidget(QWidget *newPlayer)
  147. {
  148. if (playerWidget) {
  149. playerWidget->setParent(nullptr);
  150. playerWidget->deleteLater();
  151. }
  152. playerWidget = newPlayer;
  153. playerWidget->setParent(playerContainer);
  154. QLayout *oldLayout = playerContainer->layout();
  155. if (oldLayout) {
  156. QLayoutItem *item;
  157. while ((item = oldLayout->takeAt(0)) != nullptr) {
  158. if (item->widget())
  159. item->widget()->setParent(nullptr);
  160. delete item;
  161. }
  162. delete oldLayout;
  163. }
  164. QVBoxLayout *vbox = new QVBoxLayout(playerContainer);
  165. vbox->setContentsMargins(0, 0, 0, 0);
  166. vbox->addWidget(playerWidget, 1); // 添加拉伸因子,让播放器组件占据所有可用空间
  167. // 确保播放器组件能够正确拉伸
  168. playerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  169. }
  170. void MainPanel::roomItemChanged(QListWidgetItem *item)
  171. {
  172. if (item) {
  173. const QString id = item->data(Qt::UserRole + 100).value<QString>();
  174. if (!playerWidget) {
  175. return;
  176. }
  177. // // 推流配置
  178. // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  179. // SettingsPage::Param param;
  180. // param.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  181. // param.liveName = id.toStdString();
  182. // avRecorder->setSettings(param);
  183. // }
  184. // 拉取视频流程
  185. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  186. MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
  187. QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
  188. QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
  189. roomListPromise
  190. .then([this, playWidget, id](const HttpResponse &response) {
  191. qDebug() << response.code << response.data << response.message;
  192. if (response.code != 0) {
  193. BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
  194. return;
  195. }
  196. RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
  197. qDebug() << "roomInfo.liveStatus.has_value()"
  198. << roomInfo.liveStatus.has_value();
  199. int status = roomInfo.liveStatus.value_or(0);
  200. if (status == 1) {
  201. qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
  202. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  203. }
  204. })
  205. .finally([]() { MaskOverlay::instance()->hide(); });
  206. }
  207. }
  208. }
  209. void MainPanel::handleDebouncedPlay()
  210. {
  211. // 防抖处理后的播放逻辑
  212. if (m_pendingRoomId.isEmpty() || !chatView) {
  213. return;
  214. }
  215. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  216. if (!m_isStartingPlay) {
  217. m_isStartingPlay = true;
  218. qDebug() << "[MainPanel] 防抖处理后开始播放:" << m_pendingRoomId;
  219. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + m_pendingRoomId);
  220. m_isStartingPlay = false; // 如果 startToPlay 是同步的
  221. }
  222. }
  223. // 清空待处理的房间ID
  224. m_pendingRoomId.clear();
  225. }