Move para_tmpname() to file_write.c and make it static.
[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
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 * Guess the audio format judging from filename.
96 *
97 * \param name The filename.
98 *
99 * \return This function returns \p -E_AUDIO_FORMAT if it has no idea what kind
100 * of audio file this might be. Otherwise the (non-negative) number of the
101 * audio format is returned.
102 */
103 int guess_audio_format(const char *name)
104 {
105 int i,j, len = strlen(name);
106
107 FOR_EACH_AUDIO_FORMAT(i) {
108 for (j = 0; afl[i].suffixes[j]; j++) {
109 const char *p = afl[i].suffixes[j];
110 int plen = strlen(p);
111 if (len < plen + 1)
112 continue;
113 if (name[len - plen - 1] != '.')
114 continue;
115 if (strcasecmp(name + len - plen, p))
116 continue;
117 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
118 return i;
119 }
120 }
121 return -E_AUDIO_FORMAT;
122 }
123
124 /**
125 * Pretty-print the given meta-info.
126 *
127 * \param title The title of the audio file.
128 * \param artist The artist.
129 * \param album The name of the album.
130 * \param year Year of release.
131 * \param comment Further comments.
132 *
133 * This function is called by each audio format handler to produce the tag info
134 * status items. Usually, the audio format handlers read this info from the
135 * audio file (id3 tags, vorbis comments, ...).
136 *
137 * It is OK to pass \p NULL pointers for any argument in which case a suitable
138 * string is inserted which indicates that this information is not available.
139 *
140 * \return The status item string. It must be freed by the caller.
141 */
142 char *make_taginfo(char *title, char *artist, char *album, char *year,
143 char *comment)
144 {
145 return make_message("%s: %s, by %s\n" /* taginfo1 */
146 "%s: A: %s, Y: %s, C: %s\n", /* taginfo2 */
147 status_item_list[SI_TAGINFO1],
148 (title && *title)? title : "(title tag not set)",
149 (artist && *artist)? artist : "(artist tag not set)",
150 status_item_list[SI_TAGINFO2],
151 (album && *album)? album : "(album tag not set)",
152 (year && *year)? year : "????",
153 (comment && *comment)? comment : "(comment tag not set)"
154 );
155 }
156
157 /**
158 * Call get_file_info() to obtain an afhi structure.
159 *
160 * \param path The full path of the audio file.
161 * \param data Pointer to the contents of the (mapped) file.
162 * \param size The file size in bytes.
163 * \param fd The open file descriptor.
164 * \param afhi Result pointer.
165 *
166 * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
167 * compiled in audio format handler is able to handler the file.
168 *
169 * This function tries to find an audio format handler that can interpret the
170 * file given by \a data and \a size.
171 *
172 * It first tries to determine the audio format from the filename given by \a
173 * path. If this doesn't work, all other audio format handlers are tried until
174 * one is found that can handle the file.
175 */
176 int compute_afhi(const char *path, char *data, size_t size, int fd,
177 struct afh_info *afhi)
178 {
179 int ret, i, format;
180
181 afhi->header_offset = 0;
182 afhi->header_len = 0;
183 format = guess_audio_format(path);
184
185 if (format >= 0) {
186 ret = afl[format].get_file_info(data, size, fd, afhi);
187 if (ret >= 0)
188 return format;
189 }
190 FOR_EACH_AUDIO_FORMAT(i) {
191 if (i == format) /* we already tried this one to no avail */
192 continue;
193 ret = afl[i].get_file_info(data, size, fd, afhi);
194 if (ret >= 0)
195 return i;
196 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
197 }
198 return -E_AUDIO_FORMAT;
199 }
200
201 /**
202 * Get the name of the given audio format.
203 *
204 * \param i The audio format number.
205 *
206 * This returns a pointer to statically allocated memory so it
207 * must not be freed by the caller.
208 */
209 const char *audio_format_name(int i)
210 {
211 //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
212 assert(i < 0 || i < ARRAY_SIZE(afl) - 1);
213 return i >= 0? afl[i].name : "(none)";
214 }
215
216 /**
217 * Get one chunk of audio data.
218 *
219 * \param chunk_num The number of the chunk to get.
220 * \param afhi Describes the audio file.
221 * \param map The memory mapped audio file.
222 * \param buf Result pointer.
223 * \param len The length of the chunk in bytes.
224 *
225 * Upon return, \a buf will point so memory inside \a map. The returned buffer
226 * must therefore not be freed by the caller.
227 */
228 void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi,
229 void *map, const char **buf, size_t *len)
230 {
231 size_t pos = afhi->chunk_table[chunk_num];
232 *buf = map + pos;
233 *len = afhi->chunk_table[chunk_num + 1] - pos;
234 }
235
236 uint32_t afh_get_largest_chunk_size(struct afh_info *afhi)
237 {
238 uint32_t n, largest = 0, *ct = afhi->chunk_table;
239
240 for (n = 1; n <= afhi->chunks_total; n++)
241 largest = PARA_MAX(largest, ct[n] - ct[n - 1]);
242 return largest;
243 }
244
245 /**
246 * Get the header of an audio file.
247 *
248 * \param afhi The audio file handler data describing the file.
249 * \param map The data of the audio file.
250 * \param buf The length of the header is stored here.
251 * \param len Points to a buffer containing the header on return.
252 *
253 * This function sets \a buf to \p NULL and \a len to zero if \a map or \a
254 * afhi is \p NULL, or if the current audio format does not need special
255 * header treamtment.
256 */
257 void afh_get_header(struct afh_info *afhi, void *map, const char **buf, size_t *len)
258 {
259 if (!map || !afhi || !afhi->header_len) {
260 *buf = NULL;
261 *len = 0;
262 return;
263 }
264 *len = afhi->header_len;
265 *buf = map + afhi->header_offset;
266 }