test_seek_pause_fixed.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "code/player/player_core_v2.h"
  2. #include "code/base/logger.h"
  3. #include <iostream>
  4. #include <fstream>
  5. #include <thread>
  6. #include <chrono>
  7. using namespace av::player;
  8. using namespace av;
  9. class TestCallback : public PlayerEventCallback {
  10. public:
  11. TestCallback(std::ofstream& output) : outputFile(output) {}
  12. void onStateChanged(PlayerState newState) override {
  13. std::cout << "State changed to: " << static_cast<int>(newState) << std::endl;
  14. currentState = newState;
  15. }
  16. void onMediaInfoChanged(const MediaInfo& info) override {
  17. std::cout << "Media info: " << info.filename << ", duration: " << info.duration
  18. << std::endl;
  19. }
  20. void onPositionChanged(int64_t position) override {
  21. std::cout << "Position: " << position / 1000000.0 << "s" << std::endl;
  22. currentPosition = position;
  23. }
  24. void onErrorOccurred(const std::string& error) override {
  25. std::cout << "Error: " << error << std::endl;
  26. }
  27. void onEndOfFile() override { std::cout << "End of file" << std::endl; }
  28. void onSyncError(double error, const std::string& reason) override
  29. {
  30. std::cout << "Sync error: " << error << ", reason: " << reason << std::endl;
  31. }
  32. void onFrameDropped(int64_t count) override
  33. {
  34. std::cout << "Frame dropped: " << count << std::endl;
  35. }
  36. void onVideoFrameReady(AVFrame* frame) override
  37. {
  38. // 简单处理视频帧
  39. }
  40. void onVideoRendererInitRequired(int width, int height) override
  41. {
  42. std::cout << "Video renderer init: " << width << "x" << height << std::endl;
  43. }
  44. void onVideoRendererCloseRequired() override
  45. {
  46. std::cout << "Video renderer close" << std::endl;
  47. }
  48. PlayerState currentState = PlayerState::Idle;
  49. int64_t currentPosition = 0;
  50. private:
  51. std::ofstream& outputFile;
  52. };
  53. int main() {
  54. // 创建输出文件
  55. std::ofstream outputFile("test_seek_pause_output.txt");
  56. if (!outputFile.is_open()) {
  57. std::cerr << "无法创建输出文件" << std::endl;
  58. return 1;
  59. }
  60. // 初始化日志
  61. Logger::instance().initialize("test.log", LogLevel::ERROR, false, true);
  62. Logger::instance().info("PlayerCoreV2 Example Started");
  63. // 创建播放器
  64. PlayerCoreV2 player;
  65. TestCallback callback(outputFile);
  66. player.setEventCallback(&callback);
  67. std::cout << "=== 测试修复后的暂停后seek功能 ===" << std::endl;
  68. // 请用户提供测试文件路径
  69. std::string filename = "C:/Users/zhuizhu/Videos/2.mp4";
  70. // std::cout << "请输入测试视频文件路径: ";
  71. // std::getline(std::cin, filename);
  72. // if (filename.empty()) {
  73. // std::cout << "未提供文件路径,退出测试" << std::endl;
  74. // return 1;
  75. // }
  76. // 打开文件
  77. std::cout << "\n1. 打开文件: " << filename << std::endl;
  78. auto result = player.openFile(filename);
  79. if (result != ErrorCode::SUCCESS) {
  80. std::cout << "打开文件失败" << std::endl;
  81. return 1;
  82. }
  83. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  84. // 开始播放
  85. std::cout << "\n2. 开始播放" << std::endl;
  86. player.play();
  87. std::this_thread::sleep_for(std::chrono::seconds(3));
  88. // 暂停
  89. std::cout << "\n3. 暂停播放" << std::endl;
  90. player.pause();
  91. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  92. // 获取当前位置
  93. int64_t pausePosition = player.getCurrentTime();
  94. std::cout << "暂停时位置: " << pausePosition / 1000000.0 << "s" << std::endl;
  95. // 在暂停状态下seek
  96. int64_t seekTarget = pausePosition + 5000000; // 向前seek 5秒
  97. std::cout << "\n4. 在暂停状态下seek到: " << seekTarget / 1000000.0 << "s" << std::endl;
  98. player.seek(seekTarget);
  99. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  100. // 检查seek后的位置
  101. int64_t afterSeekPosition = player.getCurrentTime();
  102. std::cout << "seek后位置: " << afterSeekPosition / 1000000.0 << "s" << std::endl;
  103. std::cout << "状态: " << static_cast<int>(callback.currentState) << std::endl;
  104. // 验证结果
  105. if (callback.currentState == PlayerState::Paused) {
  106. std::cout << "✓ 状态正确:保持暂停状态" << std::endl;
  107. } else {
  108. std::cout << "✗ 状态错误:应该保持暂停状态" << std::endl;
  109. }
  110. if (std::abs(afterSeekPosition - seekTarget) < 1000000) { // 允许1秒误差
  111. std::cout << "✓ 位置正确:seek到目标位置" << std::endl;
  112. } else {
  113. std::cout << "✗ 位置错误:seek位置不正确" << std::endl;
  114. std::cout << " 期望: " << seekTarget / 1000000.0 << "s" << std::endl;
  115. std::cout << " 实际: " << afterSeekPosition / 1000000.0 << "s" << std::endl;
  116. }
  117. // 恢复播放测试
  118. std::cout << "\n5. 恢复播放测试" << std::endl;
  119. player.play();
  120. std::this_thread::sleep_for(std::chrono::seconds(2));
  121. int64_t playPosition = player.getCurrentTime();
  122. std::cout << "恢复播放后位置: " << playPosition / 1000000.0 << "s" << std::endl;
  123. if (playPosition > afterSeekPosition) {
  124. std::cout << "✓ 播放正常:时间在前进" << std::endl;
  125. } else {
  126. std::cout << "✗ 播放异常:时间没有前进" << std::endl;
  127. }
  128. // 测试连续seek
  129. std::cout << "\n6. 测试连续seek" << std::endl;
  130. player.pause();
  131. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  132. // 第一次seek
  133. int64_t seek1 = seekTarget + 2000000; // 再向前2秒
  134. std::cout << "第一次seek到: " << seek1 / 1000000.0 << "s" << std::endl;
  135. player.seek(seek1);
  136. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  137. int64_t pos1 = player.getCurrentTime();
  138. std::cout << "第一次seek后位置: " << pos1 / 1000000.0 << "s" << std::endl;
  139. // 第二次seek
  140. int64_t seek2 = seek1 - 3000000; // 向后3秒
  141. std::cout << "第二次seek到: " << seek2 / 1000000.0 << "s" << std::endl;
  142. player.seek(seek2);
  143. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  144. int64_t pos2 = player.getCurrentTime();
  145. std::cout << "第二次seek后位置: " << pos2 / 1000000.0 << "s" << std::endl;
  146. if (callback.currentState == PlayerState::Paused) {
  147. std::cout << "✓ 连续seek测试:状态保持暂停" << std::endl;
  148. } else {
  149. std::cout << "✗ 连续seek测试:状态错误" << std::endl;
  150. }
  151. // 停止播放
  152. std::cout << "\n7. 停止播放" << std::endl;
  153. player.stop();
  154. std::cout << "\n=== 测试完成 ===" << std::endl;
  155. // 关闭输出文件
  156. outputFile.close();
  157. std::cout << "测试完成,结果已保存到 test_seek_pause_output.txt" << std::endl;
  158. return 0;
  159. }