grab_client.c: Fix another documentation bug
[paraslash.git] / afh_common.c
1 /*
2  * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
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/time.h> /* gettimeofday */
11 #include <sys/types.h>
12 #include <regex.h>
13
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "afh.h"
18
19 /* The mp3 audio format handler does not need any libs. */
20 void mp3_init(struct audio_format_handler *);
21
22 #ifdef HAVE_OGGVORBIS
23         void ogg_init(struct audio_format_handler *);
24 #endif
25 #ifdef HAVE_FAAD
26         void aac_afh_init(struct audio_format_handler *);
27 #endif
28 #ifdef HAVE_SPEEX
29         void spx_afh_init(struct audio_format_handler *);
30 #endif
31 #ifdef HAVE_FLAC
32         void flac_afh_init(struct audio_format_handler *);
33 #endif
34
35 void wma_afh_init(struct audio_format_handler *);
36 /**
37  * The list of supported audio formats.
38  *
39  * We always define the full array of audio formats even if some audio formats
40  * were not compiled in. This is because for each audio file the number of its
41  * audio format is stored in the database. We don't want that numbers to become
42  * stale just because the user installed a new version of paraslash that
43  * supports a different set of audio formats.
44  *
45  * It can still be easily detected whether an audio format is compiled in by
46  * checking if the init function pointer is not \p NULL.
47  */
48 static struct audio_format_handler afl[] = {
49         {
50                 .name = "mp3",
51                 .init = mp3_init,
52         },
53         {
54                 .name = "ogg",
55 #ifdef HAVE_OGGVORBIS
56                 .init = ogg_init,
57 #endif
58         },
59         {
60                 .name = "aac",
61 #ifdef HAVE_FAAD
62                 .init = aac_afh_init,
63 #endif
64         },
65         {
66                 .name = "wma",
67                 .init = wma_afh_init,
68         },
69         {
70                 .name = "spx",
71 #ifdef HAVE_SPEEX
72                 .init = spx_afh_init,
73 #endif
74         },
75         {
76                 .name = "flac",
77 #ifdef HAVE_FLAC
78                 .init = flac_afh_init,
79 #endif
80         },
81         {
82                 .name = NULL,
83         }
84 };
85
86 static inline int next_audio_format(int format)
87 {
88         for (;;) {
89                 if (!afl[format].name)
90                         return format;
91                 format++;
92                 if (afl[format].init)
93                         return format;
94         }
95
96 }
97
98 /** Iterate over each supported audio format. */
99 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
100
101 /**
102  * Call the init function of each supported audio format handler.
103  */
104 void afh_init(void)
105 {
106         int i;
107
108         PARA_INFO_LOG("supported audio formats: %s\n",
109                 SERVER_AUDIO_FORMATS);
110         FOR_EACH_AUDIO_FORMAT(i) {
111                 PARA_NOTICE_LOG("initializing %s handler\n",
112                         audio_format_name(i));
113                 afl[i].init(&afl[i]);
114         }
115 }
116
117 /**
118  * Guess the audio format judging from filename.
119  *
120  * \param name The filename.
121  *
122  * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
123  * of audio file this might be. Otherwise the (non-negative) number of the
124  * audio format is returned.
125  */
126 int guess_audio_format(const char *name)
127 {
128         int i,j, len = strlen(name);
129
130         FOR_EACH_AUDIO_FORMAT(i) {
131                 for (j = 0; afl[i].suffixes[j]; j++) {
132                         const char *p = afl[i].suffixes[j];
133                         int plen = strlen(p);
134                         if (len < plen + 1)
135                                 continue;
136                         if (name[len - plen - 1] != '.')
137                                 continue;
138                         if (strcasecmp(name + len - plen, p))
139                                 continue;
140 //                      PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
141                         return i;
142                 }
143         }
144         return -E_AUDIO_FORMAT;
145 }
146
147 /**
148  * Call get_file_info() to obtain an afhi structure.
149  *
150  * \param path The full path of the audio file.
151  * \param data Pointer to the contents of the (mapped) file.
152  * \param size The file size in bytes.
153  * \param fd The open file descriptor.
154  * \param afhi Result pointer.
155  *
156  * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
157  * compiled in audio format handler is able to handler the file.
158  *
159  * This function tries to find an audio format handler that can interpret the
160  * file given by \a data and \a size.
161  *
162  * It first tries to determine the audio format from the filename given by \a
163  * path. If this doesn't work, all other audio format handlers are tried until
164  * one is found that can handle the file.
165  */
166 int compute_afhi(const char *path, char *data, size_t size, int fd,
167                 struct afh_info *afhi)
168 {
169         int ret, i, format;
170
171         afhi->header_len = 0;
172         afhi->techinfo = NULL;
173         afhi->tags.artist = NULL;
174         afhi->tags.title = NULL;
175         afhi->tags.year = NULL;
176         afhi->tags.album = NULL;
177         afhi->tags.comment = NULL;
178         format = guess_audio_format(path);
179
180         if (format >= 0) {
181                 ret = afl[format].get_file_info(data, size, fd, afhi);
182                 if (ret >= 0) {
183                         ret = format;
184                         goto success;
185                 }
186         }
187         FOR_EACH_AUDIO_FORMAT(i) {
188                 if (i == format) /* we already tried this one to no avail */
189                         continue;
190                 ret = afl[i].get_file_info(data, size, fd, afhi);
191                 if (ret >= 0) {
192                         ret = i;
193                         goto success;
194                 }
195                 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
196         }
197         return -E_AUDIO_FORMAT;
198 success:
199         if (!afhi->techinfo)
200                 afhi->techinfo = para_strdup(NULL);
201         if (!afhi->tags.artist)
202                 afhi->tags.artist = para_strdup(NULL);
203         if (!afhi->tags.title)
204                 afhi->tags.title = para_strdup(NULL);
205         if (!afhi->tags.year)
206                 afhi->tags.year = para_strdup(NULL);
207         if (!afhi->tags.album)
208                 afhi->tags.album = para_strdup(NULL);
209         if (!afhi->tags.comment)
210                 afhi->tags.comment = para_strdup(NULL);
211         PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
212         PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
213         PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
214         PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
215         PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
216         PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
217         return ret;
218 }
219
220 /**
221  * Get the name of the given audio format.
222  *
223  * \param i The audio format number.
224  *
225  * \return This returns a pointer to statically allocated memory so it
226  * must not be freed by the caller.
227  */
228 const char *audio_format_name(int i)
229 {
230         if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
231                 return "???";
232         return afl[i].name;
233 }
234
235 /**
236  * Get one chunk of audio data.
237  *
238  * \param chunk_num The number of the chunk to get.
239  * \param afhi Describes the audio file.
240  * \param map The memory mapped audio file.
241  * \param buf Result pointer.
242  * \param len The length of the chunk in bytes.
243  *
244  * Upon return, \a buf will point so memory inside \a map. The returned buffer
245  * must therefore not be freed by the caller.
246  */
247 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
248                 void *map, const char **buf, size_t *len)
249 {
250         size_t pos = afhi->chunk_table[chunk_num];
251         *buf = map + pos;
252         *len = afhi->chunk_table[chunk_num + 1] - pos;
253 }
254
255 /**
256  * Get the header of an audio file.
257  *
258  * \param afhi The audio file handler data describing the file.
259  * \param audio_format_id Determines the audio format handler.
260  * \param map The data of the audio file.
261  * \param mapsize The amount of bytes of the mmapped audio file.
262  * \param buf The length of the header is stored here.
263  * \param len Points to a buffer containing the header on return.
264  *
265  * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
266  * afhi is \p NULL, or if the current audio format does not need special
267  * header treatment.
268  *
269  * Otherwise, it is checked whether the audio format handler given by
270  * \a audio_format_id defines a ->get_header() method. If it does, this
271  * method is called to obtain the header. If ->get_header() is \p NULL,
272  * a reference to the first chunk of the audio file is returned.
273  *
274  * Once the header is no longer needed, the caller must call \ref
275  * afh_free_header() to free the resources allocated by this function.
276  */
277 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
278                 void *map, size_t mapsize, char **buf, size_t *len)
279 {
280         struct audio_format_handler *afh = afl + audio_format_id;
281
282         if (!map || !afhi || !afhi->header_len) {
283                 *buf = NULL;
284                 *len = 0;
285                 return;
286         }
287         if (!afh->get_header) {
288                 *len = afhi->header_len;
289                 *buf = map;
290                 return;
291         }
292         afh->get_header(map, mapsize, buf, len);
293 }
294
295 /**
296  * Deallocate any resources obtained from afh_get_header().
297  *
298  * \param header_buf Pointer obtained via afh_get_header().
299  * \param audio_format_id Determines the audio format handler.
300  */
301 void afh_free_header(char *header_buf, uint8_t audio_format_id)
302 {
303         struct audio_format_handler *afh = afl + audio_format_id;
304
305         if (afh->get_header)
306                 free(header_buf);
307 }