From 5cd610a07468137066ea4daa5176c3e7045113b0 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Wed, 5 Aug 2020 03:38:22 +0200 Subject: ecp moved to root; fixed utils and tests --- ecp/test/Makefile | 51 +++++ ecp/test/basic.c | 110 ++++++++++ ecp/test/client.c | 80 +++++++ ecp/test/echo.c | 56 +++++ ecp/test/frag.c | 122 +++++++++++ ecp/test/init.c | 36 ++++ ecp/test/init_vconn.c | 42 ++++ ecp/test/server.c | 59 +++++ ecp/test/stress.c | 256 ++++++++++++++++++++++ ecp/test/vc_client.c | 90 ++++++++ ecp/test/vc_client_t.c | 118 ++++++++++ ecp/test/vc_server.c | 78 +++++++ ecp/test/vcs.c | 54 +++++ ecp/test/vid/Makefile | 23 ++ ecp/test/vid/cap.c | 576 +++++++++++++++++++++++++++++++++++++++++++++++++ ecp/test/vid/cap.sh | 3 + ecp/test/vid/client.c | 123 +++++++++++ ecp/test/vid/display.c | 68 ++++++ ecp/test/vid/display.h | 17 ++ ecp/test/vid/enc.c | 168 +++++++++++++++ ecp/test/vid/enc.h | 7 + ecp/test/vid/server.c | 76 +++++++ ecp/test/vid/server.h | 7 + ecp/test/vid/tools.c | 165 ++++++++++++++ ecp/test/vid/tools.h | 37 ++++ ecp/test/voip.c | 196 +++++++++++++++++ 26 files changed, 2618 insertions(+) create mode 100644 ecp/test/Makefile create mode 100644 ecp/test/basic.c create mode 100644 ecp/test/client.c create mode 100644 ecp/test/echo.c create mode 100644 ecp/test/frag.c create mode 100644 ecp/test/init.c create mode 100644 ecp/test/init_vconn.c create mode 100644 ecp/test/server.c create mode 100644 ecp/test/stress.c create mode 100644 ecp/test/vc_client.c create mode 100644 ecp/test/vc_client_t.c create mode 100644 ecp/test/vc_server.c create mode 100644 ecp/test/vcs.c create mode 100644 ecp/test/vid/Makefile create mode 100644 ecp/test/vid/cap.c create mode 100755 ecp/test/vid/cap.sh create mode 100644 ecp/test/vid/client.c create mode 100644 ecp/test/vid/display.c create mode 100644 ecp/test/vid/display.h create mode 100644 ecp/test/vid/enc.c create mode 100644 ecp/test/vid/enc.h create mode 100644 ecp/test/vid/server.c create mode 100644 ecp/test/vid/server.h create mode 100644 ecp/test/vid/tools.c create mode 100644 ecp/test/vid/tools.h create mode 100644 ecp/test/voip.c (limited to 'ecp/test') diff --git a/ecp/test/Makefile b/ecp/test/Makefile new file mode 100644 index 0000000..863a3ef --- /dev/null +++ b/ecp/test/Makefile @@ -0,0 +1,51 @@ +include ../src/Makefile.posix +CFLAGS += -I../src -I../util -Wno-int-to-void-pointer-cast + +dep=../src/build-posix/*.a ../util/libecputil.a + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +all: basic client server echo frag stress vcs vc_server vc_client vc_client_t + +basic: basic.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) + +client: client.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) + +server: server.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) + +echo: echo.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) + +frag: frag.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) + +stress: stress.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) + +vcs: vcs.o init_vconn.o $(dep) + $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + +vc_server: vc_server.o init_vconn.o $(dep) + $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + +vc_client: vc_client.o init_vconn.o $(dep) + $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + +vc_client_t: vc_client_t.o init_vconn.o $(dep) + $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) + + +opus_root=/opt/my/opus-1.1.5 +voip.o: voip.c + $(CC) $(CFLAGS) -I $(opus_root)/include -c $< + +voip: voip.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(opus_root)/.libs/libopus.a -lm $(LDFLAGS) -l asound + +clean: + rm -f *.o + rm -f basic client server echo frag stress vcs vc_server vc_client vc_client_t voip diff --git a/ecp/test/basic.c b/ecp/test/basic.c new file mode 100644 index 0000000..8268599 --- /dev/null +++ b/ecp/test/basic.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#include "core.h" + +ECPContext ctx_s; +ECPSocket sock_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; + +ECPContext ctx_c; +ECPSocket sock_c; +ECPDHKey key_perma_c; +ECPConnHandler handler_c; + +ECPNode node; +ECPConnection conn; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + uint32_t seq = 0; + + ecp_conn_handle_open(conn, sq, t, p, s, b); + if (s < 0) { + printf("OPEN ERR:%ld\n", s); + return s; + } + + char *msg = "PERA JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + + return s; +} + +ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG C:%s size:%ld\n", p, s); + return s; +} + +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG S:%s size:%ld\n", p, s); + + char *msg = "VAISTINU JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + + return s; +} + +int main(int argc, char *argv[]) { + int rv; + + rv = ecp_init(&ctx_s); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + + rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_s); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_init(&ctx_c); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_c); + handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; + handler_c.msg[MTYPE_MSG] = handle_msg_c; + ctx_c.handler[CTYPE_TEST] = &handler_c; + + rv = ecp_dhkey_generate(&ctx_c, &key_perma_c); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_c, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_c); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000"); + printf("ecp_node_init RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/client.c b/ecp/test/client.c new file mode 100644 index 0000000..e6c8208 --- /dev/null +++ b/ecp/test/client.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#include "core.h" +#include "util.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; + +ECPNode node; +ECPConnection conn; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + uint32_t seq = 0; + + ecp_conn_handle_open(conn, sq, t, p, s, b); + if (s < 0) { + printf("OPEN ERR:%ld\n", s); + return s; + } + + char *msg = "PERA JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + + return s; +} + +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG C:%s size:%ld\n", p, s); + return s; +} + + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s \n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv; + + if (argc != 2) usage(argv[0]); + + rv = ecp_init(&ctx); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler); + handler.msg[ECP_MTYPE_OPEN] = handle_open; + handler.msg[MTYPE_MSG] = handle_msg; + ctx.handler[CTYPE_TEST] = &handler; + + rv = ecp_sock_create(&sock, &ctx, NULL); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_util_node_load(&ctx, &node, argv[1]); + printf("ecp_util_node_load RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/echo.c b/ecp/test/echo.c new file mode 100644 index 0000000..ff9c01c --- /dev/null +++ b/ecp/test/echo.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +#include "core.h" +#include "util.h" + +ECPContext ctx_s; +ECPSocket sock_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + unsigned char buf[ECP_MAX_PLD]; + + memcpy(buf, p, s); + ssize_t _rv = ecp_send(conn, t, buf, s); + + return s; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s
\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv; + + if (argc != 3) usage(argv[0]); + + rv = ecp_init(&ctx_s); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + + rv = ecp_util_key_load(&ctx_s, &key_perma_s, argv[2]); + printf("ecp_util_key_load RV:%d\n", rv); + + rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_s, argv[1]); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_s); + printf("ecp_start_receiver RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/frag.c b/ecp/test/frag.c new file mode 100644 index 0000000..2bba856 --- /dev/null +++ b/ecp/test/frag.c @@ -0,0 +1,122 @@ +#include +#include +#include + +#include "core.h" + +ECPContext ctx_s; +ECPSocket sock_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; + +ECPContext ctx_c; +ECPSocket sock_c; +ECPDHKey key_perma_c; +ECPConnHandler handler_c; + +ECPNode node; +ECPConnection conn; + +ECPRBRecv rbuf_recv; +ECPRBMessage rbuf_r_msg[128]; +ECPFragIter frag_iter; +unsigned char frag_buffer[8192]; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + uint32_t seq = 0; + + ecp_conn_handle_open(conn, sq, t, p, s, b); + if (s < 0) { + printf("OPEN ERR:%ld\n", s); + return s; + } + + unsigned char content[1000]; + char *msg = "PERA JE CAR!"; + + strcpy((char *)content, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, content, sizeof(content)); + return s; +} + +ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG C:%s size:%ld\n", p, s); + + ECPRBuffer *rbuf = &conn->rbuf.recv->rbuf; + printf("RBUF: %d %d %d %d\n", rbuf->seq_start, rbuf->seq_max, rbuf->msg_start, rbuf->msg_size); + return s; +} + +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG S:%s size:%ld\n", p, s); + + unsigned char content[5000]; + char *msg = "VAISTINU JE CAR!"; + + strcpy((char *)content, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, content, sizeof(content)); + return s; +} + +int main(int argc, char *argv[]) { + int rv; + + rv = ecp_init(&ctx_s); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + + rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_s); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_init(&ctx_c); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_c); + handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; + handler_c.msg[MTYPE_MSG] = handle_msg_c; + ctx_c.handler[CTYPE_TEST] = &handler_c; + + rv = ecp_dhkey_generate(&ctx_c, &key_perma_c); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_c, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_c); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000"); + printf("ecp_node_init RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_rbuf_create(&conn, NULL, NULL, 0, &rbuf_recv, rbuf_r_msg, 128); + printf("ecp_rbuf_create RV:%d\n", rv); + + ecp_frag_iter_init(&frag_iter, frag_buffer, 8192); + rbuf_recv.frag_iter = &frag_iter; + + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/init.c b/ecp/test/init.c new file mode 100644 index 0000000..7e39f87 --- /dev/null +++ b/ecp/test/init.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#include "core.h" + +static int v_rng(void *buf, size_t bufsize) { + int fd; + + if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1; + size_t nb = read(fd, buf, bufsize); + close(fd); + if (nb != bufsize) return -1; + return 0; +} + +static ECPConnection *conn_alloc(unsigned char type) { + return malloc(sizeof(ECPConnection)); +} + +static void conn_free(ECPConnection *conn) { + free(conn); +} + +int ecp_init(ECPContext *ctx) { + int rv; + + rv = ecp_ctx_create(ctx); + if (rv) return rv; + + ctx->rng = v_rng; + ctx->conn_alloc = conn_alloc; + ctx->conn_free = conn_free; + + return ECP_OK; +} \ No newline at end of file diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c new file mode 100644 index 0000000..7e3dd04 --- /dev/null +++ b/ecp/test/init_vconn.c @@ -0,0 +1,42 @@ +#include +#include +#include + +#include "core.h" +#include "vconn/vconn.h" + +static int v_rng(void *buf, size_t bufsize) { + int fd; + + if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1; + size_t nb = read(fd, buf, bufsize); + close(fd); + if (nb != bufsize) return -1; + return 0; +} + +static ECPConnection *conn_alloc(unsigned char type) { + switch (type) { + case ECP_CTYPE_VCONN: + return malloc(sizeof(ECPVConnIn)); + default: + return malloc(sizeof(ECPConnection)); + } +} + +static void conn_free(ECPConnection *conn) { + free(conn); +} + +int ecp_init(ECPContext *ctx) { + int rv; + + rv = ecp_ctx_create_vconn(ctx); + if (rv) return rv; + + ctx->rng = v_rng; + ctx->conn_alloc = conn_alloc; + ctx->conn_free = conn_free; + + return ECP_OK; +} \ No newline at end of file diff --git a/ecp/test/server.c b/ecp/test/server.c new file mode 100644 index 0000000..f70cc79 --- /dev/null +++ b/ecp/test/server.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "core.h" +#include "util.h" + +ECPContext ctx; +ECPSocket sock; +ECPDHKey key_perma; +ECPConnHandler handler; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG S:%s size:%ld\n", p, s); + + char *msg = "VAISTINU JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + + return s; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s
\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv; + + if (argc != 3) usage(argv[0]); + + rv = ecp_init(&ctx); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler); + handler.msg[MTYPE_MSG] = handle_msg; + ctx.handler[CTYPE_TEST] = &handler; + + rv = ecp_util_key_load(&ctx, &key_perma, argv[2]); + printf("ecp_util_key_load RV:%d\n", rv); + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, argv[1]); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/stress.c b/ecp/test/stress.c new file mode 100644 index 0000000..73c511e --- /dev/null +++ b/ecp/test/stress.c @@ -0,0 +1,256 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "core.h" + +#ifdef __linux__ +#define SIGINFO SIGTSTP +#endif + +#define NUM_S 32 +#define NUM_C 256 +#define MSG_RATE 65 + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ECPContext ctx_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; +ECPSocket *sock_s; + +ECPConnHandler handler_c; +ECPContext *ctx_c; +ECPSocket *sock_c; +ECPDHKey *key_perma_c; + +ECPNode *node; +ECPConnection *conn; + +pthread_t *s_thd; +pthread_t *r_thd; +pthread_mutex_t *t_mtx; +int *t_sent, *t_rcvd; + +uint64_t t_start = 0, t_end = 0; +int c_start = 0; + +int num_s = NUM_S, num_c = NUM_C; +int msg_rate = MSG_RATE; + +static void display(void) { + int i, s = 0, r = 0; + + for (i=0; iconn_data); + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; + + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + payload.buffer = pld_buf; + payload.size = ECP_MAX_PLD; + + printf("OPEN:%d\n", idx); + while(1) { + uint32_t rnd; + c->sock->ctx->rng(&rnd, sizeof(uint32_t)); + usleep(rnd % (2000000/msg_rate)); + + ecp_pld_set_type(pld_buf, MTYPE_MSG); + ssize_t _rv = 0; + // XXX refactor + // _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); + if (c_start && (_rv > 0)) { + pthread_mutex_lock(&t_mtx[idx]); + t_sent[idx]++; + pthread_mutex_unlock(&t_mtx[idx]); + } + } +} + +ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + int idx = (int)(conn->conn_data); + int rv = 0; + + ecp_conn_handle_open(conn, sq, t, p, s, b); + rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn); + if (rv) { + char msg[256]; + sprintf(msg, "THD %d CREATE\n", idx); + perror(msg); + exit(1); + } + return s; +} + +ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + int idx = (int)(conn->conn_data); + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; + + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + payload.buffer = pld_buf; + payload.size = ECP_MAX_PLD; + + if (c_start) { + pthread_mutex_lock(&t_mtx[idx]); + t_rcvd[idx]++; + pthread_mutex_unlock(&t_mtx[idx]); + } + + // ecp_pld_set_type(payload, MTYPE_MSG); + // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); + return s; +} + +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; + + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + payload.buffer = pld_buf; + payload.size = ECP_MAX_PLD; + + ecp_pld_set_type(pld_buf, MTYPE_MSG); + // XXX refactor + // ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0)); + return s; +} + +int main(int argc, char *argv[]) { + char addr[256]; + int rv; + int i; + + ECPConnHandler handler_c; + + ECPContext *ctx_c; + ECPSocket *sock_c; + ECPDHKey *key_perma_c; + + ECPNode *node; + ECPConnection *conn; + + sock_s = malloc(num_s * sizeof(ECPSocket)); + ctx_c = malloc(num_c * sizeof(ECPContext)); + sock_c = malloc(num_c * sizeof(ECPSocket)); + key_perma_c = malloc(num_c * sizeof(ECPDHKey)); + node = malloc(num_c * sizeof(ECPNode)); + conn = malloc(num_c * sizeof(ECPConnection)); + + s_thd = malloc(num_c * sizeof(pthread_t)); + r_thd = malloc(num_c * sizeof(pthread_t)); + t_mtx = malloc(num_c * sizeof(pthread_mutex_t)); + t_sent = malloc(num_c * sizeof(int)); + t_rcvd = malloc(num_c * sizeof(int)); + memset(t_rcvd, 0, num_c * sizeof(int)); + memset(t_sent, 0, num_c * sizeof(int)); + + struct sigaction actINFO; + memset(&actINFO, 0, sizeof(actINFO)); + actINFO.sa_handler = &catchINFO; + sigaction(SIGINFO, &actINFO, NULL); + + rv = ecp_init(&ctx_s); + if (!rv) rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + + if (!rv) rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); + + for (i=0; i +#include +#include +#include + +#include "core.h" +#include "vconn/vconn.h" +#include "util.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; + +ECPConnection conn; +ECPNode node; + +ECPVConnection vconn[20]; +ECPNode vconn_node[20]; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + uint32_t seq = 0; + + ecp_conn_handle_open(conn, sq, t, p, s, b); + if (s < 0) { + printf("OPEN ERR:%ld\n", s); + return s; + } + + printf("OPEN!\n"); + + char *msg = "PERA JE CAR!"; + unsigned char buf[1156]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1156); + + return s; +} + +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("MSG S:%s size:%ld\n", p, s); + return s; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s ... \n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv, i; + + if ((argc < 3) || (argc > 22)) usage(argv[0]); + + rv = ecp_init(&ctx); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler); + handler.msg[ECP_MTYPE_OPEN] = handle_open; + handler.msg[MTYPE_MSG] = handle_msg; + ctx.handler[CTYPE_TEST] = &handler; + + rv = ecp_sock_create(&sock, &ctx, NULL); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_util_node_load(&ctx, &node, argv[1]); + printf("ecp_util_node_load RV:%d\n", rv); + + for (i=0; i +#include +#include +#include +#include + +#include "core.h" +#include "vconn/vconn.h" +#include "util.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; + +ECPNode node; +ECPConnection conn; + +ECPVConnection vconn[20]; +ECPNode vconn_node[20]; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + + +int counter = 0; +uint64_t t_start = 0; +uint64_t t_end = 0; +ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + ssize_t rv = ecp_conn_handle_open(conn, sq, t, p, s, b); + if (rv < 0) { + printf("OPEN ERR:%ld\n", s); + return rv; + } + + printf("OPEN\n"); + char *msg = "PERA JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + // ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + + struct timeval tv; + gettimeofday(&tv, NULL); + t_start = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; + + return rv; +} + +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + ecp_send(conn, t, p, s); + write(2, p+1, s-1); + fflush(stderr); + // printf("MSG C:%s size:%ld\n", p, s); + return s; + + counter++; + char *msg = "PERA JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + + if (counter % 100 == 0) { + struct timeval tv; + uint64_t t_time; + + gettimeofday(&tv, NULL); + t_end = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; + t_time = t_end - t_start; + printf("T:%f\n", (float)t_time/1000000); + t_start = t_end; + } + return s; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s ... \n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv, i; + + if ((argc < 3) || (argc > 22)) usage(argv[0]); + + rv = ecp_init(&ctx); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler); + handler.msg[ECP_MTYPE_OPEN] = handle_open; + handler.msg[MTYPE_MSG] = handle_msg; + ctx.handler[CTYPE_TEST] = &handler; + + rv = ecp_sock_create(&sock, &ctx, NULL); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_util_node_load(&ctx, &node, argv[1]); + printf("ecp_util_node_load RV:%d\n", rv); + + for (i=0; i +#include +#include +#include + +#include "core.h" +#include "vconn/vconn.h" +#include "util.h" + +ECPContext ctx; +ECPSocket sock; +ECPDHKey key_perma; +ECPConnHandler handler; + +ECPNode node; +ECPConnection conn; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + printf("OPEN RECEIVED\n"); + return ecp_conn_handle_open(conn, sq, t, p, s, b); +} + +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + // printf("MSG S:%s size:%ld\n", p, s); + + char *msg = "VAISTINU JE CAR!"; + unsigned char buf[1000]; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, strlen(msg)+1); + + return s; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s \n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv; + + if (argc != 3) usage(argv[0]); + + rv = ecp_init(&ctx); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler); + handler.msg[ECP_MTYPE_OPEN] = handle_open; + handler.msg[MTYPE_MSG] = handle_msg; + ctx.handler[CTYPE_TEST] = &handler; + + rv = ecp_util_key_load(&ctx, &key_perma, argv[1]); + printf("ecp_util_key_load RV:%d\n", rv); + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_util_node_load(&ctx, &node, argv[2]); + printf("ecp_util_node_load RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock, ECP_CTYPE_VLINK); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/vcs.c b/ecp/test/vcs.c new file mode 100644 index 0000000..23e2557 --- /dev/null +++ b/ecp/test/vcs.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +#include "core.h" +#include "util.h" +#include "vconn/vconn.h" + +ECPContext ctx; +ECPSocket sock; +ECPDHKey key_perma; + +ECPNode node; +ECPConnection conn; + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s
[node.pub]\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv; + + if ((argc < 3) || (argc > 4)) usage(argv[0]); + + rv = ecp_init(&ctx); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_util_key_load(&ctx, &key_perma, argv[2]); + printf("ecp_util_key_load RV:%d\n", rv); + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, argv[1]); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + if (argc == 4) { + rv = ecp_util_node_load(&ctx, &node, argv[3]); + printf("ecp_util_node_load RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock, ECP_CTYPE_VLINK); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); + } + + while (1) sleep(1); +} \ No newline at end of file diff --git a/ecp/test/vid/Makefile b/ecp/test/vid/Makefile new file mode 100644 index 0000000..a9b92eb --- /dev/null +++ b/ecp/test/vid/Makefile @@ -0,0 +1,23 @@ +include ../../ecp/Makefile.platform + +LIBVPX_HOME = /opt/my/libvpx +CFLAGS += -D_V4L2_KERNEL_ -I/usr/src/linux-headers-$(uname -r) -I$(LIBVPX_HOME) -I/opt/local/include/SDL2 -I../../ecp -I../../util -Wno-int-to-void-pointer-cast +LDFLAGS += -L$(LIBVPX_HOME) -L/opt/local/lib + +dep=../init_vconn.o ../../ecp/build-posix/*.a ../../util/libecputil.a + +all: cap + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +cap: cap.o enc.o tools.o server.o + $(CC) -o $@ $< enc.o tools.o server.o $(dep) -lvpx $(LDFLAGS) + +client: client.o display.o tools.o + $(CC) -o $@ $< display.o tools.o $(dep) -lvpx -lSDL2 $(LDFLAGS) + +clean: + rm -f *.o + rm -f cap client + diff --git a/ecp/test/vid/cap.c b/ecp/test/vid/cap.c new file mode 100644 index 0000000..e37c28f --- /dev/null +++ b/ecp/test/vid/cap.c @@ -0,0 +1,576 @@ +/* + * OpenCV BSD3 License + * videodev2.h BSD License (dual license) + * If you raise some license issue here simply don't use it and let us know + * + * Copyright (C) 2016 the contributors + * + * Luiz Vitor Martinez Cardoso + * Jules Thuillier + * @lex (avafinger) + * + * gcc cap.c -o cap $(pkg-config --libs --cflags opencv) -lm + * + * gcc -I/usr/src/linux-headers-VERSION/ cap.c -o cap $(pkg-config --libs --cflags opencv) -lm -O3 + * +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* ----------------------------------------------------------------------------- + * BananaPi M64 / Pine64+ (A64) or if you want to control Exposure,Hflip,Vflip + * ----------------------------------------------------------------------------- + * _V4L2_KERNEL_ should be defined and point to: /usr/src/linux-headers-version + * + * build with: gcc -I/usr/src/linux-headers-3.10.102/ cap.c -o cap $(pkg-config --libs --cflags opencv) -lm -O3 + * + * + * ----------------------------------------------------------------------------- + * OrangePi / BananaPi / NanoPi (H3) / BPI-M3 (A83T - ov5640 & ov8865) + * ----------------------------------------------------------------------------- + * _V4L2_KERNEL_ should not be defined unless you want Exposure, Hflip and Vflip + * + * build with: gcc cap.c -o cap $(pkg-config --libs --cflags opencv) -lm + * + * +*/ +//#define _V4L2_KERNEL_ // BananaPi M64 / Pine64+ only or for setting Exposure,Hflip,Vflip + +#ifdef _V4L2_KERNEL_ +/* --- A64 --- */ +#include +#else +/* --- H3 / A83T --- */ +#include "videodev2.h" +#endif + +#ifdef _V4L2_KERNEL_ +#define V4L2_MODE_VIDEO 0x0002 /* video capture */ +#define V4L2_MODE_IMAGE 0x0003 /* image capture */ +#define V4L2_MODE_PREVIEW 0x0004 /* preview capture */ +#endif + +#define N_BUFFERS 4 +#define CAP_OK 0 +#define CAP_ERROR -1 +#define CAP_ERROR_RET(s) { \ + fprintf(stderr, "v4l2: %s\n", s); \ + return CAP_ERROR; \ + } +#define CAP_CLIP(val, min, max) (((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val))) + +#define CLEAR(x) memset (&(x), 0, sizeof (x)) +#define ALIGN_4K(x) (((x) + (4095)) & ~(4095)) +#define ALIGN_16B(x) (((x) + (15)) & ~(15)) + +#include "server.h" +#include "enc.h" + +typedef struct { + void *start; + size_t length; +} v4l2_buffer_t; + +int width; +int height; +v4l2_buffer_t *buffers = NULL; +int n_buffers = N_BUFFERS; +int sensor_video_mode; +int sensor_exposure; +int sensor_hflip; +int sensor_vflip; + +double get_wall_time() +{ + struct timeval time; + if (gettimeofday(&time, NULL)) + return 0.; + return (double) time.tv_sec + (double) time.tv_usec * .000001; +} + +int yuv420p_to_bgr(void *in, int length, unsigned char *out) +{ + uint8_t *yptr, *uptr, *vptr; + uint32_t x, y, p; + + if (length < (width * height * 3) / 2) + return CAP_ERROR; + + yptr = (uint8_t *) in; + uptr = yptr + (width * height); + vptr = uptr + (width * height / 4); + p = 0; + + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + int r, g, b; + int y, u, v; + + y = *(yptr++) << 8; + u = uptr[p] - 128; + v = vptr[p] - 128; + + r = (y + (359 * v)) >> 8; + g = (y - (88 * u) - (183 * v)) >> 8; + b = (y + (454 * u)) >> 8; + + *(out++) += CAP_CLIP(b, 0x00, 0xFF); + *(out++) += CAP_CLIP(g, 0x00, 0xFF); + *(out++) += CAP_CLIP(r, 0x00, 0xFF); + + if (x & 1) + p++; + } + + if (!(y & 1)) + p -= width / 2; + } + + return CAP_ERROR; +} + +static int xioctl(int fd, int request, void *arg) +{ + int r; + int tries = 3; + + do { + r = ioctl(fd, request, arg); + } while (--tries > 0 && -1 == r && EINTR == errno); + + return r; +} + +int v4l2_display_sizes_pix_format(int fd) +{ + int ret = 0; + int fsizeind = 0; /*index for supported sizes*/ + struct v4l2_frmsizeenum fsize; + + fprintf(stderr, "V4L2 pixel sizes:\n"); + + CLEAR(fsize); + fsize.index = 0; + fsize.pixel_format = V4L2_PIX_FMT_YUV420; + + while ((ret = xioctl(fd, VIDIOC_ENUM_FRAMESIZES, &fsize)) == 0) { + fsize.index++; + if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) { + fprintf(stderr, "( %u x %u ) Pixels\n", fsize.discrete.width, fsize.discrete.height); + fsizeind++; + } + } + return fsizeind; +} + +int v4l2_display_pix_format(int fd) +{ + struct v4l2_fmtdesc fmt; + int index; + + fprintf(stderr, "V4L2 pixel formats:\n"); + + index = 0; + CLEAR(fmt); + fmt.index = index; + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + while (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) != -1) { + fprintf(stderr, "%i: [0x%08X] '%c%c%c%c' (%s)\n", index, fmt.pixelformat, fmt.pixelformat >> 0, fmt.pixelformat >> 8, fmt.pixelformat >> 16, fmt.pixelformat >> 24, fmt.description); + + memset(&fmt, 0, sizeof(fmt)); + fmt.index = ++index; + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + } + // fprintf(stderr, "\n"); +} + +#ifdef _V4L2_KERNEL_ +int v4l2_set_exposure(int fd, int exposure) +{ + struct v4l2_queryctrl queryctrl; + struct v4l2_control control; + int rc; + + fprintf(stderr, "set Exposure: %d\n", exposure); + rc = 0; + memset(&control, 0, sizeof(control)); + control.id = V4L2_CID_EXPOSURE; + rc = xioctl(fd, VIDIOC_G_CTRL, &control); + fprintf(stderr, "rc: %d - get exposure: %d\n", rc, control.value); + control.value = exposure; + rc = xioctl(fd, VIDIOC_S_CTRL, &control); + fprintf(stderr, "rc: %d - new exposure: %d\n", rc, exposure); + return rc; +} + +int v4l2_set_hflip(int fd, int hflip) +{ + struct v4l2_queryctrl queryctrl; + struct v4l2_control control; + int rc; + + fprintf(stderr, "set Hflip: %d\n", hflip); + rc = 0; + memset(&control, 0, sizeof(control)); + control.id = V4L2_CID_HFLIP; + rc = xioctl(fd, VIDIOC_G_CTRL, &control); + fprintf(stderr, "rc: %d - get value: %d\n", rc, control.value); + control.value = hflip; + rc = xioctl(fd, VIDIOC_S_CTRL, &control); + fprintf(stderr, "rc: %d - new value: %d\n", rc, control.value); + return rc; +} + +int v4l2_set_vflip(int fd, int vflip) +{ + struct v4l2_queryctrl queryctrl; + struct v4l2_control control; + int rc; + + fprintf(stderr, "set Vflip: %d\n", vflip); + rc = 0; + memset(&control, 0, sizeof(control)); + control.id = V4L2_CID_VFLIP; + rc = xioctl(fd, VIDIOC_G_CTRL, &control); + fprintf(stderr, "rc: %d - get value: %d\n", rc, control.value); + control.value = vflip; + rc = xioctl(fd, VIDIOC_S_CTRL, &control); + fprintf(stderr, "rc: %d - new value: %d\n", rc, control.value); + return rc; +} +#endif + +int v4l2_init_camera(int fd) +{ + uint32_t i; + uint32_t index; + struct v4l2_streamparm parms; + struct v4l2_format fmt; + struct v4l2_input input; + struct v4l2_capability caps; + + CLEAR(fmt); + CLEAR(input); + CLEAR(caps); + + + if (xioctl(fd, VIDIOC_QUERYCAP, &caps) == -1) { + CAP_ERROR_RET("unable to query capabilities."); + } + + if (!(caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { + CAP_ERROR_RET("doesn't support video capturing."); + } + + fprintf(stderr, "Driver: \"%s\"\n", caps.driver); + fprintf(stderr, "Card: \"%s\"\n", caps.card); + fprintf(stderr, "Bus: \"%s\"\n", caps.bus_info); + fprintf(stderr, "Version: %d.%d\n", (caps.version >> 16) && 0xff, (caps.version >> 24) && 0xff); + fprintf(stderr, "Capabilities: %08x\n", caps.capabilities); + + input.index = 0; + if (xioctl(fd, VIDIOC_ENUMINPUT, &input) == -1) { + CAP_ERROR_RET("unable to enumerate input."); + } + + fprintf(stderr, "Input: %d\n", input.index); + if (xioctl(fd, VIDIOC_S_INPUT, &input.index) == -1) { + CAP_ERROR_RET("unable to set input."); + } + + parms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + parms.parm.capture.capturemode = sensor_video_mode ? V4L2_MODE_VIDEO : V4L2_MODE_IMAGE; + parms.parm.capture.timeperframe.numerator = 1; + parms.parm.capture.timeperframe.denominator = sensor_video_mode ? 30 : 7; + if (-1 == xioctl(fd, VIDIOC_S_PARM, &parms)) { + CAP_ERROR_RET("unable to set stream parm."); + } + + v4l2_display_pix_format(fd); + v4l2_display_sizes_pix_format(fd); + fprintf(stderr, "\n"); + + fmt.fmt.pix.width = width; + fmt.fmt.pix.height = height; + fmt.fmt.pix.field = V4L2_FIELD_NONE; // V4L2_FIELD_ANY; + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; + //fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; + + if (xioctl(fd, VIDIOC_TRY_FMT, &fmt) == -1) { + CAP_ERROR_RET("failed trying to set pixel format."); + } + + if (fmt.fmt.pix.width != width || fmt.fmt.pix.height != height) { + width = fmt.fmt.pix.width; + height = fmt.fmt.pix.height; + fprintf(stderr, "Sensor size adjusted to: %dx%d pixels\n", width, height); + } else { + fprintf(stderr, "Sensor size: %dx%d pixels\n", width, height); + } + + if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) { + CAP_ERROR_RET("failed to set pixel format."); + } + + switch (fmt.fmt.pix.pixelformat) { + case V4L2_PIX_FMT_RGB24: + fprintf(stderr, "Pixel Format: V4L2_PIX_FMT_RGB24 [0x%08X]\n",fmt.fmt.pix.pixelformat); + break; + + case V4L2_PIX_FMT_YUV420: + fprintf(stderr, "Pixel Format: V4L2_PIX_FMT_YUV420 [0x%08X]\n",fmt.fmt.pix.pixelformat); + break; + + } + + return CAP_OK; +} + +int v4l2_set_mmap(int fd, int *buffers_count) +{ + int i; + int nbf; + enum v4l2_buf_type type; + struct v4l2_requestbuffers req; + struct v4l2_buffer buf; + + CLEAR(req); + req.count = sensor_video_mode ? n_buffers : 1; + req.memory = V4L2_MEMORY_MMAP; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) { + CAP_ERROR_RET("failed requesting buffers."); + } + nbf = req.count; + if (n_buffers != nbf) { + CAP_ERROR_RET("insufficient buffer memory."); + } + + buffers = (v4l2_buffer_t *) calloc(nbf, sizeof(v4l2_buffer_t)); + if (!buffers) { + CAP_ERROR_RET("failed to allocated buffers memory."); + } + + for (i = 0; i < nbf; i++) { + CLEAR(buf); + buf.index = i; + buf.memory = V4L2_MEMORY_MMAP; + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) { + CAP_ERROR_RET("failed to query buffer."); + } + buffers[i].length = buf.length; + buffers[i].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); + if (MAP_FAILED == buffers[i].start) { + CAP_ERROR_RET("failed to mmap buffer."); + } + } + + for (i = 0; i < nbf; i++) { + CLEAR(buf); + buf.index = i; + buf.memory = V4L2_MEMORY_MMAP; + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) { + CAP_ERROR_RET("failed to queue buffer."); + } + } + + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) { + CAP_ERROR_RET("failed to stream on."); + } + + *buffers_count = nbf; + + return CAP_OK; +} + +int v4l2_retrieve_frame(int fd, int buffers_count, vpx_image_t *raw, vpx_codec_ctx_t *codec, int frame_index, int kframe_interval) +{ + int sz; + fd_set fds; + unsigned char *frame_yuv; + struct timeval tv; + struct v4l2_buffer buf; + int rc; + char err_msg[128]; + int flags = 0; + + CLEAR(tv); + CLEAR(buf); + + rc = 1; + while (rc > 0) { + FD_ZERO(&fds); + FD_SET(fd, &fds); + + tv.tv_sec = 2; + tv.tv_usec = 0; + + rc = select(fd + 1, &fds, NULL, NULL, &tv); + if (-1 == rc) { + if (EINTR == errno) { + rc = 1; // try again + continue; + } + CAP_ERROR_RET("failed to select frame."); + } + /* we got something */ + break; + } + if (rc <= 0) { + sprintf(err_msg, "errno: %d - check sensor, something wrong.", errno); + CAP_ERROR_RET(err_msg); + } + + buf.memory = V4L2_MEMORY_MMAP; + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) { + CAP_ERROR_RET("failed to retrieve frame."); + } + + // fprintf(stderr, "Length: %d \tBytesused: %d \tAddress: %p\n", buf.length, buf.bytesused, &buffers[buf.index]); + + sz = ALIGN_16B(width) * height * 3 / 2; + if (!vpx_img_read(raw, buffers[buf.index].start, sz)) { + die_codec(NULL, "Failed to read image."); + } + if (frame_index % kframe_interval == 0) flags |= VPX_EFLAG_FORCE_KF; + vpx_encode_frame(codec, raw, frame_index, flags); + + if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) { + CAP_ERROR_RET("failed to queue buffer."); + } + + return CAP_OK; +} + +int v4l2_close_camera(int fd, int buffers_count) +{ + int i; + enum v4l2_buf_type type; + + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (xioctl(fd, VIDIOC_STREAMOFF, &type) == -1) { + CAP_ERROR_RET("failed to stream off."); + } + + for (i = 0; i < buffers_count; i++) + munmap(buffers[i].start, buffers[i].length); + + close(fd); +} + +int main(int argc, char *argv[]) +{ + int fd; + int target_bitrate = 200; + double after; + double before; + double avg, fps; + int buffers_count; + int kframe_interval; + vpx_codec_er_flags_t err_resilient; + char *address; + char *my_key; + char *vcs_key; + + if (argc != 14) { + CAP_ERROR_RET("./cap