av_recorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "av_recorder.h"
  2. #include <QDateTime>
  3. #include <QStatusBar>
  4. #include <capturer/finder.h>
  5. AvRecorder::AvRecorder(QWidget* parent)
  6. : QWidget(parent)
  7. {
  8. setWindowTitle("Recorder");
  9. m_settingsParam.audioParam.bitRate = 160'000;
  10. m_settingsParam.videoParam.bitRate = 8'000'000;
  11. m_settingsParam.videoParam.fps = 30;
  12. m_settingsParam.videoParam.name = Encoder<MediaType::VIDEO>::GetUsableEncoders().front();
  13. m_settingsParam.outputDir = ".";
  14. m_settingsParam.liveUrl = "rtmp://127.0.0.1:1935/hls/111111";
  15. m_settingsParam.liveName = "stream";
  16. m_glWidget = new OpenGLVideoWidget(this);
  17. WgcCapturer::Init();
  18. auto layout = new QVBoxLayout;
  19. auto hLayout = new QHBoxLayout;
  20. hLayout->addLayout(initAudioUi(), 2);
  21. hLayout->addLayout(initListUi(), 2);
  22. hLayout->addLayout(initOtherUi(), 1);
  23. initStatusBarUi();
  24. updateCaptureList();
  25. initConnect();
  26. layout->addWidget(m_glWidget, 4);
  27. layout->addLayout(hLayout, 1);
  28. layout->addWidget(m_statusBar, 0);
  29. setLayout(layout);
  30. }
  31. void AvRecorder::initConnect()
  32. {
  33. connect(m_recordBtn, &QPushButton::released, [this] {
  34. if (!m_isRecord) {
  35. auto fileName = m_settingsParam.outputDir;
  36. if (fileName.back() != '\\') {
  37. fileName.push_back('\\');
  38. }
  39. auto format = "mp4";
  40. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  41. + "." + format;
  42. // fileName += std::string("test.") + format;
  43. __CheckNo(startStream(fileName, format));
  44. m_liveBtn->setEnabled(false);
  45. m_recordBtn->setText("停止录制");
  46. } else {
  47. stopStream();
  48. m_liveBtn->setEnabled(true);
  49. m_recordBtn->setText("开始录制");
  50. }
  51. m_isRecord = !m_isRecord;
  52. });
  53. connect(m_liveBtn, &QPushButton::released, [this] {
  54. if (!m_isLive) {
  55. auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
  56. bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
  57. __CheckNo(startStream(fileName, isRtsp ? "rtsp" : "flv"));
  58. // 如果勾选了同步录像,则开始录像
  59. if (m_syncRecordBox->isChecked()) {
  60. __CheckNo(startSyncRecord());
  61. }
  62. m_recordBtn->setEnabled(false);
  63. m_liveBtn->setText("停止直播");
  64. } else {
  65. // 先停止同步录像
  66. stopSyncRecord();
  67. // 再停止直播
  68. stopStream();
  69. m_recordBtn->setEnabled(true);
  70. m_liveBtn->setText("开始直播");
  71. }
  72. m_isLive = !m_isLive;
  73. });
  74. connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, [this](float scale) {
  75. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  76. });
  77. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, [this](float scale) {
  78. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  79. });
  80. connect(m_updateListBtn, &QPushButton::released, [this] { updateCaptureList(); });
  81. connect(m_captureListWidget, &QListWidget::currentTextChanged, [this](const QString& text) {
  82. if (text.isEmpty() || m_isLocked) {
  83. return;
  84. }
  85. m_isLocked = true;
  86. stopPreview();
  87. stopCapture();
  88. startCapture(VideoCapturer::WGC);
  89. startPreview();
  90. m_isLocked = false;
  91. });
  92. connect(m_isDrawCursorBox, &QCheckBox::stateChanged, [this] {
  93. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  94. });
  95. connect(m_captureMethodBox, &QComboBox::currentTextChanged, [this](const QString& text) {
  96. if (m_isLocked || text.isEmpty()) {
  97. return;
  98. }
  99. stopPreview();
  100. stopCapture();
  101. if (text == "WGC") {
  102. startCapture(VideoCapturer::WGC);
  103. } else if (text == "DXGI") {
  104. startCapture(VideoCapturer::DXGI);
  105. } else {
  106. startCapture(VideoCapturer::GDI);
  107. }
  108. startPreview();
  109. });
  110. connect(m_settingsBtn, &QPushButton::released, [this] {
  111. auto settingsPage = std::make_unique<SettingsPage>(&m_settingsParam, this);
  112. settingsPage->exec();
  113. m_isLocked = true;
  114. stopPreview();
  115. stopCapture();
  116. startCapture(VideoCapturer::WGC);
  117. startPreview();
  118. m_isLocked = false;
  119. });
  120. m_otherTimer.callOnTimeout([this] {
  121. if (windowState() == Qt::WindowMinimized) {
  122. return;
  123. }
  124. // 音频
  125. auto info = m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX);
  126. m_microphoneWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  127. info = m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX);
  128. m_speakerWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  129. // 状态栏
  130. if (m_isRecord || m_isLive) {
  131. int interval = m_recordTime.secsTo(QTime::currentTime());
  132. int sec = interval % 60;
  133. interval /= 60;
  134. int minute = interval % 60;
  135. int hour = interval / 60;
  136. m_captureTimeLabel->setText(QString("%1:%2:%3")
  137. .arg(hour, 2, 10, QChar('0'))
  138. .arg(minute, 2, 10, QChar('0'))
  139. .arg(sec, 2, 10, QChar('0')));
  140. auto lossRate = m_videoRecorder.GetLossRate();
  141. int num = lossRate * 10000;
  142. m_videolossRate->setText(QString("丢帧率: %1.%2%")
  143. .arg(num / 100, 2, 10, QChar('0'))
  144. .arg(num % 100, 2, 10, QChar('0')));
  145. } else if (m_captureTimeLabel->text() != "00:00:00") {
  146. m_captureTimeLabel->setText("00:00:00");
  147. }
  148. });
  149. }
  150. AvRecorder::~AvRecorder()
  151. {
  152. stopSyncRecord();
  153. stopStream();
  154. stopPreview();
  155. stopCapture();
  156. WgcCapturer::Uninit();
  157. }
  158. bool AvRecorder::start()
  159. {
  160. auto timer = new QTimer(this);
  161. connect(timer, &QTimer::timeout, [this, timer] {
  162. m_isLocked = true;
  163. stopPreview();
  164. stopCapture();
  165. startCapture(VideoCapturer::WGC);
  166. startPreview();
  167. m_isLocked = false;
  168. timer->stop();
  169. });
  170. timer->start(100);
  171. return true;
  172. }
  173. void AvRecorder::startCapture(VideoCapturer::Method method)
  174. {
  175. if (m_isLocked) {
  176. m_captureMethodBox->clear();
  177. m_captureMethodBox->addItem("WGC");
  178. }
  179. QListWidgetItem* item = m_captureListWidget->currentItem();
  180. if (!item) {
  181. return;
  182. }
  183. const QString type = item->data(Qt::UserRole + 1).toString();
  184. // 判断是要捕获屏幕还是窗口
  185. int idx = m_captureListWidget->currentRow();
  186. if (idx < 0) {
  187. idx = 0;
  188. m_captureListWidget->setCurrentRow(idx);
  189. }
  190. int monitorCnt = (int) MonitorFinder::GetList().size();
  191. if (idx < monitorCnt) { // 捕获屏幕
  192. if (m_captureMethodBox->count() < 2) {
  193. m_captureMethodBox->addItem("DXGI");
  194. }
  195. m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  196. } else {
  197. if (m_captureMethodBox->count() < 2) {
  198. m_captureMethodBox->addItem("GDI");
  199. }
  200. if (type == "window") {
  201. qintptr ptrHwnd = item->data(Qt::UserRole + 2).value<qintptr>();
  202. if (::IsWindow((HWND) ptrHwnd)) {
  203. m_videoRecorder.Open((HWND) ptrHwnd, m_settingsParam.videoParam, method);
  204. }
  205. }
  206. // auto hwnd = WindowFinder::GetList()[idx - monitorCnt].hwnd;
  207. // m_videoRecorder.Open(hwnd, m_settingsParam.videoParam, method);
  208. }
  209. dealCapture();
  210. m_isDrawCursorBox->setEnabled(true);
  211. m_recordBtn->setEnabled(true);
  212. m_liveBtn->setEnabled(true);
  213. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  214. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  215. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  216. }
  217. void AvRecorder::dealCapture()
  218. {
  219. __CheckNo(m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  220. m_settingsParam.audioParam));
  221. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  222. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  223. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  224. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  225. }
  226. void AvRecorder::stopCapture()
  227. {
  228. m_videoRecorder.Close();
  229. m_audioRecorder.Close();
  230. }
  231. void AvRecorder::startPreview()
  232. {
  233. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  234. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  235. // 需要自己封装一个计时器
  236. m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  237. if (windowState() == Qt::WindowMinimized) {
  238. return;
  239. }
  240. // 视频
  241. auto frame = m_videoRecorder.GetRenderFrame();
  242. m_glWidget->Render(frame);
  243. });
  244. // 刷新率设置为 25
  245. m_otherTimer.start(40);
  246. }
  247. void AvRecorder::stopPreview()
  248. {
  249. m_videoRenderTimer.Stop();
  250. m_otherTimer.stop();
  251. }
  252. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  253. {
  254. __CheckBool(m_avMuxer.Open(path, format));
  255. __CheckBool(m_audioRecorder.LoadMuxer(m_avMuxer));
  256. __CheckBool(m_videoRecorder.LoadMuxer(m_avMuxer));
  257. __CheckBool(m_avMuxer.WriteHeader());
  258. __CheckBool(m_audioRecorder.StartRecord());
  259. __CheckBool(m_videoRecorder.StartRecord());
  260. m_recordTime = QTime::currentTime();
  261. m_captureStatusLabel->setText("状态: 正在工作");
  262. m_settingsBtn->setEnabled(false);
  263. m_captureListWidget->setEnabled(false);
  264. m_updateListBtn->setEnabled(false);
  265. m_captureMethodBox->setEnabled(false);
  266. return true;
  267. }
  268. void AvRecorder::stopStream()
  269. {
  270. m_audioRecorder.StopRecord();
  271. m_videoRecorder.StopRecord();
  272. m_avMuxer.Close();
  273. // 如果有同步录像,也需要关闭
  274. if (m_isSyncRecord) {
  275. m_recordMuxer.Close();
  276. m_isSyncRecord = false;
  277. }
  278. m_captureStatusLabel->setText("状态: 正常");
  279. m_settingsBtn->setEnabled(true);
  280. m_captureListWidget->setEnabled(true);
  281. m_updateListBtn->setEnabled(true);
  282. m_captureMethodBox->setEnabled(true);
  283. }
  284. bool AvRecorder::startSyncRecord()
  285. {
  286. auto fileName = m_settingsParam.outputDir;
  287. if (fileName.back() != '\\') {
  288. fileName.push_back('\\');
  289. }
  290. auto format = "mp4";
  291. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  292. + "_sync." + format;
  293. __CheckBool(m_recordMuxer.Open(fileName, format));
  294. __CheckBool(m_audioRecorder.LoadMuxer(m_recordMuxer));
  295. __CheckBool(m_videoRecorder.LoadMuxer(m_recordMuxer));
  296. __CheckBool(m_recordMuxer.WriteHeader());
  297. m_isSyncRecord = true;
  298. return true;
  299. }
  300. void AvRecorder::stopSyncRecord()
  301. {
  302. if (m_isSyncRecord) {
  303. m_recordMuxer.Close();
  304. m_isSyncRecord = false;
  305. }
  306. }
  307. void AvRecorder::updateCaptureList()
  308. {
  309. m_captureListWidget->clear();
  310. auto&& monitorList = MonitorFinder::GetList(true);
  311. for (auto&& monitor : monitorList) {
  312. QListWidgetItem* item = new QListWidgetItem("屏幕: "
  313. + QString::fromStdWString(monitor.title));
  314. item->setData(Qt::UserRole + 1, "monitor");
  315. item->setData(Qt::UserRole + 2, qintptr(monitor.monitor));
  316. m_captureListWidget->addItem(item);
  317. }
  318. auto&& windowList = WindowFinder::GetList(true);
  319. for (auto&& window : windowList) {
  320. QListWidgetItem* item = new QListWidgetItem("窗口: "
  321. + QString::fromStdWString(window.title));
  322. item->setData(Qt::UserRole + 1, "window");
  323. item->setData(Qt::UserRole + 2, qintptr(window.hwnd));
  324. m_captureListWidget->addItem(item);
  325. }
  326. }
  327. QVBoxLayout* AvRecorder::initListUi()
  328. {
  329. auto layout = new QVBoxLayout;
  330. m_captureListWidget = new QListWidget;
  331. layout->addWidget(m_captureListWidget);
  332. return layout;
  333. }
  334. QVBoxLayout* AvRecorder::initAudioUi()
  335. {
  336. m_microphoneWidget = new AudioWidget;
  337. m_speakerWidget = new AudioWidget;
  338. m_microphoneWidget->SetName("麦克风");
  339. m_speakerWidget->SetName("扬声器");
  340. auto layout = new QVBoxLayout;
  341. layout->addWidget(m_microphoneWidget);
  342. layout->addWidget(m_speakerWidget);
  343. return layout;
  344. }
  345. QVBoxLayout* AvRecorder::initOtherUi()
  346. {
  347. m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
  348. m_isDrawCursorBox->setChecked(true);
  349. m_isDrawCursorBox->setEnabled(false);
  350. m_syncRecordBox = new QCheckBox("直播时同步录像");
  351. m_syncRecordBox->setChecked(false);
  352. m_updateListBtn = new QPushButton("刷新窗口列表");
  353. m_recordBtn = new QPushButton("开始录制");
  354. m_recordBtn->setEnabled(false);
  355. m_liveBtn = new QPushButton("开始直播");
  356. m_liveBtn->setEnabled(false);
  357. m_settingsBtn = new QPushButton("设置");
  358. auto layout = new QVBoxLayout;
  359. layout->addWidget(m_isDrawCursorBox);
  360. layout->addWidget(m_syncRecordBox);
  361. layout->addWidget(m_updateListBtn);
  362. layout->addWidget(m_recordBtn);
  363. layout->addWidget(m_liveBtn);
  364. layout->addWidget(m_settingsBtn);
  365. return layout;
  366. }
  367. void AvRecorder::initStatusBarUi()
  368. {
  369. m_videoEncodeLabel = new QLabel;
  370. auto hLayout = new QHBoxLayout;
  371. hLayout->setContentsMargins(0, 0, 0, 0);
  372. hLayout->addWidget(new QLabel("捕获方式:"));
  373. m_captureMethodBox = new QComboBox;
  374. hLayout->addWidget(m_captureMethodBox);
  375. m_captureStatusLabel = new QLabel("状态: 正常");
  376. m_captureTimeLabel = new QLabel("00:00:00");
  377. m_videolossRate = new QLabel("丢帧率: 00.00%");
  378. m_fpsLabel = new QLabel("FPS: 30");
  379. // 创建状态栏并添加到布局中
  380. m_statusBar = new QStatusBar(this);
  381. m_statusBar->setSizeGripEnabled(false);
  382. // 添加各个状态信息到状态栏
  383. m_statusBar->addWidget(m_videoEncodeLabel);
  384. auto widget = new QWidget;
  385. widget->setLayout(hLayout);
  386. m_statusBar->addWidget(widget);
  387. m_statusBar->addWidget(m_videolossRate);
  388. m_statusBar->addWidget(m_captureStatusLabel);
  389. m_statusBar->addWidget(m_captureTimeLabel);
  390. m_statusBar->addWidget(m_fpsLabel);
  391. }