test_recorder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * AV录制器模块测试示例
  3. *
  4. * 这个文件展示了如何使用重构后的录制器模块进行音频、视频和音视频同步录制。
  5. * 基于AV模块的现有组件构建,提供了统一的录制接口。
  6. */
  7. #include "code/recorder/recorder.h"
  8. #include "code/base/logger.h"
  9. #include <QCoreApplication>
  10. #include <QTimer>
  11. #include <QDebug>
  12. #include <memory>
  13. using namespace av::recorder;
  14. using namespace av::recorder::utils;
  15. class RecorderTest : public QObject {
  16. Q_OBJECT
  17. public:
  18. RecorderTest(QObject* parent = nullptr) : QObject(parent) {}
  19. void runTests() {
  20. // 初始化录制器模块
  21. auto result = RecorderModule::initialize();
  22. if (result != av::ErrorCode::SUCCESS) {
  23. qCritical() << "Failed to initialize recorder module";
  24. return;
  25. }
  26. qInfo() << "Recorder Module Version:" << QString::fromStdString(RecorderModule::getVersion());
  27. qInfo() << "Supported formats:" << getSupportedFormatsString();
  28. qInfo() << "Supported audio codecs:" << getSupportedAudioCodecsString();
  29. qInfo() << "Supported video codecs:" << getSupportedVideoCodecsString();
  30. // 测试音频录制
  31. testAudioRecording();
  32. // 延迟测试视频录制
  33. QTimer::singleShot(3000, this, &RecorderTest::testVideoRecording);
  34. // 延迟测试音视频同步录制
  35. QTimer::singleShot(6000, this, &RecorderTest::testAVRecording);
  36. // 延迟退出
  37. QTimer::singleShot(10000, this, &RecorderTest::cleanup);
  38. }
  39. private slots:
  40. void testAudioRecording() {
  41. qInfo() << "\n=== Testing Audio Recording ===";
  42. // 创建音频录制器
  43. auto audioRecorder = createAudioRecorder();
  44. if (!audioRecorder) {
  45. qCritical() << "Failed to create audio recorder";
  46. return;
  47. }
  48. // 设置音频参数
  49. auto audioParams = getDefaultAudioParams();
  50. audioParams.outputPath = "test_audio_output.mp4";
  51. audioParams.sampleRate = 44100;
  52. audioParams.channels = 2;
  53. audioParams.bitrate = 128000;
  54. audioParams.codecName = "aac";
  55. qInfo() << "Audio params:" << formatAudioParams(audioParams);
  56. // 初始化录制器
  57. auto result = audioRecorder->initialize(audioParams);
  58. if (result != av::ErrorCode::SUCCESS) {
  59. qCritical() << "Failed to initialize audio recorder";
  60. return;
  61. }
  62. // 开始录制
  63. result = audioRecorder->startRecording();
  64. if (result != av::ErrorCode::SUCCESS) {
  65. qCritical() << "Failed to start audio recording";
  66. return;
  67. }
  68. qInfo() << "Audio recording started, will record for 2 seconds...";
  69. // 2秒后停止录制
  70. QTimer::singleShot(2000, [audioRecorder = std::move(audioRecorder)]() mutable {
  71. auto result = audioRecorder->stopRecording();
  72. if (result == av::ErrorCode::SUCCESS) {
  73. qInfo() << "Audio recording stopped successfully";
  74. // 获取统计信息
  75. auto stats = audioRecorder->getStatistics();
  76. qInfo() << "Audio recording stats:" << formatRecordingStats(stats);
  77. } else {
  78. qCritical() << "Failed to stop audio recording";
  79. }
  80. });
  81. }
  82. void testVideoRecording() {
  83. qInfo() << "\n=== Testing Video Recording ===";
  84. // 创建视频录制器
  85. auto videoRecorder = createVideoRecorder();
  86. if (!videoRecorder) {
  87. qCritical() << "Failed to create video recorder";
  88. return;
  89. }
  90. // 设置视频参数(使用推荐参数)
  91. auto videoParams = getRecommendedVideoParams();
  92. videoParams.outputPath = "test_video_output.mp4";
  93. videoParams.captureMethod = VideoCaptureMethod::SCREEN_CAPTURE;
  94. videoParams.monitorIndex = 0;
  95. videoParams.drawCursor = true;
  96. qInfo() << "Video params:" << formatVideoParams(videoParams);
  97. // 初始化录制器
  98. auto result = videoRecorder->initialize(videoParams);
  99. if (result != av::ErrorCode::SUCCESS) {
  100. qCritical() << "Failed to initialize video recorder";
  101. return;
  102. }
  103. // 开始录制
  104. result = videoRecorder->startRecording();
  105. if (result != av::ErrorCode::SUCCESS) {
  106. qCritical() << "Failed to start video recording";
  107. return;
  108. }
  109. qInfo() << "Video recording started, will record for 2 seconds...";
  110. // 2秒后停止录制
  111. QTimer::singleShot(2000, [videoRecorder = std::move(videoRecorder)]() mutable {
  112. auto result = videoRecorder->stopRecording();
  113. if (result == av::ErrorCode::SUCCESS) {
  114. qInfo() << "Video recording stopped successfully";
  115. // 获取统计信息
  116. auto stats = videoRecorder->getStatistics();
  117. qInfo() << "Video recording stats:" << formatRecordingStats(stats);
  118. } else {
  119. qCritical() << "Failed to stop video recording";
  120. }
  121. });
  122. }
  123. void testAVRecording() {
  124. qInfo() << "\n=== Testing AV Synchronized Recording ===";
  125. // 创建音视频录制器
  126. auto avRecorder = createAVRecorder();
  127. if (!avRecorder) {
  128. qCritical() << "Failed to create AV recorder";
  129. return;
  130. }
  131. // 设置音视频参数
  132. auto avParams = getDefaultAVParams();
  133. avParams.outputPath = "test_av_output.mp4";
  134. avParams.enableAudio = true;
  135. avParams.enableVideo = true;
  136. avParams.enableSync = true;
  137. avParams.syncThresholdMs = 40;
  138. avParams.dropFrameOnSync = true;
  139. // 调整音频参数
  140. avParams.audioParams.sampleRate = 44100;
  141. avParams.audioParams.channels = 2;
  142. avParams.audioParams.bitrate = 128000;
  143. // 调整视频参数
  144. avParams.videoParams.width = 1280;
  145. avParams.videoParams.height = 720;
  146. avParams.videoParams.frameRate = 30;
  147. avParams.videoParams.bitrate = 2000000;
  148. avParams.videoParams.captureMethod = VideoCaptureMethod::SCREEN_CAPTURE;
  149. qInfo() << "AV params:" << formatAVParams(avParams);
  150. // 初始化录制器
  151. auto result = avRecorder->initialize(avParams);
  152. if (result != av::ErrorCode::SUCCESS) {
  153. qCritical() << "Failed to initialize AV recorder";
  154. return;
  155. }
  156. // 开始录制
  157. result = avRecorder->startRecording();
  158. if (result != av::ErrorCode::SUCCESS) {
  159. qCritical() << "Failed to start AV recording";
  160. return;
  161. }
  162. qInfo() << "AV recording started, will record for 3 seconds...";
  163. // 3秒后停止录制
  164. QTimer::singleShot(3000, [avRecorder = std::move(avRecorder)]() mutable {
  165. auto result = avRecorder->stopRecording();
  166. if (result == av::ErrorCode::SUCCESS) {
  167. qInfo() << "AV recording stopped successfully";
  168. // 获取统计信息
  169. auto stats = avRecorder->getStatistics();
  170. qInfo() << "AV recording stats:" << formatRecordingStats(stats);
  171. // 获取同步状态
  172. auto syncStatus = avRecorder->getSyncStatus();
  173. qInfo() << "Sync status:" << formatSyncStatus(syncStatus);
  174. } else {
  175. qCritical() << "Failed to stop AV recording";
  176. }
  177. });
  178. }
  179. void cleanup() {
  180. qInfo() << "\n=== Cleaning up ===";
  181. // 清理录制器模块
  182. RecorderModule::cleanup();
  183. qInfo() << "Test completed, exiting...";
  184. QCoreApplication::quit();
  185. }
  186. private:
  187. QString getSupportedFormatsString() {
  188. auto formats = RecorderModule::getSupportedFormats();
  189. QStringList list;
  190. for (const auto& format : formats) {
  191. list << QString::fromStdString(format);
  192. }
  193. return list.join(", ");
  194. }
  195. QString getSupportedAudioCodecsString() {
  196. auto codecs = RecorderModule::getSupportedAudioCodecs();
  197. QStringList list;
  198. for (const auto& codec : codecs) {
  199. list << QString::fromStdString(codec);
  200. }
  201. return list.join(", ");
  202. }
  203. QString getSupportedVideoCodecsString() {
  204. auto codecs = RecorderModule::getSupportedVideoCodecs();
  205. QStringList list;
  206. for (const auto& codec : codecs) {
  207. list << QString::fromStdString(codec);
  208. }
  209. return list.join(", ");
  210. }
  211. QString formatAudioParams(const AudioRecorderParams& params) {
  212. return QString("SR:%1Hz, CH:%2, BR:%3bps, Codec:%4")
  213. .arg(params.sampleRate)
  214. .arg(params.channels)
  215. .arg(params.bitrate)
  216. .arg(QString::fromStdString(params.codecName));
  217. }
  218. QString formatVideoParams(const VideoRecorderParams& params) {
  219. return QString("Res:%1x%2, FPS:%3, BR:%4bps, Codec:%5")
  220. .arg(params.width)
  221. .arg(params.height)
  222. .arg(params.frameRate)
  223. .arg(params.bitrate)
  224. .arg(QString::fromStdString(params.codecName));
  225. }
  226. QString formatAVParams(const AVRecorderParams& params) {
  227. return QString("Audio:[%1], Video:[%2], Sync:%3")
  228. .arg(formatAudioParams(params.audioParams))
  229. .arg(formatVideoParams(params.videoParams))
  230. .arg(params.enableSync ? "ON" : "OFF");
  231. }
  232. QString formatRecordingStats(const av::RecordingStatistics& stats) {
  233. return QString("Duration:%1s, Frames:%2, Packets:%3, Errors:%4")
  234. .arg(stats.durationMs / 1000.0, 0, 'f', 2)
  235. .arg(stats.totalFrames)
  236. .arg(stats.totalPackets)
  237. .arg(stats.errorCount);
  238. }
  239. QString formatSyncStatus(const av::SyncStatus& status) {
  240. return QString("Drift:%1ms, Dropped:%2, Duplicated:%3")
  241. .arg(status.avgDriftMs, 0, 'f', 2)
  242. .arg(status.droppedFrames)
  243. .arg(status.duplicatedFrames);
  244. }
  245. };
  246. int main(int argc, char *argv[]) {
  247. QCoreApplication app(argc, argv);
  248. // 设置日志级别
  249. av::Logger::setLevel(av::LogLevel::INFO);
  250. qInfo() << "Starting AV Recorder Module Test...";
  251. qInfo() << "This test will demonstrate audio, video, and synchronized AV recording.";
  252. qInfo() << "Output files will be created in the current directory.";
  253. // 创建并运行测试
  254. RecorderTest test;
  255. test.runTests();
  256. return app.exec();
  257. }
  258. #include "test_recorder.moc"