MainPanel.cpp 29 KB

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