diff --git a/gear-lib/librpc/librpc.c b/gear-lib/librpc/librpc.c index 303d2c47..763c1abe 100644 --- a/gear-lib/librpc/librpc.c +++ b/gear-lib/librpc/librpc.c @@ -93,7 +93,7 @@ void print_packet(struct rpc_packet *pkt) printf("header.uuid_src = 0x%08x\n", pkt->header.uuid_src); printf("header.msg_id = 0x%08x\n", pkt->header.msg_id); printf("header.timestamp = %" PRIu64 "(%s)\n", pkt->header.timestamp, - time_str_human_by_msec(pkt->header.timestamp, ts, sizeof(ts))); + time_str_format_by_msec(pkt->header.timestamp, ts, sizeof(ts))); printf("header.payload_len = %d\n", pkt->header.payload_len); printf("header.checksum = 0x%08x\n", pkt->header.checksum); dump_packet(pkt); @@ -110,7 +110,7 @@ void print_session(struct rpc_session *ss) printf("session.uuid_src = 0x%08x\n", ss->uuid_src); printf("session.msg_id = 0x%08x\n", ss->msg_id); printf("session.timestamp = %" PRIu64 "(%s)\n", ss->timestamp, - time_str_human_by_msec(ss->timestamp, ts, sizeof(ts))); + time_str_format_by_msec(ss->timestamp, ts, sizeof(ts))); printf("session.checksum = 0x%08lx\n", ss->cseq); printf("================\n"); } @@ -158,7 +158,7 @@ size_t pack_msg(struct rpc_packet *pkt, uint32_t uuid_dst, uint32_t uuid_src, hdr->uuid_dst = uuid_dst; hdr->uuid_src = uuid_src; hdr->msg_id = msg_id; - time_info(&ti); + time_now_info(&ti); hdr->timestamp = ti.utc_msec; if (in_arg) { @@ -508,7 +508,7 @@ static struct rpc_session *rpc_session_create(struct rpcs *s, int fd, uint32_t u pkt.header.msg_id = 0; pkt.header.payload_len = sizeof(uuid); pkt.payload = &uuid; - time_info(&ti); + time_now_info(&ti); pkt.header.timestamp = ti.utc_msec; ret = rpc_send(&session->base, &pkt); diff --git a/gear-lib/librtsp/transport_session.c b/gear-lib/librtsp/transport_session.c index bf9e0b8d..d19722b5 100644 --- a/gear-lib/librtsp/transport_session.c +++ b/gear-lib/librtsp/transport_session.c @@ -108,7 +108,7 @@ static void *send_thread(struct thread *t, void *ptr) } ms->is_active = true; unsigned int ssrc = (unsigned int)rtp_ssrc(); - uint64_t pts = time_msec(); + uint64_t pts = time_now_msec(); unsigned int seq = ssrc; logd("rtp send thread %s created\n", t->name); while (t->run) { diff --git a/gear-lib/libtime/libtime.c b/gear-lib/libtime/libtime.c index d39b6ece..bf107f86 100644 --- a/gear-lib/libtime/libtime.c +++ b/gear-lib/libtime/libtime.c @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. ******************************************************************************/ -#include +#include "libtime.h" #include #include #include @@ -30,12 +30,10 @@ #include #endif -#include "libtime.h" - #define TIME_FORMAT "%Y%m%d%H%M%S" -uint64_t time_sec() +uint64_t time_now_sec() { time_t t; t = time(NULL); @@ -45,7 +43,7 @@ uint64_t time_sec() return t; } -char *time_sec_str() +char *time_now_sec_str() { time_t t; struct tm *tm; @@ -60,10 +58,10 @@ char *time_sec_str() return asctime(tm); } -char *time_str_human(char *str, int len) +char *time_now_str_format(char *str, int len) { struct time_info ti; - if (-1 == time_info(&ti)) { + if (-1 == time_now_info(&ti)) { return NULL; } snprintf(str, len, "%04d%02d%02d%02d%02d%02d", @@ -71,7 +69,7 @@ char *time_str_human(char *str, int len) return str; } -char *time_str_human_by_utc(uint32_t utc, char *str, int len) +char *time_str_format_by_utc(uint32_t utc, char *str, int len) { struct time_info ti; if (-1 == time_info_by_utc(utc, &ti)) { @@ -82,7 +80,7 @@ char *time_str_human_by_utc(uint32_t utc, char *str, int len) return str; } -char *time_str_human_by_msec(uint64_t msec, char *str, int len) +char *time_str_format_by_msec(uint64_t msec, char *str, int len) { struct time_info ti; if (-1 == time_info_by_msec(msec, &ti)) { @@ -93,14 +91,13 @@ char *time_str_human_by_msec(uint64_t msec, char *str, int len) return str; } - -char *time_str_human_by_timeval(struct timeval *tv, char *str, int len) +char *time_str_format_by_timeval(struct timeval *tv, char *str, int len) { - uint32_t sec = time_usec(tv)/1000000; - return time_str_human_by_utc(sec, str, len); + uint32_t sec = time_now_usec(tv)/1000000; + return time_str_format_by_utc(sec, str, len); } -uint64_t time_usec(struct timeval *val) +uint64_t time_now_usec(struct timeval *val) { struct timeval tv; if (val == NULL) { @@ -113,7 +110,7 @@ uint64_t time_usec(struct timeval *val) return (uint64_t)(((uint64_t)tv.tv_sec)*1000*1000 + (uint64_t)tv.tv_usec); } -uint64_t _time_clock_gettime(clockid_t clk_id) +static uint64_t _time_clock_gettime(clockid_t clk_id) { #if defined (OS_LINUX) || defined (OS_APPLE) struct timespec ts; @@ -127,22 +124,22 @@ uint64_t _time_clock_gettime(clockid_t clk_id) #endif } -uint64_t time_msec() +uint64_t time_now_msec() { - return _time_clock_gettime(CLOCK_REALTIME)/1000; + return _time_clock_gettime(CLOCK_REALTIME)/(1000*1000); } -uint64_t time_nsec() +uint64_t time_now_nsec() { return _time_clock_gettime(CLOCK_REALTIME); } -uint64_t time_nsec_bootup() +uint64_t time_bootup_nsec() { return _time_clock_gettime(CLOCK_MONOTONIC); } -char *time_msec_str(char *str, int len) +char *time_now_msec_str(char *str, int len) { char date_fmt[20]; char date_ms[4]; @@ -173,10 +170,15 @@ char *time_msec_str(char *str, int len) int time_sleep_ms(uint64_t ms) { +#if defined (OS_LINUX) struct timeval tv; tv.tv_sec = 0; tv.tv_usec = ms*1000; return select(0, NULL, NULL, NULL, &tv); +#elif defined (OS_WINDOWS) + usleep(ms*1000); + return 0; +#endif } int time_info_by_utc(uint32_t utc, struct time_info *ti) @@ -253,7 +255,7 @@ int time_info_by_msec(uint64_t msec, struct time_info *ti) return 0; } -int time_info(struct time_info *ti) +int time_now_info(struct time_info *ti) { time_t utc; struct timeval tv; @@ -319,20 +321,11 @@ int time_set_info(struct time_info *ti) } #endif -bool time_passed_sec(int sec) +uint64_t time_elapsed_ms(struct timeval *prev) { - bool ret = false; - static uint64_t last_sec = 0; - static uint64_t now_sec = 0; - now_sec = time_sec(); - if (last_sec == 0) { - last_sec = now_sec; - } - if (now_sec - last_sec >= (uint32_t)sec) { - ret = true; - last_sec = now_sec; - } else { - ret = false; - } - return ret; + struct timeval now, res; + + gettimeofday(&now, NULL); + timersub(&now, prev, &res); + return (res.tv_sec)*1000 + (res.tv_usec)/1000; } diff --git a/gear-lib/libtime/libtime.h b/gear-lib/libtime/libtime.h index 0d1e1e27..8d8659e6 100644 --- a/gear-lib/libtime/libtime.h +++ b/gear-lib/libtime/libtime.h @@ -22,10 +22,8 @@ #ifndef LIBTIME_H #define LIBTIME_H +#include #include -#if defined (__linux__) || defined (__CYGWIN__) -#include -#endif #include #define LIBTIME_VERSION "0.1.1" @@ -51,37 +49,36 @@ struct time_info { /* * accuracy second */ -uint64_t time_sec(); -char *time_sec_str(); -char *time_str_human(char *str, int len); -char *time_str_human_by_utc(uint32_t utc, char *str, int len); -char *time_str_human_by_msec(uint64_t msec, char *str, int len); -char *time_str_human_by_timeval(struct timeval *val, char *str, int len); +uint64_t time_now_sec(); +char *time_now_sec_str(); +char *time_now_format(char *str, int len); +char *time_str_format_by_utc(uint32_t utc, char *str, int len); +char *time_str_format_by_msec(uint64_t msec, char *str, int len); +char *time_str_format_by_timeval(struct timeval *val, char *str, int len); /* * accuracy milli second */ -uint64_t time_msec(); -char *time_msec_str(char *str, int len); +uint64_t time_now_msec(); +char *time_now_msec_str(char *str, int len); int time_sleep_ms(uint64_t ms); /* * accuracy micro second */ -uint64_t time_usec(struct timeval *tv); +uint64_t time_now_usec(struct timeval *tv); /* * accuracy nano second */ -uint64_t time_nsec(); -uint64_t time_nsec_bootup(); +uint64_t time_now_nsec(); +uint64_t time_bootup_nsec(); char *time_nsec_to_str(uint64_t nsec); -int time_info(struct time_info *ti); +int time_now_info(struct time_info *ti); int time_info_by_utc(uint32_t utc, struct time_info *ti); int time_info_by_msec(uint64_t msec, struct time_info *ti); - -bool time_passed_sec(int sec); +uint64_t time_elapsed_ms(struct timeval *tv); #ifdef __cplusplus } diff --git a/gear-lib/libtime/test_libtime.c b/gear-lib/libtime/test_libtime.c index 8b2080b7..e7aa15aa 100644 --- a/gear-lib/libtime/test_libtime.c +++ b/gear-lib/libtime/test_libtime.c @@ -33,20 +33,20 @@ void foo() struct time_info ti; char ts[64]; char time[32]; - printf("time_sec_str: %s", time_sec_str()); - printf("time_msec_str: %s\n", time_msec_str(time, sizeof(time))); - printf("time_sec: %" PRIu64 "\n", time_sec()); - printf("time_msec: %" PRIu64 "\n", time_msec()); - printf("time_msec: %" PRIu64 "\n", time_usec(NULL)/1000); - printf("time_usec: %" PRIu64 "\n", time_usec(NULL)); - printf("time_nsec: %" PRIu64 "\n", time_nsec()); - printf("time_nsec_bootup: %" PRIu64 "\n", time_nsec_bootup()); - time_info(&ti); - printf("time_info: %d-%d-%d %02d:%02d:%02d.%03d\n", - ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, ti.msec); - printf("time_info: %s\n", ti.str); + printf("time_now_sec_str: %s", time_now_sec_str()); + printf("time_now_msec_str: %s\n", time_now_msec_str(time, sizeof(time))); + printf("time_now_sec: %" PRIu64 "\n", time_now_sec()); + printf("time_now_msec: %" PRIu64 "\n", time_now_msec()); + printf("time_now_msec: %" PRIu64 "\n", time_now_usec(NULL)/1000); + printf("time_now_usec: %" PRIu64 "\n", time_now_usec(NULL)); + printf("time_now_nsec: %" PRIu64 "\n", time_now_nsec()); + printf("time_bootup_nsec: %" PRIu64 "\n", time_bootup_nsec()); + time_now_info(&ti); + printf("time_now_info: %d-%02d-%02d %02d:%02d:%02d.%03d\n", + ti.year, ti.mon, ti.day, ti.hour, ti.min, ti.sec, ti.msec); + printf("time_now_info.str: %s\n", ti.str); memset(ts, 0, sizeof(ts)); - printf("time_info: %s\n", time_str_human_by_msec(ti.utc_msec, ts, sizeof(ts))); + printf("time_str_by_msec: %s\n", time_str_format_by_msec(ti.utc_msec, ts, sizeof(ts))); } int main(int argc, char **argv)