forked from xxxbsd/motion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
motion.h
561 lines (452 loc) · 17.9 KB
/
motion.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* motion.h
*
* Include file for motion.c
* Copyright 2000 by Jeroen Vreeken (pe1rxq@amsat.org)
* This software is distributed under the GNU public license version 2
* See also the file 'COPYING'.
*
*/
#ifndef _INCLUDE_MOTION_H
#define _INCLUDE_MOTION_H
/* Forward declarations, used in functional definitions of headers */
struct images;
struct image_data;
#include "config.h"
/* Includes */
#ifdef HAVE_MYSQL
#include <mysql.h>
#endif
#ifdef HAVE_SQLITE3
#include <sqlite3.h>
#endif
#ifdef HAVE_PGSQL
#include <libpq-fe.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <stdint.h>
#include <pthread.h>
#ifdef __FreeBSD__
//It is unknown whether it is safe to put this into the if/else below on Apple and BSD
#include <pthread_np.h>
#endif
#include "logger.h"
#include "conf.h"
#include "stream.h"
#include "webhttpd.h"
#include "track.h"
#include "netcam.h"
#include "netcam_rtsp.h"
#include "ffmpeg.h"
#ifdef HAVE_MMAL
#include "mmalcam.h"
#endif
#ifdef __APPLE__
#define MOTION_PTHREAD_SETNAME(name) pthread_setname_np(name)
#elif defined(BSD)
#define MOTION_PTHREAD_SETNAME(name) pthread_set_name_np(pthread_self(), name)
#elif HAVE_PTHREAD_SETNAME_NP
#define MOTION_PTHREAD_SETNAME(name) pthread_setname_np(pthread_self(), name)
#else
#define MOTION_PTHREAD_SETNAME(name)
#endif
/**
* ATTRIBUTE_UNUSED:
*
* Macro used to signal to GCC unused function parameters
*/
#ifdef __GNUC__
#ifdef HAVE_ANSIDECL_H
#include <ansidecl.h>
#endif
#ifndef ATTRIBUTE_UNUSED
#define ATTRIBUTE_UNUSED __attribute__((unused))
#endif
#else
#define ATTRIBUTE_UNUSED
#endif
/* strerror_r() XSI vs GNU */
#if (defined(BSD)) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
#define XSI_STRERROR_R
#endif
/*
* The macro below defines a version of sleep using nanosleep
* If a signal such as SIG_CHLD interrupts the sleep we just continue sleeping
*/
#define SLEEP(seconds, nanoseconds) { \
struct timespec tv; \
tv.tv_sec = (seconds); \
tv.tv_nsec = (nanoseconds); \
while (nanosleep(&tv, &tv) == -1); \
}
#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define VIDEO_PALETTE_GREY 1 /* Linear greyscale */
#define VIDEO_PALETTE_HI240 2 /* High 240 cube (BT848) */
#define VIDEO_PALETTE_RGB565 3 /* 565 16 bit RGB */
#define VIDEO_PALETTE_RGB24 4 /* 24bit RGB */
#define VIDEO_PALETTE_RGB32 5 /* 32bit RGB */
#define VIDEO_PALETTE_RGB555 6 /* 555 15bit RGB */
#define VIDEO_PALETTE_YUV422 7 /* YUV422 capture */
#define VIDEO_PALETTE_YUYV 8
#define VIDEO_PALETTE_UYVY 9 /* The great thing about standards is ... */
#define VIDEO_PALETTE_YUV420 10
#define VIDEO_PALETTE_YUV411 11 /* YUV411 capture */
#define VIDEO_PALETTE_RAW 12 /* RAW capture (BT848) */
#define VIDEO_PALETTE_YUV422P 13 /* YUV 4:2:2 Planar */
#define VIDEO_PALETTE_YUV411P 14 /* YUV 4:1:1 Planar */
#define VIDEO_PALETTE_YUV420P 15 /* YUV 4:2:0 Planar */
#define VIDEO_PALETTE_YUV410P 16 /* YUV 4:1:0 Planar */
#define VIDEO_PALETTE_PLANAR 13 /* start of planar entries */
#define VIDEO_PALETTE_COMPONENT 7 /* start of component entries */
#define DEF_PALETTE 17
/* Default picture settings */
#define DEF_WIDTH 352
#define DEF_HEIGHT 288
#define DEF_QUALITY 75
#define DEF_CHANGES 1500
#define DEF_MAXFRAMERATE 100
#define DEF_NOISELEVEL 32
/* Minimum time between two 'actions' (email, sms, external) */
#define DEF_EVENT_GAP 60 /* 1 minutes */
#define DEF_MAXMOVIETIME 3600 /* 60 minutes */
#define DEF_FFMPEG_BPS 400000
#define DEF_FFMPEG_VBR 0
#define DEF_FFMPEG_CODEC "mpeg4"
#define DEF_INPUT -1
#define DEF_VIDEO_DEVICE "/dev/video0"
#define THRESHOLD_TUNE_LENGTH 256
#define MISSING_FRAMES_TIMEOUT 30 /* When failing to get picture frame from camera
we reuse the previous frame until
MISSING_FRAMES_TIMEOUT seconds has passed
and then we show a grey image instead
*/
#define WATCHDOG_TMO 30 /* 30 sec max motion_loop interval */
#define WATCHDOG_KILL -60 /* -60 sec grace period before calling thread cancel */
#define WATCHDOG_OFF -127 /* Turn off watchdog, used when we wants to quit a thread */
#define CONNECTION_KO "Lost connection"
#define CONNECTION_OK "Connection OK"
#define DEF_MAXSTREAMS 10 /* Maximum number of stream clients per camera */
#define DEF_MAXWEBQUEUE 10 /* Maximum number of stream client in queue */
#define DEF_TIMESTAMP "%Y-%m-%d\\n%T"
#define DEF_EVENTSTAMP "%Y%m%d%H%M%S"
#define DEF_SNAPPATH "%v-%Y%m%d%H%M%S-snapshot"
#define DEF_IMAGEPATH "%v-%Y%m%d%H%M%S-%q"
#define DEF_MOVIEPATH "%v-%Y%m%d%H%M%S"
#define DEF_TIMEPATH "%Y%m%d-timelapse"
#define DEF_TIMELAPSE_MODE "daily"
/* Do not break this line into two or more. Must be ONE line */
#define DEF_SQL_QUERY_START "sql_query_start insert into security_events(camera, event_time_stamp) values('%t', '%Y-%m-%d %T')"
#define DEF_SQL_QUERY "sql_query insert into security_file(camera, filename, frame, file_type, time_stamp) values('%t', '%f', '%q', '%n', '%Y-%m-%d %T')"
/* OUTPUT Image types */
#define IMAGE_TYPE_JPEG 0
#define IMAGE_TYPE_PPM 1
#define IMAGE_TYPE_WEBP 2
/* Filetype defines */
#define FTYPE_IMAGE 1
#define FTYPE_IMAGE_SNAPSHOT 2
#define FTYPE_IMAGE_MOTION 4
#define FTYPE_MPEG 8
#define FTYPE_MPEG_MOTION 16
#define FTYPE_MPEG_TIMELAPSE 32
#define FTYPE_MPEG_ANY (FTYPE_MPEG | FTYPE_MPEG_MOTION | FTYPE_MPEG_TIMELAPSE)
#define FTYPE_IMAGE_ANY (FTYPE_IMAGE | FTYPE_IMAGE_SNAPSHOT | FTYPE_IMAGE_MOTION)
/* What types of images files do we want to have */
#define NEWIMG_OFF 0
#define NEWIMG_ON 1
#define NEWIMG_FIRST 2
#define NEWIMG_BEST 4
#define NEWIMG_CENTER 8
#define LOCATE_OFF 0
#define LOCATE_ON 1
#define LOCATE_PREVIEW 2
#define LOCATE_BOX 1
#define LOCATE_REDBOX 2
#define LOCATE_CROSS 4
#define LOCATE_REDCROSS 8
#define LOCATE_NORMAL 1
#define LOCATE_BOTH 2
#define UPDATE_REF_FRAME 1
#define RESET_REF_FRAME 2
/*
* Structure to hold images information
* The idea is that this should have all information about a picture e.g. diffs, timestamp etc.
* The exception is the label information, it uses a lot of memory
* When the image is stored all texts motion marks etc. is written to the image
* so we only have to send it out when/if we want.
*/
/* A image can have detected motion in it, but dosn't trigger an event, if we use minimum_motion_frames */
#define IMAGE_MOTION 1
#define IMAGE_TRIGGER 2
#define IMAGE_SAVE 4
#define IMAGE_SAVED 8
#define IMAGE_PRECAP 16
#define IMAGE_POSTCAP 32
enum CAMERA_TYPE {
CAMERA_TYPE_UNKNOWN,
CAMERA_TYPE_V4L2,
CAMERA_TYPE_BKTR,
CAMERA_TYPE_MMAL,
CAMERA_TYPE_RTSP,
CAMERA_TYPE_NETCAM
};
enum WEBUI_LEVEL{
WEBUI_LEVEL_ALWAYS = 0,
WEBUI_LEVEL_LIMITED = 1,
WEBUI_LEVEL_ADVANCED = 2,
WEBUI_LEVEL_RESTRICTED = 3,
WEBUI_LEVEL_NEVER = 99
};
struct image_data {
unsigned char *image_norm;
unsigned char *image_high;
int diffs;
struct timeval timestamp_tv;
int shot; /* Sub second timestamp count */
/*
* Movement center to img center distance
* Note: Dist is calculated distX*distX + distY*distY
*/
unsigned long cent_dist;
unsigned int flags; /* Se IMAGE_* defines */
struct coord location; /* coordinates for center and size of last motion detection*/
int total_labels;
#ifdef HAVE_FFMPEG
AVPacket packet_norm; /* The packet from the normal resolution rtsp thread */
AVPacket packet_high; /* The packet from the high resolution rtsp thread */
#else
int packet_norm; /* Dummy variables for the without ffmpeg option */
int packet_high; /* Dummy variables for the without ffmpeg option */
#endif // HAVE_FFMPEG
};
/*
* DIFFERENCES BETWEEN imgs.width, conf.width AND rotate_data.cap_width
* (and the corresponding height values, of course)
* ===========================================================================
* Location Purpose
*
* conf The values in conf reflect width and height set in the
* configuration file. These can be set via http remote control,
* but they are not used internally by Motion, so it won't break
* anything. These values are transferred to imgs in vid_start.
*
* imgs The values in imgs are the actual output dimensions. Normally
* the output dimensions are the same as the capture dimensions,
* but for 90 or 270 degrees rotation, they are not. E.g., if
* you capture at 320x240, and rotate 90 degrees, the output
* dimensions are 240x320.
* These values are set from the conf values in vid_start, or
* from the first JPEG image in netcam_start. For 90 or 270
* degrees rotation, they are swapped in rotate_init.
*
* rotate_data The values in rotate_data are named cap_width and cap_height,
* and contain the capture dimensions. The difference between
* capture and output dimensions is explained above.
* These values are set in rotate_init.
*/
/* date/time drawing, draw.c */
int draw_text(unsigned char *image, unsigned int startx, unsigned int starty, unsigned int width, const char *text, unsigned int factor);
int initialize_chars(void);
struct images {
struct image_data *image_ring; /* The base address of the image ring buffer */
int image_ring_size;
int image_ring_in; /* Index in image ring buffer we last added a image into */
int image_ring_out; /* Index in image ring buffer we want to process next time */
unsigned char *ref; /* The reference frame */
struct image_data img_motion; /* Picture buffer for motion images */
int *ref_dyn; /* Dynamic objects to be excluded from reference frame */
struct image_data image_virgin; /* Last picture frame with no text or locate overlay */
struct image_data preview_image; /* Picture buffer for best image when enables */
unsigned char *mask; /* Buffer for the mask file */
unsigned char *smartmask;
unsigned char *smartmask_final;
unsigned char *common_buffer;
unsigned char *mask_privacy; /* Buffer for the privacy mask values */
unsigned char *mask_privacy_uv; /* Buffer for the privacy U&V values */
unsigned char *mask_privacy_high; /* Buffer for the privacy mask values */
unsigned char *mask_privacy_high_uv; /* Buffer for the privacy U&V values */
int *smartmask_buffer;
int *labels;
int *labelsize;
int width;
int height;
int type;
int picture_type; /* Output picture type IMAGE_JPEG, IMAGE_PPM */
int size_norm; /* Number of bytes for normal size image */
int width_high;
int height_high;
int size_high; /* Number of bytes for high resolution image */
int motionsize;
int labelgroup_max;
int labels_above;
int labelsize_max;
int largest_label;
};
enum FLIP_TYPE {
FLIP_TYPE_NONE,
FLIP_TYPE_HORIZONTAL,
FLIP_TYPE_VERTICAL
};
/* Contains data for image rotation, see rotate.c. */
struct rotdata {
unsigned char *buffer_norm; /* Temporary buffer for 90 and 270 degrees rotation of normal resolution image. */
unsigned char *buffer_high; /* Temporary buffer for 90 and 270 degrees rotation of high resolution image. */
int degrees; /* Degrees to rotate; copied from conf.rotate_deg. */
enum FLIP_TYPE axis; /* Rotate image over the Horizontal or Vertical axis. */
int capture_width_norm; /* Capture width of normal resolution image */
int capture_height_norm; /* Capture height of normal resolution image */
int capture_width_high; /* Capture width of high resolution image */
int capture_height_high; /* Capture height of high resolution image */
};
/*
* These used to be global variables but now each thread will have its
* own context
*/
struct context {
FILE *extpipe;
int extpipe_open;
char conf_filename[PATH_MAX];
int from_conf_dir;
int threadnr;
unsigned int daemon;
char pid_file[PATH_MAX];
char log_file[PATH_MAX];
char log_type_str[6];
int log_level;
unsigned int log_type;
unsigned int dump_config_options;
struct config conf;
struct images imgs;
struct trackoptions track;
enum CAMERA_TYPE camera_type;
struct netcam_context *netcam;
#ifdef HAVE_MMAL
struct mmalcam_context *mmalcam;
#endif
rtsp_context *rtsp; /* this structure contains the context for normal RTSP connection */
rtsp_context *rtsp_high; /* this structure contains the context for high resolution RTSP connection */
struct image_data *current_image; /* Pointer to a structure where the image, diffs etc is stored */
unsigned int new_img;
int locate_motion_mode;
int locate_motion_style;
int process_thisframe;
struct rotdata rotate_data; /* rotation data is thread-specific */
int noise;
int threshold;
int diffs_last[THRESHOLD_TUNE_LENGTH];
int smartmask_speed;
/* Commands to the motion thread */
volatile unsigned int snapshot; /* Make a snapshot */
volatile unsigned int makemovie; /* End a movie */
volatile unsigned int finish; /* End the thread */
volatile unsigned int restart; /* Restart the thread when it ends */
/* Is the motion thread running */
volatile unsigned int running;
/* Is the web control thread running */
volatile unsigned int webcontrol_running;
volatile unsigned int webcontrol_finish; /* End the thread */
volatile int watchdog;
pthread_t thread_id;
int event_nr;
int prev_event;
unsigned long long database_event_id;
unsigned int lightswitch_framecounter;
char text_event_string[PATH_MAX]; /* The text for conv. spec. %C -
we assume PATH_MAX normally 4096 characters is fine */
int postcap; /* downcounter, frames left to to send post event */
int shots;
unsigned int detecting_motion;
struct tm *currenttime_tm;
struct tm *eventtime_tm;
time_t currenttime;
time_t lasttime;
time_t eventtime;
time_t connectionlosttime; /* timestamp from connection lost */
unsigned int lastrate;
unsigned int startup_frames;
unsigned int moved;
unsigned int pause;
int missing_frame_counter; /* counts failed attempts to fetch picture frame from camera */
unsigned int lost_connection;
int video_dev;
int pipe;
int mpipe;
struct stream stream;
int stream_count;
struct stream substream;
int substream_count;
#if defined(HAVE_MYSQL) || defined(HAVE_PGSQL) || defined(HAVE_SQLITE3)
int sql_mask;
#endif
#ifdef HAVE_SQLITE3
sqlite3 *database_sqlite3;
#endif
#ifdef HAVE_MYSQL
MYSQL *database;
#endif
#ifdef HAVE_PGSQL
PGconn *database_pg;
#endif
int movie_fps;
char newfilename[PATH_MAX];
char extpipefilename[PATH_MAX];
int movie_last_shot;
struct ffmpeg *ffmpeg_output;
struct ffmpeg *ffmpeg_output_debug;
struct ffmpeg *ffmpeg_timelapse;
char timelapsefilename[PATH_MAX];
char motionfilename[PATH_MAX];
int area_minx[9], area_miny[9], area_maxx[9], area_maxy[9];
int areadetect_eventnbr;
/* ToDo Determine why we need these...just put it all into prepare? */
unsigned long long int timenow, timebefore;
unsigned int rate_limit;
time_t lastframetime;
int minimum_frame_time_downcounter;
unsigned int get_image; /* Flag used to signal that we capture new image when we run the loop */
unsigned int text_size_factor;
long int required_frame_time, frame_delay;
long int rolling_average_limit;
long int *rolling_average_data;
unsigned long int rolling_average;
int olddiffs; //only need this in here for a printf later...do we need that printf?
int smartmask_ratio;
int smartmask_count;
int previous_diffs, previous_location_x, previous_location_y;
unsigned long int time_last_frame, time_current_frame;
unsigned int smartmask_lastrate;
unsigned int passflag; //only purpose is to flag first frame vs all others.....
int rolling_frame;
};
extern pthread_mutex_t global_lock;
extern volatile int threads_running;
extern FILE *ptr_logfile;
/* TLS keys below */
extern pthread_key_t tls_key_threadnr; /* key for thread number */
int http_bindsock(int, int, int);
void * mymalloc(size_t);
void * myrealloc(void *, size_t, const char *);
FILE * myfopen(const char *, const char *);
int myfclose(FILE *);
size_t mystrftime(const struct context *, char *, size_t, const char *, const struct timeval *, const char *, int);
int create_path(const char *);
void util_threadname_set(const char *abbr, int threadnbr, const char *threadname);
void util_threadname_get(char *threadname);
int util_check_passthrough(struct context *cnt);
#endif /* _INCLUDE_MOTION_H */