Rewrite audiod help text.
[paraslash.git] / afh_common.c
1 /*
2 * Copyright (C) 1997-2008 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
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
29 /**
30 * The list of supported audio formats.
31 *
32 * We always define the full array of audio formats even if some audio formats
33 * were not compiled in. This is because for each audio file the number of its
34 * audio format is stored in the database. We don't want that numbers to become
35 * stale just because the user installed a new version of paraslash that
36 * supports a different set of audio formats.
37 *
38 * It can still be easily detected whether an audio format is compiled in by
39 * checking if the init function pointer is not \p NULL.
40 */
41 static struct audio_format_handler afl[] = {
42 {
43 .name = "mp3",
44 .init = mp3_init,
45 },
46 {
47 .name = "ogg",
48 #ifdef HAVE_OGGVORBIS
49 .init = ogg_init,
50 #endif
51 },
52 {
53 .name = "aac",
54 #ifdef HAVE_FAAD
55 .init = aac_afh_init,
56 #endif
57 },
58 {
59 .name = NULL,
60 }
61 };
62
63 static inline int next_audio_format(int format)
64 {
65 for (;;) {
66 if (!afl[format].name)
67 return format;
68 format++;
69 if (afl[format].init)
70 return format;
71 }
72
73 }
74
75 /** Iterate over each supported audio format. */
76 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
77
78 /**
79 * Call the init function of each supported audio format handler.
80 */
81 void afh_init(void)
82 {
83 int i;
84
85 PARA_INFO_LOG("supported audio formats: %s\n",
86 SUPPORTED_AUDIO_FORMATS);
87 FOR_EACH_AUDIO_FORMAT(i) {
88 PARA_NOTICE_LOG("initializing %s handler\n",
89 audio_format_name(i));
90 afl[i].init(&afl[i]);
91 }
92 }
93
94
95 /**
96 * Guess the audio format judging from filename.
97 *
98 * \param name The filename.
99 *
100 * \return This function returns -1 if it has no idea what kind of audio
101 * file this might be. Otherwise the (non-negative) number of the audio format
102 * 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 -1;
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 afhi Result pointer.
132 *
133 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
134 * compiled in audio format handler is able to handler the file.
135 *
136 * This function tries to find an audio format handler that can interpret the
137 * file given by \a data and \a size.
138 *
139 * It first tries to determine the audio format from the filename given by \a
140 * path. If this doesn't work, all other audio format handlers are tried until
141 * one is found that can handle the file.
142 */
143 int compute_afhi(const char *path, char *data, size_t size,
144 struct afh_info *afhi)
145 {
146 int ret, i, format;
147
148 afhi->header_offset = 0;
149 afhi->header_len = 0;
150 format = guess_audio_format(path);
151
152 if (format >= 0) {
153 ret = afl[format].get_file_info(data, size, afhi);
154 if (ret >= 0)
155 return format;
156 }
157 FOR_EACH_AUDIO_FORMAT(i) {
158 if (i == format) /* we already tried this one to no avail */
159 continue;
160 ret = afl[i].get_file_info(data, size, afhi);
161 if (ret >= 0)
162 return i;
163 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
164 }
165 return -E_AUDIO_FORMAT;
166 }
167
168 /**
169 * Get the name of the given audio format.
170 *
171 * \param i The audio format number.
172 *
173 * This returns a pointer to statically allocated memory so it
174 * must not be freed by the caller.
175 */
176 const char *audio_format_name(int i)
177 {
178 //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
179 assert(i < 0 || i < ARRAY_SIZE(afl) - 1);
180 return i >= 0? afl[i].name : "(none)";
181 }
182
183
184 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
185 void *map, char **buf, size_t *len)
186 {
187 size_t pos = afhi->chunk_table[chunk_num];
188 *buf = map + pos;
189 *len = afhi->chunk_table[chunk_num + 1] - pos;
190 }
191
192 /**
193 * Get the header of an audio file.
194 *
195 * \param afhi The audio file handler data describing the file.
196 * \param map The data of the audio file.
197 * \param buf The length of the header is stored here.
198 * \param len Points to a buffer containing the header on return.
199 *
200 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
201 * afhi is \p NULL, or if the current audio format does not need special
202 * header treamtment.
203 */
204 void afh_get_header(struct afh_info *afhi, void *map, char **buf, size_t *len)
205 {
206 if (!map || !afhi || ! afhi->header_len) {
207 *buf = NULL;
208 *len = 0;
209 }
210 *len = afhi->header_len;
211 *buf = map + afhi->header_offset;
212 }