RvcWsServer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #pragma once
  2. #include "websocketpp/config/asio_no_tls.hpp"
  3. #include "websocketpp/config/asio.hpp"
  4. #include "websocketpp/server.hpp"
  5. #include <boost/asio.hpp>
  6. #include <boost/thread/mutex.hpp>
  7. #include <functional>
  8. #include <memory>
  9. #include <vector>
  10. #ifdef _WIN32
  11. #include <Windows.h>
  12. #include <process.h>
  13. #else
  14. #include <semaphore.h>
  15. #endif
  16. #ifndef RVC_LIVENESS_WS_PORT
  17. #define RVC_LIVENESS_WS_PORT 9100
  18. #endif
  19. #ifndef RVC_WS_INIT_STR
  20. #define RVC_WS_INIT_STR "rvc wbsocket trans init"
  21. #endif
  22. #ifndef RVC_WS_START_TRANS_STR
  23. #define RVC_WS_START_TRANS_STR "rvc wbsocket start trans"
  24. #endif
  25. #ifndef RVC_WS_STOP_TRANS_STR
  26. #define RVC_WS_STOP_TRANS_STR "rvc wbsocket stop trans"
  27. #endif
  28. #ifndef RVC_WS_CHANGE_CAMERA_STR
  29. #define RVC_WS_CHANGE_CAMERA_STR "rvc wbsocket change camera"
  30. #endif
  31. #ifndef RVC_WS_START_CAPTURE_STR
  32. #define RVC_WS_START_CAPTURE_STR "rvc wbsocket start capture"
  33. #endif
  34. #ifndef RVC_WS_CONNECT_IDENTIFIER
  35. #define RVC_WS_CONNECT_IDENTIFIER "+"
  36. #endif
  37. #ifndef RVC_DEFAULT_FPS
  38. #define RVC_DEFAULT_FPS 5
  39. #endif
  40. #ifndef RVC_MIN_FPS
  41. #define RVC_MIN_FPS 1
  42. #endif
  43. #ifndef RVC_MAX_FPS
  44. #define RVC_MAX_FPS 30
  45. #endif
  46. namespace LivenessDetection{
  47. enum eVideoType{
  48. ePreview_Type,
  49. eCapture_Type
  50. };
  51. enum eCameraType{
  52. eCamera_Env,
  53. eCamera_Opt
  54. };
  55. typedef websocketpp::server<websocketpp::config::asio> server;
  56. typedef struct websocket_callback_s {
  57. int (*on_get_videodata)(eVideoType eType, eCameraType ecameraid, int* width, int* height, unsigned char* bmpdata, int isize, void* user_data);
  58. void *user_data;
  59. }websocket_callback_t;
  60. typedef struct rvc_video_param_s{
  61. int iwidth;
  62. int iheight;
  63. int icapwidth;
  64. int icapheight;
  65. }rvc_video_param_t;
  66. class RvcWsServer
  67. {
  68. public:
  69. RvcWsServer(void);
  70. ~RvcWsServer(void);
  71. int Init_WsServer(websocket_callback_t* pcallback, rvc_video_param_t* pparam, int iport = RVC_LIVENESS_WS_PORT);
  72. int StartVideoTransmit();
  73. int StopVideoTransmit();
  74. int StartVideoCapTransmit();
  75. private:
  76. bool validate(websocketpp::connection_hdl hdl);
  77. void on_http(websocketpp::connection_hdl hdl);
  78. void on_fail(websocketpp::connection_hdl hdl);
  79. void on_open(websocketpp::connection_hdl hdl);
  80. void on_close(websocketpp::connection_hdl hdl);
  81. void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg);
  82. int handle_initial_instructions(websocketpp::connection_hdl hdl, std::string strinstrut);
  83. int handle_change_camera_instructions(std::string strinstrut);
  84. public:
  85. volatile bool m_bconnected; // connect flag
  86. volatile bool m_bstarttrans; // start transmit
  87. volatile eCameraType m_ecameraid; // camera id
  88. long m_lstarttime;
  89. #if defined(_WIN32)
  90. HANDLE m_work_thread;
  91. HANDLE m_evt;
  92. #else
  93. pthread_t m_work_threadid;
  94. sem_t m_semt;
  95. #endif //RVC_OS_WIN
  96. websocket_callback_t m_callback;
  97. server m_wsserver; // websocket server,用于和业务层传输视频流,默认端口9100
  98. websocketpp::connection_hdl m_hdl;
  99. int m_cameraid;
  100. int m_fps;
  101. unsigned char* m_buffer;
  102. uint32_t m_ubuffer_size;
  103. unsigned char* m_capbuffer;
  104. uint32_t m_ucapbuffer_size;
  105. unsigned int m_utranstime;
  106. bool m_preview_error_log;
  107. bool m_capture_error_log;
  108. private:
  109. int m_listenport; // websocket server监听端口
  110. std::string m_struuid; // websocket connet flag
  111. };
  112. }