Skip to content

Commit

Permalink
Move ring buffer out of toxcore/util into toxav.
Browse files Browse the repository at this point in the history
Toxcore itself doesn't use this data structure. Only toxav does, so now
toxav owns the code for it.
  • Loading branch information
iphydf committed Sep 24, 2016
1 parent b588e0f commit 311fbce
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 103 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions testing/av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion toxav/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,4 +41,4 @@ libtoxav_la_LIBADD = libtoxcore.la \
$(PTHREAD_LIBS) \
$(AV_LIBS)

endif
endif
2 changes: 2 additions & 0 deletions toxav/bwcontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "bwcontroller.h"

#include "ring_buffer.h"

#include "../toxcore/logger.h"
#include "../toxcore/util.h"

Expand Down
92 changes: 92 additions & 0 deletions toxav/ring_buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "ring_buffer.h"

#include <stdlib.h>

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;
}
18 changes: 18 additions & 0 deletions toxav/ring_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef RING_BUFFER_H
#define RING_BUFFER_H

#include <stdbool.h>
#include <stdint.h>

/* 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 */
1 change: 1 addition & 0 deletions toxav/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "video.h"

#include "msi.h"
#include "ring_buffer.h"
#include "rtp.h"

#include "../toxcore/logger.h"
Expand Down
3 changes: 2 additions & 1 deletion toxav/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <pthread.h>

struct RTPMessage;
struct RingBuffer;

typedef struct VCSession_s {
/* encoding */
Expand All @@ -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 */
Expand Down
90 changes: 0 additions & 90 deletions toxcore/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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;
}
11 changes: 0 additions & 11 deletions toxcore/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */

0 comments on commit 311fbce

Please sign in to comment.