Skip to content

Commit fde780f

Browse files
committed
Add support for UDP multicast
This change extends the UDP audio device syntax to accept strings like udp:239.0.0.1:7355 This allows Direwolf to decode an audio stream sent to a multicast group address.
1 parent a1e2d1c commit fde780f

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

audio.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,23 @@ int audio_open (struct audio_s *pa)
385385
return -1;
386386
}
387387

388+
char mcaddr[20];
389+
unsigned port = DEFAULT_UDP_AUDIO_PORT;
390+
int is_multicast = 0;
391+
392+
if (sscanf(audio_in_name, "udp:%[0-9.]:%d", mcaddr, &port)==2) {
393+
is_multicast = 1;
394+
} else if (sscanf(audio_in_name, "udp:%d", &port)==1) {
395+
is_multicast = 0;
396+
} else {
397+
text_color_set(DW_COLOR_ERROR);
398+
dw_printf ("Couldn't parse address '%s'\n", audio_in_name);
399+
return -1;
400+
}
401+
388402
memset((char *) &si_me, 0, sizeof(si_me));
389403
si_me.sin_family = AF_INET;
390-
si_me.sin_port = htons((short)atoi(audio_in_name+4));
404+
si_me.sin_port = htons(port);
391405
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
392406

393407
//Bind to the socket
@@ -396,7 +410,27 @@ int audio_open (struct audio_s *pa)
396410
dw_printf ("Couldn't bind socket, errno %d\n", errno);
397411
return -1;
398412
}
399-
}
413+
414+
if (is_multicast) {
415+
struct ip_mreq mreq;
416+
417+
if(!inet_aton(mcaddr, &mreq.imr_multiaddr)) {
418+
text_color_set(DW_COLOR_ERROR);
419+
dw_printf ("Couldn't parse multicast address %s\n", mcaddr);
420+
return -1;
421+
}
422+
423+
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
424+
425+
if (setsockopt(adev[a].udp_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
426+
&mreq, sizeof(mreq))==-1) {
427+
text_color_set(DW_COLOR_ERROR);
428+
dw_printf ("Couldn't add multicast group, errno %d\n", errno);
429+
return -1;
430+
}
431+
}
432+
}
433+
400434
adev[a].inbuf_size_in_bytes = SDR_UDP_BUF_MAXLEN;
401435

402436
break;

0 commit comments

Comments
 (0)