av_recorder.cpp 18 KB

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