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
)
78 /* Parse fmtp string to integers */
79 strptr
= strdup(fmtp
);
80 for (i
=0; i
<12; i
++) {
85 intarr
[i
] = atoi(utils_strsep(&strptr
, " "));
90 /* Fill the config struct */
91 config
->frameLength
= intarr
[1];
92 config
->compatibleVersion
= intarr
[2];
93 config
->bitDepth
= intarr
[3];
94 config
->pb
= intarr
[4];
95 config
->mb
= intarr
[5];
96 config
->kb
= intarr
[6];
97 config
->numChannels
= intarr
[7];
98 config
->maxRun
= intarr
[8];
99 config
->maxFrameBytes
= intarr
[9];
100 config
->avgBitRate
= intarr
[10];
101 config
->sampleRate
= intarr
[11];
103 /* Validate supported audio types */
104 if (config
->bitDepth
!= 16) {
107 if (config
->numChannels
!= 2) {
115 set_decoder_info(alac_file
*alac
, ALACSpecificConfig
*config
)
117 unsigned char decoder_info
[48];
118 memset(decoder_info
, 0, sizeof(decoder_info
));
120 #define SET_UINT16(buf, value)do{\
121 (buf)[0] = (unsigned char)((value) >> 8);\
122 (buf)[1] = (unsigned char)(value);\
125 #define SET_UINT32(buf, value)do{\
126 (buf)[0] = (unsigned char)((value) >> 24);\
127 (buf)[1] = (unsigned char)((value) >> 16);\
128 (buf)[2] = (unsigned char)((value) >> 8);\
129 (buf)[3] = (unsigned char)(value);\
132 /* Construct decoder info buffer */
133 SET_UINT32(&decoder_info
[24], config
->frameLength
);
134 decoder_info
[28] = config
->compatibleVersion
;
135 decoder_info
[29] = config
->bitDepth
;
136 decoder_info
[30] = config
->pb
;
137 decoder_info
[31] = config
->mb
;
138 decoder_info
[32] = config
->kb
;
139 decoder_info
[33] = config
->numChannels
;
140 SET_UINT16(&decoder_info
[34], config
->maxRun
);
141 SET_UINT32(&decoder_info
[36], config
->maxFrameBytes
);
142 SET_UINT32(&decoder_info
[40], config
->avgBitRate
);
143 SET_UINT32(&decoder_info
[44], config
->sampleRate
);
144 alac_set_info(alac
, (char *) decoder_info
);
148 raop_buffer_init(const char *fmtp
,
149 const unsigned char *aeskey
,
150 const unsigned char *aesiv
)
152 raop_buffer_t
*raop_buffer
;
153 int audio_buffer_size
;
154 ALACSpecificConfig
*alacConfig
;
161 raop_buffer
= calloc(1, sizeof(raop_buffer_t
));
166 /* Parse fmtp information */
167 alacConfig
= &raop_buffer
->alacConfig
;
168 if (get_fmtp_info(alacConfig
, fmtp
) < 0) {
173 /* Allocate the output audio buffers */
174 audio_buffer_size
= alacConfig
->frameLength
*
175 alacConfig
->numChannels
*
176 alacConfig
->bitDepth
/8;
177 raop_buffer
->buffer_size
= audio_buffer_size
*
179 raop_buffer
->buffer
= malloc(raop_buffer
->buffer_size
);
180 if (!raop_buffer
->buffer
) {
184 for (i
=0; i
<RAOP_BUFFER_LENGTH
; i
++) {
185 raop_buffer_entry_t
*entry
= &raop_buffer
->entries
[i
];
186 entry
->audio_buffer_size
= audio_buffer_size
;
187 entry
->audio_buffer_len
= 0;
188 entry
->audio_buffer
= raop_buffer
->buffer
+i
*audio_buffer_size
;
191 /* Initialize ALAC decoder */
192 raop_buffer
->alac
= create_alac(alacConfig
->bitDepth
,
193 alacConfig
->numChannels
);
194 if (!raop_buffer
->alac
) {
195 free(raop_buffer
->buffer
);
199 set_decoder_info(raop_buffer
->alac
, alacConfig
);
201 /* Initialize AES keys */
202 memcpy(raop_buffer
->aeskey
, aeskey
, RAOP_AESKEY_LEN
);
203 memcpy(raop_buffer
->aesiv
, aesiv
, RAOP_AESIV_LEN
);
205 /* Mark buffer as empty */
206 raop_buffer
->is_empty
= 1;
211 raop_buffer_destroy(raop_buffer_t
*raop_buffer
)
214 free(raop_buffer
->buffer
);
215 free(raop_buffer
->alac
);
220 const ALACSpecificConfig
*
221 raop_buffer_get_config(raop_buffer_t
*raop_buffer
)
225 return &raop_buffer
->alacConfig
;
229 seqnum_cmp(unsigned short s1
, unsigned short s2
)
235 raop_buffer_queue(raop_buffer_t
*raop_buffer
, unsigned char *data
, unsigned short datalen
, int use_seqnum
)
237 unsigned char packetbuf
[RAOP_PACKET_LEN
];
238 unsigned short seqnum
;
239 raop_buffer_entry_t
*entry
;
246 /* Check packet data length is valid */
247 if (datalen
< 12 || datalen
> RAOP_PACKET_LEN
) {
251 /* Get correct seqnum for the packet */
253 seqnum
= (data
[2] << 8) | data
[3];
255 seqnum
= raop_buffer
->first_seqnum
;
258 /* If this packet is too late, just skip it */
259 if (!raop_buffer
->is_empty
&& seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
) < 0) {
263 /* Check that there is always space in the buffer, otherwise flush */
264 if (seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
+RAOP_BUFFER_LENGTH
) >= 0) {
265 raop_buffer_flush(raop_buffer
, seqnum
);
268 /* Get entry corresponding our seqnum */
269 entry
= &raop_buffer
->entries
[seqnum
% RAOP_BUFFER_LENGTH
];
270 if (entry
->available
&& seqnum_cmp(entry
->seqnum
, seqnum
) == 0) {
271 /* Packet resend, we can safely ignore */
275 /* Update the raop_buffer entry header */
276 entry
->flags
= data
[0];
277 entry
->type
= data
[1];
278 entry
->seqnum
= seqnum
;
279 entry
->timestamp
= (data
[4] << 24) | (data
[5] << 16) |
280 (data
[6] << 8) | data
[7];
281 entry
->ssrc
= (data
[8] << 24) | (data
[9] << 16) |
282 (data
[10] << 8) | data
[11];
283 entry
->available
= 1;
285 /* Decrypt audio data */
286 encryptedlen
= (datalen
-12)/16*16;
287 AES_set_key(&aes_ctx
, raop_buffer
->aeskey
, raop_buffer
->aesiv
, AES_MODE_128
);
288 AES_convert_key(&aes_ctx
);
289 AES_cbc_decrypt(&aes_ctx
, &data
[12], packetbuf
, encryptedlen
);
290 memcpy(packetbuf
+encryptedlen
, &data
[12+encryptedlen
], datalen
-12-encryptedlen
);
292 /* Decode ALAC audio data */
293 outputlen
= entry
->audio_buffer_size
;
294 decode_frame(raop_buffer
->alac
, packetbuf
, entry
->audio_buffer
, &outputlen
);
295 entry
->audio_buffer_len
= outputlen
;
297 /* Update the raop_buffer seqnums */
298 if (raop_buffer
->is_empty
) {
299 raop_buffer
->first_seqnum
= seqnum
;
300 raop_buffer
->last_seqnum
= seqnum
;
301 raop_buffer
->is_empty
= 0;
303 if (seqnum_cmp(seqnum
, raop_buffer
->last_seqnum
) > 0) {
304 raop_buffer
->last_seqnum
= seqnum
;
310 raop_buffer_dequeue(raop_buffer_t
*raop_buffer
, int *length
, int no_resend
)
313 raop_buffer_entry_t
*entry
;
315 /* Calculate number of entries in the current buffer */
316 buflen
= seqnum_cmp(raop_buffer
->last_seqnum
, raop_buffer
->first_seqnum
)+1;
318 /* Cannot dequeue from empty buffer */
319 if (raop_buffer
->is_empty
|| buflen
<= 0) {
323 /* Get the first buffer entry for inspection */
324 entry
= &raop_buffer
->entries
[raop_buffer
->first_seqnum
% RAOP_BUFFER_LENGTH
];
326 /* If we do no resends, always return the first entry */
327 } else if (!entry
->available
) {
328 /* Check how much we have space left in the buffer */
329 if (buflen
< RAOP_BUFFER_LENGTH
) {
330 /* Return nothing and hope resend gets on time */
333 /* Risk of buffer overrun, return empty buffer */
336 /* Update buffer and validate entry */
337 raop_buffer
->first_seqnum
+= 1;
338 if (!entry
->available
) {
339 /* Return an empty audio buffer to skip audio */
340 *length
= entry
->audio_buffer_size
;
341 memset(entry
->audio_buffer
, 0, *length
);
342 return entry
->audio_buffer
;
344 entry
->available
= 0;
346 /* Return entry audio buffer */
347 *length
= entry
->audio_buffer_len
;
348 entry
->audio_buffer_len
= 0;
349 return entry
->audio_buffer
;
353 raop_buffer_handle_resends(raop_buffer_t
*raop_buffer
, raop_resend_cb_t resend_cb
, void *opaque
)
355 raop_buffer_entry_t
*entry
;
360 if (seqnum_cmp(raop_buffer
->first_seqnum
, raop_buffer
->last_seqnum
) < 0) {
363 for (seqnum
=raop_buffer
->first_seqnum
; seqnum_cmp(seqnum
, raop_buffer
->last_seqnum
)<0; seqnum
++) {
364 entry
= &raop_buffer
->entries
[seqnum
% RAOP_BUFFER_LENGTH
];
365 if (entry
->available
) {
369 if (seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
) == 0) {
372 count
= seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
);
373 resend_cb(opaque
, raop_buffer
->first_seqnum
, count
);
378 raop_buffer_flush(raop_buffer_t
*raop_buffer
, int next_seq
)
384 for (i
=0; i
<RAOP_BUFFER_LENGTH
; i
++) {
385 raop_buffer
->entries
[i
].available
= 0;
386 raop_buffer
->entries
[i
].audio_buffer_len
= 0;
388 if (next_seq
< 0 || next_seq
> 0xffff) {
389 raop_buffer
->is_empty
= 1;
391 raop_buffer
->first_seqnum
= next_seq
;
392 raop_buffer
->last_seqnum
= next_seq
-1;