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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
/*
This software is Copyright (C) 2021-2022 by Ben Cahill and 2006-2022 by James C. Ahlstrom,
and is licensed for use under the GNU General Public License (GPL).
See http://www.opensource.org.
Note that there is NO WARRANTY AT ALL. USE AT YOUR OWN RISK!!
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <complex.h>
#include <math.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#ifdef MS_WINDOWS
#include <winsock2.h>
#include <stdint.h>
#else
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD)
#include <netinet/in.h>
#endif
#endif
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <fcntl.h>
#endif
#include "../quisk.h"
#include "../filter.h"
#define GRAPH_DATA_SCALE 163
#define MAX_UDP_INT16_T 600
#define UDP_SEND_INT16 200
#define REMOTE_DEBUG 0 //BMC TODO: Make this a configuration option
static SOCKET remote_radio_sound_socket = INVALID_SOCKET; // send radio sound to control_head, receive mic samples
static SOCKET control_head_sound_socket = INVALID_SOCKET; // receive radio sound from remote_radio, send mic samples
static SOCKET remote_radio_graph_socket = INVALID_SOCKET; // send graph data to control_head
static SOCKET control_head_graph_socket = INVALID_SOCKET; // receive graph data from remote_radio
static int control_head_sound_socket_started = 0; // sound stream started on the control head
static int remote_radio_sound_socket_started = 0; // sound stream started on the remote radio
static int control_head_graph_socket_started = 0; // graph data stream started on the control head
static int remote_radio_graph_socket_started = 0; // graph data stream started on the remote radio
static int control_head_data_width; // app.data_width of control head for graph data
static int packets_sent;
static int packets_recd;
// Receive stereo 16-bit pcm radio speaker sound on the control head via UDP
int read_remote_radio_sound_socket(complex double * cSamples)
{
int i, bytes, nInt16, nSamples;
int16_t buf[UDP_SEND_INT16];
double samp_r, samp_l;
struct timeval tm_wait;
fd_set fds;
static struct quisk_cHB45Filter HalfBand;
static struct quisk_cFilter cFiltInterp3;
static int init_filters=1;
if (control_head_sound_socket == INVALID_SOCKET)
return 0;
if (init_filters) {
init_filters = 0;
memset(&HalfBand, 0, sizeof(struct quisk_cHB45Filter));
quisk_filt_cInit(&cFiltInterp3, quiskAudio24p3Coefs, sizeof(quiskAudio24p3Coefs)/sizeof(double));
}
// Signal far end (server) that we're ready (this sends our address/port to far end)
if (!control_head_sound_socket_started) {
QuiskPrintf("read_remote_radio_sound_socket() sending 'rr'\n");
bytes = send(control_head_sound_socket, "rr\n", 3, 0);
if (bytes != 3)
QuiskPrintf("read_remote_radio_sound_socket(), sendto(): %s\n", strerror(errno));
}
// read all available packets, one per loop
nSamples = 0;
while (1) {
tm_wait.tv_sec = 0;
tm_wait.tv_usec = 0;
FD_ZERO (&fds);
FD_SET (control_head_sound_socket, &fds);
if (select(control_head_sound_socket + 1, &fds, NULL, NULL, &tm_wait) != 1) {
//BMC QuiskPrintf("read_remote_radio_sound_socket(): select returned %i\n", retval);
break;
}
bytes = recv(control_head_sound_socket, (char *)buf, UDP_SEND_INT16 * 2, 0);
if (bytes < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK)
QuiskPrintf("read_remote_radio_sound_socket(), recv(): %s\n", strerror(errno));
break;
}
if (bytes > 0) {
control_head_sound_socket_started = 1;
nInt16 = bytes / 2;
for (i = 0; i < nInt16; i += 2) {
samp_r = buf[i];
samp_l = buf[i + 1];
cSamples[nSamples++] = (samp_r + I * samp_l) / CLIP16 * CLIP32;
}
}
} // while(1)
nSamples = quisk_cInterpolate(cSamples, nSamples, &cFiltInterp3, 3);
nSamples = quisk_cInterp2HB45(cSamples, nSamples, &HalfBand);
return nSamples;
}
// Receive stereo 16-bit pcm microphone samples at the remote radio via UDP
int read_remote_mic_sound_socket(complex double * cSamples)
{
int i, bytes, nInt16, nSamples;
int16_t buf[UDP_SEND_INT16];
double samp_r, samp_l;
struct timeval tm_wait;
fd_set fds;
static struct quisk_cHB45Filter HalfBand;
static struct quisk_cFilter cFiltInterp3;
static int init_filters=1;
if (remote_radio_sound_socket == INVALID_SOCKET)
return 0;
if (init_filters) {
init_filters = 0;
memset(&HalfBand, 0, sizeof(struct quisk_cHB45Filter));
quisk_filt_cInit(&cFiltInterp3, quiskAudio24p3Coefs, sizeof(quiskAudio24p3Coefs)/sizeof(double));
}
// read all available packets, one per loop
nSamples = 0;
while (1) {
tm_wait.tv_sec = 0;
tm_wait.tv_usec = 0;
FD_ZERO (&fds);
FD_SET (remote_radio_sound_socket, &fds);
if (select(remote_radio_sound_socket + 1, &fds, NULL, NULL, &tm_wait) != 1) {
//BMC QuiskPrintf("read_remote_mic_sound_socket(): select returned %i\n", retval);
break;
}
bytes = recv(remote_radio_sound_socket, (char *)buf, UDP_SEND_INT16 * 2, 0);
if (bytes < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK)
QuiskPrintf("read_remote_mic_sound_socket(), recv(): %s\n", strerror(errno));
break;
}
if (bytes > 0) {
nInt16 = bytes / 2;
for (i = 0; i < nInt16; i += 2) {
samp_r = buf[i];
samp_l = buf[i + 1];
cSamples[nSamples++] = (samp_r + I * samp_l) / CLIP16 * CLIP32;
}
}
} // while(1)
nSamples = quisk_cInterpolate(cSamples, nSamples, &cFiltInterp3, 3);
nSamples = quisk_cInterp2HB45(cSamples, nSamples, &HalfBand);
return nSamples;
}
// Send stereo 16-bit pcm sound via UDP
// This code acts as UDP server for radio sound (on remote radio) or mic sound (on control head)
#define MAX_SAMPLES_FOR_REMOTE_SOUND 15000
#define RX_BUFFER_SIZE 64
// Send microphone samples from the control head to the remote radio
void send_remote_mic_sound_socket(complex double * cSamples, int nSamples)
{
int i;
ssize_t sent;
static int buffer_index=0;
static int16_t buffer[UDP_SEND_INT16];
static struct quisk_cHB45Filter HalfBand;
static struct quisk_cFilter cFiltDecim3;
static int init_filters=1, size_cBuf=0;
static complex double * cBuf=NULL;
if (init_filters) {
init_filters = 0;
memset(&HalfBand, 0, sizeof(struct quisk_cHB45Filter));
quisk_filt_cInit(&cFiltDecim3, quiskAudio24p3Coefs, sizeof(quiskAudio24p3Coefs)/sizeof(double));
}
if (nSamples > size_cBuf) {
size_cBuf = nSamples;
cBuf = (complex double *)realloc(cBuf, size_cBuf * sizeof(complex double));
}
if (control_head_sound_socket == INVALID_SOCKET)
return;
if (!control_head_sound_socket_started)
return;
memcpy(cBuf, cSamples, nSamples * sizeof(complex double)); // Do not alter cSamples
// Reduce sample rate from 48 to 8 ksps
nSamples = quisk_cDecim2HB45(cBuf, nSamples, &HalfBand);
nSamples = quisk_cDecimate(cBuf, nSamples, &cFiltDecim3, 3);
// Convert format from complex double to stereo pairs of 16-bit PCM samples.
// Buffer samples until UDP_SEND_INT16 are available, and then send the block.
for (i = 0; i < nSamples; i++) {
buffer[buffer_index++] = (int16_t)(creal(cBuf[i]) * (double)CLIP16 / CLIP32);
buffer[buffer_index++] = (int16_t)(cimag(cBuf[i]) * (double)CLIP16 / CLIP32);
if (buffer_index >= UDP_SEND_INT16) {
sent = send(control_head_sound_socket, (const char *)buffer, buffer_index * 2, 0);
if (sent != buffer_index * 2)
QuiskPrintf("send_remote_mic_sound_socket(), send(): %s\n", strerror(errno));
buffer_index = 0;
}
}
}
// Send radio speaker sound from the remote radio to the control head
void send_remote_radio_sound_socket(complex double * cSamples, int nSamples)
{ // Send nSamples samples. Each sample is sent as two shorts (4 bytes) of L/R audio data.
int i, N, sent;
static int16_t sound_lr[UDP_SEND_INT16];
static int udp_size = 0; // Keep track of UDP payload size, in shorts
char buf[RX_BUFFER_SIZE]; // For startup message
int recv_len;
int retval;
SOCKET * sock = &remote_radio_sound_socket;
static struct quisk_cHB45Filter HalfBand;
static struct quisk_cFilter cFiltDecim3;
static int init_filters=1, size_cBuf=0;
static complex double * cBuf=NULL;
#if REMOTE_DEBUG > 0 //BMC debug
// measure/monitor tools:
static float callcount = 0;
static float sampcount = 0;
static uint64_t bunchcount = 0;
static float callcount_total = 0;
static float sampcount_total = 0;
static double prior_ts = 0;
static double delta_total = 0;
#if REMOTE_DEBUG > 1 //BMC debug
static double prior_packet_ts = 0.0;
double now;
#endif
#endif
if (*sock == INVALID_SOCKET)
return;
if (init_filters) {
init_filters = 0;
memset(&HalfBand, 0, sizeof(struct quisk_cHB45Filter));
quisk_filt_cInit(&cFiltDecim3, quiskAudio24p3Coefs, sizeof(quiskAudio24p3Coefs)/sizeof(double));
}
if (nSamples > size_cBuf) {
size_cBuf = nSamples;
cBuf = (complex double *)realloc(cBuf, size_cBuf * sizeof(complex double));
}
#if REMOTE_DEBUG > 0 //BMC debug
callcount++;
#endif
// Wait for far end (client) to send its opening greetings, so we can grab its network address/port
if (!remote_radio_sound_socket_started) {
struct sockaddr_in far_addr;
#ifdef MS_WINDOWS
int addr_len = sizeof(struct sockaddr_in);
#else
socklen_t addr_len = sizeof(struct sockaddr_in);
#endif
struct timeval tm_wait;
fd_set fds;
tm_wait.tv_sec = 0;
tm_wait.tv_usec = 0;
FD_ZERO (&fds);
FD_SET (*sock, &fds);
if ((retval = select(*sock + 1, &fds, NULL, NULL, &tm_wait)) != 1) {
//BMC QuiskPrintf("send_remote_sound_socket(): select returned %i\n", retval);
return;
}
// Receive short msg, grab far end address
if ((recv_len = recvfrom(*sock, buf, RX_BUFFER_SIZE, 0, (struct sockaddr *) &far_addr, &addr_len)) == -1) {
QuiskPrintf("send_remote_sound_socket(), recvfrom(): %s\n", strerror(errno));
return;
}
else if(recv_len > 0) {
if (recv_len >= RX_BUFFER_SIZE)
buf[RX_BUFFER_SIZE - 1] = '\n';
else
buf[recv_len] = '\n';
QuiskPrintf("send_remote_sound_socket(): recv_len = %i, %s", recv_len, buf);
if (connect(*sock, (const struct sockaddr *)&far_addr, sizeof(far_addr)) != 0) {
QuiskPrintf("send_remote_sound_socket), connect(): %s\n", strerror(errno));
close(*sock);
*sock = INVALID_SOCKET;
}
else
remote_radio_sound_socket_started = 1;
}
}
memcpy(cBuf, cSamples, nSamples * sizeof(complex double)); // Do not alter cSamples
// Reduce sample rate from 48 to 8 ksps
nSamples = quisk_cDecim2HB45(cBuf, nSamples, &HalfBand);
nSamples = quisk_cDecimate(cBuf, nSamples, &cFiltDecim3, 3);
// Convert format from complex double to stereo pairs of 16-bit PCM samples, send to client
for (i = 0; i < nSamples; i++) {
sound_lr[udp_size++] = (int16_t)(creal(cBuf[i]) * (double)CLIP16 / CLIP32);
sound_lr[udp_size++] = (int16_t)(cimag(cBuf[i]) * (double)CLIP16 / CLIP32);
if (udp_size >= UDP_SEND_INT16) {
N = UDP_SEND_INT16;
udp_size = 0;
sent = send(*sock, (char *)sound_lr, N * 2, 0);
if (sent != N * 2)
QuiskPrintf("send_remote_sound_socket(), send(): %s\n", strerror(errno));
#if REMOTE_DEBUG > 0 //BMC debug
else {
sampcount += sent/4;
packets_sent++;
#if REMOTE_DEBUG > 1 //BMC debug
now = QuiskTimeSec();
QuiskPrintf("%f, send_remote_sound_socket(): now - prior = %f, samples = %u, sampcount = %lu\n",
now, now - prior_packet_ts, sent / 4, sampcount);
prior_packet_ts = now;
#endif
}
#endif
}
#if REMOTE_DEBUG > 1 //BMC debug
else {
now = QuiskTimeSec();
QuiskPrintf("%f, send_remote_sound_socket(): nSamples = %i\n", now, nSamples);
}
#endif
}
#if REMOTE_DEBUG > 0 //BMC debug
if (callcount >= 200) {
double new_ts = QuiskTimeSec();
double delta = new_ts - prior_ts;
prior_ts = new_ts;
#if REMOTE_DEBUG > 1 //BMC every 200
QuiskPrintf("send_remote_sound_socket CURRENT calls: %lu, samples %lu, deltasec %f\n", callcount, sampcount, delta);
QuiskPrintf("%f: send_remote_sound_socket CURRENT RATES (HZ): calls %f, samples %f\n", new_ts, callcount / delta, sampcount / delta);
#endif
if (bunchcount > 0) { // skip the initial bunch; prebuf may distort some numbers(?)
callcount_total += callcount;
sampcount_total += sampcount;
delta_total += delta;
}
if (bunchcount % 10 == 0 && bunchcount > 0) {
QuiskPrintf("%f: send_remote_sound_socket SUMMARY calls: %f, samples %f, deltasec %f\n",
new_ts, callcount_total, sampcount_total, delta_total);
QuiskPrintf("%f: send_remote_sound_socket SUMMARY RATES (HZ): calls %f, samples %f\n",
new_ts, callcount_total / delta_total, sampcount_total / delta_total);
}
bunchcount++;
callcount = 0;
sampcount = 0;
}
#endif
}
// Send graph data via UDP from the remote radio to the control head
void send_graph_data(double * fft_avg, int fft_size, double zoom, double deltaf, int fft_sample_rate, double scale)
{
static double * pixels = NULL;
static int n_pixels = 0;
static uint8_t sequence = 0;
uint8_t flags;
int16_t buffer[MAX_UDP_INT16_T];
int16_t block;
int pixel_index, buffer_index;
double d1, d2;
ssize_t sent;
char buf[RX_BUFFER_SIZE]; // For startup message
int recv_len;
struct sockaddr_in far_addr;
#ifdef MS_WINDOWS
int addr_len = sizeof(struct sockaddr_in);
#else
socklen_t addr_len = sizeof(struct sockaddr_in);
#endif
if (remote_radio_graph_socket == INVALID_SOCKET)
return;
if ( !control_head_data_width)
return;
if ( !remote_radio_graph_socket_started) {
// Receive short msg, grab far end address
// Receive from control head is necessary to establish a path through NAT
if ((recv_len = recvfrom(remote_radio_graph_socket, buf, RX_BUFFER_SIZE, 0, (struct sockaddr *) &far_addr, &addr_len)) < 2) {
return;
}
else {
if (connect(remote_radio_graph_socket, (const struct sockaddr *)&far_addr, sizeof(far_addr)) != 0) {
QuiskPrintf("send_remote_graph_socket), connect(): %s\n", strerror(errno));
close(remote_radio_graph_socket);
remote_radio_graph_socket = INVALID_SOCKET;
return;
}
else {
remote_radio_graph_socket_started = 1;
}
}
}
if (control_head_data_width > n_pixels) {
n_pixels = control_head_data_width;
if (pixels)
free(pixels);
pixels = (double *)malloc(n_pixels * sizeof(double));
}
if ( ! fft_avg) { // send dummy graph data
send(remote_radio_graph_socket, "dum", 3, 0);
return;
}
copy2pixels(pixels, control_head_data_width, fft_avg, fft_size, zoom, deltaf, fft_sample_rate);
// Send multiple 16-bit data blocks: {flags, sequence}, block number, 16-bit graph data
// 8-bit flags:
// bit 0: clip indicator
// 8-bit sequence: 0, 1, 2, ..., 255
block = 0;
pixel_index = 0;
while (pixel_index < control_head_data_width) {
if (quisk_get_overrange())
flags = 0x01;
else
flags = 0x00;
buffer[0] = flags << 8 | sequence;
buffer[1] = block;
buffer_index = 2;
while (buffer_index < MAX_UDP_INT16_T && pixel_index < control_head_data_width) {
d1 = pixels[pixel_index++];
if (fabs(d1) < 1e-40) // avoid log10(0)
d1 = 1E-40;
d2 = 20.0 * log10(d1) - scale;
if (d2 < -200)
d2 = -200;
else if (d2 > 0)
d2 = 0;
buffer[buffer_index++] = (int16_t)lround(d2 * GRAPH_DATA_SCALE);
}
sent = send(remote_radio_graph_socket, (const char *)buffer, buffer_index * 2, 0);
if (sent != buffer_index * 2)
QuiskPrintf("send_graph_data(), send(): %s\n", strerror(errno));
block++;
}
sequence += 1;
}
// Receive graph data via UDP on the control head
int receive_graph_data(double * fft_avg)
{
int i, i1, i2;
ssize_t count;
uint8_t seq, flags;
int16_t buffer[MAX_UDP_INT16_T];
int16_t block;
static int16_t * pixels = NULL;
static int n_pixels = 0;
static int total = 0;
static int16_t sequence = 0;
if (control_head_graph_socket == INVALID_SOCKET)
return 0;
// Signal far end (server) that we're ready (this sends our address/port to far end)
if ( !control_head_graph_socket_started) {
i = send(control_head_graph_socket, "rr\n", 3, 0);
if (i != 3)
QuiskPrintf("receive_graph_data(), send(): %s\n", strerror(errno));
}
if (n_pixels < data_width) {
n_pixels = data_width;
if (pixels)
free(pixels);
pixels = (int16_t *)malloc(n_pixels * sizeof(int16_t));
}
count = recv(control_head_graph_socket, (char *)buffer, MAX_UDP_INT16_T * 2, 0);
count /= 2; // convert to int16_t
if (count > 2) {
control_head_graph_socket_started = 1;
flags = buffer[0] >> 8;
if (flags & 0x01) // Clip
quisk_sound_state.overrange++;
seq = buffer[0] & 0xFF;
if (seq != sequence) { // new graph data
sequence = seq;
total = 0;
}
block = buffer[1];
count -= 2; // number of 16-bit graph data items
i1 = block * (MAX_UDP_INT16_T - 2);
i2 = i1 + count;
if (i1 >= 0 && i2 <= data_width) {
memcpy(pixels + i1, buffer + 2, count * 2);
total += count;
if (total == data_width) {
for (i = 0; i < data_width; i++)
fft_avg[i] = (double)pixels[i] / GRAPH_DATA_SCALE;
return data_width;
}
}
}
return 0;
}
static int start_winsock()
{
#ifdef MS_WINDOWS
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
QuiskPrintf("start_winsock(): %s\n", strerror(errno));
return 0; // failure to start winsock
}
#endif
return 1;
}
static void open_and_bind_socket(SOCKET * sock, char * ip, int port, int sndsize, char * name, int non_block)
{
struct sockaddr_in bind_addr;
const char enable = 1; // for sockopt
#ifndef MS_WINDOWS
int tos = 184; // DSCP "Expedite" (46)
#endif
if (!start_winsock()) {
QuiskPrintf("open_and_bind_socket for %s: Failure to start WinSock\n", name);
return;
}
*sock = socket(PF_INET, SOCK_DGRAM, 0);
if (*sock != INVALID_SOCKET) {
setsockopt(*sock, SOL_SOCKET, SO_SNDBUF, (char *)&sndsize, sizeof(sndsize));
setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
#ifndef MS_WINDOWS
setsockopt(*sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
#endif
// bind to this computer for receiving (and reading far-end address from client)
memset((char *) &bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(port);
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(*sock, (const struct sockaddr *)&bind_addr, sizeof(bind_addr)) != 0) {
QuiskPrintf("open_and_bind_socket(), bind(): %s\n", strerror(errno));
close(*sock);
*sock = INVALID_SOCKET;
}
else if (non_block) {
#ifdef MS_WINDOWS
unsigned long one = 1;
ioctlsocket(*sock, FIONBIO, &one); // set non-blocking
#else
int flags;
flags = fcntl(*sock, F_GETFL, 0); // set non-blocking
fcntl(*sock, F_SETFL, flags | O_NONBLOCK);
#endif
}
}
if (*sock == INVALID_SOCKET) {
QuiskPrintf("open server %s: Failure to open socket\n", name);
}
else {
QuiskPrintf("open server %s: opened socket %s port %i\n", name, ip, port);
}
}
static void open_and_connect_socket(SOCKET * sock, char * ip, int port, int sndsize, char * name, int non_block)
{
struct sockaddr_in Addr;
const char enable = 1; // for sockopt
if (!start_winsock()) {
QuiskPrintf("open_and_connect_socket for %s: Failure to start WinSock\n", name);
return;
}
*sock = socket(PF_INET, SOCK_DGRAM, 0);
if (*sock != INVALID_SOCKET) {
setsockopt(*sock, SOL_SOCKET, SO_RCVBUF, (char *)&sndsize, sizeof(sndsize));
setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
// set far-end address structure to enable sending initial packet to get things started
Addr.sin_family = AF_INET;
Addr.sin_port = htons(port);
#ifdef MS_WINDOWS
Addr.sin_addr.S_un.S_addr = inet_addr(ip);
#else
inet_aton(ip, &Addr.sin_addr);
#endif
if (connect(*sock, (const struct sockaddr *)&Addr, sizeof(Addr)) != 0) {
close(*sock);
*sock = INVALID_SOCKET;
}
else if (non_block) {
#ifdef MS_WINDOWS
unsigned long one = 1;
ioctlsocket(*sock, FIONBIO, &one); // set non-blocking
#else
int flags;
flags = fcntl(*sock, F_GETFL, 0); // set non-blocking
fcntl(*sock, F_SETFL, flags | O_NONBLOCK);
#endif
}
}
if (*sock == INVALID_SOCKET) {
QuiskPrintf("open client %s: Failure to open socket\n", name);
}
else {
QuiskPrintf("open client %s: opened socket %s port %i\n", name, ip, port);
}
}
static void close_socket(SOCKET * sock, char * name)
{
if (*sock != INVALID_SOCKET) {
close(*sock);
*sock = INVALID_SOCKET;
#ifdef MS_WINDOWS
WSACleanup();
#endif
QuiskPrintf("%s: closed socket\n", name);
}
else {
QuiskPrintf("%s: socket already closed\n", name);
}
}
// start running UDP remote sound on control_head ...
// ... receive radio sound from remote_radio, send mic sound to remote_radio
PyObject * quisk_start_control_head_remote_sound(PyObject * self, PyObject * args)
{
int radio_sound_port;
int graph_data_port;
int sndsize = 48000;
char * remote_radio_ip; // IP address of far end
char * name;
SOCKET * sock;
if (!PyArg_ParseTuple (args, "sii", &remote_radio_ip, &radio_sound_port, &graph_data_port))
return NULL;
name = "radio sound from remote_radio";
sock = &control_head_sound_socket;
open_and_connect_socket(sock, remote_radio_ip, radio_sound_port, sndsize, name, 0);
name = "graph data from remote_radio";
sock = &control_head_graph_socket;
open_and_connect_socket(sock, remote_radio_ip, graph_data_port, 1024 * 8, name, 1);
packets_sent = 0;
packets_recd = 0;
return Py_None;
}
// stop running UDP remote sound on control_head
PyObject * quisk_stop_control_head_remote_sound(PyObject * self, PyObject * args)
{
char * name;
SOCKET * sock;
if (!PyArg_ParseTuple (args, ""))
return NULL;
name = "radio sound from remote_radio";
sock = &control_head_sound_socket;
close_socket(sock, name);
name = "graph data from remote_radio";
sock = &control_head_graph_socket;
close_socket(sock, name);
control_head_sound_socket_started = 0; // reset for next time
remote_radio_sound_socket_started = 0;
control_head_graph_socket_started = 0;
remote_radio_graph_socket_started = 0;
QuiskPrintf("total packets sent = %i, recd = %i\n", packets_sent, packets_recd);
return Py_None;
}
// start running UDP remote sound on remote_radio ...
// ... send radio sound to control_head, receive mic sound from control_head
PyObject * quisk_start_remote_radio_remote_sound(PyObject * self, PyObject * args)
{
int radio_sound_port;
int graph_data_port;
int sndsize = 48000;
char * control_head_ip; // IP address of far end
char * name;
SOCKET * sock;
if (!PyArg_ParseTuple (args, "siii", &control_head_ip, &radio_sound_port,
&graph_data_port, &control_head_data_width))
return NULL;
name = "radio sound to control_head";
sock = &remote_radio_sound_socket;
open_and_bind_socket(sock, control_head_ip, radio_sound_port, sndsize, name, 0);
name = "graph data to control_head";
sock = &remote_radio_graph_socket;
open_and_bind_socket(sock, control_head_ip, graph_data_port, 1024 * 8, name, 1);
packets_sent = 0;
packets_recd = 0;
return Py_None;
}
// stop running UDP remote sound on remote_radio
PyObject * quisk_stop_remote_radio_remote_sound(PyObject * self, PyObject * args)
{
char * name;
SOCKET * sock;
if (!PyArg_ParseTuple (args, ""))
return NULL;
name = "radio sound to control_head";
sock = &remote_radio_sound_socket;
close_socket(sock, name);
name = "graph data to control_head";
sock = &remote_radio_graph_socket;
close_socket(sock, name);
control_head_sound_socket_started = 0; // reset for next time
remote_radio_sound_socket_started = 0;
control_head_graph_socket_started = 0;
remote_radio_graph_socket_started = 0;
control_head_data_width = 0;
QuiskPrintf("total packets sent = %i, recd = %i\n", packets_sent, packets_recd);
return Py_None;
}
|