av_recorder.cpp 14 KB

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