forked from ka9q/ka9q-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opussend.c
372 lines (322 loc) · 11.2 KB
/
opussend.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Multicast local audio with Opus
// Copyright Feb 2018 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <opus/opus.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
#include <portaudio.h>
#include <sysexits.h>
#include "misc.h"
#include "multicast.h"
// Global config constants
#define BUFFERSIZE (1<<18) // Size of audio ring buffer in mono samples. 2^18 is 2.73 sec at 48 kHz stereo
// Defined as macro so the Audiodata[] declaration below won't bother some compilers
int const Samprate = 48000; // Too hard to handle other sample rates right now
// Opus will notice the actual audio bandwidth, so there's no real cost to this
int const Channels = 2; // Stereo - no penalty if the audio is actually mono, Opus will figure it out
// Command line params
char *Audiodev = "";
char *Mcast_output_address_text; // Multicast address we're sending to
const char *App_path;
int Verbose; // Verbosity flag (currently unused)
// Opus codec params (with defaults)
float Opus_blocktime = 20; // 20 ms, a reasonable default
int Opus_bitrate = 32; // Opus stream audio bandwidth; default 32 kb/s
int Discontinuous = 0; // Off by default
int Fec = 0;
int Mcast_ttl = 10; // We're often routed
int IP_tos = 48; // AF12 << 2
// End of config stuff
OpusEncoder *Opus;
int Output_fd = -1;
float Audiodata[BUFFERSIZE];
int Samples_available;
int Wptr; // Write pointer for callback
static int pa_callback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData);
void cleanup(void);
void closedown(int);
// Convert unsigned number modulo buffersize to a signed 2's complement
static inline int signmod(unsigned int const a){
int y = a & (BUFFERSIZE-1);
if(y >= BUFFERSIZE/2)
y -= BUFFERSIZE;
assert(y >= -BUFFERSIZE/2 && y < BUFFERSIZE/2);
return y;
}
int main(int argc,char * const argv[]){
App_path = argv[0];
setlocale(LC_ALL,getenv("LANG"));
int c;
int List_audio = 0;
Mcast_ttl = 10; // By default, let Opus be routed
while((c = getopt(argc,argv,"I:vR:B:o:xT:Lf:p:V")) != EOF){
switch(c){
case 'L':
List_audio++;
break;
case 'p':
IP_tos = strtol(optarg,NULL,0);
break;
case 'T':
Mcast_ttl = strtol(optarg,NULL,0);
break;
case 'v':
Verbose++;
break;
case 'I':
Audiodev = optarg;
break;
case 'R':
Mcast_output_address_text = optarg;
break;
case 'B':
Opus_blocktime = strtod(optarg,NULL);
break;
case 'o':
Opus_bitrate = strtol(optarg,NULL,0);
break;
case 'x':
Discontinuous = 1;
break;
case 'f':
Fec = strtol(optarg,NULL,0);
break;
case 'V':
VERSION();
exit(EX_OK);
default:
fprintf(stderr,"Usage: %s [-V] [-x] [-v] [-o bitrate] [-B blocktime] [-I input_mcast_address] [-R output_mcast_address][-T mcast_ttl]\n",argv[0]);
fprintf(stderr,"Defaults: %s -o %d -B %.1f -I %s -R %s -T %d\n",argv[0],Opus_bitrate,Opus_blocktime,Audiodev,Mcast_output_address_text,Mcast_ttl);
exit(EX_USAGE);
}
}
// Compute opus parameters
if(Opus_blocktime != 2.5 && Opus_blocktime != 5
&& Opus_blocktime != 10 && Opus_blocktime != 20
&& Opus_blocktime != 40 && Opus_blocktime != 60
&& Opus_blocktime != 80 && Opus_blocktime != 100
&& Opus_blocktime != 120){
fprintf(stderr,"opus block time must be 2.5/5/10/20/40/60/80/100/120 ms\n");
fprintf(stderr,"80/100/120 supported only on opus 1.2 and later\n");
exit(EX_USAGE);
}
int Opus_frame_size = round(Opus_blocktime * Samprate / 1000.);
atexit(cleanup);
// Set up audio input
PaError r = Pa_Initialize();
if(r != paNoError){
fprintf(stderr,"Portaudio error: %s\n",Pa_GetErrorText(r));
close(Output_fd);
return r;
}
if(List_audio){
// On stdout, not stderr, so we can toss ALSA's noisy error messages
printf("Audio devices:\n");
int numDevices = Pa_GetDeviceCount();
for(int inDevNum=0; inDevNum < numDevices; inDevNum++){
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(inDevNum);
printf("%d: %s\n",inDevNum,deviceInfo->name);
}
exit(EX_OK);
}
int inDevNum,d;
char *nextp;
int numDevices = Pa_GetDeviceCount();
if(strlen(Audiodev) == 0){
// not specified; use default
inDevNum = Pa_GetDefaultOutputDevice();
} else if(d = strtol(Audiodev,&nextp,0),nextp != Audiodev && *nextp == '\0'){
if(d >= numDevices){
fprintf(stderr,"%d is out of range, use %s -L for a list\n",d,argv[0]);
exit(EX_IOERR);
}
inDevNum = d;
} else {
for(inDevNum=0; inDevNum < numDevices; inDevNum++){
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(inDevNum);
if(strcmp(deviceInfo->name,Audiodev) == 0)
break;
}
}
if(inDevNum == paNoDevice){
fprintf(stderr,"Portaudio: no available devices\n");
return -1;
}
PaStreamParameters inputParameters;
memset(&inputParameters,0,sizeof(inputParameters));
inputParameters.channelCount = Channels;
inputParameters.device = inDevNum;
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency = .001 * Opus_blocktime;
PaStream *Pa_Stream; // Portaudio stream handle
r = Pa_OpenStream(&Pa_Stream,
&inputParameters,
NULL, // No output stream
Samprate,
Opus_frame_size, // Read one Opus frame at a time
0,
pa_callback,
NULL);
if(r != paNoError){
fprintf(stderr,"Portaudio error: %s\n",Pa_GetErrorText(r));
close(Output_fd);
exit(EX_IOERR);
}
r = Pa_StartStream(Pa_Stream);
if(r != paNoError){
fprintf(stderr,"Portaudio error: %s\n",Pa_GetErrorText(r));
close(Output_fd);
exit(EX_IOERR);
}
// Opus is specified to operate between 6 kb/s and 510 kb/s
if(Opus_bitrate < 6000)
Opus_bitrate *= 1000; // Assume it was given in kb/s
if(Opus_bitrate > 510000)
Opus_bitrate = 510000;
int est_packet_size = round(Opus_bitrate * Opus_blocktime * .001/8);
if(est_packet_size > 1500){
fprintf(stderr,"Warning: estimated packet size %d bytes; IP framgmentation is likely\n",est_packet_size);
}
int error = 0;
Opus = opus_encoder_create(Samprate,Channels,OPUS_APPLICATION_AUDIO,&error);
if(error != OPUS_OK){
fprintf(stderr,"opus_encoder_create error %d\n",error);
exit(1);
}
error = opus_encoder_ctl(Opus,OPUS_SET_DTX(Discontinuous));
if(error != OPUS_OK){
fprintf(stderr,"opus_encoder_ctl set discontinuous %d: error %d\n",Discontinuous,error);
}
error = opus_encoder_ctl(Opus,OPUS_SET_BITRATE(Opus_bitrate));
if(error != OPUS_OK){
fprintf(stderr,"opus_encoder_ctl set bitrate %d: error %d\n",Opus_bitrate,error);
}
if(Fec){
error = opus_encoder_ctl(Opus,OPUS_SET_INBAND_FEC(1));
if(error != OPUS_OK)
fprintf(stderr,"opus_encoder_ctl set FEC on error %d\n",error);
error = opus_encoder_ctl(Opus,OPUS_SET_PACKET_LOSS_PERC(Fec));
if(error != OPUS_OK)
fprintf(stderr,"opus_encoder_ctl set FEC loss rate %d%% error %d\n",Fec,error);
}
// Always seems to return error -5 even when OK??
error = opus_encoder_ctl(Opus,OPUS_FRAMESIZE_ARG,(int)Opus_frame_size);
if(0 && error != OPUS_OK)
fprintf(stderr,"opus_encoder_ctl set framesize %d (%.1lf ms): error %d\n",Opus_frame_size,Opus_blocktime,error);
// Set up multicast transmit socket
if(!Mcast_output_address_text){
fprintf(stderr,"Must specify -R mcast_output_address\n");
exit(EX_USAGE);
}
Output_fd = setup_mcast(Mcast_output_address_text,NULL,1,Mcast_ttl,IP_tos,0,0);
if(Output_fd == -1){
fprintf(stderr,"Can't set up output on %s: %s\n",Mcast_output_address_text,strerror(errno));
exit(EX_IOERR);
}
// Set up to transmit Opus RTP/UDP/IP
struct rtp_state rtp_state_out;
memset(&rtp_state_out,0,sizeof(rtp_state_out));
rtp_state_out.ssrc = gps_time_sec();
// Graceful signal catch
signal(SIGPIPE,closedown);
signal(SIGINT,closedown);
signal(SIGKILL,closedown);
signal(SIGQUIT,closedown);
signal(SIGTERM,closedown);
signal(SIGPIPE,SIG_IGN);
int rptr = 0;
while(true){
// Wait for audio input
// I'd rather use pthread condition variables and signaling, but the portaudio people
// say you shouldn't do that in a callback. So we poll.
// Experimental "Zeno's paradox" delays to minimize number of loops without being too late
// we first sleep for half the frame time, then a quarter, and so forth until we approach
// the expected time of a new frame
int delay = Opus_blocktime * 1000;
while(signmod(Wptr - rptr) < Channels * Opus_frame_size){
if(delay >= 200)
delay /= 2; // Minimum sleep time 0.2 ms
usleep(delay);
}
float bouncebuffer[Channels * Opus_frame_size];
float *opus_input;
if(rptr + Channels * Opus_frame_size > BUFFERSIZE){
// wraps around; use bounce buffer
memcpy(bouncebuffer,Audiodata + rptr,sizeof(float)*(BUFFERSIZE-rptr));
memcpy(bouncebuffer + (BUFFERSIZE-rptr), Audiodata, sizeof(float) * (Channels * Opus_frame_size - (BUFFERSIZE-rptr)));
opus_input = bouncebuffer;
} else
opus_input = Audiodata + rptr;
rptr += Channels * Opus_frame_size;
if(rptr >= BUFFERSIZE)
rptr -= BUFFERSIZE;
struct rtp_header rtp_hdr;
memset(&rtp_hdr,0,sizeof(rtp_hdr));
rtp_hdr.version = RTP_VERS;
rtp_hdr.type = Opus_pt;
rtp_hdr.seq = rtp_state_out.seq;
rtp_hdr.ssrc = rtp_state_out.ssrc;
rtp_hdr.timestamp = rtp_state_out.timestamp;
uint8_t buffer[PKTSIZE]; // Biggest IP packet possible
uint8_t *dp = buffer;
dp = hton_rtp(dp,&rtp_hdr);
int size = opus_encode_float(Opus,opus_input,Opus_frame_size,dp,sizeof(buffer) - (dp - buffer));
if(!Discontinuous || size > 2){
dp += size;
send(Output_fd,buffer,dp - buffer,0);
rtp_state_out.seq++; // Increment RTP sequence number only if packet is sent
rtp_state_out.packets++;
rtp_state_out.bytes += size;
}
rtp_state_out.timestamp += Opus_frame_size; // Always increments, even if we suppress the frame
}
opus_encoder_destroy(Opus);
close(Output_fd);
exit(EX_OK);
}
// Portaudio callback - encode and transmit audio
// You're supposed to avoid synchronization calls here, but they seem to work
static int pa_callback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData){
float *in = (float *)inputBuffer;
assert(in != NULL);
int count = Channels*framesPerBuffer;
while(count--){
Audiodata[Wptr++] = *in++;
if(Wptr == BUFFERSIZE)
Wptr = 0;
}
return paContinue;
}
void cleanup(void){
Pa_Terminate();
if(Opus != NULL)
opus_encoder_destroy(Opus);
Opus = NULL;
if(Output_fd != -1)
close(Output_fd);
Output_fd = -1;
}
void closedown(int s){
fprintf(stderr,"Signal %d\n",s);
exit(EX_OK);
}