This repository has been archived by the owner on Jun 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
iqrecord.c
321 lines (275 loc) · 8.89 KB
/
iqrecord.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
// $Id: iqrecord.c,v 1.22 2018/12/02 09:16:45 karn Exp $
// Read and record complex I/Q stream or PCM baseband audio
// This version reverts to file I/O from an unsuccessful experiment to use mmap()
// Copyright 2018 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#if defined(linux)
#include <bsd/string.h>
#endif
#include <math.h>
#include <complex.h>
#undef I
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <locale.h>
#include <signal.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include "radio.h"
#include "attr.h"
#include "multicast.h"
// Largest Ethernet packet
// Normally this would be <1500,
// but what about Ethernet interfaces that can reassemble fragments?
// 65536 should be safe since that's the largest IPv4 datagram.
// But what about IPv6?
#define MAXPKT 65535
// size of stdio buffer for disk I/O
// This should be large to minimize write calls, but how big?
#define BUFFERSIZE (1<<20)
// One for each session being recorded
struct session {
struct session *next;
struct sockaddr iq_sender; // Sender's IP address and source port
uint32_t ssrc; // RTP stream source ID
struct rtp_state rtp_state;
int type; // RTP payload type (with marker stripped)
int channels; // 1 (PCM_MONO) or 2 (PCM_STEREO or IQ)
long long source_timestamp; // Timestamp from status header (IQ only)
double frequency; // Tuner LO frequency (IQ only)
unsigned int samprate; // Nominal sampling rate (explicit in IQ, implicitly 48 kHz in PCM)
FILE *fp; // File being recorded
void *iobuffer;
};
int Quiet;
int Mcast_ttl = 0; // We don't transmit
double Duration = INFINITY;
char IQ_mcast_address_text[256];
struct sockaddr Sender;
struct sockaddr Input_mcast_sockaddr;
int Input_fd;
struct session *Sessions;
void closedown(int a);
void input_loop(void);
void cleanup(void);
int main(int argc,char *argv[]){
#if 0 // Better done manually or in systemd?
// if we have root, up our priority and drop privileges
int prio = getpriority(PRIO_PROCESS,0);
prio = setpriority(PRIO_PROCESS,0,prio - 10);
// Quickly drop root if we have it
// The sooner we do this, the fewer options there are for abuse
if(seteuid(getuid()) != 0)
perror("seteuid");
#endif
char *locale;
locale = getenv("LANG");
setlocale(LC_ALL,locale);
// Defaults
Quiet = 0;
int c;
while((c = getopt(argc,argv,"I:l:qd:")) != EOF){
switch(c){
case 'I':
strlcpy(IQ_mcast_address_text,optarg,sizeof(IQ_mcast_address_text));
break;
case 'l':
locale = optarg;
break;
case 'q':
Quiet++; // Suppress display
break;
case 'd':
Duration = strtod(optarg,NULL);
break;
default:
fprintf(stderr,"Usage: %s -I iq multicast address [-l locale] [-q]\n",argv[0]);
exit(1);
break;
}
}
if(strlen(IQ_mcast_address_text) == 0){
fprintf(stderr,"Specify -I IQ_mcast_address_text_address\n");
exit(1);
}
setlocale(LC_ALL,locale);
// Set up input socket for multicast data stream from front end
Input_fd = setup_mcast(IQ_mcast_address_text,NULL,0,Mcast_ttl,0);
if(Input_fd == -1){
fprintf(stderr,"Can't set up I/Q input\n");
exit(1);
}
int n = 1 << 20; // 1 MB
if(setsockopt(Input_fd,SOL_SOCKET,SO_RCVBUF,&n,sizeof(n)) == -1)
perror("setsockopt");
// Graceful signal catch
signal(SIGPIPE,closedown);
signal(SIGINT,closedown);
signal(SIGKILL,closedown);
signal(SIGQUIT,closedown);
signal(SIGTERM,closedown);
signal(SIGPIPE,SIG_IGN);
atexit(cleanup);
input_loop(); // Doesn't return
exit(0);
}
void closedown(int a){
if(!Quiet)
fprintf(stderr,"iqrecord: caught signal %d: %s\n",a,strsignal(a));
exit(1); // Will call cleanup()
}
// Read from RTP network socket, assemble blocks of samples
void input_loop(){
char filename[PATH_MAX];
memset(filename,0,sizeof(filename));
double t = 0;
while(!isfinite(Duration) || t < Duration){
// Receive I/Q data from front end
unsigned char buffer[MAXPKT];
socklen_t socksize = sizeof(Sender);
int size = recvfrom(Input_fd,buffer,sizeof(buffer),0,&Sender,&socksize);
if(size <= 0){ // ??
perror("recvfrom");
usleep(50000);
continue;
}
if(size < RTP_MIN_SIZE)
continue; // Too small for RTP, ignore
unsigned char *dp = buffer;
struct rtp_header rtp;
dp = ntoh_rtp(&rtp,dp);
if(rtp.pad){
// Remove padding
size -= dp[size-1];
rtp.pad = 0;
}
// I/Q status header (if present) is in host byte order
struct status status;
if(rtp.type == IQ_PT)
dp = ntoh_status(&status,dp);
else
memset(&status,0,sizeof(status));
signed short *samples = (signed short *)dp;
size -= (dp - buffer);
struct session *sp;
for(sp = Sessions;sp != NULL;sp=sp->next){
if(sp->ssrc == rtp.ssrc
&& rtp.type == sp->type
&& memcmp(&sp->iq_sender,&Sender,sizeof(sp->iq_sender)) == 0
&& (rtp.type != IQ_PT || sp->frequency == status.frequency)){
break;
}
}
if(sp == NULL){ // Not found; create new one
sp = calloc(1,sizeof(*sp));
// Initialize entry
sp->next = Sessions;
Sessions = sp;
memcpy(&sp->iq_sender,&Sender,sizeof(sp->iq_sender));
sp->type = rtp.type;
sp->ssrc = rtp.ssrc;
switch(sp->type){
case PCM_MONO_PT:
sp->channels = 1;
sp->samprate = 48000;
sp->frequency = 0; // Not applicable
break;
case PCM_STEREO_PT:
sp->channels = 2;
sp->samprate = 48000;
sp->frequency = 0; // Not applicable
break;
case IQ_PT:
sp->channels = 2;
sp->frequency = status.frequency;
sp->samprate = status.samprate;
sp->source_timestamp = status.timestamp; // Timestamp from IQ status header
break;
}
// Create file with name iqrecord-frequency-ssrc or pcmrecord-ssrc
int suffix;
for(suffix=0;suffix<100;suffix++){
struct stat statbuf;
if(status.frequency)
snprintf(filename,sizeof(filename),"iqrecord-%.1lfHz-%lx-%d",sp->frequency,(long unsigned)sp->ssrc,suffix);
else
snprintf(filename,sizeof(filename),"pcmrecord-%lx-%d",(long unsigned)sp->ssrc,suffix);
if(stat(filename,&statbuf) == -1 && errno == ENOENT)
break;
}
if(suffix == 100){
fprintf(stderr,"Can't generate filename %s to write\n",filename);
// After this many tries, something is probably seriously wrong
exit(1);
}
sp->fp = fopen(filename,"w+");
if(sp->fp == NULL){
fprintf(stderr,"can't write file %s\n",filename);
perror("open");
continue;
}
if(!Quiet)
fprintf(stderr,"creating file %s\n",filename);
sp->iobuffer = malloc(BUFFERSIZE);
setbuffer(sp->fp,sp->iobuffer,BUFFERSIZE);
int const fd = fileno(sp->fp);
fcntl(fd,F_SETFL,O_NONBLOCK); // Let's see if this keeps us from losing data
attrprintf(fd,"samplerate","%lu",(unsigned long)sp->samprate);
attrprintf(fd,"channels","%d",sp->channels);
attrprintf(fd,"ssrc","%lx",(long unsigned)rtp.ssrc);
switch(sp->type){
case IQ_PT:
attrprintf(fd,"sampleformat","s16le");
attrprintf(fd,"frequency","%.3lf",sp->frequency);
attrprintf(fd,"source_timestamp","%lld",sp->source_timestamp);
break;
case PCM_MONO_PT:
case PCM_STEREO_PT:
attrprintf(fd,"sampleformat","s16be");
break;
case OPUS_PT: // No support yet; should put in container
break;
}
char sender_text[NI_MAXHOST];
// Don't wait for an inverse resolve that might cause us to lose data
getnameinfo((struct sockaddr *)&Sender,sizeof(Sender),sender_text,sizeof(sender_text),NULL,0,NI_NOFQDN|NI_DGRAM|NI_NUMERICHOST);
attrprintf(fd,"source","%s",sender_text);
attrprintf(fd,"multicast","%s",IQ_mcast_address_text);
struct timeval tv;
gettimeofday(&tv,NULL);
attrprintf(fd,"unixstarttime","%ld.%06ld",(long)tv.tv_sec,(long)tv.tv_usec);
}
int sample_count = size / (sizeof(*samples) * sp->channels);
off_t offset = rtp_process(&sp->rtp_state,&rtp,sample_count);
// The seek offset relative to the current position in the file is the signed (modular) difference between
// the actual and expected RTP timestamps. This should automatically handle
// 32-bit RTP timestamp wraps, which occur every ~1 days at 48 kHz and only 6 hr @ 192 kHz
// Should I limit the range on this?
if(offset)
fseeko(sp->fp,offset,SEEK_CUR);
fwrite(samples,1,size,sp->fp);
t += (double)sample_count / sp->samprate;
}
}
void cleanup(void){
while(Sessions){
// Flush and close each write stream
// Be anal-retentive about freeing and clearing stuff even though we're about to exit
struct session *next_s = Sessions->next;
fflush(Sessions->fp);
fclose(Sessions->fp);
Sessions->fp = NULL;
free(Sessions->iobuffer);
Sessions->iobuffer = NULL;
free(Sessions);
Sessions = next_s;
}
}