From 560cd963cfe67ee0d04a3f98fe4066a45438c182 Mon Sep 17 00:00:00 2001 From: zoff99 Date: Wed, 7 Feb 2024 22:47:27 +0000 Subject: [PATCH] refactor: Make ToxAV independent of toxcore internals. --- CMakeLists.txt | 1 + auto_tests/toxav_many_test.c | 14 +- other/analysis/run-cppcheck | 2 - toxav/BUILD.bazel | 123 ++-------- toxav/Makefile.inc | 1 + toxav/audio.c | 19 +- toxav/audio.h | 8 +- toxav/bwcontroller.c | 103 +++++--- toxav/bwcontroller.h | 11 +- toxav/msi.c | 453 +++++++++++++++++++++-------------- toxav/msi.h | 37 +-- toxav/rtp.c | 376 +++++++++++++++-------------- toxav/rtp.h | 30 ++- toxav/toxav.c | 337 ++++++++++++++++---------- toxav/toxav.h | 3 + toxav/toxav_hacks.h | 35 +++ toxav/video.c | 7 +- toxav/video.h | 9 +- toxcore/BUILD.bazel | 7 +- toxcore/Messenger.c | 98 +------- toxcore/Messenger.h | 47 +--- toxcore/net_crypto.c | 160 +------------ toxcore/net_crypto.h | 9 +- toxcore/tox.c | 2 +- toxcore/tox.h | 11 +- 25 files changed, 932 insertions(+), 971 deletions(-) create mode 100644 toxav/toxav_hacks.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f6d37da13..4034946362 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -391,6 +391,7 @@ if(BUILD_TOXAV) toxav/rtp.h toxav/toxav.c toxav/toxav.h + toxav/toxav_hacks.h toxav/toxav_old.c toxav/video.c toxav/video.h) diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c index 2892f7245c..fe1a1033c6 100644 --- a/auto_tests/toxav_many_test.c +++ b/auto_tests/toxav_many_test.c @@ -168,29 +168,33 @@ static void test_av_three_calls(void) Time_Data time_data; pthread_mutex_init(&time_data.lock, nullptr); { + Tox_Options *opts = tox_options_new(nullptr); + ck_assert(opts != nullptr); + tox_options_set_experimental_thread_safety(opts, true); Tox_Err_New error; - bootstrap = tox_new_log(nullptr, &error, &index[0]); + bootstrap = tox_new_log(opts, &error, &index[0]); ck_assert(error == TOX_ERR_NEW_OK); time_data.clock = current_time_monotonic(bootstrap->mono_time); set_current_time_callback(bootstrap, &time_data); - alice = tox_new_log(nullptr, &error, &index[1]); + alice = tox_new_log(opts, &error, &index[1]); ck_assert(error == TOX_ERR_NEW_OK); set_current_time_callback(alice, &time_data); - bobs[0] = tox_new_log(nullptr, &error, &index[2]); + bobs[0] = tox_new_log(opts, &error, &index[2]); ck_assert(error == TOX_ERR_NEW_OK); set_current_time_callback(bobs[0], &time_data); - bobs[1] = tox_new_log(nullptr, &error, &index[3]); + bobs[1] = tox_new_log(opts, &error, &index[3]); ck_assert(error == TOX_ERR_NEW_OK); set_current_time_callback(bobs[1], &time_data); - bobs[2] = tox_new_log(nullptr, &error, &index[4]); + bobs[2] = tox_new_log(opts, &error, &index[4]); ck_assert(error == TOX_ERR_NEW_OK); set_current_time_callback(bobs[2], &time_data); + tox_options_free(opts); } printf("Created 5 instances of Tox\n"); diff --git a/other/analysis/run-cppcheck b/other/analysis/run-cppcheck index 7e341f698d..03c597d709 100755 --- a/other/analysis/run-cppcheck +++ b/other/analysis/run-cppcheck @@ -20,8 +20,6 @@ CPPCHECK+=("--suppress=knownConditionTrueFalse") CPPCHECK+=("--suppress=missingIncludeSystem") # TODO(iphydf): Maybe fix? CPPCHECK+=("--suppress=signConversion") -# TODO(iphydf): Fixed in the toxav refactor PR. -CPPCHECK+=("--suppress=redundantAssignment") # We use this for VLAs. CPPCHECK_CXX+=("--suppress=allocaCalled") diff --git a/toxav/BUILD.bazel b/toxav/BUILD.bazel index f9a669e15d..90fb40d716 100644 --- a/toxav/BUILD.bazel +++ b/toxav/BUILD.bazel @@ -41,125 +41,30 @@ cc_library( deps = ["//c-toxcore/toxcore:ccompat"], ) -cc_library( - name = "bwcontroller", - srcs = ["bwcontroller.c"], - hdrs = ["bwcontroller.h"], - deps = [ - ":ring_buffer", - "//c-toxcore/toxcore", - "//c-toxcore/toxcore:Messenger", - "//c-toxcore/toxcore:ccompat", - "//c-toxcore/toxcore:logger", - "//c-toxcore/toxcore:mono_time", - "//c-toxcore/toxcore:tox", - "//c-toxcore/toxcore:util", - ], -) - -cc_library( - name = "rtp", - srcs = ["rtp.c"], - hdrs = ["rtp.h"], - deps = [ - ":bwcontroller", - "//c-toxcore/toxcore:Messenger", - "//c-toxcore/toxcore:ccompat", - "//c-toxcore/toxcore:logger", - "//c-toxcore/toxcore:mono_time", - "//c-toxcore/toxcore:tox", - "//c-toxcore/toxcore:util", - ], -) - -cc_test( - name = "rtp_test", - size = "small", - srcs = ["rtp_test.cc"], - deps = [ - ":rtp", - "//c-toxcore/toxcore:crypto_core", - "@com_google_googletest//:gtest", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "audio", - srcs = ["audio.c"], - hdrs = ["audio.h"], - deps = [ - ":public_api", - ":rtp", - "//c-toxcore/toxcore:ccompat", - "//c-toxcore/toxcore:logger", - "//c-toxcore/toxcore:mono_time", - "//c-toxcore/toxcore:util", - "@opus", - ], -) - -cc_library( - name = "video", - srcs = [ - "msi.c", - "video.c", - ], - hdrs = [ - "msi.h", - "video.h", - ], - deps = [ - ":audio", - ":public_api", - ":ring_buffer", - ":rtp", - "//c-toxcore/toxcore:Messenger", - "//c-toxcore/toxcore:ccompat", - "//c-toxcore/toxcore:logger", - "//c-toxcore/toxcore:mono_time", - "//c-toxcore/toxcore:network", - "//c-toxcore/toxcore:util", - "@libvpx", - ], -) - -cc_library( - name = "groupav", - srcs = ["groupav.c"], - hdrs = ["groupav.h"], - deps = [ - "//c-toxcore/toxcore", - "//c-toxcore/toxcore:ccompat", - "//c-toxcore/toxcore:group", - "//c-toxcore/toxcore:logger", - "//c-toxcore/toxcore:mono_time", - "//c-toxcore/toxcore:tox", - "//c-toxcore/toxcore:util", - "@opus", - ], -) - cc_library( name = "toxav", - srcs = [ - "toxav.c", - "toxav_old.c", - ], - hdrs = [ - "toxav.h", - ], + srcs = glob( + [ + "*.c", + "*.h", + ], + exclude = ["toxav.h"], + ), + hdrs = ["toxav.h"], visibility = ["//c-toxcore:__subpackages__"], deps = [ - ":groupav", - ":rtp", - ":video", "//c-toxcore/toxcore:Messenger", "//c-toxcore/toxcore:ccompat", + "//c-toxcore/toxcore:group", "//c-toxcore/toxcore:logger", "//c-toxcore/toxcore:mono_time", + "//c-toxcore/toxcore:net_crypto", + "//c-toxcore/toxcore:network", "//c-toxcore/toxcore:tox", "//c-toxcore/toxcore:util", + "@libsodium", + "@libvpx", + "@opus", ], ) diff --git a/toxav/Makefile.inc b/toxav/Makefile.inc index a8465a2f66..6d34135f3c 100644 --- a/toxav/Makefile.inc +++ b/toxav/Makefile.inc @@ -19,6 +19,7 @@ libtoxav_la_SOURCES = ../toxav/rtp.h \ ../toxav/ring_buffer.h \ ../toxav/ring_buffer.c \ ../toxav/toxav.h \ + ../toxav/toxav_hacks.h \ ../toxav/toxav.c \ ../toxav/toxav_old.c diff --git a/toxav/audio.c b/toxav/audio.c index 2cbc02d20a..a84df89ae8 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -13,6 +13,7 @@ #include "../toxcore/ccompat.h" #include "../toxcore/logger.h" #include "../toxcore/mono_time.h" +#include "../toxcore/network.h" static struct JitterBuffer *jbuf_new(uint32_t capacity); static void jbuf_clear(struct JitterBuffer *q); @@ -25,7 +26,9 @@ static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, uint32 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); -ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, + + +ACSession *ac_new(Mono_Time *mono_time, const Logger *log, Tox *tox, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) { ACSession *ac = (ACSession *)calloc(1, sizeof(ACSession)); @@ -150,9 +153,9 @@ void ac_iterate(ACSession *ac) ac->lp_channel_count = opus_packet_get_nb_channels(msg->data + 4); - /* NOTE: even though OPUS supports decoding mono frames with stereo decoder and vice versa, - * it didn't work quite well. - */ + /** NOTE: even though OPUS supports decoding mono frames with stereo decoder and vice versa, + * it didn't work quite well. + */ if (!reconfigure_audio_decoder(ac, ac->lp_sampling_rate, ac->lp_channel_count)) { LOGGER_WARNING(ac->log, "Failed to reconfigure decoder!"); free(msg); @@ -273,6 +276,7 @@ static struct JitterBuffer *jbuf_new(uint32_t capacity) q->capacity = capacity; return q; } + static void jbuf_clear(struct JitterBuffer *q) { while (q->bottom != q->top) { @@ -281,6 +285,7 @@ static void jbuf_clear(struct JitterBuffer *q) ++q->bottom; } } + static void jbuf_free(struct JitterBuffer *q) { if (q == nullptr) { @@ -291,6 +296,11 @@ static void jbuf_free(struct JitterBuffer *q) free(q->queue); free(q); } + +/* + * if -1 is returned the RTPMessage m needs to be free'd by the caller + * if 0 is returned the RTPMessage m is stored in the ringbuffer and must NOT be freed by the caller + */ static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m) { const uint16_t sequnum = m->header.sequnum; @@ -319,6 +329,7 @@ static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessa return 0; } + static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success) { if (q->top == q->bottom) { diff --git a/toxav/audio.h b/toxav/audio.h index 3ae4251137..d7d8fd8b1a 100644 --- a/toxav/audio.h +++ b/toxav/audio.h @@ -34,6 +34,11 @@ #define AUDIO_MAX_BUFFER_SIZE_PCM16 ((AUDIO_MAX_SAMPLE_RATE * AUDIO_MAX_FRAME_DURATION_MS) / 1000) #define AUDIO_MAX_BUFFER_SIZE_BYTES (AUDIO_MAX_BUFFER_SIZE_PCM16 * 2) +#ifndef TOX_DEFINED +#define TOX_DEFINED +typedef struct Tox Tox; +#endif /* TOX_DEFINED */ + typedef struct ACSession { Mono_Time *mono_time; const Logger *log; @@ -61,9 +66,10 @@ typedef struct ACSession { /* Audio frame receive callback */ toxav_audio_receive_frame_cb *acb; void *acb_user_data; + Tox *tox; } ACSession; -ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, +ACSession *ac_new(Mono_Time *mono_time, const Logger *log, Tox *tox, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data); void ac_kill(ACSession *ac); void ac_iterate(ACSession *ac); diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c index e9ceb96b5a..0294e35d40 100644 --- a/toxav/bwcontroller.c +++ b/toxav/bwcontroller.c @@ -10,12 +10,16 @@ #include #include "ring_buffer.h" +#include "toxav_hacks.h" #include "../toxcore/ccompat.h" #include "../toxcore/logger.h" #include "../toxcore/mono_time.h" +#include "../toxcore/network.h" +#include "../toxcore/tox_private.h" #include "../toxcore/util.h" + #define BWC_PACKET_ID 196 #define BWC_SEND_INTERVAL_MS 950 // 0.95s #define BWC_AVG_PKT_COUNT 20 @@ -38,9 +42,8 @@ typedef struct BWCRcvPkt { struct BWController { m_cb *mcb; void *mcb_user_data; - - Messenger *m; Tox *tox; + const Logger *log; uint32_t friend_number; BWCCycle cycle; @@ -49,6 +52,7 @@ struct BWController { uint32_t packet_loss_counted_cycles; Mono_Time *bwc_mono_time; + bool bwc_receive_active; /* if this is set to false then incoming bwc packets will not be processed by bwc_handle_data() */ }; struct BWCMessage { @@ -56,11 +60,11 @@ struct BWCMessage { uint32_t recv; }; -static int bwc_handle_data(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object); -static int bwc_send_custom_lossy_packet(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length); +static void bwc_handle_data(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data); static void send_update(BWController *bwc); -BWController *bwc_new(Messenger *m, Tox *tox, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data, + +BWController *bwc_new(const Logger *log, Tox *tox, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data, Mono_Time *bwc_mono_time) { BWController *retu = (BWController *)calloc(1, sizeof(BWController)); @@ -69,16 +73,18 @@ BWController *bwc_new(Messenger *m, Tox *tox, uint32_t friendnumber, m_cb *mcb, return nullptr; } - LOGGER_DEBUG(m->log, "Creating bandwidth controller"); + LOGGER_DEBUG(log, "Creating bandwidth controller"); + retu->mcb = mcb; retu->mcb_user_data = mcb_user_data; - retu->m = m; retu->friend_number = friendnumber; retu->bwc_mono_time = bwc_mono_time; const uint64_t now = current_time_monotonic(bwc_mono_time); retu->cycle.last_sent_timestamp = now; retu->cycle.last_refresh_timestamp = now; retu->tox = tox; + retu->log = log; + retu->bwc_receive_active = true; retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT); retu->cycle.lost = 0; retu->cycle.recv = 0; @@ -89,7 +95,6 @@ BWController *bwc_new(Messenger *m, Tox *tox, uint32_t friendnumber, m_cb *mcb, rb_write(retu->rcvpkt.rb, &retu->rcvpkt.packet_length_array[i]); } - m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu); return retu; } @@ -99,7 +104,6 @@ void bwc_kill(BWController *bwc) return; } - m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, nullptr, nullptr); rb_kill(bwc->rcvpkt.rb); free(bwc); } @@ -111,7 +115,7 @@ void bwc_add_lost(BWController *bwc, uint32_t bytes_lost) } if (bytes_lost > 0) { - LOGGER_DEBUG(bwc->m->log, "BWC lost(1): %d", (int)bytes_lost); + LOGGER_DEBUG(bwc->log, "BWC lost(1): %d", (int)bytes_lost); bwc->cycle.lost += bytes_lost; send_update(bwc); } @@ -135,7 +139,7 @@ static void send_update(BWController *bwc) bwc->packet_loss_counted_cycles = 0; if (bwc->cycle.lost != 0) { - LOGGER_DEBUG(bwc->m->log, "%p Sent update rcv: %u lost: %u percent: %f %%", + LOGGER_DEBUG(bwc->log, "%p Sent update rcv: %u lost: %u percent: %f %%", (void *)bwc, bwc->cycle.recv, bwc->cycle.lost, ((double)bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0); uint8_t bwc_packet[sizeof(struct BWCMessage) + 1]; @@ -148,13 +152,11 @@ static void send_update(BWController *bwc) offset += net_pack_u32(bwc_packet + offset, bwc->cycle.recv); assert(offset == sizeof(bwc_packet)); - if (bwc_send_custom_lossy_packet(bwc->tox, bwc->friend_number, bwc_packet, sizeof(bwc_packet)) == -1) { - char *netstrerror = net_new_strerror(net_error()); - char *stdstrerror = net_new_strerror(errno); - LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s", - (unsigned)sizeof(bwc_packet), stdstrerror, netstrerror); - net_kill_strerror(stdstrerror); - net_kill_strerror(netstrerror); + Tox_Err_Friend_Custom_Packet error; + tox_friend_send_lossy_packet(bwc->tox, bwc->friend_number, bwc_packet, sizeof(bwc_packet), &error); + + if (error != TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { + LOGGER_WARNING(bwc->log, "BWC send failed: %d", error); } } @@ -166,11 +168,11 @@ static void send_update(BWController *bwc) static int on_update(BWController *bwc, const struct BWCMessage *msg) { - LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", (void *)bwc); + LOGGER_DEBUG(bwc->log, "%p Got update from peer", (void *)bwc); /* Peers sent update too soon */ if (bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS > current_time_monotonic(bwc->bwc_mono_time)) { - LOGGER_INFO(bwc->m->log, "%p Rejecting extra update", (void *)bwc); + LOGGER_INFO(bwc->log, "%p Rejecting extra update", (void *)bwc); return -1; } @@ -180,8 +182,8 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg) if (lost != 0 && bwc->mcb != nullptr) { const uint32_t recv = msg->recv; - LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u percentage: %f %%", recv, lost, - ((double)lost / (recv + lost)) * 100.0); + LOGGER_DEBUG(bwc->log, "recved: %u lost: %u percentage: %f %%", recv, lost, + ((double) lost / (recv + lost)) * 100.0); bwc->mcb(bwc, bwc->friend_number, (float)lost / (recv + lost), bwc->mcb_user_data); @@ -190,28 +192,41 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg) return 0; } -/* - * return -1 on failure, 0 on success - * - */ -static int bwc_send_custom_lossy_packet(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length) +static void bwc_handle_data(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Err_Friend_Custom_Packet error; - tox_friend_send_lossy_packet(tox, friendnumber, data, (size_t)length, &error); + /* get BWController object from Tox and friend number */ + ToxAV *toxav = (ToxAV *)tox_get_av_object(tox); - if (error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { - return 0; + if (toxav == nullptr) { + // LOGGER_ERROR(log, "Could not get ToxAV object from Tox"); + return; } - return -1; -} - -static int bwc_handle_data(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object) -{ - BWController *bwc = (BWController *)object; + const Logger *log = toxav_get_logger(toxav); if (length - 1 != sizeof(struct BWCMessage)) { - return -1; + LOGGER_ERROR(log, "Actual data size and length argument do not match"); + return; + } + + const ToxAVCall *call = call_get(toxav, friend_number); + + if (call == nullptr) { + LOGGER_ERROR(log, "Could not get ToxAVCall object from ToxAV"); + return; + } + + /* get Call object from Tox and friend number */ + BWController *bwc = bwc_controller_get(call); + + if (bwc == nullptr) { + LOGGER_WARNING(log, "No session!"); + return; + } + + if (!bwc->bwc_receive_active) { + LOGGER_WARNING(log, "receiving not allowed!"); + return; } size_t offset = 1; // Ignore packet id. @@ -220,5 +235,15 @@ static int bwc_handle_data(Messenger *m, uint32_t friend_number, const uint8_t * offset += net_unpack_u32(data + offset, &msg.recv); assert(offset == length); - return on_update(bwc, &msg); + on_update(bwc, &msg); +} + +void bwc_allow_receiving(Tox *tox) +{ + tox_callback_friend_lossy_packet_per_pktid(tox, bwc_handle_data, BWC_PACKET_ID); +} + +void bwc_stop_receiving(Tox *tox) +{ + tox_callback_friend_lossy_packet_per_pktid(tox, nullptr, BWC_PACKET_ID); } diff --git a/toxav/bwcontroller.h b/toxav/bwcontroller.h index 53931c030b..0a8f618bc0 100644 --- a/toxav/bwcontroller.h +++ b/toxav/bwcontroller.h @@ -5,19 +5,24 @@ #ifndef C_TOXCORE_TOXAV_BWCONTROLLER_H #define C_TOXCORE_TOXAV_BWCONTROLLER_H -#include "../toxcore/Messenger.h" +#include + +#include "../toxcore/logger.h" +#include "../toxcore/mono_time.h" #include "../toxcore/tox.h" typedef struct BWController BWController; typedef void m_cb(BWController *bwc, uint32_t friend_number, float loss, void *user_data); -BWController *bwc_new(Messenger *m, Tox *tox, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data, - Mono_Time *bwc_mono_time); +BWController *bwc_new(const Logger *log, Tox *tox, uint32_t friendnumber, + m_cb *mcb, void *mcb_user_data, Mono_Time *bwc_mono_time); void bwc_kill(BWController *bwc); void bwc_add_lost(BWController *bwc, uint32_t bytes_lost); void bwc_add_recv(BWController *bwc, uint32_t recv_bytes); +void bwc_allow_receiving(Tox *tox); +void bwc_stop_receiving(Tox *tox); #endif /* C_TOXCORE_TOXAV_BWCONTROLLER_H */ diff --git a/toxav/msi.c b/toxav/msi.c index 1b375410b2..6865f5427f 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -9,12 +9,22 @@ #include #include +#include "toxav_hacks.h" + #include "../toxcore/ccompat.h" #include "../toxcore/logger.h" +#include "../toxcore/net_crypto.h" +#include "../toxcore/tox.h" +#include "../toxcore/tox_private.h" #include "../toxcore/util.h" #define MSI_MAXMSG_SIZE 256 +#ifndef TOXAV_DEFINED +#define TOXAV_DEFINED +typedef struct ToxAV ToxAV; +#endif /* TOXAV_DEFINED */ + /** * Protocol: * @@ -55,20 +65,20 @@ typedef struct MSIMessage { } MSIMessage; static void msg_init(MSIMessage *dest, MSIRequest request); +static void kill_call(const Logger *log, MSICall *call); static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length); static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const uint8_t *value, uint8_t value_len, uint16_t *length); -static int send_message(const Messenger *m, uint32_t friend_number, const MSIMessage *msg); -static int send_error(const Messenger *m, uint32_t friend_number, MSIError error); -static bool invoke_callback(MSICall *call, MSICallbackID cb); +static int send_message(const Logger *log, Tox *tox, uint32_t friend_number, const MSIMessage *msg); +static int send_error(const Logger *log, Tox *tox, uint32_t friend_number, MSIError error); static MSICall *get_call(MSISession *session, uint32_t friend_number); static MSICall *new_call(MSISession *session, uint32_t friend_number); -static void kill_call(MSICall *call); -static void on_peer_status(Messenger *m, uint32_t friend_number, bool is_online, void *user_data); -static void handle_init(MSICall *call, const MSIMessage *msg); -static void handle_push(MSICall *call, const MSIMessage *msg); -static void handle_pop(MSICall *call, const MSIMessage *msg); -static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *user_data); +static bool invoke_callback(const Logger *log, MSICall *call, MSICallbackID cb); +static void handle_init(const Logger *log, MSICall *call, const MSIMessage *msg); +static void handle_push(const Logger *log, MSICall *call, const MSIMessage *msg); +static void handle_pop(const Logger *log, MSICall *call, const MSIMessage *msg); +static void handle_msi_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data); /* * Public functions @@ -99,43 +109,43 @@ void msi_callback_capabilities(MSISession *session, msi_action_cb *callback) session->capabilities_callback = callback; } -MSISession *msi_new(Messenger *m) +MSISession *msi_new(const Logger *log, Tox *tox) { - if (m == nullptr) { + if (tox == nullptr) { return nullptr; } MSISession *retu = (MSISession *)calloc(1, sizeof(MSISession)); if (retu == nullptr) { - LOGGER_ERROR(m->log, "Allocation failed! Program might misbehave!"); + LOGGER_ERROR(log, "Allocation failed! Program might misbehave!"); return nullptr; } if (create_recursive_mutex(retu->mutex) != 0) { - LOGGER_ERROR(m->log, "Failed to init mutex! Program might misbehave"); + LOGGER_ERROR(log, "Failed to init mutex! Program might misbehave"); free(retu); return nullptr; } - retu->messenger = m; + retu->tox = tox; - m_callback_msi_packet(m, handle_msi_packet, retu); + // register callback + tox_callback_friend_lossless_packet_per_pktid(tox, handle_msi_packet, PACKET_ID_MSI); - /* This is called when remote terminates session */ - m_callback_connectionstatus_internal_av(m, on_peer_status, retu); - - LOGGER_DEBUG(m->log, "New msi session: %p ", (void *)retu); + LOGGER_DEBUG(log, "New msi session: %p ", (void *)retu); return retu; } -int msi_kill(MSISession *session, const Logger *log) + +int msi_kill(const Logger *log, Tox *tox, MSISession *session) { if (session == nullptr) { LOGGER_ERROR(log, "Tried to terminate non-existing session"); return -1; } - m_callback_msi_packet(session->messenger, nullptr, nullptr); + // UN-register callback + tox_callback_friend_lossless_packet_per_pktid(tox, nullptr, PACKET_ID_MSI); if (pthread_mutex_trylock(session->mutex) != 0) { LOGGER_ERROR(log, "Failed to acquire lock on msi mutex"); @@ -149,10 +159,10 @@ int msi_kill(MSISession *session, const Logger *log) MSICall *it = get_call(session, session->calls_head); while (it != nullptr) { - send_message(session->messenger, it->friend_number, &msg); + send_message(log, session->tox, it->friend_number, &msg); MSICall *temp_it = it; it = it->next; - kill_call(temp_it); /* This will eventually free session->calls */ + kill_call(log, temp_it); /* This will eventually free session->calls */ } } @@ -163,21 +173,57 @@ int msi_kill(MSISession *session, const Logger *log) free(session); return 0; } -int msi_invite(MSISession *session, MSICall **call, uint32_t friend_number, uint8_t capabilities) + +/* + * return true if friend was offline and the call was canceled + */ +bool check_peer_offline_status(const Logger *log, const Tox *tox, MSISession *session, uint32_t friend_number) +{ + if (tox == nullptr || session == nullptr) { + return false; + } + + Tox_Err_Friend_Query f_con_query_error; + const Tox_Connection f_con_status = tox_friend_get_connection_status(tox, friend_number, &f_con_query_error); + + if (f_con_status == TOX_CONNECTION_NONE) { + /* Friend is now offline */ + LOGGER_DEBUG(log, "Friend %d is now offline", friend_number); + + pthread_mutex_lock(session->mutex); + MSICall *call = get_call(session, friend_number); + + if (call == nullptr) { + pthread_mutex_unlock(session->mutex); + return true; + } + + invoke_callback(log, call, MSI_ON_PEERTIMEOUT); /* Failure is ignored */ + kill_call(log, call); + pthread_mutex_unlock(session->mutex); + return true; + } + + return false; +} + +int msi_invite(const Logger *log, MSISession *session, MSICall **call, uint32_t friend_number, uint8_t capabilities) { + LOGGER_DEBUG(log, "msi_invite:session:%p", (void *)session); + if (session == nullptr) { return -1; } - LOGGER_DEBUG(session->messenger->log, "Session: %p Inviting friend: %u", (void *)session, friend_number); + LOGGER_DEBUG(log, "Session: %p Inviting friend: %u", (void *)session, friend_number); if (pthread_mutex_trylock(session->mutex) != 0) { - LOGGER_ERROR(session->messenger->log, "Failed to acquire lock on msi mutex"); + LOGGER_ERROR(log, "Failed to acquire lock on msi mutex"); return -1; } if (get_call(session, friend_number) != nullptr) { - LOGGER_ERROR(session->messenger->log, "Already in a call"); + LOGGER_ERROR(log, "Already in a call"); pthread_mutex_unlock(session->mutex); return -1; } @@ -197,17 +243,18 @@ int msi_invite(MSISession *session, MSICall **call, uint32_t friend_number, uint msg.capabilities.exists = true; msg.capabilities.value = capabilities; - send_message(temp->session->messenger, temp->friend_number, &msg); + send_message(log, temp->session->tox, temp->friend_number, &msg); temp->state = MSI_CALL_REQUESTING; *call = temp; - LOGGER_DEBUG(session->messenger->log, "Invite sent"); + LOGGER_DEBUG(log, "Invite sent"); pthread_mutex_unlock(session->mutex); return 0; } -int msi_hangup(MSICall *call) + +int msi_hangup(const Logger *log, MSICall *call) { if (call == nullptr || call->session == nullptr) { return -1; @@ -215,16 +262,16 @@ int msi_hangup(MSICall *call) MSISession *session = call->session; - LOGGER_DEBUG(session->messenger->log, "Session: %p Hanging up call with friend: %u", (void *)call->session, + LOGGER_DEBUG(log, "Session: %p Hanging up call with friend: %u", (void *)call->session, call->friend_number); if (pthread_mutex_trylock(session->mutex) != 0) { - LOGGER_ERROR(session->messenger->log, "Failed to acquire lock on msi mutex"); + LOGGER_ERROR(log, "Failed to acquire lock on msi mutex"); return -1; } if (call->state == MSI_CALL_INACTIVE) { - LOGGER_ERROR(session->messenger->log, "Call is in invalid state!"); + LOGGER_ERROR(log, "Call is in invalid state!"); pthread_mutex_unlock(session->mutex); return -1; } @@ -232,13 +279,14 @@ int msi_hangup(MSICall *call) MSIMessage msg; msg_init(&msg, REQU_POP); - send_message(session->messenger, call->friend_number, &msg); + send_message(log, session->tox, call->friend_number, &msg); - kill_call(call); + kill_call(log, call); pthread_mutex_unlock(session->mutex); return 0; } -int msi_answer(MSICall *call, uint8_t capabilities) + +int msi_answer(const Logger *log, MSICall *call, uint8_t capabilities) { if (call == nullptr || call->session == nullptr) { return -1; @@ -246,18 +294,18 @@ int msi_answer(MSICall *call, uint8_t capabilities) MSISession *session = call->session; - LOGGER_DEBUG(session->messenger->log, "Session: %p Answering call from: %u", (void *)call->session, + LOGGER_DEBUG(log, "Session: %p Answering call from: %u", (void *)call->session, call->friend_number); if (pthread_mutex_trylock(session->mutex) != 0) { - LOGGER_ERROR(session->messenger->log, "Failed to acquire lock on msi mutex"); + LOGGER_ERROR(log, "Failed to acquire lock on msi mutex"); return -1; } if (call->state != MSI_CALL_REQUESTED) { /* Though sending in invalid state will not cause anything weird * Its better to not do it like a maniac */ - LOGGER_ERROR(session->messenger->log, "Call is in invalid state!"); + LOGGER_ERROR(log, "Call is in invalid state!"); pthread_mutex_unlock(session->mutex); return -1; } @@ -270,14 +318,15 @@ int msi_answer(MSICall *call, uint8_t capabilities) msg.capabilities.exists = true; msg.capabilities.value = capabilities; - send_message(session->messenger, call->friend_number, &msg); + send_message(log, session->tox, call->friend_number, &msg); call->state = MSI_CALL_ACTIVE; pthread_mutex_unlock(session->mutex); return 0; } -int msi_change_capabilities(MSICall *call, uint8_t capabilities) + +int msi_change_capabilities(const Logger *log, MSICall *call, uint8_t capabilities) { if (call == nullptr || call->session == nullptr) { return -1; @@ -285,16 +334,16 @@ int msi_change_capabilities(MSICall *call, uint8_t capabilities) MSISession *session = call->session; - LOGGER_DEBUG(session->messenger->log, "Session: %p Trying to change capabilities to friend %u", (void *)call->session, + LOGGER_DEBUG(log, "Session: %p Trying to change capabilities to friend %u", (void *)call->session, call->friend_number); if (pthread_mutex_trylock(session->mutex) != 0) { - LOGGER_ERROR(session->messenger->log, "Failed to acquire lock on msi mutex"); + LOGGER_ERROR(log, "Failed to acquire lock on msi mutex"); return -1; } if (call->state != MSI_CALL_ACTIVE) { - LOGGER_ERROR(session->messenger->log, "Call is in invalid state!"); + LOGGER_ERROR(log, "Call is in invalid state!"); pthread_mutex_unlock(session->mutex); return -1; } @@ -307,7 +356,7 @@ int msi_change_capabilities(MSICall *call, uint8_t capabilities) msg.capabilities.exists = true; msg.capabilities.value = capabilities; - send_message(call->session->messenger, call->friend_number, &msg); + send_message(log, call->session->tox, call->friend_number, &msg); pthread_mutex_unlock(session->mutex); return 0; @@ -351,10 +400,51 @@ static bool check_enum_high(const Logger *log, const uint8_t *bytes, uint8_t enu return true; } +static const uint8_t *msg_parse_one(const Logger *log, MSIMessage *dest, const uint8_t *it, int *size_constraint) +{ + switch (*it) { + case ID_REQUEST: { + if (!check_size(log, it, size_constraint, 1) || + !check_enum_high(log, it, REQU_POP)) { + return nullptr; + } + + dest->request.value = (MSIRequest)it[2]; + dest->request.exists = true; + return it + 3; + } + + case ID_ERROR: { + if (!check_size(log, it, size_constraint, 1) || + !check_enum_high(log, it, MSI_E_UNDISCLOSED)) { + return nullptr; + } + + dest->error.value = (MSIError)it[2]; + dest->error.exists = true; + return it + 3; + } + + case ID_CAPABILITIES: { + if (!check_size(log, it, size_constraint, 1)) { + return nullptr; + } + + dest->capabilities.value = it[2]; + dest->capabilities.exists = true; + return it + 3; + } + + default: { + LOGGER_ERROR(log, "Invalid id byte: %d", *it); + return nullptr; + } + } +} + static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length) { /* Parse raw data received from socket into MSIMessage struct */ - assert(dest != nullptr); if (length == 0 || data[length - 1] != 0) { /* End byte must have value 0 */ @@ -368,46 +458,10 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data int size_constraint = length; while (*it != 0) {/* until end byte is hit */ - switch (*it) { - case ID_REQUEST: { - if (!check_size(log, it, &size_constraint, 1) || - !check_enum_high(log, it, REQU_POP)) { - return -1; - } - - dest->request.value = (MSIRequest)it[2]; - dest->request.exists = true; - it += 3; - break; - } - - case ID_ERROR: { - if (!check_size(log, it, &size_constraint, 1) || - !check_enum_high(log, it, MSI_E_UNDISCLOSED)) { - return -1; - } - - dest->error.value = (MSIError)it[2]; - dest->error.exists = true; - it += 3; - break; - } - - case ID_CAPABILITIES: { - if (!check_size(log, it, &size_constraint, 1)) { - return -1; - } - - dest->capabilities.value = it[2]; - dest->capabilities.exists = true; - it += 3; - break; - } + it = msg_parse_one(log, dest, it, &size_constraint); - default: { - LOGGER_ERROR(log, "Invalid id byte"); - return -1; - } + if (it == nullptr) { + return -1; } } @@ -418,6 +472,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data return 0; } + static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const uint8_t *value, uint8_t value_len, uint16_t *length) { @@ -437,11 +492,48 @@ static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const uint8_ return dest + value_len; /* Set to next position ready to be written */ } -static int send_message(const Messenger *m, uint32_t friend_number, const MSIMessage *msg) + +/* Send an msi packet. + * + * return 1 on success + * return 0 on failure + */ +static int m_msi_packet(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length) { - /* Parse and send message */ - assert(m != nullptr); + // TODO(Zoff): make this better later! ------------------- + /* we need to prepend 1 byte (packet id) to data + * do this without malloc, memcpy and free in the future + */ + const size_t length_new = (size_t)length + 1; + uint8_t *data_new = (uint8_t *)malloc(length_new); + + if (data_new == nullptr) { + return 0; + } + + data_new[0] = PACKET_ID_MSI; + + if (length != 0) { + memcpy(data_new + 1, data, length); + } + + Tox_Err_Friend_Custom_Packet error; + tox_friend_send_lossless_packet(tox, friendnumber, data_new, length_new, &error); + + free(data_new); + + if (error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { + return 1; + } + + return 0; +} + +static int send_message(const Logger *log, Tox *tox, uint32_t friend_number, const MSIMessage *msg) +{ + assert(tox != nullptr); + /* Parse and send message */ uint8_t parsed[MSI_MAXMSG_SIZE]; uint8_t *it = parsed; @@ -452,7 +544,7 @@ static int send_message(const Messenger *m, uint32_t friend_number, const MSIMes it = msg_parse_header_out(ID_REQUEST, it, &cast, sizeof(cast), &size); } else { - LOGGER_DEBUG(m->log, "Must have request field"); + LOGGER_DEBUG(log, "Must have request field"); return -1; } @@ -468,26 +560,27 @@ static int send_message(const Messenger *m, uint32_t friend_number, const MSIMes } if (it == parsed) { - LOGGER_WARNING(m->log, "Parsing message failed; empty message"); + LOGGER_WARNING(log, "Parsing message failed; empty message"); return -1; } *it = 0; ++size; - if (m_msi_packet(m, friend_number, parsed, size)) { - LOGGER_DEBUG(m->log, "Sent message"); + if (m_msi_packet(tox, friend_number, parsed, size) == 1) { + LOGGER_DEBUG(log, "Sent message"); return 0; } return -1; } -static int send_error(const Messenger *m, uint32_t friend_number, MSIError error) + +static int send_error(const Logger *log, Tox *tox, uint32_t friend_number, MSIError error) { - /* Send error message */ - assert(m != nullptr); + assert(tox != nullptr); - LOGGER_DEBUG(m->log, "Sending error: %d to friend: %d", error, friend_number); + /* Send error message */ + LOGGER_DEBUG(log, "Sending error: %d to friend: %d", error, friend_number); MSIMessage msg; msg_init(&msg, REQU_POP); @@ -495,13 +588,14 @@ static int send_error(const Messenger *m, uint32_t friend_number, MSIError error msg.error.exists = true; msg.error.value = error; - send_message(m, friend_number, &msg); + send_message(log, tox, friend_number, &msg); return 0; } -static int invoke_callback_inner(MSICall *call, MSICallbackID id) + +static int invoke_callback_inner(const Logger *log, MSICall *call, MSICallbackID id) { MSISession *session = call->session; - LOGGER_DEBUG(session->messenger->log, "invoking callback function: %d", id); + LOGGER_DEBUG(log, "invoking callback function: %d", id); switch (id) { case MSI_ON_INVITE: @@ -523,15 +617,16 @@ static int invoke_callback_inner(MSICall *call, MSICallbackID id) return session->capabilities_callback(session->av, call); } - LOGGER_FATAL(session->messenger->log, "invalid callback id: %d", id); + LOGGER_FATAL(log, "invalid callback id: %d", id); return -1; } -static bool invoke_callback(MSICall *call, MSICallbackID cb) + +static bool invoke_callback(const Logger *log, MSICall *call, MSICallbackID cb) { assert(call != nullptr); - if (invoke_callback_inner(call, cb) != 0) { - LOGGER_WARNING(call->session->messenger->log, + if (invoke_callback_inner(log, call, cb) != 0) { + LOGGER_WARNING(log, "Callback state handling failed, sending error"); /* If no callback present or error happened while handling, @@ -546,6 +641,7 @@ static bool invoke_callback(MSICall *call, MSICallbackID cb) return true; } + static MSICall *get_call(MSISession *session, uint32_t friend_number) { assert(session != nullptr); @@ -556,6 +652,7 @@ static MSICall *get_call(MSISession *session, uint32_t friend_number) return session->calls[friend_number]; } + static MSICall *new_call(MSISession *session, uint32_t friend_number) { assert(session != nullptr); @@ -607,7 +704,8 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number) session->calls[friend_number] = rc; return rc; } -static void kill_call(MSICall *call) + +static void kill_call(const Logger *log, MSICall *call) { /* Assume that session mutex is locked */ if (call == nullptr) { @@ -616,7 +714,7 @@ static void kill_call(MSICall *call) MSISession *session = call->session; - LOGGER_DEBUG(session->messenger->log, "Killing call: %p", (void *)call); + LOGGER_DEBUG(log, "Killing call: %p", (void *)call); MSICall *prev = call->prev; MSICall *next = call->next; @@ -648,37 +746,12 @@ static void kill_call(MSICall *call) free(call); session->calls = nullptr; } -static void on_peer_status(Messenger *m, uint32_t friend_number, bool is_online, void *user_data) -{ - MSISession *session = (MSISession *)user_data; - - if (is_online) { - // Friend is online. - return; - } - - LOGGER_DEBUG(m->log, "Friend %d is now offline", friend_number); - - pthread_mutex_lock(session->mutex); - MSICall *call = get_call(session, friend_number); - if (call == nullptr) { - pthread_mutex_unlock(session->mutex); - return; - } - invoke_callback(call, MSI_ON_PEERTIMEOUT); /* Failure is ignored */ - kill_call(call); - pthread_mutex_unlock(session->mutex); -} -static bool try_handle_init(MSICall *call, const MSIMessage *msg) +static bool try_handle_init(const Logger *log, MSICall *call, const MSIMessage *msg) { - assert(call != nullptr); - LOGGER_DEBUG(call->session->messenger->log, - "Session: %p Handling 'init' friend: %d", (void *)call->session, call->friend_number); - if (!msg->capabilities.exists) { - LOGGER_WARNING(call->session->messenger->log, "Session: %p Invalid capabilities on 'init'", (void *)call->session); + LOGGER_WARNING(log, "Session: %p Invalid capabilities on 'init'", (void *)call->session); call->error = MSI_E_INVALID_MESSAGE; return false; } @@ -689,7 +762,7 @@ static bool try_handle_init(MSICall *call, const MSIMessage *msg) call->peer_capabilities = msg->capabilities.value; call->state = MSI_CALL_REQUESTED; - if (!invoke_callback(call, MSI_ON_INVITE)) { + if (!invoke_callback(log, call, MSI_ON_INVITE)) { return false; } @@ -704,7 +777,7 @@ static bool try_handle_init(MSICall *call, const MSIMessage *msg) * we can automatically answer the re-call. */ - LOGGER_INFO(call->session->messenger->log, "Friend is recalling us"); + LOGGER_INFO(log, "Friend is recalling us"); MSIMessage out_msg; msg_init(&out_msg, REQU_PUSH); @@ -712,7 +785,7 @@ static bool try_handle_init(MSICall *call, const MSIMessage *msg) out_msg.capabilities.exists = true; out_msg.capabilities.value = call->self_capabilities; - send_message(call->session->messenger, call->friend_number, &out_msg); + send_message(log, call->session->tox, call->friend_number, &out_msg); /* If peer changed capabilities during re-call they will * be handled accordingly during the next step @@ -722,7 +795,7 @@ static bool try_handle_init(MSICall *call, const MSIMessage *msg) case MSI_CALL_REQUESTED: // fall-through case MSI_CALL_REQUESTING: { - LOGGER_WARNING(call->session->messenger->log, "Session: %p Invalid state on 'init'", (void *)call->session); + LOGGER_WARNING(log, "Session: %p Invalid state on 'init'", (void *)call->session); call->error = MSI_E_INVALID_STATE; return false; } @@ -730,26 +803,28 @@ static bool try_handle_init(MSICall *call, const MSIMessage *msg) return true; } -static void handle_init(MSICall *call, const MSIMessage *msg) + +static void handle_init(const Logger *log, MSICall *call, const MSIMessage *msg) { assert(call != nullptr); - LOGGER_DEBUG(call->session->messenger->log, + LOGGER_DEBUG(log, "Session: %p Handling 'init' friend: %d", (void *)call->session, call->friend_number); - if (!try_handle_init(call, msg)) { - send_error(call->session->messenger, call->friend_number, call->error); - kill_call(call); + if (!try_handle_init(log, call, msg)) { + send_error(log, call->session->tox, call->friend_number, call->error); + kill_call(log, call); } } -static void handle_push(MSICall *call, const MSIMessage *msg) + +static void handle_push(const Logger *log, MSICall *call, const MSIMessage *msg) { assert(call != nullptr); - LOGGER_DEBUG(call->session->messenger->log, "Session: %p Handling 'push' friend: %d", (void *)call->session, + LOGGER_DEBUG(log, "Session: %p Handling 'push' friend: %d", (void *)call->session, call->friend_number); if (!msg->capabilities.exists) { - LOGGER_WARNING(call->session->messenger->log, "Session: %p Invalid capabilities on 'push'", (void *)call->session); + LOGGER_WARNING(log, "Session: %p Invalid capabilities on 'push'", (void *)call->session); call->error = MSI_E_INVALID_MESSAGE; goto FAILURE; } @@ -757,12 +832,11 @@ static void handle_push(MSICall *call, const MSIMessage *msg) switch (call->state) { case MSI_CALL_ACTIVE: { if (call->peer_capabilities != msg->capabilities.value) { - /* Only act if capabilities changed */ - LOGGER_INFO(call->session->messenger->log, "Friend is changing capabilities to: %u", msg->capabilities.value); + LOGGER_INFO(log, "Friend is changing capabilities to: %u", msg->capabilities.value); call->peer_capabilities = msg->capabilities.value; - if (!invoke_callback(call, MSI_ON_CAPABILITIES)) { + if (!invoke_callback(log, call, MSI_ON_CAPABILITIES)) { goto FAILURE; } } @@ -771,13 +845,13 @@ static void handle_push(MSICall *call, const MSIMessage *msg) } case MSI_CALL_REQUESTING: { - LOGGER_INFO(call->session->messenger->log, "Friend answered our call"); + LOGGER_INFO(log, "Friend answered our call"); /* Call started */ call->peer_capabilities = msg->capabilities.value; call->state = MSI_CALL_ACTIVE; - if (!invoke_callback(call, MSI_ON_START)) { + if (!invoke_callback(log, call, MSI_ON_START)) { goto FAILURE; } @@ -786,8 +860,7 @@ static void handle_push(MSICall *call, const MSIMessage *msg) case MSI_CALL_INACTIVE: // fall-through case MSI_CALL_REQUESTED: { - /* Pushes during initialization state are ignored */ - LOGGER_WARNING(call->session->messenger->log, "Ignoring invalid push"); + LOGGER_WARNING(log, "Ignoring invalid push"); break; } } @@ -795,76 +868,102 @@ static void handle_push(MSICall *call, const MSIMessage *msg) return; FAILURE: - send_error(call->session->messenger, call->friend_number, call->error); - kill_call(call); + send_error(log, call->session->tox, call->friend_number, call->error); + kill_call(log, call); } -static void handle_pop(MSICall *call, const MSIMessage *msg) + +static void handle_pop(const Logger *log, MSICall *call, const MSIMessage *msg) { assert(call != nullptr); - LOGGER_DEBUG(call->session->messenger->log, "Session: %p Handling 'pop', friend id: %d", (void *)call->session, + LOGGER_DEBUG(log, "Session: %p Handling 'pop', friend id: %d", (void *)call->session, call->friend_number); /* callback errors are ignored */ if (msg->error.exists) { - LOGGER_WARNING(call->session->messenger->log, "Friend detected an error: %d", msg->error.value); + LOGGER_WARNING(log, "Friend detected an error: %d", msg->error.value); call->error = msg->error.value; - invoke_callback(call, MSI_ON_ERROR); + invoke_callback(log, call, MSI_ON_ERROR); } else { switch (call->state) { case MSI_CALL_INACTIVE: { - LOGGER_FATAL(call->session->messenger->log, "Handling what should be impossible case"); + LOGGER_FATAL(log, "Handling what should be impossible case"); break; } case MSI_CALL_ACTIVE: { /* Hangup */ - LOGGER_INFO(call->session->messenger->log, "Friend hung up on us"); - invoke_callback(call, MSI_ON_END); + LOGGER_INFO(log, "Friend hung up on us"); + invoke_callback(log, call, MSI_ON_END); break; } case MSI_CALL_REQUESTING: { /* Reject */ - LOGGER_INFO(call->session->messenger->log, "Friend rejected our call"); - invoke_callback(call, MSI_ON_END); + LOGGER_INFO(log, "Friend rejected our call"); + invoke_callback(log, call, MSI_ON_END); break; } case MSI_CALL_REQUESTED: { /* Cancel */ - LOGGER_INFO(call->session->messenger->log, "Friend canceled call invite"); - invoke_callback(call, MSI_ON_END); + LOGGER_INFO(log, "Friend canceled call invite"); + invoke_callback(log, call, MSI_ON_END); break; } } } - kill_call(call); + kill_call(log, call); } -static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *user_data) + +static void handle_msi_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data) { - MSISession *session = (MSISession *)user_data; + const ToxAV *toxav = (ToxAV *)tox_get_av_object(tox); + + if (toxav == nullptr) { + return; + } + + const Logger *log = toxav_get_logger(toxav); + + if (length < 2) { + LOGGER_ERROR(log, "MSI packet is less than 2 bytes in size"); + // we need more than the ID byte for MSI messages + return; + } + + const uint16_t payload_length = (uint16_t)(length - 1); + + // Zoff: do not show the first byte, its always "PACKET_ID_MSI" + const uint8_t *data_strip_id_byte = data + 1; + + LOGGER_DEBUG(log, "Got msi message"); - LOGGER_DEBUG(m->log, "Got msi message"); + MSISession *session = tox_av_msi_get(toxav); + + if (session == nullptr) { + return; + } MSIMessage msg; - if (msg_parse_in(m->log, &msg, data, length) == -1) { - LOGGER_WARNING(m->log, "Error parsing message"); - send_error(m, friend_number, MSI_E_INVALID_MESSAGE); + if (msg_parse_in(log, &msg, data_strip_id_byte, payload_length) == -1) { + LOGGER_WARNING(log, "Error parsing message"); + send_error(log, tox, friend_number, MSI_E_INVALID_MESSAGE); return; } - LOGGER_DEBUG(m->log, "Successfully parsed message"); + LOGGER_DEBUG(log, "Successfully parsed message"); pthread_mutex_lock(session->mutex); MSICall *call = get_call(session, friend_number); if (call == nullptr) { if (msg.request.value != REQU_INIT) { - send_error(m, friend_number, MSI_E_STRAY_MESSAGE); + send_error(log, tox, friend_number, MSI_E_STRAY_MESSAGE); pthread_mutex_unlock(session->mutex); return; } @@ -872,7 +971,7 @@ static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_ call = new_call(session, friend_number); if (call == nullptr) { - send_error(m, friend_number, MSI_E_SYSTEM); + send_error(log, tox, friend_number, MSI_E_SYSTEM); pthread_mutex_unlock(session->mutex); return; } @@ -880,17 +979,17 @@ static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_ switch (msg.request.value) { case REQU_INIT: { - handle_init(call, &msg); + handle_init(log, call, &msg); break; } case REQU_PUSH: { - handle_push(call, &msg); + handle_push(log, call, &msg); break; } case REQU_POP: { - handle_pop(call, &msg); /* always kills the call */ + handle_pop(log, call, &msg); /* always kills the call */ break; } } diff --git a/toxav/msi.h b/toxav/msi.h index f034a35c9c..88d01572ff 100644 --- a/toxav/msi.h +++ b/toxav/msi.h @@ -11,7 +11,6 @@ #include "audio.h" #include "video.h" -#include "../toxcore/Messenger.h" #include "../toxcore/logger.h" /** @@ -42,22 +41,22 @@ typedef enum MSICapabilities { * Call state identifiers. */ typedef enum MSICallState { - MSI_CALL_INACTIVE, /* Default */ - MSI_CALL_ACTIVE, - MSI_CALL_REQUESTING, /* when sending call invite */ - MSI_CALL_REQUESTED, /* when getting call invite */ + MSI_CALL_INACTIVE = 0, /* Default */ + MSI_CALL_ACTIVE = 1, + MSI_CALL_REQUESTING = 2, /* when sending call invite */ + MSI_CALL_REQUESTED = 3, /* when getting call invite */ } MSICallState; /** * Callbacks ids that handle the states */ typedef enum MSICallbackID { - MSI_ON_INVITE, /* Incoming call */ - MSI_ON_START, /* Call (RTP transmission) started */ - MSI_ON_END, /* Call that was active ended */ - MSI_ON_ERROR, /* On protocol error */ - MSI_ON_PEERTIMEOUT, /* Peer timed out; stop the call */ - MSI_ON_CAPABILITIES, /* Peer requested capabilities change */ + MSI_ON_INVITE = 0, /* Incoming call */ + MSI_ON_START = 1, /* Call (RTP transmission) started */ + MSI_ON_END = 2, /* Call that was active ended */ + MSI_ON_ERROR = 3, /* On protocol error */ + MSI_ON_PEERTIMEOUT = 4, /* Peer timed out; stop the call */ + MSI_ON_CAPABILITIES = 5, /* Peer requested capabilities change */ } MSICallbackID; /** @@ -96,7 +95,7 @@ typedef struct MSISession { uint32_t calls_head; void *av; - Messenger *messenger; + Tox *tox; pthread_mutex_t mutex[1]; @@ -111,11 +110,11 @@ typedef struct MSISession { /** * Start the control session. */ -MSISession *msi_new(Messenger *m); +MSISession *msi_new(const Logger *log, Tox *tox); /** * Terminate control session. NOTE: all calls will be freed */ -int msi_kill(MSISession *session, const Logger *log); +int msi_kill(const Logger *log, Tox *tox, MSISession *session); /** * Callback setters. */ @@ -128,18 +127,20 @@ void msi_callback_capabilities(MSISession *session, msi_action_cb *callback); /** * Send invite request to friend_number. */ -int msi_invite(MSISession *session, MSICall **call, uint32_t friend_number, uint8_t capabilities); +int msi_invite(const Logger *log, MSISession *session, MSICall **call, uint32_t friend_number, uint8_t capabilities); /** * Hangup call. NOTE: `call` will be freed */ -int msi_hangup(MSICall *call); +int msi_hangup(const Logger *log, MSICall *call); /** * Answer call request. */ -int msi_answer(MSICall *call, uint8_t capabilities); +int msi_answer(const Logger *log, MSICall *call, uint8_t capabilities); /** * Change capabilities of the call. */ -int msi_change_capabilities(MSICall *call, uint8_t capabilities); +int msi_change_capabilities(const Logger *log, MSICall *call, uint8_t capabilities); + +bool check_peer_offline_status(const Logger *log, const Tox *tox, MSISession *session, uint32_t friend_number); #endif /* C_TOXCORE_TOXAV_MSI_H */ diff --git a/toxav/rtp.c b/toxav/rtp.c index 49c91a02ce..bb3fa14fe1 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -9,12 +9,17 @@ #include #include +#include + #include "bwcontroller.h" +#include "toxav_hacks.h" -#include "../toxcore/Messenger.h" #include "../toxcore/ccompat.h" #include "../toxcore/logger.h" #include "../toxcore/mono_time.h" +#include "../toxcore/net_crypto.h" +#include "../toxcore/tox_private.h" +#include "../toxcore/tox_struct.h" #include "../toxcore/util.h" /** @@ -23,30 +28,15 @@ */ #define VIDEO_KEEP_KEYFRAME_IN_BUFFER_FOR_MS 15 -/** - * return -1 on failure, 0 on success - * - */ -static int rtp_send_custom_lossy_packet(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length) -{ - Tox_Err_Friend_Custom_Packet error; - tox_friend_send_lossy_packet(tox, friendnumber, data, (size_t)length, &error); - - if (error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { - return 0; - } - - return -1; -} - // allocate_len is NOT including header! -static struct RTPMessage *new_message(const struct RTPHeader *header, size_t allocate_len, const uint8_t *data, - uint16_t data_length) +static struct RTPMessage *new_message(const Logger *log, const struct RTPHeader *header, size_t allocate_len, + const uint8_t *data, uint16_t data_length) { assert(allocate_len >= data_length); struct RTPMessage *msg = (struct RTPMessage *)calloc(1, sizeof(struct RTPMessage) + allocate_len); if (msg == nullptr) { + LOGGER_DEBUG(log, "Could not allocate RTPMessage buffer"); return nullptr; } @@ -241,7 +231,7 @@ static struct RTPMessage *process_frame(const Logger *log, struct RTPWorkBufferL } /** - * @param log A logger. + * @param log A pointer to the Logger object. * @param wkbl The list of in-progress frames, i.e. all the slots. * @param slot_id The slot we want to fill the data into. * @param is_keyframe Whether the data is part of a key frame. @@ -309,7 +299,7 @@ static bool fill_data_into_slot(const Logger *log, struct RTPWorkBufferList *wkb return slot->received_len == header->data_length_full; } -static void update_bwc_values(const Logger *log, RTPSession *session, const struct RTPMessage *msg) +static void update_bwc_values(RTPSession *session, const struct RTPMessage *msg) { if (session->first_packets_counter < DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT) { ++session->first_packets_counter; @@ -319,7 +309,7 @@ static void update_bwc_values(const Logger *log, RTPSession *session, const stru bwc_add_recv(session->bwc, data_length_full); if (received_length_full < data_length_full) { - LOGGER_DEBUG(log, "BWC: full length=%u received length=%d", data_length_full, received_length_full); + LOGGER_DEBUG(session->log, "BWC: full length=%u received length=%d", data_length_full, received_length_full); bwc_add_lost(session->bwc, data_length_full - received_length_full); } } @@ -347,22 +337,16 @@ static void update_bwc_values(const Logger *log, RTPSession *session, const stru * @retval -1 on error. * @retval 0 on success. */ -static int handle_video_packet(RTPSession *session, const struct RTPHeader *header, - const uint8_t *incoming_data, uint16_t incoming_data_length, const Logger *log) +static int handle_video_packet(const Logger *log, RTPSession *session, const struct RTPHeader *header, + const uint8_t *incoming_data, uint16_t incoming_data_length) { // Full frame length in bytes. The frame may be split into multiple packets, // but this value is the complete assembled frame size. const uint32_t full_frame_length = header->data_length_full; - // Current offset in the frame. If this is the first packet of a multipart - // frame or it's not a multipart frame, then this value is 0. - const uint32_t offset = header->offset_full; // without header - // The sender tells us whether this is a key frame. const bool is_keyframe = (header->flags & RTP_KEY_FRAME) != 0; - LOGGER_DEBUG(log, "-- handle_video_packet -- full lens=%u len=%u offset=%u is_keyframe=%s", - (unsigned)incoming_data_length, (unsigned)full_frame_length, (unsigned)offset, is_keyframe ? "K" : "."); LOGGER_DEBUG(log, "wkbl->next_free_entry:003=%d", session->work_buffer_list->next_free_entry); const bool is_multipart = full_frame_length != incoming_data_length; @@ -387,10 +371,13 @@ static int handle_video_packet(RTPSession *session, const struct RTPHeader *head // get_slot just told us it's full, so process_frame must return non-null. assert(m_new != nullptr); - LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-001a b0=%d b1=%d", (int)m_new->data[0], (int)m_new->data[1]); - update_bwc_values(log, session, m_new); + LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-001a b0=%d b1=%d", (int)m_new->data[0], + (int)m_new->data[1]); + update_bwc_values(session, m_new); // Pass ownership of m_new to the callback. - session->mcb(session->m->mono_time, session->cs, m_new); + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + assert(mt != nullptr); + session->mcb(mt, session->cs, m_new); // Now we no longer own m_new. m_new = nullptr; @@ -425,9 +412,12 @@ static int handle_video_packet(RTPSession *session, const struct RTPHeader *head struct RTPMessage *m_new = process_frame(log, session->work_buffer_list, slot_id); if (m_new != nullptr) { - LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-003a b0=%d b1=%d", (int)m_new->data[0], (int)m_new->data[1]); - update_bwc_values(log, session, m_new); - session->mcb(session->m->mono_time, session->cs, m_new); + LOGGER_DEBUG(log, "-- handle_video_packet -- CALLBACK-003a b0=%d b1=%d", (int)m_new->data[0], + (int)m_new->data[1]); + update_bwc_values(session, m_new); + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + assert(mt != nullptr); + session->mcb(mt, session->cs, m_new); m_new = nullptr; } @@ -436,80 +426,113 @@ static int handle_video_packet(RTPSession *session, const struct RTPHeader *head } /** - * @retval -1 on error. - * @retval 0 on success. + * receive custom lossypackets and process them. they can be incoming audio or video packets */ -static int handle_rtp_packet(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object) +void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - RTPSession *session = (RTPSession *)object; + ToxAV *toxav = (ToxAV *)tox_get_av_object(tox); - if (session == nullptr || length < RTP_HEADER_SIZE + 1) { - LOGGER_WARNING(m->log, "No session or invalid length of received buffer!"); - return -1; + if (toxav == nullptr) { + // LOGGER_WARNING(log, "ToxAV is NULL!"); + return; + } + + const Logger *log = toxav_get_logger(toxav); + + if (length < RTP_HEADER_SIZE + 1) { + LOGGER_WARNING(log, "Invalid length of received buffer!"); + return; + } + + ToxAVCall *call = call_get(toxav, friend_number); + + if (call == nullptr) { + LOGGER_WARNING(log, "ToxAVCall is NULL!"); + return; + } + + RTPSession *session = rtp_session_get(call, data[0]); + + if (session == nullptr) { + LOGGER_WARNING(log, "No session!"); + return; + } + + if (!session->rtp_receive_active) { + LOGGER_WARNING(log, "receiving not allowed!"); + return; } // Get the packet type. const uint8_t packet_type = data[0]; - ++data; - --length; + const uint8_t *payload = &data[1]; + // TODO(Zoff): is this ok? + const uint16_t payload_size = (uint16_t)length - 1; // Unpack the header. struct RTPHeader header; - rtp_header_unpack(data, &header); + rtp_header_unpack(payload, &header); if (header.pt != packet_type % 128) { - LOGGER_WARNING(m->log, "RTPHeader packet type and Tox protocol packet type did not agree: %d != %d", + LOGGER_WARNING(log, "RTPHeader packet type and Tox protocol packet type did not agree: %d != %d", header.pt, packet_type % 128); - return -1; + return; } if (header.pt != session->payload_type % 128) { - LOGGER_WARNING(m->log, "RTPHeader packet type does not match this session's payload type: %d != %d", + LOGGER_WARNING(log, "RTPHeader packet type does not match this session's payload type: %d != %d", header.pt, session->payload_type % 128); - return -1; + return; } if ((header.flags & RTP_LARGE_FRAME) != 0 && header.offset_full >= header.data_length_full) { - LOGGER_ERROR(m->log, "Invalid video packet: frame offset (%u) >= full frame length (%u)", + LOGGER_ERROR(log, "Invalid video packet: frame offset (%u) >= full frame length (%u)", (unsigned)header.offset_full, (unsigned)header.data_length_full); - return -1; + return; } if (header.offset_lower >= header.data_length_lower) { - LOGGER_ERROR(m->log, "Invalid old protocol video packet: frame offset (%u) >= full frame length (%u)", + LOGGER_ERROR(log, "Invalid old protocol video packet: frame offset (%u) >= full frame length (%u)", (unsigned)header.offset_lower, (unsigned)header.data_length_lower); - return -1; + return; } - LOGGER_DEBUG(m->log, "header.pt %d, video %d", (uint8_t)header.pt, RTP_TYPE_VIDEO % 128); + LOGGER_DEBUG(log, "header.pt %d, video %d", (uint8_t)header.pt, RTP_TYPE_VIDEO % 128); // The sender uses the new large-frame capable protocol and is sending a // video packet. if ((header.flags & RTP_LARGE_FRAME) != 0 && header.pt == (RTP_TYPE_VIDEO % 128)) { - return handle_video_packet(session, &header, data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE, m->log); + handle_video_packet(log, session, &header, &payload[RTP_HEADER_SIZE], payload_size - RTP_HEADER_SIZE); + return; } // everything below here is for the old 16 bit protocol ------------------ - if (header.data_length_lower == length - RTP_HEADER_SIZE) { + if (header.data_length_lower == payload_size - RTP_HEADER_SIZE) { /* The message is sent in single part */ /* Message is not late; pick up the latest parameters */ session->rsequnum = header.sequnum; session->rtimestamp = header.timestamp; - bwc_add_recv(session->bwc, length); + bwc_add_recv(session->bwc, payload_size); /* Invoke processing of active multiparted message */ if (session->mp != nullptr) { - session->mcb(session->m->mono_time, session->cs, session->mp); + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + assert(mt != nullptr); + session->mcb(mt, session->cs, session->mp); session->mp = nullptr; } /* The message came in the allowed time; */ - return session->mcb(session->m->mono_time, session->cs, new_message(&header, length - RTP_HEADER_SIZE, - data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE)); + session->mp = new_message(log, &header, payload_size - RTP_HEADER_SIZE, &payload[RTP_HEADER_SIZE], payload_size - RTP_HEADER_SIZE); + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + assert(mt != nullptr); + session->mcb(mt, session->cs, session->mp); + session->mp = nullptr; + return; } /* The message is sent in multiple parts */ @@ -527,24 +550,26 @@ static int handle_rtp_packet(Messenger *m, uint32_t friend_number, const uint8_t /* First case */ /* Make sure we have enough allocated memory */ - if (session->mp->header.data_length_lower - session->mp->len < length - RTP_HEADER_SIZE || + if (session->mp->header.data_length_lower - session->mp->len < payload_size - RTP_HEADER_SIZE || session->mp->header.data_length_lower <= header.offset_lower) { /* There happened to be some corruption on the stream; * continue wihtout this part */ - return 0; + return; } - memcpy(session->mp->data + header.offset_lower, data + RTP_HEADER_SIZE, - length - RTP_HEADER_SIZE); - session->mp->len += length - RTP_HEADER_SIZE; - bwc_add_recv(session->bwc, length); + memcpy(session->mp->data + header.offset_lower, &payload[RTP_HEADER_SIZE], + payload_size - RTP_HEADER_SIZE); + session->mp->len += payload_size - RTP_HEADER_SIZE; + bwc_add_recv(session->bwc, payload_size); if (session->mp->len == session->mp->header.data_length_lower) { /* Received a full message; now push it for the further * processing. */ - session->mcb(session->m->mono_time, session->cs, session->mp); + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + assert(mt != nullptr); + session->mcb(mt, session->cs, session->mp); session->mp = nullptr; } } else { @@ -553,17 +578,19 @@ static int handle_rtp_packet(Messenger *m, uint32_t friend_number, const uint8_t /* The received message part is from the old message; * discard it. */ - return 0; + return; } /* Push the previous message for processing */ - session->mcb(session->m->mono_time, session->cs, session->mp); + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + assert(mt != nullptr); + session->mcb(mt, session->cs, session->mp); session->mp = nullptr; goto NEW_MULTIPARTED; } } else { - /* In this case threat the message as if it was received in order + /* In this case treat the message as if it was received in order */ /* This is also a point for new multiparted messages */ NEW_MULTIPARTED: @@ -571,21 +598,21 @@ static int handle_rtp_packet(Messenger *m, uint32_t friend_number, const uint8_t /* Message is not late; pick up the latest parameters */ session->rsequnum = header.sequnum; session->rtimestamp = header.timestamp; - bwc_add_recv(session->bwc, length); + bwc_add_recv(session->bwc, payload_size); /* Store message. */ - session->mp = new_message(&header, header.data_length_lower, data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE); + session->mp = new_message(log, &header, header.data_length_lower, &payload[RTP_HEADER_SIZE], payload_size - RTP_HEADER_SIZE); if (session->mp != nullptr) { memmove(session->mp->data + header.offset_lower, session->mp->data, session->mp->len); } else { - LOGGER_WARNING(m->log, "new_message() returned a null pointer"); - return -1; + LOGGER_WARNING(log, "new_message() returned a null pointer"); + return; } } - return 0; + return; } size_t rtp_header_pack(uint8_t *const rdata, const struct RTPHeader *header) @@ -647,24 +674,31 @@ size_t rtp_header_unpack(const uint8_t *data, struct RTPHeader *header) return p - data; } -RTPSession *rtp_new(int payload_type, Messenger *m, Tox *tox, uint32_t friendnumber, +static uint32_t rtp_random_u32(void) +{ + uint32_t randnum; + // HINT: uses libsodium function + randombytes((uint8_t *)&randnum, sizeof(randnum)); + return randnum; +} + +RTPSession *rtp_new(const Logger *log, int payload_type, Tox *tox, ToxAV *toxav, uint32_t friendnumber, BWController *bwc, void *cs, rtp_m_cb *mcb) { assert(mcb != nullptr); assert(cs != nullptr); - assert(m != nullptr); RTPSession *session = (RTPSession *)calloc(1, sizeof(RTPSession)); if (session == nullptr) { - LOGGER_WARNING(m->log, "Alloc failed! Program might misbehave!"); + LOGGER_WARNING(log, "Alloc failed! Program might misbehave!"); return nullptr; } session->work_buffer_list = (struct RTPWorkBufferList *)calloc(1, sizeof(struct RTPWorkBufferList)); if (session->work_buffer_list == nullptr) { - LOGGER_ERROR(m->log, "out of memory while allocating work buffer list"); + LOGGER_ERROR(log, "out of memory while allocating work buffer list"); free(session); return nullptr; } @@ -672,11 +706,12 @@ RTPSession *rtp_new(int payload_type, Messenger *m, Tox *tox, uint32_t friendnum // First entry is free. session->work_buffer_list->next_free_entry = 0; - session->ssrc = payload_type == RTP_TYPE_VIDEO ? 0 : random_u32(m->rng); + session->ssrc = payload_type == RTP_TYPE_VIDEO ? 0 : rtp_random_u32(); // Zoff: what is this?? session->payload_type = payload_type; - session->m = m; session->tox = tox; + session->toxav = toxav; session->friend_number = friendnumber; + session->rtp_receive_active = true; // set NULL just in case session->mp = nullptr; @@ -687,26 +722,18 @@ RTPSession *rtp_new(int payload_type, Messenger *m, Tox *tox, uint32_t friendnum session->cs = cs; session->mcb = mcb; - if (-1 == rtp_allow_receiving(session)) { - LOGGER_WARNING(m->log, "Failed to start rtp receiving mode"); - free(session->work_buffer_list); - free(session); - return nullptr; - } - return session; } -void rtp_kill(RTPSession *session) +void rtp_kill(const Logger *log, RTPSession *session) { if (session == nullptr) { + LOGGER_WARNING(log, "No session"); return; } - LOGGER_DEBUG(session->m->log, "Terminated RTP session: %p", (void *)session); - rtp_stop_receiving(session); - - LOGGER_DEBUG(session->m->log, "Terminated RTP session V3 work_buffer_list->next_free_entry: %d", + LOGGER_DEBUG(log, "Terminated RTP session: %p", (void *)session); + LOGGER_DEBUG(log, "Terminated RTP session V3 work_buffer_list->next_free_entry: %d", (int)session->work_buffer_list->next_free_entry); for (int8_t i = 0; i < session->work_buffer_list->next_free_entry; ++i) { @@ -716,93 +743,107 @@ void rtp_kill(RTPSession *session) free(session); } -int rtp_allow_receiving(RTPSession *session) +void rtp_allow_receiving_mark(RTPSession *session) { - if (session == nullptr) { - return -1; + if (session != nullptr) { + session->rtp_receive_active = true; } +} - if (m_callback_rtp_packet(session->m, session->friend_number, session->payload_type, - handle_rtp_packet, session) == -1) { - LOGGER_WARNING(session->m->log, "Failed to register rtp receive handler"); - return -1; +void rtp_stop_receiving_mark(RTPSession *session) +{ + if (session != nullptr) { + session->rtp_receive_active = false; } +} - LOGGER_DEBUG(session->m->log, "Started receiving on session: %p", (void *)session); - return 0; +void rtp_allow_receiving(Tox *tox) +{ + // register callback + tox_callback_friend_lossy_packet_per_pktid(tox, handle_rtp_packet, RTP_TYPE_AUDIO); + tox_callback_friend_lossy_packet_per_pktid(tox, handle_rtp_packet, RTP_TYPE_VIDEO); } -int rtp_stop_receiving(RTPSession *session) +void rtp_stop_receiving(Tox *tox) { - if (session == nullptr) { - return -1; - } + // UN-register callback + tox_callback_friend_lossy_packet_per_pktid(tox, nullptr, RTP_TYPE_AUDIO); + tox_callback_friend_lossy_packet_per_pktid(tox, nullptr, RTP_TYPE_VIDEO); +} + +static void rtp_send_piece(const Logger *log, Tox *tox, uint32_t friend_number, const struct RTPHeader *header, + const uint8_t *data, uint8_t *rdata, uint16_t length) +{ + rtp_header_pack(rdata + 1, header); + memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length); - m_callback_rtp_packet(session->m, session->friend_number, session->payload_type, nullptr, nullptr); + Tox_Err_Friend_Custom_Packet error; + tox_friend_send_lossy_packet(tox, friend_number, + rdata, length + RTP_HEADER_SIZE + 1, &error); - LOGGER_DEBUG(session->m->log, "Stopped receiving on session: %p", (void *)session); - return 0; + if (error != TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { + char *netstrerror = net_new_strerror(net_error()); + LOGGER_WARNING(log, "RTP send failed (len: %d)! tox error: %d, net error: %s", + length + RTP_HEADER_SIZE + 1, error, netstrerror); + net_kill_strerror(netstrerror); + } } -/** - * Send a frame of audio or video data, chunked in @ref RTPMessage instances. - * - * @param session The A/V session to send the data for. - * @param data A byte array of length @p length. - * @param length The number of bytes to send from @p data. - * @param is_keyframe Whether this video frame is a key frame. If it is an - * audio frame, this parameter is ignored. - */ -int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, - bool is_keyframe, const Logger *log) +static struct RTPHeader rtp_default_header(const RTPSession *session, uint32_t length, bool is_keyframe) { - if (session == nullptr) { - LOGGER_ERROR(log, "No session!"); - return -1; + uint16_t length_safe = (uint16_t)length; + + if (length > UINT16_MAX) { + length_safe = UINT16_MAX; } struct RTPHeader header = {0}; - header.ve = 2; // this is unused in toxav + if (is_keyframe) { + header.flags |= RTP_KEY_FRAME; + } - header.pe = 0; + if (session->payload_type == RTP_TYPE_VIDEO) { + header.flags |= RTP_LARGE_FRAME; + } + header.ve = 2; // this is unused in toxav + header.pe = 0; header.xe = 0; - header.cc = 0; - header.ma = 0; - header.pt = session->payload_type % 128; - header.sequnum = session->sequnum; - - header.timestamp = current_time_monotonic(session->m->mono_time); - + Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + if (mt != nullptr) { + header.timestamp = current_time_monotonic(mt); + } else { + header.timestamp = 0; + } header.ssrc = session->ssrc; - header.offset_lower = 0; - - // here the highest bits gets stripped anyway, no need to do keyframe bit magic here! - header.data_length_lower = length; - - if (session->payload_type == RTP_TYPE_VIDEO) { - header.flags = RTP_LARGE_FRAME; - } - - uint16_t length_safe = (uint16_t)length; - - if (length > UINT16_MAX) { - length_safe = UINT16_MAX; - } - header.data_length_lower = length_safe; header.data_length_full = length; // without header header.offset_lower = 0; header.offset_full = 0; - if (is_keyframe) { - header.flags |= RTP_KEY_FRAME; + return header; +} + +/** + * @brief Send a frame of audio or video data, chunked in @ref RTPMessage instances. + * + * @param session The A/V session to send the data for. + * @param data A byte array of length @p length. + * @param length The number of bytes to send from @p data. + * @param is_keyframe Whether this video frame is a key frame. If it is an + * audio frame, this parameter is ignored. + */ +int rtp_send_data(const Logger *log, RTPSession *session, const uint8_t *data, uint32_t length, + bool is_keyframe) +{ + if (session == nullptr) { + return -1; } const uint16_t rdata_size = min_u32(length + RTP_HEADER_SIZE + 1, MAX_CRYPTO_DATA_SIZE); @@ -810,20 +851,15 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, memset(rdata, 0, rdata_size); rdata[0] = session->payload_type; // packet id == payload_type + struct RTPHeader header = rtp_default_header(session, length, is_keyframe); + if (MAX_CRYPTO_DATA_SIZE > (length + RTP_HEADER_SIZE + 1)) { /* * The length is lesser than the maximum allowed length (including header) * Send the packet in single piece. */ - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length); - - if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, rdata_size)) { - char *netstrerror = net_new_strerror(net_error()); - LOGGER_WARNING(session->m->log, "RTP send failed (len: %u)! net error: %s", - rdata_size, netstrerror); - net_kill_strerror(netstrerror); - } + assert(length < UINT16_MAX); + rtp_send_piece(log, session->tox, session->friend_number, &header, data, rdata, length); } else { /* * The length is greater than the maximum allowed length (including header) @@ -833,16 +869,7 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, uint16_t piece = MAX_CRYPTO_DATA_SIZE - (RTP_HEADER_SIZE + 1); while ((length - sent) + RTP_HEADER_SIZE + 1 > MAX_CRYPTO_DATA_SIZE) { - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece); - - if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, - rdata, piece + RTP_HEADER_SIZE + 1)) { - char *netstrerror = net_new_strerror(net_error()); - LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! net error: %s", - piece + RTP_HEADER_SIZE + 1, netstrerror); - net_kill_strerror(netstrerror); - } + rtp_send_piece(log, session->tox, session->friend_number, &header, data + sent, rdata, piece); sent += piece; header.offset_lower = sent; @@ -853,16 +880,7 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, piece = length - sent; if (piece != 0) { - rtp_header_pack(rdata + 1, &header); - memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece); - - if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, - piece + RTP_HEADER_SIZE + 1)) { - char *netstrerror = net_new_strerror(net_error()); - LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! net error: %s", - piece + RTP_HEADER_SIZE + 1, netstrerror); - net_kill_strerror(netstrerror); - } + rtp_send_piece(log, session->tox, session->friend_number, &header, data + sent, rdata, piece); } } diff --git a/toxav/rtp.h b/toxav/rtp.h index 8acd48d061..1e2e2e8f0d 100644 --- a/toxav/rtp.h +++ b/toxav/rtp.h @@ -9,7 +9,6 @@ #include "bwcontroller.h" -#include "../toxcore/Messenger.h" #include "../toxcore/logger.h" #include "../toxcore/tox.h" @@ -36,6 +35,11 @@ typedef enum RTP_Type { RTP_TYPE_VIDEO = 193, } RTP_Type; +#ifndef TOXAV_DEFINED +#define TOXAV_DEFINED +typedef struct ToxAV ToxAV; +#endif /* TOXAV_DEFINED */ + /** * A bit mask (up to 64 bits) specifying features of the current frame affecting * the behaviour of the decoder. @@ -157,14 +161,19 @@ typedef struct RTPSession { struct RTPMessage *mp; /* Expected parted message */ struct RTPWorkBufferList *work_buffer_list; uint8_t first_packets_counter; /* dismiss first few lost video packets */ - Messenger *m; + const Logger *log; Tox *tox; + ToxAV *toxav; uint32_t friend_number; + bool rtp_receive_active; /* if this is set to false then incoming rtp packets will not be processed by handle_rtp_packet() */ BWController *bwc; void *cs; rtp_m_cb *mcb; } RTPSession; + +void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data); + /** * Serialise an RTPHeader to bytes to be sent over the network. * @@ -183,13 +192,16 @@ size_t rtp_header_pack(uint8_t *rdata, const struct RTPHeader *header); */ size_t rtp_header_unpack(const uint8_t *data, struct RTPHeader *header); -RTPSession *rtp_new(int payload_type, Messenger *m, Tox *tox, uint32_t friendnumber, +RTPSession *rtp_new(const Logger *log, int payload_type, Tox *tox, ToxAV *toxav, uint32_t friendnumber, BWController *bwc, void *cs, rtp_m_cb *mcb); -void rtp_kill(RTPSession *session); -int rtp_allow_receiving(RTPSession *session); -int rtp_stop_receiving(RTPSession *session); +void rtp_kill(const Logger *log, RTPSession *session); +void rtp_allow_receiving_mark(RTPSession *session); +void rtp_stop_receiving_mark(RTPSession *session); +void rtp_allow_receiving(Tox *tox); +void rtp_stop_receiving(Tox *tox); + /** - * Send a frame of audio or video data, chunked in @ref RTPMessage instances. + * @brief Send a frame of audio or video data, chunked in @ref RTPMessage instances. * * @param session The A/V session to send the data for. * @param data A byte array of length @p length. @@ -197,8 +209,8 @@ int rtp_stop_receiving(RTPSession *session); * @param is_keyframe Whether this video frame is a key frame. If it is an * audio frame, this parameter is ignored. */ -int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length, - bool is_keyframe, const Logger *log); +int rtp_send_data(const Logger *log, RTPSession *session, const uint8_t *data, uint32_t length, + bool is_keyframe); #ifdef __cplusplus } /* extern "C" */ diff --git a/toxav/toxav.c b/toxav/toxav.c index 6bae7a91d1..81583221bd 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -12,11 +12,15 @@ #include "msi.h" #include "rtp.h" +#include "toxav_hacks.h" -#include "../toxcore/Messenger.h" #include "../toxcore/ccompat.h" #include "../toxcore/logger.h" #include "../toxcore/mono_time.h" +#include "../toxcore/net_crypto.h" +#include "../toxcore/network.h" +#include "../toxcore/tox.h" +#include "../toxcore/tox_private.h" #include "../toxcore/tox_struct.h" #include "../toxcore/util.h" @@ -36,7 +40,12 @@ // iteration interval that is used when no call is active #define IDLE_ITERATION_INTERVAL_MS 200 -typedef struct ToxAVCall { +#ifndef TOXAV_CALL_DEFINED +#define TOXAV_CALL_DEFINED +typedef struct ToxAVCall ToxAVCall; +#endif /* TOXAV_CALL_DEFINED */ + +struct ToxAVCall { ToxAV *av; pthread_mutex_t mutex_audio[1]; @@ -63,7 +72,7 @@ typedef struct ToxAVCall { struct ToxAVCall *prev; struct ToxAVCall *next; -} ToxAVCall; +}; /** Decode time statistics */ typedef struct DecodeTimeStats { @@ -79,8 +88,8 @@ typedef struct DecodeTimeStats { } DecodeTimeStats; struct ToxAV { + Logger *log; Tox *tox; - Messenger *m; MSISession *msi; /* Two-way storage: first is array of calls and second is list of calls with head and tail */ @@ -111,8 +120,8 @@ struct ToxAV { /* keep track of decode times for audio and video */ DecodeTimeStats audio_stats; DecodeTimeStats video_stats; - /** ToxAV's own mono_time instance */ - Mono_Time *toxav_mono_time; + + Mono_Time *toxav_mono_time; // ToxAV's own mono_time instance }; static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, void *user_data); @@ -127,11 +136,55 @@ static bool audio_bit_rate_invalid(uint32_t bit_rate); static bool video_bit_rate_invalid(uint32_t bit_rate); static bool invoke_call_state_callback(ToxAV *av, uint32_t friend_number, uint32_t state); static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *error); -static ToxAVCall *call_get(ToxAV *av, uint32_t friend_number); static ToxAVCall *call_remove(ToxAVCall *call); static bool call_prepare_transmission(ToxAVCall *call); static void call_kill_transmission(ToxAVCall *call); +MSISession *tox_av_msi_get(const ToxAV *av) +{ + if (av == nullptr) { + return nullptr; + } + + return av->msi; +} + +ToxAVCall *call_get(ToxAV *av, uint32_t friend_number) +{ + if (av == nullptr) { + return nullptr; + } + + /* Assumes mutex locked */ + if (av->calls == nullptr || av->calls_tail < friend_number) { + return nullptr; + } + + return av->calls[friend_number]; +} + +RTPSession *rtp_session_get(ToxAVCall *call, int payload_type) +{ + if (call == nullptr) { + return nullptr; + } + + if (payload_type == RTP_TYPE_VIDEO) { + return call->video_rtp; + } else { + return call->audio_rtp; + } +} + +BWController *bwc_controller_get(const ToxAVCall *call) +{ + if (call == nullptr) { + return nullptr; + } + + return call->bwc; +} + /** * @brief initialize d with default values * @param d struct to be initialized, must not be nullptr @@ -155,33 +208,26 @@ ToxAV *toxav_new(Tox *tox, Toxav_Err_New *error) goto RETURN; } - // TODO(iphydf): Don't rely on toxcore internals. - Messenger *m; - m = tox->m; - - if (m->msi_packet != nullptr) { - rc = TOXAV_ERR_NEW_MULTIPLE; - goto RETURN; - } - av = (ToxAV *)calloc(1, sizeof(ToxAV)); if (av == nullptr) { - LOGGER_WARNING(m->log, "Allocation failed!"); rc = TOXAV_ERR_NEW_MALLOC; goto RETURN; } if (create_recursive_mutex(av->mutex) != 0) { - LOGGER_WARNING(m->log, "Mutex creation failed!"); rc = TOXAV_ERR_NEW_MALLOC; goto RETURN; } + av->log = tox->m->log; av->tox = tox; - av->m = m; + av->msi = msi_new(av->log, av->tox); + + rtp_allow_receiving(av->tox); + bwc_allow_receiving(av->tox); + av->toxav_mono_time = mono_time_new(tox->sys.mem, nullptr, nullptr); - av->msi = msi_new(av->m); if (av->msi == nullptr) { pthread_mutex_destroy(av->mutex); @@ -193,6 +239,9 @@ ToxAV *toxav_new(Tox *tox, Toxav_Err_New *error) init_decode_time_stats(&av->video_stats); av->msi->av = av; + // save Tox object into toxcore + tox_set_av_object(av->tox, av); + msi_callback_invite(av->msi, callback_invite); msi_callback_start(av->msi, callback_start); msi_callback_end(av->msi, callback_end); @@ -207,12 +256,15 @@ ToxAV *toxav_new(Tox *tox, Toxav_Err_New *error) } if (rc != TOXAV_ERR_NEW_OK) { - free(av); - av = nullptr; + if (av != nullptr) { + free(av); + av = nullptr; + } } return av; } + void toxav_kill(ToxAV *av) { if (av == nullptr) { @@ -221,8 +273,16 @@ void toxav_kill(ToxAV *av) pthread_mutex_lock(av->mutex); + // unregister callbacks + for (uint8_t i = PACKET_ID_RANGE_LOSSY_AV_START; i <= PACKET_ID_RANGE_LOSSY_AV_END; ++i) { + tox_callback_friend_lossy_packet_per_pktid(av->tox, nullptr, i); + } + + rtp_stop_receiving(av->tox); + bwc_stop_receiving(av->tox); + /* To avoid possible deadlocks */ - while (av->msi != nullptr && msi_kill(av->msi, av->m->log) != 0) { + while (av->msi != nullptr && msi_kill(av->log, av->tox, av->msi) != 0) { pthread_mutex_unlock(av->mutex); pthread_mutex_lock(av->mutex); } @@ -243,13 +303,22 @@ void toxav_kill(ToxAV *av) pthread_mutex_unlock(av->mutex); pthread_mutex_destroy(av->mutex); + // set ToxAV object to NULL in toxcore, to signal ToxAV has been shutdown + tox_set_av_object(av->tox, nullptr); + free(av); } + Tox *toxav_get_tox(const ToxAV *av) { return av->tox; } +const Logger *toxav_get_logger(const ToxAV *av) +{ + return av->log; +} + uint32_t toxav_audio_iteration_interval(const ToxAV *av) { return av->calls != nullptr ? av->audio_stats.interval : IDLE_ITERATION_INTERVAL_MS; @@ -276,10 +345,11 @@ uint32_t toxav_iteration_interval(const ToxAV *av) static void calc_interval(ToxAV *av, DecodeTimeStats *stats, int32_t frame_time, uint64_t start_time) { stats->interval = frame_time < stats->average ? 0 : (frame_time - stats->average); - stats->total += current_time_monotonic(av->m->mono_time) - start_time; + stats->total += current_time_monotonic(av->toxav_mono_time) - start_time; if (++stats->count == 3) { - stats->average = stats->total / 3 + 5; /* NOTE: Magic Offset for precision */ + /* NOTE: Magic Offset for precision */ + stats->average = stats->total / 3 + 5; stats->count = 0; stats->total = 0; } @@ -300,7 +370,6 @@ static void iterate_common(ToxAV *av, bool audio) } const uint64_t start = current_time_monotonic(av->toxav_mono_time); - // time until the first audio or video frame is over int32_t frame_time = IDLE_ITERATION_INTERVAL_MS; for (ToxAVCall *i = av->calls[av->calls_head]; i != nullptr; i = i->next) { @@ -311,6 +380,15 @@ static void iterate_common(ToxAV *av, bool audio) pthread_mutex_lock(i->toxav_call_mutex); pthread_mutex_unlock(av->mutex); + const uint32_t fid = i->friend_number; + const bool is_offline = check_peer_offline_status(av->log, av->tox, i->msi_call->session, fid); + + if (is_offline) { + pthread_mutex_unlock(i->toxav_call_mutex); + pthread_mutex_lock(av->mutex); + break; + } + if (audio) { ac_iterate(i->audio); @@ -329,8 +407,6 @@ static void iterate_common(ToxAV *av, bool audio) } } - const uint32_t fid = i->friend_number; - pthread_mutex_unlock(i->toxav_call_mutex); pthread_mutex_lock(av->mutex); @@ -342,8 +418,10 @@ static void iterate_common(ToxAV *av, bool audio) DecodeTimeStats *stats = audio ? &av->audio_stats : &av->video_stats; calc_interval(av, stats, frame_time, start); + pthread_mutex_unlock(av->mutex); } + void toxav_audio_iterate(ToxAV *av) { iterate_common(av, true); @@ -388,7 +466,7 @@ bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint call->previous_self_capabilities |= audio_bit_rate > 0 ? MSI_CAP_S_AUDIO : 0; call->previous_self_capabilities |= video_bit_rate > 0 ? MSI_CAP_S_VIDEO : 0; - if (msi_invite(av->msi, &call->msi_call, friend_number, call->previous_self_capabilities) != 0) { + if (msi_invite(av->log, av->msi, &call->msi_call, friend_number, call->previous_self_capabilities) != 0) { call_remove(call); rc = TOXAV_ERR_CALL_SYNC; goto RETURN; @@ -405,6 +483,7 @@ bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint return rc == TOXAV_ERR_CALL_OK; } + void toxav_callback_call(ToxAV *av, toxav_call_cb *callback, void *user_data) { pthread_mutex_lock(av->mutex); @@ -412,6 +491,7 @@ void toxav_callback_call(ToxAV *av, toxav_call_cb *callback, void *user_data) av->ccb_user_data = user_data; pthread_mutex_unlock(av->mutex); } + bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, Toxav_Err_Answer *error) { @@ -420,7 +500,7 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui Toxav_Err_Answer rc = TOXAV_ERR_ANSWER_OK; ToxAVCall *call; - if (!m_friend_exists(av->m, friend_number)) { + if (!tox_friend_exists(av->tox, friend_number)) { rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND; goto RETURN; } @@ -452,7 +532,7 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui call->previous_self_capabilities |= audio_bit_rate > 0 ? MSI_CAP_S_AUDIO : 0; call->previous_self_capabilities |= video_bit_rate > 0 ? MSI_CAP_S_VIDEO : 0; - if (msi_answer(call->msi_call, call->previous_self_capabilities) != 0) { + if (msi_answer(av->log, call->msi_call, call->previous_self_capabilities) != 0) { rc = TOXAV_ERR_ANSWER_SYNC; } @@ -465,6 +545,7 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui return rc == TOXAV_ERR_ANSWER_OK; } + void toxav_callback_call_state(ToxAV *av, toxav_call_state_cb *callback, void *user_data) { pthread_mutex_lock(av->mutex); @@ -472,6 +553,7 @@ void toxav_callback_call_state(ToxAV *av, toxav_call_state_cb *callback, void *u av->scb_user_data = user_data; pthread_mutex_unlock(av->mutex); } + static Toxav_Err_Call_Control call_control_handle_resume(const ToxAVCall *call) { /* Only act if paused and had media transfer active before */ @@ -479,12 +561,13 @@ static Toxav_Err_Call_Control call_control_handle_resume(const ToxAVCall *call) return TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION; } - if (msi_change_capabilities(call->msi_call, call->previous_self_capabilities) == -1) { + if (msi_change_capabilities(call->av->log, call->msi_call, + call->previous_self_capabilities) == -1) { return TOXAV_ERR_CALL_CONTROL_SYNC; } - rtp_allow_receiving(call->audio_rtp); - rtp_allow_receiving(call->video_rtp); + rtp_allow_receiving_mark(call->audio_rtp); + rtp_allow_receiving_mark(call->video_rtp); return TOXAV_ERR_CALL_CONTROL_OK; } @@ -497,12 +580,12 @@ static Toxav_Err_Call_Control call_control_handle_pause(ToxAVCall *call) call->previous_self_capabilities = call->msi_call->self_capabilities; - if (msi_change_capabilities(call->msi_call, 0) == -1) { + if (msi_change_capabilities(call->av->log, call->msi_call, 0) == -1) { return TOXAV_ERR_CALL_CONTROL_SYNC; } - rtp_stop_receiving(call->audio_rtp); - rtp_stop_receiving(call->video_rtp); + rtp_stop_receiving_mark(call->audio_rtp); + rtp_stop_receiving_mark(call->video_rtp); return TOXAV_ERR_CALL_CONTROL_OK; } @@ -511,7 +594,7 @@ static Toxav_Err_Call_Control call_control_handle_cancel(ToxAVCall *call) /* Hang up */ pthread_mutex_lock(call->toxav_call_mutex); - if (msi_hangup(call->msi_call) != 0) { + if (msi_hangup(call->av->log, call->msi_call) != 0) { pthread_mutex_unlock(call->toxav_call_mutex); return TOXAV_ERR_CALL_CONTROL_SYNC; } @@ -531,13 +614,13 @@ static Toxav_Err_Call_Control call_control_handle_mute_audio(const ToxAVCall *ca return TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION; } - if (msi_change_capabilities(call->msi_call, call-> + if (msi_change_capabilities(call->av->log, call->msi_call, call-> msi_call->self_capabilities ^ MSI_CAP_R_AUDIO) == -1) { return TOXAV_ERR_CALL_CONTROL_SYNC; } - rtp_stop_receiving(call->audio_rtp); + rtp_stop_receiving_mark(call->audio_rtp); return TOXAV_ERR_CALL_CONTROL_OK; } static Toxav_Err_Call_Control call_control_handle_unmute_audio(const ToxAVCall *call) @@ -546,12 +629,12 @@ static Toxav_Err_Call_Control call_control_handle_unmute_audio(const ToxAVCall * return TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION; } - if (msi_change_capabilities(call->msi_call, call-> + if (msi_change_capabilities(call->av->log, call->msi_call, call-> msi_call->self_capabilities | MSI_CAP_R_AUDIO) == -1) { return TOXAV_ERR_CALL_CONTROL_SYNC; } - rtp_allow_receiving(call->audio_rtp); + rtp_allow_receiving_mark(call->audio_rtp); return TOXAV_ERR_CALL_CONTROL_OK; } static Toxav_Err_Call_Control call_control_handle_hide_video(const ToxAVCall *call) @@ -560,12 +643,12 @@ static Toxav_Err_Call_Control call_control_handle_hide_video(const ToxAVCall *ca return TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION; } - if (msi_change_capabilities(call->msi_call, call-> + if (msi_change_capabilities(call->av->log, call->msi_call, call-> msi_call->self_capabilities ^ MSI_CAP_R_VIDEO) == -1) { return TOXAV_ERR_CALL_CONTROL_SYNC; } - rtp_stop_receiving(call->video_rtp); + rtp_stop_receiving_mark(call->video_rtp); return TOXAV_ERR_CALL_CONTROL_OK; } static Toxav_Err_Call_Control call_control_handle_show_video(const ToxAVCall *call) @@ -574,12 +657,12 @@ static Toxav_Err_Call_Control call_control_handle_show_video(const ToxAVCall *ca return TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION; } - if (msi_change_capabilities(call->msi_call, call-> + if (msi_change_capabilities(call->av->log, call->msi_call, call-> msi_call->self_capabilities | MSI_CAP_R_VIDEO) == -1) { return TOXAV_ERR_CALL_CONTROL_SYNC; } - rtp_allow_receiving(call->video_rtp); + rtp_allow_receiving_mark(call->video_rtp); return TOXAV_ERR_CALL_CONTROL_OK; } static Toxav_Err_Call_Control call_control_handle(ToxAVCall *call, Toxav_Call_Control control) @@ -611,7 +694,7 @@ static Toxav_Err_Call_Control call_control_handle(ToxAVCall *call, Toxav_Call_Co } static Toxav_Err_Call_Control call_control(ToxAV *av, uint32_t friend_number, Toxav_Call_Control control) { - if (!m_friend_exists(av->m, friend_number)) { + if (!tox_friend_exists(av->tox, friend_number)) { return TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND; } @@ -637,13 +720,14 @@ bool toxav_call_control(ToxAV *av, uint32_t friend_number, Toxav_Call_Control co return rc == TOXAV_ERR_CALL_CONTROL_OK; } + bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_rate, Toxav_Err_Bit_Rate_Set *error) { Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK; ToxAVCall *call; - if (!m_friend_exists(av->m, friend_number)) { + if (!tox_friend_exists(av->tox, friend_number)) { rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND; goto RETURN; } @@ -662,14 +746,14 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra goto RETURN; } - LOGGER_DEBUG(av->m->log, "Setting new audio bitrate to: %d", bit_rate); + LOGGER_DEBUG(av->log, "Setting new audio bitrate to: %d", bit_rate); if (call->audio_bit_rate == bit_rate) { - LOGGER_DEBUG(av->m->log, "Audio bitrate already set to: %d", bit_rate); + LOGGER_DEBUG(av->log, "Audio bitrate already set to: %d", bit_rate); } else if (bit_rate == 0) { - LOGGER_DEBUG(av->m->log, "Turned off audio sending"); + LOGGER_DEBUG(av->log, "Turned off audio sending"); - if (msi_change_capabilities(call->msi_call, call->msi_call-> + if (msi_change_capabilities(av->log, call->msi_call, call->msi_call-> self_capabilities ^ MSI_CAP_S_AUDIO) != 0) { pthread_mutex_unlock(av->mutex); rc = TOXAV_ERR_BIT_RATE_SET_SYNC; @@ -682,10 +766,10 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra pthread_mutex_lock(call->toxav_call_mutex); if (call->audio_bit_rate == 0) { - LOGGER_DEBUG(av->m->log, "Turned on audio sending"); + LOGGER_DEBUG(av->log, "Turned on audio sending"); /* The audio has been turned off before this */ - if (msi_change_capabilities(call->msi_call, call-> + if (msi_change_capabilities(av->log, call->msi_call, call-> msi_call->self_capabilities | MSI_CAP_S_AUDIO) != 0) { pthread_mutex_unlock(call->toxav_call_mutex); pthread_mutex_unlock(av->mutex); @@ -693,7 +777,7 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra goto RETURN; } } else { - LOGGER_DEBUG(av->m->log, "Set new audio bit rate %d", bit_rate); + LOGGER_DEBUG(av->log, "Set new audio bit rate %d", bit_rate); } call->audio_bit_rate = bit_rate; @@ -709,13 +793,14 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra return rc == TOXAV_ERR_BIT_RATE_SET_OK; } + bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_rate, Toxav_Err_Bit_Rate_Set *error) { Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK; ToxAVCall *call; - if (!m_friend_exists(av->m, friend_number)) { + if (!tox_friend_exists(av->tox, friend_number)) { rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND; goto RETURN; } @@ -734,15 +819,15 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra goto RETURN; } - LOGGER_DEBUG(av->m->log, "Setting new video bitrate to: %d", bit_rate); + LOGGER_DEBUG(av->log, "Setting new video bitrate to: %d", bit_rate); if (call->video_bit_rate == bit_rate) { - LOGGER_DEBUG(av->m->log, "Video bitrate already set to: %d", bit_rate); + LOGGER_DEBUG(av->log, "Video bitrate already set to: %d", bit_rate); } else if (bit_rate == 0) { - LOGGER_DEBUG(av->m->log, "Turned off video sending"); + LOGGER_DEBUG(av->log, "Turned off video sending"); /* Video sending is turned off; notify peer */ - if (msi_change_capabilities(call->msi_call, call->msi_call-> + if (msi_change_capabilities(av->log, call->msi_call, call->msi_call-> self_capabilities ^ MSI_CAP_S_VIDEO) != 0) { pthread_mutex_unlock(av->mutex); rc = TOXAV_ERR_BIT_RATE_SET_SYNC; @@ -754,10 +839,10 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra pthread_mutex_lock(call->toxav_call_mutex); if (call->video_bit_rate == 0) { - LOGGER_DEBUG(av->m->log, "Turned on video sending"); + LOGGER_DEBUG(av->log, "Turned on video sending"); /* The video has been turned off before this */ - if (msi_change_capabilities(call->msi_call, call-> + if (msi_change_capabilities(av->log, call->msi_call, call-> msi_call->self_capabilities | MSI_CAP_S_VIDEO) != 0) { pthread_mutex_unlock(call->toxav_call_mutex); pthread_mutex_unlock(av->mutex); @@ -765,7 +850,7 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra goto RETURN; } } else { - LOGGER_DEBUG(av->m->log, "Set new video bit rate %d", bit_rate); + LOGGER_DEBUG(av->log, "Set new video bit rate %d", bit_rate); } call->video_bit_rate = bit_rate; @@ -781,6 +866,7 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra return rc == TOXAV_ERR_BIT_RATE_SET_OK; } + void toxav_callback_audio_bit_rate(ToxAV *av, toxav_audio_bit_rate_cb *callback, void *user_data) { pthread_mutex_lock(av->mutex); @@ -788,6 +874,7 @@ void toxav_callback_audio_bit_rate(ToxAV *av, toxav_audio_bit_rate_cb *callback, av->abcb_user_data = user_data; pthread_mutex_unlock(av->mutex); } + void toxav_callback_video_bit_rate(ToxAV *av, toxav_video_bit_rate_cb *callback, void *user_data) { pthread_mutex_lock(av->mutex); @@ -795,13 +882,14 @@ void toxav_callback_video_bit_rate(ToxAV *av, toxav_video_bit_rate_cb *callback, av->vbcb_user_data = user_data; pthread_mutex_unlock(av->mutex); } + bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate, Toxav_Err_Send_Frame *error) { Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK; ToxAVCall *call; - if (!m_friend_exists(av->m, friend_number)) { + if (!tox_friend_exists(av->tox, friend_number)) { rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND; goto RETURN; } @@ -859,14 +947,14 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc dest + sizeof(sampling_rate), dest_size - sizeof(sampling_rate)); if (vrc < 0) { - LOGGER_WARNING(av->m->log, "Failed to encode frame %s", opus_strerror(vrc)); + LOGGER_WARNING(av->log, "Failed to encode frame %s", opus_strerror(vrc)); pthread_mutex_unlock(call->mutex_audio); rc = TOXAV_ERR_SEND_FRAME_INVALID; goto RETURN; } - if (rtp_send_data(call->audio_rtp, dest, vrc + sizeof(sampling_rate), false, av->m->log) != 0) { - LOGGER_WARNING(av->m->log, "Failed to send audio packet"); + if (rtp_send_data(av->log, call->audio_rtp, dest, vrc + sizeof(sampling_rate), false) != 0) { + LOGGER_WARNING(av->log, "Failed to send audio packet"); rc = TOXAV_ERR_SEND_FRAME_RTP_FAILED; } } @@ -882,7 +970,7 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc return rc == TOXAV_ERR_SEND_FRAME_OK; } -static Toxav_Err_Send_Frame send_frames(const Logger *log, ToxAVCall *call) +static Toxav_Err_Send_Frame send_frames(const ToxAV *av, ToxAVCall *call) { vpx_codec_iter_t iter = nullptr; @@ -900,20 +988,15 @@ static Toxav_Err_Send_Frame send_frames(const Logger *log, ToxAVCall *call) const uint32_t frame_length_in_bytes = pkt->data.frame.sz; const int res = rtp_send_data( + av->log, call->video_rtp, (const uint8_t *)pkt->data.frame.buf, frame_length_in_bytes, - is_keyframe, - log); - - LOGGER_DEBUG(log, "+ _sending_FRAME_TYPE_==%s bytes=%d frame_len=%d", is_keyframe ? "K" : ".", - (int)pkt->data.frame.sz, (int)frame_length_in_bytes); - const uint8_t *const buf = (const uint8_t *)pkt->data.frame.buf; - LOGGER_DEBUG(log, "+ _sending_FRAME_ b0=%d b1=%d", buf[0], buf[1]); + is_keyframe); if (res < 0) { char *netstrerror = net_new_strerror(net_error()); - LOGGER_WARNING(log, "Could not send video frame: %s", netstrerror); + LOGGER_WARNING(av->log, "Could not send video frame: %s", netstrerror); net_kill_strerror(netstrerror); return TOXAV_ERR_SEND_FRAME_RTP_FAILED; } @@ -930,7 +1013,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u int vpx_encode_flags = 0; - if (!m_friend_exists(av->m, friend_number)) { + if (!tox_friend_exists(av->tox, friend_number)) { rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND; goto RETURN; } @@ -974,13 +1057,13 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u if (call->video_rtp->ssrc < VIDEO_SEND_X_KEYFRAMES_FIRST) { // Key frame flag for first frames vpx_encode_flags = VPX_EFLAG_FORCE_KF; - LOGGER_DEBUG(av->m->log, "I_FRAME_FLAG:%d only-i-frame mode", call->video_rtp->ssrc); + LOGGER_DEBUG(av->log, "I_FRAME_FLAG:%d only-i-frame mode", call->video_rtp->ssrc); ++call->video_rtp->ssrc; } else if (call->video_rtp->ssrc == VIDEO_SEND_X_KEYFRAMES_FIRST) { // normal keyframe placement vpx_encode_flags = 0; - LOGGER_DEBUG(av->m->log, "I_FRAME_FLAG:%d normal mode", call->video_rtp->ssrc); + LOGGER_DEBUG(av->log, "I_FRAME_FLAG:%d normal mode", call->video_rtp->ssrc); ++call->video_rtp->ssrc; } @@ -1009,7 +1092,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u if (vrc != VPX_CODEC_OK) { pthread_mutex_unlock(call->mutex_video); - LOGGER_ERROR(av->m->log, "Could not encode video frame: %s", vpx_codec_err_to_string(vrc)); + LOGGER_ERROR(av->log, "Could not encode video frame: %s", vpx_codec_err_to_string(vrc)); rc = TOXAV_ERR_SEND_FRAME_INVALID; goto RETURN; } @@ -1017,7 +1100,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u ++call->video->frame_counter; - rc = send_frames(av->m->log, call); + rc = send_frames(av, call); pthread_mutex_unlock(call->mutex_video); @@ -1063,7 +1146,7 @@ static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, ToxAVCall *call = (ToxAVCall *)user_data; assert(call != nullptr); - LOGGER_DEBUG(call->av->m->log, "Reported loss of %f%%", (double)loss * 100); + LOGGER_DEBUG(call->av->log, "Reported loss of %f%%", (double)loss * 100); /* if less than 10% data loss we do nothing! */ if (loss < 0.1F) { @@ -1075,7 +1158,7 @@ static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, if (call->video_bit_rate != 0) { if (call->av->vbcb == nullptr) { pthread_mutex_unlock(call->av->mutex); - LOGGER_WARNING(call->av->m->log, "No callback to report loss on"); + LOGGER_WARNING(call->av->log, "No callback to report loss on"); return; } @@ -1085,7 +1168,7 @@ static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, } else if (call->audio_bit_rate != 0) { if (call->av->abcb == nullptr) { pthread_mutex_unlock(call->av->mutex); - LOGGER_WARNING(call->av->m->log, "No callback to report loss on"); + LOGGER_WARNING(call->av->log, "No callback to report loss on"); return; } @@ -1096,6 +1179,7 @@ static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, pthread_mutex_unlock(call->av->mutex); } + static int callback_invite(void *object, MSICall *call) { ToxAV *toxav = (ToxAV *)object; @@ -1104,7 +1188,7 @@ static int callback_invite(void *object, MSICall *call) ToxAVCall *av_call = call_new(toxav, call->friend_number, nullptr); if (av_call == nullptr) { - LOGGER_WARNING(toxav->m->log, "Failed to initialize call..."); + LOGGER_WARNING(toxav->log, "Failed to initialize call..."); pthread_mutex_unlock(toxav->mutex); return -1; } @@ -1124,6 +1208,7 @@ static int callback_invite(void *object, MSICall *call) pthread_mutex_unlock(toxav->mutex); return 0; } + static int callback_start(void *object, MSICall *call) { ToxAV *toxav = (ToxAV *)object; @@ -1152,6 +1237,7 @@ static int callback_start(void *object, MSICall *call) pthread_mutex_unlock(toxav->mutex); return 0; } + static int callback_end(void *object, MSICall *call) { ToxAV *toxav = (ToxAV *)object; @@ -1167,6 +1253,7 @@ static int callback_end(void *object, MSICall *call) pthread_mutex_unlock(toxav->mutex); return 0; } + static int callback_error(void *object, MSICall *call) { ToxAV *toxav = (ToxAV *)object; @@ -1182,21 +1269,22 @@ static int callback_error(void *object, MSICall *call) pthread_mutex_unlock(toxav->mutex); return 0; } + static int callback_capabilites(void *object, MSICall *call) { ToxAV *toxav = (ToxAV *)object; pthread_mutex_lock(toxav->mutex); if ((call->peer_capabilities & MSI_CAP_S_AUDIO) != 0) { - rtp_allow_receiving(call->av_call->audio_rtp); + rtp_allow_receiving_mark(call->av_call->audio_rtp); } else { - rtp_stop_receiving(call->av_call->audio_rtp); + rtp_stop_receiving_mark(call->av_call->audio_rtp); } if ((call->peer_capabilities & MSI_CAP_S_VIDEO) != 0) { - rtp_allow_receiving(call->av_call->video_rtp); + rtp_allow_receiving_mark(call->av_call->video_rtp); } else { - rtp_stop_receiving(call->av_call->video_rtp); + rtp_stop_receiving_mark(call->av_call->video_rtp); } invoke_call_state_callback(toxav, call->friend_number, call->peer_capabilities); @@ -1204,6 +1292,7 @@ static int callback_capabilites(void *object, MSICall *call) pthread_mutex_unlock(toxav->mutex); return 0; } + static bool audio_bit_rate_invalid(uint32_t bit_rate) { /* Opus RFC 6716 section-2.1.1 dictates the following: @@ -1211,6 +1300,7 @@ static bool audio_bit_rate_invalid(uint32_t bit_rate) */ return bit_rate < 6 || bit_rate > 510; } + static bool video_bit_rate_invalid(uint32_t bit_rate) { /* https://www.webmproject.org/docs/webm-sdk/structvpx__codec__enc__cfg.html shows the following: @@ -1222,6 +1312,7 @@ static bool video_bit_rate_invalid(uint32_t bit_rate) */ return bit_rate > UINT32_MAX; } + static bool invoke_call_state_callback(ToxAV *av, uint32_t friend_number, uint32_t state) { if (av->scb != nullptr) { @@ -1239,12 +1330,17 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er Toxav_Err_Call rc = TOXAV_ERR_CALL_OK; ToxAVCall *call = nullptr; - if (!m_friend_exists(av->m, friend_number)) { + Tox_Err_Friend_Query f_con_query_error; + Tox_Connection f_con_status = TOX_CONNECTION_NONE; + + if (!tox_friend_exists(av->tox, friend_number)) { rc = TOXAV_ERR_CALL_FRIEND_NOT_FOUND; goto RETURN; } - if (m_get_friend_connectionstatus(av->m, friend_number) < 1) { + f_con_status = tox_friend_get_connection_status(av->tox, friend_number, &f_con_query_error); + + if (f_con_status == TOX_CONNECTION_NONE) { rc = TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED; goto RETURN; } @@ -1323,16 +1419,6 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er return call; } -static ToxAVCall *call_get(ToxAV *av, uint32_t friend_number) -{ - /* Assumes mutex locked */ - if (av->calls == nullptr || av->calls_tail < friend_number) { - return nullptr; - } - - return av->calls[friend_number]; -} - static ToxAVCall *call_remove(ToxAVCall *call) { if (call == nullptr) { @@ -1399,7 +1485,7 @@ static bool call_prepare_transmission(ToxAVCall *call) } if (call->active) { - LOGGER_WARNING(av->m->log, "Call already active!"); + LOGGER_WARNING(av->log, "Call already active!"); return true; } @@ -1412,43 +1498,37 @@ static bool call_prepare_transmission(ToxAVCall *call) } /* Prepare bwc */ - call->bwc = bwc_new(av->m, av->tox, call->friend_number, callback_bwc, call, av->toxav_mono_time); - - if (call->bwc == nullptr) { - LOGGER_ERROR(av->m->log, "Failed to create new bwc"); - goto FAILURE; - } + call->bwc = bwc_new(av->log, av->tox, call->friend_number, callback_bwc, call, av->toxav_mono_time); - { /* Prepare audio */ - call->audio = ac_new(av->toxav_mono_time, av->m->log, av, call->friend_number, av->acb, av->acb_user_data); + { /* Prepare audio */ + call->audio = ac_new(av->toxav_mono_time, av->log, av->tox, av, call->friend_number, av->acb, av->acb_user_data); if (call->audio == nullptr) { - LOGGER_ERROR(av->m->log, "Failed to create audio codec session"); + LOGGER_ERROR(av->log, "Failed to create audio codec session"); goto FAILURE; } - call->audio_rtp = rtp_new(RTP_TYPE_AUDIO, av->m, av->tox, call->friend_number, call->bwc, + call->audio_rtp = rtp_new(av->log, RTP_TYPE_AUDIO, av->tox, av, call->friend_number, call->bwc, call->audio, ac_queue_message); if (call->audio_rtp == nullptr) { - LOGGER_ERROR(av->m->log, "Failed to create audio rtp session"); + LOGGER_ERROR(av->log, "Failed to create audio rtp session"); goto FAILURE; } } - - { /* Prepare video */ - call->video = vc_new(av->toxav_mono_time, av->m->log, av, call->friend_number, av->vcb, av->vcb_user_data); + { /* Prepare video */ + call->video = vc_new(av->log, av->toxav_mono_time, av, call->friend_number, av->vcb, av->vcb_user_data); if (call->video == nullptr) { - LOGGER_ERROR(av->m->log, "Failed to create video codec session"); + LOGGER_ERROR(av->log, "Failed to create video codec session"); goto FAILURE; } - call->video_rtp = rtp_new(RTP_TYPE_VIDEO, av->m, av->tox, call->friend_number, call->bwc, + call->video_rtp = rtp_new(av->log, RTP_TYPE_VIDEO, av->tox, av, call->friend_number, call->bwc, call->video, vc_queue_message); if (call->video_rtp == nullptr) { - LOGGER_ERROR(av->m->log, "Failed to create video rtp session"); + LOGGER_ERROR(av->log, "Failed to create video rtp session"); goto FAILURE; } } @@ -1458,11 +1538,11 @@ static bool call_prepare_transmission(ToxAVCall *call) FAILURE: bwc_kill(call->bwc); - rtp_kill(call->audio_rtp); + rtp_kill(av->log, call->audio_rtp); ac_kill(call->audio); call->audio_rtp = nullptr; call->audio = nullptr; - rtp_kill(call->video_rtp); + rtp_kill(av->log, call->video_rtp); vc_kill(call->video); call->video_rtp = nullptr; call->video = nullptr; @@ -1489,12 +1569,14 @@ static void call_kill_transmission(ToxAVCall *call) bwc_kill(call->bwc); - rtp_kill(call->audio_rtp); + const ToxAV *av = call->av; + + rtp_kill(av->log, call->audio_rtp); ac_kill(call->audio); call->audio_rtp = nullptr; call->audio = nullptr; - rtp_kill(call->video_rtp); + rtp_kill(av->log, call->video_rtp); vc_kill(call->video); call->video_rtp = nullptr; call->video = nullptr; @@ -1502,3 +1584,12 @@ static void call_kill_transmission(ToxAVCall *call) pthread_mutex_destroy(call->mutex_audio); pthread_mutex_destroy(call->mutex_video); } + +Mono_Time *toxav_get_av_mono_time(const ToxAV *av) +{ + if (av == nullptr) { + return nullptr; + } + + return av->toxav_mono_time; +} diff --git a/toxav/toxav.h b/toxav/toxav.h index 3b4bd09c23..c305048da1 100644 --- a/toxav/toxav.h +++ b/toxav/toxav.h @@ -73,6 +73,8 @@ typedef struct Tox Tox; #endif /* !TOX_DEFINED */ #endif /* !APIGEN_IGNORE */ +#ifndef TOXAV_DEFINED +#define TOXAV_DEFINED /** * @brief The ToxAV instance type. * @@ -83,6 +85,7 @@ typedef struct Tox Tox; * notifying peers. */ typedef struct ToxAV ToxAV; +#endif /* TOXAV_DEFINED */ /** @{ * @brief Creation and destruction diff --git a/toxav/toxav_hacks.h b/toxav/toxav_hacks.h new file mode 100644 index 0000000000..2b0382d080 --- /dev/null +++ b/toxav/toxav_hacks.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2018 The TokTok team. + * Copyright © 2013-2015 Tox project. + */ +#ifndef C_TOXCORE_TOXAV_HACKS_H +#define C_TOXCORE_TOXAV_HACKS_H + +#include "bwcontroller.h" +#include "msi.h" +#include "rtp.h" + +#ifndef TOXAV_CALL_DEFINED +#define TOXAV_CALL_DEFINED +typedef struct ToxAVCall ToxAVCall; +#endif /* TOXAV_CALL_DEFINED */ + +non_null() +ToxAVCall *call_get(ToxAV *av, uint32_t friend_number); + +non_null() +RTPSession *rtp_session_get(ToxAVCall *call, int payload_type); + +non_null() +MSISession *tox_av_msi_get(const ToxAV *av); + +non_null() +BWController *bwc_controller_get(const ToxAVCall *call); + +non_null() +Mono_Time *toxav_get_av_mono_time(const ToxAV *av); + +non_null() +const Logger *toxav_get_logger(const ToxAV *av); + +#endif /* C_TOXCORE_TOXAV_HACKS_H */ diff --git a/toxav/video.c b/toxav/video.c index 301400b02f..4d1d5a9415 100644 --- a/toxav/video.c +++ b/toxav/video.c @@ -143,7 +143,7 @@ static void vc_init_encoder_cfg(const Logger *log, vpx_codec_enc_cfg_t *cfg, int #endif /* 0 */ } -VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, +VCSession *vc_new(const Logger *log, Mono_Time *mono_time, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data) { VCSession *vc = (VCSession *)calloc(1, sizeof(VCSession)); @@ -216,7 +216,7 @@ VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f /* Set encoder to some initial values */ - vpx_codec_enc_cfg_t cfg; + vpx_codec_enc_cfg_t cfg; vc_init_encoder_cfg(log, &cfg, 1); LOGGER_DEBUG(log, "Using VP8 codec for encoder (0.1)"); @@ -250,6 +250,7 @@ VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f } #endif /* 0 */ + vc->linfts = current_time_monotonic(mono_time); vc->lcfd = 60; vc->vcb = cb; @@ -258,12 +259,14 @@ VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f vc->av = av; vc->log = log; return vc; + BASE_CLEANUP_1: vpx_codec_destroy(vc->decoder); BASE_CLEANUP: pthread_mutex_destroy(vc->queue_mutex); rb_kill(vc->vbuf_raw); free(vc); + return nullptr; } diff --git a/toxav/video.h b/toxav/video.h index 57db74ac66..98e937d984 100644 --- a/toxav/video.h +++ b/toxav/video.h @@ -21,6 +21,11 @@ #include "ring_buffer.h" #include "rtp.h" +#ifndef TOX_DEFINED +#define TOX_DEFINED +typedef struct Tox Tox; +#endif /* TOX_DEFINED */ + typedef struct VCSession { /* encoding */ vpx_codec_ctx_t encoder[1]; @@ -33,7 +38,6 @@ typedef struct VCSession { uint64_t linfts; /* Last received frame time stamp */ uint32_t lcfd; /* Last calculated frame duration for incoming video payload */ - const Logger *log; ToxAV *av; uint32_t friend_number; @@ -42,9 +46,10 @@ typedef struct VCSession { void *vcb_user_data; pthread_mutex_t queue_mutex[1]; + const Logger *log; } VCSession; -VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number, +VCSession *vc_new(const Logger *log, Mono_Time *mono_time, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data); void vc_kill(VCSession *vc); void vc_iterate(VCSession *vc); diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 0f313e2af5..916c6221b2 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -6,7 +6,7 @@ exports_files( "tox.h", "tox_private.h", ], - visibility = ["//c-toxcore:__pkg__"], + visibility = ["//c-toxcore:__subpackages__"], ) cc_library( @@ -661,7 +661,10 @@ cc_library( name = "net_crypto", srcs = ["net_crypto.c"], hdrs = ["net_crypto.h"], - visibility = ["//c-toxcore/auto_tests:__pkg__"], + visibility = [ + "//c-toxcore/auto_tests:__pkg__", + "//c-toxcore/toxav:__pkg__", + ], deps = [ ":DHT", ":LAN_discovery", diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index d6dc24d37a..7c9730bb63 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -472,10 +472,6 @@ int m_delfriend(Messenger *m, int32_t friendnumber) return -1; } - if (m->friend_connectionstatuschange_internal != nullptr) { - m->friend_connectionstatuschange_internal(m, friendnumber, false, m->friend_connectionstatuschange_internal_userdata); - } - clear_receipts(m, friendnumber); remove_request_received(m->fr, m->friendlist[friendnumber].real_pk); friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, nullptr, @@ -1027,13 +1023,6 @@ void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *funct m->core_connection_change = function; } -void m_callback_connectionstatus_internal_av(Messenger *m, m_friend_connectionstatuschange_internal_cb *function, - void *userdata) -{ - m->friend_connectionstatuschange_internal = function; - m->friend_connectionstatuschange_internal_userdata = userdata; -} - non_null(1) nullable(3) static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber, void *userdata) { @@ -1081,11 +1070,6 @@ static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, ui m->friendlist[friendnumber].status = status; check_friend_tcp_udp(m, friendnumber, userdata); - - if (m->friend_connectionstatuschange_internal != nullptr) { - m->friend_connectionstatuschange_internal(m, friendnumber, is_online, - m->friend_connectionstatuschange_internal_userdata); - } } } @@ -1855,23 +1839,6 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, bool outbound, } } -/** @brief Set the callback for msi packets. */ -void m_callback_msi_packet(Messenger *m, m_msi_packet_cb *function, void *userdata) -{ - m->msi_packet = function; - m->msi_packet_userdata = userdata; -} - -/** @brief Send an msi packet. - * - * @retval true on success - * @retval false on failure - */ -bool m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length) -{ - return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length, false); -} - static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata) { @@ -1881,17 +1848,6 @@ static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t * return 1; } - if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) { - const RTP_Packet_Handler *const ph = - &m->friendlist[friendcon_id].lossy_rtp_packethandlers[data[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE]; - - if (ph->function != nullptr) { - return ph->function(m, friendcon_id, data, length, ph->object); - } - - return 1; - } - if (m->lossy_packethandler != nullptr) { m->lossy_packethandler(m, friendcon_id, data[0], data, length, userdata); } @@ -1904,38 +1860,6 @@ void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb m->lossy_packethandler = lossy_packethandler; } -int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lossy_rtp_packet_cb *function, - void *object) -{ - if (!m_friend_exists(m, friendnumber)) { - return -1; - } - - if (byte < PACKET_ID_RANGE_LOSSY_AV_START || byte > PACKET_ID_RANGE_LOSSY_AV_END) { - return -1; - } - - m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].function = function; - m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].object = object; - return 0; -} - -/** @brief High level function to send custom lossy packets. - * - * TODO(oxij): this name is confusing, because this function sends both av and custom lossy packets. - * Meanwhile, m_handle_lossy_packet routes custom packets to custom_lossy_packet_registerhandler - * as you would expect from its name. - * - * I.e. custom_lossy_packet_registerhandler's "custom lossy packet" and this "custom lossy packet" - * are not the same set of packets. - * - * @retval -1 if friend invalid. - * @retval -2 if length wrong. - * @retval -3 if first byte invalid. - * @retval -4 if friend offline. - * @retval -5 if packet failed to send because of other error. - * @retval 0 on success. - */ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) { if (!m_friend_exists(m, friendnumber)) { @@ -1946,7 +1870,6 @@ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const u return -2; } - // TODO(oxij): send_lossy_cryptpacket makes this check already, similarly for other similar places if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) { return -3; } @@ -1974,7 +1897,10 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin } if (packet[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || packet[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) { - return -1; + // allow PACKET_ID_MSI packets to be handled by custom packet handler + if (packet[0] != PACKET_ID_MSI) { + return -1; + } } if (m->lossless_packethandler != nullptr) { @@ -2356,20 +2282,6 @@ static int m_handle_packet_file_data(Messenger *m, const int friendcon_id, const return 0; } -non_null(1, 3) nullable(5) -static int m_handle_packet_msi(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata) -{ - if (data_length == 0) { - return 0; - } - - if (m->msi_packet != nullptr) { - m->msi_packet(m, friendcon_id, data, data_length, m->msi_packet_userdata); - } - - return 0; -} - non_null(1, 3) nullable(5) static int m_handle_packet_invite_groupchat(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata) { @@ -2443,7 +2355,7 @@ static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, case PACKET_ID_FILE_DATA: return m_handle_packet_file_data(m, friendcon_id, payload, payload_length, userdata); case PACKET_ID_MSI: - return m_handle_packet_msi(m, friendcon_id, payload, payload_length, userdata); + return handle_custom_lossless_packet(object, friendcon_id, data, length, userdata); case PACKET_ID_INVITE_GROUPCHAT: return m_handle_packet_invite_groupchat(m, friendcon_id, payload, payload_length, userdata); } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index cace3340b2..1fed8d3439 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -200,15 +200,11 @@ typedef void m_friend_lossy_packet_cb(Messenger *m, uint32_t friend_number, uint size_t length, void *user_data); typedef void m_friend_lossless_packet_cb(Messenger *m, uint32_t friend_number, uint8_t packet_id, const uint8_t *data, size_t length, void *user_data); -typedef void m_friend_connectionstatuschange_internal_cb(Messenger *m, uint32_t friend_number, - bool is_online, void *user_data); typedef void m_conference_invite_cb(Messenger *m, uint32_t friend_number, const uint8_t *cookie, uint16_t length, void *user_data); typedef void m_group_invite_cb(const Messenger *m, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length, void *user_data); -typedef void m_msi_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, - void *user_data); -typedef int m_lossy_rtp_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object); +typedef int m_lossy_rtp_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t len, void *object); typedef struct RTP_Packet_Handler { m_lossy_rtp_packet_cb *function; @@ -243,8 +239,6 @@ typedef struct Friend { uint32_t num_sending_files; struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; - RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_ID_RANGE_LOSSY_AV_SIZE]; - struct Receipts *receipts_start; struct Receipts *receipts_end; } Friend; @@ -301,8 +295,6 @@ struct Messenger { m_friend_typing_cb *friend_typingchange; m_friend_read_receipt_cb *read_receipt; m_friend_connection_status_cb *friend_connectionstatuschange; - m_friend_connectionstatuschange_internal_cb *friend_connectionstatuschange_internal; - void *friend_connectionstatuschange_internal_userdata; struct Group_Chats *conferences_object; m_conference_invite_cb *conference_invite; @@ -314,9 +306,6 @@ struct Messenger { m_file_recv_chunk_cb *file_filedata; m_file_chunk_request_cb *file_reqchunk; - m_msi_packet_cb *msi_packet; - void *msi_packet_userdata; - m_friend_lossy_packet_cb *lossy_packethandler; m_friend_lossless_packet_cb *lossless_packethandler; @@ -614,10 +603,6 @@ non_null() void m_callback_read_receipt(Messenger *m, m_friend_read_receipt_cb * */ non_null() void m_callback_connectionstatus(Messenger *m, m_friend_connection_status_cb *function); -/** Same as previous but for internal A/V core usage only */ -non_null() void m_callback_connectionstatus_internal_av( - Messenger *m, m_friend_connectionstatuschange_internal_cb *function, void *userdata); - /** @brief Set the callback for typing changes. */ non_null() void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function); @@ -731,42 +716,12 @@ non_null(1) nullable(5) int send_file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data, uint16_t length); -/*** A/V related */ - -/** @brief Set the callback for msi packets. */ -non_null(1) nullable(2, 3) -void m_callback_msi_packet(Messenger *m, m_msi_packet_cb *function, void *userdata); - -/** @brief Send an msi packet. - * - * @retval true on success - * @retval false on failure - */ -non_null() -bool m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length); - -/** @brief Set handlers for lossy rtp packets. - * - * @retval -1 on failure. - * @retval 0 on success. - */ -non_null(1) nullable(4, 5) -int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, - m_lossy_rtp_packet_cb *function, void *object); - /*** CUSTOM PACKETS */ /** @brief Set handlers for custom lossy packets. */ non_null() void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *lossy_packethandler); /** @brief High level function to send custom lossy packets. - * - * TODO(oxij): this name is confusing, because this function sends both av and custom lossy packets. - * Meanwhile, m_handle_lossy_packet routes custom packets to custom_lossy_packet_registerhandler - * as you would expect from its name. - * - * I.e. custom_lossy_packet_registerhandler's "custom lossy packet" and this "custom lossy packet" - * are not the same set of packets. * * @retval -1 if friend invalid. * @retval -2 if length wrong. diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1680b078b7..94b31d007a 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -122,9 +122,6 @@ typedef struct Crypto_Connection { bool maximum_speed_reached; - /* Must be a pointer, because the struct is moved in memory */ - pthread_mutex_t *mutex; - dht_pk_cb *dht_pk_callback; void *dht_pk_callback_object; uint32_t dht_pk_callback_number; @@ -143,10 +140,6 @@ struct Net_Crypto { TCP_Connections *tcp_c; Crypto_Connection *crypto_connections; - pthread_mutex_t tcp_mutex; - - pthread_mutex_t connections_mutex; - unsigned int connection_use_counter; uint32_t crypto_connections_length; /* Length of connections array. */ @@ -691,7 +684,6 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t bool direct_send_attempt = false; - pthread_mutex_lock(conn->mutex); const IP_Port ip_port = return_ip_port_connection(c, crypt_connection_id); // TODO(irungentoo): on bad networks, direct connections might not last indefinitely. @@ -703,11 +695,9 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t if (direct_connected) { if ((uint32_t)sendpacket(dht_get_net(c->dht), &ip_port, data, length) == length) { - pthread_mutex_unlock(conn->mutex); return 0; } - pthread_mutex_unlock(conn->mutex); LOGGER_WARNING(c->log, "sending packet of length %d failed", length); return -1; } @@ -724,19 +714,12 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t } } - pthread_mutex_unlock(conn->mutex); - pthread_mutex_lock(&c->tcp_mutex); const int ret = send_packet_tcp_connection(c->tcp_c, conn->connection_number_tcp, data, length); - pthread_mutex_unlock(&c->tcp_mutex); - - pthread_mutex_lock(conn->mutex); if (ret == 0) { conn->last_tcp_sent = current_time_monotonic(c->mono_time); } - pthread_mutex_unlock(conn->mutex); - if (direct_send_attempt) { return 0; } @@ -1097,7 +1080,6 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_ return -1; } - pthread_mutex_lock(conn->mutex); const uint16_t packet_size = 1 + sizeof(uint16_t) + length + CRYPTO_MAC_SIZE; VLA(uint8_t, packet, packet_size); packet[0] = NET_PACKET_CRYPTO_DATA; @@ -1106,12 +1088,10 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_ if (len + 1 + sizeof(uint16_t) != packet_size) { LOGGER_ERROR(c->log, "encryption failed: %d", len); - pthread_mutex_unlock(conn->mutex); return -1; } increment_nonce(conn->sent_nonce); - pthread_mutex_unlock(conn->mutex); return send_packet_to(c, crypt_connection_id, packet, packet_size); } @@ -1207,9 +1187,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons dt.sent_time = 0; dt.length = length; memcpy(dt.data, data, length); - pthread_mutex_lock(conn->mutex); const int64_t packet_num = add_data_end_of_buffer(c->log, c->mem, &conn->send_array, &dt); - pthread_mutex_unlock(conn->mutex); if (packet_num == -1) { return -1; @@ -1521,18 +1499,7 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id, void *userda false, userdata); } - while (true) { /* TODO(irungentoo): is this really the best way to do this? */ - pthread_mutex_lock(&c->connections_mutex); - - if (c->connection_use_counter == 0) { - break; - } - - pthread_mutex_unlock(&c->connections_mutex); - } - crypto_kill(c, crypt_connection_id); - pthread_mutex_unlock(&c->connections_mutex); } /** @brief Handle a received data packet. @@ -1635,9 +1602,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const } while (true) { - pthread_mutex_lock(conn->mutex); const int ret = read_data_beg_buffer(c->mem, &conn->recv_array, &dt); - pthread_mutex_unlock(conn->mutex); if (ret == -1) { break; @@ -1835,16 +1800,6 @@ static int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) non_null() static int create_crypto_connection(Net_Crypto *c) { - while (true) { /* TODO(irungentoo): is this really the best way to do this? */ - pthread_mutex_lock(&c->connections_mutex); - - if (c->connection_use_counter == 0) { - break; - } - - pthread_mutex_unlock(&c->connections_mutex); - } - int id = -1; for (uint32_t i = 0; i < c->crypto_connections_length; ++i) { @@ -1863,30 +1818,14 @@ static int create_crypto_connection(Net_Crypto *c) } if (id != -1) { - pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); - - if (mutex == nullptr) { - pthread_mutex_unlock(&c->connections_mutex); - return -1; - } - - if (pthread_mutex_init(mutex, nullptr) != 0) { - mem_delete(c->mem, mutex); - pthread_mutex_unlock(&c->connections_mutex); - return -1; - } - // Memsetting float/double to 0 is non-portable, so we explicitly set them to 0 c->crypto_connections[id].packet_recv_rate = 0.0; c->crypto_connections[id].packet_send_rate = 0.0; c->crypto_connections[id].last_packets_left_rem = 0.0; c->crypto_connections[id].packet_send_rate_requested = 0.0; c->crypto_connections[id].last_packets_left_requested_rem = 0.0; - c->crypto_connections[id].mutex = mutex; - c->crypto_connections[id].status = CRYPTO_CONN_NO_CONNECTION; } - pthread_mutex_unlock(&c->connections_mutex); return id; } @@ -1914,8 +1853,6 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) uint32_t i; - pthread_mutex_destroy(c->crypto_connections[crypt_connection_id].mutex); - mem_delete(c->mem, c->crypto_connections[crypt_connection_id].mutex); crypto_memzero(&c->crypto_connections[crypt_connection_id], sizeof(Crypto_Connection)); /* check if we can resize the connections array */ @@ -2098,9 +2035,7 @@ int accept_crypto_connection(Net_Crypto *c, const New_Connection *n_c) return -1; } - pthread_mutex_lock(&c->tcp_mutex); const int connection_number_tcp = new_tcp_connection_to(c->tcp_c, n_c->dht_public_key, crypt_connection_id); - pthread_mutex_unlock(&c->tcp_mutex); if (connection_number_tcp == -1) { wipe_crypto_connection(c, crypt_connection_id); @@ -2117,9 +2052,7 @@ int accept_crypto_connection(Net_Crypto *c, const New_Connection *n_c) conn->status = CRYPTO_CONN_NOT_CONFIRMED; if (create_send_handshake(c, crypt_connection_id, n_c->cookie, n_c->dht_public_key) != 0) { - pthread_mutex_lock(&c->tcp_mutex); kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp); - pthread_mutex_unlock(&c->tcp_mutex); wipe_crypto_connection(c, crypt_connection_id); return -1; } @@ -2155,9 +2088,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u Crypto_Connection *conn = &c->crypto_connections[crypt_connection_id]; - pthread_mutex_lock(&c->tcp_mutex); const int connection_number_tcp = new_tcp_connection_to(c->tcp_c, dht_public_key, crypt_connection_id); - pthread_mutex_unlock(&c->tcp_mutex); if (connection_number_tcp == -1) { wipe_crypto_connection(c, crypt_connection_id); @@ -2181,9 +2112,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u if (create_cookie_request(c, cookie_request, conn->dht_public_key, conn->cookie_request_number, conn->shared_key) != sizeof(cookie_request) || new_temp_packet(c, crypt_connection_id, cookie_request, sizeof(cookie_request)) != 0) { - pthread_mutex_lock(&c->tcp_mutex); kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp); - pthread_mutex_unlock(&c->tcp_mutex); wipe_crypto_connection(c, crypt_connection_id); return -1; } @@ -2241,11 +2170,7 @@ static int tcp_data_callback(void *object, int crypt_connection_id, const uint8_ return tcp_handle_cookie_request(c, conn->connection_number_tcp, packet, length); } - // This unlocks the mutex that at this point is locked by do_tcp before - // calling do_tcp_connections. - pthread_mutex_unlock(&c->tcp_mutex); const int ret = handle_packet_connection(c, crypt_connection_id, packet, length, false, userdata); - pthread_mutex_lock(&c->tcp_mutex); if (ret != 0) { return -1; @@ -2295,10 +2220,7 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, const IP_Port *ip return -1; } - pthread_mutex_lock(&c->tcp_mutex); - const int ret = add_tcp_relay_connection(c->tcp_c, conn->connection_number_tcp, ip_port, public_key); - pthread_mutex_unlock(&c->tcp_mutex); - return ret; + return add_tcp_relay_connection(c->tcp_c, conn->connection_number_tcp, ip_port, public_key); } /** @brief Add a tcp relay to the array. @@ -2308,10 +2230,7 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, const IP_Port *ip */ int add_tcp_relay(Net_Crypto *c, const IP_Port *ip_port, const uint8_t *public_key) { - pthread_mutex_lock(&c->tcp_mutex); - const int ret = add_tcp_relay_global(c->tcp_c, ip_port, public_key); - pthread_mutex_unlock(&c->tcp_mutex); - return ret; + return add_tcp_relay_global(c->tcp_c, ip_port, public_key); } /** @brief Return a random TCP connection number for use in send_tcp_onion_request. @@ -2322,13 +2241,9 @@ int add_tcp_relay(Net_Crypto *c, const IP_Port *ip_port, const uint8_t *public_k * return TCP connection number on success. * return -1 on failure. */ -int get_random_tcp_con_number(Net_Crypto *c) +int get_random_tcp_con_number(const Net_Crypto *c) { - pthread_mutex_lock(&c->tcp_mutex); - const int ret = get_random_tcp_onion_conn_number(c->tcp_c); - pthread_mutex_unlock(&c->tcp_mutex); - - return ret; + return get_random_tcp_onion_conn_number(c->tcp_c); } /** @brief Put IP_Port of a random onion TCP connection in ip_port. @@ -2336,13 +2251,9 @@ int get_random_tcp_con_number(Net_Crypto *c) * return true on success. * return false on failure. */ -bool get_random_tcp_conn_ip_port(Net_Crypto *c, IP_Port *ip_port) +bool get_random_tcp_conn_ip_port(const Net_Crypto *c, IP_Port *ip_port) { - pthread_mutex_lock(&c->tcp_mutex); - const bool ret = tcp_get_random_conn_ip_port(c->tcp_c, ip_port); - pthread_mutex_unlock(&c->tcp_mutex); - - return ret; + return tcp_get_random_conn_ip_port(c->tcp_c, ip_port); } /** @brief Send an onion packet via the TCP relay corresponding to tcp_connections_number. @@ -2352,11 +2263,7 @@ bool get_random_tcp_conn_ip_port(Net_Crypto *c, IP_Port *ip_port) */ int send_tcp_onion_request(Net_Crypto *c, unsigned int tcp_connections_number, const uint8_t *data, uint16_t length) { - pthread_mutex_lock(&c->tcp_mutex); - const int ret = tcp_send_onion_request(c->tcp_c, tcp_connections_number, data, length); - pthread_mutex_unlock(&c->tcp_mutex); - - return ret; + return tcp_send_onion_request(c->tcp_c, tcp_connections_number, data, length); } /** @@ -2371,12 +2278,8 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port const uint8_t *chain_keys, uint16_t chain_length, const uint8_t *data, uint16_t data_length) { - pthread_mutex_lock(&c->tcp_mutex); - const int ret = tcp_send_forward_request(logger, c->tcp_c, tcp_forwarder, dht_node, - chain_keys, chain_length, data, data_length); - pthread_mutex_unlock(&c->tcp_mutex); - - return ret; + return tcp_send_forward_request(logger, c->tcp_c, tcp_forwarder, dht_node, + chain_keys, chain_length, data, data_length); } /** @brief Copy a maximum of num random TCP relays we are connected to to tcp_relays. @@ -2386,38 +2289,28 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port * return number of relays copied to tcp_relays on success. * return 0 on failure. */ -unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num) +unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num) { if (num == 0) { return 0; } - pthread_mutex_lock(&c->tcp_mutex); - const unsigned int ret = tcp_copy_connected_relays(c->tcp_c, tcp_relays, num); - pthread_mutex_unlock(&c->tcp_mutex); - - return ret; + return tcp_copy_connected_relays(c->tcp_c, tcp_relays, num); } -uint32_t copy_connected_tcp_relays_index(Net_Crypto *c, Node_format *tcp_relays, uint16_t num, uint32_t idx) +uint32_t copy_connected_tcp_relays_index(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num, uint32_t idx) { if (num == 0) { return 0; } - pthread_mutex_lock(&c->tcp_mutex); - const uint32_t ret = tcp_copy_connected_relays_index(c->tcp_c, tcp_relays, num, idx); - pthread_mutex_unlock(&c->tcp_mutex); - - return ret; + return tcp_copy_connected_relays_index(c->tcp_c, tcp_relays, num, idx); } non_null() static void do_tcp(Net_Crypto *c, void *userdata) { - pthread_mutex_lock(&c->tcp_mutex); do_tcp_connections(c->log, c->tcp_c, userdata); - pthread_mutex_unlock(&c->tcp_mutex); for (uint32_t i = 0; i < c->crypto_connections_length; ++i) { const Crypto_Connection *conn = get_crypto_connection(c, i); @@ -2436,9 +2329,7 @@ static void do_tcp(Net_Crypto *c, void *userdata) continue; } - pthread_mutex_lock(&c->tcp_mutex); set_tcp_connection_to_status(c->tcp_c, conn->connection_number_tcp, !direct_connected); - pthread_mutex_unlock(&c->tcp_mutex); } } @@ -2593,15 +2484,12 @@ static int udp_handle_packet(void *object, const IP_Port *source, const uint8_t return -1; } - pthread_mutex_lock(conn->mutex); - if (net_family_is_ipv4(source->ip.family)) { conn->direct_lastrecv_timev4 = mono_time_get(c->mono_time); } else { conn->direct_lastrecv_timev6 = mono_time_get(c->mono_time); } - pthread_mutex_unlock(conn->mutex); return 0; } @@ -2997,26 +2885,16 @@ int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t return -1; } - pthread_mutex_lock(&c->connections_mutex); - ++c->connection_use_counter; - pthread_mutex_unlock(&c->connections_mutex); - Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); int ret = -1; if (conn != nullptr) { - pthread_mutex_lock(conn->mutex); const uint32_t buffer_start = conn->recv_array.buffer_start; const uint32_t buffer_end = conn->send_array.buffer_end; - pthread_mutex_unlock(conn->mutex); ret = send_data_packet_helper(c, crypt_connection_id, buffer_start, buffer_end, data, length); } - pthread_mutex_lock(&c->connections_mutex); - --c->connection_use_counter; - pthread_mutex_unlock(&c->connections_mutex); - return ret; } @@ -3036,9 +2914,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) send_kill_packet(c, crypt_connection_id); } - pthread_mutex_lock(&c->tcp_mutex); kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp); - pthread_mutex_unlock(&c->tcp_mutex); bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_portv4, crypt_connection_id); bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_portv6, crypt_connection_id); @@ -3135,13 +3011,6 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r set_packet_tcp_connection_callback(temp->tcp_c, &tcp_data_callback, temp); set_oob_packet_tcp_connection_callback(temp->tcp_c, &tcp_oob_callback, temp); - if (create_recursive_mutex(&temp->tcp_mutex) != 0 || - pthread_mutex_init(&temp->connections_mutex, nullptr) != 0) { - kill_tcp_connections(temp->tcp_c); - mem_delete(mem, temp); - return nullptr; - } - temp->dht = dht; new_keys(temp); @@ -3215,9 +3084,6 @@ void kill_net_crypto(Net_Crypto *c) crypto_kill(c, i); } - pthread_mutex_destroy(&c->tcp_mutex); - pthread_mutex_destroy(&c->connections_mutex); - kill_tcp_connections(c->tcp_c); bs_list_free(&c->ip_port_list); networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 0d817e4315..8368d46e39 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -40,7 +40,6 @@ /** Packets in this range are reserved for AV use. */ #define PACKET_ID_RANGE_LOSSY_START 192 #define PACKET_ID_RANGE_LOSSY_AV_START 192 -#define PACKET_ID_RANGE_LOSSY_AV_SIZE 8 #define PACKET_ID_RANGE_LOSSY_AV_END 199 /** Packets in this range can be used for anything. */ #define PACKET_ID_RANGE_LOSSY_CUSTOM_START 200 @@ -311,7 +310,7 @@ int add_tcp_relay(Net_Crypto *c, const IP_Port *ip_port, const uint8_t *public_k * return -1 on failure. */ non_null() -int get_random_tcp_con_number(Net_Crypto *c); +int get_random_tcp_con_number(const Net_Crypto *c); /** @brief Put IP_Port of a random onion TCP connection in ip_port. * @@ -319,7 +318,7 @@ int get_random_tcp_con_number(Net_Crypto *c); * return false on failure. */ non_null() -bool get_random_tcp_conn_ip_port(Net_Crypto *c, IP_Port *ip_port); +bool get_random_tcp_conn_ip_port(const Net_Crypto *c, IP_Port *ip_port); /** @brief Send an onion packet via the TCP relay corresponding to tcp_connections_number. * @@ -351,7 +350,7 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port * return 0 on failure. */ non_null() -unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num); +unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num); /** * Copy a maximum of `max_num` TCP relays we are connected to starting at the index in the TCP relay array @@ -360,7 +359,7 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u * Returns the number of relays successfully copied. */ non_null() -uint32_t copy_connected_tcp_relays_index(Net_Crypto *c, Node_format *tcp_relays, uint16_t num, uint32_t idx); +uint32_t copy_connected_tcp_relays_index(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num, uint32_t idx); /** @brief Kill a crypto connection. * diff --git a/toxcore/tox.c b/toxcore/tox.c index 03e34414a8..de805edc46 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -1058,7 +1058,7 @@ void tox_kill(Tox *tox) } tox_lock(tox); - LOGGER_ASSERT(tox->m->log, tox->m->msi_packet == nullptr, "Attempted to kill tox while toxav is still alive"); + LOGGER_ASSERT(tox->m->log, tox->toxav_object == nullptr, "Attempted to kill tox while toxav is still alive"); kill_groupchats(tox->m->conferences_object); kill_messenger(tox->m); mono_time_free(tox->sys.mem, tox->mono_time); diff --git a/toxcore/tox.h b/toxcore/tox.h index aea2db0e90..dbc3785ecf 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -3216,9 +3216,9 @@ typedef enum Tox_Err_Friend_Custom_Packet { TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_CONNECTED, /** - * The first byte of data was not in the specified range for the packet - * type. This range is 192-254 for lossy, and 69, 160-191 for lossless - * packets. + * The first byte of data was not one of the permitted values; + * for lossy packets the first byte must be in the range 192-254, + * and for lossless packets it must be either 69 or in the range 160-191. */ TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID, @@ -3269,7 +3269,7 @@ bool tox_friend_send_lossy_packet( /** * @brief Send a custom lossless packet to a friend. * - * The first byte of data must be in the range 69, 160-191. Maximum length of a + * The first byte of data must be either 69 or in the range 160-191. Maximum length of a * custom packet is TOX_MAX_CUSTOM_PACKET_SIZE. * * Lossless packet behaviour is comparable to TCP (reliability, arrive in order) @@ -3288,6 +3288,9 @@ bool tox_friend_send_lossless_packet( Tox_Err_Friend_Custom_Packet *error); /** + * tox_callback_friend_lossy_packet is the compatibility function to + * set callback for all packet IDs except those reserved for ToxAV + * * @param friend_number The friend number of the friend who sent a lossy packet. * @param data A byte array containing the received packet data. * @param length The length of the packet data byte array.