av_recorder.cpp 15 KB

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