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 *rtpmap
,
151 const unsigned char *aeskey
,
152 const unsigned char *aesiv
)
154 raop_buffer_t
*raop_buffer
;
155 int audio_buffer_size
;
156 ALACSpecificConfig
*alacConfig
;
164 raop_buffer
= calloc(1, sizeof(raop_buffer_t
));
169 /* Parse fmtp information */
170 alacConfig
= &raop_buffer
->alacConfig
;
171 if (get_fmtp_info(alacConfig
, fmtp
) < 0) {
176 /* Allocate the output audio buffers */
177 audio_buffer_size
= alacConfig
->frameLength
*
178 alacConfig
->numChannels
*
179 alacConfig
->bitDepth
/8;
180 raop_buffer
->buffer_size
= audio_buffer_size
*
182 raop_buffer
->buffer
= malloc(raop_buffer
->buffer_size
);
183 if (!raop_buffer
->buffer
) {
187 for (i
=0; i
<RAOP_BUFFER_LENGTH
; i
++) {
188 raop_buffer_entry_t
*entry
= &raop_buffer
->entries
[i
];
189 entry
->audio_buffer_size
= audio_buffer_size
;
190 entry
->audio_buffer_len
= 0;
191 entry
->audio_buffer
= (char *)raop_buffer
->buffer
+i
*audio_buffer_size
;
194 /* Initialize ALAC decoder */
195 raop_buffer
->alac
= create_alac(alacConfig
->bitDepth
,
196 alacConfig
->numChannels
);
197 if (!raop_buffer
->alac
) {
198 free(raop_buffer
->buffer
);
202 set_decoder_info(raop_buffer
->alac
, alacConfig
);
204 /* Initialize AES keys */
205 memcpy(raop_buffer
->aeskey
, aeskey
, RAOP_AESKEY_LEN
);
206 memcpy(raop_buffer
->aesiv
, aesiv
, RAOP_AESIV_LEN
);
208 /* Mark buffer as empty */
209 raop_buffer
->is_empty
= 1;
214 raop_buffer_destroy(raop_buffer_t
*raop_buffer
)
217 destroy_alac(raop_buffer
->alac
);
218 free(raop_buffer
->buffer
);
223 const ALACSpecificConfig
*
224 raop_buffer_get_config(raop_buffer_t
*raop_buffer
)
228 return &raop_buffer
->alacConfig
;
232 seqnum_cmp(unsigned short s1
, unsigned short s2
)
238 raop_buffer_queue(raop_buffer_t
*raop_buffer
, unsigned char *data
, unsigned short datalen
, int use_seqnum
)
240 unsigned char packetbuf
[RAOP_PACKET_LEN
];
241 unsigned short seqnum
;
242 raop_buffer_entry_t
*entry
;
249 /* Check packet data length is valid */
250 if (datalen
< 12 || datalen
> RAOP_PACKET_LEN
) {
254 /* Get correct seqnum for the packet */
256 seqnum
= (data
[2] << 8) | data
[3];
258 seqnum
= raop_buffer
->first_seqnum
;
261 /* If this packet is too late, just skip it */
262 if (!raop_buffer
->is_empty
&& seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
) < 0) {
266 /* Check that there is always space in the buffer, otherwise flush */
267 if (seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
+RAOP_BUFFER_LENGTH
) >= 0) {
268 raop_buffer_flush(raop_buffer
, seqnum
);
271 /* Get entry corresponding our seqnum */
272 entry
= &raop_buffer
->entries
[seqnum
% RAOP_BUFFER_LENGTH
];
273 if (entry
->available
&& seqnum_cmp(entry
->seqnum
, seqnum
) == 0) {
274 /* Packet resend, we can safely ignore */
278 /* Update the raop_buffer entry header */
279 entry
->flags
= data
[0];
280 entry
->type
= data
[1];
281 entry
->seqnum
= seqnum
;
282 entry
->timestamp
= (data
[4] << 24) | (data
[5] << 16) |
283 (data
[6] << 8) | data
[7];
284 entry
->ssrc
= (data
[8] << 24) | (data
[9] << 16) |
285 (data
[10] << 8) | data
[11];
286 entry
->available
= 1;
288 /* Decrypt audio data */
289 encryptedlen
= (datalen
-12)/16*16;
290 AES_set_key(&aes_ctx
, raop_buffer
->aeskey
, raop_buffer
->aesiv
, AES_MODE_128
);
291 AES_convert_key(&aes_ctx
);
292 AES_cbc_decrypt(&aes_ctx
, &data
[12], packetbuf
, encryptedlen
);
293 memcpy(packetbuf
+encryptedlen
, &data
[12+encryptedlen
], datalen
-12-encryptedlen
);
295 /* Decode ALAC audio data */
296 outputlen
= entry
->audio_buffer_size
;
297 decode_frame(raop_buffer
->alac
, packetbuf
, entry
->audio_buffer
, &outputlen
);
298 entry
->audio_buffer_len
= outputlen
;
300 /* Update the raop_buffer seqnums */
301 if (raop_buffer
->is_empty
) {
302 raop_buffer
->first_seqnum
= seqnum
;
303 raop_buffer
->last_seqnum
= seqnum
;
304 raop_buffer
->is_empty
= 0;
306 if (seqnum_cmp(seqnum
, raop_buffer
->last_seqnum
) > 0) {
307 raop_buffer
->last_seqnum
= seqnum
;
313 raop_buffer_dequeue(raop_buffer_t
*raop_buffer
, int *length
, int no_resend
)
316 raop_buffer_entry_t
*entry
;
318 /* Calculate number of entries in the current buffer */
319 buflen
= seqnum_cmp(raop_buffer
->last_seqnum
, raop_buffer
->first_seqnum
)+1;
321 /* Cannot dequeue from empty buffer */
322 if (raop_buffer
->is_empty
|| buflen
<= 0) {
326 /* Get the first buffer entry for inspection */
327 entry
= &raop_buffer
->entries
[raop_buffer
->first_seqnum
% RAOP_BUFFER_LENGTH
];
329 /* If we do no resends, always return the first entry */
330 } else if (!entry
->available
) {
331 /* Check how much we have space left in the buffer */
332 if (buflen
< RAOP_BUFFER_LENGTH
) {
333 /* Return nothing and hope resend gets on time */
336 /* Risk of buffer overrun, return empty buffer */
339 /* Update buffer and validate entry */
340 raop_buffer
->first_seqnum
+= 1;
341 if (!entry
->available
) {
342 /* Return an empty audio buffer to skip audio */
343 *length
= entry
->audio_buffer_size
;
344 memset(entry
->audio_buffer
, 0, *length
);
345 return entry
->audio_buffer
;
347 entry
->available
= 0;
349 /* Return entry audio buffer */
350 *length
= entry
->audio_buffer_len
;
351 entry
->audio_buffer_len
= 0;
352 return entry
->audio_buffer
;
356 raop_buffer_handle_resends(raop_buffer_t
*raop_buffer
, raop_resend_cb_t resend_cb
, void *opaque
)
358 raop_buffer_entry_t
*entry
;
363 if (seqnum_cmp(raop_buffer
->first_seqnum
, raop_buffer
->last_seqnum
) < 0) {
366 for (seqnum
=raop_buffer
->first_seqnum
; seqnum_cmp(seqnum
, raop_buffer
->last_seqnum
)<0; seqnum
++) {
367 entry
= &raop_buffer
->entries
[seqnum
% RAOP_BUFFER_LENGTH
];
368 if (entry
->available
) {
372 if (seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
) == 0) {
375 count
= seqnum_cmp(seqnum
, raop_buffer
->first_seqnum
);
376 resend_cb(opaque
, raop_buffer
->first_seqnum
, count
);
381 raop_buffer_flush(raop_buffer_t
*raop_buffer
, int next_seq
)
387 for (i
=0; i
<RAOP_BUFFER_LENGTH
; i
++) {
388 raop_buffer
->entries
[i
].available
= 0;
389 raop_buffer
->entries
[i
].audio_buffer_len
= 0;
391 if (next_seq
< 0 || next_seq
> 0xffff) {
392 raop_buffer
->is_empty
= 1;
394 raop_buffer
->first_seqnum
= next_seq
;
395 raop_buffer
->last_seqnum
= next_seq
-1;