transjpegweb.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef TRANSJPEGWEB_H
  2. #define TRANSJPEGWEB_H
  3. #include "CivetServer.h"
  4. #include <cstring>
  5. #include <mutex>
  6. #include <iostream>
  7. #ifdef _WIN32
  8. #include <windows.h>
  9. #else
  10. #include <unistd.h>
  11. #endif
  12. #include "rtspclientdown.h"
  13. #include "ivh264framedecode.h"
  14. #include <QFile>
  15. #define NUM_CAM 4
  16. #ifdef USE_WEBSOCKET
  17. class WebSocketHandler : public CivetWebSocketHandler {
  18. std::vector<struct mg_connection *> mvectorconn;
  19. std::mutex mMutex;
  20. int mnwscount = 0;
  21. public:
  22. int GetCount()
  23. {
  24. return mnwscount;
  25. }
  26. void BroadData(const char * strdata,int ndata)
  27. {
  28. int i;
  29. mMutex.lock();
  30. for(i=0;i<(int)mvectorconn.size();i++)
  31. {
  32. std::cout<<" send frame data . size : "<<ndata<<std::endl;
  33. mg_websocket_write(mvectorconn[i], MG_WEBSOCKET_OPCODE_BINARY, strdata, ndata);
  34. mnwscount++;
  35. }
  36. mMutex.unlock();
  37. }
  38. int a = 1;
  39. private:
  40. virtual bool handleConnection(CivetServer *server,
  41. const struct mg_connection *conn) {
  42. printf("WS connected\n");
  43. return true;
  44. }
  45. virtual void handleReadyState(CivetServer *server,
  46. struct mg_connection *conn) {
  47. printf("WS ready\n");
  48. mMutex.lock();
  49. mvectorconn.push_back(conn);
  50. mMutex.unlock();
  51. }
  52. virtual bool handleData(CivetServer *server,
  53. struct mg_connection *conn,
  54. int bits,
  55. char *data,
  56. size_t data_len) {
  57. return true;
  58. // return (data_len<5);
  59. }
  60. virtual void handleClose(CivetServer *server,
  61. const struct mg_connection *conn) {
  62. printf("WS closed\n");
  63. int i;
  64. mMutex.lock();
  65. for(i=0;i<(int)mvectorconn.size();i++)
  66. {
  67. if(conn == mvectorconn[i])
  68. {
  69. mvectorconn.erase(mvectorconn.begin()+i);
  70. break;
  71. }
  72. }
  73. mMutex.unlock();
  74. }
  75. };
  76. #endif
  77. class PicHandler : public CivetHandler
  78. {
  79. private:
  80. int64_t mnpicuptime = 0;
  81. std::shared_ptr<char> mpstr_data;
  82. int mnpicsize;
  83. std::mutex mmutexdata;
  84. int mnpiccount = 0;
  85. public:
  86. int GetCount()
  87. {
  88. return mnpiccount;
  89. }
  90. void SetData(std::shared_ptr<char> pstr_ptr,int ndatasize)
  91. {
  92. mmutexdata.lock();
  93. mnpicsize = ndatasize;
  94. mpstr_data = pstr_ptr;
  95. mnpicuptime = std::chrono::system_clock::now().time_since_epoch().count();
  96. mmutexdata.unlock();
  97. }
  98. bool
  99. handleGet(CivetServer *server, struct mg_connection *conn)
  100. {
  101. std::shared_ptr<char> pstr_ptr;
  102. int npicsize;
  103. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count();
  104. int64_t ndiff = nnow - mnpicuptime;
  105. if(abs(ndiff/1000000)>3000)
  106. {
  107. return false;
  108. }
  109. else
  110. {
  111. mmutexdata.lock();
  112. pstr_ptr = mpstr_data;
  113. npicsize = mnpicsize;
  114. mmutexdata.unlock();
  115. }
  116. static int ncount;
  117. mg_printf(conn,
  118. "HTTP/1.1 200 OK\r\n"
  119. "Connection: close\r\n"
  120. "Max-Age: 0\r\n"
  121. "Expires: 0\r\n"
  122. "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
  123. "Pragma: no-cache\r\n"
  124. "Content-Type: multipart/x-mixed-replace; "
  125. "boundary=--BoundaryString\r\n"
  126. "\r\n");
  127. mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
  128. mg_printf(conn,
  129. "<script type=\"text/javascript\">\r\n"
  130. "function myrefresh() {\r\n"
  131. "window.location.reload();\r\n"
  132. "}\r\n"
  133. "setTimeout('myrefresh()', 1000);\r\n"
  134. "</script>\r\n");
  135. mg_printf(conn,
  136. "--BoundaryString\r\n"
  137. "Content-type: image/jpeg\r\n"
  138. "Content-Length: %zu\r\n"
  139. "\r\n",
  140. npicsize);
  141. mg_write(conn, pstr_ptr.get(), npicsize);
  142. mg_printf(conn, "\r\n\r\n");
  143. ncount++;
  144. // printf("send pic. %d\n",ncount);
  145. mnpiccount++;
  146. return true;
  147. }
  148. };
  149. class WsStartHandler : public CivetHandler
  150. {
  151. private:
  152. int mnIndexHtmlMode = 0; //if 0 use indexpic.html, if 1 use websocket.
  153. public:
  154. void SetIndexMode(int nmode)
  155. {
  156. if(nmode == 0)mnIndexHtmlMode = 0;
  157. else mnIndexHtmlMode = 1;
  158. }
  159. bool
  160. handleGet(CivetServer *server, struct mg_connection *conn)
  161. {
  162. mg_printf(conn,
  163. "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
  164. "close\r\n\r\n");
  165. mg_printf(conn, "<!DOCTYPE html>\n");
  166. mg_printf(conn, "<html>\n<head>\n");
  167. mg_printf(conn, "<meta charset=\"UTF-8\">\n");
  168. mg_printf(conn, "<title>ADC IV Web UI</title>\n");
  169. QFile xFile;
  170. if(mnIndexHtmlMode == 0)
  171. {
  172. xFile.setFileName(":/indexpic.html");
  173. }
  174. else
  175. {
  176. xFile.setFileName(":/indexws.html");
  177. }
  178. if(xFile.open(QIODevice::ReadOnly))
  179. {
  180. QByteArray ba = xFile.readAll();
  181. mg_printf(conn,ba.data());
  182. }
  183. return 1;
  184. }
  185. };
  186. namespace iv {
  187. struct transjpegcount
  188. {
  189. int nrtspretry;
  190. int nframecount;
  191. int njpegcount;
  192. int npicreqcount;
  193. int nwsreqcount;
  194. };
  195. }
  196. class transjpegweb
  197. {
  198. public:
  199. transjpegweb(std::string strip,std::string strport,std::string strvin,std::string strpass,std::string strwebmode);
  200. private:
  201. CivetServer * mpserver;
  202. std::string mstrrtspserverport = "9554";
  203. std::string mstrrtspserverip = "111.33.136.149";
  204. std::string mstrvin = "AAAAAAAAAAAAAAAAA";
  205. std::string mstrrtsppass = "hello";
  206. rtspclientdown * mprtspdown[4];
  207. ivh264framedecode * mph264decode[NUM_CAM];
  208. std::thread * mpthreadframe[NUM_CAM];
  209. std::thread * mpthreadpic[NUM_CAM];
  210. int mnframewidth = 1920;
  211. int mnframeheight = 1080;
  212. void threadframe(int ncam);
  213. void threadpic(int ncam);
  214. bool mbthreadrun = true;
  215. WebSocketHandler * mpws[NUM_CAM];
  216. PicHandler * mppichandler[NUM_CAM];
  217. WsStartHandler * mphandleindex;
  218. iv::transjpegcount mtjcount[NUM_CAM];
  219. std::string mstrrtspstring[NUM_CAM];
  220. public:
  221. std::string GetStatics();
  222. };
  223. #endif // TRANSJPEGWEB_H