Merge all storage tables
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, 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 <unistd.h>
23 #include <stdlib.h>
24 #ifndef __USE_XOPEN
25 #define DEF_XOPEN
26 #define __USE_XOPEN /* needed for strptime */
27 #endif
28 #include <time.h>
29 #ifdef DEF_XOPEN
30 #undef __USE_XOPEN
31 #endif
32 #include <string.h>
33 #include <system_info.h>
34 #include <sys/vfs.h>
35 #include <glib/gstdio.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <ctype.h>
39 #include <aul/aul.h>
40 #include <mm_file.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include "media-util-err.h"
46 #include "media-svc-util.h"
47 #include "media-svc-db-utils.h"
48 #include "media-svc-debug.h"
49 #include "media-svc-env.h"
50 #include "media-svc-hash.h"
51 #include "media-svc-album.h"
52 #include "media-svc-localize_ch.h"
53
54 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
55
56 /* Define data structures for media type and mime type */
57 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
58 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
59 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
60 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
61 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
62 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
63 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
64 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
65 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
66
67 #define CONTENT_TYPE_NUM 5
68 #define MUSIC_MIME_NUM 29
69 #define SOUND_MIME_NUM 1
70 #define MIME_TYPE_LENGTH 255
71 #define MIME_LENGTH 50
72 #define _3GP_FILE ".3gp"
73 #define _MP4_FILE ".mp4"
74 #define _ASF_FILE ".asf"
75 #define MEDIA_SVC_ARTWORK_SIZE 2000
76 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
77
78 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
79
80 static int media_svc_pinyin_support = -1;
81
82 typedef struct {
83         char content_type[15];
84         int category_by_mime;
85 } _media_svc_content_table_s;
86
87 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
88         {"audio", MEDIA_SVC_CATEGORY_SOUND},
89         {"image", MEDIA_SVC_CATEGORY_IMAGE},
90         {"video", MEDIA_SVC_CATEGORY_VIDEO},
91         {"application", MEDIA_SVC_CATEGORY_ETC},
92         {"text", MEDIA_SVC_CATEGORY_ETC},
93 };
94
95 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
96         /*known mime types of normal files*/
97         "mpeg",
98         "ogg",
99         "x-ms-wma",
100         "x-flac",
101         "mp4",
102         /* known mime types of drm files*/
103         "mp3",
104         "x-mp3", /*alias of audio/mpeg*/
105         "x-mpeg", /*alias of audio/mpeg*/
106         "3gpp",
107         "x-ogg", /*alias of audio/ogg*/
108         "vnd.ms-playready.media.pya:*.pya", /*playready*/
109         "wma",
110         "aac",
111         "x-m4a", /*alias of audio/mp4*/
112         /* below mimes are rare*/
113         "x-vorbis+ogg",
114         "x-flac+ogg",
115         "x-matroska",
116         "ac3",
117         "mp2",
118         "x-ape",
119         "x-ms-asx",
120         "vnd.rn-realaudio",
121
122         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
123         "vorbis", /*alias of audio/x-vorbis+ogg*/
124         "x-oggflac",
125         "x-mp2", /*alias of audio/mp2*/
126         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
127         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
128         "x-wav",
129 };
130
131 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
132         "x-smaf",
133 };
134
135 char *_media_info_generate_uuid(void)
136 {
137         uuid_t uuid_value;
138         static char uuid_unparsed[37];
139
140 RETRY_GEN:
141         uuid_generate(uuid_value);
142         uuid_unparse(uuid_value, uuid_unparsed);
143
144         if (strlen(uuid_unparsed) < 36) {
145                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
146                 goto RETRY_GEN;
147         }
148
149         return uuid_unparsed;
150 }
151
152 static int __media_svc_split_to_double(char *input, double *arr)
153 {
154         char tmp_arr[255] = {0, };
155         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
156
157         if (!STRING_VALID(input)) {
158                 media_svc_error("Invalid parameter");
159                 return MS_MEDIA_ERR_INVALID_PARAMETER;
160         }
161         memset(tmp_arr, 0x0, sizeof(tmp_arr));
162
163         /*media_svc_debug("input: [%s]", input); */
164
165         len = strlen(input);
166
167         for (idx = 0; idx < (len + 1); idx++) {
168                 if (input[idx] == ' ') {
169                         continue;
170                 } else if ((input[idx] == ',') || (idx == len)) {
171                         arr[arr_idx] = atof(tmp_arr);
172                         arr_idx++;
173                         str_idx = 0;
174                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
175                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
176                 } else {
177                         tmp_arr[str_idx] = input[idx];
178                         str_idx++;
179                 }
180         }
181
182         if (arr_idx != 3) {
183                 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
184                 return MS_MEDIA_ERR_INTERNAL;
185         }
186
187         return MS_MEDIA_ERR_NONE;
188 }
189
190 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
191 {
192         ExifEntry *entry;
193         ExifTag tag;
194
195         if (ed == NULL)
196                 return MS_MEDIA_ERR_INVALID_PARAMETER;
197
198         tag = tagtype;
199
200         entry = exif_data_get_entry(ed, tag);
201         if (entry) {
202                 /* Get the contents of the tag in human-readable form */
203                 if (tag == EXIF_TAG_ORIENTATION ||
204                         tag == EXIF_TAG_PIXEL_X_DIMENSION ||
205                         tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
206                         tag == EXIF_TAG_ISO_SPEED_RATINGS) {
207
208                         if (i_value == NULL) {
209                                 media_svc_debug("i_value is NULL");
210                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
211                         }
212
213                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
214                         short exif_value = exif_get_short(entry->data, mByteOrder);
215                         *i_value = (int)exif_value;
216
217                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
218
219                         if (d_value == NULL) {
220                                 media_svc_debug("d_value is NULL");
221                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
222                         }
223
224                         /* Get the contents of the tag in human-readable form */
225                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
226                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
227                         gps_buf[strlen(gps_buf)] = '\0';
228                         int ret = MS_MEDIA_ERR_NONE;
229
230                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
231
232                         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
233                         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
234
235                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
236                 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
237
238                         if (buf == NULL) {
239                                 media_svc_debug("buf is NULL");
240                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
241                         }
242
243                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
244                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
245                         long numerator = mRational.numerator;
246                         long denominator = mRational.denominator;
247                         snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
248
249                 } else if (tag == EXIF_TAG_FNUMBER) {
250
251                         if (d_value == NULL) {
252                                 media_svc_debug("d_value is NULL");
253                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
254                         }
255
256                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
257                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
258                         long numerator = mRational.numerator;
259                         long denominator = mRational.denominator;
260
261                         *d_value = ((numerator*1.0)/(denominator*1.0));
262
263                 } else {
264
265                         if (buf == NULL) {
266                                 media_svc_debug("buf is NULL");
267                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
268                         }
269
270                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
271                         buf[strlen(buf)] = '\0';
272                 }
273         }
274
275         return MS_MEDIA_ERR_NONE;
276 }
277
278 time_t __media_svc_get_timeline_from_str(const char *timstr)
279 {
280         struct tm t;
281         time_t modified_t = 0;
282         time_t rawtime;
283         struct tm timeinfo;
284
285         if (!STRING_VALID(timstr)) {
286                 media_svc_error("Invalid Parameter");
287                 return 0;
288         }
289
290         /*Exif Format : %Y:%m:%d %H:%M:%S
291         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
292         memset(&t, 0x00, sizeof(struct tm));
293
294         tzset();
295         time(&rawtime);
296         localtime_r(&rawtime, &timeinfo);
297
298         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
299                 t.tm_isdst = timeinfo.tm_isdst;
300                 if (t.tm_isdst != 0)
301                         media_svc_debug("DST %d", t.tm_isdst);
302
303                 /* If time string has timezone */
304                 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
305                         GTimeVal timeval;
306                         char tim_tmp_str[255] = { 0, };
307
308                         /* ISO8601 Time string format */
309                         strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
310                         g_time_val_from_iso8601(tim_tmp_str, &timeval);
311                         modified_t = timeval.tv_sec;
312                         media_svc_debug("Calibrated timeval : [%lu][%s]", modified_t, tim_tmp_str);
313                 } else {
314                         /* Just localtime */
315                         modified_t = mktime(&t);
316                 }
317
318                 if (modified_t > 0)
319                         return modified_t;
320                 else
321                         media_svc_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
322         } else {
323                 media_svc_error("Failed to get timeline : [%s]", timstr);
324         }
325
326         return 0;
327 }
328
329 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
330 {
331         int idx = 0;
332
333         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
334         media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
335         media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
336
337         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
338
339         /*categorize from mimetype */
340         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
341                 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
342                         *category = (*category | content_category[idx].category_by_mime);
343                         break;
344                 }
345         }
346
347         /*in application type, exitst sound file ex) x-smafs, asf */
348         if (*category & MEDIA_SVC_CATEGORY_ETC) {
349                 int prefix_len = strlen(content_category[3].content_type) + 1;
350                 char *ext = NULL;
351
352                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
353                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
354                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
355                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
356                                 break;
357                         }
358                 }
359
360                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
361                         *category ^= MEDIA_SVC_CATEGORY_ETC;
362                         *category |= MEDIA_SVC_CATEGORY_SOUND;
363                 }
364
365                 /*"asf" must check video stream and then categorize in directly. */
366                 ext = strrchr(path, '.');
367                 if (ext != NULL) {
368                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
369                                 int audio = 0;
370                                 int video = 0;
371                                 int err = 0;
372
373                                 err = mm_file_get_stream_info(path, &audio, &video);
374                                 if (err == 0) {
375                                         if (audio > 0 && video == 0) {
376                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
377                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
378                                         } else {
379                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
380                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
381                                         }
382                                 }
383                         }
384                 }
385         }
386
387         /*check music file in sound files. */
388         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
389                 int prefix_len = strlen(content_category[0].content_type) + 1;
390
391                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
392                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
393                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
394                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
395                                 break;
396                         }
397                 }
398
399                 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
400                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
401                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
402                         *category |= MEDIA_SVC_CATEGORY_ETC;
403                 }
404         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
405                 /*some video files don't have video stream. in this case it is categorize as music. */
406                 char *ext = NULL;
407                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
408                 ext = strrchr(path, '.');
409                 if (ext != NULL) {
410                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
411                                 int audio = 0;
412                                 int video = 0;
413                                 int err = 0;
414
415                                 err = mm_file_get_stream_info(path, &audio, &video);
416                                 if (err == 0) {
417                                         if (audio > 0 && video == 0) {
418                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
419                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
420                                         }
421                                 }
422                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
423                         }
424                 }
425         }
426
427         return MS_MEDIA_ERR_NONE;
428 }
429
430 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
431 {
432         int ret = MS_MEDIA_ERR_NONE;
433         int category = 0;
434
435         media_svc_media_type_e type;
436
437         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
438         if (ret != MS_MEDIA_ERR_NONE)
439                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
440
441         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
442         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
443         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
444         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
445         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
446
447         *media_type = type;
448
449         return ret;
450 }
451
452 /*
453 drm_contentifo is not NULL, if the file is OMA DRM.
454 If the file is not OMA DRM, drm_contentinfo must be NULL.
455 */
456 static int __media_svc_get_mime_type(const char *path, char *mimetype)
457 {
458         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
459
460         /*in case of normal files or failure to get mime in drm */
461         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
462                 media_svc_error("aul_get_mime_from_file fail");
463                 return MS_MEDIA_ERR_INTERNAL;
464         }
465
466         return MS_MEDIA_ERR_NONE;
467 }
468
469 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
470 {
471         int i = 0;
472
473         for (i = strlen(file_path); i >= 0; i--) {
474                 if (file_path[i] == '.') {
475                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
476                         return true;
477                 }
478
479                 if (file_path[i] == '/')
480                         return false;
481         }
482         return false;
483 }
484
485 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
486 {
487         char *err_attr_name = NULL;
488         double gps_value = 0.0;
489         int mmf_error = FILEINFO_ERROR_NONE;
490
491         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
492         if (mmf_error == FILEINFO_ERROR_NONE) {
493                 if (longitude != NULL)
494                         *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
495         } else {
496                 SAFE_FREE(err_attr_name);
497         }
498
499         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
500         if (mmf_error == FILEINFO_ERROR_NONE) {
501                 if (latitude != NULL)
502                         *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
503         } else {
504                 SAFE_FREE(err_attr_name);
505         }
506
507         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
508         if (mmf_error == FILEINFO_ERROR_NONE) {
509                 if (altitude != NULL)
510                         *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
511         } else {
512                 SAFE_FREE(err_attr_name);
513         }
514
515         return MS_MEDIA_ERR_NONE;
516 }
517
518 static int __media_svc_resize_artwork(const char *path, const char *img_format)
519 {
520         int ret = MS_MEDIA_ERR_NONE;
521         unsigned int width = 0;
522         unsigned int height = 0;
523         unsigned int resized_width = 0;
524         unsigned int resized_height = 0;
525         mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
526
527         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
528                 media_svc_debug("type [jpeg]");
529
530                 mm_util_extract_image_info(path, &img_type, &width, &height);
531
532                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
533                         media_svc_debug("No need resizing");
534                         return MS_MEDIA_ERR_NONE;
535                 }
536
537                 /* resizing */
538                 if (width > height) {
539                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
540                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
541                 } else {
542                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
543                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
544                 }
545
546                 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
547
548         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
549                 media_svc_debug("type [png]");
550         } else {
551                 media_svc_debug("Not proper img format");
552         }
553
554         return ret;
555 }
556
557 static int __media_svc_safe_atoi(char *buffer, int *si)
558 {
559         char *end = NULL;
560         errno = 0;
561         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
562
563         const long sl = strtol(buffer, &end, 10);
564
565         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
566         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
567         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
568         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
569         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
570
571         *si = (int)sl;
572
573         return MS_MEDIA_ERR_NONE;
574 }
575
576 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
577 {
578         int ret = MS_MEDIA_ERR_NONE;
579
580         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
581
582         if (!image) {
583                 media_svc_error("invalid image..");
584                 return MS_MEDIA_ERR_INVALID_PARAMETER;
585         }
586
587         struct statfs fs;
588         char *thumb_path = NULL;
589         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
590         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
591
592         if (-1 == statfs(thumb_path, &fs)) {
593                 media_svc_error("error in statfs");
594                 SAFE_FREE(thumb_path);
595                 return MS_MEDIA_ERR_INTERNAL;
596         }
597
598         SAFE_FREE(thumb_path);
599
600         long bsize_kbytes = fs.f_bsize >> 10;
601
602         if ((bsize_kbytes * fs.f_bavail) < 1024) {
603                 media_svc_error("not enought space...");
604                 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
605         }
606
607         FILE *fp = NULL;
608         int nwrite = -1;
609         if (image != NULL && size > 0) {
610                 fp = fopen(image_path, "w");
611
612                 if (fp == NULL) {
613                         media_svc_error("failed to open file");
614                         return MS_MEDIA_ERR_INTERNAL;
615                 }
616
617                 nwrite = fwrite(image, 1, size, fp);
618                 if (nwrite != size) {
619                         media_svc_error("failed to write thumbnail");
620                         fclose(fp);
621                         return MS_MEDIA_ERR_INTERNAL;
622                 }
623                 fclose(fp);
624         }
625
626         return MS_MEDIA_ERR_NONE;
627 }
628
629 static char *__media_svc_get_title_from_filepath(const char *path)
630 {
631         char *filename = NULL;
632         char *title = NULL;
633         char    *ext = NULL;
634         int filename_len = -1;
635         int new_title_len = -1;
636
637         if (!path) {
638                 media_svc_error("path is NULL");
639                 return NULL;
640         }
641
642         filename = g_path_get_basename(path);
643         if (!STRING_VALID(filename)) {
644                 media_svc_error("wrong file name");
645                 SAFE_FREE(filename);
646                 return NULL;
647         }
648
649         filename_len = strlen(filename);
650
651         ext = g_strrstr(filename, ".");
652         if (!ext) {
653                 media_svc_error("there is no file extention");
654                 return filename;
655         }
656
657         new_title_len = filename_len - strlen(ext);
658         if (new_title_len < 1) {
659                 media_svc_error("title length is zero");
660                 SAFE_FREE(filename);
661                 return NULL;
662         }
663
664         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
665
666         SAFE_FREE(filename);
667
668         media_svc_debug("extract title is [%s]", title);
669
670         return title;
671 }
672
673 int _media_svc_remove_file(const char *path)
674 {
675         int result = -1;
676
677         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
678
679         result = remove(path);
680         if (result == 0) {
681                 media_svc_debug("success to remove file");
682                 return MS_MEDIA_ERR_NONE;
683         } else {
684                 media_svc_stderror("fail to remove file result");
685                 return MS_MEDIA_ERR_INTERNAL;
686         }
687 }
688
689 int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
690 {
691         int ret = MS_MEDIA_ERR_NONE;
692         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
693         char hash[255 + 1] = {0, };
694         char *thumb_dir = NULL;
695         char *thumbfile_ext = NULL;
696
697         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
698         if (!STRING_VALID(thumb_dir)) {
699                 media_svc_error("ms_user_get_root_thumb_store_path failed");
700                 return MS_MEDIA_ERR_INTERNAL;
701         }
702
703         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
704                 media_svc_error("Wrong path[%s]", thumb_dir);
705                 SAFE_FREE(thumb_dir);
706                 return MS_MEDIA_ERR_INTERNAL;
707         }
708
709         memset(file_ext, 0, sizeof(file_ext));
710         if (!__media_svc_get_file_ext(pathname, file_ext))
711                 media_svc_error("get file ext fail");
712
713         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
714         if (ret != MS_MEDIA_ERR_NONE) {
715                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
716                 SAFE_FREE(thumb_dir);
717                 return MS_MEDIA_ERR_INTERNAL;
718         }
719
720         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
721                 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
722                         thumbfile_ext = (char *)"jpg";
723                 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
724                         thumbfile_ext = (char *)"png";
725                 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
726                         thumbfile_ext = (char *)"gif";
727                 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
728                         thumbfile_ext = (char *)"bmp";
729                 } else {
730                         media_svc_error("Not proper img format");
731                         SAFE_FREE(thumb_dir);
732                         return MS_MEDIA_ERR_INTERNAL;
733                 }
734
735                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
736         } else {
737                 if (strcasecmp(file_ext, "PNG") == 0)
738                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
739                 else
740                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
741         }
742
743         SAFE_FREE(thumb_dir);
744
745         return MS_MEDIA_ERR_NONE;
746 }
747
748 int _media_svc_get_file_time(const char *full_path)
749 {
750         struct stat statbuf;
751         int fd = 0;
752
753         memset(&statbuf, 0, sizeof(struct stat));
754         fd = stat(full_path, &statbuf);
755         if (fd == -1) {
756                 media_svc_sec_error("stat(%s) fails.", full_path);
757                 return MS_MEDIA_ERR_INTERNAL;
758         }
759
760         return statbuf.st_mtime;
761 }
762
763 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
764 {
765         /* Set default GPS value before extracting meta information */
766         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
767         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
768         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
769
770         /* Set filename to title for all media */
771         char *title = NULL;
772         title = __media_svc_get_title_from_filepath(content_info->path);
773         if (title) {
774                 content_info->media_meta.title = g_strdup(title);
775                 SAFE_FREE(title);
776                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
777         } else {
778                 media_svc_error("Can't extract title");
779                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
780                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
781         }
782
783         /* Set default value before extracting meta information */
784         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
785         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
786
787         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
788         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
789
790         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
791         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
792
793         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
794         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
795
796         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
797         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
798
799         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
800         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
801
802         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
803         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
804
805         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
806         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
807
808         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
809         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
810
811         if (refresh) {
812                 media_svc_debug("refresh");
813                 return MS_MEDIA_ERR_NONE;
814         }
815
816         content_info->favourate = 0;
817         content_info->media_meta.rating = 0;
818
819         return MS_MEDIA_ERR_NONE;
820 }
821
822 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
823 {
824         int ret = MS_MEDIA_ERR_NONE;
825         char * media_uuid = NULL;
826         bool drm_type = false;
827         char mime_type[256] = {0, };
828         media_svc_media_type_e media_type;
829
830         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
831         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
832
833         content_info->path = g_strdup(path);
834         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
835
836         struct stat st;
837         memset(&st, 0, sizeof(struct stat));
838         if (stat(path, &st) == 0) {
839                 content_info->modified_time = st.st_mtime;
840                 content_info->timeline = content_info->modified_time;
841                 content_info->size = st.st_size;
842         } else {
843                 media_svc_stderror("stat failed");
844         }
845
846         _media_svc_set_default_value(content_info, refresh);
847
848         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
849         if (refresh) {
850                 media_svc_debug("refresh");
851                 return MS_MEDIA_ERR_NONE;
852         }
853
854         content_info->storage_uuid = g_strdup(storage_id);
855         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
856
857         content_info->storage_type = storage_type;
858         time(&content_info->added_time);
859
860         media_uuid = _media_info_generate_uuid();
861         if (media_uuid == NULL) {
862                 _media_svc_destroy_content_info(content_info);
863                 return MS_MEDIA_ERR_INTERNAL;
864         }
865
866         content_info->media_uuid = g_strdup(media_uuid);
867         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
868
869         content_info->file_name = g_path_get_basename(path);
870         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
871
872         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
873         if drm_contentinfo is not NULL, the file is OMA DRM.*/
874         ret = __media_svc_get_mime_type(path, mime_type);
875         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
876
877         media_svc_debug("mime [%s]", mime_type);
878         content_info->is_drm = drm_type;
879
880         ret = __media_svc_get_media_type(path, mime_type, &media_type);
881         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
882
883         content_info->mime_type = g_strdup(mime_type);
884         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
885
886         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
887
888         content_info->media_type = media_type;
889
890         return MS_MEDIA_ERR_NONE;
891 }
892
893 int image_360_check(char *path)
894 {
895         FILE *fp = NULL;
896         long app1_size = 0;
897         int size = 1;
898         unsigned char exif_header[4] = {0, };
899         unsigned char exif_app1[2] = {0, };
900         unsigned char exif_app1_xmp[2] = {0, };
901         long exif_app1_xmp_size = 0;
902         unsigned char exif_app1_xmp_t[2] = {0, };
903         char *xmp_data = NULL;
904         int size1 = 0;
905         int size2 = 0;
906         int fdata = 0;
907         int temp = 0;
908
909         memset(exif_header, 0x00, sizeof(exif_header));
910         memset(exif_app1, 0x00, sizeof(exif_app1));
911         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
912         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
913
914         fp = fopen(path, "rb");
915         if (fp == NULL)
916                 goto ERROR;
917
918         size = fread(exif_header, 1, sizeof(exif_header), fp);
919         if (size <= 0)
920                 goto ERROR;
921
922         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
923                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
924                 if (size <= 0)
925                         goto ERROR;
926
927                 size1 = exif_app1[0];
928                 size2 = exif_app1[1];
929
930                 app1_size = size1 * 256 + size2 - 2;
931
932                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
933                         goto ERROR;
934
935                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
936                 if (size <= 0)
937                         goto ERROR;
938
939                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
940                         int result = 0;
941                         char *ptr = NULL;
942                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
943                         if (size <= 0)
944                                 goto ERROR;
945
946                         size1 = exif_app1_xmp_t[0];
947                         size2 = exif_app1_xmp_t[1];
948
949                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
950
951                         if (exif_app1_xmp_size > 0) {
952                                 xmp_data = (char *)malloc(exif_app1_xmp_size);
953                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
954
955                                 ptr = xmp_data;
956
957                                 while (exif_app1_xmp_size >= 0) {
958                                         exif_app1_xmp_size--;
959                                         fdata = fgetc(fp);
960                                         if (fdata == EOF)
961                                                 continue;
962                                         if (fdata == '\0')
963                                                 continue;
964                                         *ptr = (char)fdata;
965                                         ptr++;
966                                         temp++;
967                                 }
968                                 ptr = ptr - temp;
969
970                                 if (strstr(ptr, "UsePanoramaViewer")
971                                 && strstr(ptr, "True")
972                                 && strstr(ptr, "ProjectionType")
973                                 && strstr(ptr, "equirectangular"))
974                                         result = 1;
975
976                                 SAFE_FREE(xmp_data);
977                         } else {
978                                 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
979                         }
980
981                         if (fp) {
982                                 fclose(fp);
983                                 fp = NULL;
984                         }
985                         return result;
986                 } else {
987                         goto ERROR;
988                 }
989         } else {
990                 goto ERROR;
991         }
992 ERROR:
993         if (fp) {
994                 fclose(fp);
995                 fp = NULL;
996         }
997         return 0;
998 }
999
1000 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
1001 {
1002         double value = 0.0;
1003         int orient_value = 0;
1004         int exif_width = 0;
1005         int exif_height = 0;
1006         ExifData *ed = NULL;
1007         bool has_datetaken = false;
1008         double fnumber = 0.0;
1009         int iso = 0;
1010         char *path = NULL;
1011
1012         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1013
1014         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
1015         media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
1016         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1017
1018         path = content_info->path;
1019
1020         /* Load an ExifData object from an EXIF file */
1021         ed = exif_data_new_from_file(path);
1022
1023         if (!ed) {
1024                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1025                 goto GET_WIDTH_HEIGHT;
1026         }
1027
1028         content_info->media_meta.is_360 = image_360_check(path);
1029
1030         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1031         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1032         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1033         content_info->media_meta.fnumber = 0.0;
1034         content_info->media_meta.iso = 0;
1035         content_info->media_meta.orientation = 0;
1036         content_info->media_meta.width = 0;
1037         content_info->media_meta.height = 0;
1038
1039         memset(buf, 0x00, sizeof(buf));
1040         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1041                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1042                         if (strlen(buf) > 0) {
1043                                 if (strcmp(buf, "S") == 0)
1044                                         value = -1 * value;
1045                         }
1046                         content_info->media_meta.latitude = value;
1047                 }
1048         }
1049
1050         memset(buf, 0x00, sizeof(buf));
1051         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1052                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1053                         if (strlen(buf) > 0) {
1054                                 if (strcmp(buf, "W") == 0)
1055                                         value = -1 * value;
1056                         }
1057                         content_info->media_meta.longitude = value;
1058                 }
1059         }
1060
1061         memset(buf, 0x00, sizeof(buf));
1062         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1063                 if (strlen(buf) > 0) {
1064                         SAFE_FREE(content_info->media_meta.description);
1065                         content_info->media_meta.description = g_strdup(buf);
1066                 }
1067         }
1068
1069         memset(buf, 0x00, sizeof(buf));
1070         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1071                 if (strlen(buf) > 0) {
1072                         has_datetaken = true;
1073                         content_info->media_meta.datetaken = g_strdup(buf);
1074
1075                         /* This is same as recorded_date */
1076                         content_info->media_meta.recorded_date = g_strdup(buf);
1077                 }
1078         }
1079
1080         memset(buf, 0x00, sizeof(buf));
1081         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1082                 if (strlen(buf) > 0) {
1083                         has_datetaken = true;
1084                         content_info->media_meta.datetaken = g_strdup(buf);
1085
1086                         /* This is same as recorded_date */
1087                         content_info->media_meta.recorded_date =  g_strdup(buf);
1088                 }
1089         }
1090
1091         if (has_datetaken) {
1092                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1093                 if (content_info->timeline == 0)
1094                         content_info->timeline = content_info->modified_time;
1095                 else
1096                         media_svc_debug("Timeline : %ld", content_info->timeline);
1097         }
1098
1099         memset(buf, 0x00, sizeof(buf));
1100         /* Get exposure_time value from exif. */
1101         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1102                 if (strlen(buf) > 0)
1103                         content_info->media_meta.exposure_time = g_strdup(buf);
1104         }
1105
1106         /* Get fnumber value from exif. */
1107         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1108                 if (fnumber > 0.0)
1109                         content_info->media_meta.fnumber = fnumber;
1110         }
1111
1112         /* Get iso value from exif. */
1113         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1114                 if (iso > 0)
1115                         content_info->media_meta.iso = iso;
1116         }
1117
1118         memset(buf, 0x00, sizeof(buf));
1119         /* Get model value from exif. */
1120         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1121                 if (strlen(buf) > 0)
1122                         content_info->media_meta.model = g_strdup(buf);
1123         }
1124
1125         /* Get orientation value from exif. */
1126         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1127                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1128                         content_info->media_meta.orientation = orient_value;
1129         }
1130
1131         /* Get width value from exif. */
1132         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1133                 if (exif_width > 0)
1134                         content_info->media_meta.width = exif_width;
1135         }
1136
1137         /* Get height value from exif. */
1138         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1139                 if (exif_height > 0)
1140                         content_info->media_meta.height = exif_height;
1141         }
1142
1143         if (ed != NULL) exif_data_unref(ed);
1144
1145 GET_WIDTH_HEIGHT:
1146
1147         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1148                 /*Get image width, height*/
1149                 unsigned int img_width = 0;
1150                 unsigned int img_height = 0;
1151                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1152
1153                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1154                 if (content_info->media_meta.width == 0)
1155                         content_info->media_meta.width = img_width;
1156
1157                 if (content_info->media_meta.height == 0)
1158                         content_info->media_meta.height = img_height;
1159         }
1160
1161         return MS_MEDIA_ERR_NONE;
1162 }
1163
1164 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1165 {
1166         MMHandleType tag = 0;
1167         char *p = NULL;
1168         int size = -1;
1169         int mmf_error = FILEINFO_ERROR_NONE;
1170         char *err_attr_name = NULL;
1171
1172         content_info->path = g_strdup(path);
1173
1174         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1175         if (mmf_error != FILEINFO_ERROR_NONE)
1176                 return MS_MEDIA_ERR_INTERNAL;
1177
1178         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1179         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1180                 content_info->media_meta.album = g_strdup(p);
1181         else
1182                 SAFE_FREE(err_attr_name);
1183
1184         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1185         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1186                 content_info->media_meta.artist = g_strdup(p);
1187         else
1188                 SAFE_FREE(err_attr_name);
1189
1190         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1191         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1192                 content_info->media_meta.album_artist = g_strdup(p);
1193         else
1194                 SAFE_FREE(err_attr_name);
1195
1196         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1197         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1198                 content_info->media_meta.genre = g_strdup(p);
1199         else
1200                 SAFE_FREE(err_attr_name);
1201
1202         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1203         if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1204                 if (!isspace(*p)) {
1205                         content_info->media_meta.title = g_strdup(p);
1206                 } else {
1207                         int idx = 0;
1208
1209                         for (idx = 0; idx < size; idx++) {
1210                                 if (isspace(*p)) {
1211                                         media_svc_debug("SPACE [%s]", p);
1212                                         p++;
1213                                         continue;
1214                                 } else {
1215                                         media_svc_debug("Not SPACE [%s]", p);
1216                                         content_info->media_meta.title = g_strdup(p);
1217                                         break;
1218                                 }
1219                         }
1220                 }
1221         }
1222
1223         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1224         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1225                 content_info->media_meta.description = g_strdup(p);
1226         else
1227                 SAFE_FREE(err_attr_name);
1228
1229         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1230         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1231                 content_info->media_meta.composer = g_strdup(p);
1232         else
1233                 SAFE_FREE(err_attr_name);
1234
1235         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1236         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1237                 content_info->media_meta.copyright = g_strdup(p);
1238         else
1239                 SAFE_FREE(err_attr_name);
1240
1241         mmf_error = mm_file_destroy_tag_attrs(tag);
1242         if (mmf_error != FILEINFO_ERROR_NONE)
1243                 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1244
1245         return MS_MEDIA_ERR_NONE;
1246 }
1247
1248 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1249 {
1250         MMHandleType content = 0;
1251         MMHandleType tag = 0;
1252         char *p = NULL;
1253         unsigned char *image = NULL;
1254         unsigned int size = 0;
1255         int mmf_error = FILEINFO_ERROR_NONE;
1256         char *err_attr_name = NULL;
1257         int album_id = 0;
1258         int ret = MS_MEDIA_ERR_NONE;
1259         int cdis_value = 0;
1260
1261         /*Get Content Tag attribute ===========*/
1262         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1263
1264         if (mmf_error == FILEINFO_ERROR_NONE) {
1265                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1266                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1267                         content_info->media_meta.album = g_strdup(p);
1268                 else
1269                         SAFE_FREE(err_attr_name);
1270
1271                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1272                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1273                         content_info->media_meta.artist = g_strdup(p);
1274                 else
1275                         SAFE_FREE(err_attr_name);
1276
1277                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1278                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1279                         content_info->media_meta.album_artist = g_strdup(p);
1280                 else
1281                         SAFE_FREE(err_attr_name);
1282
1283                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1284                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1285                         content_info->media_meta.genre = g_strdup(p);
1286                 else
1287                         SAFE_FREE(err_attr_name);
1288
1289                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1290                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1291                         if (!isspace(*p)) {
1292                                 content_info->media_meta.title = g_strdup(p);
1293                         } else {
1294                                 int idx = 0;
1295
1296                                 for (idx = 0; idx < size; idx++) {
1297                                         if (isspace(*p)) {
1298                                                 media_svc_debug("SPACE [%s]", p);
1299                                                 p++;
1300                                                 continue;
1301                                         } else {
1302                                                 media_svc_debug("Not SPACE [%s]", p);
1303                                                 content_info->media_meta.title = g_strdup(p);
1304                                                 break;
1305                                         }
1306                                 }
1307                         }
1308                 }
1309
1310                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1311                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1312                         content_info->media_meta.description = g_strdup(p);
1313                 else
1314                         SAFE_FREE(err_attr_name);
1315
1316                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1317                 if (mmf_error != FILEINFO_ERROR_NONE)
1318                         SAFE_FREE(err_attr_name);
1319
1320                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1321                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1322                         char mime_type[255] = {0, };
1323                         ret = __media_svc_get_mime_type(content_info->path, mime_type);
1324                         /*if 3gp that audio only, media_type is music */
1325                         if ((ret == MS_MEDIA_ERR_NONE) &&
1326                                 ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/mp4") == 0) ||
1327                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/3gpp") == 0) ||
1328                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "video/3gpp") == 0) ||
1329                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "audio/mp4") == 0))) {
1330                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1331                                 char *time_info = g_strdup_printf("0000:00:00 00:00:00 +0000");
1332                                 char *p_value = p;
1333                                 char *time_value = time_info;
1334                                 if (time_info != NULL) {
1335                                         while (*p_value != '\0') {
1336                                                 if (*p_value == '-')
1337                                                         *time_value = ':';
1338                                                 else
1339                                                         *time_value = *p_value;
1340                                                 time_value++;
1341                                                 p_value++;
1342                                         }
1343                                         content_info->media_meta.recorded_date = g_strdup(time_info);
1344                                         SAFE_FREE(time_info);
1345                                 } else {
1346                                         media_svc_error("memory allocation error");
1347                                 }
1348                         } else {
1349                                 content_info->media_meta.recorded_date = g_strdup(p);
1350                         }
1351
1352                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1353                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1354                                 if (content_info->timeline == 0)
1355                                         content_info->timeline = content_info->modified_time;
1356                                 else
1357                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1358
1359                                 /* This is same as datetaken */
1360                                 /* Remove compensation string */
1361                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1362                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1363                                         g_free(content_info->media_meta.recorded_date);
1364                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1365                                 } else {
1366                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1367                                 }
1368                         }
1369                 } else {
1370                         SAFE_FREE(err_attr_name);
1371                 }
1372
1373                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1374                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1375                         content_info->media_meta.composer = g_strdup(p);
1376                 else
1377                         SAFE_FREE(err_attr_name);
1378
1379                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1380                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1381                         content_info->media_meta.copyright = g_strdup(p);
1382                 else
1383                         SAFE_FREE(err_attr_name);
1384
1385                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1386                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1387                         content_info->media_meta.track_num = g_strdup(p);
1388                 else
1389                         SAFE_FREE(err_attr_name);
1390
1391                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1392                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1393                         int year = 0;
1394                         if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1395                                 content_info->media_meta.year = g_strdup(p);
1396                         else
1397                                 media_svc_debug("Wrong Year Information [%s]", p);
1398                 } else {
1399                         SAFE_FREE(err_attr_name);
1400                 }
1401
1402                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1403                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1404                         int rate = 0;
1405                         if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1406                                 content_info->media_meta.rating = rate;
1407                 } else {
1408                         SAFE_FREE(err_attr_name);
1409                         content_info->media_meta.rating = 0;
1410                 }
1411
1412                 /*Do not extract artwork for the USB Storage content*/
1413                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1414                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1415                         if (mmf_error != FILEINFO_ERROR_NONE) {
1416                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1417                                 SAFE_FREE(err_attr_name);
1418                         } else {
1419                                 /*media_svc_debug("artwork size1 [%d]", size); */
1420                         }
1421
1422                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1423                         if (mmf_error != FILEINFO_ERROR_NONE) {
1424                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1425                                 SAFE_FREE(err_attr_name);
1426                         } else {
1427                                 /*media_svc_debug("artwork size2 [%d]", size); */
1428                         }
1429
1430                         if (image != NULL && size > 0) {
1431                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1432                                 int artwork_mime_size = -1;
1433
1434                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1435                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1436                                         ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1437                                         if (ret != MS_MEDIA_ERR_NONE)
1438                                                 media_svc_error("Fail to Get Thumbnail Path");
1439
1440                                         if (strlen(thumb_path) > 0) {
1441                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1442                                                 if (ret != MS_MEDIA_ERR_NONE) {
1443                                                         media_svc_error("Fail to Save Image");
1444                                                 } else {
1445                                                         /* albumart resizing */
1446                                                         ret = __media_svc_resize_artwork(thumb_path, p);
1447                                                         if (ret != MS_MEDIA_ERR_NONE) {
1448                                                                 media_svc_error("Fail to Make Thumbnail Image");
1449                                                                 _media_svc_remove_file(thumb_path);
1450
1451                                                         } else {
1452                                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1453                                                         }
1454                                                 }
1455                                         }
1456                                 } else {
1457                                         SAFE_FREE(err_attr_name);
1458                                 }
1459                         }
1460                 }
1461
1462                 /*Initialize album_id to 0. below code will set the album_id*/
1463                 content_info->album_id = album_id;
1464                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1465                 if (ret != MS_MEDIA_ERR_NONE) {
1466                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1467                                 media_svc_debug("album does not exist. So start to make album art");
1468                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1469                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1470                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1471                                 else
1472                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1473
1474                                 content_info->album_id = album_id;
1475                         }
1476                 } else {
1477                         content_info->album_id = album_id;
1478                 }
1479
1480                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1481                         double longitude = 0.0;
1482                         double latitude = 0.0;
1483                         double altitude = 0.0;
1484
1485                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1486                         content_info->media_meta.longitude = longitude;
1487                         content_info->media_meta.latitude = latitude;
1488                         content_info->media_meta.altitude = altitude;
1489
1490                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1491                         if (mmf_error != FILEINFO_ERROR_NONE) {
1492                                 cdis_value = 0;
1493                                 SAFE_FREE(err_attr_name);
1494                         }
1495
1496                         media_svc_debug("CDIS : %d", cdis_value);
1497
1498                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1499                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1500                                 content_info->media_meta.orientation = atoi(p);
1501                         } else {
1502                                 SAFE_FREE(err_attr_name);
1503                                 content_info->media_meta.orientation = 0;
1504                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1505                         }
1506                 }
1507
1508                 mmf_error = mm_file_destroy_tag_attrs(tag);
1509                 if (mmf_error != FILEINFO_ERROR_NONE)
1510                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1511         }       else {
1512                 content_info->album_id = album_id;
1513         }
1514
1515         /*Get Content attribute ===========*/
1516         if (cdis_value == 1)
1517                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1518         else
1519                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1520
1521         if (mmf_error == FILEINFO_ERROR_NONE) {
1522                 /*Common attribute*/
1523                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1524                 if (mmf_error != FILEINFO_ERROR_NONE) {
1525                         SAFE_FREE(err_attr_name);
1526                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1527                 } else {
1528                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1529                 }
1530
1531                 /*Sound/Music attribute*/
1532                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1533
1534                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1535                         if (mmf_error != FILEINFO_ERROR_NONE) {
1536                                 SAFE_FREE(err_attr_name);
1537                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1538                         } else {
1539                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1540                         }
1541
1542                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1543                         if (mmf_error != FILEINFO_ERROR_NONE) {
1544                                 SAFE_FREE(err_attr_name);
1545                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1546                         } else {
1547                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1548                         }
1549
1550                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1551                         if (mmf_error != FILEINFO_ERROR_NONE) {
1552                                 SAFE_FREE(err_attr_name);
1553                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1554                         } else {
1555                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1556                         }
1557
1558                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1559                         if (mmf_error != FILEINFO_ERROR_NONE) {
1560                                 SAFE_FREE(err_attr_name);
1561                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1562                         } else {
1563                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1564                         }
1565                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1566                         int audio_bitrate = 0;
1567                         int video_bitrate = 0;
1568
1569                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1570                         if (mmf_error != FILEINFO_ERROR_NONE) {
1571                                 SAFE_FREE(err_attr_name);
1572                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1573                         } else {
1574                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1575                         }
1576
1577                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1578                         if (mmf_error != FILEINFO_ERROR_NONE) {
1579                                 SAFE_FREE(err_attr_name);
1580                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1581                         } else {
1582                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1583                         }
1584
1585                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1586
1587                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1588                         if (mmf_error != FILEINFO_ERROR_NONE) {
1589                                 SAFE_FREE(err_attr_name);
1590                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1591                         } else {
1592                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1593                         }
1594
1595                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1596                         if (mmf_error != FILEINFO_ERROR_NONE) {
1597                                 SAFE_FREE(err_attr_name);
1598                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1599                         } else {
1600                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1601                         }
1602                 } else {
1603                         media_svc_error("Not support type");
1604                         mmf_error = mm_file_destroy_content_attrs(content);
1605                         if (mmf_error != FILEINFO_ERROR_NONE)
1606                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1607
1608                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1609                 }
1610
1611                 mmf_error = mm_file_destroy_content_attrs(content);
1612                 if (mmf_error != FILEINFO_ERROR_NONE)
1613                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1614         } else {
1615                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1616         }
1617
1618         return MS_MEDIA_ERR_NONE;
1619 }
1620
1621 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1622 {
1623         media_svc_retm_if(content_info == NULL, "content info is NULL");
1624
1625         /* Delete media_svc_content_info_s */
1626         SAFE_FREE(content_info->media_uuid);
1627         SAFE_FREE(content_info->path);
1628         SAFE_FREE(content_info->file_name);
1629         SAFE_FREE(content_info->mime_type);
1630         SAFE_FREE(content_info->folder_uuid);
1631         SAFE_FREE(content_info->thumbnail_path);
1632         SAFE_FREE(content_info->storage_uuid);
1633
1634         /* Delete media_svc_content_meta_s */
1635         SAFE_FREE(content_info->media_meta.title);
1636         SAFE_FREE(content_info->media_meta.album);
1637         SAFE_FREE(content_info->media_meta.artist);
1638         SAFE_FREE(content_info->media_meta.album_artist);
1639         SAFE_FREE(content_info->media_meta.genre);
1640         SAFE_FREE(content_info->media_meta.composer);
1641         SAFE_FREE(content_info->media_meta.year);
1642         SAFE_FREE(content_info->media_meta.recorded_date);
1643         SAFE_FREE(content_info->media_meta.copyright);
1644         SAFE_FREE(content_info->media_meta.track_num);
1645         SAFE_FREE(content_info->media_meta.description);
1646         SAFE_FREE(content_info->media_meta.datetaken);
1647         SAFE_FREE(content_info->media_meta.exposure_time);
1648         SAFE_FREE(content_info->media_meta.model);
1649
1650         SAFE_FREE(content_info->media_meta.title_pinyin);
1651         SAFE_FREE(content_info->media_meta.album_pinyin);
1652         SAFE_FREE(content_info->media_meta.artist_pinyin);
1653         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1654         SAFE_FREE(content_info->media_meta.genre_pinyin);
1655         SAFE_FREE(content_info->media_meta.composer_pinyin);
1656         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1657         SAFE_FREE(content_info->media_meta.description_pinyin);
1658
1659         return;
1660 }
1661
1662 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1663 {
1664         bool portrait = false;
1665         double ratio = 0.0;
1666
1667         media_svc_retm_if(!orig_w, "Invalid orig_w");
1668         media_svc_retm_if(!orig_h, "Invalid orig_h");
1669         media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1670         media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1671
1672         if (orig_w < orig_h)
1673                 portrait = true;
1674
1675         /* Set smaller length to default size */
1676         if (portrait) {
1677                 if (orig_w < *thumb_w)
1678                         *thumb_w = orig_w;
1679                 ratio = (double)orig_h / (double)orig_w;
1680                 *thumb_h = *thumb_w * ratio;
1681         } else {
1682                 if (orig_h < *thumb_h)
1683                         *thumb_h = orig_h;
1684                 ratio = (double)orig_w / (double)orig_h;
1685                 *thumb_w = *thumb_h * ratio;
1686         }
1687
1688         media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1689
1690         return;
1691 }
1692
1693 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1694 {
1695         int err = MS_MEDIA_ERR_NONE;
1696         MMHandleType tag = (MMHandleType) NULL;
1697         char *p = NULL;
1698         int size = 0;
1699         char *err_msg = NULL;
1700         int _cdis_value = 0;
1701         mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1702
1703         /* Get Content Tag attribute for orientation */
1704         err = mm_file_create_tag_attrs(&tag, origin_path);
1705         if (err != FILEINFO_ERROR_NONE) {
1706                 *rot_type = MM_UTIL_ROTATE_0;
1707                 *cdis_value = 0;
1708                 return;
1709         }
1710
1711         err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1712         if (err == FILEINFO_ERROR_NONE && size >= 0) {
1713                 if (p == NULL) {
1714                         _rot_type = MM_UTIL_ROTATE_0;
1715                 } else {
1716                         if (strncmp(p, "90", size) == 0)
1717                                 _rot_type = MM_UTIL_ROTATE_90;
1718                         else if (strncmp(p, "180", size) == 0)
1719                                 _rot_type = MM_UTIL_ROTATE_180;
1720                         else if (strncmp(p, "270", size) == 0)
1721                                 _rot_type = MM_UTIL_ROTATE_270;
1722                         else
1723                                 _rot_type = MM_UTIL_ROTATE_0;
1724                 }
1725                 media_svc_debug("There is tag rotate : %d", _rot_type);
1726         } else {
1727                 media_svc_debug("There is NOT tag rotate");
1728                 _rot_type = MM_UTIL_ROTATE_0;
1729                 SAFE_FREE(err_msg);
1730         }
1731
1732         err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1733         if (err != FILEINFO_ERROR_NONE) {
1734                 _cdis_value = 0;
1735                 SAFE_FREE(err_msg);
1736         }
1737
1738         *rot_type = _rot_type;
1739         *cdis_value = _cdis_value;
1740
1741         err = mm_file_destroy_tag_attrs(tag);
1742         if (err != FILEINFO_ERROR_NONE) {
1743                 media_svc_error("fail to free tag attr - err(%x)", err);
1744         }
1745
1746         return;
1747 }
1748
1749 static int __get_video_info(int cdis_value, const char *origin_path, int *video_track_num, unsigned int *width, unsigned int *height, void **frame, size_t *size)
1750 {
1751         int err = MS_MEDIA_ERR_NONE;
1752         MMHandleType content = (MMHandleType) NULL;
1753         char *err_msg = NULL;
1754         int _video_track_num = 0;
1755         int _width = 0;
1756         int _height = 0;
1757         size_t _size = 0;
1758         void *_frame = NULL;
1759
1760         if (cdis_value == 1) {
1761                 media_svc_debug("This is CDIS vlaue 1");
1762                 err = mm_file_create_content_attrs_safe(&content, origin_path);
1763         } else {
1764                 err = mm_file_create_content_attrs(&content, origin_path);
1765         }
1766
1767         if (err != FILEINFO_ERROR_NONE) {
1768                 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1769                 return MS_MEDIA_ERR_INTERNAL;
1770         }
1771
1772         err = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1773         if (err != FILEINFO_ERROR_NONE) {
1774                 media_svc_error("mm_file_get_attrs fails : %s", err_msg);
1775                 SAFE_FREE(err_msg);
1776                 mm_file_destroy_content_attrs(content);
1777                 return MS_MEDIA_ERR_INTERNAL;
1778         }
1779
1780         *video_track_num = _video_track_num;
1781
1782         if (_video_track_num == 0) {
1783                 mm_file_destroy_content_attrs(content);
1784                 return MS_MEDIA_ERR_NONE;
1785         }
1786
1787         err = mm_file_get_attrs(content, &err_msg,
1788                                 MM_FILE_CONTENT_VIDEO_WIDTH,
1789                                 &_width,
1790                                 MM_FILE_CONTENT_VIDEO_HEIGHT,
1791                                 &_height,
1792                                 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1793                                 &_size, NULL);
1794
1795         if (err != FILEINFO_ERROR_NONE) {
1796                 media_svc_error("mm_file_get_attrs fails : %s", err_msg);
1797                 SAFE_FREE(err_msg);
1798                 mm_file_destroy_content_attrs(content);
1799                 return MS_MEDIA_ERR_INTERNAL;
1800         }
1801
1802         media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1803         if (!_frame || !_width || !_height) {
1804                 mm_file_destroy_content_attrs(content);
1805                 return MS_MEDIA_ERR_INTERNAL;
1806         }
1807
1808
1809         *width = _width;
1810         *height = _height;
1811         *size = _size;
1812         *frame = calloc(1, _size);
1813         memcpy(*frame, _frame, _size);
1814
1815         mm_file_destroy_content_attrs(content);
1816
1817         return MS_MEDIA_ERR_NONE;
1818 }
1819
1820 static int __get_video_thumb(int width, int height, void *frame, size_t size, mm_util_magick_rotate_type rot_type, const char *thumb_path, unsigned int thumb_width, unsigned int thumb_height)
1821 {
1822         int err = MS_MEDIA_ERR_NONE;
1823         mm_util_image_h img = NULL;
1824         mm_util_image_h resize_img = NULL;
1825
1826         media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1827         __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1828         if (thumb_width <= 0 || thumb_height <= 0) {
1829                 media_svc_error("Failed to get thumb size");
1830                 return MS_MEDIA_ERR_INTERNAL;
1831         }
1832
1833         media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1834
1835         err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1836         media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1837
1838         if (width > thumb_width || height > thumb_height) {
1839                 if (rot_type != MM_UTIL_ROTATE_0) {
1840                         err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1841                         if (err != MM_UTIL_ERROR_NONE)
1842                                 goto ERROR;
1843
1844                         err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1845                 } else {
1846                         err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1847                 }
1848         } else {
1849                 if (rot_type != MM_UTIL_ROTATE_0)
1850                         err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1851                 else
1852                         err = mm_util_resize_B_P(img, width, height, thumb_path);
1853         }
1854
1855 ERROR:
1856         mm_image_destroy_image(img);
1857         mm_image_destroy_image(resize_img);
1858         if (err != MS_MEDIA_ERR_NONE)
1859                 return MS_MEDIA_ERR_INTERNAL;
1860
1861         return MS_MEDIA_ERR_NONE;
1862 }
1863
1864 static int __create_video_thumbnail(const char *path, char *thumb_path)
1865 {
1866         int ret = MS_MEDIA_ERR_NONE;
1867         int cdis_value = 0;
1868         unsigned int width = 0;
1869         unsigned int height = 0;
1870         void *frame = NULL;
1871         int video_track_num = 0;
1872         size_t size = 0;
1873         mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1874
1875         __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1876         ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1877         media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1878         media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1879
1880         ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1881
1882         SAFE_FREE(frame);
1883
1884         return ret;
1885 }
1886
1887 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1888 {
1889         int ret = MS_MEDIA_ERR_NONE;
1890         unsigned int origin_w = 0;
1891         unsigned int origin_h = 0;
1892         unsigned int thumb_w = THUMB_WIDTH;
1893         unsigned int thumb_h = THUMB_HEIGHT;
1894         mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1895
1896         if (path == NULL || thumb_path == NULL) {
1897                 media_svc_error("Invalid parameter");
1898                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1899         }
1900
1901         if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
1902                         media_svc_error("Original path(%s) doesn't exist.", path);
1903                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1904         }
1905
1906         ms_user_storage_type_e store_type = -1;
1907         ret = ms_user_get_storage_type(uid, path, &store_type);
1908
1909         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1910                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1911                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1912         }
1913
1914         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1915
1916         //1. make hash path
1917         ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, THUMB_EXT, uid);
1918         if (ret != MS_MEDIA_ERR_NONE) {
1919                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1920                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1921                 return ret;
1922         }
1923
1924         //2. save thumbnail
1925         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1926                 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1927                 if (ret != MS_MEDIA_ERR_NONE) {
1928                         media_svc_error("Getting image info is failed err: %d", ret);
1929                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1930                         return MS_MEDIA_ERR_INTERNAL;
1931                 }
1932
1933                 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1934                         media_svc_error("Unsupported image codec");
1935                         return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1936                 }
1937
1938                 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1939                         media_svc_error("This original image is too big");
1940                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1941                         return MS_MEDIA_ERR_THUMB_TOO_BIG;
1942                 }
1943
1944                 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1945                 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1946                 if (ret != MM_UTIL_ERROR_NONE) {
1947                         media_svc_error("mm_util_resize_P_P err: %d", ret);
1948                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1949                         return MS_MEDIA_ERR_INTERNAL;
1950                 }
1951         } else {
1952                 return __create_video_thumbnail(path, thumb_path);
1953         }
1954
1955         return ret;
1956 }
1957
1958 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1959 {
1960         int ret = MS_MEDIA_ERR_NONE;
1961         int size = 0;
1962         pinyin_name_s *pinyinname = NULL;
1963
1964         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1965         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1966
1967         *pinyin_str = NULL;
1968
1969         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1970         if (ret == MS_MEDIA_ERR_NONE) {
1971                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1972                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1973                 else
1974                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
1975         }
1976
1977         _media_svc_pinyin_free(pinyinname, size);
1978
1979         return ret;
1980 }
1981
1982 bool _media_svc_check_pinyin_support(void)
1983 {
1984         int ret = SYSTEM_INFO_ERROR_NONE;
1985         bool is_supported = false;
1986
1987         if (media_svc_pinyin_support == -1) {
1988                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1989                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1990                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1991                         return false;
1992                 }
1993
1994                 media_svc_pinyin_support = is_supported;
1995         }
1996
1997         return media_svc_pinyin_support;
1998 }
1999
2000 int _media_svc_get_media_type(const char *path, int *mediatype)
2001 {
2002         int ret = MS_MEDIA_ERR_NONE;
2003         char mime_type[256] = {0};
2004         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
2005
2006         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
2007
2008         ret = __media_svc_get_mime_type(path, mime_type);
2009         if (ret == MS_MEDIA_ERR_NONE)
2010                 __media_svc_get_media_type(path, mime_type, &media_type);
2011         else
2012                 media_svc_error("__media_svc_get_mime_type failed");
2013
2014         *mediatype = media_type;
2015
2016         return ret;
2017 }
2018
2019 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
2020 {
2021         if ((storage_type != MS_USER_STORAGE_INTERNAL)
2022                 && (storage_type != MS_USER_STORAGE_EXTERNAL)
2023                 && (storage_type != MS_USER_STORAGE_EXTERNAL_USB)) {
2024                 media_svc_error("storage type is incorrect[%d]", storage_type);
2025                 return false;
2026         }
2027
2028         return true;
2029 }