MainPanel.cpp 9.1 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 "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. // 如果是管理也就是教师 直接过滤当前教师的选项
  105. if (AppEvent::instance()->hasRole("role.admin")) {
  106. // roomInfo.ownerId = AppEvent::instance()->userId();
  107. m_roomListWidget->hide();
  108. } else {
  109. m_roomListWidget->show();
  110. }
  111. // 设置初始化信息
  112. const QString &name = AppEvent::instance()->userName();
  113. userProfile->setUsername(name);
  114. }
  115. void MainPanel::setPushRoomId(const QString &id)
  116. {
  117. // 推流配置
  118. if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  119. SettingsPage::Param param;
  120. param.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  121. param.liveName = id.toStdString();
  122. avRecorder->setSettings(param);
  123. }
  124. // 重新进入房间
  125. chatView->initWebsocket(id);
  126. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  127. MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
  128. QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
  129. QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
  130. roomListPromise
  131. .then([this, playWidget, id](const HttpResponse &response) {
  132. qDebug() << response.code << response.data << response.message;
  133. if (response.code != 0) {
  134. BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
  135. return;
  136. }
  137. RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
  138. qDebug() << "roomInfo.liveStatus.has_value()" << roomInfo.liveStatus.has_value();
  139. int status = roomInfo.liveStatus.value_or(0);
  140. if (status == 1) {
  141. qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
  142. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  143. }
  144. })
  145. .finally([]() { MaskOverlay::instance()->hide(); });
  146. }
  147. }
  148. void MainPanel::setPlayerWidget(QWidget *newPlayer)
  149. {
  150. if (playerWidget) {
  151. playerWidget->setParent(nullptr);
  152. playerWidget->deleteLater();
  153. }
  154. playerWidget = newPlayer;
  155. playerWidget->setParent(playerContainer);
  156. QLayout *oldLayout = playerContainer->layout();
  157. if (oldLayout) {
  158. QLayoutItem *item;
  159. while ((item = oldLayout->takeAt(0)) != nullptr) {
  160. if (item->widget())
  161. item->widget()->setParent(nullptr);
  162. delete item;
  163. }
  164. delete oldLayout;
  165. }
  166. QVBoxLayout *vbox = new QVBoxLayout(playerContainer);
  167. vbox->setContentsMargins(0, 0, 0, 0);
  168. vbox->addWidget(playerWidget);
  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. }