av_recorder.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #include "av_recorder.h"
  2. #include <QDateTime>
  3. #include <QStatusBar>
  4. #include <capturer/finder.h>
  5. #include "avrecorder/capturer/video/VideoCaptureManager.h"
  6. using namespace avrecorder::video;
  7. AvRecorder::AvRecorder(QWidget* parent)
  8. : QWidget(parent)
  9. {
  10. m_settingsParam.audioParam.bitRate = 160'000;
  11. m_settingsParam.videoParam.bitRate = 8'000'000;
  12. m_settingsParam.videoParam.fps = 30;
  13. m_settingsParam.videoParam.name = Encoder<MediaType::VIDEO>::GetUsableEncoders().front();
  14. m_settingsParam.outputDir = ".";
  15. m_settingsParam.liveUrl = "rtmp://127.0.0.1:1935/stream/V1";
  16. m_settingsParam.liveName = "stream";
  17. // 1. 视频预览区
  18. m_glWidget = new OpenGLVideoWidget(this);
  19. // 2. 音频区
  20. m_microphoneWidget = new AudioWidget;
  21. m_speakerWidget = new AudioWidget;
  22. m_microphoneWidget->SetName("麦克风");
  23. m_speakerWidget->SetName("扬声器");
  24. // 3. 捕获区
  25. m_captureComboBox = new QComboBox;
  26. m_updateListBtn = new QPushButton("刷新窗口列表");
  27. QGroupBox* captureGroup = new QGroupBox("捕获源");
  28. QHBoxLayout* captureRow = new QHBoxLayout;
  29. captureRow->addWidget(m_captureComboBox);
  30. captureRow->addWidget(m_updateListBtn);
  31. captureGroup->setLayout(captureRow);
  32. // m_captureComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
  33. m_captureComboBox->setMinimumWidth(20);
  34. // 4. 音频区分组
  35. QGroupBox* audioGroup = new QGroupBox("音频");
  36. QVBoxLayout* audioLayout = new QVBoxLayout;
  37. audioLayout->addWidget(m_microphoneWidget);
  38. audioLayout->addWidget(m_speakerWidget);
  39. audioGroup->setLayout(audioLayout);
  40. //audioGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  41. // 5. 操作区
  42. m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
  43. m_isDrawCursorBox->setChecked(true);
  44. m_isDrawCursorBox->setEnabled(false);
  45. m_syncRecordBox = new QCheckBox("直播时同步录像");
  46. m_syncRecordBox->setChecked(false);
  47. m_recordBtn = new QPushButton("开始录制");
  48. m_recordBtn->setEnabled(false);
  49. m_liveBtn = new QPushButton("开始直播");
  50. m_liveBtn->setEnabled(false);
  51. m_settingsBtn = new QPushButton("设置");
  52. QGroupBox* actionGroup = new QGroupBox("操作");
  53. QVBoxLayout* actionLayout = new QVBoxLayout;
  54. QHBoxLayout* checkBoxRow = new QHBoxLayout;
  55. checkBoxRow->setContentsMargins(0, 0, 0, 0);
  56. checkBoxRow->setSpacing(8);
  57. checkBoxRow->addWidget(m_syncRecordBox);
  58. checkBoxRow->addWidget(m_isDrawCursorBox);
  59. actionLayout->addLayout(checkBoxRow);
  60. actionLayout->addWidget(m_recordBtn);
  61. actionLayout->addWidget(m_liveBtn);
  62. actionGroup->setLayout(actionLayout);
  63. //actionGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  64. // 6. 设置区
  65. QHBoxLayout* utilLayout = new QHBoxLayout;
  66. utilLayout->addWidget(m_settingsBtn);
  67. // 7. 左侧功能区(捕获区在上,音频区在下)
  68. QVBoxLayout* leftLayout = new QVBoxLayout;
  69. leftLayout->addWidget(captureGroup);
  70. leftLayout->addWidget(audioGroup);
  71. leftLayout->addStretch();
  72. // 8. 右侧功能区
  73. QVBoxLayout* rightLayout = new QVBoxLayout;
  74. rightLayout->addWidget(actionGroup);
  75. rightLayout->addLayout(utilLayout);
  76. rightLayout->addStretch();
  77. // 9. 中部主布局
  78. QHBoxLayout* centerLayout = new QHBoxLayout;
  79. centerLayout->addLayout(leftLayout, 2);
  80. centerLayout->addLayout(rightLayout, 3);
  81. // 10. 状态栏
  82. initStatusBarUi();
  83. // 让视频区尽量大
  84. // m_glWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  85. // 11. 总体布局
  86. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  87. mainLayout->addWidget(m_glWidget, 100);
  88. mainLayout->addLayout(centerLayout, 0);
  89. mainLayout->addWidget(m_statusBar, 0);
  90. setLayout(mainLayout);
  91. // 12. 连接信号槽、初始化数据
  92. initConnect();
  93. updateCaptureList();
  94. }
  95. void AvRecorder::setSettings(const SettingsPage::Param& param)
  96. {
  97. m_settingsParam.audioParam.bitRate = 160'000;
  98. m_settingsParam.videoParam.bitRate = 8'000'000;
  99. m_settingsParam.videoParam.fps = 30;
  100. m_settingsParam.videoParam.name = Encoder<MediaType::VIDEO>::GetUsableEncoders().front();
  101. m_settingsParam.outputDir = ".";
  102. m_settingsParam.liveUrl = param.liveUrl; // "rtmp://192.168.3.76:1935/stream/V1";
  103. m_settingsParam.liveName = param.liveName; // "stream";
  104. }
  105. void AvRecorder::initConnect()
  106. {
  107. connect(m_recordBtn, &QPushButton::released, this, [this] {
  108. if (!m_isRecord) {
  109. auto fileName = m_settingsParam.outputDir;
  110. if (fileName.back() != '\\') {
  111. fileName.push_back('\\');
  112. }
  113. auto format = "mp4";
  114. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  115. + "." + format;
  116. // fileName += std::string("test.") + format;
  117. __CheckNo(startStream(fileName, format));
  118. m_liveBtn->setEnabled(false);
  119. m_recordBtn->setText("停止录制");
  120. } else {
  121. stopStream();
  122. m_liveBtn->setEnabled(true);
  123. m_recordBtn->setText("开始录制");
  124. }
  125. m_isRecord = !m_isRecord;
  126. });
  127. connect(m_liveBtn, &QPushButton::released, this, [this] {
  128. if (!m_isLive) {
  129. auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
  130. bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
  131. qDebug() << "直播地址:" << QString::fromStdString(fileName);
  132. __CheckNo(startStream(fileName, isRtsp ? "rtsp" : "flv"));
  133. // 如果勾选了同步录像,则开始录像
  134. if (m_syncRecordBox->isChecked()) {
  135. __CheckNo(startSyncRecord());
  136. }
  137. m_recordBtn->setEnabled(false);
  138. m_liveBtn->setText("停止直播");
  139. } else {
  140. // 先停止同步录像
  141. stopSyncRecord();
  142. // 再停止直播
  143. stopStream();
  144. m_recordBtn->setEnabled(true);
  145. m_liveBtn->setText("开始直播");
  146. }
  147. m_isLive = !m_isLive;
  148. });
  149. connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  150. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  151. });
  152. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  153. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  154. });
  155. connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
  156. connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  157. if (text.isEmpty() || m_isLocked) {
  158. return;
  159. }
  160. m_isLocked = true;
  161. if (!(m_isRecord || m_isLive)) {
  162. stopPreview();
  163. stopCapture();
  164. startCapture(CaptureMethod::WGC);
  165. startPreview();
  166. }
  167. m_isLocked = false;
  168. });
  169. connect(m_captureComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AvRecorder::onCaptureSourceChanged);
  170. connect(m_isDrawCursorBox, &QCheckBox::stateChanged, this, [this] {
  171. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  172. });
  173. connect(m_captureMethodBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  174. if (m_isLocked || text.isEmpty()) {
  175. return;
  176. }
  177. stopPreview();
  178. stopCapture();
  179. if (text == "WGC") {
  180. startCapture(CaptureMethod::WGC);
  181. } else if (text == "DXGI") {
  182. startCapture(CaptureMethod::DXGI);
  183. } else {
  184. startCapture(CaptureMethod::GDI);
  185. }
  186. startPreview();
  187. });
  188. connect(m_settingsBtn, &QPushButton::released, this, [this] {
  189. auto settingsPage = std::make_unique<SettingsPage>(&m_settingsParam, this);
  190. settingsPage->exec();
  191. m_isLocked = true;
  192. stopPreview();
  193. stopCapture();
  194. startCapture(CaptureMethod::WGC);
  195. startPreview();
  196. m_isLocked = false;
  197. });
  198. m_otherTimer.callOnTimeout([this] {
  199. if (windowState() == Qt::WindowMinimized) {
  200. return;
  201. }
  202. // 音频
  203. auto info = m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX);
  204. m_microphoneWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  205. info = m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX);
  206. m_speakerWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  207. // 状态栏
  208. if (m_isRecord || m_isLive) {
  209. int interval = m_recordTime.secsTo(QTime::currentTime());
  210. int sec = interval % 60;
  211. interval /= 60;
  212. int minute = interval % 60;
  213. int hour = interval / 60;
  214. m_captureTimeLabel->setText(QString("%1:%2:%3")
  215. .arg(hour, 2, 10, QChar('0'))
  216. .arg(minute, 2, 10, QChar('0'))
  217. .arg(sec, 2, 10, QChar('0')));
  218. auto lossRate = m_videoRecorder.GetLossRate();
  219. if (lossRate < 0) {
  220. m_videolossRate->setText("丢帧率: 统计中");
  221. } else {
  222. int num = lossRate * 10000;
  223. m_videolossRate->setText(QString("丢帧率: %1.%2%")
  224. .arg(num / 100, 2, 10, QChar('0'))
  225. .arg(num % 100, 2, 10, QChar('0')));
  226. }
  227. } else if (m_captureTimeLabel->text() != "00:00:00") {
  228. m_captureTimeLabel->setText("00:00:00");
  229. }
  230. });
  231. }
  232. AvRecorder::~AvRecorder()
  233. {
  234. stopSyncRecord();
  235. stopStream();
  236. stopPreview();
  237. stopCapture();
  238. }
  239. bool AvRecorder::start()
  240. {
  241. auto timer = new QTimer(this);
  242. connect(timer, &QTimer::timeout, this, [this, timer] {
  243. m_isLocked = true;
  244. stopPreview();
  245. stopCapture();
  246. startCapture(CaptureMethod::WGC);
  247. startPreview();
  248. m_isLocked = false;
  249. timer->stop();
  250. });
  251. timer->start(100);
  252. return true;
  253. }
  254. void AvRecorder::startCapture(CaptureMethod method)
  255. {
  256. int idx = m_captureComboBox->currentIndex();
  257. if (idx < 0) return;
  258. int monitorCnt = (int)MonitorFinder::GetList().size();
  259. QString type = (idx < monitorCnt) ? "monitor" : "window";
  260. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  261. bool ok = false;
  262. if (m_isRecord || m_isLive) {
  263. // 推流/录制时,安全切换采集源
  264. if (idx < monitorCnt) {
  265. m_videoRecorder.SetCaptureSource(idx, method);
  266. ok = true;
  267. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  268. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  269. ok = true;
  270. }
  271. } else {
  272. // 未推流/录制时,正常 open
  273. if (idx < monitorCnt) { // 捕获屏幕
  274. ok = m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  275. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  276. ok = m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
  277. }
  278. }
  279. if (!ok) {
  280. // 可选:弹窗或日志提示
  281. return;
  282. }
  283. dealCapture();
  284. m_isDrawCursorBox->setEnabled(true);
  285. m_recordBtn->setEnabled(true);
  286. m_liveBtn->setEnabled(true);
  287. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  288. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  289. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  290. }
  291. void AvRecorder::dealCapture()
  292. {
  293. __CheckNo(m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  294. m_settingsParam.audioParam));
  295. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  296. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  297. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  298. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  299. }
  300. void AvRecorder::stopCapture()
  301. {
  302. m_videoRecorder.Close();
  303. m_audioRecorder.Close();
  304. }
  305. void AvRecorder::renderFrame()
  306. {
  307. auto frame = m_videoRecorder.GetRenderFrame();
  308. AVFrame* copiedFrame = av_frame_clone(frame);
  309. // m_glWidget->Render(copiedFrame);
  310. QMetaObject::invokeMethod(m_glWidget,
  311. "Render",
  312. Qt::QueuedConnection,
  313. Q_ARG(AVFrame*, copiedFrame));
  314. }
  315. void AvRecorder::startPreview()
  316. {
  317. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  318. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  319. // 需要自己封装一个计时器
  320. // m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  321. // if (windowState() == Qt::WindowMinimized) {
  322. // return;
  323. // }
  324. // QMetaObject::invokeMethod(this, "renderFrame", Qt::QueuedConnection);
  325. // // // 视频
  326. // // auto frame = m_videoRecorder.GetRenderFrame();
  327. // // m_glWidget->Render(frame);
  328. // });
  329. if (!m_videoRenderTimer) {
  330. m_videoRenderTimer = new QTimer(this);
  331. connect(m_videoRenderTimer, &QTimer::timeout, this, [this] {
  332. if (windowState() == Qt::WindowMinimized)
  333. return;
  334. renderFrame();
  335. });
  336. }
  337. m_videoRenderTimer->start(1000 / m_settingsParam.videoParam.fps);
  338. // 刷新率设置为 25
  339. m_otherTimer.start(40);
  340. }
  341. void AvRecorder::stopPreview()
  342. {
  343. if (m_videoRenderTimer) {
  344. m_videoRenderTimer->stop();
  345. }
  346. m_otherTimer.stop();
  347. }
  348. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  349. {
  350. if (!m_avMuxer.Open(path, format)) {
  351. qDebug() << "Failed to open muxer with path:" << QString::fromStdString(std::string(path)) << "format:" << QString::fromStdString(std::string(format));
  352. return false;
  353. }
  354. if (!m_audioRecorder.LoadMuxer(m_avMuxer)) {
  355. qDebug() << "Failed to load muxer for audio recorder";
  356. return false;
  357. }
  358. if (!m_videoRecorder.LoadMuxer(m_avMuxer)) {
  359. qDebug() << "Failed to load muxer for video recorder";
  360. return false;
  361. }
  362. if (!m_avMuxer.WriteHeader()) {
  363. qDebug() << "Failed to write muxer header";
  364. return false;
  365. }
  366. if (!m_audioRecorder.StartRecord()) {
  367. qDebug() << "Failed to start audio recording";
  368. return false;
  369. }
  370. if (!m_videoRecorder.StartRecord()) {
  371. qDebug() << "Failed to start video recording";
  372. return false;
  373. }
  374. m_recordTime = QTime::currentTime();
  375. m_captureStatusLabel->setText("状态: 正在工作");
  376. m_settingsBtn->setEnabled(false);
  377. // m_captureComboBox->setEnabled(false); // 禁用采集源切换
  378. m_updateListBtn->setEnabled(false);
  379. m_captureMethodBox->setEnabled(false); // 禁用采集方式切换
  380. return true;
  381. }
  382. void AvRecorder::stopStream()
  383. {
  384. m_audioRecorder.StopRecord();
  385. m_videoRecorder.StopRecord();
  386. m_avMuxer.Close();
  387. // 如果有同步录像,也需要关闭
  388. if (m_isSyncRecord) {
  389. m_recordMuxer.Close();
  390. m_isSyncRecord = false;
  391. }
  392. m_captureStatusLabel->setText("状态: 正常");
  393. m_settingsBtn->setEnabled(true);
  394. m_captureComboBox->setEnabled(true); // 恢复采集源切换
  395. m_updateListBtn->setEnabled(true);
  396. m_captureMethodBox->setEnabled(true); // 恢复采集方式切换
  397. }
  398. bool AvRecorder::startSyncRecord()
  399. {
  400. auto fileName = m_settingsParam.outputDir;
  401. if (fileName.back() != '\\') {
  402. fileName.push_back('\\');
  403. }
  404. auto format = "mp4";
  405. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  406. + "_sync." + format;
  407. __CheckBool(m_recordMuxer.Open(fileName, format));
  408. __CheckBool(m_audioRecorder.LoadMuxer(m_recordMuxer));
  409. __CheckBool(m_videoRecorder.LoadMuxer(m_recordMuxer));
  410. __CheckBool(m_recordMuxer.WriteHeader());
  411. m_isSyncRecord = true;
  412. return true;
  413. }
  414. void AvRecorder::stopSyncRecord()
  415. {
  416. if (m_isSyncRecord) {
  417. m_recordMuxer.Close();
  418. m_isSyncRecord = false;
  419. }
  420. }
  421. void AvRecorder::updateCaptureList()
  422. {
  423. m_captureComboBox->clear();
  424. auto&& monitorList = MonitorFinder::GetList(true);
  425. for (auto&& monitor : monitorList) {
  426. QString text = "屏幕: " + QString::fromStdWString(monitor.title);
  427. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
  428. }
  429. auto&& windowList = WindowFinder::GetList(true);
  430. for (auto&& window : windowList) {
  431. QString text = "窗口: " + QString::fromStdWString(window.title);
  432. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
  433. }
  434. }
  435. void AvRecorder::initStatusBarUi()
  436. {
  437. m_videoEncodeLabel = new QLabel;
  438. auto hLayout = new QHBoxLayout;
  439. hLayout->setContentsMargins(0, 0, 0, 0);
  440. hLayout->addWidget(new QLabel("捕获方式:"));
  441. m_captureMethodBox = new QComboBox;
  442. hLayout->addWidget(m_captureMethodBox);
  443. m_captureStatusLabel = new QLabel("状态: 正常");
  444. m_captureTimeLabel = new QLabel("00:00:00");
  445. m_videolossRate = new QLabel("丢帧率: 00.00%");
  446. m_fpsLabel = new QLabel("FPS: 30");
  447. // 创建状态栏并添加到布局中
  448. m_statusBar = new QStatusBar(this);
  449. m_statusBar->setSizeGripEnabled(false);
  450. // 添加各个状态信息到状态栏
  451. m_statusBar->addWidget(m_videoEncodeLabel);
  452. auto widget = new QWidget;
  453. widget->setLayout(hLayout);
  454. m_statusBar->addWidget(widget);
  455. m_statusBar->addWidget(m_videolossRate);
  456. m_statusBar->addWidget(m_captureStatusLabel);
  457. m_statusBar->addWidget(m_captureTimeLabel);
  458. m_statusBar->addWidget(m_fpsLabel);
  459. }
  460. void AvRecorder::updateCaptureMethodBox(bool isMonitor) {
  461. m_captureMethodBox->clear();
  462. m_captureMethodBox->addItem("WGC");
  463. if (isMonitor) {
  464. m_captureMethodBox->addItem("DXGI");
  465. } else {
  466. m_captureMethodBox->addItem("GDI");
  467. }
  468. }
  469. // 捕获源切换时调用
  470. void AvRecorder::onCaptureSourceChanged() {
  471. int idx = m_captureComboBox->currentIndex();
  472. int monitorCnt = (int)MonitorFinder::GetList().size();
  473. bool isMonitor = (idx >= 0 && idx < monitorCnt);
  474. updateCaptureMethodBox(isMonitor);
  475. // 新增:推流/录制时切换采集源不中断
  476. if (m_isRecord || m_isLive) {
  477. CaptureMethod method = CaptureMethod::WGC;
  478. QString methodText = m_captureMethodBox->currentText();
  479. if (methodText == "DXGI") method = CaptureMethod::DXGI;
  480. else if (methodText == "GDI") method = CaptureMethod::GDI;
  481. if (isMonitor) {
  482. m_videoRecorder.SetCaptureSource(idx, method);
  483. } else {
  484. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  485. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  486. }
  487. }
  488. }