media_common.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #ifndef AV_BASE_MEDIA_COMMON_H
  2. #define AV_BASE_MEDIA_COMMON_H
  3. #include "types.h"
  4. #include <string>
  5. #include <vector>
  6. #include <memory>
  7. #include <functional>
  8. #include <chrono>
  9. #include <limits>
  10. namespace av {
  11. // 字符串工具
  12. namespace string_utils {
  13. // 字符串分割
  14. std::vector<std::string> split(const std::string& str, char delimiter);
  15. std::vector<std::string> split(const std::string& str, const std::string& delimiter);
  16. // 字符串修剪
  17. std::string trim(const std::string& str);
  18. std::string trimLeft(const std::string& str);
  19. std::string trimRight(const std::string& str);
  20. // 字符串替换
  21. std::string replace(const std::string& str, const std::string& from, const std::string& to);
  22. // 大小写转换
  23. std::string toLower(const std::string& str);
  24. std::string toUpper(const std::string& str);
  25. // 字符串判断
  26. bool startsWith(const std::string& str, const std::string& prefix);
  27. bool endsWith(const std::string& str, const std::string& suffix);
  28. bool contains(const std::string& str, const std::string& substr);
  29. // 格式化
  30. template<typename... Args>
  31. std::string format(const std::string& format, Args&&... args);
  32. }
  33. // 文件路径工具
  34. namespace path_utils {
  35. // 路径操作
  36. std::string join(const std::string& path1, const std::string& path2);
  37. std::string getDirectory(const std::string& path);
  38. std::string getFilename(const std::string& path);
  39. std::string getExtension(const std::string& path);
  40. std::string removeExtension(const std::string& path);
  41. // 路径规范化
  42. std::string normalize(const std::string& path);
  43. std::string toNative(const std::string& path);
  44. // 路径判断
  45. bool exists(const std::string& path);
  46. bool isFile(const std::string& path);
  47. bool isDirectory(const std::string& path);
  48. bool isAbsolute(const std::string& path);
  49. // 目录操作
  50. bool createDirectory(const std::string& path);
  51. bool removeFile(const std::string& path);
  52. }
  53. // 时间工具
  54. namespace time_utils {
  55. // 获取当前时间戳
  56. int64_t getCurrentTimeMs();
  57. int64_t getCurrentTimeUs();
  58. // 时间格式化
  59. std::string formatTime(int64_t timeMs, const std::string& format = "%Y-%m-%d %H:%M:%S");
  60. std::string formatDuration(int64_t durationMs);
  61. // 性能计时器
  62. class Timer {
  63. public:
  64. Timer();
  65. void start();
  66. void stop();
  67. void reset();
  68. int64_t elapsedMs() const;
  69. int64_t elapsedUs() const;
  70. bool isRunning() const;
  71. private:
  72. std::chrono::high_resolution_clock::time_point m_startTime;
  73. std::chrono::high_resolution_clock::time_point m_endTime;
  74. bool m_running = false;
  75. };
  76. // RAII计时器
  77. class ScopedTimer {
  78. public:
  79. explicit ScopedTimer(const std::string& name);
  80. ~ScopedTimer();
  81. private:
  82. std::string m_name;
  83. Timer m_timer;
  84. };
  85. }
  86. // 内存工具
  87. namespace memory_utils {
  88. // 内存对齐
  89. size_t alignSize(size_t size, size_t alignment);
  90. void* alignedAlloc(size_t size, size_t alignment);
  91. void alignedFree(void* ptr);
  92. // 内存池(简单实现)
  93. class MemoryPool {
  94. public:
  95. explicit MemoryPool(size_t blockSize, size_t blockCount = 100);
  96. ~MemoryPool();
  97. void* allocate();
  98. void deallocate(void* ptr);
  99. size_t getBlockSize() const { return m_blockSize; }
  100. size_t getAvailableBlocks() const;
  101. private:
  102. size_t m_blockSize;
  103. std::vector<uint8_t> m_memory;
  104. std::vector<void*> m_freeBlocks;
  105. mutable std::mutex m_mutex;
  106. };
  107. }
  108. // FFmpeg工具
  109. namespace ffmpeg_utils {
  110. // 错误处理
  111. std::string errorToString(int errorCode);
  112. bool checkError(int ret, const std::string& operation);
  113. // 格式转换
  114. std::string pixelFormatToString(AVPixelFormat format);
  115. std::string sampleFormatToString(AVSampleFormat format);
  116. std::string codecTypeToString(AVMediaType type);
  117. // 参数验证
  118. bool isValidPixelFormat(AVPixelFormat format);
  119. bool isValidSampleFormat(AVSampleFormat format);
  120. bool isValidSampleRate(int sampleRate);
  121. bool isValidChannels(int channels);
  122. // 帧工具
  123. // 统一使用智能指针的帧分配函数
  124. AVFramePtr allocateFrame(AVPixelFormat format, int width, int height);
  125. AVFramePtr allocateAudioFrame(AVSampleFormat format, int sampleRate, int channels, int nbSamples);
  126. void freeFrame(AVFrame** frame);
  127. // 包工具
  128. AVPacket* allocatePacket();
  129. void freePacket(AVPacket** packet);
  130. // 初始化
  131. bool initializeFFmpeg();
  132. void cleanupFFmpeg();
  133. }
  134. // 数学工具
  135. namespace math_utils {
  136. // 数值限制
  137. template<typename T>
  138. T clamp(T value, T min, T max) {
  139. return (value < min) ? min : (value > max) ? max : value;
  140. }
  141. // 数值转换
  142. template<typename T, typename U>
  143. T safeCast(U value) {
  144. return static_cast<T>(clamp<U>(value,
  145. static_cast<U>(std::numeric_limits<T>::min()),
  146. static_cast<U>(std::numeric_limits<T>::max())));
  147. }
  148. // 对齐
  149. template<typename T>
  150. T alignUp(T value, T alignment) {
  151. return (value + alignment - 1) / alignment * alignment;
  152. }
  153. template<typename T>
  154. T alignDown(T value, T alignment) {
  155. return value / alignment * alignment;
  156. }
  157. }
  158. // 线程安全的单例模板
  159. template<typename T>
  160. class Singleton {
  161. public:
  162. static T& instance() {
  163. static T instance;
  164. return instance;
  165. }
  166. protected:
  167. Singleton() = default;
  168. virtual ~Singleton() = default;
  169. private:
  170. Singleton(const Singleton&) = delete;
  171. Singleton& operator=(const Singleton&) = delete;
  172. };
  173. // RAII资源管理器
  174. template<typename Resource, typename Deleter>
  175. class ResourceGuard {
  176. public:
  177. explicit ResourceGuard(Resource resource, Deleter deleter)
  178. : m_resource(resource), m_deleter(deleter), m_valid(true) {}
  179. ~ResourceGuard() {
  180. if (m_valid && m_resource) {
  181. m_deleter(m_resource);
  182. }
  183. }
  184. // 移动构造
  185. ResourceGuard(ResourceGuard&& other) noexcept
  186. : m_resource(other.m_resource), m_deleter(std::move(other.m_deleter)), m_valid(other.m_valid) {
  187. other.m_valid = false;
  188. }
  189. // 移动赋值
  190. ResourceGuard& operator=(ResourceGuard&& other) noexcept {
  191. if (this != &other) {
  192. if (m_valid && m_resource) {
  193. m_deleter(m_resource);
  194. }
  195. m_resource = other.m_resource;
  196. m_deleter = std::move(other.m_deleter);
  197. m_valid = other.m_valid;
  198. other.m_valid = false;
  199. }
  200. return *this;
  201. }
  202. // 禁止拷贝
  203. ResourceGuard(const ResourceGuard&) = delete;
  204. ResourceGuard& operator=(const ResourceGuard&) = delete;
  205. Resource get() const { return m_resource; }
  206. Resource release() {
  207. m_valid = false;
  208. return m_resource;
  209. }
  210. bool isValid() const { return m_valid; }
  211. private:
  212. Resource m_resource;
  213. Deleter m_deleter;
  214. bool m_valid;
  215. };
  216. // 便捷的资源管理器创建函数
  217. template<typename Resource, typename Deleter>
  218. ResourceGuard<Resource, Deleter> makeResourceGuard(Resource resource, Deleter deleter) {
  219. return ResourceGuard<Resource, Deleter>(resource, deleter);
  220. }
  221. } // namespace av
  222. // 便捷宏定义
  223. #define AV_SCOPED_TIMER(name) av::time_utils::ScopedTimer _timer(name)
  224. #define AV_MEASURE_TIME(name, code) \
  225. do { \
  226. av::time_utils::Timer _timer; \
  227. _timer.start(); \
  228. code; \
  229. _timer.stop(); \
  230. AV_LOGGER_INFOF("{} took {} ms", name, _timer.elapsedMs()); \
  231. } while(0)
  232. #endif // AV_BASE_MEDIA_COMMON_H