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