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
/
iqplay.c
189 lines (164 loc) · 5.11 KB
/
iqplay.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
// $Id: iqplay.c,v 1.30 2018/12/02 09:16:45 karn Exp $
// Read from IQ recording, multicast in (hopefully) real time
// Copyright 2018 Phil Karn, KA9Q
#define _GNU_SOURCE 1 // allow bind/connect/recvfrom without casting sockaddr_in6
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <locale.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include "misc.h"
#include "radio.h"
#include "multicast.h"
#include "attr.h"
int Verbose;
int Mcast_ttl = 1; // Don't send fast IQ streams beyond the local network by default
double Default_frequency = 0;
long Default_samprate = 192000;
int Blocksize = 256;
int Rtp_sock; // Socket handle for sending real time stream
// Play I/Q file with descriptor 'fd' on network socket 'sock'
int playfile(int sock,int fd,int blocksize){
struct status status;
memset(&status,0,sizeof(status));
status.samprate = Default_samprate; // Not sure this is useful
status.frequency = Default_frequency;
attrscanf(fd,"samplerate","%ld",&status.samprate);
attrscanf(fd,"frequency","%lf",&status.frequency);
if(attrscanf(fd,"source_timestamp","%lld",&status.timestamp) == -1){
double unixstarttime;
attrscanf(fd,"unixstarttime","%lf",&unixstarttime);
// Convert decimal seconds from UNIX epoch to integer nanoseconds from GPS epoch
status.timestamp = (unixstarttime - UNIX_EPOCH + GPS_UTC_OFFSET) * 1000000000LL;
}
if(Verbose)
fprintf(stderr,": start time %s, %'d samp/s, RF LO %'.1lf Hz\n",lltime(status.timestamp),status.samprate,status.frequency);
struct rtp_header rtp;
memset(&rtp,0,sizeof(rtp));
rtp.version = RTP_VERS;
rtp.type = IQ_PT; // ordinarily dynamically allocated
struct timeval start_time;
gettimeofday(&start_time,NULL);
rtp.ssrc = start_time.tv_sec;
int timestamp = 0;
int seq = 0;
// microsec between packets. Double precision is used to avoid small errors that could
// accumulate over time
double dt = (1000000. * blocksize) / status.samprate;
// Microseconds since start for next scheduled transmission; will transmit first immediately
double sked_time = 0;
while(1){
rtp.seq = seq++;
rtp.timestamp = timestamp;
timestamp += blocksize;
// Is it time yet?
while(1){
// Microseconds since start
struct timeval tv,diff;
gettimeofday(&tv,NULL);
timersub(&tv,&start_time,&diff);
double rt = 1000000. * diff.tv_sec + diff.tv_usec;
if(rt >= sked_time)
break;
if(sked_time > rt + 100){
// Use care here, s is unsigned
useconds_t s = (sked_time - rt) - 100; // sleep until 100 microseconds before
usleep(s);
}
}
unsigned char output_buffer[4*blocksize + 256]; // will this allow for largest possible RTP header??
unsigned char *dp = output_buffer;
dp = hton_rtp(dp,&rtp);
dp = hton_status(dp,&status);
if(pipefill(fd,dp,4*blocksize) <= 0)
break;
dp += 4*blocksize;
int length = dp - output_buffer;
if(send(sock,output_buffer,length,0) == -1)
perror("send");
// Update time of next scheduled transmission
sked_time += dt;
// Update nanosecond timestamp
status.timestamp += blocksize * (long long)1e9 / status.samprate;
}
return 0;
}
int main(int argc,char *argv[]){
#if 0 // Better done manually?
// 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 *dest = "iq.playback.mcast.local"; // Default for testing
char *locale = getenv("LANG");
int c;
while((c = getopt(argc,argv,"vl:b:R:f:r:T:")) != EOF){
switch(c){
case 'R':
dest = optarg;
break;
case 'r':
Default_samprate = strtol(optarg,NULL,0);
break;
case 'T':
Mcast_ttl = strtol(optarg,NULL,0);
break;
case 'v':
Verbose++;
break;
case 'l':
locale = optarg;
break;
case 'b':
Blocksize = strtol(optarg,NULL,0);
break;
case 'f': // Used only if there's no tag on a file, or for stdin
Default_frequency = strtod(optarg,NULL);
break;
}
}
if(argc < optind){
fprintf(stderr,"Usage: %s [options] [filename]\n",argv[0]);
exit(1);
}
setlocale(LC_ALL,locale);
// Set up RTP output socket
Rtp_sock = setup_mcast(dest,NULL,1,Mcast_ttl,0);
signal(SIGPIPE,SIG_IGN);
if(optind == argc){
// No file arguments, read from stdin
if(Verbose)
fprintf(stderr,"Transmitting from stdin");
playfile(Rtp_sock,0,Blocksize);
} else {
for(int i=optind;i<argc;i++){
int fd;
if((fd = open(argv[i],O_RDONLY)) == -1){
fprintf(stderr,"Can't read %s; ",argv[i]);
perror("");
continue;
}
if(Verbose)
fprintf(stderr,"Transmitting %s",argv[i]);
playfile(Rtp_sock,fd,Blocksize);
close(fd);
fd = -1;
}
}
close(Rtp_sock);
Rtp_sock = -1;
exit(0);
}