e7680e72b80f32aa8d378968f8e56826dc1b80c6
[lms] / lightmediascanner / src / plugins / id3lib / id3lib.cpp
1 /**
2  * Copyright (C) 2007 by INdT
3  *
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.
8  *
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.
13  *
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.
17  *
18  * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * Reads ID3 tags from MP3 using id3lib.
25  *
26  * @todo: write a faster module to replace this one, using no external libs.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #define _XOPEN_SOURCE 600
34 #include <lightmediascanner_plugin.h>
35 #include <lightmediascanner_utils.h>
36 #include <lightmediascanner_db.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <id3/tag.h>
41
42 extern "C" {
43
44 static void
45 _id3lib_get_string(const ID3_Frame *frame, ID3_FieldID field_id, struct lms_string_size *s)
46 {
47   ID3_Field* field;
48   ID3_TextEnc enc;
49   size_t size;
50
51   field = frame->GetField(field_id);
52   if (!field)
53     return;
54
55   enc = field->GetEncoding();
56   field->SetEncoding(ID3TE_ISO8859_1);
57   size = field->Size();
58   if (size < 1)
59     goto done;
60
61   size++;
62   s->str = (char *)malloc(size * sizeof(char));
63   s->len = field->Get(s->str, size);
64   lms_string_size_strip_and_free(s);
65
66  done:
67   field->SetEncoding(enc);
68 }
69
70 static unsigned int
71 _id3lib_get_string_as_int(const ID3_Frame *frame, ID3_FieldID field_id)
72 {
73   char buf[32];
74   ID3_Field* field;
75   size_t size;
76
77   field = frame->GetField(field_id);
78   if (!field)
79     return 0;
80
81   size = field->Get(buf, sizeof(buf));
82   if (size > 0)
83     return atoi(buf);
84
85   return 0;
86 }
87
88 inline static int
89 _id3lib_get_string_if_need(const ID3_Frame *frame, struct lms_string_size *s)
90 {
91   if (!s->str)
92     _id3lib_get_string(frame, ID3FN_TEXT, s);
93
94   return !!s->str;
95 }
96
97 static int
98 _id3lib_get_data(const ID3_Tag &tag, struct lms_audio_info *info)
99 {
100   lms_string_size band = {NULL, 0}, lead_artist = {NULL, 0};
101   ID3_Tag::ConstIterator *itr;
102   const ID3_Frame *frame;
103   int todo;
104
105   todo = 8; /* info fields left to parse: title, lead artist, band,
106                album, genre, trackno, rating, playcnt */
107
108   itr = tag.CreateIterator();
109
110   while ((frame = itr->GetNext()) != NULL && todo > 0) {
111     ID3_FrameID fid;
112
113     fid = frame->GetID();
114
115     switch (fid) {
116     case ID3FID_TITLE:
117       if (_id3lib_get_string_if_need(frame, &info->title))
118           todo--;
119       break;
120
121     case ID3FID_LEADARTIST:
122       if (_id3lib_get_string_if_need(frame, &lead_artist))
123           todo--;
124       break;
125
126     case ID3FID_BAND:
127       if (_id3lib_get_string_if_need(frame, &band))
128           todo--;
129       break;
130
131     case ID3FID_ALBUM:
132       if (_id3lib_get_string_if_need(frame, &info->album))
133           todo--;
134       break;
135
136     case ID3FID_CONTENTTYPE:
137       if (_id3lib_get_string_if_need(frame, &info->genre))
138           todo--;
139       break;
140
141     case ID3FID_TRACKNUM:
142       if (!info->trackno) {
143         info->trackno = _id3lib_get_string_as_int(frame, ID3FN_TEXT);
144         if (info->trackno)
145           todo--;
146       }
147       break;
148
149     case ID3FID_POPULARIMETER:
150       if (!info->rating) {
151         info->rating = frame->GetField(ID3FN_RATING)->Get();
152         if (info->rating)
153           todo--;
154       }
155       if (!info->playcnt) {
156         info->playcnt = frame->GetField(ID3FN_COUNTER)->Get();
157         if (info->playcnt)
158           todo--;
159       }
160       break;
161
162     default:
163       break;
164     }
165   }
166
167   if (band.str && lead_artist.str) {
168     info->artist = band;
169     free(lead_artist.str);
170   } else if (band.str)
171     info->artist = band;
172   else if (lead_artist.str)
173     info->artist = lead_artist;
174
175   delete itr;
176   return 0;
177 }
178
179 static const char _name[] = "id3lib";
180 static const struct lms_string_size _exts[] = {
181     LMS_STATIC_STRING_SIZE(".mp3")
182 };
183
184 struct plugin {
185     struct lms_plugin plugin;
186     lms_db_audio_t *audio_db;
187 };
188
189 static void *
190 _match(struct plugin *p, const char *path, int len, int base)
191 {
192     int i;
193
194     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
195     if (i < 0)
196       return NULL;
197     else
198       return (void*)(i + 1);
199 }
200
201 static int
202 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
203 {
204     struct lms_audio_info info = {0};
205     ID3_Tag tag;
206     int r;
207
208     tag.Link(finfo->path);
209     r = _id3lib_get_data(tag, &info);
210     if (r != 0)
211       goto done;
212
213     if (!info.title.str) {
214       int ext_idx;
215
216       ext_idx = ((int)match) - 1;
217       info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
218       info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
219       memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
220       info.title.str[info.title.len] = '\0';
221     }
222
223     if (info.title.str)
224       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
225     if (info.artist.str)
226       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
227     if (info.album.str)
228       lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
229     if (info.genre.str)
230       lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
231
232     info.id = finfo->id;
233     r = lms_db_audio_add(plugin->audio_db, &info);
234
235  done:
236     if (info.title.str)
237         free(info.title.str);
238     if (info.artist.str)
239         free(info.artist.str);
240     if (info.album.str)
241         free(info.album.str);
242     if (info.genre.str)
243         free(info.genre.str);
244
245     return r;
246 }
247
248 static int
249 _setup(struct plugin *plugin, struct lms_context *ctxt)
250 {
251     plugin->audio_db = lms_db_audio_new(ctxt->db);
252     if (!plugin->audio_db)
253         return -1;
254
255     return 0;
256 }
257
258 static int
259 _start(struct plugin *plugin, struct lms_context *ctxt)
260 {
261     return lms_db_audio_start(plugin->audio_db);
262 }
263
264 static int
265 _finish(struct plugin *plugin,  struct lms_context *ctxt)
266 {
267     if (plugin->audio_db)
268         return lms_db_audio_free(plugin->audio_db);
269
270     return 0;
271 }
272
273 static int
274 _close(struct plugin *plugin)
275 {
276     free(plugin);
277     return 0;
278 }
279
280 API struct lms_plugin *
281 lms_plugin_open(void)
282 {
283     struct plugin *plugin;
284
285     plugin = (struct plugin *)malloc(sizeof(*plugin));
286     plugin->plugin.name = _name;
287     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
288     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
289     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
290     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
291     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
292     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
293
294     return (struct lms_plugin *)plugin;
295 }
296
297 }