av_recorder.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. m_settingsParam.audioParam.bitRate = 160'000;
  11. m_settingsParam.videoParam.bitRate = 8'000'000;
  12. m_settingsParam.videoParam.fps = 30;
  13. m_settingsParam.videoParam.name = Encoder<MediaType::VIDEO>::GetUsableEncoders().front();
  14. m_settingsParam.outputDir = ".";
  15. m_settingsParam.liveUrl = "rtmp://127.0.0.1:1935/stream/V1";
  16. m_settingsParam.liveName = "stream";
  17. // 1. 视频预览区
  18. m_glWidget = new OpenGLVideoWidget(this);
  19. // 2. 音频区
  20. m_microphoneWidget = new AudioWidget;
  21. m_speakerWidget = new AudioWidget;
  22. m_microphoneWidget->SetName("麦克风");
  23. m_speakerWidget->SetName("扬声器");
  24. // 3. 捕获区
  25. m_captureComboBox = new QComboBox;
  26. m_updateListBtn = new QPushButton("刷新窗口列表");
  27. QGroupBox* captureGroup = new QGroupBox("捕获源");
  28. QHBoxLayout* captureRow = new QHBoxLayout;
  29. captureRow->addWidget(m_captureComboBox);
  30. captureRow->addWidget(m_updateListBtn);
  31. captureGroup->setLayout(captureRow);
  32. // m_captureComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
  33. m_captureComboBox->setMinimumWidth(20);
  34. // 4. 音频区分组
  35. QGroupBox* audioGroup = new QGroupBox("音频");
  36. QVBoxLayout* audioLayout = new QVBoxLayout;
  37. audioLayout->addWidget(m_microphoneWidget);
  38. audioLayout->addWidget(m_speakerWidget);
  39. audioGroup->setLayout(audioLayout);
  40. //audioGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  41. // 5. 操作区
  42. m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
  43. m_isDrawCursorBox->setChecked(true);
  44. m_isDrawCursorBox->setEnabled(false);
  45. m_syncRecordBox = new QCheckBox("直播时同步录像");
  46. m_syncRecordBox->setChecked(false);
  47. m_recordBtn = new QPushButton("开始录制");
  48. m_recordBtn->setEnabled(false);
  49. m_liveBtn = new QPushButton("开始直播");
  50. m_liveBtn->setEnabled(false);
  51. m_settingsBtn = new QPushButton("设置");
  52. QGroupBox* actionGroup = new QGroupBox("操作");
  53. QVBoxLayout* actionLayout = new QVBoxLayout;
  54. QHBoxLayout* checkBoxRow = new QHBoxLayout;
  55. checkBoxRow->setContentsMargins(0, 0, 0, 0);
  56. checkBoxRow->setSpacing(8);
  57. checkBoxRow->addWidget(m_syncRecordBox);
  58. checkBoxRow->addWidget(m_isDrawCursorBox);
  59. actionLayout->addLayout(checkBoxRow);
  60. actionLayout->addWidget(m_recordBtn);
  61. actionLayout->addWidget(m_liveBtn);
  62. actionGroup->setLayout(actionLayout);
  63. //actionGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  64. // 6. 设置区
  65. QHBoxLayout* utilLayout = new QHBoxLayout;
  66. utilLayout->addWidget(m_settingsBtn);
  67. // 7. 左侧功能区(捕获区在上,音频区在下)
  68. QVBoxLayout* leftLayout = new QVBoxLayout;
  69. leftLayout->addWidget(captureGroup);
  70. leftLayout->addWidget(audioGroup);
  71. leftLayout->addStretch();
  72. // 8. 右侧功能区
  73. QVBoxLayout* rightLayout = new QVBoxLayout;
  74. rightLayout->addWidget(actionGroup);
  75. rightLayout->addLayout(utilLayout);
  76. rightLayout->addStretch();
  77. // 9. 中部主布局
  78. QHBoxLayout* centerLayout = new QHBoxLayout;
  79. centerLayout->addLayout(leftLayout, 2);
  80. centerLayout->addLayout(rightLayout, 3);
  81. // 10. 状态栏
  82. initStatusBarUi();
  83. // 让视频区尽量大
  84. // m_glWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  85. // 11. 总体布局
  86. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  87. mainLayout->addWidget(m_glWidget, 100);
  88. mainLayout->addLayout(centerLayout, 0);
  89. mainLayout->addWidget(m_statusBar, 0);
  90. setLayout(mainLayout);
  91. // 12. 连接信号槽、初始化数据
  92. initConnect();
  93. updateCaptureList();
  94. }
  95. void AvRecorder::setSettings(const SettingsPage::Param& param)
  96. {
  97. m_settingsParam.audioParam.bitRate = 160'000;
  98. m_settingsParam.videoParam.bitRate = 8'000'000;
  99. m_settingsParam.videoParam.fps = 30;
  100. m_settingsParam.videoParam.name = Encoder<MediaType::VIDEO>::GetUsableEncoders().front();
  101. m_settingsParam.outputDir = ".";
  102. m_settingsParam.liveUrl = param.liveUrl; // "rtmp://192.168.3.76:1935/stream/V1";
  103. m_settingsParam.liveName = param.liveName; // "stream";
  104. }
  105. void AvRecorder::initConnect()
  106. {
  107. connect(m_recordBtn, &QPushButton::released, this, [this] {
  108. if (!m_isRecord) {
  109. auto fileName = m_settingsParam.outputDir;
  110. if (fileName.back() != '\\') {
  111. fileName.push_back('\\');
  112. }
  113. auto format = "mp4";
  114. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  115. + "." + format;
  116. // fileName += std::string("test.") + format;
  117. __CheckNo(startStream(fileName, format));
  118. m_liveBtn->setEnabled(false);
  119. m_recordBtn->setText("停止录制");
  120. } else {
  121. stopStream();
  122. m_liveBtn->setEnabled(true);
  123. m_recordBtn->setText("开始录制");
  124. }
  125. m_isRecord = !m_isRecord;
  126. });
  127. connect(m_liveBtn, &QPushButton::released, this, [this] {
  128. if (!m_isLive) {
  129. auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
  130. bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
  131. qDebug() << "直播地址:" << QString::fromStdString(fileName);
  132. __CheckNo(startStream(fileName, isRtsp ? "rtsp" : "flv"));
  133. // 如果勾选了同步录像,则开始录像
  134. if (m_syncRecordBox->isChecked()) {
  135. __CheckNo(startSyncRecord());
  136. }
  137. m_recordBtn->setEnabled(false);
  138. m_liveBtn->setText("停止直播");
  139. } else {
  140. // 先停止同步录像
  141. stopSyncRecord();
  142. // 再停止直播
  143. stopStream();
  144. m_recordBtn->setEnabled(true);
  145. m_liveBtn->setText("开始直播");
  146. }
  147. });
  148. connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  149. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  150. });
  151. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  152. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  153. });
  154. connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
  155. connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  156. if (text.isEmpty() || m_isLocked) {
  157. return;
  158. }
  159. m_isLocked = true;
  160. if (!(m_isRecord || m_isLive)) {
  161. stopPreview();
  162. stopCapture();
  163. startCapture(CaptureMethod::WGC);
  164. startPreview();
  165. }
  166. m_isLocked = false;
  167. });
  168. connect(m_captureComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AvRecorder::onCaptureSourceChanged);
  169. connect(m_isDrawCursorBox, &QCheckBox::stateChanged, this, [this] {
  170. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  171. });
  172. connect(m_captureMethodBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  173. if (m_isLocked || text.isEmpty()) {
  174. return;
  175. }
  176. stopPreview();
  177. stopCapture();
  178. if (text == "WGC") {
  179. startCapture(CaptureMethod::WGC);
  180. } else if (text == "DXGI") {
  181. startCapture(CaptureMethod::DXGI);
  182. } else {
  183. startCapture(CaptureMethod::GDI);
  184. }
  185. startPreview();
  186. });
  187. connect(m_settingsBtn, &QPushButton::released, this, [this] {
  188. auto settingsPage = std::make_unique<SettingsPage>(&m_settingsParam, this);
  189. settingsPage->exec();
  190. m_isLocked = true;
  191. stopPreview();
  192. stopCapture();
  193. startCapture(CaptureMethod::WGC);
  194. startPreview();
  195. m_isLocked = false;
  196. });
  197. m_otherTimer.callOnTimeout([this] {
  198. if (windowState() == Qt::WindowMinimized) {
  199. return;
  200. }
  201. // 音频
  202. auto info = m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX);
  203. m_microphoneWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  204. info = m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX);
  205. m_speakerWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  206. // 状态栏
  207. if (m_isRecord || m_isLive) {
  208. int interval = m_recordTime.secsTo(QTime::currentTime());
  209. int sec = interval % 60;
  210. interval /= 60;
  211. int minute = interval % 60;
  212. int hour = interval / 60;
  213. m_captureTimeLabel->setText(QString("%1:%2:%3")
  214. .arg(hour, 2, 10, QChar('0'))
  215. .arg(minute, 2, 10, QChar('0'))
  216. .arg(sec, 2, 10, QChar('0')));
  217. auto lossRate = m_videoRecorder.GetLossRate();
  218. if (lossRate < 0) {
  219. m_videolossRate->setText("丢帧率: 统计中");
  220. } else {
  221. int num = lossRate * 10000;
  222. m_videolossRate->setText(QString("丢帧率: %1.%2%")
  223. .arg(num / 100, 2, 10, QChar('0'))
  224. .arg(num % 100, 2, 10, QChar('0')));
  225. }
  226. } else if (m_captureTimeLabel->text() != "00:00:00") {
  227. m_captureTimeLabel->setText("00:00:00");
  228. }
  229. });
  230. }
  231. AvRecorder::~AvRecorder()
  232. {
  233. stopSyncRecord();
  234. stopStream();
  235. stopPreview();
  236. stopCapture();
  237. }
  238. bool AvRecorder::start()
  239. {
  240. auto timer = new QTimer(this);
  241. connect(timer, &QTimer::timeout, this, [this, timer] {
  242. m_isLocked = true;
  243. stopPreview();
  244. stopCapture();
  245. startCapture(CaptureMethod::WGC);
  246. startPreview();
  247. m_isLocked = false;
  248. timer->stop();
  249. });
  250. timer->start(100);
  251. return true;
  252. }
  253. void AvRecorder::startCapture(CaptureMethod method)
  254. {
  255. int idx = m_captureComboBox->currentIndex();
  256. if (idx < 0) return;
  257. int monitorCnt = (int)MonitorFinder::GetList().size();
  258. QString type = (idx < monitorCnt) ? "monitor" : "window";
  259. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  260. bool ok = false;
  261. if (m_isRecord || m_isLive) {
  262. // 推流/录制时,安全切换采集源
  263. if (idx < monitorCnt) {
  264. m_videoRecorder.SetCaptureSource(idx, method);
  265. ok = true;
  266. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  267. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  268. ok = true;
  269. }
  270. } else {
  271. // 未推流/录制时,正常 open
  272. if (idx < monitorCnt) { // 捕获屏幕
  273. ok = m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  274. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  275. ok = m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
  276. }
  277. }
  278. if (!ok) {
  279. // 可选:弹窗或日志提示
  280. return;
  281. }
  282. dealCapture();
  283. m_isDrawCursorBox->setEnabled(true);
  284. m_recordBtn->setEnabled(true);
  285. m_liveBtn->setEnabled(true);
  286. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  287. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  288. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  289. }
  290. void AvRecorder::dealCapture()
  291. {
  292. __CheckNo(m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  293. m_settingsParam.audioParam));
  294. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  295. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  296. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  297. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  298. }
  299. void AvRecorder::stopCapture()
  300. {
  301. m_videoRecorder.Close();
  302. m_audioRecorder.Close();
  303. }
  304. void AvRecorder::renderFrame()
  305. {
  306. auto frame = m_videoRecorder.GetRenderFrame();
  307. AVFrame* copiedFrame = av_frame_clone(frame);
  308. // m_glWidget->Render(copiedFrame);
  309. QMetaObject::invokeMethod(m_glWidget,
  310. "Render",
  311. Qt::QueuedConnection,
  312. Q_ARG(AVFrame*, copiedFrame));
  313. }
  314. void AvRecorder::startPreview()
  315. {
  316. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  317. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  318. // 需要自己封装一个计时器
  319. // m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  320. // if (windowState() == Qt::WindowMinimized) {
  321. // return;
  322. // }
  323. // QMetaObject::invokeMethod(this, "renderFrame", Qt::QueuedConnection);
  324. // // // 视频
  325. // // auto frame = m_videoRecorder.GetRenderFrame();
  326. // // m_glWidget->Render(frame);
  327. // });
  328. if (!m_videoRenderTimer) {
  329. m_videoRenderTimer = new QTimer(this);
  330. connect(m_videoRenderTimer, &QTimer::timeout, this, [this] {
  331. if (windowState() == Qt::WindowMinimized)
  332. return;
  333. renderFrame();
  334. });
  335. }
  336. m_videoRenderTimer->start(1000 / m_settingsParam.videoParam.fps);
  337. // 刷新率设置为 25
  338. m_otherTimer.start(40);
  339. }
  340. void AvRecorder::stopPreview()
  341. {
  342. if (m_videoRenderTimer) {
  343. m_videoRenderTimer->stop();
  344. }
  345. m_otherTimer.stop();
  346. }
  347. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  348. {
  349. if (!m_avMuxer.Open(path, format)) {
  350. qDebug() << "Failed to open muxer with path:" << QString::fromStdString(std::string(path)) << "format:" << QString::fromStdString(std::string(format));
  351. return false;
  352. }
  353. if (!m_audioRecorder.LoadMuxer(m_avMuxer)) {
  354. qDebug() << "Failed to load muxer for audio recorder";
  355. return false;
  356. }
  357. if (!m_videoRecorder.LoadMuxer(m_avMuxer)) {
  358. qDebug() << "Failed to load muxer for video recorder";
  359. return false;
  360. }
  361. if (!m_avMuxer.WriteHeader()) {
  362. qDebug() << "Failed to write muxer header";
  363. return false;
  364. }
  365. if (!m_audioRecorder.StartRecord()) {
  366. qDebug() << "Failed to start audio recording";
  367. return false;
  368. }
  369. if (!m_videoRecorder.StartRecord()) {
  370. qDebug() << "Failed to start video recording";
  371. return false;
  372. }
  373. m_recordTime = QTime::currentTime();
  374. m_captureStatusLabel->setText("状态: 正在工作");
  375. m_settingsBtn->setEnabled(false);
  376. m_captureComboBox->setEnabled(false); // 禁用采集源切换
  377. m_syncRecordBox->setEnabled(false);
  378. m_updateListBtn->setEnabled(false);
  379. m_captureMethodBox->setEnabled(false); // 禁用采集方式切换
  380. m_isLive = !m_isLive;
  381. return true;
  382. }
  383. void AvRecorder::stopStream()
  384. {
  385. m_audioRecorder.StopRecord();
  386. m_videoRecorder.StopRecord();
  387. // 从录制器中卸载直播muxer
  388. m_audioRecorder.UnloadMuxer(m_avMuxer);
  389. m_videoRecorder.UnloadMuxer(m_avMuxer);
  390. m_avMuxer.Close();
  391. // 如果有同步录像,也需要关闭
  392. if (m_isSyncRecord) {
  393. // 先从录制器中卸载同步录像muxer
  394. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  395. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  396. m_recordMuxer.Close();
  397. m_isSyncRecord = false;
  398. }
  399. m_captureStatusLabel->setText("状态: 正常");
  400. m_settingsBtn->setEnabled(true);
  401. m_captureComboBox->setEnabled(true); // 恢复采集源切换
  402. m_syncRecordBox->setEnabled(true);
  403. m_updateListBtn->setEnabled(true);
  404. m_captureMethodBox->setEnabled(true); // 恢复采集方式切换
  405. }
  406. bool AvRecorder::startSyncRecord()
  407. {
  408. // 检查是否已经在同步录像
  409. if (m_isSyncRecord) {
  410. qDebug() << "Sync recording is already active";
  411. return true;
  412. }
  413. // 检查是否正在直播(必须在直播状态下才能启动同步录像)
  414. if (!m_isLive) {
  415. qDebug() << "Cannot start sync recording: not in live streaming mode";
  416. return false;
  417. }
  418. auto fileName = m_settingsParam.outputDir;
  419. if (fileName.back() != '\\') {
  420. fileName.push_back('\\');
  421. }
  422. auto format = "mp4";
  423. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  424. + "_sync." + format;
  425. // 打开同步录像的muxer
  426. if (!m_recordMuxer.Open(fileName, format)) {
  427. qDebug() << "Failed to open sync record muxer";
  428. return false;
  429. }
  430. // 加载muxer到录制器
  431. if (!m_audioRecorder.LoadMuxer(m_recordMuxer)) {
  432. qDebug() << "Failed to load sync muxer for audio recorder";
  433. m_recordMuxer.Close();
  434. return false;
  435. }
  436. if (!m_videoRecorder.LoadMuxer(m_recordMuxer)) {
  437. qDebug() << "Failed to load sync muxer for video recorder";
  438. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  439. m_recordMuxer.Close();
  440. return false;
  441. }
  442. // 写入头部
  443. if (!m_recordMuxer.WriteHeader()) {
  444. qDebug() << "Failed to write sync muxer header";
  445. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  446. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  447. m_recordMuxer.Close();
  448. return false;
  449. }
  450. m_isSyncRecord = true;
  451. qDebug() << "Sync recording started successfully: " << QString::fromStdString(fileName);
  452. return true;
  453. }
  454. void AvRecorder::stopSyncRecord()
  455. {
  456. if (m_isSyncRecord) {
  457. // 先从录制器中卸载muxer
  458. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  459. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  460. // 然后关闭muxer
  461. m_recordMuxer.Close();
  462. m_isSyncRecord = false;
  463. }
  464. }
  465. void AvRecorder::updateCaptureList()
  466. {
  467. m_captureComboBox->clear();
  468. auto&& monitorList = MonitorFinder::GetList(true);
  469. for (auto&& monitor : monitorList) {
  470. QString text = "屏幕: " + QString::fromStdWString(monitor.title);
  471. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
  472. }
  473. auto&& windowList = WindowFinder::GetList(true);
  474. for (auto&& window : windowList) {
  475. QString text = "窗口: " + QString::fromStdWString(window.title);
  476. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
  477. }
  478. }
  479. void AvRecorder::initStatusBarUi()
  480. {
  481. m_videoEncodeLabel = new QLabel;
  482. auto hLayout = new QHBoxLayout;
  483. hLayout->setContentsMargins(0, 0, 0, 0);
  484. hLayout->addWidget(new QLabel("捕获方式:"));
  485. m_captureMethodBox = new QComboBox;
  486. hLayout->addWidget(m_captureMethodBox);
  487. m_captureStatusLabel = new QLabel("状态: 正常");
  488. m_captureTimeLabel = new QLabel("00:00:00");
  489. m_videolossRate = new QLabel("丢帧率: 00.00%");
  490. m_fpsLabel = new QLabel("FPS: 30");
  491. // 创建状态栏并添加到布局中
  492. m_statusBar = new QStatusBar(this);
  493. m_statusBar->setSizeGripEnabled(false);
  494. // 添加各个状态信息到状态栏
  495. m_statusBar->addWidget(m_videoEncodeLabel);
  496. auto widget = new QWidget;
  497. widget->setLayout(hLayout);
  498. m_statusBar->addWidget(widget);
  499. m_statusBar->addWidget(m_videolossRate);
  500. m_statusBar->addWidget(m_captureStatusLabel);
  501. m_statusBar->addWidget(m_captureTimeLabel);
  502. m_statusBar->addWidget(m_fpsLabel);
  503. }
  504. void AvRecorder::updateCaptureMethodBox(bool isMonitor) {
  505. m_captureMethodBox->clear();
  506. m_captureMethodBox->addItem("WGC");
  507. if (isMonitor) {
  508. m_captureMethodBox->addItem("DXGI");
  509. } else {
  510. m_captureMethodBox->addItem("GDI");
  511. }
  512. }
  513. // 捕获源切换时调用
  514. void AvRecorder::onCaptureSourceChanged() {
  515. int idx = m_captureComboBox->currentIndex();
  516. int monitorCnt = (int)MonitorFinder::GetList().size();
  517. bool isMonitor = (idx >= 0 && idx < monitorCnt);
  518. updateCaptureMethodBox(isMonitor);
  519. // 新增:推流/录制时切换采集源不中断
  520. if (m_isRecord || m_isLive) {
  521. CaptureMethod method = CaptureMethod::WGC;
  522. QString methodText = m_captureMethodBox->currentText();
  523. if (methodText == "DXGI") method = CaptureMethod::DXGI;
  524. else if (methodText == "GDI") method = CaptureMethod::GDI;
  525. if (isMonitor) {
  526. m_videoRecorder.SetCaptureSource(idx, method);
  527. } else {
  528. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  529. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  530. }
  531. }
  532. }