media_common.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. AVFrame* allocateFrame(AVPixelFormat format, int width, int height);
  124. AVFrame* allocateAudioFrame(AVSampleFormat format, int sampleRate, int channels, int nbSamples);
  125. void freeFrame(AVFrame** frame);
  126. // 包工具
  127. AVPacket* allocatePacket();
  128. void freePacket(AVPacket** packet);
  129. // 初始化
  130. bool initializeFFmpeg();
  131. void cleanupFFmpeg();
  132. }
  133. // 数学工具
  134. namespace math_utils {
  135. // 数值限制
  136. template<typename T>
  137. T clamp(T value, T min, T max) {
  138. return (value < min) ? min : (value > max) ? max : value;
  139. }
  140. // 数值转换
  141. template<typename T, typename U>
  142. T safeCast(U value) {
  143. return static_cast<T>(clamp<U>(value,
  144. static_cast<U>(std::numeric_limits<T>::min()),
  145. static_cast<U>(std::numeric_limits<T>::max())));
  146. }
  147. // 对齐
  148. template<typename T>
  149. T alignUp(T value, T alignment) {
  150. return (value + alignment - 1) / alignment * alignment;
  151. }
  152. template<typename T>
  153. T alignDown(T value, T alignment) {
  154. return value / alignment * alignment;
  155. }
  156. }
  157. // 线程安全的单例模板
  158. template<typename T>
  159. class Singleton {
  160. public:
  161. static T& instance() {
  162. static T instance;
  163. return instance;
  164. }
  165. protected:
  166. Singleton() = default;
  167. virtual ~Singleton() = default;
  168. private:
  169. Singleton(const Singleton&) = delete;
  170. Singleton& operator=(const Singleton&) = delete;
  171. };
  172. // RAII资源管理器
  173. template<typename Resource, typename Deleter>
  174. class ResourceGuard {
  175. public:
  176. explicit ResourceGuard(Resource resource, Deleter deleter)
  177. : m_resource(resource), m_deleter(deleter), m_valid(true) {}
  178. ~ResourceGuard() {
  179. if (m_valid && m_resource) {
  180. m_deleter(m_resource);
  181. }
  182. }
  183. // 移动构造
  184. ResourceGuard(ResourceGuard&& other) noexcept
  185. : m_resource(other.m_resource), m_deleter(std::move(other.m_deleter)), m_valid(other.m_valid) {
  186. other.m_valid = false;
  187. }
  188. // 移动赋值
  189. ResourceGuard& operator=(ResourceGuard&& other) noexcept {
  190. if (this != &other) {
  191. if (m_valid && m_resource) {
  192. m_deleter(m_resource);
  193. }
  194. m_resource = other.m_resource;
  195. m_deleter = std::move(other.m_deleter);
  196. m_valid = other.m_valid;
  197. other.m_valid = false;
  198. }
  199. return *this;
  200. }
  201. // 禁止拷贝
  202. ResourceGuard(const ResourceGuard&) = delete;
  203. ResourceGuard& operator=(const ResourceGuard&) = delete;
  204. Resource get() const { return m_resource; }
  205. Resource release() {
  206. m_valid = false;
  207. return m_resource;
  208. }
  209. bool isValid() const { return m_valid; }
  210. private:
  211. Resource m_resource;
  212. Deleter m_deleter;
  213. bool m_valid;
  214. };
  215. // 便捷的资源管理器创建函数
  216. template<typename Resource, typename Deleter>
  217. ResourceGuard<Resource, Deleter> makeResourceGuard(Resource resource, Deleter deleter) {
  218. return ResourceGuard<Resource, Deleter>(resource, deleter);
  219. }
  220. } // namespace av
  221. // 便捷宏定义
  222. #define AV_SCOPED_TIMER(name) av::time_utils::ScopedTimer _timer(name)
  223. #define AV_MEASURE_TIME(name, code) \
  224. do { \
  225. av::time_utils::Timer _timer; \
  226. _timer.start(); \
  227. code; \
  228. _timer.stop(); \
  229. AV_LOGGER_INFOF("{} took {} ms", name, _timer.elapsedMs()); \
  230. } while(0)
  231. #endif // AV_BASE_MEDIA_COMMON_H