av_recorder.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. if (!startStream(fileName, format)) {
  138. qDebug() << "startStream failed for recording";
  139. return;
  140. }
  141. m_liveBtn->setEnabled(false);
  142. m_recordBtn->setText("停止录制");
  143. } else {
  144. stopStream();
  145. m_liveBtn->setEnabled(true);
  146. m_recordBtn->setText("开始录制");
  147. }
  148. m_isRecord = !m_isRecord;
  149. });
  150. connect(m_liveBtn, &QPushButton::released, this, [this] {
  151. if (!m_isLive) {
  152. auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
  153. bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
  154. qDebug() << "直播地址:" << QString::fromStdString(fileName);
  155. if (!startStream(fileName, isRtsp ? "rtsp" : "flv")) {
  156. qDebug() << "startStream failed for live";
  157. return;
  158. }
  159. // 如果勾选了同步录像,则开始录像
  160. if (m_syncRecordBox->isChecked()) {
  161. if (!startSyncRecord()) {
  162. qDebug() << "startSyncRecord failed";
  163. // 不阻断直播,仅提示
  164. }
  165. }
  166. m_recordBtn->setEnabled(false);
  167. m_liveBtn->setText("停止直播");
  168. } else {
  169. // 先停止同步录像
  170. stopSyncRecord();
  171. // 再停止直播
  172. stopStream();
  173. m_recordBtn->setEnabled(true);
  174. m_liveBtn->setText("开始直播");
  175. }
  176. });
  177. connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  178. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  179. });
  180. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  181. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  182. });
  183. connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
  184. connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  185. if (text.isEmpty() || m_isLocked) {
  186. return;
  187. }
  188. m_isLocked = true;
  189. if (!(m_isRecord || m_isLive)) {
  190. stopPreview();
  191. stopCapture();
  192. startCapture(CaptureMethod::WGC);
  193. startPreview();
  194. }
  195. m_isLocked = false;
  196. });
  197. connect(m_captureComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AvRecorder::onCaptureSourceChanged);
  198. connect(m_isDrawCursorBox, &QCheckBox::stateChanged, this, [this] {
  199. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  200. });
  201. connect(m_captureMethodBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  202. if (m_isLocked || text.isEmpty()) {
  203. return;
  204. }
  205. stopPreview();
  206. stopCapture();
  207. if (text == "WGC") {
  208. startCapture(CaptureMethod::WGC);
  209. } else if (text == "DXGI") {
  210. startCapture(CaptureMethod::DXGI);
  211. } else {
  212. startCapture(CaptureMethod::GDI);
  213. }
  214. startPreview();
  215. });
  216. connect(m_settingsBtn, &QPushButton::released, this, [this] {
  217. auto settingsPage = std::make_unique<SettingsPage>(&m_settingsParam, this);
  218. settingsPage->exec();
  219. m_isLocked = true;
  220. stopPreview();
  221. stopCapture();
  222. startCapture(CaptureMethod::WGC);
  223. startPreview();
  224. m_isLocked = false;
  225. });
  226. m_otherTimer.callOnTimeout([this] {
  227. if (windowState() == Qt::WindowMinimized) {
  228. return;
  229. }
  230. // 音频
  231. auto info = m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX);
  232. m_microphoneWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  233. info = m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX);
  234. m_speakerWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  235. // 状态栏
  236. if (m_isRecord || m_isLive) {
  237. int interval = m_recordTime.secsTo(QTime::currentTime());
  238. int sec = interval % 60;
  239. interval /= 60;
  240. int minute = interval % 60;
  241. int hour = interval / 60;
  242. m_captureTimeLabel->setText(QString("%1:%2:%3")
  243. .arg(hour, 2, 10, QChar('0'))
  244. .arg(minute, 2, 10, QChar('0'))
  245. .arg(sec, 2, 10, QChar('0')));
  246. auto lossRate = m_videoRecorder.GetLossRate();
  247. if (lossRate < 0) {
  248. m_videolossRate->setText("丢帧率: 统计中");
  249. } else {
  250. int num = lossRate * 10000;
  251. m_videolossRate->setText(QString("丢帧率: %1.%2%")
  252. .arg(num / 100, 2, 10, QChar('0'))
  253. .arg(num % 100, 2, 10, QChar('0')));
  254. }
  255. } else if (m_captureTimeLabel->text() != "00:00:00") {
  256. m_captureTimeLabel->setText("00:00:00");
  257. }
  258. });
  259. }
  260. AvRecorder::~AvRecorder()
  261. {
  262. stopSyncRecord();
  263. stopStream();
  264. stopPreview();
  265. stopCapture();
  266. }
  267. bool AvRecorder::start()
  268. {
  269. auto timer = new QTimer(this);
  270. connect(timer, &QTimer::timeout, this, [this, timer] {
  271. m_isLocked = true;
  272. stopPreview();
  273. stopCapture();
  274. startCapture(CaptureMethod::WGC);
  275. startPreview();
  276. m_isLocked = false;
  277. timer->stop();
  278. });
  279. timer->start(100);
  280. return true;
  281. }
  282. void AvRecorder::startCapture(CaptureMethod method)
  283. {
  284. int idx = m_captureComboBox->currentIndex();
  285. if (idx < 0) return;
  286. int monitorCnt = (int)MonitorFinder::GetList().size();
  287. QString type = (idx < monitorCnt) ? "monitor" : "window";
  288. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  289. bool ok = false;
  290. if (m_isRecord || m_isLive) {
  291. // 推流/录制时,安全切换采集源
  292. if (idx < monitorCnt) {
  293. m_videoRecorder.SetCaptureSource(idx, method);
  294. ok = true;
  295. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  296. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  297. ok = true;
  298. }
  299. } else {
  300. // 未推流/录制时,正常 open
  301. if (idx < monitorCnt) { // 捕获屏幕
  302. ok = m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  303. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  304. ok = m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
  305. }
  306. }
  307. if (!ok) {
  308. // 可选:弹窗或日志提示
  309. return;
  310. }
  311. dealCapture();
  312. m_isDrawCursorBox->setEnabled(true);
  313. m_recordBtn->setEnabled(true);
  314. m_liveBtn->setEnabled(true);
  315. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  316. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  317. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  318. }
  319. void AvRecorder::dealCapture()
  320. {
  321. if (!m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  322. m_settingsParam.audioParam)) {
  323. qDebug() << "AudioRecorder::Open failed";
  324. return;
  325. }
  326. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  327. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  328. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  329. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  330. if (m_audioEncodeLabel) {
  331. m_audioEncodeLabel->setText(("音频编码器: " + m_settingsParam.audioParam.name).c_str());
  332. }
  333. }
  334. void AvRecorder::stopCapture()
  335. {
  336. m_videoRecorder.Close();
  337. m_audioRecorder.Close();
  338. }
  339. void AvRecorder::renderFrame()
  340. {
  341. auto frame = m_videoRecorder.GetRenderFrame();
  342. AVFrame* copiedFrame = av_frame_clone(frame);
  343. // m_glWidget->Render(copiedFrame);
  344. QMetaObject::invokeMethod(m_glWidget,
  345. "Render",
  346. Qt::QueuedConnection,
  347. Q_ARG(AVFrame*, copiedFrame));
  348. }
  349. void AvRecorder::startPreview()
  350. {
  351. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  352. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  353. // 需要自己封装一个计时器
  354. // m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  355. // if (windowState() == Qt::WindowMinimized) {
  356. // return;
  357. // }
  358. // QMetaObject::invokeMethod(this, "renderFrame", Qt::QueuedConnection);
  359. // // // 视频
  360. // // auto frame = m_videoRecorder.GetRenderFrame();
  361. // // m_glWidget->Render(frame);
  362. // });
  363. if (!m_videoRenderTimer) {
  364. m_videoRenderTimer = new QTimer(this);
  365. connect(m_videoRenderTimer, &QTimer::timeout, this, [this] {
  366. if (windowState() == Qt::WindowMinimized)
  367. return;
  368. renderFrame();
  369. });
  370. }
  371. m_videoRenderTimer->start(1000 / m_settingsParam.videoParam.fps);
  372. // 刷新率设置为 25
  373. m_otherTimer.start(40);
  374. }
  375. void AvRecorder::stopPreview()
  376. {
  377. if (m_videoRenderTimer) {
  378. m_videoRenderTimer->stop();
  379. }
  380. m_otherTimer.stop();
  381. }
  382. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  383. {
  384. if (!m_avMuxer.Open(path, format)) {
  385. qDebug() << "Failed to open muxer with path:" << QString::fromStdString(std::string(path)) << "format:" << QString::fromStdString(std::string(format));
  386. return false;
  387. }
  388. if (!m_audioRecorder.LoadMuxer(m_avMuxer)) {
  389. qDebug() << "Failed to load muxer for audio recorder";
  390. return false;
  391. }
  392. if (!m_videoRecorder.LoadMuxer(m_avMuxer)) {
  393. qDebug() << "Failed to load muxer for video recorder";
  394. return false;
  395. }
  396. if (!m_avMuxer.WriteHeader()) {
  397. qDebug() << "Failed to write muxer header";
  398. return false;
  399. }
  400. if (!m_audioRecorder.StartRecord()) {
  401. qDebug() << "Failed to start audio recording";
  402. return false;
  403. }
  404. if (!m_videoRecorder.StartRecord()) {
  405. qDebug() << "Failed to start video recording";
  406. return false;
  407. }
  408. // 同步:展示实际使用的编码器名称(考虑自动回退后的结果)
  409. {
  410. std::string used = m_videoRecorder.GetEncoderNameForMuxer(m_avMuxer);
  411. if (!used.empty()) {
  412. m_videoEncodeLabel->setText((std::string("编码器: ") + used).c_str());
  413. }
  414. std::string aused = m_audioRecorder.GetEncoderNameForMuxer(m_avMuxer);
  415. if (!aused.empty()) {
  416. m_audioEncodeLabel->setText((std::string("音频编码器: ") + aused).c_str());
  417. }
  418. }
  419. m_recordTime = QTime::currentTime();
  420. m_captureStatusLabel->setText("状态: 正在工作");
  421. m_settingsBtn->setEnabled(false);
  422. m_captureComboBox->setEnabled(false); // 禁用采集源切换
  423. m_syncRecordBox->setEnabled(false);
  424. m_updateListBtn->setEnabled(false);
  425. m_captureMethodBox->setEnabled(false); // 禁用采集方式切换
  426. m_isLive = !m_isLive;
  427. return true;
  428. }
  429. void AvRecorder::stopStream()
  430. {
  431. m_audioRecorder.StopRecord();
  432. m_videoRecorder.StopRecord();
  433. // 从录制器中卸载直播muxer
  434. m_audioRecorder.UnloadMuxer(m_avMuxer);
  435. m_videoRecorder.UnloadMuxer(m_avMuxer);
  436. m_avMuxer.Close();
  437. // 如果有同步录像,也需要关闭
  438. if (m_isSyncRecord) {
  439. // 先从录制器中卸载同步录像muxer
  440. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  441. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  442. m_recordMuxer.Close();
  443. m_isSyncRecord = false;
  444. }
  445. m_captureStatusLabel->setText("状态: 正常");
  446. m_settingsBtn->setEnabled(true);
  447. m_captureComboBox->setEnabled(true); // 恢复采集源切换
  448. m_syncRecordBox->setEnabled(true);
  449. m_updateListBtn->setEnabled(true);
  450. m_captureMethodBox->setEnabled(true); // 恢复采集方式切换
  451. }
  452. bool AvRecorder::startSyncRecord()
  453. {
  454. // 检查是否已经在同步录像
  455. if (m_isSyncRecord) {
  456. qDebug() << "Sync recording is already active";
  457. return true;
  458. }
  459. // 检查是否正在直播(必须在直播状态下才能启动同步录像)
  460. if (!m_isLive) {
  461. qDebug() << "Cannot start sync recording: not in live streaming mode";
  462. return false;
  463. }
  464. auto fileName = m_settingsParam.outputDir;
  465. if (fileName.back() != '\\') {
  466. fileName.push_back('\\');
  467. }
  468. auto format = "mp4";
  469. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  470. + "_sync." + format;
  471. // 打开同步录像的muxer
  472. if (!m_recordMuxer.Open(fileName, format)) {
  473. qDebug() << "Failed to open sync record muxer";
  474. return false;
  475. }
  476. // 加载muxer到录制器
  477. if (!m_audioRecorder.LoadMuxer(m_recordMuxer)) {
  478. qDebug() << "Failed to load sync muxer for audio recorder";
  479. m_recordMuxer.Close();
  480. return false;
  481. }
  482. if (!m_videoRecorder.LoadMuxer(m_recordMuxer)) {
  483. qDebug() << "Failed to load sync muxer for video recorder";
  484. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  485. m_recordMuxer.Close();
  486. return false;
  487. }
  488. // 写入头部
  489. if (!m_recordMuxer.WriteHeader()) {
  490. qDebug() << "Failed to write sync muxer header";
  491. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  492. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  493. m_recordMuxer.Close();
  494. return false;
  495. }
  496. m_isSyncRecord = true;
  497. qDebug() << "Sync recording started successfully: " << QString::fromStdString(fileName);
  498. return true;
  499. }
  500. void AvRecorder::stopSyncRecord()
  501. {
  502. if (m_isSyncRecord) {
  503. // 先从录制器中卸载muxer
  504. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  505. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  506. // 然后关闭muxer
  507. m_recordMuxer.Close();
  508. m_isSyncRecord = false;
  509. }
  510. }
  511. void AvRecorder::updateCaptureList()
  512. {
  513. m_captureComboBox->clear();
  514. auto&& monitorList = MonitorFinder::GetList(true);
  515. for (auto&& monitor : monitorList) {
  516. QString text = "屏幕: " + QString::fromStdWString(monitor.title);
  517. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
  518. }
  519. auto&& windowList = WindowFinder::GetList(true);
  520. for (auto&& window : windowList) {
  521. QString text = "窗口: " + QString::fromStdWString(window.title);
  522. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
  523. }
  524. }
  525. void AvRecorder::initStatusBarUi()
  526. {
  527. m_videoEncodeLabel = new QLabel;
  528. m_audioEncodeLabel = new QLabel;
  529. auto hLayout = new QHBoxLayout;
  530. hLayout->setContentsMargins(0, 0, 0, 0);
  531. hLayout->addWidget(new QLabel("捕获方式:"));
  532. m_captureMethodBox = new QComboBox;
  533. hLayout->addWidget(m_captureMethodBox);
  534. m_captureStatusLabel = new QLabel("状态: 正常");
  535. m_captureTimeLabel = new QLabel("00:00:00");
  536. m_videolossRate = new QLabel("丢帧率: 00.00%");
  537. m_fpsLabel = new QLabel("FPS: 30");
  538. // 创建状态栏并添加到布局中
  539. m_statusBar = new QStatusBar(this);
  540. m_statusBar->setSizeGripEnabled(false);
  541. // 添加各个状态信息到状态栏
  542. m_statusBar->addWidget(m_videoEncodeLabel);
  543. m_statusBar->addWidget(m_audioEncodeLabel);
  544. auto widget = new QWidget;
  545. widget->setLayout(hLayout);
  546. m_statusBar->addWidget(widget);
  547. m_statusBar->addWidget(m_videolossRate);
  548. m_statusBar->addWidget(m_captureStatusLabel);
  549. m_statusBar->addWidget(m_captureTimeLabel);
  550. m_statusBar->addWidget(m_fpsLabel);
  551. }
  552. void AvRecorder::updateCaptureMethodBox(bool isMonitor) {
  553. m_captureMethodBox->clear();
  554. m_captureMethodBox->addItem("WGC");
  555. if (isMonitor) {
  556. m_captureMethodBox->addItem("DXGI");
  557. } else {
  558. m_captureMethodBox->addItem("GDI");
  559. }
  560. }
  561. // 捕获源切换时调用
  562. void AvRecorder::onCaptureSourceChanged() {
  563. int idx = m_captureComboBox->currentIndex();
  564. int monitorCnt = (int)MonitorFinder::GetList().size();
  565. bool isMonitor = (idx >= 0 && idx < monitorCnt);
  566. updateCaptureMethodBox(isMonitor);
  567. // 新增:推流/录制时切换采集源不中断
  568. if (m_isRecord || m_isLive) {
  569. CaptureMethod method = CaptureMethod::WGC;
  570. QString methodText = m_captureMethodBox->currentText();
  571. if (methodText == "DXGI") method = CaptureMethod::DXGI;
  572. else if (methodText == "GDI") method = CaptureMethod::GDI;
  573. if (isMonitor) {
  574. m_videoRecorder.SetCaptureSource(idx, method);
  575. } else {
  576. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  577. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  578. }
  579. }
  580. }