summaryrefslogtreecommitdiff
path: root/time.c
blob: 1b51ef2350a95c18d383ffb41ce07697e58ac89d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* SPDX-License-Identifier: GPL-2.0 */

#include "para.h"
/** \file time.c Helper functions for dealing with time values. */

/**
 * Convert struct timeval to milliseconds.
 *
 * \param tv The time value value to convert.
 *
 * \return The number of milliseconds in \a tv.
 */
long unsigned tv2ms(const struct timeval *tv)
{
	return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
}

/**
 * Convert milliseconds to a struct timeval.
 *
 * \param n The number of milliseconds.
 * \param tv Result pointer.
 */
void ms2tv(long unsigned n, struct timeval *tv)
{
	tv->tv_sec = n / 1000;
	tv->tv_usec = (n % 1000) * 1000;
}

/**
 * Compute the difference of two time values.
 *
 * \param b Minuend.
 * \param a Subtrahend.
 * \param diff Result pointer.
 *
 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
 * return.
 *
 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
 */
int tv_diff(const struct timeval *b, const struct timeval *a,
		struct timeval *diff)
{
	int ret = 1;

	if ((b->tv_sec < a->tv_sec) ||
		((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
		const struct timeval *tmp = a;
		a = b;
		b = tmp;
		ret = -1;
	}
	if (!diff)
		return ret;
	diff->tv_sec = b->tv_sec - a->tv_sec;
	if (b->tv_usec < a->tv_usec) {
		diff->tv_sec--;
		diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
	} else
		diff->tv_usec = b->tv_usec - a->tv_usec;
	return ret;
}

/**
 * Add two time values.
 *
 * \param a First addend.
 * \param b Second addend.
 * \param sum Contains the sum \a a + \a b on return.
 */
void tv_add(const struct timeval *a, const struct timeval *b,
		struct timeval *sum)
{
	sum->tv_sec = a->tv_sec + b->tv_sec;
	if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
		sum->tv_sec++;
		sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
	} else
		sum->tv_usec = a->tv_usec + b->tv_usec;
}

/**
 * Compute integer multiple of given struct timeval.
 *
 * \param mult The integer value to multiply with.
 * \param tv The timevalue to multiply.
 *
 * \param result Contains \a mult * \a tv on return.
 */
void tv_scale(const unsigned long mult, const struct timeval *tv,
		struct timeval *result)
{
	uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult;

	result->tv_sec = x / 1000 / 1000;
	result->tv_usec = x % (1000 * 1000);
}

/**
 * Compute a fraction of given struct timeval.
 *
 * \param divisor The integer value to divide by.
 * \param tv The timevalue to divide.
 * \param result Contains (1 / mult) * tv on return.
 */
void tv_divide(const unsigned long divisor, const struct timeval *tv,
		struct timeval *result)
{
	uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;

	result->tv_sec = x / 1000 / 1000;
	result->tv_usec = x % (1000 * 1000);
}

/**
 * Compute a convex combination of two time values.
 *
 * \param a The first coefficient.
 * \param tv1 The first time value.
 * \param b The second coefficient.
 * \param tv2 The second time value.
 * \param result Contains the convex combination upon return.
 *
 * Compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
 * Both \a a and \a b may be negative.
 *
 * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively.
 */
int tv_convex_combination(const long a, const struct timeval *tv1,
		const long b, const struct timeval *tv2,
		struct timeval *result)
{
	struct timeval tmp1, tmp2, tmp3;
	int ret = 1;
	unsigned long a1, b1;

	if (a == 0 && b == 0) {
		result->tv_sec = 0;
		result->tv_usec = 0;
		return 0;
	}
	a1 = PARA_ABS(a);
	b1 = PARA_ABS(b);
	tv_scale(a1, tv1, &tmp1);
	tv_scale(b1, tv2, &tmp2);
	if ((a > 0 && b < 0) || (a < 0 && b > 0)) /* subtract */
		ret = tv_diff(&tmp1, &tmp2, &tmp3);
	else
		tv_add(&tmp1, &tmp2, &tmp3);
	tv_divide(a1 + b1, &tmp3, result);
	if (!a || !b) {
		if (a + b < 0)
			ret = -1;
	} else if (a < 0)
		ret = -ret;
	return ret;
}

/**
 * Compute when to send a chunk of an audio file.
 *
 * \param chunk_num The number of the chunk.
 * \param chunk_tv The duration of one chunk.
 * \param stream_start When the first chunk was sent.
 * \param result The time when to send chunk number \a chunk_num.
 *
 * This function computes \a stream_start + \a chunk_num * \a chunk_time.
 */
void compute_chunk_time(long unsigned chunk_num,
		struct timeval *chunk_tv, struct timeval *stream_start,
		struct timeval *result)
{
	struct timeval tmp;

	tv_scale(chunk_num, chunk_tv, &tmp);
	tv_add(&tmp, stream_start, result);
}

/**
 * Retrieve the time of the realtime clock.
 *
 * \param tv Where to store the result.
 *
 * Gets the current value of the system-wide real-time clock (identified by id
 * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static
 * buffer, otherwise it is stored at the location given by \a tv.
 *
 * \return This function aborts on errors. On success it returns a pointer to
 * memory containing the current time.
 *
 * \sa clock_gettime(2), gettimeofday(2).
 */
struct timeval *clock_get_realtime(struct timeval *tv)
{
	static struct timeval user_friendly;
	struct timespec t;
	int ret;

	if (!tv)
		tv = &user_friendly;
	ret = clock_gettime(CLOCK_REALTIME, &t);
	assert(ret == 0);
	tv->tv_sec = t.tv_sec;
	tv->tv_usec = t.tv_nsec / 1000;
	return tv;
}