RvcWsServer.h 2.9 KB

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