2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
23 #include "raop_buffer.h"
29 #define NO_FLUSH (-42)
33 raop_callbacks_t callbacks
;
35 /* Buffer to handle all resends */
36 raop_buffer_t
*buffer
;
38 /* Remote address as sockaddr */
39 struct sockaddr_storage remote_saddr
;
40 socklen_t remote_saddr_len
;
42 /* MUTEX LOCKED VARIABLES START */
43 /* These variables only edited mutex locked */
48 unsigned char *metadata
;
50 unsigned char *coverart
;
54 thread_handle_t thread
;
55 mutex_handle_t run_mutex
;
56 /* MUTEX LOCKED VARIABLES END */
58 /* Remote control and timing ports */
59 unsigned short control_rport
;
60 unsigned short timing_rport
;
62 /* Sockets for control, timing and data */
63 int csock
, tsock
, dsock
;
65 /* Local control, timing and data ports */
66 unsigned short control_lport
;
67 unsigned short timing_lport
;
68 unsigned short data_lport
;
70 /* Initialized after the first control packet */
71 struct sockaddr_storage control_saddr
;
72 socklen_t control_saddr_len
;
73 unsigned short control_seqnum
;
77 raop_rtp_parse_remote(raop_rtp_t
*raop_rtp
, const char *remote
)
87 current
= original
= strdup(remote
);
91 tmpstr
= utils_strsep(¤t
, " ");
92 if (strcmp(tmpstr
, "IN")) {
96 tmpstr
= utils_strsep(¤t
, " ");
97 if (!strcmp(tmpstr
, "IP4") && current
) {
99 } else if (!strcmp(tmpstr
, "IP6") && current
) {
105 if (strstr(current
, ":")) {
106 /* FIXME: iTunes sends IP4 even with an IPv6 address, does it mean something */
109 ret
= netutils_parse_address(family
, current
,
110 &raop_rtp
->remote_saddr
,
111 sizeof(raop_rtp
->remote_saddr
));
116 raop_rtp
->remote_saddr_len
= ret
;
122 raop_rtp_init(logger_t
*logger
, raop_callbacks_t
*callbacks
, const char *remote
,
123 const char *fmtp
, const unsigned char *aeskey
, const unsigned char *aesiv
)
125 raop_rtp_t
*raop_rtp
;
132 raop_rtp
= calloc(1, sizeof(raop_rtp_t
));
136 raop_rtp
->logger
= logger
;
137 memcpy(&raop_rtp
->callbacks
, callbacks
, sizeof(raop_callbacks_t
));
138 raop_rtp
->buffer
= raop_buffer_init(fmtp
, aeskey
, aesiv
);
139 if (!raop_rtp
->buffer
) {
143 if (raop_rtp_parse_remote(raop_rtp
, remote
) < 0) {
148 raop_rtp
->running
= 0;
149 raop_rtp
->joined
= 1;
150 raop_rtp
->flush
= NO_FLUSH
;
151 MUTEX_CREATE(raop_rtp
->run_mutex
);
157 raop_rtp_destroy(raop_rtp_t
*raop_rtp
)
160 raop_rtp_stop(raop_rtp
);
162 MUTEX_DESTROY(raop_rtp
->run_mutex
);
163 raop_buffer_destroy(raop_rtp
->buffer
);
164 free(raop_rtp
->metadata
);
165 free(raop_rtp
->coverart
);
171 raop_rtp_init_sockets(raop_rtp_t
*raop_rtp
, int use_ipv6
, int use_udp
)
173 int csock
= -1, tsock
= -1, dsock
= -1;
174 unsigned short cport
= 0, tport
= 0, dport
= 0;
179 csock
= netutils_init_socket(&cport
, use_ipv6
, use_udp
);
180 tsock
= netutils_init_socket(&tport
, use_ipv6
, use_udp
);
181 if (csock
== -1 || tsock
== -1) {
182 goto sockets_cleanup
;
185 dsock
= netutils_init_socket(&dport
, use_ipv6
, use_udp
);
187 goto sockets_cleanup
;
190 /* Listen to the data socket if using TCP */
192 if (listen(dsock
, 1) < 0)
193 goto sockets_cleanup
;
196 /* Set socket descriptors */
197 raop_rtp
->csock
= csock
;
198 raop_rtp
->tsock
= tsock
;
199 raop_rtp
->dsock
= dsock
;
201 /* Set port values */
202 raop_rtp
->control_lport
= cport
;
203 raop_rtp
->timing_lport
= tport
;
204 raop_rtp
->data_lport
= dport
;
208 if (csock
!= -1) closesocket(csock
);
209 if (tsock
!= -1) closesocket(tsock
);
210 if (dsock
!= -1) closesocket(dsock
);
215 raop_rtp_resend_callback(void *opaque
, unsigned short seqnum
, unsigned short count
)
217 raop_rtp_t
*raop_rtp
= opaque
;
218 unsigned char packet
[8];
219 unsigned short ourseqnum
;
220 struct sockaddr
*addr
;
224 addr
= (struct sockaddr
*)&raop_rtp
->control_saddr
;
225 addrlen
= raop_rtp
->control_saddr_len
;
227 logger_log(raop_rtp
->logger
, LOGGER_DEBUG
, "Got resend request %d %d", seqnum
, count
);
228 ourseqnum
= raop_rtp
->control_seqnum
++;
230 /* Fill the request buffer */
232 packet
[1] = 0x55|0x80;
233 packet
[2] = (ourseqnum
>> 8);
234 packet
[3] = ourseqnum
;
235 packet
[4] = (seqnum
>> 8);
237 packet
[6] = (count
>> 8);
240 ret
= sendto(raop_rtp
->csock
, (const char *)packet
, sizeof(packet
), 0, addr
, addrlen
);
242 logger_log(raop_rtp
->logger
, LOGGER_WARNING
, "Resend failed: %d", SOCKET_GET_ERROR());
249 raop_rtp_thread_udp(void *arg
)
251 raop_rtp_t
*raop_rtp
= arg
;
252 unsigned char packet
[RAOP_PACKET_LEN
];
253 unsigned int packetlen
;
254 struct sockaddr_storage saddr
;
258 const ALACSpecificConfig
*config
;
259 void *cb_data
= NULL
;
263 config
= raop_buffer_get_config(raop_rtp
->buffer
);
264 cb_data
= raop_rtp
->callbacks
.audio_init(raop_rtp
->callbacks
.cls
,
277 MUTEX_LOCK(raop_rtp
->run_mutex
);
278 if (!raop_rtp
->running
) {
279 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
282 /* Read the volume level */
283 volume_changed
= (volume
!= raop_rtp
->volume
);
284 volume
= raop_rtp
->volume
;
286 /* Read the flush value */
287 flush
= raop_rtp
->flush
;
288 raop_rtp
->flush
= NO_FLUSH
;
289 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
291 /* Call set_volume callback if changed */
292 if (volume_changed
) {
293 raop_rtp
->callbacks
.audio_set_volume(raop_rtp
->callbacks
.cls
, cb_data
, volume
);
295 if (flush
!= NO_FLUSH
) {
296 raop_buffer_flush(raop_rtp
->buffer
, flush
);
297 raop_rtp
->callbacks
.audio_flush(raop_rtp
->callbacks
.cls
, cb_data
);
300 /* Set timeout value to 5ms */
304 /* Get the correct nfds value */
305 nfds
= raop_rtp
->csock
+1;
306 if (raop_rtp
->tsock
>= nfds
)
307 nfds
= raop_rtp
->tsock
+1;
308 if (raop_rtp
->dsock
>= nfds
)
309 nfds
= raop_rtp
->dsock
+1;
311 /* Set rfds and call select */
313 FD_SET(raop_rtp
->csock
, &rfds
);
314 FD_SET(raop_rtp
->tsock
, &rfds
);
315 FD_SET(raop_rtp
->dsock
, &rfds
);
316 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
318 /* Timeout happened */
320 } else if (ret
== -1) {
321 /* FIXME: Error happened */
325 if (FD_ISSET(raop_rtp
->csock
, &rfds
)) {
326 saddrlen
= sizeof(saddr
);
327 packetlen
= recvfrom(raop_rtp
->csock
, (char *)packet
, sizeof(packet
), 0,
328 (struct sockaddr
*)&saddr
, &saddrlen
);
330 /* Get the destination address here, because we need the sin6_scope_id */
331 memcpy(&raop_rtp
->control_saddr
, &saddr
, saddrlen
);
332 raop_rtp
->control_saddr_len
= saddrlen
;
334 if (packetlen
>= 12) {
335 char type
= packet
[1] & ~0x80;
337 logger_log(raop_rtp
->logger
, LOGGER_DEBUG
, "Got control packet of type 0x%02x", type
);
339 /* Handle resent data packet */
340 int ret
= raop_buffer_queue(raop_rtp
->buffer
, packet
+4, packetlen
-4, 1);
344 } else if (FD_ISSET(raop_rtp
->tsock
, &rfds
)) {
345 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Would have timing packet in queue");
346 } else if (FD_ISSET(raop_rtp
->dsock
, &rfds
)) {
347 saddrlen
= sizeof(saddr
);
348 packetlen
= recvfrom(raop_rtp
->dsock
, (char *)packet
, sizeof(packet
), 0,
349 (struct sockaddr
*)&saddr
, &saddrlen
);
350 if (packetlen
>= 12) {
351 int no_resend
= (raop_rtp
->control_rport
== 0);
354 const void *audiobuf
;
357 ret
= raop_buffer_queue(raop_rtp
->buffer
, packet
, packetlen
, 1);
360 /* Decode all frames in queue */
361 while ((audiobuf
= raop_buffer_dequeue(raop_rtp
->buffer
, &audiobuflen
, no_resend
))) {
362 raop_rtp
->callbacks
.audio_process(raop_rtp
->callbacks
.cls
, cb_data
, audiobuf
, audiobuflen
);
365 /* Handle possible resend requests */
367 raop_buffer_handle_resends(raop_rtp
->buffer
, raop_rtp_resend_callback
, raop_rtp
);
372 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Exiting UDP RAOP thread");
373 raop_rtp
->callbacks
.audio_destroy(raop_rtp
->callbacks
.cls
, cb_data
);
379 raop_rtp_thread_tcp(void *arg
)
381 raop_rtp_t
*raop_rtp
= arg
;
383 unsigned char packet
[RAOP_PACKET_LEN
];
384 unsigned int packetlen
= 0;
387 const ALACSpecificConfig
*config
;
388 void *cb_data
= NULL
;
392 config
= raop_buffer_get_config(raop_rtp
->buffer
);
393 cb_data
= raop_rtp
->callbacks
.audio_init(raop_rtp
->callbacks
.cls
,
405 MUTEX_LOCK(raop_rtp
->run_mutex
);
406 if (!raop_rtp
->running
) {
407 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
410 volume_changed
= (volume
!= raop_rtp
->volume
);
411 volume
= raop_rtp
->volume
;
412 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
414 /* Call set_volume callback if changed */
415 if (volume_changed
) {
416 raop_rtp
->callbacks
.audio_set_volume(raop_rtp
->callbacks
.cls
, cb_data
, volume
);
419 /* Set timeout value to 5ms */
423 /* Get the correct nfds value and set rfds */
425 if (stream_fd
== -1) {
426 FD_SET(raop_rtp
->dsock
, &rfds
);
427 nfds
= raop_rtp
->dsock
+1;
429 FD_SET(stream_fd
, &rfds
);
432 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
434 /* Timeout happened */
436 } else if (ret
== -1) {
437 /* FIXME: Error happened */
438 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error in select");
441 if (stream_fd
== -1 && FD_ISSET(raop_rtp
->dsock
, &rfds
)) {
442 struct sockaddr_storage saddr
;
445 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Accepting client");
446 saddrlen
= sizeof(saddr
);
447 stream_fd
= accept(raop_rtp
->dsock
, (struct sockaddr
*)&saddr
, &saddrlen
);
448 if (stream_fd
== -1) {
449 /* FIXME: Error happened */
450 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error in accept %d %s", errno
, strerror(errno
));
454 if (stream_fd
!= -1 && FD_ISSET(stream_fd
, &rfds
)) {
455 unsigned int rtplen
=0;
457 const void *audiobuf
;
460 ret
= recv(stream_fd
, (char *)(packet
+packetlen
), sizeof(packet
)-packetlen
, 0);
462 /* TCP socket closed */
463 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "TCP socket closed");
465 } else if (ret
== -1) {
466 /* FIXME: Error happened */
467 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error in recv");
472 /* Check that we have enough bytes */
476 if (packet
[0] != '$' || packet
[1] != '\0') {
477 /* FIXME: Incorrect RTP magic bytes */
480 rtplen
= (packet
[2] << 8) | packet
[3];
481 if (rtplen
> sizeof(packet
)) {
482 /* FIXME: Too long packet */
483 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error, packet too long %d", rtplen
);
486 if (packetlen
< 4+rtplen
) {
490 /* Packet is valid, process it */
491 ret
= raop_buffer_queue(raop_rtp
->buffer
, packet
+4, rtplen
, 0);
494 /* Remove processed bytes from packet buffer */
495 memmove(packet
, packet
+4+rtplen
, packetlen
-rtplen
);
496 packetlen
-= 4+rtplen
;
498 /* Decode the received frame */
499 if ((audiobuf
= raop_buffer_dequeue(raop_rtp
->buffer
, &audiobuflen
, 1))) {
500 raop_rtp
->callbacks
.audio_process(raop_rtp
->callbacks
.cls
, cb_data
, audiobuf
, audiobuflen
);
505 /* Close the stream file descriptor */
506 if (stream_fd
!= -1) {
507 closesocket(stream_fd
);
510 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Exiting TCP RAOP thread");
511 raop_rtp
->callbacks
.audio_destroy(raop_rtp
->callbacks
.cls
, cb_data
);
517 raop_rtp_start(raop_rtp_t
*raop_rtp
, int use_udp
, unsigned short control_rport
, unsigned short timing_rport
,
518 unsigned short *control_lport
, unsigned short *timing_lport
, unsigned short *data_lport
)
524 MUTEX_LOCK(raop_rtp
->run_mutex
);
525 if (raop_rtp
->running
|| !raop_rtp
->joined
) {
526 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
530 /* Initialize ports and sockets */
531 raop_rtp
->control_rport
= control_rport
;
532 raop_rtp
->timing_rport
= timing_rport
;
533 if (raop_rtp
->remote_saddr
.ss_family
== AF_INET6
) {
536 if (raop_rtp_init_sockets(raop_rtp
, use_ipv6
, use_udp
) < 0) {
537 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Initializing sockets failed");
538 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
541 if (control_lport
) *control_lport
= raop_rtp
->control_lport
;
542 if (timing_lport
) *timing_lport
= raop_rtp
->timing_lport
;
543 if (data_lport
) *data_lport
= raop_rtp
->data_lport
;
545 /* Create the thread and initialize running values */
546 raop_rtp
->running
= 1;
547 raop_rtp
->joined
= 0;
549 THREAD_CREATE(raop_rtp
->thread
, raop_rtp_thread_udp
, raop_rtp
);
551 THREAD_CREATE(raop_rtp
->thread
, raop_rtp_thread_tcp
, raop_rtp
);
553 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
557 raop_rtp_set_volume(raop_rtp_t
*raop_rtp
, float volume
)
563 } else if (volume
< -144.0f
) {
567 /* Set volume in thread instead */
568 MUTEX_LOCK(raop_rtp
->run_mutex
);
569 raop_rtp
->volume
= volume
;
570 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
574 raop_rtp_set_metadata(raop_rtp_t
*raop_rtp
, const char *data
, int datalen
)
576 unsigned char *metadata
;
583 metadata
= malloc(datalen
);
585 memcpy(metadata
, data
, datalen
);
587 /* Set metadata in thread instead */
588 MUTEX_LOCK(raop_rtp
->run_mutex
);
589 raop_rtp
->metadata
= metadata
;
590 raop_rtp
->metadata_len
= datalen
;
591 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
595 raop_rtp_set_coverart(raop_rtp_t
*raop_rtp
, const char *data
, int datalen
)
597 unsigned char *coverart
;
604 coverart
= malloc(datalen
);
606 memcpy(coverart
, data
, datalen
);
608 /* Set coverart in thread instead */
609 MUTEX_LOCK(raop_rtp
->run_mutex
);
610 raop_rtp
->coverart
= coverart
;
611 raop_rtp
->coverart_len
= datalen
;
612 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
616 raop_rtp_flush(raop_rtp_t
*raop_rtp
, int next_seq
)
620 /* Call flush in thread instead */
621 MUTEX_LOCK(raop_rtp
->run_mutex
);
622 raop_rtp
->flush
= next_seq
;
623 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
627 raop_rtp_stop(raop_rtp_t
*raop_rtp
)
631 /* Check that we are running and thread is not
632 * joined (should never be while still running) */
633 MUTEX_LOCK(raop_rtp
->run_mutex
);
634 if (!raop_rtp
->running
|| raop_rtp
->joined
) {
635 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
638 raop_rtp
->running
= 0;
639 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
641 /* Join the thread */
642 THREAD_JOIN(raop_rtp
->thread
);
643 if (raop_rtp
->csock
!= -1) closesocket(raop_rtp
->csock
);
644 if (raop_rtp
->tsock
!= -1) closesocket(raop_rtp
->tsock
);
645 if (raop_rtp
->dsock
!= -1) closesocket(raop_rtp
->dsock
);
647 /* Flush buffer into initial state */
648 raop_buffer_flush(raop_rtp
->buffer
, -1);
650 /* Mark thread as joined */
651 MUTEX_LOCK(raop_rtp
->run_mutex
);
652 raop_rtp
->joined
= 1;
653 MUTEX_UNLOCK(raop_rtp
->run_mutex
);