forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
motion.c
4148 lines (3487 loc) · 145 KB
/
motion.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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* motion.c
*
* Detect changes in a video stream.
* Copyright 2000 by Jeroen Vreeken ([email protected])
* This software is distributed under the GNU public license version 2
* See also the file 'COPYING'.
*
*/
#include "translate.h"
#include "motion.h"
#include "ffmpeg.h"
#include "video_common.h"
#include "video_v4l2.h"
#include "video_loopback.h"
#include "conf.h"
#include "alg.h"
#include "track.h"
#include "event.h"
#include "picture.h"
#include "rotate.h"
#include "webu.h"
#define IMAGE_BUFFER_FLUSH ((unsigned int)-1)
/**
* tls_key_threadnr
*
* TLS key for storing thread number in thread-local storage.
*/
pthread_key_t tls_key_threadnr;
/**
* global_lock
*
* Protects any global variables (like 'threads_running') during updates,
* to prevent problems with multiple threads updating at the same time.
*/
//pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t global_lock;
/**
* cnt_list
*
* List of context structures, one for each main Motion thread.
*/
struct context **cnt_list = NULL;
/**
* threads_running
*
* Keeps track of number of Motion threads currently running. Also used
* by 'main' to know when all threads have exited.
*/
volatile int threads_running = 0;
/* Set this when we want main to end or restart */
volatile unsigned int finish = 0;
/* Log file used instead of stderr and syslog */
FILE *ptr_logfile = NULL;
/**
* restart
*
* Differentiates between a quit and a restart. When all threads have
* finished running, 'main' checks if 'restart' is true and if so starts
* up again (instead of just quitting).
*/
unsigned int restart = 0;
/**
* image_ring_resize
*
* This routine is called from motion_loop to resize the image precapture ringbuffer
* NOTE: This function clears all images in the old ring buffer
* Parameters:
*
* cnt Pointer to the motion context structure
* new_size The new size of the ring buffer
*
* Returns: nothing
*/
static void image_ring_resize(struct context *cnt, int new_size)
{
/*
* Only resize if :
* Not in an event and
* decreasing at last position in new buffer
* increasing at last position in old buffer
* e.g. at end of smallest buffer
*/
if (cnt->event_nr != cnt->prev_event) {
int smallest;
if (new_size < cnt->imgs.image_ring_size) /* Decreasing */
smallest = new_size;
else /* Increasing */
smallest = cnt->imgs.image_ring_size;
if (cnt->imgs.image_ring_in == smallest - 1 || smallest == 0) {
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Resizing pre_capture buffer to %d items"), new_size);
/* Create memory for new ring buffer */
struct image_data *tmp;
tmp = mymalloc(new_size * sizeof(struct image_data));
/*
* Copy all information from old to new
* Smallest is 0 at initial init
*/
if (smallest > 0)
memcpy(tmp, cnt->imgs.image_ring, sizeof(struct image_data) * smallest);
/* In the new buffers, allocate image memory */
{
int i;
for(i = smallest; i < new_size; i++) {
tmp[i].image_norm = mymalloc(cnt->imgs.size_norm);
memset(tmp[i].image_norm, 0x80, cnt->imgs.size_norm); /* initialize to grey */
if (cnt->imgs.size_high > 0){
tmp[i].image_high = mymalloc(cnt->imgs.size_high);
memset(tmp[i].image_high, 0x80, cnt->imgs.size_high);
}
}
}
/* Free the old ring */
free(cnt->imgs.image_ring);
/* Point to the new ring */
cnt->imgs.image_ring = tmp;
cnt->current_image = NULL;
cnt->imgs.image_ring_size = new_size;
cnt->imgs.image_ring_in = 0;
cnt->imgs.image_ring_out = 0;
}
}
}
/**
* image_ring_destroy
*
* This routine is called when we want to free the ring
*
* Parameters:
*
* cnt Pointer to the motion context structure
*
* Returns: nothing
*/
static void image_ring_destroy(struct context *cnt)
{
int i;
/* Exit if don't have any ring */
if (cnt->imgs.image_ring == NULL)
return;
/* Free all image buffers */
for (i = 0; i < cnt->imgs.image_ring_size; i++){
free(cnt->imgs.image_ring[i].image_norm);
if (cnt->imgs.size_high >0 ) free(cnt->imgs.image_ring[i].image_high);
}
/* Free the ring */
free(cnt->imgs.image_ring);
cnt->imgs.image_ring = NULL;
cnt->current_image = NULL;
cnt->imgs.image_ring_size = 0;
}
/**
* image_save_as_preview
*
* This routine is called when we detect motion and want to save an image in the preview buffer
*
* Parameters:
*
* cnt Pointer to the motion context structure
* img Pointer to the image_data structure we want to set as preview image
*
* Returns: nothing
*/
static void image_save_as_preview(struct context *cnt, struct image_data *img)
{
void *image_norm, *image_high;
/* Save our pointers to our memory locations for images*/
image_norm = cnt->imgs.preview_image.image_norm;
image_high = cnt->imgs.preview_image.image_high;
/* Copy over the meta data from the img into preview */
memcpy(&cnt->imgs.preview_image, img, sizeof(struct image_data));
/* Restore the pointers to the memory locations for images*/
cnt->imgs.preview_image.image_norm = image_norm;
cnt->imgs.preview_image.image_high = image_high;
/* Copy the actual images for norm and high */
memcpy(cnt->imgs.preview_image.image_norm, img->image_norm, cnt->imgs.size_norm);
if (cnt->imgs.size_high > 0){
memcpy(cnt->imgs.preview_image.image_high, img->image_high, cnt->imgs.size_high);
}
/*
* If we set output_all to yes and during the event
* there is no image with motion, diffs is 0, we are not going to save the preview event
*/
if (cnt->imgs.preview_image.diffs == 0)
cnt->imgs.preview_image.diffs = 1;
/* draw locate box here when mode = LOCATE_PREVIEW */
if (cnt->locate_motion_mode == LOCATE_PREVIEW) {
if (cnt->locate_motion_style == LOCATE_BOX) {
alg_draw_location(&img->location, &cnt->imgs, cnt->imgs.width, cnt->imgs.preview_image.image_norm,
LOCATE_BOX, LOCATE_NORMAL, cnt->process_thisframe);
} else if (cnt->locate_motion_style == LOCATE_REDBOX) {
alg_draw_red_location(&img->location, &cnt->imgs, cnt->imgs.width, cnt->imgs.preview_image.image_norm,
LOCATE_REDBOX, LOCATE_NORMAL, cnt->process_thisframe);
} else if (cnt->locate_motion_style == LOCATE_CROSS) {
alg_draw_location(&img->location, &cnt->imgs, cnt->imgs.width, cnt->imgs.preview_image.image_norm,
LOCATE_CROSS, LOCATE_NORMAL, cnt->process_thisframe);
} else if (cnt->locate_motion_style == LOCATE_REDCROSS) {
alg_draw_red_location(&img->location, &cnt->imgs, cnt->imgs.width, cnt->imgs.preview_image.image_norm,
LOCATE_REDCROSS, LOCATE_NORMAL, cnt->process_thisframe);
}
}
}
/**
* context_init
*
* Initializes a context struct with the default values for all the
* variables.
*
* Parameters:
*
* cnt - the context struct to destroy
*
* Returns: nothing
*/
static void context_init(struct context *cnt)
{
/*
* We first clear the entire structure to zero, then fill in any
* values which have non-zero default values. Note that this
* assumes that a NULL address pointer has a value of binary 0
* (this is also assumed at other places within the code, i.e.
* there are instances of "if (ptr)"). Just for possible future
* changes to this assumption, any pointers which are intended
* to be initialised to NULL are listed within a comment.
*/
memset(cnt, 0, sizeof(struct context));
cnt->noise = 255;
cnt->lastrate = 25;
memcpy(&cnt->track, &track_template, sizeof(struct trackoptions));
cnt->pipe = -1;
cnt->mpipe = -1;
cnt->vdev = NULL; /*Init to NULL to check loading parms vs web updates*/
cnt->netcam = NULL;
cnt->rtsp = NULL;
cnt->rtsp_high = NULL;
}
/**
* context_destroy
*
* Destroys a context struct by freeing allocated memory, calling the
* appropriate cleanup functions and finally freeing the struct itself.
*
* Parameters:
*
* cnt - the context struct to destroy
*
* Returns: nothing
*/
static void context_destroy(struct context *cnt)
{
unsigned int j;
/* Free memory allocated for config parameters */
for (j = 0; config_params[j].param_name != NULL; j++) {
if (config_params[j].copy == copy_string ||
config_params[j].copy == copy_uri ||
config_params[j].copy == read_camera_dir) {
void **val;
val = (void *)((char *)cnt+(int)config_params[j].conf_value);
if (*val) {
free(*val);
*val = NULL;
}
}
}
free(cnt);
}
/**
* sig_handler
*
* Our SIGNAL-Handler. We need this to handle alarms and external signals.
*/
static void sig_handler(int signo)
{
int i;
/*The FALLTHROUGH is a special comment required by compiler. Do not edit it*/
switch(signo) {
case SIGALRM:
/*
* Somebody (maybe we ourself) wants us to make a snapshot
* This feature triggers snapshots on ALL threads that have
* snapshot_interval different from 0.
*/
if (cnt_list) {
i = -1;
while (cnt_list[++i]) {
if (cnt_list[i]->conf.snapshot_interval)
cnt_list[i]->snapshot = 1;
}
}
break;
case SIGUSR1:
/* Trigger the end of a event */
if (cnt_list) {
i = -1;
while (cnt_list[++i]){
cnt_list[i]->event_stop = TRUE;
}
}
break;
case SIGHUP:
restart = 1;
/*
* Fall through, as the value of 'restart' is the only difference
* between SIGHUP and the ones below.
*/
/*FALLTHROUGH*/
case SIGINT:
/*FALLTHROUGH*/
case SIGQUIT:
/*FALLTHROUGH*/
case SIGTERM:
/*
* Somebody wants us to quit! We should finish the actual
* movie and end up!
*/
if (cnt_list) {
i = -1;
while (cnt_list[++i]) {
cnt_list[i]->webcontrol_finish = TRUE;
cnt_list[i]->event_stop = TRUE;
cnt_list[i]->finish = 1;
/*
* Don't restart thread when it ends,
* all threads restarts if global restart is set
*/
cnt_list[i]->restart = 0;
}
}
/*
* Set flag we want to quit main check threads loop
* if restart is set (above) we start up again
*/
finish = 1;
break;
case SIGSEGV:
exit(0);
case SIGVTALRM:
printf("SIGVTALRM went off\n");
break;
}
}
/**
* sigchild_handler
*
* This function is a POSIX compliant replacement of the commonly used
* signal(SIGCHLD, SIG_IGN).
*/
static void sigchild_handler(int signo ATTRIBUTE_UNUSED)
{
#ifdef WNOHANG
while (waitpid(-1, NULL, WNOHANG) > 0) {};
#endif /* WNOHANG */
return;
}
/**
* setup_signals
* Attaches handlers to a number of signals that Motion need to catch.
*/
static void setup_signals(void){
/*
* Setup signals and do some initialization. 1 in the call to
* 'motion_startup' means that Motion will become a daemon if so has been
* requested, and argc and argc are necessary for reading the command
* line options.
*/
struct sigaction sig_handler_action;
struct sigaction sigchild_action;
#ifdef SA_NOCLDWAIT
sigchild_action.sa_flags = SA_NOCLDWAIT;
#else
sigchild_action.sa_flags = 0;
#endif
sigchild_action.sa_handler = sigchild_handler;
sigemptyset(&sigchild_action.sa_mask);
#ifdef SA_RESTART
sig_handler_action.sa_flags = SA_RESTART;
#else
sig_handler_action.sa_flags = 0;
#endif
sig_handler_action.sa_handler = sig_handler;
sigemptyset(&sig_handler_action.sa_mask);
/* Enable automatic zombie reaping */
sigaction(SIGCHLD, &sigchild_action, NULL);
sigaction(SIGPIPE, &sigchild_action, NULL);
sigaction(SIGALRM, &sig_handler_action, NULL);
sigaction(SIGHUP, &sig_handler_action, NULL);
sigaction(SIGINT, &sig_handler_action, NULL);
sigaction(SIGQUIT, &sig_handler_action, NULL);
sigaction(SIGTERM, &sig_handler_action, NULL);
sigaction(SIGUSR1, &sig_handler_action, NULL);
/* use SIGVTALRM as a way to break out of the ioctl, don't restart */
sig_handler_action.sa_flags = 0;
sigaction(SIGVTALRM, &sig_handler_action, NULL);
}
/**
* motion_remove_pid
* This function remove the process id file ( pid file ) before motion exit.
*/
static void motion_remove_pid(void)
{
if ((cnt_list[0]->daemon) && (cnt_list[0]->conf.pid_file) && (restart == 0)) {
if (!unlink(cnt_list[0]->conf.pid_file))
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Removed process id file (pid file)."));
else
MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Error removing pid file"));
}
if (ptr_logfile) {
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Closing logfile (%s)."),
cnt_list[0]->conf.log_file);
myfclose(ptr_logfile);
set_log_mode(LOGMODE_NONE);
ptr_logfile = NULL;
}
}
/**
* motion_detected
*
* Called from 'motion_loop' when motion is detected
* Can be called when no motion if emulate_motion is set!
*
* Parameters:
*
* cnt - current thread's context struct
* dev - video device file descriptor
* img - pointer to the captured image_data with detected motion
*/
static void motion_detected(struct context *cnt, int dev, struct image_data *img)
{
struct config *conf = &cnt->conf;
struct images *imgs = &cnt->imgs;
struct coord *location = &img->location;
/* Draw location */
if (cnt->locate_motion_mode == LOCATE_ON) {
if (cnt->locate_motion_style == LOCATE_BOX) {
alg_draw_location(location, imgs, imgs->width, img->image_norm, LOCATE_BOX,
LOCATE_BOTH, cnt->process_thisframe);
} else if (cnt->locate_motion_style == LOCATE_REDBOX) {
alg_draw_red_location(location, imgs, imgs->width, img->image_norm, LOCATE_REDBOX,
LOCATE_BOTH, cnt->process_thisframe);
} else if (cnt->locate_motion_style == LOCATE_CROSS) {
alg_draw_location(location, imgs, imgs->width, img->image_norm, LOCATE_CROSS,
LOCATE_BOTH, cnt->process_thisframe);
} else if (cnt->locate_motion_style == LOCATE_REDCROSS) {
alg_draw_red_location(location, imgs, imgs->width, img->image_norm, LOCATE_REDCROSS,
LOCATE_BOTH, cnt->process_thisframe);
}
}
/* Calculate how centric motion is if configured preview center*/
if (cnt->new_img & NEWIMG_CENTER) {
unsigned int distX = abs((imgs->width / 2) - location->x);
unsigned int distY = abs((imgs->height / 2) - location->y);
img->cent_dist = distX * distX + distY * distY;
}
/* Do things only if we have got minimum_motion_frames */
if (img->flags & IMAGE_TRIGGER) {
/* Take action if this is a new event and we have a trigger image */
if (cnt->event_nr != cnt->prev_event) {
/*
* Reset prev_event number to current event and save event time
* in both time_t and struct tm format.
*/
cnt->prev_event = cnt->event_nr;
cnt->eventtime = img->timestamp_tv.tv_sec;
localtime_r(&cnt->eventtime, cnt->eventtime_tm);
/*
* Since this is a new event we create the event_text_string used for
* the %C conversion specifier. We may already need it for
* on_motion_detected_commend so it must be done now.
*/
mystrftime(cnt, cnt->text_event_string, sizeof(cnt->text_event_string),
cnt->conf.text_event, &img->timestamp_tv, NULL, 0);
/* EVENT_FIRSTMOTION triggers on_event_start_command and event_ffmpeg_newfile */
event(cnt, EVENT_FIRSTMOTION, img, NULL, NULL,
&cnt->imgs.image_ring[cnt->imgs.image_ring_out].timestamp_tv);
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Motion detected - starting event %d"),
cnt->event_nr);
/* always save first motion frame as preview-shot, may be changed to an other one later */
if (cnt->new_img & (NEWIMG_FIRST | NEWIMG_BEST | NEWIMG_CENTER))
image_save_as_preview(cnt, img);
}
/* EVENT_MOTION triggers event_beep and on_motion_detected_command */
event(cnt, EVENT_MOTION, NULL, NULL, NULL, &img->timestamp_tv);
}
/* Limit framerate */
if (img->shot < conf->framerate) {
/*
* If config option stream_motion is enabled, send the latest motion detected image
* to the stream but only if it is not the first shot within a second. This is to
* avoid double frames since we already have sent a frame to the stream.
* We also disable this in setup_mode.
*/
if (conf->stream_motion && !conf->setup_mode && img->shot != 1)
event(cnt, EVENT_STREAM, img, NULL, NULL, &img->timestamp_tv);
/*
* Save motion jpeg, if configured
* Output the image_out (motion) picture.
*/
if (conf->picture_output_motion)
event(cnt, EVENT_IMAGEM_DETECTED, NULL, NULL, NULL, &img->timestamp_tv);
}
/* if track enabled and auto track on */
if (cnt->track.type && cnt->track.active)
cnt->moved = track_move(cnt, dev, location, imgs, 0);
}
/**
* process_image_ring
*
* Called from 'motion_loop' to save images / send images to movie
*
* Parameters:
*
* cnt - current thread's context struct
* max_images - Max number of images to process
* Set to IMAGE_BUFFER_FLUSH to send/save all images in buffer
*/
static void process_image_ring(struct context *cnt, unsigned int max_images)
{
/*
* We are going to send an event, in the events there is still
* some code that use cnt->current_image
* so set it temporary to our image
*/
struct image_data *saved_current_image = cnt->current_image;
/* If image is flaged to be saved and not saved yet, process it */
do {
/* Check if we should save/send this image, breakout if not */
assert(cnt->imgs.image_ring_out < cnt->imgs.image_ring_size);
if ((cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & (IMAGE_SAVE | IMAGE_SAVED)) != IMAGE_SAVE)
break;
/* Set inte global context that we are working with this image */
cnt->current_image = &cnt->imgs.image_ring[cnt->imgs.image_ring_out];
if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].shot < cnt->conf.framerate) {
if (cnt->log_level >= DBG) {
char tmp[32];
const char *t;
if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_TRIGGER)
t = "Trigger";
else if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_MOTION)
t = "Motion";
else if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_PRECAP)
t = "Precap";
else if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_POSTCAP)
t = "Postcap";
else
t = "Other";
mystrftime(cnt, tmp, sizeof(tmp), "%H%M%S-%q",
&cnt->imgs.image_ring[cnt->imgs.image_ring_out].timestamp_tv, NULL, 0);
draw_text(cnt->imgs.image_ring[cnt->imgs.image_ring_out].image_norm,
cnt->imgs.width, cnt->imgs.height, 10, 20, tmp, cnt->text_scale);
draw_text(cnt->imgs.image_ring[cnt->imgs.image_ring_out].image_norm,
cnt->imgs.width, cnt->imgs.height, 10, 30, t, cnt->text_scale);
}
/* Output the picture to jpegs and ffmpeg */
event(cnt, EVENT_IMAGE_DETECTED,
&cnt->imgs.image_ring[cnt->imgs.image_ring_out], NULL, NULL,
&cnt->imgs.image_ring[cnt->imgs.image_ring_out].timestamp_tv);
/*
* Check if we must add any "filler" frames into movie to keep up fps
* Only if we are recording videos ( ffmpeg or extenal pipe )
* While the overall elapsed time might be correct, if there are
* many duplicated frames, say 10 fps, 5 duplicated, the video will
* look like it is frozen every second for half a second.
*/
if (!cnt->conf.movie_duplicate_frames) {
/* don't duplicate frames */
} else if ((cnt->imgs.image_ring[cnt->imgs.image_ring_out].shot == 0) &&
(cnt->ffmpeg_output || (cnt->conf.movie_extpipe_use && cnt->extpipe))) {
/*
* movie_last_shoot is -1 when file is created,
* we don't know how many frames there is in first sec
*/
if (cnt->movie_last_shot >= 0) {
if (cnt_list[0]->log_level >= DBG) {
int frames = cnt->movie_fps - (cnt->movie_last_shot + 1);
if (frames > 0) {
char tmp[25];
MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO
,_("Added %d fillerframes into movie"), frames);
sprintf(tmp, "Fillerframes %d", frames);
draw_text(cnt->imgs.image_ring[cnt->imgs.image_ring_out].image_norm,
cnt->imgs.width, cnt->imgs.height, 10, 40, tmp, cnt->text_scale);
}
}
/* Check how many frames it was last sec */
while ((cnt->movie_last_shot + 1) < cnt->movie_fps) {
/* Add a filler frame into encoder */
event(cnt, EVENT_FFMPEG_PUT,
&cnt->imgs.image_ring[cnt->imgs.image_ring_out], NULL, NULL,
&cnt->imgs.image_ring[cnt->imgs.image_ring_out].timestamp_tv);
cnt->movie_last_shot++;
}
}
cnt->movie_last_shot = 0;
} else if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].shot != (cnt->movie_last_shot + 1)) {
/* We are out of sync! Propably we got motion - no motion - motion */
cnt->movie_last_shot = -1;
}
/*
* Save last shot added to movie
* only when we not are within first sec
*/
if (cnt->movie_last_shot >= 0)
cnt->movie_last_shot = cnt->imgs.image_ring[cnt->imgs.image_ring_out].shot;
}
/* Mark the image as saved */
cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags |= IMAGE_SAVED;
/* Store it as a preview image, only if it has motion */
if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_MOTION) {
/* Check for most significant preview-shot when picture_output=best */
if (cnt->new_img & NEWIMG_BEST) {
if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].diffs > cnt->imgs.preview_image.diffs) {
image_save_as_preview(cnt, &cnt->imgs.image_ring[cnt->imgs.image_ring_out]);
}
}
/* Check for most significant preview-shot when picture_output=center */
if (cnt->new_img & NEWIMG_CENTER) {
if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].cent_dist < cnt->imgs.preview_image.cent_dist) {
image_save_as_preview(cnt, &cnt->imgs.image_ring[cnt->imgs.image_ring_out]);
}
}
}
/* Increment to image after last sended */
if (++cnt->imgs.image_ring_out >= cnt->imgs.image_ring_size)
cnt->imgs.image_ring_out = 0;
if (max_images != IMAGE_BUFFER_FLUSH) {
max_images--;
/* breakout if we have done max_images */
if (max_images == 0)
break;
}
/* loop until out and in is same e.g. buffer empty */
} while (cnt->imgs.image_ring_out != cnt->imgs.image_ring_in);
/* restore global context values */
cnt->current_image = saved_current_image;
}
static int init_camera_type(struct context *cnt){
cnt->camera_type = CAMERA_TYPE_UNKNOWN;
#ifdef HAVE_MMAL
if (cnt->conf.mmalcam_name) {
cnt->camera_type = CAMERA_TYPE_MMAL;
return 0;
}
#endif // HAVE_MMAL
if (cnt->conf.netcam_url) {
if ((strncmp(cnt->conf.netcam_url,"mjpeg",5) == 0) ||
(strncmp(cnt->conf.netcam_url,"v4l2" ,4) == 0) ||
(strncmp(cnt->conf.netcam_url,"file" ,4) == 0) ||
(strncmp(cnt->conf.netcam_url,"rtmp" ,4) == 0) ||
(strncmp(cnt->conf.netcam_url,"rtsp" ,4) == 0)) {
cnt->camera_type = CAMERA_TYPE_RTSP;
} else {
cnt->camera_type = CAMERA_TYPE_NETCAM;
}
return 0;
}
#ifdef HAVE_BKTR
if (strncmp(cnt->conf.video_device,"/dev/bktr",9) == 0) {
cnt->camera_type = CAMERA_TYPE_BKTR;
return 0;
}
#endif // HAVE_BKTR
#ifdef HAVE_V4L2
if (cnt->conf.video_device) {
cnt->camera_type = CAMERA_TYPE_V4L2;
return 0;
}
#endif // HAVE_V4L2
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO
, _("Unable to determine camera type (MMAL, Netcam, V4L2, BKTR)"));
return -1;
}
static void init_mask_privacy(struct context *cnt){
int indxrow, indxcol;
int start_cr, offset_cb, start_cb;
int y_index, uv_index;
int indx_img, indx_max; /* Counter and max for norm/high */
int indx_width, indx_height;
unsigned char *img_temp, *img_temp_uv;
FILE *picture;
/* Load the privacy file if any */
cnt->imgs.mask_privacy = NULL;
cnt->imgs.mask_privacy_uv = NULL;
cnt->imgs.mask_privacy_high = NULL;
cnt->imgs.mask_privacy_high_uv = NULL;
if (cnt->conf.mask_privacy) {
if ((picture = myfopen(cnt->conf.mask_privacy, "r"))) {
MOTION_LOG(INF, TYPE_ALL, NO_ERRNO, _("Opening privacy mask file"));
/*
* NOTE: The mask is expected to have the output dimensions. I.e., the mask
* applies to the already rotated image, not the capture image. Thus, use
* width and height from imgs.
*/
cnt->imgs.mask_privacy = get_pgm(picture, cnt->imgs.width, cnt->imgs.height);
/* We only need the "or" mask for the U & V chrominance area. */
cnt->imgs.mask_privacy_uv = mymalloc((cnt->imgs.height * cnt->imgs.width) / 2);
if (cnt->imgs.size_high > 0){
MOTION_LOG(INF, TYPE_ALL, NO_ERRNO
,_("Opening high resolution privacy mask file"));
rewind(picture);
cnt->imgs.mask_privacy_high = get_pgm(picture, cnt->imgs.width_high, cnt->imgs.height_high);
cnt->imgs.mask_privacy_high_uv = mymalloc((cnt->imgs.height_high * cnt->imgs.width_high) / 2);
}
myfclose(picture);
} else {
MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO
,_("Error opening mask file %s"), cnt->conf.mask_privacy);
/* Try to write an empty mask file to make it easier for the user to edit it */
put_fixed_mask(cnt, cnt->conf.mask_privacy);
}
if (!cnt->imgs.mask_privacy) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO
,_("Failed to read mask privacy image. Mask privacy feature disabled."));
} else {
MOTION_LOG(INF, TYPE_ALL, NO_ERRNO
,_("Mask privacy file \"%s\" loaded."), cnt->conf.mask_privacy);
indx_img = 1;
indx_max = 1;
if (cnt->imgs.size_high > 0) indx_max = 2;
while (indx_img <= indx_max){
if (indx_img == 1){
start_cr = (cnt->imgs.height * cnt->imgs.width);
offset_cb = ((cnt->imgs.height * cnt->imgs.width)/4);
start_cb = start_cr + offset_cb;
indx_width = cnt->imgs.width;
indx_height = cnt->imgs.height;
img_temp = cnt->imgs.mask_privacy;
img_temp_uv = cnt->imgs.mask_privacy_uv;
} else {
start_cr = (cnt->imgs.height_high * cnt->imgs.width_high);
offset_cb = ((cnt->imgs.height_high * cnt->imgs.width_high)/4);
start_cb = start_cr + offset_cb;
indx_width = cnt->imgs.width_high;
indx_height = cnt->imgs.height_high;
img_temp = cnt->imgs.mask_privacy_high;
img_temp_uv = cnt->imgs.mask_privacy_high_uv;
}
for (indxrow = 0; indxrow < indx_height; indxrow++) {
for (indxcol = 0; indxcol < indx_width; indxcol++) {
y_index = indxcol + (indxrow * indx_width);
if (img_temp[y_index] == 0xff) {
if ((indxcol % 2 == 0) && (indxrow % 2 == 0) ){
uv_index = (indxcol/2) + ((indxrow * indx_width)/4);
img_temp[start_cr + uv_index] = 0xff;
img_temp[start_cb + uv_index] = 0xff;
img_temp_uv[uv_index] = 0x00;
img_temp_uv[offset_cb + uv_index] = 0x00;
}
} else {
img_temp[y_index] = 0x00;
if ((indxcol % 2 == 0) && (indxrow % 2 == 0) ){
uv_index = (indxcol/2) + ((indxrow * indx_width)/4);
img_temp[start_cr + uv_index] = 0x00;
img_temp[start_cb + uv_index] = 0x00;
img_temp_uv[uv_index] = 0x80;
img_temp_uv[offset_cb + uv_index] = 0x80;
}
}
}
}
indx_img++;
}
}
}
}
static void init_text_scale(struct context *cnt){
/* Consider that web interface may change conf values at any moment.
* The below can put two sections in the image so make sure that after
* scaling does not occupy more than 1/4 of image (10 pixels * 2 lines)
*/
cnt->text_scale = cnt->conf.text_scale;
if (cnt->text_scale <= 0) cnt->text_scale = 1;
if ((cnt->text_scale * 10 * 2) > (cnt->imgs.width / 4)) {
cnt->text_scale = (cnt->imgs.width / (4 * 10 * 2));
if (cnt->text_scale <= 0) cnt->text_scale = 1;
MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO
,_("Invalid text scale. Adjusted to %d"), cnt->text_scale);
}
if ((cnt->text_scale * 10 * 2) > (cnt->imgs.height / 4)) {
cnt->text_scale = (cnt->imgs.height / (4 * 10 * 2));
if (cnt->text_scale <= 0) cnt->text_scale = 1;
MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO
,_("Invalid text scale. Adjusted to %d"), cnt->text_scale);
}
/* If we had to modify the scale, change conf so we don't get another message */
cnt->conf.text_scale = cnt->text_scale;
}
static void mot_stream_init(struct context *cnt){
/* The image buffers are allocated in event_stream_put if needed*/
pthread_mutex_init(&cnt->mutex_stream, NULL);
cnt->imgs.substream_image = NULL;
cnt->stream_norm.jpeg_size = 0;
cnt->stream_norm.jpeg_data = NULL;
cnt->stream_norm.cnct_count = 0;
cnt->stream_sub.jpeg_size = 0;
cnt->stream_sub.jpeg_data = NULL;
cnt->stream_sub.cnct_count = 0;
cnt->stream_motion.jpeg_size = 0;
cnt->stream_motion.jpeg_data = NULL;
cnt->stream_motion.cnct_count = 0;
cnt->stream_source.jpeg_size = 0;
cnt->stream_source.jpeg_data = NULL;
cnt->stream_source.cnct_count = 0;
}
static void mot_stream_deinit(struct context *cnt){
/* Need to check whether buffers were allocated since init
* function defers the allocations to event_stream_put
*/
pthread_mutex_destroy(&cnt->mutex_stream);
if (cnt->imgs.substream_image != NULL){
free(cnt->imgs.substream_image);
cnt->imgs.substream_image = NULL;
}
if (cnt->stream_norm.jpeg_data != NULL){
free(cnt->stream_norm.jpeg_data);
cnt->stream_norm.jpeg_data = NULL;
}
if (cnt->stream_sub.jpeg_data != NULL){
free(cnt->stream_sub.jpeg_data);
cnt->stream_sub.jpeg_data = NULL;
}
if (cnt->stream_motion.jpeg_data != NULL){
free(cnt->stream_motion.jpeg_data);
cnt->stream_motion.jpeg_data = NULL;
}
if (cnt->stream_source.jpeg_data != NULL){
free(cnt->stream_source.jpeg_data);
cnt->stream_source.jpeg_data = NULL;
}
}
/* TODO: dbse functions are to be moved to separate module in future change*/
static void dbse_global_deinit(void){
MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO, _("Closing MYSQL"));
#ifdef HAVE_MYSQL
mysql_library_end();
#endif /* HAVE_MYSQL */
}
static void dbse_global_init(void){
MOTION_LOG(DBG, TYPE_DB, NO_ERRNO,_("Initializing database"));
/* Initialize all the database items */
#ifdef HAVE_MYSQL
if (mysql_library_init(0, NULL, NULL)) {
fprintf(stderr, "could not initialize MySQL library\n");
exit(1);
}
#endif /* HAVE_MYSQL */
#ifdef HAVE_SQLITE3
int indx;
/* database_sqlite3 == NULL if not changed causes each thread to create their own
* sqlite3 connection this will only happens when using a non-threaded sqlite version */
cnt_list[0]->database_sqlite3=NULL;
if (cnt_list[0]->conf.database_type && ((!strcmp(cnt_list[0]->conf.database_type, "sqlite3")) && cnt_list[0]->conf.database_dbname)) {
MOTION_LOG(NTC, TYPE_DB, NO_ERRNO
,_("SQLite3 Database filename %s")
,cnt_list[0]->conf.database_dbname);
int thread_safe = sqlite3_threadsafe();
if (thread_safe > 0) {
MOTION_LOG(NTC, TYPE_DB, NO_ERRNO, _("SQLite3 is threadsafe"));
MOTION_LOG(NTC, TYPE_DB, NO_ERRNO, _("SQLite3 serialized %s")
,(sqlite3_config(SQLITE_CONFIG_SERIALIZED)?_("FAILED"):_("SUCCESS")));