VLC  3.0.23
vlc_mtime.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_mtime.h: high resolution time management functions
3  *****************************************************************************
4  * This header provides portable high precision time management functions,
5  * which should be the only ones used in other segments of the program, since
6  * functions like gettimeofday() and ftime() are not always supported.
7  * Most functions are declared as inline or as macros since they are only
8  * interfaces to system calls and have to be called frequently.
9  * 'm' stands for 'micro', since maximum resolution is the microsecond.
10  * Functions prototyped are implemented in interface/mtime.c.
11  *****************************************************************************
12  * Copyright (C) 1996, 1997, 1998, 1999, 2000 VLC authors and VideoLAN
13  * $Id$
14  *
15  * Authors: Vincent Seguin <seguin@via.ecp.fr>
16  *
17  * This program is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU Lesser General Public License as published by
19  * the Free Software Foundation; either version 2.1 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with this program; if not, write to the Free Software Foundation,
29  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
30  *****************************************************************************/
31 
32 #ifndef __VLC_MTIME_H
33 # define __VLC_MTIME_H 1
34 
35 /*****************************************************************************
36  * LAST_MDATE: date which will never happen
37  *****************************************************************************
38  * This date can be used as a 'never' date, to mark missing events in a function
39  * supposed to return a date, such as nothing to display in a function
40  * returning the date of the first image to be displayed. It can be used in
41  * comparaison with other values: all existing dates will be earlier.
42  *****************************************************************************/
43 #define LAST_MDATE ((vlc_tick_t)((uint64_t)(-1)/2))
44 
45 /*
46  * vlc_tick_t <> milliseconds (ms) conversions
47  */
48 #if (CLOCK_FREQ % 1000) == 0
49 #define VLC_TICK_FROM_MS(ms) ((CLOCK_FREQ / INT64_C(1000)) * (ms))
50 #define MS_FROM_VLC_TICK(vtk) ((vtk) / (CLOCK_FREQ / INT64_C(1000)))
51 #elif (1000 % CLOCK_FREQ) == 0
52 #define VLC_TICK_FROM_MS(ms) ((ms) / (INT64_C(1000) / CLOCK_FREQ))
53 #define MS_FROM_VLC_TICK(vtk) ((vtk) * (INT64_C(1000) / CLOCK_FREQ))
54 #else /* rounded overflowing conversion */
55 #define VLC_TICK_FROM_MS(ms) (CLOCK_FREQ * (ms) / 1000)
56 #define MS_FROM_VLC_TICK(vtk) ((vtk) * 1000 / CLOCK_FREQ)
57 #endif /* CLOCK_FREQ / 1000 */
58 
59 
60 /*
61  * vlc_tick_t <> seconds (sec) conversions
62  */
63 #define VLC_TICK_FROM_SEC(sec) (CLOCK_FREQ * (sec))
64 #define SEC_FROM_VLC_TICK(vtk) ((vtk) / CLOCK_FREQ)
65 
66 #ifdef __cplusplus
67 #include <type_traits>
68 
69 template <typename T>
70 static inline auto vlc_tick_from_sec(T sec)
71  -> typename std::enable_if<std::is_integral<T>::value, vlc_tick_t>::type
72 {
73  return CLOCK_FREQ * sec;
74 }
75 
76 /* seconds in floating point */
77 static inline vlc_tick_t vlc_tick_from_sec(double secf)
78 {
79  return (vlc_tick_t)(CLOCK_FREQ * secf); /* TODO use llround ? */
80 }
81 #else /* !__cplusplus */
82 static inline vlc_tick_t vlc_tick_from_seci(int64_t sec)
83 {
84  return CLOCK_FREQ * sec;
85 }
86 /* seconds in floating point */
87 static inline vlc_tick_t vlc_tick_from_secf(double secf)
88 {
89  return (vlc_tick_t)(CLOCK_FREQ * secf); /* TODO use llround ? */
90 }
91 
92 #define vlc_tick_from_sec(sec) _Generic((sec), \
93  double: vlc_tick_from_secf(sec), \
94  float: vlc_tick_from_secf(sec), \
95  default: vlc_tick_from_seci(sec) )
96 #endif /* !__cplusplus */
97 
98 /* seconds in floating point from vlc_tick_t */
99 static inline double secf_from_vlc_tick(vlc_tick_t vtk)
100 {
101  return (double)vtk / (double)CLOCK_FREQ;
102 }
103 
104 
105 /*****************************************************************************
106  * MSTRTIME_MAX_SIZE: maximum possible size of mstrtime
107  *****************************************************************************
108  * This values is the maximal possible size of the string returned by the
109  * mstrtime() function, including '-' and the final '\0'. It should be used to
110  * allocate the buffer.
111  *****************************************************************************/
112 #define MSTRTIME_MAX_SIZE 22
113 
114 /*****************************************************************************
115  * Prototypes
116  *****************************************************************************/
117 VLC_API char * secstotimestr( char *psz_buffer, int32_t secs );
118 
119 /*****************************************************************************
120  * date_t: date incrementation without long-term rounding errors
121  *****************************************************************************/
122 struct date_t
123 {
125  uint32_t i_divider_num;
126  uint32_t i_divider_den;
127  uint32_t i_remainder;
128 };
129 
130 VLC_API void date_Init( date_t *, uint32_t, uint32_t );
131 VLC_API void date_Change( date_t *, uint32_t, uint32_t );
132 VLC_API void date_Set( date_t *, vlc_tick_t );
133 VLC_API vlc_tick_t date_Get( const date_t * );
135 VLC_API vlc_tick_t date_Increment( date_t *, uint32_t );
136 VLC_API vlc_tick_t date_Decrement( date_t *, uint32_t );
137 VLC_API uint64_t NTPtime64( void );
138 #endif /* !__VLC_MTIME_ */
secf_from_vlc_tick
static double secf_from_vlc_tick(vlc_tick_t vtk)
Definition: vlc_mtime.h:98
date_Set
void date_Set(date_t *, vlc_tick_t)
Set the date value of a date_t.
Definition: mtime.c:115
VLC_API
#define VLC_API
Definition: fourcc_gen.c:30
date_t::i_divider_num
uint32_t i_divider_num
Definition: vlc_mtime.h:122
vlc_common.h
secstotimestr
char * secstotimestr(char *psz_buffer, int32_t secs)
Convert seconds to a time in the format h:mm:ss.
Definition: mtime.c:49
date_t::i_remainder
uint32_t i_remainder
Definition: vlc_mtime.h:124
vlc_tick_t
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_common.h:152
date_t::i_divider_den
uint32_t i_divider_den
Definition: vlc_mtime.h:123
CLOCK_FREQ
#define CLOCK_FREQ
Definition: vlc_config.h:45
date_Change
void date_Change(date_t *, uint32_t, uint32_t)
Change a date_t.
Definition: mtime.c:101
date_t
Definition: vlc_mtime.h:118
vlc_tick_from_seci
static vlc_tick_t vlc_tick_from_seci(int64_t sec)
Definition: vlc_mtime.h:81
NTPtime64
uint64_t NTPtime64(void)
Definition: mtime.c:201
date_Increment
vlc_tick_t date_Increment(date_t *, uint32_t)
Increment the date and return the result, taking into account rounding errors.
Definition: mtime.c:151
date_Get
vlc_tick_t date_Get(const date_t *)
Get the date of a date_t.
Definition: mtime.c:127
date_t::date
vlc_tick_t date
Definition: vlc_mtime.h:121
vlc_tick_from_secf
static vlc_tick_t vlc_tick_from_secf(double secf)
Definition: vlc_mtime.h:86
date_Decrement
vlc_tick_t date_Decrement(date_t *, uint32_t)
Decrement the date and return the result, taking into account rounding errors.
Definition: mtime.c:179
date_Init
void date_Init(date_t *, uint32_t, uint32_t)
Initialize a date_t.
Definition: mtime.c:85
vlc_tick_from_sec
#define vlc_tick_from_sec(sec)
Definition: vlc_mtime.h:91
date_Move
void date_Move(date_t *, vlc_tick_t)
Move forwards or backwards the date of a date_t.
Definition: mtime.c:138