| @@ -10,10 +10,8 @@ · 9 unmodified lines |
| 10 | 10 | #include <netinet/in.h> |
| 11 | 11 | #include <arpa/inet.h> |
| 12 | 12 | |
| 13 | | −#define MULTICAST_TARGET "239.255.77.77" |
| 14 | | −#define MULTICAST_PORT 4010 |
| 15 | | − |
| 16 | | −#define BYTES_PER_SAMPLE 2 |
| 13 | +#define DEFAULT_MULTICAST_GROUP "239.255.77.77" |
| 14 | +#define DEFAULT_PORT 4010 |
| 17 | 15 | |
| 18 | 16 | #define MAX_SO_PACKETSIZE 1154 |
| 19 | 17 | #define CHANNELS 2 |
| @@ -32,8 +30,23 @@ static void alsa_error(const char *msg, int r) · 12 unmodified lines |
| 32 | 30 | |
| 33 | 31 | static void show_usage(const char *arg0) |
| 34 | 32 | { |
| 35 | | − fprintf(stderr, "Usage: %s [-v] [-i interface_name_or_address] [-t target_latency_ms]\n", |
| 36 | | − arg0); |
| 33 | + fprintf(stderr, "\n"); |
| 34 | + fprintf(stderr, "Usage: %s [-u] [-v] [-p <port>] [-i <iface>] [-g <group>] [-t <target_latency_ms>]\n", arg0); |
| 35 | + fprintf(stderr, "\n"); |
| 36 | + fprintf(stderr, " All command line options are optional. Default is to use\n"); |
| 37 | + fprintf(stderr, " multicast with group address 239.255.77.77, port 4010.\n"); |
| 38 | + fprintf(stderr, "\n"); |
| 39 | + fprintf(stderr, " -u : Use unicast instead of multicast.\n"); |
| 40 | + fprintf(stderr, " -v : Verbose operation.\n"); |
| 41 | + fprintf(stderr, " -p <port> : Use <port> instead of default port 4010.\n"); |
| 42 | + fprintf(stderr, " Applies to both multicast and unicast.\n"); |
| 43 | + fprintf(stderr, " -i <iface> : Use local interface <iface>. Either the IP\n"); |
| 44 | + fprintf(stderr, " or the interface name can be specified. In\n"); |
| 45 | + fprintf(stderr, " multicast mode, uses this interface for IGMP.\n"); |
| 46 | + fprintf(stderr, " In unicast, binds to this interface only.\n"); |
| 47 | + fprintf(stderr, " -g <group> : Multicast group address. Multicast mode only.\n"); |
| 48 | + fprintf(stderr, " -t <latency>: Target latency in milliseconds. Defaults to 50ms.\n"); |
| 49 | + fprintf(stderr, "\n"); |
| 37 | 50 | exit(1); |
| 38 | 51 | } |
| 39 | 52 | |
| @@ -149,22 +162,38 @@ int main(int argc, char *argv[]) · 109 unmodified lines |
| 149 | 162 | struct ip_mreq imreq; |
| 150 | 163 | snd_pcm_t *snd; |
| 151 | 164 | unsigned char buf[MAX_SO_PACKETSIZE]; |
| 152 | | − in_addr_t interface = INADDR_ANY; |
| 153 | 165 | int opt; |
| 154 | 166 | int verbosity = 0; |
| 155 | 167 | int samples; |
| 156 | 168 | snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; |
| 157 | 169 | unsigned int bytes_per_sample = 2; |
| 158 | 170 | unsigned int rate = 44100; |
| 159 | | − int target_latency_ms = 50; |
| 160 | 171 | unsigned char cur_server_rate = 0; |
| 161 | 172 | unsigned char cur_server_size = 0; |
| 162 | 173 | |
| 163 | | − while ((opt = getopt(argc, argv, "i:t:v")) != -1) { |
| 174 | + // Command line options |
| 175 | + int use_unicast = 0; |
| 176 | + char *multicast_group = NULL; |
| 177 | + in_addr_t interface = INADDR_ANY; |
| 178 | + uint16_t port = DEFAULT_PORT; |
| 179 | + int target_latency_ms = 50; |
| 180 | + |
| 181 | + |
| 182 | + while ((opt = getopt(argc, argv, "i:g:p:t:vuh")) != -1) { |
| 164 | 183 | switch (opt) { |
| 165 | 184 | case 'i': |
| 166 | 185 | interface = get_interface(optarg); |
| 167 | 186 | break; |
| 187 | + case 'p': |
| 188 | + port = atoi(optarg); |
| 189 | + if (!port) show_usage(argv[0]); |
| 190 | + break; |
| 191 | + case 'u': |
| 192 | + use_unicast = 1; |
| 193 | + break; |
| 194 | + case 'g': |
| 195 | + multicast_group = strdup(optarg); |
| 196 | + break; |
| 168 | 197 | case 't': |
| 169 | 198 | target_latency_ms = atoi(optarg); |
| 170 | 199 | break; |
| @@ -180,23 +209,21 @@ int main(int argc, char *argv[]) · 9 unmodified lines |
| 180 | 209 | show_usage(argv[0]); |
| 181 | 210 | } |
| 182 | 211 | |
| 183 | | − // const char *device = "default"; |
| 184 | | − // ret = snd_pcm_open(&snd, device, SND_PCM_STREAM_PLAYBACK, 0); |
| 185 | | − // SNDCHK("snd_pcm_open", ret); |
| 186 | | − |
| 187 | 212 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 188 | 213 | |
| 189 | 214 | memset((void *)&servaddr, 0, sizeof(servaddr)); |
| 190 | 215 | servaddr.sin_family = AF_INET; |
| 191 | | − servaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 192 | | − servaddr.sin_port = htons(MULTICAST_PORT); |
| 216 | + servaddr.sin_addr.s_addr = use_unicast ? interface : htonl(INADDR_ANY); |
| 217 | + servaddr.sin_port = htons(port); |
| 193 | 218 | bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); |
| 194 | 219 | |
| 195 | | − imreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_TARGET); |
| 196 | | − imreq.imr_interface.s_addr = interface; |
| 220 | + if (!use_unicast) { |
| 221 | + imreq.imr_multiaddr.s_addr = inet_addr(multicast_group ? multicast_group : DEFAULT_MULTICAST_GROUP); |
| 222 | + imreq.imr_interface.s_addr = interface; |
| 197 | 223 | |
| 198 | | − setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 199 | | − (const void *)&imreq, sizeof(struct ip_mreq)); |
| 224 | + setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 225 | + (const void *)&imreq, sizeof(struct ip_mreq)); |
| 226 | + } |
| 200 | 227 | |
| 201 | 228 | if (setup_alsa(&snd, format, rate, verbosity, target_latency_ms) == -1) { |
| 202 | 229 | return -1; |
| @@ -217,19 +244,22 @@ int main(int argc, char *argv[]) · 14 unmodified lines |
| 217 | 244 | case 24: format = SND_PCM_FORMAT_S24_3LE; bytes_per_sample = 3; break; |
| 218 | 245 | case 32: format = SND_PCM_FORMAT_S32_LE; bytes_per_sample = 4; break; |
| 219 | 246 | default: |
| 220 | | − printf("Unsupported sample size %hhu, not playing until next format switch.\n", cur_server_size); |
| 247 | + if (verbosity > 0) |
| 248 | + printf("Unsupported sample size %hhu, not playing until next format switch.\n", cur_server_size); |
| 221 | 249 | rate = 0; |
| 222 | 250 | } |
| 223 | 251 | |
| 224 | 252 | if (rate) { |
| 225 | 253 | close_alsa(snd); |
| 226 | 254 | if (setup_alsa(&snd, format, rate, verbosity, target_latency_ms) == -1) { |
| 227 | | − printf("Unable to set up ALSA with sample rate %u and sample size %hhu, not playing until next format switch.\n", rate, cur_server_size); |
| 255 | + if (verbosity > 0) |
| 256 | + printf("Unable to set up ALSA with sample rate %u and sample size %hhu, not playing until next format switch.\n", rate, cur_server_size); |
| 228 | 257 | snd = NULL; |
| 229 | 258 | rate = 0; |
| 230 | 259 | } |
| 231 | 260 | else { |
| 232 | | − printf("Switched format to sample rate %u and sample size %hhu.\n", rate, cur_server_size); |
| 261 | + if (verbosity > 0) |
| 262 | + printf("Switched format to sample rate %u and sample size %hhu.\n", rate, cur_server_size); |
| 233 | 263 | } |
| 234 | 264 | } |
| 235 | 265 | } |