MainPanel.cpp 9.0 KB

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