我想使用mongoose轻量级web服务器实现MJPEG流。有人能给我一些关于我如何有效地做到这一点的指示吗?我已经有了需要流式处理的jpeg文件,并且我理解MJPEG概念。我只需要知道如何从协议级别实现MJPEG流。需要实现mjpeg流
0
A
回答
1
几乎所有ip摄像头实现的http上的mjpeg是multipart mime消息的子类型。 http://en.wikipedia.org/wiki/MIME#Mixed-Replace
你必须用你的实现来替换cvloop()来填充jbuf。
#include <vector>
#include "mongoose.h"
#ifdef _WIN32
#include <windows.h>
#define SLEEP(ms) Sleep(ms)
#endif
#ifdef __linux__
#define SLEEP(ms) usleep(ms*1000)
#endif
std::vector<unsigned char> jbuf;//fill this with a single jpeg image data
#include "opencv2/highgui/highgui.hpp"
void cvloop()
{
cv::VideoCapture cap("c:/toprocess/frame_%004d.jpg");
cv::Mat frame;
for(;;)
{
cap>>frame;
std::vector<unsigned char> buffer;
std::vector<int> param(2);
param[0]=CV_IMWRITE_JPEG_QUALITY;
param[1]=40;
imencode(".jpg",frame,buffer,param);
jbuf.swap(buffer);
SLEEP(50);
}
}
// This function will be called by mongoose on every new request.
static int begin_request_handler(struct mg_connection *conn) {
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=b\r\n"
"Cache-Control: no-store\r\n"
"Pragma: no-cache\r\n"
"Connection: close\r\n"
"\r\n");
for(;;)
{
if(jbuf.size()<4 ||
(jbuf[0]!=0xff && jbuf[0]!=0xd8) ||
(jbuf[jbuf.size()-2]!=0xff && jbuf[jbuf.size()-1]!=0xd9))
{
SLEEP(10);
continue;
}
mg_printf(conn,
"--b\r\n"
"Content-Type: image/jpeg\r\n"
"Content-length: %d\r\n"
"\r\n",jbuf.size());
int ret=mg_write(conn,&jbuf[0], jbuf.size());
if(ret==0||ret==-1) return 1;
SLEEP(50);
}
return 1;
}
int main(void) {
struct mg_context *ctx;
struct mg_callbacks callbacks;
// List of options. Last element must be NULL.
const char *options[] = {"listening_ports", "8080",NULL};
// Prepare callbacks structure. We have only one callback, the rest are NULL.
memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
// Start the web server.
ctx = mg_start(&callbacks, NULL, options);
cvloop();
// Wait until user hits "enter". Server is running in separate thread.
// Navigating to http://localhost:8080 will invoke begin_request_handler().
getchar();
// Stop the server.
mg_stop(ctx);
return 0;
}
0
自2014年3月4日有实施猫鼬样品mjpeg
相关问题
- 1. iOS和实时流mjpeg
- 2. 写MJPEG流盘
- 3. 需要实现XSLT
- 4. Mjpeg VLC和HTTP流
- 5. 实现需要用foldr
- 6. 需要示例实现MessageHeaders.WriteHeaderContents
- 7. 需要PEP302实现细节
- 8. 何时需要实现operator []?
- 9. MJPEG流媒体和解码
- 10. 查看MJPEG视频流
- 11. VLC - 如何发挥MJPEG流
- 12. MPlayer如何识别MJPEG流?
- 13. 使用GStreamer创建MJPEG流
- 14. 延迟MJPEG流10秒
- 15. MJPEG网络流到OpenCV 2
- 16. 的Android连接到MJPEG流 -
- 17. 需要建议:如何实现动态工作流创建
- 18. 为什么我需要我需要在子类中实现IDisposable()
- 19. 流实现
- 20. 我的LinkedBlockingQueue实现需要同步吗?
- 21. 在java中需要内部实现HashTable
- 22. 需要一个ROT47 JavaScript实现
- 23. 在实现omnipay需要帮助
- 24. Android C2DM需要的简单实现
- 25. C2DM collapse_key实现需要说明
- 26. 如何实现从不需要投
- 27. 需要实现签名捕获
- 28. 在n树实现中需要帮助
- 29. XMPP服务器(Openfire)需要实现吗?
- 30. 在Hashtable实现中需要帮助