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