diff --git a/doc/CODE_STANDARD b/doc/CODE_STANDARD index 9f52d191..0699b96a 100644 --- a/doc/CODE_STANDARD +++ b/doc/CODE_STANDARD @@ -120,9 +120,7 @@ GOOD EXAMPLE if ((picture=fopen(cnt->conf.mask_file, "r"))) { cnt->imgs.mask=get_pgm(cnt, picture, cnt->imgs.width, cnt->imgs.height); - fclose(picture); } else { - put_fixed_mask(cnt, cnt->conf.mask_file); printf("Hello world\n"); } @@ -172,6 +170,8 @@ switch (expr) { statement; } +RULE 5A +if, while and for statements shall always use braces. -------------------- diff --git a/src/alg.c b/src/alg.c index 9b03fe8b..602ae19c 100644 --- a/src/alg.c +++ b/src/alg.c @@ -78,16 +78,16 @@ void alg_locate_center_size(struct images *imgs, int width, int height, struct c for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { if (*(labels++) & 32768) { - if (x > cent->x) + if (x > cent->x) { xdist += x - cent->x; - else if (x < cent->x) + } else if (x < cent->x) { xdist += cent->x - x; - - if (y > cent->y) + } + if (y > cent->y) { ydist += y - cent->y; - else if (y < cent->y) + } else if (y < cent->y) { ydist += cent->y - y; - + } centc++; } } @@ -97,16 +97,16 @@ void alg_locate_center_size(struct images *imgs, int width, int height, struct c for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { if (*(out++)) { - if (x > cent->x) + if (x > cent->x) { xdist += x - cent->x; - else if (x < cent->x) + } else if (x < cent->x) { xdist += cent->x - x; - - if (y > cent->y) + } + if (y > cent->y) { ydist += y - cent->y; - else if (y < cent->y) + } else if (y < cent->y) { ydist += cent->y - y; - + } centc++; } } @@ -126,25 +126,28 @@ void alg_locate_center_size(struct images *imgs, int width, int height, struct c cent->maxy = cent->y + ydist / centc * 2; } - if (cent->maxx > width - 1) + if (cent->maxx > width - 1) { cent->maxx = width - 1; - else if (cent->maxx < 0) + } else if (cent->maxx < 0) { cent->maxx = 0; - - if (cent->maxy > height - 1) + } + if (cent->maxy > height - 1) { cent->maxy = height - 1; - else if (cent->maxy < 0) + } else if (cent->maxy < 0) { cent->maxy = 0; + } - if (cent->minx > width - 1) + if (cent->minx > width - 1) { cent->minx = width - 1; - else if (cent->minx < 0) + } else if (cent->minx < 0) { cent->minx = 0; + } - if (cent->miny > height - 1) + if (cent->miny > height - 1) { cent->miny = height - 1; - else if (cent->miny < 0) + } else if (cent->miny < 0) { cent->miny = 0; + } /* Align for better locate box handling */ cent->minx += cent->minx % 2; @@ -370,8 +373,9 @@ void alg_noise_tune(struct context *cnt, unsigned char *new) for (; i > 0; i--) { diff = ABS(*ref - *new); - if (mask) + if (mask) { diff = ((diff * *mask++) / 255); + } if (*smartmask) { sum += diff + 1; @@ -383,8 +387,10 @@ void alg_noise_tune(struct context *cnt, unsigned char *new) smartmask++; } - if (count > 3) /* Avoid divide by zero. */ + if (count > 3) { + /* Avoid divide by zero. */ sum /= count / 3; + } /* 5: safe, 4: regular, 3: more sensitive */ cnt->noise = 4 + (cnt->noise + sum) / 2; @@ -399,22 +405,26 @@ void alg_threshold_tune(struct context *cnt, int diffs, int motion) int i; int sum = 0, top = diffs; - if (!diffs) + if (!diffs) { return; + } - if (motion) + if (motion) { diffs = cnt->threshold / 4; + } for (i = 0; i < THRESHOLD_TUNE_LENGTH - 1; i++) { sum += cnt->diffs_last[i]; - if (cnt->diffs_last[i + 1] && !motion) + if (cnt->diffs_last[i + 1] && !motion) { cnt->diffs_last[i] = cnt->diffs_last[i + 1]; - else + } else { cnt->diffs_last[i] = cnt->threshold / 4; + } - if (cnt->diffs_last[i] > top) + if (cnt->diffs_last[i] > top) { top = cnt->diffs_last[i]; + } } sum += cnt->diffs_last[i]; @@ -422,11 +432,13 @@ void alg_threshold_tune(struct context *cnt, int diffs, int motion) sum /= THRESHOLD_TUNE_LENGTH / 4; - if (sum < top * 2) + if (sum < top * 2) { sum = top * 2; + } - if (sum < cnt->conf.threshold) + if (sum < cnt->conf.threshold) { cnt->threshold = (cnt->threshold + sum) / 2; + } } /* @@ -463,8 +475,9 @@ static int iflood(int x, int y, int width, int height, Segment stack[MAXS], *sp = stack; /* Stack of filled segments. */ int count = 0; - if (x < 0 || x >= width || y < 0 || y >= height) + if (x < 0 || x >= width || y < 0 || y >= height) { return 0; + } PUSH(y, x, x, 1); /* Needed in some cases. */ PUSH(y+1, x, x, -1); /* Seed segment (popped 1st). */ @@ -481,13 +494,15 @@ static int iflood(int x, int y, int width, int height, count++; } - if (x >= x1) + if (x >= x1) { goto skip; + } l = x + 1; - if (l < x1) + if (l < x1) { PUSH(y, l, x1 - 1, -dy); /* Leak on left? */ + } x = x1 + 1; @@ -499,12 +514,13 @@ static int iflood(int x, int y, int width, int height, PUSH(y, l, x - 1, dy); - if (x > x2 + 1) + if (x > x2 + 1) { PUSH(y, x2 + 1, x - 1, -dy); /* Leak on right? */ + } skip: - for (x++; x <= x2 && !(out[y * width + x] != 0 && labels[y * width + x] == oldvalue); x++); + for (x++; x <= x2 && !(out[y * width + x] != 0 && labels[y * width + x] == oldvalue); x++) {} l = x; } while (x <= x2); @@ -548,8 +564,9 @@ static int alg_labeling(struct context *cnt) } /* Already visited by iflood */ - if (labels[pixelpos] > 0) + if (labels[pixelpos] > 0) { continue; + } labelsize = iflood(ix, iy, width, height, out, labels, current_label, 0); @@ -563,8 +580,9 @@ static int alg_labeling(struct context *cnt) labelsize = iflood(ix, iy, width, height, out, labels, current_label + 32768, current_label); imgs->labelgroup_max += labelsize; imgs->labels_above++; - } else if(max_under < labelsize) + } else if(max_under < labelsize) { max_under = labelsize; + } if (imgs->labelsize_max < labelsize) { imgs->labelsize_max = labelsize; @@ -627,10 +645,11 @@ static int dilate9(unsigned char *img, int width, int height, void *buffer) row3 = rowTemp; /* If we're at the last row, fill with zeros, otherwise copy from img. */ - if (y == height - 1) + if (y == height - 1) { memset(row3, 0, width); - else + } else { memcpy(row3, yp+width, width); + } /* Init slots 0 and 1 in the moving window. */ window[0] = MAX3(row1[0], row2[0], row3[0]); @@ -652,10 +671,11 @@ static int dilate9(unsigned char *img, int width, int height, void *buffer) * If the value is larger than the current max, use it. Otherwise, * calculate a new max (because the new value may not be the max. */ - if (latest >= blob) + if (latest >= blob) { blob = latest; - else + } else { blob = MAX3(window[0], window[1], window[2]); + } /* Write the max value (blob) to the image. */ if (blob != 0) { @@ -664,8 +684,9 @@ static int dilate9(unsigned char *img, int width, int height, void *buffer) } /* Wrap around the window index if necessary. */ - if (++widx == 3) + if (++widx == 3) { widx = 0; + } } /* Store zeros in the vertical sides. */ @@ -710,10 +731,11 @@ static int dilate5(unsigned char *img, int width, int height, void *buffer) row3 = rowTemp; /* If we're at the last row, fill with zeros, otherwise copy from img. */ - if (y == height - 1) + if (y == height - 1) { memset(row3, 0, width); - else + } else { memcpy(row3, yp + width, width); + } /* Init mem and set blob to force an evaluation of the entire + shape. */ mem = MAX2(row2[0], row2[1]); @@ -766,10 +788,11 @@ static int erode9(unsigned char *img, int width, int height, void *buffer, unsig memcpy(Row1, Row2, width); memcpy(Row2, Row3, width); - if (y == height-1) + if (y == height-1) { memset(Row3, flag, width); - else + } else { memcpy(Row3, img + (y+1) * width, width); + } for (i = width - 2; i >= 1; i--) { if (Row1[i - 1] == 0 || @@ -780,10 +803,11 @@ static int erode9(unsigned char *img, int width, int height, void *buffer, unsig Row2[i + 1] == 0 || Row3[i - 1] == 0 || Row3[i] == 0 || - Row3[i + 1] == 0) + Row3[i + 1] == 0) { img[y * width + i] = 0; - else + } else { sum++; + } } img[y * width] = img[y * width + width - 1] = flag; @@ -810,20 +834,22 @@ static int erode5(unsigned char *img, int width, int height, void *buffer, unsig memcpy(Row1, Row2, width); memcpy(Row2, Row3, width); - if (y == height-1) + if (y == height-1) { memset(Row3, flag, width); - else + } else { memcpy(Row3, img + (y + 1) * width, width); + } for (i = width - 2; i >= 1; i--) { if (Row1[i] == 0 || Row2[i - 1] == 0 || Row2[i] == 0 || Row2[i + 1] == 0 || - Row3[i] == 0) + Row3[i] == 0) { img[y * width + i] = 0; - else + } else { sum++; + } } img[y * width] = img[y * width + width - 1] = flag; @@ -847,13 +873,15 @@ int alg_despeckle(struct context *cnt, int olddiffs) for (i = 0; i < len; i++) { switch (cnt->conf.despeckle_filter[i]) { case 'E': - if ((diffs = erode9(out, width, height, common_buffer, 0)) == 0) + if ((diffs = erode9(out, width, height, common_buffer, 0)) == 0) { i = len; + } done = 1; break; case 'e': - if ((diffs = erode5(out, width, height, common_buffer, 0)) == 0) + if ((diffs = erode5(out, width, height, common_buffer, 0)) == 0) { i = len; + } done = 1; break; case 'D': @@ -875,8 +903,9 @@ int alg_despeckle(struct context *cnt, int olddiffs) /* If conf.despeckle_filter contains any valid action EeDdl */ if (done) { - if (done != 2) + if (done != 2) { cnt->imgs.labelsize_max = 0; // Disable Labeling + } return diffs; } else { cnt->imgs.labelsize_max = 0; // Disable Labeling @@ -900,23 +929,26 @@ void alg_tune_smartmask(struct context *cnt) for (i = 0; i < motionsize; i++) { /* Decrease smart_mask sensitivity every 5*speed seconds only. */ - if (smartmask[i] > 0) + if (smartmask[i] > 0) { smartmask[i]--; + } /* Increase smart_mask sensitivity based on the buffered values. */ diff = smartmask_buffer[i]/sensitivity; if (diff) { - if (smartmask[i] <= diff + 80) + if (smartmask[i] <= diff + 80) { smartmask[i] += diff; - else + } else { smartmask[i] = 80; + } smartmask_buffer[i] %= sensitivity; } /* Transfer raw mask to the final stage when above trigger value. */ - if (smartmask[i] > 20) + if (smartmask[i] > 20) { smartmask_final[i] = 0; - else + } else { smartmask_final[i] = 255; + } } /* Further expansion (here:erode due to inverted logic!) of the mask. */ diff = erode9(smartmask_final, cnt->imgs.width, cnt->imgs.height, @@ -1096,15 +1128,32 @@ int alg_diff_standard(struct context *cnt, unsigned char *new) movq_r2r(mm3, mm0); /* U */ /* Add to *smartmask_buffer. This is probably the fastest way to do it. */ + /* TODO: Revise this to use a loop */ if (cnt->event_nr != cnt->prev_event) { - if (mmtemp.ub[0]) smartmask_buffer[0] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[1]) smartmask_buffer[1] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[2]) smartmask_buffer[2] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[3]) smartmask_buffer[3] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[4]) smartmask_buffer[4] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[5]) smartmask_buffer[5] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[6]) smartmask_buffer[6] += SMARTMASK_SENSITIVITY_INCR; - if (mmtemp.ub[7]) smartmask_buffer[7] += SMARTMASK_SENSITIVITY_INCR; + if (mmtemp.ub[0]) { + smartmask_buffer[0] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[1]) { + smartmask_buffer[1] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[2]) { + smartmask_buffer[2] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[3]) { + smartmask_buffer[3] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[4]) { + smartmask_buffer[4] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[5]) { + smartmask_buffer[5] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[6]) { + smartmask_buffer[6] += SMARTMASK_SENSITIVITY_INCR; + } + if (mmtemp.ub[7]) { + smartmask_buffer[7] += SMARTMASK_SENSITIVITY_INCR; + } } smartmask_buffer += 8; @@ -1168,8 +1217,9 @@ int alg_diff_standard(struct context *cnt, unsigned char *new) for (; i > 0; i--) { register unsigned char curdiff = (int)(abs(*ref - *new)); /* Using a temp variable is 12% faster. */ /* Apply fixed mask */ - if (mask) + if (mask) { curdiff = ((int)(curdiff * *mask++) / 255); + } if (smartmask_speed) { if (curdiff > noise) { @@ -1180,11 +1230,13 @@ int alg_diff_standard(struct context *cnt, unsigned char *new) * speed=10) we add 5 here. NOT related to the 5 at ratio- * calculation. */ - if (cnt->event_nr != cnt->prev_event) + if (cnt->event_nr != cnt->prev_event) { (*smartmask_buffer) += SMARTMASK_SENSITIVITY_INCR; + } /* Apply smart_mask */ - if (!*smartmask_final) + if (!*smartmask_final) { curdiff = 0; + } } smartmask_final++; smartmask_buffer++; @@ -1212,8 +1264,9 @@ static char alg_diff_fast(struct context *cnt, int max_n_changes, unsigned char int noise = cnt->noise; unsigned char *ref = imgs->ref; - if (!step % 2) + if (!step % 2) { step++; + } /* We're checking only 1 of several pixels. */ max_n_changes /= step; @@ -1223,8 +1276,9 @@ static char alg_diff_fast(struct context *cnt, int max_n_changes, unsigned char register unsigned char curdiff = (int)(abs((char)(*ref - *new))); /* Using a temp variable is 12% faster. */ if (curdiff > noise) { diffs++; - if (diffs > max_n_changes) + if (diffs > max_n_changes) { return 1; + } } ref += step; new += step; @@ -1242,8 +1296,9 @@ int alg_diff(struct context *cnt, unsigned char *new) { int diffs = 0; - if (alg_diff_fast(cnt, cnt->conf.threshold / 2, new)) + if (alg_diff_fast(cnt, cnt->conf.threshold / 2, new)) { diffs = alg_diff_standard(cnt, new); + } return diffs; } @@ -1258,14 +1313,17 @@ int alg_lightswitch(struct context *cnt, int diffs) { struct images *imgs = &cnt->imgs; - if (cnt->conf.lightswitch_percent < 0) + if (cnt->conf.lightswitch_percent < 0) { cnt->conf.lightswitch_percent = 0; - if (cnt->conf.lightswitch_percent > 100) + } + if (cnt->conf.lightswitch_percent > 100) { cnt->conf.lightswitch_percent = 100; + } /* Is lightswitch percent of the image changed? */ - if (diffs > (imgs->motionsize * cnt->conf.lightswitch_percent / 100)) + if (diffs > (imgs->motionsize * cnt->conf.lightswitch_percent / 100)) { return 1; + } return 0; } @@ -1284,15 +1342,18 @@ int alg_switchfilter(struct context *cnt, int diffs, unsigned char *newimg) for (y = 0; y < cnt->imgs.height; y++) { line = 0; for (x = 0; x < cnt->imgs.width; x++) { - if (*(out++)) + if (*(out++)) { line++; + } } - if (line > cnt->imgs.width / 18) + if (line > cnt->imgs.width / 18) { vertlines++; + } - if (line > linediff * 2) + if (line > linediff * 2) { lines++; + } } if (vertlines > cnt->imgs.height / 10 && lines < vertlines / 3 && @@ -1332,8 +1393,10 @@ void alg_update_reference_frame(struct context *cnt, int action) unsigned char *smartmask = cnt->imgs.smartmask_final; unsigned char *out = cnt->imgs.img_motion.image_norm; - if (cnt->lastrate > 5) /* Match rate limit */ + /* Match rate limit */ + if (cnt->lastrate > 5) { accept_timer /= (cnt->lastrate / 3); + } if (action == UPDATE_REF_FRAME) { /* Black&white only for better performance. */ threshold_ref = cnt->noise * EXCLUDE_LEVEL_PERCENT / 100; diff --git a/src/conf.c b/src/conf.c index c5cc1ece..ac531d06 100644 --- a/src/conf.c +++ b/src/conf.c @@ -2043,11 +2043,12 @@ static void conf_cmdline(struct context *cnt, int thread) * if necessary. This is accomplished by calling mystrcpy(); * see this function for more information. */ - while ((c = getopt(conf->argc, conf->argv, "bc:d:hmns?p:k:l:")) != EOF) + while ((c = getopt(conf->argc, conf->argv, "bc:d:hmns?p:k:l:")) != EOF) { switch (c) { case 'c': - if (thread == -1) + if (thread == -1) { strcpy(cnt->conf_filename, optarg); + } break; case 'b': cnt->daemon = 1; @@ -2060,8 +2061,9 @@ static void conf_cmdline(struct context *cnt, int thread) break; case 'd': /* No validation - just take what user gives. */ - if (thread == -1) + if (thread == -1) { cnt->log_level = (unsigned int)atoi(optarg); + } break; case 'k': if (thread == -1) { @@ -2090,6 +2092,7 @@ static void conf_cmdline(struct context *cnt, int thread) usage(); exit(1); } + } optind = 1; } @@ -2109,7 +2112,9 @@ struct context **conf_cmdparse(struct context **cnt, const char *cmd, const char { unsigned int i = 0; - if (!cmd) return cnt; + if (!cmd) { + return cnt; + } /* * We search through config_params until we find a param_name that matches @@ -2120,7 +2125,7 @@ struct context **conf_cmdparse(struct context **cnt, const char *cmd, const char /* If config_param is string we don't want to check arg1. */ if (strcasecmp(config_type(&config_params[i]), "string")) { - if (config_params[i].conf_value && !arg1){ + if (config_params[i].conf_value && !arg1) { return cnt; } } @@ -2154,7 +2159,7 @@ struct context **conf_cmdparse(struct context **cnt, const char *cmd, const char MOTION_LOG(ALR, TYPE_ALL, NO_ERRNO, _("%s after version %s") , dep_config_params[i].info, dep_config_params[i].last_version); - if (dep_config_params[i].copy != NULL){ + if (dep_config_params[i].copy != NULL) { /* If the depreciated option is a vid item, copy_video_params is called * with the array index sent instead of the context structure member pointer. */ @@ -2167,8 +2172,7 @@ struct context **conf_cmdparse(struct context **cnt, const char *cmd, const char !strcmp(dep_config_params[i].name,"input") || !strcmp(dep_config_params[i].name,"norm") || !strcmp(dep_config_params[i].name,"frequency") || - !strcmp(dep_config_params[i].name,"vid_control_params") ) - { + !strcmp(dep_config_params[i].name,"vid_control_params")) { cnt = copy_video_params(cnt, arg1, i); } else if (!strcmp(dep_config_params[i].name,"netcam_decoder") || @@ -2178,8 +2182,7 @@ struct context **conf_cmdparse(struct context **cnt, const char *cmd, const char !strcmp(dep_config_params[i].name,"netcam_ratehigh") || !strcmp(dep_config_params[i].name,"netcam_proxy") || !strcmp(dep_config_params[i].name,"netcam_tolerant_check") || - !strcmp(dep_config_params[i].name,"netcam_keepalive") ) - { + !strcmp(dep_config_params[i].name,"netcam_keepalive")) { cnt = copy_netcam_params(cnt, arg1, i); } else { @@ -2218,31 +2221,35 @@ static struct context **conf_process(struct context **cnt, FILE *fp) char *beg = NULL, *end = NULL; while (fgets(line, PATH_MAX-1, fp)) { - if (!(line[0] == '#' || line[0] == ';' || strlen(line) < 2)) {/* skipcomment */ - + if (!(line[0] == '#' || line[0] == ';' || strlen(line) < 2)) { + /* skipcomment */ arg1 = NULL; /* Trim white space and any CR or LF at the end of the line. */ end = line + strlen(line) - 1; /* Point to the last non-null character in the string. */ - while (end >= line && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) + while (end >= line && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) { end--; + } *(end+1) = '\0'; /* If line is only whitespace we continue to the next line. */ - if (strlen(line) == 0) + if (strlen(line) == 0) { continue; + } /* Trim leading whitespace from the line and find command. */ beg = line; - while (*beg == ' ' || *beg == '\t') + while (*beg == ' ' || *beg == '\t') { beg++; + } cmd = beg; /* Command starts here. */ - while (*beg != ' ' && *beg != '\t' && *beg != '=' && *beg != '\0') + while (*beg != ' ' && *beg != '\t' && *beg != '=' && *beg != '\0') { beg++; + } *beg = '\0'; /* Command string terminates here. */ @@ -2250,8 +2257,9 @@ static struct context **conf_process(struct context **cnt, FILE *fp) beg++; if (strlen(beg) > 0) { - while (*beg == ' ' || *beg == '\t' || *beg == '=' || *beg == '\n' || *beg == '\r') + while (*beg == ' ' || *beg == '\t' || *beg == '=' || *beg == '\n' || *beg == '\r') { beg++; + } /* @@ -2297,8 +2305,9 @@ void conf_print(struct context **cnt) conffile = myfopen(cnt[thread]->conf_filename, "w"); - if (!conffile) + if (!conffile) { continue; + } char timestamp[32]; time_t now = time(0); @@ -2318,10 +2327,11 @@ void conf_print(struct context **cnt) * If the option is a text_* and first char is a space put * quotation marks around to allow leading spaces. */ - if (strncmp(config_params[i].param_name, "text", 4) || strncmp(retval, " ", 1)) + if (strncmp(config_params[i].param_name, "text", 4) || strncmp(retval, " ", 1)) { fprintf(conffile, "%s %s\n\n", config_params[i].param_name, retval); - else + } else { fprintf(conffile, "%s \"%s\"\n\n", config_params[i].param_name, retval); + } } else { val = NULL; config_params[i].print(cnt, &val, i, thread); @@ -2334,19 +2344,21 @@ void conf_print(struct context **cnt) if (val) { fprintf(conffile, "%s\n", config_params[i].param_help); - if (strlen(val) > 0) + if (strlen(val) > 0) { fprintf(conffile, "%s\n", val); - else + } else { fprintf(conffile, "; camera %s/camera1.conf\n", sysconfdir); + } free(val); } else if (thread == 0) { char value[PATH_MAX]; /* The 'camera_dir' option should keep the installed default value */ - if (!strncmp(config_params[i].param_name, "camera_dir", 10)) + if (!strncmp(config_params[i].param_name, "camera_dir", 10)) { sprintf(value, "%s", sysconfdir"/conf.d"); - else + } else { sprintf(value, "%s", "value"); + } fprintf(conffile, "%s\n", config_params[i].param_help); fprintf(conffile, "; %s %s\n\n", config_params[i].param_name, value); @@ -2427,19 +2439,21 @@ struct context **conf_load(struct context **cnt) conf_cmdline(cnt[0], -1); - if (cnt[0]->conf_filename[0]) { /* User has supplied filename on Command-line. */ - strncpy(filename, cnt[0]->conf_filename, PATH_MAX-1); - filename[PATH_MAX-1] = '\0'; - fp = fopen (filename, "r"); + if (cnt[0]->conf_filename[0]) { + /* User has supplied filename on Command-line. */ + strncpy(filename, cnt[0]->conf_filename, PATH_MAX-1); + filename[PATH_MAX-1] = '\0'; + fp = fopen (filename, "r"); } if (!fp) { /* Command-line didn't work, try current dir. */ char path[PATH_MAX]; - if (cnt[0]->conf_filename[0]) + if (cnt[0]->conf_filename[0]) { MOTION_LOG(ALR, TYPE_ALL, SHOW_ERRNO ,_("Configfile %s not found - trying defaults.") ,filename); + } if (getcwd(path, sizeof(path)) == NULL) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Error getcwd")); @@ -2460,10 +2474,11 @@ struct context **conf_load(struct context **cnt) snprintf(filename, PATH_MAX, "%s/motion.conf", sysconfdir); fp = fopen(filename, "r"); - if (!fp) /* There is no config file.... use defaults. */ + if (!fp) { /* There is no config file.... use defaults. */ MOTION_LOG(ALR, TYPE_ALL, SHOW_ERRNO ,_("could not open configfile %s") ,filename); + } } } @@ -2472,7 +2487,7 @@ struct context **conf_load(struct context **cnt) retcd = snprintf(cnt[0]->conf_filename ,sizeof(cnt[0]->conf_filename) ,"%s",filename); - if ((retcd < 0) || (retcd >= (int)sizeof(cnt[0]->conf_filename))){ + if ((retcd < 0) || (retcd >= (int)sizeof(cnt[0]->conf_filename))) { MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO ,_("Invalid file name %s"), filename); } else { @@ -2498,24 +2513,29 @@ struct context **conf_load(struct context **cnt) */ i = -1; - while (cnt[++i]) + while (cnt[++i]) { conf_cmdline(cnt[i], i); + } /* If pid file was passed from Command-line copy to main thread conf struct. */ - if (cnt[0]->pid_file[0]) + if (cnt[0]->pid_file[0]) { cnt[0]->conf.pid_file = mystrcpy(cnt[0]->conf.pid_file, cnt[0]->pid_file); + } /* If log file was passed from Command-line copy to main thread conf struct. */ - if (cnt[0]->log_file[0]) + if (cnt[0]->log_file[0]) { cnt[0]->conf.log_file = mystrcpy(cnt[0]->conf.log_file, cnt[0]->log_file); + } /* If log type string was passed from Command-line copy to main thread conf struct. */ - if (cnt[0]->log_type_str[0]) + if (cnt[0]->log_type_str[0]) { cnt[0]->conf.log_type = mystrcpy(cnt[0]->conf.log_type, cnt[0]->log_type_str); + } /* if log level was passed from Command-line copy to main thread conf struct. */ - if (cnt[0]->log_level != -1) + if (cnt[0]->log_level != -1) { cnt[0]->conf.log_level = cnt[0]->log_level; + } config_parms_intl(); @@ -2536,7 +2556,9 @@ void conf_output_parms(struct context **cnt) unsigned int i, t = 0; const char *name, *value; - while(cnt[++t]); + while(cnt[++t]) { + continue; + } MOTION_LOG(INF, TYPE_ALL, NO_ERRNO ,_("Writing configuration parameters from all files (%d):"), t); @@ -2557,18 +2579,21 @@ void conf_output_parms(struct context **cnt) !strncmp(name, "webcontrol_key", 14) || !strncmp(name, "webcontrol_cert", 15) || !strncmp(name, "database_user", 13) || - !strncmp(name, "database_password", 17)) - { + !strncmp(name, "database_password", 17)) { motion_log(INF, TYPE_ALL, NO_ERRNO,0 ,_("%-25s "), name); + } else { - if (strncmp(name, "text", 4) || strncmp(value, " ", 1)) + if (strncmp(name, "text", 4) || strncmp(value, " ", 1)) { motion_log(INF, TYPE_ALL, NO_ERRNO,0, "%-25s %s", name, value); - else + } else { motion_log(INF, TYPE_ALL, NO_ERRNO,0, "%-25s \"%s\"", name, value); + } } } else { - if (t == 0) motion_log(INF, TYPE_ALL, NO_ERRNO,0, "%-25s ", name); + if (t == 0) { + motion_log(INF, TYPE_ALL, NO_ERRNO,0, "%-25s ", name); + } } i++; } @@ -2593,7 +2618,8 @@ void malloc_strings(struct context *cnt) char **val; while (config_params[i].param_name != NULL) { if (config_params[i].copy == copy_string || - config_params[i].copy == copy_uri) { /* if member is a string */ + config_params[i].copy == copy_uri) { + /* if member is a string */ /* val is made to point to a pointer to the current string. */ val = (char **)((char *)cnt+config_params[i].conf_value); @@ -2656,8 +2682,9 @@ static struct context **copy_bool(struct context **cnt, const char *str, int val *((int *)tmp) = 0; } - if (cnt[0]->threadnr) + if (cnt[0]->threadnr) { return cnt; + } } return cnt; @@ -2686,8 +2713,9 @@ static struct context **copy_int(struct context **cnt, const char *str, int val_ } else { *((int *)tmp) = atoi(str); } - if (cnt[0]->threadnr) + if (cnt[0]->threadnr) { return cnt; + } } return cnt; @@ -2724,8 +2752,9 @@ struct context **copy_string(struct context **cnt, const char *str, int val_ptr) * Set the option on all threads if setting the option * for thread 0; otherwise just set that one thread's option. */ - if (cnt[0]->threadnr) + if (cnt[0]->threadnr) { return cnt; + } } return cnt; @@ -2745,17 +2774,19 @@ static struct context **copy_video_params(struct context **cnt, const char *conf indx_vid = 0; while (config_params[indx_vid].param_name != NULL) { - if (!strcmp(config_params[indx_vid].param_name,"video_params")) break; + if (!strcmp(config_params[indx_vid].param_name,"video_params")) { + break; + } indx_vid++; } - if (strcmp(config_params[indx_vid].param_name,"video_params")){ + if (strcmp(config_params[indx_vid].param_name,"video_params")) { MOTION_LOG(ALR, TYPE_ALL, NO_ERRNO ,_("Unable to locate video_params")); return cnt; } - if (config_val == NULL){ + if (config_val == NULL) { MOTION_LOG(ALR, TYPE_ALL, NO_ERRNO ,_("No value provided to put into video_params")); } @@ -2763,9 +2794,14 @@ static struct context **copy_video_params(struct context **cnt, const char *conf /* If the depreciated option is the default, then just return */ parmval = atoi(config_val); if (!strcmp(dep_config_params[config_indx].name,"power_line_frequency") && - (parmval == -1)) return cnt; + (parmval == -1)) { + return cnt; + } + if (strcmp(dep_config_params[config_indx].name,"power_line_frequency") && - (parmval == 0)) return cnt; + (parmval == 0)) { + return cnt; + } /* Remove underscore from parm name and add quotes*/ if (!strcmp(dep_config_params[config_indx].name,"power_line_frequency")) { @@ -2828,17 +2864,19 @@ static struct context **copy_netcam_params(struct context **cnt, const char *con indx = 0; while (config_params[indx].param_name != NULL) { - if (!strcmp(config_params[indx].param_name,"netcam_params")) break; + if (!strcmp(config_params[indx].param_name,"netcam_params")) { + break; + } indx++; } - if (strcmp(config_params[indx].param_name,"netcam_params")){ + if (strcmp(config_params[indx].param_name,"netcam_params")) { MOTION_LOG(ALR, TYPE_ALL, NO_ERRNO ,_("Unable to locate netcam_params")); return cnt; } - if (config_val == NULL){ + if (config_val == NULL) { MOTION_LOG(ALR, TYPE_ALL, NO_ERRNO ,_("No value provided to put into netcam_params")); return cnt; @@ -2846,8 +2884,7 @@ static struct context **copy_netcam_params(struct context **cnt, const char *con /* Remove underscore from parm name and add quotes*/ if (!strcmp(dep_config_params[config_indx].name,"netcam_use_tcp") || - !strcmp(dep_config_params[config_indx].name,"rtsp_uses_tcp")) - { + !strcmp(dep_config_params[config_indx].name,"rtsp_uses_tcp")) { parm_len = strlen("rtsp_transport = tcp") + 1; parm_new = mymalloc(parm_len); if ( !strcmp(config_val,"on")) { @@ -2862,8 +2899,7 @@ static struct context **copy_netcam_params(struct context **cnt, const char *con snprintf(parm_new, parm_len, "%s%s", "decoder = ", config_val); } else if (!strcmp(dep_config_params[config_indx].name,"netcam_rate") || - !strcmp(dep_config_params[config_indx].name,"netcam_ratehigh")) - { + !strcmp(dep_config_params[config_indx].name,"netcam_ratehigh")) { parm_len = strlen("capture_rate = ") + strlen(config_val) + 1; parm_new = mymalloc(parm_len); snprintf(parm_new, parm_len, "%s%s", "capture_rate = ", config_val); @@ -2890,7 +2926,7 @@ static struct context **copy_netcam_params(struct context **cnt, const char *con /* Recall that the current parms have already been processed by time this is called */ i = -1; while (cnt[++i]) { - if (!strcmp(dep_config_params[config_indx].name,"netcam_ratehigh")){ + if (!strcmp(dep_config_params[config_indx].name,"netcam_ratehigh")) { if (cnt[i]->conf.netcam_high_params != NULL) { parm_len = strlen(cnt[i]->conf.netcam_high_params) + 1; orig_parm = mymalloc(parm_len); @@ -2955,8 +2991,9 @@ static struct context **copy_text_double(struct context **cnt, const char *str, *((int *)tmp) = 1; } - if (cnt[0]->threadnr) + if (cnt[0]->threadnr) { return cnt; + } } return cnt; @@ -2983,8 +3020,9 @@ static struct context **copy_html_output(struct context **cnt, const char *str, *((int *)tmp) = 1; } - if (cnt[0]->threadnr) + if (cnt[0]->threadnr) { return cnt; + } } return cnt; @@ -3043,8 +3081,9 @@ char *mystrcpy(char *to, const char *from) * same value as from. */ - if (to != NULL) + if (to != NULL) { free(to); + } return mystrdup(from); } @@ -3093,14 +3132,18 @@ char *mystrdup(const char *from) */ const char *config_type(config_param *configparam) { - if (configparam->copy == copy_string) + if (configparam->copy == copy_string) { return "string"; - if (configparam->copy == copy_int) + } + if (configparam->copy == copy_int) { return "int"; - if (configparam->copy == copy_bool) + } + if (configparam->copy == copy_bool) { return "bool"; - if (configparam->copy == copy_uri) + } + if (configparam->copy == copy_uri) { return "uri"; + } return "unknown"; } @@ -3117,14 +3160,15 @@ static const char *print_bool(struct context **cnt, char **str ATTRIBUTE_UNUSED, { int val = config_params[parm].conf_value; - if (threadnr && - *(int*)((char *)cnt[threadnr] + val) == *(int*)((char *)cnt[0] + val)) + if (threadnr && *(int*)((char *)cnt[threadnr] + val) == *(int*)((char *)cnt[0] + val)) { return NULL; + } - if (*(int*)((char *)cnt[threadnr] + val)) + if (*(int*)((char *)cnt[threadnr] + val)) { return "on"; - else + } else { return "off"; + } } /** @@ -3148,8 +3192,9 @@ static const char *print_string(struct context **cnt, cptr0 = (const char **)((char *)cnt[0] + val); cptr1 = (const char **)((char *)cnt[threadnr] + val); - if ((threadnr) && (*cptr0 != NULL) && (*cptr1 != NULL) && (!strcmp(*cptr0, *cptr1))) + if ((threadnr) && (*cptr0 != NULL) && (*cptr1 != NULL) && (!strcmp(*cptr0, *cptr1))) { return NULL; + } return *cptr1; } @@ -3170,9 +3215,9 @@ static const char *print_int(struct context **cnt, char **str ATTRIBUTE_UNUSED, static char retval[20]; int val = config_params[parm].conf_value; - if (threadnr && - *(int*)((char *)cnt[threadnr] + val) == *(int*)((char *)cnt[0] + val)) + if (threadnr && *(int*)((char *)cnt[threadnr] + val) == *(int*)((char *)cnt[0] + val)) { return NULL; + } sprintf(retval, "%d", *(int*)((char *)cnt[threadnr] + val)); @@ -3192,16 +3237,18 @@ static const char *print_camera(struct context **cnt, char **str, char *retval; unsigned int i = 0; - if (!str || threadnr) + if (!str || threadnr) { return NULL; + } retval = mymalloc(1); retval[0] = 0; while (cnt[++i]) { /* Skip config files loaded from conf directory */ - if (cnt[i]->from_conf_dir) + if (cnt[i]->from_conf_dir) { continue; + } retval = myrealloc(retval, strlen(retval) + strlen(cnt[i]->conf_filename) + 10, "print_camera"); @@ -3229,18 +3276,12 @@ struct context **read_camera_dir(struct context **cnt, const char *str, int val) char conf_file[PATH_MAX]; dp = opendir(str); - if (dp != NULL) - { - while( (ep = readdir(dp)) ) - { + if (dp != NULL) { + while( (ep = readdir(dp)) ) { name_len = strlen(ep->d_name); if (name_len > strlen(EXTENSION) && - (strncmp(EXTENSION, - (ep->d_name + name_len - strlen(EXTENSION)), - strlen(EXTENSION)) == 0 - ) - ) - { + (strncmp(EXTENSION,(ep->d_name + name_len - strlen(EXTENSION)), + strlen(EXTENSION)) == 0 )) { memset(conf_file, '\0', sizeof(conf_file)); snprintf(conf_file, sizeof(conf_file) - 1, "%s/%s", str, ep->d_name); @@ -3251,14 +3292,14 @@ struct context **read_camera_dir(struct context **cnt, const char *str, int val) * set it as created from conf directory. */ i = 0; - while (cnt[++i]); + while (cnt[++i]) { + continue; + } cnt[i-1]->from_conf_dir = 1; - } + } } closedir(dp); - } - else - { + } else { MOTION_LOG(ALR, TYPE_ALL, SHOW_ERRNO ,_("Camera directory config %s not found"), str); return cnt; @@ -3288,8 +3329,9 @@ static struct context **config_camera(struct context **cnt, const char *str, int i; FILE *fp; - if (cnt[0]->threadnr) + if (cnt[0]->threadnr) { return cnt; + } fp = fopen(str, "r"); @@ -3302,7 +3344,9 @@ static struct context **config_camera(struct context **cnt, const char *str, /* Find the current number of threads defined. */ i = -1; - while (cnt[++i]); + while (cnt[++i]) { + continue; + } /* * Make space for the threads + the terminating NULL pointer @@ -3379,7 +3423,7 @@ static void config_parms_intl() * it will be printed when called from the conf_load. */ - if (FALSE){ + if (FALSE) { MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","daemon",_("daemon")); MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","setup_mode",_("setup_mode")); MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO,"%s:%s","pid_file",_("pid_file")); diff --git a/src/draw.c b/src/draw.c index 1993f386..28ae9674 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1076,13 +1076,17 @@ static int draw_textn(unsigned char *image, int startx, int starty, int width, int pos, line_offset, next_char_offs; unsigned char *image_ptr, *char_ptr; - if (startx > width / 2) + if (startx > width / 2) { startx -= len * (6 * factor); + } - if (startx + len * 6 * factor >= width) + if (startx + len * 6 * factor >= width) { len = (width-startx-1)/(6*factor); + } - if ((startx < 1) || (starty < 1) || (len < 1)) return 0; + if ((startx < 1) || (starty < 1) || (len < 1)) { + return 0; + } line_offset = width - (7 * factor); next_char_offs = (width * 8 * factor) - (6 * factor); @@ -1092,7 +1096,9 @@ static int draw_textn(unsigned char *image, int startx, int starty, int width, for (pos = 0; pos < len; pos++) { int pos_check = (int)text[pos]; - if ((pos_check <0) || (pos_check >127)) pos_check = 45; /* Use a - for non ascii characters*/ + if ((pos_check <0) || (pos_check >127)) { + pos_check = 45; /* Use a - for non ascii characters*/ + } char_ptr = char_arr_ptr[pos_check]; @@ -1134,24 +1140,32 @@ int draw_text(unsigned char *image, int width, int height, int startx, int start begin = end = text; txtlen = 0; while ((end = strstr(end, NEWLINE))) { - if ((end - begin)>txtlen) txtlen = (end - begin); + if ((end - begin)>txtlen) { + txtlen = (end - begin); + } num_nl++; end += sizeof(NEWLINE)-1; begin = end; } - if (txtlen == 0) txtlen = strlen(text); + if (txtlen == 0) { + txtlen = strlen(text); + } /* Adjust the factor if it is out of bounds * txtlen at this point is the approx length of longest line */ - if ((txtlen * 7 * factor) > width){ + if ((txtlen * 7 * factor) > width) { factor = (width / (txtlen * 7)); - if (factor <= 0) factor = 1; + if (factor <= 0) { + factor = 1; + } } - if (((num_nl+1) * 8 * factor) > height){ + if (((num_nl+1) * 8 * factor) > height) { factor = (height / ((num_nl+1) * 8)); - if (factor <= 0) factor = 1; + if (factor <= 0) { + factor = 1; + } } line_space = factor * 9; diff --git a/src/event.c b/src/event.c index 1f2befd4..9a2148eb 100644 --- a/src/event.c +++ b/src/event.c @@ -89,8 +89,9 @@ static void exec_command(struct context *cnt, char *command, char *filename, int * Close any file descriptor except console because we will * like to see error messages */ - for (i = getdtablesize() - 1; i > 2; i--) + for (i = getdtablesize() - 1; i > 2; i--) { close(i); + } execl("/bin/sh", "sh", "-c", stamp, " &", NULL); @@ -126,8 +127,9 @@ static void event_beep(struct context *cnt, motion_event type ATTRIBUTE_UNUSED, void *ftype ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (!cnt->conf.quiet) + if (!cnt->conf.quiet) { printf("\a"); + } } /** @@ -145,11 +147,13 @@ static void on_picture_save_command(struct context *cnt, { int filetype = (unsigned long)arg; - if ((filetype & FTYPE_IMAGE_ANY) != 0 && cnt->conf.on_picture_save) + if ((filetype & FTYPE_IMAGE_ANY) != 0 && cnt->conf.on_picture_save) { exec_command(cnt, cnt->conf.on_picture_save, filename, filetype); + } - if ((filetype & FTYPE_MPEG_ANY) != 0 && cnt->conf.on_movie_start) + if ((filetype & FTYPE_MPEG_ANY) != 0 && cnt->conf.on_movie_start) { exec_command(cnt, cnt->conf.on_movie_start, filename, filetype); + } } static void on_motion_detected_command(struct context *cnt, @@ -158,8 +162,9 @@ static void on_motion_detected_command(struct context *cnt, char *dummy2 ATTRIBUTE_UNUSED, void *dummy3 ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (cnt->conf.on_motion_detected) + if (cnt->conf.on_motion_detected) { exec_command(cnt, cnt->conf.on_motion_detected, NULL, 0); + } } #if defined(HAVE_MYSQL) || defined(HAVE_PGSQL) || defined(HAVE_SQLITE3) || defined(HAVE_MARIADB) @@ -309,8 +314,9 @@ static void event_sqlnewfile(struct context *cnt, motion_event type ATTRIBUTE_U int sqltype = (unsigned long)arg; /* Only log the file types we want */ - if (!(cnt->conf.database_type) || (sqltype & cnt->sql_mask) == 0) + if (!(cnt->conf.database_type) || (sqltype & cnt->sql_mask) == 0) { return; + } /* * We place the code in a block so we only spend time making space in memory @@ -333,8 +339,9 @@ static void event_sqlfileclose(struct context *cnt, motion_event type ATTRIBUTE int sqltype = (unsigned long)arg; /* Only log the file types we want */ - if (!(cnt->conf.database_type) || (sqltype & cnt->sql_mask) == 0) + if (!(cnt->conf.database_type) || (sqltype & cnt->sql_mask) == 0) { return; + } /* * We place the code in a block so we only spend time making space in memory @@ -358,8 +365,9 @@ static void on_area_command(struct context *cnt, char *dummy2 ATTRIBUTE_UNUSED, void *dummy3 ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (cnt->conf.on_area_detected) + if (cnt->conf.on_area_detected) { exec_command(cnt, cnt->conf.on_area_detected, NULL, 0); + } } static void on_event_start_command(struct context *cnt, @@ -368,8 +376,9 @@ static void on_event_start_command(struct context *cnt, char *dummy2 ATTRIBUTE_UNUSED, void *dummy3 ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (cnt->conf.on_event_start) + if (cnt->conf.on_event_start) { exec_command(cnt, cnt->conf.on_event_start, NULL, 0); + } } static void on_event_end_command(struct context *cnt, @@ -378,8 +387,9 @@ static void on_event_end_command(struct context *cnt, char *dummy2 ATTRIBUTE_UNUSED, void *dummy3 ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (cnt->conf.on_event_end) + if (cnt->conf.on_event_end) { exec_command(cnt, cnt->conf.on_event_end, NULL, 0); + } } static void event_stream_put(struct context *cnt, @@ -391,11 +401,11 @@ static void event_stream_put(struct context *cnt, pthread_mutex_lock(&cnt->mutex_stream); /* Normal stream processing */ - if (cnt->stream_norm.cnct_count > 0){ - if (cnt->stream_norm.jpeg_data == NULL){ + if (cnt->stream_norm.cnct_count > 0) { + if (cnt->stream_norm.jpeg_data == NULL) { cnt->stream_norm.jpeg_data = mymalloc(cnt->imgs.size_norm); } - if (img_data->image_norm != NULL){ + if (img_data->image_norm != NULL) { cnt->stream_norm.jpeg_size = put_picture_memory(cnt ,cnt->stream_norm.jpeg_data ,cnt->imgs.size_norm @@ -407,17 +417,16 @@ static void event_stream_put(struct context *cnt, } /* Substream processing */ - if (cnt->stream_sub.cnct_count > 0){ - if (cnt->stream_sub.jpeg_data == NULL){ + if (cnt->stream_sub.cnct_count > 0) { + if (cnt->stream_sub.jpeg_data == NULL) { cnt->stream_sub.jpeg_data = mymalloc(cnt->imgs.size_norm); } - if (img_data->image_norm != NULL){ + if (img_data->image_norm != NULL) { /* Resulting substream image must be multiple of 8 */ if (((cnt->imgs.width % 16) == 0) && ((cnt->imgs.height % 16) == 0)) { - subsize = ((cnt->imgs.width / 2) * (cnt->imgs.height / 2) * 3 / 2); - if (cnt->imgs.substream_image == NULL){ + if (cnt->imgs.substream_image == NULL) { cnt->imgs.substream_image = mymalloc(subsize); } pic_scale_img(cnt->imgs.width @@ -445,11 +454,11 @@ static void event_stream_put(struct context *cnt, } /* Motion stream processing */ - if (cnt->stream_motion.cnct_count > 0){ - if (cnt->stream_motion.jpeg_data == NULL){ + if (cnt->stream_motion.cnct_count > 0) { + if (cnt->stream_motion.jpeg_data == NULL) { cnt->stream_motion.jpeg_data = mymalloc(cnt->imgs.size_norm); } - if (cnt->imgs.img_motion.image_norm != NULL){ + if (cnt->imgs.img_motion.image_norm != NULL) { cnt->stream_motion.jpeg_size = put_picture_memory(cnt ,cnt->stream_motion.jpeg_data ,cnt->imgs.size_norm @@ -461,11 +470,11 @@ static void event_stream_put(struct context *cnt, } /* Source stream processing */ - if (cnt->stream_source.cnct_count > 0){ - if (cnt->stream_source.jpeg_data == NULL){ + if (cnt->stream_source.cnct_count > 0) { + if (cnt->stream_source.jpeg_data == NULL) { cnt->stream_source.jpeg_data = mymalloc(cnt->imgs.size_norm); } - if (cnt->imgs.image_virgin.image_norm != NULL){ + if (cnt->imgs.image_virgin.image_norm != NULL) { cnt->stream_source.jpeg_size = put_picture_memory(cnt ,cnt->stream_source.jpeg_data ,cnt->imgs.size_norm @@ -487,20 +496,23 @@ static void event_vlp_putpipe(struct context *cnt, struct timeval *tv1 ATTRIBUTE_UNUSED) { if (*(int *)devpipe >= 0) { - if (vlp_putpipe(*(int *)devpipe, img_data->image_norm, cnt->imgs.size_norm) == -1) + if (vlp_putpipe(*(int *)devpipe, img_data->image_norm, cnt->imgs.size_norm) == -1) { MOTION_LOG(ERR, TYPE_EVENTS, SHOW_ERRNO ,_("Failed to put image into video pipe")); + } } } #endif /* defined(HAVE_V4L2) && !defined(BSD) */ const char *imageext(struct context *cnt) { - if (cnt->imgs.picture_type == IMAGE_TYPE_PPM) + if (cnt->imgs.picture_type == IMAGE_TYPE_PPM) { return "ppm"; + } - if (cnt->imgs.picture_type == IMAGE_TYPE_WEBP) + if (cnt->imgs.picture_type == IMAGE_TYPE_WEBP) { return "webp"; + } return "jpg"; } @@ -521,10 +533,11 @@ static void event_image_detect(struct context *cnt, * conf.imagepath would normally be defined but if someone deleted it by control interface * it is better to revert to the default than fail */ - if (cnt->conf.picture_filename) + if (cnt->conf.picture_filename) { imagepath = cnt->conf.picture_filename; - else + } else { imagepath = DEF_IMAGEPATH; + } mystrftime(cnt, filename, sizeof(filename), imagepath, currenttime_tv, NULL, 0); snprintf(fullfilename, PATH_MAX, "%.*s/%.*s.%s" @@ -560,10 +573,11 @@ static void event_imagem_detect(struct context *cnt, * conf.picture_filename would normally be defined but if someone deleted it by control interface * it is better to revert to the default than fail */ - if (cnt->conf.picture_filename) + if (cnt->conf.picture_filename) { imagepath = cnt->conf.picture_filename; - else + } else { imagepath = DEF_IMAGEPATH; + } mystrftime(cnt, filename, sizeof(filename), imagepath, currenttime_tv, NULL, 0); @@ -592,8 +606,9 @@ static void event_image_snapshot(struct context *cnt, int offset = 0; int len = strlen(cnt->conf.snapshot_filename); - if (len >= 9) + if (len >= 9) { offset = len - 8; + } if (strcmp(cnt->conf.snapshot_filename+offset, "lastsnap")) { char linkpath[PATH_MAX]; @@ -602,10 +617,11 @@ static void event_image_snapshot(struct context *cnt, * conf.snapshot_filename would normally be defined but if someone deleted it by control interface * it is better to revert to the default than fail */ - if (cnt->conf.snapshot_filename) + if (cnt->conf.snapshot_filename) { snappath = cnt->conf.snapshot_filename; - else + } else { snappath = DEF_SNAPPATH; + } mystrftime(cnt, filepath, sizeof(filepath), snappath, currenttime_tv, NULL, 0); snprintf(filename, PATH_MAX, "%.*s.%s" @@ -727,10 +743,11 @@ static void event_image_preview(struct context *cnt, * conf.picture_filename would normally be defined but if someone deleted it by * control interface it is better to revert to the default than fail. */ - if (cnt->conf.picture_filename) + if (cnt->conf.picture_filename) { imagepath = cnt->conf.picture_filename; - else + } else { imagepath = (char *)DEF_IMAGEPATH; + } mystrftime(cnt, filename, sizeof(filename), imagepath, &cnt->imgs.preview_image.timestamp_tv, NULL, 0); snprintf(previewname, PATH_MAX, "%.*s/%.*s.%s" @@ -759,8 +776,9 @@ static void event_camera_lost(struct context *cnt, struct image_data *img_data ATTRIBUTE_UNUSED, char *dummy1 ATTRIBUTE_UNUSED, void *dummy2 ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (cnt->conf.on_camera_lost) + if (cnt->conf.on_camera_lost) { exec_command(cnt, cnt->conf.on_camera_lost, NULL, 0); + } } static void event_camera_found(struct context *cnt, @@ -768,8 +786,9 @@ static void event_camera_found(struct context *cnt, struct image_data *img_data ATTRIBUTE_UNUSED, char *dummy1 ATTRIBUTE_UNUSED, void *dummy2 ATTRIBUTE_UNUSED, struct timeval *tv1 ATTRIBUTE_UNUSED) { - if (cnt->conf.on_camera_found) + if (cnt->conf.on_camera_found) { exec_command(cnt, cnt->conf.on_camera_found, NULL, 0); + } } static void on_movie_end_command(struct context *cnt, @@ -779,8 +798,9 @@ static void on_movie_end_command(struct context *cnt, { int filetype = (unsigned long) arg; - if ((filetype & FTYPE_MPEG_ANY) && cnt->conf.on_movie_end) + if ((filetype & FTYPE_MPEG_ANY) && cnt->conf.on_movie_end) { exec_command(cnt, cnt->conf.on_movie_end, filename, filetype); + } } static void event_extpipe_end(struct context *cnt, @@ -839,10 +859,10 @@ static void event_create_extpipe(struct context *cnt, } else if (errno == ENOENT) { MOTION_LOG(ERR, TYPE_EVENTS, SHOW_ERRNO ,_("path not found, trying to create it %s ..."), cnt->conf.target_dir); - if (create_path(cnt->extpipefilename) == -1) - return ; - } - else { + if (create_path(cnt->extpipefilename) == -1) { + return; + } + } else { MOTION_LOG(ERR, TYPE_EVENTS, SHOW_ERRNO ,_("error accesing path %s"), cnt->conf.target_dir); return ; @@ -850,13 +870,14 @@ static void event_create_extpipe(struct context *cnt, } /* Always create any path specified as file name */ - if (create_path(cnt->extpipefilename) == -1) - return ; + if (create_path(cnt->extpipefilename) == -1) { + return; + } mystrftime(cnt, stamp, sizeof(stamp), cnt->conf.movie_extpipe, currenttime_tv, cnt->extpipefilename, 0); retcd = snprintf(cnt->extpipecmdline, PATH_MAX, "%s", stamp); - if ((retcd < 0 ) || (retcd >= PATH_MAX)){ + if ((retcd < 0 ) || (retcd >= PATH_MAX)) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO , _("Error specifying command line: %s"), cnt->extpipecmdline); return; @@ -891,14 +912,16 @@ static void event_extpipe_put(struct context *cnt, passthrough = util_check_passthrough(cnt); /* Check that is open */ if ((cnt->extpipe_open) && (fileno(cnt->extpipe) > 0)) { - if ((cnt->imgs.size_high > 0) && (!passthrough)){ - if (!fwrite(img_data->image_high, cnt->imgs.size_high, 1, cnt->extpipe)) + if ((cnt->imgs.size_high > 0) && (!passthrough)) { + if (!fwrite(img_data->image_high, cnt->imgs.size_high, 1, cnt->extpipe)) { MOTION_LOG(ERR, TYPE_EVENTS, SHOW_ERRNO ,_("Error writing in pipe , state error %d"), ferror(cnt->extpipe)); + } } else { - if (!fwrite(img_data->image_norm, cnt->imgs.size_norm, 1, cnt->extpipe)) + if (!fwrite(img_data->image_norm, cnt->imgs.size_norm, 1, cnt->extpipe)) { MOTION_LOG(ERR, TYPE_EVENTS, SHOW_ERRNO ,_("Error writing in pipe , state error %d"), ferror(cnt->extpipe)); + } } } else { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO @@ -919,7 +942,9 @@ static void event_new_video(struct context *cnt, MOTION_LOG(INF, TYPE_EVENTS, NO_ERRNO, _("Source FPS %d"), cnt->movie_fps); - if (cnt->movie_fps < 2) cnt->movie_fps = 2; + if (cnt->movie_fps < 2) { + cnt->movie_fps = 2; + } } @@ -937,17 +962,19 @@ static void event_ffmpeg_newfile(struct context *cnt, long codenbr; int retcd; - if (!cnt->conf.movie_output && !cnt->conf.movie_output_motion) + if (!cnt->conf.movie_output && !cnt->conf.movie_output_motion) { return; + } /* * conf.mpegpath would normally be defined but if someone deleted it by control interface * it is better to revert to the default than fail */ - if (cnt->conf.movie_filename) + if (cnt->conf.movie_filename) { moviepath = cnt->conf.movie_filename; - else + } else { moviepath = DEF_MOVIEPATH; + } mystrftime(cnt, stamp, sizeof(stamp), moviepath, currenttime_tv, NULL, 0); @@ -1029,7 +1056,7 @@ static void event_ffmpeg_newfile(struct context *cnt, } if (cnt->conf.movie_output) { cnt->ffmpeg_output = mymalloc(sizeof(struct ffmpeg)); - if (cnt->imgs.size_high > 0){ + if (cnt->imgs.size_high > 0) { cnt->ffmpeg_output->width = cnt->imgs.width_high; cnt->ffmpeg_output->height = cnt->imgs.height_high; cnt->ffmpeg_output->high_resolution = TRUE; @@ -1061,7 +1088,7 @@ static void event_ffmpeg_newfile(struct context *cnt, retcd = ffmpeg_open(cnt->ffmpeg_output); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO ,_("Error opening context for movie output.")); free(cnt->ffmpeg_output); @@ -1098,7 +1125,7 @@ static void event_ffmpeg_newfile(struct context *cnt, cnt->ffmpeg_output_motion->rtsp_data = NULL; retcd = ffmpeg_open(cnt->ffmpeg_output_motion); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO ,_("ffopen_open error creating (motion) file [%s]"), cnt->motionfilename); free(cnt->ffmpeg_output_motion); @@ -1126,10 +1153,11 @@ static void event_ffmpeg_timelapse(struct context *cnt, * conf.timelapse_filename would normally be defined but if someone deleted it by control interface * it is better to revert to the default than fail */ - if (cnt->conf.timelapse_filename) + if (cnt->conf.timelapse_filename) { timepath = cnt->conf.timelapse_filename; - else + } else { timepath = DEF_TIMEPATH; + } mystrftime(cnt, tmp, sizeof(tmp), timepath, currenttime_tv, NULL, 0); @@ -1141,7 +1169,7 @@ static void event_ffmpeg_timelapse(struct context *cnt, , tmp); passthrough = util_check_passthrough(cnt); cnt->ffmpeg_timelapse = mymalloc(sizeof(struct ffmpeg)); - if ((cnt->imgs.size_high > 0) && (!passthrough)){ + if ((cnt->imgs.size_high > 0) && (!passthrough)) { cnt->ffmpeg_timelapse->width = cnt->imgs.width_high; cnt->ffmpeg_timelapse->height = cnt->imgs.height_high; cnt->ffmpeg_timelapse->high_resolution = TRUE; @@ -1165,8 +1193,7 @@ static void event_ffmpeg_timelapse(struct context *cnt, cnt->ffmpeg_timelapse->rtsp_data = NULL; if ((strcmp(cnt->conf.timelapse_codec,"mpg") == 0) || - (strcmp(cnt->conf.timelapse_codec,"swf") == 0) ){ - + (strcmp(cnt->conf.timelapse_codec,"swf") == 0)) { if (strcmp(cnt->conf.timelapse_codec,"swf") == 0) { MOTION_LOG(WRN, TYPE_EVENTS, NO_ERRNO ,_("The swf container for timelapse no longer supported. Using mpg container.")); @@ -1187,7 +1214,7 @@ static void event_ffmpeg_timelapse(struct context *cnt, retcd = ffmpeg_open(cnt->ffmpeg_timelapse); } - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO ,_("ffopen_open error creating (timelapse) file [%s]"), cnt->timelapsefilename); free(cnt->ffmpeg_timelapse); @@ -1209,7 +1236,7 @@ static void event_ffmpeg_put(struct context *cnt, void *dummy2 ATTRIBUTE_UNUSED, struct timeval *currenttime_tv) { if (cnt->ffmpeg_output) { - if (ffmpeg_put_image(cnt->ffmpeg_output, img_data, currenttime_tv) == -1){ + if (ffmpeg_put_image(cnt->ffmpeg_output, img_data, currenttime_tv) == -1) { MOTION_LOG(ERR, TYPE_EVENTS, NO_ERRNO, _("Error encoding image")); } } @@ -1423,7 +1450,8 @@ void event(struct context *cnt, motion_event type, struct image_data *img_data, int i=-1; while (event_handlers[++i].handler) { - if (type == event_handlers[i].type) + if (type == event_handlers[i].type) { event_handlers[i].handler(cnt, type, img_data, filename, eventdata, tv1); + } } } diff --git a/src/ffmpeg.c b/src/ffmpeg.c index 70e7df12..3dfd9668 100644 --- a/src/ffmpeg.c +++ b/src/ffmpeg.c @@ -168,7 +168,7 @@ int my_copy_packet(AVPacket *dest_pkt, AVPacket *src_pkt) * We therefore disable the pass through recording and * for this function, simply do not do anything */ - if (dest_pkt == src_pkt ){ + if (dest_pkt == src_pkt ) { return 0; } else { return 0; @@ -217,8 +217,7 @@ static int ffmpeg_timelapse_exists(const char *fname) { FILE *file; file = fopen(fname, "r"); - if (file) - { + if (file) { fclose(file); return 1; } @@ -230,7 +229,9 @@ static int ffmpeg_timelapse_append(struct ffmpeg *ffmpeg, AVPacket pkt) FILE *file; file = fopen(ffmpeg->filename, "a"); - if (!file) return -1; + if (!file) { + return -1; + } fwrite(pkt.data,1,pkt.size,file); @@ -251,8 +252,10 @@ static int ffmpeg_lockmgr_cb(void **arg, enum AVLockOp op) switch (op) { case AV_LOCK_CREATE: mutex = malloc(sizeof(*mutex)); - if (!mutex) + if (!mutex) { return AVERROR(ENOMEM); + } + if ((err = pthread_mutex_init(mutex, NULL))) { free(mutex); return AVERROR(err); @@ -260,18 +263,19 @@ static int ffmpeg_lockmgr_cb(void **arg, enum AVLockOp op) *arg = mutex; return 0; case AV_LOCK_OBTAIN: - if ((err = pthread_mutex_lock(mutex))) + if ((err = pthread_mutex_lock(mutex))) { return AVERROR(err); - + } return 0; case AV_LOCK_RELEASE: - if ((err = pthread_mutex_unlock(mutex))) + if ((err = pthread_mutex_unlock(mutex))) { return AVERROR(err); - + } return 0; case AV_LOCK_DESTROY: - if (mutex) + if (mutex) { pthread_mutex_destroy(mutex); + } free(mutex); *arg = NULL; return 0; @@ -283,17 +287,17 @@ static int ffmpeg_lockmgr_cb(void **arg, enum AVLockOp op) static void ffmpeg_free_context(struct ffmpeg *ffmpeg) { - if (ffmpeg->picture != NULL){ + if (ffmpeg->picture != NULL) { my_frame_free(ffmpeg->picture); ffmpeg->picture = NULL; } - if (ffmpeg->ctx_codec != NULL){ + if (ffmpeg->ctx_codec != NULL) { my_avcodec_close(ffmpeg->ctx_codec); ffmpeg->ctx_codec = NULL; } - if (ffmpeg->oc != NULL){ + if (ffmpeg->oc != NULL) { avformat_free_context(ffmpeg->oc); ffmpeg->oc = NULL; } @@ -324,7 +328,7 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg) /* Only the newer codec and containers can handle the really fast FPS */ if (((strcmp(codec_name, "msmpeg4") == 0) || (strcmp(codec_name, "mpeg4") == 0) || - (strcmp(codec_name, "swf") == 0) ) && (ffmpeg->fps >50)){ + (strcmp(codec_name, "swf") == 0) ) && (ffmpeg->fps >50)) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("The frame rate specified is too high for the ffmpeg movie type specified. " "Choose a different ffmpeg container or lower framerate.")); @@ -334,7 +338,7 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg) } retcd = snprintf(basename,PATH_MAX,"%s",ffmpeg->filename); - if ((retcd < 0) || (retcd >= PATH_MAX)){ + if ((retcd < 0) || (retcd >= PATH_MAX)) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error setting base file name")); ffmpeg_free_context(ffmpeg); @@ -342,12 +346,14 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg) return -1; } - if (ffmpeg->tlapse == TIMELAPSE_APPEND){ + if (ffmpeg->tlapse == TIMELAPSE_APPEND) { ffmpeg->oc->oformat = av_guess_format ("mpeg2video", NULL, NULL); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_MPEG2VIDEO; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_MPEG2VIDEO; + } + retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.mpg",basename); - if ((!ffmpeg->oc->oformat) || - (retcd < 0) || (retcd >= PATH_MAX)){ + if ((!ffmpeg->oc->oformat) || (retcd < 0) || (retcd >= PATH_MAX)) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error setting timelapse append for codec %s"), codec_name); ffmpeg_free_context(ffmpeg); @@ -366,7 +372,9 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg) if (strcmp(codec_name, "msmpeg4") == 0) { ffmpeg->oc->oformat = av_guess_format("avi", NULL, NULL); retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.avi",basename); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_MSMPEG4V2; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_MSMPEG4V2; + } } if (strcmp(codec_name, "swf") == 0) { @@ -377,13 +385,17 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg) if (strcmp(codec_name, "flv") == 0) { ffmpeg->oc->oformat = av_guess_format("flv", NULL, NULL); retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.flv",basename); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_FLV1; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_FLV1; + } } if (strcmp(codec_name, "ffv1") == 0) { ffmpeg->oc->oformat = av_guess_format("avi", NULL, NULL); retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.avi",basename); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_FFV1; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_FFV1; + } } if (strcmp(codec_name, "mov") == 0) { @@ -394,23 +406,29 @@ static int ffmpeg_get_oformat(struct ffmpeg *ffmpeg) if (strcmp(codec_name, "mp4") == 0) { ffmpeg->oc->oformat = av_guess_format("mp4", NULL, NULL); retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.mp4",basename); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_H264; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_H264; + } } if (strcmp(codec_name, "mkv") == 0) { ffmpeg->oc->oformat = av_guess_format("matroska", NULL, NULL); retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.mkv",basename); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_H264; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_H264; + } } if (strcmp(codec_name, "hevc") == 0) { ffmpeg->oc->oformat = av_guess_format("mp4", NULL, NULL); retcd = snprintf(ffmpeg->filename,PATH_MAX,"%s.mp4",basename); - if (ffmpeg->oc->oformat) ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_HEVC; + if (ffmpeg->oc->oformat) { + ffmpeg->oc->oformat->video_codec = MY_CODEC_ID_HEVC; + } } //Check for valid results - if ((retcd < 0) || (retcd >= PATH_MAX)){ + if ((retcd < 0) || (retcd >= PATH_MAX)) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error setting file name")); ffmpeg_free_context(ffmpeg); @@ -445,14 +463,14 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) char errstr[128]; retcd = avcodec_send_frame(ffmpeg->ctx_codec, ffmpeg->picture); - if (retcd < 0 ){ + if (retcd < 0 ) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error sending frame for encoding:%s"),errstr); return -1; } retcd = avcodec_receive_packet(ffmpeg->ctx_codec, &ffmpeg->pkt); - if (retcd == AVERROR(EAGAIN)){ + if (retcd == AVERROR(EAGAIN)) { //Buffered packet. Throw special return code av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(DBG, TYPE_ENCODER, NO_ERRNO @@ -460,7 +478,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) my_packet_unref(ffmpeg->pkt); return -2; } - if (retcd < 0 ){ + if (retcd < 0 ) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error receiving encoded packet video:%s"),errstr); @@ -468,7 +486,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) return -1; } - if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M){ + if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M) { if (ffmpeg_encode_nal(ffmpeg)) { // Throw special return code return -2; @@ -483,20 +501,20 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) int got_packet_ptr; retcd = avcodec_encode_video2(ffmpeg->ctx_codec, &ffmpeg->pkt, ffmpeg->picture, &got_packet_ptr); - if (retcd < 0 ){ + if (retcd < 0 ) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Error encoding video:%s"),errstr); //Packet is freed upon failure of encoding return -1; } - if (got_packet_ptr == 0){ + if (got_packet_ptr == 0) { //Buffered packet. Throw special return code my_packet_unref(ffmpeg->pkt); return -2; } /* This kills compiler warnings. Nal setting is only for recent ffmpeg versions*/ - if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M){ + if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M) { if (ffmpeg_encode_nal(ffmpeg)) { // Throw special return code return -2; @@ -515,12 +533,12 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) video_outbuf = mymalloc(video_outbuf_size); retcd = avcodec_encode_video(ffmpeg->video_st->codec, video_outbuf, video_outbuf_size, ffmpeg->picture); - if (retcd < 0 ){ + if (retcd < 0 ) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Error encoding video")); my_packet_unref(ffmpeg->pkt); return -1; } - if (retcd == 0 ){ + if (retcd == 0 ) { // No bytes encoded => buffered=>special handling my_packet_unref(ffmpeg->pkt); return -2; @@ -530,8 +548,9 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) ffmpeg->pkt.size = retcd; ffmpeg->pkt.data = video_outbuf; - if (ffmpeg->picture->key_frame == 1) - ffmpeg->pkt.flags |= AV_PKT_FLAG_KEY; + if (ffmpeg->picture->key_frame == 1) { + ffmpeg->pkt.flags |= AV_PKT_FLAG_KEY; + } ffmpeg->pkt.pts = ffmpeg->picture->pts; ffmpeg->pkt.dts = ffmpeg->pkt.pts; @@ -539,7 +558,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg) free(video_outbuf); /* This kills compiler warnings. Nal setting is only for recent ffmpeg versions*/ - if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M){ + if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M) { if (ffmpeg_encode_nal(ffmpeg)) { // Throw special return code return -2; @@ -561,7 +580,7 @@ static int ffmpeg_set_pts(struct ffmpeg *ffmpeg, const struct timeval *tv1) ffmpeg->picture->pts = ffmpeg->last_pts; } else { pts_interval = ((1000000L * (tv1->tv_sec - ffmpeg->start_time.tv_sec)) + tv1->tv_usec - ffmpeg->start_time.tv_usec); - if (pts_interval < 0){ + if (pts_interval < 0) { /* This can occur when we have pre-capture frames. Reset start time of video. */ ffmpeg_reset_movie_start_time(ffmpeg, tv1); pts_interval = 0; @@ -572,16 +591,16 @@ static int ffmpeg_set_pts(struct ffmpeg *ffmpeg, const struct timeval *tv1) } else ffmpeg->picture->pts = av_rescale_q(pts_interval,(AVRational){1, 1000000L},ffmpeg->video_st->time_base) + ffmpeg->base_pts; - if (ffmpeg->test_mode == TRUE){ + if (ffmpeg->test_mode == TRUE) { MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO ,_("PTS %"PRId64" Base PTS %"PRId64" ms interval %"PRId64" timebase %d-%d") ,ffmpeg->picture->pts,ffmpeg->base_pts,pts_interval ,ffmpeg->video_st->time_base.num,ffmpeg->video_st->time_base.den); } - if (ffmpeg->picture->pts <= ffmpeg->last_pts){ + if (ffmpeg->picture->pts <= ffmpeg->last_pts) { //We have a problem with our motion loop timing and sending frames or the rounding into the PTS. - if (ffmpeg->test_mode == TRUE){ + if (ffmpeg->test_mode == TRUE) { MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO, _("BAD TIMING!! Frame skipped.")); } return -1; @@ -601,14 +620,14 @@ static int ffmpeg_set_pktpts(struct ffmpeg *ffmpeg, const struct timeval *tv1) ffmpeg->pkt.pts = ffmpeg->last_pts; } else { pts_interval = ((1000000L * (tv1->tv_sec - ffmpeg->start_time.tv_sec)) + tv1->tv_usec - ffmpeg->start_time.tv_usec); - if (pts_interval < 0){ + if (pts_interval < 0) { /* This can occur when we have pre-capture frames. Reset start time of video. */ ffmpeg_reset_movie_start_time(ffmpeg, tv1); pts_interval = 0; } ffmpeg->pkt.pts = av_rescale_q(pts_interval,(AVRational){1, 1000000L},ffmpeg->video_st->time_base) + ffmpeg->base_pts; - if (ffmpeg->test_mode == TRUE){ + if (ffmpeg->test_mode == TRUE) { MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO ,_("PTS %"PRId64" Base PTS %"PRId64" ms interval %"PRId64" timebase %d-%d Change %d") ,ffmpeg->pkt.pts @@ -618,9 +637,9 @@ static int ffmpeg_set_pktpts(struct ffmpeg *ffmpeg, const struct timeval *tv1) ,(ffmpeg->pkt.pts-ffmpeg->last_pts) ); } - if (ffmpeg->pkt.pts <= ffmpeg->last_pts){ + if (ffmpeg->pkt.pts <= ffmpeg->last_pts) { //We have a problem with our motion loop timing and sending frames or the rounding into the PTS. - if (ffmpeg->test_mode == TRUE){ + if (ffmpeg->test_mode == TRUE) { MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO, _("BAD TIMING!! Frame skipped.")); } return -1; @@ -635,11 +654,14 @@ static int ffmpeg_set_quality(struct ffmpeg *ffmpeg) { ffmpeg->opts = 0; - if (ffmpeg->quality > 100) ffmpeg->quality = 100; + if (ffmpeg->quality > 100) { + ffmpeg->quality = 100; + } if (ffmpeg->ctx_codec->codec_id == MY_CODEC_ID_H264 || - ffmpeg->ctx_codec->codec_id == MY_CODEC_ID_HEVC){ - if (ffmpeg->quality <= 0) + ffmpeg->ctx_codec->codec_id == MY_CODEC_ID_HEVC) { + if (ffmpeg->quality <= 0) { ffmpeg->quality = 45; // default to 45% quality + } av_dict_set(&ffmpeg->opts, "preset", "ultrafast", 0); av_dict_set(&ffmpeg->opts, "tune", "zerolatency", 0); /* This next if statement needs validation. Are mpeg4omx @@ -652,8 +674,10 @@ static int ffmpeg_set_quality(struct ffmpeg *ffmpeg) // bit_rate = ffmpeg->width * ffmpeg->height * ffmpeg->fps * quality_factor ffmpeg->quality = (int)(((int64_t)ffmpeg->width * ffmpeg->height * ffmpeg->fps * ffmpeg->quality) >> 7); // Clip bit rate to min - if (ffmpeg->quality < 4000) // magic number + if (ffmpeg->quality < 4000) { + // magic number ffmpeg->quality = 4000; + } ffmpeg->ctx_codec->profile = FF_PROFILE_H264_HIGH; ffmpeg->ctx_codec->bit_rate = ffmpeg->quality; } else { @@ -665,7 +689,7 @@ static int ffmpeg_set_quality(struct ffmpeg *ffmpeg) } } else { /* The selection of 8000 is a subjective number based upon viewing output files */ - if (ffmpeg->quality > 0){ + if (ffmpeg->quality > 0) { ffmpeg->quality =(int)(((100-ffmpeg->quality)*(100-ffmpeg->quality)*(100-ffmpeg->quality) * 8000) / 1000000) + 1; ffmpeg->ctx_codec->flags |= MY_CODEC_FLAG_QSCALE; ffmpeg->ctx_codec->global_quality=ffmpeg->quality; @@ -708,8 +732,9 @@ static const char *ffmpeg_codec_is_blacklisted(const char *codec_name) i_mx = (size_t)(sizeof(blacklisted_codec)/sizeof(blacklisted_codec[0])); for (i = 0; i < i_mx; i++) { - if (strcmp(codec_name, blacklisted_codec[i].codec_name) == 0) + if (strcmp(codec_name, blacklisted_codec[i].codec_name) == 0) { return blacklisted_codec[i].reason; + } } return NULL; } @@ -736,8 +761,9 @@ static int ffmpeg_set_codec_preferred(struct ffmpeg *ffmpeg) } } } - if (!ffmpeg->codec) + if (!ffmpeg->codec) { ffmpeg->codec = avcodec_find_encoder(ffmpeg->oc->oformat->video_codec); + } if (!ffmpeg->codec) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Codec %s not found"), ffmpeg->codec_name); @@ -745,18 +771,19 @@ static int ffmpeg_set_codec_preferred(struct ffmpeg *ffmpeg) return -1; } - if (strcmp(ffmpeg->codec->name, "h264_v4l2m2m") == 0){ + if (strcmp(ffmpeg->codec->name, "h264_v4l2m2m") == 0) { ffmpeg->preferred_codec = USER_CODEC_V4L2M2M; - } else if (strcmp(ffmpeg->codec->name, "h264_omx") == 0){ + } else if (strcmp(ffmpeg->codec->name, "h264_omx") == 0) { ffmpeg->preferred_codec = USER_CODEC_H264OMX; - } else if (strcmp(ffmpeg->codec->name, "mpeg4_omx") == 0){ + } else if (strcmp(ffmpeg->codec->name, "mpeg4_omx") == 0) { ffmpeg->preferred_codec = USER_CODEC_MPEG4OMX; } else { ffmpeg->preferred_codec = USER_CODEC_DEFAULT; } - if (ffmpeg->codec_name[codec_name_len]) + if (ffmpeg->codec_name[codec_name_len]) { MOTION_LOG(NTC, TYPE_ENCODER, NO_ERRNO,_("Using codec %s"), ffmpeg->codec->name); + } return 0; @@ -770,7 +797,9 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg) int chkrate; retcd = ffmpeg_set_codec_preferred(ffmpeg); - if (retcd != 0) return retcd; + if (retcd != 0) { + return retcd; + } #if ( MYFFVER >= 57041) //If we provide the codec to this, it results in a memory leak. ffmpeg ticket: 5714 @@ -800,9 +829,9 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg) if (ffmpeg->tlapse != TIMELAPSE_NONE) { ffmpeg->ctx_codec->gop_size = 1; } else { - if (ffmpeg->fps <= 5){ + if (ffmpeg->fps <= 5) { ffmpeg->ctx_codec->gop_size = 1; - } else if (ffmpeg->fps > 30){ + } else if (ffmpeg->fps > 30) { ffmpeg->ctx_codec->gop_size = 15; } else { ffmpeg->ctx_codec->gop_size = (ffmpeg->fps / 2); @@ -814,7 +843,7 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg) ** a very poor quality playback. We can set the FPS to a higher number and ** then let the PTS display the frames correctly. */ - if ((ffmpeg->tlapse == TIMELAPSE_NONE) && (ffmpeg->fps <= 5)){ + if ((ffmpeg->tlapse == TIMELAPSE_NONE) && (ffmpeg->fps <= 5)) { if ((strcmp(ffmpeg->codec_name, "msmpeg4") == 0) || (strcmp(ffmpeg->codec_name, "flv") == 0) || (strcmp(ffmpeg->codec_name, "mov") == 0) || @@ -833,13 +862,13 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg) ffmpeg->ctx_codec->height = ffmpeg->height; ffmpeg->ctx_codec->time_base.num = 1; ffmpeg->ctx_codec->time_base.den = ffmpeg->fps; - if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M){ + if (ffmpeg->preferred_codec == USER_CODEC_V4L2M2M) { ffmpeg->ctx_codec->pix_fmt = AV_PIX_FMT_NV21; } else { ffmpeg->ctx_codec->pix_fmt = MY_PIX_FMT_YUV420P; } ffmpeg->ctx_codec->max_b_frames = 0; - if (strcmp(ffmpeg->codec_name, "ffv1") == 0){ + if (strcmp(ffmpeg->codec_name, "ffv1") == 0) { ffmpeg->ctx_codec->strict_std_compliance = -2; ffmpeg->ctx_codec->level = 3; } @@ -855,7 +884,7 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg) } retcd = ffmpeg_set_quality(ffmpeg); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Unable to set quality")); return -1; } @@ -876,7 +905,7 @@ static int ffmpeg_set_codec(struct ffmpeg *ffmpeg) retcd = avcodec_open2(ffmpeg->ctx_codec, ffmpeg->codec, &ffmpeg->opts); chkrate++; } - if (retcd < 0){ + if (retcd < 0) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Could not open codec %s"),errstr); av_dict_free(&ffmpeg->opts); @@ -920,33 +949,40 @@ static int ffmpeg_alloc_video_buffer(AVFrame *frame, int align) int ret, i, padded_height; int plane_padding = FFMAX(16 + 16/*STRIDE_ALIGN*/, align); - if (!desc) + if (!desc) { return AVERROR(EINVAL); + } - if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0) + if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0) { return ret; + } if (!frame->linesize[0]) { - if (align <= 0) + if (align <= 0) { align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */ + } for(i=1; i<=align; i+=i) { ret = av_image_fill_linesizes(frame->linesize, frame->format, FFALIGN(frame->width, i)); - if (ret < 0) + if (ret < 0) { return ret; - if (!(frame->linesize[0] & (align-1))) + } + if (!(frame->linesize[0] & (align-1))) { break; + } } - for (i = 0; i < 4 && frame->linesize[i]; i++) + for (i = 0; i < 4 && frame->linesize[i]; i++) { frame->linesize[i] = FFALIGN(frame->linesize[i], align); + } } padded_height = FFALIGN(frame->height, 32); if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height, - NULL, frame->linesize)) < 0) + NULL, frame->linesize)) < 0) { return ret; + } frame->buf[0] = av_buffer_alloc(ret + 4*plane_padding); if (!frame->buf[0]) { @@ -984,8 +1020,9 @@ static int ffmpeg_set_picture(struct ffmpeg *ffmpeg) } /* Take care of variable bitrate setting. */ - if (ffmpeg->quality) + if (ffmpeg->quality) { ffmpeg->picture->quality = ffmpeg->quality; + } ffmpeg->picture->linesize[0] = ffmpeg->ctx_codec->width; ffmpeg->picture->linesize[1] = ffmpeg->ctx_codec->width / 2; @@ -1054,7 +1091,7 @@ static int ffmpeg_set_outputfile(struct ffmpeg *ffmpeg) * items here */ retcd = avformat_write_header(ffmpeg->oc, NULL); - if (retcd < 0){ + if (retcd < 0) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Could not write ffmpeg header %s"),errstr); @@ -1082,7 +1119,7 @@ static int ffmpeg_flush_codec(struct ffmpeg *ffmpeg) int recv_cd = 0; char errstr[128]; - if (ffmpeg->passthrough){ + if (ffmpeg->passthrough) { return 0; } @@ -1090,19 +1127,19 @@ static int ffmpeg_flush_codec(struct ffmpeg *ffmpeg) recv_cd = 0; if (ffmpeg->tlapse == TIMELAPSE_NONE) { retcd = avcodec_send_frame(ffmpeg->ctx_codec, NULL); - if (retcd < 0 ){ + if (retcd < 0 ) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error entering draining mode:%s"),errstr); return -1; } - while (recv_cd != AVERROR_EOF){ + while (recv_cd != AVERROR_EOF) { av_init_packet(&ffmpeg->pkt); ffmpeg->pkt.data = NULL; ffmpeg->pkt.size = 0; recv_cd = avcodec_receive_packet(ffmpeg->ctx_codec, &ffmpeg->pkt); - if (recv_cd != AVERROR_EOF){ - if (recv_cd < 0){ + if (recv_cd != AVERROR_EOF) { + if (recv_cd < 0) { av_strerror(recv_cd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Error draining codec:%s"),errstr); @@ -1130,7 +1167,7 @@ static int ffmpeg_flush_codec(struct ffmpeg *ffmpeg) /* Dummy to kill warnings. No draining in older ffmpeg versions */ if (ffmpeg) { return 0; - } else{ + } else { return 0; } #endif @@ -1153,8 +1190,8 @@ static int ffmpeg_put_frame(struct ffmpeg *ffmpeg, const struct timeval *tv1) } retcd = ffmpeg_encode_video(ffmpeg); - if (retcd != 0){ - if (retcd != -2){ + if (retcd != 0) { + if (retcd != -2) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Error while encoding picture")); } my_packet_unref(ffmpeg->pkt); @@ -1235,14 +1272,16 @@ static int ffmpeg_passthru_put(struct ffmpeg *ffmpeg, struct image_data *img_dat int idnbr_image, idnbr_lastwritten, idnbr_stop, idnbr_firstkey; int indx, indx_lastwritten, indx_firstkey; - if (ffmpeg->rtsp_data == NULL) return -1; + if (ffmpeg->rtsp_data == NULL) { + return -1; + } if ((ffmpeg->rtsp_data->status == RTSP_NOTCONNECTED ) || - (ffmpeg->rtsp_data->status == RTSP_RECONNECTING ) ){ + (ffmpeg->rtsp_data->status == RTSP_RECONNECTING )) { return 0; } - if (ffmpeg->high_resolution){ + if (ffmpeg->high_resolution) { idnbr_image = img_data->idnbr_high; } else { idnbr_image = img_data->idnbr_norm; @@ -1257,27 +1296,27 @@ static int ffmpeg_passthru_put(struct ffmpeg *ffmpeg, struct image_data *img_dat for(indx = 0; indx < ffmpeg->rtsp_data->pktarray_size; indx++) { if ((ffmpeg->rtsp_data->pktarray[indx].iswritten) && - (ffmpeg->rtsp_data->pktarray[indx].idnbr > idnbr_lastwritten)){ + (ffmpeg->rtsp_data->pktarray[indx].idnbr > idnbr_lastwritten)) { idnbr_lastwritten=ffmpeg->rtsp_data->pktarray[indx].idnbr; indx_lastwritten = indx; } if ((ffmpeg->rtsp_data->pktarray[indx].idnbr > idnbr_stop) && - (ffmpeg->rtsp_data->pktarray[indx].idnbr <= idnbr_image)){ + (ffmpeg->rtsp_data->pktarray[indx].idnbr <= idnbr_image)) { idnbr_stop=ffmpeg->rtsp_data->pktarray[indx].idnbr; } if ((ffmpeg->rtsp_data->pktarray[indx].iskey) && - (ffmpeg->rtsp_data->pktarray[indx].idnbr <= idnbr_firstkey)){ + (ffmpeg->rtsp_data->pktarray[indx].idnbr <= idnbr_firstkey)) { idnbr_firstkey=ffmpeg->rtsp_data->pktarray[indx].idnbr; indx_firstkey = indx; } } - if (idnbr_stop == 0){ + if (idnbr_stop == 0) { pthread_mutex_unlock(&ffmpeg->rtsp_data->mutex_pktarray); return 0; } - if (indx_lastwritten != -1){ + if (indx_lastwritten != -1) { indx = indx_lastwritten; } else if (indx_firstkey != -1) { indx = indx_firstkey; @@ -1285,16 +1324,20 @@ static int ffmpeg_passthru_put(struct ffmpeg *ffmpeg, struct image_data *img_dat indx = 0; } - while (TRUE){ + while (TRUE) { if ((!ffmpeg->rtsp_data->pktarray[indx].iswritten) && (ffmpeg->rtsp_data->pktarray[indx].packet.size > 0) && (ffmpeg->rtsp_data->pktarray[indx].idnbr > idnbr_lastwritten) && (ffmpeg->rtsp_data->pktarray[indx].idnbr <= idnbr_image)) { ffmpeg_passthru_write(ffmpeg, indx); } - if (ffmpeg->rtsp_data->pktarray[indx].idnbr == idnbr_stop) break; + if (ffmpeg->rtsp_data->pktarray[indx].idnbr == idnbr_stop) { + break; + } indx++; - if (indx == ffmpeg->rtsp_data->pktarray_size ) indx = 0; + if (indx == ffmpeg->rtsp_data->pktarray_size) { + indx = 0; + } } pthread_mutex_unlock(&ffmpeg->rtsp_data->mutex_pktarray); return 0; @@ -1306,7 +1349,7 @@ static int ffmpeg_passthru_codec(struct ffmpeg *ffmpeg) int retcd; AVStream *stream_in; - if (ffmpeg->rtsp_data == NULL){ + if (ffmpeg->rtsp_data == NULL) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("RTSP context not available.")); return -1; } @@ -1314,14 +1357,14 @@ static int ffmpeg_passthru_codec(struct ffmpeg *ffmpeg) pthread_mutex_lock(&ffmpeg->rtsp_data->mutex_transfer); if ((ffmpeg->rtsp_data->status == RTSP_NOTCONNECTED ) || - (ffmpeg->rtsp_data->status == RTSP_RECONNECTING ) ){ + (ffmpeg->rtsp_data->status == RTSP_RECONNECTING )) { MOTION_LOG(NTC, TYPE_ENCODER, NO_ERRNO ,_("rtsp camera not ready for pass-through.")); pthread_mutex_unlock(&ffmpeg->rtsp_data->mutex_transfer); return -1; } - if (strcmp(ffmpeg->codec_name, "mp4") != 0){ + if (strcmp(ffmpeg->codec_name, "mp4") != 0) { MOTION_LOG(NTC, TYPE_ENCODER, NO_ERRNO ,_("pass-through mode enabled. Changing to MP4 container.")); ffmpeg->codec_name = "mp4"; @@ -1346,7 +1389,7 @@ static int ffmpeg_passthru_codec(struct ffmpeg *ffmpeg) } retcd = avcodec_parameters_copy(ffmpeg->video_st->codecpar, stream_in->codecpar); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Unable to copy codec parameters")); pthread_mutex_unlock(&ffmpeg->rtsp_data->mutex_transfer); return -1; @@ -1364,7 +1407,7 @@ static int ffmpeg_passthru_codec(struct ffmpeg *ffmpeg) } retcd = avcodec_copy_context(ffmpeg->video_st->codec, stream_in->codec); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Unable to copy codec parameters")); pthread_mutex_unlock(&ffmpeg->rtsp_data->mutex_transfer); return -1; @@ -1397,12 +1440,11 @@ void ffmpeg_avcodec_log(void *ignoreme ATTRIBUTE_UNUSED, int errno_flag ATTRIBUT * the log level. Now we put the avcodec messages to INF level since their error * are not necessarily our errors. */ - if (errno_flag <= AV_LOG_WARNING){ + if (errno_flag <= AV_LOG_WARNING) { /* Flatten the message coming in from avcodec. */ vsnprintf(buf, sizeof(buf), fmt, vl); end = buf + strlen(buf); - if (end > buf && end[-1] == '\n') - { + if (end > buf && end[-1] == '\n') { *--end = 0; } @@ -1415,7 +1457,7 @@ static void ffmpeg_put_pix_nv21(struct ffmpeg *ffmpeg, struct image_data *img_da unsigned char *image,*imagecr, *imagecb; int cr_len, x, y; - if (ffmpeg->high_resolution){ + if (ffmpeg->high_resolution) { image = img_data->image_high; } else { image = img_data->image_norm; @@ -1441,7 +1483,7 @@ static void ffmpeg_put_pix_yuv420(struct ffmpeg *ffmpeg, struct image_data *img_ { unsigned char *image; - if (ffmpeg->high_resolution){ + if (ffmpeg->high_resolution) { image = img_data->image_high; } else { image = img_data->image_norm; @@ -1484,8 +1526,7 @@ void ffmpeg_global_init(void) /* TODO: Determine if this is even needed for older versions */ int ret; ret = av_lockmgr_register(ffmpeg_lockmgr_cb); - if (ret < 0) - { + if (ret < 0) { MOTION_LOG(EMG, TYPE_ALL, SHOW_ERRNO, _("av_lockmgr_register failed (%d)"), ret); exit(1); } @@ -1501,8 +1542,7 @@ void ffmpeg_global_deinit(void) avformat_network_deinit(); #if ( MYFFVER < 58000) /* TODO Determine if this is even needed for old versions */ - if (av_lockmgr_register(NULL) < 0) - { + if (av_lockmgr_register(NULL) < 0) { MOTION_LOG(EMG, TYPE_ALL, SHOW_ERRNO ,_("av_lockmgr_register reset failed on cleanup")); } @@ -1550,20 +1590,20 @@ int ffmpeg_open(struct ffmpeg *ffmpeg) } retcd = ffmpeg_set_stream(ffmpeg); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Could not set the stream")); return -1; } retcd = ffmpeg_set_picture(ffmpeg); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Could not set the stream")); return -1; } } retcd = ffmpeg_set_outputfile(ffmpeg); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Could not set the stream")); return -1; } @@ -1585,10 +1625,10 @@ void ffmpeg_close(struct ffmpeg *ffmpeg) if (ffmpeg != NULL) { - if (ffmpeg_flush_codec(ffmpeg) < 0){ + if (ffmpeg_flush_codec(ffmpeg) < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Error flushing codec")); } - if (ffmpeg->oc->pb != NULL){ + if (ffmpeg->oc->pb != NULL) { if (ffmpeg->tlapse != TIMELAPSE_APPEND) { av_write_trailer(ffmpeg->oc); } @@ -1603,7 +1643,9 @@ void ffmpeg_close(struct ffmpeg *ffmpeg) } #else - if (ffmpeg != NULL) free(ffmpeg); + if (ffmpeg != NULL) { + free(ffmpeg); + } #endif // HAVE_FFMPEG } @@ -1627,7 +1669,7 @@ int ffmpeg_put_image(struct ffmpeg *ffmpeg, struct image_data *img_data, const s } ffmpeg->gop_cnt ++; - if (ffmpeg->gop_cnt == ffmpeg->ctx_codec->gop_size ){ + if (ffmpeg->gop_cnt == ffmpeg->ctx_codec->gop_size ) { ffmpeg->picture->pict_type = AV_PICTURE_TYPE_I; ffmpeg->picture->key_frame = 1; ffmpeg->gop_cnt = 0; @@ -1645,14 +1687,14 @@ int ffmpeg_put_image(struct ffmpeg *ffmpeg, struct image_data *img_data, const s while ((retcd == -2) && (ffmpeg->tlapse != TIMELAPSE_NONE)) { retcd = ffmpeg_put_frame(ffmpeg, tv1); cnt++; - if (cnt > 50){ + if (cnt > 50) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO ,_("Excessive attempts to clear buffered packet")); retcd = -1; } } //non timelapse buffered is ok - if (retcd == -2){ + if (retcd == -2) { retcd = 0; MOTION_LOG(DBG, TYPE_ENCODER, NO_ERRNO, _("Buffered packet")); } @@ -1672,8 +1714,9 @@ void ffmpeg_reset_movie_start_time(struct ffmpeg *ffmpeg, const struct timeval * { #ifdef HAVE_FFMPEG int64_t one_frame_interval = av_rescale_q(1,(AVRational){1, ffmpeg->fps},ffmpeg->video_st->time_base); - if (one_frame_interval <= 0) + if (one_frame_interval <= 0) { one_frame_interval = 1; + } ffmpeg->base_pts = ffmpeg->last_pts + one_frame_interval; ffmpeg->start_time.tv_sec = tv1->tv_sec; diff --git a/src/jpegutils.c b/src/jpegutils.c index 14fd1b65..8d6464f6 100644 --- a/src/jpegutils.c +++ b/src/jpegutils.c @@ -101,8 +101,9 @@ static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr, const UI /* Define a Huffman table */ int nsymbols, len; - if (*htblptr == NULL) + if (*htblptr == NULL) { *htblptr = jpeg_alloc_huff_table((j_common_ptr) dinfo); + } /* Copy the number-of-symbols-of-each-code-length counts. */ memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits)); @@ -114,11 +115,13 @@ static void add_huff_table(j_decompress_ptr dinfo, JHUFF_TBL **htblptr, const UI */ nsymbols = 0; - for (len = 1; len <= 16; len++) + for (len = 1; len <= 16; len++) { nsymbols += bits[len]; + } - if (nsymbols < 1 || nsymbols > 256) + if (nsymbols < 1 || nsymbols > 256) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, _("%s: Given jpeg buffer was too small")); + } memcpy((*htblptr)->huffval, val, nsymbols * sizeof(UINT8)); } @@ -240,8 +243,9 @@ static boolean jpgutl_fill_input_buffer(j_decompress_ptr cinfo) static void jpgutl_skip_data(j_decompress_ptr cinfo, long num_bytes) { if (num_bytes > 0) { - if (num_bytes > (long) cinfo->src->bytes_in_buffer) + if (num_bytes > (long) cinfo->src->bytes_in_buffer) { num_bytes = (long) cinfo->src->bytes_in_buffer; + } cinfo->src->next_input_byte += (size_t) num_bytes; cinfo->src->bytes_in_buffer -= (size_t) num_bytes; } @@ -542,7 +546,9 @@ int jpgutl_decode_jpeg (unsigned char *jpeg_data_in, int jpeg_data_len, * only a partial image could be returned which would * trigger many false positive motion detections */ - if (jerr.warning_seen > 2) return -1; + if (jerr.warning_seen > 2) { + return -1; + } return 0; diff --git a/src/logger.c b/src/logger.c index d5431852..076dd5f7 100644 --- a/src/logger.c +++ b/src/logger.c @@ -102,11 +102,13 @@ void set_log_mode(int mode) log_mode = mode; //printf("set log mode %d\n", mode); - if (mode == LOGMODE_SYSLOG && prev_mode != LOGMODE_SYSLOG) + if (mode == LOGMODE_SYSLOG && prev_mode != LOGMODE_SYSLOG) { openlog("motion", LOG_PID, LOG_USER); + } - if (mode != LOGMODE_SYSLOG && prev_mode == LOGMODE_SYSLOG) + if (mode != LOGMODE_SYSLOG && prev_mode == LOGMODE_SYSLOG) { closelog(); + } } /** @@ -123,8 +125,9 @@ FILE * set_logfile(const char *logfile_name) logfile = myfopen(logfile_name, "a"); /* If logfile was opened correctly */ - if (logfile) + if (logfile) { set_log_mode(LOGMODE_FILE); + } return logfile; } @@ -186,12 +189,14 @@ void motion_log(int level, unsigned int type, int errno_flag,int fncname, const /* Exit if level is greater than log_level */ - if ((unsigned int)level > log_level) + if ((unsigned int)level > log_level) { return; + } /* Exit if type is not equal to log_type and not TYPE_ALL */ - if ((log_type != TYPE_ALL) && (type != log_type)) + if ((log_type != TYPE_ALL) && (type != log_type)) { return; + } //printf("log_type %d, type %d level %d\n", log_type, type, level); @@ -226,7 +231,7 @@ void motion_log(int level, unsigned int type, int errno_flag,int fncname, const } /* Prepend the format specifier for the function name */ - if (fncname){ + if (fncname) { snprintf(usrfmt, sizeof (usrfmt),"%s: %s", "%s", fmt); } else { snprintf(usrfmt, sizeof (usrfmt),"%s",fmt); @@ -264,10 +269,10 @@ void motion_log(int level, unsigned int type, int errno_flag,int fncname, const #endif } - if ((!strcmp(buf,flood_msg)) && (flood_cnt <= 5000)){ + if ((!strcmp(buf,flood_msg)) && (flood_cnt <= 5000)) { flood_cnt++; } else { - if (flood_cnt > 1){ + if (flood_cnt > 1) { snprintf(flood_repeats,1024,"[%d:%s] [%s] [%s] Above message repeats %d times", threadnr, threadname, get_log_level_str(level) , get_log_type_str(type), flood_cnt-1); diff --git a/src/mmalcam.c b/src/mmalcam.c index 2f482fc1..33b7c7c0 100644 --- a/src/mmalcam.c +++ b/src/mmalcam.c @@ -96,7 +96,7 @@ static void set_video_port_format(mmalcam_context_ptr mmalcam, MMAL_ES_FORMAT_T set_port_format(mmalcam, format); format->es->video.frame_rate.num = mmalcam->framerate; format->es->video.frame_rate.den = VIDEO_FRAME_RATE_DEN; - if (mmalcam->framerate > 30){ + if (mmalcam->framerate > 30) { /* The pi noir camera could not determine autoexpose at high frame rates */ MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO, _("A high frame rate can cause problems with exposure of images")); MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO, _("If autoexposure is not working, try a lower frame rate.")); @@ -386,8 +386,9 @@ int mmalcam_next(struct context *cnt, struct image_data *img_data) { mmalcam_context_ptr mmalcam; - if ((!cnt) || (!cnt->mmalcam)) + if ((!cnt) || (!cnt->mmalcam)) { return NETCAM_FATAL_ERROR; + } mmalcam = cnt->mmalcam; @@ -415,9 +416,10 @@ int mmalcam_next(struct context *cnt, struct image_data *img_data) status = mmal_port_send_buffer(mmalcam->camera_capture_port, new_buffer); } - if (!new_buffer || status != MMAL_SUCCESS) + if (!new_buffer || status != MMAL_SUCCESS) { MOTION_LOG(ERR, TYPE_VIDEO, NO_ERRNO ,_("Unable to return a buffer to the camera video port")); + } } rotate_map(cnt,img_data); diff --git a/src/motion.c b/src/motion.c index ff709169..f558f672 100644 --- a/src/motion.c +++ b/src/motion.c @@ -95,10 +95,11 @@ static void image_ring_resize(struct context *cnt, int new_size) if (cnt->event_nr != cnt->prev_event) { int smallest; - if (new_size < cnt->imgs.image_ring_size) /* Decreasing */ + if (new_size < cnt->imgs.image_ring_size) { /* Decreasing */ smallest = new_size; - else /* Increasing */ + } 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 @@ -112,9 +113,9 @@ static void image_ring_resize(struct context *cnt, int new_size) * Copy all information from old to new * Smallest is 0 at initial init */ - if (smallest > 0) + if (smallest > 0) { memcpy(tmp, cnt->imgs.image_ring, sizeof(struct image_data) * smallest); - + } /* In the new buffers, allocate image memory */ { @@ -122,7 +123,7 @@ static void image_ring_resize(struct context *cnt, int new_size) 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){ + 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); } @@ -160,13 +161,16 @@ static void image_ring_destroy(struct context *cnt) int i; /* Exit if don't have any ring */ - if (cnt->imgs.image_ring == NULL) + if (cnt->imgs.image_ring == NULL) { return; + } /* Free all image buffers */ - for (i = 0; i < cnt->imgs.image_ring_size; i++){ + 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); + if (cnt->imgs.size_high >0 ) { + free(cnt->imgs.image_ring[i].image_high); + } } /* Free the ring */ @@ -206,7 +210,7 @@ static void image_save_as_preview(struct context *cnt, struct image_data *img) /* 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){ + if (cnt->imgs.size_high > 0) { memcpy(cnt->imgs.preview_image.image_high, img->image_high, cnt->imgs.size_high); } @@ -214,8 +218,9 @@ static void image_save_as_preview(struct context *cnt, struct image_data *img) * 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) + 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) { @@ -329,8 +334,9 @@ static void sig_handler(int signo) if (cnt_list) { i = -1; while (cnt_list[++i]) { - if (cnt_list[i]->conf.snapshot_interval) + if (cnt_list[i]->conf.snapshot_interval) { cnt_list[i]->snapshot = 1; + } } } @@ -339,7 +345,7 @@ static void sig_handler(int signo) /* Trigger the end of a event */ if (cnt_list) { i = -1; - while (cnt_list[++i]){ + while (cnt_list[++i]) { cnt_list[i]->event_stop = TRUE; } } @@ -397,7 +403,9 @@ static void sig_handler(int signo) static void sigchild_handler(int signo ATTRIBUTE_UNUSED) { #ifdef WNOHANG - while (waitpid(-1, NULL, WNOHANG) > 0) {}; + while (waitpid(-1, NULL, WNOHANG) > 0) { + continue; + } #endif /* WNOHANG */ return; } @@ -454,10 +462,11 @@ static void setup_signals(void) 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)) + if (!unlink(cnt_list[0]->conf.pid_file)) { MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Removed process id file (pid file).")); - else + } else { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Error removing pid file")); + } } if (ptr_logfile) { @@ -541,8 +550,10 @@ static void motion_detected(struct context *cnt, int dev, struct image_data *img indx = cnt->imgs.image_ring_out-1; do { indx++; - if (indx == cnt->imgs.image_ring_size) indx = 0; - if ((cnt->imgs.image_ring[indx].flags & (IMAGE_SAVE | IMAGE_SAVED)) == IMAGE_SAVE){ + if (indx == cnt->imgs.image_ring_size) { + indx = 0; + } + if ((cnt->imgs.image_ring[indx].flags & (IMAGE_SAVE | IMAGE_SAVED)) == IMAGE_SAVE) { event(cnt, EVENT_FIRSTMOTION, img, NULL, NULL, &cnt->imgs.image_ring[indx].timestamp_tv); indx = cnt->imgs.image_ring_in; } @@ -552,11 +563,13 @@ static void motion_detected(struct context *cnt, int dev, struct image_data *img 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)) + if (cnt->new_img & (NEWIMG_FIRST | NEWIMG_BEST | NEWIMG_CENTER)) { image_save_as_preview(cnt, img); + } /* Save first motion frame */ - if (cnt->new_img & NEWIMG_FIRST) + if (cnt->new_img & NEWIMG_FIRST) { event(cnt, EVENT_IMAGE_PREVIEW, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); + } } @@ -572,20 +585,23 @@ static void motion_detected(struct context *cnt, int dev, struct image_data *img * 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) + 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) + 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) + if (cnt->track.type && cnt->track.active) { cnt->moved = track_move(cnt, dev, location, imgs, 0); + } } @@ -614,8 +630,9 @@ static void process_image_ring(struct context *cnt, unsigned int max_images) 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) + 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]; @@ -625,16 +642,17 @@ static void process_image_ring(struct context *cnt, unsigned int max_images) char tmp[32]; const char *t; - if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_TRIGGER) + 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) + } 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) + } 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) + } else if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].flags & IMAGE_POSTCAP) { t = "Postcap"; - else + } 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); @@ -697,8 +715,9 @@ static void process_image_ring(struct context *cnt, unsigned int max_images) * Save last shot added to movie * only when we not are within first sec */ - if (cnt->movie_last_shot >= 0) + 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 */ @@ -721,14 +740,16 @@ static void process_image_ring(struct context *cnt, unsigned int max_images) } /* Increment to image after last sended */ - if (++cnt->imgs.image_ring_out >= cnt->imgs.image_ring_size) + 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) + if (max_images == 0) { break; + } } /* loop until out and in is same e.g. buffer empty */ @@ -815,7 +836,7 @@ static void init_mask_privacy(struct context *cnt) /* 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){ + if (cnt->imgs.size_high > 0) { MOTION_LOG(INF, TYPE_ALL, NO_ERRNO ,_("Opening high resolution privacy mask file")); rewind(picture); @@ -840,10 +861,12 @@ static void init_mask_privacy(struct context *cnt) indx_img = 1; indx_max = 1; - if (cnt->imgs.size_high > 0) indx_max = 2; + if (cnt->imgs.size_high > 0) { + indx_max = 2; + } - while (indx_img <= indx_max){ - if (indx_img == 1){ + 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; @@ -865,7 +888,7 @@ static void init_mask_privacy(struct context *cnt) 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) ){ + 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; @@ -874,7 +897,7 @@ static void init_mask_privacy(struct context *cnt) } } else { img_temp[y_index] = 0x00; - if ((indxcol % 2 == 0) && (indxrow % 2 == 0) ){ + 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; @@ -900,18 +923,24 @@ static void init_text_scale(struct context *cnt) */ cnt->text_scale = cnt->conf.text_scale; - if (cnt->text_scale <= 0) cnt->text_scale = 1; + 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; + 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; + 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); } @@ -956,27 +985,27 @@ static void mot_stream_deinit(struct context *cnt) pthread_mutex_destroy(&cnt->mutex_stream); - if (cnt->imgs.substream_image != NULL){ + if (cnt->imgs.substream_image != NULL) { free(cnt->imgs.substream_image); cnt->imgs.substream_image = NULL; } - if (cnt->stream_norm.jpeg_data != 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){ + 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){ + 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){ + if (cnt->stream_source.jpeg_data != NULL) { free(cnt->stream_source.jpeg_data); cnt->stream_source.jpeg_data = NULL; } @@ -1029,9 +1058,10 @@ static void dbse_global_init(void) } MOTION_LOG(NTC, TYPE_DB, NO_ERRNO,_("database_busy_timeout %d msec"), cnt_list[0]->conf.database_busy_timeout); - if (sqlite3_busy_timeout( cnt_list[0]->database_sqlite3, cnt_list[0]->conf.database_busy_timeout) != SQLITE_OK) + if (sqlite3_busy_timeout( cnt_list[0]->database_sqlite3, cnt_list[0]->conf.database_busy_timeout) != SQLITE_OK) { MOTION_LOG(ERR, TYPE_DB, NO_ERRNO,_("database_busy_timeout failed %s") ,sqlite3_errmsg( cnt_list[0]->database_sqlite3)); + } } } /* Cascade to all threads */ @@ -1054,7 +1084,7 @@ static int dbse_init_mysql(struct context *cnt) cnt->database_event_id = 0; cnt->database = mymalloc(sizeof(MYSQL)); mysql_init(cnt->database); - if ((cnt->conf.database_port < 0) || (cnt->conf.database_port > 65535)){ + if ((cnt->conf.database_port < 0) || (cnt->conf.database_port > 65535)) { dbport = 0; } else { dbport = cnt->conf.database_port; @@ -1101,10 +1131,11 @@ static int dbse_init_sqlite3(struct context *cnt) } MOTION_LOG(NTC, TYPE_DB, NO_ERRNO ,_("database_busy_timeout %d msec"), cnt->conf.database_busy_timeout); - if (sqlite3_busy_timeout(cnt->database_sqlite3, cnt->conf.database_busy_timeout) != SQLITE_OK) + if (sqlite3_busy_timeout(cnt->database_sqlite3, cnt->conf.database_busy_timeout) != SQLITE_OK) { MOTION_LOG(ERR, TYPE_DB, NO_ERRNO ,_("database_busy_timeout failed %s") ,sqlite3_errmsg(cnt->database_sqlite3)); + } } #else (void)cnt; /* Avoid compiler warnings */ @@ -1157,13 +1188,19 @@ static int dbse_init(struct context *cnt) ,_("Database backend %s"), cnt->conf.database_type); retcd = dbse_init_mysql(cnt); - if (retcd != 0) return retcd; + if (retcd != 0) { + return retcd; + } retcd = dbse_init_sqlite3(cnt); - if (retcd != 0) return retcd; + if (retcd != 0) { + return retcd; + } retcd = dbse_init_pgsql(cnt); - if (retcd != 0) return retcd; + if (retcd != 0) { + return retcd; + } /* Set the sql mask file according to the SQL config options*/ cnt->sql_mask = cnt->conf.sql_log_picture * (FTYPE_IMAGE + FTYPE_IMAGE_MOTION) + @@ -1270,13 +1307,15 @@ static int motion_init(struct context *cnt) ,_("Camera %d started: motion detection %s"), cnt->camera_id, cnt->pause ? _("Disabled"):_("Enabled")); - if (!cnt->conf.target_dir) + if (!cnt->conf.target_dir) { cnt->conf.target_dir = mystrdup("."); + } - if (init_camera_type(cnt) != 0 ) return -3; + if (init_camera_type(cnt) != 0 ) { + return -3; + } - if ((cnt->camera_type != CAMERA_TYPE_RTSP) && - (cnt->movie_passthrough)) { + if ((cnt->camera_type != CAMERA_TYPE_RTSP) && (cnt->movie_passthrough)) { MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO,_("Pass-through processing disabled.")); cnt->movie_passthrough = FALSE; } @@ -1303,8 +1342,12 @@ static int motion_init(struct context *cnt) MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO ,_("Adjusting height to next higher multiple of 8 (%d)."), cnt->conf.height); } - if (cnt->conf.width < 64) cnt->conf.width = 64; - if (cnt->conf.height < 64) cnt->conf.height = 64; + if (cnt->conf.width < 64) { + cnt->conf.width = 64; + } + if (cnt->conf.height < 64) { + cnt->conf.height = 64; + } /* set the device settings */ cnt->video_dev = vid_start(cnt); @@ -1337,7 +1380,7 @@ static int motion_init(struct context *cnt) ,cnt->imgs.width, cnt->imgs.height); return -3; } - if ((cnt->imgs.width < 64) || (cnt->imgs.height < 64)){ + if ((cnt->imgs.width < 64) || (cnt->imgs.height < 64)) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO ,_("Motion only supports width and height greater than or equal to 64 %dx%d") ,cnt->imgs.width, cnt->imgs.height); @@ -1371,7 +1414,7 @@ static int motion_init(struct context *cnt) cnt->imgs.labelsize = mymalloc((cnt->imgs.motionsize/2+1) * sizeof(*cnt->imgs.labelsize)); cnt->imgs.preview_image.image_norm = mymalloc(cnt->imgs.size_norm); cnt->imgs.common_buffer = mymalloc(3 * cnt->imgs.width * cnt->imgs.height); - if (cnt->imgs.size_high > 0){ + if (cnt->imgs.size_high > 0) { cnt->imgs.image_virgin.image_high = mymalloc(cnt->imgs.size_high); cnt->imgs.preview_image.image_high = mymalloc(cnt->imgs.size_high); } @@ -1379,9 +1422,9 @@ static int motion_init(struct context *cnt) mot_stream_init(cnt); /* Set output picture type */ - if (!strcmp(cnt->conf.picture_type, "ppm")) + if (!strcmp(cnt->conf.picture_type, "ppm")) { cnt->imgs.picture_type = IMAGE_TYPE_PPM; - else if (!strcmp(cnt->conf.picture_type, "webp")) { + } else if (!strcmp(cnt->conf.picture_type, "webp")) { #ifdef HAVE_WEBP cnt->imgs.picture_type = IMAGE_TYPE_WEBP; #else @@ -1390,9 +1433,9 @@ static int motion_init(struct context *cnt) ,_("webp image format is not available, failing back to jpeg")); cnt->imgs.picture_type = IMAGE_TYPE_JPEG; #endif /* HAVE_WEBP */ - } - else + } else { cnt->imgs.picture_type = IMAGE_TYPE_JPEG; + } /* * Now is a good time to init rotation data. Since vid_start has been @@ -1412,8 +1455,9 @@ static int motion_init(struct context *cnt) int i; for (i = 0; i < 5; i++) { - if (vid_next(cnt, &cnt->imgs.image_virgin) == 0) + if (vid_next(cnt, &cnt->imgs.image_virgin) == 0) { break; + } SLEEP(2, 0); } @@ -1461,7 +1505,9 @@ static int motion_init(struct context *cnt) #endif /* HAVE_V4L2 && !BSD */ retcd = dbse_init(cnt); - if (retcd != 0) return retcd; + if (retcd != 0) { + return retcd; + } /* Load the mask file if any */ if (cnt->conf.mask_file) { @@ -1508,7 +1554,7 @@ static int motion_init(struct context *cnt) /* Set threshold value */ cnt->threshold = cnt->conf.threshold; - if (cnt->conf.threshold_maximum > cnt->conf.threshold ){ + if (cnt->conf.threshold_maximum > cnt->conf.threshold ) { cnt->threshold_maximum = cnt->conf.threshold_maximum; } else { cnt->threshold_maximum = (cnt->imgs.height * cnt->imgs.width * 3) / 2; @@ -1518,8 +1564,9 @@ static int motion_init(struct context *cnt) cnt->moved = 8; /* Work out expected frame rate based on config setting */ - if (cnt->conf.framerate < 2) + if (cnt->conf.framerate < 2) { cnt->conf.framerate = 2; + } /* 2 sec startup delay so FPS is calculated correct */ cnt->startup_frames = (cnt->conf.framerate * 2) + cnt->conf.pre_capture + cnt->conf.minimum_motion_frames; @@ -1537,14 +1584,16 @@ static int motion_init(struct context *cnt) cnt->rolling_average_data = mymalloc(sizeof(cnt->rolling_average_data) * cnt->rolling_average_limit); /* Preset history buffer with expected frame rate */ - for (indx = 0; indx < cnt->rolling_average_limit; indx++) + for (indx = 0; indx < cnt->rolling_average_limit; indx++) { cnt->rolling_average_data[indx] = cnt->required_frame_time; + } cnt->track_posx = 0; cnt->track_posy = 0; - if (cnt->track.type) + if (cnt->track.type) { cnt->moved = track_center(cnt, cnt->video_dev, 0, 0, 0); + } /* Initialize area detection */ cnt->area_minx[0] = cnt->area_minx[3] = cnt->area_minx[6] = 0; @@ -1652,19 +1701,29 @@ static void motion_cleanup(struct context *cnt) free(cnt->imgs.smartmask_buffer); cnt->imgs.smartmask_buffer = NULL; - if (cnt->imgs.mask) free(cnt->imgs.mask); + if (cnt->imgs.mask) { + free(cnt->imgs.mask); + } cnt->imgs.mask = NULL; - if (cnt->imgs.mask_privacy) free(cnt->imgs.mask_privacy); + if (cnt->imgs.mask_privacy) { + free(cnt->imgs.mask_privacy); + } cnt->imgs.mask_privacy = NULL; - if (cnt->imgs.mask_privacy_uv) free(cnt->imgs.mask_privacy_uv); + if (cnt->imgs.mask_privacy_uv) { + free(cnt->imgs.mask_privacy_uv); + } cnt->imgs.mask_privacy_uv = NULL; - if (cnt->imgs.mask_privacy_high) free(cnt->imgs.mask_privacy_high); + if (cnt->imgs.mask_privacy_high) { + free(cnt->imgs.mask_privacy_high); + } cnt->imgs.mask_privacy_high = NULL; - if (cnt->imgs.mask_privacy_high_uv) free(cnt->imgs.mask_privacy_high_uv); + if (cnt->imgs.mask_privacy_high_uv) { + free(cnt->imgs.mask_privacy_high_uv); + } cnt->imgs.mask_privacy_high_uv = NULL; free(cnt->imgs.common_buffer); @@ -1673,7 +1732,7 @@ static void motion_cleanup(struct context *cnt) free(cnt->imgs.preview_image.image_norm); cnt->imgs.preview_image.image_norm = NULL; - if (cnt->imgs.size_high > 0){ + if (cnt->imgs.size_high > 0) { free(cnt->imgs.image_virgin.image_high); cnt->imgs.image_virgin.image_high = NULL; @@ -1695,7 +1754,9 @@ static void motion_cleanup(struct context *cnt) cnt->mpipe = -1; } - if (cnt->rolling_average_data != NULL) free(cnt->rolling_average_data); + if (cnt->rolling_average_data != NULL) { + free(cnt->rolling_average_data); + } /* Cleanup the current time structure */ @@ -1713,7 +1774,9 @@ static void motion_cleanup(struct context *cnt) static void mlp_mask_privacy(struct context *cnt) { - if (cnt->imgs.mask_privacy == NULL) return; + if (cnt->imgs.mask_privacy == NULL) { + return; + } /* * This function uses long operations to process 4 (32 bit) or 8 (64 bit) @@ -1732,10 +1795,12 @@ static void mlp_mask_privacy(struct context *cnt) indx_img = 1; indx_max = 1; - if (cnt->imgs.size_high > 0) indx_max = 2; + if (cnt->imgs.size_high > 0) { + indx_max = 2; + } increment = sizeof(unsigned long); - while (indx_img <= indx_max){ + while (indx_img <= indx_max) { if (indx_img == 1) { /* Normal Resolution */ index_y = cnt->imgs.height * cnt->imgs.width; @@ -1779,7 +1844,9 @@ static void mlp_mask_privacy(struct context *cnt) } while (--index_crcb >= 0) { - if (*(mask++) == 0x00) *image = 0x80; // Mask last remaining bytes. + if (*(mask++) == 0x00) { + *image = 0x80; // Mask last remaining bytes. + } image += 1; } @@ -1851,11 +1918,13 @@ static void mlp_prepare(struct context *cnt) * this sanity check must go in the main loop :(, before pre_captures * are attempted. */ - if (cnt->conf.minimum_motion_frames < 1) + if (cnt->conf.minimum_motion_frames < 1) { cnt->conf.minimum_motion_frames = 1; + } - if (cnt->conf.pre_capture < 0) + if (cnt->conf.pre_capture < 0) { cnt->conf.pre_capture = 0; + } /* * Check if our buffer is still the right size @@ -1864,8 +1933,9 @@ static void mlp_prepare(struct context *cnt) */ frame_buffer_size = cnt->conf.pre_capture + cnt->conf.minimum_motion_frames; - if (cnt->imgs.image_ring_size != frame_buffer_size) + if (cnt->imgs.image_ring_size != frame_buffer_size) { image_ring_resize(cnt, frame_buffer_size); + } /* Get time for current frame */ cnt->currenttime = time(NULL); @@ -1888,8 +1958,9 @@ static void mlp_prepare(struct context *cnt) if (cnt->conf.minimum_frame_time) { cnt->minimum_frame_time_downcounter--; - if (cnt->minimum_frame_time_downcounter == 0) + if (cnt->minimum_frame_time_downcounter == 0) { cnt->get_image = 1; + } } else { cnt->get_image = 1; } @@ -1899,8 +1970,9 @@ static void mlp_prepare(struct context *cnt) /* Increase the shots variable for each frame captured within this second */ cnt->shots++; - if (cnt->startup_frames > 0) + if (cnt->startup_frames > 0) { cnt->startup_frames--; + } } @@ -1916,13 +1988,15 @@ static void mlp_resetimages(struct context *cnt) } /* ring_buffer_in is pointing to current pos, update before put in a new image */ - if (++cnt->imgs.image_ring_in >= cnt->imgs.image_ring_size) + if (++cnt->imgs.image_ring_in >= cnt->imgs.image_ring_size) { cnt->imgs.image_ring_in = 0; + } /* Check if we have filled the ring buffer, throw away last image */ if (cnt->imgs.image_ring_in == cnt->imgs.image_ring_out) { - if (++cnt->imgs.image_ring_out >= cnt->imgs.image_ring_size) + if (++cnt->imgs.image_ring_out >= cnt->imgs.image_ring_size) { cnt->imgs.image_ring_out = 0; + } } /* cnt->current_image points to position in ring where to store image, diffs etc. */ @@ -1969,8 +2043,7 @@ static int mlp_retry(struct context *cnt) */ int size_high; - if (cnt->video_dev < 0 && - cnt->currenttime % 10 == 0 && cnt->shots == 0) { + if (cnt->video_dev < 0 && cnt->currenttime % 10 == 0 && cnt->shots == 0) { MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO ,_("Retrying until successful connection with camera")); cnt->video_dev = vid_start(cnt); @@ -1986,7 +2059,7 @@ static int mlp_retry(struct context *cnt) return 1; } - if ((cnt->imgs.width < 64) || (cnt->imgs.height < 64)){ + if ((cnt->imgs.width < 64) || (cnt->imgs.height < 64)) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO ,_("Motion only supports width and height greater than or equal to 64 %dx%d") ,cnt->imgs.width, cnt->imgs.height); @@ -2017,7 +2090,9 @@ static int mlp_retry(struct context *cnt) * and the vid_start ONLY re-populates the height/width so we can check the size here. */ size_high = (cnt->imgs.width_high * cnt->imgs.height_high * 3) / 2; - if (cnt->imgs.size_high != size_high) return 1; + if (cnt->imgs.size_high != size_high) { + return 1; + } } return 0; } @@ -2039,10 +2114,11 @@ static int mlp_capture(struct context *cnt) * <0 = fatal error - leave the thread by breaking out of the main loop * >0 = non fatal error - copy last image or show grey image with message */ - if (cnt->video_dev >= 0) + if (cnt->video_dev >= 0) { vid_return_code = vid_next(cnt, cnt->current_image); - else + } else { vid_return_code = 1; /* Non fatal error */ + } // VALID PICTURE if (vid_return_code == 0) { @@ -2125,7 +2201,7 @@ static int mlp_capture(struct context *cnt) * First missed frame - store timestamp * Don't reset time when thread restarts */ - if (cnt->connectionlosttime == 0){ + if (cnt->connectionlosttime == 0) { cnt->connectionlosttime = cnt->currenttime; } @@ -2145,10 +2221,11 @@ static int mlp_capture(struct context *cnt) } else { cnt->lost_connection = 1; - if (cnt->video_dev >= 0) + if (cnt->video_dev >= 0) { tmpin = "CONNECTION TO CAMERA LOST\\nSINCE %Y-%m-%d %T"; - else + } else { tmpin = "UNABLE TO OPEN VIDEO DEVICE\\nSINCE %Y-%m-%d %T"; + } tv1.tv_sec=cnt->connectionlosttime; tv1.tv_usec = 0; @@ -2205,10 +2282,11 @@ static void mlp_detection(struct context *cnt) * motion, the alg_diff will trigger alg_diff_standard * anyway */ - if (cnt->detecting_motion || cnt->conf.setup_mode) + if (cnt->detecting_motion || cnt->conf.setup_mode) { cnt->current_image->diffs = alg_diff_standard(cnt, cnt->imgs.image_vprvcy.image_norm); - else + } else { cnt->current_image->diffs = alg_diff(cnt, cnt->imgs.image_vprvcy.image_norm); + } /* Lightswitch feature - has light intensity changed? * This can happen due to change of light conditions or due to a sudden change of the camera @@ -2220,13 +2298,15 @@ static void mlp_detection(struct context *cnt) if (alg_lightswitch(cnt, cnt->current_image->diffs)) { MOTION_LOG(INF, TYPE_ALL, NO_ERRNO, _("Lightswitch detected")); - if (cnt->conf.lightswitch_frames < 1) + if (cnt->conf.lightswitch_frames < 1) { cnt->conf.lightswitch_frames = 1; - else if (cnt->conf.lightswitch_frames > 1000) + } else if (cnt->conf.lightswitch_frames > 1000) { cnt->conf.lightswitch_frames = 1000; + } - if (cnt->moved < (unsigned int)cnt->conf.lightswitch_frames) + if (cnt->moved < (unsigned int)cnt->conf.lightswitch_frames) { cnt->moved = (unsigned int)cnt->conf.lightswitch_frames; + } cnt->current_image->diffs = 0; alg_update_reference_frame(cnt, RESET_REF_FRAME); @@ -2248,7 +2328,6 @@ static void mlp_detection(struct context *cnt) if ((cnt->current_image->diffs <= cnt->threshold) || (cnt->current_image->diffs > cnt->threshold_maximum)) { - cnt->current_image->diffs = 0; MOTION_LOG(INF, TYPE_ALL, NO_ERRNO, _("Switchfilter detected")); } @@ -2311,8 +2390,9 @@ static void mlp_tuning(struct context *cnt) * no frames have been recorded and only once per second */ if ((cnt->conf.noise_tune && cnt->shots == 0) && - (!cnt->detecting_motion && (cnt->current_image->diffs <= cnt->threshold))) + (!cnt->detecting_motion && (cnt->current_image->diffs <= cnt->threshold))) { alg_noise_tune(cnt, cnt->imgs.image_vprvcy.image_norm); + } /* @@ -2325,7 +2405,7 @@ static void mlp_tuning(struct context *cnt) * if we are not threshold tuning lets make sure that remote controlled * changes of threshold are used. */ - if (cnt->conf.threshold_tune){ + if (cnt->conf.threshold_tune) { alg_threshold_tune(cnt, cnt->current_image->diffs, cnt->detecting_motion); } @@ -2335,7 +2415,7 @@ static void mlp_tuning(struct context *cnt) * for adding the locate rectangle */ if ((cnt->current_image->diffs > cnt->threshold) && - (cnt->current_image->diffs < cnt->threshold_maximum)){ + (cnt->current_image->diffs < cnt->threshold_maximum)) { alg_locate_center_size(&cnt->imgs , cnt->imgs.width @@ -2392,25 +2472,29 @@ static void mlp_overlay(struct context *cnt) /* Smartmask overlay */ if (cnt->smartmask_speed && (cnt->conf.picture_output_motion || cnt->conf.movie_output_motion || - cnt->conf.setup_mode || (cnt->stream_motion.cnct_count > 0))) + cnt->conf.setup_mode || (cnt->stream_motion.cnct_count > 0))) { overlay_smartmask(cnt, cnt->imgs.img_motion.image_norm); + } /* Largest labels overlay */ if (cnt->imgs.largest_label && (cnt->conf.picture_output_motion || cnt->conf.movie_output_motion || - cnt->conf.setup_mode || (cnt->stream_motion.cnct_count > 0))) + cnt->conf.setup_mode || (cnt->stream_motion.cnct_count > 0))) { overlay_largest_label(cnt, cnt->imgs.img_motion.image_norm); + } /* Fixed mask overlay */ if (cnt->imgs.mask && (cnt->conf.picture_output_motion || cnt->conf.movie_output_motion || - cnt->conf.setup_mode || (cnt->stream_motion.cnct_count > 0))) + cnt->conf.setup_mode || (cnt->stream_motion.cnct_count > 0))) { overlay_fixed_mask(cnt, cnt->imgs.img_motion.image_norm); + } /* Add changed pixels in upper right corner of the pictures */ if (cnt->conf.text_changes) { - if (!cnt->pause) + if (!cnt->pause) { sprintf(tmp, "%d", cnt->current_image->diffs); - else + } else { sprintf(tmp, "-"); + } draw_text(cnt->current_image->image_norm, cnt->imgs.width, cnt->imgs.height, cnt->imgs.width - 10, 10, tmp, cnt->text_scale); @@ -2479,8 +2563,9 @@ static void mlp_actions(struct context *cnt) * no motion then we reset the start movie time so that we do not * get a pause in the movie. */ - if ( (cnt->detecting_motion == 0) && (cnt->ffmpeg_output != NULL) ) + if ( (cnt->detecting_motion == 0) && (cnt->ffmpeg_output != NULL) ) { ffmpeg_reset_movie_start_time(cnt->ffmpeg_output, &cnt->current_image->timestamp_tv); + } cnt->detecting_motion = 1; if (cnt->conf.post_capture > 0) { /* Setup the postcap counter */ @@ -2490,7 +2575,7 @@ static void mlp_actions(struct context *cnt) cnt->current_image->flags |= (IMAGE_TRIGGER | IMAGE_SAVE); /* Mark all images in image_ring to be saved */ - for (indx = 0; indx < cnt->imgs.image_ring_size; indx++){ + for (indx = 0; indx < cnt->imgs.image_ring_size; indx++) { cnt->imgs.image_ring[indx].flags |= IMAGE_SAVE; } @@ -2506,13 +2591,15 @@ static void mlp_actions(struct context *cnt) int pos = cnt->imgs.image_ring_in; for (indx = 0; indx < cnt->conf.minimum_motion_frames; indx++) { - if (cnt->imgs.image_ring[pos].flags & IMAGE_MOTION) + if (cnt->imgs.image_ring[pos].flags & IMAGE_MOTION) { frame_count++; + } - if (pos == 0) + if (pos == 0) { pos = cnt->imgs.image_ring_size-1; - else + } else { pos--; + } } if (frame_count >= cnt->conf.minimum_motion_frames) { @@ -2522,8 +2609,9 @@ static void mlp_actions(struct context *cnt) * no motion then we reset the start movie time so that we do not * get a pause in the movie. */ - if ( (cnt->detecting_motion == 0) && (cnt->ffmpeg_output != NULL) ) + if ( (cnt->detecting_motion == 0) && (cnt->ffmpeg_output != NULL) ) { ffmpeg_reset_movie_start_time(cnt->ffmpeg_output, &cnt->current_image->timestamp_tv); + } cnt->detecting_motion = 1; @@ -2532,8 +2620,9 @@ static void mlp_actions(struct context *cnt) //MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO, "Setup post capture %d", cnt->postcap); /* Mark all images in image_ring to be saved */ - for (indx = 0; indx < cnt->imgs.image_ring_size; indx++) + for (indx = 0; indx < cnt->imgs.image_ring_size; indx++) { cnt->imgs.image_ring[indx].flags |= IMAGE_SAVE; + } } else if (cnt->postcap > 0) { /* we have motion in this frame, but not enought frames for trigger. Check postcap */ @@ -2555,15 +2644,16 @@ static void mlp_actions(struct context *cnt) /* Done with postcap, so just have the image in the precap buffer */ cnt->current_image->flags |= IMAGE_PRECAP; /* gapless movie feature */ - if ((cnt->conf.event_gap == 0) && (cnt->detecting_motion == 1)) + if ((cnt->conf.event_gap == 0) && (cnt->detecting_motion == 1)) { cnt->event_stop = TRUE; + } cnt->detecting_motion = 0; } /* Update last frame saved time, so we can end event after gap time */ - if (cnt->current_image->flags & IMAGE_SAVE) + if (cnt->current_image->flags & IMAGE_SAVE) { cnt->lasttime = cnt->current_image->timestamp_tv.tv_sec; - + } mlp_areadetect(cnt); @@ -2599,7 +2689,7 @@ static void mlp_actions(struct context *cnt) event(cnt, EVENT_ENDMOTION, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); - if (cnt->track.type){ + if (cnt->track.type) { cnt->moved = track_center(cnt, cnt->video_dev, 0, 0, 0); } @@ -2697,34 +2787,33 @@ static void mlp_timelapse(struct context *cnt) if (timestamp_tm.tm_min == 0 && (cnt->time_current_frame % 60 < cnt->time_last_frame % 60) && cnt->shots == 0) { - if (strcasecmp(cnt->conf.timelapse_mode, "manual") == 0) { ;/* No action */ /* If we are daily, raise timelapseend event at midnight */ } else if (strcasecmp(cnt->conf.timelapse_mode, "daily") == 0) { - if (timestamp_tm.tm_hour == 0) + if (timestamp_tm.tm_hour == 0) { event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); - + } /* handle the hourly case */ } else if (strcasecmp(cnt->conf.timelapse_mode, "hourly") == 0) { event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); /* If we are weekly-sunday, raise timelapseend event at midnight on sunday */ } else if (strcasecmp(cnt->conf.timelapse_mode, "weekly-sunday") == 0) { - if (timestamp_tm.tm_wday == 0 && - timestamp_tm.tm_hour == 0) + if (timestamp_tm.tm_wday == 0 && timestamp_tm.tm_hour == 0) { event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); + } /* If we are weekly-monday, raise timelapseend event at midnight on monday */ } else if (strcasecmp(cnt->conf.timelapse_mode, "weekly-monday") == 0) { - if (timestamp_tm.tm_wday == 1 && - timestamp_tm.tm_hour == 0) + if (timestamp_tm.tm_wday == 1 && timestamp_tm.tm_hour == 0) { event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); + } /* If we are monthly, raise timelapseend event at midnight on first day of month */ } else if (strcasecmp(cnt->conf.timelapse_mode, "monthly") == 0) { - if (timestamp_tm.tm_mday == 1 && - timestamp_tm.tm_hour == 0) + if (timestamp_tm.tm_mday == 1 && timestamp_tm.tm_hour == 0) { event(cnt, EVENT_TIMELAPSEEND, NULL, NULL, NULL, &cnt->current_image->timestamp_tv); + } /* If invalid we report in syslog once and continue in manual mode */ } else { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO @@ -2778,9 +2867,10 @@ static void mlp_loopback(struct context *cnt) event(cnt, EVENT_IMAGE, cnt->current_image, NULL, &cnt->pipe, &cnt->current_image->timestamp_tv); - if (!cnt->conf.stream_motion || cnt->shots == 1) + if (!cnt->conf.stream_motion || cnt->shots == 1) { event(cnt, EVENT_STREAM, cnt->current_image, NULL, NULL, &cnt->current_image->timestamp_tv); + } } event(cnt, EVENT_IMAGEM, &cnt->imgs.img_motion, NULL, &cnt->mpipe, &cnt->current_image->timestamp_tv); @@ -2792,42 +2882,48 @@ static void mlp_parmsupdate(struct context *cnt) /***** MOTION LOOP - ONCE PER SECOND PARAMETER UPDATE SECTION *****/ /* Check for some config parameter changes but only every second */ - if (cnt->shots != 0) return; + if (cnt->shots != 0) { + return; + } init_text_scale(cnt); /* Initialize and validate text_scale */ - if (strcasecmp(cnt->conf.picture_output, "on") == 0) + if (strcasecmp(cnt->conf.picture_output, "on") == 0) { cnt->new_img = NEWIMG_ON; - else if (strcasecmp(cnt->conf.picture_output, "first") == 0) + } else if (strcasecmp(cnt->conf.picture_output, "first") == 0) { cnt->new_img = NEWIMG_FIRST; - else if (strcasecmp(cnt->conf.picture_output, "best") == 0) + } else if (strcasecmp(cnt->conf.picture_output, "best") == 0) { cnt->new_img = NEWIMG_BEST; - else if (strcasecmp(cnt->conf.picture_output, "center") == 0) + } else if (strcasecmp(cnt->conf.picture_output, "center") == 0) { cnt->new_img = NEWIMG_CENTER; - else + } else { cnt->new_img = NEWIMG_OFF; + } - if (strcasecmp(cnt->conf.locate_motion_mode, "on") == 0) + if (strcasecmp(cnt->conf.locate_motion_mode, "on") == 0) { cnt->locate_motion_mode = LOCATE_ON; - else if (strcasecmp(cnt->conf.locate_motion_mode, "preview") == 0) + } else if (strcasecmp(cnt->conf.locate_motion_mode, "preview") == 0) { cnt->locate_motion_mode = LOCATE_PREVIEW; - else + } else { cnt->locate_motion_mode = LOCATE_OFF; + } - if (strcasecmp(cnt->conf.locate_motion_style, "box") == 0) + if (strcasecmp(cnt->conf.locate_motion_style, "box") == 0) { cnt->locate_motion_style = LOCATE_BOX; - else if (strcasecmp(cnt->conf.locate_motion_style, "redbox") == 0) + } else if (strcasecmp(cnt->conf.locate_motion_style, "redbox") == 0) { cnt->locate_motion_style = LOCATE_REDBOX; - else if (strcasecmp(cnt->conf.locate_motion_style, "cross") == 0) + } else if (strcasecmp(cnt->conf.locate_motion_style, "cross") == 0) { cnt->locate_motion_style = LOCATE_CROSS; - else if (strcasecmp(cnt->conf.locate_motion_style, "redcross") == 0) + } else if (strcasecmp(cnt->conf.locate_motion_style, "redcross") == 0) { cnt->locate_motion_style = LOCATE_REDCROSS; - else + } else { cnt->locate_motion_style = LOCATE_BOX; + } /* Sanity check for smart_mask_speed, silly value disables smart mask */ - if (cnt->conf.smart_mask_speed < 0 || cnt->conf.smart_mask_speed > 10) + if (cnt->conf.smart_mask_speed < 0 || cnt->conf.smart_mask_speed > 10) { cnt->conf.smart_mask_speed = 0; + } /* Has someone changed smart_mask_speed or framerate? */ if (cnt->conf.smart_mask_speed != cnt->smartmask_speed || @@ -2849,13 +2945,13 @@ static void mlp_parmsupdate(struct context *cnt) dbse_sqlmask_update(cnt); cnt->threshold = cnt->conf.threshold; - if (cnt->conf.threshold_maximum > cnt->conf.threshold ){ + if (cnt->conf.threshold_maximum > cnt->conf.threshold ) { cnt->threshold_maximum = cnt->conf.threshold_maximum; } else { cnt->threshold_maximum = (cnt->imgs.height * cnt->imgs.width * 3) / 2; } - if (!cnt->conf.noise_tune){ + if (!cnt->conf.noise_tune) { cnt->noise = cnt->conf.noise_level; } @@ -2874,10 +2970,11 @@ static void mlp_frametiming(struct context *cnt) * Work out expected frame rate based on config setting which may * have changed from http-control */ - if (cnt->conf.framerate) + if (cnt->conf.framerate) { cnt->required_frame_time = 1000000L / cnt->conf.framerate; - else + } else { cnt->required_frame_time = 0; + } /* Get latest time to calculate time taken to process video data */ gettimeofday(&tv2, NULL); @@ -2887,34 +2984,39 @@ static void mlp_frametiming(struct context *cnt) * Update history buffer but ignore first pass as timebefore * variable will be inaccurate */ - if (cnt->passflag) + if (cnt->passflag) { cnt->rolling_average_data[cnt->rolling_frame] = cnt->timenow - cnt->timebefore; - else + } else { cnt->passflag = 1; + } cnt->rolling_frame++; - if (cnt->rolling_frame >= cnt->rolling_average_limit) + if (cnt->rolling_frame >= cnt->rolling_average_limit) { cnt->rolling_frame = 0; + } /* Calculate 10 second average and use deviation in delay calculation */ cnt->rolling_average = 0L; - for (indx = 0; indx < cnt->rolling_average_limit; indx++) + for (indx = 0; indx < cnt->rolling_average_limit; indx++) { cnt->rolling_average += cnt->rolling_average_data[indx]; + } cnt->rolling_average /= cnt->rolling_average_limit; cnt->frame_delay = cnt->required_frame_time - elapsedtime - (cnt->rolling_average - cnt->required_frame_time); if (cnt->frame_delay > 0) { /* Apply delay to meet frame time */ - if (cnt->frame_delay > cnt->required_frame_time) + if (cnt->frame_delay > cnt->required_frame_time) { cnt->frame_delay = cnt->required_frame_time; + } /* Delay time in nanoseconds for SLEEP */ delay_time_nsec = cnt->frame_delay * 1000; - if (delay_time_nsec > 999999999) + if (delay_time_nsec > 999999999) { delay_time_nsec = 999999999; + } /* SLEEP as defined in motion.h A safe sleep using nanosleep */ SLEEP(0, delay_time_nsec); @@ -2932,13 +3034,17 @@ static void *motion_loop(void *arg) { struct context *cnt = arg; - if (motion_init(cnt) == 0){ + if (motion_init(cnt) == 0) { while (!cnt->finish || cnt->event_stop) { mlp_prepare(cnt); if (cnt->get_image) { mlp_resetimages(cnt); - if (mlp_retry(cnt) == 1) break; - if (mlp_capture(cnt) == 1) break; + if (mlp_retry(cnt) == 1) { + break; + } + if (mlp_capture(cnt) == 1) { + break; + } mlp_detection(cnt); mlp_tuning(cnt); mlp_overlay(cnt); @@ -3020,8 +3126,9 @@ static void become_daemon(void) MOTION_LOG(EMG, TYPE_ALL, SHOW_ERRNO ,_("Exit motion, cannot create process" " id file (pid file) %s"), cnt_list[0]->conf.pid_file); - if (ptr_logfile) + if (ptr_logfile) { myfclose(ptr_logfile); + } exit(0); } } @@ -3030,8 +3137,9 @@ static void become_daemon(void) * Changing dir to root enables people to unmount a disk * without having to stop Motion */ - if (chdir("/")) + if (chdir("/")) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Could not change directory")); + } #if (defined(BSD) && !defined(__APPLE__)) @@ -3063,10 +3171,11 @@ static void become_daemon(void) } /* Now it is safe to add the PID creation to the logs */ - if (pidf) + if (pidf) { MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO ,_("Created process id file %s. Process ID is %d") ,cnt_list[0]->conf.pid_file, getpid()); + } sigaction(SIGTTOU, &sig_ign_action, NULL); sigaction(SIGTTIN, &sig_ign_action, NULL); @@ -3114,8 +3223,9 @@ static void motion_shutdown(void) webu_stop(cnt_list); - while (cnt_list[++i]) + while (cnt_list[++i]) { context_destroy(cnt_list[i]); + } free(cnt_list); cnt_list = NULL; @@ -3130,8 +3240,8 @@ static void motion_camera_ids(void) /* Set defaults */ indx = 0; - while (cnt_list[indx] != NULL){ - if (cnt_list[indx]->conf.camera_id > 0){ + while (cnt_list[indx] != NULL) { + if (cnt_list[indx]->conf.camera_id > 0) { cnt_list[indx]->camera_id = cnt_list[indx]->conf.camera_id; } else { cnt_list[indx]->camera_id = indx; @@ -3141,21 +3251,24 @@ static void motion_camera_ids(void) invalid_ids = FALSE; indx = 0; - while (cnt_list[indx] != NULL){ - if (cnt_list[indx]->camera_id > 32000) invalid_ids = TRUE; + while (cnt_list[indx] != NULL) { + if (cnt_list[indx]->camera_id > 32000) { + invalid_ids = TRUE; + } indx2 = indx + 1; - while (cnt_list[indx2] != NULL){ - if (cnt_list[indx]->camera_id == cnt_list[indx2]->camera_id) invalid_ids = TRUE; - + while (cnt_list[indx2] != NULL) { + if (cnt_list[indx]->camera_id == cnt_list[indx2]->camera_id) { + invalid_ids = TRUE; + } indx2++; } indx++; } - if (invalid_ids){ + if (invalid_ids) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO ,_("Camara IDs are not unique or have values over 32,000. Falling back to thread numbers")); indx = 0; - while (cnt_list[indx] != NULL){ + while (cnt_list[indx] != NULL) { cnt_list[indx]->camera_id = indx; indx++; } @@ -3315,8 +3428,9 @@ static void motion_startup(int daemonize, int argc, char *argv[]) } } - if (cnt_list[0]->conf.setup_mode) + if (cnt_list[0]->conf.setup_mode) { MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO,_("Motion running in setup mode.")); + } conf_output_parms(cnt_list); @@ -3350,13 +3464,13 @@ static void motion_start_thread(struct context *cnt) char service[6]; pthread_attr_t thread_attr; - if (strcmp(cnt->conf_filename, "")){ + if (strcmp(cnt->conf_filename, "")) { cnt->conf_filename[sizeof(cnt->conf_filename) - 1] = '\0'; MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Camera ID: %d is from %s") ,cnt->camera_id, cnt->conf_filename); } - if (cnt->conf.netcam_url){ + if (cnt->conf.netcam_url) { snprintf(service,6,"%s",cnt->conf.netcam_url); MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO,_("Camera ID: %d Camera Name: %s Service: %s") ,cnt->camera_id, cnt->conf.camera_name,service); @@ -3386,7 +3500,9 @@ static void motion_start_thread(struct context *cnt) } /* Compare against stream ports of other threads. */ for (i = 1; cnt_list[i]; i++) { - if (cnt_list[i] == cnt) continue; + if (cnt_list[i] == cnt) { + continue; + } if (cnt_list[i]->conf.stream_port == cnt->conf.stream_port) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO @@ -3469,7 +3585,9 @@ static void motion_watchdog(int indx) * Best to just not get into a watchdog situation... */ - if (!cnt_list[indx]->running) return; + if (!cnt_list[indx]->running) { + return; + } cnt_list[indx]->watchdog--; if (cnt_list[indx]->watchdog == 0) { @@ -3485,15 +3603,15 @@ static void motion_watchdog(int indx) ,_("Thread %d - Watchdog timeout did NOT restart, killing it!") , cnt_list[indx]->threadnr); if ((cnt_list[indx]->camera_type == CAMERA_TYPE_RTSP) && - (cnt_list[indx]->rtsp != NULL)){ + (cnt_list[indx]->rtsp != NULL)) { pthread_cancel(cnt_list[indx]->rtsp->thread_id); } if ((cnt_list[indx]->camera_type == CAMERA_TYPE_RTSP) && - (cnt_list[indx]->rtsp_high != NULL)){ + (cnt_list[indx]->rtsp_high != NULL)) { pthread_cancel(cnt_list[indx]->rtsp_high->thread_id); } if ((cnt_list[indx]->camera_type == CAMERA_TYPE_NETCAM) && - (cnt_list[indx]->netcam != NULL)){ + (cnt_list[indx]->netcam != NULL)) { pthread_cancel(cnt_list[indx]->netcam->thread_id); } pthread_cancel(cnt_list[indx]->thread_id); @@ -3501,7 +3619,7 @@ static void motion_watchdog(int indx) if (cnt_list[indx]->watchdog < WATCHDOG_KILL) { if ((cnt_list[indx]->camera_type == CAMERA_TYPE_NETCAM) && - (cnt_list[indx]->rtsp != NULL)){ + (cnt_list[indx]->rtsp != NULL)) { if (!cnt_list[indx]->rtsp->handler_finished && pthread_kill(cnt_list[indx]->rtsp->thread_id, 0) == ESRCH) { cnt_list[indx]->rtsp->handler_finished = TRUE; @@ -3514,7 +3632,7 @@ static void motion_watchdog(int indx) } } if ((cnt_list[indx]->camera_type == CAMERA_TYPE_NETCAM) && - (cnt_list[indx]->rtsp_high != NULL)){ + (cnt_list[indx]->rtsp_high != NULL)) { if (!cnt_list[indx]->rtsp_high->handler_finished && pthread_kill(cnt_list[indx]->rtsp_high->thread_id, 0) == ESRCH) { cnt_list[indx]->rtsp_high->handler_finished = TRUE; @@ -3527,7 +3645,7 @@ static void motion_watchdog(int indx) } } if ((cnt_list[indx]->camera_type == CAMERA_TYPE_NETCAM) && - (cnt_list[indx]->netcam != NULL)){ + (cnt_list[indx]->netcam != NULL)) { if (!cnt_list[indx]->netcam->handler_finished && pthread_kill(cnt_list[indx]->netcam->thread_id, 0) == ESRCH) { pthread_mutex_lock(&global_lock); @@ -3540,7 +3658,7 @@ static void motion_watchdog(int indx) } } if (cnt_list[indx]->running && - pthread_kill(cnt_list[indx]->thread_id, 0) == ESRCH){ + pthread_kill(cnt_list[indx]->thread_id, 0) == ESRCH) { MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO ,_("Thread %d - Cleaning thread.") , cnt_list[indx]->threadnr); @@ -3571,8 +3689,9 @@ static int motion_check_threadcount(void) motion_threads_running = 0; for (indx = (cnt_list[1] != NULL ? 1 : 0); cnt_list[indx]; indx++) { - if (cnt_list[indx]->running || cnt_list[indx]->restart) + if (cnt_list[indx]->running || cnt_list[indx]->restart) { motion_threads_running++; + } } /* If the web control/streams are in finish/shutdown, we @@ -3583,7 +3702,7 @@ static int motion_check_threadcount(void) * to restart the cameras and keep Motion running. */ indx = 0; - while (cnt_list[indx] != NULL){ + while (cnt_list[indx] != NULL) { if ((cnt_list[indx]->webcontrol_finish == FALSE) && ((cnt_list[indx]->webcontrol_daemon != NULL) || (cnt_list[indx]->webstream_daemon != NULL))) { @@ -3636,7 +3755,9 @@ int main (int argc, char **argv) translate_init(); do { - if (restart) motion_restart(argc, argv); + if (restart) { + motion_restart(argc, argv); + } for (i = cnt_list[1] != NULL ? 1 : 0; cnt_list[i]; i++) { cnt_list[i]->threadnr = i ? i : 1; @@ -3648,7 +3769,9 @@ int main (int argc, char **argv) while (1) { SLEEP(1, 0); - if (motion_check_threadcount()) break; + if (motion_check_threadcount()) { + break; + } for (i = (cnt_list[1] != NULL ? 1 : 0); cnt_list[i]; i++) { /* Check if threads wants to be restarted */ @@ -3667,7 +3790,9 @@ int main (int argc, char **argv) MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Threads finished")); /* Rest for a while if we're supposed to restart. */ - if (restart) SLEEP(1, 0); + if (restart) { + SLEEP(1, 0); + } } while (restart); /* loop if we're supposed to restart */ @@ -3779,10 +3904,11 @@ int create_path(const char *path) char *start; mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; - if (path[0] == '/') + if (path[0] == '/') { start = strchr(path + 1, '/'); - else + } else { start = strchr(path, '/'); + } while (start) { char *buffer = mystrdup(path); @@ -3797,8 +3923,9 @@ int create_path(const char *path) start = strchr(start + 1, '/'); - if (!start) + if (!start) { MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("creating directory %s"), buffer); + } free(buffer); } @@ -3826,15 +3953,18 @@ FILE * myfopen(const char *path, const char *mode) { /* first, just try to open the file */ FILE *dummy = fopen(path, mode); - if (dummy) return dummy; + if (dummy) { + return dummy; + } /* could not open file... */ /* path did not exist? */ if (errno == ENOENT) { /* create path for file... */ - if (create_path(path) == -1) + if (create_path(path) == -1) { return NULL; + } /* and retry opening the file */ dummy = fopen(path, mode); @@ -3865,8 +3995,9 @@ int myfclose(FILE* fh) { int rval = fclose(fh); - if (rval != 0) + if (rval != 0) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Error closing file")); + } return rval; } @@ -4043,11 +4174,12 @@ size_t mystrftime(const struct context *cnt, char *s, size_t max, const char *us break; case 'C': // text_event - if (cnt->text_event_string[0]) + if (cnt->text_event_string[0]) { snprintf(tempstr, PATH_MAX, "%*s", width, cnt->text_event_string); - else + } else { ++pos_userformat; + } break; case 'w': // picture width @@ -4065,34 +4197,40 @@ size_t mystrftime(const struct context *cnt, char *s, size_t max, const char *us break; } - if (filename) + if (filename) { snprintf(tempstr, PATH_MAX, "%*s", width, filename); - else + } else { ++pos_userformat; + } break; case 'n': // sqltype - if (sqltype) + if (sqltype) { sprintf(tempstr, "%*d", width, sqltype); - else + } else { ++pos_userformat; + } break; case '{': // long format specifier word. { const char *word = ++pos_userformat; - while ((*pos_userformat != '}') && (*pos_userformat != 0)) + while ((*pos_userformat != '}') && (*pos_userformat != 0)) { ++pos_userformat; + } mystrftime_long (cnt, width, word, (int)(pos_userformat-word), tempstr); - if (*pos_userformat == '\0') --pos_userformat; + if (*pos_userformat == '\0') { + --pos_userformat; + } } break; case '$': // thread name - if (cnt->conf.camera_name && cnt->conf.camera_name[0]) + if (cnt->conf.camera_name && cnt->conf.camera_name[0]) { snprintf(tempstr, PATH_MAX, "%s", cnt->conf.camera_name); - else + } else { ++pos_userformat; + } break; default: // Any other code is copied with the %-sign @@ -4106,8 +4244,9 @@ size_t mystrftime(const struct context *cnt, char *s, size_t max, const char *us * 'tempstr' to 'format'. */ if (tempstr[0]) { - while ((*format = *tempstr++) != '\0') + while ((*format = *tempstr++) != '\0') { ++format; + } continue; } } @@ -4133,7 +4272,7 @@ void util_threadname_set(const char *abbr, int threadnbr, const char *threadname */ char tname[16]; - if (abbr != NULL){ + if (abbr != NULL) { snprintf(tname, sizeof(tname), "%s%d%s%s",abbr,threadnbr, threadname ? ":" : "", threadname ? threadname : ""); @@ -4169,12 +4308,13 @@ void util_threadname_get(char *threadname) int util_check_passthrough(struct context *cnt) { #if ( MYFFVER < 55000) - if (cnt->movie_passthrough) + if (cnt->movie_passthrough) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("FFMPEG version too old. Disabling pass-through processing.")); + } return 0; #else - if (cnt->movie_passthrough){ + if (cnt->movie_passthrough) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("pass-through enabled.")); return 1; @@ -4191,23 +4331,30 @@ void util_trim(char *parm) { int indx, indx_st, indx_en; - if (parm == NULL) return; + if (parm == NULL) { + return; + } indx_en = strlen(parm) - 1; - if (indx_en == -1) return; + if (indx_en == -1) { + return; + } indx_st = 0; - while (isspace(parm[indx_st]) && (indx_st <= indx_en)) indx_st++; - if (indx_st > indx_en){ + while (isspace(parm[indx_st]) && (indx_st <= indx_en)) { + indx_st++; + } + if (indx_st > indx_en) { parm[0]= '\0'; return; } - while (isspace(parm[indx_en]) && (indx_en > indx_st)) indx_en--; + while (isspace(parm[indx_en]) && (indx_en > indx_st)) { + indx_en--; + } - for (indx = indx_st; indx<=indx_en; indx++) - { + for (indx = indx_st; indx<=indx_en; indx++) { parm[indx-indx_st] = parm[indx]; } parm[indx_en-indx_st+1] = '\0'; @@ -4236,7 +4383,7 @@ static void util_parms_add(struct params_context *parameters if (parm_nm != NULL) { parameters->params_array[indx].param_name = (char*)mymalloc(strlen(parm_nm)+1); retcd = sprintf(parameters->params_array[indx].param_name,"%s",parm_nm); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO,_("Error setting parm >%s<"),parm_nm); free(parameters->params_array[indx].param_name); parameters->params_array[indx].param_name = NULL; @@ -4248,7 +4395,7 @@ static void util_parms_add(struct params_context *parameters if (parm_vl != NULL) { parameters->params_array[indx].param_value = (char*)mymalloc(strlen(parm_vl)+1); retcd = sprintf(parameters->params_array[indx].param_value,"%s",parm_vl); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO,_("Error setting parm >%s<"),parm_vl); free(parameters->params_array[indx].param_value); parameters->params_array[indx].param_value = NULL; @@ -4275,8 +4422,7 @@ static void util_parms_extract(struct params_context *parameters if ((indxnm_en != 0) && (indxvl_st != 0) && ((indxnm_en - indxnm_st) > 0) && - ((indxvl_en - indxvl_st) > 0)) - { + ((indxvl_en - indxvl_st) > 0)) { parm_nm = mymalloc(PATH_MAX); parm_vl = mymalloc(PATH_MAX); @@ -4301,8 +4447,12 @@ static void util_parms_extract(struct params_context *parameters util_parms_add(parameters, parm_nm, parm_vl); - if (parm_nm != NULL) free(parm_nm); - if (parm_vl != NULL) free(parm_vl); + if (parm_nm != NULL) { + free(parm_nm); + } + if (parm_vl != NULL) { + free(parm_vl); + } } } @@ -4356,8 +4506,7 @@ static void util_parms_parse_qte(struct params_context *parameters, char *parmln { int indxnm_st, indxnm_en, indxvl_st, indxvl_en; - while (strstr(parmlne,"\"") != NULL) - { + while (strstr(parmlne,"\"") != NULL) { indxnm_st = 0; indxnm_en = 0; indxvl_st = 0; @@ -4386,8 +4535,7 @@ static void util_parms_parse_comma(struct params_context *parameters, char *parm { int indxnm_st, indxnm_en, indxvl_st, indxvl_en; - while (strstr(parmlne,",") != NULL) - { + while (strstr(parmlne,",") != NULL) { indxnm_st = 0; indxnm_en = 0; indxvl_st = 0; @@ -4412,10 +4560,11 @@ void util_parms_free(struct params_context *parameters) { int indx; - if (parameters == NULL) return; + if (parameters == NULL) { + return; + } - for (indx = 0; indx < parameters->params_count; indx++) - { + for (indx = 0; indx < parameters->params_count; indx++) { if (parameters->params_array[indx].param_name != NULL) { free(parameters->params_array[indx].param_name); } @@ -4481,7 +4630,7 @@ void util_parms_parse(struct params_context *parameters, char *confparm) indxvl_en = strlen(parmlne); if (strstr(parmlne + 1,"=") != NULL) { indxnm_en = strstr(parmlne + 1,"=") - parmlne; - if ((size_t)indxnm_en < strlen(parmlne)){ + if ((size_t)indxnm_en < strlen(parmlne)) { indxvl_st = indxnm_en + 1; } } @@ -4502,10 +4651,13 @@ void util_parms_add_default(struct params_context *parameters int indx, dflt; dflt = TRUE; - for (indx = 0; indx < parameters->params_count; indx++) - { - if ( !strcmp(parameters->params_array[indx].param_name, parm_nm) ) dflt = FALSE; + for (indx = 0; indx < parameters->params_count; indx++) { + if ( !strcmp(parameters->params_array[indx].param_name, parm_nm) ) { + dflt = FALSE; + } + } + if (dflt == TRUE) { + util_parms_add(parameters, parm_nm, parm_vl); } - if (dflt == TRUE) util_parms_add(parameters, parm_nm, parm_vl); } \ No newline at end of file diff --git a/src/netcam.c b/src/netcam.c index f4abf5e4..dea162a2 100644 --- a/src/netcam.c +++ b/src/netcam.c @@ -109,14 +109,17 @@ void netcam_url_parse(struct url_t *parse_url, const char *text_url) regex_t pattbuf; regmatch_t matches[10]; - if (!strncmp(text_url, "file", 4)) + if (!strncmp(text_url, "file", 4)) { re = "(file)://(((.*):(.*))@)?([/:])?(:([0-9]+))?($|(/[^*]*))"; + } - if (!strncmp(text_url, "jpeg", 4)) + if (!strncmp(text_url, "jpeg", 4)) { re = "(jpeg)://(((.*):(.*))@)?([/:])?(:([0-9]+))?($|(/[^*]*))"; + } - if (!strncmp(text_url, "v4l2", 4)) + if (!strncmp(text_url, "v4l2", 4)) { re = "(v4l2)://(((.*):(.*))@)?([/:])?(:([0-9]+))?($|(/[^*]*))"; + } /* Note that log messages are commented out to avoid leaking info related * to user/host/pass etc. Keeing them in the code for easier debugging if @@ -170,14 +173,15 @@ void netcam_url_parse(struct url_t *parse_url, const char *text_url) } if (((!parse_url->port) && (parse_url->service)) || ((parse_url->port > 65535) && (parse_url->service))) { - if (!strcmp(parse_url->service, "http")) + if (!strcmp(parse_url->service, "http")) { parse_url->port = 80; - else if (!strcmp(parse_url->service, "ftp")) + } else if (!strcmp(parse_url->service, "ftp")) { parse_url->port = 21; - else if (!strcmp(parse_url->service, "rtmp")) + } else if (!strcmp(parse_url->service, "rtmp")) { parse_url->port = 1935; - else if (!strcmp(parse_url->service, "rtsp")) + } else if (!strcmp(parse_url->service, "rtsp")) { parse_url->port = 554; + } MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO, _("Using port number %d"),parse_url->port); } @@ -340,8 +344,9 @@ static void *netcam_handler_loop(void *arg) /* If FTP connection, attempt to re-connect to server. */ if (netcam->ftp) { close(netcam->ftp->control_file_desc); - if (ftp_connect(netcam) < 0) + if (ftp_connect(netcam) < 0) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO,_("Trying to re-connect")); + } } continue; } @@ -374,8 +379,9 @@ static void *netcam_handler_loop(void *arg) * us, we just continue. In either event, we clear * the start_capture flag set by the main loop. */ - if (!netcam->start_capture) + if (!netcam->start_capture) { pthread_cond_wait(&netcam->cap_cond, &netcam->mutex); + } netcam->start_capture = 0; @@ -430,7 +436,9 @@ void netcam_cleanup(netcam_context_ptr netcam, int init_retry_flag) { struct timespec waittime; - if (!netcam) return; + if (!netcam) { + return; + } /* * This 'lock' is just a bit of "defensive" programming. It should @@ -440,8 +448,9 @@ void netcam_cleanup(netcam_context_ptr netcam, int init_retry_flag) */ pthread_mutex_lock(&netcam->mutex); - if (netcam->cnt->netcam == NULL) + if (netcam->cnt->netcam == NULL) { return; + } /* * We set the netcam_context pointer in the motion main-loop context @@ -462,8 +471,9 @@ void netcam_cleanup(netcam_context_ptr netcam, int init_retry_flag) * netcam->mutex locked. */ - if (netcam->caps.streaming == NCS_UNSUPPORTED) + if (netcam->caps.streaming == NCS_UNSUPPORTED) { pthread_cond_signal(&netcam->cap_cond); + } /* @@ -562,8 +572,9 @@ int netcam_next(struct context *cnt, struct image_data *img_data) * Here we have some more "defensive programming". This check should * never be true, but if it is just return with a "fatal error". */ - if ((!cnt) || (!cnt->netcam)) + if ((!cnt) || (!cnt->netcam)) { return NETCAM_FATAL_ERROR; + } netcam = cnt->netcam; @@ -591,8 +602,9 @@ int netcam_next(struct context *cnt, struct image_data *img_data) * approach is to just return a NULL (failed) to the caller (an * error message has already been produced by the libjpeg routines). */ - if (setjmp(netcam->setjmp_buffer)) + if (setjmp(netcam->setjmp_buffer)) { return NETCAM_GENERAL_ERROR | NETCAM_JPEG_CONV_ERROR; + } /* If there was no error, process the latest image buffer. */ return netcam_proc_jpeg(netcam, img_data); @@ -659,11 +671,9 @@ int netcam_start(struct context *cnt) util_parms_add_default(netcam->parameters,"keepalive","off"); util_parms_add_default(netcam->parameters,"tolerant_check","off"); /*false*/ - for (indx = 0; indx < netcam->parameters->params_count; indx++) - { + for (indx = 0; indx < netcam->parameters->params_count; indx++) { if ( !strcmp(netcam->parameters->params_array[indx].param_name,"proxy") && - strcmp(netcam->parameters->params_array[indx].param_name,"NULL") ) - { + strcmp(netcam->parameters->params_array[indx].param_name,"NULL")) { netcam_url_parse(&url, netcam->parameters->params_array[indx].param_value); if (!url.host) { MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO @@ -706,8 +716,7 @@ int netcam_start(struct context *cnt) return -1; } - for (indx = 0; indx < netcam->parameters->params_count; indx++) - { + for (indx = 0; indx < netcam->parameters->params_count; indx++) { if (!strcmp(netcam->parameters->params_array[indx].param_name,"proxy")) { if (!strcmp(netcam->parameters->params_array[indx].param_name,"NULL")) { netcam->connect_host = url.host; @@ -773,7 +782,9 @@ int netcam_start(struct context *cnt) } netcam_url_free(&url); - if (retval < 0) return -1; + if (retval < 0) { + return -1; + } /* * We expect that, at this point, we should be positioned to read diff --git a/src/netcam_ftp.c b/src/netcam_ftp.c index fa1fe915..0b71b71c 100644 --- a/src/netcam_ftp.c +++ b/src/netcam_ftp.c @@ -55,14 +55,16 @@ ftp_context_pointer ftp_new_context(void) */ void ftp_free_context(ftp_context_pointer ctxt) { - if (ctxt == NULL) + if (ctxt == NULL) { return; + } free(ctxt->path); free(ctxt->user); free(ctxt->passwd); - if (ctxt->control_file_desc >= 0) + if (ctxt->control_file_desc >= 0) { close(ctxt->control_file_desc); + } free(ctxt); } @@ -86,32 +88,37 @@ static int ftp_parse_response(char *buf, int len) { int val = 0; - if (len < 3) + if (len < 3) { return -1; + } - if ((*buf >= '0') && (*buf <= '9')) + if ((*buf >= '0') && (*buf <= '9')) { val = val * 10 + (*buf - '0'); - else + } else { return 0; + } buf++; - if ((*buf >= '0') && (*buf <= '9')) + if ((*buf >= '0') && (*buf <= '9')) { val = val * 10 + (*buf - '0'); - else + } else { return 0; + } buf++; - if ((*buf >= '0') && (*buf <= '9')) + if ((*buf >= '0') && (*buf <= '9')) { val = val * 10 + (*buf - '0'); - else + } else { return 0; + } buf++; - if (*buf == '-') + if (*buf == '-') { return -val; + } return val; } @@ -133,17 +140,21 @@ static int ftp_get_more(ftp_context_pointer ctxt) int size; /* Validate that our context structure is valid. */ - if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) + if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) { return -1; + } - if ((ctxt->control_buffer_index < 0) || (ctxt->control_buffer_index > FTP_BUF_SIZE)) + if ((ctxt->control_buffer_index < 0) || (ctxt->control_buffer_index > FTP_BUF_SIZE)) { return -1; + } - if ((ctxt->control_buffer_used < 0) || (ctxt->control_buffer_used > FTP_BUF_SIZE)) + if ((ctxt->control_buffer_used < 0) || (ctxt->control_buffer_used > FTP_BUF_SIZE)) { return -1; + } - if (ctxt->control_buffer_index > ctxt->control_buffer_used) + if (ctxt->control_buffer_index > ctxt->control_buffer_used) { return -1; + } /* First pack the control buffer. */ if (ctxt->control_buffer_index > 0) { @@ -156,8 +167,9 @@ static int ftp_get_more(ftp_context_pointer ctxt) size = FTP_BUF_SIZE - ctxt->control_buffer_used; - if (size == 0) + if (size == 0) { return 0; + } /* Read the amount left on the control connection. */ if ((len = recv(ctxt->control_file_desc, @@ -191,8 +203,9 @@ static int ftp_get_response(ftp_context_pointer ctxt) int len; int res = -1, cur = -1; - if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) + if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) { return -1; + } get_more: /* @@ -201,12 +214,14 @@ static int ftp_get_response(ftp_context_pointer ctxt) */ len = ftp_get_more(ctxt); - if (len < 0) + if (len < 0) { return -1; + } - if ((ctxt->control_buffer_used == 0) && (len == 0)) + if ((ctxt->control_buffer_used == 0) && (len == 0)) { return -1; + } ptr = &ctxt->control_buffer[ctxt->control_buffer_index]; @@ -223,32 +238,38 @@ static int ftp_get_response(ftp_context_pointer ctxt) res = cur; ptr += 3; ctxt->control_buffer_answer = ptr - ctxt->control_buffer; - while ((ptr < end) && (*ptr != '\n')) + while ((ptr < end) && (*ptr != '\n')) { ptr++; + } - if (*ptr == '\n') + if (*ptr == '\n') { ptr++; + } - if (*ptr == '\r') + if (*ptr == '\r') { ptr++; + } break; } - while ((ptr < end) && (*ptr != '\n')) + while ((ptr < end) && (*ptr != '\n')) { ptr++; + } if (ptr >= end) { ctxt->control_buffer_index = ctxt->control_buffer_used; goto get_more; } - if (*ptr != '\r') + if (*ptr != '\r') { ptr++; + } } - if (res < 0) + if (res < 0) { goto get_more; + } ctxt->control_buffer_index = ptr - ctxt->control_buffer; @@ -267,10 +288,11 @@ static int ftp_send_user(ftp_context_pointer ctxt) int len; int res; - if (ctxt->user == NULL) + if (ctxt->user == NULL) { snprintf(buf, sizeof(buf), "USER anonymous\r\n"); - else + } else { snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user); + } buf[sizeof(buf) - 1] = 0; len = strlen(buf); @@ -293,10 +315,11 @@ static int ftp_send_passwd(ftp_context_pointer ctxt) int len; int res; - if (ctxt->passwd == NULL) + if (ctxt->passwd == NULL) { snprintf(buf, sizeof(buf), "PASS anonymous@\r\n"); - else + } else { snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd); + } buf[sizeof(buf) - 1] = 0; len = strlen(buf); @@ -326,8 +349,9 @@ static int ftp_quit(ftp_context_pointer ctxt) char buf[200]; int len, res; - if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) + if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) { return -1; + } snprintf(buf, sizeof(buf), "QUIT\r\n"); len = strlen(buf); @@ -360,22 +384,26 @@ int ftp_connect(netcam_context_ptr netcam) int res; int addrlen = sizeof (struct sockaddr_in); - if (netcam == NULL) + if (netcam == NULL) { return -1; + } ctxt = netcam->ftp; - if (ctxt == NULL) + if (ctxt == NULL) { return -1; + } - if (netcam->connect_host == NULL) + if (netcam->connect_host == NULL) { return -1; + } /* Do the blocking DNS query. */ port = netcam->connect_port; - if (port == 0) + if (port == 0) { port = 21; + } memset (&ctxt->ftp_address, 0, sizeof(ctxt->ftp_address)); @@ -510,12 +538,14 @@ static int ftp_get_connection(ftp_context_pointer ctxt) struct sockaddr_in data_address; unsigned int data_address_length; - if (ctxt == NULL) - return -1; + if (ctxt == NULL) { + return -1; + } /* Set up a socket for our data address. */ - if (ctxt->data_file_desc != -1) + if (ctxt->data_file_desc != -1) { close(ctxt->data_file_desc); + } memset (&data_address, 0, sizeof(data_address)); ctxt->data_file_desc = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -566,8 +596,9 @@ static int ftp_get_connection(ftp_context_pointer ctxt) /* Parse the IP address and port supplied by the server. */ cur = &ctxt->control_buffer[ctxt->control_buffer_answer]; - while (((*cur < '0') || (*cur > '9')) && *cur != '\0') + while (((*cur < '0') || (*cur > '9')) && *cur != '\0') { cur++; + } if (sscanf(cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5]) != 6) { @@ -579,15 +610,15 @@ static int ftp_get_connection(ftp_context_pointer ctxt) return -1; } - for (i = 0; i < 6; i++) + for (i = 0; i < 6; i++) { ad[i] = (unsigned char) (temp[i] & 0xff) ; + } memcpy (&((struct sockaddr_in *)&data_address)->sin_addr, &ad[0], 4); memcpy (&((struct sockaddr_in *)&data_address)->sin_port, &ad[4], 2); /* Now try to connect to the data port. */ - if (connect(ctxt->data_file_desc, (struct sockaddr *) &data_address, - data_address_length) < 0) { + if (connect(ctxt->data_file_desc, (struct sockaddr *) &data_address, data_address_length) < 0) { MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO,_("Failed to create a data connection")); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; @@ -605,8 +636,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) ((struct sockaddr_in *)&data_address)->sin_port = 0; /* Bind to the socket - should give us a unique port. */ - if (bind(ctxt->data_file_desc, (struct sockaddr *) &data_address, - data_address_length) < 0) { + if (bind(ctxt->data_file_desc, (struct sockaddr *) &data_address, data_address_length) < 0) { MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO,_("bind failed")); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; @@ -674,8 +704,9 @@ static int ftp_close_connection(ftp_context_pointer ctxt) fd_set rfd, efd; struct timeval tv; - if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) + if ((ctxt == NULL) || (ctxt->control_file_desc < 0)) { return -1; + } close(ctxt->data_file_desc); ctxt->data_file_desc = -1; @@ -728,14 +759,16 @@ int ftp_get_socket(ftp_context_pointer ctxt) int res, len; int acfd; - if ((ctxt == NULL) || (ctxt->path == NULL)) + if ((ctxt == NULL) || (ctxt->path == NULL)) { return -1; + } /* Set up the data connection. */ ctxt->data_file_desc = ftp_get_connection(ctxt); - if (ctxt->data_file_desc == -1) + if (ctxt->data_file_desc == -1) { return -1; + } /* Generate a "retrieve" command for the file. */ snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path); @@ -842,23 +875,28 @@ int ftp_send_type(ftp_context_pointer ctxt, char type) */ int ftp_read(ftp_context_pointer ctxt, void *dest, int len) { - if (ctxt == NULL) + if (ctxt == NULL) { return -1; + } - if (ctxt->data_file_desc < 0) + if (ctxt->data_file_desc < 0) { return 0; + } - if (dest == NULL) + if (dest == NULL) { return -1; + } - if (len <= 0) + if (len <= 0) { return 0; + } len = recv(ctxt->data_file_desc, dest, len, 0); if (len <= 0) { - if (len < 0) + if (len < 0) { MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO,_("recv failed in ftp_read")); + } ftp_close_connection(ctxt); } @@ -878,8 +916,9 @@ int ftp_read(ftp_context_pointer ctxt, void *dest, int len) */ int ftp_close(ftp_context_pointer ctxt) { - if (ctxt == NULL) + if (ctxt == NULL) { return -1; + } if (ctxt->data_file_desc >= 0) { close(ctxt->data_file_desc); @@ -925,8 +964,9 @@ static int netcam_read_ftp_jpeg(netcam_context_ptr netcam) netcam_check_buffsize(buffer, FTP_BUF_SIZE); /* Do the read */ - if ((len = ftp_read(netcam->ftp, buffer->ptr + buffer->used, FTP_BUF_SIZE)) < 0) + if ((len = ftp_read(netcam->ftp, buffer->ptr + buffer->used, FTP_BUF_SIZE)) < 0) { return -1; + } buffer->used += len; } while (len > 0); @@ -942,15 +982,16 @@ int netcam_setup_ftp(netcam_context_ptr netcam, struct url_t *url) const char *ptr; - if ((netcam->ftp = ftp_new_context()) == NULL) + if ((netcam->ftp = ftp_new_context()) == NULL) { return -1; + } /* * We copy the strings out of the url structure into the ftp_context * structure. By setting url->{string} to NULL we effectively "take * ownership" of the string away from the URL (i.e. it won't be freed * when we cleanup the url structure later). */ - if (strcmp(url->path,"/")){ + if (strcmp(url->path,"/")) { netcam->ftp->path = mystrdup(url->path + 1); } else { netcam->ftp->path = mystrdup(url->path); diff --git a/src/netcam_http.c b/src/netcam_http.c index 6bab3343..938b2db9 100644 --- a/src/netcam_http.c +++ b/src/netcam_http.c @@ -96,9 +96,10 @@ static long netcam_check_content_length(char *header) * we were able to recognize the header section and the * number we might as well try to use it. */ - if (length > 0) + if (length > 0) { MOTION_LOG(WRN, TYPE_NETCAM, NO_ERRNO ,_("malformed token Content-Length but value %ld"), length); + } } MOTION_LOG(DBG, TYPE_NETCAM, NO_ERRNO,_("Content-Length %ld"), length); @@ -124,8 +125,9 @@ static int netcam_check_keepalive(char *header) { char *content_type = NULL; - if (!header_process(header, "Keep-Alive", http_process_type, &content_type)) + if (!header_process(header, "Keep-Alive", http_process_type, &content_type)) { return -1; + } /* We do not detect the second field or other case mixes at present. */ free(content_type); @@ -152,11 +154,13 @@ static int netcam_check_close(char *header) char *type = NULL; int ret = -1; - if (!header_process(header, "Connection", http_process_type, &type)) + if (!header_process(header, "Connection", http_process_type, &type)) { return -1; + } - if (!strcmp(type, "close")) /* strcmp returns 0 for match. */ + if (!strcmp(type, "close")) { /* strcmp returns 0 for match. */ ret = 1; + } free(type); @@ -185,8 +189,9 @@ static int netcam_check_content_type(char *header) char *content_type = NULL; int ret; - if (!header_process(header, "Content-type", http_process_type, &content_type)) + if (!header_process(header, "Content-type", http_process_type, &content_type)) { return -1; + } MOTION_LOG(DBG, TYPE_NETCAM, NO_ERRNO,_("Content-type %s"), content_type); @@ -224,8 +229,9 @@ int netcam_read_next_header(netcam_context_ptr netcam) char *header; /* Return if not connected */ - if (netcam->sock == -1) + if (netcam->sock == -1) { return -1; + } /* * We are expecting a header which *must* contain a mime-type of * image/jpeg, and *might* contain a Content-Length. @@ -263,8 +269,9 @@ int netcam_read_next_header(netcam_context_ptr netcam) retval = (strstr(header, netcam->boundary) == NULL); free(header); - if (!retval) + if (!retval) { break; + } } } @@ -277,8 +284,9 @@ int netcam_read_next_header(netcam_context_ptr netcam) return -1; } - if (*header == 0) + if (*header == 0) { break; + } if ((retval = netcam_check_content_type(header)) >= 0) { if (retval != 1) { @@ -407,8 +415,9 @@ int netcam_read_first_header(netcam_context_ptr netcam) continue; } - if (*header == 0) /* Blank line received */ + if (*header == 0) { /* Blank line received */ break; + } /* Check if this line is the content type. */ if ((ret = netcam_check_content_type(header)) >= 0) { @@ -422,12 +431,13 @@ int netcam_read_first_header(netcam_context_ptr netcam) */ switch (ret) { case 1: /* Not streaming */ - if (netcam->connect_keepalive) + if (netcam->connect_keepalive) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO ,_("Non-streaming camera (keep-alive set)")); - else + } else { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO ,_("Non-streaming camera (keep-alive not set)")); + } netcam->caps.streaming = NCS_UNSUPPORTED; break; @@ -618,8 +628,9 @@ int netcam_read_first_header(netcam_context_ptr netcam) void netcam_disconnect(netcam_context_ptr netcam) { if (netcam->sock > 0) { - if (close(netcam->sock) < 0) + if (close(netcam->sock) < 0) { MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, _("disconnect")); + } netcam->sock = -1; } @@ -659,10 +670,11 @@ int netcam_connect(netcam_context_ptr netcam, int err_flag) /* Lookup the hostname given in the netcam URL. */ if ((ret = getaddrinfo(netcam->connect_host, port, NULL, &ai)) != 0) { - if (!err_flag) + if (!err_flag) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("getaddrinfo() failed (%s): %s") ,netcam->connect_host, gai_strerror(ret)); + } MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO,_("disconnecting netcam (1)")); @@ -755,9 +767,10 @@ int netcam_connect(netcam_context_ptr netcam, int err_flag) /* If the connect failed with anything except EINPROGRESS, error. */ if ((ret < 0) && (back_err != EINPROGRESS)) { - if (!err_flag) + if (!err_flag) { MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO ,_("connect() failed (%d)"), back_err); + } MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO,_("disconnecting netcam (4)")); @@ -773,8 +786,9 @@ int netcam_connect(netcam_context_ptr netcam, int err_flag) ret = select(FD_SETSIZE, NULL, &fd_w, NULL, &selecttime); if (ret == 0) { /* 0 means timeout. */ - if (!err_flag) + if (!err_flag) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("timeout on connect()")); + } MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO,_("disconnecting netcam (2)")); @@ -797,8 +811,9 @@ int netcam_connect(netcam_context_ptr netcam, int err_flag) /* If the return code is anything except 0, error on connect. */ if (ret) { - if (!err_flag) + if (!err_flag) { MOTION_LOG(ERR, TYPE_NETCAM, SHOW_ERRNO, _("connect returned error")); + } MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO,_("disconnecting netcam (3)")); @@ -818,14 +833,16 @@ void netcam_check_buffsize(netcam_buff_ptr buff, size_t numbytes) int real_alloc; int new_size; - if ((buff->size - buff->used) >= numbytes) + if ((buff->size - buff->used) >= numbytes) { return; + } min_size_to_alloc = numbytes - (buff->size - buff->used); real_alloc = ((min_size_to_alloc / NETCAM_BUFFSIZE) * NETCAM_BUFFSIZE); - if ((min_size_to_alloc - real_alloc) > 0) + if ((min_size_to_alloc - real_alloc) > 0) { real_alloc += NETCAM_BUFFSIZE; + } new_size = buff->size + real_alloc; @@ -849,8 +866,9 @@ void netcam_image_read_complete(netcam_context_ptr netcam) struct timeval curtime; netcam_buff *xchg; - if (gettimeofday(&curtime, NULL) < 0) + if (gettimeofday(&curtime, NULL) < 0) { MOTION_LOG(WRN, TYPE_NETCAM, SHOW_ERRNO, "gettimeofday"); + } netcam->receiving->image_time = curtime; /* @@ -963,10 +981,11 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) /* Assure the target buffer is empty. */ buffer->used = 0; /* Prepare for read loop. */ - if (buffer->content_length != 0) + if (buffer->content_length != 0) { remaining = buffer->content_length; - else + } else { remaining = 9999999; + } /* Now read in the data. */ while (remaining) { @@ -974,8 +993,9 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) if (netcam->response->buffer_left <= 0) { retval = rbuf_read_bufferful(netcam); - if (retval <= 0) + if (retval <= 0) { break; + } netcam->response->buffer_left = retval; netcam->response->buffer_pos = netcam->response->buffer; @@ -999,23 +1019,27 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) * First a quick check if the string *might* * be in the current buffer. */ - if (rlen > remaining) + if (rlen > remaining) { rlen = remaining; + } - if (remaining < netcam->boundary_length) + if (remaining < netcam->boundary_length) { break; + } - if ((ptr = memchr(rptr, *bptr, rlen)) == NULL) + if ((ptr = memchr(rptr, *bptr, rlen)) == NULL) { /* Boundary not here (normal path) */ break; + } /* * At least the first char was found in the * buffer - check for the rest. */ rem = rlen - (ptr - rptr); for (ix = 1; (ix < rem) && (ix < netcam->boundary_length); ix++) { - if (ptr[ix] != bptr[ix]) + if (ptr[ix] != bptr[ix]) { break; + } } if ((ix != netcam->boundary_length) && (ix != rem)) { @@ -1027,9 +1051,10 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) rptr += ix; rlen -= ix; - if (rlen <= 0) + if (rlen <= 0) { /* boundary not in buffer - go copy out */ break; + } /* * Not yet decided - continue * through input. @@ -1044,8 +1069,9 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) * exit the main loop. */ if (ix == netcam->boundary_length) { - if ((ptr - netcam->response->buffer) < (int) remaining) + if ((ptr - netcam->response->buffer) < (int) remaining) { remaining = ptr - netcam->response->buffer; + } /* Go copy everything up to boundary. */ break; @@ -1118,8 +1144,9 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) } /* End of while(1) input buffer search. */ /* !bptr shows we're processing split boundary. */ - if (!bptr) + if (!bptr) { continue; + } } /* end of if (bptr) */ /* boundary string not present, so just write out as much data as possible. */ @@ -1198,8 +1225,9 @@ static int netcam_http_request(netcam_context_ptr netcam) break;; } - if (netcam_read_first_header(netcam) >= 0) + if (netcam_read_first_header(netcam) >= 0) { break; + } MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO,_("Error reading first header - re-trying")); } @@ -1254,10 +1282,11 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) * for the camera. We assume the first of these has a higher * relevance. */ - if (cnt->conf.netcam_userpass) + if (cnt->conf.netcam_userpass) { ptr = cnt->conf.netcam_userpass; - else + } else { ptr = url->userpass; + } /* motion_base64_encode needs up to 3 additional chars. */ if (ptr) { @@ -1346,10 +1375,11 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) * which is read-only. */ - if (netcam->connect_keepalive) + if (netcam->connect_keepalive) { ix += strlen(connect_req_keepalive); - else + } else { ix += strlen(connect_req_close); + } /* @@ -1357,10 +1387,11 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) * If the configuration is anything other than 1.1, use 1.0 * as a default. This avoids a chance of being left with none. */ - if (netcam->connect_http_11 == TRUE) + if (netcam->connect_http_11 == TRUE) { connect_req = connect_req_http11; - else + } else { connect_req = connect_req_http10; + } /* * Now that we know how much space we need, we can allocate space @@ -1373,10 +1404,11 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) sprintf(netcam->connect_request, connect_req, ptr, netcam->connect_host); - if (netcam->connect_keepalive) + if (netcam->connect_keepalive) { strcat(netcam->connect_request, connect_req_keepalive); - else + } else { strcat(netcam->connect_request, connect_req_close); + } if (userpass) { @@ -1424,14 +1456,16 @@ int netcam_setup_html(netcam_context_ptr netcam, struct url_t *url) * This netcam is http-based, so build the required URL and * structures, like the connection-string and so on. */ - if (netcam_http_build_url(netcam, url) < 0) + if (netcam_http_build_url(netcam, url) < 0) { return -1; + } /* * Then we will send our http request and get headers. */ - if (netcam_http_request(netcam) < 0) - return -1; + if (netcam_http_request(netcam) < 0) { + return -1; + } /* * If this is a streaming camera, we need to position just @@ -1469,8 +1503,9 @@ static int netcam_mjpg_buffer_refill(netcam_context_ptr netcam) { int retval; - if (netcam->response->buffer_left > 0) + if (netcam->response->buffer_left > 0) { return netcam->response->buffer_left; + } while (1) { retval = rbuf_read_bufferful(netcam); @@ -1485,8 +1520,9 @@ static int netcam_mjpg_buffer_refill(netcam_context_ptr netcam) } } - if (retval > 0) + if (retval > 0) { break; + } } netcam->response->buffer_left = retval; @@ -1546,8 +1582,9 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) /* Assure the target buffer is empty. */ buffer->used = 0; - if (netcam_mjpg_buffer_refill(netcam) < 0) + if (netcam_mjpg_buffer_refill(netcam) < 0) { return -1; + } /* Loop until we have a complete JPG. */ while (1) { @@ -1564,8 +1601,9 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) /* If we don't have received a full header, refill our buffer. */ if (read_bytes < sizeof(mh)) { - if (netcam_mjpg_buffer_refill(netcam) < 0) + if (netcam_mjpg_buffer_refill(netcam) < 0) { return -1; + } } } @@ -1577,8 +1615,9 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) * We shall reconnect to restart the stream, and get a chance * to resync. */ - if (netcam_http_request(netcam) < 0) + if (netcam_http_request(netcam) < 0) { return -1; /* We lost the cam... bail out. */ + } /* Even there, we need to resync. */ buffer->used = 0; continue ; @@ -1599,8 +1638,9 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) if (retval < (int) (mh.mh_chunksize - read_bytes)) { /* MOTION_LOG(EMG, TYPE_NETCAM, NO_ERRNO, "Chunk incomplete, going to refill."); */ - if (netcam_mjpg_buffer_refill(netcam) < 0) + if (netcam_mjpg_buffer_refill(netcam) < 0) { return -1; + } } } @@ -1648,12 +1688,14 @@ int netcam_setup_mjpg(netcam_context_ptr netcam, struct url_t *url) * This netcam is http-based, so build the required URL and * structures, like the connection-string and so on. */ - if (netcam_http_build_url(netcam, url) != 0) + if (netcam_http_build_url(netcam, url) != 0) { return -1; + } /* Then we will send our http request and get headers. */ - if (netcam_http_request(netcam) < 0) + if (netcam_http_request(netcam) < 0) { return -1; + } /* We have a special type of streaming camera. */ netcam->caps.streaming = NCS_BLOCK; @@ -1728,8 +1770,9 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) ,_("processing new file image - st_mtime %d"), netcam->file->last_st_mtime); /* Assure there's enough room in the buffer. */ - while (buffer->size < (size_t)statbuf.st_size) + while (buffer->size < (size_t)statbuf.st_size) { netcam_check_buffsize(buffer, statbuf.st_size); + } /* Do the read */ @@ -1765,8 +1808,9 @@ tfile_context *file_new_context(void) void file_free_context(tfile_context* ctxt) { - if (ctxt == NULL) + if (ctxt == NULL) { return; + } free(ctxt->path); free(ctxt); @@ -1775,8 +1819,9 @@ void file_free_context(tfile_context* ctxt) int netcam_setup_file(netcam_context_ptr netcam, struct url_t *url) { - if ((netcam->file = file_new_context()) == NULL) + if ((netcam->file = file_new_context()) == NULL) { return -1; + } /* * We copy the strings out of the url structure into the ftp_context @@ -1819,16 +1864,19 @@ ssize_t netcam_recv(netcam_context_ptr netcam, void *buffptr, size_t buffsize) fd_set fd_r; struct timeval selecttime; - if (netcam->sock < 0) + if (netcam->sock < 0) { return -1; /* We are not connected, it's impossible to receive data. */ + } FD_ZERO(&fd_r); FD_SET(netcam->sock, &fd_r); selecttime = netcam->timeout; retval = select(FD_SETSIZE, &fd_r, NULL, NULL, &selecttime); - if (retval == 0) /* 0 means timeout */ + if (retval == 0) { + /* 0 means timeout */ return -1; + } return recv(netcam->sock, buffptr, buffsize, 0); } diff --git a/src/netcam_jpeg.c b/src/netcam_jpeg.c index e6fff5e3..6ed9d514 100644 --- a/src/netcam_jpeg.c +++ b/src/netcam_jpeg.c @@ -144,11 +144,12 @@ static void netcam_memory_src(j_decompress_ptr cinfo, char *data, int length) { netcam_src_ptr src; - if (cinfo->src == NULL) + if (cinfo->src == NULL) { cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (netcam_source_mgr)); + } src = (netcam_src_ptr)cinfo->src; @@ -228,8 +229,9 @@ static void netcam_output_message(j_common_ptr cinfo) * care about. */ if ((cinfo->err->msg_code != JWRN_EXTRANEOUS_DATA) && - (cinfo->err->msg_code == JWRN_NOT_SEQUENTIAL) && (!netcam->netcam_tolerant_check)) + (cinfo->err->msg_code == JWRN_NOT_SEQUENTIAL) && (!netcam->netcam_tolerant_check)) { netcam->jpeg_error |= 2; /* Set flag to show problem */ + } /* * Format the message according to library standards. @@ -340,8 +342,9 @@ static int netcam_init_jpeg(netcam_context_ptr netcam, j_decompress_ptr cinfo) /* Start the decompressor. */ jpeg_start_decompress(cinfo); - if (netcam->jpeg_error) + if (netcam->jpeg_error) { MOTION_LOG(DBG, TYPE_NETCAM, NO_ERRNO,_("jpeg_error %d"), netcam->jpeg_error); + } return netcam->jpeg_error; } @@ -419,8 +422,9 @@ static int netcam_image_conv(netcam_context_ptr netcam, rotate_map(netcam->cnt, img_data); - if (netcam->jpeg_error) + if (netcam->jpeg_error) { MOTION_LOG(DBG, TYPE_NETCAM, NO_ERRNO,_("jpeg_error %d"), netcam->jpeg_error); + } return netcam->jpeg_error; } diff --git a/src/netcam_rtsp.c b/src/netcam_rtsp.c index 493c8dc5..e463cf60 100644 --- a/src/netcam_rtsp.c +++ b/src/netcam_rtsp.c @@ -40,7 +40,9 @@ static int netcam_rtsp_check_pixfmt(struct rtsp_context *rtsp_data) retcd = -1; if (((enum AVPixelFormat)rtsp_data->frame->format == MY_PIX_FMT_YUV420P) || - ((enum AVPixelFormat)rtsp_data->frame->format == MY_PIX_FMT_YUVJ420P)) retcd = 0; + ((enum AVPixelFormat)rtsp_data->frame->format == MY_PIX_FMT_YUVJ420P)) { + retcd = 0; + } return retcd; @@ -51,7 +53,7 @@ static void netcam_rtsp_pktarray_free(struct rtsp_context *rtsp_data) int indx; pthread_mutex_lock(&rtsp_data->mutex_pktarray); - if (rtsp_data->pktarray_size > 0){ + if (rtsp_data->pktarray_size > 0) { for(indx = 0; indx < rtsp_data->pktarray_size; indx++) { if (rtsp_data->pktarray[indx].packet.data != NULL) { my_packet_unref(rtsp_data->pktarray[indx].packet); @@ -82,14 +84,30 @@ static void netcam_rtsp_null_context(struct rtsp_context *rtsp_data) static void netcam_rtsp_close_context(struct rtsp_context *rtsp_data) { - if (rtsp_data->swsctx != NULL) sws_freeContext(rtsp_data->swsctx); - if (rtsp_data->swsframe_in != NULL) my_frame_free(rtsp_data->swsframe_in); - if (rtsp_data->swsframe_out != NULL) my_frame_free(rtsp_data->swsframe_out); - if (rtsp_data->frame != NULL) my_frame_free(rtsp_data->frame); - if (rtsp_data->pktarray != NULL) netcam_rtsp_pktarray_free(rtsp_data); - if (rtsp_data->codec_context != NULL) my_avcodec_close(rtsp_data->codec_context); - if (rtsp_data->format_context != NULL) avformat_close_input(&rtsp_data->format_context); - if (rtsp_data->transfer_format != NULL) avformat_close_input(&rtsp_data->transfer_format); + if (rtsp_data->swsctx != NULL) { + sws_freeContext(rtsp_data->swsctx); + } + if (rtsp_data->swsframe_in != NULL) { + my_frame_free(rtsp_data->swsframe_in); + } + if (rtsp_data->swsframe_out != NULL) { + my_frame_free(rtsp_data->swsframe_out); + } + if (rtsp_data->frame != NULL) { + my_frame_free(rtsp_data->frame); + } + if (rtsp_data->pktarray != NULL) { + netcam_rtsp_pktarray_free(rtsp_data); + } + if (rtsp_data->codec_context != NULL) { + my_avcodec_close(rtsp_data->codec_context); + } + if (rtsp_data->format_context != NULL) { + avformat_close_input(&rtsp_data->format_context); + } + if (rtsp_data->transfer_format != NULL) { + avformat_close_input(&rtsp_data->transfer_format); + } #if (MYFFVER >= 57083) if (rtsp_data->hw_device_ctx != NULL) av_buffer_unref(&rtsp_data->hw_device_ctx); #endif @@ -122,7 +140,7 @@ static void netcam_rtsp_pktarray_resize(struct context *cnt, int is_highres) struct packet_item *tmp; int newsize; - if (is_highres){ + if (is_highres) { idnbr_last = cnt->imgs.image_ring[cnt->imgs.image_ring_out].idnbr_high; idnbr_first = cnt->imgs.image_ring[cnt->imgs.image_ring_in].idnbr_high; rtsp_data = cnt->rtsp_high; @@ -132,17 +150,21 @@ static void netcam_rtsp_pktarray_resize(struct context *cnt, int is_highres) rtsp_data = cnt->rtsp; } - if (!rtsp_data->passthrough) return; + if (!rtsp_data->passthrough) { + return; + } /* The 30 is arbitrary */ /* Double the size plus double last diff so we don't catch our tail */ newsize =((idnbr_first - idnbr_last) * 2 ) + ((rtsp_data->idnbr - idnbr_last ) * 2); - if (newsize < 30) newsize = 30; + if (newsize < 30) { + newsize = 30; + } pthread_mutex_lock(&rtsp_data->mutex_pktarray); - if ((rtsp_data->pktarray_size < newsize) || (rtsp_data->pktarray_size < 30)){ + if ((rtsp_data->pktarray_size < newsize) || (rtsp_data->pktarray_size < 30)) { tmp = mymalloc(newsize * sizeof(struct packet_item)); - if (rtsp_data->pktarray_size > 0 ){ + if (rtsp_data->pktarray_size > 0 ) { memcpy(tmp, rtsp_data->pktarray, sizeof(struct packet_item) * rtsp_data->pktarray_size); } for(indx = rtsp_data->pktarray_size; indx < newsize; indx++) { @@ -154,7 +176,9 @@ static void netcam_rtsp_pktarray_resize(struct context *cnt, int is_highres) tmp[indx].iswritten = FALSE; } - if (rtsp_data->pktarray != NULL) free(rtsp_data->pktarray); + if (rtsp_data->pktarray != NULL) { + free(rtsp_data->pktarray); + } rtsp_data->pktarray = tmp; rtsp_data->pktarray_size = newsize; @@ -174,13 +198,13 @@ static void netcam_rtsp_pktarray_add(struct rtsp_context *rtsp_data) pthread_mutex_lock(&rtsp_data->mutex_pktarray); - if (rtsp_data->pktarray_size == 0){ + if (rtsp_data->pktarray_size == 0) { pthread_mutex_unlock(&rtsp_data->mutex_pktarray); return; } /* Recall pktarray_size is one based but pktarray is zero based */ - if (rtsp_data->pktarray_index == (rtsp_data->pktarray_size-1) ){ + if (rtsp_data->pktarray_index == (rtsp_data->pktarray_size-1) ) { indx_next = 0; } else { indx_next = rtsp_data->pktarray_index + 1; @@ -226,8 +250,8 @@ static int netcam_decode_sw(struct rtsp_context *rtsp_data) char errstr[128]; retcd = avcodec_receive_frame(rtsp_data->codec_context, rtsp_data->frame); - if ((rtsp_data->interrupted) || (rtsp_data->finish) || (retcd < 0) ){ - if (retcd == AVERROR(EAGAIN)){ + if ((rtsp_data->interrupted) || (rtsp_data->finish) || (retcd < 0) ) { + if (retcd == AVERROR(EAGAIN)) { retcd = 0; } else if (retcd == AVERROR_INVALIDDATA) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO @@ -252,7 +276,9 @@ static int netcam_decode_sw(struct rtsp_context *rtsp_data) char errstr[128]; retcd = avcodec_decode_video2(rtsp_data->codec_context, rtsp_data->frame, &check, &rtsp_data->packet_recv); - if ((rtsp_data->interrupted) || (rtsp_data->finish)) return -1; + if ((rtsp_data->interrupted) || (rtsp_data->finish)) { + return -1; + } if (retcd == AVERROR_INVALIDDATA) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO, _("Ignoring packet with invalid data")); @@ -265,7 +291,9 @@ static int netcam_decode_sw(struct rtsp_context *rtsp_data) return -1; } - if (check == 0 || retcd == 0) return 0; + if (check == 0 || retcd == 0) { + return 0; + } return 1; @@ -284,8 +312,8 @@ static int netcam_decode_vaapi(struct rtsp_context *rtsp_data) hw_frame = my_frame_alloc(); retcd = avcodec_receive_frame(rtsp_data->codec_context, hw_frame); - if ((rtsp_data->interrupted) || (rtsp_data->finish) || (retcd < 0) ){ - if (retcd == AVERROR(EAGAIN)){ + if ((rtsp_data->interrupted) || (rtsp_data->finish) || (retcd < 0) ) { + if (retcd == AVERROR(EAGAIN)) { retcd = 0; } else if (retcd == AVERROR_INVALIDDATA) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO @@ -396,16 +424,21 @@ static int netcam_rtsp_decode_video(struct rtsp_context *rtsp_data) * We should consider adding a maximum count of these errors and reset every time * we get a good image. */ - if (rtsp_data->finish) return 0; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return 0; + } retcd = avcodec_send_packet(rtsp_data->codec_context, &rtsp_data->packet_recv); - if ((rtsp_data->interrupted) || (rtsp_data->finish)) return -1; + if ((rtsp_data->interrupted) || (rtsp_data->finish)) { + return -1; + } if (retcd == AVERROR_INVALIDDATA) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("Ignoring packet with invalid data")); return 0; } - if (retcd < 0 && retcd != AVERROR_EOF){ + if (retcd < 0 && retcd != AVERROR_EOF) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("Error sending packet to codec: %s"), errstr); @@ -426,9 +459,11 @@ static int netcam_rtsp_decode_video(struct rtsp_context *rtsp_data) int retcd; - if (rtsp_data->finish) return 0; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + return 0; /* This just speeds up the shutdown time */ + } - if (!strcasecmp(rtsp_data->decoder_nm,"vaapi")){ + if (!strcasecmp(rtsp_data->decoder_nm,"vaapi")) { retcd = netcam_decode_vaapi(rtsp_data); } else if (!strcasecmp(rtsp_data->decoder_nm,"cuda")) { retcd = netcam_decode_cuda(rtsp_data); @@ -448,10 +483,15 @@ static int netcam_rtsp_decode_packet(struct rtsp_context *rtsp_data) int frame_size; int retcd; - if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return -1; + } retcd = netcam_rtsp_decode_video(rtsp_data); - if (retcd <= 0) return retcd; + if (retcd <= 0) { + return retcd; + } frame_size = my_image_get_buffer_size((enum AVPixelFormat) rtsp_data->frame->format ,rtsp_data->frame->width @@ -483,7 +523,9 @@ static void netcam_hwdecoders(struct rtsp_context *rtsp_data) #if ( MYFFVER >= 57083) /* High Res pass through does not decode images into frames*/ - if (rtsp_data->high_resolution && rtsp_data->passthrough) return; + if (rtsp_data->high_resolution && rtsp_data->passthrough) { + return; + } if ((rtsp_data->hw_type == AV_HWDEVICE_TYPE_NONE) && (rtsp_data->first_image)) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO @@ -506,7 +548,7 @@ static void netcam_hwdecoders(struct rtsp_context *rtsp_data) return; #else - if (strcasecmp(rtsp_data->decoder_nm,"NULL")){ + if (strcasecmp(rtsp_data->decoder_nm,"NULL")) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: netcam_decoder %s disabled.") , rtsp_data->cameratype, rtsp_data->decoder_nm); @@ -526,7 +568,9 @@ static enum AVPixelFormat netcam_getfmt_vaapi(AVCodecContext *avctx, const enum (void)avctx; for (p = pix_fmts; *p != -1; p++) { - if (*p == AV_PIX_FMT_VAAPI) return *p; + if (*p == AV_PIX_FMT_VAAPI) { + return *p; + } } MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO,_("Failed to get vaapi pix format")); @@ -563,11 +607,11 @@ static void netcam_rtsp_decoder_error(struct rtsp_context *rtsp_data, int retcd, char errstr[128]; int indx; - if (rtsp_data->interrupted){ + if (rtsp_data->interrupted) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Interrupted."),rtsp_data->cameratype); } else { - if (retcd < 0){ + if (retcd < 0) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: %s: %s") @@ -578,7 +622,7 @@ static void netcam_rtsp_decoder_error(struct rtsp_context *rtsp_data, int retcd, } } - if (strcasecmp(rtsp_data->decoder_nm,"NULL")){ + if (strcasecmp(rtsp_data->decoder_nm,"NULL")) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO ,_("%s: Decoder %s did not work.") ,rtsp_data->cameratype, rtsp_data->decoder_nm); @@ -586,9 +630,8 @@ static void netcam_rtsp_decoder_error(struct rtsp_context *rtsp_data, int retcd, ,_("%s: Ignoring and removing the user requested decoder %s") ,rtsp_data->cameratype, rtsp_data->decoder_nm); - for (indx = 0; indx < rtsp_data->parameters->params_count; indx++) - { - if ( !strcmp(rtsp_data->parameters->params_array[indx].param_name,"decoder") ){ + for (indx = 0; indx < rtsp_data->parameters->params_count; indx++) { + if ( !strcmp(rtsp_data->parameters->params_array[indx].param_name,"decoder") ) { free(rtsp_data->parameters->params_array[indx].param_value); rtsp_data->parameters->params_array[indx].param_value = mymalloc(5); snprintf(rtsp_data->decoder_nm, 5, "%s","NULL"); @@ -614,7 +657,7 @@ static int netcam_init_vaapi(struct rtsp_context *rtsp_data) ,_("%s: Initializing vaapi decoder"),rtsp_data->cameratype); rtsp_data->hw_type = av_hwdevice_find_type_by_name("vaapi"); - if (rtsp_data->hw_type == AV_HWDEVICE_TYPE_NONE){ + if (rtsp_data->hw_type == AV_HWDEVICE_TYPE_NONE) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO,_("%s: Unable to find vaapi hw device") , rtsp_data->cameratype); netcam_rtsp_decoder_error(rtsp_data, 0, "av_hwdevice"); @@ -622,7 +665,7 @@ static int netcam_init_vaapi(struct rtsp_context *rtsp_data) } rtsp_data->codec_context = avcodec_alloc_context3(rtsp_data->decoder); - if ((rtsp_data->codec_context == NULL) || (rtsp_data->interrupted)){ + if ((rtsp_data->codec_context == NULL) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, 0, "avcodec_alloc_context3"); return -1; } @@ -642,7 +685,7 @@ static int netcam_init_vaapi(struct rtsp_context *rtsp_data) AV_HWACCEL_FLAG_IGNORE_LEVEL; retcd = av_hwdevice_ctx_create(&rtsp_data->hw_device_ctx, rtsp_data->hw_type, NULL, NULL, 0); - if (retcd < 0){ + if (retcd < 0) { netcam_rtsp_decoder_error(rtsp_data, retcd, "hwctx"); return -1; } @@ -719,7 +762,7 @@ static int netcam_init_swdecoder(struct rtsp_context *rtsp_data) MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Initializing decoder"),rtsp_data->cameratype); - if (strcasecmp(rtsp_data->decoder_nm,"NULL")){ + if (strcasecmp(rtsp_data->decoder_nm,"NULL")) { rtsp_data->decoder = avcodec_find_decoder_by_name(rtsp_data->decoder_nm); if (rtsp_data->decoder == NULL) { netcam_rtsp_decoder_error(rtsp_data, 0, "avcodec_find_decoder_by_name"); @@ -731,13 +774,13 @@ static int netcam_init_swdecoder(struct rtsp_context *rtsp_data) if (rtsp_data->decoder == NULL) { rtsp_data->decoder = avcodec_find_decoder(rtsp_data->strm->codecpar->codec_id); } - if ((rtsp_data->decoder == NULL) || (rtsp_data->interrupted)){ + if ((rtsp_data->decoder == NULL) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, 0, "avcodec_find_decoder"); return -1; } rtsp_data->codec_context = avcodec_alloc_context3(rtsp_data->decoder); - if ((rtsp_data->codec_context == NULL) || (rtsp_data->interrupted)){ + if ((rtsp_data->codec_context == NULL) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, 0, "avcodec_alloc_context3"); return -1; } @@ -761,7 +804,7 @@ static int netcam_init_swdecoder(struct rtsp_context *rtsp_data) return -1; } retcd = avcodec_open2(rtsp_data->codec_context, rtsp_data->decoder, NULL); - if ((retcd < 0) || (rtsp_data->interrupted)){ + if ((retcd < 0) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, retcd, "avcodec_open2"); return -1; } @@ -775,34 +818,37 @@ static int netcam_rtsp_open_codec(struct rtsp_context *rtsp_data) #if ( MYFFVER >= 57041) int retcd; - if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return -1; + } netcam_hwdecoders(rtsp_data); rtsp_data->decoder=NULL; retcd = av_find_best_stream(rtsp_data->format_context , AVMEDIA_TYPE_VIDEO, -1, -1, &rtsp_data->decoder, 0); - if ((retcd < 0) || (rtsp_data->interrupted)){ + if ((retcd < 0) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, retcd, "av_find_best_stream"); return -1; } rtsp_data->video_stream_index = retcd; rtsp_data->strm = rtsp_data->format_context->streams[rtsp_data->video_stream_index]; - if (!strcasecmp(rtsp_data->decoder_nm,"vaapi")){ + if (!strcasecmp(rtsp_data->decoder_nm,"vaapi")) { retcd = netcam_init_vaapi(rtsp_data); } else if (!strcasecmp(rtsp_data->decoder_nm,"cuda")){ retcd = netcam_init_cuda(rtsp_data); } else { retcd = netcam_init_swdecoder(rtsp_data); } - if ((retcd < 0) || (rtsp_data->interrupted)){ + if ((retcd < 0) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, retcd, "initdecoder"); return -1; } retcd = avcodec_open2(rtsp_data->codec_context,rtsp_data->decoder, NULL); - if ((retcd < 0) || (rtsp_data->interrupted)){ + if ((retcd < 0) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, retcd, "avcodec_open2"); return -1; } @@ -815,13 +861,16 @@ static int netcam_rtsp_open_codec(struct rtsp_context *rtsp_data) int retcd; - if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return -1; + } netcam_hwdecoders(rtsp_data); rtsp_data->decoder = NULL; retcd = av_find_best_stream(rtsp_data->format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); - if ((retcd < 0) || (rtsp_data->interrupted)){ + if ((retcd < 0) || (rtsp_data->interrupted)) { netcam_rtsp_decoder_error(rtsp_data, retcd, "av_find_best_stream"); return -1; } @@ -829,7 +878,7 @@ static int netcam_rtsp_open_codec(struct rtsp_context *rtsp_data) rtsp_data->strm = rtsp_data->format_context->streams[rtsp_data->video_stream_index]; /* This is currently always true for older ffmpeg until it is built */ - if (!strcasecmp(rtsp_data->decoder_nm,"vaapi")){ + if (!strcasecmp(rtsp_data->decoder_nm,"vaapi")) { retcd = netcam_init_vaapi(rtsp_data); } else if (!strcasecmp(rtsp_data->decoder_nm,"cuda")){ retcd = netcam_init_cuda(rtsp_data); @@ -858,7 +907,7 @@ static int netcam_rtsp_interrupt(void *ctx) { struct rtsp_context *rtsp_data = ctx; - if (rtsp_data->finish){ + if (rtsp_data->finish) { rtsp_data->interrupted = TRUE; return TRUE; } @@ -905,11 +954,14 @@ static int netcam_rtsp_interrupt(void *ctx) static int netcam_rtsp_open_sws(struct rtsp_context *rtsp_data) { - if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return -1; + } rtsp_data->swsframe_in = my_frame_alloc(); if (rtsp_data->swsframe_in == NULL) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to allocate swsframe_in.")); } netcam_rtsp_close_context(rtsp_data); @@ -918,7 +970,7 @@ static int netcam_rtsp_open_sws(struct rtsp_context *rtsp_data) rtsp_data->swsframe_out = my_frame_alloc(); if (rtsp_data->swsframe_out == NULL) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to allocate swsframe_out.")); } netcam_rtsp_close_context(rtsp_data); @@ -938,7 +990,7 @@ static int netcam_rtsp_open_sws(struct rtsp_context *rtsp_data) ,MY_PIX_FMT_YUV420P ,SWS_BICUBIC,NULL,NULL,NULL); if (rtsp_data->swsctx == NULL) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to allocate scaling context.")); } netcam_rtsp_close_context(rtsp_data); @@ -950,7 +1002,7 @@ static int netcam_rtsp_open_sws(struct rtsp_context *rtsp_data) ,rtsp_data->imgsize.width ,rtsp_data->imgsize.height); if (rtsp_data->swsframe_size <= 0) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Error determining size of frame out")); } netcam_rtsp_close_context(rtsp_data); @@ -972,10 +1024,15 @@ static int netcam_rtsp_resize(struct rtsp_context *rtsp_data) char errstr[128]; uint8_t *buffer_out; - if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return -1; + } if (rtsp_data->swsctx == NULL) { - if (netcam_rtsp_open_sws(rtsp_data) < 0) return -1; + if (netcam_rtsp_open_sws(rtsp_data) < 0) { + return -1; + } } retcd=my_image_fill_arrays( @@ -985,7 +1042,7 @@ static int netcam_rtsp_resize(struct rtsp_context *rtsp_data) ,rtsp_data->frame->width ,rtsp_data->frame->height); if (retcd < 0) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("Error allocating picture in: %s"), errstr); @@ -1003,7 +1060,7 @@ static int netcam_rtsp_resize(struct rtsp_context *rtsp_data) ,rtsp_data->imgsize.width ,rtsp_data->imgsize.height); if (retcd < 0) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("Error allocating picture out: %s"), errstr); @@ -1021,7 +1078,7 @@ static int netcam_rtsp_resize(struct rtsp_context *rtsp_data) ,rtsp_data->swsframe_out->data ,rtsp_data->swsframe_out->linesize); if (retcd < 0) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("Error resizing/reformatting: %s"), errstr); @@ -1038,7 +1095,7 @@ static int netcam_rtsp_resize(struct rtsp_context *rtsp_data) ,rtsp_data->imgsize.height ,rtsp_data->swsframe_size); if (retcd < 0) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("Error putting frame into output buffer: %s"), errstr); @@ -1062,7 +1119,10 @@ static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data) char errstr[128]; netcam_buff *xchg; - if (rtsp_data->finish) return -1; /* This just speeds up the shutdown time */ + if (rtsp_data->finish) { + /* This just speeds up the shutdown time */ + return -1; + } /* * The av_read_play use here is a hack. At low frame rates the function @@ -1108,9 +1168,11 @@ static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data) */ while ((!haveimage) && (!rtsp_data->interrupted)) { retcd = av_read_frame(rtsp_data->format_context, &rtsp_data->packet_recv); - if (retcd < 0 ) errcnt++; + if (retcd < 0 ) { + errcnt++; + } if ((rtsp_data->interrupted) || (errcnt > 1)) { - if (rtsp_data->interrupted){ + if (rtsp_data->interrupted) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Interrupted"),rtsp_data->cameratype); } else { @@ -1124,18 +1186,20 @@ static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data) return -1; } else { errcnt = 0; - if (rtsp_data->packet_recv.stream_index == rtsp_data->video_stream_index){ + if (rtsp_data->packet_recv.stream_index == rtsp_data->video_stream_index) { /* For a high resolution pass-through we don't decode the image */ - if (rtsp_data->high_resolution && rtsp_data->passthrough){ - if (rtsp_data->packet_recv.data != NULL) size_decoded = 1; + if (rtsp_data->high_resolution && rtsp_data->passthrough) { + if (rtsp_data->packet_recv.data != NULL) { + size_decoded = 1; + } } else { size_decoded = netcam_rtsp_decode_packet(rtsp_data); } } - if (size_decoded > 0 ){ + if (size_decoded > 0 ) { haveimage = TRUE; - } else if (size_decoded == 0){ + } else if (size_decoded == 0) { /* Did not fail, just didn't get anything. Try again */ my_packet_unref(rtsp_data->packet_recv); av_init_packet(&rtsp_data->packet_recv); @@ -1158,14 +1222,16 @@ static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data) /* Skip status change on our first image to keep the "next" function waiting * until the handler thread gets going */ - if (!rtsp_data->first_image) rtsp_data->status = RTSP_CONNECTED; + if (!rtsp_data->first_image) { + rtsp_data->status = RTSP_CONNECTED; + } /* Skip resize/pix format for high pass-through */ - if (!(rtsp_data->high_resolution && rtsp_data->passthrough)){ + if (!(rtsp_data->high_resolution && rtsp_data->passthrough)) { if ((rtsp_data->imgsize.width != rtsp_data->frame->width) || (rtsp_data->imgsize.height != rtsp_data->frame->height) || - (netcam_rtsp_check_pixfmt(rtsp_data) != 0) ){ - if (netcam_rtsp_resize(rtsp_data) < 0){ + (netcam_rtsp_check_pixfmt(rtsp_data) != 0)) { + if (netcam_rtsp_resize(rtsp_data) < 0) { my_packet_unref(rtsp_data->packet_recv); netcam_rtsp_close_context(rtsp_data); return -1; @@ -1175,7 +1241,9 @@ static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data) pthread_mutex_lock(&rtsp_data->mutex); rtsp_data->idnbr++; - if (rtsp_data->passthrough) netcam_rtsp_pktarray_add(rtsp_data); + if (rtsp_data->passthrough) { + netcam_rtsp_pktarray_add(rtsp_data); + } if (!(rtsp_data->high_resolution && rtsp_data->passthrough)) { xchg = rtsp_data->img_latest; rtsp_data->img_latest = rtsp_data->img_recv; @@ -1185,7 +1253,7 @@ static int netcam_rtsp_read_image(struct rtsp_context *rtsp_data) my_packet_unref(rtsp_data->packet_recv); - if (rtsp_data->format_context->streams[rtsp_data->video_stream_index]->avg_frame_rate.den > 0){ + if (rtsp_data->format_context->streams[rtsp_data->video_stream_index]->avg_frame_rate.den > 0) { rtsp_data->src_fps = ( (rtsp_data->format_context->streams[rtsp_data->video_stream_index]->avg_frame_rate.num / rtsp_data->format_context->streams[rtsp_data->video_stream_index]->avg_frame_rate.den) + @@ -1200,11 +1268,13 @@ static int netcam_rtsp_ntc(struct rtsp_context *rtsp_data) if ((rtsp_data->finish) || (!rtsp_data->first_image) || - (rtsp_data->high_resolution && rtsp_data->passthrough)) return 0; + (rtsp_data->high_resolution && rtsp_data->passthrough)) { + return 0; + } if ((rtsp_data->imgsize.width != rtsp_data->frame->width) || (rtsp_data->imgsize.height != rtsp_data->frame->height) || - (netcam_rtsp_check_pixfmt(rtsp_data) != 0) ){ + (netcam_rtsp_check_pixfmt(rtsp_data) != 0)) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, ""); MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO, "******************************************************"); if ((rtsp_data->imgsize.width != rtsp_data->frame->width) || @@ -1255,19 +1325,18 @@ static void netcam_rtsp_set_options(struct rtsp_context *rtsp_data) tmpval = mymalloc(PATH_MAX); if ((strncmp(rtsp_data->service, "rtsp", 4) == 0 ) || - (strncmp(rtsp_data->service, "rtmp", 4) == 0 ) ) - { + (strncmp(rtsp_data->service, "rtmp", 4) == 0 )) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO,_("%s: Setting rtsp/rtmp") ,rtsp_data->cameratype); util_parms_add_default(rtsp_data->parameters,"rtsp_transport","tcp"); util_parms_add_default(rtsp_data->parameters,"allowed_media_types", "video"); - } else if (strncmp(rtsp_data->service, "http", 4) == 0 ){ + } else if (strncmp(rtsp_data->service, "http", 4) == 0 ) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Setting input_format mjpeg"),rtsp_data->cameratype); rtsp_data->format_context->iformat = av_find_input_format("mjpeg"); - } else if (strncmp(rtsp_data->service, "v4l2", 4) == 0 ){ + } else if (strncmp(rtsp_data->service, "v4l2", 4) == 0 ) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Setting input_format video4linux2"),rtsp_data->cameratype); rtsp_data->format_context->iformat = av_find_input_format("video4linux2"); @@ -1283,7 +1352,7 @@ static void netcam_rtsp_set_options(struct rtsp_context *rtsp_data) rtsp_data->interruptduration = 55; - } else if (strncmp(rtsp_data->service, "file", 4) == 0 ){ + } else if (strncmp(rtsp_data->service, "file", 4) == 0 ) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Setting up movie file"),rtsp_data->cameratype); @@ -1295,16 +1364,14 @@ static void netcam_rtsp_set_options(struct rtsp_context *rtsp_data) free(tmpval); /* Write the options to the context, while skipping the Motion ones */ - for (indx = 0; indx < rtsp_data->parameters->params_count; indx++) - { + for (indx = 0; indx < rtsp_data->parameters->params_count; indx++) { if (strcmp(rtsp_data->parameters->params_array[indx].param_name,"decoder") && - strcmp(rtsp_data->parameters->params_array[indx].param_name,"capture_rate") ) - { + strcmp(rtsp_data->parameters->params_array[indx].param_name,"capture_rate")) { av_dict_set(&rtsp_data->opts , rtsp_data->parameters->params_array[indx].param_name , rtsp_data->parameters->params_array[indx].param_value , 0); - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO,_("%s: option: %s = %s") ,rtsp_data->cameratype ,rtsp_data->parameters->params_array[indx].param_name @@ -1326,7 +1393,7 @@ static void netcam_rtsp_set_path (struct context *cnt, struct rtsp_context *rtsp memset(&url, 0, sizeof(url)); - if (rtsp_data->high_resolution){ + if (rtsp_data->high_resolution) { netcam_url_parse(&url, cnt->conf.netcam_high_url); } else { netcam_url_parse(&url, cnt->conf.netcam_url); @@ -1373,7 +1440,9 @@ static void netcam_rtsp_set_path (struct context *cnt, struct rtsp_context *rtsp sprintf(rtsp_data->service, "%s",url.service); netcam_url_free(&url); - if (userpass) free (userpass); + if (userpass) { + free (userpass); + } } @@ -1427,22 +1496,23 @@ static void netcam_rtsp_set_parms (struct context *cnt, struct rtsp_context *rts rtsp_data->capture_nbr = -1; rtsp_data->src_fps = -99; /* Default to invalid value so we can test for whether real value exist */ - for (indx = 0; indx < rtsp_data->parameters->params_count; indx++) - { - if ( !strcmp(rtsp_data->parameters->params_array[indx].param_name,"decoder") ){ + for (indx = 0; indx < rtsp_data->parameters->params_count; indx++) { + if ( !strcmp(rtsp_data->parameters->params_array[indx].param_name,"decoder")) { val_len = strlen(rtsp_data->parameters->params_array[indx].param_value) + 1; rtsp_data->decoder_nm = mymalloc(val_len); snprintf(rtsp_data->decoder_nm, val_len , "%s",rtsp_data->parameters->params_array[indx].param_value); } - if ( !strcmp(rtsp_data->parameters->params_array[indx].param_name,"capture_rate") ){ + if ( !strcmp(rtsp_data->parameters->params_array[indx].param_name,"capture_rate")) { rtsp_data->capture_rate = atoi(rtsp_data->parameters->params_array[indx].param_value); } } - if (rtsp_data->capture_rate < 1 ) rtsp_data->capture_rate = 1; + if (rtsp_data->capture_rate < 1 ) { + rtsp_data->capture_rate = 1; + } /* If this is the norm and we have a highres, then disable passthru on the norm */ if ((!rtsp_data->high_resolution) && (cnt->conf.netcam_high_url)) { @@ -1516,12 +1586,14 @@ static int netcam_rtsp_copy_stream(struct rtsp_context *rtsp_data) int retcd; pthread_mutex_lock(&rtsp_data->mutex_transfer); - if (rtsp_data->transfer_format != NULL) avformat_close_input(&rtsp_data->transfer_format); + if (rtsp_data->transfer_format != NULL) { + avformat_close_input(&rtsp_data->transfer_format); + } rtsp_data->transfer_format = avformat_alloc_context(); transfer_stream = avformat_new_stream(rtsp_data->transfer_format, NULL); stream_in = rtsp_data->format_context->streams[rtsp_data->video_stream_index]; retcd = avcodec_parameters_copy(transfer_stream->codecpar, stream_in->codecpar); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("Unable to copy codec parameters")); pthread_mutex_unlock(&rtsp_data->mutex_transfer); @@ -1538,12 +1610,14 @@ static int netcam_rtsp_copy_stream(struct rtsp_context *rtsp_data) int retcd; pthread_mutex_lock(&rtsp_data->mutex_transfer); - if (rtsp_data->transfer_format != NULL) avformat_close_input(&rtsp_data->transfer_format); + if (rtsp_data->transfer_format != NULL) { + avformat_close_input(&rtsp_data->transfer_format); + } rtsp_data->transfer_format = avformat_alloc_context(); transfer_stream = avformat_new_stream(rtsp_data->transfer_format, NULL); stream_in = rtsp_data->format_context->streams[rtsp_data->video_stream_index]; retcd = avcodec_copy_context(transfer_stream->codec, stream_in->codec); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Unable to copy codec parameters")); pthread_mutex_unlock(&rtsp_data->mutex_transfer); return -1; @@ -1563,10 +1637,12 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) int retcd; char errstr[128]; - if (rtsp_data->finish) return -1; + if (rtsp_data->finish) { + return -1; + } if (rtsp_data->path == NULL) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("Null path passed to connect")); } return -1; @@ -1587,15 +1663,17 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) netcam_rtsp_set_options(rtsp_data); retcd = avformat_open_input(&rtsp_data->format_context, rtsp_data->path, NULL, &rtsp_data->opts); - if ((retcd < 0) || (rtsp_data->interrupted) || (rtsp_data->finish) ){ - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if ((retcd < 0) || (rtsp_data->interrupted) || (rtsp_data->finish)) { + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Unable to open camera(%s): %s") , rtsp_data->cameratype, rtsp_data->camera_name, errstr); } av_dict_free(&rtsp_data->opts); - if (rtsp_data->interrupted) netcam_rtsp_close_context(rtsp_data); + if (rtsp_data->interrupted) { + netcam_rtsp_close_context(rtsp_data); + } return -1; } av_dict_free(&rtsp_data->opts); @@ -1604,8 +1682,8 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) /* fill out stream information */ retcd = avformat_find_stream_info(rtsp_data->format_context, NULL); - if ((retcd < 0) || (rtsp_data->interrupted) || (rtsp_data->finish) ){ - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if ((retcd < 0) || (rtsp_data->interrupted) || (rtsp_data->finish)) { + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Unable to find stream info: %s") @@ -1627,8 +1705,8 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) util_threadname_set(NULL, 0, rtsp_data->threadname); - if ((retcd < 0) || (rtsp_data->interrupted) || (rtsp_data->finish) ){ - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if ((retcd < 0) || (rtsp_data->interrupted) || (rtsp_data->finish)) { + if (rtsp_data->status == RTSP_NOTCONNECTED) { av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Unable to open codec context: %s") @@ -1646,14 +1724,14 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) return -1; } - if (rtsp_data->high_resolution){ + if (rtsp_data->high_resolution) { rtsp_data->imgsize.width = rtsp_data->codec_context->width; rtsp_data->imgsize.height = rtsp_data->codec_context->height; } rtsp_data->frame = my_frame_alloc(); if (rtsp_data->frame == NULL) { - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Unable to allocate frame."),rtsp_data->cameratype); } @@ -1661,10 +1739,10 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) return -1; } - if (rtsp_data->passthrough){ + if (rtsp_data->passthrough) { retcd = netcam_rtsp_copy_stream(rtsp_data); - if ((retcd < 0) || (rtsp_data->interrupted)){ - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if ((retcd < 0) || (rtsp_data->interrupted)) { + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Failed to copy stream for pass-through.") ,rtsp_data->cameratype); @@ -1675,8 +1753,8 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) /* Validate that the previous steps opened the camera */ retcd = netcam_rtsp_read_image(rtsp_data); - if ((retcd < 0) || (rtsp_data->interrupted)){ - if (rtsp_data->status == RTSP_NOTCONNECTED){ + if ((retcd < 0) || (rtsp_data->interrupted)) { + if (rtsp_data->status == RTSP_NOTCONNECTED) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Failed to read first image"),rtsp_data->cameratype); } @@ -1691,11 +1769,17 @@ static int netcam_rtsp_open_context(struct rtsp_context *rtsp_data) static int netcam_rtsp_connect(struct rtsp_context *rtsp_data) { - if (netcam_rtsp_open_context(rtsp_data) < 0) return -1; + if (netcam_rtsp_open_context(rtsp_data) < 0) { + return -1; + } - if (netcam_rtsp_ntc(rtsp_data) < 0 ) return -1; + if (netcam_rtsp_ntc(rtsp_data) < 0) { + return -1; + } - if (netcam_rtsp_read_image(rtsp_data) < 0) return -1; + if (netcam_rtsp_read_image(rtsp_data) < 0) { + return -1; + } /* We use the status for determining whether to grab a image from * the Motion loop(see "next" function). When we are initially starting, @@ -1703,7 +1787,7 @@ static int netcam_rtsp_connect(struct rtsp_context *rtsp_data) * Motion loop to start quite yet on this first image so we do * not set the status to connected */ - if (!rtsp_data->first_image){ + if (!rtsp_data->first_image) { rtsp_data->status = RTSP_CONNECTED; @@ -1715,7 +1799,7 @@ static int netcam_rtsp_connect(struct rtsp_context *rtsp_data) ,_("%s: Netcam capture FPS is %d.") ,rtsp_data->cameratype, rtsp_data->capture_rate); - if (rtsp_data->src_fps > 0){ + if (rtsp_data->src_fps > 0) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO ,_("%s: Camera source is %d FPS") ,rtsp_data->cameratype, rtsp_data->src_fps); @@ -1725,7 +1809,7 @@ static int netcam_rtsp_connect(struct rtsp_context *rtsp_data) ,rtsp_data->cameratype); } - if (rtsp_data->capture_rate < rtsp_data->src_fps){ + if (rtsp_data->capture_rate < rtsp_data->src_fps) { MOTION_LOG(WRN, TYPE_NETCAM, NO_ERRNO ,_("%s: Capture FPS rate is less than camera FPS. Decoding errors will occur.") , rtsp_data->cameratype); @@ -1744,26 +1828,33 @@ static void netcam_rtsp_shutdown(struct rtsp_context *rtsp_data) if (rtsp_data) { netcam_rtsp_close_context(rtsp_data); - if (rtsp_data->path != NULL) free(rtsp_data->path); + if (rtsp_data->path != NULL) { + free(rtsp_data->path); + } rtsp_data->path = NULL; - if (rtsp_data->img_latest != NULL){ + if (rtsp_data->img_latest != NULL) { free(rtsp_data->img_latest->ptr); free(rtsp_data->img_latest); } rtsp_data->img_latest = NULL; - if (rtsp_data->img_recv != NULL){ + if (rtsp_data->img_recv != NULL) { free(rtsp_data->img_recv->ptr); free(rtsp_data->img_recv); } rtsp_data->img_recv = NULL; - if (rtsp_data->decoder_nm != NULL) free(rtsp_data->decoder_nm); + if (rtsp_data->decoder_nm != NULL) { + free(rtsp_data->decoder_nm); + } rtsp_data->decoder_nm = NULL; util_parms_free (rtsp_data->parameters); - if (rtsp_data->parameters != NULL) free(rtsp_data->parameters); + + if (rtsp_data->parameters != NULL) { + free(rtsp_data->parameters); + } rtsp_data->parameters = NULL; } @@ -1775,7 +1866,9 @@ static void netcam_rtsp_handler_wait(struct rtsp_context *rtsp_data) long usec_maxrate, usec_delay; - if (rtsp_data->capture_rate < 1 ) rtsp_data->capture_rate = 1; + if (rtsp_data->capture_rate < 1 ) { + rtsp_data->capture_rate = 1; + } usec_maxrate = (1000000L / rtsp_data->capture_rate); @@ -1786,7 +1879,7 @@ static void netcam_rtsp_handler_wait(struct rtsp_context *rtsp_data) usec_delay = usec_maxrate - ((rtsp_data->frame_curr_tm.tv_sec - rtsp_data->frame_prev_tm.tv_sec) * 1000000L) - (rtsp_data->frame_curr_tm.tv_usec - rtsp_data->frame_prev_tm.tv_usec); - if ((usec_delay > 0) && (usec_delay < 1000000L)){ + if ((usec_delay > 0) && (usec_delay < 1000000L)) { SLEEP(0, usec_delay * 1000); } @@ -1798,7 +1891,7 @@ static void netcam_rtsp_handler_reconnect(struct rtsp_context *rtsp_data) int retcd; if ((rtsp_data->status == RTSP_CONNECTED) || - (rtsp_data->status == RTSP_READINGIMAGE)){ + (rtsp_data->status == RTSP_READINGIMAGE)) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO ,_("%s: Reconnecting with camera...."),rtsp_data->cameratype); } @@ -1810,10 +1903,10 @@ static void netcam_rtsp_handler_reconnect(struct rtsp_context *rtsp_data) * before we go into the long wait phase */ retcd = netcam_rtsp_connect(rtsp_data); - if (retcd < 0){ - if (rtsp_data->reconnect_count < 100){ + if (retcd < 0) { + if (rtsp_data->reconnect_count < 100) { rtsp_data->reconnect_count++; - } else if (rtsp_data->reconnect_count == 100){ + } else if (rtsp_data->reconnect_count == 100) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO ,_("%s: Camera did not reconnect."), rtsp_data->cameratype); MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO @@ -1915,10 +2008,12 @@ static int netcam_rtsp_start_handler(struct rtsp_context *rtsp_data) wait_counter = 60; while (wait_counter > 0) { pthread_mutex_lock(&rtsp_data->mutex); - if (rtsp_data->img_latest->ptr != NULL ) wait_counter = -1; + if (rtsp_data->img_latest->ptr != NULL) { + wait_counter = -1; + } pthread_mutex_unlock(&rtsp_data->mutex); - if (wait_counter > 0 ){ + if (wait_counter > 0) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Waiting for first image from the handler."),rtsp_data->cameratype); SLEEP(0,5000000); @@ -1947,14 +2042,19 @@ int netcam_rtsp_setup(struct context *cnt) cnt->rtsp = NULL; cnt->rtsp_high = NULL; - if (netcam_rtsp_set_dimensions(cnt) < 0 ) return -1; + if (netcam_rtsp_set_dimensions(cnt) < 0) { + return -1; + } indx_cam = 1; - indx_max = 1; - if (cnt->conf.netcam_high_url) indx_max = 2; + if (cnt->conf.netcam_high_url) { + indx_max = 2; + } else { + indx_max = 1; + } - while (indx_cam <= indx_max){ - if (indx_cam == 1){ + while (indx_cam <= indx_max) { + if (indx_cam == 1) { cnt->rtsp = rtsp_new_context(); if (cnt->rtsp == NULL) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO @@ -1978,10 +2078,12 @@ int netcam_rtsp_setup(struct context *cnt) netcam_rtsp_set_parms(cnt, rtsp_data); - if (netcam_rtsp_connect(rtsp_data) < 0) return -1; + if (netcam_rtsp_connect(rtsp_data) < 0) { + return -1; + } retcd = netcam_rtsp_read_image(rtsp_data); - if (retcd < 0){ + if (retcd < 0) { MOTION_LOG(CRT, TYPE_NETCAM, NO_ERRNO ,_("Failed trying to read first image - retval:%d"), retcd); rtsp_data->status = RTSP_NOTCONNECTED; @@ -1996,12 +2098,14 @@ int netcam_rtsp_setup(struct context *cnt) * to set the dimension parameters here (it is done in the set_parms). For high res * we must get the dimensions from the first image captured */ - if (rtsp_data->high_resolution){ + if (rtsp_data->high_resolution) { cnt->imgs.width_high = rtsp_data->imgsize.width; cnt->imgs.height_high = rtsp_data->imgsize.height; } - if (netcam_rtsp_start_handler(rtsp_data) < 0 ) return -1; + if (netcam_rtsp_start_handler(rtsp_data) < 0) { + return -1; + } indx_cam++; } @@ -2010,8 +2114,9 @@ int netcam_rtsp_setup(struct context *cnt) #else /* No FFmpeg/Libav */ /* Stop compiler warnings */ - if (cnt) + if (cnt) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("FFmpeg/Libav not found on computer. No RTSP support")); + } return -1; #endif /* End #ifdef HAVE_FFMPEG */ } @@ -2022,9 +2127,9 @@ int netcam_rtsp_next(struct context *cnt, struct image_data *img_data) /* This is called from the motion loop thread */ if ((cnt->rtsp->status == RTSP_RECONNECTING) || - (cnt->rtsp->status == RTSP_NOTCONNECTED)){ + (cnt->rtsp->status == RTSP_NOTCONNECTED)) { return 1; - } + } pthread_mutex_lock(&cnt->rtsp->mutex); netcam_rtsp_pktarray_resize(cnt, FALSE); memcpy(img_data->image_norm @@ -2033,10 +2138,11 @@ int netcam_rtsp_next(struct context *cnt, struct image_data *img_data) img_data->idnbr_norm = cnt->rtsp->idnbr; pthread_mutex_unlock(&cnt->rtsp->mutex); - if (cnt->rtsp_high){ + if (cnt->rtsp_high) { if ((cnt->rtsp_high->status == RTSP_RECONNECTING) || - (cnt->rtsp_high->status == RTSP_NOTCONNECTED)) return 1; - + (cnt->rtsp_high->status == RTSP_NOTCONNECTED)) { + return 1; + } pthread_mutex_lock(&cnt->rtsp_high->mutex); netcam_rtsp_pktarray_resize(cnt, TRUE); if (!(cnt->rtsp_high->high_resolution && cnt->rtsp_high->passthrough)) { @@ -2055,8 +2161,9 @@ int netcam_rtsp_next(struct context *cnt, struct image_data *img_data) #else /* No FFmpeg/Libav */ /* Stop compiler warnings */ - if ((cnt) || (img_data)) + if ((cnt) || (img_data)) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("FFmpeg/Libav not found on computer. No RTSP support")); + } return -1; #endif /* End #ifdef HAVE_FFMPEG */ } @@ -2075,17 +2182,20 @@ void netcam_rtsp_cleanup(struct context *cnt, int init_retry_flag) struct rtsp_context *rtsp_data; indx_cam = 1; - indx_max = 1; - if (cnt->rtsp_high) indx_max = 2; + if (cnt->rtsp_high) { + indx_max = 2; + } else { + indx_max = 1; + } while (indx_cam <= indx_max) { - if (indx_cam == 1){ + if (indx_cam == 1) { rtsp_data = cnt->rtsp; } else { rtsp_data = cnt->rtsp_high; } - if (rtsp_data){ + if (rtsp_data) { MOTION_LOG(INF, TYPE_NETCAM, NO_ERRNO ,_("%s: Shutting down network camera."),rtsp_data->cameratype); @@ -2107,7 +2217,7 @@ void netcam_rtsp_cleanup(struct context *cnt, int init_retry_flag) /* pthread_kill(rtsp_data->thread_id); */ pthread_cancel(rtsp_data->thread_id); pthread_kill(rtsp_data->thread_id, SIGVTALRM); /* This allows the cancel to be processed */ - if (!init_retry_flag){ + if (!init_retry_flag) { pthread_mutex_lock(&global_lock); threads_running--; pthread_mutex_unlock(&global_lock); @@ -2122,7 +2232,7 @@ void netcam_rtsp_cleanup(struct context *cnt, int init_retry_flag) free(rtsp_data); rtsp_data = NULL; - if (indx_cam == 1){ + if (indx_cam == 1) { MOTION_LOG(NTC, TYPE_NETCAM, NO_ERRNO ,_("Normal resolution: Shut down complete.")); } else { @@ -2137,8 +2247,9 @@ void netcam_rtsp_cleanup(struct context *cnt, int init_retry_flag) #else /* No FFmpeg/Libav */ /* Stop compiler warnings */ - if ((cnt) || (init_retry_flag)) + if ((cnt) || (init_retry_flag)) { MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, _("FFmpeg/Libav not found on computer. No RTSP support")); + } return; #endif /* End #ifdef HAVE_FFMPEG */ diff --git a/src/netcam_wget.c b/src/netcam_wget.c index b48045d9..3224a471 100644 --- a/src/netcam_wget.c +++ b/src/netcam_wget.c @@ -75,8 +75,9 @@ int header_get(netcam_context_ptr netcam, char **hdr, enum header_get_flags flag for (i = 0; 1; i++) { int res; /* #### Use DO_REALLOC? */ - if (i > bufsize - 1) + if (i > bufsize - 1) { *hdr = (char *)myrealloc(*hdr, (bufsize <<= 1), ""); + } res = RBUF_READCHAR (netcam, *hdr + i); @@ -100,8 +101,9 @@ int header_get(netcam_context_ptr netcam, char **hdr, enum header_get_flags flag return HG_ERROR; } /* If the next character is HT or SP, just continue. */ - if (next == '\t' || next == ' ') + if (next == '\t' || next == ' ') { continue; + } } /* @@ -109,8 +111,9 @@ int header_get(netcam_context_ptr netcam, char **hdr, enum header_get_flags flag * decrement I until it points to the last available * whitespace. */ - while (i > 0 && isspace((*hdr)[i - 1])) + while (i > 0 && isspace((*hdr)[i - 1])) { --i; + } (*hdr)[i] = '\0'; break; @@ -141,8 +144,9 @@ int header_process(const char *header, const char *name, while (*name && (tolower (*name) == tolower (*header))) ++name, ++header; - if (*name || *header++ != ':') + if (*name || *header++ != ':') { return 0; + } header += skip_lws (header); return ((*procfun) (header, arg)); @@ -161,12 +165,14 @@ int header_extract_number(const char *header, void *closure) const char *p = header; long result; - for (result = 0; isdigit (*p); p++) + for (result = 0; isdigit (*p); p++) { result = 10 * result + (*p - '0'); + } /* Failure if no number present. */ - if (p == header) + if (p == header) { return 0; + } /* Skip trailing whitespace. */ p += skip_lws (p); @@ -175,8 +181,9 @@ int header_extract_number(const char *header, void *closure) *(long *)closure = result; /* Indicate failure if trailing garbage is present. */ - if (*p) + if (*p) { return 0; + } return 1; } @@ -202,8 +209,9 @@ int skip_lws(const char *string) { const char *p = string; - while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') + while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') { ++p; + } return p - string; } @@ -243,10 +251,11 @@ void motion_base64_encode(const char *s, char *store, int length) } /* Pad the result if necessary... */ - if (i == length + 1) + if (i == length + 1) { *(p - 1) = '='; - else if (i == length + 2) + } else if (i == length + 2) { *(p - 1) = *(p - 2) = '='; + } /* ...and zero-terminate it. */ *p = '\0'; @@ -273,11 +282,13 @@ int http_process_type(const char *hdr, void *arg) /* Locate P on `;' or the terminating zero, whichever comes first. */ const char *p = strchr (hdr, ';'); - if (!p) + if (!p) { p = hdr + strlen (hdr); + } - while (p > hdr && isspace (*(p - 1))) + while (p > hdr && isspace (*(p - 1))) { --p; + } *result = strdupdelim (hdr, p); return 1; @@ -339,8 +350,9 @@ int rbuf_flush(netcam_context_ptr netcam, char *where, int maxsize) } else { int howmuch = MINVAL ((int)netcam->response->buffer_left, maxsize); - if (where) + if (where) { memcpy(where, netcam->response->buffer_pos, howmuch); + } netcam->response->buffer_left -= howmuch; netcam->response->buffer_pos += howmuch; @@ -358,12 +370,14 @@ int http_result_code(const char *header) char *cptr; /* Assure the header starts out right. */ - if (strncmp(header, "HTTP", 4)) + if (strncmp(header, "HTTP", 4)) { return -1; + } /* Find the space following the HTTP/1.x */ - if ((cptr = strchr(header+4, ' ')) == NULL) + if ((cptr = strchr(header+4, ' ')) == NULL) { return -1; + } return atoi(cptr + 1); } diff --git a/src/picture.c b/src/picture.c index f2e0cf9d..ad5da75b 100644 --- a/src/picture.c +++ b/src/picture.c @@ -284,11 +284,13 @@ unsigned prepare_exif(unsigned char **exif, put_uint16(writing.buf, ifd0_tagcount); writing.buf += 2; - if (description) + if (description) { put_stringentry(&writing, TIFF_TAG_IMAGE_DESCRIPTION, description, 1); + } - if (datetime) + if (datetime) { put_stringentry(&writing, TIFF_TAG_DATETIME, datetime, 1); + } if (ifd1_tagcount > 0) { /* Offset of IFD1 - TIFF header + IFD0 size. */ @@ -315,14 +317,17 @@ unsigned prepare_exif(unsigned char **exif, memcpy(writing.buf + 2, exif_version_tag, 12); /* tag 0x9000 */ writing.buf += 14; - if (datetime) + if (datetime) { put_stringentry(&writing, EXIF_TAG_ORIGINAL_DATETIME, datetime, 1); + } - if (box) + if (box) { put_subjectarea(&writing, box); + } - if (subtime) + if (subtime) { put_stringentry(&writing, EXIF_TAG_ORIGINAL_DATETIME_SS, subtime, 0); + } put_uint32(writing.buf, 0); /* Next IFD = 0 (no next IFD) */ writing.buf += 4; @@ -398,14 +403,14 @@ static void put_webp_yuv420p_file(FILE *fp, { /* Create a config present and check for compatible library version */ WebPConfig webp_config; - if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)){ + if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) { MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO, _("libwebp version error")); return; } /* Create the input data structure and check for compatible library version */ WebPPicture webp_image; - if (!WebPPictureInit(&webp_image)){ + if (!WebPPictureInit(&webp_image)) { MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("libwebp version error")); return; } @@ -413,7 +418,7 @@ static void put_webp_yuv420p_file(FILE *fp, /* Allocate the image buffer based on image width and height */ webp_image.width = width; webp_image.height = height; - if (!WebPPictureAlloc(&webp_image)){ + if (!WebPPictureAlloc(&webp_image)) { MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("libwebp image buffer allocation error")); return; } @@ -430,8 +435,9 @@ static void put_webp_yuv420p_file(FILE *fp, webp_image.custom_ptr = (void*) &webp_writer; /* Encode the YUV image as webp */ - if (!WebPEncode(&webp_config, &webp_image)) + if (!WebPEncode(&webp_config, &webp_image)) { MOTION_LOG(WRN, TYPE_CORE, NO_ERRNO,_("libwebp image compression error")); + } /* A bitstream object is needed for the muxing proces */ WebPData webp_bitstream; @@ -450,8 +456,9 @@ static void put_webp_yuv420p_file(FILE *fp, } /* Write the webp final bitstream to the file */ - if (fwrite(webp_output.bytes, sizeof(uint8_t), webp_output.size, fp) != webp_output.size) + if (fwrite(webp_output.bytes, sizeof(uint8_t), webp_output.size, fp) != webp_output.size) { MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("unable to save webp image to file")); + } #if WEBP_ENCODER_ABI_VERSION > 0x0202 /* writer.mem must be freed by calling WebPMemoryWriterClear */ @@ -570,18 +577,21 @@ static void put_ppm_bgr24_file(FILE *picture, unsigned char *image, int width, i r = r >> 16; g = g >> 16; b = b >> 16; - if (r < 0) + if (r < 0) { r = 0; - else if (r > 255) + } else if (r > 255) { r = 255; - if (g < 0) + } + if (g < 0) { g = 0; - else if (g > 255) + } else if (g > 255) { g = 255; - if (b < 0) + } + if (b < 0) { b = 0; - else if (b > 255) + } else if (b > 255) { b = 255; + } rgb[0] = b; rgb[1] = g; @@ -629,7 +639,6 @@ void overlay_smartmask(struct context *cnt, unsigned char *out) if (smartmask[line + x] == 0 || smartmask[line + x + 1] == 0 || smartmask[line + width + x] == 0 || smartmask[line + width + x + 1] == 0) { - *out_v = 255; *out_u = 128; } @@ -640,8 +649,9 @@ void overlay_smartmask(struct context *cnt, unsigned char *out) out_y = out; /* Set colour intensity for smartmask. */ for (i = 0; i < imgs->motionsize; i++) { - if (smartmask[i] == 0) + if (smartmask[i] == 0) { *out_y = 0; + } out_y++; } } @@ -673,7 +683,6 @@ void overlay_fixed_mask(struct context *cnt, unsigned char *out) if (mask[line + x] == 0 || mask[line + x + 1] == 0 || mask[line + width + x] == 0 || mask[line + width + x + 1] == 0) { - *out_v = 0; *out_u = 0; } @@ -684,8 +693,9 @@ void overlay_fixed_mask(struct context *cnt, unsigned char *out) out_y = out; /* Set colour intensity for mask. */ for (i = 0; i < imgs->motionsize; i++) { - if (mask[i] == 0) + if (mask[i] == 0) { *out_y = 0; + } out_y++; } } @@ -717,7 +727,6 @@ void overlay_largest_label(struct context *cnt, unsigned char *out) if (labels[line + x] & 32768 || labels[line + x + 1] & 32768 || labels[line + width + x] & 32768 || labels[line + width + x + 1] & 32768) { - *out_u = 255; *out_v = 128; } @@ -728,8 +737,9 @@ void overlay_largest_label(struct context *cnt, unsigned char *out) out_y = out; /* Set intensity for coloured label to have better visibility. */ for (i = 0; i < imgs->motionsize; i++) { - if (*labels++ & 32768) + if (*labels++ & 32768) { *out_y = 0; + } out_y++; } } @@ -761,7 +771,7 @@ int put_picture_memory(struct context *cnt, unsigned char* dest_image, int image */ gettimeofday(&tv1, NULL); - if (!cnt->conf.stream_grey){ + if (!cnt->conf.stream_grey) { return jpgutl_put_yuv420p(dest_image, image_size, image, width, height, quality, cnt ,&tv1,NULL); } else { @@ -792,13 +802,17 @@ static void put_picture_fd(struct context *cnt, FILE *picture, unsigned char *im if (cnt->imgs.picture_type == IMAGE_TYPE_PPM) { put_ppm_bgr24_file(picture, image, width, height); } else { - if (dummy == 1){ + if (dummy == 1) { #ifdef HAVE_WEBP - if (cnt->imgs.picture_type == IMAGE_TYPE_WEBP) - put_webp_yuv420p_file(picture, image, width, height, quality, cnt, &(cnt->current_image->timestamp_tv), &(cnt->current_image->location)); + if (cnt->imgs.picture_type == IMAGE_TYPE_WEBP) { + put_webp_yuv420p_file(picture, image, width, height, quality, cnt + , &(cnt->current_image->timestamp_tv), &(cnt->current_image->location)); + } #endif /* HAVE_WEBP */ - if (cnt->imgs.picture_type == IMAGE_TYPE_JPEG) - put_jpeg_yuv420p_file(picture, image, width, height, quality, cnt, &(cnt->current_image->timestamp_tv), &(cnt->current_image->location)); + if (cnt->imgs.picture_type == IMAGE_TYPE_JPEG) { + put_jpeg_yuv420p_file(picture, image, width, height, quality, cnt + , &(cnt->current_image->timestamp_tv), &(cnt->current_image->location)); + } } else { put_jpeg_grey_file(picture, image, width, height, quality, cnt, &(cnt->current_image->timestamp_tv), &(cnt->current_image->location)); } @@ -859,9 +873,11 @@ unsigned char *get_pgm(FILE *picture, int width, int height) /* Skip comment */ line[0] = '#'; - while (line[0] == '#') - if (!fgets(line, 255, picture)) + while (line[0] == '#') { + if (!fgets(line, 255, picture)) { return NULL; + } + } /* Read image size */ if (sscanf(line, "%d %d", &mask_width, &mask_height) != 2) { @@ -872,9 +888,11 @@ unsigned char *get_pgm(FILE *picture, int width, int height) /* Maximum value */ line[0] = '#'; - while (line[0] == '#') - if (!fgets(line, 255, picture)) + while (line[0] == '#') { + if (!fgets(line, 255, picture)) { return NULL; + } + } if (sscanf(line, "%d", &maxval) != 1) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO @@ -890,11 +908,13 @@ unsigned char *get_pgm(FILE *picture, int width, int height) image = mymalloc((mask_width * mask_height * 3) / 2); for (y = 0; y < mask_height; y++) { - if ((int)fread(&image[y * mask_width], 1, mask_width, picture) != mask_width) + if ((int)fread(&image[y * mask_width], 1, mask_width, picture) != mask_width) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Failed reading image data from pgm file")); + } - for (x = 0; x < mask_width; x++) + for (x = 0; x < mask_width; x++) { image[y * mask_width + x] = (int)image[y * mask_width + x] * 255 / maxval; + } } @@ -974,16 +994,18 @@ void pic_scale_img(int width_src, int height_src, unsigned char *img_src, unsign { int i = 0, x, y; - for (y = 0; y < height_src; y+=2) - for (x = 0; x < width_src; x+=2) + for (y = 0; y < height_src; y+=2) { + for (x = 0; x < width_src; x+=2) { img_dst[i++] = img_src[y * width_src + x]; + } + } - for (y = 0; y < height_src / 2; y+=2) - for (x = 0; x < width_src; x += 4) - { + for (y = 0; y < height_src / 2; y+=2) { + for (x = 0; x < width_src; x += 4) { img_dst[i++] = img_src[(width_src * height_src) + (y * width_src) + x]; img_dst[i++] = img_src[(width_src * height_src) + (y * width_src) + (x + 1)]; } + } return; } diff --git a/src/rotate.c b/src/rotate.c index e94e367b..424f3304 100644 --- a/src/rotate.c +++ b/src/rotate.c @@ -136,9 +136,9 @@ static void rot90cw(unsigned char *src, register unsigned char *dst, int size, endp = src + size; for (base = endp - width; base < endp; base++) { src = base; - for (j = 0; j < height; j++, src -= width) + for (j = 0; j < height; j++, src -= width) { *dst++ = *src; - + } } } @@ -170,9 +170,9 @@ static inline void rot90ccw(unsigned char *src, register unsigned char *dst, dst = dst + size - 1; for (base = endp - width; base < endp; base++) { src = base; - for (j = 0; j < height; j++, src -= width) + for (j = 0; j < height; j++, src -= width) { *dst-- = *src; - + } } } @@ -253,15 +253,19 @@ void rotate_init(struct context *cnt) * If we're not rotating, let's exit once we have setup the capture dimensions * and output dimensions properly. */ - if (cnt->rotate_data.degrees == 0) return; + if (cnt->rotate_data.degrees == 0) { + return; + } /* * Allocate memory if rotating 90 or 270 degrees, because those rotations * cannot be performed in-place (they can, but it would be too slow). */ - if ((cnt->rotate_data.degrees == 90) || (cnt->rotate_data.degrees == 270)){ + if ((cnt->rotate_data.degrees == 90) || (cnt->rotate_data.degrees == 270)) { cnt->rotate_data.buffer_norm = mymalloc(size_norm); - if (size_high > 0 ) cnt->rotate_data.buffer_high = mymalloc(size_high); + if (size_high > 0) { + cnt->rotate_data.buffer_high = mymalloc(size_high); + } } } @@ -280,11 +284,13 @@ void rotate_init(struct context *cnt) void rotate_deinit(struct context *cnt) { - if (cnt->rotate_data.buffer_norm) + if (cnt->rotate_data.buffer_norm) { free(cnt->rotate_data.buffer_norm); + } - if (cnt->rotate_data.buffer_high) + if (cnt->rotate_data.buffer_high) { free(cnt->rotate_data.buffer_high); + } } /** @@ -320,11 +326,15 @@ int rotate_map(struct context *cnt, struct image_data *img_data) unsigned char *img; unsigned char *temp_buff; - if (cnt->rotate_data.degrees == 0 && cnt->rotate_data.axis == FLIP_TYPE_NONE) return 0; + if (cnt->rotate_data.degrees == 0 && cnt->rotate_data.axis == FLIP_TYPE_NONE) { + return 0; + } indx = 0; indx_max = 0; - if ((cnt->rotate_data.capture_width_high != 0) && (cnt->rotate_data.capture_height_high != 0)) indx_max = 1; + if ((cnt->rotate_data.capture_width_high != 0) && (cnt->rotate_data.capture_height_high != 0)) { + indx_max = 1; + } while (indx <= indx_max) { deg = cnt->rotate_data.degrees; @@ -332,7 +342,7 @@ int rotate_map(struct context *cnt, struct image_data *img_data) wh4 = 0; w2 = 0; h2 = 0; - if (indx == 0 ){ + if (indx == 0) { img = img_data->image_norm; width = cnt->rotate_data.capture_width_norm; height = cnt->rotate_data.capture_height_norm; diff --git a/src/track.c b/src/track.c index d17bb6a2..b701c219 100644 --- a/src/track.c +++ b/src/track.c @@ -75,8 +75,9 @@ unsigned int track_center(struct context *cnt, int dev ATTRIBUTE_UNUSED, { struct coord cent; - if (!manual && !cnt->track.active) + if (!manual && !cnt->track.active) { return 0; + } if (cnt->track.type == TRACK_TYPE_STEPPER) { unsigned int ret; @@ -84,21 +85,23 @@ unsigned int track_center(struct context *cnt, int dev ATTRIBUTE_UNUSED, if (!ret) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO,_("internal error")); return 0; + } else { + return ret; } - else return ret; } else if (cnt->track.type == TRACK_TYPE_SERVO) { return servo_center(cnt, xoff, yoff); - } - #ifdef HAVE_V4L2 - else if (cnt->track.type == TRACK_TYPE_PWC) + + #ifdef HAVE_V4L2 /*dreadful coding method imo */ + } else if (cnt->track.type == TRACK_TYPE_PWC) { return lqos_center(cnt, dev, xoff, yoff); - else if (cnt->track.type == TRACK_TYPE_UVC) + } else if (cnt->track.type == TRACK_TYPE_UVC) { return uvc_center(cnt, dev, xoff, yoff); #endif - else if (cnt->track.type == TRACK_TYPE_IOMOJO) + + } else if (cnt->track.type == TRACK_TYPE_IOMOJO) { return iomojo_center(cnt, xoff, yoff); - else if (cnt->track.type == TRACK_TYPE_GENERIC) { - if (cnt->track.generic_move){ + } else if (cnt->track.type == TRACK_TYPE_GENERIC) { + if (cnt->track.generic_move) { cent.x = -cnt->track_posx; cent.y = -cnt->track_posy; return generic_move(cnt, TRACK_CENTER, manual,0 ,0 ,¢ , NULL); @@ -118,26 +121,30 @@ unsigned int track_move(struct context *cnt, int dev, struct coord *cent, struct unsigned int manual) { - if (!manual && !cnt->track.active) + if (!manual && !cnt->track.active) { return 0; + } - if (cnt->track.type == TRACK_TYPE_STEPPER) + if (cnt->track.type == TRACK_TYPE_STEPPER) { return stepper_move(cnt, cent, imgs); - else if (cnt->track.type == TRACK_TYPE_SERVO) + } else if (cnt->track.type == TRACK_TYPE_SERVO) { return servo_move(cnt, cent, imgs, manual); + #ifdef HAVE_V4L2 - else if (cnt->track.type == TRACK_TYPE_PWC) + } else if (cnt->track.type == TRACK_TYPE_PWC) { return lqos_move(cnt, dev, cent, imgs, manual); - else if (cnt->track.type == TRACK_TYPE_UVC) + } else if (cnt->track.type == TRACK_TYPE_UVC) { return uvc_move(cnt, dev, cent, imgs, manual); #endif - else if (cnt->track.type == TRACK_TYPE_IOMOJO) + + } else if (cnt->track.type == TRACK_TYPE_IOMOJO) { return iomojo_move(cnt, dev, cent, imgs); - else if (cnt->track.type == TRACK_TYPE_GENERIC) { - if (cnt->track.generic_move) + } else if (cnt->track.type == TRACK_TYPE_GENERIC) { + if (cnt->track.generic_move) { return generic_move(cnt, TRACK_MOVE, manual, 0, 0, cent, imgs); - else + } else { return cnt->track.move_wait; // FIX ME. I chose to return something reasonable. + } } MOTION_LOG(WRN, TYPE_TRACK, SHOW_ERRNO @@ -169,7 +176,9 @@ static unsigned int stepper_command(struct context *cnt, unsigned int motor, return 0; } - while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 1); + while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 1) { + continue; + } if (time(NULL) >= timeout + 2) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO,_("Status byte timeout!")); @@ -225,24 +234,33 @@ static unsigned int stepper_center(struct context *cnt, int x_offset, int y_offs stepper_command(cnt, cnt->track.motorx, STEPPER_COMMAND_SPEED, cnt->track.speed); stepper_command(cnt, cnt->track.motorx, STEPPER_COMMAND_LEFT_N, cnt->track.maxx); - while (stepper_status(cnt, cnt->track.motorx) & STEPPER_STATUS_LEFT); + while (stepper_status(cnt, cnt->track.motorx) & STEPPER_STATUS_LEFT) { + continue; + } stepper_command(cnt, cnt->track.motorx, STEPPER_COMMAND_RIGHT_N, cnt->track.maxx / 2 + x_offset * cnt->track.stepsize); - while (stepper_status(cnt, cnt->track.motorx) & STEPPER_STATUS_RIGHT); + while (stepper_status(cnt, cnt->track.motorx) & STEPPER_STATUS_RIGHT) { + continue; + } /* y-axis */ stepper_command(cnt, cnt->track.motory, STEPPER_COMMAND_SPEED, cnt->track.speed); stepper_command(cnt, cnt->track.motory, STEPPER_COMMAND_UP_N, cnt->track.maxy); - while (stepper_status(cnt, cnt->track.motory) & STEPPER_STATUS_UP) + while (stepper_status(cnt, cnt->track.motory) & STEPPER_STATUS_UP) { + continue; + } stepper_command(cnt, cnt->track.motory, STEPPER_COMMAND_DOWN_N, cnt->track.maxy / 2 + y_offset * cnt->track.stepsize); - while (stepper_status(cnt, cnt->track.motory) & STEPPER_STATUS_DOWN); + + while (stepper_status(cnt, cnt->track.motory) & STEPPER_STATUS_DOWN) { + continue; + } return cnt->track.move_wait; } @@ -283,7 +301,9 @@ static unsigned int stepper_move(struct context *cnt, data = data * cnt->track.stepsize / imgs->width; - if (data) stepper_command(cnt, cnt->track.motorx, command, data); + if (data) { + stepper_command(cnt, cnt->track.motorx, command, data); + } /* y-axis */ @@ -299,8 +319,9 @@ static unsigned int stepper_move(struct context *cnt, data = data * cnt->track.stepsize / imgs->height; - if (data) + if (data) { stepper_command(cnt, cnt->track.motory, command, data); + } return cnt->track.move_wait; @@ -368,7 +389,9 @@ static unsigned int servo_command(struct context *cnt, unsigned int motor, return 0; } - while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 1); + while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 1) { + continue; + } if (time(NULL) >= timeout + 2) { MOTION_LOG(ERR, TYPE_TRACK, NO_ERRNO,_("Status byte timeout!")); @@ -427,10 +450,11 @@ static unsigned int servo_move(struct context *cnt, struct coord *cent, if ((cnt->track.motorx_reverse && (offset > 0)) || - (!cnt->track.motorx_reverse && (offset < 0))) + (!cnt->track.motorx_reverse && (offset < 0))) { command = SERVO_COMMAND_LEFT_N; - else + } else { command = SERVO_COMMAND_RIGHT_N; + } data = abs(offset); @@ -453,10 +477,11 @@ static unsigned int servo_move(struct context *cnt, struct coord *cent, offset = cent->y * cnt->track.stepsize; if ((cnt->track.motory_reverse && (offset > 0)) || - (!cnt->track.motory_reverse && (offset < 0))) + (!cnt->track.motory_reverse && (offset < 0))) { command = SERVO_COMMAND_UP_N; - else + } else { command = SERVO_COMMAND_DOWN_N; + } data = abs(offset); @@ -478,19 +503,21 @@ static unsigned int servo_move(struct context *cnt, struct coord *cent, /* Move left */ if (cent->x < imgs->width / 2) { - if (cnt->track.motorx_reverse) + if (cnt->track.motorx_reverse) { command = SERVO_COMMAND_RIGHT_N; - else + } else { command = SERVO_COMMAND_LEFT_N; + } data = imgs->width / 2 - cent->x; } /* Move right */ if (cent->x > imgs->width / 2) { - if (cnt->track.motorx_reverse) + if (cnt->track.motorx_reverse) { command = SERVO_COMMAND_LEFT_N; - else + } else { command = SERVO_COMMAND_RIGHT_N; + } data = cent->x - imgs->width / 2; } @@ -528,19 +555,21 @@ static unsigned int servo_move(struct context *cnt, struct coord *cent, /* Move down */ if (cent->y < imgs->height / 2) { - if (cnt->track.motory_reverse) + if (cnt->track.motory_reverse) { command = SERVO_COMMAND_UP_N; - else + } else { command = SERVO_COMMAND_DOWN_N; + } data = imgs->height / 2 - cent->y; } /* Move up */ if (cent->y > imgs->height / 2) { - if (cnt->track.motory_reverse) + if (cnt->track.motory_reverse) { command = SERVO_COMMAND_DOWN_N; - else + } else { command = SERVO_COMMAND_UP_N; + } data = cent->y - imgs->height / 2; } @@ -616,10 +645,11 @@ static unsigned int servo_center(struct context *cnt, int x_offset, int y_offset ,cnt->track.stepsize); /* x-axis */ - if (cnt->track.motorx_reverse) + if (cnt->track.motorx_reverse) { x_offset_abs = (128 - cnt->track.homex) - (x_offset * cnt->track.stepsize) + 128; - else + } else { x_offset_abs = cnt->track.homex + (x_offset * cnt->track.stepsize); + } if (x_offset_abs <= cnt->track.maxx && x_offset_abs >= cnt->track.minx) { /* Set Speed , TODO : it should be done only when speed changes */ @@ -628,10 +658,11 @@ static unsigned int servo_center(struct context *cnt, int x_offset, int y_offset } /* y-axis */ - if (cnt->track.motory_reverse) + if (cnt->track.motory_reverse) { y_offset_abs = (128 - cnt->track.homey) - (y_offset * cnt->track.stepsize) + 128; - else + } else { y_offset_abs = cnt->track.homey + (y_offset * cnt->track.stepsize); + } if (y_offset_abs <= cnt->track.maxy && y_offset_abs >= cnt->track.minx) { /* Set Speed , TODO : it should be done only when speed changes */ @@ -654,11 +685,14 @@ static char iomojo_command(struct context *cnt, char *command, int len, unsigned char buffer[1]; time_t timeout = time(NULL); - if (write(cnt->track.dev, command, len) != len) + if (write(cnt->track.dev, command, len) != len) { return 0; + } if (ret) { - while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 2); + while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 2) { + continue; + } if (time(NULL) >= timeout + 2) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO,_("Return byte timeout!")); @@ -677,8 +711,9 @@ static void iomojo_setspeed(struct context *cnt, unsigned int speed) command[1] = cnt->track.iomojo_id; command[2] = speed; - if (iomojo_command(cnt, command, 3, 1) != IOMOJO_SETSPEED_RET) + if (iomojo_command(cnt, command, 3, 1) != IOMOJO_SETSPEED_RET) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO,_("Unable to set camera speed")); + } } static void iomojo_movehome(struct context *cnt) @@ -736,11 +771,13 @@ static unsigned int iomojo_center(struct context *cnt, int x_offset, int y_offse y_offset *= -1; } - if (x_offset > 180) + if (x_offset > 180) { x_offset = 180; + } - if (y_offset > 60) + if (y_offset > 60) { y_offset = 60; + } command[0] = IOMOJO_MOVEOFFSET_CMD; command[1] = cnt->track.iomojo_id; @@ -763,9 +800,11 @@ static unsigned int iomojo_move(struct context *cnt, int dev, struct coord *cent int nx = 0, ny = 0; int i; - if (dev < 0) - if (!iomojo_center(cnt, 0, 0)) + if (dev < 0) { + if (!iomojo_center(cnt, 0, 0)) { return 0; + } + } if (cent->x < imgs->width / 2) { direction |= IOMOJO_DIRECTION_LEFT; @@ -791,11 +830,13 @@ static unsigned int iomojo_move(struct context *cnt, int dev, struct coord *cent ny = ny * 72 / imgs->height; if (nx || ny) { - if (nx > 180) + if (nx > 180) { nx = 180; + } - if (ny > 60) + if (ny > 60) { ny = 60; + } command[0] = IOMOJO_MOVEOFFSET_CMD; command[1] = cnt->track.iomojo_id; @@ -805,10 +846,11 @@ static unsigned int iomojo_move(struct context *cnt, int dev, struct coord *cent iomojo_command(cnt, command, 5, 0); /* Number of frames to skip while moving */ - if (ny >= nx) + if (ny >= nx) { i = 25 * ny / 90; - else + } else { i = 25 * nx / 90; + } return i; } @@ -851,17 +893,20 @@ static unsigned int lqos_center(struct context *cnt, int dev, int x_angle, int y cnt->track.maxy = pmr.tilt_max; } - if (ioctl(dev, VIDIOCPWCMPTGANGLE, &pma) == -1) + if (ioctl(dev, VIDIOCPWCMPTGANGLE, &pma) == -1) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO ,_("ioctl VIDIOCPWCMPTGANGLE")); + } pma.absolute = 1; - if (x_angle * 100 < cnt->track.maxx && x_angle * 100 > cnt->track.minx) + if (x_angle * 100 < cnt->track.maxx && x_angle * 100 > cnt->track.minx) { pma.pan = x_angle * 100; + } - if (y_angle * 100 < cnt->track.maxy && y_angle * 100 > cnt->track.miny) + if (y_angle * 100 < cnt->track.maxy && y_angle * 100 > cnt->track.miny) { pma.tilt = y_angle * 100; + } if (ioctl(dev, VIDIOCPWCMPTSANGLE, &pma) == -1) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO @@ -885,11 +930,12 @@ static unsigned int lqos_move(struct context *cnt, int dev, struct coord *cent, /* If we are on auto track we calculate delta, otherwise we use user input in degrees times 100 */ if (!manual) { - if (delta_x > imgs->width * 3/8 && delta_x < imgs->width * 5/8) + if (delta_x > imgs->width * 3/8 && delta_x < imgs->width * 5/8) { return 0; - if (delta_y > imgs->height * 3/8 && delta_y < imgs->height * 5/8) + } + if (delta_y > imgs->height * 3/8 && delta_y < imgs->height * 5/8) { return 0; - + } move_x_degrees = delta_x * cnt->track.step_angle_x * 100 / (imgs->width / 2); move_y_degrees = -delta_y * cnt->track.step_angle_y * 100 / (imgs->height / 2); } else { @@ -912,26 +958,31 @@ static unsigned int lqos_move(struct context *cnt, int dev, struct coord *cent, } /* Get current camera position */ - if (ioctl(dev, VIDIOCPWCMPTGANGLE, &pma) == -1) + if (ioctl(dev, VIDIOCPWCMPTGANGLE, &pma) == -1) { MOTION_LOG(ERR, TYPE_TRACK, SHOW_ERRNO ,_("ioctl VIDIOCPWCMPTGANGLE")); + } /* * Check current position of camera and see if we need to adjust * values down to what is left to move */ - if (move_x_degrees < 0 && (cnt->track.minx - pma.pan) > move_x_degrees) + if (move_x_degrees < 0 && (cnt->track.minx - pma.pan) > move_x_degrees) { move_x_degrees = (cnt->track.minx - pma.pan); + } - if (move_x_degrees > 0 && (cnt->track.maxx - pma.pan) < move_x_degrees) + if (move_x_degrees > 0 && (cnt->track.maxx - pma.pan) < move_x_degrees) { move_x_degrees = (cnt->track.maxx - pma.pan); + } - if (move_y_degrees < 0 && (cnt->track.miny - pma.tilt) > move_y_degrees) + if (move_y_degrees < 0 && (cnt->track.miny - pma.tilt) > move_y_degrees) { move_y_degrees = (cnt->track.miny - pma.tilt); + } - if (move_y_degrees > 0 && (cnt->track.maxy - pma.tilt) < move_y_degrees) + if (move_y_degrees > 0 && (cnt->track.maxy - pma.tilt) < move_y_degrees) { move_y_degrees = (cnt->track.maxy - pma.tilt); + } /* Move camera relative to current position */ pma.absolute = 0; @@ -1040,11 +1091,13 @@ static unsigned int uvc_center(struct context *cnt, int dev, int x_angle, int y_ ,_("INPUT_PARAM_ABS X_Angel %d, Y_Angel %d ") ,x_angle, y_angle); - if (x_angle <= cnt->track.maxx && x_angle >= cnt->track.minx) + if (x_angle <= cnt->track.maxx && x_angle >= cnt->track.minx) { move_x_degrees = x_angle - (cnt->track.pan_angle); + } - if (y_angle <= cnt->track.maxy && y_angle >= cnt->track.miny) + if (y_angle <= cnt->track.maxy && y_angle >= cnt->track.miny) { move_y_degrees = y_angle - (cnt->track.tilt_angle); + } /* @@ -1077,8 +1130,9 @@ static unsigned int uvc_center(struct context *cnt, int dev, int x_angle, int y_ } /* DWe 30.03.07 We must wait a little,before we set the next CMD, otherwise PAN is mad ... */ - if ((move_x_degrees != 0) && (move_y_degrees != 0)) + if ((move_x_degrees != 0) && (move_y_degrees != 0)) { SLEEP(1, 0); + } if (move_y_degrees != 0) { control_s.id = V4L2_CID_TILT_RELATIVE; @@ -1179,11 +1233,12 @@ static unsigned int uvc_move(struct context *cnt, int dev, struct coord *cent, /* If we are on auto track we calculate delta, otherwise we use user input in degrees */ if (!manual) { - if (delta_x > imgs->width * 3/8 && delta_x < imgs->width * 5/8) + if (delta_x > imgs->width * 3/8 && delta_x < imgs->width * 5/8) { return 0; - if (delta_y > imgs->height * 3/8 && delta_y < imgs->height * 5/8) + } + if (delta_y > imgs->height * 3/8 && delta_y < imgs->height * 5/8) { return 0; - + } move_x_degrees = delta_x * cnt->track.step_angle_x / (imgs->width / 2); move_y_degrees = -delta_y * cnt->track.step_angle_y / (imgs->height / 2); } else { @@ -1207,17 +1262,21 @@ static unsigned int uvc_move(struct context *cnt, int dev, struct coord *cent, * Check current position of camera and see if we need to adjust * values down to what is left to move */ - if (move_x_degrees < 0 && (cnt->track.minx - cnt->track.pan_angle) > move_x_degrees) + if (move_x_degrees < 0 && (cnt->track.minx - cnt->track.pan_angle) > move_x_degrees) { move_x_degrees = cnt->track.minx - cnt->track.pan_angle; + } - if (move_x_degrees > 0 && (cnt->track.maxx - cnt->track.pan_angle) < move_x_degrees) + if (move_x_degrees > 0 && (cnt->track.maxx - cnt->track.pan_angle) < move_x_degrees) { move_x_degrees = cnt->track.maxx - cnt->track.pan_angle; + } - if (move_y_degrees < 0 && (cnt->track.miny - cnt->track.tilt_angle) > move_y_degrees) + if (move_y_degrees < 0 && (cnt->track.miny - cnt->track.tilt_angle) > move_y_degrees) { move_y_degrees = cnt->track.miny - cnt->track.tilt_angle; + } - if (move_y_degrees > 0 && (cnt->track.maxy - cnt->track.tilt_angle) < move_y_degrees) + if (move_y_degrees > 0 && (cnt->track.maxy - cnt->track.tilt_angle) < move_y_degrees) { move_y_degrees = cnt->track.maxy - cnt->track.tilt_angle; + } } MOTION_LOG(DBG, TYPE_TRACK, NO_ERRNO @@ -1261,8 +1320,9 @@ static unsigned int uvc_move(struct context *cnt, int dev, struct coord *cent, } /* DWe 30.03.07 We must wait a little,before we set the next CMD, otherwise PAN is mad ... */ - if ((move_x_degrees != 0) && (move_y_degrees != 0)) + if ((move_x_degrees != 0) && (move_y_degrees != 0)) { SLEEP (1, 0); + } if (move_y_degrees != 0) { @@ -1289,11 +1349,13 @@ static unsigned int uvc_move(struct context *cnt, int dev, struct coord *cent, ,_("Before_REL_Y_Angel : x= %d , Y= %d") ,cnt->track.pan_angle, cnt->track.tilt_angle); - if (move_x_degrees != 0) + if (move_x_degrees != 0) { cnt->track.pan_angle += -pan.s16.pan / INCPANTILT; + } - if (move_y_degrees != 0) + if (move_y_degrees != 0) { cnt->track.tilt_angle += -pan.s16.tilt / INCPANTILT; + } MOTION_LOG(DBG, TYPE_TRACK, NO_ERRNO ,_("After_REL_Y_Angel : x= %d , Y= %d") @@ -1321,8 +1383,9 @@ static unsigned int generic_move(struct context *cnt, enum track_action action, setsid(); /* Provides data as environment variables */ - if (manual) + if (manual) { setenv("TRACK_MANUAL", "manual", 1); + } switch (action) { case TRACK_CENTER: setenv("TRACK_ACTION", "center", 1); @@ -1352,8 +1415,9 @@ static unsigned int generic_move(struct context *cnt, enum track_action action, * Close any file descriptor except console because we will * like to see error messages */ - for (i = getdtablesize() - 1; i > 2; i--) + for (i = getdtablesize() - 1; i > 2; i--) { close(i); + } execl("/bin/sh", "sh", "-c", fmtcmd, " &", NULL); diff --git a/src/translate.c b/src/translate.c index 9de963d1..dfae52f5 100644 --- a/src/translate.c +++ b/src/translate.c @@ -25,7 +25,9 @@ void translate_locale_chg(const char *langcd) /* Invoke external function to change locale*/ ++_nl_msg_cat_cntr; #else - if (langcd != NULL) MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO,"No native language support"); + if (langcd != NULL) { + MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO,"No native language support"); + } #endif } @@ -58,7 +60,7 @@ void translate_init(void) char* translate_text(const char *msgid) { #ifdef HAVE_GETTEXT - if (nls_enabled){ + if (nls_enabled) { return (char*)gettext(msgid); } else { return (char*)msgid; diff --git a/src/video_bktr.c b/src/video_bktr.c index 1db22c1b..97b158e5 100644 --- a/src/video_bktr.c +++ b/src/video_bktr.c @@ -323,8 +323,9 @@ static int bktr_set_geometry(struct video_dev *viddev, int width, int height) h_max = BKTR_PAL_HEIGHT; } - if (height <= h_max / 2) + if (height <= h_max / 2) { geom.oformat |= METEOR_GEO_EVEN_ONLY; + } geom.frames = 1; @@ -346,17 +347,29 @@ static void bktr_picture_controls(struct context *cnt, struct video_dev *viddev) int indx_user, retcd; struct vdev_usrctrl_ctx *usritem; - if (!cnt->vdev->update_parms) return; + if (!cnt->vdev->update_parms) { + return; + } retcd = vid_parms_parse(cnt); - if (retcd < 0) return; + if (retcd < 0) { + return; + } - for (indx_user=0; indx_uservdev->usrctrl_count; indx_user++){ + for (indx_user=0; indx_uservdev->usrctrl_count; indx_user++) { usritem=&cnt->vdev->usrctrl_array[indx_user]; - if (!strcasecmp(usritem->ctrl_name,"contrast")) bktr_set_contrast(dev,usritem->ctrl_value); - if (!strcasecmp(usritem->ctrl_name,"hue")) bktr_set_hue(dev,usritem->ctrl_value); - if (!strcasecmp(usritem->ctrl_name,"brightness")) bktr_set_brightness(dev,usritem->ctrl_value); - if (!strcasecmp(usritem->ctrl_name,"saturation")) bktr_set_saturation(dev,usritem->ctrl_value); + if (!strcasecmp(usritem->ctrl_name,"contrast")) { + bktr_set_contrast(dev,usritem->ctrl_value); + } + if (!strcasecmp(usritem->ctrl_name,"hue")) { + bktr_set_hue(dev,usritem->ctrl_value); + } + if (!strcasecmp(usritem->ctrl_name,"brightness")) { + bktr_set_brightness(dev,usritem->ctrl_value); + } + if (!strcasecmp(usritem->ctrl_name,"saturation")) { + bktr_set_saturation(dev,usritem->ctrl_value); + } } cnt->vdev->update_parms = FALSE; @@ -510,10 +523,11 @@ static unsigned char *bktr_device_init(struct video_dev *viddev, int width, int } } - if (viddev->bktr_method == METEOR_CAP_CONTINOUS) + if (viddev->bktr_method == METEOR_CAP_CONTINOUS) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "METEORCAPTUR METEOR_CAP_CONTINOUS"); - else + } else { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "METEORCAPTUR METEOR_CAP_SINGLE"); + } SLEEP(1, 0); @@ -567,14 +581,16 @@ static int bktr_capture(struct video_dev *viddev, unsigned char *map, int width, cap_map = viddev->bktr_buffers[viddev->bktr_curbuffer]; viddev->bktr_curbuffer++; - if (viddev->bktr_curbuffer >= viddev->bktr_maxbuffer) + if (viddev->bktr_curbuffer >= viddev->bktr_maxbuffer) { viddev->bktr_curbuffer = 0; + } /* Capture */ if (viddev->bktr_method == METEOR_CAP_CONTINOUS) { - if (bktr_frame_waiting) + if (bktr_frame_waiting) { bktr_frame_waiting = 0; + } } else if (ioctl(dev_bktr, METEORCAPTUR, &single) < 0) { MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO @@ -631,20 +647,24 @@ static void bktr_set_input(struct context *cnt, struct video_dev *viddev, unsign int dummy; long frequnits = freq; - if ((dummy = bktr_set_input_device(viddev, input)) == -1) + if ((dummy = bktr_set_input_device(viddev, input)) == -1) { return; + } viddev->input = dummy; - if ((dummy = bktr_set_input_format(viddev, norm)) == -1) + if ((dummy = bktr_set_input_format(viddev, norm)) == -1) { return; + } viddev->norm = dummy; - if ((viddev->bktr_tuner != NULL) && (viddev->input == BKTR_IN_TV) && + if ((viddev->bktr_tuner != NULL) && + (viddev->input == BKTR_IN_TV) && (frequnits > 0)) { - if (bktr_set_freq(viddev, freq) == -1) + if (bktr_set_freq(viddev, freq) == -1) { return; + } } bktr_picture_controls(cnt, viddev); @@ -652,8 +672,9 @@ static void bktr_set_input(struct context *cnt, struct video_dev *viddev, unsign viddev->frequency = freq; /* skip a few frames if needed */ - for (dummy = 0; dummy < skip; dummy++) + for (dummy = 0; dummy < skip; dummy++) { bktr_capture(viddev, map, width, height); + } } else { /* No round robin - we only adjust picture controls */ bktr_picture_controls(cnt, viddev); @@ -693,8 +714,9 @@ void bktr_cleanup(struct context *cnt) pthread_mutex_lock(&bktr_mutex); while (dev) { - if (dev->fd_device == cnt->video_dev) + if (dev->fd_device == cnt->video_dev) { break; + } prev = dev; dev = dev->next; } @@ -705,15 +727,15 @@ void bktr_cleanup(struct context *cnt) cnt->video_dev = -1; /* free the information we collected regarding the controls */ - if (cnt->vdev != NULL){ - if (cnt->vdev->usrctrl_count > 0){ - for (indx=0;indxvdev->usrctrl_count;indx++){ + if (cnt->vdev != NULL) { + if (cnt->vdev->usrctrl_count > 0) { + for (indx=0;indxvdev->usrctrl_count;indx++) { free(cnt->vdev->usrctrl_array[indx].ctrl_name); cnt->vdev->usrctrl_array[indx].ctrl_name=NULL; } } cnt->vdev->usrctrl_count = 0; - if (cnt->vdev->usrctrl_array != NULL){ + if (cnt->vdev->usrctrl_array != NULL) { free(cnt->vdev->usrctrl_array); cnt->vdev->usrctrl_array = NULL; } @@ -730,8 +752,9 @@ void bktr_cleanup(struct context *cnt) MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO ,_("Closing video device %s"), dev->video_device); - if (dev->bktr_fdtuner > 0) + if (dev->bktr_fdtuner > 0) { close(dev->bktr_fdtuner); + } if (dev->fd_device > 0) { if (dev->bktr_method == METEOR_CAP_CONTINOUS) { @@ -750,10 +773,11 @@ void bktr_cleanup(struct context *cnt) pthread_mutex_lock(&bktr_mutex); /* Remove from list */ - if (prev == NULL) + if (prev == NULL) { viddevs = dev->next; - else + } else { prev->next = dev->next; + } pthread_mutex_unlock(&bktr_mutex); @@ -776,7 +800,9 @@ void bktr_cleanup(struct context *cnt) } #else - if (!cnt) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO,_("BKTR is not enabled.")); + if (!cnt) { + MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO,_("BKTR is not enabled.")); + } #endif } @@ -816,15 +842,14 @@ int bktr_start(struct context *cnt) width = conf->width; height = conf->height; - for (indx = 0; indx < cnt->vdev->params_count; indx++) - { - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "input") ){ + for (indx = 0; indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "input")) { input = atoi(cnt->vdev->params_array[indx].param_value); } - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "norm") ){ + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "norm")) { norm = atoi(cnt->vdev->params_array[indx].param_value); } - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "frequency") ){ + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "frequency")) { frequency = atol(cnt->vdev->params_array[indx].param_value); } } @@ -957,7 +982,9 @@ int bktr_start(struct context *cnt) return fd_device; #else - if (!cnt) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO,_("BKTR is not enabled.")); + if (!cnt) { + MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO,_("BKTR is not enabled.")); + } return -1; #endif @@ -981,14 +1008,17 @@ int bktr_next(struct context *cnt, struct image_data *img_data) dev = viddevs; while (dev) { - if (dev->fd_device == dev_bktr) + if (dev->fd_device == dev_bktr) { break; + } dev = dev->next; } pthread_mutex_unlock(&bktr_mutex); - if (dev == NULL) return -1; + if (dev == NULL) { + return -1; + } if (dev->owner != cnt->threadnr) { pthread_mutex_lock(&dev->mutex); @@ -1012,7 +1042,9 @@ int bktr_next(struct context *cnt, struct image_data *img_data) return ret; #else - if (!cnt || !img_data) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO,_("BKTR is not enabled.")); + if (!cnt || !img_data) { + MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO,_("BKTR is not enabled.")); + } return -1; #endif diff --git a/src/video_common.c b/src/video_common.c index 27c73da6..32e09f16 100644 --- a/src/video_common.c +++ b/src/video_common.c @@ -425,12 +425,12 @@ int vid_mjpegtoyuv420p(unsigned char *map, unsigned char *cap_map, int width, in Some cameras are sending multiple SOIs in the buffer. Move the pointer to the last SOI in the buffer and proceed. */ - while (ptr_buffer != NULL && ((size - soi_pos - 1) > 2) ){ + while (ptr_buffer != NULL && ((size - soi_pos - 1) > 2)) { soi_pos = ptr_buffer - cap_map; ptr_buffer = memmem(cap_map + soi_pos + 1, size - soi_pos - 1, "\xff\xd8", 2); } - if (soi_pos != 0){ + if (soi_pos != 0) { MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO,_("SOI position adjusted by %d bytes."), soi_pos); } @@ -489,7 +489,9 @@ void vid_greytoyuv420p(unsigned char *map, unsigned char *cap_map, int width, in int vid_parms_parse(struct context *cnt) { - if (cnt->vdev->update_params == FALSE) return 0; + if (cnt->vdev->update_params == FALSE) { + return 0; + } /* Put in the user specified parameters */ util_parms_parse(cnt->vdev, cnt->conf.video_params); @@ -678,15 +680,17 @@ int vid_next(struct context *cnt, struct image_data *img_data) #endif if (cnt->camera_type == CAMERA_TYPE_NETCAM) { - if (cnt->video_dev == -1) + if (cnt->video_dev == -1) { return NETCAM_GENERAL_ERROR; + } return netcam_next(cnt, img_data); } if (cnt->camera_type == CAMERA_TYPE_RTSP) { - if (cnt->video_dev == -1) + if (cnt->video_dev == -1) { return NETCAM_GENERAL_ERROR; + } return netcam_rtsp_next(cnt, img_data); } diff --git a/src/video_loopback.c b/src/video_loopback.c index 1c48f276..fae5cc83 100644 --- a/src/video_loopback.c +++ b/src/video_loopback.c @@ -72,7 +72,9 @@ static int vlp_open_vidpipe(void) if ((tfd = open(buffer, O_RDWR|O_CLOEXEC)) >= 0) { strncpy(pipepath, buffer, sizeof(pipepath)); - if (pipe_fd >= 0) close(pipe_fd); + if (pipe_fd >= 0) { + close(pipe_fd); + } pipe_fd = tfd; break; } @@ -83,8 +85,9 @@ static int vlp_open_vidpipe(void) closedir(dir); - if (pipe_fd >= 0) - MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO,_("Opened %s as pipe output"), pipepath); + if (pipe_fd >= 0) { + MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO,_("Opened %s as pipe output"), pipepath); + } return pipe_fd; } @@ -132,9 +135,11 @@ static void vlp_show_vcap(struct v4l2_capability *cap) MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "cap.bus_info: %s",cap->bus_info); MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "cap.card: %u.%u.%u",(vers >> 16) & 0xFF,(vers >> 8) & 0xFF,vers & 0xFF); MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "Device capabilities"); - for (i=0;cap_list[i].code;i++) - if (c & cap_list[i].code) + for (i=0;cap_list[i].code;i++) { + if (c & cap_list[i].code) { MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "%s",cap_list[i].cap); + } + } MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "------------------------"); } diff --git a/src/video_v4l2.c b/src/video_v4l2.c index 616206b9..fff7a8ca 100644 --- a/src/video_v4l2.c +++ b/src/video_v4l2.c @@ -108,7 +108,7 @@ static void v4l2_palette_init(palette_item *palette_array) palette_array[20].v4l2id = V4L2_PIX_FMT_GREY; palette_array[21].v4l2id = V4L2_PIX_FMT_H264; - for (indx=0; indx <=V4L2_PALETTE_COUNT_MAX; indx++ ){ + for (indx=0; indx <=V4L2_PALETTE_COUNT_MAX; indx++ ) { sprintf(palette_array[indx].fourcc ,"%c%c%c%c" ,palette_array[indx].v4l2id >> 0 ,palette_array[indx].v4l2id >> 8 @@ -137,7 +137,7 @@ static void v4l2_vdev_free(struct context *cnt) { /* free the information we collected regarding the controls */ - if (cnt->vdev != NULL){ + if (cnt->vdev != NULL) { util_parms_free(cnt->vdev); free(cnt->vdev); @@ -173,17 +173,19 @@ static int v4l2_ctrls_count(struct video_dev *curdev) memset(&vid_ctrl, 0, sizeof(struct v4l2_queryctrl)); vid_ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; while (xioctl (vid_source, VIDIOC_QUERYCTRL, &vid_ctrl) == 0) { - if (vid_ctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS){ + if (vid_ctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS) { vid_ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL; continue; } curdev->devctrl_count++; if (vid_ctrl.type == V4L2_CTRL_TYPE_MENU) { - for (indx = vid_ctrl.minimum; indx<=vid_ctrl.maximum; indx++){ + for (indx = vid_ctrl.minimum; indx<=vid_ctrl.maximum; indx++) { memset(&vid_menu, 0, sizeof(struct v4l2_querymenu)); vid_menu.id = vid_ctrl.id; vid_menu.index = indx; - if (xioctl(vid_source, VIDIOC_QUERYMENU, &vid_menu) == 0) curdev->devctrl_count++; + if (xioctl(vid_source, VIDIOC_QUERYMENU, &vid_menu) == 0) { + curdev->devctrl_count++; + } } } vid_ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL; @@ -203,7 +205,7 @@ static int v4l2_ctrls_list(struct video_dev *curdev) int indx, indx_ctrl; curdev->devctrl_array = NULL; - if (curdev->devctrl_count == 0 ){ + if (curdev->devctrl_count == 0 ) { MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, _("No Controls found for device")); return 0; } @@ -214,7 +216,7 @@ static int v4l2_ctrls_list(struct video_dev *curdev) vid_ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; indx_ctrl = 0; while (xioctl (vid_source, VIDIOC_QUERYCTRL, &vid_ctrl) == 0) { - if (vid_ctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS){ + if (vid_ctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS) { vid_ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL; continue; } @@ -236,11 +238,11 @@ static int v4l2_ctrls_list(struct video_dev *curdev) curdev->devctrl_array[indx_ctrl].ctrl_maximum = vid_ctrl.maximum; if (vid_ctrl.type == V4L2_CTRL_TYPE_MENU) { - for (indx = vid_ctrl.minimum; indx<=vid_ctrl.maximum; indx++){ + for (indx = vid_ctrl.minimum; indx<=vid_ctrl.maximum; indx++) { memset(&vid_menu, 0, sizeof(struct v4l2_querymenu)); vid_menu.id = vid_ctrl.id; vid_menu.index = indx; - if (xioctl(vid_source, VIDIOC_QUERYMENU, &vid_menu) == 0){ + if (xioctl(vid_source, VIDIOC_QUERYMENU, &vid_menu) == 0) { indx_ctrl++; curdev->devctrl_array[indx_ctrl].ctrl_id = vid_ctrl.id; @@ -262,11 +264,11 @@ static int v4l2_ctrls_list(struct video_dev *curdev) vid_ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL; } - if (curdev->devctrl_count != 0 ){ + if (curdev->devctrl_count != 0 ) { MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, _("---------Controls---------")); MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, _(" V4L2 ID Name and Range")); - for (indx=0; indx < curdev->devctrl_count; indx++){ - if (curdev->devctrl_array[indx].ctrl_menuitem){ + for (indx=0; indx < curdev->devctrl_count; indx++) { + if (curdev->devctrl_array[indx].ctrl_menuitem) { MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, " %s %s" ,curdev->devctrl_array[indx].ctrl_iddesc ,curdev->devctrl_array[indx].ctrl_name); @@ -293,12 +295,12 @@ static int v4l2_ctrls_set(struct video_dev *curdev) struct v4l2_control vid_ctrl; int indx_dev, retcd; - if (vid_source == NULL){ + if (vid_source == NULL) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO,_("Device not ready")); return -1; } - for (indx_dev= 0;indx_devdevctrl_count;indx_dev++){ + for (indx_dev= 0;indx_devdevctrl_count;indx_dev++) { devitem=&curdev->devctrl_array[indx_dev]; if (!devitem->ctrl_menuitem) { if (devitem->ctrl_currval != devitem->ctrl_newval) { @@ -312,10 +314,11 @@ static int v4l2_ctrls_set(struct video_dev *curdev) ,devitem->ctrl_iddesc, devitem->ctrl_name ,devitem->ctrl_newval,retcd); } else { - if (curdev->starting) + if (curdev->starting) { MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO ,_("Set control \"%s\" to value %d") ,devitem->ctrl_name, devitem->ctrl_newval); + } devitem->ctrl_currval = devitem->ctrl_newval; } @@ -333,9 +336,11 @@ static int v4l2_parms_set(struct context *cnt, struct video_dev *curdev) struct params_item_ctx *usritem; int indx_dev, indx_user; - if (cnt->conf.roundrobin_skip < 0) cnt->conf.roundrobin_skip = 1; + if (cnt->conf.roundrobin_skip < 0) { + cnt->conf.roundrobin_skip = 1; + } - if (curdev->devctrl_count == 0){ + if (curdev->devctrl_count == 0) { cnt->vdev->update_params = FALSE; return 0; } @@ -343,7 +348,7 @@ static int v4l2_parms_set(struct context *cnt, struct video_dev *curdev) for (indx_dev=0; indx_devdevctrl_count; indx_dev++ ) { devitem=&curdev->devctrl_array[indx_dev]; devitem->ctrl_newval = devitem->ctrl_default; - for (indx_user=0; indx_uservdev->params_count; indx_user++){ + for (indx_user=0; indx_uservdev->params_count; indx_user++) { usritem=&cnt->vdev->params_array[indx_user]; if ((!strcasecmp(devitem->ctrl_iddesc,usritem->param_name)) || (!strcasecmp(devitem->ctrl_name ,usritem->param_name))) { @@ -351,12 +356,12 @@ static int v4l2_parms_set(struct context *cnt, struct video_dev *curdev) case V4L2_CTRL_TYPE_MENU: /*FALLTHROUGH*/ case V4L2_CTRL_TYPE_INTEGER: - if (atoi(usritem->param_value) < devitem->ctrl_minimum){ + if (atoi(usritem->param_value) < devitem->ctrl_minimum) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO ,_("%s control option value %s is below minimum. Using minimum %d") ,devitem->ctrl_name, usritem->param_value, devitem->ctrl_minimum); devitem->ctrl_newval = devitem->ctrl_minimum; - } else if (atoi(usritem->param_value) > devitem->ctrl_maximum){ + } else if (atoi(usritem->param_value) > devitem->ctrl_maximum) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO ,_("%s control option value %s is above maximum. Using maximum %d") ,devitem->ctrl_name, usritem->param_value, devitem->ctrl_maximum); @@ -395,7 +400,9 @@ static int v4l2_autobright(struct context *cnt, struct video_dev *curdev, int me char cid_exp[15],cid_expabs[15],cid_bright[15]; - if ((method == 0) || (method > 3)) return 0; + if ((method == 0) || (method > 3)) { + return 0; + } /* Set the values for the control variables */ parm_hysteresis = 20; @@ -409,7 +416,7 @@ static int v4l2_autobright(struct context *cnt, struct video_dev *curdev, int me sprintf(cid_exp,"ID%08d",V4L2_CID_EXPOSURE); sprintf(cid_expabs,"ID%08d",V4L2_CID_EXPOSURE_ABSOLUTE); - for (indx = 0;indx < cnt->vdev->params_count; indx++){ + for (indx = 0;indx < cnt->vdev->params_count; indx++) { usritem=&cnt->vdev->params_array[indx]; if ((method == 1) && ((!strcasecmp(usritem->param_name,"brightness")) || @@ -427,36 +434,35 @@ static int v4l2_autobright(struct context *cnt, struct video_dev *curdev, int me } device_value = -1; - for (indx = 0;indx < curdev->devctrl_count; indx++){ + for (indx = 0;indx < curdev->devctrl_count; indx++) { devitem=&curdev->devctrl_array[indx]; - if ((method == 1) && - (devitem->ctrl_id == V4L2_CID_BRIGHTNESS)) { + if ((method == 1) && (devitem->ctrl_id == V4L2_CID_BRIGHTNESS)) { device_value = devitem->ctrl_currval; parm_max = devitem->ctrl_maximum; parm_min = devitem->ctrl_minimum; - if (target == -1){ + if (target == -1) { target = (int) ((devitem->ctrl_maximum - devitem->ctrl_minimum)/2); } - } else if ((method == 2) && - (devitem->ctrl_id == V4L2_CID_EXPOSURE)) { + } else if ((method == 2) && (devitem->ctrl_id == V4L2_CID_EXPOSURE)) { device_value = devitem->ctrl_currval; parm_max = devitem->ctrl_maximum; parm_min = devitem->ctrl_minimum; - if (target == -1){ + if (target == -1) { target = (int) ((devitem->ctrl_maximum - devitem->ctrl_minimum)/2); } - } else if ((method == 3) && - (devitem->ctrl_id == V4L2_CID_EXPOSURE_ABSOLUTE)) { + } else if ((method == 3) && (devitem->ctrl_id == V4L2_CID_EXPOSURE_ABSOLUTE)) { device_value = devitem->ctrl_currval; parm_max = devitem->ctrl_maximum; parm_min = devitem->ctrl_minimum; - if (target == -1){ + if (target == -1) { target = (int) ((devitem->ctrl_maximum - devitem->ctrl_minimum)/2); } } } /* If we can not find control just give up */ - if (device_value == -1) return 0; + if (device_value == -1) { + return 0; + } avg = 0; pixel_count = 0; @@ -500,17 +506,14 @@ static int v4l2_autobright(struct context *cnt, struct video_dev *curdev, int me //MOTION_LOG(INF, TYPE_VIDEO, NO_ERRNO, "Up Avg %d step: %d device:%d",avg,step,device_value); } - if (make_change){ - for (indx = 0;indx < curdev->devctrl_count; indx++){ + if (make_change) { + for (indx = 0;indx < curdev->devctrl_count; indx++) { devitem=&curdev->devctrl_array[indx]; - if ((method == 1) && - (devitem->ctrl_id == V4L2_CID_BRIGHTNESS)) { + if ((method == 1) && (devitem->ctrl_id == V4L2_CID_BRIGHTNESS)) { devitem->ctrl_newval = device_value; - } else if ((method == 2) && - (devitem->ctrl_id == V4L2_CID_EXPOSURE)) { + } else if ((method == 2) && (devitem->ctrl_id == V4L2_CID_EXPOSURE)) { devitem->ctrl_newval = device_value; - } else if ((method == 3) && - (devitem->ctrl_id == V4L2_CID_EXPOSURE_ABSOLUTE)) { + } else if ((method == 3) && (devitem->ctrl_id == V4L2_CID_EXPOSURE_ABSOLUTE)) { devitem->ctrl_newval = device_value; } } @@ -528,14 +531,16 @@ static int v4l2_input_select(struct context *cnt, struct video_dev *curdev) int indx, tmpinp; tmpinp = -1; - for (indx = 0;indx < cnt->vdev->params_count; indx++){ - if ( !strcmp(cnt->vdev->params_array[indx].param_name,"input")){ + for (indx = 0;indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name,"input")) { tmpinp = atoi(cnt->vdev->params_array[indx].param_value); break; } } - if ((tmpinp == curdev->input) && (!curdev->starting)) return 0; + if ((tmpinp == curdev->input) && (!curdev->starting)) { + return 0; + } memset(&input, 0, sizeof (struct v4l2_input)); if (tmpinp == -1 ) { @@ -552,18 +557,18 @@ static int v4l2_input_select(struct context *cnt, struct video_dev *curdev) return -1; } - if (curdev->starting){ + if (curdev->starting) { MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO ,_("Name = \"%s\", type 0x%08X, status %08x") ,input.name, input.type, input.status); } - if ((input.type & V4L2_INPUT_TYPE_TUNER) && (curdev->starting)){ + if ((input.type & V4L2_INPUT_TYPE_TUNER) && (curdev->starting)) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO ,_("Name = \"%s\",- TUNER"),input.name); } - if ((input.type & V4L2_INPUT_TYPE_CAMERA) && (curdev->starting)){ + if ((input.type & V4L2_INPUT_TYPE_CAMERA) && (curdev->starting)) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO,_("Name = \"%s\"- CAMERA"),input.name); } @@ -590,17 +595,19 @@ static int v4l2_norm_select(struct context *cnt, struct video_dev *curdev) int norm, indx; norm = 0; - for (indx = 0;indx < cnt->vdev->params_count; indx++){ - if ( !strcmp(cnt->vdev->params_array[indx].param_name,"norm")){ + for (indx = 0;indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name,"norm")) { norm = atoi(cnt->vdev->params_array[indx].param_value); break; } } - if ((norm == curdev->norm) && (!curdev->starting)) return 0; + if ((norm == curdev->norm) && (!curdev->starting)) { + return 0; + } if (xioctl(vid_source, VIDIOC_G_STD, &std_id) == -1) { - if (curdev->starting){ + if (curdev->starting) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO ,_("Device does not support specifying PAL/NTSC norm")); } @@ -612,9 +619,10 @@ static int v4l2_norm_select(struct context *cnt, struct video_dev *curdev) standard.index = 0; while (xioctl(vid_source, VIDIOC_ENUMSTD, &standard) == 0) { - if ((standard.id & std_id) && (curdev->starting)) + if ((standard.id & std_id) && (curdev->starting)) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO ,_("- video standard %s"), standard.name); + } standard.index++; } @@ -629,7 +637,7 @@ static int v4l2_norm_select(struct context *cnt, struct video_dev *curdev) std_id = V4L2_STD_PAL; } - if (xioctl(vid_source, VIDIOC_S_STD, &std_id) == -1){ + if (xioctl(vid_source, VIDIOC_S_STD, &std_id) == -1) { MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO ,_("Error selecting standard method %d VIDIOC_S_STD") ,(int)std_id); @@ -662,14 +670,16 @@ static int v4l2_frequency_select(struct context *cnt, struct video_dev *curdev) long usrfreq; usrfreq = 0; - for (indx = 0;indx < cnt->vdev->params_count; indx++){ - if ( !strcmp(cnt->vdev->params_array[indx].param_name,"frequency")){ + for (indx = 0;indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name,"frequency")) { usrfreq = atol(cnt->vdev->params_array[indx].param_value); break; } } - if ((usrfreq == curdev->frequency) && (!curdev->starting)) return 0; + if ((usrfreq == curdev->frequency) && (!curdev->starting)) { + return 0; + } /* If this input is attached to a tuner, set the frequency. */ if (curdev->device_type & V4L2_INPUT_TYPE_TUNER) { @@ -683,7 +693,7 @@ static int v4l2_frequency_select(struct context *cnt, struct video_dev *curdev) return 0; } - if (curdev->starting){ + if (curdev->starting) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, _("Set tuner %d"), tuner.index); } @@ -699,7 +709,7 @@ static int v4l2_frequency_select(struct context *cnt, struct video_dev *curdev) return 0; } - if (curdev->starting){ + if (curdev->starting) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, _("Set Frequency to %ul"), freq.frequency); } } @@ -812,14 +822,13 @@ static int v4l2_pixfmt_select(struct context *cnt, struct video_dev *curdev) } indx_palette = 17; - for (indx = 0; indx < cnt->vdev->params_count; indx++) - { - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "palette") ){ + for (indx = 0; indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "palette")) { indx_palette = atoi(cnt->vdev->params_array[indx].param_value); }; } - if (indx_palette == 21 ) { + if (indx_palette == 21) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO ,_("H264(21) format not supported via videodevice. Changing to default palette")); indx_palette = 17; @@ -828,7 +837,7 @@ static int v4l2_pixfmt_select(struct context *cnt, struct video_dev *curdev) /* First we try setting the config file value */ if ((indx_palette >= 0) && (indx_palette <= V4L2_PALETTE_COUNT_MAX)) { retcd = v4l2_pixfmt_set(cnt, curdev,palette_array[indx_palette].v4l2id); - if (retcd >= 0){ + if (retcd >= 0) { free(palette_array); return 0; } @@ -856,10 +865,12 @@ static int v4l2_pixfmt_select(struct context *cnt, struct video_dev *curdev) } /* Adjust indx_palette if larger value found */ /* Prevent the selection of H264 since this module does not support it */ - for (indx = 0; indx <= V4L2_PALETTE_COUNT_MAX; indx++) + for (indx = 0; indx <= V4L2_PALETTE_COUNT_MAX; indx++) { if ((palette_array[indx].v4l2id == fmtd.pixelformat) && - (palette_array[indx].v4l2id != V4L2_PIX_FMT_H264)) + (palette_array[indx].v4l2id != V4L2_PIX_FMT_H264)) { indx_palette = indx; + } + } memset(&fmtd, 0, sizeof(struct v4l2_fmtdesc)); fmtd.index = ++v4l2_pal; @@ -868,7 +879,7 @@ static int v4l2_pixfmt_select(struct context *cnt, struct video_dev *curdev) if (indx_palette >= 0) { retcd = v4l2_pixfmt_set(cnt, curdev, palette_array[indx_palette].v4l2id); - if (retcd >= 0){ + if (retcd >= 0) { if (curdev->starting) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO ,_("Selected palette %s") @@ -901,7 +912,9 @@ static int v4l2_mmap_set(struct video_dev *curdev) int buffer_index; /* Does the device support streaming? */ - if (!(vid_source->cap.capabilities & V4L2_CAP_STREAMING)) return -1; + if (!(vid_source->cap.capabilities & V4L2_CAP_STREAMING)) { + return -1; + } memset(&vid_source->req, 0, sizeof(struct v4l2_requestbuffers)); @@ -1055,8 +1068,9 @@ static int v4l2_capture(struct context *cnt, struct video_dev *curdev, unsigned if (errno == EIO) { vid_source->pframe++; - if ((u32)vid_source->pframe >= vid_source->req.count) + if ((u32)vid_source->pframe >= vid_source->req.count) { vid_source->pframe = 0; + } vid_source->buf.index = vid_source->pframe; MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO @@ -1178,15 +1192,14 @@ static int v4l2_device_init(struct context *cnt, struct video_dev *curdev) curdev->usage_count = 1; - for (indx = 0; indx < cnt->vdev->params_count; indx++) - { - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "input") ){ + for (indx = 0; indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "input")) { curdev->input = atoi(cnt->vdev->params_array[indx].param_value); } - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "norm") ){ + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "norm")) { curdev->norm = atoi(cnt->vdev->params_array[indx].param_value); } - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "frequency") ){ + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "frequency")) { curdev->frequency = atol(cnt->vdev->params_array[indx].param_value); } } @@ -1215,60 +1228,74 @@ static void v4l2_device_select(struct context *cnt, struct video_dev *curdev, un int indx, retcd, newvals; - if (curdev->v4l2_private == NULL){ + if (curdev->v4l2_private == NULL) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO,_("Device not ready")); return; } newvals = FALSE; - for (indx = 0; indx < cnt->vdev->params_count; indx++) - { + for (indx = 0; indx < cnt->vdev->params_count; indx++) { if (!strcmp(cnt->vdev->params_array[indx].param_name, "input") && - atoi(cnt->vdev->params_array[indx].param_value) != curdev->input) - { + atoi(cnt->vdev->params_array[indx].param_value) != curdev->input) { newvals = TRUE; } if (!strcmp(cnt->vdev->params_array[indx].param_name, "norm") && - atoi(cnt->vdev->params_array[indx].param_value) != curdev->norm) - { + atoi(cnt->vdev->params_array[indx].param_value) != curdev->norm) { newvals = TRUE; } if (!strcmp(cnt->vdev->params_array[indx].param_name, "frequency") && - atol(cnt->vdev->params_array[indx].param_value) != curdev->frequency) - { + atol(cnt->vdev->params_array[indx].param_value) != curdev->frequency) { newvals = TRUE; } } if (newvals) { retcd = v4l2_input_select(cnt, curdev); - if (retcd == 0) retcd = v4l2_norm_select(cnt, curdev); - if (retcd == 0) retcd = v4l2_frequency_select(cnt, curdev); - if (retcd == 0) retcd = vid_parms_parse(cnt); - if (retcd == 0) retcd = v4l2_parms_set(cnt, curdev); - if (retcd == 0) retcd = v4l2_autobright(cnt, curdev, cnt->conf.auto_brightness); - if (retcd == 0) retcd = v4l2_ctrls_set(curdev); - if (retcd < 0 ){ + if (retcd == 0) { + retcd = v4l2_norm_select(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_frequency_select(cnt, curdev); + } + if (retcd == 0) { + retcd = vid_parms_parse(cnt); + } + if (retcd == 0) { + retcd = v4l2_parms_set(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_autobright(cnt, curdev, cnt->conf.auto_brightness); + } + if (retcd == 0) { + retcd = v4l2_ctrls_set(curdev); + } + if (retcd < 0) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO ,_("Errors occurred during device select")); } /* Clear the buffers from previous "robin" pictures*/ - for (indx =0; indx < curdev->buffer_count; indx++){ + for (indx =0; indx < curdev->buffer_count; indx++) { v4l2_capture(cnt, curdev, map); } /* Skip the requested round robin frame count */ - for (indx = 1; indx < cnt->conf.roundrobin_skip; indx++){ + for (indx = 1; indx < cnt->conf.roundrobin_skip; indx++) { v4l2_capture(cnt, curdev, map); } } else { /* No round robin - we only adjust picture controls */ retcd = vid_parms_parse(cnt); - if (retcd == 0) retcd = v4l2_parms_set(cnt, curdev); - if (retcd == 0) retcd = v4l2_autobright(cnt, curdev, cnt->conf.auto_brightness); - if (retcd == 0) retcd = v4l2_ctrls_set(curdev); + if (retcd == 0) { + retcd = v4l2_parms_set(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_autobright(cnt, curdev, cnt->conf.auto_brightness); + } + if (retcd == 0) { + retcd = v4l2_ctrls_set(curdev); + } if (retcd < 0 ) { MOTION_LOG(WRN, TYPE_VIDEO, NO_ERRNO ,_("Errors occurred during device select")); @@ -1283,9 +1310,8 @@ static int v4l2_device_open(struct context *cnt, struct video_dev *curdev) /* Open the video device */ usrinp = -1; - for (indx = 0; indx < cnt->vdev->params_count; indx++) - { - if ( !strcmp(cnt->vdev->params_array[indx].param_name, "input") ){ + for (indx = 0; indx < cnt->vdev->params_count; indx++) { + if ( !strcmp(cnt->vdev->params_array[indx].param_name, "input")) { usrinp = atoi(cnt->vdev->params_array[indx].param_value); } } @@ -1324,11 +1350,11 @@ static void v4l2_device_close(struct video_dev *curdev) type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if (vid_source != NULL){ + if (vid_source != NULL) { xioctl(vid_source, VIDIOC_STREAMOFF, &type); } - if (vid_source->fd_device != -1){ + if (vid_source->fd_device != -1) { close(vid_source->fd_device); vid_source->fd_device = -1; } @@ -1343,20 +1369,20 @@ static void v4l2_device_cleanup(struct video_dev *curdev) int indx2; if (vid_source->buffers != NULL) { - for (indx = 0; indx < vid_source->req.count; indx++){ + for (indx = 0; indx < vid_source->req.count; indx++) { munmap(vid_source->buffers[indx].ptr, vid_source->buffers[indx].size); } free(vid_source->buffers); vid_source->buffers = NULL; } - if (vid_source != NULL){ + if (vid_source != NULL) { free(vid_source); curdev->v4l2_private = NULL; } - if (curdev->devctrl_count != 0 ){ - for (indx2=0; indx2 < curdev->devctrl_count; indx2++){ + if (curdev->devctrl_count != 0) { + for (indx2=0; indx2 < curdev->devctrl_count; indx2++) { free(curdev->devctrl_array[indx2].ctrl_iddesc); free(curdev->devctrl_array[indx2].ctrl_name); curdev->devctrl_array[indx2].ctrl_iddesc = NULL; @@ -1386,30 +1412,42 @@ static int v4l2_device_capability(struct video_dev *curdev) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, "cap.capabilities=0x%08X",vid_source->cap.capabilities); MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, "------------------------"); - if (vid_source->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) + if (vid_source->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- VIDEO_CAPTURE"); - if (vid_source->cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) + } + if (vid_source->cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- VIDEO_OUTPUT"); - if (vid_source->cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) + } + if (vid_source->cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- VIDEO_OVERLAY"); - if (vid_source->cap.capabilities & V4L2_CAP_VBI_CAPTURE) + } + if (vid_source->cap.capabilities & V4L2_CAP_VBI_CAPTURE) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- VBI_CAPTURE"); - if (vid_source->cap.capabilities & V4L2_CAP_VBI_OUTPUT) + } + if (vid_source->cap.capabilities & V4L2_CAP_VBI_OUTPUT) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- VBI_OUTPUT"); - if (vid_source->cap.capabilities & V4L2_CAP_RDS_CAPTURE) + } + if (vid_source->cap.capabilities & V4L2_CAP_RDS_CAPTURE) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- RDS_CAPTURE"); - if (vid_source->cap.capabilities & V4L2_CAP_TUNER) + } + if (vid_source->cap.capabilities & V4L2_CAP_TUNER) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- TUNER"); - if (vid_source->cap.capabilities & V4L2_CAP_AUDIO) + } + if (vid_source->cap.capabilities & V4L2_CAP_AUDIO) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- AUDIO"); - if (vid_source->cap.capabilities & V4L2_CAP_READWRITE) + } + if (vid_source->cap.capabilities & V4L2_CAP_READWRITE) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- READWRITE"); - if (vid_source->cap.capabilities & V4L2_CAP_ASYNCIO) + } + if (vid_source->cap.capabilities & V4L2_CAP_ASYNCIO) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- ASYNCIO"); - if (vid_source->cap.capabilities & V4L2_CAP_STREAMING) + } + if (vid_source->cap.capabilities & V4L2_CAP_STREAMING) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- STREAMING"); - if (vid_source->cap.capabilities & V4L2_CAP_TIMEPERFRAME) + } + if (vid_source->cap.capabilities & V4L2_CAP_TIMEPERFRAME) { MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "- TIMEPERFRAME"); + } if (!(vid_source->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { MOTION_LOG(ERR, TYPE_VIDEO, NO_ERRNO, _("Device does not support capturing.")); @@ -1481,8 +1519,12 @@ int v4l2_start(struct context *cnt) while (curdev) { if (!strcmp(cnt->conf.video_device, curdev->video_device)) { retcd = v4l2_vdev_init(cnt); - if (retcd == 0) retcd = vid_parms_parse(cnt); - if (retcd == 0) retcd = v4l2_imgs_set(cnt, curdev); + if (retcd == 0) { + retcd = vid_parms_parse(cnt); + } + if (retcd == 0) { + retcd = v4l2_imgs_set(cnt, curdev); + } if (retcd == 0) { curdev->usage_count++; retcd = curdev->fd_device; @@ -1498,31 +1540,63 @@ int v4l2_start(struct context *cnt) curdev->starting = TRUE; retcd = v4l2_vdev_init(cnt); - if (retcd == 0) retcd = vid_parms_parse(cnt); - if (retcd == 0) retcd = v4l2_device_init(cnt, curdev); - if (retcd == 0) retcd = v4l2_device_open(cnt, curdev); - if (retcd == 0) retcd = v4l2_device_capability(curdev); - if (retcd == 0) retcd = v4l2_input_select(cnt, curdev); - if (retcd == 0) retcd = v4l2_norm_select(cnt, curdev); - if (retcd == 0) retcd = v4l2_frequency_select(cnt, curdev); - if (retcd == 0) retcd = v4l2_pixfmt_select(cnt, curdev); - if (retcd == 0) retcd = v4l2_fps_set(cnt, curdev); - if (retcd == 0) retcd = v4l2_ctrls_count(curdev); - if (retcd == 0) retcd = v4l2_ctrls_list(curdev); - if (retcd == 0) retcd = v4l2_parms_set(cnt, curdev); - if (retcd == 0) retcd = v4l2_ctrls_set(curdev); - if (retcd == 0) retcd = v4l2_mmap_set(curdev); - if (retcd == 0) retcd = v4l2_imgs_set(cnt, curdev); - if (retcd < 0){ + if (retcd == 0) { + retcd = vid_parms_parse(cnt); + } + if (retcd == 0) { + retcd = v4l2_device_init(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_device_open(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_device_capability(curdev); + } + if (retcd == 0) { + retcd = v4l2_input_select(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_norm_select(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_frequency_select(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_pixfmt_select(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_fps_set(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_ctrls_count(curdev); + } + if (retcd == 0) { + retcd = v4l2_ctrls_list(curdev); + } + if (retcd == 0) { + retcd = v4l2_parms_set(cnt, curdev); + } + if (retcd == 0) { + retcd = v4l2_ctrls_set(curdev); + } + if (retcd == 0) { + retcd = v4l2_mmap_set(curdev); + } + if (retcd == 0) { + retcd = v4l2_imgs_set(cnt, curdev); + } + if (retcd < 0) { /* These may need more work to consider all the fail scenarios*/ - if (curdev->v4l2_private != NULL){ + if (curdev->v4l2_private != NULL) { free(curdev->v4l2_private); curdev->v4l2_private = NULL; } pthread_mutexattr_destroy(&curdev->attr); pthread_mutex_destroy(&curdev->mutex); v4l2_vdev_free(cnt); - if (curdev->fd_device != -1) close(curdev->fd_device); + if (curdev->fd_device != -1) { + close(curdev->fd_device); + } free(curdev); pthread_mutex_unlock(&v4l2_mutex); return retcd; @@ -1538,7 +1612,9 @@ int v4l2_start(struct context *cnt) return curdev->fd_device; #else - if (!cnt) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, _("V4L2 is not enabled.")); + if (!cnt) { + MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, _("V4L2 is not enabled.")); + } return -1; #endif // HAVE_V4l2 } @@ -1553,8 +1629,9 @@ void v4l2_cleanup(struct context *cnt) /* Cleanup the v4l2 part */ pthread_mutex_lock(&v4l2_mutex); while (dev) { - if (dev->fd_device == cnt->video_dev) + if (dev->fd_device == cnt->video_dev) { break; + } prev = dev; dev = dev->next; } @@ -1580,10 +1657,11 @@ void v4l2_cleanup(struct context *cnt) dev->fd_device = -1; /* Remove from list */ - if (prev == NULL) + if (prev == NULL) { video_devices = dev->next; - else + } else { prev->next = dev->next; + } pthread_mutexattr_destroy(&dev->attr); pthread_mutex_destroy(&dev->mutex); @@ -1606,7 +1684,9 @@ void v4l2_cleanup(struct context *cnt) #else - if (!cnt) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, _("V4L2 is not enabled.")); + if (!cnt) { + MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, _("V4L2 is not enabled.")); + } #endif // HAVE_V4L2 } @@ -1620,13 +1700,14 @@ int v4l2_next(struct context *cnt, struct image_data *img_data) pthread_mutex_lock(&v4l2_mutex); dev = video_devices; while (dev) { - if (dev->fd_device == cnt->video_dev) + if (dev->fd_device == cnt->video_dev) { break; + } dev = dev->next; } pthread_mutex_unlock(&v4l2_mutex); - if (dev == NULL){ + if (dev == NULL) { return -1; } @@ -1650,7 +1731,9 @@ int v4l2_next(struct context *cnt, struct image_data *img_data) return ret; #else - if (!cnt || !img_data) MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, _("V4L2 is not enabled.")); + if (!cnt || !img_data) { + MOTION_LOG(DBG, TYPE_VIDEO, NO_ERRNO, _("V4L2 is not enabled.")); + } return -1; #endif // HAVE_V4L2 } @@ -1689,8 +1772,9 @@ int v4l2_palette_valid(char *video_device, int v4l2_palette) fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; retcd = 0; while (xioctl(vid_source, VIDIOC_ENUM_FMT, &fmtd) != -1) { - if (palette_array[v4l2_palette].v4l2id == fmtd.pixelformat ) retcd = 1; - + if (palette_array[v4l2_palette].v4l2id == fmtd.pixelformat) { + retcd = 1; + } memset(&fmtd, 0, sizeof(struct v4l2_fmtdesc)); fmtd.index = ++device_palette; fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; @@ -1705,7 +1789,9 @@ int v4l2_palette_valid(char *video_device, int v4l2_palette) return retcd; #else /* We do not have v4l2 so we can not determine whether it is valid or not */ - if ((video_device) || (v4l2_palette)) return 0; + if ((video_device) || (v4l2_palette)) { + return 0; + } return 0; #endif // HAVE_V4L2 } @@ -1725,7 +1811,7 @@ void v4l2_palette_fourcc(int v4l2_palette, char *fourcc) v4l2_palette_init(palette_array); - if ((v4l2_palette > V4L2_PALETTE_COUNT_MAX) || (v4l2_palette < 0)){ + if ((v4l2_palette > V4L2_PALETTE_COUNT_MAX) || (v4l2_palette < 0)) { sprintf(fourcc,"%s","NULL"); } else { sprintf(fourcc,"%s",palette_array[v4l2_palette].fourcc); @@ -1736,7 +1822,9 @@ void v4l2_palette_fourcc(int v4l2_palette, char *fourcc) return; #else sprintf(fourcc,"%s","NULL"); - if (v4l2_palette) return; + if (v4l2_palette) { + return; + } return; #endif // HAVE_V4L2 } @@ -1808,7 +1896,9 @@ int v4l2_parms_valid(char *video_device, int v4l2_palette, int v4l2_fps, int v4l ((int)dev_sizes.discrete.width == v4l2_width) && ((int)dev_sizes.discrete.height == v4l2_height) && ((int)dev_frameint.discrete.numerator == 1) && - ((int)dev_frameint.discrete.denominator == v4l2_fps)) retcd = 1; + ((int)dev_frameint.discrete.denominator == v4l2_fps)) { + retcd = 1; + } memset(&dev_frameint, 0, sizeof(struct v4l2_frmivalenum)); dev_frameint.index = ++indx_frameint; dev_frameint.pixel_format = dev_format.pixelformat; @@ -1834,7 +1924,9 @@ int v4l2_parms_valid(char *video_device, int v4l2_palette, int v4l2_fps, int v4l #else /* We do not have v4l2 so we can not determine whether it is valid or not */ if ((video_device) || (v4l2_fps) || (v4l2_palette) || - (v4l2_width) || (v4l2_height) ) return 0; + (v4l2_width) || (v4l2_height)) { + return 0; + } return 0; #endif // HAVE_V4L2 } diff --git a/src/webu.c b/src/webu.c index e69ae138..cfa4c0fc 100644 --- a/src/webu.c +++ b/src/webu.c @@ -95,14 +95,17 @@ static void webu_context_init(struct context **cntlst, struct context *cnt, stru /* get the number of cameras and threads */ indx = 0; - if (webui->cntlst != NULL){ - while (webui->cntlst[++indx]); + if (webui->cntlst != NULL) { + while (webui->cntlst[++indx]) { + continue; + } } webui->cam_threads = indx; webui->cam_count = indx; - if (indx > 1) + if (indx > 1) { webui->cam_count--; + } /* 1 thread, 1 camera = just motion.conf. * 2 thread, 1 camera, then using motion.conf plus a separate camera file */ @@ -142,28 +145,35 @@ static void webu_context_null(struct webui_ctx *webui) return; } +static void webu_context_free_var(char* varin) +{ + if (varin != NULL) { + free(varin); + } +} + static void webu_context_free(struct webui_ctx *webui) { - if (webui->hostname != NULL) free(webui->hostname); - if (webui->url != NULL) free(webui->url); - if (webui->uri_camid != NULL) free(webui->uri_camid); - if (webui->uri_cmd1 != NULL) free(webui->uri_cmd1); - if (webui->uri_cmd2 != NULL) free(webui->uri_cmd2); - if (webui->uri_parm1 != NULL) free(webui->uri_parm1); - if (webui->uri_value1 != NULL) free(webui->uri_value1); - if (webui->uri_parm2 != NULL) free(webui->uri_parm2); - if (webui->uri_value2 != NULL) free(webui->uri_value2); - if (webui->lang != NULL) free(webui->lang); - if (webui->lang_full != NULL) free(webui->lang_full); - if (webui->resp_page != NULL) free(webui->resp_page); - if (webui->auth_user != NULL) free(webui->auth_user); - if (webui->auth_pass != NULL) free(webui->auth_pass); - if (webui->auth_denied != NULL) free(webui->auth_denied); - if (webui->auth_opaque != NULL) free(webui->auth_opaque); - if (webui->auth_realm != NULL) free(webui->auth_realm); - if (webui->clientip != NULL) free(webui->clientip); - if (webui->text_eol != NULL) free(webui->text_eol); + webu_context_free_var(webui->hostname); + webu_context_free_var(webui->url); + webu_context_free_var(webui->uri_camid); + webu_context_free_var(webui->uri_cmd1); + webu_context_free_var(webui->uri_cmd2); + webu_context_free_var(webui->uri_parm1); + webu_context_free_var(webui->uri_value1); + webu_context_free_var(webui->uri_parm2); + webu_context_free_var(webui->uri_value2); + webu_context_free_var(webui->lang); + webu_context_free_var(webui->lang_full); + webu_context_free_var(webui->resp_page); + webu_context_free_var(webui->auth_user); + webu_context_free_var(webui->auth_pass); + webu_context_free_var(webui->auth_denied); + webu_context_free_var(webui->auth_opaque); + webu_context_free_var(webui->auth_realm); + webu_context_free_var(webui->clientip); + webu_context_free_var(webui->text_eol); webu_context_null(webui); @@ -181,13 +191,13 @@ static void webu_badreq(struct webui_ctx *webui) * bad request response either with or without the HTML tags. */ if (webui->cnt != NULL) { - if (webui->cnt->conf.webcontrol_interface == 1){ + if (webui->cnt->conf.webcontrol_interface == 1) { webu_text_badreq(webui); } else { webu_html_badreq(webui); } } else if (webui->cntlst != NULL) { - if (webui->cntlst[0]->conf.webcontrol_interface == 1){ + if (webui->cntlst[0]->conf.webcontrol_interface == 1) { webu_text_badreq(webui); } else { webu_html_badreq(webui); @@ -209,11 +219,11 @@ void webu_write(struct webui_ctx *webui, const char *buf) resp_len = strlen(buf); temp_size = webui->resp_size; - while ((resp_len + webui->resp_used) > temp_size){ + while ((resp_len + webui->resp_used) > temp_size) { temp_size = temp_size + (WEBUI_LEN_RESP * 10); } - if (temp_size > webui->resp_size){ + if (temp_size > webui->resp_size) { temp_resp = mymalloc(webui->resp_size); memcpy(temp_resp, webui->resp_page, webui->resp_size); free(webui->resp_page); @@ -242,12 +252,14 @@ static void webu_parms_edit(struct webui_ctx *webui) */ int indx, is_nbr; - if (strlen(webui->uri_camid) > 0){ + if (strlen(webui->uri_camid) > 0) { is_nbr = TRUE; - for (indx=0; indx < (int)strlen(webui->uri_camid);indx++){ - if ((webui->uri_camid[indx] > '9') || (webui->uri_camid[indx] < '0')) is_nbr = FALSE; + for (indx=0; indx < (int)strlen(webui->uri_camid);indx++) { + if ((webui->uri_camid[indx] > '9') || (webui->uri_camid[indx] < '0')) { + is_nbr = FALSE; + } } - if (is_nbr){ + if (is_nbr) { webui->thread_nbr = atoi(webui->uri_camid); } else { webui->thread_nbr = -1; @@ -265,14 +277,14 @@ static void webu_parms_edit(struct webui_ctx *webui) * Also set/convert the camid into the thread number. */ - if (webui->cntlst != NULL){ - if (webui->thread_nbr < 0){ + if (webui->cntlst != NULL) { + if (webui->thread_nbr < 0) { webui->cnt = webui->cntlst[0]; webui->thread_nbr = 0; } else { indx = 0; - while (webui->cntlst[indx] != NULL){ - if (webui->cntlst[indx]->camera_id == webui->thread_nbr){ + while (webui->cntlst[indx] != NULL) { + if (webui->cntlst[indx]->camera_id == webui->thread_nbr) { webui->thread_nbr = indx; break; } @@ -311,66 +323,76 @@ static void webu_parseurl_parms(struct webui_ctx *webui, char *st_pos) */ last_parm = FALSE; en_pos = strstr(st_pos,"?"); - if (en_pos != NULL){ + if (en_pos != NULL) { parm_len = en_pos - st_pos + 1; - if (parm_len >= WEBUI_LEN_PARM) return; + if (parm_len >= WEBUI_LEN_PARM) { + return; + } snprintf(webui->uri_cmd2, parm_len,"%s", st_pos); /* Get the parameter name */ st_pos = st_pos + parm_len; /* Move past the command */ en_pos = strstr(st_pos,"="); - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url) - parm_len; last_parm = TRUE; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return; + if (parm_len >= WEBUI_LEN_PARM) { + return; + } snprintf(webui->uri_parm1, parm_len,"%s", st_pos); - if (!last_parm){ + if (!last_parm) { /* Get the parameter value */ st_pos = st_pos + parm_len; /* Move past the equals sign */ - if (!strcasecmp(webui->uri_parm1,"x") || !strcasecmp(webui->uri_parm1,"pan") ){ + if (!strcasecmp(webui->uri_parm1,"x") || !strcasecmp(webui->uri_parm1,"pan") ) { en_pos = strstr(st_pos,"&"); } else { en_pos = NULL; } - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url) - parm_len; last_parm = TRUE; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return; + if (parm_len >= WEBUI_LEN_PARM) { + return; + } snprintf(webui->uri_value1, parm_len,"%s", st_pos); } - if (!last_parm){ + if (!last_parm) { /* Get the next parameter name */ st_pos = st_pos + parm_len; /* Move past the previous command */ en_pos = strstr(st_pos,"="); - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url) - parm_len; last_parm = TRUE; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return; + if (parm_len >= WEBUI_LEN_PARM) { + return; + } snprintf(webui->uri_parm2, parm_len,"%s", st_pos); } - if (!last_parm){ + if (!last_parm) { /* Get the next parameter value */ st_pos = st_pos + parm_len; /* Move past the equals sign */ en_pos = strstr(st_pos,"&"); - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url) - parm_len; last_parm = TRUE; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return; + if (parm_len >= WEBUI_LEN_PARM) { + return; + } snprintf(webui->uri_value2, parm_len,"%s", st_pos); } @@ -418,14 +440,18 @@ static int webu_parseurl(struct webui_ctx *webui) webu_parseurl_reset(webui); - if (strlen(webui->url) == 0) return -1; + if (strlen(webui->url) == 0) { + return -1; + } MHD_http_unescape(webui->url); MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Decoded url: %s"),webui->url); /* Home page */ - if (strlen(webui->url) == 1) return 0; + if (strlen(webui->url) == 1) { + return 0; + } last_slash = 0; @@ -434,42 +460,54 @@ static int webu_parseurl(struct webui_ctx *webui) * stream by using http://localhost:port/stream */ st_pos = webui->url + 1; /* Move past the first "/" */ - if (*st_pos == '-') return -1; /* Never allow a negative number */ + if (*st_pos == '-') { + /* Never allow a negative number */ + return -1; + } en_pos = strstr(st_pos,"/"); - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url); last_slash = 1; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */ + if (parm_len >= WEBUI_LEN_PARM) { + /* var was malloc'd to WEBUI_LEN_PARM */ + return -1; + } snprintf(webui->uri_camid, parm_len,"%s", st_pos); - if (!last_slash){ + if (!last_slash) { /* Get cmd1 or action */ st_pos = st_pos + parm_len; /* Move past the camid */ en_pos = strstr(st_pos,"/"); - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url) - parm_len ; last_slash = 1; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */ + if (parm_len >= WEBUI_LEN_PARM) { + /* var was malloc'd to WEBUI_LEN_PARM */ + return -1; + } snprintf(webui->uri_cmd1, parm_len,"%s", st_pos); } - if (!last_slash){ + if (!last_slash) { /* Get cmd2 or action */ st_pos = st_pos + parm_len; /* Move past the first command */ en_pos = strstr(st_pos,"/"); - if (en_pos == NULL){ + if (en_pos == NULL) { parm_len = strlen(webui->url) - parm_len; last_slash = 1; } else { parm_len = en_pos - st_pos + 1; } - if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */ + if (parm_len >= WEBUI_LEN_PARM) { + /* var was malloc'd to WEBUI_LEN_PARM */ + return -1; + } snprintf(webui->uri_cmd2, parm_len,"%s", st_pos); } @@ -503,32 +541,33 @@ void webu_process_action(struct webui_ctx *webui) if ((strcmp(webui->uri_cmd2,"makemovie") == 0) || (strcmp(webui->uri_cmd2,"eventend") == 0)) { if (webui->thread_nbr == 0 && webui->cam_threads > 1) { - while (webui->cntlst[++indx]){ + while (webui->cntlst[++indx]) { webui->cntlst[indx]->event_stop = TRUE; } } else { webui->cnt->event_stop = TRUE; } - } else if (strcmp(webui->uri_cmd2,"eventstart") == 0){ + } else if (strcmp(webui->uri_cmd2,"eventstart") == 0) { if (webui->thread_nbr == 0 && webui->cam_threads > 1) { - while (webui->cntlst[++indx]){ + while (webui->cntlst[++indx]) { webui->cntlst[indx]->event_user = TRUE; } } else { webui->cnt->event_user = TRUE; } - } else if (!strcmp(webui->uri_cmd2,"snapshot")){ + } else if (!strcmp(webui->uri_cmd2,"snapshot")) { if (webui->thread_nbr == 0 && webui->cam_threads > 1) { - while (webui->cntlst[++indx]) - webui->cntlst[indx]->snapshot = 1; + while (webui->cntlst[++indx]) { + webui->cntlst[indx]->snapshot = 1; + } } else { webui->cnt->snapshot = 1; } - } else if (!strcmp(webui->uri_cmd2,"restart")){ + } else if (!strcmp(webui->uri_cmd2,"restart")) { if (webui->thread_nbr == 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, _("Restarting all threads")); webui->cntlst[0]->webcontrol_finish = TRUE; @@ -544,9 +583,9 @@ void webu_process_action(struct webui_ctx *webui) } - } else if (!strcmp(webui->uri_cmd2,"quit")){ + } else if (!strcmp(webui->uri_cmd2,"quit")) { if (webui->thread_nbr == 0 && webui->cam_threads > 1) { - while (webui->cntlst[++indx]){ + while (webui->cntlst[++indx]) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, _("Quitting thread %d"),webui->thread_nbr); webui->cntlst[indx]->restart = FALSE; @@ -563,9 +602,9 @@ void webu_process_action(struct webui_ctx *webui) webui->cnt->finish = TRUE; } - } else if (!strcmp(webui->uri_cmd2,"end")){ + } else if (!strcmp(webui->uri_cmd2,"end")) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, _("Motion terminating")); - while (webui->cntlst[indx]){ + while (webui->cntlst[indx]) { webui->cntlst[indx]->webcontrol_finish = TRUE; webui->cntlst[indx]->restart = FALSE; webui->cntlst[indx]->event_stop = TRUE; @@ -575,7 +614,7 @@ void webu_process_action(struct webui_ctx *webui) } - } else if (!strcmp(webui->uri_cmd2,"start")){ + } else if (!strcmp(webui->uri_cmd2,"start")) { if (webui->thread_nbr == 0 && webui->cam_threads > 1) { do { webui->cntlst[indx]->pause = 0; @@ -583,7 +622,7 @@ void webu_process_action(struct webui_ctx *webui) } else { webui->cnt->pause = 0; } - } else if (!strcmp(webui->uri_cmd2,"pause")){ + } else if (!strcmp(webui->uri_cmd2,"pause")) { if (webui->thread_nbr == 0 && webui->cam_threads > 1) { do { webui->cntlst[indx]->pause = 1; @@ -592,14 +631,14 @@ void webu_process_action(struct webui_ctx *webui) webui->cnt->pause = 1; } - } else if (!strcmp(webui->uri_cmd2,"connection")){ + } else if (!strcmp(webui->uri_cmd2,"connection")) { webu_text_connection(webui); - } else if (!strcmp(webui->uri_cmd2,"status")){ + } else if (!strcmp(webui->uri_cmd2,"status")) { webu_text_status(webui); } else if ((!strcmp(webui->uri_cmd2,"write")) || - (!strcmp(webui->uri_cmd2,"writeyes"))){ + (!strcmp(webui->uri_cmd2,"writeyes"))) { conf_print(webui->cntlst); } else { @@ -626,7 +665,7 @@ static int webu_process_config_set(struct webui_ctx *webui) snprintf(temp_name, WEBUI_LEN_PARM, "%s", webui->uri_parm1); indx=0; while (dep_config_params[indx].name != NULL) { - if (strcmp(dep_config_params[indx].name,webui->uri_parm1) == 0){ + if (strcmp(dep_config_params[indx].name,webui->uri_parm1) == 0) { snprintf(temp_name, WEBUI_LEN_PARM, "%s", dep_config_params[indx].newname); break; } @@ -643,14 +682,16 @@ static int webu_process_config_set(struct webui_ctx *webui) indx++; continue; } - if (!strcmp(temp_name, config_params[indx].param_name)) break; + if (!strcmp(temp_name, config_params[indx].param_name)) { + break; + } indx++; } /* If we found the parm, assign it. If the loop above did not find the parm * then we ignore the request */ - if (config_params[indx].param_name != NULL){ - if (strlen(webui->uri_parm1) > 0){ + if (config_params[indx].param_name != NULL) { + if (strlen(webui->uri_parm1) > 0) { /* This is legacy assumption on the pointers being sequential * We send in the original parm name so it will trigger the depreciated warnings * and perform any required transformations from old parm to new parm @@ -660,14 +701,14 @@ static int webu_process_config_set(struct webui_ctx *webui) /*If we are updating vid parms, set the flag to update the device.*/ if (!strcmp(config_params[indx].param_name, "vid_control_params") && - (webui->cntlst[webui->thread_nbr]->vdev != NULL)){ + (webui->cntlst[webui->thread_nbr]->vdev != NULL)) { webui->cntlst[webui->thread_nbr]->vdev->update_params = TRUE; } /* If changing language, do it now */ - if (!strcmp(config_params[indx].param_name, "native_language")){ + if (!strcmp(config_params[indx].param_name, "native_language")) { nls_enabled = webui->cntlst[webui->thread_nbr]->conf.native_language; - if (nls_enabled){ + if (nls_enabled) { MOTION_LOG(INF, TYPE_ALL, NO_ERRNO,_("Native Language : on")); } else { MOTION_LOG(INF, TYPE_ALL, NO_ERRNO,_("Native Language : off")); @@ -773,20 +814,24 @@ static void webu_clientip(struct webui_ctx *webui) int is_ipv6; is_ipv6 = FALSE; - if (webui->cnt != NULL ){ - if (webui->cnt->conf.webcontrol_ipv6) is_ipv6 = TRUE; + if (webui->cnt != NULL ) { + if (webui->cnt->conf.webcontrol_ipv6) { + is_ipv6 = TRUE; + } } else { - if (webui->cntlst[0]->conf.webcontrol_ipv6) is_ipv6 = TRUE; + if (webui->cntlst[0]->conf.webcontrol_ipv6) { + is_ipv6 = TRUE; + } } con_info = MHD_get_connection_info(webui->connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS); - if (is_ipv6){ + if (is_ipv6) { con_socket6 = (struct sockaddr_in6 *)con_info->client_addr; ip_dst = inet_ntop(AF_INET6, &con_socket6->sin6_addr, client, WEBUI_LEN_URLI); - if (ip_dst == NULL){ + if (ip_dst == NULL) { snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", "Unknown"); } else { - if (strncmp(client,"::ffff:",7) == 0){ + if (strncmp(client,"::ffff:",7) == 0) { snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", client + 7); } else { snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", client); @@ -795,7 +840,7 @@ static void webu_clientip(struct webui_ctx *webui) } else { con_socket4 = (struct sockaddr_in *)con_info->client_addr; ip_dst = inet_ntop(AF_INET, &con_socket4->sin_addr, client, WEBUI_LEN_URLI); - if (ip_dst == NULL){ + if (ip_dst == NULL) { snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", "Unknown"); } else { snprintf(webui->clientip,WEBUI_LEN_URLI,"%s",client); @@ -820,18 +865,18 @@ static void webu_hostname(struct webui_ctx *webui, int ctrl) int host_len; hdr = MHD_lookup_connection_value (webui->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_HOST); - if (hdr != NULL){ + if (hdr != NULL) { snprintf(webui->hostname, WEBUI_LEN_PARM, "%s", hdr); /* IPv6 addresses have :'s in them so special case them */ - if (webui->hostname[0] == '['){ + if (webui->hostname[0] == '[') { en_pos = strstr(webui->hostname, "]"); - if (en_pos != NULL){ + if (en_pos != NULL) { host_len = en_pos - webui->hostname + 2; snprintf(webui->hostname, host_len, "%s", hdr); } } else { en_pos = strstr(webui->hostname, ":"); - if (en_pos != NULL){ + if (en_pos != NULL) { host_len = en_pos - webui->hostname + 1; snprintf(webui->hostname, host_len, "%s", hdr); } @@ -844,14 +889,14 @@ static void webu_hostname(struct webui_ctx *webui, int ctrl) * so we can use this protocol as we are building the html page or * streams. */ - if (ctrl){ - if (webui->cnt->conf.webcontrol_tls){ + if (ctrl) { + if (webui->cnt->conf.webcontrol_tls) { snprintf(webui->hostproto,6,"%s","https"); } else { snprintf(webui->hostproto,6,"%s","http"); } } else { - if (webui->cnt->conf.stream_tls){ + if (webui->cnt->conf.stream_tls) { snprintf(webui->hostproto,6,"%s","https"); } else { snprintf(webui->hostproto,6,"%s","http"); @@ -872,7 +917,9 @@ static int webu_mhd_digest_fail(struct webui_ctx *webui,int signal_stale) response = MHD_create_response_from_buffer(strlen(webui->auth_denied) ,(void *)webui->auth_denied, MHD_RESPMEM_PERSISTENT); - if (response == NULL) return MHD_NO; + if (response == NULL) { + return MHD_NO; + } retcd = MHD_queue_auth_fail_response(webui->connection, webui->auth_realm ,webui->auth_opaque, response @@ -898,13 +945,17 @@ static int webu_mhd_digest(struct webui_ctx *webui) } /* Check for valid user name */ - if (strcmp(user, webui->auth_user) != 0){ + if (strcmp(user, webui->auth_user) != 0) { MOTION_LOG(ALR, TYPE_STREAM, NO_ERRNO ,_("Failed authentication from %s"), webui->clientip); - if (user != NULL) free(user); + if (user != NULL) { + free(user); + } return webu_mhd_digest_fail(webui, MHD_NO); } - if (user != NULL) free(user); + if (user != NULL) { + free(user); + } /* Check the password as well*/ retcd = MHD_digest_auth_check(webui->connection, webui->auth_realm @@ -935,7 +986,9 @@ static int webu_mhd_basic_fail(struct webui_ctx *webui) response = MHD_create_response_from_buffer(strlen(webui->auth_denied) ,(void *)webui->auth_denied, MHD_RESPMEM_PERSISTENT); - if (response == NULL) return MHD_NO; + if (response == NULL) { + return MHD_NO; + } retcd = MHD_queue_basic_auth_fail_response (webui->connection, webui->auth_realm, response); @@ -954,23 +1007,34 @@ static int webu_mhd_basic(struct webui_ctx *webui) user = NULL; user = MHD_basic_auth_get_username_password (webui->connection, &pass); - if ((user == NULL) || (pass == NULL)){ - if (user != NULL) free(user); - if (pass != NULL) free(pass); + if ((user == NULL) || (pass == NULL)) { + if (user != NULL) { + free(user); + } + if (pass != NULL) { + free(pass); + } return webu_mhd_basic_fail(webui); } if ((strcmp(user, webui->auth_user) != 0) || (strcmp(pass, webui->auth_pass) != 0)) { MOTION_LOG(ALR, TYPE_STREAM, NO_ERRNO ,_("Failed authentication from %s"),webui->clientip); - if (user != NULL) free(user); - if (pass != NULL) free(pass); - + if (user != NULL) { + free(user); + } + if (pass != NULL) { + free(pass); + } return webu_mhd_basic_fail(webui); } - if (user != NULL) free(user); - if (pass != NULL) free(pass); + if (user != NULL) { + free(user); + } + if (pass != NULL) { + free(pass); + } webui->authenticated = TRUE; return MHD_YES; @@ -983,15 +1047,19 @@ static void webu_mhd_auth_parse(struct webui_ctx *webui, int ctrl) char *col_pos; /* Parse apart the user:pass provided*/ - if (webui->auth_user != NULL) free(webui->auth_user); - if (webui->auth_pass != NULL) free(webui->auth_pass); + if (webui->auth_user != NULL) { + free(webui->auth_user); + } + if (webui->auth_pass != NULL) { + free(webui->auth_pass); + } webui->auth_user = NULL; webui->auth_pass = NULL; if (ctrl){ auth_len = strlen(webui->cnt->conf.webcontrol_authentication); col_pos = strstr(webui->cnt->conf.webcontrol_authentication,":"); - if (col_pos == NULL){ + if (col_pos == NULL) { webui->auth_user = mymalloc(auth_len+1); webui->auth_pass = mymalloc(2); snprintf(webui->auth_user, auth_len + 1, "%s" @@ -1007,7 +1075,7 @@ static void webu_mhd_auth_parse(struct webui_ctx *webui, int ctrl) } else { auth_len = strlen(webui->cnt->conf.stream_authentication); col_pos = strstr(webui->cnt->conf.stream_authentication,":"); - if (col_pos == NULL){ + if (col_pos == NULL) { webui->auth_user = mymalloc(auth_len+1); webui->auth_pass = mymalloc(2); snprintf(webui->auth_user, auth_len + 1, "%s" @@ -1041,39 +1109,42 @@ static int webu_mhd_auth(struct webui_ctx *webui, int ctrl) snprintf(webui->auth_realm, WEBUI_LEN_PARM, "%s","Motion"); - if (ctrl){ + if (ctrl) { /* Authentication for the webcontrol*/ - if (webui->cnt->conf.webcontrol_authentication == NULL){ + if (webui->cnt->conf.webcontrol_authentication == NULL) { webui->authenticated = TRUE; - if (webui->cnt->conf.webcontrol_auth_method != 0){ + if (webui->cnt->conf.webcontrol_auth_method != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("No webcontrol user:pass provided")); } return MHD_YES; } - if (webui->auth_user == NULL) webu_mhd_auth_parse(webui, ctrl); - - if (webui->cnt->conf.webcontrol_auth_method == 1){ - return webu_mhd_basic(webui); - } else if (webui->cnt->conf.webcontrol_auth_method == 2){ - return webu_mhd_digest(webui); + if (webui->auth_user == NULL) { + webu_mhd_auth_parse(webui, ctrl); } + if (webui->cnt->conf.webcontrol_auth_method == 1) { + return webu_mhd_basic(webui); + } else if (webui->cnt->conf.webcontrol_auth_method == 2) { + return webu_mhd_digest(webui); + } } else { /* Authentication for the streams */ - if (webui->cnt->conf.stream_authentication == NULL){ + if (webui->cnt->conf.stream_authentication == NULL) { webui->authenticated = TRUE; - if (webui->cnt->conf.stream_auth_method != 0){ + if (webui->cnt->conf.stream_auth_method != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("No stream user:pass provided")); } return MHD_YES; } - if (webui->auth_user == NULL) webu_mhd_auth_parse(webui, ctrl); + if (webui->auth_user == NULL) { + webu_mhd_auth_parse(webui, ctrl); + } if (webui->cnt->conf.stream_auth_method == 1) { return webu_mhd_basic(webui); - } else if (webui->cnt->conf.stream_auth_method == 2){ + } else if (webui->cnt->conf.stream_auth_method == 2) { return webu_mhd_digest(webui); } } @@ -1099,24 +1170,24 @@ static int webu_mhd_send(struct webui_ctx *webui, int ctrl) response = MHD_create_response_from_buffer (strlen(webui->resp_page) ,(void *)webui->resp_page, MHD_RESPMEM_PERSISTENT); - if (!response){ + if (!response) { MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, _("Invalid response")); return MHD_NO; } - if (webui->cnt != NULL){ - if (ctrl){ - if (webui->cnt->conf.webcontrol_cors_header != NULL){ + if (webui->cnt != NULL) { + if (ctrl) { + if (webui->cnt->conf.webcontrol_cors_header != NULL) { MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN , webui->cnt->conf.webcontrol_cors_header); } - if (webui->cnt->conf.webcontrol_interface == 1){ + if (webui->cnt->conf.webcontrol_interface == 1) { MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain;"); } else { MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); } } else { - if (webui->cnt->conf.stream_cors_header != NULL){ + if (webui->cnt->conf.stream_cors_header != NULL) { MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN , webui->cnt->conf.stream_cors_header); } @@ -1192,7 +1263,7 @@ static mymhd_retcd webu_answer_ctrl(void *cls return MHD_YES; } - if (strcmp (method, "GET") != 0){ + if (strcmp (method, "GET") != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Invalid Method requested: %s"),method); return MHD_NO; } @@ -1204,15 +1275,17 @@ static mymhd_retcd webu_answer_ctrl(void *cls webui->connection = connection; /* Throw bad URLS back to user*/ - if ((webui->cnt == NULL) || (strlen(webui->url) == 0)){ + if ((webui->cnt == NULL) || (strlen(webui->url) == 0)) { webu_badreq(webui); retcd = webu_mhd_send(webui, FALSE); return retcd; } - if (webui->cnt->webcontrol_finish) return MHD_NO; + if (webui->cnt->webcontrol_finish) { + return MHD_NO; + } - if (strlen(webui->clientip) == 0){ + if (strlen(webui->clientip) == 0) { webu_clientip(webui); } @@ -1220,7 +1293,9 @@ static mymhd_retcd webu_answer_ctrl(void *cls if (!webui->authenticated) { retcd = webu_mhd_auth(webui, TRUE); - if (!webui->authenticated) return retcd; + if (!webui->authenticated) { + return retcd; + } } if ((webui->cntlst[0]->conf.webcontrol_interface == 1) || @@ -1231,7 +1306,7 @@ static mymhd_retcd webu_answer_ctrl(void *cls } retcd = webu_mhd_send(webui, TRUE); - if (retcd == MHD_NO){ + if (retcd == MHD_NO) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("send page failed %d"),retcd); } return retcd; @@ -1265,7 +1340,7 @@ static mymhd_retcd webu_answer_strm(void *cls return MHD_YES; } - if (strcmp (method, "GET") != 0){ + if (strcmp (method, "GET") != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Invalid Method requested: %s"),method); return MHD_NO; } @@ -1275,7 +1350,7 @@ static mymhd_retcd webu_answer_strm(void *cls webui->connection = connection; /* Throw bad URLS back to user*/ - if ((webui->cnt == NULL) || (strlen(webui->url) == 0)){ + if ((webui->cnt == NULL) || (strlen(webui->url) == 0)) { webu_badreq(webui); retcd = webu_mhd_send(webui, FALSE); return retcd; @@ -1292,7 +1367,7 @@ static mymhd_retcd webu_answer_strm(void *cls return MHD_NO; } - if (strlen(webui->clientip) == 0){ + if (strlen(webui->clientip) == 0) { webu_clientip(webui); } @@ -1300,21 +1375,23 @@ static mymhd_retcd webu_answer_strm(void *cls if (!webui->authenticated) { retcd = webu_mhd_auth(webui, FALSE); - if (!webui->authenticated) return retcd; + if (!webui->authenticated) { + return retcd; + } } webu_answer_strm_type(webui); retcd = 0; - if (webui->cnct_type == WEBUI_CNCT_STATIC){ + if (webui->cnct_type == WEBUI_CNCT_STATIC) { retcd = webu_stream_static(webui); - if (retcd == MHD_NO){ + if (retcd == MHD_NO) { webu_badreq(webui); retcd = webu_mhd_send(webui, FALSE); } } else if (webui->cnct_type != WEBUI_CNCT_UNKNOWN) { retcd = webu_stream_mjpeg(webui); - if (retcd == MHD_NO){ + if (retcd == MHD_NO) { webu_badreq(webui); retcd = webu_mhd_send(webui, FALSE); } @@ -1323,7 +1400,7 @@ static mymhd_retcd webu_answer_strm(void *cls retcd = webu_mhd_send(webui, FALSE); } - if (retcd == MHD_NO){ + if (retcd == MHD_NO) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("send page failed %d"),retcd); } return retcd; @@ -1362,7 +1439,7 @@ static void *webu_mhd_init(void *cls, const char *uri, struct MHD_Connection *co snprintf(webui->url,WEBUI_LEN_URLI,"%s",uri); retcd = webu_parseurl(webui); - if (retcd != 0){ + if (retcd != 0) { webu_parseurl_reset(webui); memset(webui->url,'\0',WEBUI_LEN_URLI); } @@ -1397,7 +1474,7 @@ static void *webu_mhd_init_one(void *cls, const char *uri, struct MHD_Connection snprintf(webui->url,WEBUI_LEN_URLI,"%s",uri); retcd = webu_parseurl(webui); - if (retcd != 0){ + if (retcd != 0) { webu_parseurl_reset(webui); memset(webui->url,'\0',WEBUI_LEN_URLI); } @@ -1420,27 +1497,27 @@ static void webu_mhd_deinit(void *cls (void)cls; (void)toe; - if (webui->cnct_type == WEBUI_CNCT_FULL ){ + if (webui->cnct_type == WEBUI_CNCT_FULL ) { pthread_mutex_lock(&webui->cnt->mutex_stream); webui->cnt->stream_norm.cnct_count--; pthread_mutex_unlock(&webui->cnt->mutex_stream); - } else if (webui->cnct_type == WEBUI_CNCT_SUB ){ + } else if (webui->cnct_type == WEBUI_CNCT_SUB ) { pthread_mutex_lock(&webui->cnt->mutex_stream); webui->cnt->stream_sub.cnct_count--; pthread_mutex_unlock(&webui->cnt->mutex_stream); - } else if (webui->cnct_type == WEBUI_CNCT_MOTION ){ + } else if (webui->cnct_type == WEBUI_CNCT_MOTION ) { pthread_mutex_lock(&webui->cnt->mutex_stream); webui->cnt->stream_motion.cnct_count--; pthread_mutex_unlock(&webui->cnt->mutex_stream); - } else if (webui->cnct_type == WEBUI_CNCT_SOURCE ){ + } else if (webui->cnct_type == WEBUI_CNCT_SOURCE ) { pthread_mutex_lock(&webui->cnt->mutex_stream); webui->cnt->stream_source.cnct_count--; pthread_mutex_unlock(&webui->cnt->mutex_stream); - } else if (webui->cnct_type == WEBUI_CNCT_STATIC ){ + } else if (webui->cnct_type == WEBUI_CNCT_STATIC ) { pthread_mutex_lock(&webui->cnt->mutex_stream); webui->cnt->stream_norm.cnct_count--; pthread_mutex_unlock(&webui->cnt->mutex_stream); @@ -1460,13 +1537,13 @@ static void webu_mhd_features_basic(struct mhdstart_ctx *mhdst) #else int retcd; retcd = MHD_is_feature_supported (MHD_FEATURE_BASIC_AUTH); - if (retcd == MHD_YES){ + if (retcd == MHD_YES) { MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO ,_("Basic authentication: available")); } else { - if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_auth_method == 1)){ + if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_auth_method == 1)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Basic authentication: disabled")); mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_auth_method = 0; - } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_auth_method == 1)){ + } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_auth_method == 1)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Basic authentication: disabled")); mhdst->cnt[mhdst->indxthrd]->conf.stream_auth_method = 0; } else { @@ -1484,13 +1561,13 @@ static void webu_mhd_features_digest(struct mhdstart_ctx *mhdst) #else int retcd; retcd = MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH); - if (retcd == MHD_YES){ + if (retcd == MHD_YES) { MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO ,_("Digest authentication: available")); } else { - if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_auth_method == 2)){ + if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_auth_method == 2)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Digest authentication: disabled")); mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_auth_method = 0; - } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_auth_method == 2)){ + } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_auth_method == 2)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Digest authentication: disabled")); mhdst->cnt[mhdst->indxthrd]->conf.stream_auth_method = 0; } else { @@ -1507,18 +1584,22 @@ static void webu_mhd_features_ipv6(struct mhdstart_ctx *mhdst) * IPv4 */ #if MHD_VERSION < 0x00094400 - if (mhdst->ipv6){ + if (mhdst->ipv6) { MOTION_LOG(INF, TYPE_STREAM, NO_ERRNO ,_("libmicrohttpd libary too old ipv6 disabled")); - if (mhdst->ipv6) mhdst->ipv6 = 0; + if (mhdst->ipv6) { + mhdst->ipv6 = 0; + } } #else int retcd; retcd = MHD_is_feature_supported (MHD_FEATURE_IPv6); - if (retcd == MHD_YES){ + if (retcd == MHD_YES) { MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO ,_("IPV6: available")); } else { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("IPV6: disabled")); - if (mhdst->ipv6) mhdst->ipv6 = 0; + if (mhdst->ipv6) { + mhdst->ipv6 = 0; + } } #endif } @@ -1530,7 +1611,7 @@ static void webu_mhd_features_tls(struct mhdstart_ctx *mhdst) * support the ssl/tls request. */ #if MHD_VERSION < 0x00094400 - if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls)){ + if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls)) { MOTION_LOG(INF, TYPE_STREAM, NO_ERRNO ,_("libmicrohttpd libary too old SSL/TLS disabled")); mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls = 0; } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls)) { @@ -1540,13 +1621,13 @@ static void webu_mhd_features_tls(struct mhdstart_ctx *mhdst) #else int retcd; retcd = MHD_is_feature_supported (MHD_FEATURE_SSL); - if (retcd == MHD_YES){ + if (retcd == MHD_YES) { MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO ,_("SSL/TLS: available")); } else { - if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls)){ + if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("SSL/TLS: disabled")); mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls = 0; - } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls)){ + } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("SSL/TLS: disabled")); mhdst->cnt[mhdst->indxthrd]->conf.stream_tls = 0; } else { @@ -1587,14 +1668,14 @@ static char *webu_mhd_loadfile(const char *fname) file_char = NULL; } else { infile = fopen(fname, "rb"); - if (infile != NULL){ + if (infile != NULL) { fseek(infile, 0, SEEK_END); file_size = ftell(infile); - if (file_size > 0 ){ + if (file_size > 0 ) { file_char = mymalloc(file_size +1); fseek(infile, 0, SEEK_SET); read_size = fread(file_char, file_size, 1, infile); - if (read_size > 0 ){ + if (read_size > 0 ) { file_char[file_size] = 0; } else { free(file_char); @@ -1619,8 +1700,8 @@ static void webu_mhd_checktls(struct mhdstart_ctx *mhdst) * they also need to provide a certificate and key file. If those are not provided * then we revise the configuration request for ssl/tls */ - if (mhdst->ctrl){ - if (mhdst->cnt[0]->conf.webcontrol_tls){ + if (mhdst->ctrl) { + if (mhdst->cnt[0]->conf.webcontrol_tls) { if ((mhdst->cnt[0]->conf.webcontrol_cert == NULL) || (mhdst->tls_cert == NULL)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("SSL/TLS requested but no cert file provided. SSL/TLS disabled")); @@ -1633,7 +1714,7 @@ static void webu_mhd_checktls(struct mhdstart_ctx *mhdst) } } } else { - if (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls){ + if (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls) { if ((mhdst->cnt[0]->conf.webcontrol_cert == NULL) || (mhdst->tls_cert == NULL)) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("SSL/TLS requested but no cert file provided. SSL/TLS disabled")); @@ -1658,7 +1739,7 @@ static void webu_mhd_opts_init(struct mhdstart_ctx *mhdst) * each camera), then we call a different init function which only wants the single * motion context for that particular camera. */ - if ((!mhdst->ctrl) && (mhdst->indxthrd != 0)){ + if ((!mhdst->ctrl) && (mhdst->indxthrd != 0)) { mhdst->mhd_ops[mhdst->mhd_opt_nbr].option = MHD_OPTION_URI_LOG_CALLBACK; mhdst->mhd_ops[mhdst->mhd_opt_nbr].value = (intptr_t)webu_mhd_init_one; mhdst->mhd_ops[mhdst->mhd_opt_nbr].ptr_value = mhdst->cnt[mhdst->indxthrd]; @@ -1688,8 +1769,8 @@ static void webu_mhd_opts_localhost(struct mhdstart_ctx *mhdst) * motion configuation option of localhost only. */ - if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_localhost)){ - if (mhdst->ipv6){ + if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_localhost)) { + if (mhdst->ipv6) { memset(&mhdst->lpbk_ipv6, 0, sizeof(struct sockaddr_in6)); mhdst->lpbk_ipv6.sin6_family = AF_INET6; mhdst->lpbk_ipv6.sin6_port = htons(mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_port); @@ -1711,8 +1792,8 @@ static void webu_mhd_opts_localhost(struct mhdstart_ctx *mhdst) mhdst->mhd_ops[mhdst->mhd_opt_nbr].ptr_value = (struct sockaddr *)(&mhdst->lpbk_ipv4); mhdst->mhd_opt_nbr++; } - } else if((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_localhost)){ - if (mhdst->ipv6){ + } else if((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_localhost)) { + if (mhdst->ipv6) { memset(&mhdst->lpbk_ipv6, 0, sizeof(struct sockaddr_in6)); mhdst->lpbk_ipv6.sin6_family = AF_INET6; mhdst->lpbk_ipv6.sin6_port = htons(mhdst->cnt[mhdst->indxthrd]->conf.stream_port); @@ -1821,11 +1902,13 @@ static void webu_mhd_flags(struct mhdstart_ctx *mhdst) /* This sets the MHD startup flags based upon what user put into configuration */ mhdst->mhd_flags = MHD_USE_THREAD_PER_CONNECTION; - if (mhdst->ipv6) mhdst->mhd_flags = mhdst->mhd_flags | MHD_USE_DUAL_STACK; + if (mhdst->ipv6) { + mhdst->mhd_flags = mhdst->mhd_flags | MHD_USE_DUAL_STACK; + } - if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls)){ + if ((mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.webcontrol_tls)) { mhdst->mhd_flags = mhdst->mhd_flags | MHD_USE_SSL; - } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls)){ + } else if ((!mhdst->ctrl) && (mhdst->cnt[mhdst->indxthrd]->conf.stream_tls)) { mhdst->mhd_flags = mhdst->mhd_flags | MHD_USE_SSL; } @@ -1858,7 +1941,7 @@ static void webu_start_ctrl(struct context **cnt) ,sizeof(cnt[0]->webcontrol_digest_rand),"%d",randnbr); cnt[0]->webcontrol_daemon = NULL; - if (cnt[0]->conf.webcontrol_port != 0 ){ + if (cnt[0]->conf.webcontrol_port != 0 ) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Starting webcontrol on port %d") ,cnt[0]->conf.webcontrol_port); @@ -1875,7 +1958,7 @@ static void webu_start_ctrl(struct context **cnt) ,MHD_OPTION_ARRAY, mhdst.mhd_ops ,MHD_OPTION_END); free(mhdst.mhd_ops); - if (cnt[0]->webcontrol_daemon == NULL){ + if (cnt[0]->webcontrol_daemon == NULL) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Unable to start MHD")); } else { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO @@ -1884,8 +1967,12 @@ static void webu_start_ctrl(struct context **cnt) } } - if (mhdst.tls_cert != NULL) free(mhdst.tls_cert); - if (mhdst.tls_key != NULL) free(mhdst.tls_key); + if (mhdst.tls_cert != NULL) { + free(mhdst.tls_cert); + } + if (mhdst.tls_key != NULL) { + free(mhdst.tls_key); + } return; } @@ -1894,10 +1981,10 @@ static void webu_strm_ntc(struct context **cnt, int indxthrd) { int indx; - if (indxthrd == 0 ){ + if (indxthrd == 0 ) { if (cnt[1] != NULL) { indx = 1; - while (cnt[indx] != NULL){ + while (cnt[indx] != NULL) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Started camera %d stream on port/camera_id %d/%d") ,cnt[indx]->camera_id @@ -1940,10 +2027,10 @@ static void webu_start_strm(struct context **cnt) snprintf(cnt[0]->webstream_digest_rand ,sizeof(cnt[0]->webstream_digest_rand),"%d",randnbr); - while (cnt[mhdst.indxthrd] != NULL){ + while (cnt[mhdst.indxthrd] != NULL) { cnt[mhdst.indxthrd]->webstream_daemon = NULL; - if (cnt[mhdst.indxthrd]->conf.stream_port != 0 ){ - if (mhdst.indxthrd == 0){ + if (cnt[mhdst.indxthrd]->conf.stream_port != 0 ) { + if (mhdst.indxthrd == 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Starting all camera streams on port %d") ,cnt[mhdst.indxthrd]->conf.stream_port); @@ -1958,7 +2045,7 @@ static void webu_start_strm(struct context **cnt) webu_mhd_features(&mhdst); webu_mhd_opts(&mhdst); webu_mhd_flags(&mhdst); - if (mhdst.indxthrd == 0){ + if (mhdst.indxthrd == 0) { cnt[mhdst.indxthrd]->webstream_daemon = MHD_start_daemon (mhdst.mhd_flags ,cnt[mhdst.indxthrd]->conf.stream_port ,NULL, NULL @@ -1974,7 +2061,7 @@ static void webu_start_strm(struct context **cnt) ,MHD_OPTION_END); } free(mhdst.mhd_ops); - if (cnt[mhdst.indxthrd]->webstream_daemon == NULL){ + if (cnt[mhdst.indxthrd]->webstream_daemon == NULL) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Unable to start stream for camera %d") ,cnt[mhdst.indxthrd]->camera_id); @@ -1984,8 +2071,12 @@ static void webu_start_strm(struct context **cnt) } mhdst.indxthrd++; } - if (mhdst.tls_cert != NULL) free(mhdst.tls_cert); - if (mhdst.tls_key != NULL) free(mhdst.tls_key); + if (mhdst.tls_cert != NULL) { + free(mhdst.tls_cert); + } + if (mhdst.tls_key != NULL) { + free(mhdst.tls_key); + } return; } @@ -1998,14 +2089,14 @@ static void webu_start_ports(struct context **cnt) */ int indx, indx2; - if (cnt[0]->conf.webcontrol_port != 0){ + if (cnt[0]->conf.webcontrol_port != 0) { indx = 0; - while (cnt[indx] != NULL){ - if ((cnt[0]->conf.webcontrol_port == cnt[indx]->conf.webcontrol_port) && (indx > 0)){ + while (cnt[indx] != NULL) { + if ((cnt[0]->conf.webcontrol_port == cnt[indx]->conf.webcontrol_port) && (indx > 0)) { cnt[indx]->conf.webcontrol_port = 0; } - if (cnt[0]->conf.webcontrol_port == cnt[indx]->conf.stream_port){ + if (cnt[0]->conf.webcontrol_port == cnt[indx]->conf.stream_port) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Duplicate port requested %d") ,cnt[indx]->conf.stream_port); @@ -2018,12 +2109,12 @@ static void webu_start_ports(struct context **cnt) /* Now check on the stream ports */ indx = 0; - while (cnt[indx] != NULL){ - if (cnt[indx]->conf.stream_port != 0){ + while (cnt[indx] != NULL) { + if (cnt[indx]->conf.stream_port != 0) { indx2 = indx + 1; - while (cnt[indx2] != NULL){ - if (cnt[indx]->conf.stream_port == cnt[indx2]->conf.stream_port){ - if (indx != 0){ + while (cnt[indx2] != NULL) { + if (cnt[indx]->conf.stream_port == cnt[indx2]->conf.stream_port) { + if (indx != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO ,_("Duplicate port requested %d") ,cnt[indx2]->conf.stream_port); @@ -2044,15 +2135,15 @@ void webu_stop(struct context **cnt) */ int indxthrd; - if (cnt[0]->webcontrol_daemon != NULL){ + if (cnt[0]->webcontrol_daemon != NULL) { cnt[0]->webcontrol_finish = TRUE; MHD_stop_daemon (cnt[0]->webcontrol_daemon); } indxthrd = 0; - while (cnt[indxthrd] != NULL){ - if (cnt[indxthrd]->webstream_daemon != NULL){ + while (cnt[indxthrd] != NULL) { + if (cnt[indxthrd]->webstream_daemon != NULL) { cnt[indxthrd]->webcontrol_finish = TRUE; MHD_stop_daemon (cnt[indxthrd]->webstream_daemon); } @@ -2080,7 +2171,7 @@ void webu_start(struct context **cnt) indxthrd = 0; - while (cnt[indxthrd] != NULL){ + while (cnt[indxthrd] != NULL) { cnt[indxthrd]->webstream_daemon = NULL; cnt[indxthrd]->webcontrol_daemon = NULL; cnt[indxthrd]->webcontrol_finish = FALSE; diff --git a/src/webu_html.c b/src/webu_html.c index 5900ec65..6cffaba1 100644 --- a/src/webu_html.c +++ b/src/webu_html.c @@ -256,9 +256,9 @@ static void webu_html_navbar_camera(struct webui_ctx *webui) char response[WEBUI_LEN_RESP]; int indx; - if (webui->cam_threads == 1){ + if (webui->cam_threads == 1) { /* Only Motion.conf file */ - if (webui->cntlst[0]->conf.camera_name == NULL){ + if (webui->cntlst[0]->conf.camera_name == NULL) { snprintf(response, sizeof (response), "
\n" " \n" @@ -279,7 +279,7 @@ static void webu_html_navbar_camera(struct webui_ctx *webui) ,webui->cntlst[0]->conf.camera_name); webu_write(webui, response); } - } else if (webui->cam_threads > 1){ + } else if (webui->cam_threads > 1) { /* Motion.conf + separate camera.conf file */ snprintf(response, sizeof (response), "
\n" @@ -290,8 +290,8 @@ static void webu_html_navbar_camera(struct webui_ctx *webui) ,_("All")); webu_write(webui, response); - for (indx=1;indx <= webui->cam_count;indx++){ - if (webui->cntlst[indx]->conf.camera_name == NULL){ + for (indx=1;indx <= webui->cam_count;indx++) { + if (webui->cntlst[indx]->conf.camera_name == NULL) { snprintf(response, sizeof (response), " %s %d\n" ,webui->cntlst[indx]->camera_id @@ -379,15 +379,15 @@ static void webu_html_config_notice(struct webui_ctx *webui) */ char response[WEBUI_LEN_RESP]; - if (webui->cntlst[0]->conf.webcontrol_parms == 0){ + if (webui->cntlst[0]->conf.webcontrol_parms == 0) { snprintf(response, sizeof (response), "

webcontrol_parms = 0 (%s)

\n" ,_("No Configuration Options")); - } else if (webui->cntlst[0]->conf.webcontrol_parms == 1){ + } else if (webui->cntlst[0]->conf.webcontrol_parms == 1) { snprintf(response, sizeof (response), "

webcontrol_parms = 1 (%s)

\n" ,_("Limited Configuration Options")); - } else if (webui->cntlst[0]->conf.webcontrol_parms == 2){ + } else if (webui->cntlst[0]->conf.webcontrol_parms == 2) { snprintf(response, sizeof (response), "

webcontrol_parms = 2 (%s)

\n" ,_("Advanced Configuration Options")); @@ -404,7 +404,7 @@ static void webu_html_h3desc(struct webui_ctx *webui) /* Write out the status description for the camera */ char response[WEBUI_LEN_RESP]; - if (webui->cam_threads == 1){ + if (webui->cam_threads == 1) { snprintf(response, sizeof (response), "
\n" "

%s (%s)

\n" @@ -461,10 +461,10 @@ static void webu_html_config(struct webui_ctx *webui) */ val_temp=malloc(PATH_MAX); indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || - (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER)){ + (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER)) { indx_parm++; continue; } @@ -477,15 +477,15 @@ static void webu_html_config(struct webui_ctx *webui) webu_write(webui, response); memset(val_temp,'\0',PATH_MAX); - if (val_main != NULL){ + if (val_main != NULL) { snprintf(response, sizeof (response),"%s", val_main); webu_write(webui, response); snprintf(val_temp, PATH_MAX,"%s", val_main); } /* Loop through all the treads and see if any have a different value from motion.conf */ - if (webui->cam_threads > 1){ - for (indx=1;indx <= webui->cam_count;indx++){ + if (webui->cam_threads > 1) { + for (indx=1;indx <= webui->cam_count;indx++) { val_thread=config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, indx); diff_vals = FALSE; if (((strlen(val_temp) == 0) && (val_thread == NULL)) || @@ -494,9 +494,11 @@ static void webu_html_config(struct webui_ctx *webui) } else if (((strlen(val_temp) == 0) && (val_thread != NULL)) ) { diff_vals = TRUE; } else { - if (strcasecmp(val_temp, val_thread)) diff_vals = TRUE; + if (strcasecmp(val_temp, val_thread)) { + diff_vals = TRUE; + } } - if (diff_vals){ + if (diff_vals) { snprintf(response, sizeof (response),"%s","\" \\ \n"); webu_write(webui, response); @@ -504,7 +506,7 @@ static void webu_html_config(struct webui_ctx *webui) " data-cam_%05d=\"" ,webui->cntlst[indx]->camera_id); webu_write(webui, response); - if (val_thread != NULL){ + if (val_thread != NULL) { snprintf(response, sizeof (response),"%s", val_thread); webu_write(webui, response); } @@ -514,7 +516,7 @@ static void webu_html_config(struct webui_ctx *webui) /* Terminate the open quote and option. For foreign language put hint in () */ if (!strcasecmp(webui->lang,"en") || !strcasecmp(config_params[indx_parm].param_name - ,_(config_params[indx_parm].param_name))){ + ,_(config_params[indx_parm].param_name))) { snprintf(response, sizeof (response),"\" >%s\n", config_params[indx_parm].param_name); webu_write(webui, response); @@ -603,20 +605,20 @@ static void webu_html_strminfo(struct strminfo_ctx *strm_info, int indx) snprintf(strm_info->proto,WEBUI_LEN_LNK,"%s","http"); } } - if (strm_info->motion_images){ + if (strm_info->motion_images) { snprintf(strm_info->lnk_ref,WEBUI_LEN_LNK,"%s","/motion"); snprintf(strm_info->lnk_src,WEBUI_LEN_LNK,"%s","/motion"); } else { /* Assign what images and links we want */ - if (strm_info->cntlst[indx]->conf.stream_preview_method == 1){ + if (strm_info->cntlst[indx]->conf.stream_preview_method == 1) { /* Substream for preview */ snprintf(strm_info->lnk_ref,WEBUI_LEN_LNK,"%s","/stream"); snprintf(strm_info->lnk_src,WEBUI_LEN_LNK,"%s","/substream"); - } else if (strm_info->cntlst[indx]->conf.stream_preview_method == 2){ + } else if (strm_info->cntlst[indx]->conf.stream_preview_method == 2) { /* Static image for preview */ snprintf(strm_info->lnk_ref,WEBUI_LEN_LNK,"%s","/stream"); snprintf(strm_info->lnk_src,WEBUI_LEN_LNK,"%s","/current"); - } else if (strm_info->cntlst[indx]->conf.stream_preview_method == 4){ + } else if (strm_info->cntlst[indx]->conf.stream_preview_method == 4) { /* Source image for preview */ snprintf(strm_info->lnk_ref,WEBUI_LEN_LNK,"%s","/source"); snprintf(strm_info->lnk_src,WEBUI_LEN_LNK,"%s","/source"); @@ -646,16 +648,19 @@ static void webu_html_preview(struct webui_ctx *webui) "

\n"); webu_write(webui, response); - indx_st = 1; - if (webui->cam_threads == 1) indx_st = 0; + if (webui->cam_threads == 1) { + indx_st = 0; + } else { + indx_st = 1; + } - for (indx = indx_st; indxcam_threads; indx++){ - if (webui->cntlst[indx]->conf.stream_preview_newline){ + for (indx = indx_st; indxcam_threads; indx++) { + if (webui->cntlst[indx]->conf.stream_preview_newline) { snprintf(response, sizeof (response),"%s","
\n"); webu_write(webui, response); } - if (webui->cntlst[indx]->conf.stream_preview_method == 3){ + if (webui->cntlst[indx]->conf.stream_preview_method == 3) { preview_scale = 45; } else { preview_scale = webui->cntlst[indx]->conf.stream_preview_scale; @@ -673,7 +678,7 @@ static void webu_html_preview(struct webui_ctx *webui) ,preview_scale); webu_write(webui, response); - if (webui->cntlst[indx]->conf.stream_preview_method == 3){ + if (webui->cntlst[indx]->conf.stream_preview_method == 3) { strm_info.motion_images = TRUE; webu_html_strminfo(&strm_info,indx); snprintf(response, sizeof (response), @@ -715,10 +720,10 @@ static void webu_html_script_action(struct webui_ctx *webui) snprintf(response, sizeof (response),"%s", " function action_click(actval) {\n" - " if (actval == \"config\"){\n" + " if (actval == \"config\") {\n" " document.getElementById('trk_form').style.display=\"none\";\n" " document.getElementById('cfg_form').style.display=\"inline\";\n" - " } else if (actval == \"track\"){\n" + " } else if (actval == \"track\") {\n" " document.getElementById('cfg_form').style.display=\"none\";\n" " document.getElementById('trk_form').style.display=\"inline\";\n" " } else {\n" @@ -740,7 +745,7 @@ static void webu_html_script_action(struct webui_ctx *webui) webu_write(webui, response); snprintf(response, sizeof (response), - " if (camnbr == \"all00\"){\n" + " if (camnbr == \"all00\") {\n" " url = url + \"%05d\";\n" " } else {\n" " url = url + camnbr;\n" @@ -771,18 +776,21 @@ static void webu_html_script_camera_thread(struct webui_ctx *webui) int indx, indx_st, preview_scale; struct strminfo_ctx strm_info; - indx_st = 1; - if (webui->cam_threads == 1) indx_st = 0; + if (webui->cam_threads == 1) { + indx_st = 0; + } else { + indx_st = 1; + } strm_info.cntlst = webui->cntlst; - for (indx = indx_st; indxcam_threads; indx++){ + for (indx = indx_st; indxcam_threads; indx++) { snprintf(response, sizeof (response), - " if (camid == \"cam_%05d\"){\n" + " if (camid == \"cam_%05d\") {\n" ,webui->cntlst[indx]->camera_id); webu_write(webui, response); - if (webui->cntlst[indx]->conf.stream_preview_method == 3){ + if (webui->cntlst[indx]->conf.stream_preview_method == 3) { preview_scale = 45; } else { preview_scale = 95; @@ -799,7 +807,7 @@ static void webu_html_script_camera_thread(struct webui_ctx *webui) ,strm_info.lnk_camid, strm_info.lnk_src, preview_scale); webu_write(webui, response); - if (webui->cntlst[indx]->conf.stream_preview_method == 3){ + if (webui->cntlst[indx]->conf.stream_preview_method == 3) { strm_info.motion_images = TRUE; webu_html_strminfo(&strm_info, indx); snprintf(response, sizeof (response), @@ -812,7 +820,7 @@ static void webu_html_script_camera_thread(struct webui_ctx *webui) webu_write(webui, response); } - if (webui->cntlst[indx]->conf.camera_name == NULL){ + if (webui->cntlst[indx]->conf.camera_name == NULL) { snprintf(response, sizeof (response), " header=\"

%s %d (%s)

\"\n" @@ -848,27 +856,29 @@ static void webu_html_script_camera_all(struct webui_ctx *webui) int indx, indx_st, preview_scale; struct strminfo_ctx strm_info; - - indx_st = 1; - if (webui->cam_threads == 1) indx_st = 0; + if (webui->cam_threads == 1) { + indx_st = 0; + } else { + indx_st = 1; + } strm_info.cntlst = webui->cntlst; - snprintf(response, sizeof (response), " if (camid == \"cam_all00\"){\n"); + snprintf(response, sizeof (response), " if (camid == \"cam_all00\") {\n"); webu_write(webui, response); - for (indx = indx_st; indxcam_threads; indx++){ - if (indx == indx_st){ + for (indx = indx_st; indxcam_threads; indx++) { + if (indx == indx_st) { snprintf(response, sizeof (response),"%s"," preview = \"\";\n"); webu_write(webui, response); } - if (webui->cntlst[indx]->conf.stream_preview_newline){ + if (webui->cntlst[indx]->conf.stream_preview_newline) { snprintf(response, sizeof (response),"%s"," preview = preview + \"
\";\n"); webu_write(webui, response); } - if (webui->cntlst[indx]->conf.stream_preview_method == 3){ + if (webui->cntlst[indx]->conf.stream_preview_method == 3) { preview_scale = 45; } else { preview_scale = webui->cntlst[indx]->conf.stream_preview_scale; @@ -886,7 +896,7 @@ static void webu_html_script_camera_all(struct webui_ctx *webui) ,preview_scale); webu_write(webui, response); - if (webui->cntlst[indx]->conf.stream_preview_method == 3){ + if (webui->cntlst[indx]->conf.stream_preview_method == 3) { strm_info.motion_images = TRUE; webu_html_strminfo(&strm_info, indx); snprintf(response, sizeof (response), @@ -947,7 +957,7 @@ static void webu_html_script_menucam(struct webui_ctx *webui) snprintf(response, sizeof (response),"%s", " function display_cameras() {\n" " document.getElementById('act_btn').style.display = 'none';\n" - " if (document.getElementById('cam_btn').style.display == 'block'){\n" + " if (document.getElementById('cam_btn').style.display == 'block') {\n" " document.getElementById('cam_btn').style.display = 'none';\n" " } else {\n" " document.getElementById('cam_btn').style.display = 'block';\n" @@ -965,7 +975,7 @@ static void webu_html_script_menuact(struct webui_ctx *webui) snprintf(response, sizeof (response),"%s", " function display_actions() {\n" " document.getElementById('cam_btn').style.display = 'none';\n" - " if (document.getElementById('act_btn').style.display == 'block'){\n" + " if (document.getElementById('act_btn').style.display == 'block') {\n" " document.getElementById('act_btn').style.display = 'none';\n" " } else {\n" " document.getElementById('act_btn').style.display = 'block';\n" @@ -1020,7 +1030,7 @@ static void webu_html_script_cfgclk(struct webui_ctx *webui) snprintf(response, sizeof (response), " var optval=encodeURI(baseval);\n" - " if (camnbr == \"all00\"){\n" + " if (camnbr == \"all00\") {\n" " url = url + \"%05d\";\n" " } else {\n" " url = url + camnbr;\n" @@ -1054,7 +1064,7 @@ static void webu_html_script_cfgchg(struct webui_ctx *webui) " var camSel = 'data-'+ document.getElementById('h3_cam').getAttribute('data-cam');\n" " var opts = document.getElementById('cfg_parms');\n" " var optval = opts.options[opts.selectedIndex].getAttribute(camSel);\n" - " if (optval == null){\n" + " if (optval == null) {\n" " optval = opts.options[opts.selectedIndex].getAttribute('data-cam_all00');\n" " }\n" " document.getElementById('cfg_value').value = optval;\n" @@ -1070,7 +1080,7 @@ static void webu_html_script_trkchg(struct webui_ctx *webui) " function track_change() {\n" " var opts = document.getElementById('trk_option');\n" " var optval = opts.options[opts.selectedIndex].getAttribute('data-trk');\n" - " if (optval == 'pan'){\n" + " if (optval == 'pan') {\n" " document.getElementById('trk_panx').disabled=false;\n" " document.getElementById('trk_tilty').disabled = false;\n" " document.getElementById('trk_lblx').style.display='none';\n" @@ -1080,7 +1090,7 @@ static void webu_html_script_trkchg(struct webui_ctx *webui) webu_write(webui, response); snprintf(response, sizeof (response),"%s", - " } else if (optval =='abs'){\n" + " } else if (optval =='abs') {\n" " document.getElementById('trk_panx').disabled=false;\n" " document.getElementById('trk_tilty').disabled = false;\n" " document.getElementById('trk_lblx').value = 'X';\n" @@ -1123,7 +1133,7 @@ static void webu_html_script_trkclk(struct webui_ctx *webui) webu_write(webui, response); snprintf(response, sizeof (response), - " if (camnbr == \"all00\"){\n" + " if (camnbr == \"all00\") {\n" " url = url + \"%05d\";\n" " } else {\n" " url = url + camnbr;\n" @@ -1133,7 +1143,7 @@ static void webu_html_script_trkclk(struct webui_ctx *webui) snprintf(response, sizeof (response),"%s", - " if (optsel == 'pan'){\n" + " if (optsel == 'pan') {\n" " url = url + '/track/set?pan=' + optval1 + '&tilt=' + optval2;\n" " } else if (optsel == 'abs') {\n" " url = url + '/track/set?x=' + optval1 + '&y=' + optval2;\n" @@ -1267,13 +1277,13 @@ void webu_html_main(struct webui_ctx *webui) } else if (!strcmp(webui->uri_cmd1,"config")) { retcd = webu_process_config(webui); - } else if (!strcmp(webui->uri_cmd1,"action")){ + } else if (!strcmp(webui->uri_cmd1,"action")) { webu_process_action(webui); - } else if (!strcmp(webui->uri_cmd1,"detection")){ + } else if (!strcmp(webui->uri_cmd1,"detection")) { webu_process_action(webui); - } else if (!strcmp(webui->uri_cmd1,"track")){ + } else if (!strcmp(webui->uri_cmd1,"track")) { retcd = webu_process_track(webui); } else{ @@ -1283,7 +1293,9 @@ void webu_html_main(struct webui_ctx *webui) retcd = -1; } - if (retcd < 0) webu_html_badreq(webui); + if (retcd < 0) { + webu_html_badreq(webui); + } return; } diff --git a/src/webu_stream.c b/src/webu_stream.c index 03666f2c..2981325f 100644 --- a/src/webu_stream.c +++ b/src/webu_stream.c @@ -21,8 +21,10 @@ static void webu_stream_mjpeg_checkbuffers(struct webui_ctx *webui) { /* Allocate buffers if needed */ - if (webui->resp_size < (size_t)webui->cnt->imgs.size_norm){ - if (webui->resp_page != NULL) free(webui->resp_page); + if (webui->resp_size < (size_t)webui->cnt->imgs.size_norm) { + if (webui->resp_page != NULL) { + free(webui->resp_page); + } webui->resp_page = mymalloc(webui->cnt->imgs.size_norm); memset(webui->resp_page,'\0',webui->cnt->imgs.size_norm); webui->resp_size = webui->cnt->imgs.size_norm; @@ -48,12 +50,16 @@ static void webu_stream_mjpeg_delay(struct webui_ctx *webui) */ stream_delay = ((time_curr.tv_usec - webui->time_last.tv_usec)*1000) + ((time_curr.tv_sec - webui->time_last.tv_sec)*1000000000); - if (stream_delay < 0) stream_delay = 0; - if (stream_delay > 1000000000 ) stream_delay = 1000000000; + if (stream_delay < 0) { + stream_delay = 0; + } + if (stream_delay > 1000000000 ) { + stream_delay = 1000000000; + } - if (webui->stream_fps >= 1){ + if (webui->stream_fps >= 1) { stream_rate = ( (1000000000 / webui->stream_fps) - stream_delay); - if ((stream_rate > 0) && (stream_rate < 1000000000)){ + if ((stream_rate > 0) && (stream_rate < 1000000000)) { SLEEP(0,stream_rate); } else if (stream_rate == 1000000000) { SLEEP(1,0); @@ -73,16 +79,16 @@ static void webu_stream_mjpeg_getimg(struct webui_ctx *webui) memset(webui->resp_page, '\0', webui->resp_size); /* Assign to a local pointer the stream we want */ - if (webui->cnct_type == WEBUI_CNCT_FULL){ + if (webui->cnct_type == WEBUI_CNCT_FULL) { local_stream = &webui->cnt->stream_norm; - } else if (webui->cnct_type == WEBUI_CNCT_SUB){ + } else if (webui->cnct_type == WEBUI_CNCT_SUB) { local_stream = &webui->cnt->stream_sub; - } else if (webui->cnct_type == WEBUI_CNCT_MOTION){ + } else if (webui->cnct_type == WEBUI_CNCT_MOTION) { local_stream = &webui->cnt->stream_motion; - } else if (webui->cnct_type == WEBUI_CNCT_SOURCE){ + } else if (webui->cnct_type == WEBUI_CNCT_SOURCE) { local_stream = &webui->cnt->stream_source; } else { @@ -91,7 +97,7 @@ static void webu_stream_mjpeg_getimg(struct webui_ctx *webui) /* Copy jpg from the motion loop thread */ pthread_mutex_lock(&webui->cnt->mutex_stream); - if ((!webui->cnt->detecting_motion) && (webui->cnt->conf.stream_motion)){ + if ((!webui->cnt->detecting_motion) && (webui->cnt->conf.stream_motion)) { webui->stream_fps = 1; } else { webui->stream_fps = webui->cnt->conf.stream_maxrate; @@ -131,9 +137,11 @@ static ssize_t webu_stream_mjpeg_response (void *cls, uint64_t pos, char *buf, s (void)pos; /*Remove compiler warning */ - if (webui->cnt->webcontrol_finish) return -1; + if (webui->cnt->webcontrol_finish) { + return -1; + } - if ((webui->stream_pos == 0) || (webui->resp_used == 0)){ + if ((webui->stream_pos == 0) || (webui->resp_used == 0)) { webu_stream_mjpeg_delay(webui); @@ -142,7 +150,9 @@ static ssize_t webu_stream_mjpeg_response (void *cls, uint64_t pos, char *buf, s webu_stream_mjpeg_getimg(webui); - if (webui->resp_used == 0) return 0; + if (webui->resp_used == 0) { + return 0; + } } if ((webui->resp_used - webui->stream_pos) > max) { @@ -154,7 +164,7 @@ static ssize_t webu_stream_mjpeg_response (void *cls, uint64_t pos, char *buf, s memcpy(buf, webui->resp_page + webui->stream_pos, sent_bytes); webui->stream_pos = webui->stream_pos + sent_bytes; - if (webui->stream_pos >= webui->resp_used){ + if (webui->stream_pos >= webui->resp_used) { webui->stream_pos = 0; } @@ -172,7 +182,7 @@ static void webu_stream_static_getimg(struct webui_ctx *webui) memset(webui->resp_page, '\0', webui->resp_size); pthread_mutex_lock(&webui->cnt->mutex_stream); - if (webui->cnt->stream_norm.jpeg_data == NULL){ + if (webui->cnt->stream_norm.jpeg_data == NULL) { pthread_mutex_unlock(&webui->cnt->mutex_stream); return; } @@ -189,13 +199,13 @@ static int webu_stream_checks(struct webui_ctx *webui) /* Perform edits to determine whether the user specified a valid URL * for the particular port */ - if ((webui->cntlst != NULL) && (webui->thread_nbr >= webui->cam_threads)){ + if ((webui->cntlst != NULL) && (webui->thread_nbr >= webui->cam_threads)) { MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO , _("Invalid thread specified: %s"),webui->url); return -1; } - if ((webui->cntlst != NULL) && (webui->thread_nbr < 0) && (webui->cam_threads > 1)){ + if ((webui->cntlst != NULL) && (webui->thread_nbr < 0) && (webui->cam_threads > 1)) { MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO , _("Invalid thread specified: %s"),webui->url); return -1; @@ -258,7 +268,7 @@ static void webu_stream_cnct_count(struct webui_ctx *webui) pthread_mutex_unlock(&webui->cnt->mutex_stream); } - if (cnct_count == 1){ + if (cnct_count == 1) { /* This is the first connection so we need to wait half a sec * so that the motion loop on the other thread can update image */ @@ -273,7 +283,9 @@ int webu_stream_mjpeg(struct webui_ctx *webui) int retcd; struct MHD_Response *response; - if (webu_stream_checks(webui) == -1) return MHD_NO; + if (webu_stream_checks(webui) == -1) { + return MHD_NO; + } webu_stream_cnct_count(webui); @@ -283,12 +295,12 @@ int webu_stream_mjpeg(struct webui_ctx *webui) response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024 ,&webu_stream_mjpeg_response, webui, NULL); - if (!response){ + if (!response) { MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, _("Invalid response")); return MHD_NO; } - if (webui->cnt->conf.stream_cors_header != NULL){ + if (webui->cnt->conf.stream_cors_header != NULL) { MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN , webui->cnt->conf.stream_cors_header); } @@ -309,7 +321,9 @@ int webu_stream_static(struct webui_ctx *webui) struct MHD_Response *response; char resp_used[20]; - if (webu_stream_checks(webui) == -1) return MHD_NO; + if (webu_stream_checks(webui) == -1) { + return MHD_NO; + } webu_stream_cnct_count(webui); @@ -324,12 +338,12 @@ int webu_stream_static(struct webui_ctx *webui) response = MHD_create_response_from_buffer (webui->resp_size ,(void *)webui->resp_page, MHD_RESPMEM_MUST_COPY); - if (!response){ + if (!response) { MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, _("Invalid response")); return MHD_NO; } - if (webui->cnt->conf.stream_cors_header != NULL){ + if (webui->cnt->conf.stream_cors_header != NULL) { MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN , webui->cnt->conf.stream_cors_header); } diff --git a/src/webu_text.c b/src/webu_text.c index 9b3ef9ef..b2e428a2 100644 --- a/src/webu_text.c +++ b/src/webu_text.c @@ -39,7 +39,7 @@ static void webu_text_camera_name(struct webui_ctx *webui) { char response[WEBUI_LEN_RESP]; - if (webui->cntlst[webui->thread_nbr]->conf.camera_name == NULL){ + if (webui->cntlst[webui->thread_nbr]->conf.camera_name == NULL) { snprintf(response,sizeof(response), "Camera %s %s\n" ,webui->uri_camid,webui->text_eol @@ -126,7 +126,7 @@ static void webu_text_page_raw(struct webui_ctx *webui) ); webu_write(webui, response); - if (webui->cam_threads > 1){ + if (webui->cam_threads > 1) { for (indx = 1; indx < webui->cam_threads; indx++) { snprintf(response, sizeof (response), "%d \n" @@ -152,9 +152,9 @@ static void webu_text_page_basic(struct webui_ctx *webui) ,webui->cntlst[0]->camera_id); webu_write(webui, response); - if (webui->cam_threads > 1){ + if (webui->cam_threads > 1) { for (indx = 1; indx < webui->cam_threads; indx++) { - if (webui->cntlst[indx]->conf.camera_name == NULL){ + if (webui->cntlst[indx]->conf.camera_name == NULL) { snprintf(response, sizeof (response), "Camera %d
\n" , webui->cntlst[indx]->camera_id @@ -181,17 +181,17 @@ static void webu_text_list_raw(struct webui_ctx *webui) const char *val_parm; indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER) || - ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))){ + ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))) { indx_parm++; continue; } val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, webui->thread_nbr); - if (val_parm == NULL){ + if (val_parm == NULL) { val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, 0); } snprintf(response, sizeof (response), @@ -227,17 +227,17 @@ static void webu_text_list_basic(struct webui_ctx *webui) webu_write(webui, response); indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER) || - ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))){ + ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))) { indx_parm++; continue; } val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, webui->thread_nbr); - if (val_parm == NULL){ + if (val_parm == NULL) { val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, 0); } snprintf(response, sizeof (response), @@ -284,17 +284,17 @@ static void webu_text_set_menu(struct webui_ctx *webui) webu_write(webui, response); indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER) || - ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))){ + ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))) { indx_parm++; continue; } val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, webui->thread_nbr); - if (val_parm == NULL){ + if (val_parm == NULL) { val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, 0); } snprintf(response, sizeof(response), @@ -337,7 +337,7 @@ static void webu_text_set_query(struct webui_ctx *webui) webu_text_camera_name(webui); indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER) || @@ -348,7 +348,7 @@ static void webu_text_set_query(struct webui_ctx *webui) } val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, webui->thread_nbr); - if (val_parm == NULL){ + if (val_parm == NULL) { val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, 0); } @@ -380,7 +380,7 @@ static void webu_text_set_assign(struct webui_ctx *webui) retcd = webu_process_config(webui); - if (retcd == 0){ + if (retcd == 0) { webu_text_header(webui); webu_text_back(webui,"/config"); @@ -419,11 +419,11 @@ static void webu_text_get_menu(struct webui_ctx *webui) webu_write(webui, response); indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER) || - ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))){ + ((webui->thread_nbr != 0) && (config_params[indx_parm].main_thread != 0))) { indx_parm++; continue; } @@ -655,33 +655,33 @@ static void webu_text_action(struct webui_ctx *webui) { /* Call the action functions */ - if (!strcmp(webui->uri_cmd2,"makemovie")){ + if (!strcmp(webui->uri_cmd2,"makemovie")) { webu_text_action_makemovie(webui); - } else if (strcmp(webui->uri_cmd2,"eventstart") == 0){ + } else if (strcmp(webui->uri_cmd2,"eventstart") == 0) { webu_text_action_eventstart(webui); - } else if (!strcmp(webui->uri_cmd2,"eventend")){ + } else if (!strcmp(webui->uri_cmd2,"eventend")) { webu_text_action_eventend(webui); - } else if (!strcmp(webui->uri_cmd2,"snapshot")){ + } else if (!strcmp(webui->uri_cmd2,"snapshot")) { webu_text_action_snapshot(webui); - } else if (!strcmp(webui->uri_cmd2,"restart")){ + } else if (!strcmp(webui->uri_cmd2,"restart")) { webu_text_action_restart(webui); - } else if (!strcmp(webui->uri_cmd2,"start")){ + } else if (!strcmp(webui->uri_cmd2,"start")) { webu_text_action_start(webui); - } else if (!strcmp(webui->uri_cmd2,"pause")){ + } else if (!strcmp(webui->uri_cmd2,"pause")) { webu_text_action_pause(webui); } else if ((!strcmp(webui->uri_cmd2,"quit")) || - (!strcmp(webui->uri_cmd2,"end"))){ + (!strcmp(webui->uri_cmd2,"end"))) { webu_text_action_quit(webui); } else if ((!strcmp(webui->uri_cmd2,"write")) || - (!strcmp(webui->uri_cmd2,"writeyes"))){ + (!strcmp(webui->uri_cmd2,"writeyes"))) { webu_text_action_write(webui); } else { @@ -729,7 +729,7 @@ static void webu_text_track(struct webui_ctx *webui) int retcd; retcd = webu_process_track(webui); - if (retcd == 0){ + if (retcd == 0) { webu_text_header(webui); webu_text_back(webui,"/track"); @@ -909,7 +909,7 @@ void webu_text_get_query(struct webui_ctx *webui) snprintf(temp_name, WEBUI_LEN_PARM, "%s", webui->uri_value1); indx_parm=0; while (dep_config_params[indx_parm].name != NULL) { - if (strcmp(dep_config_params[indx_parm].name, webui->uri_value1) == 0){ + if (strcmp(dep_config_params[indx_parm].name, webui->uri_value1) == 0) { snprintf(temp_name, WEBUI_LEN_PARM, "%s", dep_config_params[indx_parm].newname); break; } @@ -917,22 +917,22 @@ void webu_text_get_query(struct webui_ctx *webui) } indx_parm = 0; - while (config_params[indx_parm].param_name != NULL){ + while (config_params[indx_parm].param_name != NULL) { if ((config_params[indx_parm].webui_level > webui->cntlst[0]->conf.webcontrol_parms) || (config_params[indx_parm].webui_level == WEBUI_LEVEL_NEVER) || strcmp(webui->uri_parm1,"query") || - strcmp(temp_name, config_params[indx_parm].param_name)){ + strcmp(temp_name, config_params[indx_parm].param_name)) { indx_parm++; continue; } val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, webui->thread_nbr); - if (val_parm == NULL){ + if (val_parm == NULL) { val_parm = config_params[indx_parm].print(webui->cntlst, NULL, indx_parm, 0); } - if (strcmp(webui->uri_value1, config_params[indx_parm].param_name) != 0){ + if (strcmp(webui->uri_value1, config_params[indx_parm].param_name) != 0) { MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO , _("'%s' option is depreciated. New option name is '%s'") ,webui->uri_value1, config_params[indx_parm].param_name); @@ -967,7 +967,7 @@ void webu_text_get_query(struct webui_ctx *webui) break; } - if (config_params[indx_parm].param_name == NULL){ + if (config_params[indx_parm].param_name == NULL) { webu_text_badreq(webui); } @@ -984,9 +984,12 @@ void webu_text_status(struct webui_ctx *webui) webu_text_back(webui,"/detection"); - if (webui->thread_nbr == 0){ - indx_st = 1; - if (webui->cam_threads == 1) indx_st = 0; + if (webui->thread_nbr == 0) { + if (webui->cam_threads == 1) { + indx_st = 0; + } else { + indx_st = 1; + } for (indx = indx_st; indx < webui->cam_threads; indx++) { snprintf(response, sizeof(response), @@ -1023,9 +1026,12 @@ void webu_text_connection(struct webui_ctx *webui) webu_text_camera_name(webui); - if (webui->thread_nbr == 0){ - indx_st = 1; - if (webui->cam_threads == 1) indx_st = 0; + if (webui->thread_nbr == 0) { + if (webui->cam_threads == 1) { + indx_st = 0; + } else { + indx_st = 1; + } for (indx = indx_st; indx < webui->cam_threads; indx++) { snprintf(response,sizeof(response) @@ -1072,7 +1078,7 @@ void webu_text_main(struct webui_ctx *webui) webu_text_seteol(webui); - if (strlen(webui->uri_camid) == 0){ + if (strlen(webui->uri_camid) == 0) { if (webui->cntlst[0]->conf.webcontrol_interface == 2) { webu_text_page_basic(webui); } else { @@ -1134,11 +1140,11 @@ void webu_text_main(struct webui_ctx *webui) webu_text_action(webui); } else if ((strcmp(webui->uri_cmd1,"action") == 0) && - (strcmp(webui->uri_cmd2,"quit") == 0)){ + (strcmp(webui->uri_cmd2,"quit") == 0)) { webu_text_action(webui); } else if ((strcmp(webui->uri_cmd1,"action") == 0) && - (strcmp(webui->uri_cmd2,"end") == 0)){ + (strcmp(webui->uri_cmd2,"end") == 0)) { webu_text_action(webui); } else if (!strcmp(webui->uri_cmd1,"action")) { @@ -1149,7 +1155,7 @@ void webu_text_main(struct webui_ctx *webui) (strlen(webui->uri_parm1) == 0)) { webu_text_track_pantilt(webui); - } else if ((strcmp(webui->uri_cmd1,"track") == 0)){ + } else if ((strcmp(webui->uri_cmd1,"track") == 0)) { webu_text_track(webui); } else{