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[50];
154 uuid_generate(uuid_value);
155 uuid_unparse(uuid_value, uuid_unparsed);
157 /*media_svc_debug("UUID : %s", uuid_unparsed); */
158 return uuid_unparsed;
161 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
163 if (!x_src || strlen(x_src) == 0) {
164 media_svc_error("x_src is NULL");
169 media_svc_error("length is Wrong");
173 strncpy(x_dst, x_src, max_len - 1);
174 x_dst[max_len - 1] = '\0';
177 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
181 if (!STRING_VALID(src)) {
182 media_svc_error("invalid src");
183 return MS_MEDIA_ERR_INVALID_PARAMETER;
188 len = strlen(src) + 1;
192 media_svc_error("malloc failed");
193 return MS_MEDIA_ERR_INTERNAL;
196 strncpy(*dst, src, len);
200 return MS_MEDIA_ERR_NONE;
203 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
205 if (!STRING_VALID(src)) {
206 media_svc_error("invalid src");
207 return MS_MEDIA_ERR_INVALID_PARAMETER;
212 *dst = malloc(copysize + 1);
215 media_svc_error("malloc failed");
216 return MS_MEDIA_ERR_INTERNAL;
219 strncpy(*dst, src, copysize);
223 return MS_MEDIA_ERR_NONE;
226 static int __media_svc_split_to_double(char *input, double *arr)
228 char tmp_arr[255] = {0, };
229 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
231 if (!STRING_VALID(input)) {
232 media_svc_error("Invalid parameter");
233 return MS_MEDIA_ERR_INVALID_PARAMETER;
235 memset(tmp_arr, 0x0, sizeof(tmp_arr));
237 /*media_svc_debug("input: [%s]", input); */
241 for (idx = 0; idx < (len + 1); idx++) {
242 if (input[idx] == ' ') {
244 } else if ((input[idx] == ',') || (idx == len)) {
245 arr[arr_idx] = atof(tmp_arr);
248 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
249 memset(tmp_arr, 0x0, sizeof(tmp_arr));
251 tmp_arr[str_idx] = input[idx];
257 media_svc_error("Error when parsing GPS [%d]", arr_idx);
258 return MS_MEDIA_ERR_INTERNAL;
261 return MS_MEDIA_ERR_NONE;
264 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, int ifdtype, long tagtype)
270 /*media_svc_debug("ExifData is NULL"); */
271 return MS_MEDIA_ERR_INVALID_PARAMETER;
276 entry = exif_data_get_entry(ed, tag);
278 /* Get the contents of the tag in human-readable form */
279 if (tag == EXIF_TAG_ORIENTATION ||
280 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
281 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
282 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
284 if (i_value == NULL) {
285 media_svc_error("i_value is NULL");
286 return MS_MEDIA_ERR_INVALID_PARAMETER;
289 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
290 short exif_value = exif_get_short(entry->data, mByteOrder);
291 /*media_svc_debug("%s : %d", exif_tag_get_name_in_ifd(tag, ifd), exif_value); */
292 *i_value = (int)exif_value;
294 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
296 if (d_value == NULL) {
297 media_svc_error("d_value is NULL");
298 return MS_MEDIA_ERR_INVALID_PARAMETER;
301 /* Get the contents of the tag in human-readable form */
302 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
303 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
304 gps_buf[strlen(gps_buf)] = '\0';
305 int ret = MS_MEDIA_ERR_NONE;
306 /*media_svc_debug("%s: [%s]", exif_tag_get_name_in_ifd(tag, ifd), gps_buf); */
308 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
310 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
311 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
313 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
314 /*media_svc_debug("GPS value is [%f], %f, %f, %f", *d_value, tmp_arr[0], tmp_arr[1], tmp_arr[2]); */
315 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
318 media_svc_error("buf is NULL");
319 return MS_MEDIA_ERR_INVALID_PARAMETER;
322 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
323 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
324 long numerator = mRational.numerator;
325 long denominator = mRational.denominator;
326 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
328 } else if (tag == EXIF_TAG_FNUMBER) {
330 if (d_value == NULL) {
331 media_svc_error("d_value is NULL");
332 return MS_MEDIA_ERR_INVALID_PARAMETER;
335 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
336 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
337 long numerator = mRational.numerator;
338 long denominator = mRational.denominator;
340 *d_value = ((numerator*1.0)/(denominator*1.0));
345 media_svc_error("buf is NULL");
346 return MS_MEDIA_ERR_INVALID_PARAMETER;
349 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
350 buf[strlen(buf)] = '\0';
354 return MS_MEDIA_ERR_NONE;
357 time_t __media_svc_get_timeline_from_str(const char *timstr)
360 time_t modified_t = 0;
364 if (!STRING_VALID(timstr)) {
365 media_svc_error("Invalid Parameter");
369 /*Exif Format : %Y:%m:%d %H:%M:%S
370 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
371 memset(&t, 0x00, sizeof(struct tm));
375 localtime_r(&rawtime, &timeinfo);
377 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
378 t.tm_isdst = timeinfo.tm_isdst;
379 if (t.tm_isdst != 0) {
380 media_svc_error("DST %d", t.tm_isdst);
383 modified_t = mktime(&t);
384 if (modified_t > 0) {
387 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);
390 media_svc_error("Failed to get timeline : [%s]", timstr);
396 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
400 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
402 /*categorize from mimetype */
403 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
404 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
405 *category = (*category | content_category[idx].category_by_mime);
410 /*in application type, exitst sound file ex) x-smafs */
411 if (*category & MEDIA_SVC_CATEGORY_ETC) {
412 int prefix_len = strlen(content_category[0].content_type);
414 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
415 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
416 *category ^= MEDIA_SVC_CATEGORY_ETC;
417 *category |= MEDIA_SVC_CATEGORY_SOUND;
422 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
423 *category ^= MEDIA_SVC_CATEGORY_ETC;
424 *category |= MEDIA_SVC_CATEGORY_SOUND;
428 /*check music file in soun files. */
429 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
430 int prefix_len = strlen(content_category[0].content_type) + 1;
432 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
433 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
434 *category ^= MEDIA_SVC_CATEGORY_SOUND;
435 *category |= MEDIA_SVC_CATEGORY_MUSIC;
440 /*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*/
441 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
442 *category ^= MEDIA_SVC_CATEGORY_SOUND;
443 *category |= MEDIA_SVC_CATEGORY_ETC;
445 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
446 /*some video files don't have video stream. in this case it is categorize as music. */
448 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
449 ext = strrchr(path, '.');
451 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
456 err = mm_file_get_stream_info(path, &audio, &video);
458 if (audio > 0 && video == 0) {
459 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
460 *category |= MEDIA_SVC_CATEGORY_MUSIC;
463 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
468 return MS_MEDIA_ERR_NONE;
471 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
473 int ret = MS_MEDIA_ERR_NONE;
476 media_svc_media_type_e type;
478 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
479 if (ret != MS_MEDIA_ERR_NONE) {
480 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
483 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
484 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
485 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
486 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
487 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
495 drm_contentifo is not NULL, if the file is OMA DRM.
496 If the file is not OMA DRM, drm_contentinfo must be NULL.
498 static int __media_svc_get_mime_type(const char *path, char *mimetype)
501 return MS_MEDIA_ERR_INVALID_PARAMETER;
503 /*in case of normal files or failure to get mime in drm */
504 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
505 media_svc_error("aul_get_mime_from_file fail");
506 return MS_MEDIA_ERR_INTERNAL;
509 return MS_MEDIA_ERR_NONE;
512 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
516 for (i = strlen(file_path); i >= 0; i--) {
517 if (file_path[i] == '.') {
518 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
522 if (file_path[i] == '/') {
529 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
531 char *err_attr_name = NULL;
532 double gps_value = 0.0;
533 int mmf_error = FILEINFO_ERROR_NONE;
535 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
536 if (mmf_error == FILEINFO_ERROR_NONE) {
537 if (longitude != NULL) {
538 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
541 SAFE_FREE(err_attr_name);
544 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
545 if (mmf_error == FILEINFO_ERROR_NONE) {
546 if (latitude != NULL) {
547 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
550 SAFE_FREE(err_attr_name);
553 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
554 if (mmf_error == FILEINFO_ERROR_NONE) {
555 if (altitude != NULL) {
556 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
559 SAFE_FREE(err_attr_name);
562 return MS_MEDIA_ERR_NONE;
565 static char *__media_svc_get_thumb_path(uid_t uid)
567 char *result_passwd = NULL;
568 struct group *grpinfo = NULL;
569 if (uid == getuid()) {
570 grpinfo = getgrnam("users");
571 if (grpinfo == NULL) {
572 media_svc_error("getgrnam(users) returns NULL !");
575 result_passwd = g_strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
577 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
578 struct passwd *userinfo = getpwuid(uid);
579 if (userinfo == NULL) {
580 media_svc_error("getpwuid(%d) returns NULL !", uid);
583 grpinfo = getgrnam("users");
584 if (grpinfo == NULL) {
585 media_svc_error("getgrnam(users) returns NULL !");
588 /* Compare git_t type and not group name */
589 if (grpinfo->gr_gid != userinfo->pw_gid) {
590 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
593 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
594 result_passwd = g_strdup(passwd_str);
597 return result_passwd;
600 static int __media_svc_resize_artwork(void *image, int size, const char *img_format, void **resize_image, int *resize_size)
602 int ret = MS_MEDIA_ERR_NONE;
603 void *raw_image = NULL;
606 unsigned int raw_size = 0;
607 void *resized_raw_image = NULL;
608 int resized_width = 0;
609 int resized_height = 0;
610 unsigned int buf_size = 0;
612 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
613 media_svc_debug("type [jpeg] size [%d]", size);
615 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
616 if (ret != MS_MEDIA_ERR_NONE) {
617 media_svc_error("image_util_decode_jpeg_from_memory failed");
618 *resize_image = image;
620 return MS_MEDIA_ERR_NONE;
623 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
624 media_svc_debug("No need resizing");
625 *resize_image = image;
627 SAFE_FREE(raw_image);
628 return MS_MEDIA_ERR_NONE;
631 if (width > height) {
632 resized_height = MEDIA_SVC_ARTWORK_SIZE;
633 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
635 resized_width = MEDIA_SVC_ARTWORK_SIZE;
636 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
639 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
641 resized_raw_image = malloc(buf_size);
642 memset(resized_raw_image, 0, buf_size);
644 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
645 if (ret != MS_MEDIA_ERR_NONE) {
646 media_svc_error("image_util_resize failed");
647 *resize_image = image;
649 SAFE_FREE(raw_image);
650 return MS_MEDIA_ERR_NONE;
652 SAFE_FREE(raw_image);
655 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
656 if (ret != MS_MEDIA_ERR_NONE) {
657 media_svc_error("image_util_encode_jpeg_to_memory failed");
658 *resize_image = image;
660 return MS_MEDIA_ERR_NONE;
661 SAFE_FREE(resized_raw_image);
663 SAFE_FREE(resized_raw_image);
665 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
666 media_svc_debug("type [png] size [%d]", size);
667 *resize_image = image;
671 media_svc_debug("Not proper img format");
672 *resize_image = image;
679 static int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
681 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
684 media_svc_error("invalid image..");
685 return MS_MEDIA_ERR_INVALID_PARAMETER;
689 char *thumb_path = __media_svc_get_thumb_path(uid);
690 if (!STRING_VALID(thumb_path)) {
691 media_svc_error("fail to get thumb_path");
692 return MS_MEDIA_ERR_INTERNAL;
695 if (-1 == statfs(thumb_path, &fs)) {
696 media_svc_error("error in statfs");
697 SAFE_FREE(thumb_path);
698 return MS_MEDIA_ERR_INTERNAL;
701 SAFE_FREE(thumb_path);
703 long bsize_kbytes = fs.f_bsize >> 10;
705 if ((bsize_kbytes * fs.f_bavail) < 1024) {
706 media_svc_error("not enought space...");
707 return MS_MEDIA_ERR_INTERNAL;
712 if (image != NULL && size > 0) {
713 fp = fopen(image_path, "w");
716 media_svc_error("failed to open file");
717 return MS_MEDIA_ERR_INTERNAL;
720 nwrite = fwrite(image, 1, size, fp);
721 if (nwrite != size) {
722 media_svc_error("failed to write thumbnail");
724 return MS_MEDIA_ERR_INTERNAL;
729 return MS_MEDIA_ERR_NONE;
732 static char *_media_svc_get_title_from_filepath(const char *path)
734 char *filename = NULL;
737 int filename_len = -1;
738 int new_title_len = -1;
741 media_svc_error("path is NULL");
745 filename = g_path_get_basename(path);
746 if (!STRING_VALID(filename)) {
747 media_svc_error("wrong file name");
752 filename_len = strlen(filename);
754 ext = g_strrstr(filename, ".");
756 media_svc_error("there is no file extention");
760 new_title_len = filename_len - strlen(ext);
761 if (new_title_len < 1) {
762 media_svc_error("title length is zero");
767 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
771 media_svc_debug("extract title is [%s]", title);
776 int _media_svc_rename_file(const char *old_name, const char *new_name)
778 if ((old_name == NULL) || (new_name == NULL)) {
779 media_svc_error("invalid file name");
780 return MS_MEDIA_ERR_INVALID_PARAMETER;
783 if (rename(old_name, new_name) < 0) {
784 media_svc_stderror(" ");
785 return MS_MEDIA_ERR_INTERNAL;
788 return MS_MEDIA_ERR_NONE;
791 int _media_svc_remove_file(const char *path)
795 result = remove(path);
797 media_svc_debug("success to remove file");
798 return MS_MEDIA_ERR_NONE;
800 media_svc_stderror("fail to remove file result");
801 return MS_MEDIA_ERR_INTERNAL;
805 int _media_svc_remove_all_files_in_dir(const char *dir_path)
808 struct dirent *result;
810 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
813 dir = opendir(dir_path);
815 media_svc_error("%s is not exist", dir_path);
816 return MS_MEDIA_ERR_INVALID_PARAMETER;
819 while (!readdir_r(dir, &entry, &result)) {
823 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
826 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
828 if (stat(filename, &st) != 0) {
831 if (S_ISDIR(st.st_mode)) {
834 if (unlink(filename) != 0) {
835 media_svc_stderror("failed to remove");
837 return MS_MEDIA_ERR_INTERNAL;
842 return MS_MEDIA_ERR_NONE;
845 char *_media_svc_get_thumb_internal_path(uid_t uid)
847 char *result_passwd = NULL;
848 struct group *grpinfo = NULL;
849 if (uid == getuid()) {
850 grpinfo = getgrnam("users");
851 if (grpinfo == NULL) {
852 media_svc_error("getgrnam(users) returns NULL !");
855 result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
857 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
858 struct passwd *userinfo = getpwuid(uid);
859 if (userinfo == NULL) {
860 media_svc_error("getpwuid(%d) returns NULL !", uid);
863 grpinfo = getgrnam("users");
864 if (grpinfo == NULL) {
865 media_svc_error("getgrnam(users) returns NULL !");
868 /* Compare git_t type and not group name */
869 if (grpinfo->gr_gid != userinfo->pw_gid) {
870 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
873 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
874 result_passwd = g_strdup(passwd_str);
877 return result_passwd;
880 char *_media_svc_get_thumb_external_path(uid_t uid)
882 char *result_passwd = NULL;
883 struct group *grpinfo = NULL;
884 if (uid == getuid()) {
885 grpinfo = getgrnam("users");
886 if (grpinfo == NULL) {
887 media_svc_error("getgrnam(users) returns NULL !");
890 result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
892 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
893 struct passwd *userinfo = getpwuid(uid);
894 if (userinfo == NULL) {
895 media_svc_error("getpwuid(%d) returns NULL !", uid);
898 grpinfo = getgrnam("users");
899 if (grpinfo == NULL) {
900 media_svc_error("getgrnam(users) returns NULL !");
903 /* Compare git_t type and not group name */
904 if (grpinfo->gr_gid != userinfo->pw_gid) {
905 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
908 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
909 result_passwd = g_strdup(passwd_str);
912 return result_passwd;
915 static int __media_svc_check_thumb_dir(const char *thumb_dir)
920 dir = opendir(thumb_dir);
924 media_svc_stderror("opendir fail");
925 if (errno == ENOENT) {
926 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
927 ret = mkdir(thumb_dir, 0777);
929 media_svc_error("make fail");
936 ret = chmod(thumb_dir, 0777);
938 media_svc_stderror("chmod failed");
940 ret = chown(thumb_dir, 5000, 5000);
942 media_svc_stderror("chown failed");
946 return MS_MEDIA_ERR_NONE;
952 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)
954 int ret = MS_MEDIA_ERR_NONE;
955 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
956 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
957 char *thumb_dir = NULL;
958 char hash[255 + 1] = {0, };
959 char *thumbfile_ext = NULL;
960 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
961 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
963 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
964 media_svc_error("fail to get thumbnail path");
965 SAFE_FREE(internal_thumb_path);
966 SAFE_FREE(external_thumb_path);
967 return MS_MEDIA_ERR_INTERNAL;
970 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
972 ret = __media_svc_check_thumb_dir(thumb_dir);
973 if (ret != MS_MEDIA_ERR_NONE) {
974 media_svc_error("__media_svc_check_thumb_dir");
975 SAFE_FREE(internal_thumb_path);
976 SAFE_FREE(external_thumb_path);
977 return MS_MEDIA_ERR_INTERNAL;
980 memset(file_ext, 0, sizeof(file_ext));
981 if (!__media_svc_get_file_ext(pathname, file_ext)) {
982 media_svc_error("get file ext fail");
985 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
986 if (ret != MS_MEDIA_ERR_NONE) {
987 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
988 SAFE_FREE(internal_thumb_path);
989 SAFE_FREE(external_thumb_path);
990 return MS_MEDIA_ERR_INTERNAL;
993 /*media_svc_debug("img format is [%s]", img_format); */
995 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
996 thumbfile_ext = (char *)"jpg";
997 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
998 thumbfile_ext = (char *)"png";
999 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1000 thumbfile_ext = (char *)"gif";
1001 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1002 thumbfile_ext = (char *)"bmp";
1004 media_svc_error("Not proper img format");
1005 SAFE_FREE(internal_thumb_path);
1006 SAFE_FREE(external_thumb_path);
1007 return MS_MEDIA_ERR_INTERNAL;
1010 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1011 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1012 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1014 SAFE_FREE(internal_thumb_path);
1015 SAFE_FREE(external_thumb_path);
1017 return MS_MEDIA_ERR_NONE;
1020 int _media_svc_get_file_time(const char *full_path)
1022 struct stat statbuf;
1025 memset(&statbuf, 0, sizeof(struct stat));
1026 fd = stat(full_path, &statbuf);
1028 media_svc_error("stat(%s) fails.", full_path);
1029 return MS_MEDIA_ERR_INTERNAL;
1032 return statbuf.st_mtime;
1035 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1037 int ret = MS_MEDIA_ERR_NONE;
1039 /* Set default GPS value before extracting meta information */
1040 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1041 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1042 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1044 /* Set filename to title for all media */
1046 title = _media_svc_get_title_from_filepath(content_info->path);
1048 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1049 if (ret != MS_MEDIA_ERR_NONE)
1050 media_svc_error("strcpy error");
1053 media_svc_error("Can't extract title");
1054 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1055 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1058 /* Set default value before extracting meta information */
1059 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1060 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1062 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1063 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1065 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1066 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1068 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1069 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1071 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1072 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1074 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1075 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1077 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1078 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1080 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1081 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1083 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1084 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1087 media_svc_debug("refresh");
1088 return MS_MEDIA_ERR_NONE;
1091 content_info->played_count = 0;
1092 content_info->last_played_time = 0;
1093 content_info->last_played_position = 0;
1094 content_info->favourate = 0;
1095 content_info->media_meta.rating = 0;
1097 return MS_MEDIA_ERR_NONE;
1100 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1101 const char *path, media_svc_media_type_e *media_type, bool refresh)
1103 int ret = MS_MEDIA_ERR_NONE;
1104 char * media_uuid = NULL;
1105 char * file_name = NULL;
1106 bool drm_type = false;
1107 char mime_type[256] = {0, };
1109 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1110 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1112 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1114 memset(&st, 0, sizeof(struct stat));
1115 if (stat(path, &st) == 0) {
1116 content_info->modified_time = st.st_mtime;
1117 content_info->timeline = content_info->modified_time;
1118 content_info->size = st.st_size;
1119 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1121 media_svc_stderror("stat failed");
1125 _media_svc_set_default_value(content_info, refresh);
1127 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1129 media_svc_debug("refresh");
1130 return MS_MEDIA_ERR_NONE;
1133 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1134 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1136 content_info->storage_type = storage_type;
1137 time(&content_info->added_time);
1139 media_uuid = _media_info_generate_uuid();
1140 if (media_uuid == NULL) {
1141 _media_svc_destroy_content_info(content_info);
1142 return MS_MEDIA_ERR_INTERNAL;
1145 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1146 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1148 file_name = g_path_get_basename(path);
1149 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1150 SAFE_FREE(file_name);
1151 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1153 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1154 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1155 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1156 ret = __media_svc_get_mime_type(path, mime_type);
1157 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1159 media_svc_debug("mime [%s]", mime_type);
1160 content_info->is_drm = drm_type;
1162 ret = __media_svc_get_media_type(path, mime_type, media_type);
1163 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1165 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1166 media_svc_error("invalid media_type condition[%d]", *media_type);
1167 return MS_MEDIA_ERR_INVALID_PARAMETER;
1170 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1171 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1173 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1175 content_info->media_type = *media_type;
1178 return MS_MEDIA_ERR_NONE;
1181 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1183 int ret = MS_MEDIA_ERR_NONE;
1185 int orient_value = 0;
1187 int exif_height = 0;
1188 ExifData *ed = NULL;
1189 int has_datetaken = FALSE;
1190 int datetaken_size = 19;
1191 double fnumber = 0.0;
1195 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1196 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1197 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1198 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1200 memset(buf, 0x00, sizeof(buf));
1201 memset(description_buf, 0x00, sizeof(description_buf));
1202 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1203 memset(model_buf, 0x00, sizeof(model_buf));
1205 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1206 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1207 return MS_MEDIA_ERR_INVALID_PARAMETER;
1210 path = content_info->path;
1211 if (!STRING_VALID(path)) {
1212 media_svc_error("Invalid Path");
1213 return MS_MEDIA_ERR_INVALID_PARAMETER;
1216 /* Load an ExifData object from an EXIF file */
1217 ed = exif_data_new_from_file(path);
1220 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1221 goto GET_WIDTH_HEIGHT;
1224 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1225 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1226 if (strlen(buf) > 0) {
1227 if (strcmp(buf, "S") == 0) {
1231 content_info->media_meta.latitude = value;
1233 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1236 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1239 memset(buf, 0x00, sizeof(buf));
1241 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1242 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1243 if (strlen(buf) > 0) {
1244 if (strcmp(buf, "W") == 0) {
1248 content_info->media_meta.longitude = value;
1250 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1253 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1256 memset(buf, 0x00, sizeof(buf));
1258 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1259 if (strlen(description_buf) == 0) {
1260 /*media_svc_debug("Use 'No description'"); */
1261 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1262 if (ret != MS_MEDIA_ERR_NONE)
1263 media_svc_error("strcpy error");
1265 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1266 if (ret != MS_MEDIA_ERR_NONE)
1267 media_svc_error("strcpy error");
1270 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1271 if (ret != MS_MEDIA_ERR_NONE)
1272 media_svc_error("strcpy error");
1275 memset(buf, 0x00, sizeof(buf));
1277 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1278 if (strlen(buf) == 0) {
1279 /*media_svc_debug("time is NULL"); */
1281 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1282 if (ret != MS_MEDIA_ERR_NONE) {
1283 media_svc_error("strcpy error");
1285 has_datetaken = TRUE;
1286 /* This is same as recorded_date */
1287 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1288 if (ret != MS_MEDIA_ERR_NONE)
1289 media_svc_error("strcpy error");
1294 memset(buf, 0x00, sizeof(buf));
1296 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1297 if (strlen(buf) == 0) {
1298 /*media_svc_debug("time is NULL"); */
1300 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1301 if (ret != MS_MEDIA_ERR_NONE) {
1302 media_svc_error("strcpy error");
1304 has_datetaken = TRUE;
1305 /* This is same as recorded_date */
1306 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1307 if (ret != MS_MEDIA_ERR_NONE)
1308 media_svc_error("strcpy error");
1313 if (has_datetaken) {
1314 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1315 if (content_info->timeline == 0) {
1316 content_info->timeline = content_info->modified_time;
1318 media_svc_debug("Timeline : %ld", content_info->timeline);
1322 /* Get exposure_time value from exif. */
1323 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1324 if (strlen(exposure_time_buf) == 0) {
1325 //media_svc_debug("exposure_time_buf is NULL");
1327 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1328 if (ret != MS_MEDIA_ERR_NONE)
1329 media_svc_error("strcpy error");
1333 /* Get fnumber value from exif. */
1334 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_IFD_0, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1335 if (fnumber > 0.0) {
1336 content_info->media_meta.fnumber = fnumber;
1338 content_info->media_meta.fnumber = 0.0;
1341 content_info->media_meta.fnumber = 0.0;
1344 /* Get iso value from exif. */
1345 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_IFD_0, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1347 content_info->media_meta.iso = iso;
1349 content_info->media_meta.iso = 0;
1352 content_info->media_meta.iso = 0;
1355 /* Get model value from exif. */
1356 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1357 if (strlen(model_buf) == 0) {
1358 //media_svc_debug("model_buf is NULL");
1360 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1361 if (ret != MS_MEDIA_ERR_NONE)
1362 media_svc_error("strcpy error");
1366 /* Get orientation value from exif. */
1367 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1368 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1369 content_info->media_meta.orientation = orient_value;
1371 content_info->media_meta.orientation = 0;
1374 content_info->media_meta.orientation = 0;
1377 /* Get width value from exif. */
1378 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1379 if (exif_width > 0) {
1380 content_info->media_meta.width = exif_width;
1382 content_info->media_meta.width = 0;
1385 content_info->media_meta.width = 0;
1388 /* Get height value from exif. */
1389 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1390 if (exif_height > 0) {
1391 content_info->media_meta.height = exif_height;
1393 content_info->media_meta.height = 0;
1396 content_info->media_meta.height = 0;
1399 if (ed != NULL) exif_data_unref(ed);
1403 if (content_info->media_meta.width == 0 ||
1404 content_info->media_meta.height == 0) {
1405 /*Get image width, height*/
1406 unsigned int img_width = 0;
1407 unsigned int img_height = 0;
1408 ImgCodecType img_type = IMG_CODEC_NONE;
1410 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1412 if (content_info->media_meta.width == 0)
1413 content_info->media_meta.width = img_width;
1415 if (content_info->media_meta.height == 0)
1416 content_info->media_meta.height = img_height;
1419 return MS_MEDIA_ERR_NONE;
1422 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1424 MMHandleType tag = 0;
1427 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1428 int mmf_error = FILEINFO_ERROR_NONE;
1429 char *err_attr_name = NULL;
1432 int ret = MS_MEDIA_ERR_NONE;
1434 /*Get Content Tag attribute ===========*/
1435 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1437 if (mmf_error == FILEINFO_ERROR_NONE) {
1438 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1439 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1440 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1441 if (ret != MS_MEDIA_ERR_NONE)
1442 media_svc_error("strcpy error");
1444 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1446 SAFE_FREE(err_attr_name);
1447 /*media_svc_debug("album - unknown"); */
1450 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1451 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1452 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1453 if (ret != MS_MEDIA_ERR_NONE)
1454 media_svc_error("strcpy error");
1455 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1457 SAFE_FREE(err_attr_name);
1458 /*media_svc_debug("artist - unknown"); */
1461 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1462 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1463 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1464 if (ret != MS_MEDIA_ERR_NONE)
1465 media_svc_error("strcpy error");
1466 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1468 SAFE_FREE(err_attr_name);
1469 /*media_svc_debug("album_artist - unknown"); */
1472 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1473 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1474 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1475 if (ret != MS_MEDIA_ERR_NONE)
1476 media_svc_error("strcpy error");
1478 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1479 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1481 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1482 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1486 SAFE_FREE(err_attr_name);
1487 /*media_svc_debug("genre - unknown"); */
1490 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1491 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1493 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1494 if (ret != MS_MEDIA_ERR_NONE)
1495 media_svc_error("strcpy error");
1497 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1501 for (idx = 0; idx < size; idx++) {
1503 media_svc_debug("SPACE [%s]", p);
1507 media_svc_debug("Not SPACE [%s]", p);
1508 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1509 if (ret != MS_MEDIA_ERR_NONE)
1510 media_svc_error("strcpy error");
1516 media_svc_debug("Can't extract title. All string is space");
1517 title = _media_svc_get_title_from_filepath(content_info->path);
1519 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1520 if (ret != MS_MEDIA_ERR_NONE)
1521 media_svc_error("strcpy error");
1524 media_svc_error("Can't extract title");
1529 SAFE_FREE(err_attr_name);
1530 title = _media_svc_get_title_from_filepath(content_info->path);
1532 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1533 if (ret != MS_MEDIA_ERR_NONE)
1534 media_svc_error("strcpy error");
1537 media_svc_error("Can't extract title");
1541 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1542 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1543 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1544 if (ret != MS_MEDIA_ERR_NONE)
1545 media_svc_error("strcpy error");
1546 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1548 SAFE_FREE(err_attr_name);
1551 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1552 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1553 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1554 if (ret != MS_MEDIA_ERR_NONE)
1555 media_svc_error("strcpy error");
1556 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1557 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1559 /*media_svc_debug("composer - unknown"); */
1560 SAFE_FREE(err_attr_name);
1563 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1564 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1565 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1566 if (ret != MS_MEDIA_ERR_NONE)
1567 media_svc_error("strcpy error");
1568 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1569 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1571 /*media_svc_debug("copyright - unknown"); */
1572 SAFE_FREE(err_attr_name);
1575 mmf_error = mm_file_destroy_tag_attrs(tag);
1576 if (mmf_error != FILEINFO_ERROR_NONE) {
1577 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1580 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1581 char *no_tag_title = NULL;
1582 media_svc_error("no tag information");
1584 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1586 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1587 if (ret != MS_MEDIA_ERR_NONE)
1588 media_svc_error("strcpy error");
1589 SAFE_FREE(no_tag_title);
1591 media_svc_error("Can't extract title");
1594 content_info->album_id = album_id;
1597 return MS_MEDIA_ERR_NONE;
1600 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1602 MMHandleType content = 0;
1603 MMHandleType tag = 0;
1607 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1608 int mmf_error = FILEINFO_ERROR_NONE;
1609 char *err_attr_name = NULL;
1611 bool extract_thumbnail = FALSE;
1612 bool append_album = FALSE;
1614 int ret = MS_MEDIA_ERR_NONE;
1616 int resize_size = -1;
1617 void *resize_image = NULL;
1619 /*Get Content Tag attribute ===========*/
1620 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1622 if (mmf_error == FILEINFO_ERROR_NONE) {
1623 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1624 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1625 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1626 if (ret != MS_MEDIA_ERR_NONE)
1627 media_svc_error("strcpy error");
1629 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1631 SAFE_FREE(err_attr_name);
1632 /*media_svc_debug("album - unknown"); */
1635 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1636 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1637 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1638 if (ret != MS_MEDIA_ERR_NONE)
1639 media_svc_error("strcpy error");
1640 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1642 SAFE_FREE(err_attr_name);
1643 /*media_svc_debug("artist - unknown"); */
1646 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1647 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1648 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1649 if (ret != MS_MEDIA_ERR_NONE)
1650 media_svc_error("strcpy error");
1651 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1653 SAFE_FREE(err_attr_name);
1654 /*media_svc_debug("album_artist - unknown"); */
1657 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1658 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1659 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1660 if (ret != MS_MEDIA_ERR_NONE)
1661 media_svc_error("strcpy error");
1663 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1664 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1666 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1667 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1671 SAFE_FREE(err_attr_name);
1672 /*media_svc_debug("genre - unknown"); */
1675 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1676 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1678 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1679 if (ret != MS_MEDIA_ERR_NONE)
1680 media_svc_error("strcpy error");
1682 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1686 for (idx = 0; idx < size; idx++) {
1688 media_svc_debug("SPACE [%s]", p);
1692 media_svc_debug("Not SPACE [%s]", p);
1693 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1694 if (ret != MS_MEDIA_ERR_NONE)
1695 media_svc_error("strcpy error");
1701 media_svc_debug("Can't extract title. All string is space");
1702 title = _media_svc_get_title_from_filepath(content_info->path);
1704 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1705 if (ret != MS_MEDIA_ERR_NONE)
1706 media_svc_error("strcpy error");
1709 media_svc_error("Can't extract title");
1714 SAFE_FREE(err_attr_name);
1715 title = _media_svc_get_title_from_filepath(content_info->path);
1717 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1718 if (ret != MS_MEDIA_ERR_NONE)
1719 media_svc_error("strcpy error");
1722 media_svc_error("Can't extract title");
1726 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1727 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1728 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1729 if (ret != MS_MEDIA_ERR_NONE)
1730 media_svc_error("strcpy error");
1731 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1733 SAFE_FREE(err_attr_name);
1736 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1737 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1738 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1739 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1740 char time_info[64] = {0, };
1741 char p_value[64] = {0, };
1743 memset(time_info, 0x00, sizeof(time_info));
1744 memset(p_value, 0x00, sizeof(p_value));
1745 strncpy(p_value, p, size);
1746 for (idx = 0; idx < size; idx++) {
1747 if (p_value[idx] == '-') {
1748 time_info[idx] = ':';
1749 } else if (p_value[idx] != '\0') {
1750 time_info[idx] = p_value[idx];
1752 media_svc_error("strcpy error");
1756 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1758 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1761 if (ret != MS_MEDIA_ERR_NONE) {
1762 media_svc_error("strcpy error");
1764 /* This is same as datetaken */
1766 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1768 int datetaken_size = 19;
1769 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1771 if (ret != MS_MEDIA_ERR_NONE)
1772 media_svc_error("strcpy error");
1774 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1775 if (content_info->timeline == 0) {
1776 content_info->timeline = content_info->modified_time;
1778 media_svc_debug("Timeline : %ld", content_info->timeline);
1781 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1783 SAFE_FREE(err_attr_name);
1786 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1787 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1788 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1789 if (ret != MS_MEDIA_ERR_NONE)
1790 media_svc_error("strcpy error");
1791 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1792 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1794 /*media_svc_debug("composer - unknown"); */
1795 SAFE_FREE(err_attr_name);
1798 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1799 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1800 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1801 if (ret != MS_MEDIA_ERR_NONE)
1802 media_svc_error("strcpy error");
1803 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1804 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1806 /*media_svc_debug("copyright - unknown"); */
1807 SAFE_FREE(err_attr_name);
1810 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1811 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1812 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1813 if (ret != MS_MEDIA_ERR_NONE)
1814 media_svc_error("strcpy error");
1816 SAFE_FREE(err_attr_name);
1819 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1820 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1822 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1823 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1824 if (ret != MS_MEDIA_ERR_NONE)
1825 media_svc_error("strcpy error");
1827 media_svc_debug("Wrong Year Information [%s]", p);
1830 SAFE_FREE(err_attr_name);
1833 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1834 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1835 content_info->media_meta.rating = atoi(p);
1837 SAFE_FREE(err_attr_name);
1838 content_info->media_meta.rating = 0;
1841 /*Initialize album_id to 0. below code will set the album_id*/
1842 content_info->album_id = album_id;
1843 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1845 if (ret != MS_MEDIA_ERR_NONE) {
1846 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1847 media_svc_debug("album does not exist. So start to make album art");
1848 extract_thumbnail = TRUE;
1849 append_album = TRUE;
1851 extract_thumbnail = TRUE;
1852 append_album = FALSE;
1855 content_info->album_id = album_id;
1856 append_album = FALSE;
1858 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1859 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1861 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1862 extract_thumbnail = TRUE;
1864 media_svc_debug("album already exists. don't need to make album art");
1865 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1866 extract_thumbnail = TRUE;
1870 /*Do not extract artwork for the USB Storage content*/
1871 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1872 extract_thumbnail = FALSE;
1874 if (extract_thumbnail == TRUE) {
1875 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1876 if (mmf_error != FILEINFO_ERROR_NONE) {
1877 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1878 SAFE_FREE(err_attr_name);
1880 /*media_svc_debug("artwork size1 [%d]", size); */
1883 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1884 if (mmf_error != FILEINFO_ERROR_NONE) {
1885 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1886 SAFE_FREE(err_attr_name);
1888 /*media_svc_debug("artwork size2 [%d]", size); */
1890 if (image != NULL && size > 0) {
1891 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1892 int artwork_mime_size = -1;
1894 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1895 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1896 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1897 if (ret != MS_MEDIA_ERR_NONE) {
1898 media_svc_error("Fail to Get Thumbnail Path");
1900 /* albumart resizing */
1901 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1903 SAFE_FREE(err_attr_name);
1906 if (strlen(thumb_path) > 0) {
1907 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1908 if (ret != MS_MEDIA_ERR_NONE) {
1909 media_svc_error("Fail to Save Thumbnail Image");
1911 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1912 if (ret != MS_MEDIA_ERR_NONE)
1913 media_svc_error("strcpy error");
1919 if (append_album == TRUE) {
1920 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1921 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1922 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1924 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1926 content_info->album_id = album_id;
1929 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1930 double longitude = 0.0;
1931 double latitude = 0.0;
1932 double altitude = 0.0;
1934 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1935 content_info->media_meta.longitude = longitude;
1936 content_info->media_meta.latitude = latitude;
1937 content_info->media_meta.altitude = altitude;
1939 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1940 if (mmf_error != FILEINFO_ERROR_NONE) {
1942 SAFE_FREE(err_attr_name);
1945 media_svc_debug("CDIS : %d", cdis_value);
1948 mmf_error = mm_file_destroy_tag_attrs(tag);
1949 if (mmf_error != FILEINFO_ERROR_NONE) {
1950 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1953 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1954 char *no_tag_title = NULL;
1955 media_svc_error("no tag information");
1957 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1959 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1960 if (ret != MS_MEDIA_ERR_NONE)
1961 media_svc_error("strcpy error");
1962 SAFE_FREE(no_tag_title);
1964 media_svc_error("Can't extract title");
1967 content_info->album_id = album_id;
1970 /*Get Content attribute ===========*/
1971 if (cdis_value == 1) {
1972 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1974 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1977 if (mmf_error == FILEINFO_ERROR_NONE) {
1978 /*Common attribute*/
1979 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1980 if (mmf_error != FILEINFO_ERROR_NONE) {
1981 SAFE_FREE(err_attr_name);
1982 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1984 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1987 /*Sound/Music attribute*/
1988 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1990 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1991 if (mmf_error != FILEINFO_ERROR_NONE) {
1992 SAFE_FREE(err_attr_name);
1993 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1995 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1998 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1999 if (mmf_error != FILEINFO_ERROR_NONE) {
2000 SAFE_FREE(err_attr_name);
2001 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2003 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2006 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2007 if (mmf_error != FILEINFO_ERROR_NONE) {
2008 SAFE_FREE(err_attr_name);
2009 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2011 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2014 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2015 if (mmf_error != FILEINFO_ERROR_NONE) {
2016 SAFE_FREE(err_attr_name);
2017 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2019 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2021 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2022 int audio_bitrate = 0;
2023 int video_bitrate = 0;
2025 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2026 if (mmf_error != FILEINFO_ERROR_NONE) {
2027 SAFE_FREE(err_attr_name);
2028 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2030 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2033 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2034 if (mmf_error != FILEINFO_ERROR_NONE) {
2035 SAFE_FREE(err_attr_name);
2036 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2038 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2041 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2043 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2044 if (mmf_error != FILEINFO_ERROR_NONE) {
2045 SAFE_FREE(err_attr_name);
2046 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2048 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2051 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2052 if (mmf_error != FILEINFO_ERROR_NONE) {
2053 SAFE_FREE(err_attr_name);
2054 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2056 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2060 media_svc_error("Not support type");
2061 mmf_error = mm_file_destroy_content_attrs(content);
2062 if (mmf_error != FILEINFO_ERROR_NONE) {
2063 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2065 return MS_MEDIA_ERR_INVALID_PARAMETER;
2068 mmf_error = mm_file_destroy_content_attrs(content);
2069 if (mmf_error != FILEINFO_ERROR_NONE) {
2070 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2073 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2076 return MS_MEDIA_ERR_NONE;
2079 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2081 media_svc_retm_if(content_info == NULL, "content info is NULL");
2083 /* Delete media_svc_content_info_s */
2084 SAFE_FREE(content_info->media_uuid);
2085 SAFE_FREE(content_info->path);
2086 SAFE_FREE(content_info->file_name);
2087 SAFE_FREE(content_info->mime_type);
2088 SAFE_FREE(content_info->folder_uuid);
2089 SAFE_FREE(content_info->thumbnail_path);
2090 SAFE_FREE(content_info->storage_uuid);
2092 /* Delete media_svc_content_meta_s */
2093 SAFE_FREE(content_info->media_meta.title);
2094 SAFE_FREE(content_info->media_meta.album);
2095 SAFE_FREE(content_info->media_meta.artist);
2096 SAFE_FREE(content_info->media_meta.album_artist);
2097 SAFE_FREE(content_info->media_meta.genre);
2098 SAFE_FREE(content_info->media_meta.composer);
2099 SAFE_FREE(content_info->media_meta.year);
2100 SAFE_FREE(content_info->media_meta.recorded_date);
2101 SAFE_FREE(content_info->media_meta.copyright);
2102 SAFE_FREE(content_info->media_meta.track_num);
2103 SAFE_FREE(content_info->media_meta.description);
2104 SAFE_FREE(content_info->media_meta.datetaken);
2105 SAFE_FREE(content_info->media_meta.exposure_time);
2106 SAFE_FREE(content_info->media_meta.model);
2107 SAFE_FREE(content_info->media_meta.weather);
2108 SAFE_FREE(content_info->media_meta.category);
2109 SAFE_FREE(content_info->media_meta.keyword);
2110 SAFE_FREE(content_info->media_meta.location_tag);
2111 SAFE_FREE(content_info->media_meta.content_name);
2112 SAFE_FREE(content_info->media_meta.age_rating);
2113 SAFE_FREE(content_info->media_meta.author);
2114 SAFE_FREE(content_info->media_meta.provider);
2116 SAFE_FREE(content_info->media_meta.title_pinyin);
2117 SAFE_FREE(content_info->media_meta.album_pinyin);
2118 SAFE_FREE(content_info->media_meta.artist_pinyin);
2119 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2120 SAFE_FREE(content_info->media_meta.genre_pinyin);
2121 SAFE_FREE(content_info->media_meta.composer_pinyin);
2122 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2123 SAFE_FREE(content_info->media_meta.description_pinyin);
2128 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2130 if (STRING_VALID(path)) {
2131 char *internal_path = _media_svc_get_path(uid);
2132 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2133 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2134 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2135 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2136 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2137 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2139 media_svc_error("Invalid Path");
2140 SAFE_FREE(internal_path);
2141 return MS_MEDIA_ERR_INVALID_PARAMETER;
2143 SAFE_FREE(internal_path);
2145 media_svc_error("INVALID parameter");
2146 return MS_MEDIA_ERR_INVALID_PARAMETER;
2149 return MS_MEDIA_ERR_NONE;
2152 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2155 size_t i, count = 0;
2156 size_t oldlen = strlen(olds);
2157 if (oldlen < 1) return s;
2158 size_t newlen = strlen(news);
2160 if (newlen != oldlen) {
2161 for (i = 0; s[i] != '\0';) {
2162 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2165 } else i = strlen(s);
2168 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2169 if (result == NULL) return NULL;
2173 if (memcmp(s, olds, oldlen) == 0) {
2174 memcpy(sr, news, newlen);
2177 } else *sr++ = *s++;
2185 bool _media_svc_is_drm_file(const char *path)
2190 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)
2192 int ret = MS_MEDIA_ERR_NONE;
2194 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2196 if (ret != MS_MEDIA_ERR_NONE) {
2197 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2198 ret = MS_MEDIA_ERR_INTERNAL;
2200 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2206 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2208 int ret = MS_MEDIA_ERR_NONE;
2210 pinyin_name_s *pinyinname = NULL;
2214 if (!STRING_VALID(src_str)) {
2215 media_svc_debug("String is invalid");
2219 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2220 if (ret == MS_MEDIA_ERR_NONE) {
2221 if (STRING_VALID(pinyinname[0].pinyin_name))
2222 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2224 *pinyin_str = strdup(src_str); //Return Original Non China Character
2227 _media_svc_pinyin_free(pinyinname, size);
2232 bool _media_svc_check_pinyin_support(void)
2238 int _media_svc_get_ini_value()
2240 if (g_ini_value == -1) {
2241 dictionary *dict = NULL;
2243 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2245 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2249 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2250 iniparser_freedict(dict);
2252 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2257 char* _media_svc_get_title_from_path(const char *path)
2259 char *filename = NULL;
2262 int filename_len = -1;
2263 int new_title_len = -1;
2266 media_svc_error("path is NULL");
2270 filename = g_path_get_basename(path);
2271 if (!STRING_VALID(filename)) {
2272 media_svc_error("wrong file name");
2273 SAFE_FREE(filename);
2277 filename_len = strlen(filename);
2279 ext = g_strrstr(filename, ".");
2281 media_svc_error("there is no file extention");
2285 new_title_len = filename_len - strlen(ext);
2286 if (new_title_len < 1) {
2287 media_svc_error("title length is zero");
2288 SAFE_FREE(filename);
2292 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2294 SAFE_FREE(filename);
2296 media_svc_debug("extract title is [%s]", title);
2301 #define BUF_LENGHT 256
2303 void _media_svc_print_stderror(void)
2305 char buf[BUF_LENGHT] = {0,};
2307 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));