summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-09-22 15:20:33 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-09-24 23:36:50 +0100
commitf60900c4b813abbce213db6de217837645c6590e (patch)
tree02e9223f1627f1c647b42fa3f623b7354b6c6331
parent15cb4261665bab4ef02a5b1b9db48b9477c9b87a (diff)
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.
-rw-r--r--CMakeLists.txt1
-rw-r--r--testing/av_test.c16
-rw-r--r--toxav/Makefile.inc4
-rw-r--r--toxav/bwcontroller.c2
-rw-r--r--toxav/ring_buffer.c92
-rw-r--r--toxav/ring_buffer.h18
-rw-r--r--toxav/video.c1
-rw-r--r--toxav/video.h3
-rw-r--r--toxcore/util.c90
-rw-r--r--toxcore/util.h11
10 files changed, 135 insertions, 103 deletions
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)
184 toxav/bwcontroller.c 184 toxav/bwcontroller.c
185 toxav/group.c 185 toxav/group.c
186 toxav/msi.c 186 toxav/msi.c
187 toxav/ring_buffer.c
187 toxav/rtp.c 188 toxav/rtp.c
188 toxav/toxav.c 189 toxav/toxav.c
189 toxav/toxav_old.c 190 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 @@
22 * -lopencv_highgui -lopencv_imgproc -lsndfile -pthread -lvpx -lopus -lsodium -lportaudio 22 * -lopencv_highgui -lopencv_imgproc -lsndfile -pthread -lvpx -lopus -lsodium -lportaudio
23 */ 23 */
24 24
25// XXX: Hack because toxav doesn't really expose ring_buffer, but this av test
26// uses it. Not all of these functions are used, but when linking statically,
27// not renaming them will cause multiple definition errors, so we need to rename
28// all of them.
29#define RingBuffer TestRingBuffer
30#define rb_full test_rb_full
31#define rb_empty test_rb_empty
32#define rb_write test_rb_write
33#define rb_read test_rb_read
34#define rb_new test_rb_new
35#define rb_kill test_rb_kill
36#define rb_size test_rb_size
37#define rb_data test_rb_data
38#include "../toxav/ring_buffer.c"
39
40#include "../toxav/ring_buffer.h"
25#include "../toxav/toxav.h" 41#include "../toxav/toxav.h"
26#include "../toxcore/network.h" /* current_time_monotonic() */ 42#include "../toxcore/network.h" /* current_time_monotonic() */
27#include "../toxcore/tox.h" 43#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 \
16 ../toxav/video.c \ 16 ../toxav/video.c \
17 ../toxav/bwcontroller.h \ 17 ../toxav/bwcontroller.h \
18 ../toxav/bwcontroller.c \ 18 ../toxav/bwcontroller.c \
19 ../toxav/ring_buffer.h \
20 ../toxav/ring_buffer.c \
19 ../toxav/toxav.h \ 21 ../toxav/toxav.h \
20 ../toxav/toxav.c \ 22 ../toxav/toxav.c \
21 ../toxav/toxav_old.c 23 ../toxav/toxav_old.c
@@ -39,4 +41,4 @@ libtoxav_la_LIBADD = libtoxcore.la \
39 $(PTHREAD_LIBS) \ 41 $(PTHREAD_LIBS) \
40 $(AV_LIBS) 42 $(AV_LIBS)
41 43
42endif \ No newline at end of file 44endif
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 @@
25 25
26#include "bwcontroller.h" 26#include "bwcontroller.h"
27 27
28#include "ring_buffer.h"
29
28#include "../toxcore/logger.h" 30#include "../toxcore/logger.h"
29#include "../toxcore/util.h" 31#include "../toxcore/util.h"
30 32
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 @@
1#include "ring_buffer.h"
2
3#include <stdlib.h>
4
5struct RingBuffer {
6 uint16_t size; /* Max size */
7 uint16_t start;
8 uint16_t end;
9 void **data;
10};
11
12bool rb_full(const RingBuffer *b)
13{
14 return (b->end + 1) % b->size == b->start;
15}
16bool rb_empty(const RingBuffer *b)
17{
18 return b->end == b->start;
19}
20void *rb_write(RingBuffer *b, void *p)
21{
22 void *rc = NULL;
23
24 if ((b->end + 1) % b->size == b->start) { /* full */
25 rc = b->data[b->start];
26 }
27
28 b->data[b->end] = p;
29 b->end = (b->end + 1) % b->size;
30
31 if (b->end == b->start) {
32 b->start = (b->start + 1) % b->size;
33 }
34
35 return rc;
36}
37bool rb_read(RingBuffer *b, void **p)
38{
39 if (b->end == b->start) { /* Empty */
40 *p = NULL;
41 return false;
42 }
43
44 *p = b->data[b->start];
45 b->start = (b->start + 1) % b->size;
46 return true;
47}
48RingBuffer *rb_new(int size)
49{
50 RingBuffer *buf = calloc(sizeof(RingBuffer), 1);
51
52 if (!buf) {
53 return NULL;
54 }
55
56 buf->size = size + 1; /* include empty elem */
57
58 if (!(buf->data = calloc(buf->size, sizeof(void *)))) {
59 free(buf);
60 return NULL;
61 }
62
63 return buf;
64}
65void rb_kill(RingBuffer *b)
66{
67 if (b) {
68 free(b->data);
69 free(b);
70 }
71}
72uint16_t rb_size(const RingBuffer *b)
73{
74 if (rb_empty(b)) {
75 return 0;
76 }
77
78 return
79 b->end > b->start ?
80 b->end - b->start :
81 (b->size - b->start) + b->end;
82}
83uint16_t rb_data(const RingBuffer *b, void **dest)
84{
85 uint16_t i = 0;
86
87 for (; i < rb_size(b); i++) {
88 dest[i] = b->data[(b->start + i) % b->size];
89 }
90
91 return i;
92}
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 @@
1#ifndef RING_BUFFER_H
2#define RING_BUFFER_H
3
4#include <stdbool.h>
5#include <stdint.h>
6
7/* Ring buffer */
8typedef struct RingBuffer RingBuffer;
9bool rb_full(const RingBuffer *b);
10bool rb_empty(const RingBuffer *b);
11void *rb_write(RingBuffer *b, void *p);
12bool rb_read(RingBuffer *b, void **p);
13RingBuffer *rb_new(int size);
14void rb_kill(RingBuffer *b);
15uint16_t rb_size(const RingBuffer *b);
16uint16_t rb_data(const RingBuffer *b, void **dest);
17
18#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 @@
26#include "video.h" 26#include "video.h"
27 27
28#include "msi.h" 28#include "msi.h"
29#include "ring_buffer.h"
29#include "rtp.h" 30#include "rtp.h"
30 31
31#include "../toxcore/logger.h" 32#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 @@
39#include <pthread.h> 39#include <pthread.h>
40 40
41struct RTPMessage; 41struct RTPMessage;
42struct RingBuffer;
42 43
43typedef struct VCSession_s { 44typedef struct VCSession_s {
44 /* encoding */ 45 /* encoding */
@@ -47,7 +48,7 @@ typedef struct VCSession_s {
47 48
48 /* decoding */ 49 /* decoding */
49 vpx_codec_ctx_t decoder[1]; 50 vpx_codec_ctx_t decoder[1];
50 RingBuffer *vbuf_raw; /* Un-decoded data */ 51 struct RingBuffer *vbuf_raw; /* Un-decoded data */
51 52
52 uint64_t linfts; /* Last received frame time stamp */ 53 uint64_t linfts; /* Last received frame time stamp */
53 uint32_t lcfd; /* Last calculated frame duration for incoming video payload */ 54 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)
193 193
194 return 0; 194 return 0;
195} 195}
196
197
198struct RingBuffer {
199 uint16_t size; /* Max size */
200 uint16_t start;
201 uint16_t end;
202 void **data;
203};
204
205bool rb_full(const RingBuffer *b)
206{
207 return (b->end + 1) % b->size == b->start;
208}
209bool rb_empty(const RingBuffer *b)
210{
211 return b->end == b->start;
212}
213void *rb_write(RingBuffer *b, void *p)
214{
215 void *rc = NULL;
216
217 if ((b->end + 1) % b->size == b->start) { /* full */
218 rc = b->data[b->start];
219 }
220
221 b->data[b->end] = p;
222 b->end = (b->end + 1) % b->size;
223
224 if (b->end == b->start) {
225 b->start = (b->start + 1) % b->size;
226 }
227
228 return rc;
229}
230bool rb_read(RingBuffer *b, void **p)
231{
232 if (b->end == b->start) { /* Empty */
233 *p = NULL;
234 return false;
235 }
236
237 *p = b->data[b->start];
238 b->start = (b->start + 1) % b->size;
239 return true;
240}
241RingBuffer *rb_new(int size)
242{
243 RingBuffer *buf = (RingBuffer *)calloc(sizeof(RingBuffer), 1);
244
245 if (!buf) {
246 return NULL;
247 }
248
249 buf->size = size + 1; /* include empty elem */
250
251 if (!(buf->data = (void **)calloc(buf->size, sizeof(void *)))) {
252 free(buf);
253 return NULL;
254 }
255
256 return buf;
257}
258void rb_kill(RingBuffer *b)
259{
260 if (b) {
261 free(b->data);
262 free(b);
263 }
264}
265uint16_t rb_size(const RingBuffer *b)
266{
267 if (rb_empty(b)) {
268 return 0;
269 }
270
271 return
272 b->end > b->start ?
273 b->end - b->start :
274 (b->size - b->start) + b->end;
275}
276uint16_t rb_data(const RingBuffer *b, void **dest)
277{
278 uint16_t i = 0;
279
280 for (; i < rb_size(b); i++) {
281 dest[i] = b->data[(b->start + i) % b->size];
282 }
283
284 return i;
285}
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,
58/* Returns -1 if failed or 0 if success */ 58/* Returns -1 if failed or 0 if success */
59int create_recursive_mutex(pthread_mutex_t *mutex); 59int create_recursive_mutex(pthread_mutex_t *mutex);
60 60
61/* Ring buffer */
62typedef struct RingBuffer RingBuffer;
63bool rb_full(const RingBuffer *b);
64bool rb_empty(const RingBuffer *b);
65void *rb_write(RingBuffer *b, void *p);
66bool rb_read(RingBuffer *b, void **p);
67RingBuffer *rb_new(int size);
68void rb_kill(RingBuffer *b);
69uint16_t rb_size(const RingBuffer *b);
70uint16_t rb_data(const RingBuffer *b, void **dest);
71
72#endif /* __UTIL_H__ */ 61#endif /* __UTIL_H__ */