VideoPlayerAPI.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "VideoPlayerAPI.h"
  2. #include <QApplication>
  3. #include <QLibrary>
  4. #include <qchar.h>
  5. #include <qlibrary.h>
  6. #include "spdlog/spdlog.h"
  7. /* 定义函数 */
  8. using FuncCreateVideoPlayer = IVideoPlayer* (*)();
  9. using FuncDestroyVideoPlayer = void (*)(IVideoPlayer* player);
  10. /* 创建函数指针 */
  11. FuncCreateVideoPlayer pCreateVideoPlayer = nullptr;
  12. FuncDestroyVideoPlayer pDestroyVideoPlayer = nullptr;
  13. /* 加载动态库 */
  14. bool loadVideoPlayerLibrary()
  15. {
  16. QString libPath = QApplication::applicationDirPath();
  17. #ifdef QT_DEBUG
  18. libPath += "/libVideoPlayerd"; // Debug版本
  19. #else
  20. libPath += "/libVideoPlayer"; // Release版本
  21. #endif
  22. #if defined (Q_OS_LINUX)
  23. libPath += ".so"; // 或者 .dll 或者 .dylib
  24. #elif defined (Q_OS_WIN)
  25. libPath += ".dll"; // Windows下的动态库
  26. #elif defined (Q_OS_MACOS)
  27. libPath += ".dylib"; // macOS下的动态库
  28. #endif
  29. QLibrary lib(libPath);
  30. if(!lib.load())
  31. {
  32. SPDLOG_ERROR("加载VideoPlayerAPI库失败: {}", lib.errorString().toStdString());
  33. SPDLOG_ERROR("VideoPlayer动态库路径: {}", libPath.toStdString());
  34. return false;
  35. }
  36. pCreateVideoPlayer = reinterpret_cast<FuncCreateVideoPlayer>(lib.resolve("createVideoPlayer"));
  37. if(!pCreateVideoPlayer)
  38. {
  39. SPDLOG_ERROR("无法解析createVideoPlayer函数: {}", lib.errorString().toStdString());
  40. return false;
  41. }
  42. pDestroyVideoPlayer = reinterpret_cast<FuncDestroyVideoPlayer>(lib.resolve("destroyVideoPlayer"));
  43. if(!pDestroyVideoPlayer)
  44. {
  45. SPDLOG_ERROR("无法解析destroyVideoPlayer函数: {}", lib.errorString().toStdString());
  46. return false;
  47. }
  48. return true;
  49. }
  50. /* 创建播放器 */
  51. IVideoPlayer* createPlayer()
  52. {
  53. if(pCreateVideoPlayer == nullptr)
  54. {
  55. SPDLOG_ERROR("createVideoPlayer函数指针为空,请先加载动态库");
  56. return nullptr;
  57. }
  58. return pCreateVideoPlayer();
  59. }
  60. /* 销毁播放器 */
  61. void destroyPlayer(IVideoPlayer* player)
  62. {
  63. if(pDestroyVideoPlayer == nullptr)
  64. {
  65. SPDLOG_ERROR("destroyVideoPlayer函数指针为空,请先加载动态库");
  66. return;
  67. }
  68. pDestroyVideoPlayer(player);
  69. }