av_recorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. auto layout = new QVBoxLayout;
  18. auto hLayout = new QHBoxLayout;
  19. hLayout->addLayout(initAudioUi(), 2);
  20. hLayout->addLayout(initListUi(), 2);
  21. hLayout->addLayout(initOtherUi(), 1);
  22. initStatusBarUi();
  23. initConnect();
  24. layout->addWidget(m_glWidget, 4);
  25. layout->addLayout(hLayout, 1);
  26. layout->addWidget(m_statusBar, 0);
  27. setLayout(layout);
  28. // 初始化数据信息
  29. updateCaptureList();
  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, [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, [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, [this](float scale) {
  86. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  87. });
  88. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  89. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  90. });
  91. connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
  92. connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [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, [this] {
  104. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  105. });
  106. connect(m_captureMethodBox, &QComboBox::currentTextChanged, this, [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, [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, [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. int idx = m_captureComboBox->currentIndex();
  191. if (idx < 0) {
  192. return;
  193. }
  194. int monitorCnt = (int)MonitorFinder::GetList().size();
  195. QString type = (idx < monitorCnt) ? "monitor" : "window";
  196. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  197. if (idx < monitorCnt) { // 捕获屏幕
  198. if (m_captureMethodBox->count() < 2) {
  199. m_captureMethodBox->addItem("DXGI");
  200. }
  201. m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  202. } else {
  203. if (m_captureMethodBox->count() < 2) {
  204. m_captureMethodBox->addItem("GDI");
  205. }
  206. if (type == "window") {
  207. if (::IsWindow((HWND)ptrHwnd)) {
  208. m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
  209. }
  210. }
  211. }
  212. dealCapture();
  213. m_isDrawCursorBox->setEnabled(true);
  214. m_recordBtn->setEnabled(true);
  215. m_liveBtn->setEnabled(true);
  216. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  217. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  218. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  219. }
  220. void AvRecorder::dealCapture()
  221. {
  222. __CheckNo(m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  223. m_settingsParam.audioParam));
  224. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  225. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  226. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  227. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  228. }
  229. void AvRecorder::stopCapture()
  230. {
  231. m_videoRecorder.Close();
  232. m_audioRecorder.Close();
  233. }
  234. void AvRecorder::startPreview()
  235. {
  236. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  237. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  238. // 需要自己封装一个计时器
  239. m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  240. if (windowState() == Qt::WindowMinimized) {
  241. return;
  242. }
  243. // 视频
  244. auto frame = m_videoRecorder.GetRenderFrame();
  245. m_glWidget->Render(frame);
  246. });
  247. // 刷新率设置为 25
  248. m_otherTimer.start(40);
  249. }
  250. void AvRecorder::stopPreview()
  251. {
  252. m_videoRenderTimer.Stop();
  253. m_otherTimer.stop();
  254. }
  255. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  256. {
  257. __CheckBool(m_avMuxer.Open(path, format));
  258. __CheckBool(m_audioRecorder.LoadMuxer(m_avMuxer));
  259. __CheckBool(m_videoRecorder.LoadMuxer(m_avMuxer));
  260. __CheckBool(m_avMuxer.WriteHeader());
  261. __CheckBool(m_audioRecorder.StartRecord());
  262. __CheckBool(m_videoRecorder.StartRecord());
  263. m_recordTime = QTime::currentTime();
  264. m_captureStatusLabel->setText("状态: 正在工作");
  265. m_settingsBtn->setEnabled(false);
  266. m_captureComboBox->setEnabled(false);
  267. m_updateListBtn->setEnabled(false);
  268. m_captureMethodBox->setEnabled(false);
  269. return true;
  270. }
  271. void AvRecorder::stopStream()
  272. {
  273. m_audioRecorder.StopRecord();
  274. m_videoRecorder.StopRecord();
  275. m_avMuxer.Close();
  276. // 如果有同步录像,也需要关闭
  277. if (m_isSyncRecord) {
  278. m_recordMuxer.Close();
  279. m_isSyncRecord = false;
  280. }
  281. m_captureStatusLabel->setText("状态: 正常");
  282. m_settingsBtn->setEnabled(true);
  283. m_captureComboBox->setEnabled(true);
  284. m_updateListBtn->setEnabled(true);
  285. m_captureMethodBox->setEnabled(true);
  286. }
  287. bool AvRecorder::startSyncRecord()
  288. {
  289. auto fileName = m_settingsParam.outputDir;
  290. if (fileName.back() != '\\') {
  291. fileName.push_back('\\');
  292. }
  293. auto format = "mp4";
  294. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  295. + "_sync." + format;
  296. __CheckBool(m_recordMuxer.Open(fileName, format));
  297. __CheckBool(m_audioRecorder.LoadMuxer(m_recordMuxer));
  298. __CheckBool(m_videoRecorder.LoadMuxer(m_recordMuxer));
  299. __CheckBool(m_recordMuxer.WriteHeader());
  300. m_isSyncRecord = true;
  301. return true;
  302. }
  303. void AvRecorder::stopSyncRecord()
  304. {
  305. if (m_isSyncRecord) {
  306. m_recordMuxer.Close();
  307. m_isSyncRecord = false;
  308. }
  309. }
  310. void AvRecorder::updateCaptureList()
  311. {
  312. m_captureComboBox->clear();
  313. auto&& monitorList = MonitorFinder::GetList(true);
  314. for (auto&& monitor : monitorList) {
  315. QString text = "屏幕: " + QString::fromStdWString(monitor.title);
  316. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
  317. }
  318. auto&& windowList = WindowFinder::GetList(true);
  319. for (auto&& window : windowList) {
  320. QString text = "窗口: " + QString::fromStdWString(window.title);
  321. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
  322. }
  323. }
  324. QVBoxLayout* AvRecorder::initListUi()
  325. {
  326. auto layout = new QVBoxLayout;
  327. m_captureComboBox = new QComboBox;
  328. layout->addWidget(m_captureComboBox);
  329. return layout;
  330. }
  331. QVBoxLayout* AvRecorder::initAudioUi()
  332. {
  333. m_microphoneWidget = new AudioWidget;
  334. m_speakerWidget = new AudioWidget;
  335. m_microphoneWidget->SetName("麦克风");
  336. m_speakerWidget->SetName("扬声器");
  337. auto layout = new QVBoxLayout;
  338. layout->addWidget(m_microphoneWidget);
  339. layout->addWidget(m_speakerWidget);
  340. return layout;
  341. }
  342. QVBoxLayout* AvRecorder::initOtherUi()
  343. {
  344. m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
  345. m_isDrawCursorBox->setChecked(true);
  346. m_isDrawCursorBox->setEnabled(false);
  347. m_syncRecordBox = new QCheckBox("直播时同步录像");
  348. m_syncRecordBox->setChecked(false);
  349. m_updateListBtn = new QPushButton("刷新窗口列表");
  350. m_recordBtn = new QPushButton("开始录制");
  351. m_recordBtn->setEnabled(false);
  352. m_liveBtn = new QPushButton("开始直播");
  353. m_liveBtn->setEnabled(false);
  354. m_settingsBtn = new QPushButton("设置");
  355. auto layout = new QVBoxLayout;
  356. layout->addWidget(m_isDrawCursorBox);
  357. layout->addWidget(m_syncRecordBox);
  358. layout->addWidget(m_updateListBtn);
  359. layout->addWidget(m_recordBtn);
  360. layout->addWidget(m_liveBtn);
  361. layout->addWidget(m_settingsBtn);
  362. return layout;
  363. }
  364. void AvRecorder::initStatusBarUi()
  365. {
  366. m_videoEncodeLabel = new QLabel;
  367. auto hLayout = new QHBoxLayout;
  368. hLayout->setContentsMargins(0, 0, 0, 0);
  369. hLayout->addWidget(new QLabel("捕获方式:"));
  370. m_captureMethodBox = new QComboBox;
  371. hLayout->addWidget(m_captureMethodBox);
  372. m_captureStatusLabel = new QLabel("状态: 正常");
  373. m_captureTimeLabel = new QLabel("00:00:00");
  374. m_videolossRate = new QLabel("丢帧率: 00.00%");
  375. m_fpsLabel = new QLabel("FPS: 30");
  376. // 创建状态栏并添加到布局中
  377. m_statusBar = new QStatusBar(this);
  378. m_statusBar->setSizeGripEnabled(false);
  379. // 添加各个状态信息到状态栏
  380. m_statusBar->addWidget(m_videoEncodeLabel);
  381. auto widget = new QWidget;
  382. widget->setLayout(hLayout);
  383. m_statusBar->addWidget(widget);
  384. m_statusBar->addWidget(m_videolossRate);
  385. m_statusBar->addWidget(m_captureStatusLabel);
  386. m_statusBar->addWidget(m_captureTimeLabel);
  387. m_statusBar->addWidget(m_fpsLabel);
  388. }