basic.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. extern "C" {
  8. #include <libavcodec/avcodec.h>
  9. #include <libavformat/avformat.h>
  10. }
  11. // ***************
  12. // MUTEX
  13. extern std::mutex __mtx;
  14. // ***************
  15. // debug function
  16. #define __AVDEBUG
  17. #ifdef __AVDEBUG
  18. #define __DebugPrint(fmtStr, ...) \
  19. std::printf("[" __FILE__ ", line:%d] " fmtStr "\n", __LINE__, ##__VA_ARGS__)
  20. #define __Str(exp) #exp
  21. #define __Check(retVal, ...) \
  22. do { \
  23. if (!(__VA_ARGS__)) { \
  24. __DebugPrint(__Str(__VA_ARGS__) " failed"); \
  25. return retVal; \
  26. } \
  27. } while (false)
  28. #else
  29. #define __DebugPrint(fmtStr, ...)
  30. #define __Check(retVal, ...) \
  31. do { \
  32. if (!(__VA_ARGS__)) { \
  33. return retVal; \
  34. } \
  35. } while (false)
  36. #endif
  37. #define __CheckNo(...) __Check(, __VA_ARGS__)
  38. #define __CheckBool(...) __Check(false, __VA_ARGS__)
  39. #define __CheckNullptr(...) __Check(nullptr, __VA_ARGS__)
  40. enum class MediaType {
  41. AUDIO,
  42. VIDEO
  43. };
  44. // ***************
  45. // memory function
  46. template <typename T, typename Func>
  47. void Free(T*& ptr, Func&& func)
  48. {
  49. static_assert(std::is_convertible_v<Func, std::function<void()>>, "Type Func should be std::function<void()>");
  50. if (ptr == nullptr) {
  51. return;
  52. }
  53. func();
  54. ptr = nullptr;
  55. }
  56. //***************
  57. // time function
  58. // Sleep x ms
  59. inline void SleepMs(int timeMs)
  60. {
  61. std::this_thread::sleep_for(std::chrono::milliseconds(timeMs));
  62. }
  63. // =================== 音频全局设置 ===================
  64. // 此处配置影响全局的音频捕获和编码参数,旨在平衡音质与文件大小。
  65. // --- 采样率 (Sample Rate) ---
  66. // 48000 Hz: 专业音频和视频的标准,提供高质量的音频。
  67. // 44100 Hz: CD音质标准,兼容性好。
  68. // 16000 Hz: 通讯领域常用,适用于纯语音,体积小。
  69. // 推荐使用 48000 或 44100。
  70. constexpr int AUDIO_SAMPLE_RATE = 48000;
  71. // --- 声道数 (Channels) ---
  72. // 1 (Mono): 单声道。体积小,适用于语音、旁白等场景。兼容性最佳。
  73. // 2 (Stereo): 立体声。提供空间感,适用于音乐、游戏等场景。体积是单声道的两倍。
  74. // 为了平衡音质和文件大小,推荐使用单声道。
  75. constexpr int AUDIO_CHANNEL = 1;
  76. // --- 采样格式 (Sample Format) ---
  77. // FFmpeg编码器期望的输入格式。捕获的PCM数据会被重采样成此格式。
  78. // AV_SAMPLE_FMT_FLTP: 浮点型,平面存储。可以提供更大的动态范围。
  79. constexpr AVSampleFormat AUDIO_FMT = AV_SAMPLE_FMT_FLTP;
  80. // --- 捕获采样位深 (Capture Bit Depth) ---
  81. // 从系统音频设备请求的原始PCM数据的位深度。
  82. // 16-bit (Signed Integer): 兼容性极好,是大多数音频设备和API的标准格式。
  83. // 编码时,此格式数据会被转换为上面定义的 AUDIO_FMT (如32-bit float)。
  84. constexpr int AUDIO_CAPTURE_BITS_PER_SAMPLE = 16;
  85. // --- 设备索引 ---
  86. constexpr int MICROPHONE_INDEX = 0;
  87. constexpr int SPEAKER_INDEX = 1;
  88. #endif