summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/av_test.c8
-rw-r--r--toxav/audio.c10
-rw-r--r--toxav/audio.h7
-rw-r--r--toxav/bwcontroller.c10
-rw-r--r--toxav/rtp.c16
-rw-r--r--toxav/rtp.h2
-rw-r--r--toxav/toxav.c8
-rw-r--r--toxav/video.c11
-rw-r--r--toxav/video.h6
-rw-r--r--toxcore/mono_time.c38
-rw-r--r--toxcore/mono_time.h2
-rw-r--r--toxcore/net_crypto.c23
12 files changed, 73 insertions, 68 deletions
diff --git a/testing/av_test.c b/testing/av_test.c
index a36d0a00..d484250b 100644
--- a/testing/av_test.c
+++ b/testing/av_test.c
@@ -45,6 +45,7 @@ extern "C" {
45#include "../toxav/ring_buffer.c" 45#include "../toxav/ring_buffer.c"
46 46
47#include "../toxav/toxav.h" 47#include "../toxav/toxav.h"
48#include "../toxcore/Messenger.h"
48#include "../toxcore/mono_time.h" /* current_time_monotonic() */ 49#include "../toxcore/mono_time.h" /* current_time_monotonic() */
49#include "../toxcore/tox.h" 50#include "../toxcore/tox.h"
50#include "../toxcore/util.h" 51#include "../toxcore/util.h"
@@ -564,6 +565,9 @@ CHECK_ARG:
564 565
565 initialize_tox(&bootstrap, &AliceAV, &AliceCC, &BobAV, &BobCC); 566 initialize_tox(&bootstrap, &AliceAV, &AliceCC, &BobAV, &BobCC);
566 567
568 // TODO(iphydf): Don't depend on toxcore internals
569 const Mono_Time *mono_time = (*(Messenger **)bootstrap)->mono_time;
570
567 if (TEST_TRANSFER_A) { 571 if (TEST_TRANSFER_A) {
568 SNDFILE *af_handle; 572 SNDFILE *af_handle;
569 SF_INFO af_info; 573 SF_INFO af_info;
@@ -657,7 +661,7 @@ CHECK_ARG:
657 printf("Sample rate %d\n", af_info.samplerate); 661 printf("Sample rate %d\n", af_info.samplerate);
658 662
659 while (start_time + expected_time > time(nullptr)) { 663 while (start_time + expected_time > time(nullptr)) {
660 uint64_t enc_start_time = current_time_monotonic(); 664 uint64_t enc_start_time = current_time_monotonic(mono_time);
661 int64_t count = sf_read_short(af_handle, PCM, frame_size); 665 int64_t count = sf_read_short(af_handle, PCM, frame_size);
662 666
663 if (count > 0) { 667 if (count > 0) {
@@ -670,7 +674,7 @@ CHECK_ARG:
670 } 674 }
671 675
672 iterate_tox(bootstrap, AliceAV, BobAV, nullptr); 676 iterate_tox(bootstrap, AliceAV, BobAV, nullptr);
673 c_sleep((audio_frame_duration - (current_time_monotonic() - enc_start_time) - 1)); 677 c_sleep((audio_frame_duration - (current_time_monotonic(mono_time) - enc_start_time) - 1));
674 } 678 }
675 679
676 printf("Played file in: %lu; stopping stream...\n", time(nullptr) - start_time); 680 printf("Played file in: %lu; stopping stream...\n", time(nullptr) - start_time);
diff --git a/toxav/audio.c b/toxav/audio.c
index 729a4922..f1284f54 100644
--- a/toxav/audio.c
+++ b/toxav/audio.c
@@ -44,7 +44,8 @@ static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8
44 44
45 45
46 46
47ACSession *ac_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) 47ACSession *ac_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
48 toxav_audio_receive_frame_cb *cb, void *cb_data)
48{ 49{
49 ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1); 50 ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1);
50 51
@@ -75,6 +76,7 @@ ACSession *ac_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_au
75 goto BASE_CLEANUP; 76 goto BASE_CLEANUP;
76 } 77 }
77 78
79 ac->mono_time = mono_time;
78 ac->log = log; 80 ac->log = log;
79 81
80 /* Initialize encoders with default values */ 82 /* Initialize encoders with default values */
@@ -214,7 +216,7 @@ void ac_iterate(ACSession *ac)
214 pthread_mutex_unlock(ac->queue_mutex); 216 pthread_mutex_unlock(ac->queue_mutex);
215} 217}
216 218
217int ac_queue_message(void *acp, struct RTPMessage *msg) 219int ac_queue_message(const Mono_Time *mono_time, void *acp, struct RTPMessage *msg)
218{ 220{
219 if (!acp || !msg) { 221 if (!acp || !msg) {
220 return -1; 222 return -1;
@@ -496,7 +498,7 @@ bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_b
496bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels) 498bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels)
497{ 499{
498 if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) { 500 if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) {
499 if (current_time_monotonic() - ac->ldrts < 500) { 501 if (current_time_monotonic(ac->mono_time) - ac->ldrts < 500) {
500 return false; 502 return false;
501 } 503 }
502 504
@@ -510,7 +512,7 @@ bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t chan
510 512
511 ac->ld_sample_rate = sampling_rate; 513 ac->ld_sample_rate = sampling_rate;
512 ac->ld_channel_count = channels; 514 ac->ld_channel_count = channels;
513 ac->ldrts = current_time_monotonic(); 515 ac->ldrts = current_time_monotonic(ac->mono_time);
514 516
515 opus_decoder_destroy(ac->decoder); 517 opus_decoder_destroy(ac->decoder);
516 ac->decoder = new_dec; 518 ac->decoder = new_dec;
diff --git a/toxav/audio.h b/toxav/audio.h
index f71aaed3..6a19e6b6 100644
--- a/toxav/audio.h
+++ b/toxav/audio.h
@@ -50,6 +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 const Logger *log; 54 const Logger *log;
54 55
55 /* encoding */ 56 /* encoding */
@@ -77,11 +78,11 @@ typedef struct ACSession_s {
77 void *acb_user_data; 78 void *acb_user_data;
78} ACSession; 79} ACSession;
79 80
80ACSession *ac_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, 81ACSession *ac_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
81 void *cb_data); 82 toxav_audio_receive_frame_cb *cb, void *cb_data);
82void ac_kill(ACSession *ac); 83void ac_kill(ACSession *ac);
83void ac_iterate(ACSession *ac); 84void ac_iterate(ACSession *ac);
84int ac_queue_message(void *acp, struct RTPMessage *msg); 85int ac_queue_message(const Mono_Time *mono_time, void *acp, struct RTPMessage *msg);
85int 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);
86 87
87#endif /* AUDIO_H */ 88#endif /* AUDIO_H */
diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c
index 460cf118..695bb99b 100644
--- a/toxav/bwcontroller.c
+++ b/toxav/bwcontroller.c
@@ -83,7 +83,7 @@ BWController *bwc_new(Messenger *m, uint32_t friendnumber, m_cb *mcb, void *mcb_
83 retu->mcb_user_data = mcb_user_data; 83 retu->mcb_user_data = mcb_user_data;
84 retu->m = m; 84 retu->m = m;
85 retu->friend_number = friendnumber; 85 retu->friend_number = friendnumber;
86 uint64_t now = current_time_monotonic(); 86 uint64_t now = current_time_monotonic(m->mono_time);
87 retu->cycle.last_sent_timestamp = now; 87 retu->cycle.last_sent_timestamp = now;
88 retu->cycle.last_refresh_timestamp = now; 88 retu->cycle.last_refresh_timestamp = now;
89 retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT); 89 retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT);
@@ -138,7 +138,7 @@ void bwc_add_recv(BWController *bwc, uint32_t recv_bytes)
138void send_update(BWController *bwc) 138void send_update(BWController *bwc)
139{ 139{
140 if (bwc->packet_loss_counted_cycles > BWC_AVG_LOSS_OVER_CYCLES_COUNT && 140 if (bwc->packet_loss_counted_cycles > BWC_AVG_LOSS_OVER_CYCLES_COUNT &&
141 current_time_monotonic() - bwc->cycle.last_sent_timestamp > BWC_SEND_INTERVAL_MS) { 141 current_time_monotonic(bwc->m->mono_time) - bwc->cycle.last_sent_timestamp > BWC_SEND_INTERVAL_MS) {
142 bwc->packet_loss_counted_cycles = 0; 142 bwc->packet_loss_counted_cycles = 0;
143 143
144 if (bwc->cycle.lost) { 144 if (bwc->cycle.lost) {
@@ -159,7 +159,7 @@ void send_update(BWController *bwc)
159 } 159 }
160 } 160 }
161 161
162 bwc->cycle.last_sent_timestamp = current_time_monotonic(); 162 bwc->cycle.last_sent_timestamp = current_time_monotonic(bwc->m->mono_time);
163 bwc->cycle.lost = 0; 163 bwc->cycle.lost = 0;
164 bwc->cycle.recv = 0; 164 bwc->cycle.recv = 0;
165 } 165 }
@@ -170,12 +170,12 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg)
170 LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", (void *)bwc); 170 LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", (void *)bwc);
171 171
172 /* Peers sent update too soon */ 172 /* Peers sent update too soon */
173 if (bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS > current_time_monotonic()) { 173 if (bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS > current_time_monotonic(bwc->m->mono_time)) {
174 LOGGER_INFO(bwc->m->log, "%p Rejecting extra update", (void *)bwc); 174 LOGGER_INFO(bwc->m->log, "%p Rejecting extra update", (void *)bwc);
175 return -1; 175 return -1;
176 } 176 }
177 177
178 bwc->cycle.last_recv_timestamp = current_time_monotonic(); 178 bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->m->mono_time);
179 179
180 uint32_t recv = net_ntohl(msg->recv); 180 uint32_t recv = net_ntohl(msg->recv);
181 uint32_t lost = net_ntohl(msg->lost); 181 uint32_t lost = net_ntohl(msg->lost);
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 61297937..77cae626 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -389,7 +389,7 @@ static int handle_video_packet(RTPSession *session, const struct RTPHeader *head
389 LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-001a b0=%d b1=%d", (int)m_new->data[0], (int)m_new->data[1]); 389 LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-001a b0=%d b1=%d", (int)m_new->data[0], (int)m_new->data[1]);
390 update_bwc_values(log, session, m_new); 390 update_bwc_values(log, session, m_new);
391 // Pass ownership of m_new to the callback. 391 // Pass ownership of m_new to the callback.
392 session->mcb(session->cs, m_new); 392 session->mcb(session->m->mono_time, session->cs, m_new);
393 // Now we no longer own m_new. 393 // Now we no longer own m_new.
394 m_new = nullptr; 394 m_new = nullptr;
395 395
@@ -426,7 +426,7 @@ static int handle_video_packet(RTPSession *session, const struct RTPHeader *head
426 if (m_new) { 426 if (m_new) {
427 LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-003a b0=%d b1=%d", (int)m_new->data[0], (int)m_new->data[1]); 427 LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-003a b0=%d b1=%d", (int)m_new->data[0], (int)m_new->data[1]);
428 update_bwc_values(log, session, m_new); 428 update_bwc_values(log, session, m_new);
429 session->mcb(session->cs, m_new); 429 session->mcb(session->m->mono_time, session->cs, m_new);
430 430
431 m_new = nullptr; 431 m_new = nullptr;
432 } 432 }
@@ -499,15 +499,15 @@ static int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t
499 499
500 /* Invoke processing of active multiparted message */ 500 /* Invoke processing of active multiparted message */
501 if (session->mp) { 501 if (session->mp) {
502 session->mcb(session->cs, session->mp); 502 session->mcb(session->m->mono_time, session->cs, session->mp);
503 session->mp = nullptr; 503 session->mp = nullptr;
504 } 504 }
505 505
506 /* The message came in the allowed time; 506 /* The message came in the allowed time;
507 */ 507 */
508 508
509 return session->mcb(session->cs, new_message(&header, length - RTP_HEADER_SIZE, data + RTP_HEADER_SIZE, 509 return session->mcb(session->m->mono_time, session->cs, new_message(&header, length - RTP_HEADER_SIZE,
510 length - RTP_HEADER_SIZE)); 510 data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE));
511 } 511 }
512 512
513 /* The message is sent in multiple parts */ 513 /* The message is sent in multiple parts */
@@ -542,7 +542,7 @@ static int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t
542 /* Received a full message; now push it for the further 542 /* Received a full message; now push it for the further
543 * processing. 543 * processing.
544 */ 544 */
545 session->mcb(session->cs, session->mp); 545 session->mcb(session->m->mono_time, session->cs, session->mp);
546 session->mp = nullptr; 546 session->mp = nullptr;
547 } 547 }
548 } else { 548 } else {
@@ -555,7 +555,7 @@ static int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t
555 } 555 }
556 556
557 /* Push the previous message for processing */ 557 /* Push the previous message for processing */
558 session->mcb(session->cs, session->mp); 558 session->mcb(session->m->mono_time, session->cs, session->mp);
559 559
560 session->mp = nullptr; 560 session->mp = nullptr;
561 goto NEW_MULTIPARTED; 561 goto NEW_MULTIPARTED;
@@ -760,7 +760,7 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
760 760
761 header.sequnum = session->sequnum; 761 header.sequnum = session->sequnum;
762 762
763 header.timestamp = current_time_monotonic(); 763 header.timestamp = current_time_monotonic(session->m->mono_time);
764 764
765 header.ssrc = session->ssrc; 765 header.ssrc = session->ssrc;
766 766
diff --git a/toxav/rtp.h b/toxav/rtp.h
index 225f4c03..42997594 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(void *cs, struct RTPMessage *msg); 162typedef int rtp_m_cb(const 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/toxav.c b/toxav/toxav.c
index 84e67858..19648936 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -248,7 +248,7 @@ void toxav_iterate(ToxAV *av)
248 return; 248 return;
249 } 249 }
250 250
251 uint64_t start = current_time_monotonic(); 251 uint64_t start = current_time_monotonic(av->m->mono_time);
252 int32_t rc = 500; 252 int32_t rc = 500;
253 253
254 ToxAVCall *i = av->calls[av->calls_head]; 254 ToxAVCall *i = av->calls[av->calls_head];
@@ -286,7 +286,7 @@ void toxav_iterate(ToxAV *av)
286 pthread_mutex_unlock(av->mutex); 286 pthread_mutex_unlock(av->mutex);
287 287
288 av->interval = rc < av->dmssa ? 0 : (rc - av->dmssa); 288 av->interval = rc < av->dmssa ? 0 : (rc - av->dmssa);
289 av->dmsst += current_time_monotonic() - start; 289 av->dmsst += current_time_monotonic(av->m->mono_time) - start;
290 290
291 if (++av->dmssc == 3) { 291 if (++av->dmssc == 3) {
292 av->dmssa = av->dmsst / 3 + 5 /* NOTE Magic Offset for precission */; 292 av->dmssa = av->dmsst / 3 + 5 /* NOTE Magic Offset for precission */;
@@ -1325,7 +1325,7 @@ bool call_prepare_transmission(ToxAVCall *call)
1325 call->bwc = bwc_new(av->m, call->friend_number, callback_bwc, call); 1325 call->bwc = bwc_new(av->m, call->friend_number, callback_bwc, call);
1326 1326
1327 { /* Prepare audio */ 1327 { /* Prepare audio */
1328 call->audio = ac_new(av->m->log, av, call->friend_number, av->acb, av->acb_user_data); 1328 call->audio = ac_new(av->m->mono_time, av->m->log, av, call->friend_number, av->acb, av->acb_user_data);
1329 1329
1330 if (!call->audio) { 1330 if (!call->audio) {
1331 LOGGER_ERROR(av->m->log, "Failed to create audio codec session"); 1331 LOGGER_ERROR(av->m->log, "Failed to create audio codec session");
@@ -1341,7 +1341,7 @@ bool call_prepare_transmission(ToxAVCall *call)
1341 } 1341 }
1342 } 1342 }
1343 { /* Prepare video */ 1343 { /* Prepare video */
1344 call->video = vc_new(av->m->log, av, call->friend_number, av->vcb, av->vcb_user_data); 1344 call->video = vc_new(av->m->mono_time, av->m->log, av, call->friend_number, av->vcb, av->vcb_user_data);
1345 1345
1346 if (!call->video) { 1346 if (!call->video) {
1347 LOGGER_ERROR(av->m->log, "Failed to create video codec session"); 1347 LOGGER_ERROR(av->m->log, "Failed to create video codec session");
diff --git a/toxav/video.c b/toxav/video.c
index 94f10230..bc164cea 100644
--- a/toxav/video.c
+++ b/toxav/video.c
@@ -159,7 +159,8 @@ 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 Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data) 162VCSession *vc_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
163 toxav_video_receive_frame_cb *cb, void *cb_data)
163{ 164{
164 VCSession *vc = (VCSession *)calloc(sizeof(VCSession), 1); 165 VCSession *vc = (VCSession *)calloc(sizeof(VCSession), 1);
165 vpx_codec_err_t rc; 166 vpx_codec_err_t rc;
@@ -264,7 +265,7 @@ VCSession *vc_new(const Logger *log, ToxAV *av, uint32_t friend_number, toxav_vi
264 goto BASE_CLEANUP_1; 265 goto BASE_CLEANUP_1;
265 } 266 }
266 */ 267 */
267 vc->linfts = current_time_monotonic(); 268 vc->linfts = current_time_monotonic(mono_time);
268 vc->lcfd = 60; 269 vc->lcfd = 60;
269 vc->vcb = cb; 270 vc->vcb = cb;
270 vc->vcb_user_data = cb_data; 271 vc->vcb_user_data = cb_data;
@@ -355,7 +356,7 @@ void vc_iterate(VCSession *vc)
355 } 356 }
356} 357}
357 358
358int vc_queue_message(void *vcp, struct RTPMessage *msg) 359int vc_queue_message(const Mono_Time *mono_time, void *vcp, struct RTPMessage *msg)
359{ 360{
360 /* This function is called with complete messages 361 /* This function is called with complete messages
361 * they have already been assembled. 362 * they have already been assembled.
@@ -389,9 +390,9 @@ int vc_queue_message(void *vcp, struct RTPMessage *msg)
389 free(rb_write(vc->vbuf_raw, msg)); 390 free(rb_write(vc->vbuf_raw, msg));
390 391
391 /* Calculate time it took for peer to send us this frame */ 392 /* Calculate time it took for peer to send us this frame */
392 uint32_t t_lcfd = current_time_monotonic() - vc->linfts; 393 uint32_t t_lcfd = current_time_monotonic(mono_time) - vc->linfts;
393 vc->lcfd = t_lcfd > 100 ? vc->lcfd : t_lcfd; 394 vc->lcfd = t_lcfd > 100 ? vc->lcfd : t_lcfd;
394 vc->linfts = current_time_monotonic(); 395 vc->linfts = current_time_monotonic(mono_time);
395 pthread_mutex_unlock(vc->queue_mutex); 396 pthread_mutex_unlock(vc->queue_mutex);
396 return 0; 397 return 0;
397} 398}
diff --git a/toxav/video.h b/toxav/video.h
index 898c986a..14d866d1 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 Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, 63VCSession *vc_new(const Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
64 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(void *vcp, struct RTPMessage *msg); 67int vc_queue_message(const 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 3c415420..7d881b09 100644
--- a/toxcore/mono_time.c
+++ b/toxcore/mono_time.c
@@ -39,7 +39,7 @@ Mono_Time *mono_time_new(void)
39 } 39 }
40 40
41 monotime->time = 0; 41 monotime->time = 0;
42 monotime->base_time = 0; 42 monotime->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(monotime) / 1000ULL);
43 43
44 mono_time_update(monotime); 44 mono_time_update(monotime);
45 45
@@ -53,11 +53,7 @@ void mono_time_free(Mono_Time *monotime)
53 53
54void mono_time_update(Mono_Time *monotime) 54void mono_time_update(Mono_Time *monotime)
55{ 55{
56 if (monotime->base_time == 0) { 56 monotime->time = (current_time_monotonic(monotime) / 1000ULL) + monotime->base_time;
57 monotime->base_time = ((uint64_t)time(nullptr) - (current_time_monotonic() / 1000ULL));
58 }
59
60 monotime->time = (current_time_monotonic() / 1000ULL) + monotime->base_time;
61} 57}
62 58
63uint64_t mono_time_get(const Mono_Time *monotime) 59uint64_t mono_time_get(const Mono_Time *monotime)
@@ -74,34 +70,34 @@ bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_
74//!TOKSTYLE- 70//!TOKSTYLE-
75// No global mutable state in Tokstyle. 71// No global mutable state in Tokstyle.
76#ifdef OS_WIN32 72#ifdef OS_WIN32
77static uint64_t last_monotime; 73static uint64_t last_clock_mono;
78static uint64_t add_monotime; 74static uint64_t add_clock_mono;
79#endif 75#endif
80//!TOKSTYLE+ 76//!TOKSTYLE+
81 77
82/* return current monotonic time in milliseconds (ms). */ 78/* return current monotonic time in milliseconds (ms). */
83uint64_t current_time_monotonic(void) 79uint64_t current_time_monotonic(const Mono_Time *monotime)
84{ 80{
85 uint64_t time; 81 uint64_t time;
86#ifdef OS_WIN32 82#ifdef OS_WIN32
87 uint64_t old_add_monotime = add_monotime; 83 uint64_t old_add_clock_mono = add_clock_mono;
88 time = (uint64_t)GetTickCount() + add_monotime; 84 time = (uint64_t)GetTickCount() + add_clock_mono;
89 85
90 /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race 86 /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race
91 * conditions when multiple threads call this function at once */ 87 * conditions when multiple threads call this function at once */
92 if (time + 0x10000 < last_monotime) { 88 if (time + 0x10000 < last_clock_mono) {
93 uint32_t add = ~0; 89 uint32_t add = ~0;
94 /* use old_add_monotime rather than simply incrementing add_monotime, to handle the case that many threads 90 /* use old_add_clock_mono rather than simply incrementing add_clock_mono, to handle the case that many threads
95 * simultaneously detect an overflow */ 91 * simultaneously detect an overflow */
96 add_monotime = old_add_monotime + add; 92 add_clock_mono = old_add_clock_mono + add;
97 time += add; 93 time += add;
98 } 94 }
99 95
100 last_monotime = time; 96 last_clock_mono = time;
101#else 97#else
102 struct timespec monotime; 98 struct timespec clock_mono;
103#if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW) 99#if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW)
104 clock_gettime(CLOCK_MONOTONIC_RAW, &monotime); 100 clock_gettime(CLOCK_MONOTONIC_RAW, &clock_mono);
105#elif defined(__APPLE__) 101#elif defined(__APPLE__)
106 clock_serv_t muhclock; 102 clock_serv_t muhclock;
107 mach_timespec_t machtime; 103 mach_timespec_t machtime;
@@ -110,12 +106,12 @@ uint64_t current_time_monotonic(void)
110 clock_get_time(muhclock, &machtime); 106 clock_get_time(muhclock, &machtime);
111 mach_port_deallocate(mach_task_self(), muhclock); 107 mach_port_deallocate(mach_task_self(), muhclock);
112 108
113 monotime.tv_sec = machtime.tv_sec; 109 clock_mono.tv_sec = machtime.tv_sec;
114 monotime.tv_nsec = machtime.tv_nsec; 110 clock_mono.tv_nsec = machtime.tv_nsec;
115#else 111#else
116 clock_gettime(CLOCK_MONOTONIC, &monotime); 112 clock_gettime(CLOCK_MONOTONIC, &clock_mono);
117#endif 113#endif
118 time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL); 114 time = 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
119#endif 115#endif
120 return time; 116 return time;
121} 117}
diff --git a/toxcore/mono_time.h b/toxcore/mono_time.h
index 1d2dd539..489bf138 100644
--- a/toxcore/mono_time.h
+++ b/toxcore/mono_time.h
@@ -50,7 +50,7 @@ uint64_t mono_time_get(const Mono_Time *monotime);
50bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_t timeout); 50bool mono_time_is_timeout(const Mono_Time *monotime, 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(void); 53uint64_t current_time_monotonic(const Mono_Time *monotime);
54 54
55#ifdef __cplusplus 55#ifdef __cplusplus
56} 56}
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index a577b6c7..11795827 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -703,7 +703,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
703 pthread_mutex_lock(&conn->mutex); 703 pthread_mutex_lock(&conn->mutex);
704 704
705 if (ret == 0) { 705 if (ret == 0) {
706 conn->last_tcp_sent = current_time_monotonic(); 706 conn->last_tcp_sent = current_time_monotonic(c->mono_time);
707 } 707 }
708 708
709 pthread_mutex_unlock(&conn->mutex); 709 pthread_mutex_unlock(&conn->mutex);
@@ -957,8 +957,8 @@ static int generate_request_packet(const Logger *log, uint8_t *data, uint16_t le
957 * return -1 on failure. 957 * return -1 on failure.
958 * return number of requested packets on success. 958 * return number of requested packets on success.
959 */ 959 */
960static int handle_request_packet(const Logger *log, Packets_Array *send_array, const uint8_t *data, uint16_t length, 960static int handle_request_packet(const Mono_Time *mono_time, const Logger *log, Packets_Array *send_array,
961 uint64_t *latest_send_time, uint64_t rtt_time) 961 const uint8_t *data, uint16_t length, uint64_t *latest_send_time, uint64_t rtt_time)
962{ 962{
963 if (length == 0) { 963 if (length == 0) {
964 return -1; 964 return -1;
@@ -978,7 +978,7 @@ static int handle_request_packet(const Logger *log, Packets_Array *send_array, c
978 uint32_t n = 1; 978 uint32_t n = 1;
979 uint32_t requested = 0; 979 uint32_t requested = 0;
980 980
981 const uint64_t temp_time = current_time_monotonic(); 981 const uint64_t temp_time = current_time_monotonic(mono_time);
982 uint64_t l_sent_time = ~0; 982 uint64_t l_sent_time = ~0;
983 983
984 for (uint32_t i = send_array->buffer_start; i != send_array->buffer_end; ++i) { 984 for (uint32_t i = send_array->buffer_start; i != send_array->buffer_end; ++i) {
@@ -1120,7 +1120,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
1120 return -1; 1120 return -1;
1121 } 1121 }
1122 1122
1123 dt->sent_time = current_time_monotonic(); 1123 dt->sent_time = current_time_monotonic(c->mono_time);
1124 } 1124 }
1125 1125
1126 conn->maximum_speed_reached = 0; 1126 conn->maximum_speed_reached = 0;
@@ -1173,7 +1173,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
1173 Packet_Data *dt1 = nullptr; 1173 Packet_Data *dt1 = nullptr;
1174 1174
1175 if (get_data_pointer(c->log, &conn->send_array, &dt1, packet_num) == 1) { 1175 if (get_data_pointer(c->log, &conn->send_array, &dt1, packet_num) == 1) {
1176 dt1->sent_time = current_time_monotonic(); 1176 dt1->sent_time = current_time_monotonic(c->mono_time);
1177 } 1177 }
1178 } else { 1178 } else {
1179 conn->maximum_speed_reached = 1; 1179 conn->maximum_speed_reached = 1;
@@ -1279,7 +1279,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32
1279 return -1; 1279 return -1;
1280 } 1280 }
1281 1281
1282 const uint64_t temp_time = current_time_monotonic(); 1282 const uint64_t temp_time = current_time_monotonic(c->mono_time);
1283 uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array); 1283 uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array);
1284 1284
1285 for (i = 0; i < array_size; ++i) { 1285 for (i = 0; i < array_size; ++i) {
@@ -1395,7 +1395,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
1395 return -1; 1395 return -1;
1396 } 1396 }
1397 1397
1398 conn->temp_packet_sent_time = current_time_monotonic(); 1398 conn->temp_packet_sent_time = current_time_monotonic(c->mono_time);
1399 ++conn->temp_packet_num_sent; 1399 ++conn->temp_packet_num_sent;
1400 return 0; 1400 return 0;
1401} 1401}
@@ -1545,7 +1545,8 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
1545 rtt_time = DEFAULT_TCP_PING_CONNECTION; 1545 rtt_time = DEFAULT_TCP_PING_CONNECTION;
1546 } 1546 }
1547 1547
1548 int requested = handle_request_packet(c->log, &conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time); 1548 int requested = handle_request_packet(c->mono_time, c->log, &conn->send_array, real_data, real_length, &rtt_calc_time,
1549 rtt_time);
1549 1550
1550 if (requested == -1) { 1551 if (requested == -1) {
1551 return -1; 1552 return -1;
@@ -1598,7 +1599,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
1598 } 1599 }
1599 1600
1600 if (rtt_calc_time != 0) { 1601 if (rtt_calc_time != 0) {
1601 uint64_t rtt_time = current_time_monotonic() - rtt_calc_time; 1602 uint64_t rtt_time = current_time_monotonic(c->mono_time) - rtt_calc_time;
1602 1603
1603 if (rtt_time < conn->rtt_time) { 1604 if (rtt_time < conn->rtt_time) {
1604 conn->rtt_time = rtt_time; 1605 conn->rtt_time = rtt_time;
@@ -2441,7 +2442,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2441 2442
2442static void send_crypto_packets(Net_Crypto *c) 2443static void send_crypto_packets(Net_Crypto *c)
2443{ 2444{
2444 const uint64_t temp_time = current_time_monotonic(); 2445 const uint64_t temp_time = current_time_monotonic(c->mono_time);
2445 double total_send_rate = 0; 2446 double total_send_rate = 0;
2446 uint32_t peak_request_packet_interval = ~0; 2447 uint32_t peak_request_packet_interval = ~0;
2447 2448