forked from JPery/MJPEGWriter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMJPEGWriter.h
103 lines (76 loc) · 1.71 KB
/
MJPEGWriter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef MJPEG_MJPEG_H
#define MJPEG_MJPEG_H
#ifdef WIN32
#include <Ws2tcpip.h>
#include <winsock2.h>
#include <Windows.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <csignal>
#define SOCKET int
#define HOSTENT struct hostent
#define SOCKADDR struct sockaddr
#define SOCKADDR_IN struct sockaddr_in
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <thread>
#include <mutex>
#include <array>
#include <opencv2/opencv.hpp>
struct clientFrame
{
unsigned char *outbuf;
int outlen;
int client;
};
struct clientPayload
{
void *context;
clientFrame cf;
};
void mySleep(int sleepMs);
class MJPEG
{
public:
MJPEG(int port = 0);
~MJPEG();
bool release();
bool open();
bool isOpened();
void start();
void stop();
void write(cv::Mat &frame);
bool startError = true;
private:
SOCKET sock;
fd_set master;
int timeout;
int quality; // jpeg compression [1..100]
std::vector<int> clients;
std::thread *thread_listen, *thread_write;
std::mutex mutex_writer, mutex_cout, mutex_client;
cv::Mat lastFrame;
int port;
const static unsigned short num_connections = 10;
int _write(int sock, char *s, int len);
int _read(int socket, char *buffer);
static void listen_Helper(void *context);
static void writer_Helper(void *context);
static void *clientWrite_Helper(void *payload);
void Listener();
void Writer();
void ClientWrite(clientFrame &cf);
};
#endif //MJPEG_MJPEG_H