From 9a96bb9a5bb414afb137c094f987740581ecebda Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 19 Jul 2018 21:47:51 +0000 Subject: [PATCH] Reduce nesting by doing more early returns on error. This almost entirely avoids any else-after-return in toxcore. One case is left, and that one is more readable this way. Why no else after return: https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return Why exemptions exist: https://blog.mozilla.org/nnethercote/2009/08/31/no-else-after-return-considered-harmful/ --- toxav/video.c | 81 ++++++++++++++++++++++++------------------------- toxcore/group.c | 12 +++----- 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/toxav/video.c b/toxav/video.c index 5145c29c..17969003 100644 --- a/toxav/video.c +++ b/toxav/video.c @@ -299,55 +299,52 @@ void vc_iterate(VCSession *vc) return; } - struct RTPMessage *p; - - vpx_codec_err_t rc; - pthread_mutex_lock(vc->queue_mutex); - uint32_t full_data_len; + struct RTPMessage *p; - if (rb_read((RingBuffer *)vc->vbuf_raw, (void **)&p)) { - pthread_mutex_unlock(vc->queue_mutex); - const struct RTPHeader *const header = &p->header; - - if (header->flags & RTP_LARGE_FRAME) { - full_data_len = header->data_length_full; - LOGGER_DEBUG(vc->log, "vc_iterate:001:full_data_len=%d", (int)full_data_len); - } else { - full_data_len = p->len; - LOGGER_DEBUG(vc->log, "vc_iterate:002"); - } - - LOGGER_DEBUG(vc->log, "vc_iterate: rb_read p->len=%d p->header.xe=%d", (int)full_data_len, p->header.xe); - LOGGER_DEBUG(vc->log, "vc_iterate: rb_read rb size=%d", (int)rb_size((RingBuffer *)vc->vbuf_raw)); - rc = vpx_codec_decode(vc->decoder, p->data, full_data_len, nullptr, MAX_DECODE_TIME_US); - free(p); - - if (rc != VPX_CODEC_OK) { - LOGGER_ERROR(vc->log, "Error decoding video: %d %s", (int)rc, vpx_codec_err_to_string(rc)); - } else { - /* Play decoded images */ - vpx_codec_iter_t iter = nullptr; - vpx_image_t *dest = nullptr; - - while ((dest = vpx_codec_get_frame(vc->decoder, &iter)) != nullptr) { - if (vc->vcb.first) { - vc->vcb.first(vc->av, vc->friend_number, dest->d_w, dest->d_h, - (const uint8_t *)dest->planes[0], (const uint8_t *)dest->planes[1], (const uint8_t *)dest->planes[2], - dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb.second); - } - - vpx_img_free(dest); // is this needed? none of the VPx examples show that - } - } - - return; - } else { + if (!rb_read((RingBuffer *)vc->vbuf_raw, (void **)&p)) { LOGGER_TRACE(vc->log, "no Video frame data available"); + pthread_mutex_unlock(vc->queue_mutex); + return; } pthread_mutex_unlock(vc->queue_mutex); + const struct RTPHeader *const header = &p->header; + + uint32_t full_data_len; + + if (header->flags & RTP_LARGE_FRAME) { + full_data_len = header->data_length_full; + LOGGER_DEBUG(vc->log, "vc_iterate:001:full_data_len=%d", (int)full_data_len); + } else { + full_data_len = p->len; + LOGGER_DEBUG(vc->log, "vc_iterate:002"); + } + + LOGGER_DEBUG(vc->log, "vc_iterate: rb_read p->len=%d p->header.xe=%d", (int)full_data_len, p->header.xe); + LOGGER_DEBUG(vc->log, "vc_iterate: rb_read rb size=%d", (int)rb_size((RingBuffer *)vc->vbuf_raw)); + const vpx_codec_err_t rc = vpx_codec_decode(vc->decoder, p->data, full_data_len, nullptr, MAX_DECODE_TIME_US); + free(p); + + if (rc != VPX_CODEC_OK) { + LOGGER_ERROR(vc->log, "Error decoding video: %d %s", (int)rc, vpx_codec_err_to_string(rc)); + return; + } + + /* Play decoded images */ + vpx_codec_iter_t iter = nullptr; + vpx_image_t *dest = nullptr; + + while ((dest = vpx_codec_get_frame(vc->decoder, &iter)) != nullptr) { + if (vc->vcb.first) { + vc->vcb.first(vc->av, vc->friend_number, dest->d_w, dest->d_h, + (const uint8_t *)dest->planes[0], (const uint8_t *)dest->planes[1], (const uint8_t *)dest->planes[2], + dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb.second); + } + + vpx_img_free(dest); // is this needed? none of the VPx examples show that + } } int vc_queue_message(void *vcp, struct RTPMessage *msg) diff --git a/toxcore/group.c b/toxcore/group.c index 95de1afb..826ea239 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -2270,12 +2270,9 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; - - return 0; } else { // top_distance < MAX_LOSSY_COUNT - unsigned int i; - - for (i = g->group[peer_index].bottom_lossy_number; i != (g->group[peer_index].bottom_lossy_number + top_distance); + for (unsigned int i = g->group[peer_index].bottom_lossy_number; + i != g->group[peer_index].bottom_lossy_number + top_distance; ++i) { g->group[peer_index].recv_lossy[i % MAX_LOSSY_COUNT] = 0; } @@ -2283,9 +2280,10 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; - - return 0; } + + return 0; + } static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)