-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaphttp1.c
103 lines (93 loc) · 2.67 KB
/
caphttp1.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
#include <glib.h>
#define _GNU_SOURCEinclude <gio/gio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include <time.h>
#include <sys/time.h>
#include <capture.h>
#include <axsdk/axhttp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#define file "/tmp/jpg.jpg"
char frames[2];
char resolution[7];
char* camera;
char* seq(char* s1, char* s2) {
char *combine = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(combine, s1);
strcat(combine, s2);
return combine;
}
int main(void) {
int process;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int newsockfd;
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
unsigned char buffer[256];
//char* array = "resolution=640x480&fps=25";
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5005);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
listen(sockfd, 5);
clilen = sizeof(cli_addr);
for (;;) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
bzero(buffer, 256);
syslog(LOG_INFO, "Connection established");
process = fork();
if (process < 0) {
syslog(LOG_INFO, "Error on fork");
exit(1);
}
if (process == 0) {
close(sockfd);
read(newsockfd, &resolution[0], 7); //read data from client
read(newsockfd, &frames[0], 2); //(resolution, fps)
char* res = &resolution[0];
char* fps = &frames[0];
char* con1 = seq("resolution=", res);
char* con2 = seq(con1, "&fps=");
camera = seq(con2, fps);
syslog(LOG_INFO, "Resolution received");
syslog(LOG_INFO, "FPS received");
syslog(LOG_INFO, res);
syslog(LOG_INFO, fps);
media_stream *stream;
stream = capture_open_stream(IMAGE_JPEG, camera);
syslog(LOG_INFO, "stream Open");
while (1) {
media_frame *frame;
frame = capture_get_frame(stream);
syslog(LOG_INFO, "capture frame");
void *data;
data = capture_frame_data(frame);
syslog(LOG_INFO, "capture data");
size_t frame_size = capture_frame_size(frame);
syslog(LOG_INFO, "capture frame_size %d", frame_size);
int size = htonl((int) frame_size);
syslog(LOG_INFO, "%d", frame_size);
//unsigned char image[frame_size];
syslog(LOG_INFO, "trying to send frame size");
write(newsockfd, &size, sizeof(int)); //send size to client
syslog(LOG_INFO, "sent frame size");
write(newsockfd, data, frame_size);
syslog(LOG_INFO, "received frame size");
capture_frame_free(frame);
}
capture_close_stream(stream);
exit(0);
} else {
close(newsockfd);
}
}
close(sockfd);
return (0);
}