| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843 |
- #include "MainPanel.h"
- #include <QSplitter>
- #include <QVBoxLayout>
- #include <util/jsonmapper.h>
- #include <QDebug>
- #include <qtpromise/qpromise.h>
- #include <qtpromise/qpromisefuture.h>
- #include <qtpromise/qpromisehelpers.h>
- #include <QtConcurrent>
- #include "widgets/bubbletip.h"
- #include "widgets/chatView/chatwindow.h"
- #include "widgets/functionbutton.h"
- #include "widgets/maskoverlay.h"
- #include "widgets/statswidget.h"
- #include "widgets/userprofilewidget.h"
- #include "AVPlayer/avplayerwidget.h"
- #include "api/roomapi.h"
- #include "appevent.h"
- #include "widgets/recorderwidget.h"
- #include <QComboBox>
- #include <QLabel>
- #include <QHBoxLayout>
- namespace IconUtils {
- QIcon createSettingsIcon()
- {
- QPixmap icon(24, 24);
- icon.fill(Qt::transparent);
- QPainter painter(&icon);
- painter.setRenderHint(QPainter::Antialiasing);
- // 绘制齿轮形状
- painter.setPen(Qt::white);
- painter.setBrush(Qt::white);
- painter.drawEllipse(4, 4, 16, 16);
- painter.setPen(Qt::NoPen);
- painter.setBrush(QColor(64, 158, 255));
- painter.drawEllipse(8, 8, 8, 8);
- painter.setBrush(Qt::white);
- painter.drawRect(11, 2, 2, 6);
- painter.drawRect(11, 16, 2, 6);
- painter.drawRect(2, 11, 6, 2);
- painter.drawRect(16, 11, 6, 2);
- return QIcon(icon);
- }
- QIcon createSearchIcon()
- {
- QPixmap icon(24, 24);
- icon.fill(Qt::transparent);
- QPainter painter(&icon);
- painter.setRenderHint(QPainter::Antialiasing);
- // 绘制放大镜
- painter.setPen(Qt::white);
- painter.setBrush(Qt::white);
- painter.drawEllipse(2, 2, 14, 14);
- painter.setPen(Qt::NoPen);
- painter.setBrush(QColor(64, 158, 255));
- painter.drawEllipse(4, 4, 10, 10);
- painter.setBrush(Qt::white);
- QPolygon handle;
- handle << QPoint(14, 14) << QPoint(20, 20) << QPoint(18, 22) << QPoint(12, 16);
- painter.drawPolygon(handle);
- return QIcon(icon);
- }
- QIcon createUserIcon()
- {
- QPixmap icon(24, 24);
- icon.fill(Qt::transparent);
- QPainter painter(&icon);
- painter.setRenderHint(QPainter::Antialiasing);
- // 绘制用户轮廓
- painter.setPen(Qt::white);
- painter.setBrush(Qt::white);
- painter.drawEllipse(4, 2, 16, 16); // 头部
- painter.drawRect(6, 18, 12, 4); // 身体
- return QIcon(icon);
- }
- QIcon createAudioDeviceIcon()
- {
- QPixmap icon(24, 24);
- icon.fill(Qt::transparent);
- QPainter painter(&icon);
- painter.setRenderHint(QPainter::Antialiasing);
- // 绘制音频设备图标(麦克风+扬声器组合)
- painter.setPen(QPen(Qt::white, 1.5));
- painter.setBrush(Qt::white);
-
- // 麦克风部分
- painter.drawRoundedRect(3, 2, 6, 8, 2, 2);
- painter.drawLine(6, 10, 6, 13);
- painter.drawLine(4, 13, 8, 13);
-
- // 扬声器部分
- painter.drawRect(13, 8, 3, 3);
- painter.drawPolygon(QPolygon() << QPoint(16, 8) << QPoint(19, 6) << QPoint(19, 13) << QPoint(16, 11));
- painter.drawArc(20, 7, 2, 4, 0, 180 * 16);
- return QIcon(icon);
- }
- QIcon createStreamIcon()
- {
- QPixmap icon(24, 24);
- icon.fill(Qt::transparent);
- QPainter painter(&icon);
- painter.setRenderHint(QPainter::Antialiasing);
- // 绘制推流图标(播放按钮+信号波纹)
- painter.setPen(QPen(Qt::white, 1.5));
- painter.setBrush(Qt::white);
-
- // 播放按钮(三角形)
- QPolygon triangle;
- triangle << QPoint(6, 4) << QPoint(6, 20) << QPoint(18, 12);
- painter.drawPolygon(triangle);
-
- // 信号波纹
- painter.setBrush(Qt::NoBrush);
- painter.drawArc(16, 8, 4, 8, 0, 180 * 16);
- painter.drawArc(18, 6, 4, 12, 0, 180 * 16);
- painter.drawArc(20, 4, 4, 16, 0, 180 * 16);
- return QIcon(icon);
- }
- } // namespace IconUtils
- MainPanel::MainPanel(QWidget *parent)
- : QWidget(parent)
- , userProfile(nullptr)
- , chatView(nullptr)
- {
- // 初始化防抖定时器
- m_debounceTimer = new QTimer(this);
- m_debounceTimer->setSingleShot(true);
- m_debounceTimer->setInterval(500); // 500ms防抖延迟
- connect(m_debounceTimer, &QTimer::timeout, this, &MainPanel::handleDebouncedPlay);
- // setupUI
- userProfile = new UserProfileWidget(this);
- webSocketClient = new WebSocketClient(this);
- chatView = new ChatWindow(webSocketClient);
- chatView->setMinimumWidth(400);
- // 连接聊天窗口关闭请求信号
- connect(chatView, &ChatWindow::windowCloseRequested, this, &MainPanel::onChatWindowCloseRequested);
- statsWidget = new StatsWidget(this);
- QWidget *rightWidget = new QWidget;
- QVBoxLayout *vbox = new QVBoxLayout(rightWidget);
- vbox->setContentsMargins(0, 0, 0, 0);
- vbox->addWidget(userProfile, 0);
- vbox->addWidget(statsWidget, 0);
-
- // 创建聊天窗口容器
- m_chatContainer = new QWidget(rightWidget);
- QVBoxLayout *chatLayout = new QVBoxLayout(m_chatContainer);
- chatLayout->setContentsMargins(0, 0, 0, 0);
- chatLayout->addWidget(chatView);
- vbox->addWidget(m_chatContainer, 1);
- splitter = new QSplitter(Qt::Horizontal, this);
- playerContainer = new QWidget(this);
- splitter->addWidget(playerContainer);
- splitter->addWidget(rightWidget);
- splitter->setStretchFactor(0, 60);
- splitter->setStretchFactor(1, 30);
- QVBoxLayout *mainLayout = new QVBoxLayout(this);
- mainLayout->addWidget(splitter, 1);
- mainLayout->setContentsMargins(0, 0, 0, 0);
- mainLayout->setSpacing(0);
- setLayout(mainLayout);
- // 为playerContainer设置初始布局
- QVBoxLayout *playerLayout = new QVBoxLayout(playerContainer);
- playerLayout->setContentsMargins(0, 0, 0, 0);
-
- buttonGroup = new PopoverButtonGroup(Qt::Horizontal, playerContainer);
- // 添加功能按钮
- FunctionButton *settingsBtn = new FunctionButton(IconUtils::createSettingsIcon(), "设置", this);
- Popover *settingsPopover = new Popover(this);
- // settingsPopover->setContentWidget(settingsContent);
- buttonGroup->addButton(settingsBtn, settingsPopover);
- FunctionButton *searchBtn = new FunctionButton(IconUtils::createSearchIcon(), "搜索", this);
- Popover *searchPopover = new Popover(this);
- // searchPopover->setContentWidget(searchContent);
- buttonGroup->addButton(searchBtn, searchPopover);
- FunctionButton *userBtn = new FunctionButton(IconUtils::createUserIcon(), "用户", this);
- Popover *userPopover = new Popover(this);
- // userPopover->setContentWidget(userContent);
- buttonGroup->addButton(userBtn, userPopover);
- // 添加音频设备选择按钮
- FunctionButton *audioDeviceBtn = new FunctionButton(IconUtils::createAudioDeviceIcon(), "音频设备", this);
- Popover *audioDevicePopover = new Popover(this);
- // // 使用解耦合版本的音频设备选择器
- // m_audioDeviceSelectorDecoupled = new AudioDeviceSelectorIconDecoupled(this);
- // audioDevicePopover->setContentWidget(m_audioDeviceSelectorDecoupled);
- // 使用 RecorderAudioWidget 作为 Popover 内容(麦克风 + 扬声器)
- QWidget *audioContent = new QWidget(audioDevicePopover);
- QVBoxLayout *audioLayout = new QVBoxLayout(audioContent);
- audioLayout->setContentsMargins(8, 8, 8, 8);
- audioLayout->setSpacing(8);
- // 麦克风区域
- QLabel *micTitle = new QLabel(tr("麦克风"), audioContent);
- micTitle->setStyleSheet("font-weight:600;");
- m_micWidget = new QComboBox(audioContent);
- m_micWidget->setEditable(false);
- audioLayout->addWidget(micTitle);
- audioLayout->addWidget(m_micWidget);
- // 扬声器区域
- QLabel *spkTitle = new QLabel(tr("扬声器"), audioContent);
- spkTitle->setStyleSheet("font-weight:600; margin-top:4px;");
- m_speakerWidget = new QComboBox(audioContent);
- m_speakerWidget->setEditable(false);
- audioLayout->addWidget(spkTitle);
- audioLayout->addWidget(m_speakerWidget);
- // 新增:编码器区域
- QLabel *encTitle = new QLabel(tr("视频编码器"), audioContent);
- encTitle->setStyleSheet("font-weight:600; margin-top:4px;");
- m_encoderWidget = new QComboBox(audioContent);
- m_encoderWidget->setEditable(false);
- audioLayout->addWidget(encTitle);
- audioLayout->addWidget(m_encoderWidget);
- // 新增:录制控制区域
- QLabel *controlTitle = new QLabel(tr("录制控制"), audioContent);
- controlTitle->setStyleSheet("font-weight:600; margin-top:8px;");
- audioLayout->addWidget(controlTitle);
- // 选项区域
- m_drawCursorCheckBox = new QCheckBox(tr("绘制鼠标指针"), audioContent);
- m_drawCursorCheckBox->setChecked(true);
- m_syncRecordCheckBox = new QCheckBox(tr("推流时同步录制"), audioContent);
- audioLayout->addWidget(m_drawCursorCheckBox);
- audioLayout->addWidget(m_syncRecordCheckBox);
- // 按钮区域
- QHBoxLayout *buttonLayout = new QHBoxLayout();
- m_recordButton = new QPushButton(tr("开始录制"), audioContent);
- m_settingsButton = new QPushButton(tr("设置"), audioContent);
-
- buttonLayout->addWidget(m_recordButton);
- buttonLayout->addWidget(m_settingsButton);
- audioLayout->addLayout(buttonLayout);
- // 创建独立的推流按钮
- m_streamButton = new FunctionButton(IconUtils::createStreamIcon(), "推流", this);
- audioDevicePopover->setContentWidget(audioContent);
- // 将按钮与 Popover 关联
- buttonGroup->addButton(audioDeviceBtn, audioDevicePopover);
- // 添加独立的推流按钮到buttonGroup
- buttonGroup->addButton(m_streamButton);
- // 设备枚举与填充逻辑
- auto populateAudioDevices = [this]() {
- // 清空缓存
- m_micDevices.clear();
- m_speakerDevices.clear();
- // 获取麦克风列表
- AMRECORDER_DEVICE *micArray = nullptr;
- int micCount = recorder_get_mics(&micArray);
- QStringList micNames;
- int defaultMicIndex = -1;
- for (int i = 0; i < micCount; ++i) {
- m_micDevices.push_back(micArray[i]);
- micNames << QString::fromUtf8(micArray[i].name);
- if (micArray[i].is_default && defaultMicIndex < 0) defaultMicIndex = i;
- }
- if (micArray) recorder_free_array(micArray);
- // 获取扬声器列表
- AMRECORDER_DEVICE *spkArray = nullptr;
- int spkCount = recorder_get_speakers(&spkArray);
- QStringList spkNames;
- int defaultSpkIndex = -1;
- for (int i = 0; i < spkCount; ++i) {
- m_speakerDevices.push_back(spkArray[i]);
- spkNames << QString::fromUtf8(spkArray[i].name);
- if (spkArray[i].is_default && defaultSpkIndex < 0) defaultSpkIndex = i;
- }
- if (spkArray) recorder_free_array(spkArray);
- // 更新 UI 列表
- if (m_micWidget) {
- m_micWidget->clear();
- m_micWidget->addItems(micNames);
- }
- if (m_speakerWidget) {
- m_speakerWidget->clear();
- m_speakerWidget->addItems(spkNames);
- }
- // 恢复或设置默认选择
- if (m_micWidget) {
- if (defaultMicIndex >= 0 && defaultMicIndex < m_micDevices.size()) {
- m_micWidget->setCurrentText(QString::fromUtf8(m_micDevices[defaultMicIndex].name));
- } else if (!micNames.isEmpty()) {
- m_micWidget->setCurrentIndex(0);
- }
- }
- if (m_speakerWidget) {
- if (defaultSpkIndex >= 0 && defaultSpkIndex < m_speakerDevices.size()) {
- m_speakerWidget->setCurrentText(
- QString::fromUtf8(m_speakerDevices[defaultSpkIndex].name));
- } else if (!spkNames.isEmpty()) {
- m_speakerWidget->setCurrentIndex(0);
- }
- }
- // 将当前选择传递给 RecorderWidget(若存在)
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- // 选中麦克风
- if (m_micWidget) {
- const QString sel = m_micWidget->currentText();
- for (const auto &d : m_micDevices) {
- if (sel == QString::fromUtf8(d.name)) { rec->setMicDevice(d); break; }
- }
- }
- // 选中扬声器
- if (m_speakerWidget) {
- const QString sel = m_speakerWidget->currentText();
- for (const auto &d : m_speakerDevices) {
- if (sel == QString::fromUtf8(d.name)) { rec->setSpeakerDevice(d); break; }
- }
- }
- }
- };
- // 新增:编码器枚举与填充逻辑
- auto populateEncoders = [this]() {
- m_encoderList.clear();
- m_selectedEncoderId = -1;
- AMRECORDER_ENCODERS *encArray = nullptr;
- int encCount = recorder_get_vencoders(&encArray);
- QStringList encNames;
- for (int i = 0; i < encCount; ++i) {
- m_encoderList.push_back(encArray[i]);
- encNames << QString::fromUtf8(encArray[i].name);
- }
- if (encArray) recorder_free_array(encArray);
- if (m_encoderWidget) {
- m_encoderWidget->clear();
- m_encoderWidget->addItems(encNames);
- if (!m_encoderList.isEmpty()) {
- m_encoderWidget->setCurrentIndex(0);
- m_selectedEncoderId = m_encoderList[0].id;
- }
- }
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- if (m_selectedEncoderId >= 0) rec->setVideoEncoderId(m_selectedEncoderId);
- }
- };
- // 首次填充设备和编码器
- populateAudioDevices();
- populateEncoders();
- // 连接设备选择变化 -> 传给 RecorderWidget
- connect(m_micWidget, &QComboBox::currentTextChanged, this, [this](const QString &deviceName) {
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- for (const auto &d : m_micDevices) {
- if (deviceName == QString::fromUtf8(d.name)) {
- rec->setMicDevice(d);
- break;
- }
- }
- }
- });
- connect(m_speakerWidget, &QComboBox::currentTextChanged, this, [this](const QString &deviceName) {
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- for (const auto &d : m_speakerDevices) {
- if (deviceName == QString::fromUtf8(d.name)) {
- rec->setSpeakerDevice(d);
- break;
- }
- }
- }
- });
- // 新增:编码器选择变化 -> 传给 RecorderWidget
- connect(m_encoderWidget, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
- if (index >= 0 && index < m_encoderList.size()) {
- m_selectedEncoderId = m_encoderList[index].id;
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- rec->setVideoEncoderId(m_selectedEncoderId);
- }
- }
- });
- // 新增:录制控制按钮信号连接
- connect(m_recordButton, &QPushButton::clicked, this, &MainPanel::onRecordButtonClicked);
- connect(m_streamButton, &QPushButton::clicked, this, &MainPanel::onStreamButtonClicked);
- connect(m_settingsButton, &QPushButton::clicked, this, &MainPanel::onSettingsButtonClicked);
- // 维持原有的AudioDeviceSelectorIcon兼容代码(保持注释)
- // m_audioDeviceSelector = new AudioDeviceSelectorIcon(this);
- // connect(m_audioDeviceSelector, &AudioDeviceSelectorIcon::microphoneDeviceSelected,
- // this, [this](const AudioDeviceInfo& device) {
- // qDebug() << "[MainPanel] 麦克风设备已选择(兼容模式):" << device.name;
- // // 通知AvRecorder进行设备切换
- // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
- // //avRecorder->switchMicrophoneDevice(device.id, device.name);
- // }
- // });
- // connect(m_audioDeviceSelector, &AudioDeviceSelectorIcon::speakerDeviceSelected,
- // this, [this](const AudioDeviceInfo& device) {
- // qDebug() << "[MainPanel] 扬声器设备已选择:" << device.name;
- // // 通知AvRecorder进行设备切换
- // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(playerWidget)) {
- // // avRecorder->switchSpeakerDevice(device.id, device.name);
- // }
- // });
- // 添加一个动作按钮到按钮组(没有Popover)
- FunctionButton *actionButton = new FunctionButton(IconUtils::createSettingsIcon(),
- "执行操作",
- this);
- buttonGroup->addButton(actionButton, nullptr);
-
- // 将buttonGroup添加到playerContainer的布局中
- playerLayout = qobject_cast<QVBoxLayout*>(playerContainer->layout());
- if (!playerLayout) {
- playerLayout = new QVBoxLayout(playerContainer);
- playerLayout->setContentsMargins(0, 0, 0, 0);
- }
- playerLayout->addStretch(1); // 添加弹性空间,将buttonGroup推到底部
- playerLayout->addWidget(buttonGroup, 0); // 添加buttonGroup到底部,不拉伸
- buttonGroup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 使用固定大小策略
- // initConnect
- connect(AppEvent::instance(), &AppEvent::connectionStateChanged, this, [this](bool connected) {
- if (userProfile) {
- userProfile->setStatus(connected ? "在线" : "离线");
- }
- });
- connect(userProfile, &UserProfileWidget::logoutClicked, this, &MainPanel::logoutClicked);
- connect(webSocketClient, &WebSocketClient::statsUpdate, statsWidget, &StatsWidget::updateStats);
- connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
- // 这里可以处理 liveStatus 相关逻辑
- QJsonParseError err;
- QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &err);
- if (err.error != QJsonParseError::NoError || !doc.isObject()) {
- qDebug() << "[MainPanel] liveStatus: 解析失败" << err.errorString();
- return;
- }
- QJsonObject obj = doc.object();
- int liveStatus = obj.value("liveStatus").toInt(0); // 默认-1
- if (liveStatus == 1) {
- qDebug() << "[MainPanel] liveStatus: 直播中" << chatView;
- if (chatView) {
- const QString id = webSocketClient->roomId();
- // 使用防抖机制处理频繁的请求
- m_pendingRoomId = id;
- m_debounceTimer->start(); // 重新开始计时,如果在500ms内再次收到请求,会重置定时器
- }
- // 你的处理逻辑
- } else if (liveStatus == 2) {
- qDebug() << "[MainPanel] liveStatus: 未开播";
- // 你的处理逻辑
- } else {
- qDebug() << "[MainPanel] liveStatus: 未知状态" << liveStatus;
- }
- });
- }
- MainPanel::~MainPanel()
- {
- if (userProfile) {
- delete userProfile;
- userProfile = nullptr;
- }
- if (m_recorderStandalone) {
- m_recorderStandalone->deleteLater();
- m_recorderStandalone = nullptr;
- }
- if (m_avPlayerStandalone) {
- m_avPlayerStandalone->deleteLater();
- m_avPlayerStandalone = nullptr;
- }
- }
- void MainPanel::setRole(const QStringList &roleList)
- {
- QWidget *newPlayer = nullptr;
- if (roleList.contains("role.admin")) {
- newPlayer = new RecorderWidget(this);
- } else {
- newPlayer = new AVPlayerWidget(this);
- }
- setPlayerWidget(newPlayer);
- // 设置初始化信息
- const QString &name = AppEvent::instance()->userName();
- userProfile->setUsername(name);
- }
- void MainPanel::setPushRoomId(const QString &id)
- {
- // 推流配置
- if (RecorderWidget *recorderWidget = qobject_cast<RecorderWidget *>(playerWidget)) {
- RecorderWidget::Settings settings;
- settings.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
- settings.liveName = id.toStdString();
- recorderWidget->setSettings(settings);
- }
- // 重新进入房间
- chatView->initWebsocket(id);
- if (AVPlayerWidget *playWidget = qobject_cast<AVPlayerWidget *>(playerWidget)) {
- MaskOverlay::instance()->show(nullptr, 0, MaskOverlay::ActiveWindow);
- QFuture<HttpResponse> getRoomFuture = getRoomApi(id);
- QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomFuture);
- roomListPromise
- .then([this, playWidget, id](const HttpResponse &response) {
- qDebug() << response.code << response.data << response.message;
- if (response.code != 0) {
- BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
- return;
- }
- RoomInfo roomInfo = JsonMapper::formJsonEx<RoomInfo>(response.data.toObject());
- qDebug() << "roomInfo.liveStatus.has_value()" << roomInfo.liveStatus.has_value();
- int status = roomInfo.liveStatus.value_or(0);
- if (status == 1) {
- qDebug() << "open" << ("rtmp://106.55.186.74:1935/stream/V1/" + id);
- playWidget->play("rtmp://106.55.186.74:1935/stream/V1/" + id);
- }
- })
- .finally([]() { MaskOverlay::instance()->hide(); });
- }
- }
- void MainPanel::setPlayerWidget(QWidget *newPlayer)
- {
- if (playerWidget) {
- playerWidget->setParent(nullptr);
- playerWidget->deleteLater();
- }
- playerWidget = newPlayer;
- playerWidget->setParent(playerContainer);
-
- // 获取现有布局并清理(保留buttonGroup)
- QVBoxLayout *vbox = qobject_cast<QVBoxLayout*>(playerContainer->layout());
- if (vbox) {
- // 清理除了buttonGroup之外的所有项目
- QLayoutItem *item;
- while (vbox->count() > 0) {
- item = vbox->takeAt(0);
- if (item->widget() && item->widget() != buttonGroup) {
- item->widget()->setParent(nullptr);
- }
- if (item->spacerItem()) {
- delete item; // 删除spacer
- } else if (item->widget() != buttonGroup) {
- delete item;
- }
- }
- } else {
- // 如果没有布局或布局类型不对,创建新的
- if (playerContainer->layout()) {
- delete playerContainer->layout();
- }
- vbox = new QVBoxLayout(playerContainer);
- vbox->setContentsMargins(0, 0, 0, 0);
- }
-
- // 重新添加组件:播放器在上,buttonGroup在下
- vbox->addWidget(playerWidget, 1); // 添加拉伸因子,让播放器组件占据所有可用空间
- vbox->addWidget(buttonGroup, 0); // 添加buttonGroup到底部,不拉伸
- // 确保播放器组件能够正确拉伸
- playerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-
- // 确保buttonGroup固定在底部
- buttonGroup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 使用固定大小策略
- // 如果新的 player 是 RecorderWidget,则同步当前选择的音频设备
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- if (m_micWidget) {
- const QString sel = m_micWidget->currentText();
- for (const auto &d : m_micDevices) {
- if (sel == QString::fromUtf8(d.name)) { rec->setMicDevice(d); break; }
- }
- }
- if (m_speakerWidget) {
- const QString sel = m_speakerWidget->currentText();
- for (const auto &d : m_speakerDevices) {
- if (sel == QString::fromUtf8(d.name)) { rec->setSpeakerDevice(d); break; }
- }
- }
- // 同步编码器
- if (m_selectedEncoderId >= 0) {
- rec->setVideoEncoderId(m_selectedEncoderId);
- } else if (m_encoderWidget && m_encoderWidget->count() > 0 && !m_encoderList.isEmpty()) {
- int idx = m_encoderWidget->currentIndex();
- if (idx >= 0 && idx < m_encoderList.size()) {
- rec->setVideoEncoderId(m_encoderList[idx].id);
- }
- }
- }
- }
- void MainPanel::handleDebouncedPlay()
- {
- // 防抖处理后的播放逻辑
- if (m_pendingRoomId.isEmpty() || !chatView) {
- return;
- }
- if (AVPlayerWidget *playWidget = qobject_cast<AVPlayerWidget *>(playerWidget)) {
- if (!m_isStartingPlay) {
- m_isStartingPlay = true;
- qDebug() << "[MainPanel] 防抖处理后开始播放:" << m_pendingRoomId;
- playWidget->play("rtmp://106.55.186.74:1935/stream/V1/" + m_pendingRoomId);
- m_isStartingPlay = false; // 如果 startToPlay 是同步的
- }
- }
- // 清空待处理的房间ID
- m_pendingRoomId.clear();
- }
- void MainPanel::initAudioDeviceSelectors()
- {
- // 注册音频设备选择器工厂
- // DeviceManager* deviceManager = DeviceManager::instance();
- // AudioDeviceSelectorFactory* audioFactory = new AudioDeviceSelectorFactory(this);
- // deviceManager->registerFactory("audio", audioFactory);
- // // 创建麦克风和扬声器选择器
- // m_microphoneSelector = deviceManager->createSelector("audio", "microphone");
- // m_speakerSelector = deviceManager->createSelector("audio", "speaker");
- // // 设置到解耦版本的UI组件
- // if (m_audioDeviceSelectorDecoupled) {
- // m_audioDeviceSelectorDecoupled->setMicrophoneSelector(m_microphoneSelector);
- // m_audioDeviceSelectorDecoupled->setSpeakerSelector(m_speakerSelector);
- // }
- qDebug() << "[MainPanel] 音频设备选择器初始化完成";
- }
- void MainPanel::showRecorderStandalone()
- {
- if (!m_recorderStandalone) {
- m_recorderStandalone = new RecorderWidget(nullptr);
- m_recorderStandalone->setAttribute(Qt::WA_DeleteOnClose, false);
- connect(m_recorderStandalone, &QObject::destroyed, this, [this]() {
- m_recorderStandalone = nullptr;
- });
- m_recorderStandalone->setWindowTitle(tr("RecorderWidget"));
- }
- m_recorderStandalone->show();
- m_recorderStandalone->raise();
- m_recorderStandalone->activateWindow();
- }
- void MainPanel::showPlayerStandalone()
- {
- if (!m_avPlayerStandalone) {
- m_avPlayerStandalone = new AVPlayerWidget(nullptr);
- m_avPlayerStandalone->setAttribute(Qt::WA_DeleteOnClose, false);
- connect(m_avPlayerStandalone, &QObject::destroyed, this, [this]() {
- m_avPlayerStandalone = nullptr;
- });
- m_avPlayerStandalone->setWindowTitle(tr("AVPlayerWidget"));
- }
- m_avPlayerStandalone->show();
- m_avPlayerStandalone->raise();
- m_avPlayerStandalone->activateWindow();
- }
- void MainPanel::showChatStandalone()
- {
- if (!chatView) {
- return; // 如果聊天窗口不存在,直接返回
- }
-
- // 从容器中移除聊天窗口(如果在容器中)
- if (m_chatContainer && chatView->parent() == m_chatContainer) {
- chatView->setParent(nullptr);
- }
-
- // 设置为独立窗口
- chatView->setWindowFlags(Qt::Window);
- chatView->setAttribute(Qt::WA_DeleteOnClose, false);
- chatView->setMinimumWidth(400);
- chatView->setWindowTitle(tr("ChatWindow"));
- chatView->show();
- chatView->raise();
- chatView->activateWindow();
- }
- void MainPanel::showChatEmbedded()
- {
- if (!chatView) {
- return; // 如果聊天窗口不存在,直接返回
- }
-
- // 如果当前是独立窗口模式,先隐藏
- if (chatView->isWindow()) {
- chatView->hide();
- }
-
- // 重置窗口标志为普通widget
- chatView->setWindowFlags(Qt::Widget);
-
- // 将聊天窗口重新添加到容器中
- if (m_chatContainer) {
- chatView->setParent(m_chatContainer);
- // 如果容器有布局,将聊天窗口添加到布局中
- if (m_chatContainer->layout()) {
- m_chatContainer->layout()->addWidget(chatView);
- }
- chatView->show();
- chatView->raise();
- }
- }
- // 新增:录制控制按钮的槽函数实现
- void MainPanel::onRecordButtonClicked()
- {
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- // 同步选项设置到RecorderWidget
- // 注意:这里需要RecorderWidget提供设置选项的接口
-
- // 调用RecorderWidget的录制方法
- rec->onRecordButtonClicked();
-
- // 更新按钮文本
- if (m_recordButton) {
- if (m_recordButton->text() == tr("开始录制")) {
- m_recordButton->setText(tr("停止录制"));
- } else {
- m_recordButton->setText(tr("开始录制"));
- }
- }
- }
- }
- void MainPanel::onStreamButtonClicked()
- {
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- // 同步选项设置到RecorderWidget
- rec->onStreamButtonClicked();
- // 更新按钮文本和状态
- if (m_streamButton) {
- if (!m_isStreaming) {
- // 开始推流
- m_streamButton->setText(tr("停止推流"));
- m_isStreaming = true;
-
- // 隐藏预览
- rec->hidePreview();
-
- // 显示独立聊天窗口
- showChatStandalone();
- } else {
- // 停止推流
- m_streamButton->setText(tr("推流"));
- m_isStreaming = false;
-
- // 恢复预览显示
- rec->showPreview();
- }
- }
- }
- }
- void MainPanel::onSettingsButtonClicked()
- {
- if (RecorderWidget *rec = qobject_cast<RecorderWidget *>(playerWidget)) {
- // 调用RecorderWidget的设置方法
- rec->onSettingsButtonClicked();
- }
- }
- void MainPanel::onChatWindowCloseRequested()
- {
- if (!chatView) {
- return;
- }
-
- if (m_isStreaming) {
- // 推流状态下,只隐藏窗口
- chatView->hide();
- } else {
- // 非推流状态下,切换回嵌入式显示
- showChatEmbedded();
- }
- }
|