test_decoder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "code/base/logger.h"
  2. #include "code/base/types.h"
  3. #include "code/codec/codec_audio_decoder.h"
  4. #include "code/codec/codec_video_decoder.h"
  5. #include <chrono>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <memory>
  9. #include <vector>
  10. extern "C" {
  11. #include <libavformat/avformat.h>
  12. #include <libavcodec/avcodec.h>
  13. #include <libavutil/avutil.h>
  14. }
  15. using namespace av;
  16. using namespace av::codec;
  17. class DecoderTester {
  18. public:
  19. DecoderTester() {
  20. // 初始化FFmpeg
  21. av_log_set_level(AV_LOG_INFO);
  22. // 初始化日志系统
  23. av::Logger::instance().setLevel(av::LogLevel::DEBUG);
  24. av::Logger::instance().addOutput(std::make_unique<av::QtOutput>());
  25. std::cout << "=== AV解码器测试程序 ===" << std::endl;
  26. std::cout << "FFmpeg版本: " << av_version_info() << std::endl;
  27. }
  28. ~DecoderTester() {
  29. // 日志系统自动清理
  30. }
  31. // 测试视频解码器基本功能
  32. bool testVideoDecoderBasic() {
  33. std::cout << "\n--- 测试视频解码器基本功能 ---" << std::endl;
  34. try {
  35. // 创建解码器
  36. auto decoder = std::make_unique<VideoDecoder>();
  37. // 测试支持的解码器列表
  38. auto supportedDecoders = VideoDecoder::getSupportedDecoders();
  39. std::cout << "支持的视频解码器数量: " << supportedDecoders.size() << std::endl;
  40. for (const auto& codecName : supportedDecoders) {
  41. std::cout << " - " << codecName;
  42. if (VideoDecoder::isHardwareDecoder(codecName)) {
  43. std::cout << " (硬件加速)";
  44. }
  45. std::cout << std::endl;
  46. }
  47. // 测试H.264解码器初始化
  48. VideoDecoderParams params;
  49. params.codecName = "h264";
  50. params.pixelFormat = AV_PIX_FMT_YUV420P;
  51. params.hardwareAccel = false; // 先测试软件解码
  52. params.threadCount = 4;
  53. ErrorCode result = decoder->open(params);
  54. if (result != ErrorCode::SUCCESS) {
  55. std::cerr << "打开H.264解码器失败: " << static_cast<int>(result) << std::endl;
  56. return false;
  57. }
  58. std::cout << "H.264解码器初始化成功" << std::endl;
  59. std::cout << "解码器名称: " << decoder->getDecoderName() << std::endl;
  60. std::cout << "是否硬件解码器: " << (decoder->isHardwareDecoder() ? "是" : "否") << std::endl;
  61. // 测试统计信息
  62. auto stats = decoder->getStats();
  63. std::cout << "初始统计信息:" << std::endl;
  64. std::cout << " 解码帧数: " << stats.decodedFrames << std::endl;
  65. std::cout << " 丢弃帧数: " << stats.droppedFrames << std::endl;
  66. std::cout << " 错误次数: " << stats.errorCount << std::endl;
  67. decoder->close();
  68. std::cout << "视频解码器基本功能测试通过" << std::endl;
  69. return true;
  70. } catch (const std::exception& e) {
  71. std::cerr << "视频解码器测试异常: " << e.what() << std::endl;
  72. return false;
  73. }
  74. }
  75. // 测试音频解码器基本功能
  76. bool testAudioDecoderBasic() {
  77. std::cout << "\n--- 测试音频解码器基本功能 ---" << std::endl;
  78. try {
  79. // 创建解码器
  80. auto decoder = std::make_unique<AudioDecoder>();
  81. // 测试AAC解码器初始化
  82. AudioDecoderParams params;
  83. params.codecName = "aac";
  84. params.sampleRate = 44100;
  85. params.channels = 2;
  86. params.sampleFormat = AV_SAMPLE_FMT_S16;
  87. params.enableResampling = true;
  88. ErrorCode result = decoder->open(params);
  89. if (result != ErrorCode::SUCCESS) {
  90. std::cerr << "打开AAC解码器失败: " << static_cast<int>(result) << std::endl;
  91. return false;
  92. }
  93. std::cout << "AAC解码器初始化成功" << std::endl;
  94. std::cout << "解码器名称: " << decoder->getDecoderName() << std::endl;
  95. std::cout << "是否启用重采样: " << (decoder->isResamplingEnabled() ? "是" : "否") << std::endl;
  96. // 测试统计信息
  97. auto stats = decoder->getStats();
  98. std::cout << "初始统计信息:" << std::endl;
  99. std::cout << " 解码帧数: " << stats.decodedFrames << std::endl;
  100. std::cout << " 重采样帧数: " << stats.resampledFrames << std::endl;
  101. std::cout << " 错误次数: " << stats.errorCount << std::endl;
  102. decoder->close();
  103. std::cout << "音频解码器基本功能测试通过" << std::endl;
  104. return true;
  105. } catch (const std::exception& e) {
  106. std::cerr << "音频解码器测试异常: " << e.what() << std::endl;
  107. return false;
  108. }
  109. }
  110. // 测试硬件加速解码器
  111. bool testHardwareDecoder() {
  112. std::cout << "\n--- 测试硬件加速解码器 ---" << std::endl;
  113. try {
  114. // 获取推荐的硬件解码器
  115. std::string recommendedDecoder = VideoDecoder::getRecommendedDecoder();
  116. if (recommendedDecoder.empty()) {
  117. std::cout << "未找到推荐的硬件解码器,跳过测试" << std::endl;
  118. return true;
  119. }
  120. std::cout << "推荐的解码器: " << recommendedDecoder << std::endl;
  121. auto decoder = std::make_unique<VideoDecoder>();
  122. VideoDecoderParams params;
  123. params.codecName = recommendedDecoder;
  124. params.hardwareAccel = true;
  125. params.lowLatency = true;
  126. ErrorCode result = decoder->open(params);
  127. if (result != ErrorCode::SUCCESS) {
  128. std::cout << "硬件解码器打开失败,可能不支持: " << static_cast<int>(result) << std::endl;
  129. return true; // 不算失败,因为硬件支持因环境而异
  130. }
  131. std::cout << "硬件解码器测试成功" << std::endl;
  132. std::cout << "解码器名称: " << decoder->getDecoderName() << std::endl;
  133. std::cout << "是否硬件解码器: " << (decoder->isHardwareDecoder() ? "是" : "否") << std::endl;
  134. decoder->close();
  135. return true;
  136. } catch (const std::exception& e) {
  137. std::cout << "硬件解码器测试异常(可能正常): " << e.what() << std::endl;
  138. return true; // 硬件相关异常不算失败
  139. }
  140. }
  141. // 测试解码器工厂
  142. bool testDecoderFactory() {
  143. std::cout << "\n--- 测试解码器工厂 ---" << std::endl;
  144. try {
  145. // 测试创建最佳解码器
  146. auto bestDecoder = VideoDecoder::VideoDecoderFactory::createBest(false); // 优先软件解码
  147. if (!bestDecoder) {
  148. std::cerr << "创建最佳解码器失败" << std::endl;
  149. return false;
  150. }
  151. std::cout << "成功创建最佳软件解码器" << std::endl;
  152. // 测试创建指定解码器
  153. auto h264Decoder = VideoDecoder::VideoDecoderFactory::create("h264");
  154. if (!h264Decoder) {
  155. std::cerr << "创建H.264解码器失败" << std::endl;
  156. return false;
  157. }
  158. std::cout << "成功创建H.264解码器" << std::endl;
  159. // 测试创建硬件解码器
  160. auto hwDecoder = VideoDecoder::VideoDecoderFactory::createBest(true); // 优先硬件解码
  161. if (hwDecoder) {
  162. std::cout << "成功创建硬件解码器" << std::endl;
  163. } else {
  164. std::cout << "硬件解码器不可用(正常)" << std::endl;
  165. }
  166. std::cout << "解码器工厂测试通过" << std::endl;
  167. return true;
  168. } catch (const std::exception& e) {
  169. std::cerr << "解码器工厂测试异常: " << e.what() << std::endl;
  170. return false;
  171. }
  172. }
  173. // 测试解码器状态管理
  174. bool testDecoderStateManagement() {
  175. std::cout << "\n--- 测试解码器状态管理 ---" << std::endl;
  176. try {
  177. auto decoder = std::make_unique<VideoDecoder>();
  178. // 测试初始状态
  179. if (decoder->getState() != CodecState::IDLE) {
  180. std::cerr << "初始状态错误" << std::endl;
  181. return false;
  182. }
  183. // 测试打开
  184. VideoDecoderParams params;
  185. params.codecName = "h264";
  186. ErrorCode result = decoder->open(params);
  187. if (result != ErrorCode::SUCCESS) {
  188. std::cerr << "打开失败" << std::endl;
  189. return false;
  190. }
  191. if (decoder->getState() != CodecState::OPENED) {
  192. std::cerr << "打开后状态错误" << std::endl;
  193. return false;
  194. }
  195. // 测试重置
  196. result = decoder->reset();
  197. if (result != ErrorCode::SUCCESS) {
  198. std::cerr << "重置失败" << std::endl;
  199. return false;
  200. }
  201. if (decoder->getState() != CodecState::OPENED) {
  202. std::cerr << "重置后状态错误" << std::endl;
  203. return false;
  204. }
  205. // 测试关闭
  206. decoder->close();
  207. if (decoder->getState() != CodecState::IDLE) {
  208. std::cerr << "关闭后状态错误" << std::endl;
  209. return false;
  210. }
  211. std::cout << "解码器状态管理测试通过" << std::endl;
  212. return true;
  213. } catch (const std::exception& e) {
  214. std::cerr << "解码器状态管理测试异常: " << e.what() << std::endl;
  215. return false;
  216. }
  217. }
  218. // 测试错误处理
  219. bool testErrorHandling() {
  220. std::cout << "\n--- 测试错误处理 ---" << std::endl;
  221. try {
  222. auto decoder = std::make_unique<VideoDecoder>();
  223. // 测试无效参数
  224. VideoDecoderParams invalidParams;
  225. invalidParams.codecName = ""; // 空名称
  226. ErrorCode result = decoder->open(invalidParams);
  227. if (result == ErrorCode::SUCCESS) {
  228. std::cerr << "应该拒绝空解码器名称" << std::endl;
  229. return false;
  230. }
  231. // 测试不存在的解码器
  232. VideoDecoderParams nonExistentParams;
  233. nonExistentParams.codecName = "non_existent_codec";
  234. result = decoder->open(nonExistentParams);
  235. if (result == ErrorCode::SUCCESS) {
  236. std::cerr << "应该拒绝不存在的解码器" << std::endl;
  237. return false;
  238. }
  239. // 测试状态错误
  240. auto decoder2 = std::make_unique<VideoDecoder>();
  241. VideoDecoderParams emptyParams;
  242. result = decoder2->open(emptyParams); // 空参数打开
  243. if (result == ErrorCode::SUCCESS) {
  244. std::cerr << "应该拒绝空参数的打开操作" << std::endl;
  245. return false;
  246. }
  247. std::cout << "错误处理测试通过" << std::endl;
  248. return true;
  249. } catch (const std::exception& e) {
  250. std::cerr << "错误处理测试异常: " << e.what() << std::endl;
  251. return false;
  252. }
  253. }
  254. // 运行所有测试
  255. bool runAllTests() {
  256. std::cout << "\n开始运行解码器测试套件..." << std::endl;
  257. bool allPassed = true;
  258. allPassed &= testVideoDecoderBasic();
  259. allPassed &= testAudioDecoderBasic();
  260. allPassed &= testHardwareDecoder();
  261. allPassed &= testDecoderFactory();
  262. allPassed &= testDecoderStateManagement();
  263. allPassed &= testErrorHandling();
  264. std::cout << "\n=== 测试结果 ===" << std::endl;
  265. if (allPassed) {
  266. std::cout << "✅ 所有测试通过!" << std::endl;
  267. } else {
  268. std::cout << "❌ 部分测试失败!" << std::endl;
  269. }
  270. return allPassed;
  271. }
  272. };
  273. int main() {
  274. try {
  275. DecoderTester tester;
  276. bool success = tester.runAllTests();
  277. return success ? 0 : 1;
  278. } catch (const std::exception& e) {
  279. std::cerr << "程序异常: " << e.what() << std::endl;
  280. return 1;
  281. } catch (...) {
  282. std::cerr << "未知异常" << std::endl;
  283. return 1;
  284. }
  285. }