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.
This commit is contained in:
iphydf 2016-09-22 15:20:33 +01:00
parent 15cb426166
commit f60900c4b8
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
10 changed files with 135 additions and 103 deletions

View File

@ -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

View File

@ -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"

View File

@ -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
endif

View File

@ -25,6 +25,8 @@
#include "bwcontroller.h"
#include "ring_buffer.h"
#include "../toxcore/logger.h"
#include "../toxcore/util.h"

92
toxav/ring_buffer.c Normal file
View File

@ -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
toxav/ring_buffer.h Normal file
View File

@ -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 */

View File

@ -26,6 +26,7 @@
#include "video.h"
#include "msi.h"
#include "ring_buffer.h"
#include "rtp.h"
#include "../toxcore/logger.h"

View File

@ -39,6 +39,7 @@
#include <pthread.h>
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 */

View File

@ -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;
}

View File

@ -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__ */