MainPanel.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "MainPanel.h"
  2. #include <QSplitter>
  3. #include <QVBoxLayout>
  4. #include <util/jsonmapper.h>
  5. #include <QDebug>
  6. #include <qtpromise/qpromise.h>
  7. #include <qtpromise/qpromisefuture.h>
  8. #include <qtpromise/qpromisehelpers.h>
  9. #include <QtConcurrent>
  10. #include "widgets/bubbletip.h"
  11. #include "widgets/chatView/chatwindow.h"
  12. #include "advanceddockingsystem/dockmanager.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. // 初始化 DockManager
  37. m_dockManager = new ADS::DockManager(this);
  38. m_dockManager->setStyleSheet(""); // 使用应用程序的样式表
  39. // 创建右侧面板作为可停靠的 DockWidget
  40. QWidget *rightWidget = new QWidget;
  41. QVBoxLayout *vbox = new QVBoxLayout(rightWidget);
  42. vbox->setContentsMargins(0, 0, 0, 0);
  43. vbox->addWidget(userProfile, 0);
  44. vbox->addWidget(statsWidget, 0);
  45. vbox->addWidget(chatView, 1);
  46. ADS::DockWidget* rightDockWidget = new ADS::DockWidget("聊天面板", this);
  47. rightDockWidget->setWidget(rightWidget);
  48. rightDockWidget->setFeature(ADS::DockWidget::DockWidgetClosable, true);
  49. // 创建中央内容
  50. playerContainer = new QWidget(this);
  51. // 创建中央播放器 DockWidget
  52. ADS::DockWidget* centerDockWidget = new ADS::DockWidget("播放器", this);
  53. centerDockWidget->setWidget(playerContainer);
  54. centerDockWidget->setFeature(ADS::DockWidget::DockWidgetClosable, false);
  55. centerDockWidget->setFeature(ADS::DockWidget::DockWidgetMovable, false);
  56. centerDockWidget->setFeature(ADS::DockWidget::DockWidgetFloatable, false);
  57. // 将 DockWidget 添加到 DockManager
  58. // m_dockManager->addDockWidget(ADS::LeftDockWidgetArea, leftDockWidget);
  59. m_dockManager->addDockWidget(ADS::CenterDockWidgetArea, centerDockWidget);
  60. m_dockManager->addDockWidget(ADS::RightDockWidgetArea, rightDockWidget);
  61. // 保存 splitter 引用以保持兼容性
  62. splitter = nullptr; // 不再使用 QSplitter
  63. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  64. mainLayout->addWidget(m_dockManager, 1);
  65. mainLayout->setContentsMargins(0, 0, 0, 0);
  66. mainLayout->setSpacing(0);
  67. setLayout(mainLayout);
  68. // initConnect
  69. connect(AppEvent::instance(), &AppEvent::connectionStateChanged, this, [this](bool connected) {
  70. if (userProfile) {
  71. userProfile->setStatus(connected ? "在线" : "离线");
  72. }
  73. });
  74. connect(userProfile, &UserProfileWidget::logoutClicked, this, &MainPanel::logoutClicked);
  75. connect(webSocketClient, &WebSocketClient::statsUpdate, statsWidget, &StatsWidget::updateStats);
  76. connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
  77. // 这里可以处理 liveStatus 相关逻辑
  78. QJsonParseError err;
  79. QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &err);
  80. if (err.error != QJsonParseError::NoError || !doc.isObject()) {
  81. qDebug() << "[MainPanel] liveStatus: 解析失败" << err.errorString();
  82. return;
  83. }
  84. QJsonObject obj = doc.object();
  85. int liveStatus = obj.value("liveStatus").toInt(0); // 默认-1
  86. if (liveStatus == 1) {
  87. qDebug() << "[MainPanel] liveStatus: 直播中" << chatView;
  88. if (chatView) {
  89. const QString id = webSocketClient->roomId();
  90. // 使用防抖机制处理频繁的请求
  91. m_pendingRoomId = id;
  92. m_debounceTimer->start(); // 重新开始计时,如果在500ms内再次收到请求,会重置定时器
  93. }
  94. // 你的处理逻辑
  95. } else if (liveStatus == 2) {
  96. qDebug() << "[MainPanel] liveStatus: 未开播";
  97. // 你的处理逻辑
  98. } else {
  99. qDebug() << "[MainPanel] liveStatus: 未知状态" << liveStatus;
  100. }
  101. });
  102. }
  103. MainPanel::~MainPanel()
  104. {
  105. if (userProfile) {
  106. delete userProfile;
  107. userProfile = nullptr;
  108. }
  109. }
  110. void MainPanel::setRole(const QStringList &roleList)
  111. {
  112. QWidget *newPlayer = nullptr;
  113. if (roleList.contains("role.admin")) {
  114. newPlayer = new AvRecorder(this);
  115. } else {
  116. newPlayer = new PlayWidget(this);
  117. }
  118. setPlayerWidget(newPlayer);
  119. // 设置初始化信息
  120. const QString &name = AppEvent::instance()->userName();
  121. userProfile->setUsername(name);
  122. }
  123. void MainPanel::setPushRoomId(const QString &id)
  124. {
  125. // 推流配置
  126. if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  127. SettingsPage::Param param;
  128. param.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  129. param.liveName = id.toStdString();
  130. avRecorder->setSettings(param);
  131. }
  132. // 重新进入房间
  133. chatView->initWebsocket(id);
  134. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  135. MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
  136. QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
  137. QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
  138. roomListPromise
  139. .then([this, playWidget, id](const HttpResponse &response) {
  140. qDebug() << response.code << response.data << response.message;
  141. if (response.code != 0) {
  142. BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
  143. return;
  144. }
  145. RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
  146. qDebug() << "roomInfo.liveStatus.has_value()" << roomInfo.liveStatus.has_value();
  147. int status = roomInfo.liveStatus.value_or(0);
  148. if (status == 1) {
  149. qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
  150. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  151. }
  152. })
  153. .finally([]() { MaskOverlay::instance()->hide(); });
  154. }
  155. }
  156. void MainPanel::setPlayerWidget(QWidget *newPlayer)
  157. {
  158. if (playerWidget) {
  159. playerWidget->setParent(nullptr);
  160. playerWidget->deleteLater();
  161. }
  162. playerWidget = newPlayer;
  163. playerWidget->setParent(playerContainer);
  164. QLayout *oldLayout = playerContainer->layout();
  165. if (oldLayout) {
  166. QLayoutItem *item;
  167. while ((item = oldLayout->takeAt(0)) != nullptr) {
  168. if (item->widget())
  169. item->widget()->setParent(nullptr);
  170. delete item;
  171. }
  172. delete oldLayout;
  173. }
  174. QVBoxLayout *vbox = new QVBoxLayout(playerContainer);
  175. vbox->setContentsMargins(0, 0, 0, 0);
  176. vbox->addWidget(playerWidget, 1); // 添加拉伸因子,让播放器组件占据所有可用空间
  177. // 确保播放器组件能够正确拉伸
  178. playerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  179. }
  180. void MainPanel::handleDebouncedPlay()
  181. {
  182. // 防抖处理后的播放逻辑
  183. if (m_pendingRoomId.isEmpty() || !chatView) {
  184. return;
  185. }
  186. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  187. if (!m_isStartingPlay) {
  188. m_isStartingPlay = true;
  189. qDebug() << "[MainPanel] 防抖处理后开始播放:" << m_pendingRoomId;
  190. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + m_pendingRoomId);
  191. m_isStartingPlay = false; // 如果 startToPlay 是同步的
  192. }
  193. }
  194. // 清空待处理的房间ID
  195. m_pendingRoomId.clear();
  196. }