basic.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. constexpr int AUDIO_SAMPLE_RATE = 48000;
  65. constexpr int AUDIO_CHANNEL = 1;
  66. constexpr AVSampleFormat AUDIO_FMT = AV_SAMPLE_FMT_FLTP;
  67. constexpr int MICROPHONE_INDEX = 0;
  68. constexpr int SPEAKER_INDEX = 1;
  69. #endif