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, long tagtype)
270 return MS_MEDIA_ERR_INVALID_PARAMETER;
275 entry = exif_data_get_entry(ed, tag);
277 /* Get the contents of the tag in human-readable form */
278 if (tag == EXIF_TAG_ORIENTATION ||
279 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
280 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
281 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
283 if (i_value == NULL) {
284 media_svc_error("i_value is NULL");
285 return MS_MEDIA_ERR_INVALID_PARAMETER;
288 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
289 short exif_value = exif_get_short(entry->data, mByteOrder);
290 *i_value = (int)exif_value;
292 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
294 if (d_value == NULL) {
295 media_svc_error("d_value is NULL");
296 return MS_MEDIA_ERR_INVALID_PARAMETER;
299 /* Get the contents of the tag in human-readable form */
300 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
301 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
302 gps_buf[strlen(gps_buf)] = '\0';
303 int ret = MS_MEDIA_ERR_NONE;
305 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
307 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
308 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
310 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
311 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
314 media_svc_error("buf is NULL");
315 return MS_MEDIA_ERR_INVALID_PARAMETER;
318 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
319 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
320 long numerator = mRational.numerator;
321 long denominator = mRational.denominator;
322 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
324 } else if (tag == EXIF_TAG_FNUMBER) {
326 if (d_value == NULL) {
327 media_svc_error("d_value is NULL");
328 return MS_MEDIA_ERR_INVALID_PARAMETER;
331 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
332 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
333 long numerator = mRational.numerator;
334 long denominator = mRational.denominator;
336 *d_value = ((numerator*1.0)/(denominator*1.0));
341 media_svc_error("buf is NULL");
342 return MS_MEDIA_ERR_INVALID_PARAMETER;
345 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
346 buf[strlen(buf)] = '\0';
350 return MS_MEDIA_ERR_NONE;
353 time_t __media_svc_get_timeline_from_str(const char *timstr)
356 time_t modified_t = 0;
360 if (!STRING_VALID(timstr)) {
361 media_svc_error("Invalid Parameter");
365 /*Exif Format : %Y:%m:%d %H:%M:%S
366 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
367 memset(&t, 0x00, sizeof(struct tm));
371 localtime_r(&rawtime, &timeinfo);
373 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
374 t.tm_isdst = timeinfo.tm_isdst;
375 if (t.tm_isdst != 0) {
376 media_svc_error("DST %d", t.tm_isdst);
379 modified_t = mktime(&t);
380 if (modified_t > 0) {
383 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);
386 media_svc_error("Failed to get timeline : [%s]", timstr);
392 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
396 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
398 /*categorize from mimetype */
399 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
400 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
401 *category = (*category | content_category[idx].category_by_mime);
406 /*in application type, exitst sound file ex) x-smafs */
407 if (*category & MEDIA_SVC_CATEGORY_ETC) {
408 int prefix_len = strlen(content_category[0].content_type);
410 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
411 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
412 *category ^= MEDIA_SVC_CATEGORY_ETC;
413 *category |= MEDIA_SVC_CATEGORY_SOUND;
418 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
419 *category ^= MEDIA_SVC_CATEGORY_ETC;
420 *category |= MEDIA_SVC_CATEGORY_SOUND;
424 /*check music file in soun files. */
425 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
426 int prefix_len = strlen(content_category[0].content_type) + 1;
428 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
429 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
430 *category ^= MEDIA_SVC_CATEGORY_SOUND;
431 *category |= MEDIA_SVC_CATEGORY_MUSIC;
436 /*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*/
437 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
438 *category ^= MEDIA_SVC_CATEGORY_SOUND;
439 *category |= MEDIA_SVC_CATEGORY_ETC;
441 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
442 /*some video files don't have video stream. in this case it is categorize as music. */
444 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
445 ext = strrchr(path, '.');
447 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
452 err = mm_file_get_stream_info(path, &audio, &video);
454 if (audio > 0 && video == 0) {
455 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
456 *category |= MEDIA_SVC_CATEGORY_MUSIC;
459 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
464 return MS_MEDIA_ERR_NONE;
467 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
469 int ret = MS_MEDIA_ERR_NONE;
472 media_svc_media_type_e type;
474 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
475 if (ret != MS_MEDIA_ERR_NONE) {
476 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
479 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
480 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
481 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
482 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
483 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
491 drm_contentifo is not NULL, if the file is OMA DRM.
492 If the file is not OMA DRM, drm_contentinfo must be NULL.
494 static int __media_svc_get_mime_type(const char *path, char *mimetype)
497 return MS_MEDIA_ERR_INVALID_PARAMETER;
499 /*in case of normal files or failure to get mime in drm */
500 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
501 media_svc_error("aul_get_mime_from_file fail");
502 return MS_MEDIA_ERR_INTERNAL;
505 return MS_MEDIA_ERR_NONE;
508 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
512 for (i = strlen(file_path); i >= 0; i--) {
513 if (file_path[i] == '.') {
514 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
518 if (file_path[i] == '/') {
525 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
527 char *err_attr_name = NULL;
528 double gps_value = 0.0;
529 int mmf_error = FILEINFO_ERROR_NONE;
531 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
532 if (mmf_error == FILEINFO_ERROR_NONE) {
533 if (longitude != NULL) {
534 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
537 SAFE_FREE(err_attr_name);
540 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
541 if (mmf_error == FILEINFO_ERROR_NONE) {
542 if (latitude != NULL) {
543 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
546 SAFE_FREE(err_attr_name);
549 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
550 if (mmf_error == FILEINFO_ERROR_NONE) {
551 if (altitude != NULL) {
552 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
555 SAFE_FREE(err_attr_name);
558 return MS_MEDIA_ERR_NONE;
561 static char *__media_svc_get_thumb_path(uid_t uid)
563 char *result_passwd = NULL;
564 struct group *grpinfo = NULL;
565 if (uid == getuid()) {
566 grpinfo = getgrnam("users");
567 if (grpinfo == NULL) {
568 media_svc_error("getgrnam(users) returns NULL !");
571 result_passwd = g_strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
573 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
574 struct passwd *userinfo = getpwuid(uid);
575 if (userinfo == NULL) {
576 media_svc_error("getpwuid(%d) returns NULL !", uid);
579 grpinfo = getgrnam("users");
580 if (grpinfo == NULL) {
581 media_svc_error("getgrnam(users) returns NULL !");
584 /* Compare git_t type and not group name */
585 if (grpinfo->gr_gid != userinfo->pw_gid) {
586 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
589 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
590 result_passwd = g_strdup(passwd_str);
593 return result_passwd;
596 static int __media_svc_resize_artwork(void *image, int size, const char *img_format, void **resize_image, int *resize_size)
598 int ret = MS_MEDIA_ERR_NONE;
599 void *raw_image = NULL;
602 unsigned int raw_size = 0;
603 void *resized_raw_image = NULL;
604 int resized_width = 0;
605 int resized_height = 0;
606 unsigned int buf_size = 0;
608 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
609 media_svc_debug("type [jpeg] size [%d]", size);
611 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
612 if (ret != MS_MEDIA_ERR_NONE) {
613 media_svc_error("image_util_decode_jpeg_from_memory failed");
614 *resize_image = image;
616 return MS_MEDIA_ERR_NONE;
619 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
620 media_svc_debug("No need resizing");
621 *resize_image = image;
623 SAFE_FREE(raw_image);
624 return MS_MEDIA_ERR_NONE;
627 if (width > height) {
628 resized_height = MEDIA_SVC_ARTWORK_SIZE;
629 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
631 resized_width = MEDIA_SVC_ARTWORK_SIZE;
632 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
635 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
637 resized_raw_image = malloc(buf_size);
639 if (resized_raw_image == NULL) {
640 media_svc_error("malloc failed");
641 *resize_image = image;
643 SAFE_FREE(raw_image);
644 return MS_MEDIA_ERR_NONE;
647 memset(resized_raw_image, 0, buf_size);
649 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
650 if (ret != MS_MEDIA_ERR_NONE) {
651 media_svc_error("image_util_resize failed");
652 *resize_image = image;
654 SAFE_FREE(raw_image);
655 SAFE_FREE(resized_raw_image);
656 return MS_MEDIA_ERR_NONE;
658 SAFE_FREE(raw_image);
661 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
662 if (ret != MS_MEDIA_ERR_NONE) {
663 media_svc_error("image_util_encode_jpeg_to_memory failed");
664 *resize_image = image;
666 SAFE_FREE(resized_raw_image);
667 return MS_MEDIA_ERR_NONE;
669 SAFE_FREE(resized_raw_image);
671 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
672 media_svc_debug("type [png] size [%d]", size);
673 *resize_image = image;
677 media_svc_debug("Not proper img format");
678 *resize_image = image;
685 static int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
687 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
690 media_svc_error("invalid image..");
691 return MS_MEDIA_ERR_INVALID_PARAMETER;
695 char *thumb_path = __media_svc_get_thumb_path(uid);
696 if (!STRING_VALID(thumb_path)) {
697 media_svc_error("fail to get thumb_path");
698 return MS_MEDIA_ERR_INTERNAL;
701 if (-1 == statfs(thumb_path, &fs)) {
702 media_svc_error("error in statfs");
703 SAFE_FREE(thumb_path);
704 return MS_MEDIA_ERR_INTERNAL;
707 SAFE_FREE(thumb_path);
709 long bsize_kbytes = fs.f_bsize >> 10;
711 if ((bsize_kbytes * fs.f_bavail) < 1024) {
712 media_svc_error("not enought space...");
713 return MS_MEDIA_ERR_INTERNAL;
718 if (image != NULL && size > 0) {
719 fp = fopen(image_path, "w");
722 media_svc_error("failed to open file");
723 return MS_MEDIA_ERR_INTERNAL;
726 nwrite = fwrite(image, 1, size, fp);
727 if (nwrite != size) {
728 media_svc_error("failed to write thumbnail");
730 return MS_MEDIA_ERR_INTERNAL;
735 return MS_MEDIA_ERR_NONE;
738 static char *_media_svc_get_title_from_filepath(const char *path)
740 char *filename = NULL;
743 int filename_len = -1;
744 int new_title_len = -1;
747 media_svc_error("path is NULL");
751 filename = g_path_get_basename(path);
752 if (!STRING_VALID(filename)) {
753 media_svc_error("wrong file name");
758 filename_len = strlen(filename);
760 ext = g_strrstr(filename, ".");
762 media_svc_error("there is no file extention");
766 new_title_len = filename_len - strlen(ext);
767 if (new_title_len < 1) {
768 media_svc_error("title length is zero");
773 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
777 media_svc_debug("extract title is [%s]", title);
782 int _media_svc_rename_file(const char *old_name, const char *new_name)
784 if ((old_name == NULL) || (new_name == NULL)) {
785 media_svc_error("invalid file name");
786 return MS_MEDIA_ERR_INVALID_PARAMETER;
789 if (rename(old_name, new_name) < 0) {
790 media_svc_stderror(" ");
791 return MS_MEDIA_ERR_INTERNAL;
794 return MS_MEDIA_ERR_NONE;
797 int _media_svc_remove_file(const char *path)
801 result = remove(path);
803 media_svc_debug("success to remove file");
804 return MS_MEDIA_ERR_NONE;
806 media_svc_stderror("fail to remove file result");
807 return MS_MEDIA_ERR_INTERNAL;
811 int _media_svc_remove_all_files_in_dir(const char *dir_path)
814 struct dirent *result;
816 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
819 dir = opendir(dir_path);
821 media_svc_error("%s is not exist", dir_path);
822 return MS_MEDIA_ERR_INVALID_PARAMETER;
825 while (!readdir_r(dir, &entry, &result)) {
829 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
832 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
834 if (stat(filename, &st) != 0) {
837 if (S_ISDIR(st.st_mode)) {
840 if (unlink(filename) != 0) {
841 media_svc_stderror("failed to remove");
843 return MS_MEDIA_ERR_INTERNAL;
848 return MS_MEDIA_ERR_NONE;
851 char *_media_svc_get_thumb_internal_path(uid_t uid)
853 char *result_passwd = NULL;
854 struct group *grpinfo = NULL;
855 if (uid == getuid()) {
856 grpinfo = getgrnam("users");
857 if (grpinfo == NULL) {
858 media_svc_error("getgrnam(users) returns NULL !");
861 result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
863 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
864 struct passwd *userinfo = getpwuid(uid);
865 if (userinfo == NULL) {
866 media_svc_error("getpwuid(%d) returns NULL !", uid);
869 grpinfo = getgrnam("users");
870 if (grpinfo == NULL) {
871 media_svc_error("getgrnam(users) returns NULL !");
874 /* Compare git_t type and not group name */
875 if (grpinfo->gr_gid != userinfo->pw_gid) {
876 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
879 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
880 result_passwd = g_strdup(passwd_str);
883 return result_passwd;
886 char *_media_svc_get_thumb_external_path(uid_t uid)
888 char *result_passwd = NULL;
889 struct group *grpinfo = NULL;
890 if (uid == getuid()) {
891 grpinfo = getgrnam("users");
892 if (grpinfo == NULL) {
893 media_svc_error("getgrnam(users) returns NULL !");
896 result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
898 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
899 struct passwd *userinfo = getpwuid(uid);
900 if (userinfo == NULL) {
901 media_svc_error("getpwuid(%d) returns NULL !", uid);
904 grpinfo = getgrnam("users");
905 if (grpinfo == NULL) {
906 media_svc_error("getgrnam(users) returns NULL !");
909 /* Compare git_t type and not group name */
910 if (grpinfo->gr_gid != userinfo->pw_gid) {
911 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
914 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
915 result_passwd = g_strdup(passwd_str);
918 return result_passwd;
921 static int __media_svc_check_thumb_dir(const char *thumb_dir)
926 dir = opendir(thumb_dir);
930 media_svc_stderror("opendir fail");
931 if (errno == ENOENT) {
932 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
933 ret = mkdir(thumb_dir, 0777);
935 media_svc_error("make fail");
942 ret = chmod(thumb_dir, 0777);
944 media_svc_stderror("chmod failed");
946 ret = chown(thumb_dir, 5000, 5000);
948 media_svc_stderror("chown failed");
952 return MS_MEDIA_ERR_NONE;
958 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)
960 int ret = MS_MEDIA_ERR_NONE;
961 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
962 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
963 char *thumb_dir = NULL;
964 char hash[255 + 1] = {0, };
965 char *thumbfile_ext = NULL;
966 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
967 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
969 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
970 media_svc_error("fail to get thumbnail path");
971 SAFE_FREE(internal_thumb_path);
972 SAFE_FREE(external_thumb_path);
973 return MS_MEDIA_ERR_INTERNAL;
976 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
978 ret = __media_svc_check_thumb_dir(thumb_dir);
979 if (ret != MS_MEDIA_ERR_NONE) {
980 media_svc_error("__media_svc_check_thumb_dir");
981 SAFE_FREE(internal_thumb_path);
982 SAFE_FREE(external_thumb_path);
983 return MS_MEDIA_ERR_INTERNAL;
986 memset(file_ext, 0, sizeof(file_ext));
987 if (!__media_svc_get_file_ext(pathname, file_ext)) {
988 media_svc_error("get file ext fail");
991 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
992 if (ret != MS_MEDIA_ERR_NONE) {
993 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
994 SAFE_FREE(internal_thumb_path);
995 SAFE_FREE(external_thumb_path);
996 return MS_MEDIA_ERR_INTERNAL;
999 /*media_svc_debug("img format is [%s]", img_format); */
1001 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1002 thumbfile_ext = (char *)"jpg";
1003 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1004 thumbfile_ext = (char *)"png";
1005 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1006 thumbfile_ext = (char *)"gif";
1007 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1008 thumbfile_ext = (char *)"bmp";
1010 media_svc_error("Not proper img format");
1011 SAFE_FREE(internal_thumb_path);
1012 SAFE_FREE(external_thumb_path);
1013 return MS_MEDIA_ERR_INTERNAL;
1016 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1017 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1018 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1020 SAFE_FREE(internal_thumb_path);
1021 SAFE_FREE(external_thumb_path);
1023 return MS_MEDIA_ERR_NONE;
1026 int _media_svc_get_file_time(const char *full_path)
1028 struct stat statbuf;
1031 memset(&statbuf, 0, sizeof(struct stat));
1032 fd = stat(full_path, &statbuf);
1034 media_svc_error("stat(%s) fails.", full_path);
1035 return MS_MEDIA_ERR_INTERNAL;
1038 return statbuf.st_mtime;
1041 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1043 int ret = MS_MEDIA_ERR_NONE;
1045 /* Set default GPS value before extracting meta information */
1046 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1047 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1048 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1050 /* Set filename to title for all media */
1052 title = _media_svc_get_title_from_filepath(content_info->path);
1054 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1055 if (ret != MS_MEDIA_ERR_NONE)
1056 media_svc_error("strcpy error");
1059 media_svc_error("Can't extract title");
1060 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1061 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1064 /* Set default value before extracting meta information */
1065 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, 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.copyright, 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.track_num, 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, 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.artist, 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.album_artist, 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.genre, MEDIA_SVC_TAG_UNKNOWN);
1084 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1086 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1087 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1089 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1090 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1093 media_svc_debug("refresh");
1094 return MS_MEDIA_ERR_NONE;
1097 content_info->played_count = 0;
1098 content_info->last_played_time = 0;
1099 content_info->last_played_position = 0;
1100 content_info->favourate = 0;
1101 content_info->media_meta.rating = 0;
1103 return MS_MEDIA_ERR_NONE;
1106 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1107 const char *path, media_svc_media_type_e *media_type, bool refresh)
1109 int ret = MS_MEDIA_ERR_NONE;
1110 char * media_uuid = NULL;
1111 char * file_name = NULL;
1112 bool drm_type = false;
1113 char mime_type[256] = {0, };
1115 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1116 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1118 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1120 memset(&st, 0, sizeof(struct stat));
1121 if (stat(path, &st) == 0) {
1122 content_info->modified_time = st.st_mtime;
1123 content_info->timeline = content_info->modified_time;
1124 content_info->size = st.st_size;
1125 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1127 media_svc_stderror("stat failed");
1131 _media_svc_set_default_value(content_info, refresh);
1133 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1135 media_svc_debug("refresh");
1136 return MS_MEDIA_ERR_NONE;
1139 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1140 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1142 content_info->storage_type = storage_type;
1143 time(&content_info->added_time);
1145 media_uuid = _media_info_generate_uuid();
1146 if (media_uuid == NULL) {
1147 _media_svc_destroy_content_info(content_info);
1148 return MS_MEDIA_ERR_INTERNAL;
1151 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1152 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1154 file_name = g_path_get_basename(path);
1155 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1156 SAFE_FREE(file_name);
1157 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1159 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1160 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1161 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1162 ret = __media_svc_get_mime_type(path, mime_type);
1163 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1165 media_svc_debug("mime [%s]", mime_type);
1166 content_info->is_drm = drm_type;
1168 ret = __media_svc_get_media_type(path, mime_type, media_type);
1169 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1171 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1172 media_svc_error("invalid media_type condition[%d]", *media_type);
1173 return MS_MEDIA_ERR_INVALID_PARAMETER;
1176 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1177 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1179 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1181 content_info->media_type = *media_type;
1184 return MS_MEDIA_ERR_NONE;
1187 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1189 int ret = MS_MEDIA_ERR_NONE;
1191 int orient_value = 0;
1193 int exif_height = 0;
1194 ExifData *ed = NULL;
1195 int has_datetaken = FALSE;
1196 int datetaken_size = 19;
1197 double fnumber = 0.0;
1201 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1202 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1203 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1204 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1206 memset(buf, 0x00, sizeof(buf));
1207 memset(description_buf, 0x00, sizeof(description_buf));
1208 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1209 memset(model_buf, 0x00, sizeof(model_buf));
1211 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1212 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1213 return MS_MEDIA_ERR_INVALID_PARAMETER;
1216 path = content_info->path;
1217 if (!STRING_VALID(path)) {
1218 media_svc_error("Invalid Path");
1219 return MS_MEDIA_ERR_INVALID_PARAMETER;
1222 /* Load an ExifData object from an EXIF file */
1223 ed = exif_data_new_from_file(path);
1226 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1227 goto GET_WIDTH_HEIGHT;
1230 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1231 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1232 if (strlen(buf) > 0) {
1233 if (strcmp(buf, "S") == 0) {
1237 content_info->media_meta.latitude = value;
1239 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1242 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1245 memset(buf, 0x00, sizeof(buf));
1247 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1248 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1249 if (strlen(buf) > 0) {
1250 if (strcmp(buf, "W") == 0) {
1254 content_info->media_meta.longitude = value;
1256 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1259 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1262 memset(buf, 0x00, sizeof(buf));
1264 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1265 if (strlen(description_buf) == 0) {
1266 /*media_svc_debug("Use 'No description'"); */
1267 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1268 if (ret != MS_MEDIA_ERR_NONE)
1269 media_svc_error("strcpy error");
1271 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1272 if (ret != MS_MEDIA_ERR_NONE)
1273 media_svc_error("strcpy error");
1276 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1277 if (ret != MS_MEDIA_ERR_NONE)
1278 media_svc_error("strcpy error");
1281 memset(buf, 0x00, sizeof(buf));
1283 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1284 if (strlen(buf) == 0) {
1285 /*media_svc_debug("time is NULL"); */
1287 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1288 if (ret != MS_MEDIA_ERR_NONE) {
1289 media_svc_error("strcpy error");
1291 has_datetaken = TRUE;
1292 /* This is same as recorded_date */
1293 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1294 if (ret != MS_MEDIA_ERR_NONE)
1295 media_svc_error("strcpy error");
1300 memset(buf, 0x00, sizeof(buf));
1302 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1303 if (strlen(buf) == 0) {
1304 /*media_svc_debug("time is NULL"); */
1306 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1307 if (ret != MS_MEDIA_ERR_NONE) {
1308 media_svc_error("strcpy error");
1310 has_datetaken = TRUE;
1311 /* This is same as recorded_date */
1312 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1313 if (ret != MS_MEDIA_ERR_NONE)
1314 media_svc_error("strcpy error");
1319 if (has_datetaken) {
1320 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1321 if (content_info->timeline == 0) {
1322 content_info->timeline = content_info->modified_time;
1324 media_svc_debug("Timeline : %ld", content_info->timeline);
1328 /* Get exposure_time value from exif. */
1329 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1330 if (strlen(exposure_time_buf) == 0) {
1331 //media_svc_debug("exposure_time_buf is NULL");
1333 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1334 if (ret != MS_MEDIA_ERR_NONE)
1335 media_svc_error("strcpy error");
1339 /* Get fnumber value from exif. */
1340 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1341 if (fnumber > 0.0) {
1342 content_info->media_meta.fnumber = fnumber;
1344 content_info->media_meta.fnumber = 0.0;
1347 content_info->media_meta.fnumber = 0.0;
1350 /* Get iso value from exif. */
1351 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1353 content_info->media_meta.iso = iso;
1355 content_info->media_meta.iso = 0;
1358 content_info->media_meta.iso = 0;
1361 /* Get model value from exif. */
1362 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1363 if (strlen(model_buf) == 0) {
1364 //media_svc_debug("model_buf is NULL");
1366 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1367 if (ret != MS_MEDIA_ERR_NONE)
1368 media_svc_error("strcpy error");
1372 /* Get orientation value from exif. */
1373 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1374 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1375 content_info->media_meta.orientation = orient_value;
1377 content_info->media_meta.orientation = 0;
1380 content_info->media_meta.orientation = 0;
1383 /* Get width value from exif. */
1384 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1385 if (exif_width > 0) {
1386 content_info->media_meta.width = exif_width;
1388 content_info->media_meta.width = 0;
1391 content_info->media_meta.width = 0;
1394 /* Get height value from exif. */
1395 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1396 if (exif_height > 0) {
1397 content_info->media_meta.height = exif_height;
1399 content_info->media_meta.height = 0;
1402 content_info->media_meta.height = 0;
1405 if (ed != NULL) exif_data_unref(ed);
1409 if (content_info->media_meta.width == 0 ||
1410 content_info->media_meta.height == 0) {
1411 /*Get image width, height*/
1412 unsigned int img_width = 0;
1413 unsigned int img_height = 0;
1414 ImgCodecType img_type = IMG_CODEC_NONE;
1416 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1418 if (content_info->media_meta.width == 0)
1419 content_info->media_meta.width = img_width;
1421 if (content_info->media_meta.height == 0)
1422 content_info->media_meta.height = img_height;
1425 return MS_MEDIA_ERR_NONE;
1428 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1430 MMHandleType tag = 0;
1433 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1434 int mmf_error = FILEINFO_ERROR_NONE;
1435 char *err_attr_name = NULL;
1438 int ret = MS_MEDIA_ERR_NONE;
1440 /*Get Content Tag attribute ===========*/
1441 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1443 if (mmf_error == FILEINFO_ERROR_NONE) {
1444 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1445 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1446 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1447 if (ret != MS_MEDIA_ERR_NONE)
1448 media_svc_error("strcpy error");
1450 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1452 SAFE_FREE(err_attr_name);
1453 /*media_svc_debug("album - unknown"); */
1456 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1457 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1458 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1459 if (ret != MS_MEDIA_ERR_NONE)
1460 media_svc_error("strcpy error");
1461 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1463 SAFE_FREE(err_attr_name);
1464 /*media_svc_debug("artist - unknown"); */
1467 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1468 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1469 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1470 if (ret != MS_MEDIA_ERR_NONE)
1471 media_svc_error("strcpy error");
1472 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1474 SAFE_FREE(err_attr_name);
1475 /*media_svc_debug("album_artist - unknown"); */
1478 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1479 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1480 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1481 if (ret != MS_MEDIA_ERR_NONE)
1482 media_svc_error("strcpy error");
1484 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1485 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1487 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1488 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1492 SAFE_FREE(err_attr_name);
1493 /*media_svc_debug("genre - unknown"); */
1496 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1497 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1499 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1500 if (ret != MS_MEDIA_ERR_NONE)
1501 media_svc_error("strcpy error");
1503 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1507 for (idx = 0; idx < size; idx++) {
1509 media_svc_debug("SPACE [%s]", p);
1513 media_svc_debug("Not SPACE [%s]", p);
1514 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1515 if (ret != MS_MEDIA_ERR_NONE)
1516 media_svc_error("strcpy error");
1522 media_svc_debug("Can't extract title. All string is space");
1523 title = _media_svc_get_title_from_filepath(content_info->path);
1525 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1526 if (ret != MS_MEDIA_ERR_NONE)
1527 media_svc_error("strcpy error");
1530 media_svc_error("Can't extract title");
1535 SAFE_FREE(err_attr_name);
1536 title = _media_svc_get_title_from_filepath(content_info->path);
1538 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1539 if (ret != MS_MEDIA_ERR_NONE)
1540 media_svc_error("strcpy error");
1543 media_svc_error("Can't extract title");
1547 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1548 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1549 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1550 if (ret != MS_MEDIA_ERR_NONE)
1551 media_svc_error("strcpy error");
1552 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1554 SAFE_FREE(err_attr_name);
1557 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1558 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1559 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1560 if (ret != MS_MEDIA_ERR_NONE)
1561 media_svc_error("strcpy error");
1562 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1563 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1565 /*media_svc_debug("composer - unknown"); */
1566 SAFE_FREE(err_attr_name);
1569 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1570 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1571 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1572 if (ret != MS_MEDIA_ERR_NONE)
1573 media_svc_error("strcpy error");
1574 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1575 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1577 /*media_svc_debug("copyright - unknown"); */
1578 SAFE_FREE(err_attr_name);
1581 mmf_error = mm_file_destroy_tag_attrs(tag);
1582 if (mmf_error != FILEINFO_ERROR_NONE) {
1583 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1586 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1587 char *no_tag_title = NULL;
1588 media_svc_error("no tag information");
1590 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1592 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1593 if (ret != MS_MEDIA_ERR_NONE)
1594 media_svc_error("strcpy error");
1595 SAFE_FREE(no_tag_title);
1597 media_svc_error("Can't extract title");
1600 content_info->album_id = album_id;
1603 return MS_MEDIA_ERR_NONE;
1606 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1608 MMHandleType content = 0;
1609 MMHandleType tag = 0;
1613 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1614 int mmf_error = FILEINFO_ERROR_NONE;
1615 char *err_attr_name = NULL;
1617 bool extract_thumbnail = FALSE;
1618 bool append_album = FALSE;
1620 int ret = MS_MEDIA_ERR_NONE;
1622 int resize_size = -1;
1623 void *resize_image = NULL;
1625 /*Get Content Tag attribute ===========*/
1626 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1628 if (mmf_error == FILEINFO_ERROR_NONE) {
1629 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1630 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1631 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1632 if (ret != MS_MEDIA_ERR_NONE)
1633 media_svc_error("strcpy error");
1635 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1637 SAFE_FREE(err_attr_name);
1638 /*media_svc_debug("album - unknown"); */
1641 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1642 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1643 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1644 if (ret != MS_MEDIA_ERR_NONE)
1645 media_svc_error("strcpy error");
1646 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1648 SAFE_FREE(err_attr_name);
1649 /*media_svc_debug("artist - unknown"); */
1652 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1653 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1654 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1655 if (ret != MS_MEDIA_ERR_NONE)
1656 media_svc_error("strcpy error");
1657 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1659 SAFE_FREE(err_attr_name);
1660 /*media_svc_debug("album_artist - unknown"); */
1663 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1664 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1665 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1666 if (ret != MS_MEDIA_ERR_NONE)
1667 media_svc_error("strcpy error");
1669 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1670 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1672 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1673 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1677 SAFE_FREE(err_attr_name);
1678 /*media_svc_debug("genre - unknown"); */
1681 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1682 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1684 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1685 if (ret != MS_MEDIA_ERR_NONE)
1686 media_svc_error("strcpy error");
1688 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1692 for (idx = 0; idx < size; idx++) {
1694 media_svc_debug("SPACE [%s]", p);
1698 media_svc_debug("Not SPACE [%s]", p);
1699 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1700 if (ret != MS_MEDIA_ERR_NONE)
1701 media_svc_error("strcpy error");
1707 media_svc_debug("Can't extract title. All string is space");
1708 title = _media_svc_get_title_from_filepath(content_info->path);
1710 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1711 if (ret != MS_MEDIA_ERR_NONE)
1712 media_svc_error("strcpy error");
1715 media_svc_error("Can't extract title");
1720 SAFE_FREE(err_attr_name);
1721 title = _media_svc_get_title_from_filepath(content_info->path);
1723 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1724 if (ret != MS_MEDIA_ERR_NONE)
1725 media_svc_error("strcpy error");
1728 media_svc_error("Can't extract title");
1732 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1733 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1734 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1735 if (ret != MS_MEDIA_ERR_NONE)
1736 media_svc_error("strcpy error");
1737 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1739 SAFE_FREE(err_attr_name);
1742 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1743 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1744 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1745 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1746 char time_info[64] = {0, };
1747 char p_value[64] = {0, };
1749 memset(time_info, 0x00, sizeof(time_info));
1750 memset(p_value, 0x00, sizeof(p_value));
1751 strncpy(p_value, p, size);
1752 for (idx = 0; idx < size; idx++) {
1753 if (p_value[idx] == '-') {
1754 time_info[idx] = ':';
1755 } else if (p_value[idx] != '\0') {
1756 time_info[idx] = p_value[idx];
1758 media_svc_error("strcpy error");
1762 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1764 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1767 if (ret != MS_MEDIA_ERR_NONE) {
1768 media_svc_error("strcpy error");
1770 /* This is same as datetaken */
1772 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1774 int datetaken_size = 19;
1775 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1777 if (ret != MS_MEDIA_ERR_NONE)
1778 media_svc_error("strcpy error");
1780 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1781 if (content_info->timeline == 0) {
1782 content_info->timeline = content_info->modified_time;
1784 media_svc_debug("Timeline : %ld", content_info->timeline);
1787 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1789 SAFE_FREE(err_attr_name);
1792 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1793 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1794 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1795 if (ret != MS_MEDIA_ERR_NONE)
1796 media_svc_error("strcpy error");
1797 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1798 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1800 /*media_svc_debug("composer - unknown"); */
1801 SAFE_FREE(err_attr_name);
1804 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1805 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1806 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1807 if (ret != MS_MEDIA_ERR_NONE)
1808 media_svc_error("strcpy error");
1809 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1810 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1812 /*media_svc_debug("copyright - unknown"); */
1813 SAFE_FREE(err_attr_name);
1816 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1817 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1818 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1819 if (ret != MS_MEDIA_ERR_NONE)
1820 media_svc_error("strcpy error");
1822 SAFE_FREE(err_attr_name);
1825 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1826 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1828 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1829 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1830 if (ret != MS_MEDIA_ERR_NONE)
1831 media_svc_error("strcpy error");
1833 media_svc_debug("Wrong Year Information [%s]", p);
1836 SAFE_FREE(err_attr_name);
1839 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1840 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1841 content_info->media_meta.rating = atoi(p);
1843 SAFE_FREE(err_attr_name);
1844 content_info->media_meta.rating = 0;
1847 /*Initialize album_id to 0. below code will set the album_id*/
1848 content_info->album_id = album_id;
1849 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1851 if (ret != MS_MEDIA_ERR_NONE) {
1852 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1853 media_svc_debug("album does not exist. So start to make album art");
1854 extract_thumbnail = TRUE;
1855 append_album = TRUE;
1857 extract_thumbnail = TRUE;
1858 append_album = FALSE;
1861 content_info->album_id = album_id;
1862 append_album = FALSE;
1864 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1865 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1867 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1868 extract_thumbnail = TRUE;
1870 media_svc_debug("album already exists. don't need to make album art");
1871 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1872 extract_thumbnail = TRUE;
1876 /*Do not extract artwork for the USB Storage content*/
1877 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1878 extract_thumbnail = FALSE;
1880 if (extract_thumbnail == TRUE) {
1881 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1882 if (mmf_error != FILEINFO_ERROR_NONE) {
1883 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1884 SAFE_FREE(err_attr_name);
1886 /*media_svc_debug("artwork size1 [%d]", size); */
1889 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1890 if (mmf_error != FILEINFO_ERROR_NONE) {
1891 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1892 SAFE_FREE(err_attr_name);
1894 /*media_svc_debug("artwork size2 [%d]", size); */
1896 if (image != NULL && size > 0) {
1897 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1898 int artwork_mime_size = -1;
1900 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1901 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1902 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1903 if (ret != MS_MEDIA_ERR_NONE) {
1904 media_svc_error("Fail to Get Thumbnail Path");
1906 /* albumart resizing */
1907 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1909 SAFE_FREE(err_attr_name);
1912 if (strlen(thumb_path) > 0) {
1913 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1914 if (ret != MS_MEDIA_ERR_NONE) {
1915 media_svc_error("Fail to Save Thumbnail Image");
1917 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1918 if (ret != MS_MEDIA_ERR_NONE)
1919 media_svc_error("strcpy error");
1925 if (append_album == TRUE) {
1926 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1927 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1928 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1930 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1932 content_info->album_id = album_id;
1935 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1936 double longitude = 0.0;
1937 double latitude = 0.0;
1938 double altitude = 0.0;
1940 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1941 content_info->media_meta.longitude = longitude;
1942 content_info->media_meta.latitude = latitude;
1943 content_info->media_meta.altitude = altitude;
1945 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1946 if (mmf_error != FILEINFO_ERROR_NONE) {
1948 SAFE_FREE(err_attr_name);
1951 media_svc_debug("CDIS : %d", cdis_value);
1954 mmf_error = mm_file_destroy_tag_attrs(tag);
1955 if (mmf_error != FILEINFO_ERROR_NONE) {
1956 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1959 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1960 char *no_tag_title = NULL;
1961 media_svc_error("no tag information");
1963 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1965 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1966 if (ret != MS_MEDIA_ERR_NONE)
1967 media_svc_error("strcpy error");
1968 SAFE_FREE(no_tag_title);
1970 media_svc_error("Can't extract title");
1973 content_info->album_id = album_id;
1976 /*Get Content attribute ===========*/
1977 if (cdis_value == 1) {
1978 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1980 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1983 if (mmf_error == FILEINFO_ERROR_NONE) {
1984 /*Common attribute*/
1985 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1986 if (mmf_error != FILEINFO_ERROR_NONE) {
1987 SAFE_FREE(err_attr_name);
1988 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1990 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1993 /*Sound/Music attribute*/
1994 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1996 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1997 if (mmf_error != FILEINFO_ERROR_NONE) {
1998 SAFE_FREE(err_attr_name);
1999 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2001 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2004 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2005 if (mmf_error != FILEINFO_ERROR_NONE) {
2006 SAFE_FREE(err_attr_name);
2007 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2009 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2012 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2013 if (mmf_error != FILEINFO_ERROR_NONE) {
2014 SAFE_FREE(err_attr_name);
2015 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2017 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2020 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2021 if (mmf_error != FILEINFO_ERROR_NONE) {
2022 SAFE_FREE(err_attr_name);
2023 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2025 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2027 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2028 int audio_bitrate = 0;
2029 int video_bitrate = 0;
2031 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2032 if (mmf_error != FILEINFO_ERROR_NONE) {
2033 SAFE_FREE(err_attr_name);
2034 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2036 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2039 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2040 if (mmf_error != FILEINFO_ERROR_NONE) {
2041 SAFE_FREE(err_attr_name);
2042 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2044 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2047 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2049 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2050 if (mmf_error != FILEINFO_ERROR_NONE) {
2051 SAFE_FREE(err_attr_name);
2052 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2054 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2057 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2058 if (mmf_error != FILEINFO_ERROR_NONE) {
2059 SAFE_FREE(err_attr_name);
2060 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2062 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2066 media_svc_error("Not support type");
2067 mmf_error = mm_file_destroy_content_attrs(content);
2068 if (mmf_error != FILEINFO_ERROR_NONE) {
2069 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2071 return MS_MEDIA_ERR_INVALID_PARAMETER;
2074 mmf_error = mm_file_destroy_content_attrs(content);
2075 if (mmf_error != FILEINFO_ERROR_NONE) {
2076 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2079 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2082 return MS_MEDIA_ERR_NONE;
2085 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2087 media_svc_retm_if(content_info == NULL, "content info is NULL");
2089 /* Delete media_svc_content_info_s */
2090 SAFE_FREE(content_info->media_uuid);
2091 SAFE_FREE(content_info->path);
2092 SAFE_FREE(content_info->file_name);
2093 SAFE_FREE(content_info->mime_type);
2094 SAFE_FREE(content_info->folder_uuid);
2095 SAFE_FREE(content_info->thumbnail_path);
2096 SAFE_FREE(content_info->storage_uuid);
2098 /* Delete media_svc_content_meta_s */
2099 SAFE_FREE(content_info->media_meta.title);
2100 SAFE_FREE(content_info->media_meta.album);
2101 SAFE_FREE(content_info->media_meta.artist);
2102 SAFE_FREE(content_info->media_meta.album_artist);
2103 SAFE_FREE(content_info->media_meta.genre);
2104 SAFE_FREE(content_info->media_meta.composer);
2105 SAFE_FREE(content_info->media_meta.year);
2106 SAFE_FREE(content_info->media_meta.recorded_date);
2107 SAFE_FREE(content_info->media_meta.copyright);
2108 SAFE_FREE(content_info->media_meta.track_num);
2109 SAFE_FREE(content_info->media_meta.description);
2110 SAFE_FREE(content_info->media_meta.datetaken);
2111 SAFE_FREE(content_info->media_meta.exposure_time);
2112 SAFE_FREE(content_info->media_meta.model);
2113 SAFE_FREE(content_info->media_meta.weather);
2114 SAFE_FREE(content_info->media_meta.category);
2115 SAFE_FREE(content_info->media_meta.keyword);
2116 SAFE_FREE(content_info->media_meta.location_tag);
2117 SAFE_FREE(content_info->media_meta.content_name);
2118 SAFE_FREE(content_info->media_meta.age_rating);
2119 SAFE_FREE(content_info->media_meta.author);
2120 SAFE_FREE(content_info->media_meta.provider);
2122 SAFE_FREE(content_info->media_meta.title_pinyin);
2123 SAFE_FREE(content_info->media_meta.album_pinyin);
2124 SAFE_FREE(content_info->media_meta.artist_pinyin);
2125 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2126 SAFE_FREE(content_info->media_meta.genre_pinyin);
2127 SAFE_FREE(content_info->media_meta.composer_pinyin);
2128 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2129 SAFE_FREE(content_info->media_meta.description_pinyin);
2134 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2136 if (STRING_VALID(path)) {
2137 char *internal_path = _media_svc_get_path(uid);
2138 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2139 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2140 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2141 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2142 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2143 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2145 media_svc_error("Invalid Path");
2146 SAFE_FREE(internal_path);
2147 return MS_MEDIA_ERR_INVALID_PARAMETER;
2149 SAFE_FREE(internal_path);
2151 media_svc_error("INVALID parameter");
2152 return MS_MEDIA_ERR_INVALID_PARAMETER;
2155 return MS_MEDIA_ERR_NONE;
2158 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2161 size_t i, count = 0;
2162 size_t oldlen = strlen(olds);
2163 if (oldlen < 1) return s;
2164 size_t newlen = strlen(news);
2166 if (newlen != oldlen) {
2167 for (i = 0; s[i] != '\0';) {
2168 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2171 } else i = strlen(s);
2174 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2175 if (result == NULL) return NULL;
2179 if (memcmp(s, olds, oldlen) == 0) {
2180 memcpy(sr, news, newlen);
2183 } else *sr++ = *s++;
2191 bool _media_svc_is_drm_file(const char *path)
2196 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)
2198 int ret = MS_MEDIA_ERR_NONE;
2200 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2202 if (ret != MS_MEDIA_ERR_NONE) {
2203 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2204 ret = MS_MEDIA_ERR_INTERNAL;
2206 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2212 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2214 int ret = MS_MEDIA_ERR_NONE;
2216 pinyin_name_s *pinyinname = NULL;
2220 if (!STRING_VALID(src_str)) {
2221 media_svc_debug("String is invalid");
2225 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2226 if (ret == MS_MEDIA_ERR_NONE) {
2227 if (STRING_VALID(pinyinname[0].pinyin_name))
2228 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2230 *pinyin_str = strdup(src_str); //Return Original Non China Character
2233 _media_svc_pinyin_free(pinyinname, size);
2238 bool _media_svc_check_pinyin_support(void)
2244 int _media_svc_get_ini_value()
2246 if (g_ini_value == -1) {
2247 dictionary *dict = NULL;
2249 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2251 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2255 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2256 iniparser_freedict(dict);
2258 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2263 char* _media_svc_get_title_from_path(const char *path)
2265 char *filename = NULL;
2268 int filename_len = -1;
2269 int new_title_len = -1;
2272 media_svc_error("path is NULL");
2276 filename = g_path_get_basename(path);
2277 if (!STRING_VALID(filename)) {
2278 media_svc_error("wrong file name");
2279 SAFE_FREE(filename);
2283 filename_len = strlen(filename);
2285 ext = g_strrstr(filename, ".");
2287 media_svc_error("there is no file extention");
2291 new_title_len = filename_len - strlen(ext);
2292 if (new_title_len < 1) {
2293 media_svc_error("title length is zero");
2294 SAFE_FREE(filename);
2298 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2300 SAFE_FREE(filename);
2302 media_svc_debug("extract title is [%s]", title);
2307 #define BUF_LENGHT 256
2309 void _media_svc_print_stderror(void)
2311 char buf[BUF_LENGHT] = {0,};
2313 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));