recorder.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "recorder.h"
  2. #include "../base/logger.h"
  3. #include <filesystem>
  4. #include <algorithm>
  5. namespace av {
  6. namespace recorder {
  7. // 静态成员初始化
  8. bool RecorderModule::initialized_ = false;
  9. ErrorCode RecorderModule::initialize() {
  10. if (initialized_) {
  11. return ErrorCode::SUCCESS;
  12. }
  13. AV_LOGGER_INFOF("Initializing Recorder Module v{}", getVersion());
  14. // TODO: 初始化FFmpeg库(如果需要)
  15. // TODO: 检查硬件编码器支持
  16. // TODO: 初始化音频设备枚举
  17. initialized_ = true;
  18. AV_LOGGER_INFO("Recorder Module initialized successfully");
  19. return ErrorCode::SUCCESS;
  20. }
  21. void RecorderModule::cleanup() {
  22. if (!initialized_) {
  23. return;
  24. }
  25. AV_LOGGER_INFO("Cleaning up Recorder Module");
  26. // TODO: 清理资源
  27. initialized_ = false;
  28. AV_LOGGER_INFO("Recorder Module cleaned up");
  29. }
  30. bool RecorderModule::isInitialized() {
  31. return initialized_;
  32. }
  33. std::vector<std::string> RecorderModule::getSupportedFormats() {
  34. return {
  35. "mp4", "avi", "mkv", "mov", "flv", "webm",
  36. "mp3", "wav", "aac", "ogg", "flac"
  37. };
  38. }
  39. std::vector<std::string> RecorderModule::getSupportedAudioCodecs() {
  40. return {
  41. "aac", "mp3", "opus", "vorbis", "flac", "pcm_s16le"
  42. };
  43. }
  44. std::vector<std::string> RecorderModule::getSupportedVideoCodecs() {
  45. return {
  46. "h264", "h265", "vp8", "vp9", "av1", "mpeg4"
  47. };
  48. }
  49. namespace utils {
  50. AudioRecorderParams getDefaultAudioParams() {
  51. AudioRecorderParams params;
  52. params.capturerType = capture::CapturerType::AUDIO_MIC;
  53. params.sampleRate = 44100;
  54. params.channels = 2;
  55. params.bitsPerSample = 16;
  56. params.sampleFormat = AV_SAMPLE_FMT_S16;
  57. params.codecName = "aac";
  58. params.bitrate = 128000;
  59. params.volumeScale = 1.0f;
  60. params.enableMixing = false;
  61. params.format = "mp4";
  62. return params;
  63. }
  64. VideoRecorderParams getDefaultVideoParams() {
  65. VideoRecorderParams params;
  66. params.captureMethod = VideoCaptureMethod::SCREEN_CAPTURE;
  67. params.monitorIndex = 0;
  68. params.width = 1920;
  69. params.height = 1080;
  70. params.frameRate = 30;
  71. params.pixelFormat = AV_PIX_FMT_YUV420P;
  72. params.codecName = "h264";
  73. params.bitrate = 5000000; // 5 Mbps
  74. params.keyFrameInterval = 30;
  75. params.preset = "medium";
  76. params.drawCursor = true;
  77. params.captureAudio = false;
  78. params.format = "mp4";
  79. return params;
  80. }
  81. AVRecorderParams getDefaultAVParams() {
  82. AVRecorderParams params;
  83. params.audioParams = getDefaultAudioParams();
  84. params.videoParams = getDefaultVideoParams();
  85. params.enableAudio = true;
  86. params.enableVideo = true;
  87. params.enableSync = true;
  88. params.syncThresholdMs = 40;
  89. params.dropFrameOnSync = true;
  90. params.format = "mp4";
  91. return params;
  92. }
  93. bool isValidOutputPath(const std::string& path) {
  94. if (path.empty()) {
  95. return false;
  96. }
  97. try {
  98. std::filesystem::path fsPath(path);
  99. // 检查父目录是否存在或可创建
  100. auto parentPath = fsPath.parent_path();
  101. if (!parentPath.empty() && !std::filesystem::exists(parentPath)) {
  102. // 尝试创建目录
  103. std::error_code ec;
  104. std::filesystem::create_directories(parentPath, ec);
  105. if (ec) {
  106. AV_LOGGER_ERRORF("Cannot create output directory: {}", ec.message());
  107. return false;
  108. }
  109. }
  110. // 检查文件扩展名
  111. auto extension = fsPath.extension().string();
  112. if (extension.empty()) {
  113. return false;
  114. }
  115. // 转换为小写
  116. std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  117. // 检查是否为支持的格式
  118. auto supportedFormats = RecorderModule::getSupportedFormats();
  119. auto formatName = extension.substr(1); // 去掉点号
  120. return std::find(supportedFormats.begin(), supportedFormats.end(), formatName) != supportedFormats.end();
  121. } catch (const std::exception& e) {
  122. AV_LOGGER_ERRORF("Invalid output path: {}", e.what());
  123. return false;
  124. }
  125. }
  126. VideoRecorderParams getRecommendedVideoParams() {
  127. VideoRecorderParams params = getDefaultVideoParams();
  128. // TODO: 根据系统性能调整参数
  129. // 这里可以检查:
  130. // - CPU性能
  131. // - 可用内存
  132. // - 硬件编码器支持
  133. // - 显示器分辨率
  134. // 简单的性能检测示例
  135. auto availableMemory = std::thread::hardware_concurrency(); // 简化的性能指标
  136. if (availableMemory <= 4) {
  137. // 低性能设置
  138. params.width = 1280;
  139. params.height = 720;
  140. params.frameRate = 24;
  141. params.bitrate = 2000000; // 2 Mbps
  142. params.preset = "fast";
  143. } else if (availableMemory <= 8) {
  144. // 中等性能设置
  145. params.width = 1920;
  146. params.height = 1080;
  147. params.frameRate = 30;
  148. params.bitrate = 5000000; // 5 Mbps
  149. params.preset = "medium";
  150. } else {
  151. // 高性能设置
  152. params.width = 1920;
  153. params.height = 1080;
  154. params.frameRate = 60;
  155. params.bitrate = 8000000; // 8 Mbps
  156. params.preset = "slow";
  157. }
  158. AV_LOGGER_INFOF("Recommended video params: {}x{} @ {}fps, {} bps",
  159. params.width, params.height, params.frameRate, params.bitrate);
  160. return params;
  161. }
  162. } // namespace utils
  163. } // namespace recorder
  164. } // namespace av