Improve code reusage: string strip and free functions.
[lms] / lightmediascanner / src / plugins / mp4 / mp4.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  * mp4 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 <mp4.h>
34
35 enum StreamTypes {
36     STREAM_TYPE_UNKNOWN = 0,
37     STREAM_TYPE_AUDIO,
38     STREAM_TYPE_VIDEO
39 };
40
41 struct mp4_info {
42     struct lms_string_size title;
43     struct lms_string_size artist;
44     struct lms_string_size album;
45     struct lms_string_size genre;
46 };
47
48 struct plugin {
49     struct lms_plugin plugin;
50     lms_db_audio_t *audio_db;
51     lms_db_video_t *video_db;
52 };
53
54 static const char _name[] = "mp4";
55 static const struct lms_string_size _exts[] = {
56     LMS_STATIC_STRING_SIZE(".mp4")
57 };
58
59 static void *
60 _match(struct plugin *p, const char *path, int len, int base)
61 {
62     int i;
63
64     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
65     if (i < 0)
66       return NULL;
67     else
68       return (void*)(i + 1);
69 }
70
71 static int
72 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
73 {
74     struct mp4_info info = {{0}, {0}, {0}, {0}};
75     struct lms_audio_info audio_info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
76     struct lms_video_info video_info = {0, {0}, {0}};
77     int r, stream_type = STREAM_TYPE_AUDIO;
78     MP4FileHandle mp4_fh;
79     u_int32_t num_tracks;
80
81     mp4_fh = MP4Read(finfo->path, 0);
82     if (mp4_fh == MP4_INVALID_FILE_HANDLE) {
83         fprintf(stderr, "ERROR: cannot read mp4 file\n");
84         return -1;
85     }
86
87     MP4GetMetadataName(mp4_fh, &info.title.str);
88     if (info.title.str)
89         info.title.len = strlen(info.title.str);
90     MP4GetMetadataArtist(mp4_fh, &info.artist.str);
91     if (info.artist.str)
92         info.artist.len = strlen(info.artist.str);
93     MP4GetMetadataAlbum(mp4_fh, &info.album.str);
94     if (info.album.str)
95         info.album.len = strlen(info.album.str);
96     MP4GetMetadataGenre(mp4_fh, &info.genre.str);
97     if (info.genre.str)
98         info.genre.len = strlen(info.genre.str);
99
100     /* check if the file contains a video track */
101     num_tracks = MP4GetNumberOfTracks(mp4_fh, MP4_VIDEO_TRACK_TYPE, 0);
102     if (num_tracks > 0)
103         stream_type = STREAM_TYPE_VIDEO;
104
105     lms_string_size_strip_and_free(&info.title);
106     lms_string_size_strip_and_free(&info.artist);
107     lms_string_size_strip_and_free(&info.album);
108     lms_string_size_strip_and_free(&info.genre);
109
110     if (!info.title.str) {
111         int ext_idx;
112         ext_idx = ((int)match) - 1;
113         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
114         info.title.str = malloc((info.title.len + 1) * sizeof(char));
115         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
116         info.title.str[info.title.len] = '\0';
117     }
118     lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
119
120     if (info.artist.str)
121         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
122     if (info.album.str)
123         lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
124     if (info.genre.str)
125         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
126
127 #if 0
128     fprintf(stderr, "file %s info\n", finfo->path);
129     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
130     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
131     fprintf(stderr, "\talbum='%s'\n", info.album.str);
132     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
133 #endif
134
135     if (stream_type == STREAM_TYPE_AUDIO) {
136         audio_info.id = finfo->id;
137         audio_info.title = info.title;
138         audio_info.artist = info.artist;
139         audio_info.album = info.album;
140         audio_info.genre = info.genre;
141         r = lms_db_audio_add(plugin->audio_db, &audio_info);
142     }
143     else {
144         video_info.id = finfo->id;
145         video_info.title = info.title;
146         video_info.artist = info.artist;
147         r = lms_db_video_add(plugin->video_db, &video_info);
148     }
149
150     MP4Close(mp4_fh);
151
152     if (info.title.str)
153         free(info.title.str);
154     if (info.artist.str)
155         free(info.artist.str);
156     if (info.album.str)
157         free(info.album.str);
158     if (info.genre.str)
159         free(info.genre.str);
160
161     return r;
162 }
163
164 static int
165 _setup(struct plugin *plugin, struct lms_context *ctxt)
166 {
167     plugin->audio_db = lms_db_audio_new(ctxt->db);
168     if (!plugin->audio_db)
169         return -1;
170     plugin->video_db = lms_db_video_new(ctxt->db);
171     if (!plugin->video_db)
172         return -1;
173
174     return 0;
175 }
176
177 static int
178 _start(struct plugin *plugin, struct lms_context *ctxt)
179 {
180     int r;
181     r = lms_db_audio_start(plugin->audio_db);
182     r |= lms_db_video_start(plugin->video_db);
183     return r;
184 }
185
186 static int
187 _finish(struct plugin *plugin, struct lms_context *ctxt)
188 {
189     if (plugin->audio_db)
190         lms_db_audio_free(plugin->audio_db);
191     if (plugin->video_db)
192         lms_db_video_free(plugin->video_db);
193
194     return 0;
195 }
196
197 static int
198 _close(struct plugin *plugin)
199 {
200     free(plugin);
201     return 0;
202 }
203
204 API struct lms_plugin *
205 lms_plugin_open(void)
206 {
207     struct plugin *plugin;
208
209     plugin = (struct plugin *)malloc(sizeof(*plugin));
210     plugin->plugin.name = _name;
211     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
212     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
213     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
214     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
215     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
216     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
217
218     return (struct lms_plugin *)plugin;
219 }