//main.cpp
#include <iostream>
#include "cameradata.h"
#include <unistd.h>
#include "mongoose.h"
#include <time.h>
using namespace std;
Camera m_camera;
unsigned char data[IMAGE_W*IMAGE_H*2] = {0};
static void ev_handler(struct mg_connection *connection, int ev, void *p) {
int len;
if (ev == MG_EV_HTTP_REQUEST) {
mg_printf(connection, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
// time_t start_t, end_t;
len = m_camera.GetCameraData(data);
// time(&start_t);
mg_send_http_chunk(connection, (const char*)data, len);
mg_send_http_chunk(connection, "", 0);
/* time(&end_t);
double diff_t;
diff_t = difftime(end_t, start_t);
printf("执行时间 = %f\n", diff_t);*/
}
return;
}
int main()
{
cout << "Hello World!" << endl;
m_camera.CameraInit("/dev/video0");
struct mg_mgr mgr;
struct mg_connection *c;
mg_mgr_init(&mgr, NULL);
c = mg_bind(&mgr, "9005", ev_handler);
mg_set_protocol_http_websocket(c);
while(1) {
mg_mgr_poll(&mgr, 1000);
}
m_camera.CameraClose();
mg_mgr_free(&mgr);
return 0;
}
m_camera 看之前v4l2博客,
mongoose版本为6.17,github上下载源码