device: allow to use dummy as a way to test streamer
This adds loopback tests via `tests/`
This commit is contained in:
@ -1,11 +1,18 @@
|
||||
#include "dummy.h"
|
||||
#include "device/buffer.h"
|
||||
#include "device/buffer_list.h"
|
||||
#include "util/opts/log.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int dummy_buffer_open(buffer_t *buf)
|
||||
{
|
||||
buf->dummy = calloc(1, sizeof(buffer_dummy_t));
|
||||
buf->start = buf->buf_list->dummy->data;
|
||||
buf->used = buf->buf_list->dummy->length;
|
||||
buf->length = buf->buf_list->dummy->length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -16,15 +23,39 @@ void dummy_buffer_close(buffer_t *buf)
|
||||
|
||||
int dummy_buffer_enqueue(buffer_t *buf, const char *who)
|
||||
{
|
||||
return -1;
|
||||
unsigned index = buf->index;
|
||||
if (write(buf->buf_list->dummy->fds[1], &index, sizeof(index)) != sizeof(index)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dummy_buffer_list_dequeue(buffer_list_t *buf_list, buffer_t **bufp)
|
||||
{
|
||||
return -1;
|
||||
unsigned index = 0;
|
||||
int n = read(buf_list->dummy->fds[0], &index, sizeof(index));
|
||||
if (n != sizeof(index)) {
|
||||
LOG_INFO(buf_list, "Received invalid result from `read`: %d", n);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (index >= (unsigned)buf_list->nbufs) {
|
||||
LOG_INFO(buf_list, "Received invalid index from `read`: %d >= %d", index, buf_list->nbufs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*bufp = buf_list->bufs[index];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dummy_buffer_list_pollfd(buffer_list_t *buf_list, struct pollfd *pollfd, bool can_dequeue)
|
||||
{
|
||||
return -1;
|
||||
int count_enqueued = buffer_list_count_enqueued(buf_list);
|
||||
pollfd->fd = buf_list->dummy->fds[0]; // write end
|
||||
pollfd->events = POLLHUP;
|
||||
if (can_dequeue && count_enqueued > 0) {
|
||||
pollfd->events |= POLLIN;
|
||||
}
|
||||
pollfd->revents = 0;
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,25 +1,69 @@
|
||||
#include "dummy.h"
|
||||
#include "device/buffer_list.h"
|
||||
#include "device/device.h"
|
||||
#include "util/opts/log.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int dummy_buffer_list_open(buffer_list_t *buf_list)
|
||||
{
|
||||
int fd = -1;
|
||||
|
||||
buf_list->dummy = calloc(1, sizeof(buffer_list_dummy_t));
|
||||
return 0;
|
||||
buf_list->dummy->fds[0] = -1;
|
||||
buf_list->dummy->fds[1] = -1;
|
||||
|
||||
if (!buf_list->do_capture) {
|
||||
LOG_ERROR(buf_list, "Only capture mode supported");
|
||||
}
|
||||
|
||||
if (pipe2(buf_list->dummy->fds, O_DIRECT|O_CLOEXEC) < 0) {
|
||||
LOG_INFO(buf_list, "Cannot open `pipe2`.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd = open(buf_list->dev->path, O_RDWR|O_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
LOG_ERROR(buf_list, "Can't open device: %s", buf_list->dev->path);
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0) {
|
||||
LOG_ERROR(buf_list, "Can't get fstat: %s", buf_list->dev->path);
|
||||
}
|
||||
|
||||
buf_list->dummy->data = malloc(st.st_size);
|
||||
if (!buf_list->dummy->data) {
|
||||
LOG_ERROR(buf_list, "Can't allocate %ld bytes for %s", st.st_size, buf_list->dev->path);
|
||||
}
|
||||
|
||||
buf_list->dummy->length = read(fd, buf_list->dummy->data, st.st_size);
|
||||
if (!buf_list->dummy->data) {
|
||||
LOG_ERROR(buf_list, "Can't read %ld bytes for %s. Only read %zu.", st.st_size, buf_list->dev->path, buf_list->dummy->length);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return buf_list->fmt.nbufs;
|
||||
|
||||
error:
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void dummy_buffer_list_close(buffer_list_t *buf_list)
|
||||
{
|
||||
free(buf_list->dummy);
|
||||
}
|
||||
if (buf_list->dummy) {
|
||||
close(buf_list->dummy->fds[0]);
|
||||
close(buf_list->dummy->fds[1]);
|
||||
free(buf_list->dummy->data);
|
||||
}
|
||||
|
||||
int dummy_buffer_list_set_buffers(buffer_list_t *buf_list, int nbufs)
|
||||
{
|
||||
return -1;
|
||||
free(buf_list->dummy);
|
||||
}
|
||||
|
||||
int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on)
|
||||
{
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
int dummy_device_open(device_t *dev)
|
||||
{
|
||||
dev->opts.allow_dma = false;
|
||||
dev->dummy = calloc(1, sizeof(device_dummy_t));
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct buffer_s buffer_t;
|
||||
typedef struct buffer_list_s buffer_list_t;
|
||||
@ -12,6 +13,9 @@ typedef struct device_dummy_s {
|
||||
} device_dummy_t;
|
||||
|
||||
typedef struct buffer_list_dummy_s {
|
||||
int fds[2];
|
||||
void *data;
|
||||
size_t length;
|
||||
} buffer_list_dummy_t;
|
||||
|
||||
typedef struct buffer_dummy_s {
|
||||
@ -32,5 +36,4 @@ int dummy_buffer_list_pollfd(buffer_list_t *buf_list, struct pollfd *pollfd, boo
|
||||
|
||||
int dummy_buffer_list_open(buffer_list_t *buf_list);
|
||||
void dummy_buffer_list_close(buffer_list_t *buf_list);
|
||||
int dummy_buffer_list_set_buffers(buffer_list_t *buf_list, int nbufs);
|
||||
int dummy_buffer_list_set_stream(buffer_list_t *buf_list, bool do_on);
|
||||
|
Reference in New Issue
Block a user