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
|
#include "server.h"
#include "miav.h"
#include <stdio.h>
#include <stdlib.h>
#include "mov_encoder.h"
#include "img_encoder.h"
#include "dv.h"
void saveFrameAsImage(char* cpr, Frame *f)
{
char fname[256];
ImgEncoder imgenc;
sprintf(fname, "image-%s-%d.jpeg", cpr, rand());
imgenc.encode(f, fname, 100);
}
MovEncoder *newMovEncoder(char* cpr)
{
MovEncoder *enc;
struct tm *ltime;
char fname[256];
time_t t = time(NULL);
ltime = localtime(&t);
sprintf(fname, "%.2d%.2d%.2d%.2d%.2d%.2d-%s.mpg",
ltime->tm_year + 1900,
ltime->tm_mon,
ltime->tm_mday,
ltime->tm_hour,
ltime->tm_min,
ltime->tm_sec, cpr);
enc = new MovEncoder(fname);
return enc;
}
void newConnection(Socket *s)
{
n_savestate savestate = LATER;
n_header h;
Frame *f;
Frame *freeze_frame = NULL;
MovEncoder *enc = NULL;
f = new Frame(NULL, 0);
printf("New connection[pid: %d]...\n", getpid());
Network n = Network(s);
while(int ret = n.recvPackage(&h, f->data, DVPACKAGE_SIZE)) {
if(ret == -1) {
fprintf(stderr, "An error occurred...!\n");
break;
}
printf("Read: %d bytes\t", ret);
printf("\ttyp: %d\t", h.header_type);
printf("\tcpr: %s\t", h.header.h_data.cpr);
printf("\tfrz: %d\t", h.header.h_data.freeze);
printf("\tsht: %d\n", h.header.h_data.snapshot);
printf("\tsave: %d\n", h.header.h_data.savestate);
if(h.header.h_data.snapshot) {
if(freeze_frame) {
saveFrameAsImage(h.header.h_data.cpr, freeze_frame);
delete freeze_frame;
freeze_frame = NULL;
} else {
saveFrameAsImage(h.header.h_data.cpr, f);
}
}
if(h.header.h_data.record) {
if(!enc) enc = newMovEncoder(h.header.h_data.cpr);
enc->encode(f);
}
if(h.header.h_data.savestate) {
savestate = h.header.h_data.savestate;
}
if(h.header.h_data.freeze) {
if(freeze_frame) delete freeze_frame;
freeze_frame = f;
} else {
delete f;
}
f = new Frame(NULL, 0);
}
delete f;
if(enc) delete enc;
printf("Connection end[pid: %d]...\n", getpid());
}
|