media_common.h 7.6 KB

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