MainPanel.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // setupUI
  25. userProfile = new UserProfileWidget(this);
  26. webSocketClient = new WebSocketClient(this);
  27. chatView = new ChatWindow(webSocketClient);
  28. chatView->setMinimumWidth(400);
  29. QWidget *rightWidget = new QWidget;
  30. QVBoxLayout *vbox = new QVBoxLayout(rightWidget);
  31. vbox->setContentsMargins(0, 0, 0, 0);
  32. vbox->addWidget(userProfile, 0);
  33. vbox->addWidget(chatView, 1);
  34. m_roomListWidget = new QListWidget;
  35. splitter = new QSplitter(Qt::Horizontal, this);
  36. playerContainer = new QWidget(this);
  37. splitter->addWidget(m_roomListWidget);
  38. splitter->addWidget(playerContainer);
  39. splitter->addWidget(rightWidget);
  40. splitter->setStretchFactor(0, 10);
  41. splitter->setStretchFactor(1, 60);
  42. splitter->setStretchFactor(2, 30);
  43. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  44. mainLayout->addWidget(splitter, 1);
  45. mainLayout->setContentsMargins(0, 0, 0, 0);
  46. mainLayout->setSpacing(0);
  47. setLayout(mainLayout);
  48. // initConnect
  49. connect(AppEvent::instance(), &AppEvent::connectionStateChanged, this, [this](bool connected) {
  50. if (userProfile) {
  51. userProfile->setStatus(connected ? "在线" : "离线");
  52. }
  53. });
  54. connect(m_roomListWidget, &QListWidget::itemDoubleClicked, this, &MainPanel::roomItemChanged);
  55. connect(userProfile, &UserProfileWidget::logoutClicked, this, &MainPanel::logoutClicked);
  56. connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
  57. // 这里可以处理 liveStatus 相关逻辑
  58. QJsonParseError err;
  59. QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &err);
  60. if (err.error != QJsonParseError::NoError || !doc.isObject()) {
  61. qDebug() << "[MainPanel] liveStatus: 解析失败" << err.errorString();
  62. return;
  63. }
  64. QJsonObject obj = doc.object();
  65. int liveStatus = obj.value("liveStatus").toInt(0); // 默认-1
  66. if (liveStatus == 1) {
  67. qDebug() << "[MainPanel] liveStatus: 直播中";
  68. if (chatView) {
  69. const QString id = webSocketClient->roomId();
  70. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  71. if (!playWidget->isPlaying()) {
  72. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  73. // QFuture<void> playFuture = QtConcurrent::run([playWidget, id] {
  74. // playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  75. // });
  76. // QtPromise::QPromise<void> playPromise = QtPromise::resolve(playFuture);
  77. // playPromise.then([] {
  78. // // 可选:播放成功后UI反馈
  79. // });
  80. }
  81. }
  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. // 如果是管理也就是教师 直接过滤当前教师的选项
  109. if (AppEvent::instance()->hasRole("role.admin")) {
  110. // roomInfo.ownerId = AppEvent::instance()->userId();
  111. m_roomListWidget->hide();
  112. } else {
  113. m_roomListWidget->show();
  114. }
  115. // 设置初始化信息
  116. const QString &name = AppEvent::instance()->userName();
  117. userProfile->setUsername(name);
  118. }
  119. void MainPanel::setPushRoomId(const QString &id)
  120. {
  121. // 推流配置
  122. if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  123. SettingsPage::Param param;
  124. param.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  125. param.liveName = id.toStdString();
  126. avRecorder->setSettings(param);
  127. }
  128. // 重新进入房间
  129. chatView->initWebsocket(id);
  130. }
  131. void MainPanel::setPlayerWidget(QWidget *newPlayer)
  132. {
  133. if (playerWidget) {
  134. playerWidget->setParent(nullptr);
  135. playerWidget->deleteLater();
  136. }
  137. playerWidget = newPlayer;
  138. playerWidget->setParent(playerContainer);
  139. QLayout *oldLayout = playerContainer->layout();
  140. if (oldLayout) {
  141. QLayoutItem *item;
  142. while ((item = oldLayout->takeAt(0)) != nullptr) {
  143. if (item->widget())
  144. item->widget()->setParent(nullptr);
  145. delete item;
  146. }
  147. delete oldLayout;
  148. }
  149. QVBoxLayout *vbox = new QVBoxLayout(playerContainer);
  150. vbox->setContentsMargins(0, 0, 0, 0);
  151. vbox->addWidget(playerWidget);
  152. }
  153. void MainPanel::roomItemChanged(QListWidgetItem *item)
  154. {
  155. if (item) {
  156. const QString id = item->data(Qt::UserRole + 100).value<QString>();
  157. if (!playerWidget) {
  158. return;
  159. }
  160. // // 推流配置
  161. // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  162. // SettingsPage::Param param;
  163. // param.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  164. // param.liveName = id.toStdString();
  165. // avRecorder->setSettings(param);
  166. // }
  167. // 拉取视频流程
  168. if (PlayWidget *playWidget = qobject_cast<PlayWidget *>(playerWidget)) {
  169. MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
  170. QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
  171. QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
  172. roomListPromise
  173. .then([this, playWidget, id](const HttpResponse &response) {
  174. qDebug() << response.code << response.data << response.message;
  175. if (response.code != 0) {
  176. BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
  177. return;
  178. }
  179. RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
  180. qDebug() << "roomInfo.liveStatus.has_value()"
  181. << roomInfo.liveStatus.has_value();
  182. int status = roomInfo.liveStatus.value_or(0);
  183. if (status == 1) {
  184. qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
  185. playWidget->startToPlay("rtmp://106.55.186.74:1935/stream/V1/" + id);
  186. }
  187. })
  188. .finally([]() { MaskOverlay::instance()->hide(); });
  189. }
  190. }
  191. }