av_recorder.cpp 22 KB

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