From 5fc7650ff53942e22b4bd29b6496b01dd70c3b5d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 17 Apr 2011 16:05:32 +0000 Subject: time boundary check works now diff --git a/src/writer.c b/src/writer.c index 3c63ccc..f45422c 100644 --- a/src/writer.c +++ b/src/writer.c @@ -40,6 +40,23 @@ #include "datatypes.h" #include "log.h" +static int compute_filname(struct tm* time, const char* type, file_t* file, const char* format, const char* dir, mode_t mode) +{ + log_printf(INFO, "%s time: %02d:%02d:%02d on %d.%d.%d", type, time->tm_hour, time->tm_min, time->tm_sec, time->tm_mday, time->tm_mon+1, time->tm_year+1900); + + char name[256]; + strftime(name, sizeof(name), format, time); + asprintf(&(file->path_), "%s/%s", dir, name); + if(!file->path_) + return -2; + + log_printf(INFO, "%s filename is: %s(.?)", type, file->path_); + file->fd_ = -1; + file->mode_ = mode; + + return 0; +} + static int init_time_boundaries(writer_t* writer) { if(!writer) @@ -51,42 +68,19 @@ static int init_time_boundaries(writer_t* writer) struct tm bd_time; localtime_r(&(now.tv_sec), &bd_time); - log_printf(INFO, "it's now: %02d:%02d:%02d on %d.%d.%d", bd_time.tm_hour, bd_time.tm_min, bd_time.tm_sec, bd_time.tm_mday, bd_time.tm_mon+1, bd_time.tm_year+1900); - - char name[256]; - strftime(name, sizeof(name), writer->name_format_, &bd_time); - asprintf(&(writer->current_.path_), "%s/%s", writer->output_dir_, name); - if(!writer->current_.path_) { - return -2; - } - log_printf(INFO, "current filename is: %s(.?)", writer->current_.path_); - writer->current_.fd_ = -1; - writer->current_.mode_ = writer->mode_; + int ret = compute_filname(&bd_time, "current", &(writer->current_), writer->name_format_, writer->output_dir_, writer->mode_); + if(ret) return ret; bd_time.tm_sec = 0; bd_time.tm_min = 0; - time_t T = mktime(&bd_time); T+=3600; - localtime_r(&T, &bd_time); - log_printf(INFO, "next boundary is at: %02d:%02d:%02d on %d.%d.%d", bd_time.tm_hour, bd_time.tm_min, bd_time.tm_sec, bd_time.tm_mday, bd_time.tm_mon+1, bd_time.tm_year+1900); - - strftime(name, sizeof(name), writer->name_format_, &bd_time); - asprintf(&(writer->next_.path_), "%s/%s", writer->output_dir_, name); - if(!writer->next_.path_) { - free(writer->current_.path_); - return -2; - } - log_printf(INFO, "next filename will be: %s(.?)", writer->next_.path_); - writer->next_.fd_ = -1; - writer->next_.mode_ = writer->mode_; - struct timespec b = { T, 0 }; writer->next_boundary_ = b; - return 0; + return compute_filname(&bd_time, "next", &(writer->next_), writer->name_format_, writer->output_dir_, writer->mode_); } int writer_init(writer_t* writer, GMainLoop *loop, const char* name_format, mode_t mode, const char* output_dir, int interval, int offset) @@ -119,6 +113,9 @@ int writer_init(writer_t* writer, GMainLoop *loop, const char* name_format, mode static int open_file(file_t* file) { + if(file->fd_ > 0) // file already open! + return -1; + char* orig_path = file->path_; int cnt = 0; do { @@ -159,17 +156,24 @@ static int check_boundaries(writer_t* writer) struct timespec now; clock_gettime(CLOCK_REALTIME, &now); - struct tm now_bd; - localtime_r(&(now.tv_sec), &now_bd); - log_printf(DEBUG, "it's now: %02d:%02d:%02d on %d.%d.%d", now_bd.tm_hour, now_bd.tm_min, now_bd.tm_sec, now_bd.tm_mday, now_bd.tm_mon+1, now_bd.tm_year+1900); - - // TODO: check if boundary has been reached - // if yes: - // - open new file + GstClockTime tmp = GST_TIMESPEC_TO_TIME(now); + GstClockTime boundary = GST_TIMESPEC_TO_TIME(writer->next_boundary_); + tmp += writer->offset_; + if(tmp >= boundary) { + struct tm now_bd; + localtime_r(&(now.tv_sec), &now_bd); + log_printf(DEBUG, "boundary reached! it's now: %02d:%02d:%02d.%06d on %d.%d.%d", now_bd.tm_hour, now_bd.tm_min, now_bd.tm_sec, now.tv_nsec/1000, now_bd.tm_mday, now_bd.tm_mon+1, now_bd.tm_year+1900); + + int ret = open_file(&(writer->next_)); +// if(ret) return ret; // TODO: stop writer on open_file error ??? + // - add new file // - remove old file + // - calculate next filename and boundary + // - call post processing script from fd_removed callback + } return 0; } -- cgit v0.10.2