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 Renato Chencarek <renato.chencarek@openbossa.org>
31 #include <lightmediascanner_plugin.h>
32 #include <lightmediascanner_db.h>
35 #include <vorbis/codec.h>
36 #include <vorbis/vorbisfile.h>
39 _id3_tag_size(FILE *file)
44 if (fread(tmp, 1, 4, file) == 4) {
45 if (tmp[0] == 'I' && tmp[1] == 'D' &&
46 tmp[2] == '3' && tmp[3] < 0xFF) {
47 fseek(file, 2, SEEK_CUR);
48 if (fread(tmp, 1, 4, file) == 4) {
49 size = 10 + ( (long)(tmp[3])
50 | ((long)(tmp[2]) << 7)
51 | ((long)(tmp[1]) << 14)
52 | ((long)(tmp[0]) << 21) );
63 _get_vorbis_comment(FILE *file, vorbis_comment *vc)
66 int bytes, i, chunks = 0;
78 ogg_sync_init(&osync);
81 buffer = ogg_sync_buffer(&osync, CHUNKSIZE);
82 bytes = fread(buffer, 1, CHUNKSIZE, file);
84 ogg_sync_wrote(&osync, bytes);
86 if (ogg_sync_pageout(&osync, &og) == 1)
93 serial = ogg_page_serialno(&og);
95 ogg_stream_init(&os, serial);
96 vorbis_info_init(&vi);
97 vorbis_comment_init(vc);
99 if (ogg_stream_pagein(&os, &og) < 0)
101 if (ogg_stream_packetout(&os, &header) != 1)
103 if (vorbis_synthesis_headerin(&vi, vc, &header) != 0)
108 while (i < nheaders) {
109 while (i < nheaders) {
110 int result = ogg_sync_pageout(&osync, &og);
113 else if (result == 1) {
114 ogg_stream_pagein(&os, &og);
115 while (i < nheaders) {
116 result = ogg_stream_packetout(&os, &header);
122 vorbis_synthesis_headerin(&vi, vc, &header);
128 buffer = ogg_sync_buffer(&osync, CHUNKSIZE);
129 bytes = fread(buffer, 1, CHUNKSIZE, file);
131 if (bytes == 0 && i < 2)
134 ogg_sync_wrote(&osync, bytes);
137 ogg_stream_clear(&os);
138 ogg_sync_clear(&osync);
139 vorbis_info_clear(&vi);
145 _parse_ogg(const char *filename, struct lms_audio_info *info)
155 file = fopen(filename, "rb");
159 fseek(file, _id3_tag_size(file), SEEK_SET);
161 if (_get_vorbis_comment(file, &vc) != 0)
164 tag = vorbis_comment_query(&vc, "TITLE", 0);
166 if (tag && size > 0) {
167 info->title.len = size;
168 info->title.str = malloc(size * sizeof(char));
169 memcpy(info->title.str, tag, size);
170 lms_string_size_strip_and_free(&info->title);
173 tag = vorbis_comment_query(&vc, "ARTIST", 0);
175 if (tag && size > 0) {
176 info->artist.len = size;
177 info->artist.str = malloc(size * sizeof(char));
178 memcpy(info->artist.str, tag, size);
179 lms_string_size_strip_and_free(&info->artist);
182 tag = vorbis_comment_query(&vc, "ALBUM", 0);
184 if (tag && size > 0) {
185 info->album.len = size;
186 info->album.str = malloc(size * sizeof(char));
187 memcpy(info->album.str, tag, size);
188 lms_string_size_strip_and_free(&info->album);
191 tag = vorbis_comment_query(&vc, "TRACKNUMBER", 0);
193 info->trackno = atoi(tag);
195 tag = vorbis_comment_query(&vc, "GENRE", 0);
197 if (tag && size > 0) {
198 info->genre.len = size;
199 info->genre.str = malloc(size * sizeof(char));
200 memcpy(info->genre.str, tag, size);
201 lms_string_size_strip_and_free(&info->genre);
205 vorbis_comment_clear(&vc);
211 static const char _name[] = "ogg";
212 static const struct lms_string_size _exts[] = {
213 LMS_STATIC_STRING_SIZE(".ogg")
217 struct lms_plugin plugin;
218 lms_db_audio_t *audio_db;
222 _match(struct plugin *p, const char *path, int len, int base)
226 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
230 return (void*)(i + 1);
234 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
236 struct lms_audio_info info = {0};
239 r = _parse_ogg(finfo->path, &info);
243 if (!info.title.str) {
246 ext_idx = ((int)match) - 1;
247 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
248 info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
249 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
250 info.title.str[info.title.len] = '\0';
254 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
256 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
258 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
260 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
263 r = lms_db_audio_add(plugin->audio_db, &info);
267 free(info.title.str);
269 free(info.artist.str);
271 free(info.album.str);
273 free(info.genre.str);
279 _setup(struct plugin *plugin, struct lms_context *ctxt)
281 plugin->audio_db = lms_db_audio_new(ctxt->db);
282 if (!plugin->audio_db)
289 _start(struct plugin *plugin, struct lms_context *ctxt)
291 return lms_db_audio_start(plugin->audio_db);
295 _finish(struct plugin *plugin, struct lms_context *ctxt)
297 if (plugin->audio_db)
298 return lms_db_audio_free(plugin->audio_db);
304 _close(struct plugin *plugin)
310 API struct lms_plugin *
311 lms_plugin_open(void)
313 struct plugin *plugin;
315 plugin = (struct plugin *)malloc(sizeof(*plugin));
316 plugin->plugin.name = _name;
317 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
318 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
319 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
320 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
321 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
322 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
324 return (struct lms_plugin *)plugin;