From 06f74eed2686a9cab4191908c3fd30f0c15a380b Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Tue, 6 Jun 2017 17:01:22 +0200 Subject: voip test case added --- code/test/Makefile | 11 +-- code/test/echo.c | 53 +++++++++++++++ code/test/voip.c | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 254 insertions(+), 4 deletions(-) create mode 100644 code/test/echo.c create mode 100644 code/test/voip.c diff --git a/code/test/Makefile b/code/test/Makefile index 6c5a7e5..8f42021 100644 --- a/code/test/Makefile +++ b/code/test/Makefile @@ -6,7 +6,10 @@ dep=../core/libecpcore.a ../core/crypto/libecpcr.a ../core/htable/libecpht.a ../ %.o: %.c $(CC) $(CFLAGS) -c $< -all: client server basic stress proxy pr_server pr_client +all: basic client server echo stress proxy pr_server pr_client + +basic: basic.o init.o $(dep) + $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) client: client.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) @@ -14,7 +17,7 @@ client: client.o init.o $(dep) server: server.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) -basic: basic.o init.o $(dep) +echo: echo.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) stress: stress.o init.o $(dep) @@ -29,8 +32,8 @@ pr_server: pr_server.o init_proxy.o $(dep) pr_client: pr_client.o init_proxy.o $(dep) $(CC) -o $@ $< init_proxy.o $(dep) $(LDFLAGS) -opus_root=../../../opus-1.1.5 +opus_root=../../../opus-1.1.5 voip.o: voip.c $(CC) $(CFLAGS) -I $(opus_root)/include -c $< @@ -39,4 +42,4 @@ voip: voip.o init.o $(dep) clean: rm -f *.o - rm -f basic stress client server proxy pr_server pr_client + rm -f basic client server echo stress proxy pr_server pr_client voip diff --git a/code/test/echo.c b/code/test/echo.c new file mode 100644 index 0000000..2494868 --- /dev/null +++ b/code/test/echo.c @@ -0,0 +1,53 @@ +#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, unsigned char t, unsigned char *p, ssize_t s) { + ssize_t rv = ecp_send(conn, p-ECP_SIZE_MSG_HDR, ECP_SIZE_MSG_HDR+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/code/test/voip.c b/code/test/voip.c new file mode 100644 index 0000000..673bb8a --- /dev/null +++ b/code/test/voip.c @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include + +#include + +#include "core.h" +#include "util.h" + +ECPContext ctx_c; +ECPSocket sock_c; +ECPConnHandler handler_c; + +ECPNode node; +ECPConnection conn; +int open_done = 0; + +snd_pcm_t *handle_plb; +snd_pcm_t *handle_cpt; +OpusEncoder *opus_enc; +OpusDecoder *opus_dec; +// 2.5, 5, 10, 20, 40 or 60 ms +snd_pcm_uframes_t alsa_frames = 160; +unsigned char *alsa_out_buf = NULL; +unsigned char *alsa_in_buf = NULL; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +int a_open(char *dev_name, snd_pcm_t **handle, snd_pcm_hw_params_t **hw_params, snd_pcm_stream_t stream, snd_pcm_format_t format, unsigned int *nchannels, unsigned int *sample_rate, snd_pcm_uframes_t *frames, size_t *buf_size) { + int bits, err = 0; + unsigned int fragments = 2; + unsigned int frame_size; + + err = snd_pcm_open(handle, dev_name, stream, 0); + if (!err) err = snd_pcm_hw_params_malloc(hw_params); + if (!err) err = snd_pcm_hw_params_any(*handle, *hw_params); + if (!err) err = snd_pcm_hw_params_set_access(*handle, *hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); + if (!err) err = snd_pcm_hw_params_set_format(*handle, *hw_params, format); + if (!err) err = snd_pcm_hw_params_set_rate_near(*handle, *hw_params, sample_rate, 0); + if (!err) err = snd_pcm_hw_params_set_channels_near(*handle, *hw_params, nchannels); + if (!err) err = snd_pcm_hw_params_set_periods_near(*handle, *hw_params, &fragments, 0); + if (!err) err = snd_pcm_hw_params_set_period_size_near(*handle, *hw_params, frames, 0); + if (!err) err = snd_pcm_hw_params(*handle, *hw_params); + if (err) return err; + + bits = snd_pcm_hw_params_get_sbits(*hw_params); + if (bits < 0) return bits; + + frame_size = *nchannels * (bits / 8); + *buf_size = frame_size * *frames; + + return 0; +} + +int a_prepare(snd_pcm_t *handle, snd_pcm_hw_params_t *hw_params, unsigned char *buf, snd_pcm_uframes_t frames) { + snd_pcm_drop(handle); + snd_pcm_prepare(handle); + + if (snd_pcm_stream(handle) == SND_PCM_STREAM_PLAYBACK) { + int i, err; + unsigned int fragments; + + err = snd_pcm_hw_params_get_periods(hw_params, &fragments, NULL); + if (err) return err; + + for (i=0; i\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + int rv; + + if (argc != 2) usage(argv[0]); + + 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_sock_create(&sock_c, &ctx_c, NULL); + 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_util_node_load(&ctx_c, &node, argv[1]); + printf("ecp_util_node_load 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(!open_done) sleep(1); + + unsigned char payload[ECP_MAX_PLD]; + unsigned char *opus_buf; + + while(1) { + ecp_pld_set_type(payload, MTYPE_MSG); + opus_buf = ecp_pld_get_buf(payload); + opus_int32 len = a_read(handle_cpt, alsa_in_buf, alsa_frames, opus_enc, opus_buf, ECP_MAX_MSG); + if (len < 0) continue; + ssize_t _rv = ecp_send(&conn, payload, len); + } +} \ No newline at end of file -- cgit v1.2.3