-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
347 lines (321 loc) · 9.13 KB
/
Copy pathmain.c
File metadata and controls
347 lines (321 loc) · 9.13 KB
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
/*
* main.cpp -- AIS Decoder
*
* Copyright (C) 2013
* Astra Paging Ltd / AISHub (info@aishub.net)
*
* AISDecoder is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* AISDecoder uses parts of GNUAIS project (http://gnuais.sourceforge.net/)
*
*/
#ifndef WIN32
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include "config.h"
#include "sounddecoder.h"
#include "callbacks.h"
#ifdef HAVE_ALSA
#include "alsaaudio/alsaaudio.h"
#endif
#ifndef WIN32
#ifdef HAVE_ALSA
#define HELP_AUDIO_DEVICE "-D\tALSA input device\n\tUse '-D list' to see all available system devices.\n"
#else
#define HELP_AUDIO_DEVICE ""
#endif
#ifdef HAVE_PULSEAUDIO
#ifdef HAVE_ALSA
#define HELP_AUDIO_DRIVERS "pulse,alsa,file"
#else
#define HELP_AUDIO_DRIVERS "pulse,file"
#endif
#else
#ifdef HAVE_ALSA
#define HELP_AUDIO_DRIVERS "alsa,file"
#endif
#endif
#else
#define HELP_AUDIO_DRIVERS "winmm,file"
#define HELP_AUDIO_DEVICE "-D\tNumeric value for input device (default WAVE_MAPPER)\n\tUse '-D list' to see all available system devices.\n"
#endif
#define HELP_MSG \
"Usage: " PROJECT_NAME " -h hostname -p port -a " HELP_AUDIO_DRIVERS " [-f /path/file.raw] [-l]\n\n"\
"-h\tDestination host or IP address\n"\
"-p\tDestination UDP port\n"\
"-a\tAudio driver [" HELP_AUDIO_DRIVERS "]\n" HELP_AUDIO_DEVICE\
"-c\tSound channels [stereo,mono,left,right] (default stereo)\n"\
"-f\tFull path to 48kHz raw audio file. Use \"-\" for stdin. \n"\
"-l\tLog sound levels to console (stderr)\n"\
"-d\tLog NMEA sentences to console (stderr)\n"\
"-H\tDisplay this help\n"
#define MAX_BUFFER_LENGTH 2048
static char buffer[MAX_BUFFER_LENGTH];
static unsigned int buffer_count=0;
#ifdef WIN32
WSADATA wsaData;
void printInDevices() {
unsigned int count = waveInGetNumDevs();
WAVEINCAPS caps;
unsigned int i = 0;
for (i = 0; i < count; i++) {
if (waveInGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR) {
fprintf(stderr, "%u: %s\r\n", i, caps.szPname);
} else {
fprintf(stderr, "Can't read devices\r\n");
exit(EXIT_FAILURE);
}
}
}
#endif
#ifdef HAVE_ALSA
void printInDevices() {
char **hints;
/* Enumerate sound devices */
int err = snd_device_name_hint(0, "pcm", (void***)&hints);
if (err != 0) {
fprintf(stderr, "Can't read data\n");
exit(EXIT_FAILURE);
}
char** n = hints;
while (*n != NULL) {
char *name = snd_device_name_get_hint(*n, "NAME");
char *desc = snd_device_name_get_hint(*n, "DESC");
char *type = snd_device_name_get_hint(*n, "IOID");
if (name != NULL && desc != NULL) {
if (type == NULL || !strcmp(type, "Input")) {
printf("%s\n%s: <%s>\n\n", desc, type == NULL ? "Input/Output" : "Input",name);
}
}
if (name != NULL) free(name);
if (name != NULL) free(desc);
if (name != NULL) free(type);
n++;
}
//Free hint buffer too
snd_device_name_free_hint((void**)hints);
snd_config_update_free_global();
}
#endif
static int sock;
static struct addrinfo* addr=NULL;
static int initSocket(const char *host, const char *portname);
static int show_levels=0;
static int debug_nmea=0;
void sound_level_changed(float level, int channel, unsigned char high) {
if (high != 0)
fprintf(stderr, "Level on ch %d too high: %.0f %%\n", channel, level);
else
fprintf(stderr, "Level on ch %d: %.0f %%\n", channel, level);
}
void nmea_sentence_received(const char *sentence,
unsigned int length,
unsigned char sentences,
unsigned char sentencenum) {
if (sentences == 1) {
if (sendto(sock, sentence, length, 0, addr->ai_addr, addr->ai_addrlen) == -1) abort();
if (debug_nmea) fprintf(stderr, "%s", sentence);
} else {
if (buffer_count + length < MAX_BUFFER_LENGTH) {
memcpy(&buffer[buffer_count], sentence, length);
buffer_count += length;
} else {
buffer_count=0;
}
if (sentences == sentencenum && buffer_count > 0) {
if (sendto(sock, buffer, buffer_count, 0, addr->ai_addr, addr->ai_addrlen) == -1) abort();
if (debug_nmea) fprintf(stderr, "%s", buffer);
buffer_count=0;
};
}
}
#define CMD_PARAMS_COMMON "h:p:a:lHf:dc:"
#ifndef WIN32
#ifdef HAVE_ALSA
#define CMD_PARAMS CMD_PARAMS_COMMON "D:"
#else
#define CMD_PARAMS CMD_PARAMS_COMMON
#endif
#else
#define CMD_PARAMS CMD_PARAMS_COMMON "D:"
#endif
int main(int argc, char *argv[]) {
Sound_Channels channels = SOUND_CHANNELS_STEREO;
char *host, *port, *file_name=NULL;
const char *params=CMD_PARAMS;
int alsa=0, pulse=0, file=0, winmm=0;
int hfnd=0, pfnd=0, afnd=0;
#ifdef WIN32
unsigned int deviceId=WAVE_MAPPER;
#endif
#ifdef HAVE_ALSA
char *alsaDevice=NULL;
#endif
int opt;
while ((opt = getopt(argc, argv, params)) != -1) {
switch (opt) {
case 'h':
host = optarg;
hfnd = 1;
break;
case 'p':
port = optarg;
pfnd = 1;
break;
case 'a':
#ifdef HAVE_PULSEAUDIO
pulse = strcmp(optarg, "pulse") == 0;
#endif
#ifdef HAVE_ALSA
alsa = strcmp(optarg, "alsa") == 0;
#endif
#ifdef WIN32
winmm = strcmp(optarg, "winmm") == 0;
#endif
file = strcmp(optarg, "file") == 0;
afnd = 1;
break;
case 'c':
if (!strcmp(optarg, "mono")) channels = SOUND_CHANNELS_MONO;
else if (!strcmp(optarg, "left")) channels = SOUND_CHANNELS_LEFT;
else if (!strcmp(optarg, "right")) channels = SOUND_CHANNELS_RIGHT;
break;
case 'l':
show_levels = 1;
break;
case 'f':
file_name = optarg;
break;
case 'd':
debug_nmea = 1;
break;
#ifdef WIN32
case 'D':
if (!strcmp(optarg, "list")) {
printInDevices();
return EXIT_SUCCESS;
} else {
deviceId = atoi(optarg);
}
break;
#endif
#ifdef HAVE_ALSA
case 'D':
if (!strcmp(optarg, "list")) {
printInDevices();
return EXIT_SUCCESS;
} else {
alsaDevice = optarg;
}
break;
#endif
case 'H':
default:
fprintf(stderr, HELP_MSG);
return EXIT_SUCCESS;
break;
}
}
if (argc < 2) {
fprintf(stderr, HELP_MSG);
return EXIT_FAILURE;
}
if (!hfnd) {
fprintf(stderr, "Host is not set\n");
return EXIT_FAILURE;
}
if (!pfnd) {
fprintf(stderr, "Port is not set\n");
return EXIT_FAILURE;
}
if (!afnd) {
fprintf(stderr, "Audio driver is not set\n");
return EXIT_FAILURE;
}
if (!alsa && !pulse && !winmm && !file) {
fprintf(stderr, "Invalid audio driver\n");
return EXIT_FAILURE;
}
if (!initSocket(host, port)) {
return EXIT_FAILURE;
}
if (show_levels) on_sound_level_changed=sound_level_changed;
on_nmea_sentence_received=nmea_sentence_received;
Sound_Driver driver = DRIVER_FILE;
#ifdef HAVE_ALSA
if (alsa) driver = DRIVER_ALSA;
#endif
#ifdef HAVE_PULSEAUDIO
if (pulse) driver = DRIVER_PULSE;
#endif
int OK=0;
#ifdef WIN32
if (!file) driver = DRIVER_WINMM;
OK=initSoundDecoder(channels, driver, file_name, deviceId);
#else
#ifdef HAVE_ALSA
OK=initSoundDecoder(channels, driver, file_name, alsaDevice);
#else
OK=initSoundDecoder(channels, driver, file_name);
#endif
#endif
int stop=0;
if (OK) {
runSoundDecoder(&stop);
} else {
fprintf(stderr, "%s\n", errorSoundDecoder);
}
freeSoundDecoder();
freeaddrinfo(addr);
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
int initSocket(const char *host, const char *portname) {
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_DGRAM;
hints.ai_protocol=IPPROTO_UDP;
#ifndef WIN32
hints.ai_flags=AI_ADDRCONFIG;
#else
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 0;
}
#endif
int err=getaddrinfo(host, portname, &hints, &addr);
if (err!=0) {
fprintf(stderr, "Failed to resolve remote socket address!\n");
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
sock=socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sock==-1) {
fprintf(stderr, "%s",strerror(errno));
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
return 1;
}