#include "av_recorder.h" #include #include #include #include "avrecorder/capturer/video/VideoCaptureManager.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; m_settingsParam.videoParam.name = Encoder::GetUsableEncoders().front(); 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); 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); // 10. 状态栏 initStatusBarUi(); // 让视频区尽量大 // m_glWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); // 11. 总体布局 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; m_settingsParam.videoParam.name = Encoder::GetUsableEncoders().front(); 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; __CheckNo(startStream(fileName, format)); 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); __CheckNo(startStream(fileName, isRtsp ? "rtsp" : "flv")); // 如果勾选了同步录像,则开始录像 if (m_syncRecordBox->isChecked()) { __CheckNo(startSyncRecord()); } m_recordBtn->setEnabled(false); m_liveBtn->setText("停止直播"); } else { // 先停止同步录像 stopSyncRecord(); // 再停止直播 stopStream(); m_recordBtn->setEnabled(true); m_liveBtn->setText("开始直播"); } }); 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_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::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] { auto settingsPage = std::make_unique(&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(); 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() { __CheckNo(m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker}, m_settingsParam.audioParam)); 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()); } void AvRecorder::stopCapture() { m_videoRecorder.Close(); m_audioRecorder.Close(); } void AvRecorder::renderFrame() { auto frame = m_videoRecorder.GetRenderFrame(); 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; } 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 = !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; 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); 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(); m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method); } } }