summaryrefslogtreecommitdiff
path: root/src/timestamp.c
blob: dedf8d9b4652b10a8aa5667b32f34d80cbb20004 (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
/**
 * @file timestamp.c  Timestamp helpers
 *
 * Copyright (C) 2010 Creytiv.com
 */

#include <re.h>
#include <baresip.h>
#include "core.h"


/**
 * Check if a 32-bit timestamp wraps around
 *
 * @param ts_new  Timestamp of the current packet
 * @param ts_old  Timestamp of the previous packet
 *
 * @return Integer describing the wrap-around

 * @retval -1  backwards wrap-around
 * @retval  0  no wrap-around
 * @retval  1  forward wrap-around
 */
int timestamp_wrap(uint32_t ts_new, uint32_t ts_old)
{
	int32_t delta;

	if (ts_new < ts_old) {

		delta = (int32_t)ts_new - (int32_t)ts_old;

		if (delta > 0)
			return 1;
	}
	else if ((int32_t)(ts_old - ts_new) > 0) {

		return -1;
	}

	return 0;
}


/**
 * Calculate the total timestamp duration, in timestamp units.
 * The duration is calculated as the delta between the
 * last extended timestamp and the first extended timestamp.
 *
 * @param ts  Receiver timestamp struct
 *
 * @return Timestamp duration
 */
uint64_t timestamp_duration(const struct timestamp_recv *ts)
{
	uint64_t last_ext;

	if (!ts || !ts->is_set)
		return 0;

	last_ext = calc_extended_timestamp(ts->num_wraps, ts->last);

	return last_ext - ts->first;
}