2 This file is part of PulseAudio.
4 Copyright 2008 Colin Guthrie
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
38 /* TODO: Replace OpenSSL with NSS */
39 #include <openssl/err.h>
40 #include <openssl/rand.h>
41 #include <openssl/aes.h>
42 #include <openssl/rsa.h>
43 #include <openssl/engine.h>
45 #include <pulse/xmalloc.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/socket-util.h>
50 #include <pulsecore/log.h>
51 #include <pulsecore/macro.h>
52 #include <pulsecore/strbuf.h>
53 #include <pulsecore/random.h>
54 #include <pulsecore/poll.h>
56 #include "raop_client.h"
57 #include "rtsp_client.h"
60 #define AES_CHUNKSIZE 16
62 #define JACK_STATUS_DISCONNECTED 0
63 #define JACK_STATUS_CONNECTED 1
65 #define JACK_TYPE_ANALOG 0
66 #define JACK_TYPE_DIGITAL 1
68 #define VOLUME_DEF -30
69 #define VOLUME_MIN -144
73 struct pa_raop_client
{
82 /* Encryption Related bits */
84 uint8_t aes_iv
[AES_CHUNKSIZE
]; /* initialization vector for aes-cbc */
85 uint8_t aes_nv
[AES_CHUNKSIZE
]; /* next vector for aes-cbc */
86 uint8_t aes_key
[AES_CHUNKSIZE
]; /* key for aes-cbc */
94 pa_raop_client_cb_t callback
;
96 pa_raop_client_closed_cb_t closed_callback
;
97 void* closed_userdata
;
101 * Function to write bits into a buffer.
102 * @param buffer Handle to the buffer. It will be incremented if new data requires it.
103 * @param bit_pos A pointer to a position buffer to keep track the current write location (0 for MSB, 7 for LSB)
104 * @param size A pointer to the byte size currently written. This allows the calling function to do simple buffer overflow checks
105 * @param data The data to write
106 * @param data_bit_len The number of bits from data to write
108 static inline void bit_writer(uint8_t **buffer
, uint8_t *bit_pos
, int *size
, uint8_t data
, uint8_t data_bit_len
) {
109 int bits_left
, bit_overflow
;
115 /* If bit pos is zero, we will definatly use at least one bit from the current byte so size increments. */
119 /* Calc the number of bits left in the current byte of buffer */
120 bits_left
= 7 - *bit_pos
+ 1;
121 /* Calc the overflow of bits in relation to how much space we have left... */
122 bit_overflow
= bits_left
- data_bit_len
;
123 if (bit_overflow
>= 0) {
124 /* We can fit the new data in our current byte */
125 /* As we write from MSB->LSB we need to left shift by the overflow amount */
126 bit_data
= data
<< bit_overflow
;
128 **buffer
|= bit_data
;
131 /* If our data fits exactly into the current byte, we need to increment our pointer */
132 if (0 == bit_overflow
) {
133 /* Do not increment size as it will be incremeneted on next call as bit_pos is zero */
137 *bit_pos
+= data_bit_len
;
140 /* bit_overflow is negative, there for we will need a new byte from our buffer */
141 /* Firstly fill up what's left in the current byte */
142 bit_data
= data
>> -bit_overflow
;
143 **buffer
|= bit_data
;
144 /* Increment our buffer pointer and size counter*/
147 **buffer
= data
<< (8 + bit_overflow
);
148 *bit_pos
= -bit_overflow
;
152 static int rsa_encrypt(uint8_t *text
, int len
, uint8_t *res
) {
154 "59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUtwC"
155 "5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDR"
156 "KSKv6kDqnw4UwPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuB"
157 "OitnZ/bDzPHrTOZz0Dew0uowxf/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJ"
158 "Q+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/UAaHqn9JdsBWLUEpVviYnh"
159 "imNVvYFZeCXg/IdTQ+x4IRdiXNv5hEew==";
160 const char e
[] = "AQAB";
161 uint8_t modules
[256];
167 size
= pa_base64_decode(n
, modules
);
168 rsa
->n
= BN_bin2bn(modules
, size
, NULL
);
169 size
= pa_base64_decode(e
, exponent
);
170 rsa
->e
= BN_bin2bn(exponent
, size
, NULL
);
172 size
= RSA_public_encrypt(len
, text
, res
, rsa
, RSA_PKCS1_OAEP_PADDING
);
177 static int aes_encrypt(pa_raop_client
* c
, uint8_t *data
, int size
) {
183 memcpy(c
->aes_nv
, c
->aes_iv
, AES_CHUNKSIZE
);
184 while (i
+AES_CHUNKSIZE
<= size
) {
186 for (j
=0; j
<AES_CHUNKSIZE
; ++j
)
187 buf
[j
] ^= c
->aes_nv
[j
];
189 AES_encrypt(buf
, buf
, &c
->aes
);
190 memcpy(c
->aes_nv
, buf
, AES_CHUNKSIZE
);
196 static inline void rtrimchar(char *str
, char rc
) {
197 char *sp
= str
+ strlen(str
) - 1;
198 while (sp
>= str
&& *sp
== rc
) {
204 static void on_connection(pa_socket_client
*sc
, pa_iochannel
*io
, void *userdata
) {
205 pa_raop_client
*c
= userdata
;
209 pa_assert(c
->sc
== sc
);
210 pa_assert(c
->fd
< 0);
211 pa_assert(c
->callback
);
213 pa_socket_client_unref(c
->sc
);
217 pa_log("Connection failed: %s", pa_cstrerror(errno
));
221 c
->fd
= pa_iochannel_get_send_fd(io
);
223 pa_iochannel_set_noclose(io
, TRUE
);
224 pa_iochannel_socket_set_sndbuf(io
, 1024);
225 pa_iochannel_free(io
);
227 pa_make_tcp_socket_low_delay(c
->fd
);
229 pa_log_debug("Connection established");
230 c
->callback(c
->fd
, c
->userdata
);
233 static void rtsp_cb(pa_rtsp_client
*rtsp
, pa_rtsp_state state
, pa_headerlist
* headers
, void *userdata
) {
234 pa_raop_client
* c
= userdata
;
237 pa_assert(rtsp
== c
->rtsp
);
240 case STATE_CONNECT
: {
243 char *key
, *iv
, *sac
, *sdp
;
248 pa_log_debug("RAOP: CONNECTED");
249 ip
= pa_rtsp_localip(c
->rtsp
);
250 /* First of all set the url properly */
251 url
= pa_sprintf_malloc("rtsp://%s/%s", ip
, c
->sid
);
252 pa_rtsp_set_url(c
->rtsp
, url
);
255 /* Now encrypt our aes_public key to send to the device */
256 i
= rsa_encrypt(c
->aes_key
, AES_CHUNKSIZE
, rsakey
);
257 pa_base64_encode(rsakey
, i
, &key
);
259 pa_base64_encode(c
->aes_iv
, AES_CHUNKSIZE
, &iv
);
262 pa_random(&rand_data
, sizeof(rand_data
));
263 pa_base64_encode(&rand_data
, AES_CHUNKSIZE
, &sac
);
265 pa_rtsp_add_header(c
->rtsp
, "Apple-Challenge", sac
);
266 sdp
= pa_sprintf_malloc(
268 "o=iTunes %s 0 IN IP4 %s\r\n"
272 "m=audio 0 RTP/AVP 96\r\n"
273 "a=rtpmap:96 AppleLossless\r\n"
274 "a=fmtp:96 4096 0 16 40 10 14 2 255 0 0 44100\r\n"
277 c
->sid
, ip
, c
->host
, key
, iv
);
278 pa_rtsp_announce(c
->rtsp
, sdp
);
287 pa_log_debug("RAOP: ANNOUNCED");
288 pa_rtsp_remove_header(c
->rtsp
, "Apple-Challenge");
289 pa_rtsp_setup(c
->rtsp
);
293 char *aj
= pa_xstrdup(pa_headerlist_gets(headers
, "Audio-Jack-Status"));
294 pa_log_debug("RAOP: SETUP");
297 char delimiters
[] = ";";
298 const char* token_state
= NULL
;
299 c
->jack_type
= JACK_TYPE_ANALOG
;
300 c
->jack_status
= JACK_STATUS_DISCONNECTED
;
302 while ((token
= pa_split(aj
, delimiters
, &token_state
))) {
303 if ((pc
= strstr(token
, "="))) {
305 if (!strcmp(token
, "type") && !strcmp(pc
+1, "digital")) {
306 c
->jack_type
= JACK_TYPE_DIGITAL
;
309 if (!strcmp(token
,"connected"))
310 c
->jack_status
= JACK_STATUS_CONNECTED
;
316 pa_log_warn("Audio Jack Status missing");
318 pa_rtsp_record(c
->rtsp
, &c
->seq
, &c
->rtptime
);
323 uint32_t port
= pa_rtsp_serverport(c
->rtsp
);
324 pa_log_debug("RAOP: RECORDED");
326 if (!(c
->sc
= pa_socket_client_new_string(c
->core
->mainloop
, TRUE
, c
->host
, port
))) {
327 pa_log("failed to connect to server '%s:%d'", c
->host
, port
);
330 pa_socket_client_set_callback(c
->sc
, on_connection
, c
);
335 pa_log_debug("RAOP: FLUSHED");
339 pa_log_debug("RAOP: TEARDOWN");
342 case STATE_SET_PARAMETER
:
343 pa_log_debug("RAOP: SET_PARAMETER");
346 case STATE_DISCONNECTED
:
347 pa_assert(c
->closed_callback
);
350 pa_log_debug("RTSP control channel closed");
351 pa_rtsp_client_free(c
->rtsp
);
354 /* We do not close the fd, we leave it to the closed callback to do that */
358 pa_socket_client_unref(c
->sc
);
363 c
->closed_callback(c
->closed_userdata
);
368 pa_raop_client
* pa_raop_client_new(pa_core
*core
, const char* host
) {
369 pa_raop_client
* c
= pa_xnew0(pa_raop_client
, 1);
376 c
->host
= pa_xstrdup(host
);
378 if (pa_raop_connect(c
)) {
379 pa_raop_client_free(c
);
386 void pa_raop_client_free(pa_raop_client
* c
) {
390 pa_rtsp_client_free(c
->rtsp
);
396 int pa_raop_connect(pa_raop_client
* c
) {
407 pa_log_debug("Connection already in progress");
411 c
->rtsp
= pa_rtsp_client_new(c
->core
->mainloop
, c
->host
, 5000, "iTunes/4.6 (Macintosh; U; PPC Mac OS X 10.3)");
413 /* Initialise the AES encryption system */
414 pa_random(c
->aes_iv
, sizeof(c
->aes_iv
));
415 pa_random(c
->aes_key
, sizeof(c
->aes_key
));
416 memcpy(c
->aes_nv
, c
->aes_iv
, sizeof(c
->aes_nv
));
417 AES_set_encrypt_key(c
->aes_key
, 128, &c
->aes
);
419 /* Generate random instance id */
420 pa_random(&rand_data
, sizeof(rand_data
));
421 c
->sid
= pa_sprintf_malloc("%u", rand_data
.a
);
422 sci
= pa_sprintf_malloc("%08x%08x",rand_data
.b
, rand_data
.c
);
423 pa_rtsp_add_header(c
->rtsp
, "Client-Instance", sci
);
425 pa_rtsp_set_callback(c
->rtsp
, rtsp_cb
, c
);
426 return pa_rtsp_connect(c
->rtsp
);
430 int pa_raop_flush(pa_raop_client
* c
) {
433 pa_rtsp_flush(c
->rtsp
, c
->seq
, c
->rtptime
);
438 int pa_raop_client_set_volume(pa_raop_client
* c
, pa_volume_t volume
) {
445 db
= pa_sw_volume_to_dB(volume
);
448 else if (db
> VOLUME_MAX
)
451 param
= pa_sprintf_malloc("volume: %0.6f\r\n", db
);
453 /* We just hit and hope, cannot wait for the callback */
454 rv
= pa_rtsp_setparameter(c
->rtsp
, param
);
460 int pa_raop_client_encode_sample(pa_raop_client
* c
, pa_memchunk
* raw
, pa_memchunk
* encoded
) {
464 uint8_t *ibp
, *maxibp
;
469 static uint8_t header
[] = {
470 0x24, 0x00, 0x00, 0x00,
471 0xF0, 0xFF, 0x00, 0x00,
472 0x00, 0x00, 0x00, 0x00,
473 0x00, 0x00, 0x00, 0x00,
475 int header_size
= sizeof(header
);
478 pa_assert(c
->fd
> 0);
480 pa_assert(raw
->memblock
);
481 pa_assert(raw
->length
> 0);
484 /* We have to send 4 byte chunks */
485 bsize
= (int)(raw
->length
/ 4);
488 /* Leave 16 bytes extra to allow for the ALAC header which is about 55 bits */
489 bufmax
= length
+ header_size
+ 16;
490 pa_memchunk_reset(encoded
);
491 encoded
->memblock
= pa_memblock_new(c
->core
->mempool
, bufmax
);
492 b
= pa_memblock_acquire(encoded
->memblock
);
493 memcpy(b
, header
, header_size
);
495 /* Now write the actual samples */
496 bp
= b
+ header_size
;
498 bit_writer(&bp
,&bpos
,&size
,1,3); /* channel=1, stereo */
499 bit_writer(&bp
,&bpos
,&size
,0,4); /* unknown */
500 bit_writer(&bp
,&bpos
,&size
,0,8); /* unknown */
501 bit_writer(&bp
,&bpos
,&size
,0,4); /* unknown */
502 bit_writer(&bp
,&bpos
,&size
,1,1); /* hassize */
503 bit_writer(&bp
,&bpos
,&size
,0,2); /* unused */
504 bit_writer(&bp
,&bpos
,&size
,1,1); /* is-not-compressed */
506 /* size of data, integer, big endian */
507 bit_writer(&bp
,&bpos
,&size
,(bsize
>>24)&0xff,8);
508 bit_writer(&bp
,&bpos
,&size
,(bsize
>>16)&0xff,8);
509 bit_writer(&bp
,&bpos
,&size
,(bsize
>>8)&0xff,8);
510 bit_writer(&bp
,&bpos
,&size
,(bsize
)&0xff,8);
512 ibp
= p
= pa_memblock_acquire(raw
->memblock
);
513 maxibp
= p
+ raw
->length
- 4;
514 while (ibp
<= maxibp
) {
515 /* Byte swap stereo data */
516 bit_writer(&bp
,&bpos
,&size
,*(ibp
+1),8);
517 bit_writer(&bp
,&bpos
,&size
,*(ibp
+0),8);
518 bit_writer(&bp
,&bpos
,&size
,*(ibp
+3),8);
519 bit_writer(&bp
,&bpos
,&size
,*(ibp
+2),8);
524 pa_memblock_release(raw
->memblock
);
525 encoded
->length
= header_size
+ size
;
527 /* store the lenght (endian swapped: make this better) */
528 len
= size
+ header_size
- 4;
530 *(b
+ 3) = len
& 0xff;
532 /* encrypt our data */
533 aes_encrypt(c
, (b
+ header_size
), size
);
535 /* We're done with the chunk */
536 pa_memblock_release(encoded
->memblock
);
542 void pa_raop_client_set_callback(pa_raop_client
* c
, pa_raop_client_cb_t callback
, void *userdata
) {
545 c
->callback
= callback
;
546 c
->userdata
= userdata
;
549 void pa_raop_client_set_closed_callback(pa_raop_client
* c
, pa_raop_client_closed_cb_t callback
, void *userdata
) {
552 c
->closed_callback
= callback
;
553 c
->closed_userdata
= userdata
;