#define _DEFAULT_SOURCE #include #include #include #include #include #include #include #include "configuration.h" #include "miniwebcam.h" int running = 1; pthread_mutex_t mtx; static void sig_int(int); int SetupSignals(); std::string GetDefaultConfig(std::string name); int timedeiff_ms(timeval *t1); VideoFrame inputimage; VideoFrameFloat inputfloat; VideoFrameFloat inputfloatfilter; VideoFrame currentimage; VideoFrame currentimagefloat; VideoDevice *vdev= NULL; double framespersecond = -1; // 60secs average frames per second static void *thread_webserver(void *ignored_argument) { WebCamServer webserver; webserver.SetupPorts(config.http_port, config.https_port); webserver.SetupSSL(config.ssl_key, config.ssl_cert); webserver.Start(); while (running) { webserver.Loop(); usleep (200); } running = 0; // force running to 0 webserver.Stop(); return NULL; }; static void *thread_video(void *ignored_argument) { timeval tv_fps; time_t time_fps, time_refreshctls; float current_brightness = 0.5; float brightness_error_sum = 0.0f; float brightness_error_integral = 0.0f; int brightness_sample_count = 0; const int brightness_settle_frames = 5; const float brightness_deadband = 0.01f; const float brightness_kp = 0.10f; const float brightness_ki = 0.01f; const float brightness_integral_limit = 2.0f; time_fps = time(NULL); time_refreshctls = time(NULL); gettimeofday(&tv_fps, NULL); if (config.vdev_dumpfile.length() > 0) { vdev = new VideoDevice_Dump(); } else { vdev = new VideoDevice_V4L2(); } if (vdev->SetDevice (config.vdev_device, config.vdev_cdevice, config.vdev_width, config.vdev_height, convert_to_pixelformat(config.vdev_format)) == 0) { debug ("could not setup device.\n"); goto thread_video_exit; // we use goto for a better cleanup of the thread } if (vdev->SetIOMode (config.vdev_iomode) == 0) { debug ("could not setup iomode.\n"); goto thread_video_exit; } if (vdev->Start() == 0) { debug ("Error: could not start video.\n"); goto thread_video_exit; } currentimage.SetSize (config.web_width, config.web_height); while (running) { // if (vdev->GetFrame(&inputimage, &inputfloat) == 0) { if (vdev->GetFrame(&inputimage, NULL) == 0) { vdev->Stop(); vdev->Start(); } if (time(NULL) - time_refreshctls > 10) { vdev->RefreshCtrls(); time_refreshctls = time(NULL); } if (time(NULL) - time_fps > 5) { time_fps = time(NULL); debug ("FPS: %f", framespersecond); debug ("Brightness: %f", current_brightness); } Lock(); current_brightness = 0.0f; inputimage.CopyTo(¤timage, config.web_width, config.web_height, ¤t_brightness); if (inputfloatfilter.AddScaledImage(&inputfloat, config.filter_ratio) == 0) inputfloat.CopyTo(&inputfloatfilter); inputfloatfilter.CopyTo(¤timagefloat); UnLock(); // calculate and set new brightness value if auto_brightness is enabled if (config.auto_brightness > 0) { std::list ctrls = vdev->GetDevCtrls(); std::list::iterator it; int found = 0; for (it = ctrls.begin(); it != ctrls.end(); ++it) { if (it->id == config.auto_brightness) { found = 1; break; } } if (!found) { debug ("Auto Brightness control ID %d not found in device controls.", config.auto_brightness); config.auto_brightness = 0; // disable auto brightness if control not found } if (found) { if (config.auto_brightness_target < 0.0f) config.auto_brightness_target = 0.0f; if (config.auto_brightness_target > 1.0f) config.auto_brightness_target = 1.0f; brightness_error_sum += config.auto_brightness_target - current_brightness; if (++brightness_sample_count >= brightness_settle_frames) { float error = brightness_error_sum / brightness_sample_count; int control_range = it->max - it->min; brightness_error_sum = 0.0f; brightness_sample_count = 0; if (std::fabs(error) <= brightness_deadband) { brightness_error_integral = 0.0f; } else if (control_range > 0) { if (!((it->value <= it->min && error < 0.0f) || (it->value >= it->max && error > 0.0f))) { brightness_error_integral += error; if (brightness_error_integral > brightness_integral_limit) brightness_error_integral = brightness_integral_limit; if (brightness_error_integral < -brightness_integral_limit) brightness_error_integral = -brightness_integral_limit; } float adjustment = (brightness_kp * error + brightness_ki * brightness_error_integral) * control_range; int max_step = control_range / 32; if (max_step < 1) max_step = 1; if (adjustment > max_step) adjustment = max_step; if (adjustment < -max_step) adjustment = -max_step; int change = static_cast(std::lround(adjustment)); if (change == 0) change = error > 0.0f ? 1 : -1; int new_ctrl_value = it->value + change; if (new_ctrl_value < it->min) new_ctrl_value = it->min; if (new_ctrl_value > it->max) new_ctrl_value = it->max; debug("Auto Brightness: current=%f, target=%f, error=%f, new_ctrl_value=%d", current_brightness, config.auto_brightness_target, error, new_ctrl_value); if (new_ctrl_value != it->value) vdev->SetDevCtrl(config.auto_brightness, new_ctrl_value); } } } } // calculate the frames per second float time_diff = timedeiff_ms(&tv_fps); if (isnan(framespersecond) || isinf(framespersecond) || framespersecond < 0.0) framespersecond = 1.0/((float)time_diff/1000.0); else framespersecond = (framespersecond * 59.0 + 1.0/((float)time_diff/1000.0)) / 60.0; } thread_video_exit: running = 0; // force the other threads to stop vdev->Stop(); delete vdev; vdev = NULL; return NULL; } void ErrorExit(std::string text, int errorcode) { printf ("Error: %s\n", text.c_str()); exit (errorcode); }; int Lock () { if (pthread_mutex_lock(&mtx) == 0) return 1; else return 0; }; int UnLock () { if (pthread_mutex_unlock(&mtx) == 0) return 1; else return 0; }; int main(int argc, char **argv) { pthread_t thr_webserver; pthread_t thr_video; // prepare signals, mutex and test output image if (SetupSignals() == 0) return 0; pthread_mutex_init(&mtx, NULL); currentimage.TestScreen(1920, 1080); // read and setup config config.LoadFile (config.GetFilename()); config.LoadFile (GetDefaultConfig("miniwebcam.conf")); config.LoadArgs (argc, argv); if (config.GetInitFlags() & CONF_INITFLAGS_PRINT) { config.PrintConfig(); return 0; } if (config.GetInitFlags() & CONF_INITFLAGS_HELP) { config.Help(); return 0; } if (config.runasdaemon && daemon(0, 0) != 0) { perror("could not daemonize"); return EX_OSERR; } printf ("MiniWebCam:\n"); debug_enable(config.debug); pthread_create(&thr_webserver, NULL, &thread_webserver, NULL); pthread_create(&thr_video, NULL, &thread_video, NULL); while (running) { usleep (1000); } pthread_join(thr_webserver, NULL); pthread_join(thr_video, NULL); return 0; }; std::string GetDefaultConfig(std::string name) { std::string fn = ""; char *hd = getenv ((char*)"HOME"); if (hd != NULL) { fn = hd; fn = fn + "/." + name; } return fn; }; int SetupSignals() { if (signal(SIGINT, sig_int) == SIG_ERR) { errorexit ("%s:%d could not set signal for SIGINT\n", __FILE__, __LINE__); return 0; } if (signal(SIGTERM, sig_int) == SIG_ERR) { errorexit ("%s:%d could not set signal for SIGTERM\n", __FILE__, __LINE__); return 0; } if (signal(SIGHUP, sig_int) == SIG_ERR) { errorexit ("%s:%d could not set signal for SIGHUB\n", __FILE__, __LINE__); return 0; } if (signal(SIGUSR1, sig_int) == SIG_ERR) { errorexit ("%s:%d could not set signal for SIGHUB\n", __FILE__, __LINE__); return 0; } return 1; }; static void sig_int(int sig) { if (signal (SIGINT, sig_int) == SIG_ERR) errorexit ("could not set up signal SIGINT\n"); printf ("\n\nSignal Int\n\n"); running = 0; }; int isfile(std::string fname) { struct stat st; if (stat (fname.c_str(), &st) == -1) return 0; if ((st.st_mode & S_IFMT) == S_IFREG) return 1; return 0; }; int timedeiff_ms(timeval *t1) { timeval t2; int res; gettimeofday(&t2, NULL); res = ((t2.tv_sec - t1->tv_sec) * 1000 + (t2.tv_usec - t1->tv_usec) / 1000); *t1 = t2; return res; };