From f60900c4b813abbce213db6de217837645c6590e Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 22 Sep 2016 15:20:33 +0100 Subject: [PATCH] Move ring buffer out of toxcore/util into toxav. Toxcore itself doesn't use this data structure. Only toxav does, so now toxav owns the code for it. --- CMakeLists.txt | 1 + testing/av_test.c | 16 ++++++++ toxav/Makefile.inc | 4 +- toxav/bwcontroller.c | 2 + toxav/ring_buffer.c | 92 ++++++++++++++++++++++++++++++++++++++++++++ toxav/ring_buffer.h | 18 +++++++++ toxav/video.c | 1 + toxav/video.h | 3 +- toxcore/util.c | 90 ------------------------------------------- toxcore/util.h | 11 ------ 10 files changed, 135 insertions(+), 103 deletions(-) create mode 100644 toxav/ring_buffer.c create mode 100644 toxav/ring_buffer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f5ee2770..58e3e673 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,6 +184,7 @@ if(BUILD_TOXAV) toxav/bwcontroller.c toxav/group.c toxav/msi.c + toxav/ring_buffer.c toxav/rtp.c toxav/toxav.c toxav/toxav_old.c diff --git a/testing/av_test.c b/testing/av_test.c index 02dc5f36..637fb9ac 100644 --- a/testing/av_test.c +++ b/testing/av_test.c @@ -22,6 +22,22 @@ * -lopencv_highgui -lopencv_imgproc -lsndfile -pthread -lvpx -lopus -lsodium -lportaudio */ +// XXX: Hack because toxav doesn't really expose ring_buffer, but this av test +// uses it. Not all of these functions are used, but when linking statically, +// not renaming them will cause multiple definition errors, so we need to rename +// all of them. +#define RingBuffer TestRingBuffer +#define rb_full test_rb_full +#define rb_empty test_rb_empty +#define rb_write test_rb_write +#define rb_read test_rb_read +#define rb_new test_rb_new +#define rb_kill test_rb_kill +#define rb_size test_rb_size +#define rb_data test_rb_data +#include "../toxav/ring_buffer.c" + +#include "../toxav/ring_buffer.h" #include "../toxav/toxav.h" #include "../toxcore/network.h" /* current_time_monotonic() */ #include "../toxcore/tox.h" diff --git a/toxav/Makefile.inc b/toxav/Makefile.inc index 377588d9..c22d0903 100644 --- a/toxav/Makefile.inc +++ b/toxav/Makefile.inc @@ -16,6 +16,8 @@ libtoxav_la_SOURCES = ../toxav/rtp.h \ ../toxav/video.c \ ../toxav/bwcontroller.h \ ../toxav/bwcontroller.c \ + ../toxav/ring_buffer.h \ + ../toxav/ring_buffer.c \ ../toxav/toxav.h \ ../toxav/toxav.c \ ../toxav/toxav_old.c @@ -39,4 +41,4 @@ libtoxav_la_LIBADD = libtoxcore.la \ $(PTHREAD_LIBS) \ $(AV_LIBS) -endif \ No newline at end of file +endif diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c index f5578465..48afba82 100644 --- a/toxav/bwcontroller.c +++ b/toxav/bwcontroller.c @@ -25,6 +25,8 @@ #include "bwcontroller.h" +#include "ring_buffer.h" + #include "../toxcore/logger.h" #include "../toxcore/util.h" diff --git a/toxav/ring_buffer.c b/toxav/ring_buffer.c new file mode 100644 index 00000000..cef3e943 --- /dev/null +++ b/toxav/ring_buffer.c @@ -0,0 +1,92 @@ +#include "ring_buffer.h" + +#include + +struct RingBuffer { + uint16_t size; /* Max size */ + uint16_t start; + uint16_t end; + void **data; +}; + +bool rb_full(const RingBuffer *b) +{ + return (b->end + 1) % b->size == b->start; +} +bool rb_empty(const RingBuffer *b) +{ + return b->end == b->start; +} +void *rb_write(RingBuffer *b, void *p) +{ + void *rc = NULL; + + if ((b->end + 1) % b->size == b->start) { /* full */ + rc = b->data[b->start]; + } + + b->data[b->end] = p; + b->end = (b->end + 1) % b->size; + + if (b->end == b->start) { + b->start = (b->start + 1) % b->size; + } + + return rc; +} +bool rb_read(RingBuffer *b, void **p) +{ + if (b->end == b->start) { /* Empty */ + *p = NULL; + return false; + } + + *p = b->data[b->start]; + b->start = (b->start + 1) % b->size; + return true; +} +RingBuffer *rb_new(int size) +{ + RingBuffer *buf = calloc(sizeof(RingBuffer), 1); + + if (!buf) { + return NULL; + } + + buf->size = size + 1; /* include empty elem */ + + if (!(buf->data = calloc(buf->size, sizeof(void *)))) { + free(buf); + return NULL; + } + + return buf; +} +void rb_kill(RingBuffer *b) +{ + if (b) { + free(b->data); + free(b); + } +} +uint16_t rb_size(const RingBuffer *b) +{ + if (rb_empty(b)) { + return 0; + } + + return + b->end > b->start ? + b->end - b->start : + (b->size - b->start) + b->end; +} +uint16_t rb_data(const RingBuffer *b, void **dest) +{ + uint16_t i = 0; + + for (; i < rb_size(b); i++) { + dest[i] = b->data[(b->start + i) % b->size]; + } + + return i; +} diff --git a/toxav/ring_buffer.h b/toxav/ring_buffer.h new file mode 100644 index 00000000..42b4479d --- /dev/null +++ b/toxav/ring_buffer.h @@ -0,0 +1,18 @@ +#ifndef RING_BUFFER_H +#define RING_BUFFER_H + +#include +#include + +/* Ring buffer */ +typedef struct RingBuffer RingBuffer; +bool rb_full(const RingBuffer *b); +bool rb_empty(const RingBuffer *b); +void *rb_write(RingBuffer *b, void *p); +bool rb_read(RingBuffer *b, void **p); +RingBuffer *rb_new(int size); +void rb_kill(RingBuffer *b); +uint16_t rb_size(const RingBuffer *b); +uint16_t rb_data(const RingBuffer *b, void **dest); + +#endif /* RING_BUFFER_H */ diff --git a/toxav/video.c b/toxav/video.c index 5bb3b8ae..93ebbb16 100644 --- a/toxav/video.c +++ b/toxav/video.c @@ -26,6 +26,7 @@ #include "video.h" #include "msi.h" +#include "ring_buffer.h" #include "rtp.h" #include "../toxcore/logger.h" diff --git a/toxav/video.h b/toxav/video.h index 6bc9ae5c..b82197c6 100644 --- a/toxav/video.h +++ b/toxav/video.h @@ -39,6 +39,7 @@ #include struct RTPMessage; +struct RingBuffer; typedef struct VCSession_s { /* encoding */ @@ -47,7 +48,7 @@ typedef struct VCSession_s { /* decoding */ vpx_codec_ctx_t decoder[1]; - RingBuffer *vbuf_raw; /* Un-decoded data */ + struct RingBuffer *vbuf_raw; /* Un-decoded data */ uint64_t linfts; /* Last received frame time stamp */ uint32_t lcfd; /* Last calculated frame duration for incoming video payload */ diff --git a/toxcore/util.c b/toxcore/util.c index 93a3d436..25068db6 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -193,93 +193,3 @@ int create_recursive_mutex(pthread_mutex_t *mutex) return 0; } - - -struct RingBuffer { - uint16_t size; /* Max size */ - uint16_t start; - uint16_t end; - void **data; -}; - -bool rb_full(const RingBuffer *b) -{ - return (b->end + 1) % b->size == b->start; -} -bool rb_empty(const RingBuffer *b) -{ - return b->end == b->start; -} -void *rb_write(RingBuffer *b, void *p) -{ - void *rc = NULL; - - if ((b->end + 1) % b->size == b->start) { /* full */ - rc = b->data[b->start]; - } - - b->data[b->end] = p; - b->end = (b->end + 1) % b->size; - - if (b->end == b->start) { - b->start = (b->start + 1) % b->size; - } - - return rc; -} -bool rb_read(RingBuffer *b, void **p) -{ - if (b->end == b->start) { /* Empty */ - *p = NULL; - return false; - } - - *p = b->data[b->start]; - b->start = (b->start + 1) % b->size; - return true; -} -RingBuffer *rb_new(int size) -{ - RingBuffer *buf = (RingBuffer *)calloc(sizeof(RingBuffer), 1); - - if (!buf) { - return NULL; - } - - buf->size = size + 1; /* include empty elem */ - - if (!(buf->data = (void **)calloc(buf->size, sizeof(void *)))) { - free(buf); - return NULL; - } - - return buf; -} -void rb_kill(RingBuffer *b) -{ - if (b) { - free(b->data); - free(b); - } -} -uint16_t rb_size(const RingBuffer *b) -{ - if (rb_empty(b)) { - return 0; - } - - return - b->end > b->start ? - b->end - b->start : - (b->size - b->start) + b->end; -} -uint16_t rb_data(const RingBuffer *b, void **dest) -{ - uint16_t i = 0; - - for (; i < rb_size(b); i++) { - dest[i] = b->data[(b->start + i) % b->size]; - } - - return i; -} diff --git a/toxcore/util.h b/toxcore/util.h index 840f0a3e..20469b75 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -58,15 +58,4 @@ int load_state(load_state_callback_func load_state_callback, void *outer, /* Returns -1 if failed or 0 if success */ int create_recursive_mutex(pthread_mutex_t *mutex); -/* Ring buffer */ -typedef struct RingBuffer RingBuffer; -bool rb_full(const RingBuffer *b); -bool rb_empty(const RingBuffer *b); -void *rb_write(RingBuffer *b, void *p); -bool rb_read(RingBuffer *b, void **p); -RingBuffer *rb_new(int size); -void rb_kill(RingBuffer *b); -uint16_t rb_size(const RingBuffer *b); -uint16_t rb_data(const RingBuffer *b, void **dest); - #endif /* __UTIL_H__ */