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