basic.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef __BASIC_FUCN_H__
  2. #define __BASIC_FUCN_H__
  3. #define __STDC_FORMAT_MACROS
  4. #include <functional>
  5. #include <mutex>
  6. #include <thread>
  7. #include <cstdio>
  8. extern "C" {
  9. #include <libavcodec/avcodec.h>
  10. #include <libavformat/avformat.h>
  11. #include <libavutil/error.h>
  12. }
  13. #include <winerror.h>
  14. // ***************
  15. // MUTEX
  16. extern std::mutex __mtx;
  17. // ***************
  18. // debug function
  19. #define __AVDEBUG
  20. #ifdef __AVDEBUG
  21. #define __DebugPrint(fmtStr, ...) \
  22. std::printf("[" __FILE__ ", line:%d, func:%s] " fmtStr "\n", __LINE__, __FUNCTION__, ##__VA_ARGS__)
  23. #else
  24. #define __DebugPrint(fmtStr, ...)
  25. #endif
  26. #define __Str(exp) #exp
  27. // Legacy __Check* macros removed. Use CheckBool/CheckHR/CheckFF helpers below.
  28. // ================= Function-based checks (preferred going forward) =================
  29. // Return false on failure and print diagnostics. Callers can early-return appropriate values.
  30. inline bool CheckBool(bool condition, const char* what = nullptr)
  31. {
  32. if (!condition) {
  33. if (what) {
  34. __DebugPrint("Check failed: %s", what);
  35. } else {
  36. __DebugPrint("Check failed");
  37. }
  38. return false;
  39. }
  40. return true;
  41. }
  42. inline bool CheckHR(HRESULT hr, const char* what = nullptr)
  43. {
  44. if (FAILED(hr)) {
  45. if (what) {
  46. __DebugPrint("HRESULT failed (0x%08lx): %s", static_cast<unsigned long>(hr), what);
  47. } else {
  48. __DebugPrint("HRESULT failed (0x%08lx)", static_cast<unsigned long>(hr));
  49. }
  50. return false;
  51. }
  52. return true;
  53. }
  54. inline bool CheckFF(int rc, const char* what = nullptr)
  55. {
  56. if (rc < 0) {
  57. char __errbuf[AV_ERROR_MAX_STRING_SIZE];
  58. av_strerror(rc, __errbuf, sizeof(__errbuf));
  59. if (what) {
  60. __DebugPrint("FFmpeg failed: rc=%d (%s) at %s", rc, __errbuf, what);
  61. } else {
  62. __DebugPrint("FFmpeg failed: rc=%d (%s)", rc, __errbuf);
  63. }
  64. return false;
  65. }
  66. return true;
  67. }
  68. enum class MediaType {
  69. AUDIO,
  70. VIDEO
  71. };
  72. // ***************
  73. // memory function
  74. template <typename T, typename Func>
  75. void Free(T*& ptr, Func&& func)
  76. {
  77. static_assert(std::is_convertible_v<Func, std::function<void()>>, "Type Func should be std::function<void()>");
  78. if (ptr == nullptr) {
  79. return;
  80. }
  81. func();
  82. ptr = nullptr;
  83. }
  84. //***************
  85. // time function
  86. // Sleep x ms
  87. inline void SleepMs(int timeMs)
  88. {
  89. std::this_thread::sleep_for(std::chrono::milliseconds(timeMs));
  90. }
  91. // =================== 音频全局设置 ===================
  92. // 此处配置影响全局的音频捕获和编码参数,旨在平衡音质与文件大小。
  93. // --- 采样率 (Sample Rate) ---
  94. // 48000 Hz: 专业音频和视频的标准,提供高质量的音频。
  95. // 44100 Hz: CD音质标准,兼容性好。
  96. // 16000 Hz: 通讯领域常用,适用于纯语音,体积小。
  97. // 推荐使用 48000 或 44100。
  98. constexpr int AUDIO_SAMPLE_RATE = 48000;
  99. // --- 声道数 (Channels) ---
  100. // 1 (Mono): 单声道。体积小,适用于语音、旁白等场景。兼容性最佳。
  101. // 2 (Stereo): 立体声。提供空间感,适用于音乐、游戏等场景。体积是单声道的两倍。
  102. // 为了平衡音质和文件大小,推荐使用单声道。
  103. constexpr int AUDIO_CHANNEL = 1;
  104. // --- 采样格式 (Sample Format) ---
  105. // FFmpeg编码器期望的输入格式。捕获的PCM数据会被重采样成此格式。
  106. // AV_SAMPLE_FMT_FLTP: 浮点型,平面存储。可以提供更大的动态范围。
  107. constexpr AVSampleFormat AUDIO_FMT = AV_SAMPLE_FMT_FLTP;
  108. // --- 捕获采样位深 (Capture Bit Depth) ---
  109. // 从系统音频设备请求的原始PCM数据的位深度。
  110. // 16-bit (Signed Integer): 兼容性极好,是大多数音频设备和API的标准格式。
  111. // 编码时,此格式数据会被转换为上面定义的 AUDIO_FMT (如32-bit float)。
  112. constexpr int AUDIO_CAPTURE_BITS_PER_SAMPLE = 16;
  113. // --- 设备索引 ---
  114. constexpr int MICROPHONE_INDEX = 0;
  115. constexpr int SPEAKER_INDEX = 1;
  116. #endif