/src/rtpproxy/src/rtpp_time.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org> |
3 | | * Copyright (c) 2006-2014 Sippy Software, Inc., http://www.sippysoft.com |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | | * SUCH DAMAGE. |
26 | | * |
27 | | */ |
28 | | |
29 | | #include <sys/time.h> |
30 | | #include <math.h> |
31 | | #include <time.h> |
32 | | |
33 | | #include "rtpp_types.h" |
34 | | #include "rtpp_time.h" |
35 | | |
36 | | static double |
37 | | _getdtime(clockid_t clock_id) |
38 | 0 | { |
39 | 0 | struct timespec tp; |
40 | |
|
41 | 0 | if (clock_gettime(clock_id, &tp) == -1) |
42 | 0 | return (-1); |
43 | | |
44 | 0 | return timespec2dtime(&tp); |
45 | 0 | } |
46 | | |
47 | | double |
48 | | getdtime(void) |
49 | 0 | { |
50 | |
|
51 | 0 | return (_getdtime(RTPP_CLOCK_MONO)); |
52 | 0 | } |
53 | | |
54 | | void |
55 | | rtpp_timestamp_get(struct rtpp_timestamp *tp) |
56 | 0 | { |
57 | |
|
58 | 0 | tp->wall = _getdtime(RTPP_CLOCK_REAL); |
59 | 0 | tp->mono = _getdtime(RTPP_CLOCK_MONO); |
60 | 0 | } |
61 | | |
62 | | void |
63 | | dtime2mtimespec(double dtime, struct timespec *mtime) |
64 | 0 | { |
65 | |
|
66 | 0 | SEC(mtime) = trunc(dtime); |
67 | 0 | NSEC(mtime) = round((double)NSEC_MAX * (dtime - (double)SEC(mtime))); |
68 | 0 | } |
69 | | |
70 | | void |
71 | | dtime2timeval(double dtime, struct timeval *tvp) |
72 | 0 | { |
73 | |
|
74 | 0 | SEC(tvp) = trunc(dtime); |
75 | 0 | USEC(tvp) = round((double)USEC_MAX * (dtime - (double)SEC(tvp))); |
76 | 0 | if (USEC(tvp) == USEC_MAX) { |
77 | 0 | SEC(tvp)++; |
78 | 0 | USEC(tvp) = 0; |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | const char * |
83 | | get_mclock_name(void) |
84 | 0 | { |
85 | |
|
86 | 0 | return (RTPP_MCLOCK_NAME); |
87 | 0 | } |