plugins: Use shared function to add fallback name
[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 static const char PV[] = PACKAGE_VERSION; /* mp4.h screws PACKAGE_VERSION */
28
29 #include <lightmediascanner_plugin.h>
30 #include <lightmediascanner_db.h>
31 #include <shared/util.h>
32
33 #ifdef HAVE_MP4V2
34 #include <mp4v2/mp4v2.h>
35 #else
36 #include <mp4.h>
37 #endif
38 #include <string.h>
39 #include <stdlib.h>
40
41 enum StreamTypes {
42     STREAM_TYPE_UNKNOWN = 0,
43     STREAM_TYPE_AUDIO,
44     STREAM_TYPE_VIDEO
45 };
46
47 struct mp4_info {
48     struct lms_string_size title;
49     struct lms_string_size artist;
50     struct lms_string_size album;
51     struct lms_string_size genre;
52     u_int16_t trackno;
53 };
54
55 struct plugin {
56     struct lms_plugin plugin;
57     lms_db_audio_t *audio_db;
58     lms_db_video_t *video_db;
59 };
60
61 static const char _name[] = "mp4";
62 static const struct lms_string_size _exts[] = {
63     LMS_STATIC_STRING_SIZE(".mp4"),
64     LMS_STATIC_STRING_SIZE(".m4a"),
65     LMS_STATIC_STRING_SIZE(".m4v"),
66     LMS_STATIC_STRING_SIZE(".mov"),
67     LMS_STATIC_STRING_SIZE(".qt"),
68     LMS_STATIC_STRING_SIZE(".3gp")
69 };
70 static const char *_cats[] = {
71     "multimedia",
72     "audio",
73     "video",
74     NULL
75 };
76 static const char *_authors[] = {
77     "Andre Moreira Magalhaes",
78     NULL
79 };
80
81 static void *
82 _match(struct plugin *p, const char *path, int len, int base)
83 {
84     long i;
85
86     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
87     if (i < 0)
88       return NULL;
89     else
90       return (void*)(i + 1);
91 }
92
93 static int
94 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
95 {
96     struct mp4_info info = { };
97     struct lms_audio_info audio_info = { };
98     struct lms_video_info video_info = { };
99     int r, stream_type = STREAM_TYPE_AUDIO;
100     MP4FileHandle mp4_fh;
101     u_int32_t num_tracks;
102
103 #ifdef HAVE_MP4V2_2_0_API
104     mp4_fh = MP4Read(finfo->path);
105 #else
106     mp4_fh = MP4Read(finfo->path, 0);
107 #endif
108     if (mp4_fh == MP4_INVALID_FILE_HANDLE) {
109         fprintf(stderr, "ERROR: cannot read mp4 file %s\n", finfo->path);
110         return -1;
111     }
112
113     /* check if the file contains a video track */
114     num_tracks = MP4GetNumberOfTracks(mp4_fh, MP4_VIDEO_TRACK_TYPE, 0);
115     if (num_tracks > 0)
116         stream_type = STREAM_TYPE_VIDEO;
117
118     MP4GetMetadataName(mp4_fh, &info.title.str);
119     if (info.title.str)
120         info.title.len = strlen(info.title.str);
121     MP4GetMetadataArtist(mp4_fh, &info.artist.str);
122     if (info.artist.str)
123         info.artist.len = strlen(info.artist.str);
124
125     if (stream_type == STREAM_TYPE_AUDIO) {
126         u_int16_t total_tracks;
127
128         MP4GetMetadataAlbum(mp4_fh, &info.album.str);
129         if (info.album.str)
130             info.album.len = strlen(info.album.str);
131         MP4GetMetadataGenre(mp4_fh, &info.genre.str);
132         if (info.genre.str)
133             info.genre.len = strlen(info.genre.str);
134
135         MP4GetMetadataTrack(mp4_fh, &info.trackno, &total_tracks);
136     }
137
138     lms_string_size_strip_and_free(&info.title);
139     lms_string_size_strip_and_free(&info.artist);
140     lms_string_size_strip_and_free(&info.album);
141     lms_string_size_strip_and_free(&info.genre);
142
143     if (!info.title.str)
144         info.title = str_extract_name_from_path(finfo->path, finfo->path_len,
145                                                 finfo->base,
146                                                 &_exts[((long) match) - 1],
147                                                 NULL);
148     if (info.title.str)
149         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
150
151     if (info.artist.str)
152         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
153     if (info.album.str)
154         lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
155     if (info.genre.str)
156         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
157
158 #if 0
159     fprintf(stderr, "file %s info\n", finfo->path);
160     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
161     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
162     fprintf(stderr, "\talbum='%s'\n", info.album.str);
163     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
164 #endif
165
166     if (stream_type == STREAM_TYPE_AUDIO) {
167         audio_info.id = finfo->id;
168         audio_info.title = info.title;
169         audio_info.artist = info.artist;
170         audio_info.album = info.album;
171         audio_info.genre = info.genre;
172         audio_info.trackno = info.trackno;
173         r = lms_db_audio_add(plugin->audio_db, &audio_info);
174     }
175     else {
176         video_info.id = finfo->id;
177         video_info.title = info.title;
178         video_info.artist = info.artist;
179         r = lms_db_video_add(plugin->video_db, &video_info);
180     }
181
182 #ifdef HAVE_MP4V2_2_0_API
183     MP4Close(mp4_fh, 0);
184 #else
185     MP4Close(mp4_fh);
186 #endif
187
188     free(info.title.str);
189     free(info.artist.str);
190     free(info.album.str);
191     free(info.genre.str);
192
193     return r;
194 }
195
196 static int
197 _setup(struct plugin *plugin, struct lms_context *ctxt)
198 {
199     plugin->audio_db = lms_db_audio_new(ctxt->db);
200     if (!plugin->audio_db)
201         return -1;
202     plugin->video_db = lms_db_video_new(ctxt->db);
203     if (!plugin->video_db)
204         return -1;
205
206     return 0;
207 }
208
209 static int
210 _start(struct plugin *plugin, struct lms_context *ctxt)
211 {
212     int r;
213     r = lms_db_audio_start(plugin->audio_db);
214     r |= lms_db_video_start(plugin->video_db);
215     return r;
216 }
217
218 static int
219 _finish(struct plugin *plugin, struct lms_context *ctxt)
220 {
221     if (plugin->audio_db)
222         lms_db_audio_free(plugin->audio_db);
223     if (plugin->video_db)
224         lms_db_video_free(plugin->video_db);
225
226     return 0;
227 }
228
229 static int
230 _close(struct plugin *plugin)
231 {
232     free(plugin);
233     return 0;
234 }
235
236 API struct lms_plugin *
237 lms_plugin_open(void)
238 {
239     struct plugin *plugin;
240
241     plugin = (struct plugin *)malloc(sizeof(*plugin));
242     plugin->plugin.name = _name;
243     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
244     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
245     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
246     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
247     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
248     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
249
250     return (struct lms_plugin *)plugin;
251 }
252
253 API const struct lms_plugin_info *
254 lms_plugin_info(void)
255 {
256     static struct lms_plugin_info info = {
257         _name,
258         _cats,
259         "MP4 files (MP4, M4A, MOV, QT, 3GP)",
260         PV,
261         _authors,
262         "http://lms.garage.maemo.org"
263     };
264
265     return &info;
266 }