update toxcore, toxav fixes merged
Some checks are pending
ContinuousDelivery / linux-ubuntu (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android]) (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android]) (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android]) (push) Waiting to run
ContinuousDelivery / windows (push) Waiting to run
ContinuousDelivery / windows-asan (push) Waiting to run
ContinuousDelivery / release (push) Blocked by required conditions
ContinuousIntegration / linux (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android]) (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android]) (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android]) (push) Waiting to run
ContinuousIntegration / macos (push) Waiting to run
ContinuousIntegration / windows (push) Waiting to run
Some checks are pending
ContinuousDelivery / linux-ubuntu (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android]) (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android]) (push) Waiting to run
ContinuousDelivery / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android]) (push) Waiting to run
ContinuousDelivery / windows (push) Waiting to run
ContinuousDelivery / windows-asan (push) Waiting to run
ContinuousDelivery / release (push) Blocked by required conditions
ContinuousIntegration / linux (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:arm64-v8a vcpkg_toolkit:arm64-android]) (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:armeabi-v7a vcpkg_toolkit:arm-neon-android]) (push) Waiting to run
ContinuousIntegration / android (map[ndk_abi:x86_64 vcpkg_toolkit:x64-android]) (push) Waiting to run
ContinuousIntegration / macos (push) Waiting to run
ContinuousIntegration / windows (push) Waiting to run
Merge commit 'cae0ab9c5c75eda665d21dc36cfeef48c1e04b53'
This commit is contained in:
103
external/toxcore/c-toxcore/toxav/bwcontroller.c
vendored
103
external/toxcore/c-toxcore/toxav/bwcontroller.c
vendored
@ -10,12 +10,16 @@
|
||||
#include <string.h>
|
||||
|
||||
#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, "Got BWCMessage of insufficient size.");
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user