| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- #include "av_recorder.h"
- #include <QAction>
- #include <QDateTime>
- #include <QMenu>
- #include <QMessageBox>
- #include <QStatusBar>
- #include <QTimer>
- #include <capturer/finder.h>
- using namespace avrecorder::video;
- AvRecorder::AvRecorder(QWidget* parent)
- : QWidget(parent)
- {
- m_settingsParam.audioParam.bitRate = 160'000;
- m_settingsParam.videoParam.bitRate = 8'000'000;
- m_settingsParam.videoParam.fps = 30;
- {
- const auto& encs = Encoder<MediaType::VIDEO>::GetUsableEncoders();
- m_settingsParam.videoParam.name = encs.empty() ? std::string("libx264") : encs.front();
- }
- {
- const auto& aencs = Encoder<MediaType::AUDIO>::GetUsableEncoders();
- m_settingsParam.audioParam.name = aencs.empty() ? std::string("aac") : aencs.front();
- }
- // 设置安全的默认分辨率,避免在捕获首帧前width/height为0
- m_settingsParam.videoParam.width = 1920;
- m_settingsParam.videoParam.height = 1080;
- m_settingsParam.outputDir = ".";
- m_settingsParam.liveUrl = "rtmp://127.0.0.1:1935/stream/V1";
- m_settingsParam.liveName = "stream";
- // 1. 视频预览区
- m_glWidget = new OpenGLVideoWidget(this);
- // 2. 音频区
- m_microphoneWidget = new AudioWidget;
- m_speakerWidget = new AudioWidget;
- m_microphoneWidget->SetName("麦克风");
- m_speakerWidget->SetName("扬声器");
- // 3. 捕获区
- m_captureComboBox = new QComboBox;
- m_updateListBtn = new QPushButton("刷新窗口列表");
- QGroupBox* captureGroup = new QGroupBox("捕获源");
- QHBoxLayout* captureRow = new QHBoxLayout;
- captureRow->addWidget(m_captureComboBox);
- captureRow->addWidget(m_updateListBtn);
- captureGroup->setLayout(captureRow);
- // m_captureComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
- m_captureComboBox->setMinimumWidth(20);
- // 4. 音频区分组
- QGroupBox* audioGroup = new QGroupBox("音频");
- QVBoxLayout* audioLayout = new QVBoxLayout;
- audioLayout->addWidget(m_microphoneWidget);
- audioLayout->addWidget(m_speakerWidget);
-
- // 添加音频设备选择按钮
- m_audioDeviceBtn = new QPushButton("选择音频设备");
- m_audioDeviceBtn->setToolTip("打开音频设备选择窗口");
- audioLayout->addWidget(m_audioDeviceBtn);
-
- audioGroup->setLayout(audioLayout);
- //audioGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
- // 5. 操作区
- m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
- m_isDrawCursorBox->setChecked(true);
- m_isDrawCursorBox->setEnabled(false);
- m_syncRecordBox = new QCheckBox("直播时同步录像");
- m_syncRecordBox->setChecked(false);
- m_recordBtn = new QPushButton("开始录制");
- m_recordBtn->setEnabled(false);
- m_liveBtn = new QPushButton("开始直播");
- m_liveBtn->setEnabled(false);
- m_settingsBtn = new QPushButton("设置");
- QGroupBox* actionGroup = new QGroupBox("操作");
- QVBoxLayout* actionLayout = new QVBoxLayout;
- QHBoxLayout* checkBoxRow = new QHBoxLayout;
- checkBoxRow->setContentsMargins(0, 0, 0, 0);
- checkBoxRow->setSpacing(8);
- checkBoxRow->addWidget(m_syncRecordBox);
- checkBoxRow->addWidget(m_isDrawCursorBox);
- actionLayout->addLayout(checkBoxRow);
- actionLayout->addWidget(m_recordBtn);
- actionLayout->addWidget(m_liveBtn);
- actionGroup->setLayout(actionLayout);
- //actionGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
- // 6. 设置区
- QHBoxLayout* utilLayout = new QHBoxLayout;
- utilLayout->addWidget(m_settingsBtn);
- // 7. 左侧功能区(捕获区在上,音频区在下)
- QVBoxLayout* leftLayout = new QVBoxLayout;
- leftLayout->addWidget(captureGroup);
- leftLayout->addWidget(audioGroup);
- leftLayout->addStretch();
- // 8. 右侧功能区
- QVBoxLayout* rightLayout = new QVBoxLayout;
- rightLayout->addWidget(actionGroup);
- rightLayout->addLayout(utilLayout);
- rightLayout->addStretch();
- // 9. 中部主布局
- QHBoxLayout* centerLayout = new QHBoxLayout;
- centerLayout->addLayout(leftLayout, 2);
- centerLayout->addLayout(rightLayout, 3);
- // 11. 状态栏
- initStatusBarUi();
- // 让视频区尽量大
- // m_glWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
- // 12. 总体布局
- QVBoxLayout* mainLayout = new QVBoxLayout(this);
- mainLayout->addWidget(m_glWidget, 100);
- mainLayout->addLayout(centerLayout, 0);
- mainLayout->addWidget(m_statusBar, 0);
- setLayout(mainLayout);
- // 12. 连接信号槽、初始化数据
- initConnect();
- updateCaptureList();
- }
- void AvRecorder::setSettings(const SettingsPage::Param& param)
- {
- m_settingsParam.audioParam.bitRate = 160'000;
- m_settingsParam.videoParam.bitRate = 8'000'000;
- m_settingsParam.videoParam.fps = 30;
- {
- const auto& encs = Encoder<MediaType::VIDEO>::GetUsableEncoders();
- m_settingsParam.videoParam.name = encs.empty() ? std::string("libx264") : encs.front();
- }
- {
- const auto& aencs = Encoder<MediaType::AUDIO>::GetUsableEncoders();
- m_settingsParam.audioParam.name = aencs.empty() ? std::string("aac") : aencs.front();
- }
- // 重置时也保持安全默认分辨率,实际分辨率会在捕获后更新
- m_settingsParam.videoParam.width = 1920;
- m_settingsParam.videoParam.height = 1080;
- m_settingsParam.outputDir = ".";
- m_settingsParam.liveUrl = param.liveUrl; // "rtmp://192.168.3.76:1935/stream/V1";
- m_settingsParam.liveName = param.liveName; // "stream";
- }
- void AvRecorder::initConnect()
- {
- connect(m_recordBtn, &QPushButton::released, this, [this] {
- if (!m_isRecord) {
- auto fileName = m_settingsParam.outputDir;
- if (fileName.back() != '\\') {
- fileName.push_back('\\');
- }
- auto format = "mp4";
- fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
- + "." + format;
- // fileName += std::string("test.") + format;
- if (!startStream(fileName, format)) {
- qDebug() << "startStream failed for recording";
- return;
- }
- m_liveBtn->setEnabled(false);
- m_recordBtn->setText("停止录制");
- } else {
- stopStream();
- m_liveBtn->setEnabled(true);
- m_recordBtn->setText("开始录制");
- }
- m_isRecord = !m_isRecord;
- });
- connect(m_liveBtn, &QPushButton::released, this, [this] {
- if (!m_isLive) {
- auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
- bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
- qDebug() << "直播地址:" << QString::fromStdString(fileName);
- if (!startStream(fileName, isRtsp ? "rtsp" : "flv")) {
- qDebug() << "startStream failed for live";
- return;
- }
- // 如果勾选了同步录像,则开始录像
- if (m_syncRecordBox->isChecked()) {
- if (!startSyncRecord()) {
- qDebug() << "startSyncRecord failed";
- // 不阻断直播,仅提示
- }
- }
- m_recordBtn->setEnabled(false);
- m_liveBtn->setText("停止直播");
- // 显式设置直播状态
- m_isLive = true;
- } else {
- // 先停止同步录像
- stopSyncRecord();
- // 再停止直播
- stopStream();
- m_recordBtn->setEnabled(true);
- m_liveBtn->setText("开始直播");
- // 显式清除直播状态
- m_isLive = false;
- }
- });
- connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
- m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
- });
- connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
- m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
- });
- connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
-
- // 连接音频设备选择按钮
- connect(m_audioDeviceBtn, &QPushButton::clicked, this, &AvRecorder::showAudioDeviceMenu);
-
- connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
- if (text.isEmpty() || m_isLocked) {
- return;
- }
- m_isLocked = true;
- if (!(m_isRecord || m_isLive)) {
- stopPreview();
- stopCapture();
- startCapture(CaptureMethod::WGC);
- startPreview();
- }
- m_isLocked = false;
- });
- connect(m_captureComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AvRecorder::onCaptureSourceChanged);
- connect(m_isDrawCursorBox, &QCheckBox::stateChanged, this, [this] {
- m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
- });
- connect(m_captureMethodBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
- if (m_isLocked || text.isEmpty()) {
- return;
- }
- stopPreview();
- stopCapture();
- if (text == "WGC") {
- startCapture(CaptureMethod::WGC);
- } else if (text == "DXGI") {
- startCapture(CaptureMethod::DXGI);
- } else {
- startCapture(CaptureMethod::GDI);
- }
- startPreview();
- });
- connect(m_settingsBtn, &QPushButton::released, this, [this] {
- // 运行中禁止打开设置以切换编码器,避免异常
- if (m_isRecord || m_isLive || m_isSyncRecord) {
- QMessageBox::warning(this, "提示", "正在直播/录制,无法修改编码器设置。请先停止后再修改。");
- return;
- }
- auto settingsPage = std::make_unique<SettingsPage>(&m_settingsParam, this);
- settingsPage->exec();
- m_isLocked = true;
- stopPreview();
- stopCapture();
- startCapture(CaptureMethod::WGC);
- startPreview();
- m_isLocked = false;
- });
- m_otherTimer.callOnTimeout([this] {
- if (windowState() == Qt::WindowMinimized) {
- return;
- }
- // 音频
- auto info = m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX);
- m_microphoneWidget->ShowVolume(info == nullptr ? 0 : info->volume);
- info = m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX);
- m_speakerWidget->ShowVolume(info == nullptr ? 0 : info->volume);
- // 状态栏
- if (m_isRecord || m_isLive) {
- int interval = m_recordTime.secsTo(QTime::currentTime());
- int sec = interval % 60;
- interval /= 60;
- int minute = interval % 60;
- int hour = interval / 60;
- m_captureTimeLabel->setText(QString("%1:%2:%3")
- .arg(hour, 2, 10, QChar('0'))
- .arg(minute, 2, 10, QChar('0'))
- .arg(sec, 2, 10, QChar('0')));
- auto lossRate = m_videoRecorder.GetLossRate();
- if (lossRate < 0) {
- m_videolossRate->setText("丢帧率: 统计中");
- } else {
- int num = lossRate * 10000;
- m_videolossRate->setText(QString("丢帧率: %1.%2%")
- .arg(num / 100, 2, 10, QChar('0'))
- .arg(num % 100, 2, 10, QChar('0')));
- }
- } else if (m_captureTimeLabel->text() != "00:00:00") {
- m_captureTimeLabel->setText("00:00:00");
- }
- });
- }
- AvRecorder::~AvRecorder()
- {
- stopSyncRecord();
- stopStream();
- stopPreview();
- stopCapture();
- }
- bool AvRecorder::start()
- {
- auto timer = new QTimer(this);
- connect(timer, &QTimer::timeout, this, [this, timer] {
- m_isLocked = true;
- stopPreview();
- stopCapture();
- startCapture(CaptureMethod::WGC);
- startPreview();
- m_isLocked = false;
- timer->stop();
- });
- timer->start(100);
- return true;
- }
- void AvRecorder::startCapture(CaptureMethod method)
- {
- int idx = m_captureComboBox->currentIndex();
- if (idx < 0) return;
- int monitorCnt = (int)MonitorFinder::GetList().size();
- QString type = (idx < monitorCnt) ? "monitor" : "window";
- qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
- bool ok = false;
- if (m_isRecord || m_isLive) {
- // 推流/录制时,安全切换采集源
- if (idx < monitorCnt) {
- m_videoRecorder.SetCaptureSource(idx, method);
- ok = true;
- } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
- m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
- ok = true;
- }
- } else {
- // 未推流/录制时,正常 open
- if (idx < monitorCnt) { // 捕获屏幕
- ok = m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
- } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
- ok = m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
- }
- }
- if (!ok) {
- // 可选:弹窗或日志提示
- return;
- }
- dealCapture();
- m_isDrawCursorBox->setEnabled(true);
- m_recordBtn->setEnabled(true);
- m_liveBtn->setEnabled(true);
- m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
- m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
- m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
- }
- void AvRecorder::dealCapture()
- {
- if (!m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
- m_settingsParam.audioParam)) {
- qDebug() << "AudioRecorder::Open failed";
- return;
- }
- m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
- m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
- m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
- m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
- if (m_audioEncodeLabel) {
- m_audioEncodeLabel->setText(("音频编码器: " + m_settingsParam.audioParam.name).c_str());
- }
- }
- void AvRecorder::stopCapture()
- {
- m_videoRecorder.Close();
- m_audioRecorder.Close();
- }
- void AvRecorder::renderFrame()
- {
- auto frame = m_videoRecorder.GetRenderFrame();
- if (!frame) {
- qDebug() << "renderFrame error";
- return;
- }
- AVFrame* copiedFrame = av_frame_clone(frame);
- // m_glWidget->Render(copiedFrame);
- QMetaObject::invokeMethod(m_glWidget,
- "Render",
- Qt::QueuedConnection,
- Q_ARG(AVFrame*, copiedFrame));
- }
- void AvRecorder::startPreview()
- {
- m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
- // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
- // 需要自己封装一个计时器
- // m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
- // if (windowState() == Qt::WindowMinimized) {
- // return;
- // }
- // QMetaObject::invokeMethod(this, "renderFrame", Qt::QueuedConnection);
- // // // 视频
- // // auto frame = m_videoRecorder.GetRenderFrame();
- // // m_glWidget->Render(frame);
- // });
- if (!m_videoRenderTimer) {
- m_videoRenderTimer = new QTimer(this);
- connect(m_videoRenderTimer, &QTimer::timeout, this, [this] {
- if (windowState() == Qt::WindowMinimized)
- return;
- renderFrame();
- });
- }
- m_videoRenderTimer->start(1000 / m_settingsParam.videoParam.fps);
- // 刷新率设置为 25
- m_otherTimer.start(40);
- }
- void AvRecorder::stopPreview()
- {
- if (m_videoRenderTimer) {
- m_videoRenderTimer->stop();
- }
- m_otherTimer.stop();
- }
- bool AvRecorder::startStream(std::string_view path, std::string_view format)
- {
- if (!m_avMuxer.Open(path, format)) {
- qDebug() << "Failed to open muxer with path:" << QString::fromStdString(std::string(path)) << "format:" << QString::fromStdString(std::string(format));
- return false;
- }
- if (!m_audioRecorder.LoadMuxer(m_avMuxer)) {
- qDebug() << "Failed to load muxer for audio recorder";
- return false;
- }
- if (!m_videoRecorder.LoadMuxer(m_avMuxer)) {
- qDebug() << "Failed to load muxer for video recorder";
- return false;
- }
- if (!m_avMuxer.WriteHeader()) {
- qDebug() << "Failed to write muxer header";
- return false;
- }
- if (!m_audioRecorder.StartRecord()) {
- qDebug() << "Failed to start audio recording";
- return false;
- }
- if (!m_videoRecorder.StartRecord()) {
- qDebug() << "Failed to start video recording";
- return false;
- }
- // 同步:展示实际使用的编码器名称(考虑自动回退后的结果)
- {
- std::string used = m_videoRecorder.GetEncoderNameForMuxer(m_avMuxer);
- if (!used.empty()) {
- m_videoEncodeLabel->setText((std::string("编码器: ") + used).c_str());
- }
- std::string aused = m_audioRecorder.GetEncoderNameForMuxer(m_avMuxer);
- if (!aused.empty()) {
- m_audioEncodeLabel->setText((std::string("音频编码器: ") + aused).c_str());
- }
- }
- m_recordTime = QTime::currentTime();
- m_captureStatusLabel->setText("状态: 正在工作");
- m_settingsBtn->setEnabled(false);
- m_captureComboBox->setEnabled(false); // 禁用采集源切换
- m_syncRecordBox->setEnabled(false);
- m_updateListBtn->setEnabled(false);
- m_captureMethodBox->setEnabled(false); // 禁用采集方式切换
- // 注意:不要在这里修改 m_isLive,由调用方(按钮逻辑)根据场景设置
- return true;
- }
- void AvRecorder::stopStream()
- {
- m_audioRecorder.StopRecord();
- m_videoRecorder.StopRecord();
-
- // 从录制器中卸载直播muxer
- m_audioRecorder.UnloadMuxer(m_avMuxer);
- m_videoRecorder.UnloadMuxer(m_avMuxer);
-
- m_avMuxer.Close();
- // 如果有同步录像,也需要关闭
- if (m_isSyncRecord) {
- // 先从录制器中卸载同步录像muxer
- m_audioRecorder.UnloadMuxer(m_recordMuxer);
- m_videoRecorder.UnloadMuxer(m_recordMuxer);
-
- m_recordMuxer.Close();
- m_isSyncRecord = false;
- }
- m_captureStatusLabel->setText("状态: 正常");
- m_settingsBtn->setEnabled(true);
- m_captureComboBox->setEnabled(true); // 恢复采集源切换
- m_syncRecordBox->setEnabled(true);
- m_updateListBtn->setEnabled(true);
- m_captureMethodBox->setEnabled(true); // 恢复采集方式切换
- }
- bool AvRecorder::startSyncRecord()
- {
- // 检查是否已经在同步录像
- if (m_isSyncRecord) {
- qDebug() << "Sync recording is already active";
- return true;
- }
-
- // 检查是否正在直播(必须在直播状态下才能启动同步录像)
- if (!m_isLive) {
- qDebug() << "Cannot start sync recording: not in live streaming mode";
- return false;
- }
-
- auto fileName = m_settingsParam.outputDir;
- if (fileName.back() != '\\') {
- fileName.push_back('\\');
- }
- auto format = "mp4";
- fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
- + "_sync." + format;
- // 打开同步录像的muxer
- if (!m_recordMuxer.Open(fileName, format)) {
- qDebug() << "Failed to open sync record muxer";
- return false;
- }
-
- // 加载muxer到录制器
- if (!m_audioRecorder.LoadMuxer(m_recordMuxer)) {
- qDebug() << "Failed to load sync muxer for audio recorder";
- m_recordMuxer.Close();
- return false;
- }
-
- if (!m_videoRecorder.LoadMuxer(m_recordMuxer)) {
- qDebug() << "Failed to load sync muxer for video recorder";
- m_audioRecorder.UnloadMuxer(m_recordMuxer);
- m_recordMuxer.Close();
- return false;
- }
-
- // 写入头部
- if (!m_recordMuxer.WriteHeader()) {
- qDebug() << "Failed to write sync muxer header";
- m_audioRecorder.UnloadMuxer(m_recordMuxer);
- m_videoRecorder.UnloadMuxer(m_recordMuxer);
- m_recordMuxer.Close();
- return false;
- }
-
- m_isSyncRecord = true;
- qDebug() << "Sync recording started successfully: " << QString::fromStdString(fileName);
- return true;
- }
- void AvRecorder::stopSyncRecord()
- {
- if (m_isSyncRecord) {
- // 先从录制器中卸载muxer
- m_audioRecorder.UnloadMuxer(m_recordMuxer);
- m_videoRecorder.UnloadMuxer(m_recordMuxer);
-
- // 然后关闭muxer
- m_recordMuxer.Close();
- m_isSyncRecord = false;
- }
- }
- void AvRecorder::updateCaptureList()
- {
- m_captureComboBox->clear();
- auto&& monitorList = MonitorFinder::GetList(true);
- for (auto&& monitor : monitorList) {
- QString text = "屏幕: " + QString::fromStdWString(monitor.title);
- m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
- }
- auto&& windowList = WindowFinder::GetList(true);
- for (auto&& window : windowList) {
- QString text = "窗口: " + QString::fromStdWString(window.title);
- m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
- }
- }
- void AvRecorder::initStatusBarUi()
- {
- m_videoEncodeLabel = new QLabel;
- m_audioEncodeLabel = new QLabel;
- auto hLayout = new QHBoxLayout;
- hLayout->setContentsMargins(0, 0, 0, 0);
- hLayout->addWidget(new QLabel("捕获方式:"));
- m_captureMethodBox = new QComboBox;
- hLayout->addWidget(m_captureMethodBox);
- m_captureStatusLabel = new QLabel("状态: 正常");
- m_captureTimeLabel = new QLabel("00:00:00");
- m_videolossRate = new QLabel("丢帧率: 00.00%");
- m_fpsLabel = new QLabel("FPS: 30");
- // 创建状态栏并添加到布局中
- m_statusBar = new QStatusBar(this);
- m_statusBar->setSizeGripEnabled(false);
- // 添加各个状态信息到状态栏
- m_statusBar->addWidget(m_videoEncodeLabel);
- m_statusBar->addWidget(m_audioEncodeLabel);
- auto widget = new QWidget;
- widget->setLayout(hLayout);
- m_statusBar->addWidget(widget);
- m_statusBar->addWidget(m_videolossRate);
- m_statusBar->addWidget(m_captureStatusLabel);
- m_statusBar->addWidget(m_captureTimeLabel);
- m_statusBar->addWidget(m_fpsLabel);
- }
- void AvRecorder::updateCaptureMethodBox(bool isMonitor) {
- m_captureMethodBox->clear();
- m_captureMethodBox->addItem("WGC");
- if (isMonitor) {
- m_captureMethodBox->addItem("DXGI");
- } else {
- m_captureMethodBox->addItem("GDI");
- }
- }
- // 捕获源切换时调用
- void AvRecorder::onCaptureSourceChanged() {
- int idx = m_captureComboBox->currentIndex();
- int monitorCnt = (int)MonitorFinder::GetList().size();
- bool isMonitor = (idx >= 0 && idx < monitorCnt);
- updateCaptureMethodBox(isMonitor);
- // 新增:推流/录制时切换采集源不中断
- if (m_isRecord || m_isLive) {
- CaptureMethod method = CaptureMethod::WGC;
- QString methodText = m_captureMethodBox->currentText();
- if (methodText == "DXGI") method = CaptureMethod::DXGI;
- else if (methodText == "GDI") method = CaptureMethod::GDI;
- if (isMonitor) {
- m_videoRecorder.SetCaptureSource(idx, method);
- } else {
- qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
- m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
- }
- }
- }
- void AvRecorder::showAudioDeviceMenu()
- {
- QMenu *menu = new QMenu(this);
- // // 麦克风设备子菜单
- // QMenu *micMenu = menu->addMenu("麦克风设备");
- // const auto inputDevices = QMediaDevices::audioInputs();
- // for (const auto &device : inputDevices) {
- // QAction *action = micMenu->addAction(device.description());
- // action->setData(device.id());
- // action->setCheckable(true);
- // if (device == QMediaDevices::defaultAudioInput()) {
- // action->setChecked(true);
- // action->setText(device.description() + " (默认)");
- // }
- // connect(action, &QAction::triggered, this, [this, device]() {
- // qDebug() << "选择麦克风设备:" << device.description() << "ID:" << device.id();
- // // 这里可以添加实际的设备切换逻辑
- // if (m_microphoneWidget) {
- // // 更新麦克风组件显示
- // }
- // });
- // }
- // menu->addSeparator();
- // // 扬声器设备子菜单
- // QMenu *speakerMenu = menu->addMenu("扬声器设备");
- // const auto outputDevices = QMediaDevices::audioOutputs();
- // for (const auto &device : outputDevices) {
- // QAction *action = speakerMenu->addAction(device.description());
- // action->setData(device.id());
- // action->setCheckable(true);
- // if (device == QMediaDevices::defaultAudioOutput()) {
- // action->setChecked(true);
- // action->setText(device.description() + " (默认)");
- // }
- // connect(action, &QAction::triggered, this, [this, device]() {
- // qDebug() << "选择扬声器设备:" << device.description() << "ID:" << device.id();
- // // 这里可以添加实际的设备切换逻辑
- // if (m_speakerWidget) {
- // // 更新扬声器组件显示
- // }
- // });
- // }
- menu->addSeparator();
-
- // 刷新设备列表
- QAction *refreshAction = menu->addAction("刷新设备列表");
- connect(refreshAction, &QAction::triggered, this, [this]() {
- qDebug() << "刷新音频设备列表";
- // 重新显示菜单以刷新设备列表
- QTimer::singleShot(100, this, &AvRecorder::showAudioDeviceMenu);
- });
-
- // 在按钮下方显示菜单
- QPoint pos = m_audioDeviceBtn->mapToGlobal(QPoint(0, m_audioDeviceBtn->height()));
- menu->exec(pos);
-
- menu->deleteLater();
- }
- void AvRecorder::onMicrophoneDeviceSelected()
- {
- // 麦克风设备选择处理
- }
- void AvRecorder::onSpeakerDeviceSelected()
- {
- // 扬声器设备选择处理
- }
|