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.
20 #include "raop_buffer.h"
25 #include "crypto/crypto.h"
26 #include "alac/alac.h"
28 #define RAOP_BUFFER_LENGTH 16
31 /* Packet available */
37 unsigned short seqnum
;
38 unsigned int timestamp
;
41 /* Audio buffer of valid length */
42 int audio_buffer_size
;
45 } raop_buffer_entry_t
;
47 struct raop_buffer_s
{
49 unsigned char aeskey
[RAOP_AESKEY_LEN
];
50 unsigned char aesiv
[RAOP_AESIV_LEN
];
53 ALACSpecificConfig alacConfig
;
56 /* First and last seqnum */
58 unsigned short first_seqnum
;
59 unsigned short last_seqnum
;
61 /* RTP buffer entries */
62 raop_buffer_entry_t entries
[RAOP_BUFFER_LENGTH
];
64 /* Buffer of all audio buffers */
72 get_fmtp_info(ALACSpecificConfig
*config
, const char *fmtp
)
79 /* Parse fmtp string to integers */
80 original
= strptr
= strdup(fmtp
);
81 for (i
=0; i
<12; i
++) {
86 intarr
[i
] = atoi(utils_strsep(&strptr
, " "));
89 original
= strptr
= NULL
;
91 /* Fill the config struct */
92 config
->frameLength
= intarr
[1];
93 config
->compatibleVersion
= intarr
[2];
94 config
->bitDepth
= intarr
[3];
95 config
->pb
= intarr
[4];
96 config
->mb
= intarr
[5];
97 config
->kb
= intarr
[6];
98 config
->numChannels
= intarr
[7];
99 config
->maxRun
= intarr
[8];
100 config
->maxFrameBytes
= intarr
[9];
101 config
->avgBitRate
= intarr
[10];
102 config
->sampleRate
= intarr
[11];
104 /* Validate supported audio types */
105 if (config
->bitDepth
!= 16) {
108 if (config
->numChannels
!= 2) {
116 set_decoder_info(alac_file
*alac
, ALACSpecificConfig
*config
)
118 unsigned char decoder_info
[48];
119 memset(decoder_info
, 0, sizeof(decoder_info
));
121 #define SET_UINT16(buf, value)do{\
122 (buf)[0] = (unsigned char)((value) >> 8);\
123 (buf)[1] = (unsigned char)(value);\
126 #define SET_UINT32(buf, value)do{\
127 (buf)[0] = (unsigned char)((value) >> 24);\
128 (buf)[1] = (unsigned char)((value) >> 16);\
129 (buf)[2] = (unsigned char)((value) >> 8);\
130 (buf)[3] = (unsigned char)(value);\
133 /* Construct decoder info buffer */
134 SET_UINT32(&decoder_info
[24], config
->frameLength
);
135 decoder_info
[28] = config
->compatibleVersion
;
136 decoder_info
[29] = config
->bitDepth
;
137 decoder_info
[30] = config
->pb
;
138 decoder_info
[31] = config
->mb
;
139 decoder_info
[32] = config
->kb
;
140 decoder_info
[33] = config
->numChannels
;
141 SET_UINT16(&decoder_info
[34], config
->maxRun
);
142 SET_UINT32(&decoder_info
[36], config
->maxFrameBytes
);
143 SET_UINT32(&decoder_info
[40], config
->avgBitRate
);
144 SET_UINT32(&decoder_info
[44], config
->sampleRate
);
145 alac_set_info(alac
, (char *) decoder_info
);
149 raop_buffer_init(const char *fmtp
,
150 const unsigned char *aeskey
,
151 const unsigned char *aesiv
)
153 raop_buffer_t
*raop_buffer
;
154 int audio_buffer_size
;
155 ALACSpecificConfig
*alacConfig
;
162 raop_buffer
= calloc(1, sizeof(raop_buffer_t
));
167 /* Parse fmtp information */
168 alacConfig
= &raop_buffer
->alacConfig
;
169 if (get_fmtp_info(alacConfig
, fmtp
) < 0) {
174 /* Allocate the output audio buffers */
175 audio_buffer_size
= alacConfig
->frameLength
*
176 alacConfig
->numChannels
*
177 alacConfig
->bitDepth
/8;
178 raop_buffer
->buffer_size
= audio_buffer_size
*
180 raop_buffer
->buffer
= malloc(raop_buffer
->buffer_size
);
181 if (!raop_buffer
->buffer
) {
185 for (i
=0; i
<RAOP_BUFFER_LENGTH
; i
++) {
186 raop_buffer_entry_t
*entry
= &raop_buffer
->entries
[i
];
187 entry
->audio_buffer_size
= audio_buffer_size
;
188 entry
->audio_buffer_len
= 0;
189 entry
->audio_buffer
= (char *)raop_buffer
->buffer
+i
*audio_buffer_size
;
192 /* Initialize ALAC decoder */
193 raop_buffer
->alac
= create_alac(alacConfig
->bitDepth
,
194 alacConfig
->numChannels
);
195 if (!raop_buffer
->alac
) {
196 free(raop_buffer
->buffer
);
200 set_decoder_info(raop_buffer
->alac
, alacConfig
);
202 /* Initialize AES keys */
203 memcpy(raop_buffer
->aeskey
, aeskey
, RAOP_AESKEY_LEN
);
204 memcpy(raop_buffer
->aesiv
, aesiv
, RAOP_AESIV_LEN
);
206 /* Mark buffer as empty */
207 raop_buffer
->is_empty
= 1;
212 raop_buffer_destroy(raop_buffer_t
*raop_buffer
)
215 destroy_alac(raop_buffer
->alac
);
216 free(raop_buffer
->buffer
);
221 const ALACSpecificConfig
*
222 raop_buffer_get_config(raop_buffer_t
*raop_buffer
)
226 return &raop_buffer
->alacConfig
;
230 seqnum_cmp(unsigned short s1
, unsigned short s2
)
236 raop_buffer_queue(raop_buffer_t
*raop_buffer
, unsigned char *data
, unsigned short datalen
, int use_seqnum
)
238 unsigned char packetbuf
[RAOP_PACKET_LEN
];
239 unsigned short seqnum
;
240 raop_buffer_entry_t
*entry
;
247 /* Check packet data length is valid */
248 if (datalen
< 12 || datalen
> RAOP_PACKET_LEN
) {
252 /* Get correct seqnum for the packet */
254 seqnum
= (data
[2] << 8) | data
[3];
256 seqnum
= raop_buffer
->first_seqnum
;
259 /* If this packet is too late, just skip it */
260 if (!raop_buffer
->is_empty
&& seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
) < 0) {
264 /* Check that there is always space in the buffer, otherwise flush */
265 if (seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
+RAOP_BUFFER_LENGTH
) >= 0) {
266 raop_buffer_flush(raop_buffer
, seqnum
);
269 /* Get entry corresponding our seqnum */
270 entry
= &raop_buffer
->entries
[seqnum
% RAOP_BUFFER_LENGTH
];
271 if (entry
->available
&& seqnum_cmp(entry
->seqnum
, seqnum
) == 0) {
272 /* Packet resend, we can safely ignore */
276 /* Update the raop_buffer entry header */
277 entry
->flags
= data
[0];
278 entry
->type
= data
[1];
279 entry
->seqnum
= seqnum
;
280 entry
->timestamp
= (data
[4] << 24) | (data
[5] << 16) |
281 (data
[6] << 8) | data
[7];
282 entry
->ssrc
= (data
[8] << 24) | (data
[9] << 16) |
283 (data
[10] << 8) | data
[11];
284 entry
->available
= 1;
286 /* Decrypt audio data */
287 encryptedlen
= (datalen
-12)/16*16;
288 AES_set_key(&aes_ctx
, raop_buffer
->aeskey
, raop_buffer
->aesiv
, AES_MODE_128
);
289 AES_convert_key(&aes_ctx
);
290 AES_cbc_decrypt(&aes_ctx
, &data
[12], packetbuf
, encryptedlen
);
291 memcpy(packetbuf
+encryptedlen
, &data
[12+encryptedlen
], datalen
-12-encryptedlen
);
293 /* Decode ALAC audio data */
294 outputlen
= entry
->audio_buffer_size
;
295 decode_frame(raop_buffer
->alac
, packetbuf
, entry
->audio_buffer
, &outputlen
);
296 entry
->audio_buffer_len
= outputlen
;
298 /* Update the raop_buffer seqnums */
299 if (raop_buffer
->is_empty
) {
300 raop_buffer
->first_seqnum
= seqnum
;
301 raop_buffer
->last_seqnum
= seqnum
;
302 raop_buffer
->is_empty
= 0;
304 if (seqnum_cmp(seqnum
, raop_buffer
->last_seqnum
) > 0) {
305 raop_buffer
->last_seqnum
= seqnum
;
311 raop_buffer_dequeue(raop_buffer_t
*raop_buffer
, int *length
, int no_resend
)
314 raop_buffer_entry_t
*entry
;
316 /* Calculate number of entries in the current buffer */
317 buflen
= seqnum_cmp(raop_buffer
->last_seqnum
, raop_buffer
->first_seqnum
)+1;
319 /* Cannot dequeue from empty buffer */
320 if (raop_buffer
->is_empty
|| buflen
<= 0) {
324 /* Get the first buffer entry for inspection */
325 entry
= &raop_buffer
->entries
[raop_buffer
->first_seqnum
% RAOP_BUFFER_LENGTH
];
327 /* If we do no resends, always return the first entry */
328 } else if (!entry
->available
) {
329 /* Check how much we have space left in the buffer */
330 if (buflen
< RAOP_BUFFER_LENGTH
) {
331 /* Return nothing and hope resend gets on time */
334 /* Risk of buffer overrun, return empty buffer */
337 /* Update buffer and validate entry */
338 raop_buffer
->first_seqnum
+= 1;
339 if (!entry
->available
) {
340 /* Return an empty audio buffer to skip audio */
341 *length
= entry
->audio_buffer_size
;
342 memset(entry
->audio_buffer
, 0, *length
);
343 return entry
->audio_buffer
;
345 entry
->available
= 0;
347 /* Return entry audio buffer */
348 *length
= entry
->audio_buffer_len
;
349 entry
->audio_buffer_len
= 0;
350 return entry
->audio_buffer
;
354 raop_buffer_handle_resends(raop_buffer_t
*raop_buffer
, raop_resend_cb_t resend_cb
, void *opaque
)
356 raop_buffer_entry_t
*entry
;
361 if (seqnum_cmp(raop_buffer
->first_seqnum
, raop_buffer
->last_seqnum
) < 0) {
364 for (seqnum
=raop_buffer
->first_seqnum
; seqnum_cmp(seqnum
, raop_buffer
->last_seqnum
)<0; seqnum
++) {
365 entry
= &raop_buffer
->entries
[seqnum
% RAOP_BUFFER_LENGTH
];
366 if (entry
->available
) {
370 if (seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
) == 0) {
373 count
= seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
);
374 resend_cb(opaque
, raop_buffer
->first_seqnum
, count
);
379 raop_buffer_flush(raop_buffer_t
*raop_buffer
, int next_seq
)
385 for (i
=0; i
<RAOP_BUFFER_LENGTH
; i
++) {
386 raop_buffer
->entries
[i
].available
= 0;
387 raop_buffer
->entries
[i
].audio_buffer_len
= 0;
389 if (next_seq
< 0 || next_seq
> 0xffff) {
390 raop_buffer
->is_empty
= 1;
392 raop_buffer
->first_seqnum
= next_seq
;
393 raop_buffer
->last_seqnum
= next_seq
-1;