recorderwidget.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #include "recorderwidget.h"
  2. #include <QApplication>
  3. #include <QDateTime>
  4. #include <QDebug>
  5. #include <QMessageBox>
  6. #include <QFileDialog>
  7. #include <QStandardPaths>
  8. #include <QOpenGLFunctions>
  9. #include <QPainter>
  10. // 静态实例指针
  11. RecorderWidget* RecorderWidget::s_instance = nullptr;
  12. RecorderWidget::RecorderWidget(QWidget *parent)
  13. : QWidget(parent)
  14. , m_mainLayout(nullptr)
  15. , m_previewWidget(nullptr)
  16. , m_recordButton(nullptr)
  17. , m_streamButton(nullptr)
  18. , m_settingsButton(nullptr)
  19. , m_audioDeviceButton(nullptr)
  20. , m_drawCursorCheckBox(nullptr)
  21. , m_syncRecordCheckBox(nullptr)
  22. , m_encoderComboBox(nullptr)
  23. , m_micComboBox(nullptr)
  24. , m_speakerComboBox(nullptr)
  25. , m_statusBar(nullptr)
  26. , m_statusLabel(nullptr)
  27. , m_timeLabel(nullptr)
  28. , m_encoderLabel(nullptr)
  29. , m_previewTimer(nullptr)
  30. , m_statusTimer(nullptr)
  31. , m_micDevices(nullptr)
  32. , m_speakerDevices(nullptr)
  33. , m_encoders(nullptr)
  34. , m_micCount(0)
  35. , m_speakerCount(0)
  36. , m_encoderCount(0)
  37. , m_isRecording(false)
  38. , m_isStreaming(false)
  39. , m_isInitialized(false)
  40. , m_previewWidth(0)
  41. , m_previewHeight(0)
  42. {
  43. // 设置静态实例指针
  44. s_instance = this;
  45. // 初始化默认设置
  46. m_settings.liveUrl = "rtmp://106.55.186.74:1935/stream/V1";
  47. m_settings.liveName = "stream";
  48. m_settings.outputDir = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation).toStdString();
  49. m_settings.videoBitRate = 8000000;
  50. m_settings.videoFrameRate = 30;
  51. m_settings.videoQuality = 100;
  52. m_settings.audioBitRate = 128000;
  53. initUI();
  54. initRecorder();
  55. // 创建定时器
  56. m_previewTimer = new QTimer(this);
  57. connect(m_previewTimer, &QTimer::timeout, this, &RecorderWidget::updatePreview);
  58. m_statusTimer = new QTimer(this);
  59. connect(m_statusTimer, &QTimer::timeout, this, &RecorderWidget::updateStatus);
  60. m_statusTimer->start(1000); // 每秒更新一次状态
  61. }
  62. RecorderWidget::~RecorderWidget()
  63. {
  64. releaseRecorder();
  65. s_instance = nullptr;
  66. }
  67. void RecorderWidget::initUI()
  68. {
  69. m_mainLayout = new QVBoxLayout(this);
  70. m_mainLayout->setContentsMargins(0, 0, 0, 0);
  71. // 预览区域
  72. m_previewWidget = new QOpenGLWidget(this);
  73. m_previewWidget->setMinimumSize(640, 480);
  74. m_previewWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  75. // 控制区域
  76. QWidget* controlWidget = new QWidget(this);
  77. QHBoxLayout* controlLayout = new QHBoxLayout(controlWidget);
  78. // 左侧设备选择区
  79. QGroupBox* deviceGroup = new QGroupBox("设备选择", this);
  80. QVBoxLayout* deviceLayout = new QVBoxLayout(deviceGroup);
  81. // 编码器选择
  82. QHBoxLayout* encoderLayout = new QHBoxLayout();
  83. encoderLayout->addWidget(new QLabel("编码器:"));
  84. m_encoderComboBox = new QComboBox(this);
  85. encoderLayout->addWidget(m_encoderComboBox);
  86. deviceLayout->addLayout(encoderLayout);
  87. // 麦克风选择
  88. QHBoxLayout* micLayout = new QHBoxLayout();
  89. micLayout->addWidget(new QLabel("麦克风:"));
  90. m_micComboBox = new QComboBox(this);
  91. micLayout->addWidget(m_micComboBox);
  92. deviceLayout->addLayout(micLayout);
  93. // 扬声器选择
  94. QHBoxLayout* speakerLayout = new QHBoxLayout();
  95. speakerLayout->addWidget(new QLabel("扬声器:"));
  96. m_speakerComboBox = new QComboBox(this);
  97. speakerLayout->addWidget(m_speakerComboBox);
  98. deviceLayout->addLayout(speakerLayout);
  99. // 音频设备按钮
  100. m_audioDeviceButton = new QPushButton("刷新设备", this);
  101. deviceLayout->addWidget(m_audioDeviceButton);
  102. // 右侧操作区
  103. QGroupBox* actionGroup = new QGroupBox("操作", this);
  104. QVBoxLayout* actionLayout = new QVBoxLayout(actionGroup);
  105. // 选项
  106. m_drawCursorCheckBox = new QCheckBox("绘制鼠标指针", this);
  107. m_drawCursorCheckBox->setChecked(true);
  108. m_syncRecordCheckBox = new QCheckBox("推流时同步录制", this);
  109. actionLayout->addWidget(m_drawCursorCheckBox);
  110. actionLayout->addWidget(m_syncRecordCheckBox);
  111. // 按钮
  112. m_recordButton = new QPushButton("开始录制", this);
  113. m_streamButton = new QPushButton("开始推流", this);
  114. m_settingsButton = new QPushButton("设置", this);
  115. actionLayout->addWidget(m_recordButton);
  116. actionLayout->addWidget(m_streamButton);
  117. actionLayout->addWidget(m_settingsButton);
  118. // 添加到控制布局
  119. controlLayout->addWidget(deviceGroup, 1);
  120. controlLayout->addWidget(actionGroup, 1);
  121. controlLayout->addStretch();
  122. // 状态栏
  123. initStatusBar();
  124. // 添加到主布局
  125. m_mainLayout->addWidget(m_previewWidget, 1);
  126. m_mainLayout->addWidget(controlWidget, 0);
  127. m_mainLayout->addWidget(m_statusBar, 0);
  128. // 连接信号槽
  129. connect(m_recordButton, &QPushButton::clicked, this, &RecorderWidget::onRecordButtonClicked);
  130. connect(m_streamButton, &QPushButton::clicked, this, &RecorderWidget::onStreamButtonClicked);
  131. connect(m_settingsButton, &QPushButton::clicked, this, &RecorderWidget::onSettingsButtonClicked);
  132. connect(m_audioDeviceButton, &QPushButton::clicked, this, &RecorderWidget::onAudioDeviceButtonClicked);
  133. }
  134. void RecorderWidget::initStatusBar()
  135. {
  136. m_statusBar = new QStatusBar(this);
  137. m_statusLabel = new QLabel("状态: 就绪", this);
  138. m_timeLabel = new QLabel("00:00:00", this);
  139. m_encoderLabel = new QLabel("编码器: 未选择", this);
  140. m_statusBar->addWidget(m_statusLabel);
  141. m_statusBar->addPermanentWidget(m_encoderLabel);
  142. m_statusBar->addPermanentWidget(m_timeLabel);
  143. }
  144. void RecorderWidget::initRecorder()
  145. {
  146. // 设置回调函数
  147. setupCallbacks();
  148. // 设置录制器日志路径
  149. QString logPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/recorder.log";
  150. recorder_set_logpath(logPath.toUtf8().constData());
  151. // 延迟刷新设备和编码器列表,避免在构造函数中调用可能导致异常的API
  152. QTimer::singleShot(100, this, [this]() {
  153. refreshAudioDevices();
  154. refreshVideoEncoders();
  155. });
  156. }
  157. void RecorderWidget::releaseRecorder()
  158. {
  159. if (m_isRecording || m_isStreaming) {
  160. recorder_stop();
  161. }
  162. if (m_isInitialized) {
  163. recorder_release();
  164. m_isInitialized = false;
  165. }
  166. // 释放设备数组
  167. if (m_micDevices) {
  168. recorder_free_array(m_micDevices);
  169. m_micDevices = nullptr;
  170. }
  171. if (m_speakerDevices) {
  172. recorder_free_array(m_speakerDevices);
  173. m_speakerDevices = nullptr;
  174. }
  175. if (m_encoders) {
  176. recorder_free_array(m_encoders);
  177. m_encoders = nullptr;
  178. }
  179. }
  180. void RecorderWidget::setupCallbacks()
  181. {
  182. m_callbacks.func_duration = onDurationCallback;
  183. m_callbacks.func_error = onErrorCallback;
  184. m_callbacks.func_device_change = onDeviceChangeCallback;
  185. m_callbacks.func_preview_yuv = onPreviewYUVCallback;
  186. m_callbacks.func_preview_audio = onPreviewAudioCallback;
  187. }
  188. void RecorderWidget::refreshAudioDevices()
  189. {
  190. // 释放之前的设备数组
  191. if (m_micDevices) {
  192. recorder_free_array(m_micDevices);
  193. m_micDevices = nullptr;
  194. }
  195. if (m_speakerDevices) {
  196. recorder_free_array(m_speakerDevices);
  197. m_speakerDevices = nullptr;
  198. }
  199. // 获取麦克风设备
  200. try {
  201. m_micCount = recorder_get_mics(&m_micDevices);
  202. m_micComboBox->clear();
  203. for (int i = 0; i < m_micCount; ++i) {
  204. QString deviceName = QString::fromUtf8(m_micDevices[i].name);
  205. m_micComboBox->addItem(deviceName);
  206. if (m_micDevices[i].is_default) {
  207. m_micComboBox->setCurrentIndex(i);
  208. }
  209. }
  210. } catch (...) {
  211. m_micCount = 0;
  212. qWarning() << "Failed to get microphone devices";
  213. }
  214. // 获取扬声器设备
  215. try {
  216. m_speakerCount = recorder_get_speakers(&m_speakerDevices);
  217. m_speakerComboBox->clear();
  218. for (int i = 0; i < m_speakerCount; ++i) {
  219. QString deviceName = QString::fromUtf8(m_speakerDevices[i].name);
  220. m_speakerComboBox->addItem(deviceName);
  221. if (m_speakerDevices[i].is_default) {
  222. m_speakerComboBox->setCurrentIndex(i);
  223. }
  224. }
  225. } catch (...) {
  226. m_speakerCount = 0;
  227. qWarning() << "Failed to get speaker devices";
  228. }
  229. }
  230. void RecorderWidget::refreshVideoEncoders()
  231. {
  232. // 释放之前的编码器数组
  233. if (m_encoders) {
  234. recorder_free_array(m_encoders);
  235. m_encoders = nullptr;
  236. }
  237. // 获取视频编码器
  238. try {
  239. m_encoderCount = recorder_get_vencoders(&m_encoders);
  240. m_encoderComboBox->clear();
  241. for (int i = 0; i < m_encoderCount; ++i) {
  242. QString encoderName = QString::fromUtf8(m_encoders[i].name);
  243. m_encoderComboBox->addItem(encoderName);
  244. }
  245. // 默认选择第一个编码器
  246. if (m_encoderCount > 0) {
  247. m_encoderComboBox->setCurrentIndex(0);
  248. m_encoderLabel->setText(QString("编码器: %1").arg(m_encoders[0].name));
  249. }
  250. } catch (...) {
  251. m_encoderCount = 0;
  252. qWarning() << "Failed to get video encoders";
  253. m_encoderLabel->setText("编码器: 获取失败");
  254. }
  255. }
  256. void RecorderWidget::setSettings(const Settings& settings)
  257. {
  258. m_settings = settings;
  259. }
  260. bool RecorderWidget::startRecording()
  261. {
  262. if (m_isRecording) {
  263. return true;
  264. }
  265. // 准备录制设置
  266. memset(&m_recorderSetting, 0, sizeof(m_recorderSetting));
  267. // 视频设置
  268. m_recorderSetting.v_left = 0;
  269. m_recorderSetting.v_top = 0;
  270. m_recorderSetting.v_width = GetSystemMetrics(SM_CXSCREEN);
  271. m_recorderSetting.v_height = GetSystemMetrics(SM_CYSCREEN);
  272. m_recorderSetting.v_qb = m_settings.videoQuality;
  273. m_recorderSetting.v_bit_rate = m_settings.videoBitRate;
  274. m_recorderSetting.v_frame_rate = m_settings.videoFrameRate;
  275. // 选择编码器
  276. int encoderIndex = m_encoderComboBox->currentIndex();
  277. if (encoderIndex >= 0 && encoderIndex < m_encoderCount) {
  278. m_recorderSetting.v_enc_id = m_encoders[encoderIndex].id;
  279. }
  280. // 输出文件
  281. QString fileName = QString::fromStdString(m_settings.outputDir);
  282. if (!fileName.endsWith("/") && !fileName.endsWith("\\")) {
  283. fileName += "/";
  284. }
  285. fileName += QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss") + ".mp4";
  286. strcpy_s(m_recorderSetting.output, fileName.toUtf8().constData());
  287. // 音频设备设置
  288. int micIndex = m_micComboBox->currentIndex();
  289. if (micIndex >= 0 && micIndex < m_micCount) {
  290. strcpy_s(m_recorderSetting.a_mic.id, m_micDevices[micIndex].id);
  291. strcpy_s(m_recorderSetting.a_mic.name, m_micDevices[micIndex].name);
  292. m_recorderSetting.a_mic.is_default = m_micDevices[micIndex].is_default;
  293. }
  294. int speakerIndex = m_speakerComboBox->currentIndex();
  295. if (speakerIndex >= 0 && speakerIndex < m_speakerCount) {
  296. strcpy_s(m_recorderSetting.a_speaker.id, m_speakerDevices[speakerIndex].id);
  297. strcpy_s(m_recorderSetting.a_speaker.name, m_speakerDevices[speakerIndex].name);
  298. m_recorderSetting.a_speaker.is_default = m_speakerDevices[speakerIndex].is_default;
  299. }
  300. // 异步初始化和启动录制器,避免主线程阻塞
  301. QFuture<void> future = QtConcurrent::run([this]() {
  302. // 初始化录制器
  303. int result = recorder_init(m_recorderSetting, m_callbacks);
  304. if (result != 0) {
  305. QString error = QString("初始化录制器失败: %1").arg(recorder_err2str(result));
  306. QMetaObject::invokeMethod(this, [this, error]() {
  307. emit errorOccurred(error);
  308. m_recordButton->setText("开始录制");
  309. m_streamButton->setEnabled(true);
  310. m_statusLabel->setText("状态: 就绪");
  311. }, Qt::QueuedConnection);
  312. return;
  313. }
  314. m_isInitialized = true;
  315. // 启用预览
  316. recorder_set_preview_enabled(1);
  317. // 开始录制
  318. result = recorder_start();
  319. if (result != 0) {
  320. QString error = QString("开始录制失败: %1").arg(recorder_err2str(result));
  321. QMetaObject::invokeMethod(this, [this, error]() {
  322. emit errorOccurred(error);
  323. recorder_release();
  324. m_isInitialized = false;
  325. m_recordButton->setText("开始录制");
  326. m_streamButton->setEnabled(true);
  327. m_statusLabel->setText("状态: 就绪");
  328. }, Qt::QueuedConnection);
  329. return;
  330. }
  331. // 成功启动录制
  332. QMetaObject::invokeMethod(this, [this]() {
  333. m_isRecording = true;
  334. m_recordStartTime = QTime::currentTime();
  335. // 开始预览定时器
  336. m_previewTimer->start(33); // 约30fps
  337. emit recordingStarted();
  338. }, Qt::QueuedConnection);
  339. });
  340. // 立即更新UI状态,显示正在初始化
  341. m_statusLabel->setText("状态: 初始化中...");
  342. return true;
  343. }
  344. void RecorderWidget::stopRecording()
  345. {
  346. if (!m_isRecording) {
  347. return;
  348. }
  349. recorder_stop();
  350. if (m_isInitialized) {
  351. recorder_release();
  352. m_isInitialized = false;
  353. }
  354. m_isRecording = false;
  355. // 停止预览定时器
  356. m_previewTimer->stop();
  357. emit recordingStopped();
  358. }
  359. bool RecorderWidget::startStreaming()
  360. {
  361. if (m_isStreaming) {
  362. return true;
  363. }
  364. // 准备推流设置
  365. memset(&m_recorderSetting, 0, sizeof(m_recorderSetting));
  366. // 视频设置
  367. m_recorderSetting.v_left = 0;
  368. m_recorderSetting.v_top = 0;
  369. m_recorderSetting.v_width = GetSystemMetrics(SM_CXSCREEN);
  370. m_recorderSetting.v_height = GetSystemMetrics(SM_CYSCREEN);
  371. m_recorderSetting.v_qb = m_settings.videoQuality;
  372. m_recorderSetting.v_bit_rate = m_settings.videoBitRate;
  373. m_recorderSetting.v_frame_rate = m_settings.videoFrameRate;
  374. // 选择编码器
  375. int encoderIndex = m_encoderComboBox->currentIndex();
  376. if (encoderIndex >= 0 && encoderIndex < m_encoderCount) {
  377. m_recorderSetting.v_enc_id = m_encoders[encoderIndex].id;
  378. }
  379. // 推流地址
  380. QString streamUrl = QString::fromStdString(m_settings.liveUrl + "/" + m_settings.liveName);
  381. strcpy_s(m_recorderSetting.output, streamUrl.toUtf8().constData());
  382. // 音频设备设置
  383. int micIndex = m_micComboBox->currentIndex();
  384. if (micIndex >= 0 && micIndex < m_micCount) {
  385. strcpy_s(m_recorderSetting.a_mic.id, m_micDevices[micIndex].id);
  386. strcpy_s(m_recorderSetting.a_mic.name, m_micDevices[micIndex].name);
  387. m_recorderSetting.a_mic.is_default = m_micDevices[micIndex].is_default;
  388. }
  389. int speakerIndex = m_speakerComboBox->currentIndex();
  390. if (speakerIndex >= 0 && speakerIndex < m_speakerCount) {
  391. strcpy_s(m_recorderSetting.a_speaker.id, m_speakerDevices[speakerIndex].id);
  392. strcpy_s(m_recorderSetting.a_speaker.name, m_speakerDevices[speakerIndex].name);
  393. m_recorderSetting.a_speaker.is_default = m_speakerDevices[speakerIndex].is_default;
  394. }
  395. // 异步初始化和启动推流,避免主线程阻塞
  396. QFuture<void> future = QtConcurrent::run([this]() {
  397. // 初始化录制器
  398. int result = recorder_init(m_recorderSetting, m_callbacks);
  399. if (result != 0) {
  400. QString error = QString("初始化推流失败: %1").arg(recorder_err2str(result));
  401. QMetaObject::invokeMethod(this, [this, error]() {
  402. emit errorOccurred(error);
  403. m_streamButton->setText("开始推流");
  404. m_recordButton->setEnabled(true);
  405. m_statusLabel->setText("状态: 就绪");
  406. }, Qt::QueuedConnection);
  407. return;
  408. }
  409. m_isInitialized = true;
  410. // 启用预览
  411. recorder_set_preview_enabled(1);
  412. // 开始推流
  413. result = recorder_start();
  414. if (result != 0) {
  415. QString error = QString("开始推流失败: %1").arg(recorder_err2str(result));
  416. QMetaObject::invokeMethod(this, [this, error]() {
  417. emit errorOccurred(error);
  418. recorder_release();
  419. m_isInitialized = false;
  420. m_streamButton->setText("开始推流");
  421. m_recordButton->setEnabled(true);
  422. m_statusLabel->setText("状态: 就绪");
  423. }, Qt::QueuedConnection);
  424. return;
  425. }
  426. // 成功启动推流
  427. QMetaObject::invokeMethod(this, [this]() {
  428. m_isStreaming = true;
  429. m_recordStartTime = QTime::currentTime();
  430. // 开始预览定时器
  431. m_previewTimer->start(33); // 约30fps
  432. emit streamingStarted();
  433. }, Qt::QueuedConnection);
  434. });
  435. // 立即更新UI状态,显示正在初始化
  436. m_statusLabel->setText("状态: 连接中...");
  437. return true;
  438. }
  439. void RecorderWidget::stopStreaming()
  440. {
  441. if (!m_isStreaming) {
  442. return;
  443. }
  444. recorder_stop();
  445. if (m_isInitialized) {
  446. recorder_release();
  447. m_isInitialized = false;
  448. }
  449. m_isStreaming = false;
  450. // 停止预览定时器
  451. m_previewTimer->stop();
  452. emit streamingStopped();
  453. }
  454. void RecorderWidget::onRecordButtonClicked()
  455. {
  456. if (!m_isRecording) {
  457. if (startRecording()) {
  458. m_recordButton->setText("停止录制");
  459. m_streamButton->setEnabled(false);
  460. m_statusLabel->setText("状态: 录制中");
  461. }
  462. } else {
  463. stopRecording();
  464. m_recordButton->setText("开始录制");
  465. m_streamButton->setEnabled(true);
  466. m_statusLabel->setText("状态: 就绪");
  467. }
  468. }
  469. void RecorderWidget::onStreamButtonClicked()
  470. {
  471. if (!m_isStreaming) {
  472. if (startStreaming()) {
  473. m_streamButton->setText("停止推流");
  474. m_recordButton->setEnabled(!m_syncRecordCheckBox->isChecked());
  475. m_statusLabel->setText("状态: 推流中");
  476. }
  477. } else {
  478. stopStreaming();
  479. m_streamButton->setText("开始推流");
  480. m_recordButton->setEnabled(true);
  481. m_statusLabel->setText("状态: 就绪");
  482. }
  483. }
  484. void RecorderWidget::onSettingsButtonClicked()
  485. {
  486. // 打开设置对话框
  487. QMessageBox::information(this, "设置", "设置功能待实现");
  488. }
  489. void RecorderWidget::onAudioDeviceButtonClicked()
  490. {
  491. refreshAudioDevices();
  492. QMessageBox::information(this, "音频设备", "设备列表已刷新");
  493. }
  494. void RecorderWidget::updatePreview()
  495. {
  496. // 预览更新在回调函数中处理
  497. if (m_previewWidget && m_previewWidth > 0 && m_previewHeight > 0) {
  498. m_previewWidget->update();
  499. }
  500. }
  501. void RecorderWidget::updateStatus()
  502. {
  503. if (m_isRecording || m_isStreaming) {
  504. QTime currentTime = QTime::currentTime();
  505. int elapsed = m_recordStartTime.secsTo(currentTime);
  506. int hours = elapsed / 3600;
  507. int minutes = (elapsed % 3600) / 60;
  508. int seconds = elapsed % 60;
  509. QString timeStr = QString("%1:%2:%3")
  510. .arg(hours, 2, 10, QChar('0'))
  511. .arg(minutes, 2, 10, QChar('0'))
  512. .arg(seconds, 2, 10, QChar('0'));
  513. m_timeLabel->setText(timeStr);
  514. } else {
  515. m_timeLabel->setText("00:00:00");
  516. }
  517. }
  518. // 静态回调函数实现
  519. void RecorderWidget::onDurationCallback(uint64_t duration)
  520. {
  521. if (s_instance) {
  522. // 可以在这里处理持续时间更新
  523. qDebug() << "Recording duration:" << duration << "ms";
  524. }
  525. }
  526. void RecorderWidget::onErrorCallback(int error)
  527. {
  528. if (s_instance) {
  529. QString errorStr = QString("录制错误: %1").arg(recorder_err2str(error));
  530. QMetaObject::invokeMethod(s_instance, "errorOccurred", Qt::QueuedConnection, Q_ARG(QString, errorStr));
  531. }
  532. }
  533. void RecorderWidget::onDeviceChangeCallback(int type)
  534. {
  535. if (s_instance) {
  536. // 设备变化时刷新设备列表
  537. QMetaObject::invokeMethod(s_instance, "refreshAudioDevices", Qt::QueuedConnection);
  538. }
  539. }
  540. void RecorderWidget::onPreviewYUVCallback(const unsigned char *data, unsigned int size, int width, int height, int type)
  541. {
  542. if (s_instance && data && size > 0) {
  543. QMutexLocker locker(&s_instance->m_previewMutex);
  544. s_instance->m_previewData = QByteArray(reinterpret_cast<const char*>(data), size);
  545. s_instance->m_previewWidth = width;
  546. s_instance->m_previewHeight = height;
  547. }
  548. }
  549. void RecorderWidget::onPreviewAudioCallback()
  550. {
  551. // 音频预览回调,暂时不处理
  552. }