summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/dht_test.c5
-rw-r--r--auto_tests/run_auto_test.h5
-rw-r--r--testing/av_test.c2
-rw-r--r--toxav/audio.c4
-rw-r--r--toxav/audio.h6
-rw-r--r--toxav/rtp.h2
-rw-r--r--toxav/video.c4
-rw-r--r--toxav/video.h4
-rw-r--r--toxcore/mono_time.c97
-rw-r--r--toxcore/mono_time.h4
-rw-r--r--toxcore/mono_time_test.cc4
-rw-r--r--toxcore/net_crypto.c2
12 files changed, 71 insertions, 68 deletions
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c
index 232b7d95..f9c09dd5 100644
--- a/auto_tests/dht_test.c
+++ b/auto_tests/dht_test.c
@@ -600,9 +600,10 @@ static void ip_callback(void *data, int32_t number, IP_Port ip_port)
600 600
601#define NUM_DHT_FRIENDS 20 601#define NUM_DHT_FRIENDS 20
602 602
603static uint64_t get_clock_callback(void *user_data) 603static uint64_t get_clock_callback(Mono_Time *mono_time, void *user_data)
604{ 604{
605 return *(uint64_t *)user_data; 605 const uint64_t *clock = (const uint64_t *)user_data;
606 return *clock;
606} 607}
607 608
608static void test_DHT_test(void) 609static void test_DHT_test(void)
diff --git a/auto_tests/run_auto_test.h b/auto_tests/run_auto_test.h
index 602ad524..bcbfed55 100644
--- a/auto_tests/run_auto_test.h
+++ b/auto_tests/run_auto_test.h
@@ -42,9 +42,10 @@ static void iterate_all_wait(uint32_t tox_count, Tox **toxes, State *state, uint
42 c_sleep(20); 42 c_sleep(20);
43} 43}
44 44
45static uint64_t get_state_clock_callback(void *user_data) 45static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
46{ 46{
47 return ((State *)user_data)->clock; 47 const State *state = (const State *)user_data;
48 return state->clock;
48} 49}
49 50
50static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *state)) 51static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *state))
diff --git a/testing/av_test.c b/testing/av_test.c
index 18bb3105..ca2b2779 100644
--- a/testing/av_test.c
+++ b/testing/av_test.c
@@ -569,7 +569,7 @@ CHECK_ARG:
569 initialize_tox(&bootstrap, &AliceAV, &AliceCC, &BobAV, &BobCC); 569 initialize_tox(&bootstrap, &AliceAV, &AliceCC, &BobAV, &BobCC);
570 570
571 // TODO(iphydf): Don't depend on toxcore internals 571 // TODO(iphydf): Don't depend on toxcore internals
572 const Mono_Time *mono_time = (*(Messenger **)bootstrap)->mono_time; 572 Mono_Time *mono_time = (*(Messenger **)bootstrap)->mono_time;
573 573
574 if (TEST_TRANSFER_A) { 574 if (TEST_TRANSFER_A) {
575 SNDFILE *af_handle; 575 SNDFILE *af_handle;
diff --git a/toxav/audio.c b/toxav/audio.c
index 5da41570..de007424 100644
--- a/toxav/audio.c
+++ b/toxav/audio.c
@@ -44,7 +44,7 @@ static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8
44 44
45 45
46 46
47ACSession *ac_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, 47ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
48 toxav_audio_receive_frame_cb *cb, void *cb_data) 48 toxav_audio_receive_frame_cb *cb, void *cb_data)
49{ 49{
50 ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1); 50 ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1);
@@ -216,7 +216,7 @@ void ac_iterate(ACSession *ac)
216 pthread_mutex_unlock(ac->queue_mutex); 216 pthread_mutex_unlock(ac->queue_mutex);
217} 217}
218 218
219int ac_queue_message(const Mono_Time *mono_time, void *acp, struct RTPMessage *msg) 219int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg)
220{ 220{
221 if (!acp || !msg) { 221 if (!acp || !msg) {
222 return -1; 222 return -1;
diff --git a/toxav/audio.h b/toxav/audio.h
index 1a33a2ae..a323a08e 100644
--- a/toxav/audio.h
+++ b/toxav/audio.h
@@ -50,7 +50,7 @@
50#define AUDIO_MAX_BUFFER_SIZE_BYTES (AUDIO_MAX_BUFFER_SIZE_PCM16 * 2) 50#define AUDIO_MAX_BUFFER_SIZE_BYTES (AUDIO_MAX_BUFFER_SIZE_PCM16 * 2)
51 51
52typedef struct ACSession_s { 52typedef struct ACSession_s {
53 const Mono_Time *mono_time; 53 Mono_Time *mono_time;
54 const Logger *log; 54 const Logger *log;
55 55
56 /* encoding */ 56 /* encoding */
@@ -78,11 +78,11 @@ typedef struct ACSession_s {
78 void *acb_user_data; 78 void *acb_user_data;
79} ACSession; 79} ACSession;
80 80
81ACSession *ac_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, 81ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
82 toxav_audio_receive_frame_cb *cb, void *cb_data); 82 toxav_audio_receive_frame_cb *cb, void *cb_data);
83void ac_kill(ACSession *ac); 83void ac_kill(ACSession *ac);
84void ac_iterate(ACSession *ac); 84void ac_iterate(ACSession *ac);
85int ac_queue_message(const Mono_Time *mono_time, void *acp, struct RTPMessage *msg); 85int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg);
86int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels); 86int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels);
87 87
88#endif /* AUDIO_H */ 88#endif /* AUDIO_H */
diff --git a/toxav/rtp.h b/toxav/rtp.h
index 42997594..e068f6ac 100644
--- a/toxav/rtp.h
+++ b/toxav/rtp.h
@@ -159,7 +159,7 @@ struct RTPWorkBufferList {
159 159
160#define DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT 10 160#define DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT 10
161 161
162typedef int rtp_m_cb(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg); 162typedef int rtp_m_cb(Mono_Time *mono_time, void *cs, struct RTPMessage *msg);
163 163
164/** 164/**
165 * RTP control session. 165 * RTP control session.
diff --git a/toxav/video.c b/toxav/video.c
index 3f4d96e1..ab47ae97 100644
--- a/toxav/video.c
+++ b/toxav/video.c
@@ -159,7 +159,7 @@ static void vc_init_encoder_cfg(const Logger *log, vpx_codec_enc_cfg_t *cfg, int
159#endif 159#endif
160} 160}
161 161
162VCSession *vc_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, 162VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
163 toxav_video_receive_frame_cb *cb, void *cb_data) 163 toxav_video_receive_frame_cb *cb, void *cb_data)
164{ 164{
165 VCSession *vc = (VCSession *)calloc(sizeof(VCSession), 1); 165 VCSession *vc = (VCSession *)calloc(sizeof(VCSession), 1);
@@ -357,7 +357,7 @@ void vc_iterate(VCSession *vc)
357 } 357 }
358} 358}
359 359
360int vc_queue_message(const Mono_Time *mono_time, void *vcp, struct RTPMessage *msg) 360int vc_queue_message(Mono_Time *mono_time, void *vcp, struct RTPMessage *msg)
361{ 361{
362 /* This function is called with complete messages 362 /* This function is called with complete messages
363 * they have already been assembled. 363 * they have already been assembled.
diff --git a/toxav/video.h b/toxav/video.h
index 2eee1c05..16f4658b 100644
--- a/toxav/video.h
+++ b/toxav/video.h
@@ -60,11 +60,11 @@ typedef struct VCSession_s {
60 pthread_mutex_t queue_mutex[1]; 60 pthread_mutex_t queue_mutex[1];
61} VCSession; 61} VCSession;
62 62
63VCSession *vc_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, 63VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
64 toxav_video_receive_frame_cb *cb, void *cb_data); 64 toxav_video_receive_frame_cb *cb, void *cb_data);
65void vc_kill(VCSession *vc); 65void vc_kill(VCSession *vc);
66void vc_iterate(VCSession *vc); 66void vc_iterate(VCSession *vc);
67int vc_queue_message(const Mono_Time *mono_time, void *vcp, struct RTPMessage *msg); 67int vc_queue_message(Mono_Time *mono_time, void *vcp, struct RTPMessage *msg);
68int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height, int16_t kf_max_dist); 68int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height, int16_t kf_max_dist);
69 69
70#endif /* VIDEO_H */ 70#endif /* VIDEO_H */
diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c
index aa272749..6e4bc067 100644
--- a/toxcore/mono_time.c
+++ b/toxcore/mono_time.c
@@ -28,12 +28,52 @@
28struct Mono_Time { 28struct Mono_Time {
29 uint64_t time; 29 uint64_t time;
30 uint64_t base_time; 30 uint64_t base_time;
31#ifdef OS_WIN32
32 uint64_t last_clock_mono;
33 uint64_t add_clock_mono;
34#endif
31 35
32 mono_time_current_time_cb *current_time_callback; 36 mono_time_current_time_cb *current_time_callback;
33 void *user_data; 37 void *user_data;
34}; 38};
35 39
36static mono_time_current_time_cb current_time_monotonic_default; 40static uint64_t current_time_monotonic_default(Mono_Time *mono_time, void *user_data)
41{
42 uint64_t time;
43#ifdef OS_WIN32
44 uint64_t old_add_clock_mono = mono_time->add_clock_mono;
45 time = (uint64_t)GetTickCount() + mono_time->add_clock_mono;
46
47 /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race
48 * conditions when multiple threads call this function at once */
49 if (time + 0x10000 < mono_time->last_clock_mono) {
50 uint32_t add = ~0;
51 /* use old_add_clock_mono rather than simply incrementing add_clock_mono, to handle the case that many threads
52 * simultaneously detect an overflow */
53 mono_time->add_clock_mono = old_add_clock_mono + add;
54 time += add;
55 }
56
57 mono_time->last_clock_mono = time;
58#else
59 struct timespec clock_mono;
60#if defined(__APPLE__)
61 clock_serv_t muhclock;
62 mach_timespec_t machtime;
63
64 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &muhclock);
65 clock_get_time(muhclock, &machtime);
66 mach_port_deallocate(mach_task_self(), muhclock);
67
68 clock_mono.tv_sec = machtime.tv_sec;
69 clock_mono.tv_nsec = machtime.tv_nsec;
70#else
71 clock_gettime(CLOCK_MONOTONIC, &clock_mono);
72#endif
73 time = 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
74#endif
75 return time;
76}
37 77
38Mono_Time *mono_time_new(void) 78Mono_Time *mono_time_new(void)
39{ 79{
@@ -46,6 +86,11 @@ Mono_Time *mono_time_new(void)
46 mono_time->current_time_callback = current_time_monotonic_default; 86 mono_time->current_time_callback = current_time_monotonic_default;
47 mono_time->user_data = nullptr; 87 mono_time->user_data = nullptr;
48 88
89#ifdef OS_WIN32
90 mono_time->last_clock_mono = 0;
91 mono_time->add_clock_mono = 0;
92#endif
93
49 mono_time->time = 0; 94 mono_time->time = 0;
50 mono_time->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(mono_time) / 1000ULL); 95 mono_time->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(mono_time) / 1000ULL);
51 96
@@ -87,53 +132,7 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time,
87} 132}
88 133
89/* return current monotonic time in milliseconds (ms). */ 134/* return current monotonic time in milliseconds (ms). */
90uint64_t current_time_monotonic(const Mono_Time *mono_time) 135uint64_t current_time_monotonic(Mono_Time *mono_time)
91{ 136{
92 return mono_time->current_time_callback(mono_time->user_data); 137 return mono_time->current_time_callback(mono_time, mono_time->user_data);
93}
94
95//!TOKSTYLE-
96// No global mutable state in Tokstyle.
97#ifdef OS_WIN32
98static uint64_t last_clock_mono;
99static uint64_t add_clock_mono;
100#endif
101//!TOKSTYLE+
102
103static uint64_t current_time_monotonic_default(void *user_data)
104{
105 uint64_t time;
106#ifdef OS_WIN32
107 uint64_t old_add_clock_mono = add_clock_mono;
108 time = (uint64_t)GetTickCount() + add_clock_mono;
109
110 /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race
111 * conditions when multiple threads call this function at once */
112 if (time + 0x10000 < last_clock_mono) {
113 uint32_t add = ~0;
114 /* use old_add_clock_mono rather than simply incrementing add_clock_mono, to handle the case that many threads
115 * simultaneously detect an overflow */
116 add_clock_mono = old_add_clock_mono + add;
117 time += add;
118 }
119
120 last_clock_mono = time;
121#else
122 struct timespec clock_mono;
123#if defined(__APPLE__)
124 clock_serv_t muhclock;
125 mach_timespec_t machtime;
126
127 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &muhclock);
128 clock_get_time(muhclock, &machtime);
129 mach_port_deallocate(mach_task_self(), muhclock);
130
131 clock_mono.tv_sec = machtime.tv_sec;
132 clock_mono.tv_nsec = machtime.tv_nsec;
133#else
134 clock_gettime(CLOCK_MONOTONIC, &clock_mono);
135#endif
136 time = 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
137#endif
138 return time;
139} 138}
diff --git a/toxcore/mono_time.h b/toxcore/mono_time.h
index b735f2c3..c2934d06 100644
--- a/toxcore/mono_time.h
+++ b/toxcore/mono_time.h
@@ -50,9 +50,9 @@ uint64_t mono_time_get(const Mono_Time *mono_time);
50bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout); 50bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout);
51 51
52/* return current monotonic time in milliseconds (ms). */ 52/* return current monotonic time in milliseconds (ms). */
53uint64_t current_time_monotonic(const Mono_Time *mono_time); 53uint64_t current_time_monotonic(Mono_Time *mono_time);
54 54
55typedef uint64_t mono_time_current_time_cb(void *user_data); 55typedef uint64_t mono_time_current_time_cb(Mono_Time *mono_time, void *user_data);
56 56
57/* Override implementation of current_time_monotonic() (for tests). 57/* Override implementation of current_time_monotonic() (for tests).
58 * 58 *
diff --git a/toxcore/mono_time_test.cc b/toxcore/mono_time_test.cc
index 6e81ac7e..3b33a241 100644
--- a/toxcore/mono_time_test.cc
+++ b/toxcore/mono_time_test.cc
@@ -35,7 +35,9 @@ TEST(MonoTime, IsTimeout) {
35 mono_time_free(mono_time); 35 mono_time_free(mono_time);
36} 36}
37 37
38static uint64_t test_current_time_callback(void *user_data) { return *(uint64_t *)user_data; } 38uint64_t test_current_time_callback(Mono_Time *mono_time, void *user_data) {
39 return *(uint64_t *)user_data;
40}
39 41
40TEST(MonoTime, CustomTime) { 42TEST(MonoTime, CustomTime) {
41 Mono_Time *mono_time = mono_time_new(); 43 Mono_Time *mono_time = mono_time_new();
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index a64ed970..736b9c4a 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -972,7 +972,7 @@ static int generate_request_packet(const Logger *log, uint8_t *data, uint16_t le
972 * return -1 on failure. 972 * return -1 on failure.
973 * return number of requested packets on success. 973 * return number of requested packets on success.
974 */ 974 */
975static int handle_request_packet(const Mono_Time *mono_time, const Logger *log, Packets_Array *send_array, 975static int handle_request_packet(Mono_Time *mono_time, const Logger *log, Packets_Array *send_array,
976 const uint8_t *data, uint16_t length, uint64_t *latest_send_time, uint64_t rtt_time) 976 const uint8_t *data, uint16_t length, uint64_t *latest_send_time, uint64_t rtt_time)
977{ 977{
978 if (length == 0) { 978 if (length == 0) {