4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <glib/gstdio.h>
34 #include <libexif/exif-data.h>
35 #include <media-thumbnail.h>
36 #include <media-util.h>
37 #include <uuid/uuid.h>
38 #include <img-codec-parser.h>
39 #include <image_util.h>
42 #include "media-util-err.h"
43 #include "media-svc-util.h"
44 #include "media-svc-db-utils.h"
45 #include "media-svc-debug.h"
46 #include "media-svc-env.h"
47 #include "media-svc-hash.h"
48 #include "media-svc-album.h"
49 #include "media-svc-localize-utils.h"
50 #include "media-svc-localize_ch.h"
51 #include "media-svc-localize_tw.h"
53 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
55 /* Define data structures for media type and mime type */
56 #define MEDIA_SVC_CATEGORY_UNKNOWN 0x00000000 /**< Default */
57 #define MEDIA_SVC_CATEGORY_ETC 0x00000001 /**< ETC category */
58 #define MEDIA_SVC_CATEGORY_IMAGE 0x00000002 /**< Image category */
59 #define MEDIA_SVC_CATEGORY_VIDEO 0x00000004 /**< Video category */
60 #define MEDIA_SVC_CATEGORY_MUSIC 0x00000008 /**< Music category */
61 #define MEDIA_SVC_CATEGORY_SOUND 0x00000010 /**< Sound category */
63 #define CONTENT_TYPE_NUM 5
64 #define MUSIC_MIME_NUM 29
65 #define SOUND_MIME_NUM 1
66 #define MIME_TYPE_LENGTH 255
67 #define MIME_LENGTH 50
68 #define _3GP_FILE ".3gp"
69 #define _MP4_FILE ".mp4"
70 #define _ASF_FILE ".asf"
71 #define MEDIA_SVC_INI_GET_INT(dict, key, value, default) \
73 value = iniparser_getint(dict, key, default); \
74 media_svc_debug("get %s = %d", key, value); \
76 #define MEDIA_SVC_INI_DEFAULT_PATH "/usr/etc/media_content_config.ini"
77 #define MEDIA_SVC_ARTWORK_SIZE 2000
79 static int g_ini_value = -1;
82 char content_type[15];
84 } _media_svc_content_table_s;
86 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
87 {"audio", MEDIA_SVC_CATEGORY_SOUND},
88 {"image", MEDIA_SVC_CATEGORY_IMAGE},
89 {"video", MEDIA_SVC_CATEGORY_VIDEO},
90 {"application", MEDIA_SVC_CATEGORY_ETC},
91 {"text", MEDIA_SVC_CATEGORY_ETC},
94 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
95 /*known mime types of normal files*/
101 /* known mime types of drm files*/
103 "x-mp3", /*alias of audio/mpeg*/
104 "x-mpeg", /*alias of audio/mpeg*/
106 "x-ogg", /*alias of audio/ogg*/
107 "vnd.ms-playready.media.pya:*.pya", /*playready*/
110 "x-m4a", /*alias of audio/mp4*/
111 /* below mimes are rare*/
121 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
122 "vorbis", /*alias of audio/x-vorbis+ogg*/
124 "x-mp2", /*alias of audio/mp2*/
125 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
126 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
130 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
135 MEDIA_SVC_EXTRACTED_FIELD_NONE = 0x00000001,
136 MEDIA_SVC_EXTRACTED_FIELD_TITLE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
137 MEDIA_SVC_EXTRACTED_FIELD_DESC = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
138 MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
139 MEDIA_SVC_EXTRACTED_FIELD_AUTHOR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
140 MEDIA_SVC_EXTRACTED_FIELD_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
141 MEDIA_SVC_EXTRACTED_FIELD_GENRE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
142 MEDIA_SVC_EXTRACTED_FIELD_ALBUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
143 MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
144 MEDIA_SVC_EXTRACTED_FIELD_YEAR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
145 MEDIA_SVC_EXTRACTED_FIELD_CATEGORY = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
146 MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
147 } media_svc_extracted_field_e;
149 char *_media_info_generate_uuid(void)
152 static char uuid_unparsed[37];
155 uuid_generate(uuid_value);
156 uuid_unparse(uuid_value, uuid_unparsed);
158 if (strlen(uuid_unparsed) < 36) {
159 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
163 return uuid_unparsed;
166 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
168 if (!x_src || strlen(x_src) == 0) {
169 media_svc_error("x_src is NULL");
174 media_svc_error("length is Wrong");
178 strncpy(x_dst, x_src, max_len - 1);
179 x_dst[max_len - 1] = '\0';
182 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
186 if (!STRING_VALID(src)) {
187 media_svc_error("invalid src");
188 return MS_MEDIA_ERR_INVALID_PARAMETER;
193 len = strlen(src) + 1;
197 media_svc_error("malloc failed");
198 return MS_MEDIA_ERR_INTERNAL;
201 strncpy(*dst, src, len);
205 return MS_MEDIA_ERR_NONE;
208 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
210 if (!STRING_VALID(src)) {
211 media_svc_error("invalid src");
212 return MS_MEDIA_ERR_INVALID_PARAMETER;
217 *dst = malloc(copysize + 1);
220 media_svc_error("malloc failed");
221 return MS_MEDIA_ERR_INTERNAL;
224 strncpy(*dst, src, copysize);
228 return MS_MEDIA_ERR_NONE;
231 static int __media_svc_split_to_double(char *input, double *arr)
233 char tmp_arr[255] = {0, };
234 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
236 if (!STRING_VALID(input)) {
237 media_svc_error("Invalid parameter");
238 return MS_MEDIA_ERR_INVALID_PARAMETER;
240 memset(tmp_arr, 0x0, sizeof(tmp_arr));
242 /*media_svc_debug("input: [%s]", input); */
246 for (idx = 0; idx < (len + 1); idx++) {
247 if (input[idx] == ' ') {
249 } else if ((input[idx] == ',') || (idx == len)) {
250 arr[arr_idx] = atof(tmp_arr);
253 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
254 memset(tmp_arr, 0x0, sizeof(tmp_arr));
256 tmp_arr[str_idx] = input[idx];
262 media_svc_error("Error when parsing GPS [%d]", arr_idx);
263 return MS_MEDIA_ERR_INTERNAL;
266 return MS_MEDIA_ERR_NONE;
269 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
275 return MS_MEDIA_ERR_INVALID_PARAMETER;
280 entry = exif_data_get_entry(ed, tag);
282 /* Get the contents of the tag in human-readable form */
283 if (tag == EXIF_TAG_ORIENTATION ||
284 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
285 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
286 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
288 if (i_value == NULL) {
289 media_svc_error("i_value is NULL");
290 return MS_MEDIA_ERR_INVALID_PARAMETER;
293 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
294 short exif_value = exif_get_short(entry->data, mByteOrder);
295 *i_value = (int)exif_value;
297 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
299 if (d_value == NULL) {
300 media_svc_error("d_value is NULL");
301 return MS_MEDIA_ERR_INVALID_PARAMETER;
304 /* Get the contents of the tag in human-readable form */
305 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
306 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
307 gps_buf[strlen(gps_buf)] = '\0';
308 int ret = MS_MEDIA_ERR_NONE;
310 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
312 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
313 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
315 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
316 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
319 media_svc_error("buf is NULL");
320 return MS_MEDIA_ERR_INVALID_PARAMETER;
323 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
324 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
325 long numerator = mRational.numerator;
326 long denominator = mRational.denominator;
327 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
329 } else if (tag == EXIF_TAG_FNUMBER) {
331 if (d_value == NULL) {
332 media_svc_error("d_value is NULL");
333 return MS_MEDIA_ERR_INVALID_PARAMETER;
336 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
337 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
338 long numerator = mRational.numerator;
339 long denominator = mRational.denominator;
341 *d_value = ((numerator*1.0)/(denominator*1.0));
346 media_svc_error("buf is NULL");
347 return MS_MEDIA_ERR_INVALID_PARAMETER;
350 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
351 buf[strlen(buf)] = '\0';
355 return MS_MEDIA_ERR_NONE;
358 time_t __media_svc_get_timeline_from_str(const char *timstr)
361 time_t modified_t = 0;
365 if (!STRING_VALID(timstr)) {
366 media_svc_error("Invalid Parameter");
370 /*Exif Format : %Y:%m:%d %H:%M:%S
371 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
372 memset(&t, 0x00, sizeof(struct tm));
376 localtime_r(&rawtime, &timeinfo);
378 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
379 t.tm_isdst = timeinfo.tm_isdst;
380 if (t.tm_isdst != 0) {
381 media_svc_error("DST %d", t.tm_isdst);
384 modified_t = mktime(&t);
385 if (modified_t > 0) {
388 media_svc_error("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);
391 media_svc_error("Failed to get timeline : [%s]", timstr);
397 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
401 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
403 /*categorize from mimetype */
404 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
405 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
406 *category = (*category | content_category[idx].category_by_mime);
411 /*in application type, exitst sound file ex) x-smafs */
412 if (*category & MEDIA_SVC_CATEGORY_ETC) {
413 int prefix_len = strlen(content_category[0].content_type);
415 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
416 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
417 *category ^= MEDIA_SVC_CATEGORY_ETC;
418 *category |= MEDIA_SVC_CATEGORY_SOUND;
423 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
424 *category ^= MEDIA_SVC_CATEGORY_ETC;
425 *category |= MEDIA_SVC_CATEGORY_SOUND;
429 /*check music file in soun files. */
430 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
431 int prefix_len = strlen(content_category[0].content_type) + 1;
433 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
434 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
435 *category ^= MEDIA_SVC_CATEGORY_SOUND;
436 *category |= MEDIA_SVC_CATEGORY_MUSIC;
441 /*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*/
442 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
443 *category ^= MEDIA_SVC_CATEGORY_SOUND;
444 *category |= MEDIA_SVC_CATEGORY_ETC;
446 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
447 /*some video files don't have video stream. in this case it is categorize as music. */
449 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
450 ext = strrchr(path, '.');
452 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
457 err = mm_file_get_stream_info(path, &audio, &video);
459 if (audio > 0 && video == 0) {
460 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
461 *category |= MEDIA_SVC_CATEGORY_MUSIC;
464 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
469 return MS_MEDIA_ERR_NONE;
472 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
474 int ret = MS_MEDIA_ERR_NONE;
477 media_svc_media_type_e type;
479 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
480 if (ret != MS_MEDIA_ERR_NONE) {
481 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
484 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
485 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
486 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
487 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
488 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
496 drm_contentifo is not NULL, if the file is OMA DRM.
497 If the file is not OMA DRM, drm_contentinfo must be NULL.
499 static int __media_svc_get_mime_type(const char *path, char *mimetype)
502 return MS_MEDIA_ERR_INVALID_PARAMETER;
504 /*in case of normal files or failure to get mime in drm */
505 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
506 media_svc_error("aul_get_mime_from_file fail");
507 return MS_MEDIA_ERR_INTERNAL;
510 return MS_MEDIA_ERR_NONE;
513 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
517 for (i = strlen(file_path); i >= 0; i--) {
518 if (file_path[i] == '.') {
519 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
523 if (file_path[i] == '/') {
530 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
532 char *err_attr_name = NULL;
533 double gps_value = 0.0;
534 int mmf_error = FILEINFO_ERROR_NONE;
536 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
537 if (mmf_error == FILEINFO_ERROR_NONE) {
538 if (longitude != NULL) {
539 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
542 SAFE_FREE(err_attr_name);
545 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
546 if (mmf_error == FILEINFO_ERROR_NONE) {
547 if (latitude != NULL) {
548 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
551 SAFE_FREE(err_attr_name);
554 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
555 if (mmf_error == FILEINFO_ERROR_NONE) {
556 if (altitude != NULL) {
557 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
560 SAFE_FREE(err_attr_name);
563 return MS_MEDIA_ERR_NONE;
566 static char *__media_svc_get_thumb_path(uid_t uid)
568 char *result_passwd = NULL;
569 struct group *grpinfo = NULL;
570 if (uid == getuid()) {
571 grpinfo = getgrnam("users");
572 if (grpinfo == NULL) {
573 media_svc_error("getgrnam(users) returns NULL !");
576 result_passwd = g_strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
578 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
579 struct passwd *userinfo = getpwuid(uid);
580 if (userinfo == NULL) {
581 media_svc_error("getpwuid(%d) returns NULL !", uid);
584 grpinfo = getgrnam("users");
585 if (grpinfo == NULL) {
586 media_svc_error("getgrnam(users) returns NULL !");
589 /* Compare git_t type and not group name */
590 if (grpinfo->gr_gid != userinfo->pw_gid) {
591 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
594 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
595 result_passwd = g_strdup(passwd_str);
598 return result_passwd;
601 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
603 int ret = MS_MEDIA_ERR_NONE;
604 unsigned char *raw_image = NULL;
607 unsigned int raw_size = 0;
608 void *resized_raw_image = NULL;
609 int resized_width = 0;
610 int resized_height = 0;
611 unsigned int buf_size = 0;
613 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
614 media_svc_debug("type [jpeg] size [%d]", size);
616 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
617 if (ret != MS_MEDIA_ERR_NONE) {
618 media_svc_error("image_util_decode_jpeg_from_memory failed");
619 *resize_image = image;
621 return MS_MEDIA_ERR_NONE;
624 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
625 media_svc_debug("No need resizing");
626 *resize_image = image;
628 SAFE_FREE(raw_image);
629 return MS_MEDIA_ERR_NONE;
632 if (width > height) {
633 resized_height = MEDIA_SVC_ARTWORK_SIZE;
634 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
636 resized_width = MEDIA_SVC_ARTWORK_SIZE;
637 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
640 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
642 resized_raw_image = malloc(buf_size);
644 if (resized_raw_image == NULL) {
645 media_svc_error("malloc failed");
646 *resize_image = image;
648 SAFE_FREE(raw_image);
649 return MS_MEDIA_ERR_NONE;
652 memset(resized_raw_image, 0, buf_size);
654 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
655 if (ret != MS_MEDIA_ERR_NONE) {
656 media_svc_error("image_util_resize failed");
657 *resize_image = image;
659 SAFE_FREE(raw_image);
660 SAFE_FREE(resized_raw_image);
661 return MS_MEDIA_ERR_NONE;
663 SAFE_FREE(raw_image);
666 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
667 if (ret != MS_MEDIA_ERR_NONE) {
668 media_svc_error("image_util_encode_jpeg_to_memory failed");
669 *resize_image = image;
671 SAFE_FREE(resized_raw_image);
672 return MS_MEDIA_ERR_NONE;
674 SAFE_FREE(resized_raw_image);
676 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
677 media_svc_debug("type [png] size [%d]", size);
678 *resize_image = image;
682 media_svc_debug("Not proper img format");
683 *resize_image = image;
690 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
692 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
695 media_svc_error("invalid image..");
696 return MS_MEDIA_ERR_INVALID_PARAMETER;
700 char *thumb_path = __media_svc_get_thumb_path(uid);
701 if (!STRING_VALID(thumb_path)) {
702 media_svc_error("fail to get thumb_path");
703 return MS_MEDIA_ERR_INTERNAL;
706 if (-1 == statfs(thumb_path, &fs)) {
707 media_svc_error("error in statfs");
708 SAFE_FREE(thumb_path);
709 return MS_MEDIA_ERR_INTERNAL;
712 SAFE_FREE(thumb_path);
714 long bsize_kbytes = fs.f_bsize >> 10;
716 if ((bsize_kbytes * fs.f_bavail) < 1024) {
717 media_svc_error("not enought space...");
718 return MS_MEDIA_ERR_INTERNAL;
723 if (image != NULL && size > 0) {
724 fp = fopen(image_path, "w");
727 media_svc_error("failed to open file");
728 return MS_MEDIA_ERR_INTERNAL;
731 nwrite = fwrite(image, 1, size, fp);
732 if (nwrite != size) {
733 media_svc_error("failed to write thumbnail");
735 return MS_MEDIA_ERR_INTERNAL;
740 return MS_MEDIA_ERR_NONE;
743 static char *_media_svc_get_title_from_filepath(const char *path)
745 char *filename = NULL;
748 int filename_len = -1;
749 int new_title_len = -1;
752 media_svc_error("path is NULL");
756 filename = g_path_get_basename(path);
757 if (!STRING_VALID(filename)) {
758 media_svc_error("wrong file name");
763 filename_len = strlen(filename);
765 ext = g_strrstr(filename, ".");
767 media_svc_error("there is no file extention");
771 new_title_len = filename_len - strlen(ext);
772 if (new_title_len < 1) {
773 media_svc_error("title length is zero");
778 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
782 media_svc_debug("extract title is [%s]", title);
787 int _media_svc_rename_file(const char *old_name, const char *new_name)
789 if ((old_name == NULL) || (new_name == NULL)) {
790 media_svc_error("invalid file name");
791 return MS_MEDIA_ERR_INVALID_PARAMETER;
794 if (rename(old_name, new_name) < 0) {
795 media_svc_stderror(" ");
796 return MS_MEDIA_ERR_INTERNAL;
799 return MS_MEDIA_ERR_NONE;
802 int _media_svc_remove_file(const char *path)
806 result = remove(path);
808 media_svc_debug("success to remove file");
809 return MS_MEDIA_ERR_NONE;
811 media_svc_stderror("fail to remove file result");
812 return MS_MEDIA_ERR_INTERNAL;
816 int _media_svc_remove_all_files_in_dir(const char *dir_path)
819 struct dirent *result;
821 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
824 dir = opendir(dir_path);
826 media_svc_error("%s is not exist", dir_path);
827 return MS_MEDIA_ERR_INVALID_PARAMETER;
830 while (!readdir_r(dir, &entry, &result)) {
834 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
837 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
839 if (stat(filename, &st) != 0) {
842 if (S_ISDIR(st.st_mode)) {
845 if (unlink(filename) != 0) {
846 media_svc_stderror("failed to remove");
848 return MS_MEDIA_ERR_INTERNAL;
853 return MS_MEDIA_ERR_NONE;
856 char *_media_svc_get_thumb_internal_path(uid_t uid)
858 char *result_passwd = NULL;
859 struct group *grpinfo = NULL;
860 if (uid == getuid()) {
861 grpinfo = getgrnam("users");
862 if (grpinfo == NULL) {
863 media_svc_error("getgrnam(users) returns NULL !");
866 result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
868 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
869 struct passwd *userinfo = getpwuid(uid);
870 if (userinfo == NULL) {
871 media_svc_error("getpwuid(%d) returns NULL !", uid);
874 grpinfo = getgrnam("users");
875 if (grpinfo == NULL) {
876 media_svc_error("getgrnam(users) returns NULL !");
879 /* Compare git_t type and not group name */
880 if (grpinfo->gr_gid != userinfo->pw_gid) {
881 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
884 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
885 result_passwd = g_strdup(passwd_str);
888 return result_passwd;
891 char *_media_svc_get_thumb_external_path(uid_t uid)
893 char *result_passwd = NULL;
894 struct group *grpinfo = NULL;
895 if (uid == getuid()) {
896 grpinfo = getgrnam("users");
897 if (grpinfo == NULL) {
898 media_svc_error("getgrnam(users) returns NULL !");
901 result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
903 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
904 struct passwd *userinfo = getpwuid(uid);
905 if (userinfo == NULL) {
906 media_svc_error("getpwuid(%d) returns NULL !", uid);
909 grpinfo = getgrnam("users");
910 if (grpinfo == NULL) {
911 media_svc_error("getgrnam(users) returns NULL !");
914 /* Compare git_t type and not group name */
915 if (grpinfo->gr_gid != userinfo->pw_gid) {
916 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
919 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
920 result_passwd = g_strdup(passwd_str);
923 return result_passwd;
926 static int __media_svc_check_thumb_dir(const char *thumb_dir)
931 dir = opendir(thumb_dir);
935 media_svc_stderror("opendir fail");
936 if (errno == ENOENT) {
937 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
938 ret = mkdir(thumb_dir, 0777);
940 media_svc_error("make fail");
947 ret = chmod(thumb_dir, 0777);
949 media_svc_stderror("chmod failed");
951 ret = chown(thumb_dir, 5000, 5000);
953 media_svc_stderror("chown failed");
957 return MS_MEDIA_ERR_NONE;
963 int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
965 int ret = MS_MEDIA_ERR_NONE;
966 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
967 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
968 char *thumb_dir = NULL;
969 char hash[255 + 1] = {0, };
970 char *thumbfile_ext = NULL;
971 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
972 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
974 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
975 media_svc_error("fail to get thumbnail path");
976 SAFE_FREE(internal_thumb_path);
977 SAFE_FREE(external_thumb_path);
978 return MS_MEDIA_ERR_INTERNAL;
981 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
983 ret = __media_svc_check_thumb_dir(thumb_dir);
984 if (ret != MS_MEDIA_ERR_NONE) {
985 media_svc_error("__media_svc_check_thumb_dir");
986 SAFE_FREE(internal_thumb_path);
987 SAFE_FREE(external_thumb_path);
988 return MS_MEDIA_ERR_INTERNAL;
991 memset(file_ext, 0, sizeof(file_ext));
992 if (!__media_svc_get_file_ext(pathname, file_ext)) {
993 media_svc_error("get file ext fail");
996 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
997 if (ret != MS_MEDIA_ERR_NONE) {
998 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
999 SAFE_FREE(internal_thumb_path);
1000 SAFE_FREE(external_thumb_path);
1001 return MS_MEDIA_ERR_INTERNAL;
1004 /*media_svc_debug("img format is [%s]", img_format); */
1006 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1007 thumbfile_ext = (char *)"jpg";
1008 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1009 thumbfile_ext = (char *)"png";
1010 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1011 thumbfile_ext = (char *)"gif";
1012 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1013 thumbfile_ext = (char *)"bmp";
1015 media_svc_error("Not proper img format");
1016 SAFE_FREE(internal_thumb_path);
1017 SAFE_FREE(external_thumb_path);
1018 return MS_MEDIA_ERR_INTERNAL;
1021 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1022 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1023 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1025 SAFE_FREE(internal_thumb_path);
1026 SAFE_FREE(external_thumb_path);
1028 return MS_MEDIA_ERR_NONE;
1031 int _media_svc_get_file_time(const char *full_path)
1033 struct stat statbuf;
1036 memset(&statbuf, 0, sizeof(struct stat));
1037 fd = stat(full_path, &statbuf);
1039 media_svc_error("stat(%s) fails.", full_path);
1040 return MS_MEDIA_ERR_INTERNAL;
1043 return statbuf.st_mtime;
1046 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1048 int ret = MS_MEDIA_ERR_NONE;
1050 /* Set default GPS value before extracting meta information */
1051 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1052 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1053 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1055 /* Set filename to title for all media */
1057 title = _media_svc_get_title_from_filepath(content_info->path);
1059 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1060 if (ret != MS_MEDIA_ERR_NONE)
1061 media_svc_error("strcpy error");
1064 media_svc_error("Can't extract title");
1065 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1066 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1069 /* Set default value before extracting meta information */
1070 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1071 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1073 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1074 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1076 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1077 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1079 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1080 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1082 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1083 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1085 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1086 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1088 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1089 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1091 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1092 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1094 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1095 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1098 media_svc_debug("refresh");
1099 return MS_MEDIA_ERR_NONE;
1102 content_info->played_count = 0;
1103 content_info->last_played_time = 0;
1104 content_info->last_played_position = 0;
1105 content_info->favourate = 0;
1106 content_info->media_meta.rating = 0;
1108 return MS_MEDIA_ERR_NONE;
1111 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1112 const char *path, media_svc_media_type_e *media_type, bool refresh)
1114 int ret = MS_MEDIA_ERR_NONE;
1115 char * media_uuid = NULL;
1116 char * file_name = NULL;
1117 bool drm_type = false;
1118 char mime_type[256] = {0, };
1120 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1121 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1123 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1125 memset(&st, 0, sizeof(struct stat));
1126 if (stat(path, &st) == 0) {
1127 content_info->modified_time = st.st_mtime;
1128 content_info->timeline = content_info->modified_time;
1129 content_info->size = st.st_size;
1130 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1132 media_svc_stderror("stat failed");
1136 _media_svc_set_default_value(content_info, refresh);
1138 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1140 media_svc_debug("refresh");
1141 return MS_MEDIA_ERR_NONE;
1144 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1145 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1147 content_info->storage_type = storage_type;
1148 time(&content_info->added_time);
1150 media_uuid = _media_info_generate_uuid();
1151 if (media_uuid == NULL) {
1152 _media_svc_destroy_content_info(content_info);
1153 return MS_MEDIA_ERR_INTERNAL;
1156 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1157 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1159 file_name = g_path_get_basename(path);
1160 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1161 SAFE_FREE(file_name);
1162 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1164 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1165 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1166 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1167 ret = __media_svc_get_mime_type(path, mime_type);
1168 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1170 media_svc_debug("mime [%s]", mime_type);
1171 content_info->is_drm = drm_type;
1173 ret = __media_svc_get_media_type(path, mime_type, media_type);
1174 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1176 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1177 media_svc_error("invalid media_type condition[%d]", *media_type);
1178 return MS_MEDIA_ERR_INVALID_PARAMETER;
1181 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1182 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1184 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1186 content_info->media_type = *media_type;
1189 return MS_MEDIA_ERR_NONE;
1192 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1194 int ret = MS_MEDIA_ERR_NONE;
1196 int orient_value = 0;
1198 int exif_height = 0;
1199 ExifData *ed = NULL;
1200 int has_datetaken = FALSE;
1201 int datetaken_size = 19;
1202 double fnumber = 0.0;
1206 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1207 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1208 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1209 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1211 memset(buf, 0x00, sizeof(buf));
1212 memset(description_buf, 0x00, sizeof(description_buf));
1213 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1214 memset(model_buf, 0x00, sizeof(model_buf));
1216 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1217 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1218 return MS_MEDIA_ERR_INVALID_PARAMETER;
1221 path = content_info->path;
1222 if (!STRING_VALID(path)) {
1223 media_svc_error("Invalid Path");
1224 return MS_MEDIA_ERR_INVALID_PARAMETER;
1227 /* Load an ExifData object from an EXIF file */
1228 ed = exif_data_new_from_file(path);
1231 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1232 goto GET_WIDTH_HEIGHT;
1235 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1236 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1237 if (strlen(buf) > 0) {
1238 if (strcmp(buf, "S") == 0) {
1242 content_info->media_meta.latitude = value;
1244 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1247 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1250 memset(buf, 0x00, sizeof(buf));
1252 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1253 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1254 if (strlen(buf) > 0) {
1255 if (strcmp(buf, "W") == 0) {
1259 content_info->media_meta.longitude = value;
1261 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1264 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1267 memset(buf, 0x00, sizeof(buf));
1269 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1270 if (strlen(description_buf) == 0) {
1271 /*media_svc_debug("Use 'No description'"); */
1272 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1273 if (ret != MS_MEDIA_ERR_NONE)
1274 media_svc_error("strcpy error");
1276 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1277 if (ret != MS_MEDIA_ERR_NONE)
1278 media_svc_error("strcpy error");
1281 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1282 if (ret != MS_MEDIA_ERR_NONE)
1283 media_svc_error("strcpy error");
1286 memset(buf, 0x00, sizeof(buf));
1288 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1289 if (strlen(buf) == 0) {
1290 /*media_svc_debug("time is NULL"); */
1292 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1293 if (ret != MS_MEDIA_ERR_NONE) {
1294 media_svc_error("strcpy error");
1296 has_datetaken = TRUE;
1297 /* This is same as recorded_date */
1298 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1299 if (ret != MS_MEDIA_ERR_NONE)
1300 media_svc_error("strcpy error");
1305 memset(buf, 0x00, sizeof(buf));
1307 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1308 if (strlen(buf) == 0) {
1309 /*media_svc_debug("time is NULL"); */
1311 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1312 if (ret != MS_MEDIA_ERR_NONE) {
1313 media_svc_error("strcpy error");
1315 has_datetaken = TRUE;
1316 /* This is same as recorded_date */
1317 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1318 if (ret != MS_MEDIA_ERR_NONE)
1319 media_svc_error("strcpy error");
1324 if (has_datetaken) {
1325 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1326 if (content_info->timeline == 0) {
1327 content_info->timeline = content_info->modified_time;
1329 media_svc_debug("Timeline : %ld", content_info->timeline);
1333 /* Get exposure_time value from exif. */
1334 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1335 if (strlen(exposure_time_buf) == 0) {
1336 //media_svc_debug("exposure_time_buf is NULL");
1338 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1339 if (ret != MS_MEDIA_ERR_NONE)
1340 media_svc_error("strcpy error");
1344 /* Get fnumber value from exif. */
1345 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1346 if (fnumber > 0.0) {
1347 content_info->media_meta.fnumber = fnumber;
1349 content_info->media_meta.fnumber = 0.0;
1352 content_info->media_meta.fnumber = 0.0;
1355 /* Get iso value from exif. */
1356 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1358 content_info->media_meta.iso = iso;
1360 content_info->media_meta.iso = 0;
1363 content_info->media_meta.iso = 0;
1366 /* Get model value from exif. */
1367 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1368 if (strlen(model_buf) == 0) {
1369 //media_svc_debug("model_buf is NULL");
1371 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1372 if (ret != MS_MEDIA_ERR_NONE)
1373 media_svc_error("strcpy error");
1377 /* Get orientation value from exif. */
1378 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1379 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1380 content_info->media_meta.orientation = orient_value;
1382 content_info->media_meta.orientation = 0;
1385 content_info->media_meta.orientation = 0;
1388 /* Get width value from exif. */
1389 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1390 if (exif_width > 0) {
1391 content_info->media_meta.width = exif_width;
1393 content_info->media_meta.width = 0;
1396 content_info->media_meta.width = 0;
1399 /* Get height value from exif. */
1400 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1401 if (exif_height > 0) {
1402 content_info->media_meta.height = exif_height;
1404 content_info->media_meta.height = 0;
1407 content_info->media_meta.height = 0;
1410 if (ed != NULL) exif_data_unref(ed);
1414 if (content_info->media_meta.width == 0 ||
1415 content_info->media_meta.height == 0) {
1416 /*Get image width, height*/
1417 unsigned int img_width = 0;
1418 unsigned int img_height = 0;
1419 ImgCodecType img_type = IMG_CODEC_NONE;
1421 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1423 if (content_info->media_meta.width == 0)
1424 content_info->media_meta.width = img_width;
1426 if (content_info->media_meta.height == 0)
1427 content_info->media_meta.height = img_height;
1430 return MS_MEDIA_ERR_NONE;
1433 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1435 MMHandleType tag = 0;
1438 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1439 int mmf_error = FILEINFO_ERROR_NONE;
1440 char *err_attr_name = NULL;
1443 int ret = MS_MEDIA_ERR_NONE;
1445 /*Get Content Tag attribute ===========*/
1446 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1448 if (mmf_error == FILEINFO_ERROR_NONE) {
1449 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1450 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1451 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1452 if (ret != MS_MEDIA_ERR_NONE)
1453 media_svc_error("strcpy error");
1455 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1457 SAFE_FREE(err_attr_name);
1458 /*media_svc_debug("album - unknown"); */
1461 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1462 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1463 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1464 if (ret != MS_MEDIA_ERR_NONE)
1465 media_svc_error("strcpy error");
1466 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1468 SAFE_FREE(err_attr_name);
1469 /*media_svc_debug("artist - unknown"); */
1472 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1473 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1474 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1475 if (ret != MS_MEDIA_ERR_NONE)
1476 media_svc_error("strcpy error");
1477 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1479 SAFE_FREE(err_attr_name);
1480 /*media_svc_debug("album_artist - unknown"); */
1483 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1484 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1485 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1486 if (ret != MS_MEDIA_ERR_NONE)
1487 media_svc_error("strcpy error");
1489 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1490 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1492 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1493 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1497 SAFE_FREE(err_attr_name);
1498 /*media_svc_debug("genre - unknown"); */
1501 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1502 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1504 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1505 if (ret != MS_MEDIA_ERR_NONE)
1506 media_svc_error("strcpy error");
1508 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1512 for (idx = 0; idx < size; idx++) {
1514 media_svc_debug("SPACE [%s]", p);
1518 media_svc_debug("Not SPACE [%s]", p);
1519 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1520 if (ret != MS_MEDIA_ERR_NONE)
1521 media_svc_error("strcpy error");
1527 media_svc_debug("Can't extract title. All string is space");
1528 title = _media_svc_get_title_from_filepath(content_info->path);
1530 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1531 if (ret != MS_MEDIA_ERR_NONE)
1532 media_svc_error("strcpy error");
1535 media_svc_error("Can't extract title");
1540 SAFE_FREE(err_attr_name);
1541 title = _media_svc_get_title_from_filepath(content_info->path);
1543 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1544 if (ret != MS_MEDIA_ERR_NONE)
1545 media_svc_error("strcpy error");
1548 media_svc_error("Can't extract title");
1552 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1553 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1554 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1555 if (ret != MS_MEDIA_ERR_NONE)
1556 media_svc_error("strcpy error");
1557 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1559 SAFE_FREE(err_attr_name);
1562 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1563 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1564 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1565 if (ret != MS_MEDIA_ERR_NONE)
1566 media_svc_error("strcpy error");
1567 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1568 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1570 /*media_svc_debug("composer - unknown"); */
1571 SAFE_FREE(err_attr_name);
1574 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1575 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1576 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1577 if (ret != MS_MEDIA_ERR_NONE)
1578 media_svc_error("strcpy error");
1579 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1580 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1582 /*media_svc_debug("copyright - unknown"); */
1583 SAFE_FREE(err_attr_name);
1586 mmf_error = mm_file_destroy_tag_attrs(tag);
1587 if (mmf_error != FILEINFO_ERROR_NONE) {
1588 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1591 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1592 char *no_tag_title = NULL;
1593 media_svc_error("no tag information");
1595 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1597 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1598 if (ret != MS_MEDIA_ERR_NONE)
1599 media_svc_error("strcpy error");
1600 SAFE_FREE(no_tag_title);
1602 media_svc_error("Can't extract title");
1605 content_info->album_id = album_id;
1608 return MS_MEDIA_ERR_NONE;
1611 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1613 MMHandleType content = 0;
1614 MMHandleType tag = 0;
1616 unsigned char *image = NULL;
1617 unsigned int size = 0;
1618 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1619 int mmf_error = FILEINFO_ERROR_NONE;
1620 char *err_attr_name = NULL;
1622 bool extract_thumbnail = FALSE;
1623 bool append_album = FALSE;
1625 int ret = MS_MEDIA_ERR_NONE;
1627 unsigned int resize_size = 0;
1628 unsigned char *resize_image = NULL;
1630 /*Get Content Tag attribute ===========*/
1631 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1633 if (mmf_error == FILEINFO_ERROR_NONE) {
1634 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1635 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1636 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1637 if (ret != MS_MEDIA_ERR_NONE)
1638 media_svc_error("strcpy error");
1640 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1642 SAFE_FREE(err_attr_name);
1643 /*media_svc_debug("album - unknown"); */
1646 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1647 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1648 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1649 if (ret != MS_MEDIA_ERR_NONE)
1650 media_svc_error("strcpy error");
1651 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1653 SAFE_FREE(err_attr_name);
1654 /*media_svc_debug("artist - unknown"); */
1657 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1658 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1659 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1660 if (ret != MS_MEDIA_ERR_NONE)
1661 media_svc_error("strcpy error");
1662 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1664 SAFE_FREE(err_attr_name);
1665 /*media_svc_debug("album_artist - unknown"); */
1668 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1669 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1670 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1671 if (ret != MS_MEDIA_ERR_NONE)
1672 media_svc_error("strcpy error");
1674 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1675 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1677 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1678 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1682 SAFE_FREE(err_attr_name);
1683 /*media_svc_debug("genre - unknown"); */
1686 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1687 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1689 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1690 if (ret != MS_MEDIA_ERR_NONE)
1691 media_svc_error("strcpy error");
1693 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1697 for (idx = 0; idx < size; idx++) {
1699 media_svc_debug("SPACE [%s]", p);
1703 media_svc_debug("Not SPACE [%s]", p);
1704 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1705 if (ret != MS_MEDIA_ERR_NONE)
1706 media_svc_error("strcpy error");
1712 media_svc_debug("Can't extract title. All string is space");
1713 title = _media_svc_get_title_from_filepath(content_info->path);
1715 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1716 if (ret != MS_MEDIA_ERR_NONE)
1717 media_svc_error("strcpy error");
1720 media_svc_error("Can't extract title");
1725 SAFE_FREE(err_attr_name);
1726 title = _media_svc_get_title_from_filepath(content_info->path);
1728 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1729 if (ret != MS_MEDIA_ERR_NONE)
1730 media_svc_error("strcpy error");
1733 media_svc_error("Can't extract title");
1737 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1738 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1739 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1740 if (ret != MS_MEDIA_ERR_NONE)
1741 media_svc_error("strcpy error");
1742 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1744 SAFE_FREE(err_attr_name);
1747 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1748 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1749 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1750 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1751 char time_info[64] = {0, };
1752 char p_value[64] = {0, };
1754 memset(time_info, 0x00, sizeof(time_info));
1755 memset(p_value, 0x00, sizeof(p_value));
1756 strncpy(p_value, p, size);
1757 for (idx = 0; idx < size; idx++) {
1758 if (p_value[idx] == '-') {
1759 time_info[idx] = ':';
1760 } else if (p_value[idx] != '\0') {
1761 time_info[idx] = p_value[idx];
1763 media_svc_error("strcpy error");
1767 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1769 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1772 if (ret != MS_MEDIA_ERR_NONE) {
1773 media_svc_error("strcpy error");
1775 /* This is same as datetaken */
1777 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1779 int datetaken_size = 19;
1780 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1782 if (ret != MS_MEDIA_ERR_NONE)
1783 media_svc_error("strcpy error");
1785 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1786 if (content_info->timeline == 0) {
1787 content_info->timeline = content_info->modified_time;
1789 media_svc_debug("Timeline : %ld", content_info->timeline);
1792 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1794 SAFE_FREE(err_attr_name);
1797 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1798 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1799 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1800 if (ret != MS_MEDIA_ERR_NONE)
1801 media_svc_error("strcpy error");
1802 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1803 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1805 /*media_svc_debug("composer - unknown"); */
1806 SAFE_FREE(err_attr_name);
1809 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1810 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1811 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1812 if (ret != MS_MEDIA_ERR_NONE)
1813 media_svc_error("strcpy error");
1814 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1815 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1817 /*media_svc_debug("copyright - unknown"); */
1818 SAFE_FREE(err_attr_name);
1821 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1822 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1823 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1824 if (ret != MS_MEDIA_ERR_NONE)
1825 media_svc_error("strcpy error");
1827 SAFE_FREE(err_attr_name);
1830 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1831 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1833 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1834 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1835 if (ret != MS_MEDIA_ERR_NONE)
1836 media_svc_error("strcpy error");
1838 media_svc_debug("Wrong Year Information [%s]", p);
1841 SAFE_FREE(err_attr_name);
1844 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1845 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1846 content_info->media_meta.rating = atoi(p);
1848 SAFE_FREE(err_attr_name);
1849 content_info->media_meta.rating = 0;
1852 /*Initialize album_id to 0. below code will set the album_id*/
1853 content_info->album_id = album_id;
1854 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1856 if (ret != MS_MEDIA_ERR_NONE) {
1857 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1858 media_svc_debug("album does not exist. So start to make album art");
1859 extract_thumbnail = TRUE;
1860 append_album = TRUE;
1862 extract_thumbnail = TRUE;
1863 append_album = FALSE;
1866 content_info->album_id = album_id;
1867 append_album = FALSE;
1869 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1870 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1872 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1873 extract_thumbnail = TRUE;
1875 media_svc_debug("album already exists. don't need to make album art");
1876 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1877 extract_thumbnail = TRUE;
1881 /*Do not extract artwork for the USB Storage content*/
1882 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1883 extract_thumbnail = FALSE;
1885 if (extract_thumbnail == TRUE) {
1886 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1887 if (mmf_error != FILEINFO_ERROR_NONE) {
1888 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1889 SAFE_FREE(err_attr_name);
1891 /*media_svc_debug("artwork size1 [%d]", size); */
1894 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1895 if (mmf_error != FILEINFO_ERROR_NONE) {
1896 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1897 SAFE_FREE(err_attr_name);
1899 /*media_svc_debug("artwork size2 [%d]", size); */
1901 if (image != NULL && size > 0) {
1902 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1903 int artwork_mime_size = -1;
1905 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1906 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1907 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1908 if (ret != MS_MEDIA_ERR_NONE) {
1909 media_svc_error("Fail to Get Thumbnail Path");
1911 /* albumart resizing */
1912 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1914 SAFE_FREE(err_attr_name);
1917 if (strlen(thumb_path) > 0) {
1918 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1919 if (ret != MS_MEDIA_ERR_NONE) {
1920 media_svc_error("Fail to Save Thumbnail Image");
1922 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1923 if (ret != MS_MEDIA_ERR_NONE)
1924 media_svc_error("strcpy error");
1930 if (append_album == TRUE) {
1931 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1932 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1933 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1935 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1937 content_info->album_id = album_id;
1940 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1941 double longitude = 0.0;
1942 double latitude = 0.0;
1943 double altitude = 0.0;
1945 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1946 content_info->media_meta.longitude = longitude;
1947 content_info->media_meta.latitude = latitude;
1948 content_info->media_meta.altitude = altitude;
1950 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1951 if (mmf_error != FILEINFO_ERROR_NONE) {
1953 SAFE_FREE(err_attr_name);
1956 media_svc_debug("CDIS : %d", cdis_value);
1959 mmf_error = mm_file_destroy_tag_attrs(tag);
1960 if (mmf_error != FILEINFO_ERROR_NONE) {
1961 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1964 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1965 char *no_tag_title = NULL;
1966 media_svc_error("no tag information");
1968 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1970 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1971 if (ret != MS_MEDIA_ERR_NONE)
1972 media_svc_error("strcpy error");
1973 SAFE_FREE(no_tag_title);
1975 media_svc_error("Can't extract title");
1978 content_info->album_id = album_id;
1981 /*Get Content attribute ===========*/
1982 if (cdis_value == 1) {
1983 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1985 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1988 if (mmf_error == FILEINFO_ERROR_NONE) {
1989 /*Common attribute*/
1990 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1991 if (mmf_error != FILEINFO_ERROR_NONE) {
1992 SAFE_FREE(err_attr_name);
1993 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1995 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1998 /*Sound/Music attribute*/
1999 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2001 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2002 if (mmf_error != FILEINFO_ERROR_NONE) {
2003 SAFE_FREE(err_attr_name);
2004 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2006 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2009 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2010 if (mmf_error != FILEINFO_ERROR_NONE) {
2011 SAFE_FREE(err_attr_name);
2012 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2014 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2017 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2018 if (mmf_error != FILEINFO_ERROR_NONE) {
2019 SAFE_FREE(err_attr_name);
2020 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2022 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2025 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2026 if (mmf_error != FILEINFO_ERROR_NONE) {
2027 SAFE_FREE(err_attr_name);
2028 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2030 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2032 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2033 int audio_bitrate = 0;
2034 int video_bitrate = 0;
2036 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2037 if (mmf_error != FILEINFO_ERROR_NONE) {
2038 SAFE_FREE(err_attr_name);
2039 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2041 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2044 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2045 if (mmf_error != FILEINFO_ERROR_NONE) {
2046 SAFE_FREE(err_attr_name);
2047 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2049 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2052 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2054 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2055 if (mmf_error != FILEINFO_ERROR_NONE) {
2056 SAFE_FREE(err_attr_name);
2057 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2059 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2062 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2063 if (mmf_error != FILEINFO_ERROR_NONE) {
2064 SAFE_FREE(err_attr_name);
2065 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2067 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2071 media_svc_error("Not support type");
2072 mmf_error = mm_file_destroy_content_attrs(content);
2073 if (mmf_error != FILEINFO_ERROR_NONE) {
2074 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2076 return MS_MEDIA_ERR_INVALID_PARAMETER;
2079 mmf_error = mm_file_destroy_content_attrs(content);
2080 if (mmf_error != FILEINFO_ERROR_NONE) {
2081 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2084 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2087 return MS_MEDIA_ERR_NONE;
2090 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2092 media_svc_retm_if(content_info == NULL, "content info is NULL");
2094 /* Delete media_svc_content_info_s */
2095 SAFE_FREE(content_info->media_uuid);
2096 SAFE_FREE(content_info->path);
2097 SAFE_FREE(content_info->file_name);
2098 SAFE_FREE(content_info->mime_type);
2099 SAFE_FREE(content_info->folder_uuid);
2100 SAFE_FREE(content_info->thumbnail_path);
2101 SAFE_FREE(content_info->storage_uuid);
2103 /* Delete media_svc_content_meta_s */
2104 SAFE_FREE(content_info->media_meta.title);
2105 SAFE_FREE(content_info->media_meta.album);
2106 SAFE_FREE(content_info->media_meta.artist);
2107 SAFE_FREE(content_info->media_meta.album_artist);
2108 SAFE_FREE(content_info->media_meta.genre);
2109 SAFE_FREE(content_info->media_meta.composer);
2110 SAFE_FREE(content_info->media_meta.year);
2111 SAFE_FREE(content_info->media_meta.recorded_date);
2112 SAFE_FREE(content_info->media_meta.copyright);
2113 SAFE_FREE(content_info->media_meta.track_num);
2114 SAFE_FREE(content_info->media_meta.description);
2115 SAFE_FREE(content_info->media_meta.datetaken);
2116 SAFE_FREE(content_info->media_meta.exposure_time);
2117 SAFE_FREE(content_info->media_meta.model);
2118 SAFE_FREE(content_info->media_meta.weather);
2119 SAFE_FREE(content_info->media_meta.category);
2120 SAFE_FREE(content_info->media_meta.keyword);
2121 SAFE_FREE(content_info->media_meta.location_tag);
2122 SAFE_FREE(content_info->media_meta.content_name);
2123 SAFE_FREE(content_info->media_meta.age_rating);
2124 SAFE_FREE(content_info->media_meta.author);
2125 SAFE_FREE(content_info->media_meta.provider);
2127 SAFE_FREE(content_info->media_meta.title_pinyin);
2128 SAFE_FREE(content_info->media_meta.album_pinyin);
2129 SAFE_FREE(content_info->media_meta.artist_pinyin);
2130 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2131 SAFE_FREE(content_info->media_meta.genre_pinyin);
2132 SAFE_FREE(content_info->media_meta.composer_pinyin);
2133 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2134 SAFE_FREE(content_info->media_meta.description_pinyin);
2139 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2141 if (STRING_VALID(path)) {
2142 char *internal_path = _media_svc_get_path(uid);
2143 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2144 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2145 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2146 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2147 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2148 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2150 media_svc_error("Invalid Path");
2151 SAFE_FREE(internal_path);
2152 return MS_MEDIA_ERR_INVALID_PARAMETER;
2154 SAFE_FREE(internal_path);
2156 media_svc_error("INVALID parameter");
2157 return MS_MEDIA_ERR_INVALID_PARAMETER;
2160 return MS_MEDIA_ERR_NONE;
2163 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2166 size_t i, count = 0;
2167 size_t oldlen = strlen(olds);
2168 if (oldlen < 1) return s;
2169 size_t newlen = strlen(news);
2171 if (newlen != oldlen) {
2172 for (i = 0; s[i] != '\0';) {
2173 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2176 } else i = strlen(s);
2179 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2180 if (result == NULL) return NULL;
2184 if (memcmp(s, olds, oldlen) == 0) {
2185 memcpy(sr, news, newlen);
2188 } else *sr++ = *s++;
2196 bool _media_svc_is_drm_file(const char *path)
2201 int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
2203 int ret = MS_MEDIA_ERR_NONE;
2205 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2207 if (ret != MS_MEDIA_ERR_NONE) {
2208 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2209 ret = MS_MEDIA_ERR_INTERNAL;
2211 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2217 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2219 int ret = MS_MEDIA_ERR_NONE;
2221 pinyin_name_s *pinyinname = NULL;
2225 if (!STRING_VALID(src_str)) {
2226 media_svc_debug("String is invalid");
2230 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2231 if (ret == MS_MEDIA_ERR_NONE) {
2232 if (STRING_VALID(pinyinname[0].pinyin_name))
2233 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2235 *pinyin_str = strdup(src_str); //Return Original Non China Character
2238 _media_svc_pinyin_free(pinyinname, size);
2243 bool _media_svc_check_pinyin_support(void)
2249 int _media_svc_get_ini_value()
2251 if (g_ini_value == -1) {
2252 dictionary *dict = NULL;
2254 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2256 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2260 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2261 iniparser_freedict(dict);
2263 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2268 char* _media_svc_get_title_from_path(const char *path)
2270 char *filename = NULL;
2273 int filename_len = -1;
2274 int new_title_len = -1;
2277 media_svc_error("path is NULL");
2281 filename = g_path_get_basename(path);
2282 if (!STRING_VALID(filename)) {
2283 media_svc_error("wrong file name");
2284 SAFE_FREE(filename);
2288 filename_len = strlen(filename);
2290 ext = g_strrstr(filename, ".");
2292 media_svc_error("there is no file extention");
2296 new_title_len = filename_len - strlen(ext);
2297 if (new_title_len < 1) {
2298 media_svc_error("title length is zero");
2299 SAFE_FREE(filename);
2303 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2305 SAFE_FREE(filename);
2307 media_svc_debug("extract title is [%s]", title);
2312 #define BUF_LENGHT 256
2314 void _media_svc_print_stderror(void)
2316 char buf[BUF_LENGHT] = {0,};
2318 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));