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 */
49 unsigned char *metadata
;
51 unsigned char *coverart
;
55 thread_handle_t thread
;
56 mutex_handle_t run_mutex
;
57 /* MUTEX LOCKED VARIABLES END */
59 /* Remote control and timing ports */
60 unsigned short control_rport
;
61 unsigned short timing_rport
;
63 /* Sockets for control, timing and data */
64 int csock
, tsock
, dsock
;
66 /* Local control, timing and data ports */
67 unsigned short control_lport
;
68 unsigned short timing_lport
;
69 unsigned short data_lport
;
71 /* Initialized after the first control packet */
72 struct sockaddr_storage control_saddr
;
73 socklen_t control_saddr_len
;
74 unsigned short control_seqnum
;
78 raop_rtp_parse_remote(raop_rtp_t
*raop_rtp
, const char *remote
)
88 current
= original
= strdup(remote
);
92 tmpstr
= utils_strsep(¤t
, " ");
93 if (strcmp(tmpstr
, "IN")) {
97 tmpstr
= utils_strsep(¤t
, " ");
98 if (!strcmp(tmpstr
, "IP4") && current
) {
100 } else if (!strcmp(tmpstr
, "IP6") && current
) {
106 if (strstr(current
, ":")) {
107 /* FIXME: iTunes sends IP4 even with an IPv6 address, does it mean something */
110 ret
= netutils_parse_address(family
, current
,
111 &raop_rtp
->remote_saddr
,
112 sizeof(raop_rtp
->remote_saddr
));
117 raop_rtp
->remote_saddr_len
= ret
;
123 raop_rtp_init(logger_t
*logger
, raop_callbacks_t
*callbacks
, const char *remote
,
124 const char *fmtp
, const unsigned char *aeskey
, const unsigned char *aesiv
)
126 raop_rtp_t
*raop_rtp
;
133 raop_rtp
= calloc(1, sizeof(raop_rtp_t
));
137 raop_rtp
->logger
= logger
;
138 memcpy(&raop_rtp
->callbacks
, callbacks
, sizeof(raop_callbacks_t
));
139 raop_rtp
->buffer
= raop_buffer_init(fmtp
, aeskey
, aesiv
);
140 if (!raop_rtp
->buffer
) {
144 if (raop_rtp_parse_remote(raop_rtp
, remote
) < 0) {
149 raop_rtp
->running
= 0;
150 raop_rtp
->joined
= 1;
151 raop_rtp
->flush
= NO_FLUSH
;
152 MUTEX_CREATE(raop_rtp
->run_mutex
);
158 raop_rtp_destroy(raop_rtp_t
*raop_rtp
)
161 raop_rtp_stop(raop_rtp
);
163 MUTEX_DESTROY(raop_rtp
->run_mutex
);
164 raop_buffer_destroy(raop_rtp
->buffer
);
165 free(raop_rtp
->metadata
);
166 free(raop_rtp
->coverart
);
172 raop_rtp_init_sockets(raop_rtp_t
*raop_rtp
, int use_ipv6
, int use_udp
)
174 int csock
= -1, tsock
= -1, dsock
= -1;
175 unsigned short cport
= 0, tport
= 0, dport
= 0;
180 csock
= netutils_init_socket(&cport
, use_ipv6
, use_udp
);
181 tsock
= netutils_init_socket(&tport
, use_ipv6
, use_udp
);
182 if (csock
== -1 || tsock
== -1) {
183 goto sockets_cleanup
;
186 dsock
= netutils_init_socket(&dport
, use_ipv6
, use_udp
);
188 goto sockets_cleanup
;
191 /* Listen to the data socket if using TCP */
193 if (listen(dsock
, 1) < 0)
194 goto sockets_cleanup
;
197 /* Set socket descriptors */
198 raop_rtp
->csock
= csock
;
199 raop_rtp
->tsock
= tsock
;
200 raop_rtp
->dsock
= dsock
;
202 /* Set port values */
203 raop_rtp
->control_lport
= cport
;
204 raop_rtp
->timing_lport
= tport
;
205 raop_rtp
->data_lport
= dport
;
209 if (csock
!= -1) closesocket(csock
);
210 if (tsock
!= -1) closesocket(tsock
);
211 if (dsock
!= -1) closesocket(dsock
);
216 raop_rtp_resend_callback(void *opaque
, unsigned short seqnum
, unsigned short count
)
218 raop_rtp_t
*raop_rtp
= opaque
;
219 unsigned char packet
[8];
220 unsigned short ourseqnum
;
221 struct sockaddr
*addr
;
225 addr
= (struct sockaddr
*)&raop_rtp
->control_saddr
;
226 addrlen
= raop_rtp
->control_saddr_len
;
228 logger_log(raop_rtp
->logger
, LOGGER_DEBUG
, "Got resend request %d %d", seqnum
, count
);
229 ourseqnum
= raop_rtp
->control_seqnum
++;
231 /* Fill the request buffer */
233 packet
[1] = 0x55|0x80;
234 packet
[2] = (ourseqnum
>> 8);
235 packet
[3] = ourseqnum
;
236 packet
[4] = (seqnum
>> 8);
238 packet
[6] = (count
>> 8);
241 ret
= sendto(raop_rtp
->csock
, (const char *)packet
, sizeof(packet
), 0, addr
, addrlen
);
243 logger_log(raop_rtp
->logger
, LOGGER_WARNING
, "Resend failed: %d", SOCKET_GET_ERROR());
250 raop_rtp_process_events(raop_rtp_t
*raop_rtp
, void *cb_data
)
255 unsigned char *metadata
;
257 unsigned char *coverart
;
262 MUTEX_LOCK(raop_rtp
->run_mutex
);
263 if (!raop_rtp
->running
) {
264 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
268 /* Read the volume level */
269 volume
= raop_rtp
->volume
;
270 volume_changed
= raop_rtp
->volume_changed
;
271 raop_rtp
->volume_changed
= 0;
273 /* Read the flush value */
274 flush
= raop_rtp
->flush
;
275 raop_rtp
->flush
= NO_FLUSH
;
277 /* Read the metadata */
278 metadata
= raop_rtp
->metadata
;
279 metadata_len
= raop_rtp
->metadata_len
;
280 raop_rtp
->metadata
= NULL
;
281 raop_rtp
->metadata_len
= 0;
283 /* Read the coverart */
284 coverart
= raop_rtp
->coverart
;
285 coverart_len
= raop_rtp
->coverart_len
;
286 raop_rtp
->coverart
= NULL
;
287 raop_rtp
->coverart_len
= 0;
288 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
290 /* Call set_volume callback if changed */
291 if (volume_changed
) {
292 if (raop_rtp
->callbacks
.audio_set_volume
) {
293 raop_rtp
->callbacks
.audio_set_volume(raop_rtp
->callbacks
.cls
, cb_data
, volume
);
297 /* Handle flush if requested */
298 if (flush
!= NO_FLUSH
) {
299 raop_buffer_flush(raop_rtp
->buffer
, flush
);
300 if (raop_rtp
->callbacks
.audio_flush
) {
301 raop_rtp
->callbacks
.audio_flush(raop_rtp
->callbacks
.cls
, cb_data
);
304 if (metadata
!= NULL
) {
305 if (raop_rtp
->callbacks
.audio_set_metadata
) {
306 raop_rtp
->callbacks
.audio_set_metadata(raop_rtp
->callbacks
.cls
, cb_data
, metadata
, metadata_len
);
311 if (coverart
!= NULL
) {
312 if (raop_rtp
->callbacks
.audio_set_coverart
) {
313 raop_rtp
->callbacks
.audio_set_coverart(raop_rtp
->callbacks
.cls
, cb_data
, coverart
, coverart_len
);
322 raop_rtp_thread_udp(void *arg
)
324 raop_rtp_t
*raop_rtp
= arg
;
325 unsigned char packet
[RAOP_PACKET_LEN
];
326 unsigned int packetlen
;
327 struct sockaddr_storage saddr
;
330 const ALACSpecificConfig
*config
;
331 void *cb_data
= NULL
;
335 config
= raop_buffer_get_config(raop_rtp
->buffer
);
336 cb_data
= raop_rtp
->callbacks
.audio_init(raop_rtp
->callbacks
.cls
,
346 /* Check if we are still running and process callbacks */
347 if (raop_rtp_process_events(raop_rtp
, cb_data
)) {
351 /* Set timeout value to 5ms */
355 /* Get the correct nfds value */
356 nfds
= raop_rtp
->csock
+1;
357 if (raop_rtp
->tsock
>= nfds
)
358 nfds
= raop_rtp
->tsock
+1;
359 if (raop_rtp
->dsock
>= nfds
)
360 nfds
= raop_rtp
->dsock
+1;
362 /* Set rfds and call select */
364 FD_SET(raop_rtp
->csock
, &rfds
);
365 FD_SET(raop_rtp
->tsock
, &rfds
);
366 FD_SET(raop_rtp
->dsock
, &rfds
);
367 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
369 /* Timeout happened */
371 } else if (ret
== -1) {
372 /* FIXME: Error happened */
376 if (FD_ISSET(raop_rtp
->csock
, &rfds
)) {
377 saddrlen
= sizeof(saddr
);
378 packetlen
= recvfrom(raop_rtp
->csock
, (char *)packet
, sizeof(packet
), 0,
379 (struct sockaddr
*)&saddr
, &saddrlen
);
381 /* Get the destination address here, because we need the sin6_scope_id */
382 memcpy(&raop_rtp
->control_saddr
, &saddr
, saddrlen
);
383 raop_rtp
->control_saddr_len
= saddrlen
;
385 if (packetlen
>= 12) {
386 char type
= packet
[1] & ~0x80;
388 logger_log(raop_rtp
->logger
, LOGGER_DEBUG
, "Got control packet of type 0x%02x", type
);
390 /* Handle resent data packet */
391 int ret
= raop_buffer_queue(raop_rtp
->buffer
, packet
+4, packetlen
-4, 1);
395 } else if (FD_ISSET(raop_rtp
->tsock
, &rfds
)) {
396 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Would have timing packet in queue");
397 } else if (FD_ISSET(raop_rtp
->dsock
, &rfds
)) {
398 saddrlen
= sizeof(saddr
);
399 packetlen
= recvfrom(raop_rtp
->dsock
, (char *)packet
, sizeof(packet
), 0,
400 (struct sockaddr
*)&saddr
, &saddrlen
);
401 if (packetlen
>= 12) {
402 int no_resend
= (raop_rtp
->control_rport
== 0);
405 const void *audiobuf
;
408 ret
= raop_buffer_queue(raop_rtp
->buffer
, packet
, packetlen
, 1);
411 /* Decode all frames in queue */
412 while ((audiobuf
= raop_buffer_dequeue(raop_rtp
->buffer
, &audiobuflen
, no_resend
))) {
413 raop_rtp
->callbacks
.audio_process(raop_rtp
->callbacks
.cls
, cb_data
, audiobuf
, audiobuflen
);
416 /* Handle possible resend requests */
418 raop_buffer_handle_resends(raop_rtp
->buffer
, raop_rtp_resend_callback
, raop_rtp
);
423 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Exiting UDP RAOP thread");
424 raop_rtp
->callbacks
.audio_destroy(raop_rtp
->callbacks
.cls
, cb_data
);
430 raop_rtp_thread_tcp(void *arg
)
432 raop_rtp_t
*raop_rtp
= arg
;
434 unsigned char packet
[RAOP_PACKET_LEN
];
435 unsigned int packetlen
= 0;
437 const ALACSpecificConfig
*config
;
438 void *cb_data
= NULL
;
442 config
= raop_buffer_get_config(raop_rtp
->buffer
);
443 cb_data
= raop_rtp
->callbacks
.audio_init(raop_rtp
->callbacks
.cls
,
453 /* Check if we are still running and process callbacks */
454 if (raop_rtp_process_events(raop_rtp
, cb_data
)) {
458 /* Set timeout value to 5ms */
462 /* Get the correct nfds value and set rfds */
464 if (stream_fd
== -1) {
465 FD_SET(raop_rtp
->dsock
, &rfds
);
466 nfds
= raop_rtp
->dsock
+1;
468 FD_SET(stream_fd
, &rfds
);
471 ret
= select(nfds
, &rfds
, NULL
, NULL
, &tv
);
473 /* Timeout happened */
475 } else if (ret
== -1) {
476 /* FIXME: Error happened */
477 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error in select");
480 if (stream_fd
== -1 && FD_ISSET(raop_rtp
->dsock
, &rfds
)) {
481 struct sockaddr_storage saddr
;
484 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Accepting client");
485 saddrlen
= sizeof(saddr
);
486 stream_fd
= accept(raop_rtp
->dsock
, (struct sockaddr
*)&saddr
, &saddrlen
);
487 if (stream_fd
== -1) {
488 /* FIXME: Error happened */
489 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error in accept %d %s", errno
, strerror(errno
));
493 if (stream_fd
!= -1 && FD_ISSET(stream_fd
, &rfds
)) {
494 unsigned int rtplen
=0;
496 const void *audiobuf
;
499 ret
= recv(stream_fd
, (char *)(packet
+packetlen
), sizeof(packet
)-packetlen
, 0);
501 /* TCP socket closed */
502 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "TCP socket closed");
504 } else if (ret
== -1) {
505 /* FIXME: Error happened */
506 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error in recv");
511 /* Check that we have enough bytes */
515 if (packet
[0] != '$' || packet
[1] != '\0') {
516 /* FIXME: Incorrect RTP magic bytes */
519 rtplen
= (packet
[2] << 8) | packet
[3];
520 if (rtplen
> sizeof(packet
)) {
521 /* FIXME: Too long packet */
522 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Error, packet too long %d", rtplen
);
525 if (packetlen
< 4+rtplen
) {
529 /* Packet is valid, process it */
530 ret
= raop_buffer_queue(raop_rtp
->buffer
, packet
+4, rtplen
, 0);
533 /* Remove processed bytes from packet buffer */
534 memmove(packet
, packet
+4+rtplen
, packetlen
-rtplen
);
535 packetlen
-= 4+rtplen
;
537 /* Decode the received frame */
538 if ((audiobuf
= raop_buffer_dequeue(raop_rtp
->buffer
, &audiobuflen
, 1))) {
539 raop_rtp
->callbacks
.audio_process(raop_rtp
->callbacks
.cls
, cb_data
, audiobuf
, audiobuflen
);
544 /* Close the stream file descriptor */
545 if (stream_fd
!= -1) {
546 closesocket(stream_fd
);
549 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Exiting TCP RAOP thread");
550 raop_rtp
->callbacks
.audio_destroy(raop_rtp
->callbacks
.cls
, cb_data
);
556 raop_rtp_start(raop_rtp_t
*raop_rtp
, int use_udp
, unsigned short control_rport
, unsigned short timing_rport
,
557 unsigned short *control_lport
, unsigned short *timing_lport
, unsigned short *data_lport
)
563 MUTEX_LOCK(raop_rtp
->run_mutex
);
564 if (raop_rtp
->running
|| !raop_rtp
->joined
) {
565 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
569 /* Initialize ports and sockets */
570 raop_rtp
->control_rport
= control_rport
;
571 raop_rtp
->timing_rport
= timing_rport
;
572 if (raop_rtp
->remote_saddr
.ss_family
== AF_INET6
) {
575 if (raop_rtp_init_sockets(raop_rtp
, use_ipv6
, use_udp
) < 0) {
576 logger_log(raop_rtp
->logger
, LOGGER_INFO
, "Initializing sockets failed");
577 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
580 if (control_lport
) *control_lport
= raop_rtp
->control_lport
;
581 if (timing_lport
) *timing_lport
= raop_rtp
->timing_lport
;
582 if (data_lport
) *data_lport
= raop_rtp
->data_lport
;
584 /* Create the thread and initialize running values */
585 raop_rtp
->running
= 1;
586 raop_rtp
->joined
= 0;
588 THREAD_CREATE(raop_rtp
->thread
, raop_rtp_thread_udp
, raop_rtp
);
590 THREAD_CREATE(raop_rtp
->thread
, raop_rtp_thread_tcp
, raop_rtp
);
592 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
596 raop_rtp_set_volume(raop_rtp_t
*raop_rtp
, float volume
)
602 } else if (volume
< -144.0f
) {
606 /* Set volume in thread instead */
607 MUTEX_LOCK(raop_rtp
->run_mutex
);
608 raop_rtp
->volume
= volume
;
609 raop_rtp
->volume_changed
= 1;
610 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
614 raop_rtp_set_metadata(raop_rtp_t
*raop_rtp
, const char *data
, int datalen
)
616 unsigned char *metadata
;
623 metadata
= malloc(datalen
);
625 memcpy(metadata
, data
, datalen
);
627 /* Set metadata in thread instead */
628 MUTEX_LOCK(raop_rtp
->run_mutex
);
629 raop_rtp
->metadata
= metadata
;
630 raop_rtp
->metadata_len
= datalen
;
631 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
635 raop_rtp_set_coverart(raop_rtp_t
*raop_rtp
, const char *data
, int datalen
)
637 unsigned char *coverart
;
644 coverart
= malloc(datalen
);
646 memcpy(coverart
, data
, datalen
);
648 /* Set coverart in thread instead */
649 MUTEX_LOCK(raop_rtp
->run_mutex
);
650 raop_rtp
->coverart
= coverart
;
651 raop_rtp
->coverart_len
= datalen
;
652 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
656 raop_rtp_flush(raop_rtp_t
*raop_rtp
, int next_seq
)
660 /* Call flush in thread instead */
661 MUTEX_LOCK(raop_rtp
->run_mutex
);
662 raop_rtp
->flush
= next_seq
;
663 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
667 raop_rtp_stop(raop_rtp_t
*raop_rtp
)
671 /* Check that we are running and thread is not
672 * joined (should never be while still running) */
673 MUTEX_LOCK(raop_rtp
->run_mutex
);
674 if (!raop_rtp
->running
|| raop_rtp
->joined
) {
675 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
678 raop_rtp
->running
= 0;
679 MUTEX_UNLOCK(raop_rtp
->run_mutex
);
681 /* Join the thread */
682 THREAD_JOIN(raop_rtp
->thread
);
683 if (raop_rtp
->csock
!= -1) closesocket(raop_rtp
->csock
);
684 if (raop_rtp
->tsock
!= -1) closesocket(raop_rtp
->tsock
);
685 if (raop_rtp
->dsock
!= -1) closesocket(raop_rtp
->dsock
);
687 /* Flush buffer into initial state */
688 raop_buffer_flush(raop_rtp
->buffer
, -1);
690 /* Mark thread as joined */
691 MUTEX_LOCK(raop_rtp
->run_mutex
);
692 raop_rtp
->joined
= 1;
693 MUTEX_UNLOCK(raop_rtp
->run_mutex
);