av_recorder.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. #include "av_recorder.h"
  2. #include <QAction>
  3. #include <QDateTime>
  4. #include <QMenu>
  5. #include <QMessageBox>
  6. #include <QStatusBar>
  7. #include <QTimer>
  8. #include <capturer/finder.h>
  9. using namespace avrecorder::video;
  10. AvRecorder::AvRecorder(QWidget* parent)
  11. : QWidget(parent)
  12. {
  13. m_settingsParam.audioParam.bitRate = 160'000;
  14. m_settingsParam.videoParam.bitRate = 8'000'000;
  15. m_settingsParam.videoParam.fps = 30;
  16. {
  17. const auto& encs = Encoder<MediaType::VIDEO>::GetUsableEncoders();
  18. m_settingsParam.videoParam.name = encs.empty() ? std::string("libx264") : encs.front();
  19. }
  20. {
  21. const auto& aencs = Encoder<MediaType::AUDIO>::GetUsableEncoders();
  22. m_settingsParam.audioParam.name = aencs.empty() ? std::string("aac") : aencs.front();
  23. }
  24. // 设置安全的默认分辨率,避免在捕获首帧前width/height为0
  25. m_settingsParam.videoParam.width = 1920;
  26. m_settingsParam.videoParam.height = 1080;
  27. m_settingsParam.outputDir = ".";
  28. m_settingsParam.liveUrl = "rtmp://127.0.0.1:1935/stream/V1";
  29. m_settingsParam.liveName = "stream";
  30. // 1. 视频预览区
  31. m_glWidget = new OpenGLVideoWidget(this);
  32. // 2. 音频区
  33. m_microphoneWidget = new AudioWidget;
  34. m_speakerWidget = new AudioWidget;
  35. m_microphoneWidget->SetName("麦克风");
  36. m_speakerWidget->SetName("扬声器");
  37. // 3. 捕获区
  38. m_captureComboBox = new QComboBox;
  39. m_updateListBtn = new QPushButton("刷新窗口列表");
  40. QGroupBox* captureGroup = new QGroupBox("捕获源");
  41. QHBoxLayout* captureRow = new QHBoxLayout;
  42. captureRow->addWidget(m_captureComboBox);
  43. captureRow->addWidget(m_updateListBtn);
  44. captureGroup->setLayout(captureRow);
  45. // m_captureComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
  46. m_captureComboBox->setMinimumWidth(20);
  47. // 4. 音频区分组
  48. QGroupBox* audioGroup = new QGroupBox("音频");
  49. QVBoxLayout* audioLayout = new QVBoxLayout;
  50. audioLayout->addWidget(m_microphoneWidget);
  51. audioLayout->addWidget(m_speakerWidget);
  52. // 添加音频设备选择按钮
  53. m_audioDeviceBtn = new QPushButton("选择音频设备");
  54. m_audioDeviceBtn->setToolTip("打开音频设备选择窗口");
  55. audioLayout->addWidget(m_audioDeviceBtn);
  56. audioGroup->setLayout(audioLayout);
  57. //audioGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  58. // 5. 操作区
  59. m_isDrawCursorBox = new QCheckBox("绘制鼠标指针");
  60. m_isDrawCursorBox->setChecked(true);
  61. m_isDrawCursorBox->setEnabled(false);
  62. m_syncRecordBox = new QCheckBox("直播时同步录像");
  63. m_syncRecordBox->setChecked(false);
  64. m_recordBtn = new QPushButton("开始录制");
  65. m_recordBtn->setEnabled(false);
  66. m_liveBtn = new QPushButton("开始直播");
  67. m_liveBtn->setEnabled(false);
  68. m_settingsBtn = new QPushButton("设置");
  69. QGroupBox* actionGroup = new QGroupBox("操作");
  70. QVBoxLayout* actionLayout = new QVBoxLayout;
  71. QHBoxLayout* checkBoxRow = new QHBoxLayout;
  72. checkBoxRow->setContentsMargins(0, 0, 0, 0);
  73. checkBoxRow->setSpacing(8);
  74. checkBoxRow->addWidget(m_syncRecordBox);
  75. checkBoxRow->addWidget(m_isDrawCursorBox);
  76. actionLayout->addLayout(checkBoxRow);
  77. actionLayout->addWidget(m_recordBtn);
  78. actionLayout->addWidget(m_liveBtn);
  79. actionGroup->setLayout(actionLayout);
  80. //actionGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  81. // 6. 设置区
  82. QHBoxLayout* utilLayout = new QHBoxLayout;
  83. utilLayout->addWidget(m_settingsBtn);
  84. // 7. 左侧功能区(捕获区在上,音频区在下)
  85. QVBoxLayout* leftLayout = new QVBoxLayout;
  86. leftLayout->addWidget(captureGroup);
  87. leftLayout->addWidget(audioGroup);
  88. leftLayout->addStretch();
  89. // 8. 右侧功能区
  90. QVBoxLayout* rightLayout = new QVBoxLayout;
  91. rightLayout->addWidget(actionGroup);
  92. rightLayout->addLayout(utilLayout);
  93. rightLayout->addStretch();
  94. // 9. 中部主布局
  95. QHBoxLayout* centerLayout = new QHBoxLayout;
  96. centerLayout->addLayout(leftLayout, 2);
  97. centerLayout->addLayout(rightLayout, 3);
  98. // 10. 功能键区域
  99. m_openBtn = new QPushButton("打开");
  100. m_saveBtn = new QPushButton("保存");
  101. m_shareBtn = new QPushButton("共享屏幕");
  102. m_loginBtn = new QPushButton("登录");
  103. m_meetingBtn = new QPushButton("会议");
  104. m_aiBtn = new QPushButton("AI助理");
  105. m_appBtn = new QPushButton("应用");
  106. m_conferenceBtn = new QPushButton("线程会议");
  107. // 设置功能键样式
  108. QList<QPushButton*> functionBtns = {m_openBtn, m_saveBtn, m_shareBtn, m_loginBtn,
  109. m_meetingBtn, m_aiBtn, m_appBtn, m_conferenceBtn};
  110. for (auto* btn : functionBtns) {
  111. btn->setMinimumHeight(35);
  112. btn->setMaximumHeight(35);
  113. btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  114. }
  115. QHBoxLayout* functionLayout = new QHBoxLayout;
  116. functionLayout->setSpacing(8);
  117. functionLayout->setContentsMargins(10, 5, 10, 5);
  118. functionLayout->addWidget(m_openBtn);
  119. functionLayout->addWidget(m_saveBtn);
  120. functionLayout->addWidget(m_shareBtn);
  121. functionLayout->addWidget(m_loginBtn);
  122. functionLayout->addWidget(m_meetingBtn);
  123. functionLayout->addWidget(m_aiBtn);
  124. functionLayout->addWidget(m_appBtn);
  125. functionLayout->addWidget(m_conferenceBtn);
  126. // 11. 状态栏
  127. initStatusBarUi();
  128. // 让视频区尽量大
  129. // m_glWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  130. // 12. 总体布局
  131. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  132. mainLayout->addWidget(m_glWidget, 100);
  133. mainLayout->addLayout(functionLayout, 0); // 添加功能键区域
  134. mainLayout->addLayout(centerLayout, 0);
  135. mainLayout->addWidget(m_statusBar, 0);
  136. setLayout(mainLayout);
  137. // 12. 连接信号槽、初始化数据
  138. initConnect();
  139. updateCaptureList();
  140. }
  141. void AvRecorder::setSettings(const SettingsPage::Param& param)
  142. {
  143. m_settingsParam.audioParam.bitRate = 160'000;
  144. m_settingsParam.videoParam.bitRate = 8'000'000;
  145. m_settingsParam.videoParam.fps = 30;
  146. {
  147. const auto& encs = Encoder<MediaType::VIDEO>::GetUsableEncoders();
  148. m_settingsParam.videoParam.name = encs.empty() ? std::string("libx264") : encs.front();
  149. }
  150. {
  151. const auto& aencs = Encoder<MediaType::AUDIO>::GetUsableEncoders();
  152. m_settingsParam.audioParam.name = aencs.empty() ? std::string("aac") : aencs.front();
  153. }
  154. // 重置时也保持安全默认分辨率,实际分辨率会在捕获后更新
  155. m_settingsParam.videoParam.width = 1920;
  156. m_settingsParam.videoParam.height = 1080;
  157. m_settingsParam.outputDir = ".";
  158. m_settingsParam.liveUrl = param.liveUrl; // "rtmp://192.168.3.76:1935/stream/V1";
  159. m_settingsParam.liveName = param.liveName; // "stream";
  160. }
  161. void AvRecorder::initConnect()
  162. {
  163. connect(m_recordBtn, &QPushButton::released, this, [this] {
  164. if (!m_isRecord) {
  165. auto fileName = m_settingsParam.outputDir;
  166. if (fileName.back() != '\\') {
  167. fileName.push_back('\\');
  168. }
  169. auto format = "mp4";
  170. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  171. + "." + format;
  172. // fileName += std::string("test.") + format;
  173. if (!startStream(fileName, format)) {
  174. qDebug() << "startStream failed for recording";
  175. return;
  176. }
  177. m_liveBtn->setEnabled(false);
  178. m_recordBtn->setText("停止录制");
  179. } else {
  180. stopStream();
  181. m_liveBtn->setEnabled(true);
  182. m_recordBtn->setText("开始录制");
  183. }
  184. m_isRecord = !m_isRecord;
  185. });
  186. connect(m_liveBtn, &QPushButton::released, this, [this] {
  187. if (!m_isLive) {
  188. auto fileName = m_settingsParam.liveUrl + "/" + m_settingsParam.liveName;
  189. bool isRtsp = m_settingsParam.liveUrl.find("rtsp") != std::string::npos;
  190. qDebug() << "直播地址:" << QString::fromStdString(fileName);
  191. if (!startStream(fileName, isRtsp ? "rtsp" : "flv")) {
  192. qDebug() << "startStream failed for live";
  193. return;
  194. }
  195. // 如果勾选了同步录像,则开始录像
  196. if (m_syncRecordBox->isChecked()) {
  197. if (!startSyncRecord()) {
  198. qDebug() << "startSyncRecord failed";
  199. // 不阻断直播,仅提示
  200. }
  201. }
  202. m_recordBtn->setEnabled(false);
  203. m_liveBtn->setText("停止直播");
  204. // 显式设置直播状态
  205. m_isLive = true;
  206. } else {
  207. // 先停止同步录像
  208. stopSyncRecord();
  209. // 再停止直播
  210. stopStream();
  211. m_recordBtn->setEnabled(true);
  212. m_liveBtn->setText("开始直播");
  213. // 显式清除直播状态
  214. m_isLive = false;
  215. }
  216. });
  217. connect(m_microphoneWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  218. m_audioRecorder.SetVolumeScale(scale, MICROPHONE_INDEX);
  219. });
  220. connect(m_speakerWidget, &AudioWidget::SetVolumeScale, this, [this](float scale) {
  221. m_audioRecorder.SetVolumeScale(scale, SPEAKER_INDEX);
  222. });
  223. connect(m_updateListBtn, &QPushButton::released, this, [this] { updateCaptureList(); });
  224. // 连接音频设备选择按钮
  225. connect(m_audioDeviceBtn, &QPushButton::clicked, this, &AvRecorder::showAudioDeviceMenu);
  226. connect(m_captureComboBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  227. if (text.isEmpty() || m_isLocked) {
  228. return;
  229. }
  230. m_isLocked = true;
  231. if (!(m_isRecord || m_isLive)) {
  232. stopPreview();
  233. stopCapture();
  234. startCapture(CaptureMethod::WGC);
  235. startPreview();
  236. }
  237. m_isLocked = false;
  238. });
  239. connect(m_captureComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AvRecorder::onCaptureSourceChanged);
  240. connect(m_isDrawCursorBox, &QCheckBox::stateChanged, this, [this] {
  241. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  242. });
  243. connect(m_captureMethodBox, &QComboBox::currentTextChanged, this, [this](const QString& text) {
  244. if (m_isLocked || text.isEmpty()) {
  245. return;
  246. }
  247. stopPreview();
  248. stopCapture();
  249. if (text == "WGC") {
  250. startCapture(CaptureMethod::WGC);
  251. } else if (text == "DXGI") {
  252. startCapture(CaptureMethod::DXGI);
  253. } else {
  254. startCapture(CaptureMethod::GDI);
  255. }
  256. startPreview();
  257. });
  258. connect(m_settingsBtn, &QPushButton::released, this, [this] {
  259. // 运行中禁止打开设置以切换编码器,避免异常
  260. if (m_isRecord || m_isLive || m_isSyncRecord) {
  261. QMessageBox::warning(this, "提示", "正在直播/录制,无法修改编码器设置。请先停止后再修改。");
  262. return;
  263. }
  264. auto settingsPage = std::make_unique<SettingsPage>(&m_settingsParam, this);
  265. settingsPage->exec();
  266. m_isLocked = true;
  267. stopPreview();
  268. stopCapture();
  269. startCapture(CaptureMethod::WGC);
  270. startPreview();
  271. m_isLocked = false;
  272. });
  273. m_otherTimer.callOnTimeout([this] {
  274. if (windowState() == Qt::WindowMinimized) {
  275. return;
  276. }
  277. // 音频
  278. auto info = m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX);
  279. m_microphoneWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  280. info = m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX);
  281. m_speakerWidget->ShowVolume(info == nullptr ? 0 : info->volume);
  282. // 状态栏
  283. if (m_isRecord || m_isLive) {
  284. int interval = m_recordTime.secsTo(QTime::currentTime());
  285. int sec = interval % 60;
  286. interval /= 60;
  287. int minute = interval % 60;
  288. int hour = interval / 60;
  289. m_captureTimeLabel->setText(QString("%1:%2:%3")
  290. .arg(hour, 2, 10, QChar('0'))
  291. .arg(minute, 2, 10, QChar('0'))
  292. .arg(sec, 2, 10, QChar('0')));
  293. auto lossRate = m_videoRecorder.GetLossRate();
  294. if (lossRate < 0) {
  295. m_videolossRate->setText("丢帧率: 统计中");
  296. } else {
  297. int num = lossRate * 10000;
  298. m_videolossRate->setText(QString("丢帧率: %1.%2%")
  299. .arg(num / 100, 2, 10, QChar('0'))
  300. .arg(num % 100, 2, 10, QChar('0')));
  301. }
  302. } else if (m_captureTimeLabel->text() != "00:00:00") {
  303. m_captureTimeLabel->setText("00:00:00");
  304. }
  305. });
  306. // 连接功能键信号槽
  307. connect(m_openBtn, &QPushButton::clicked, this, [this] {
  308. QMessageBox::information(this, "提示", "打开功能");
  309. });
  310. connect(m_saveBtn, &QPushButton::clicked, this, [this] {
  311. QMessageBox::information(this, "提示", "保存功能");
  312. });
  313. connect(m_shareBtn, &QPushButton::clicked, this, [this] {
  314. QMessageBox::information(this, "提示", "共享屏幕功能");
  315. });
  316. connect(m_loginBtn, &QPushButton::clicked, this, [this] {
  317. QMessageBox::information(this, "提示", "登录功能");
  318. });
  319. connect(m_meetingBtn, &QPushButton::clicked, this, [this] {
  320. QMessageBox::information(this, "提示", "会议功能");
  321. });
  322. connect(m_aiBtn, &QPushButton::clicked, this, [this] {
  323. QMessageBox::information(this, "提示", "AI助理功能");
  324. });
  325. connect(m_appBtn, &QPushButton::clicked, this, [this] {
  326. QMessageBox::information(this, "提示", "应用功能");
  327. });
  328. connect(m_conferenceBtn, &QPushButton::clicked, this, [this] {
  329. QMessageBox::information(this, "提示", "线程会议功能");
  330. });
  331. }
  332. AvRecorder::~AvRecorder()
  333. {
  334. stopSyncRecord();
  335. stopStream();
  336. stopPreview();
  337. stopCapture();
  338. }
  339. bool AvRecorder::start()
  340. {
  341. auto timer = new QTimer(this);
  342. connect(timer, &QTimer::timeout, this, [this, timer] {
  343. m_isLocked = true;
  344. stopPreview();
  345. stopCapture();
  346. startCapture(CaptureMethod::WGC);
  347. startPreview();
  348. m_isLocked = false;
  349. timer->stop();
  350. });
  351. timer->start(100);
  352. return true;
  353. }
  354. void AvRecorder::startCapture(CaptureMethod method)
  355. {
  356. int idx = m_captureComboBox->currentIndex();
  357. if (idx < 0) return;
  358. int monitorCnt = (int)MonitorFinder::GetList().size();
  359. QString type = (idx < monitorCnt) ? "monitor" : "window";
  360. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  361. bool ok = false;
  362. if (m_isRecord || m_isLive) {
  363. // 推流/录制时,安全切换采集源
  364. if (idx < monitorCnt) {
  365. m_videoRecorder.SetCaptureSource(idx, method);
  366. ok = true;
  367. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  368. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  369. ok = true;
  370. }
  371. } else {
  372. // 未推流/录制时,正常 open
  373. if (idx < monitorCnt) { // 捕获屏幕
  374. ok = m_videoRecorder.Open(idx, m_settingsParam.videoParam, method);
  375. } else if (type == "window" && ::IsWindow((HWND)ptrHwnd)) {
  376. ok = m_videoRecorder.Open((HWND)ptrHwnd, m_settingsParam.videoParam, method);
  377. }
  378. }
  379. if (!ok) {
  380. // 可选:弹窗或日志提示
  381. return;
  382. }
  383. dealCapture();
  384. m_isDrawCursorBox->setEnabled(true);
  385. m_recordBtn->setEnabled(true);
  386. m_liveBtn->setEnabled(true);
  387. m_videoRecorder.SetIsDrawCursor(m_isDrawCursorBox->isChecked());
  388. m_audioRecorder.SetVolumeScale(m_microphoneWidget->GetVolume(), MICROPHONE_INDEX);
  389. m_audioRecorder.SetVolumeScale(m_speakerWidget->GetVolume(), SPEAKER_INDEX);
  390. }
  391. void AvRecorder::dealCapture()
  392. {
  393. if (!m_audioRecorder.Open({AudioCapturer::Microphone, AudioCapturer::Speaker},
  394. m_settingsParam.audioParam)) {
  395. qDebug() << "AudioRecorder::Open failed";
  396. return;
  397. }
  398. m_microphoneWidget->setEnabled(m_audioRecorder.GetCaptureInfo(MICROPHONE_INDEX) != nullptr);
  399. m_speakerWidget->setEnabled(m_audioRecorder.GetCaptureInfo(SPEAKER_INDEX) != nullptr);
  400. m_fpsLabel->setText(QString("FPS: %1").arg(m_settingsParam.videoParam.fps));
  401. m_videoEncodeLabel->setText(("编码器: " + m_settingsParam.videoParam.name).c_str());
  402. if (m_audioEncodeLabel) {
  403. m_audioEncodeLabel->setText(("音频编码器: " + m_settingsParam.audioParam.name).c_str());
  404. }
  405. }
  406. void AvRecorder::stopCapture()
  407. {
  408. m_videoRecorder.Close();
  409. m_audioRecorder.Close();
  410. }
  411. void AvRecorder::renderFrame()
  412. {
  413. auto frame = m_videoRecorder.GetRenderFrame();
  414. if (!frame) {
  415. qDebug() << "renderFrame error";
  416. return;
  417. }
  418. AVFrame* copiedFrame = av_frame_clone(frame);
  419. // m_glWidget->Render(copiedFrame);
  420. QMetaObject::invokeMethod(m_glWidget,
  421. "Render",
  422. Qt::QueuedConnection,
  423. Q_ARG(AVFrame*, copiedFrame));
  424. }
  425. void AvRecorder::startPreview()
  426. {
  427. m_glWidget->Open(m_settingsParam.videoParam.width, m_settingsParam.videoParam.height);
  428. // 视频需要做到和帧率一样的渲染速度,QTimer 达不到要求
  429. // 需要自己封装一个计时器
  430. // m_videoRenderTimer.Start(m_settingsParam.videoParam.fps, [this] {
  431. // if (windowState() == Qt::WindowMinimized) {
  432. // return;
  433. // }
  434. // QMetaObject::invokeMethod(this, "renderFrame", Qt::QueuedConnection);
  435. // // // 视频
  436. // // auto frame = m_videoRecorder.GetRenderFrame();
  437. // // m_glWidget->Render(frame);
  438. // });
  439. if (!m_videoRenderTimer) {
  440. m_videoRenderTimer = new QTimer(this);
  441. connect(m_videoRenderTimer, &QTimer::timeout, this, [this] {
  442. if (windowState() == Qt::WindowMinimized)
  443. return;
  444. renderFrame();
  445. });
  446. }
  447. m_videoRenderTimer->start(1000 / m_settingsParam.videoParam.fps);
  448. // 刷新率设置为 25
  449. m_otherTimer.start(40);
  450. }
  451. void AvRecorder::stopPreview()
  452. {
  453. if (m_videoRenderTimer) {
  454. m_videoRenderTimer->stop();
  455. }
  456. m_otherTimer.stop();
  457. }
  458. bool AvRecorder::startStream(std::string_view path, std::string_view format)
  459. {
  460. if (!m_avMuxer.Open(path, format)) {
  461. qDebug() << "Failed to open muxer with path:" << QString::fromStdString(std::string(path)) << "format:" << QString::fromStdString(std::string(format));
  462. return false;
  463. }
  464. if (!m_audioRecorder.LoadMuxer(m_avMuxer)) {
  465. qDebug() << "Failed to load muxer for audio recorder";
  466. return false;
  467. }
  468. if (!m_videoRecorder.LoadMuxer(m_avMuxer)) {
  469. qDebug() << "Failed to load muxer for video recorder";
  470. return false;
  471. }
  472. if (!m_avMuxer.WriteHeader()) {
  473. qDebug() << "Failed to write muxer header";
  474. return false;
  475. }
  476. if (!m_audioRecorder.StartRecord()) {
  477. qDebug() << "Failed to start audio recording";
  478. return false;
  479. }
  480. if (!m_videoRecorder.StartRecord()) {
  481. qDebug() << "Failed to start video recording";
  482. return false;
  483. }
  484. // 同步:展示实际使用的编码器名称(考虑自动回退后的结果)
  485. {
  486. std::string used = m_videoRecorder.GetEncoderNameForMuxer(m_avMuxer);
  487. if (!used.empty()) {
  488. m_videoEncodeLabel->setText((std::string("编码器: ") + used).c_str());
  489. }
  490. std::string aused = m_audioRecorder.GetEncoderNameForMuxer(m_avMuxer);
  491. if (!aused.empty()) {
  492. m_audioEncodeLabel->setText((std::string("音频编码器: ") + aused).c_str());
  493. }
  494. }
  495. m_recordTime = QTime::currentTime();
  496. m_captureStatusLabel->setText("状态: 正在工作");
  497. m_settingsBtn->setEnabled(false);
  498. m_captureComboBox->setEnabled(false); // 禁用采集源切换
  499. m_syncRecordBox->setEnabled(false);
  500. m_updateListBtn->setEnabled(false);
  501. m_captureMethodBox->setEnabled(false); // 禁用采集方式切换
  502. // 注意:不要在这里修改 m_isLive,由调用方(按钮逻辑)根据场景设置
  503. return true;
  504. }
  505. void AvRecorder::stopStream()
  506. {
  507. m_audioRecorder.StopRecord();
  508. m_videoRecorder.StopRecord();
  509. // 从录制器中卸载直播muxer
  510. m_audioRecorder.UnloadMuxer(m_avMuxer);
  511. m_videoRecorder.UnloadMuxer(m_avMuxer);
  512. m_avMuxer.Close();
  513. // 如果有同步录像,也需要关闭
  514. if (m_isSyncRecord) {
  515. // 先从录制器中卸载同步录像muxer
  516. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  517. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  518. m_recordMuxer.Close();
  519. m_isSyncRecord = false;
  520. }
  521. m_captureStatusLabel->setText("状态: 正常");
  522. m_settingsBtn->setEnabled(true);
  523. m_captureComboBox->setEnabled(true); // 恢复采集源切换
  524. m_syncRecordBox->setEnabled(true);
  525. m_updateListBtn->setEnabled(true);
  526. m_captureMethodBox->setEnabled(true); // 恢复采集方式切换
  527. }
  528. bool AvRecorder::startSyncRecord()
  529. {
  530. // 检查是否已经在同步录像
  531. if (m_isSyncRecord) {
  532. qDebug() << "Sync recording is already active";
  533. return true;
  534. }
  535. // 检查是否正在直播(必须在直播状态下才能启动同步录像)
  536. if (!m_isLive) {
  537. qDebug() << "Cannot start sync recording: not in live streaming mode";
  538. return false;
  539. }
  540. auto fileName = m_settingsParam.outputDir;
  541. if (fileName.back() != '\\') {
  542. fileName.push_back('\\');
  543. }
  544. auto format = "mp4";
  545. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").toStdString()
  546. + "_sync." + format;
  547. // 打开同步录像的muxer
  548. if (!m_recordMuxer.Open(fileName, format)) {
  549. qDebug() << "Failed to open sync record muxer";
  550. return false;
  551. }
  552. // 加载muxer到录制器
  553. if (!m_audioRecorder.LoadMuxer(m_recordMuxer)) {
  554. qDebug() << "Failed to load sync muxer for audio recorder";
  555. m_recordMuxer.Close();
  556. return false;
  557. }
  558. if (!m_videoRecorder.LoadMuxer(m_recordMuxer)) {
  559. qDebug() << "Failed to load sync muxer for video recorder";
  560. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  561. m_recordMuxer.Close();
  562. return false;
  563. }
  564. // 写入头部
  565. if (!m_recordMuxer.WriteHeader()) {
  566. qDebug() << "Failed to write sync muxer header";
  567. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  568. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  569. m_recordMuxer.Close();
  570. return false;
  571. }
  572. m_isSyncRecord = true;
  573. qDebug() << "Sync recording started successfully: " << QString::fromStdString(fileName);
  574. return true;
  575. }
  576. void AvRecorder::stopSyncRecord()
  577. {
  578. if (m_isSyncRecord) {
  579. // 先从录制器中卸载muxer
  580. m_audioRecorder.UnloadMuxer(m_recordMuxer);
  581. m_videoRecorder.UnloadMuxer(m_recordMuxer);
  582. // 然后关闭muxer
  583. m_recordMuxer.Close();
  584. m_isSyncRecord = false;
  585. }
  586. }
  587. void AvRecorder::updateCaptureList()
  588. {
  589. m_captureComboBox->clear();
  590. auto&& monitorList = MonitorFinder::GetList(true);
  591. for (auto&& monitor : monitorList) {
  592. QString text = "屏幕: " + QString::fromStdWString(monitor.title);
  593. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(monitor.monitor)));
  594. }
  595. auto&& windowList = WindowFinder::GetList(true);
  596. for (auto&& window : windowList) {
  597. QString text = "窗口: " + QString::fromStdWString(window.title);
  598. m_captureComboBox->addItem(text, QVariant::fromValue(qintptr(window.hwnd)));
  599. }
  600. }
  601. void AvRecorder::initStatusBarUi()
  602. {
  603. m_videoEncodeLabel = new QLabel;
  604. m_audioEncodeLabel = new QLabel;
  605. auto hLayout = new QHBoxLayout;
  606. hLayout->setContentsMargins(0, 0, 0, 0);
  607. hLayout->addWidget(new QLabel("捕获方式:"));
  608. m_captureMethodBox = new QComboBox;
  609. hLayout->addWidget(m_captureMethodBox);
  610. m_captureStatusLabel = new QLabel("状态: 正常");
  611. m_captureTimeLabel = new QLabel("00:00:00");
  612. m_videolossRate = new QLabel("丢帧率: 00.00%");
  613. m_fpsLabel = new QLabel("FPS: 30");
  614. // 创建状态栏并添加到布局中
  615. m_statusBar = new QStatusBar(this);
  616. m_statusBar->setSizeGripEnabled(false);
  617. // 添加各个状态信息到状态栏
  618. m_statusBar->addWidget(m_videoEncodeLabel);
  619. m_statusBar->addWidget(m_audioEncodeLabel);
  620. auto widget = new QWidget;
  621. widget->setLayout(hLayout);
  622. m_statusBar->addWidget(widget);
  623. m_statusBar->addWidget(m_videolossRate);
  624. m_statusBar->addWidget(m_captureStatusLabel);
  625. m_statusBar->addWidget(m_captureTimeLabel);
  626. m_statusBar->addWidget(m_fpsLabel);
  627. }
  628. void AvRecorder::updateCaptureMethodBox(bool isMonitor) {
  629. m_captureMethodBox->clear();
  630. m_captureMethodBox->addItem("WGC");
  631. if (isMonitor) {
  632. m_captureMethodBox->addItem("DXGI");
  633. } else {
  634. m_captureMethodBox->addItem("GDI");
  635. }
  636. }
  637. // 捕获源切换时调用
  638. void AvRecorder::onCaptureSourceChanged() {
  639. int idx = m_captureComboBox->currentIndex();
  640. int monitorCnt = (int)MonitorFinder::GetList().size();
  641. bool isMonitor = (idx >= 0 && idx < monitorCnt);
  642. updateCaptureMethodBox(isMonitor);
  643. // 新增:推流/录制时切换采集源不中断
  644. if (m_isRecord || m_isLive) {
  645. CaptureMethod method = CaptureMethod::WGC;
  646. QString methodText = m_captureMethodBox->currentText();
  647. if (methodText == "DXGI") method = CaptureMethod::DXGI;
  648. else if (methodText == "GDI") method = CaptureMethod::GDI;
  649. if (isMonitor) {
  650. m_videoRecorder.SetCaptureSource(idx, method);
  651. } else {
  652. qintptr ptrHwnd = m_captureComboBox->currentData().value<qintptr>();
  653. m_videoRecorder.SetCaptureSource((HWND)ptrHwnd, method);
  654. }
  655. }
  656. }
  657. void AvRecorder::showAudioDeviceMenu()
  658. {
  659. QMenu *menu = new QMenu(this);
  660. // // 麦克风设备子菜单
  661. // QMenu *micMenu = menu->addMenu("麦克风设备");
  662. // const auto inputDevices = QMediaDevices::audioInputs();
  663. // for (const auto &device : inputDevices) {
  664. // QAction *action = micMenu->addAction(device.description());
  665. // action->setData(device.id());
  666. // action->setCheckable(true);
  667. // if (device == QMediaDevices::defaultAudioInput()) {
  668. // action->setChecked(true);
  669. // action->setText(device.description() + " (默认)");
  670. // }
  671. // connect(action, &QAction::triggered, this, [this, device]() {
  672. // qDebug() << "选择麦克风设备:" << device.description() << "ID:" << device.id();
  673. // // 这里可以添加实际的设备切换逻辑
  674. // if (m_microphoneWidget) {
  675. // // 更新麦克风组件显示
  676. // }
  677. // });
  678. // }
  679. // menu->addSeparator();
  680. // // 扬声器设备子菜单
  681. // QMenu *speakerMenu = menu->addMenu("扬声器设备");
  682. // const auto outputDevices = QMediaDevices::audioOutputs();
  683. // for (const auto &device : outputDevices) {
  684. // QAction *action = speakerMenu->addAction(device.description());
  685. // action->setData(device.id());
  686. // action->setCheckable(true);
  687. // if (device == QMediaDevices::defaultAudioOutput()) {
  688. // action->setChecked(true);
  689. // action->setText(device.description() + " (默认)");
  690. // }
  691. // connect(action, &QAction::triggered, this, [this, device]() {
  692. // qDebug() << "选择扬声器设备:" << device.description() << "ID:" << device.id();
  693. // // 这里可以添加实际的设备切换逻辑
  694. // if (m_speakerWidget) {
  695. // // 更新扬声器组件显示
  696. // }
  697. // });
  698. // }
  699. menu->addSeparator();
  700. // 刷新设备列表
  701. QAction *refreshAction = menu->addAction("刷新设备列表");
  702. connect(refreshAction, &QAction::triggered, this, [this]() {
  703. qDebug() << "刷新音频设备列表";
  704. // 重新显示菜单以刷新设备列表
  705. QTimer::singleShot(100, this, &AvRecorder::showAudioDeviceMenu);
  706. });
  707. // 在按钮下方显示菜单
  708. QPoint pos = m_audioDeviceBtn->mapToGlobal(QPoint(0, m_audioDeviceBtn->height()));
  709. menu->exec(pos);
  710. menu->deleteLater();
  711. }
  712. void AvRecorder::onMicrophoneDeviceSelected()
  713. {
  714. // 麦克风设备选择处理
  715. }
  716. void AvRecorder::onSpeakerDeviceSelected()
  717. {
  718. // 扬声器设备选择处理
  719. }