MainPanel.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 "widgets/functionbutton.h"
  13. #include "widgets/maskoverlay.h"
  14. #include "widgets/statswidget.h"
  15. #include "widgets/userprofilewidget.h"
  16. #include "AVPlayer/avplayerwidget.h"
  17. #include "api/roomapi.h"
  18. #include "appevent.h"
  19. #include "widgets/recorderwidget.h"
  20. #include <QComboBox>
  21. #include <QLabel>
  22. #include <QHBoxLayout>
  23. namespace IconUtils {
  24. QIcon createSettingsIcon()
  25. {
  26. QPixmap icon(24, 24);
  27. icon.fill(Qt::transparent);
  28. QPainter painter(&icon);
  29. painter.setRenderHint(QPainter::Antialiasing);
  30. // 绘制齿轮形状
  31. painter.setPen(Qt::white);
  32. painter.setBrush(Qt::white);
  33. painter.drawEllipse(4, 4, 16, 16);
  34. painter.setPen(Qt::NoPen);
  35. painter.setBrush(QColor(64, 158, 255));
  36. painter.drawEllipse(8, 8, 8, 8);
  37. painter.setBrush(Qt::white);
  38. painter.drawRect(11, 2, 2, 6);
  39. painter.drawRect(11, 16, 2, 6);
  40. painter.drawRect(2, 11, 6, 2);
  41. painter.drawRect(16, 11, 6, 2);
  42. return QIcon(icon);
  43. }
  44. QIcon createSearchIcon()
  45. {
  46. QPixmap icon(24, 24);
  47. icon.fill(Qt::transparent);
  48. QPainter painter(&icon);
  49. painter.setRenderHint(QPainter::Antialiasing);
  50. // 绘制放大镜
  51. painter.setPen(Qt::white);
  52. painter.setBrush(Qt::white);
  53. painter.drawEllipse(2, 2, 14, 14);
  54. painter.setPen(Qt::NoPen);
  55. painter.setBrush(QColor(64, 158, 255));
  56. painter.drawEllipse(4, 4, 10, 10);
  57. painter.setBrush(Qt::white);
  58. QPolygon handle;
  59. handle << QPoint(14, 14) << QPoint(20, 20) << QPoint(18, 22) << QPoint(12, 16);
  60. painter.drawPolygon(handle);
  61. return QIcon(icon);
  62. }
  63. QIcon createUserIcon()
  64. {
  65. QPixmap icon(24, 24);
  66. icon.fill(Qt::transparent);
  67. QPainter painter(&icon);
  68. painter.setRenderHint(QPainter::Antialiasing);
  69. // 绘制用户轮廓
  70. painter.setPen(Qt::white);
  71. painter.setBrush(Qt::white);
  72. painter.drawEllipse(4, 2, 16, 16); // 头部
  73. painter.drawRect(6, 18, 12, 4); // 身体
  74. return QIcon(icon);
  75. }
  76. QIcon createAudioDeviceIcon()
  77. {
  78. QPixmap icon(24, 24);
  79. icon.fill(Qt::transparent);
  80. QPainter painter(&icon);
  81. painter.setRenderHint(QPainter::Antialiasing);
  82. // 绘制音频设备图标(麦克风+扬声器组合)
  83. painter.setPen(QPen(Qt::white, 1.5));
  84. painter.setBrush(Qt::white);
  85. // 麦克风部分
  86. painter.drawRoundedRect(3, 2, 6, 8, 2, 2);
  87. painter.drawLine(6, 10, 6, 13);
  88. painter.drawLine(4, 13, 8, 13);
  89. // 扬声器部分
  90. painter.drawRect(13, 8, 3, 3);
  91. painter.drawPolygon(QPolygon() << QPoint(16, 8) << QPoint(19, 6) << QPoint(19, 13) << QPoint(16, 11));
  92. painter.drawArc(20, 7, 2, 4, 0, 180 * 16);
  93. return QIcon(icon);
  94. }
  95. } // namespace IconUtils
  96. MainPanel::MainPanel(QWidget *parent)
  97. : QWidget(parent)
  98. , userProfile(nullptr)
  99. , chatView(nullptr)
  100. {
  101. // 初始化防抖定时器
  102. m_debounceTimer = new QTimer(this);
  103. m_debounceTimer->setSingleShot(true);
  104. m_debounceTimer->setInterval(500); // 500ms防抖延迟
  105. connect(m_debounceTimer, &QTimer::timeout, this, &MainPanel::handleDebouncedPlay);
  106. // setupUI
  107. userProfile = new UserProfileWidget(this);
  108. webSocketClient = new WebSocketClient(this);
  109. chatView = new ChatWindow(webSocketClient);
  110. chatView->setMinimumWidth(400);
  111. statsWidget = new StatsWidget(this);
  112. QWidget *rightWidget = new QWidget;
  113. QVBoxLayout *vbox = new QVBoxLayout(rightWidget);
  114. vbox->setContentsMargins(0, 0, 0, 0);
  115. vbox->addWidget(userProfile, 0);
  116. vbox->addWidget(statsWidget, 0);
  117. vbox->addWidget(chatView, 1);
  118. splitter = new QSplitter(Qt::Horizontal, this);
  119. playerContainer = new QWidget(this);
  120. splitter->addWidget(playerContainer);
  121. splitter->addWidget(rightWidget);
  122. splitter->setStretchFactor(0, 60);
  123. splitter->setStretchFactor(1, 30);
  124. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  125. mainLayout->addWidget(splitter, 1);
  126. mainLayout->setContentsMargins(0, 0, 0, 0);
  127. mainLayout->setSpacing(0);
  128. setLayout(mainLayout);
  129. // 为playerContainer设置初始布局
  130. QVBoxLayout *playerLayout = new QVBoxLayout(playerContainer);
  131. playerLayout->setContentsMargins(0, 0, 0, 0);
  132. buttonGroup = new PopoverButtonGroup(Qt::Horizontal, playerContainer);
  133. // 添加功能按钮
  134. FunctionButton *settingsBtn = new FunctionButton(IconUtils::createSettingsIcon(), "设置", this);
  135. Popover *settingsPopover = new Popover(this);
  136. // settingsPopover->setContentWidget(settingsContent);
  137. buttonGroup->addButton(settingsBtn, settingsPopover);
  138. FunctionButton *searchBtn = new FunctionButton(IconUtils::createSearchIcon(), "搜索", this);
  139. Popover *searchPopover = new Popover(this);
  140. // searchPopover->setContentWidget(searchContent);
  141. buttonGroup->addButton(searchBtn, searchPopover);
  142. FunctionButton *userBtn = new FunctionButton(IconUtils::createUserIcon(), "用户", this);
  143. Popover *userPopover = new Popover(this);
  144. // userPopover->setContentWidget(userContent);
  145. buttonGroup->addButton(userBtn, userPopover);
  146. // 添加音频设备选择按钮
  147. FunctionButton *audioDeviceBtn = new FunctionButton(IconUtils::createAudioDeviceIcon(), "音频设备", this);
  148. Popover *audioDevicePopover = new Popover(this);
  149. // // 使用解耦合版本的音频设备选择器
  150. // m_audioDeviceSelectorDecoupled = new AudioDeviceSelectorIconDecoupled(this);
  151. // audioDevicePopover->setContentWidget(m_audioDeviceSelectorDecoupled);
  152. // 使用 RecorderAudioWidget 作为 Popover 内容(麦克风 + 扬声器)
  153. QWidget *audioContent = new QWidget(audioDevicePopover);
  154. QVBoxLayout *audioLayout = new QVBoxLayout(audioContent);
  155. audioLayout->setContentsMargins(8, 8, 8, 8);
  156. audioLayout->setSpacing(8);
  157. // 麦克风区域
  158. QLabel *micTitle = new QLabel(tr("麦克风"), audioContent);
  159. micTitle->setStyleSheet("font-weight:600;");
  160. m_micWidget = new QComboBox(audioContent);
  161. m_micWidget->setEditable(false);
  162. audioLayout->addWidget(micTitle);
  163. audioLayout->addWidget(m_micWidget);
  164. // 扬声器区域
  165. QLabel *spkTitle = new QLabel(tr("扬声器"), audioContent);
  166. spkTitle->setStyleSheet("font-weight:600; margin-top:4px;");
  167. m_speakerWidget = new QComboBox(audioContent);
  168. m_speakerWidget->setEditable(false);
  169. audioLayout->addWidget(spkTitle);
  170. audioLayout->addWidget(m_speakerWidget);
  171. // 新增:编码器区域
  172. QLabel *encTitle = new QLabel(tr("视频编码器"), audioContent);
  173. encTitle->setStyleSheet("font-weight:600; margin-top:4px;");
  174. m_encoderWidget = new QComboBox(audioContent);
  175. m_encoderWidget->setEditable(false);
  176. audioLayout->addWidget(encTitle);
  177. audioLayout->addWidget(m_encoderWidget);
  178. // 新增:录制控制区域
  179. QLabel *controlTitle = new QLabel(tr("录制控制"), audioContent);
  180. controlTitle->setStyleSheet("font-weight:600; margin-top:8px;");
  181. audioLayout->addWidget(controlTitle);
  182. // 选项区域
  183. m_drawCursorCheckBox = new QCheckBox(tr("绘制鼠标指针"), audioContent);
  184. m_drawCursorCheckBox->setChecked(true);
  185. m_syncRecordCheckBox = new QCheckBox(tr("推流时同步录制"), audioContent);
  186. audioLayout->addWidget(m_drawCursorCheckBox);
  187. audioLayout->addWidget(m_syncRecordCheckBox);
  188. // 按钮区域
  189. QHBoxLayout *buttonLayout = new QHBoxLayout();
  190. m_recordButton = new QPushButton(tr("开始录制"), audioContent);
  191. m_streamButton = new QPushButton(tr("开始推流"), audioContent);
  192. m_settingsButton = new QPushButton(tr("设置"), audioContent);
  193. buttonLayout->addWidget(m_recordButton);
  194. buttonLayout->addWidget(m_streamButton);
  195. buttonLayout->addWidget(m_settingsButton);
  196. audioLayout->addLayout(buttonLayout);
  197. audioDevicePopover->setContentWidget(audioContent);
  198. // 将按钮与 Popover 关联
  199. buttonGroup->addButton(audioDeviceBtn, audioDevicePopover);
  200. // 设备枚举与填充逻辑
  201. auto populateAudioDevices = [this]() {
  202. // 清空缓存
  203. m_micDevices.clear();
  204. m_speakerDevices.clear();
  205. // 获取麦克风列表
  206. AMRECORDER_DEVICE *micArray = nullptr;
  207. int micCount = recorder_get_mics(&micArray);
  208. QStringList micNames;
  209. int defaultMicIndex = -1;
  210. for (int i = 0; i < micCount; ++i) {
  211. m_micDevices.push_back(micArray[i]);
  212. micNames << QString::fromUtf8(micArray[i].name);
  213. if (micArray[i].is_default && defaultMicIndex < 0) defaultMicIndex = i;
  214. }
  215. if (micArray) recorder_free_array(micArray);
  216. // 获取扬声器列表
  217. AMRECORDER_DEVICE *spkArray = nullptr;
  218. int spkCount = recorder_get_speakers(&spkArray);
  219. QStringList spkNames;
  220. int defaultSpkIndex = -1;
  221. for (int i = 0; i < spkCount; ++i) {
  222. m_speakerDevices.push_back(spkArray[i]);
  223. spkNames << QString::fromUtf8(spkArray[i].name);
  224. if (spkArray[i].is_default && defaultSpkIndex < 0) defaultSpkIndex = i;
  225. }
  226. if (spkArray) recorder_free_array(spkArray);
  227. // 更新 UI 列表
  228. if (m_micWidget) {
  229. m_micWidget->clear();
  230. m_micWidget->addItems(micNames);
  231. }
  232. if (m_speakerWidget) {
  233. m_speakerWidget->clear();
  234. m_speakerWidget->addItems(spkNames);
  235. }
  236. // 恢复或设置默认选择
  237. if (m_micWidget) {
  238. if (defaultMicIndex >= 0 && defaultMicIndex < m_micDevices.size()) {
  239. m_micWidget->setCurrentText(QString::fromUtf8(m_micDevices[defaultMicIndex].name));
  240. } else if (!micNames.isEmpty()) {
  241. m_micWidget->setCurrentIndex(0);
  242. }
  243. }
  244. if (m_speakerWidget) {
  245. if (defaultSpkIndex >= 0 && defaultSpkIndex < m_speakerDevices.size()) {
  246. m_speakerWidget->setCurrentText(
  247. QString::fromUtf8(m_speakerDevices[defaultSpkIndex].name));
  248. } else if (!spkNames.isEmpty()) {
  249. m_speakerWidget->setCurrentIndex(0);
  250. }
  251. }
  252. // 将当前选择传递给 RecorderWidget(若存在)
  253. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  254. // 选中麦克风
  255. if (m_micWidget) {
  256. const QString sel = m_micWidget->currentText();
  257. for (const auto &d : m_micDevices) {
  258. if (sel == QString::fromUtf8(d.name)) { rec->setMicDevice(d); break; }
  259. }
  260. }
  261. // 选中扬声器
  262. if (m_speakerWidget) {
  263. const QString sel = m_speakerWidget->currentText();
  264. for (const auto &d : m_speakerDevices) {
  265. if (sel == QString::fromUtf8(d.name)) { rec->setSpeakerDevice(d); break; }
  266. }
  267. }
  268. }
  269. };
  270. // 新增:编码器枚举与填充逻辑
  271. auto populateEncoders = [this]() {
  272. m_encoderList.clear();
  273. m_selectedEncoderId = -1;
  274. AMRECORDER_ENCODERS *encArray = nullptr;
  275. int encCount = recorder_get_vencoders(&encArray);
  276. QStringList encNames;
  277. for (int i = 0; i < encCount; ++i) {
  278. m_encoderList.push_back(encArray[i]);
  279. encNames << QString::fromUtf8(encArray[i].name);
  280. }
  281. if (encArray) recorder_free_array(encArray);
  282. if (m_encoderWidget) {
  283. m_encoderWidget->clear();
  284. m_encoderWidget->addItems(encNames);
  285. if (!m_encoderList.isEmpty()) {
  286. m_encoderWidget->setCurrentIndex(0);
  287. m_selectedEncoderId = m_encoderList[0].id;
  288. }
  289. }
  290. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  291. if (m_selectedEncoderId >= 0) rec->setVideoEncoderId(m_selectedEncoderId);
  292. }
  293. };
  294. // 首次填充设备和编码器
  295. populateAudioDevices();
  296. populateEncoders();
  297. // 连接设备选择变化 -> 传给 RecorderWidget
  298. connect(m_micWidget, &QComboBox::currentTextChanged, this, [this](const QString &deviceName) {
  299. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  300. for (const auto &d : m_micDevices) {
  301. if (deviceName == QString::fromUtf8(d.name)) {
  302. rec->setMicDevice(d);
  303. break;
  304. }
  305. }
  306. }
  307. });
  308. connect(m_speakerWidget, &QComboBox::currentTextChanged, this, [this](const QString &deviceName) {
  309. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  310. for (const auto &d : m_speakerDevices) {
  311. if (deviceName == QString::fromUtf8(d.name)) {
  312. rec->setSpeakerDevice(d);
  313. break;
  314. }
  315. }
  316. }
  317. });
  318. // 新增:编码器选择变化 -> 传给 RecorderWidget
  319. connect(m_encoderWidget, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
  320. if (index >= 0 && index < m_encoderList.size()) {
  321. m_selectedEncoderId = m_encoderList[index].id;
  322. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  323. rec->setVideoEncoderId(m_selectedEncoderId);
  324. }
  325. }
  326. });
  327. // 新增:录制控制按钮信号连接
  328. connect(m_recordButton, &QPushButton::clicked, this, &MainPanel::onRecordButtonClicked);
  329. connect(m_streamButton, &QPushButton::clicked, this, &MainPanel::onStreamButtonClicked);
  330. connect(m_settingsButton, &QPushButton::clicked, this, &MainPanel::onSettingsButtonClicked);
  331. // 维持原有的AudioDeviceSelectorIcon兼容代码(保持注释)
  332. // m_audioDeviceSelector = new AudioDeviceSelectorIcon(this);
  333. // connect(m_audioDeviceSelector, &AudioDeviceSelectorIcon::microphoneDeviceSelected,
  334. // this, [this](const AudioDeviceInfo& device) {
  335. // qDebug() << "[MainPanel] 麦克风设备已选择(兼容模式):" << device.name;
  336. // // 通知AvRecorder进行设备切换
  337. // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  338. // //avRecorder->switchMicrophoneDevice(device.id, device.name);
  339. // }
  340. // });
  341. // connect(m_audioDeviceSelector, &AudioDeviceSelectorIcon::speakerDeviceSelected,
  342. // this, [this](const AudioDeviceInfo& device) {
  343. // qDebug() << "[MainPanel] 扬声器设备已选择:" << device.name;
  344. // // 通知AvRecorder进行设备切换
  345. // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
  346. // // avRecorder->switchSpeakerDevice(device.id, device.name);
  347. // }
  348. // });
  349. // 添加一个动作按钮到按钮组(没有Popover)
  350. FunctionButton *actionButton = new FunctionButton(IconUtils::createSettingsIcon(),
  351. "执行操作",
  352. this);
  353. buttonGroup->addButton(actionButton, nullptr);
  354. // 将buttonGroup添加到playerContainer的布局中
  355. playerLayout = qobject_cast<QVBoxLayout*>(playerContainer->layout());
  356. if (!playerLayout) {
  357. playerLayout = new QVBoxLayout(playerContainer);
  358. playerLayout->setContentsMargins(0, 0, 0, 0);
  359. }
  360. playerLayout->addStretch(1); // 添加弹性空间,将buttonGroup推到底部
  361. playerLayout->addWidget(buttonGroup, 0); // 添加buttonGroup到底部,不拉伸
  362. buttonGroup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 使用固定大小策略
  363. // initConnect
  364. connect(AppEvent::instance(), &AppEvent::connectionStateChanged, this, [this](bool connected) {
  365. if (userProfile) {
  366. userProfile->setStatus(connected ? "在线" : "离线");
  367. }
  368. });
  369. connect(userProfile, &UserProfileWidget::logoutClicked, this, &MainPanel::logoutClicked);
  370. connect(webSocketClient, &WebSocketClient::statsUpdate, statsWidget, &StatsWidget::updateStats);
  371. connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
  372. // 这里可以处理 liveStatus 相关逻辑
  373. QJsonParseError err;
  374. QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &err);
  375. if (err.error != QJsonParseError::NoError || !doc.isObject()) {
  376. qDebug() << "[MainPanel] liveStatus: 解析失败" << err.errorString();
  377. return;
  378. }
  379. QJsonObject obj = doc.object();
  380. int liveStatus = obj.value("liveStatus").toInt(0); // 默认-1
  381. if (liveStatus == 1) {
  382. qDebug() << "[MainPanel] liveStatus: 直播中" << chatView;
  383. if (chatView) {
  384. const QString id = webSocketClient->roomId();
  385. // 使用防抖机制处理频繁的请求
  386. m_pendingRoomId = id;
  387. m_debounceTimer->start(); // 重新开始计时,如果在500ms内再次收到请求,会重置定时器
  388. }
  389. // 你的处理逻辑
  390. } else if (liveStatus == 2) {
  391. qDebug() << "[MainPanel] liveStatus: 未开播";
  392. // 你的处理逻辑
  393. } else {
  394. qDebug() << "[MainPanel] liveStatus: 未知状态" << liveStatus;
  395. }
  396. });
  397. }
  398. MainPanel::~MainPanel()
  399. {
  400. if (userProfile) {
  401. delete userProfile;
  402. userProfile = nullptr;
  403. }
  404. if (m_recorderStandalone) {
  405. m_recorderStandalone->deleteLater();
  406. m_recorderStandalone = nullptr;
  407. }
  408. if (m_avPlayerStandalone) {
  409. m_avPlayerStandalone->deleteLater();
  410. m_avPlayerStandalone = nullptr;
  411. }
  412. if (m_chatStandalone) {
  413. m_chatStandalone->deleteLater();
  414. m_chatStandalone = nullptr;
  415. }
  416. }
  417. void MainPanel::setRole(const QStringList &roleList)
  418. {
  419. QWidget *newPlayer = nullptr;
  420. if (roleList.contains("role.admin")) {
  421. newPlayer = new RecorderWidget(this);
  422. } else {
  423. newPlayer = new AVPlayerWidget(this);
  424. }
  425. setPlayerWidget(newPlayer);
  426. // 设置初始化信息
  427. const QString &name = AppEvent::instance()->userName();
  428. userProfile->setUsername(name);
  429. }
  430. void MainPanel::setPushRoomId(const QString &id)
  431. {
  432. // 推流配置
  433. if (RecorderWidget *recorderWidget = qobject_cast<RecorderWidget *>(playerWidget)) {
  434. RecorderWidget::Settings settings;
  435. settings.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  436. settings.liveName = id.toStdString();
  437. recorderWidget->setSettings(settings);
  438. }
  439. // 重新进入房间
  440. chatView->initWebsocket(id);
  441. if (m_chatStandalone) {
  442. m_chatStandalone->initWebsocket(id);
  443. }
  444. if (AVPlayerWidget *playWidget = qobject_cast<AVPlayerWidget *>(playerWidget)) {
  445. MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
  446. QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
  447. QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
  448. roomListPromise
  449. .then([this, playWidget, id](const HttpResponse &response) {
  450. qDebug() << response.code << response.data << response.message;
  451. if (response.code != 0) {
  452. BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
  453. return;
  454. }
  455. RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
  456. qDebug() << "roomInfo.liveStatus.has_value()" << roomInfo.liveStatus.has_value();
  457. int status = roomInfo.liveStatus.value_or(0);
  458. if (status == 1) {
  459. qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
  460. playWidget->play("rtmp://106.55.186.74:1935/stream/V1/" + id);
  461. }
  462. })
  463. .finally([]() { MaskOverlay::instance()->hide(); });
  464. }
  465. }
  466. void MainPanel::setPlayerWidget(QWidget *newPlayer)
  467. {
  468. if (playerWidget) {
  469. playerWidget->setParent(nullptr);
  470. playerWidget->deleteLater();
  471. }
  472. playerWidget = newPlayer;
  473. playerWidget->setParent(playerContainer);
  474. // 获取现有布局并清理(保留buttonGroup)
  475. QVBoxLayout *vbox = qobject_cast<QVBoxLayout*>(playerContainer->layout());
  476. if (vbox) {
  477. // 清理除了buttonGroup之外的所有项目
  478. QLayoutItem *item;
  479. while (vbox->count() > 0) {
  480. item = vbox->takeAt(0);
  481. if (item->widget() && item->widget() != buttonGroup) {
  482. item->widget()->setParent(nullptr);
  483. }
  484. if (item->spacerItem()) {
  485. delete item; // 删除spacer
  486. } else if (item->widget() != buttonGroup) {
  487. delete item;
  488. }
  489. }
  490. } else {
  491. // 如果没有布局或布局类型不对,创建新的
  492. if (playerContainer->layout()) {
  493. delete playerContainer->layout();
  494. }
  495. vbox = new QVBoxLayout(playerContainer);
  496. vbox->setContentsMargins(0, 0, 0, 0);
  497. }
  498. // 重新添加组件:播放器在上,buttonGroup在下
  499. vbox->addWidget(playerWidget, 1); // 添加拉伸因子,让播放器组件占据所有可用空间
  500. vbox->addWidget(buttonGroup, 0); // 添加buttonGroup到底部,不拉伸
  501. // 确保播放器组件能够正确拉伸
  502. playerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  503. // 确保buttonGroup固定在底部
  504. buttonGroup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 使用固定大小策略
  505. // 如果新的 player 是 RecorderWidget,则同步当前选择的音频设备
  506. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  507. if (m_micWidget) {
  508. const QString sel = m_micWidget->currentText();
  509. for (const auto &d : m_micDevices) {
  510. if (sel == QString::fromUtf8(d.name)) { rec->setMicDevice(d); break; }
  511. }
  512. }
  513. if (m_speakerWidget) {
  514. const QString sel = m_speakerWidget->currentText();
  515. for (const auto &d : m_speakerDevices) {
  516. if (sel == QString::fromUtf8(d.name)) { rec->setSpeakerDevice(d); break; }
  517. }
  518. }
  519. // 同步编码器
  520. if (m_selectedEncoderId >= 0) {
  521. rec->setVideoEncoderId(m_selectedEncoderId);
  522. } else if (m_encoderWidget && m_encoderWidget->count() > 0 && !m_encoderList.isEmpty()) {
  523. int idx = m_encoderWidget->currentIndex();
  524. if (idx >= 0 && idx < m_encoderList.size()) {
  525. rec->setVideoEncoderId(m_encoderList[idx].id);
  526. }
  527. }
  528. }
  529. }
  530. void MainPanel::handleDebouncedPlay()
  531. {
  532. // 防抖处理后的播放逻辑
  533. if (m_pendingRoomId.isEmpty() || !chatView) {
  534. return;
  535. }
  536. if (AVPlayerWidget *playWidget = qobject_cast<AVPlayerWidget *>(playerWidget)) {
  537. if (!m_isStartingPlay) {
  538. m_isStartingPlay = true;
  539. qDebug() << "[MainPanel] 防抖处理后开始播放:" << m_pendingRoomId;
  540. playWidget->play("rtmp://106.55.186.74:1935/stream/V1/" + m_pendingRoomId);
  541. m_isStartingPlay = false; // 如果 startToPlay 是同步的
  542. }
  543. }
  544. // 清空待处理的房间ID
  545. m_pendingRoomId.clear();
  546. }
  547. void MainPanel::initAudioDeviceSelectors()
  548. {
  549. // 注册音频设备选择器工厂
  550. // DeviceManager* deviceManager = DeviceManager::instance();
  551. // AudioDeviceSelectorFactory* audioFactory = new AudioDeviceSelectorFactory(this);
  552. // deviceManager->registerFactory("audio", audioFactory);
  553. // // 创建麦克风和扬声器选择器
  554. // m_microphoneSelector = deviceManager->createSelector("audio", "microphone");
  555. // m_speakerSelector = deviceManager->createSelector("audio", "speaker");
  556. // // 设置到解耦版本的UI组件
  557. // if (m_audioDeviceSelectorDecoupled) {
  558. // m_audioDeviceSelectorDecoupled->setMicrophoneSelector(m_microphoneSelector);
  559. // m_audioDeviceSelectorDecoupled->setSpeakerSelector(m_speakerSelector);
  560. // }
  561. qDebug() << "[MainPanel] 音频设备选择器初始化完成";
  562. }
  563. void MainPanel::showRecorderStandalone()
  564. {
  565. if (!m_recorderStandalone) {
  566. m_recorderStandalone = new RecorderWidget(nullptr);
  567. m_recorderStandalone->setAttribute(Qt::WA_DeleteOnClose, false);
  568. connect(m_recorderStandalone, &QObject::destroyed, this, [this]() {
  569. m_recorderStandalone = nullptr;
  570. });
  571. m_recorderStandalone->setWindowTitle(tr("RecorderWidget"));
  572. }
  573. m_recorderStandalone->show();
  574. m_recorderStandalone->raise();
  575. m_recorderStandalone->activateWindow();
  576. }
  577. void MainPanel::showPlayerStandalone()
  578. {
  579. if (!m_avPlayerStandalone) {
  580. m_avPlayerStandalone = new AVPlayerWidget(nullptr);
  581. m_avPlayerStandalone->setAttribute(Qt::WA_DeleteOnClose, false);
  582. connect(m_avPlayerStandalone, &QObject::destroyed, this, [this]() {
  583. m_avPlayerStandalone = nullptr;
  584. });
  585. m_avPlayerStandalone->setWindowTitle(tr("AVPlayerWidget"));
  586. }
  587. m_avPlayerStandalone->show();
  588. m_avPlayerStandalone->raise();
  589. m_avPlayerStandalone->activateWindow();
  590. }
  591. void MainPanel::showChatStandalone()
  592. {
  593. if (!m_chatStandalone) {
  594. m_chatStandalone = new ChatWindow(webSocketClient);
  595. m_chatStandalone->setAttribute(Qt::WA_DeleteOnClose, false);
  596. connect(m_chatStandalone, &QObject::destroyed, this, [this]() {
  597. m_chatStandalone = nullptr;
  598. });
  599. m_chatStandalone->setMinimumWidth(400);
  600. m_chatStandalone->setWindowTitle(tr("ChatWindow"));
  601. // 同步当前房间
  602. if (webSocketClient) {
  603. const QString rid = webSocketClient->roomId();
  604. if (!rid.isEmpty()) {
  605. m_chatStandalone->initWebsocket(rid);
  606. }
  607. }
  608. }
  609. m_chatStandalone->show();
  610. m_chatStandalone->raise();
  611. m_chatStandalone->activateWindow();
  612. }
  613. void MainPanel::showChatEmbedded()
  614. {
  615. if (chatView) {
  616. chatView->show();
  617. chatView->raise();
  618. }
  619. if (m_chatStandalone) {
  620. m_chatStandalone->hide();
  621. }
  622. }
  623. // 新增:录制控制按钮的槽函数实现
  624. void MainPanel::onRecordButtonClicked()
  625. {
  626. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  627. // 同步选项设置到RecorderWidget
  628. // 注意:这里需要RecorderWidget提供设置选项的接口
  629. // 调用RecorderWidget的录制方法
  630. rec->onRecordButtonClicked();
  631. // 更新按钮文本
  632. if (m_recordButton) {
  633. if (m_recordButton->text() == tr("开始录制")) {
  634. m_recordButton->setText(tr("停止录制"));
  635. } else {
  636. m_recordButton->setText(tr("开始录制"));
  637. }
  638. }
  639. }
  640. }
  641. void MainPanel::onStreamButtonClicked()
  642. {
  643. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  644. // 同步选项设置到RecorderWidget
  645. // 调用RecorderWidget的推流方法
  646. rec->onStreamButtonClicked();
  647. // 更新按钮文本
  648. if (m_streamButton) {
  649. if (m_streamButton->text() == tr("开始推流")) {
  650. m_streamButton->setText(tr("停止推流"));
  651. } else {
  652. m_streamButton->setText(tr("开始推流"));
  653. }
  654. }
  655. }
  656. }
  657. void MainPanel::onSettingsButtonClicked()
  658. {
  659. if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
  660. // 调用RecorderWidget的设置方法
  661. rec->onSettingsButtonClicked();
  662. }
  663. }