2 * Copyright (C) 1997-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afh_common.c Common audio format handler functions. */
9 #include <sys/mman.h> /* mmap */
10 #include <sys/time.h> /* gettimeofday */
11 #include <sys/types.h>
19 /* The mp3 audio format handler does not need any libs. */
20 void mp3_init(struct audio_format_handler *);
23 void ogg_init(struct audio_format_handler *);
26 void aac_afh_init(struct audio_format_handler *);
29 void spx_afh_init(struct audio_format_handler *);
32 void flac_afh_init(struct audio_format_handler *);
35 void wma_afh_init(struct audio_format_handler *);
37 /** The list of all status items */
38 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
41 * The list of supported audio formats.
43 * We always define the full array of audio formats even if some audio formats
44 * were not compiled in. This is because for each audio file the number of its
45 * audio format is stored in the database. We don't want that numbers to become
46 * stale just because the user installed a new version of paraslash that
47 * supports a different set of audio formats.
49 * It can still be easily detected whether an audio format is compiled in by
50 * checking if the init function pointer is not \p NULL.
52 static struct audio_format_handler afl[] = {
82 .init = flac_afh_init,
90 static inline int next_audio_format(int format)
93 if (!afl[format].name)
102 /** Iterate over each supported audio format. */
103 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
106 * Call the init function of each supported audio format handler.
112 PARA_INFO_LOG("supported audio formats: %s\n",
113 SERVER_AUDIO_FORMATS);
114 FOR_EACH_AUDIO_FORMAT(i) {
115 PARA_NOTICE_LOG("initializing %s handler\n",
116 audio_format_name(i));
117 afl[i].init(&afl[i]);
122 * Guess the audio format judging from filename.
124 * \param name The filename.
126 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
127 * of audio file this might be. Otherwise the (non-negative) number of the
128 * audio format is returned.
130 int guess_audio_format(const char *name)
132 int i,j, len = strlen(name);
134 FOR_EACH_AUDIO_FORMAT(i) {
135 for (j = 0; afl[i].suffixes[j]; j++) {
136 const char *p = afl[i].suffixes[j];
137 int plen = strlen(p);
140 if (name[len - plen - 1] != '.')
142 if (strcasecmp(name + len - plen, p))
144 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
148 return -E_AUDIO_FORMAT;
152 * Call get_file_info() to obtain an afhi structure.
154 * \param path The full path of the audio file.
155 * \param data Pointer to the contents of the (mapped) file.
156 * \param size The file size in bytes.
157 * \param fd The open file descriptor.
158 * \param afhi Result pointer.
160 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
161 * compiled in audio format handler is able to handler the file.
163 * This function tries to find an audio format handler that can interpret the
164 * file given by \a data and \a size.
166 * It first tries to determine the audio format from the filename given by \a
167 * path. If this doesn't work, all other audio format handlers are tried until
168 * one is found that can handle the file.
170 int compute_afhi(const char *path, char *data, size_t size, int fd,
171 struct afh_info *afhi)
175 afhi->header_len = 0;
176 afhi->techinfo = NULL;
177 afhi->tags.artist = NULL;
178 afhi->tags.title = NULL;
179 afhi->tags.year = NULL;
180 afhi->tags.album = NULL;
181 afhi->tags.comment = NULL;
182 format = guess_audio_format(path);
185 ret = afl[format].get_file_info(data, size, fd, afhi);
191 FOR_EACH_AUDIO_FORMAT(i) {
192 if (i == format) /* we already tried this one to no avail */
194 ret = afl[i].get_file_info(data, size, fd, afhi);
199 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
201 return -E_AUDIO_FORMAT;
204 afhi->techinfo = para_strdup(NULL);
205 if (!afhi->tags.artist)
206 afhi->tags.artist = para_strdup(NULL);
207 if (!afhi->tags.title)
208 afhi->tags.title = para_strdup(NULL);
209 if (!afhi->tags.year)
210 afhi->tags.year = para_strdup(NULL);
211 if (!afhi->tags.album)
212 afhi->tags.album = para_strdup(NULL);
213 if (!afhi->tags.comment)
214 afhi->tags.comment = para_strdup(NULL);
215 PARA_DEBUG_LOG("techinfo: %s\n", afhi->techinfo);
216 PARA_DEBUG_LOG("artist: %s\n", afhi->tags.artist);
217 PARA_DEBUG_LOG("title: %s\n", afhi->tags.title);
218 PARA_DEBUG_LOG("year: %s\n", afhi->tags.year);
219 PARA_DEBUG_LOG("album: %s\n", afhi->tags.album);
220 PARA_DEBUG_LOG("comment: %s\n", afhi->tags.comment);
225 * Deallocate contents of a filled-in ahi structure
227 * \param afhi The structure to clear.
229 * The given pointer is kept, everything else is freed.
231 void clear_afhi(struct afh_info *afhi)
235 free(afhi->chunk_table);
236 free(afhi->techinfo);
237 free(afhi->tags.artist);
238 free(afhi->tags.title);
239 free(afhi->tags.year);
240 free(afhi->tags.album);
241 free(afhi->tags.comment);
245 * Get the name of the given audio format.
247 * \param i The audio format number.
249 * \return This returns a pointer to statically allocated memory so it
250 * must not be freed by the caller.
252 const char *audio_format_name(int i)
254 if (i < 0 || i >= ARRAY_SIZE(afl) - 1)
260 * Get one chunk of audio data.
262 * \param chunk_num The number of the chunk to get.
263 * \param afhi Describes the audio file.
264 * \param map The memory mapped audio file.
265 * \param buf Result pointer.
266 * \param len The length of the chunk in bytes.
268 * Upon return, \a buf will point so memory inside \a map. The returned buffer
269 * must therefore not be freed by the caller.
271 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
272 void *map, const char **buf, size_t *len)
274 size_t pos = afhi->chunk_table[chunk_num];
276 *len = afhi->chunk_table[chunk_num + 1] - pos;
280 * Get the header of an audio file.
282 * \param afhi The audio file handler data describing the file.
283 * \param audio_format_id Determines the audio format handler.
284 * \param map The data of the audio file.
285 * \param mapsize The amount of bytes of the mmapped audio file.
286 * \param buf The length of the header is stored here.
287 * \param len Points to a buffer containing the header on return.
289 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
290 * afhi is \p NULL, or if the current audio format does not need special
293 * Otherwise, it is checked whether the audio format handler given by
294 * \a audio_format_id defines a ->get_header() method. If it does, this
295 * method is called to obtain the header. If ->get_header() is \p NULL,
296 * a reference to the first chunk of the audio file is returned.
298 * Once the header is no longer needed, the caller must call \ref
299 * afh_free_header() to free the resources allocated by this function.
301 void afh_get_header(struct afh_info *afhi, uint8_t audio_format_id,
302 void *map, size_t mapsize, char **buf, size_t *len)
304 struct audio_format_handler *afh = afl + audio_format_id;
306 if (!map || !afhi || !afhi->header_len) {
311 if (!afh->get_header) {
312 *len = afhi->header_len;
316 afh->get_header(map, mapsize, buf, len);
320 * Deallocate any resources obtained from afh_get_header().
322 * \param header_buf Pointer obtained via afh_get_header().
323 * \param audio_format_id Determines the audio format handler.
325 void afh_free_header(char *header_buf, uint8_t audio_format_id)
327 struct audio_format_handler *afh = afl + audio_format_id;