From e3935cbf88b25f29a41416f91090361cd82dc5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Thu, 27 Jun 2019 14:43:47 +0200 Subject: [PATCH] Refactor Takion Callbacks --- lib/include/chiaki/takion.h | 34 +++++++++++++++++++++++++--------- lib/src/senkusha.c | 24 ++++++++++++++++++------ lib/src/streamconnection.c | 34 ++++++++++++++++++++++------------ lib/src/takion.c | 26 ++++++++++++++++---------- 4 files changed, 81 insertions(+), 37 deletions(-) diff --git a/lib/include/chiaki/takion.h b/lib/include/chiaki/takion.h index 424fda2..0a600b7 100644 --- a/lib/include/chiaki/takion.h +++ b/lib/include/chiaki/takion.h @@ -69,18 +69,36 @@ typedef struct chiaki_takion_congestion_packet_t } ChiakiTakionCongestionPacket; -typedef void (*ChiakiTakionDataCallback)(ChiakiTakionMessageDataType, uint8_t *buf, size_t buf_size, void *user); -typedef void (*ChiakiTakionAVCallback)(ChiakiTakionAVPacket *packet, void *user); +typedef enum { + CHIAKI_TAKION_EVENT_TYPE_DATA, + CHIAKI_TAKION_EVENT_TYPE_AV +} ChiakiTakionEventType; +typedef struct chiaki_takion_event_t +{ + ChiakiTakionEventType type; + union + { + struct + { + ChiakiTakionMessageDataType data_type; + uint8_t *buf; + size_t buf_size; + } data; + + ChiakiTakionAVPacket *av; + }; +} ChiakiTakionEvent; + +typedef void (*ChiakiTakionCallback)(ChiakiTakionEvent *event, void *user); typedef struct chiaki_takion_connect_info_t { ChiakiLog *log; struct sockaddr *sa; socklen_t sa_len; - ChiakiTakionDataCallback data_cb; - void *data_cb_user; - ChiakiTakionAVCallback av_cb; + ChiakiTakionCallback cb; + void *cb_user; void *av_cb_user; bool enable_crypt; } ChiakiTakionConnectInfo; @@ -105,10 +123,8 @@ typedef struct chiaki_takion_t ChiakiGKCrypt *gkcrypt_remote; // if NULL (default), remote gmacs are IGNORED (!) and everything is expected to be unencrypted - ChiakiTakionDataCallback data_cb; - void *data_cb_user; - ChiakiTakionAVCallback av_cb; - void *av_cb_user; + ChiakiTakionCallback cb; + void *cb_user; int sock; ChiakiThread thread; ChiakiStopPipe stop_pipe; diff --git a/lib/src/senkusha.c b/lib/src/senkusha.c index 321abfb..fe6c6fd 100644 --- a/lib/src/senkusha.c +++ b/lib/src/senkusha.c @@ -45,7 +45,8 @@ typedef struct senkusha_t } Senkusha; -static void senkusha_takion_data(ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size, void *user); +static void senkusha_takion_cb(ChiakiTakionEvent *event, void *user); +static void senkusha_takion_data(Senkusha *senkusha, ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size); static ChiakiErrorCode senkusha_send_big(Senkusha *senkusha); static ChiakiErrorCode senkusha_send_disconnect(Senkusha *senkusha); @@ -70,8 +71,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session) err = set_port(takion_info.sa, htons(SENKUSHA_PORT)); assert(err == CHIAKI_ERR_SUCCESS); - takion_info.data_cb = senkusha_takion_data; - takion_info.data_cb_user = &senkusha; + takion_info.cb = senkusha_takion_cb; + takion_info.cb_user = &senkusha; err = chiaki_takion_connect(&senkusha.takion, &takion_info); free(takion_info.sa); @@ -121,13 +122,24 @@ error_bang_mirai: return err; } -static void senkusha_takion_data(ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size, void *user) +static void senkusha_takion_cb(ChiakiTakionEvent *event, void *user) +{ + Senkusha *senkusha = user; + switch(event->type) + { + case CHIAKI_TAKION_EVENT_TYPE_DATA: + senkusha_takion_data(senkusha, event->data.data_type, event->data.buf, event->data.buf_size); + break; + default: + break; + } +} + +static void senkusha_takion_data(Senkusha *senkusha, ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size) { if(data_type != CHIAKI_TAKION_MESSAGE_DATA_TYPE_PROTOBUF) return; - Senkusha *senkusha = user; - tkproto_TakionMessage msg; memset(&msg, 0, sizeof(msg)); diff --git a/lib/src/streamconnection.c b/lib/src/streamconnection.c index 0918ea2..72f6655 100644 --- a/lib/src/streamconnection.c +++ b/lib/src/streamconnection.c @@ -50,14 +50,15 @@ typedef enum { } StreamConnectionState; -static void stream_connection_takion_data(ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size, void *user); +static void stream_connection_takion_cb(ChiakiTakionEvent *event, void *user); +static void stream_connection_takion_data(ChiakiStreamConnection *stream_connection, ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size); static ChiakiErrorCode stream_connection_send_big(ChiakiStreamConnection *stream_connection); static ChiakiErrorCode stream_connection_send_disconnect(ChiakiStreamConnection *stream_connection); static void stream_connection_takion_data_idle(ChiakiStreamConnection *stream_connection, uint8_t *buf, size_t buf_size); static void stream_connection_takion_data_expect_bang(ChiakiStreamConnection *stream_connection, uint8_t *buf, size_t buf_size); static void stream_connection_takion_data_expect_streaminfo(ChiakiStreamConnection *stream_connection, uint8_t *buf, size_t buf_size); static ChiakiErrorCode stream_connection_send_streaminfo_ack(ChiakiStreamConnection *stream_connection); -static void stream_connection_takion_av(ChiakiTakionAVPacket *packet, void *user); +static void stream_connection_takion_av(ChiakiStreamConnection *stream_connection, ChiakiTakionAVPacket *packet); static ChiakiErrorCode stream_connection_send_heartbeat(ChiakiStreamConnection *stream_connection); @@ -124,10 +125,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiStreamConnectio err = set_port(takion_info.sa, htons(STREAM_CONNECTION_PORT)); assert(err == CHIAKI_ERR_SUCCESS); - takion_info.data_cb = stream_connection_takion_data; - takion_info.data_cb_user = stream_connection; - takion_info.av_cb = stream_connection_takion_av; - takion_info.av_cb_user = stream_connection; + takion_info.cb = stream_connection_takion_cb; + takion_info.cb_user = stream_connection; // TODO: make this call stoppable err = chiaki_takion_connect(&stream_connection->takion, &takion_info); @@ -251,14 +250,27 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_stop(ChiakiStreamConnecti return err == CHIAKI_ERR_SUCCESS ? unlock_err : err; } +static void stream_connection_takion_cb(ChiakiTakionEvent *event, void *user) +{ + ChiakiStreamConnection *stream_connection = user; + switch(event->type) + { + case CHIAKI_TAKION_EVENT_TYPE_DATA: + stream_connection_takion_data(stream_connection, event->data.data_type, event->data.buf, event->data.buf_size); + break; + case CHIAKI_TAKION_EVENT_TYPE_AV: + stream_connection_takion_av(stream_connection, event->av); + break; + default: + break; + } +} -static void stream_connection_takion_data(ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size, void *user) +static void stream_connection_takion_data(ChiakiStreamConnection *stream_connection, ChiakiTakionMessageDataType data_type, uint8_t *buf, size_t buf_size) { if(data_type != CHIAKI_TAKION_MESSAGE_DATA_TYPE_PROTOBUF) return; - ChiakiStreamConnection *stream_connection = user; - chiaki_mutex_lock(&stream_connection->state_mutex); switch(stream_connection->state) { @@ -627,10 +639,8 @@ static ChiakiErrorCode stream_connection_send_disconnect(ChiakiStreamConnection } -static void stream_connection_takion_av(ChiakiTakionAVPacket *packet, void *user) +static void stream_connection_takion_av(ChiakiStreamConnection *stream_connection, ChiakiTakionAVPacket *packet) { - ChiakiStreamConnection *stream_connection = user; - chiaki_gkcrypt_decrypt(stream_connection->gkcrypt_remote, packet->key_pos + CHIAKI_GKCRYPT_BLOCK_SIZE, packet->data, packet->data_size); /*CHIAKI_LOGD(stream_connection->log, "AV: index: %u,%u; b@0x1a: %d; is_video: %d; 0xa: %u; 0xc: %u; 0xe: %u; codec: %u; 0x18: %u; adaptive_stream: %u, 0x2c: %u\n", diff --git a/lib/src/takion.c b/lib/src/takion.c index 96882c8..d9d22e6 100644 --- a/lib/src/takion.c +++ b/lib/src/takion.c @@ -156,10 +156,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki return ret; takion->key_pos_local = 0; takion->gkcrypt_remote = NULL; - takion->data_cb = info->data_cb; - takion->data_cb_user = info->data_cb_user; - takion->av_cb = info->av_cb; - takion->av_cb_user = info->av_cb_user; + takion->cb = info->cb; + takion->cb_user = info->cb_user; takion->something = TAKION_LOCAL_SOMETHING; takion->tag_local = 0x4823; // "random" tag @@ -619,11 +617,14 @@ static void takion_handle_packet_message_data(ChiakiTakion *takion, uint8_t type if(data_type != CHIAKI_TAKION_MESSAGE_DATA_TYPE_PROTOBUF && data_type != CHIAKI_TAKION_MESSAGE_DATA_TYPE_9) CHIAKI_LOGW(takion->log, "Takion received data with unexpected data type %#x\n", data_type); - else if(takion->data_cb) + else if(takion->cb) { - uint8_t *data = buf + 9; - size_t data_size = buf_size - 9; - takion->data_cb((ChiakiTakionMessageDataType)data_type, data, data_size, takion->data_cb_user); + ChiakiTakionEvent event = { 0 }; + event.type = CHIAKI_TAKION_EVENT_TYPE_DATA; + event.data.data_type = (ChiakiTakionMessageDataType)data_type; + event.data.buf = buf + 9; + event.data.buf_size = buf_size - 9; + takion->cb(&event, takion->cb_user); } chiaki_takion_send_message_data_ack(takion, 0, channel, seq_num); @@ -843,8 +844,13 @@ static void takion_handle_packet_av(ChiakiTakion *takion, uint8_t base_type, uin return; } - if(takion->av_cb) - takion->av_cb(&packet, takion->av_cb_user); + if(takion->cb) + { + ChiakiTakionEvent event = { 0 }; + event.type = CHIAKI_TAKION_EVENT_TYPE_AV; + event.av = &packet; + takion->cb(&event, takion->cb_user); + } } #define AV_HEADER_SIZE_VIDEO 0x17