2 * Copyright (C) 2013 Intel Corporation. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * @author Lucas De Marchi <lucas.demarchi@intel.com>
28 * Format specification:
29 * http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/WAVE.html
30 * http://www.sonicspot.com/guide/wavefiles.html
32 * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/RIFF.html#Info
33 * http://www.aelius.com/njh/wavemetatools/bsiwave_tag_map.html
36 #include <lightmediascanner_plugin.h>
37 #include <lightmediascanner_db.h>
38 #include <lightmediascanner_charset_conv.h>
39 #include <shared/util.h>
41 #include <sys/types.h>
50 #define DECL_STR(cname, str) \
51 static const struct lms_string_size cname = LMS_STATIC_STRING_SIZE(str)
53 DECL_STR(_dlna_lpcm, "LPCM");
55 DECL_STR(_dlna_mime_44_mono, "audio/L16;rate=44100;channels=1");
56 DECL_STR(_dlna_mime_44_stereo, "audio/L16;rate=44100;channels=2");
57 DECL_STR(_dlna_mime_48_mono, "audio/L16;rate=48000;channels=1");
58 DECL_STR(_dlna_mime_48_stereo, "audio/L16;rate=48000;channels=2");
62 _fill_dlna_profile(struct lms_audio_info *info)
64 if (info->channels == 1 && info->sampling_rate == 44100) {
65 info->dlna_profile = _dlna_lpcm;
66 info->dlna_mime = _dlna_mime_44_mono;
67 } else if (info->channels == 2 && info->sampling_rate == 44100) {
68 info->dlna_profile = _dlna_lpcm;
69 info->dlna_mime = _dlna_mime_44_stereo;
70 } else if (info->channels == 1 && info->sampling_rate == 48000) {
71 info->dlna_profile = _dlna_lpcm;
72 info->dlna_mime = _dlna_mime_48_mono;
73 } else if (info->channels == 2 && info->sampling_rate == 48000) {
74 info->dlna_profile = _dlna_lpcm;
75 info->dlna_mime = _dlna_mime_48_stereo;
79 static const char _name[] = "wave";
80 static const struct lms_string_size _exts[] = {
81 LMS_STATIC_STRING_SIZE(".wav"),
82 LMS_STATIC_STRING_SIZE(".wave"),
84 static const char *_cats[] = {
88 static const char *_authors[] = {
92 static const struct lms_string_size _container = LMS_STATIC_STRING_SIZE("wave");
95 struct lms_plugin plugin;
96 lms_db_audio_t *audio_db;
100 _match(struct plugin *p, const char *path, int len, int base)
104 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
108 return (void*)(i + 1);
112 _parse_info(int fd, struct lms_audio_info *info)
118 } __attribute__((packed)) hdr;
121 /* Search the LIST INFO chunk */
125 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
128 size = get_le32(&hdr.size) - sizeof(hdr.infoid);
130 if (memcmp(hdr.id, "LIST", 4) == 0
131 && memcmp(hdr.infoid, "INFO", 4) == 0) {
134 } else if ((size & 0x1) && memcmp(hdr.id, "data", 4) == 0)
135 /* 'data' chunk has a 1-byte padding if size is odd */
138 lseek(fd, size, SEEK_CUR);
141 while (maxsize > 8) {
144 struct lms_string_size *str;
146 if (read(fd, chunkid, sizeof(chunkid)) != sizeof(chunkid)
147 || read(fd, &size, sizeof(size)) != sizeof(size))
150 size = le32toh(size);
152 if (memcmp(chunkid, "INAM", 4) == 0)
154 else if (memcmp(chunkid, "IART", 4) == 0)
156 else if (memcmp(chunkid, "IPRD", 4) == 0)
158 else if (memcmp(chunkid, "IGNR", 4) == 0)
161 lseek(fd, size, SEEK_CUR);
166 str->str = malloc(size + 1);
167 read(fd, str->str, size);
168 str->str[size] = '\0';
172 /* Ignore trailing '\0', even if they are not part of the previous
174 while (maxsize > 0 && read(fd, chunkid, 1) == 1) {
175 if (chunkid[0] != '\0') {
176 lseek(fd, -1, SEEK_CUR);
188 _parse_fmt(int fd, struct lms_audio_info *info)
193 uint16_t audio_format;
195 uint32_t sample_rate;
197 uint16_t block_align;
199 } __attribute__((packed)) fmt;
202 if (read(fd, &fmt, sizeof(fmt)) != sizeof(fmt)
203 || memcmp(fmt.fmt, "fmt ", 4) != 0)
206 info->channels = get_le16(&fmt.n_channels);
207 info->bitrate = get_le32(&fmt.byte_rate) * 8;
208 info->sampling_rate = get_le32(&fmt.sample_rate);
210 remain = get_le32(&fmt.size) - sizeof(fmt) +
211 offsetof(struct fmt, audio_format);
213 lseek(fd, remain, SEEK_CUR);
219 _parse_wave(int fd, struct lms_audio_info *info)
225 } __attribute__((packed)) hdr;
227 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)
228 || memcmp(hdr.riff, "RIFF", 4) != 0
229 || memcmp(hdr.wave, "WAVE", 4) != 0
230 || _parse_fmt(fd, info) < 0)
233 info->container = _container;
239 _parse(struct plugin *plugin, struct lms_context *ctxt,
240 const struct lms_file_info *finfo, void *match)
242 struct lms_audio_info info = { };
245 fd = open(finfo->path, O_RDONLY);
249 r = _parse_wave(fd, &info);
253 /* Ignore errors, waves likely don't have any additional information */
254 _parse_info(fd, &info);
257 info.title = str_extract_name_from_path(finfo->path, finfo->path_len,
259 &_exts[((long) match) - 1],
264 _fill_dlna_profile(&info);
266 r = lms_db_audio_add(plugin->audio_db, &info);
269 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
272 free(info.title.str);
273 free(info.artist.str);
274 free(info.album.str);
275 free(info.genre.str);
281 _setup(struct plugin *plugin, struct lms_context *ctxt)
283 plugin->audio_db = lms_db_audio_new(ctxt->db);
284 if (!plugin->audio_db)
291 _start(struct plugin *plugin, struct lms_context *ctxt)
294 return lms_db_audio_start(plugin->audio_db);
298 _finish(struct plugin *plugin, struct lms_context *ctxt)
300 if (plugin->audio_db)
301 lms_db_audio_free(plugin->audio_db);
307 _close(struct plugin *plugin)
313 API struct lms_plugin *
314 lms_plugin_open(void)
316 struct lms_plugin *plugin;
318 plugin = (struct lms_plugin *) malloc(sizeof(struct plugin));
319 plugin->name = _name;
320 plugin->match = (lms_plugin_match_fn_t) _match;
321 plugin->parse = (lms_plugin_parse_fn_t) _parse;
322 plugin->close = (lms_plugin_close_fn_t) _close;
323 plugin->setup = (lms_plugin_setup_fn_t) _setup;
324 plugin->start = (lms_plugin_start_fn_t) _start;
325 plugin->finish = (lms_plugin_finish_fn_t) _finish;
330 API const struct lms_plugin_info *
331 lms_plugin_info(void)
333 static struct lms_plugin_info info = {