support some format.
[platform/core/multimedia/libmm-fileinfo.git] / tests / mm_file_test.c
1 /*
2  * libmm-fileinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31
32 #include <mm_file.h>
33 #include <mm_error.h>
34
35 #include "mm_file_traverse.h"
36
37 #define MM_TIME_CHECK_START \
38 { FILE *msg_tmp_fp = fopen("time_check.txt", "a+"); struct timeval start, finish; gettimeofday(&start, NULL);
39 #define MM_TIME_CHECK_FINISH(title) \
40 gettimeofday(&finish, NULL); \
41 double end_time = (finish.tv_sec + 1e-6*finish.tv_usec); \
42 double start_time = (start.tv_sec + 1e-6*start.tv_usec); \
43 if(msg_tmp_fp != NULL) { \
44 fprintf(msg_tmp_fp, "%s\n", title); \
45 fprintf(msg_tmp_fp, " - start_time:   %3.5lf sec\n", start_time); \
46 fprintf(msg_tmp_fp, " - finish_time:  %3.5lf sec\n", end_time); \
47 fprintf(msg_tmp_fp, " - elapsed time: %3.5lf sec\n", end_time - start_time); \
48 fflush(msg_tmp_fp); fclose(msg_tmp_fp); }}
49
50 typedef struct _mmfile_value {
51         int len;
52         union {
53                 int i_val;
54                 double d_val;
55                 char *s_val;
56                 void *p_val;
57         }value;
58 }mmfile_value_t;
59
60 typedef struct _TagContext {
61         mmfile_value_t artist;
62         mmfile_value_t title;
63         mmfile_value_t album;
64         mmfile_value_t album_artist;
65         mmfile_value_t genre;
66         mmfile_value_t author;
67         mmfile_value_t copyright;
68         mmfile_value_t date;                    //string
69         mmfile_value_t recdate;                 //string
70         mmfile_value_t description;
71         mmfile_value_t comment;
72         mmfile_value_t artwork;         //data
73         mmfile_value_t artwork_size;    //int
74         mmfile_value_t artwork_mime;
75         mmfile_value_t track_num;
76         mmfile_value_t classfication;
77         mmfile_value_t rating;
78         mmfile_value_t conductor;
79         mmfile_value_t longitude;               //-> double
80         mmfile_value_t latitude;                
81         mmfile_value_t altitude;                //<-double
82         mmfile_value_t unsynclyrics;
83         mmfile_value_t synclyrics_size;
84         mmfile_value_t rotate;                  //string
85 }TagContext_t;
86
87 typedef struct _ContentContext {
88         int duration;
89         int video_codec;
90         int video_bitrate;
91         int video_fps;
92         int video_w;
93         int video_h;
94         int video_track_id;
95         int video_track_num;
96         int audio_codec;
97         int audio_bitrate;
98         int audio_channel;
99         int audio_samplerate;
100         int audio_track_id;
101         int audio_track_num;
102         int audio_bitpersample;
103         mmfile_value_t thumbnail;
104 }ContentContext_t;
105
106
107 char * AudioCodecTypeString [] = {
108         "AMR", "G723.1", "MP3", "OGG", "AAC", "WMA", "MMF", "ADPCM", "WAVE", "WAVE NEW",        // 0~9
109         "MIDI", "IMELODY", "MXMF", "MPEG1-Layer1 codec", "MPEG1-Layer2 codec",  // 10~14
110         "G711", "G722", "G722.1", "G722.2  (AMR-WB)", "G723 wideband speech",   // 15~19
111         "G726 (ADPCM)", "G728 speech", "G729", "G729a", "G729.1",       // 20~24
112         "Real", "AAC-Low complexity", "AAC-Main profile", "AAC-Scalable sample rate", "AAC-Long term prediction",       // 25~29
113         "AAC-High Efficiency v1", "AAC-High efficiency v2",     "DolbyDigital", "Apple Lossless", "Sony proprietary",   // 30~34
114         "SPEEX", "Vorbis", "AIFF", "AU", "None (will be deprecated)",   //35~39
115         "PCM", "ALAW", "MULAW", "MS ADPCM", "FLAC"      // 40~44
116 };
117
118
119 char * VideoCodecTypeString [] = {
120         "None (will be deprecated)",    // 0
121         "H263", "H264", "H26L", "MPEG4", "MPEG1",       // 1~5
122         "WMV", "DIVX", "XVID", "H261", "H262/MPEG2-part2",      // 6~10
123         "H263v2", "H263v3", "Motion JPEG", "MPEG2", "MPEG4 part-2 Simple profile",      // 11~15
124         "MPEG4 part-2 Advanced Simple profile", "MPEG4 part-2 Main profile", "MPEG4 part-2 Core profile", "MPEG4 part-2 Adv Coding Eff profile", "MPEG4 part-2 Adv RealTime Simple profile",    // 16~20
125         "MPEG4 part-10 (h.264)", "Real", "VC-1", "AVS", "Cinepak",      // 21~25
126         "Indeo", "Theora", "Flv"        // 26~28
127 };
128
129
130
131 FILE *fpFailList = NULL;
132
133 static int mmfile_get_file_infomation (void *data, void* user_data, bool file_test);
134
135 inline static int mm_file_is_little_endian (void)
136 {
137     int i = 0x00000001;
138     return ((char *)&i)[0];
139 }
140
141 #define READ_FROM_FILE(FILE_PATH, data, size) \
142 do{     \
143         FILE * fp = fopen (FILE_PATH, "r");     \
144         if (fp) {       \
145                         fseek (fp, 0, SEEK_END);        \
146                         size = ftell(fp);       \
147                         fseek (fp, 0, SEEK_SET);        \
148                         if(size > 0) data = malloc (size);      \
149                         if(data != NULL ) { if (fread (data, size, sizeof(char), fp) != size) { printf("fread error\n"); } }    \
150                         fclose (fp);    \
151                         printf("file size = %d\n", size );      \
152         }       \
153 }while(0)
154
155 static int
156 _is_file_exist (const char *filename)
157 {
158         int ret = 1;
159         if (filename) {
160                 const char* to_access = (strstr(filename,"file://")!=NULL)? filename+7:filename;
161                 ret = access (to_access, R_OK );
162                 if (ret != 0) {
163                         printf  ("file [%s] not found.\n", to_access);
164                 }
165         }
166         return !ret;
167 }
168
169
170 int main(int argc, char **argv)
171 {
172     struct stat statbuf;
173         bool file_test = true;          //if you want to test mm_file_create_content_XXX_from_memory() set file_test to false
174
175     if (_is_file_exist (argv[1])) {
176         int ret = lstat (argv[1], &statbuf);
177             if ( ret < 0 ) {
178                 printf ("lstat error[%d]\n", ret);
179                 return MMFILE_FAIL;
180         }
181
182                 if (fpFailList == NULL) {
183                         fpFailList = fopen ("/opt/var/log/mmfile_fails.txt", "w");
184                 }
185
186                 if ( S_ISDIR (statbuf.st_mode) )        {
187                         mmfile_get_file_names (argv[1], mmfile_get_file_infomation, NULL);
188                 } else {
189                         mmfile_get_file_infomation (argv[1], NULL, file_test);
190                 }
191
192                 if (fpFailList != NULL) {
193                         fflush (fpFailList);
194                         fclose (fpFailList);
195                 }
196     }
197
198     return 0;//exit(0);
199 }
200
201 static int mmfile_get_file_infomation (void *data, void* user_data, bool file_test)
202 {
203         MMHandleType content_attrs = 0;
204         MMHandleType tag_attrs = 0;
205         char *err_attr_name = NULL;
206         int audio_track_num = 0;
207         int video_track_num = 0;
208         int ret = 0;
209         char filename[512];
210
211         memset (filename, 0x00, 512);
212         memcpy (filename, (char*)data, strlen ((char*)data));
213
214         MM_TIME_CHECK_START
215
216         printf ("Extracting information for [%s] \n", filename);
217         /* get track info */
218         ret = mm_file_get_stream_info(filename, &audio_track_num, &video_track_num);
219         if (ret == MM_ERROR_NONE) {
220                 printf ("# audio=%d, video=%d\n", audio_track_num, video_track_num);
221         } else {
222                 printf ("Failed to mm_file_get_stream_info() error=[%x]\n", ret);
223         }
224
225         if(file_test) {
226                 /* get content handle */
227                 ret = mm_file_create_content_attrs(&content_attrs, filename);
228         } else {
229                 int file_size = 0;
230                 unsigned char * buffer = NULL;
231                 /* Read file */
232                 READ_FROM_FILE(filename, buffer, file_size);
233
234                 ret =mm_file_create_content_attrs_from_memory(&content_attrs, buffer, file_size, MM_FILE_FORMAT_3GP);
235         }
236
237         if (ret == MM_ERROR_NONE && content_attrs) {
238                 ContentContext_t ccontent;
239                 memset (&ccontent, 0, sizeof (ContentContext_t));
240
241                 ret = mm_file_get_attrs(content_attrs, &err_attr_name, MM_FILE_CONTENT_DURATION, &ccontent.duration, NULL);
242                 printf("# duration: %d\n", ccontent.duration);
243
244                 if (ret != MM_ERROR_NONE && err_attr_name)
245                 {
246                         printf("failed to get %s\n", err_attr_name);
247                         free(err_attr_name);
248                         err_attr_name = NULL;
249                 }
250
251                 if (audio_track_num)
252                 {
253                         ret = mm_file_get_attrs(content_attrs,
254                                                                         NULL,
255                                                                         MM_FILE_CONTENT_AUDIO_CODEC, &ccontent.audio_codec,
256                                                                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &ccontent.audio_samplerate,
257                                                                         MM_FILE_CONTENT_AUDIO_BITRATE, &ccontent.audio_bitrate,
258                                                                         MM_FILE_CONTENT_AUDIO_CHANNELS, &ccontent.audio_channel,
259                                                                         MM_FILE_CONTENT_AUDIO_TRACK_INDEX, &ccontent.audio_track_id,
260                                                                         MM_FILE_CONTENT_AUDIO_TRACK_COUNT, &ccontent.audio_track_num,
261                                                                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &ccontent.audio_bitpersample,
262                                                                         NULL);
263
264                         if(ret != MM_ERROR_NONE) {
265                                 printf("failed to get audio attrs\n");
266                         } else {
267                                 printf ("[Audio] ----------------------------------------- \n");
268                                 printf("# audio codec: %d ", ccontent.audio_codec);
269                                 printf ("[%s]\n", (ccontent.audio_codec >= 0 && ccontent.audio_codec < MM_AUDIO_CODEC_NUM)? AudioCodecTypeString[ccontent.audio_codec] : "Invalid");
270                                 printf("# audio samplerate: %d Hz\n", ccontent.audio_samplerate);
271                                 printf("# audio bitrate: %d bps\n", ccontent.audio_bitrate);
272                                 printf("# audio channel: %d\n", ccontent.audio_channel);
273                                 printf("# audio track id: %d\n", ccontent.audio_track_id);
274                                 printf("# audio track num: %d\n", ccontent.audio_track_num);
275                                 printf("# audio bit per sample: %d\n", ccontent.audio_bitpersample);
276                         }
277                 }
278         
279                 if (video_track_num)
280                 {
281                         ret = mm_file_get_attrs(content_attrs,
282                                                                         NULL,
283                                                                         MM_FILE_CONTENT_VIDEO_CODEC, &ccontent.video_codec,
284                                                                         MM_FILE_CONTENT_VIDEO_BITRATE, &ccontent.video_bitrate,
285                                                                         MM_FILE_CONTENT_VIDEO_FPS, &ccontent.video_fps,
286                                                                         MM_FILE_CONTENT_VIDEO_TRACK_INDEX, &ccontent.video_track_id,
287                                                                         MM_FILE_CONTENT_VIDEO_WIDTH, &ccontent.video_w,
288                                                                         MM_FILE_CONTENT_VIDEO_HEIGHT, &ccontent.video_h,
289                                                                         MM_FILE_CONTENT_VIDEO_THUMBNAIL, &ccontent.thumbnail.value.p_val, &ccontent.thumbnail.len,
290                                                                         NULL);
291
292                         if(ret != MM_ERROR_NONE) {
293                                 printf("failed to get video attrs\n");
294                         } else {
295                                 printf ("[Video] ----------------------------------------- \n");
296                                 printf("# video codec: %d ", ccontent.video_codec);
297                                 printf ("[%s]\n", (ccontent.video_codec >= 0 && ccontent.video_codec < MM_VIDEO_CODEC_NUM)? VideoCodecTypeString[ccontent.video_codec] : "Invalid");
298                                 printf("# video bitrate: %d bps\n", ccontent.video_bitrate);
299                                 printf("# video fps: %d\n", ccontent.video_fps);
300                                 printf("# video track id: %d\n", ccontent.video_track_id);
301                                 printf("# video width/height: %d x %d\n", ccontent.video_w, ccontent.video_h);
302                                 printf("# video thumbnail: %p\n", ccontent.thumbnail.value.p_val);
303                         }
304                 }
305
306                 mm_file_destroy_content_attrs(content_attrs);
307         } else {
308                 printf ("Failed to mm_file_create_content_attrs() error=[%x]\n", ret);
309         }
310
311         if(file_test) {
312                 /* get tag handle */
313                 ret = mm_file_create_tag_attrs(&tag_attrs, filename);
314         } else {
315                 int file_size = 0;
316                 unsigned char * buffer = NULL;
317                 /* Read file */
318                 READ_FROM_FILE(filename, buffer, file_size);
319
320                 ret =mm_file_create_tag_attrs_from_memory(&tag_attrs, buffer, file_size, MM_FILE_FORMAT_3GP);
321         }
322
323         if (ret == MM_ERROR_NONE && tag_attrs) {
324                 TagContext_t ctag;
325                 memset (&ctag, 0, sizeof (TagContext_t));
326                 /* get attributes of tag  */
327                 ret = mm_file_get_attrs( tag_attrs,
328                                                                         &err_attr_name,
329                                                                         MM_FILE_TAG_ARTIST, &ctag.artist.value.s_val, &ctag.artist.len,
330                                                                         MM_FILE_TAG_ALBUM, &ctag.album.value.s_val, &ctag.album.len,
331                                                                         MM_FILE_TAG_ALBUM_ARTIST, &ctag.album_artist.value.s_val, &ctag.album_artist.len,
332                                                                         MM_FILE_TAG_TITLE, &ctag.title.value.s_val, &ctag.title.len,
333                                                                         MM_FILE_TAG_GENRE, &ctag.genre.value.s_val, &ctag.genre.len,
334                                                                         MM_FILE_TAG_AUTHOR, &ctag.author.value.s_val, &ctag.author.len,
335                                                                         MM_FILE_TAG_COPYRIGHT, &ctag.copyright.value.s_val, &ctag.copyright.len,
336                                                                         MM_FILE_TAG_DATE, &ctag.date.value.s_val, &ctag.date.len,
337                                                                         MM_FILE_TAG_RECDATE, &ctag.recdate.value.s_val, &ctag.recdate.len,
338                                                                         MM_FILE_TAG_DESCRIPTION, &ctag.description.value.s_val, &ctag.description.len,
339                                                                         MM_FILE_TAG_COMMENT, &ctag.comment.value.s_val, &ctag.comment.len,
340                                                                         MM_FILE_TAG_ARTWORK, &ctag.artwork.value.p_val, &ctag.artwork.len,
341                                                                         MM_FILE_TAG_ARTWORK_SIZE, &ctag.artwork_size.value.i_val,
342                                                                         MM_FILE_TAG_ARTWORK_MIME, &ctag.artwork_mime.value.s_val, &ctag.artwork_mime.len,
343                                                                         MM_FILE_TAG_TRACK_NUM, &ctag.track_num.value.s_val, &ctag.track_num.len,
344                                                                         MM_FILE_TAG_CLASSIFICATION, &ctag.classfication.value.s_val, &ctag.classfication.len,
345                                                                         MM_FILE_TAG_RATING, &ctag.rating.value.s_val, &ctag.rating.len,
346                                                                         MM_FILE_TAG_LONGITUDE, &ctag.longitude.value.d_val,
347                                                                         MM_FILE_TAG_LATIDUE, &ctag.latitude.value.d_val,
348                                                                         MM_FILE_TAG_ALTIDUE, &ctag.altitude.value.d_val,
349                                                                         MM_FILE_TAG_CONDUCTOR, &ctag.conductor.value.s_val, &ctag.conductor.len,
350                                                                         MM_FILE_TAG_UNSYNCLYRICS, &ctag.unsynclyrics.value.s_val, &ctag.unsynclyrics.len,
351                                                                         MM_FILE_TAG_SYNCLYRICS_NUM, &ctag.synclyrics_size.value.i_val,
352                                                                         MM_FILE_TAG_ROTATE, &ctag.rotate.value.s_val, &ctag.rotate.len,
353                                                                         NULL);
354                 if (ret != MM_ERROR_NONE &&  err_attr_name)
355                 {
356                         printf("failed to get %s attrs\n", err_attr_name);
357                         free(err_attr_name);
358                         err_attr_name = NULL;
359
360                         if (msg_tmp_fp) /* opened by MM_TIME_CHECK_START */
361                         {
362                                 fclose (msg_tmp_fp);
363                                 msg_tmp_fp = NULL;
364                         }
365                         mm_file_destroy_tag_attrs(tag_attrs);
366                         return -1;
367                 }
368
369                 /* print tag information         */
370                 printf ("[Tag] =================================== \n");
371                 printf("# artist: [%s]\n", ctag.artist.value.s_val);
372                 printf("# title: [%s]\n", ctag.title.value.s_val);
373                 printf("# album: [%s]\n", ctag.album.value.s_val);
374                 printf("# album_artist: [%s]\n", ctag.album_artist.value.s_val);
375                 printf("# genre: [%s]\n", ctag.genre.value.s_val);
376                 printf("# author: [%s]\n", ctag.author.value.s_val);
377                 printf("# copyright: [%s]\n", ctag.copyright.value.s_val);
378                 printf("# year: [%s]\n", ctag.date.value.s_val);
379                 printf("# recdate: [%s]\n", ctag.recdate.value.s_val);
380                 printf("# description: [%s]\n", ctag.description.value.s_val);
381                 printf("# comment: [%s]\n", ctag.comment.value.s_val);
382                 printf("# artwork: [%p]\n", ctag.artwork.value.p_val);
383                 printf("# artwork_size: [%d]\n", ctag.artwork_size.value.i_val);
384                 printf("# artwork_mime: [%s]\n", ctag.artwork_mime.value.s_val);
385                 printf("# track number: [%s]\n", ctag.track_num.value.s_val);
386                 printf("# classification: [%s]\n", ctag.classfication.value.s_val);
387                 printf("# rating: [%s]\n", ctag.rating.value.s_val);
388                 printf("# longitude: [%f]\n", ctag.longitude.value.d_val);
389                 printf("# latitude: [%f]\n", ctag.latitude.value.d_val);
390                 printf("# altitude: [%f]\n", ctag.altitude.value.d_val);
391                 printf("# conductor: [%s]\n", ctag.conductor.value.s_val);
392                 printf("# unsynclyrics_length: [%d]\n", ctag.unsynclyrics.len);
393                 printf("# unsynclyrics: [%s]\n", ctag.unsynclyrics.value.s_val);
394                 printf("# synclyrics size: [%d]\n", ctag.synclyrics_size.value.i_val);
395                 printf("# rotate: [%s]\n", ctag.rotate.value.s_val);
396
397                 if(ctag.synclyrics_size.value.i_val > 0) {
398                         int idx = 0;
399                         unsigned long time_info = 0;
400                         char * lyrics_info = NULL;
401
402                         printf("# synclyrics: \n");
403                         
404                         for(idx = 0; idx < ctag.synclyrics_size.value.i_val; idx++) {
405                                 ret = mm_file_get_synclyrics_info(tag_attrs, idx, &time_info, &lyrics_info);
406                                 if(ret == MM_ERROR_NONE) {
407                                         printf("[%2d][%6ld][%s]\n", idx, time_info, lyrics_info);
408                                 } else {
409                                         printf("Error when get lyrics\n");
410                                         break;
411                                 }
412                         }
413                 }
414                 
415                 /* release tag */
416                 ret = mm_file_destroy_tag_attrs(tag_attrs);
417                 if (ret != MM_ERROR_NONE) {
418                         printf("Error mm_file_destroy_tag_attrs: %d", ret);
419                         if (msg_tmp_fp) {
420                                 fclose (msg_tmp_fp);
421                                 msg_tmp_fp = NULL;
422                         }
423                         return -1;
424                 }
425         } else {
426                 printf ("Failed to mm_file_create_tag_attrs() error=[%x]\n", ret);
427         }
428
429
430         printf ("=================================================\n\n");
431
432         MM_TIME_CHECK_FINISH (filename);
433
434     return 0;
435 }