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