1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#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 ECP_OK;
}
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 ECP_OK;
}
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);
return ECP_OK;
}
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;
}
a_init();
open_done = 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) {
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_init(&sock_c, &ctx_c, NULL);
printf("ecp_sock_init 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_init(&conn, &sock_c, CTYPE_TEST);
printf("ecp_conn_init 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, 0);
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_pld_send(&conn, payload, len);
}
}
|