#include "av_recorder.h" #include #include #include #include #include #include #include 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::GetUsableEncoders(); m_settingsParam.videoParam.name = encs.empty() ? std::string("libx264") : encs.front(); } { const auto& aencs = Encoder::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_captureSourceWidget = new CaptureSourceWidget(this); // 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(m_captureSourceWidget); 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(); } 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::GetUsableEncoders(); m_settingsParam.videoParam.name = encs.empty() ? std::string("libx264") : encs.front(); } { const auto& aencs = Encoder::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_audioDeviceBtn, &QPushButton::clicked, this, &AvRecorder::showAudioDeviceMenu); // 连接捕获源控件信号 connect(m_captureSourceWidget, &CaptureSourceWidget::captureSourceTextChanged, this, [this](const QString& text) { if (text.isEmpty() || m_isLocked) { return; } m_isLocked = true; if (!(m_isRecord || m_isLive)) { stopPreview(); stopCapture(); startCapture(CaptureMethod::DXGI); startPreview(); } m_isLocked = false; }); connect(m_captureSourceWidget, &CaptureSourceWidget::captureSourceIndexChanged, this, &AvRecorder::onCaptureSourceChanged); connect(m_captureSourceWidget, &CaptureSourceWidget::refreshRequested, this, []() { // 刷新请求处理,可以在这里添加额外的逻辑 qDebug() << "Capture source list refreshed"; }); 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(&m_settingsParam, this); settingsPage->exec(); m_isLocked = true; stopPreview(); stopCapture(); startCapture(CaptureMethod::DXGI); 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::DXGI); startPreview(); m_isLocked = false; timer->stop(); }); timer->start(100); return true; } void AvRecorder::startCapture(CaptureMethod method) { int idx = m_captureSourceWidget->getCurrentIndex(); if (idx < 0) return; int monitorCnt = (int) MonitorFinder::GetList().size(); QString type = (idx < monitorCnt) ? "monitor" : "window"; qintptr ptrHwnd = m_captureSourceWidget->getCurrentData().value(); 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_captureSourceWidget->setEnabled(false); // 禁用采集源切换 m_syncRecordBox->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_captureSourceWidget->setEnabled(true); // 恢复采集源切换 m_syncRecordBox->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::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_captureSourceWidget->getCurrentIndex(); int monitorCnt = (int) MonitorFinder::GetList().size(); bool isMonitor = (idx >= 0 && idx < monitorCnt); updateCaptureMethodBox(isMonitor); // 新增:推流/录制时切换采集源不中断 if (m_isRecord || m_isLive) { CaptureMethod method = CaptureMethod::DXGI; QString methodText = m_captureMethodBox->currentText(); if (methodText == "DXGI") method = CaptureMethod::DXGI; else if (methodText == "GDI") method = CaptureMethod::GDI; else method = CaptureMethod::WGC; if (isMonitor) { m_videoRecorder.SetCaptureSource(idx, method); } else { qintptr ptrHwnd = m_captureSourceWidget->getCurrentData().value(); 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() { // 扬声器设备选择处理 }