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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* server.cc
*
* Mon Nov 8 11:35:01 CET 2004
* Copyright 2004 Bent Bisballe
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of MIaV.
*
* MIaV is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MIaV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIaV; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
/*
* $Id$
*/
/*
* $Log$
* Revision 1.24 2005/06/19 11:44:14 deva
* Cleaned up a log of logging.
* Fixed server queue (shouldn't happen).
* Added user and group lookup.
*
* Revision 1.23 2005/06/14 18:58:35 deva
* *** empty log message ***
*
* Revision 1.22 2005/06/14 12:29:40 deva
* Incorporated the use of the Info object everywhere... also using the log functionality.
*
* Revision 1.21 2005/05/26 21:32:39 deva
* *** empty log message ***
*
* Revision 1.20 2005/05/26 12:48:36 deva
* *** empty log message ***
*
* Revision 1.19 2005/05/22 16:34:44 deva
* Fix: Connection is now taken down when taking a sanpshot without recording.
*
* Revision 1.18 2005/05/22 15:49:22 deva
* Added multithreaded encoding support.
*
* Revision 1.17 2005/05/17 19:16:26 deva
* Made new mpeg writer work, with proper file permissions.
*
* Revision 1.16 2005/05/17 15:12:51 deva
* Fixed file rights (All read on files and directories, and all execute on directories).
*
* Revision 1.15 2005/05/17 14:30:56 deva
* Added code, preparing threaded encoding.
*
* Revision 1.14 2005/05/16 16:00:57 deva
* Lots of stuff!
*
* Revision 1.13 2005/05/09 16:40:20 deva
* Added optimize yuv conversion code
*
* Revision 1.12 2005/05/07 10:56:18 deva
* Changed print outs
*
* Revision 1.11 2005/05/03 08:31:59 deva
* Removed the error object, and replaced it with a more generic info object.
*
* Revision 1.10 2005/05/02 19:47:43 deva
* Fixed overlapping cpr numbers on the server (now it saves one cpr pr.
* connection, and ignores any changes sent)
*
* Revision 1.9 2005/05/02 18:46:15 deva
* Files are now saved in a custom folder (defined in miav.conf)
*
* Revision 1.8 2005/05/01 09:56:26 deva
* Added Id and Log tags to all files
*/
#include "server.h"
#include "miav.h"
#include <stdio.h>
#include <stdlib.h>
// For mkdir
#include <sys/stat.h>
#include <sys/types.h>
// For unlink
#include <unistd.h>
// For errno
#include <errno.h>
// For inet_ntoa
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "miav_config.h"
#include "mov_encoder_thread.h"
#include "img_encoder.h"
#include "server_status.h"
#include "dv.h"
void saveFrameAsImage(char* cpr, Frame *f)
{
ImgEncoder imgenc;
struct tm *ltime;
time_t t = time(NULL);
FILE *fp;
int cnt = 0;
char fname[256];
char birthmonth[3];
char date[9];
string *root = config->readString("server_root");
// Test for server root writeability
sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand());
fp = fopen(fname, "w");
if(!fp) {
int r = rand();
fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str());
fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
sprintf(fname, "/tmp/miav-%d", r);
imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
return;
}
fclose(fp);
unlink(fname);
// Check for cpr length correctness
if(strlen(cpr) != 11) {
int r = rand();
fprintf(stderr, "Illigal CPR, it must have length 11, it had length %d\n", strlen(cpr));
fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
sprintf(fname, "/tmp/miav-%d", r);
imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
return;
}
// Copy the bytes representing the birth month from the cpr
// [dd][mm][yy]-[nn][nn]
strncpy(birthmonth, &cpr[2], 2);
birthmonth[2] = 0;
// Create folder named birthmonth in server root
sprintf(fname, "%s/%s", root->c_str(), birthmonth);
if(!mkdir(fname, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) == -1 && errno != EEXIST) {
int r = rand();
fprintf(stderr, "Not possible to create subfolder %s\n", fname);
fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
sprintf(fname, "/tmp/miav-%d", r);
imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
return;
}
// Create folder named cpr in serverroot/birthmonth
sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr);
if(!mkdir(fname, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) == -1 && errno != EEXIST) {
int r = rand();
fprintf(stderr, "Not possible to create subfolder %s\n", fname);
fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
sprintf(fname, "/tmp/miav-%d", r);
imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
return;
}
// Create date (today) in [yyyy][mm][dd]
ltime = localtime(&t);
sprintf(date, "%.4d%.2d%.2d",
ltime->tm_year + 1900,
ltime->tm_mon,
ltime->tm_mday);
// Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg
sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);
// test filename-[cnt] for existamce cnt++ until not existing.
fp = fopen(fname, "r");
while(fp) {
fclose(fp);
cnt++;
sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);
fp = fopen(fname, "r");
}
sprintf(fname, "%s/%s/%s/%s-%s-%.3d", root->c_str(), birthmonth, cpr, cpr, date, cnt);
fprintf(stderr, "Success - using filename: [%s.jpg]\n", fname); fflush(stderr);
imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
}
void newConnection(Socket *socket, Info *info)
{
char cpr[256];
bool hasCpr = false;
ServerStatus status(info);
n_savestate savestate = LATER;
n_header h;
Frame *frame;
Frame *freeze_frame = NULL;
MovEncoderThread *enc = NULL;
frame = new Frame(NULL, DVPACKAGE_SIZE);
info->info("New connection (%s)", inet_ntoa(socket->socketaddr.sin_addr));
Network network = Network(socket, info);
while(int ret = network.recvPackage(&h, frame->data, frame->size)) {
status.checkPoint();
if(ret == -1) {
info->error("A network error ocurred, terminating session");
break;
}
if(!hasCpr) {
sprintf(cpr, h.header.h_data.cpr);
hasCpr = true;
}
// printf("Read: %d bytes ", ret);
// printf("typ: %d ", h.header_type);
// fprintf(stdout, "cpr: %s ", cpr);
// fprintf(stdout, "frz: %d ", h.header.h_data.freeze);
// fprintf(stdout, "sht: %d ", h.header.h_data.snapshot);
// fprintf(stdout, "save: %d ", h.header.h_data.savestate);
// fflush(stdout);
if(h.header.h_data.snapshot) {
if(freeze_frame) {
saveFrameAsImage(cpr, freeze_frame);
delete freeze_frame;
freeze_frame = NULL;
} else {
saveFrameAsImage(cpr, frame);
}
}
if(h.header.h_data.savestate) {
savestate = h.header.h_data.savestate;
}
if(h.header.h_data.freeze) {
if(freeze_frame) delete freeze_frame;
// copy the frame into another temporary one.
freeze_frame = new Frame(frame->data, frame->size);
}
// This one must be last!
if(h.header.h_data.record) {
// if(!enc) enc = newMovEncoder(cpr);
if(!enc) enc = new MovEncoderThread(cpr, info);
enc->encode(frame);
}
frame = new Frame(NULL, DVPACKAGE_SIZE);
}
// TODO: Use save state
info->info("Closing connection...");
if(enc) delete enc;
info->info("Connection closed");
}
|