From 2d84681529161fd6add331286483ec58c034ba45 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 11 Aug 2018 14:11:55 +0000 Subject: [PATCH] Fix ToxAv's use of `struct Tox`. * Fix `toxav_get_tox` to return tox, not messenger. * Fix the casts from Tox* to Messenger* in toxav_old.c. * Pass Tox instead of Messenger to public group AV callbacks. --- toxav/BUILD.bazel | 2 +- toxav/groupav.c | 21 ++++++++++++--------- toxav/groupav.h | 9 +++++++-- toxav/toxav.c | 4 +++- toxav/toxav_old.c | 13 ++++++++----- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/toxav/BUILD.bazel b/toxav/BUILD.bazel index 1c1b3556..151ac48e 100644 --- a/toxav/BUILD.bazel +++ b/toxav/BUILD.bazel @@ -99,7 +99,7 @@ cc_library( srcs = ["groupav.c"], hdrs = ["groupav.h"], deps = [ - "//c-toxcore/toxcore:group", + "//c-toxcore/toxcore:toxcore", "@opus", ], ) diff --git a/toxav/groupav.c b/toxav/groupav.c index 091f1255..b10a7f8e 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -163,6 +163,7 @@ static Group_Audio_Packet *dequeue(Group_JitterBuffer *q, int *success) typedef struct Group_AV { const Logger *log; + Tox *tox; Group_Chats *g_c; OpusEncoder *audio_encoder; @@ -231,7 +232,8 @@ static int recreate_encoder(Group_AV *group_av) return 0; } -static Group_AV *new_group_av(const Logger *log, Group_Chats *g_c, audio_data_cb *audio_callback, void *userdata) +static Group_AV *new_group_av(const Logger *log, Tox *tox, Group_Chats *g_c, audio_data_cb *audio_callback, + void *userdata) { if (!g_c) { return nullptr; @@ -244,6 +246,7 @@ static Group_AV *new_group_av(const Logger *log, Group_Chats *g_c, audio_data_cb } group_av->log = log; + group_av->tox = tox; group_av->g_c = g_c; group_av->audio_data = audio_callback; @@ -386,7 +389,7 @@ static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, uint3 if (out_audio) { if (group_av->audio_data) { - group_av->audio_data(group_av->g_c->m, groupnumber, friendgroupnumber, out_audio, out_audio_samples, + group_av->audio_data(group_av->tox, groupnumber, friendgroupnumber, out_audio, out_audio_samples, peer_av->decoder_channels, sample_rate, group_av->userdata); } @@ -433,10 +436,10 @@ static int handle_group_audio_packet(void *object, uint32_t groupnumber, uint32_ * return 0 on success. * return -1 on failure. */ -static int groupchat_enable_av(const Logger *log, Group_Chats *g_c, uint32_t groupnumber, +static int groupchat_enable_av(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t groupnumber, audio_data_cb *audio_callback, void *userdata) { - Group_AV *group_av = new_group_av(log, g_c, audio_callback, userdata); + Group_AV *group_av = new_group_av(log, tox, g_c, audio_callback, userdata); if (group_av == nullptr) { return -1; @@ -459,7 +462,7 @@ static int groupchat_enable_av(const Logger *log, Group_Chats *g_c, uint32_t gro * return group number on success. * return -1 on failure. */ -int add_av_groupchat(const Logger *log, Group_Chats *g_c, audio_data_cb *audio_callback, void *userdata) +int add_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, audio_data_cb *audio_callback, void *userdata) { int groupnumber = add_groupchat(g_c, GROUPCHAT_TYPE_AV); @@ -467,7 +470,7 @@ int add_av_groupchat(const Logger *log, Group_Chats *g_c, audio_data_cb *audio_c return -1; } - if (groupchat_enable_av(log, g_c, groupnumber, audio_callback, userdata) == -1) { + if (groupchat_enable_av(log, tox, g_c, groupnumber, audio_callback, userdata) == -1) { del_groupchat(g_c, groupnumber); return -1; } @@ -480,8 +483,8 @@ int add_av_groupchat(const Logger *log, Group_Chats *g_c, audio_data_cb *audio_c * returns group number on success * returns -1 on failure. */ -int join_av_groupchat(const Logger *log, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, uint16_t length, - audio_data_cb *audio_callback, void *userdata) +int join_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, + uint16_t length, audio_data_cb *audio_callback, void *userdata) { int groupnumber = join_groupchat(g_c, friendnumber, GROUPCHAT_TYPE_AV, data, length); @@ -489,7 +492,7 @@ int join_av_groupchat(const Logger *log, Group_Chats *g_c, uint32_t friendnumber return -1; } - if (groupchat_enable_av(log, g_c, groupnumber, audio_callback, userdata) == -1) { + if (groupchat_enable_av(log, tox, g_c, groupnumber, audio_callback, userdata) == -1) { del_groupchat(g_c, groupnumber); return -1; } diff --git a/toxav/groupav.h b/toxav/groupav.h index 4d9efafb..31c9abba 100644 --- a/toxav/groupav.h +++ b/toxav/groupav.h @@ -21,12 +21,16 @@ #define C_TOXCORE_TOXAV_GROUPAV_H #include "../toxcore/group.h" +#include "../toxcore/tox.h" /* Audio encoding/decoding */ #include #define GROUP_AUDIO_PACKET_ID 192 +// TODO(iphydf): Use this better typed one instead of the void-pointer one below. +// typedef void audio_data_cb(Tox *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t *pcm, +// uint32_t samples, uint8_t channels, uint32_t sample_rate, void *userdata); typedef void audio_data_cb(void *tox, uint32_t groupnumber, uint32_t peernumber, const int16_t *pcm, uint32_t samples, uint8_t channels, uint32_t sample_rate, void *userdata); @@ -35,14 +39,15 @@ typedef void audio_data_cb(void *tox, uint32_t groupnumber, uint32_t peernumber, * return group number on success. * return -1 on failure. */ -int add_av_groupchat(const Logger *log, Group_Chats *g_c, audio_data_cb *audio_callback, void *userdata); +int add_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, audio_data_cb *audio_callback, void *userdata); /* Join a AV group (you need to have been invited first.) * * returns group number on success * returns -1 on failure. */ -int join_av_groupchat(const Logger *log, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, uint16_t length, +int join_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t friendnumber, const uint8_t *data, + uint16_t length, audio_data_cb *audio_callback, void *userdata); diff --git a/toxav/toxav.c b/toxav/toxav.c index ad2976d6..6f47a0bc 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -80,6 +80,7 @@ typedef struct ToxAVCall_s { } ToxAVCall; struct ToxAV { + Tox *tox; Messenger *m; MSISession *msi; @@ -166,6 +167,7 @@ ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error) goto END; } + av->tox = tox; av->m = m; av->msi = msi_new(av->m); @@ -230,7 +232,7 @@ void toxav_kill(ToxAV *av) } Tox *toxav_get_tox(const ToxAV *av) { - return (Tox *) av->m; + return av->tox; } uint32_t toxav_iteration_interval(const ToxAV *av) { diff --git a/toxav/toxav_old.c b/toxav/toxav_old.c index 6c9032e7..2ea9def4 100644 --- a/toxav/toxav_old.c +++ b/toxav/toxav_old.c @@ -37,8 +37,9 @@ */ int toxav_add_av_groupchat(Tox *tox, audio_data_cb *audio_callback, void *userdata) { - Messenger *m = (Messenger *)tox; - return add_av_groupchat(m->log, m->conferences_object, audio_callback, userdata); + // TODO(iphydf): Don't rely on toxcore internals. + Messenger *m = *(Messenger **)tox; + return add_av_groupchat(m->log, tox, m->conferences_object, audio_callback, userdata); } /* Join a AV group (you need to have been invited first.) @@ -54,8 +55,9 @@ int toxav_add_av_groupchat(Tox *tox, audio_data_cb *audio_callback, void *userda int toxav_join_av_groupchat(Tox *tox, uint32_t friendnumber, const uint8_t *data, uint16_t length, audio_data_cb *audio_callback, void *userdata) { - Messenger *m = (Messenger *)tox; - return join_av_groupchat(m->log, m->conferences_object, friendnumber, data, length, audio_callback, userdata); + // TODO(iphydf): Don't rely on toxcore internals. + Messenger *m = *(Messenger **)tox; + return join_av_groupchat(m->log, tox, m->conferences_object, friendnumber, data, length, audio_callback, userdata); } /* Send audio to the group chat. @@ -74,6 +76,7 @@ int toxav_join_av_groupchat(Tox *tox, uint32_t friendnumber, const uint8_t *data int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t sample_rate) { - Messenger *m = (Messenger *)tox; + // TODO(iphydf): Don't rely on toxcore internals. + Messenger *m = *(Messenger **)tox; return group_send_audio(m->conferences_object, groupnumber, pcm, samples, channels, sample_rate); }