Added flac plugin.
[lms] / lightmediascanner / src / plugins / flac / flac.c
1 /**
2  * Copyright (C) 2008 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 Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * flac file parser.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <lightmediascanner_plugin.h>
32 #include <lightmediascanner_db.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <FLAC/metadata.h>
37
38 struct plugin {
39     struct lms_plugin plugin;
40     lms_db_audio_t *audio_db;
41 };
42
43 static const char _name[] = "flac";
44 static const struct lms_string_size _exts[] = {
45     LMS_STATIC_STRING_SIZE(".flac")
46 };
47
48 static void *
49 _match(struct plugin *p, const char *path, int len, int base)
50 {
51     int i;
52
53     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
54     if (i < 0)
55       return NULL;
56     else
57       return (void*)(i + 1);
58 }
59
60 static int
61 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
62 {
63     struct lms_audio_info info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
64     FLAC__StreamMetadata *tags = 0;
65     char *str;
66     int i, len, r;
67
68     if (!FLAC__metadata_get_tags(finfo->path, &tags)) {
69         fprintf(stderr, "ERROR: cannot retrieve file %s tags\n", finfo->path);
70         return -1;
71     }
72
73     for (i = 0; i < tags->data.vorbis_comment.num_comments; i++) {
74         str = (char *) tags->data.vorbis_comment.comments[i].entry;
75         len = tags->data.vorbis_comment.comments[i].length;
76         if (strncmp(str, "TITLE=", 6) == 0) {
77             info.title.str = str + 6;
78             info.title.len = len;
79         }
80         else if (strncmp(str, "ARTIST=", 7) == 0) {
81             info.artist.str = str + 7;
82             info.artist.len = len;
83         }
84         else if (strncmp(str, "ALBUM=", 6) == 0) {
85             info.album.str = str + 6;
86             info.album.len = len;
87         }
88         else if (strncmp(str, "GENRE=", 6) == 0) {
89             info.genre.str = str + 6;
90             info.genre.len = len;
91         }
92         else if (strncmp(str, "TRACKNUMBER=", 12) == 0)
93             info.trackno = atoi(str + 12);
94     }
95
96     lms_string_size_strip_and_free(&info.title);
97     lms_string_size_strip_and_free(&info.artist);
98     lms_string_size_strip_and_free(&info.album);
99     lms_string_size_strip_and_free(&info.genre);
100
101     if (!info.title.str) {
102         int ext_idx;
103         ext_idx = ((int)match) - 1;
104         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
105         info.title.str = malloc((info.title.len + 1) * sizeof(char));
106         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
107         info.title.str[info.title.len] = '\0';
108     }
109     lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
110
111     if (info.artist.str)
112         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
113     if (info.album.str)
114         lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
115     if (info.genre.str)
116         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
117
118 #if 0
119     fprintf(stderr, "file %s info\n", finfo->path);
120     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
121     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
122     fprintf(stderr, "\talbum='%s'\n", info.album.str);
123     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
124     fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
125 #endif
126
127     info.id = finfo->id;
128     r = lms_db_audio_add(plugin->audio_db, &info);
129
130     FLAC__metadata_object_delete(tags);
131
132     return r;
133 }
134
135 static int
136 _setup(struct plugin *plugin, struct lms_context *ctxt)
137 {
138     plugin->audio_db = lms_db_audio_new(ctxt->db);
139     if (!plugin->audio_db)
140         return -1;
141     return 0;
142 }
143
144 static int
145 _start(struct plugin *plugin, struct lms_context *ctxt)
146 {
147     return lms_db_audio_start(plugin->audio_db);
148 }
149
150 static int
151 _finish(struct plugin *plugin, struct lms_context *ctxt)
152 {
153     if (plugin->audio_db)
154         lms_db_audio_free(plugin->audio_db);
155     return 0;
156 }
157
158 static int
159 _close(struct plugin *plugin)
160 {
161     free(plugin);
162     return 0;
163 }
164
165 API struct lms_plugin *
166 lms_plugin_open(void)
167 {
168     struct plugin *plugin;
169
170     plugin = (struct plugin *)malloc(sizeof(*plugin));
171     plugin->plugin.name = _name;
172     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
173     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
174     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
175     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
176     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
177     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
178
179     return (struct lms_plugin *)plugin;
180 }