Simplify the set_log_callback, it is better for thread safety
[deb_shairplay.git] / src / lib / raop_buffer.c
CommitLineData
23e7e3ae
JVH
1/**
2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
3 *
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.
8 *
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.
13 */
14
2340bcd3
JVH
15#include <stdlib.h>
16#include <string.h>
17#include <assert.h>
18#include <math.h>
19
20#include "raop_buffer.h"
21#include "raop_rtp.h"
22#include "utils.h"
23
24#include <stdint.h>
25#include "crypto/crypto.h"
26#include "alac/alac.h"
27
28#define RAOP_BUFFER_LENGTH 16
29
30typedef struct {
31 /* Packet available */
32 int available;
33
34 /* RTP header */
35 unsigned char flags;
36 unsigned char type;
37 unsigned short seqnum;
38 unsigned int timestamp;
39 unsigned int ssrc;
40
41 /* Audio buffer of valid length */
42 int audio_buffer_size;
43 int audio_buffer_len;
44 void *audio_buffer;
45} raop_buffer_entry_t;
46
47struct raop_buffer_s {
48 /* AES key and IV */
49 unsigned char aeskey[RAOP_AESKEY_LEN];
50 unsigned char aesiv[RAOP_AESIV_LEN];
51
52 /* ALAC decoder */
53 ALACSpecificConfig alacConfig;
54 alac_file *alac;
55
56 /* First and last seqnum */
57 int is_empty;
58 unsigned short first_seqnum;
59 unsigned short last_seqnum;
60
61 /* RTP buffer entries */
62 raop_buffer_entry_t entries[RAOP_BUFFER_LENGTH];
63
64 /* Buffer of all audio buffers */
65 int buffer_size;
66 void *buffer;
67};
68
69
70
71static int
72get_fmtp_info(ALACSpecificConfig *config, const char *fmtp)
73{
74 int intarr[12];
a5670bb8 75 char *original;
2340bcd3
JVH
76 char *strptr;
77 int i;
78
79 /* Parse fmtp string to integers */
a5670bb8 80 original = strptr = strdup(fmtp);
2340bcd3
JVH
81 for (i=0; i<12; i++) {
82 if (strptr == NULL) {
a5670bb8 83 free(original);
2340bcd3
JVH
84 return -1;
85 }
86 intarr[i] = atoi(utils_strsep(&strptr, " "));
87 }
a5670bb8
JVH
88 free(original);
89 original = strptr = NULL;
2340bcd3
JVH
90
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];
103
104 /* Validate supported audio types */
105 if (config->bitDepth != 16) {
106 return -2;
107 }
108 if (config->numChannels != 2) {
109 return -3;
110 }
111
112 return 0;
113}
114
115static void
116set_decoder_info(alac_file *alac, ALACSpecificConfig *config)
117{
118 unsigned char decoder_info[48];
119 memset(decoder_info, 0, sizeof(decoder_info));
120
121#define SET_UINT16(buf, value)do{\
122 (buf)[0] = (unsigned char)((value) >> 8);\
123 (buf)[1] = (unsigned char)(value);\
124 }while(0)
125
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);\
131 }while(0)
132
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);
146}
147
148raop_buffer_t *
149raop_buffer_init(const char *fmtp,
150 const unsigned char *aeskey,
151 const unsigned char *aesiv)
152{
153 raop_buffer_t *raop_buffer;
154 int audio_buffer_size;
155 ALACSpecificConfig *alacConfig;
156 int i;
157
158 assert(fmtp);
159 assert(aeskey);
160 assert(aesiv);
161
162 raop_buffer = calloc(1, sizeof(raop_buffer_t));
163 if (!raop_buffer) {
164 return NULL;
165 }
166
167 /* Parse fmtp information */
168 alacConfig = &raop_buffer->alacConfig;
169 if (get_fmtp_info(alacConfig, fmtp) < 0) {
170 free(raop_buffer);
171 return NULL;
172 }
173
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 *
179 RAOP_BUFFER_LENGTH;
180 raop_buffer->buffer = malloc(raop_buffer->buffer_size);
181 if (!raop_buffer->buffer) {
182 free(raop_buffer);
183 return NULL;
184 }
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;
566c9bf8 189 entry->audio_buffer = (char *)raop_buffer->buffer+i*audio_buffer_size;
2340bcd3
JVH
190 }
191
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);
197 free(raop_buffer);
198 return NULL;
199 }
200 set_decoder_info(raop_buffer->alac, alacConfig);
201
202 /* Initialize AES keys */
203 memcpy(raop_buffer->aeskey, aeskey, RAOP_AESKEY_LEN);
204 memcpy(raop_buffer->aesiv, aesiv, RAOP_AESIV_LEN);
205
206 /* Mark buffer as empty */
207 raop_buffer->is_empty = 1;
208 return raop_buffer;
209}
210
211void
212raop_buffer_destroy(raop_buffer_t *raop_buffer)
213{
214 if (raop_buffer) {
982a0c26 215 destroy_alac(raop_buffer->alac);
2340bcd3 216 free(raop_buffer->buffer);
2340bcd3
JVH
217 free(raop_buffer);
218 }
219}
220
221const ALACSpecificConfig *
222raop_buffer_get_config(raop_buffer_t *raop_buffer)
223{
224 assert(raop_buffer);
225
226 return &raop_buffer->alacConfig;
227}
228
229static short
230seqnum_cmp(unsigned short s1, unsigned short s2)
231{
232 return (s1 - s2);
233}
234
235int
236raop_buffer_queue(raop_buffer_t *raop_buffer, unsigned char *data, unsigned short datalen, int use_seqnum)
237{
238 unsigned char packetbuf[RAOP_PACKET_LEN];
239 unsigned short seqnum;
240 raop_buffer_entry_t *entry;
241 int encryptedlen;
242 AES_CTX aes_ctx;
243 int outputlen;
244
245 assert(raop_buffer);
246
247 /* Check packet data length is valid */
248 if (datalen < 12 || datalen > RAOP_PACKET_LEN) {
249 return -1;
250 }
251
252 /* Get correct seqnum for the packet */
253 if (use_seqnum) {
254 seqnum = (data[2] << 8) | data[3];
255 } else {
256 seqnum = raop_buffer->first_seqnum;
257 }
258
259 /* If this packet is too late, just skip it */
260 if (!raop_buffer->is_empty && seqnum_cmp(seqnum, raop_buffer->first_seqnum) < 0) {
261 return 0;
262 }
263
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);
267 }
268
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 */
273 return 0;
274 }
275
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;
285
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);
292
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;
297
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;
303 }
304 if (seqnum_cmp(seqnum, raop_buffer->last_seqnum) > 0) {
305 raop_buffer->last_seqnum = seqnum;
306 }
307 return 1;
308}
309
310const void *
311raop_buffer_dequeue(raop_buffer_t *raop_buffer, int *length, int no_resend)
312{
313 short buflen;
314 raop_buffer_entry_t *entry;
315
316 /* Calculate number of entries in the current buffer */
317 buflen = seqnum_cmp(raop_buffer->last_seqnum, raop_buffer->first_seqnum)+1;
318
319 /* Cannot dequeue from empty buffer */
320 if (raop_buffer->is_empty || buflen <= 0) {
321 return NULL;
322 }
323
324 /* Get the first buffer entry for inspection */
325 entry = &raop_buffer->entries[raop_buffer->first_seqnum % RAOP_BUFFER_LENGTH];
326 if (no_resend) {
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 */
332 return NULL;
333 }
334 /* Risk of buffer overrun, return empty buffer */
335 }
336
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;
344 }
345 entry->available = 0;
346
347 /* Return entry audio buffer */
348 *length = entry->audio_buffer_len;
349 entry->audio_buffer_len = 0;
350 return entry->audio_buffer;
351}
352
353void
354raop_buffer_handle_resends(raop_buffer_t *raop_buffer, raop_resend_cb_t resend_cb, void *opaque)
355{
356 raop_buffer_entry_t *entry;
357
358 assert(raop_buffer);
359 assert(resend_cb);
360
361 if (seqnum_cmp(raop_buffer->first_seqnum, raop_buffer->last_seqnum) < 0) {
362 int seqnum, count;
363
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) {
367 break;
368 }
369 }
370 if (seqnum_cmp(seqnum, raop_buffer->first_seqnum) == 0) {
371 return;
372 }
373 count = seqnum_cmp(seqnum, raop_buffer->first_seqnum);
374 resend_cb(opaque, raop_buffer->first_seqnum, count);
375 }
376}
377
378void
379raop_buffer_flush(raop_buffer_t *raop_buffer, int next_seq)
380{
381 int i;
382
383 assert(raop_buffer);
384
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;
388 }
389 if (next_seq < 0 || next_seq > 0xffff) {
390 raop_buffer->is_empty = 1;
391 } else {
392 raop_buffer->first_seqnum = next_seq;
393 raop_buffer->last_seqnum = next_seq-1;
394 }
395}