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