2 * Copyright (C) 2007 by INdT
4 * This program 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
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
24 * Reads ID3 tags from MP3 using id3lib.
26 * @todo: write a faster module to replace this one, using no external libs.
33 #define _XOPEN_SOURCE 600
34 #include <lightmediascanner_plugin.h>
35 #include <lightmediascanner_utils.h>
36 #include <lightmediascanner_db.h>
45 _id3lib_get_string(const ID3_Frame *frame, ID3_FieldID field_id, struct lms_string_size *s)
51 field = frame->GetField(field_id);
55 enc = field->GetEncoding();
56 field->SetEncoding(ID3TE_ISO8859_1);
62 s->str = (char *)malloc(size * sizeof(char));
63 s->len = field->Get(s->str, size);
65 lms_strstrip(s->str, &s->len);
74 field->SetEncoding(enc);
78 _id3lib_get_string_as_int(const ID3_Frame *frame, ID3_FieldID field_id)
84 field = frame->GetField(field_id);
88 size = field->Get(buf, sizeof(buf));
96 _id3lib_get_string_if_need(const ID3_Frame *frame, struct lms_string_size *s)
99 _id3lib_get_string(frame, ID3FN_TEXT, s);
105 _id3lib_get_data(const ID3_Tag &tag, struct lms_audio_info *info)
107 lms_string_size band = {NULL, 0}, lead_artist = {NULL, 0};
108 ID3_Tag::ConstIterator *itr;
109 const ID3_Frame *frame;
112 todo = 8; /* info fields left to parse: title, lead artist, band,
113 album, genre, trackno, rating, playcnt */
115 itr = tag.CreateIterator();
117 while ((frame = itr->GetNext()) != NULL && todo > 0) {
120 fid = frame->GetID();
124 if (_id3lib_get_string_if_need(frame, &info->title))
128 case ID3FID_LEADARTIST:
129 if (_id3lib_get_string_if_need(frame, &lead_artist))
134 if (_id3lib_get_string_if_need(frame, &band))
139 if (_id3lib_get_string_if_need(frame, &info->album))
143 case ID3FID_CONTENTTYPE:
144 if (_id3lib_get_string_if_need(frame, &info->genre))
148 case ID3FID_TRACKNUM:
149 if (!info->trackno) {
150 info->trackno = _id3lib_get_string_as_int(frame, ID3FN_TEXT);
156 case ID3FID_POPULARIMETER:
158 info->rating = frame->GetField(ID3FN_RATING)->Get();
162 if (!info->playcnt) {
163 info->playcnt = frame->GetField(ID3FN_COUNTER)->Get();
174 if (band.str && lead_artist.str) {
176 free(lead_artist.str);
179 else if (lead_artist.str)
180 info->artist = lead_artist;
186 static const char _name[] = "id3lib";
187 static const struct lms_string_size _exts[] = {
188 LMS_STATIC_STRING_SIZE(".mp3")
192 struct lms_plugin plugin;
193 lms_db_audio_t *audio_db;
197 _match(struct plugin *p, const char *path, int len, int base)
201 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
205 return (void*)(i + 1);
209 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
211 struct lms_audio_info info = {0};
215 tag.Link(finfo->path);
216 r = _id3lib_get_data(tag, &info);
220 if (!info.title.str) {
223 ext_idx = ((int)match) - 1;
224 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
225 info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
226 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
227 info.title.str[info.title.len] = '\0';
231 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
233 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
235 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
237 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
240 r = lms_db_audio_add(plugin->audio_db, &info);
244 free(info.title.str);
246 free(info.artist.str);
248 free(info.album.str);
250 free(info.genre.str);
256 _setup(struct plugin *plugin, struct lms_context *ctxt)
258 plugin->audio_db = lms_db_audio_new(ctxt->db);
259 if (!plugin->audio_db)
266 _start(struct plugin *plugin, struct lms_context *ctxt)
268 return lms_db_audio_start(plugin->audio_db);
272 _finish(struct plugin *plugin, struct lms_context *ctxt)
274 if (plugin->audio_db)
275 return lms_db_audio_free(plugin->audio_db);
281 _close(struct plugin *plugin)
287 API struct lms_plugin *
288 lms_plugin_open(void)
290 struct plugin *plugin;
292 plugin = (struct plugin *)malloc(sizeof(*plugin));
293 plugin->plugin.name = _name;
294 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
295 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
296 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
297 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
298 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
299 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
301 return (struct lms_plugin *)plugin;