Skip to content

Commit

Permalink
cleanup: Apply stronger type checks and fix errors.
Browse files Browse the repository at this point in the history
Found a bug, too: file recv and chunk events were intermittently
converting to `uint32_t`, losing precision.
  • Loading branch information
iphydf committed Feb 17, 2022
1 parent 0f35346 commit 7b2f6c3
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion auto_tests/messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ int main(void)
options.port_range[0] = 41234;
options.port_range[1] = 44234;
options.log_callback = (logger_cb *)print_debug_log;
unsigned int err;
Messenger_Error err;
m = new_messenger(mono_time, &options, &err);
ck_assert(err == MESSENGER_ERROR_NONE);

Expand Down
2 changes: 1 addition & 1 deletion testing/Messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int main(int argc, char *argv[])

Messenger_Options options = {0};
options.ipv6enabled = ipv6enabled;
unsigned int err;
Messenger_Error err;
m = new_messenger(mono_time, &options, &err);

if (!m) {
Expand Down
22 changes: 11 additions & 11 deletions toxav/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ static void jbuf_clear(struct JitterBuffer *q);
static void jbuf_free(struct JitterBuffer *q);
static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m);
static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success);
static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate,
int32_t channel_count);
static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr,
uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch);
static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels);
static OpusEncoder *create_audio_encoder(const Logger *log, uint32_t bit_rate, uint32_t sampling_rate,
uint8_t channel_count);
static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, uint32_t new_br, uint32_t new_sr,
uint8_t new_ch, uint32_t *old_br, uint32_t *old_sr, uint8_t *old_ch);
static bool reconfigure_audio_decoder(ACSession *ac, uint32_t sampling_rate, uint8_t channels);



Expand Down Expand Up @@ -242,7 +242,7 @@ int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg)
return 0;
}

int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels)
int ac_reconfigure_encoder(ACSession *ac, uint32_t bit_rate, uint32_t sampling_rate, uint8_t channels)
{
if (!ac || !reconfigure_audio_encoder(ac->log, &ac->encoder, bit_rate,
sampling_rate, channels,
Expand Down Expand Up @@ -362,8 +362,8 @@ static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success)
*success = 0;
return nullptr;
}
static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate,
int32_t channel_count)
static OpusEncoder *create_audio_encoder(const Logger *log, uint32_t bit_rate, uint32_t sampling_rate,
uint8_t channel_count)
{
int status = OPUS_OK;
/*
Expand Down Expand Up @@ -456,8 +456,8 @@ static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, in
return nullptr;
}

static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr,
uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch)
static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, uint32_t new_br, uint32_t new_sr,
uint8_t new_ch, uint32_t *old_br, uint32_t *old_sr, uint8_t *old_ch)
{
/* Values are checked in toxav.c */
if (*old_sr != new_sr || *old_ch != new_ch) {
Expand Down Expand Up @@ -488,7 +488,7 @@ static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_
return true;
}

static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels)
static bool reconfigure_audio_decoder(ACSession *ac, uint32_t sampling_rate, uint8_t channels)
{
if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) {
if (current_time_monotonic(ac->mono_time) - ac->ldrts < 500) {
Expand Down
18 changes: 9 additions & 9 deletions toxav/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ typedef struct ACSession {

/* encoding */
OpusEncoder *encoder;
int32_t le_sample_rate; /* Last encoder sample rate */
int32_t le_channel_count; /* Last encoder channel count */
int32_t le_bit_rate; /* Last encoder bit rate */
uint32_t le_sample_rate; /* Last encoder sample rate */
uint8_t le_channel_count; /* Last encoder channel count */
uint32_t le_bit_rate; /* Last encoder bit rate */

/* decoding */
OpusDecoder *decoder;
int32_t lp_channel_count; /* Last packet channel count */
int32_t lp_sampling_rate; /* Last packet sample rate */
int32_t lp_frame_duration; /* Last packet frame duration */
int32_t ld_sample_rate; /* Last decoder sample rate */
int32_t ld_channel_count; /* Last decoder channel count */
uint8_t lp_channel_count; /* Last packet channel count */
uint32_t lp_sampling_rate; /* Last packet sample rate */
uint32_t lp_frame_duration; /* Last packet frame duration */
uint32_t ld_sample_rate; /* Last decoder sample rate */
uint8_t ld_channel_count; /* Last decoder channel count */
uint64_t ldrts; /* Last decoder reconfiguration time stamp */
void *j_buf;

Expand All @@ -68,6 +68,6 @@ ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f
void ac_kill(ACSession *ac);
void ac_iterate(ACSession *ac);
int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg);
int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels);
int ac_reconfigure_encoder(ACSession *ac, uint32_t bit_rate, uint32_t sampling_rate, uint8_t channels);

#endif // C_TOXCORE_TOXAV_AUDIO_H
2 changes: 1 addition & 1 deletion toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static void iterate_common(ToxAV *av, bool audio)
if (i->msi_call->self_capabilities & MSI_CAP_R_VIDEO &&
i->msi_call->peer_capabilities & MSI_CAP_S_VIDEO) {
pthread_mutex_lock(i->video->queue_mutex);
frame_time = min_u32(i->video->lcfd, frame_time);
frame_time = min_s32(i->video->lcfd, frame_time);
pthread_mutex_unlock(i->video->queue_mutex);
}
}
Expand Down
8 changes: 5 additions & 3 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2483,15 +2483,17 @@ void do_messenger(Messenger *m, void *userdata)

for (uint32_t dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) {
if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) {
m2dht[friend_idx] = dhtfriend;
assert(dhtfriend < INT32_MAX);
m2dht[friend_idx] = (int32_t)dhtfriend;
break;
}
}
}

for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
if (m2dht[friend_idx] >= 0) {
dht2m[m2dht[friend_idx]] = friend_idx;
assert(friend_idx < INT32_MAX);
dht2m[m2dht[friend_idx]] = (int32_t)friend_idx;
}
}

Expand Down Expand Up @@ -3164,7 +3166,7 @@ uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_s
*
* if error is not NULL it will be set to one of the values in the enum above.
*/
Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsigned int *error)
Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, Messenger_Error *error)
{
if (!options) {
return nullptr;
Expand Down
6 changes: 2 additions & 4 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#ifndef C_TOXCORE_TOXCORE_MESSENGER_H
#define C_TOXCORE_TOXCORE_MESSENGER_H

#include <time.h>

#include "TCP_server.h"
#include "friend_connection.h"
#include "friend_requests.h"
Expand Down Expand Up @@ -260,7 +258,7 @@ struct Messenger {
Friend *friendlist;
uint32_t numfriends;

time_t lastdump;
uint64_t lastdump;
uint8_t is_receiving_file;

bool has_added_relays; // If the first connection has occurred in do_messenger
Expand Down Expand Up @@ -754,7 +752,7 @@ typedef enum Messenger_Error {
* if error is not NULL it will be set to one of the values in the enum above.
*/
non_null()
Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsigned int *error);
Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, Messenger_Error *error);

/** Run this before closing shop
* Free all datastructures.
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ void do_TCP_connection(const Logger *logger, const Mono_Time *mono_time,

if (sizeof(data) == len) {
if (handle_handshake(tcp_connection, data) == 0) {
tcp_connection->kill_at = -1;
tcp_connection->kill_at = UINT64_MAX;
tcp_connection->status = TCP_CLIENT_CONFIRMED;
} else {
tcp_connection->kill_at = 0;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ uint32_t tox_event_file_recv_get_kind(const Tox_Event_File_Recv *file_recv)

non_null()
static void tox_event_file_recv_set_file_size(Tox_Event_File_Recv *file_recv,
uint32_t file_size)
uint64_t file_size)
{
assert(file_recv != nullptr);
file_recv->file_size = file_size;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/events/file_recv_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ uint32_t tox_event_file_recv_chunk_get_file_number(const Tox_Event_File_Recv_Chu

non_null()
static void tox_event_file_recv_chunk_set_position(Tox_Event_File_Recv_Chunk *file_recv_chunk,
uint32_t position)
uint64_t position)
{
assert(file_recv_chunk != nullptr);
file_recv_chunk->position = position;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ static void populate_path_nodes_tcp(Onion_Client *onion_c)
#define RUN_COUNT_FRIEND_ANNOUNCE_BEGINNING 17

#define ONION_FRIEND_BACKOFF_FACTOR 4
#define ONION_FRIEND_MAX_PING_INTERVAL (5*60*MAX_ONION_CLIENTS)
#define ONION_FRIEND_MAX_PING_INTERVAL (uint64_t)(5*60*MAX_ONION_CLIENTS)

non_null()
static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
Expand Down
2 changes: 1 addition & 1 deletion toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)

lock(tox);

unsigned int m_error;
Messenger_Error m_error;
tox->m = new_messenger(tox->mono_time, &m_options, &m_error);

// TODO(iphydf): Clarify this code, check for NULL before new_groupchats, so
Expand Down

0 comments on commit 7b2f6c3

Please sign in to comment.