av_recorder.cpp 28 KB

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