forked from billw2/pikrellcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpserver_mjpeg.c
261 lines (216 loc) · 5.83 KB
/
tcpserver_mjpeg.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* MJPEG TCP server
* Brijesh Singh <[email protected]>
*
* Live preview of the MJPEG Camera Stream. The stream can be viewed directly on browser
* by accessing http://<YOUR_IP>:8081/mjpeg_live.php or Andriod MJPEG viewer apps
* (tinycam monitor etc).
*
* Eventhough the server is designed to work with single producer and consumer mind but we do
* not refuse the connection from more than one clients.
*/
#include "pikrellcam.h"
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#define MAX_WIDTH 1920
#define MAX_HEIGHT 1080
#define MAX_IMAGE_SIZE (MAX_WIDTH * MAX_HEIGHT * 1.5) /* worst case compression */
#define SERVER_PORT 9999
#define MAX_BUF_SIZE 1024
#define NUM_CIRC_BUFS 2
struct buffer {
int len;
char *data;
};
struct client_info {
int fd;
struct sockaddr_in sockaddr;
};
static int server_fd;
static int head, tail;
static pthread_t handler_tid;
static struct buffer buffers[NUM_CIRC_BUFS];
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static int new_connection_log_count;
/* returned a new buffer, after use the buffer must be free'd with image_buffer_free() */
static struct buffer* client_queue_get()
{
struct buffer *buf;
buf = calloc(1, sizeof(*buf));
if (!buf)
return NULL;
buf->data = malloc(MAX_IMAGE_SIZE);
if (!buf->data) {
free(buf);
return NULL;
}
pthread_mutex_lock(&mutex);
if (head == tail) /* queue is empty */
pthread_cond_wait(&cond, &mutex);
buf->len = buffers[head].len;
memcpy(buf->data, buffers[head].data, buf->len);
head = (head + 1) % NUM_CIRC_BUFS;
pthread_mutex_unlock(&mutex);
return buf;
}
static void image_buffer_free(struct buffer *buf)
{
if (buf) {
free(buf->data);
free(buf);
}
}
static void* handle_client(void *args)
{
char *data = NULL;
int i, fd;
struct client_info *client = args;
char header[MAX_BUF_SIZE];
struct buffer *buf = NULL;
if (++new_connection_log_count < 30) /* punt - FIXME */
log_printf("new connection from host '%s' on port '%d'\n",
inet_ntoa(client->sockaddr.sin_addr),
ntohs(client->sockaddr.sin_port));
/* We had delayed the circular buffer allocation until atleast one client is
* connected to the socket. let allocate the buffer now */
for (i = 0; i < NUM_CIRC_BUFS; i++) {
/* if the buffer is already allocated by some other client then share it */
if (!buffers[i].data) {
buffers[i].data = malloc(MAX_IMAGE_SIZE);
if (!buffers[i].data) {
log_printf("failed to allocate memory %s\n", strerror(errno));
goto failed;
}
}
}
fd = client->fd;
while(1) {
buf = client_queue_get();
if (!buf)
goto failed;
/* send JPEG boundary start header */
memset(header, '\0', MAX_BUF_SIZE);
snprintf(header, MAX_BUF_SIZE,
"\r\n--fooboundary\r\n"
"Content-type: image/jpeg\r\n\r\n");
if (send(fd, header, strlen(header), MSG_NOSIGNAL) < 0)
goto failed;
/* send image contents */
if (send(fd, buf->data, buf->len, MSG_NOSIGNAL) < 0)
goto failed;
/* we are done with the image buffer */
image_buffer_free(buf);
}
failed:
if (new_connection_log_count < 30) /* punt - FIXME */
log_printf("closing connection from host '%s' on port '%d'\n",
inet_ntoa(client->sockaddr.sin_addr),
ntohs(client->sockaddr.sin_port));
image_buffer_free(buf);
if (data)
free(data);
close(client->fd);
free(client);
pthread_detach(pthread_self());
return NULL;
}
static void add_new_connection (int server_fd)
{
int fd;
pthread_t tid;
socklen_t size;
struct sockaddr_in client;
struct client_info *c;
size = sizeof(client);
fd = accept(server_fd, (struct sockaddr*)&client, &size);
if (fd < 0) {
log_printf("failed to accept");
return;
}
c = calloc(1, sizeof(*c));
if (!c)
return;
memcpy(&c->sockaddr, &client, sizeof(struct sockaddr_in));
c->fd = fd;
pthread_create(&tid, NULL, handle_client, (void*)c);
return;
}
static int create_sock (int port)
{
struct sockaddr_in server;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
log_printf("socket create failed: %s\n", strerror(errno));
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
server.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr*) &server, sizeof(server)) < 0) {
log_printf("socket bind failed: %s\n", strerror(errno));
return 1;
}
if (listen(server_fd, 3) < 0) {
log_printf("socket listen failed: %s\n", strerror(errno));
return 1;
}
log_printf("MJPEG server is listening on port '%d'\n", port);
return 0;
}
static void* handler(void *unused)
{
int i;
fd_set active_fd_set, read_fd_set;
FD_ZERO(&active_fd_set);
FD_SET(server_fd, &active_fd_set);
while(1) {
/* block until input arrives on one or more active sockets */
read_fd_set = active_fd_set;
if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
log_printf("select failure: %s\n", strerror(errno) );
goto failed;
}
/* service all the socket with input pending */
for(i=0; i < FD_SETSIZE; ++i) {
if (FD_ISSET(i, &read_fd_set)) {
if (i == server_fd)
add_new_connection(i);
}
}
}
failed:
pthread_detach(pthread_self());
return NULL;
}
char* mjpeg_server_queue_get()
{
return buffers[tail].data;
}
void mjpeg_server_queue_put(char *data, int len)
{
pthread_mutex_lock(&mutex);
buffers[tail].len = len;
tail = (tail + 1) % NUM_CIRC_BUFS;
if (tail == head) /* queue is full */
head = head + 1;
if (head >= NUM_CIRC_BUFS) /* if head reach to end of queue then reset it */
head = 0;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}
int setup_mjpeg_tcp_server()
{
/* create a server socket */
if (create_sock(SERVER_PORT))
return 1;
/* spwan a thread to accept connections */
pthread_create(&handler_tid, NULL, handler, NULL);
return 0;
}