av_recorder.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. QVBoxLayout* captureLayout = new QVBoxLayout;
  30. QHBoxLayout* captureRow = new QHBoxLayout;
  31. captureRow->addWidget(m_captureComboBox);
  32. captureRow->addWidget(m_updateListBtn);
  33. captureLayout->addLayout(captureRow);
  34. captureGroup->setLayout(captureLayout);
  35. captureGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  36. // 4. 音频区分组
  37. QGroupBox* audioGroup = new QGroupBox("音频");
  38. QVBoxLayout* audioLayout = new QVBoxLayout;
  39. audioLayout->addWidget(m_microphoneWidget);
  40. audioLayout->addWidget(m_speakerWidget);
  41. audioGroup->setLayout(audioLayout);
  42. audioGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  43. // 5. 操作区
  44. m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
  45. m_isDrawCursorBox->setChecked(true);
  46. m_isDrawCursorBox->setEnabled(false);
  47. m_syncRecordBox = new QCheckBox("直播时同步录像");
  48. m_syncRecordBox->setChecked(false);
  49. m_recordBtn = new QPushButton("开始录制");
  50. m_recordBtn->setEnabled(false);
  51. m_liveBtn = new QPushButton("开始直播");
  52. m_liveBtn->setEnabled(false);
  53. m_settingsBtn = new QPushButton("设置");
  54. QGroupBox* actionGroup = new QGroupBox("操作");
  55. QVBoxLayout* actionLayout = new QVBoxLayout;
  56. QHBoxLayout* checkBoxRow = new QHBoxLayout;
  57. checkBoxRow->setContentsMargins(0, 0, 0, 0);
  58. checkBoxRow->setSpacing(8);
  59. checkBoxRow->addWidget(m_syncRecordBox);
  60. checkBoxRow->addWidget(m_isDrawCursorBox);
  61. actionLayout->addLayout(checkBoxRow);
  62. actionLayout->addWidget(m_recordBtn);
  63. actionLayout->addWidget(m_liveBtn);
  64. actionGroup->setLayout(actionLayout);
  65. actionGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  66. // 6. 设置区
  67. QHBoxLayout* utilLayout = new QHBoxLayout;
  68. utilLayout->addWidget(m_settingsBtn);
  69. // 7. 左侧功能区(捕获区在上,音频区在下)
  70. QVBoxLayout* leftLayout = new QVBoxLayout;
  71. leftLayout->addWidget(captureGroup);
  72. leftLayout->addWidget(audioGroup);
  73. leftLayout->addStretch();
  74. // 8. 右侧功能区
  75. QVBoxLayout* rightLayout = new QVBoxLayout;
  76. rightLayout->addWidget(actionGroup);
  77. rightLayout->addLayout(utilLayout);
  78. rightLayout->addStretch();
  79. // 9. 中部主布局
  80. QHBoxLayout* centerLayout = new QHBoxLayout;
  81. centerLayout->addLayout(leftLayout, 2);
  82. centerLayout->addLayout(rightLayout, 3);
  83. // 10. 状态栏
  84. initStatusBarUi();
  85. // 让视频区尽量大
  86. m_glWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  87. // 11. 总体布局
  88. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  89. mainLayout->addWidget(m_glWidget, 100);
  90. mainLayout->addLayout(centerLayout, 0);
  91. mainLayout->addWidget(m_statusBar, 0);
  92. setLayout(mainLayout);
  93. // 12. 连接信号槽、初始化数据
  94. initConnect();
  95. updateCaptureList();
  96. }
  97. void AvRecorder::setSettings(const SettingsPage::Param& param)
  98. {
  99. m_settingsParam.audioParam.bitRate = 160'000;
  100. m_settingsParam.videoParam.bitRate = 8'000'000;
  101. m_settingsParam.videoParam.fps = 30;
  102. m_settingsParam.videoParam.name = Encoder<MediaType::VIDEO>::GetUsableEncoders().front();
  103. m_settingsParam.outputDir = ".";
  104. m_settingsParam.liveUrl = param.liveUrl; // "rtmp://192.168.3.76:1935/stream/V1";
  105. m_settingsParam.liveName = param.liveName; // "stream";
  106. }
  107. void AvRecorder::initConnect()
  108. {
  109. connect(m_recordBtn, &QPushButton::released, this, [this] {
  110. if (!m_isRecord) {
  111. auto fileName = m_settingsParam.outputDir;
  112. if (fileName.back() != '\\') {
  113. fileName.push_back('\\');
  114. }
  115. auto format = "mp4";
  116. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  117. + "." + format;
  118. // fileName += std::string("test.") + format;
  119. __CheckNo(startStream(fileName, format));
  120. m_liveBtn->setEnabled(false);
  121. m_recordBtn->setText("停止录制");
  122. } else {
  123. stopStream();
  124. m_liveBtn->setEnabled(true);
  125. m_recordBtn->setText("开始录制");
  126. }
  127. m_isRecord = !m_isRecord;
  128. });
  129. connect(m_liveBtn, &QPushButton::released, this, [this] {
  130. if (!m_isLive) {
  131. auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
  132. bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
  133. qDebug() << "直播地址:" << QString::fromStdString(fileName);
  134. __CheckNo(startStream(fileName, isRtsp ? "rtsp" : "flv"));
  135. // 如果勾选了同步录像,则开始录像
  136. if (m_syncRecordBox->isChecked()) {
  137. __CheckNo(startSyncRecord());
  138. }
  139. m_recordBtn->setEnabled(false);
  140. m_liveBtn->setText("停止直播");
  141. } else {
  142. // 先停止同步录像
  143. stopSyncRecord();
  144. // 再停止直播
  145. stopStream();
  146. m_recordBtn->setEnabled(true);
  147. m_liveBtn->setText("开始直播");
  148. }
  149. m_isLive = !m_isLive;
  150. });
  151. connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  152. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  153. });
  154. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  155. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  156. });
  157. connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
  158. connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  159. if (text.isEmpty() || m_isLocked) {
  160. return;
  161. }
  162. m_isLocked = true;
  163. stopPreview();
  164. stopCapture();
  165. startCapture(CaptureMethod::WGC);
  166. startPreview();
  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 (idx < monitorCnt) { // 捕获屏幕
  263. ok = m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  264. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  265. ok = m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
  266. }
  267. if (!ok) {
  268. // 可选:弹窗或日志提示
  269. return;
  270. }
  271. dealCapture();
  272. m_isDrawCursorBox->setEnabled(true);
  273. m_recordBtn->setEnabled(true);
  274. m_liveBtn->setEnabled(true);
  275. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  276. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  277. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  278. }
  279. void AvRecorder::dealCapture()
  280. {
  281. __CheckNo(m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  282. m_settingsParam.audioParam));
  283. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  284. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  285. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  286. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  287. }
  288. void AvRecorder::stopCapture()
  289. {
  290. m_videoRecorder.Close();
  291. m_audioRecorder.Close();
  292. }
  293. void AvRecorder::startPreview()
  294. {
  295. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  296. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  297. // 需要自己封装一个计时器
  298. m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  299. if (windowState() == Qt::WindowMinimized) {
  300. return;
  301. }
  302. // 视频
  303. auto frame = m_videoRecorder.GetRenderFrame();
  304. m_glWidget->Render(frame);
  305. });
  306. // 刷新率设置为 25
  307. m_otherTimer.start(40);
  308. }
  309. void AvRecorder::stopPreview()
  310. {
  311. m_videoRenderTimer.Stop();
  312. m_otherTimer.stop();
  313. }
  314. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  315. {
  316. __CheckBool(m_avMuxer.Open(path, format));
  317. __CheckBool(m_audioRecorder.LoadMuxer(m_avMuxer));
  318. __CheckBool(m_videoRecorder.LoadMuxer(m_avMuxer));
  319. __CheckBool(m_avMuxer.WriteHeader());
  320. __CheckBool(m_audioRecorder.StartRecord());
  321. __CheckBool(m_videoRecorder.StartRecord());
  322. m_recordTime = QTime::currentTime();
  323. m_captureStatusLabel->setText("状态: 正在工作");
  324. m_settingsBtn->setEnabled(false);
  325. m_captureComboBox->setEnabled(false);
  326. m_updateListBtn->setEnabled(false);
  327. m_captureMethodBox->setEnabled(false);
  328. return true;
  329. }
  330. void AvRecorder::stopStream()
  331. {
  332. m_audioRecorder.StopRecord();
  333. m_videoRecorder.StopRecord();
  334. m_avMuxer.Close();
  335. // 如果有同步录像,也需要关闭
  336. if (m_isSyncRecord) {
  337. m_recordMuxer.Close();
  338. m_isSyncRecord = false;
  339. }
  340. m_captureStatusLabel->setText("状态: 正常");
  341. m_settingsBtn->setEnabled(true);
  342. m_captureComboBox->setEnabled(true);
  343. m_updateListBtn->setEnabled(true);
  344. m_captureMethodBox->setEnabled(true);
  345. }
  346. bool AvRecorder::startSyncRecord()
  347. {
  348. auto fileName = m_settingsParam.outputDir;
  349. if (fileName.back() != '\\') {
  350. fileName.push_back('\\');
  351. }
  352. auto format = "mp4";
  353. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  354. + "_sync." + format;
  355. __CheckBool(m_recordMuxer.Open(fileName, format));
  356. __CheckBool(m_audioRecorder.LoadMuxer(m_recordMuxer));
  357. __CheckBool(m_videoRecorder.LoadMuxer(m_recordMuxer));
  358. __CheckBool(m_recordMuxer.WriteHeader());
  359. m_isSyncRecord = true;
  360. return true;
  361. }
  362. void AvRecorder::stopSyncRecord()
  363. {
  364. if (m_isSyncRecord) {
  365. m_recordMuxer.Close();
  366. m_isSyncRecord = false;
  367. }
  368. }
  369. void AvRecorder::updateCaptureList()
  370. {
  371. m_captureComboBox->clear();
  372. auto&& monitorList = MonitorFinder::GetList(true);
  373. for (auto&& monitor : monitorList) {
  374. QString text = "屏幕: " + QString::fromStdWString(monitor.title);
  375. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
  376. }
  377. auto&& windowList = WindowFinder::GetList(true);
  378. for (auto&& window : windowList) {
  379. QString text = "窗口: " + QString::fromStdWString(window.title);
  380. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
  381. }
  382. }
  383. void AvRecorder::initStatusBarUi()
  384. {
  385. m_videoEncodeLabel = new QLabel;
  386. auto hLayout = new QHBoxLayout;
  387. hLayout->setContentsMargins(0, 0, 0, 0);
  388. hLayout->addWidget(new QLabel("捕获方式:"));
  389. m_captureMethodBox = new QComboBox;
  390. hLayout->addWidget(m_captureMethodBox);
  391. m_captureStatusLabel = new QLabel("状态: 正常");
  392. m_captureTimeLabel = new QLabel("00:00:00");
  393. m_videolossRate = new QLabel("丢帧率: 00.00%");
  394. m_fpsLabel = new QLabel("FPS: 30");
  395. // 创建状态栏并添加到布局中
  396. m_statusBar = new QStatusBar(this);
  397. m_statusBar->setSizeGripEnabled(false);
  398. // 添加各个状态信息到状态栏
  399. m_statusBar->addWidget(m_videoEncodeLabel);
  400. auto widget = new QWidget;
  401. widget->setLayout(hLayout);
  402. m_statusBar->addWidget(widget);
  403. m_statusBar->addWidget(m_videolossRate);
  404. m_statusBar->addWidget(m_captureStatusLabel);
  405. m_statusBar->addWidget(m_captureTimeLabel);
  406. m_statusBar->addWidget(m_fpsLabel);
  407. }
  408. void AvRecorder::updateCaptureMethodBox(bool isMonitor) {
  409. m_captureMethodBox->clear();
  410. m_captureMethodBox->addItem("WGC");
  411. if (isMonitor) {
  412. m_captureMethodBox->addItem("DXGI");
  413. } else {
  414. m_captureMethodBox->addItem("GDI");
  415. }
  416. }
  417. // 捕获源切换时调用
  418. void AvRecorder::onCaptureSourceChanged() {
  419. int idx = m_captureComboBox->currentIndex();
  420. int monitorCnt = (int)MonitorFinder::GetList().size();
  421. bool isMonitor = (idx >= 0 && idx < monitorCnt);
  422. updateCaptureMethodBox(isMonitor);
  423. }