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);
638 memset(resized_raw_image, 0, buf_size);
640 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
641 if (ret != MS_MEDIA_ERR_NONE) {
642 media_svc_error("image_util_resize failed");
643 *resize_image = image;
645 SAFE_FREE(raw_image);
646 return MS_MEDIA_ERR_NONE;
648 SAFE_FREE(raw_image);
651 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
652 if (ret != MS_MEDIA_ERR_NONE) {
653 media_svc_error("image_util_encode_jpeg_to_memory failed");
654 *resize_image = image;
656 return MS_MEDIA_ERR_NONE;
657 SAFE_FREE(resized_raw_image);
659 SAFE_FREE(resized_raw_image);
661 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
662 media_svc_debug("type [png] size [%d]", size);
663 *resize_image = image;
667 media_svc_debug("Not proper img format");
668 *resize_image = image;
675 static int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
677 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
680 media_svc_error("invalid image..");
681 return MS_MEDIA_ERR_INVALID_PARAMETER;
685 char *thumb_path = __media_svc_get_thumb_path(uid);
686 if (!STRING_VALID(thumb_path)) {
687 media_svc_error("fail to get thumb_path");
688 return MS_MEDIA_ERR_INTERNAL;
691 if (-1 == statfs(thumb_path, &fs)) {
692 media_svc_error("error in statfs");
693 SAFE_FREE(thumb_path);
694 return MS_MEDIA_ERR_INTERNAL;
697 SAFE_FREE(thumb_path);
699 long bsize_kbytes = fs.f_bsize >> 10;
701 if ((bsize_kbytes * fs.f_bavail) < 1024) {
702 media_svc_error("not enought space...");
703 return MS_MEDIA_ERR_INTERNAL;
708 if (image != NULL && size > 0) {
709 fp = fopen(image_path, "w");
712 media_svc_error("failed to open file");
713 return MS_MEDIA_ERR_INTERNAL;
716 nwrite = fwrite(image, 1, size, fp);
717 if (nwrite != size) {
718 media_svc_error("failed to write thumbnail");
720 return MS_MEDIA_ERR_INTERNAL;
725 return MS_MEDIA_ERR_NONE;
728 static char *_media_svc_get_title_from_filepath(const char *path)
730 char *filename = NULL;
733 int filename_len = -1;
734 int new_title_len = -1;
737 media_svc_error("path is NULL");
741 filename = g_path_get_basename(path);
742 if (!STRING_VALID(filename)) {
743 media_svc_error("wrong file name");
748 filename_len = strlen(filename);
750 ext = g_strrstr(filename, ".");
752 media_svc_error("there is no file extention");
756 new_title_len = filename_len - strlen(ext);
757 if (new_title_len < 1) {
758 media_svc_error("title length is zero");
763 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
767 media_svc_debug("extract title is [%s]", title);
772 int _media_svc_rename_file(const char *old_name, const char *new_name)
774 if ((old_name == NULL) || (new_name == NULL)) {
775 media_svc_error("invalid file name");
776 return MS_MEDIA_ERR_INVALID_PARAMETER;
779 if (rename(old_name, new_name) < 0) {
780 media_svc_stderror(" ");
781 return MS_MEDIA_ERR_INTERNAL;
784 return MS_MEDIA_ERR_NONE;
787 int _media_svc_remove_file(const char *path)
791 result = remove(path);
793 media_svc_debug("success to remove file");
794 return MS_MEDIA_ERR_NONE;
796 media_svc_stderror("fail to remove file result");
797 return MS_MEDIA_ERR_INTERNAL;
801 int _media_svc_remove_all_files_in_dir(const char *dir_path)
804 struct dirent *result;
806 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
809 dir = opendir(dir_path);
811 media_svc_error("%s is not exist", dir_path);
812 return MS_MEDIA_ERR_INVALID_PARAMETER;
815 while (!readdir_r(dir, &entry, &result)) {
819 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
822 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
824 if (stat(filename, &st) != 0) {
827 if (S_ISDIR(st.st_mode)) {
830 if (unlink(filename) != 0) {
831 media_svc_stderror("failed to remove");
833 return MS_MEDIA_ERR_INTERNAL;
838 return MS_MEDIA_ERR_NONE;
841 char *_media_svc_get_thumb_internal_path(uid_t uid)
843 char *result_passwd = NULL;
844 struct group *grpinfo = NULL;
845 if (uid == getuid()) {
846 grpinfo = getgrnam("users");
847 if (grpinfo == NULL) {
848 media_svc_error("getgrnam(users) returns NULL !");
851 result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
853 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
854 struct passwd *userinfo = getpwuid(uid);
855 if (userinfo == NULL) {
856 media_svc_error("getpwuid(%d) returns NULL !", uid);
859 grpinfo = getgrnam("users");
860 if (grpinfo == NULL) {
861 media_svc_error("getgrnam(users) returns NULL !");
864 /* Compare git_t type and not group name */
865 if (grpinfo->gr_gid != userinfo->pw_gid) {
866 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
869 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
870 result_passwd = g_strdup(passwd_str);
873 return result_passwd;
876 char *_media_svc_get_thumb_external_path(uid_t uid)
878 char *result_passwd = NULL;
879 struct group *grpinfo = NULL;
880 if (uid == getuid()) {
881 grpinfo = getgrnam("users");
882 if (grpinfo == NULL) {
883 media_svc_error("getgrnam(users) returns NULL !");
886 result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
888 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
889 struct passwd *userinfo = getpwuid(uid);
890 if (userinfo == NULL) {
891 media_svc_error("getpwuid(%d) returns NULL !", uid);
894 grpinfo = getgrnam("users");
895 if (grpinfo == NULL) {
896 media_svc_error("getgrnam(users) returns NULL !");
899 /* Compare git_t type and not group name */
900 if (grpinfo->gr_gid != userinfo->pw_gid) {
901 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
904 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
905 result_passwd = g_strdup(passwd_str);
908 return result_passwd;
911 static int __media_svc_check_thumb_dir(const char *thumb_dir)
916 dir = opendir(thumb_dir);
920 media_svc_stderror("opendir fail");
921 if (errno == ENOENT) {
922 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
923 ret = mkdir(thumb_dir, 0777);
925 media_svc_error("make fail");
932 ret = chmod(thumb_dir, 0777);
934 media_svc_stderror("chmod failed");
936 ret = chown(thumb_dir, 5000, 5000);
938 media_svc_stderror("chown failed");
942 return MS_MEDIA_ERR_NONE;
948 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)
950 int ret = MS_MEDIA_ERR_NONE;
951 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
952 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
953 char *thumb_dir = NULL;
954 char hash[255 + 1] = {0, };
955 char *thumbfile_ext = NULL;
956 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
957 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
959 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
960 media_svc_error("fail to get thumbnail path");
961 SAFE_FREE(internal_thumb_path);
962 SAFE_FREE(external_thumb_path);
963 return MS_MEDIA_ERR_INTERNAL;
966 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
968 ret = __media_svc_check_thumb_dir(thumb_dir);
969 if (ret != MS_MEDIA_ERR_NONE) {
970 media_svc_error("__media_svc_check_thumb_dir");
971 SAFE_FREE(internal_thumb_path);
972 SAFE_FREE(external_thumb_path);
973 return MS_MEDIA_ERR_INTERNAL;
976 memset(file_ext, 0, sizeof(file_ext));
977 if (!__media_svc_get_file_ext(pathname, file_ext)) {
978 media_svc_error("get file ext fail");
981 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
982 if (ret != MS_MEDIA_ERR_NONE) {
983 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
984 SAFE_FREE(internal_thumb_path);
985 SAFE_FREE(external_thumb_path);
986 return MS_MEDIA_ERR_INTERNAL;
989 /*media_svc_debug("img format is [%s]", img_format); */
991 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
992 thumbfile_ext = (char *)"jpg";
993 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
994 thumbfile_ext = (char *)"png";
995 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
996 thumbfile_ext = (char *)"gif";
997 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
998 thumbfile_ext = (char *)"bmp";
1000 media_svc_error("Not proper img format");
1001 SAFE_FREE(internal_thumb_path);
1002 SAFE_FREE(external_thumb_path);
1003 return MS_MEDIA_ERR_INTERNAL;
1006 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1007 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1008 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1010 SAFE_FREE(internal_thumb_path);
1011 SAFE_FREE(external_thumb_path);
1013 return MS_MEDIA_ERR_NONE;
1016 int _media_svc_get_file_time(const char *full_path)
1018 struct stat statbuf;
1021 memset(&statbuf, 0, sizeof(struct stat));
1022 fd = stat(full_path, &statbuf);
1024 media_svc_error("stat(%s) fails.", full_path);
1025 return MS_MEDIA_ERR_INTERNAL;
1028 return statbuf.st_mtime;
1031 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1033 int ret = MS_MEDIA_ERR_NONE;
1035 /* Set default GPS value before extracting meta information */
1036 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1037 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1038 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1040 /* Set filename to title for all media */
1042 title = _media_svc_get_title_from_filepath(content_info->path);
1044 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1045 if (ret != MS_MEDIA_ERR_NONE)
1046 media_svc_error("strcpy error");
1049 media_svc_error("Can't extract title");
1050 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1051 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1054 /* Set default value before extracting meta information */
1055 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1056 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1058 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1059 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1061 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1062 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1064 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1065 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1067 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1068 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1070 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1071 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1073 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1074 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1076 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1077 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1079 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1080 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1083 media_svc_debug("refresh");
1084 return MS_MEDIA_ERR_NONE;
1087 content_info->played_count = 0;
1088 content_info->last_played_time = 0;
1089 content_info->last_played_position = 0;
1090 content_info->favourate = 0;
1091 content_info->media_meta.rating = 0;
1093 return MS_MEDIA_ERR_NONE;
1096 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1097 const char *path, media_svc_media_type_e *media_type, bool refresh)
1099 int ret = MS_MEDIA_ERR_NONE;
1100 char * media_uuid = NULL;
1101 char * file_name = NULL;
1102 bool drm_type = false;
1103 char mime_type[256] = {0, };
1105 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1106 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1108 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1110 memset(&st, 0, sizeof(struct stat));
1111 if (stat(path, &st) == 0) {
1112 content_info->modified_time = st.st_mtime;
1113 content_info->timeline = content_info->modified_time;
1114 content_info->size = st.st_size;
1115 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1117 media_svc_stderror("stat failed");
1121 _media_svc_set_default_value(content_info, refresh);
1123 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1125 media_svc_debug("refresh");
1126 return MS_MEDIA_ERR_NONE;
1129 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1130 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1132 content_info->storage_type = storage_type;
1133 time(&content_info->added_time);
1135 media_uuid = _media_info_generate_uuid();
1136 if (media_uuid == NULL) {
1137 _media_svc_destroy_content_info(content_info);
1138 return MS_MEDIA_ERR_INTERNAL;
1141 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1142 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1144 file_name = g_path_get_basename(path);
1145 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1146 SAFE_FREE(file_name);
1147 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1149 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1150 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1151 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1152 ret = __media_svc_get_mime_type(path, mime_type);
1153 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1155 media_svc_debug("mime [%s]", mime_type);
1156 content_info->is_drm = drm_type;
1158 ret = __media_svc_get_media_type(path, mime_type, media_type);
1159 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1161 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1162 media_svc_error("invalid media_type condition[%d]", *media_type);
1163 return MS_MEDIA_ERR_INVALID_PARAMETER;
1166 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1167 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1169 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1171 content_info->media_type = *media_type;
1174 return MS_MEDIA_ERR_NONE;
1177 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1179 int ret = MS_MEDIA_ERR_NONE;
1181 int orient_value = 0;
1183 int exif_height = 0;
1184 ExifData *ed = NULL;
1185 int has_datetaken = FALSE;
1186 int datetaken_size = 19;
1187 double fnumber = 0.0;
1191 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1192 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1193 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1194 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1196 memset(buf, 0x00, sizeof(buf));
1197 memset(description_buf, 0x00, sizeof(description_buf));
1198 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1199 memset(model_buf, 0x00, sizeof(model_buf));
1201 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1202 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1203 return MS_MEDIA_ERR_INVALID_PARAMETER;
1206 path = content_info->path;
1207 if (!STRING_VALID(path)) {
1208 media_svc_error("Invalid Path");
1209 return MS_MEDIA_ERR_INVALID_PARAMETER;
1212 /* Load an ExifData object from an EXIF file */
1213 ed = exif_data_new_from_file(path);
1216 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1217 goto GET_WIDTH_HEIGHT;
1220 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1221 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1222 if (strlen(buf) > 0) {
1223 if (strcmp(buf, "S") == 0) {
1227 content_info->media_meta.latitude = value;
1229 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1232 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1235 memset(buf, 0x00, sizeof(buf));
1237 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1238 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1239 if (strlen(buf) > 0) {
1240 if (strcmp(buf, "W") == 0) {
1244 content_info->media_meta.longitude = value;
1246 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1249 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1252 memset(buf, 0x00, sizeof(buf));
1254 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1255 if (strlen(description_buf) == 0) {
1256 /*media_svc_debug("Use 'No description'"); */
1257 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1258 if (ret != MS_MEDIA_ERR_NONE)
1259 media_svc_error("strcpy error");
1261 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1262 if (ret != MS_MEDIA_ERR_NONE)
1263 media_svc_error("strcpy error");
1266 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1267 if (ret != MS_MEDIA_ERR_NONE)
1268 media_svc_error("strcpy error");
1271 memset(buf, 0x00, sizeof(buf));
1273 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1274 if (strlen(buf) == 0) {
1275 /*media_svc_debug("time is NULL"); */
1277 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1278 if (ret != MS_MEDIA_ERR_NONE) {
1279 media_svc_error("strcpy error");
1281 has_datetaken = TRUE;
1282 /* This is same as recorded_date */
1283 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1284 if (ret != MS_MEDIA_ERR_NONE)
1285 media_svc_error("strcpy error");
1290 memset(buf, 0x00, sizeof(buf));
1292 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1293 if (strlen(buf) == 0) {
1294 /*media_svc_debug("time is NULL"); */
1296 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1297 if (ret != MS_MEDIA_ERR_NONE) {
1298 media_svc_error("strcpy error");
1300 has_datetaken = TRUE;
1301 /* This is same as recorded_date */
1302 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1303 if (ret != MS_MEDIA_ERR_NONE)
1304 media_svc_error("strcpy error");
1309 if (has_datetaken) {
1310 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1311 if (content_info->timeline == 0) {
1312 content_info->timeline = content_info->modified_time;
1314 media_svc_debug("Timeline : %ld", content_info->timeline);
1318 /* Get exposure_time value from exif. */
1319 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1320 if (strlen(exposure_time_buf) == 0) {
1321 //media_svc_debug("exposure_time_buf is NULL");
1323 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1324 if (ret != MS_MEDIA_ERR_NONE)
1325 media_svc_error("strcpy error");
1329 /* Get fnumber value from exif. */
1330 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1331 if (fnumber > 0.0) {
1332 content_info->media_meta.fnumber = fnumber;
1334 content_info->media_meta.fnumber = 0.0;
1337 content_info->media_meta.fnumber = 0.0;
1340 /* Get iso value from exif. */
1341 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1343 content_info->media_meta.iso = iso;
1345 content_info->media_meta.iso = 0;
1348 content_info->media_meta.iso = 0;
1351 /* Get model value from exif. */
1352 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1353 if (strlen(model_buf) == 0) {
1354 //media_svc_debug("model_buf is NULL");
1356 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1357 if (ret != MS_MEDIA_ERR_NONE)
1358 media_svc_error("strcpy error");
1362 /* Get orientation value from exif. */
1363 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1364 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1365 content_info->media_meta.orientation = orient_value;
1367 content_info->media_meta.orientation = 0;
1370 content_info->media_meta.orientation = 0;
1373 /* Get width value from exif. */
1374 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1375 if (exif_width > 0) {
1376 content_info->media_meta.width = exif_width;
1378 content_info->media_meta.width = 0;
1381 content_info->media_meta.width = 0;
1384 /* Get height value from exif. */
1385 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1386 if (exif_height > 0) {
1387 content_info->media_meta.height = exif_height;
1389 content_info->media_meta.height = 0;
1392 content_info->media_meta.height = 0;
1395 if (ed != NULL) exif_data_unref(ed);
1399 if (content_info->media_meta.width == 0 ||
1400 content_info->media_meta.height == 0) {
1401 /*Get image width, height*/
1402 unsigned int img_width = 0;
1403 unsigned int img_height = 0;
1404 ImgCodecType img_type = IMG_CODEC_NONE;
1406 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1408 if (content_info->media_meta.width == 0)
1409 content_info->media_meta.width = img_width;
1411 if (content_info->media_meta.height == 0)
1412 content_info->media_meta.height = img_height;
1415 return MS_MEDIA_ERR_NONE;
1418 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1420 MMHandleType tag = 0;
1423 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1424 int mmf_error = FILEINFO_ERROR_NONE;
1425 char *err_attr_name = NULL;
1428 int ret = MS_MEDIA_ERR_NONE;
1430 /*Get Content Tag attribute ===========*/
1431 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1433 if (mmf_error == FILEINFO_ERROR_NONE) {
1434 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1435 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1436 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1437 if (ret != MS_MEDIA_ERR_NONE)
1438 media_svc_error("strcpy error");
1440 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1442 SAFE_FREE(err_attr_name);
1443 /*media_svc_debug("album - unknown"); */
1446 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1447 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1448 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1449 if (ret != MS_MEDIA_ERR_NONE)
1450 media_svc_error("strcpy error");
1451 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1453 SAFE_FREE(err_attr_name);
1454 /*media_svc_debug("artist - unknown"); */
1457 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1458 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1459 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1460 if (ret != MS_MEDIA_ERR_NONE)
1461 media_svc_error("strcpy error");
1462 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1464 SAFE_FREE(err_attr_name);
1465 /*media_svc_debug("album_artist - unknown"); */
1468 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1469 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1470 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1471 if (ret != MS_MEDIA_ERR_NONE)
1472 media_svc_error("strcpy error");
1474 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1475 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1477 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1478 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1482 SAFE_FREE(err_attr_name);
1483 /*media_svc_debug("genre - unknown"); */
1486 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1487 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1489 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1490 if (ret != MS_MEDIA_ERR_NONE)
1491 media_svc_error("strcpy error");
1493 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1497 for (idx = 0; idx < size; idx++) {
1499 media_svc_debug("SPACE [%s]", p);
1503 media_svc_debug("Not SPACE [%s]", p);
1504 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1505 if (ret != MS_MEDIA_ERR_NONE)
1506 media_svc_error("strcpy error");
1512 media_svc_debug("Can't extract title. All string is space");
1513 title = _media_svc_get_title_from_filepath(content_info->path);
1515 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1516 if (ret != MS_MEDIA_ERR_NONE)
1517 media_svc_error("strcpy error");
1520 media_svc_error("Can't extract title");
1525 SAFE_FREE(err_attr_name);
1526 title = _media_svc_get_title_from_filepath(content_info->path);
1528 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1529 if (ret != MS_MEDIA_ERR_NONE)
1530 media_svc_error("strcpy error");
1533 media_svc_error("Can't extract title");
1537 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1538 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1539 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1540 if (ret != MS_MEDIA_ERR_NONE)
1541 media_svc_error("strcpy error");
1542 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1544 SAFE_FREE(err_attr_name);
1547 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1548 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1549 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1550 if (ret != MS_MEDIA_ERR_NONE)
1551 media_svc_error("strcpy error");
1552 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1553 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1555 /*media_svc_debug("composer - unknown"); */
1556 SAFE_FREE(err_attr_name);
1559 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1560 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1561 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1562 if (ret != MS_MEDIA_ERR_NONE)
1563 media_svc_error("strcpy error");
1564 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1565 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1567 /*media_svc_debug("copyright - unknown"); */
1568 SAFE_FREE(err_attr_name);
1571 mmf_error = mm_file_destroy_tag_attrs(tag);
1572 if (mmf_error != FILEINFO_ERROR_NONE) {
1573 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1576 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1577 char *no_tag_title = NULL;
1578 media_svc_error("no tag information");
1580 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1582 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1583 if (ret != MS_MEDIA_ERR_NONE)
1584 media_svc_error("strcpy error");
1585 SAFE_FREE(no_tag_title);
1587 media_svc_error("Can't extract title");
1590 content_info->album_id = album_id;
1593 return MS_MEDIA_ERR_NONE;
1596 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1598 MMHandleType content = 0;
1599 MMHandleType tag = 0;
1603 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1604 int mmf_error = FILEINFO_ERROR_NONE;
1605 char *err_attr_name = NULL;
1607 bool extract_thumbnail = FALSE;
1608 bool append_album = FALSE;
1610 int ret = MS_MEDIA_ERR_NONE;
1612 int resize_size = -1;
1613 void *resize_image = NULL;
1615 /*Get Content Tag attribute ===========*/
1616 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1618 if (mmf_error == FILEINFO_ERROR_NONE) {
1619 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1620 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1621 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1622 if (ret != MS_MEDIA_ERR_NONE)
1623 media_svc_error("strcpy error");
1625 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1627 SAFE_FREE(err_attr_name);
1628 /*media_svc_debug("album - unknown"); */
1631 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1632 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1633 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1634 if (ret != MS_MEDIA_ERR_NONE)
1635 media_svc_error("strcpy error");
1636 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1638 SAFE_FREE(err_attr_name);
1639 /*media_svc_debug("artist - unknown"); */
1642 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1643 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1644 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1645 if (ret != MS_MEDIA_ERR_NONE)
1646 media_svc_error("strcpy error");
1647 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1649 SAFE_FREE(err_attr_name);
1650 /*media_svc_debug("album_artist - unknown"); */
1653 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1654 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1655 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1656 if (ret != MS_MEDIA_ERR_NONE)
1657 media_svc_error("strcpy error");
1659 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1660 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1662 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1663 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1667 SAFE_FREE(err_attr_name);
1668 /*media_svc_debug("genre - unknown"); */
1671 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1672 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1674 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1675 if (ret != MS_MEDIA_ERR_NONE)
1676 media_svc_error("strcpy error");
1678 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1682 for (idx = 0; idx < size; idx++) {
1684 media_svc_debug("SPACE [%s]", p);
1688 media_svc_debug("Not SPACE [%s]", p);
1689 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1690 if (ret != MS_MEDIA_ERR_NONE)
1691 media_svc_error("strcpy error");
1697 media_svc_debug("Can't extract title. All string is space");
1698 title = _media_svc_get_title_from_filepath(content_info->path);
1700 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1701 if (ret != MS_MEDIA_ERR_NONE)
1702 media_svc_error("strcpy error");
1705 media_svc_error("Can't extract title");
1710 SAFE_FREE(err_attr_name);
1711 title = _media_svc_get_title_from_filepath(content_info->path);
1713 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1714 if (ret != MS_MEDIA_ERR_NONE)
1715 media_svc_error("strcpy error");
1718 media_svc_error("Can't extract title");
1722 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1723 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1724 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1725 if (ret != MS_MEDIA_ERR_NONE)
1726 media_svc_error("strcpy error");
1727 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1729 SAFE_FREE(err_attr_name);
1732 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1733 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1734 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1735 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1736 char time_info[64] = {0, };
1737 char p_value[64] = {0, };
1739 memset(time_info, 0x00, sizeof(time_info));
1740 memset(p_value, 0x00, sizeof(p_value));
1741 strncpy(p_value, p, size);
1742 for (idx = 0; idx < size; idx++) {
1743 if (p_value[idx] == '-') {
1744 time_info[idx] = ':';
1745 } else if (p_value[idx] != '\0') {
1746 time_info[idx] = p_value[idx];
1748 media_svc_error("strcpy error");
1752 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1754 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1757 if (ret != MS_MEDIA_ERR_NONE) {
1758 media_svc_error("strcpy error");
1760 /* This is same as datetaken */
1762 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1764 int datetaken_size = 19;
1765 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1767 if (ret != MS_MEDIA_ERR_NONE)
1768 media_svc_error("strcpy error");
1770 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1771 if (content_info->timeline == 0) {
1772 content_info->timeline = content_info->modified_time;
1774 media_svc_debug("Timeline : %ld", content_info->timeline);
1777 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1779 SAFE_FREE(err_attr_name);
1782 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1783 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1784 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1785 if (ret != MS_MEDIA_ERR_NONE)
1786 media_svc_error("strcpy error");
1787 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1788 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1790 /*media_svc_debug("composer - unknown"); */
1791 SAFE_FREE(err_attr_name);
1794 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1795 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1796 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1797 if (ret != MS_MEDIA_ERR_NONE)
1798 media_svc_error("strcpy error");
1799 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1800 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1802 /*media_svc_debug("copyright - unknown"); */
1803 SAFE_FREE(err_attr_name);
1806 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1807 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1808 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1809 if (ret != MS_MEDIA_ERR_NONE)
1810 media_svc_error("strcpy error");
1812 SAFE_FREE(err_attr_name);
1815 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1816 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1818 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1819 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1820 if (ret != MS_MEDIA_ERR_NONE)
1821 media_svc_error("strcpy error");
1823 media_svc_debug("Wrong Year Information [%s]", p);
1826 SAFE_FREE(err_attr_name);
1829 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1830 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1831 content_info->media_meta.rating = atoi(p);
1833 SAFE_FREE(err_attr_name);
1834 content_info->media_meta.rating = 0;
1837 /*Initialize album_id to 0. below code will set the album_id*/
1838 content_info->album_id = album_id;
1839 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1841 if (ret != MS_MEDIA_ERR_NONE) {
1842 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1843 media_svc_debug("album does not exist. So start to make album art");
1844 extract_thumbnail = TRUE;
1845 append_album = TRUE;
1847 extract_thumbnail = TRUE;
1848 append_album = FALSE;
1851 content_info->album_id = album_id;
1852 append_album = FALSE;
1854 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1855 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1857 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1858 extract_thumbnail = TRUE;
1860 media_svc_debug("album already exists. don't need to make album art");
1861 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1862 extract_thumbnail = TRUE;
1866 /*Do not extract artwork for the USB Storage content*/
1867 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1868 extract_thumbnail = FALSE;
1870 if (extract_thumbnail == TRUE) {
1871 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1872 if (mmf_error != FILEINFO_ERROR_NONE) {
1873 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1874 SAFE_FREE(err_attr_name);
1876 /*media_svc_debug("artwork size1 [%d]", size); */
1879 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1880 if (mmf_error != FILEINFO_ERROR_NONE) {
1881 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1882 SAFE_FREE(err_attr_name);
1884 /*media_svc_debug("artwork size2 [%d]", size); */
1886 if (image != NULL && size > 0) {
1887 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1888 int artwork_mime_size = -1;
1890 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1891 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1892 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1893 if (ret != MS_MEDIA_ERR_NONE) {
1894 media_svc_error("Fail to Get Thumbnail Path");
1896 /* albumart resizing */
1897 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1899 SAFE_FREE(err_attr_name);
1902 if (strlen(thumb_path) > 0) {
1903 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1904 if (ret != MS_MEDIA_ERR_NONE) {
1905 media_svc_error("Fail to Save Thumbnail Image");
1907 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1908 if (ret != MS_MEDIA_ERR_NONE)
1909 media_svc_error("strcpy error");
1915 if (append_album == TRUE) {
1916 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1917 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1918 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1920 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1922 content_info->album_id = album_id;
1925 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1926 double longitude = 0.0;
1927 double latitude = 0.0;
1928 double altitude = 0.0;
1930 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1931 content_info->media_meta.longitude = longitude;
1932 content_info->media_meta.latitude = latitude;
1933 content_info->media_meta.altitude = altitude;
1935 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1936 if (mmf_error != FILEINFO_ERROR_NONE) {
1938 SAFE_FREE(err_attr_name);
1941 media_svc_debug("CDIS : %d", cdis_value);
1944 mmf_error = mm_file_destroy_tag_attrs(tag);
1945 if (mmf_error != FILEINFO_ERROR_NONE) {
1946 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1949 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1950 char *no_tag_title = NULL;
1951 media_svc_error("no tag information");
1953 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1955 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1956 if (ret != MS_MEDIA_ERR_NONE)
1957 media_svc_error("strcpy error");
1958 SAFE_FREE(no_tag_title);
1960 media_svc_error("Can't extract title");
1963 content_info->album_id = album_id;
1966 /*Get Content attribute ===========*/
1967 if (cdis_value == 1) {
1968 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1970 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1973 if (mmf_error == FILEINFO_ERROR_NONE) {
1974 /*Common attribute*/
1975 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1976 if (mmf_error != FILEINFO_ERROR_NONE) {
1977 SAFE_FREE(err_attr_name);
1978 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1980 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1983 /*Sound/Music attribute*/
1984 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1986 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1987 if (mmf_error != FILEINFO_ERROR_NONE) {
1988 SAFE_FREE(err_attr_name);
1989 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1991 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1994 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1995 if (mmf_error != FILEINFO_ERROR_NONE) {
1996 SAFE_FREE(err_attr_name);
1997 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1999 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2002 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2003 if (mmf_error != FILEINFO_ERROR_NONE) {
2004 SAFE_FREE(err_attr_name);
2005 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2007 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2010 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2011 if (mmf_error != FILEINFO_ERROR_NONE) {
2012 SAFE_FREE(err_attr_name);
2013 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2015 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2017 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2018 int audio_bitrate = 0;
2019 int video_bitrate = 0;
2021 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2022 if (mmf_error != FILEINFO_ERROR_NONE) {
2023 SAFE_FREE(err_attr_name);
2024 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2026 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2029 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2030 if (mmf_error != FILEINFO_ERROR_NONE) {
2031 SAFE_FREE(err_attr_name);
2032 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2034 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2037 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2039 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2040 if (mmf_error != FILEINFO_ERROR_NONE) {
2041 SAFE_FREE(err_attr_name);
2042 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2044 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2047 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2048 if (mmf_error != FILEINFO_ERROR_NONE) {
2049 SAFE_FREE(err_attr_name);
2050 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2052 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2056 media_svc_error("Not support type");
2057 mmf_error = mm_file_destroy_content_attrs(content);
2058 if (mmf_error != FILEINFO_ERROR_NONE) {
2059 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2061 return MS_MEDIA_ERR_INVALID_PARAMETER;
2064 mmf_error = mm_file_destroy_content_attrs(content);
2065 if (mmf_error != FILEINFO_ERROR_NONE) {
2066 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2069 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2072 return MS_MEDIA_ERR_NONE;
2075 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2077 media_svc_retm_if(content_info == NULL, "content info is NULL");
2079 /* Delete media_svc_content_info_s */
2080 SAFE_FREE(content_info->media_uuid);
2081 SAFE_FREE(content_info->path);
2082 SAFE_FREE(content_info->file_name);
2083 SAFE_FREE(content_info->mime_type);
2084 SAFE_FREE(content_info->folder_uuid);
2085 SAFE_FREE(content_info->thumbnail_path);
2086 SAFE_FREE(content_info->storage_uuid);
2088 /* Delete media_svc_content_meta_s */
2089 SAFE_FREE(content_info->media_meta.title);
2090 SAFE_FREE(content_info->media_meta.album);
2091 SAFE_FREE(content_info->media_meta.artist);
2092 SAFE_FREE(content_info->media_meta.album_artist);
2093 SAFE_FREE(content_info->media_meta.genre);
2094 SAFE_FREE(content_info->media_meta.composer);
2095 SAFE_FREE(content_info->media_meta.year);
2096 SAFE_FREE(content_info->media_meta.recorded_date);
2097 SAFE_FREE(content_info->media_meta.copyright);
2098 SAFE_FREE(content_info->media_meta.track_num);
2099 SAFE_FREE(content_info->media_meta.description);
2100 SAFE_FREE(content_info->media_meta.datetaken);
2101 SAFE_FREE(content_info->media_meta.exposure_time);
2102 SAFE_FREE(content_info->media_meta.model);
2103 SAFE_FREE(content_info->media_meta.weather);
2104 SAFE_FREE(content_info->media_meta.category);
2105 SAFE_FREE(content_info->media_meta.keyword);
2106 SAFE_FREE(content_info->media_meta.location_tag);
2107 SAFE_FREE(content_info->media_meta.content_name);
2108 SAFE_FREE(content_info->media_meta.age_rating);
2109 SAFE_FREE(content_info->media_meta.author);
2110 SAFE_FREE(content_info->media_meta.provider);
2112 SAFE_FREE(content_info->media_meta.title_pinyin);
2113 SAFE_FREE(content_info->media_meta.album_pinyin);
2114 SAFE_FREE(content_info->media_meta.artist_pinyin);
2115 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2116 SAFE_FREE(content_info->media_meta.genre_pinyin);
2117 SAFE_FREE(content_info->media_meta.composer_pinyin);
2118 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2119 SAFE_FREE(content_info->media_meta.description_pinyin);
2124 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2126 if (STRING_VALID(path)) {
2127 char *internal_path = _media_svc_get_path(uid);
2128 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2129 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2130 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2131 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2132 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2133 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2135 media_svc_error("Invalid Path");
2136 SAFE_FREE(internal_path);
2137 return MS_MEDIA_ERR_INVALID_PARAMETER;
2139 SAFE_FREE(internal_path);
2141 media_svc_error("INVALID parameter");
2142 return MS_MEDIA_ERR_INVALID_PARAMETER;
2145 return MS_MEDIA_ERR_NONE;
2148 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2151 size_t i, count = 0;
2152 size_t oldlen = strlen(olds);
2153 if (oldlen < 1) return s;
2154 size_t newlen = strlen(news);
2156 if (newlen != oldlen) {
2157 for (i = 0; s[i] != '\0';) {
2158 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2161 } else i = strlen(s);
2164 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2165 if (result == NULL) return NULL;
2169 if (memcmp(s, olds, oldlen) == 0) {
2170 memcpy(sr, news, newlen);
2173 } else *sr++ = *s++;
2181 bool _media_svc_is_drm_file(const char *path)
2186 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)
2188 int ret = MS_MEDIA_ERR_NONE;
2190 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2192 if (ret != MS_MEDIA_ERR_NONE) {
2193 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2194 ret = MS_MEDIA_ERR_INTERNAL;
2196 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2202 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2204 int ret = MS_MEDIA_ERR_NONE;
2206 pinyin_name_s *pinyinname = NULL;
2210 if (!STRING_VALID(src_str)) {
2211 media_svc_debug("String is invalid");
2215 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2216 if (ret == MS_MEDIA_ERR_NONE) {
2217 if (STRING_VALID(pinyinname[0].pinyin_name))
2218 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2220 *pinyin_str = strdup(src_str); //Return Original Non China Character
2223 _media_svc_pinyin_free(pinyinname, size);
2228 bool _media_svc_check_pinyin_support(void)
2234 int _media_svc_get_ini_value()
2236 if (g_ini_value == -1) {
2237 dictionary *dict = NULL;
2239 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2241 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2245 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2246 iniparser_freedict(dict);
2248 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2253 char* _media_svc_get_title_from_path(const char *path)
2255 char *filename = NULL;
2258 int filename_len = -1;
2259 int new_title_len = -1;
2262 media_svc_error("path is NULL");
2266 filename = g_path_get_basename(path);
2267 if (!STRING_VALID(filename)) {
2268 media_svc_error("wrong file name");
2269 SAFE_FREE(filename);
2273 filename_len = strlen(filename);
2275 ext = g_strrstr(filename, ".");
2277 media_svc_error("there is no file extention");
2281 new_title_len = filename_len - strlen(ext);
2282 if (new_title_len < 1) {
2283 media_svc_error("title length is zero");
2284 SAFE_FREE(filename);
2288 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2290 SAFE_FREE(filename);
2292 media_svc_debug("extract title is [%s]", title);
2297 #define BUF_LENGHT 256
2299 void _media_svc_print_stderror(void)
2301 char buf[BUF_LENGHT] = {0,};
2303 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));