123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #ifndef TRANSJPEGWEB_H
- #define TRANSJPEGWEB_H
- #include "CivetServer.h"
- #include <cstring>
- #include <mutex>
- #include <iostream>
- #ifdef _WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #endif
- #include "rtspclientdown.h"
- #include "ivh264framedecode.h"
- #include <QFile>
- #define NUM_CAM 4
- #ifdef USE_WEBSOCKET
- class WebSocketHandler : public CivetWebSocketHandler {
- std::vector<struct mg_connection *> mvectorconn;
- std::mutex mMutex;
- int mnwscount = 0;
- public:
- int GetCount()
- {
- return mnwscount;
- }
- void BroadData(const char * strdata,int ndata)
- {
- int i;
- mMutex.lock();
- for(i=0;i<(int)mvectorconn.size();i++)
- {
- std::cout<<" send frame data . size : "<<ndata<<std::endl;
- mg_websocket_write(mvectorconn[i], MG_WEBSOCKET_OPCODE_BINARY, strdata, ndata);
- mnwscount++;
- }
- mMutex.unlock();
- }
- int a = 1;
- private:
- virtual bool handleConnection(CivetServer *server,
- const struct mg_connection *conn) {
- printf("WS connected\n");
- return true;
- }
- virtual void handleReadyState(CivetServer *server,
- struct mg_connection *conn) {
- printf("WS ready\n");
- mMutex.lock();
- mvectorconn.push_back(conn);
- mMutex.unlock();
- }
- virtual bool handleData(CivetServer *server,
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len) {
- return true;
- // return (data_len<5);
- }
- virtual void handleClose(CivetServer *server,
- const struct mg_connection *conn) {
- printf("WS closed\n");
- int i;
- mMutex.lock();
- for(i=0;i<(int)mvectorconn.size();i++)
- {
- if(conn == mvectorconn[i])
- {
- mvectorconn.erase(mvectorconn.begin()+i);
- break;
- }
- }
- mMutex.unlock();
- }
- };
- #endif
- class PicHandler : public CivetHandler
- {
- private:
- int64_t mnpicuptime = 0;
- std::shared_ptr<char> mpstr_data;
- int mnpicsize;
- std::mutex mmutexdata;
- int mnpiccount = 0;
- public:
- int GetCount()
- {
- return mnpiccount;
- }
- void SetData(std::shared_ptr<char> pstr_ptr,int ndatasize)
- {
- mmutexdata.lock();
- mnpicsize = ndatasize;
- mpstr_data = pstr_ptr;
- mnpicuptime = std::chrono::system_clock::now().time_since_epoch().count();
- mmutexdata.unlock();
- }
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- std::shared_ptr<char> pstr_ptr;
- int npicsize;
- int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count();
- int64_t ndiff = nnow - mnpicuptime;
- if(abs(ndiff/1000000)>3000)
- {
- return false;
- }
- else
- {
- mmutexdata.lock();
- pstr_ptr = mpstr_data;
- npicsize = mnpicsize;
- mmutexdata.unlock();
- }
- static int ncount;
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\n"
- "Connection: close\r\n"
- "Max-Age: 0\r\n"
- "Expires: 0\r\n"
- "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
- "Pragma: no-cache\r\n"
- "Content-Type: multipart/x-mixed-replace; "
- "boundary=--BoundaryString\r\n"
- "\r\n");
- mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
- mg_printf(conn,
- "<script type=\"text/javascript\">\r\n"
- "function myrefresh() {\r\n"
- "window.location.reload();\r\n"
- "}\r\n"
- "setTimeout('myrefresh()', 1000);\r\n"
- "</script>\r\n");
- mg_printf(conn,
- "--BoundaryString\r\n"
- "Content-type: image/jpeg\r\n"
- "Content-Length: %zu\r\n"
- "\r\n",
- npicsize);
- mg_write(conn, pstr_ptr.get(), npicsize);
- mg_printf(conn, "\r\n\r\n");
- ncount++;
- // printf("send pic. %d\n",ncount);
- mnpiccount++;
- return true;
- }
- };
- class WsStartHandler : public CivetHandler
- {
- private:
- int mnIndexHtmlMode = 0; //if 0 use indexpic.html, if 1 use websocket.
- public:
- void SetIndexMode(int nmode)
- {
- if(nmode == 0)mnIndexHtmlMode = 0;
- else mnIndexHtmlMode = 1;
- }
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
- "close\r\n\r\n");
- mg_printf(conn, "<!DOCTYPE html>\n");
- mg_printf(conn, "<html>\n<head>\n");
- mg_printf(conn, "<meta charset=\"UTF-8\">\n");
- mg_printf(conn, "<title>ADC IV Web UI</title>\n");
- QFile xFile;
- if(mnIndexHtmlMode == 0)
- {
- xFile.setFileName(":/indexpic.html");
- }
- else
- {
- xFile.setFileName(":/indexws.html");
- }
- if(xFile.open(QIODevice::ReadOnly))
- {
- QByteArray ba = xFile.readAll();
- mg_printf(conn,ba.data());
- }
- return 1;
- }
- };
- namespace iv {
- struct transjpegcount
- {
- int nrtspretry;
- int nframecount;
- int njpegcount;
- int npicreqcount;
- int nwsreqcount;
- };
- }
- class transjpegweb
- {
- public:
- transjpegweb(std::string strip,std::string strport,std::string strvin,std::string strpass,std::string strwebmode);
- private:
- CivetServer * mpserver;
- std::string mstrrtspserverport = "9554";
- std::string mstrrtspserverip = "111.33.136.149";
- std::string mstrvin = "AAAAAAAAAAAAAAAAA";
- std::string mstrrtsppass = "hello";
- rtspclientdown * mprtspdown[4];
- ivh264framedecode * mph264decode[NUM_CAM];
- std::thread * mpthreadframe[NUM_CAM];
- std::thread * mpthreadpic[NUM_CAM];
- int mnframewidth = 1920;
- int mnframeheight = 1080;
- void threadframe(int ncam);
- void threadpic(int ncam);
- bool mbthreadrun = true;
- WebSocketHandler * mpws[NUM_CAM];
- PicHandler * mppichandler[NUM_CAM];
- WsStartHandler * mphandleindex;
- iv::transjpegcount mtjcount[NUM_CAM];
- std::string mstrrtspstring[NUM_CAM];
- public:
- std::string GetStatics();
- };
- #endif // TRANSJPEGWEB_H
|