summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@MacBookPro.lan>2017-06-06 17:01:22 +0200
committerUros Majstorovic <majstor@MacBookPro.lan>2017-06-06 17:01:22 +0200
commit06f74eed2686a9cab4191908c3fd30f0c15a380b (patch)
treec945e3e1e553a9e9b45fd4a086bc2bbb3723c2cc
parent25762246461186b92b71d256e098e0a7640875f1 (diff)
voip test case added
-rw-r--r--code/test/Makefile11
-rw-r--r--code/test/echo.c53
-rw-r--r--code/test/voip.c194
3 files changed, 254 insertions, 4 deletions
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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#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 <address> <node.priv>\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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <alsa/asoundlib.h>
+
+#include <opus.h>
+
+#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<fragments; i++) snd_pcm_writei(handle, buf, frames);
+ }
+ return 0;
+}
+
+opus_int32 a_read(snd_pcm_t *handle, unsigned char *buf, snd_pcm_uframes_t frames, OpusEncoder *enc, unsigned char *opus_buf, opus_int32 opus_size) {
+ snd_pcm_sframes_t frames_in;
+
+ while ((frames_in = snd_pcm_readi(handle, buf, frames)) < 0) {
+ if (frames_in == -EAGAIN)
+ continue;
+ snd_pcm_prepare(handle);
+ }
+ return opus_encode(enc, (opus_int16 *)buf, frames_in, opus_buf, opus_size);
+}
+
+snd_pcm_sframes_t a_write(OpusDecoder *dec, unsigned char *opus_buf, opus_int32 opus_size, snd_pcm_t *handle, unsigned char *buf, snd_pcm_uframes_t frames) {
+ snd_pcm_sframes_t frames_in, frames_out;
+
+ frames_in = opus_decode(dec, opus_buf, opus_size, (opus_int16 *)buf, frames, 0);
+ while ((frames_out = snd_pcm_writei(handle, buf, frames_in)) < 0) {
+ if (frames_out == -EAGAIN)
+ continue;
+ snd_pcm_prepare(handle);
+ }
+ return frames_out;
+}
+
+int a_init(void) {
+ char *dev_name = "hw:1,0";
+ unsigned int nchannels = 1;
+ unsigned int sample_rate = 16000;
+
+ snd_pcm_hw_params_t *hw_params_plb;
+ snd_pcm_hw_params_t *hw_params_cpt;
+ size_t buf_size;
+
+ a_open(dev_name, &handle_plb, &hw_params_plb, SND_PCM_STREAM_PLAYBACK, SND_PCM_FORMAT_S16_LE, &nchannels, &sample_rate, &alsa_frames, &buf_size);
+ alsa_out_buf = malloc(buf_size);
+ memset(alsa_out_buf, 0, buf_size);
+ a_prepare(handle_plb, hw_params_plb, alsa_out_buf, alsa_frames);
+
+ a_open(dev_name, &handle_cpt, &hw_params_cpt, SND_PCM_STREAM_CAPTURE, SND_PCM_FORMAT_S16_LE, &nchannels, &sample_rate, &alsa_frames, &buf_size);
+ alsa_in_buf = malloc(buf_size);
+ memset(alsa_in_buf, 0, buf_size);
+ a_prepare(handle_cpt, hw_params_cpt, alsa_in_buf, alsa_frames);
+
+ int size;
+ size = opus_encoder_get_size(nchannels);
+ opus_enc = malloc(size);
+ opus_encoder_init(opus_enc, sample_rate, nchannels, OPUS_APPLICATION_VOIP);
+
+ size = opus_decoder_get_size(nchannels);
+ opus_dec = malloc(size);
+ opus_decoder_init(opus_dec, sample_rate, nchannels);
+}
+
+ssize_t handle_open_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) {
+ uint32_t seq = 0;
+
+ ecp_conn_handle_open(conn, t, p, s);
+ if (s < 0) {
+ printf("OPEN ERR:%ld\n", s);
+ return 0;
+ }
+
+ a_init();
+ open_done = 1;
+ return s;
+}
+
+ssize_t handle_msg_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) {
+ a_write(opus_dec, p, s, handle_plb, alsa_out_buf, alsa_frames);
+ return s;
+}
+
+
+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 != 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