RvcWsServer.h 3.1 KB

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