Commit f68caac8 authored by Titouan Soulard's avatar Titouan Soulard

libtrx: use common circular buffer implementation

parent 4763fd3c
......@@ -34,8 +34,8 @@ out/libpotoml.so: common/hashtable.lo libpotoml/toml.lo
out/libtrx_rdma.so: libtrx/trx_rdma.lo out/libcapulet.so
gcc -shared -fPIC -DPIC -o $@ $< $(LIB_FLAGS) -lcapulet
out/libtrx_play.so: libtrx/trx_play.lo
gcc -shared -fPIC -DPIC -o $@ $< $(LIB_FLAGS)
out/libtrx_play.so: common/circular_buffer.lo libtrx/trx_play.lo
gcc -shared -fPIC -DPIC -o $@ $^ $(LIB_FLAGS)
out/rdma_standalone: example/rdma_standalone.o out/libcapulet.so
gcc $(CFLAGS) -o $@ $< $(LIB_FLAGS) -lcapulet
......
......@@ -5,18 +5,55 @@
#include <unistd.h>
#include "amarisoft/trx_driver.h"
#include "common/circular_buffer.h"
#define SAMPLE_RATE_KHZ 30720
#define COMPLEX_SIZE (sizeof(TRXComplex) / sizeof(char))
struct TRXPlayContext {
TRXComplex *samples;
uint64_t sample_count;
uint64_t current_sample;
struct CommonCBBuffer *samples_buffer;
trx_timestamp_t last_timestamp;
char *samples_file_path;
};
int trx_play_start(TRXState *s, const TRXDriverParams2 *p) {
struct TRXPlayContext *trx_play_ctx;
FILE *samples_file_handle;
size_t result;
uint32_t samples_file_size;
char *samples_file_content;
trx_play_ctx = s->opaque;
samples_file_handle = fopen(trx_play_ctx->samples_file_path, "r");
if(!samples_file_handle) {
fprintf(stderr, "trx_play: file %s does not exist\n", trx_play_ctx->samples_file_path);
return -1;
}
// Allocate the right size
fseek(samples_file_handle, 0, SEEK_END);
samples_file_size = ftell(samples_file_handle);
rewind(samples_file_handle);
samples_file_content = malloc(samples_file_size);
result = fread(samples_file_content, samples_file_size, 1, samples_file_handle);
if(result != 1) {
fprintf(stderr, "trx_play: could not read samples from file\n");
return -1;
}
// Use an infinite circular buffer with same length as the file
trx_play_ctx->samples_buffer = common_circular_buffer_create(samples_file_size, true);
common_circular_buffer_write(trx_play_ctx->samples_buffer, (void *) samples_file_content, samples_file_size);
printf("trx_play: looping %lu frames from %s\n", samples_file_size / COMPLEX_SIZE, trx_play_ctx->samples_file_path);
fclose(samples_file_handle);
free(trx_play_ctx->samples_file_path);
return 0;
}
......@@ -26,38 +63,15 @@ void trx_play_write(TRXState *s, trx_timestamp_t timestamp, const void **samples
int trx_play_read(TRXState *s, trx_timestamp_t *ptimestamp, void **psamples, int count, int rx_port_index, TRXReadMetadata *md) {
struct TRXPlayContext *trx_play_ctx;
uint64_t extra_samples;
uint64_t copy_samples;
TRXComplex **channels;
trx_play_ctx = s->opaque;
channels = (TRXComplex **) psamples;
// Restrict count to buffer length
if(count > trx_play_ctx->sample_count) count = trx_play_ctx->sample_count;
// Handle reading after end of buffer
if(trx_play_ctx->current_sample + count >= trx_play_ctx->sample_count) {
extra_samples = trx_play_ctx->current_sample + count - trx_play_ctx->sample_count;
copy_samples = count - extra_samples;
memcpy(channels[0], trx_play_ctx->samples + trx_play_ctx->current_sample, copy_samples * sizeof(TRXComplex));
memcpy(channels[1], trx_play_ctx->samples + trx_play_ctx->current_sample, copy_samples * sizeof(TRXComplex));
if(extra_samples > 0) {
memcpy(channels[0] + copy_samples, trx_play_ctx->samples, extra_samples * sizeof(TRXComplex));
memcpy(channels[1] + copy_samples, trx_play_ctx->samples, extra_samples * sizeof(TRXComplex));
}
trx_play_ctx->current_sample = extra_samples;
} else {
memcpy(channels[0], trx_play_ctx->samples + trx_play_ctx->current_sample, count * sizeof(TRXComplex));
memcpy(channels[1], trx_play_ctx->samples + trx_play_ctx->current_sample, count * sizeof(TRXComplex));
trx_play_ctx->current_sample += count;
}
// Ignoring result: reading an infinite buffer will always work
common_circular_buffer_read(trx_play_ctx->samples_buffer, (void *) channels[0], count * sizeof(TRXComplex));
memcpy((void *) channels[1], (void *) channels[0], count * sizeof(TRXComplex));
trx_play_ctx->last_timestamp += count;
*ptimestamp = trx_play_ctx->last_timestamp;
......@@ -70,63 +84,30 @@ static void trx_play_end(TRXState *s) {
trx_play_ctx = s->opaque;
free(trx_play_ctx->samples);
common_circular_buffer_free(trx_play_ctx->samples_buffer);
free(trx_play_ctx);
}
int trx_driver_init(TRXState *s) {
struct TRXPlayContext *trx_play_ctx = malloc(sizeof(struct TRXPlayContext));
FILE *samples_file_handle;
size_t result;
uint32_t sample_count;
char *samples_file_path;
char *samples_file_content;
if(s->trx_api_version != TRX_API_VERSION) {
fprintf(stderr, "trx_play: ABI compatibility mismatch between LTEENB and TRX driver (LTEENB ABI version=%d, TRX driver ABI version=%d)\n", s->trx_api_version, TRX_API_VERSION);
return -1;
}
samples_file_path = trx_get_param_string(s, "file_path");
if(!samples_file_path) {
trx_play_ctx->samples_file_path = trx_get_param_string(s, "file_path");
if(!trx_play_ctx->samples_file_path) {
fprintf(stderr, "trx_play: missing `file_path` parameter\n");
return -1;
}
samples_file_handle = fopen(samples_file_path, "r");
if(!samples_file_handle) {
fprintf(stderr, "trx_play: file %s does not exist\n", samples_file_path);
return -1;
}
// Allocate the right size
fseek(samples_file_handle, 0, SEEK_END);
sample_count = ftell(samples_file_handle) / COMPLEX_SIZE;
rewind(samples_file_handle);
samples_file_content = malloc(sample_count * sizeof(TRXComplex));
result = fread(samples_file_content, sample_count * sizeof(TRXComplex), 1, samples_file_handle);
if(result != 1) {
fprintf(stderr, "trx_play: could not read samples from file\n");
return -1;
}
trx_play_ctx->samples = (TRXComplex *) samples_file_content;
trx_play_ctx->sample_count = sample_count;
trx_play_ctx->current_sample = 0;
trx_play_ctx->last_timestamp = 0;
printf("trx_play: looping %u frames from %s\n", sample_count, samples_file_path);
s->opaque = trx_play_ctx;
s->trx_start_func2 = trx_play_start;
s->trx_write_func2 = trx_play_write;
s->trx_read_func2 = trx_play_read;
s->trx_end_func = trx_play_end;
fclose(samples_file_handle);
free(samples_file_path);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment