Merge branch 'refs/heads/t/server_cmds'
[paraslash.git] / afh_common.c
1 /*
2 * Copyright (C) 1997-2014 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file afh_common.c Common audio format handler functions. */
8
9 #include <sys/mman.h> /* mmap */
10 #include <sys/types.h>
11 #include <regex.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "string.h"
16 #include "afh.h"
17
18 /* The mp3 audio format handler does not need any libs. */
19 void mp3_init(struct audio_format_handler *);
20
21 #ifdef HAVE_OGGVORBIS
22 void ogg_init(struct audio_format_handler *);
23 #endif
24 #ifdef HAVE_FAAD
25 void aac_afh_init(struct audio_format_handler *);
26 #endif
27 #ifdef HAVE_SPEEX
28 void spx_afh_init(struct audio_format_handler *);
29 #endif
30 #ifdef HAVE_FLAC
31 void flac_afh_init(struct audio_format_handler *);
32 #endif
33
34 #ifdef HAVE_OPUS
35 void opus_afh_init(struct audio_format_handler *);
36 #endif
37
38 void wma_afh_init(struct audio_format_handler *);
39
40 /** The list of all status items */
41 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
42
43 /**
44 * The list of supported audio formats.
45 *
46 * We always define the full array of audio formats even if some audio formats
47 * were not compiled in. This is because for each audio file the number of its
48 * audio format is stored in the database. We don't want that numbers to become
49 * stale just because the user installed a new version of paraslash that
50 * supports a different set of audio formats.
51 *
52 * It can still be easily detected whether an audio format is compiled in by
53 * checking if the init function pointer is not \p NULL.
54 */
55 static struct audio_format_handler afl[] = {
56 {
57 .name = "mp3",
58 .init = mp3_init,
59 },
60 {
61 .name = "ogg",
62 #ifdef HAVE_OGGVORBIS
63 .init = ogg_init,
64 #endif
65 },
66 {
67 .name = "aac",
68 #ifdef HAVE_FAAD
69 .init = aac_afh_init,
70 #endif
71 },
72 {
73 .name = "wma",
74 .init = wma_afh_init,
75 },
76 {
77 .name = "spx",
78 #ifdef HAVE_SPEEX
79 .init = spx_afh_init,
80 #endif
81 },
82 {
83 .name = "flac",
84 #ifdef HAVE_FLAC
85 .init = flac_afh_init,
86 #endif
87 },
88 {
89 .name = "opus",
90 #ifdef HAVE_OPUS
91 .init = opus_afh_init,
92 #endif
93 },
94 {
95 .name = NULL,
96 }
97 };
98
99 static inline int next_audio_format(int format)
100 {
101 for (;;) {
102 if (!afl[format].name)
103 return format;
104 format++;
105 if (afl[format].init)
106 return format;
107 }
108
109 }
110
111 /** Iterate over each supported audio format. */
112 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
113
114 /**
115 * Call the init function of each supported audio format handler.
116 */
117 void afh_init(void)
118 {
119 int i;
120
121 PARA_INFO_LOG("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
122 FOR_EACH_AUDIO_FORMAT(i) {
123 PARA_NOTICE_LOG("initializing %s handler\n",
124 audio_format_name(i));
125 afl[i].init(&afl[i]);
126 }
127 }
128
129 /**
130 * Guess the audio format judging from filename.
131 *
132 * \param name The filename.
133 *
134 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
135 * of audio file this might be. Otherwise the (non-negative) number of the
136 * audio format is returned.
137 */
138 int guess_audio_format(const char *name)
139 {
140 int i,j, len = strlen(name);
141
142 FOR_EACH_AUDIO_FORMAT(i) {
143 for (j = 0; afl[i].suffixes[j]; j++) {
144 const char *p = afl[i].suffixes[j];
145 int plen = strlen(p);
146 if (len < plen + 1)
147 continue;
148 if (name[len - plen - 1] != '.')
149 continue;
150 if (strcasecmp(name + len - plen, p))
151 continue;
152 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
153 return i;
154 }
155 }
156 return -E_AUDIO_FORMAT;
157 }
158
159 /**
160 * Call get_file_info() to obtain an afhi structure.
161 *
162 * \param path The full path of the audio file.
163 * \param data Pointer to the contents of the (mapped) file.
164 * \param size The file size in bytes.
165 * \param fd The open file descriptor.
166 * \param afhi Result pointer.
167 *
168 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
169 * compiled in audio format handler is able to handler the file.
170 *
171 * This function tries to find an audio format handler that can interpret the
172 * file given by \a data and \a size.
173 *
174 * It first tries to determine the audio format from the filename given by \a
175 * path. If this doesn't work, all other audio format handlers are tried until
176 * one is found that can handle the file.
177 */
178 int compute_afhi(const char *path, char *data, size_t size, int fd,
179 struct afh_info *afhi)
180 {
181 int ret, i, format;
182
183 afhi->header_len = 0;
184 afhi->techinfo = NULL;
185 afhi->tags.artist = NULL;
186 afhi->tags.title = NULL;
187 afhi->tags.year = NULL;
188 afhi->tags.album = NULL;
189 afhi->tags.comment = NULL;
190 format = guess_audio_format(path);
191
192 if (format >= 0) {
193 ret = afl[format].get_file_info(data, size, fd, afhi);
194 if (ret >= 0) {
195 ret = format;
196 goto success;
197 }
198 }
199 FOR_EACH_AUDIO_FORMAT(i) {
200 if (i == format) /* we already tried this one to no avail */
201 continue;
202 ret = afl[i].get_file_info(data, size, fd, afhi);
203 if (ret >= 0) {
204 ret = i;
205 goto success;
206 }
207 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
208 }
209 return -E_AUDIO_FORMAT;
210 success:
211 if (!afhi->techinfo)
212 afhi->techinfo = para_strdup(NULL);
213 if (!afhi->tags.artist)
214 afhi->tags.artist = para_strdup(NULL);
215 if (!afhi->tags.title)
216 afhi->tags.title = para_strdup(NULL);
217 if (!afhi->tags.year)
218 afhi->tags.year = para_strdup(NULL);
219 if (!afhi->tags.album)
220 afhi->tags.album = para_strdup(NULL);
221 if (!afhi->tags.comment)
222 afhi->tags.comment = para_strdup(NULL);
223 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
224 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
225 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
226 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
227 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
228 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
229 return ret;
230 }
231
232 /**
233 * Deallocate the contents of an afh_info structure.
234 *
235 * \param afhi The structure to clear.
236 *
237 * This only frees the memory the various pointer fields of \a afhi point to.
238 * It does *not* free \a afhi itself.
239 */
240 void clear_afhi(struct afh_info *afhi)
241 {
242 if (!afhi)
243 return;
244 free(afhi->chunk_table);
245 free(afhi->techinfo);
246 free(afhi->tags.artist);
247 free(afhi->tags.title);
248 free(afhi->tags.year);
249 free(afhi->tags.album);
250 free(afhi->tags.comment);
251 }
252
253 /**
254 * Get the name of the given audio format.
255 *
256 * \param i The audio format number.
257 *
258 * \return This returns a pointer to statically allocated memory so it
259 * must not be freed by the caller.
260 */
261 const char *audio_format_name(int i)
262 {
263 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
264 return "???";
265 return afl[i].name;
266 }
267
268 static inline size_t get_chunk_len(long unsigned chunk_num,
269 const struct afh_info *afhi)
270 {
271 return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num];
272 }
273
274 /**
275 * Get one chunk of audio data.
276 *
277 * \param chunk_num The number of the chunk to get.
278 * \param afhi Describes the audio file.
279 * \param map The memory mapped audio file.
280 * \param buf Result pointer.
281 * \param len The length of the chunk in bytes.
282 *
283 * Upon return, \a buf will point so memory inside \a map. The returned buffer
284 * must therefore not be freed by the caller.
285 */
286 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
287 void *map, const char **buf, size_t *len)
288 {
289 size_t pos = afhi->chunk_table[chunk_num];
290 *buf = map + pos;
291 *len = get_chunk_len(chunk_num, afhi);
292 }
293
294 /**
295 * Find a suitable start chunk.
296 *
297 * \param approx_chunk_num Upper bound for the chunk number to return.
298 * \param afhi Needed for the chunk table.
299 *
300 * \return The first non-empty chunk <= \a approx_chunk_num.
301 *
302 * \sa \ref afh_get_chunk().
303 */
304 int32_t afh_get_start_chunk(int32_t approx_chunk_num,
305 const struct afh_info *afhi)
306 {
307 int32_t k;
308
309 for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--)
310 if (get_chunk_len(k, afhi) > 0)
311 break;
312 return k;
313 }
314
315 /**
316 * Get the header of an audio file.
317 *
318 * \param afhi The audio file handler data describing the file.
319 * \param audio_format_id Determines the audio format handler.
320 * \param map The data of the audio file.
321 * \param mapsize The amount of bytes of the mmapped audio file.
322 * \param buf The length of the header is stored here.
323 * \param len Points to a buffer containing the header on return.
324 *
325 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
326 * afhi is \p NULL, or if the current audio format does not need special
327 * header treatment.
328 *
329 * Otherwise, it is checked whether the audio format handler given by
330 * \a audio_format_id defines a ->get_header() method. If it does, this
331 * method is called to obtain the header. If ->get_header() is \p NULL,
332 * a reference to the first chunk of the audio file is returned.
333 *
334 * Once the header is no longer needed, the caller must call \ref
335 * afh_free_header() to free the resources allocated by this function.
336 */
337 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
338 void *map, size_t mapsize, char **buf, size_t *len)
339 {
340 struct audio_format_handler *afh = afl + audio_format_id;
341
342 if (!map || !afhi || !afhi->header_len) {
343 *buf = NULL;
344 *len = 0;
345 return;
346 }
347 if (!afh->get_header) {
348 *len = afhi->header_len;
349 *buf = map;
350 return;
351 }
352 afh->get_header(map, mapsize, buf, len);
353 }
354
355 /**
356 * Deallocate any resources obtained from afh_get_header().
357 *
358 * \param header_buf Pointer obtained via afh_get_header().
359 * \param audio_format_id Determines the audio format handler.
360 */
361 void afh_free_header(char *header_buf, uint8_t audio_format_id)
362 {
363 struct audio_format_handler *afh = afl + audio_format_id;
364
365 if (afh->get_header)
366 free(header_buf);
367 }
368
369 /**
370 * Pretty-print the contents of a struct afh_info into a buffer.
371 *
372 * \param audio_format_num The audio format number.
373 * \param afhi Pointer to the structure that contains the information.
374 * \param result Pretty-printed ahfi is here after the call.
375 *
376 * The \a result buffer is dynamically allocated and should be freed by the
377 * caller.
378 *
379 * \return The number of bytes. This function never fails.
380 */
381 unsigned afh_get_afhi_txt(int audio_format_num, struct afh_info *afhi, char **result)
382 {
383 return xasprintf(result, "%s: %dkbit/s\n" /* bitrate */
384 "%s: %s\n" /* format */
385 "%s: %dHz\n" /* frequency */
386 "%s: %d\n" /* channels */
387 "%s: %lu\n" /* seconds total */
388 "%s: %lu: %lu\n" /* chunk time */
389 "%s: %lu\n" /* num chunks */
390 "%s: %s\n" /* techinfo */
391 "%s: %s\n" /* artist */
392 "%s: %s\n" /* title */
393 "%s: %s\n" /* year */
394 "%s: %s\n" /* album */
395 "%s: %s\n", /* comment */
396 status_item_list[SI_BITRATE], afhi->bitrate,
397 status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
398 status_item_list[SI_FREQUENCY], afhi->frequency,
399 status_item_list[SI_CHANNELS], afhi->channels,
400 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
401 status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
402 (long unsigned)afhi->chunk_tv.tv_usec,
403 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
404 status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
405 status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
406 status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
407 status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
408 status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
409 status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
410 );
411 }