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 SYSCONFDIR"/multimedia/media_content_config.ini"
77 #define MEDIA_SVC_ARTWORK_SIZE 2000
79 static int g_ini_value = -1;
82 char content_type[15];
84 } _media_svc_content_table_s;
86 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
87 {"audio", MEDIA_SVC_CATEGORY_SOUND},
88 {"image", MEDIA_SVC_CATEGORY_IMAGE},
89 {"video", MEDIA_SVC_CATEGORY_VIDEO},
90 {"application", MEDIA_SVC_CATEGORY_ETC},
91 {"text", MEDIA_SVC_CATEGORY_ETC},
94 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
95 /*known mime types of normal files*/
101 /* known mime types of drm files*/
103 "x-mp3", /*alias of audio/mpeg*/
104 "x-mpeg", /*alias of audio/mpeg*/
106 "x-ogg", /*alias of audio/ogg*/
107 "vnd.ms-playready.media.pya:*.pya", /*playready*/
110 "x-m4a", /*alias of audio/mp4*/
111 /* below mimes are rare*/
121 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
122 "vorbis", /*alias of audio/x-vorbis+ogg*/
124 "x-mp2", /*alias of audio/mp2*/
125 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
126 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
130 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
135 MEDIA_SVC_EXTRACTED_FIELD_NONE = 0x00000001,
136 MEDIA_SVC_EXTRACTED_FIELD_TITLE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
137 MEDIA_SVC_EXTRACTED_FIELD_DESC = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
138 MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
139 MEDIA_SVC_EXTRACTED_FIELD_AUTHOR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
140 MEDIA_SVC_EXTRACTED_FIELD_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
141 MEDIA_SVC_EXTRACTED_FIELD_GENRE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
142 MEDIA_SVC_EXTRACTED_FIELD_ALBUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
143 MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
144 MEDIA_SVC_EXTRACTED_FIELD_YEAR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
145 MEDIA_SVC_EXTRACTED_FIELD_CATEGORY = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
146 MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
147 } media_svc_extracted_field_e;
149 char *_media_info_generate_uuid(void)
152 static char uuid_unparsed[37];
155 uuid_generate(uuid_value);
156 uuid_unparse(uuid_value, uuid_unparsed);
158 if (strlen(uuid_unparsed) < 36) {
159 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
163 return uuid_unparsed;
166 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
168 if (!x_src || strlen(x_src) == 0) {
169 media_svc_error("x_src is NULL");
174 media_svc_error("length is Wrong");
178 strncpy(x_dst, x_src, max_len - 1);
179 x_dst[max_len - 1] = '\0';
182 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
186 if (!STRING_VALID(src)) {
187 media_svc_error("invalid src");
188 return MS_MEDIA_ERR_INVALID_PARAMETER;
193 len = strlen(src) + 1;
197 media_svc_error("malloc failed");
198 return MS_MEDIA_ERR_INTERNAL;
201 strncpy(*dst, src, len);
205 return MS_MEDIA_ERR_NONE;
208 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
210 if (!STRING_VALID(src)) {
211 media_svc_error("invalid src");
212 return MS_MEDIA_ERR_INVALID_PARAMETER;
217 *dst = malloc(copysize + 1);
220 media_svc_error("malloc failed");
221 return MS_MEDIA_ERR_INTERNAL;
224 strncpy(*dst, src, copysize);
228 return MS_MEDIA_ERR_NONE;
231 static int __media_svc_split_to_double(char *input, double *arr)
233 char tmp_arr[255] = {0, };
234 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
236 if (!STRING_VALID(input)) {
237 media_svc_error("Invalid parameter");
238 return MS_MEDIA_ERR_INVALID_PARAMETER;
240 memset(tmp_arr, 0x0, sizeof(tmp_arr));
242 /*media_svc_debug("input: [%s]", input); */
246 for (idx = 0; idx < (len + 1); idx++) {
247 if (input[idx] == ' ') {
249 } else if ((input[idx] == ',') || (idx == len)) {
250 arr[arr_idx] = atof(tmp_arr);
253 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
254 memset(tmp_arr, 0x0, sizeof(tmp_arr));
256 tmp_arr[str_idx] = input[idx];
262 media_svc_error("Error when parsing GPS [%d]", arr_idx);
263 return MS_MEDIA_ERR_INTERNAL;
266 return MS_MEDIA_ERR_NONE;
269 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
275 return MS_MEDIA_ERR_INVALID_PARAMETER;
280 entry = exif_data_get_entry(ed, tag);
282 /* Get the contents of the tag in human-readable form */
283 if (tag == EXIF_TAG_ORIENTATION ||
284 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
285 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
286 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
288 if (i_value == NULL) {
289 media_svc_error("i_value is NULL");
290 return MS_MEDIA_ERR_INVALID_PARAMETER;
293 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
294 short exif_value = exif_get_short(entry->data, mByteOrder);
295 *i_value = (int)exif_value;
297 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
299 if (d_value == NULL) {
300 media_svc_error("d_value is NULL");
301 return MS_MEDIA_ERR_INVALID_PARAMETER;
304 /* Get the contents of the tag in human-readable form */
305 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
306 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
307 gps_buf[strlen(gps_buf)] = '\0';
308 int ret = MS_MEDIA_ERR_NONE;
310 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
312 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
313 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
315 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
316 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
319 media_svc_error("buf is NULL");
320 return MS_MEDIA_ERR_INVALID_PARAMETER;
323 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
324 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
325 long numerator = mRational.numerator;
326 long denominator = mRational.denominator;
327 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
329 } else if (tag == EXIF_TAG_FNUMBER) {
331 if (d_value == NULL) {
332 media_svc_error("d_value is NULL");
333 return MS_MEDIA_ERR_INVALID_PARAMETER;
336 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
337 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
338 long numerator = mRational.numerator;
339 long denominator = mRational.denominator;
341 *d_value = ((numerator*1.0)/(denominator*1.0));
346 media_svc_error("buf is NULL");
347 return MS_MEDIA_ERR_INVALID_PARAMETER;
350 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
351 buf[strlen(buf)] = '\0';
355 return MS_MEDIA_ERR_NONE;
358 time_t __media_svc_get_timeline_from_str(const char *timstr)
361 time_t modified_t = 0;
365 if (!STRING_VALID(timstr)) {
366 media_svc_error("Invalid Parameter");
370 /*Exif Format : %Y:%m:%d %H:%M:%S
371 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
372 memset(&t, 0x00, sizeof(struct tm));
376 localtime_r(&rawtime, &timeinfo);
378 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
379 t.tm_isdst = timeinfo.tm_isdst;
380 if (t.tm_isdst != 0) {
381 media_svc_error("DST %d", t.tm_isdst);
384 modified_t = mktime(&t);
385 if (modified_t > 0) {
388 media_svc_error("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
391 media_svc_error("Failed to get timeline : [%s]", timstr);
397 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
401 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
403 /*categorize from mimetype */
404 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
405 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
406 *category = (*category | content_category[idx].category_by_mime);
411 /*in application type, exitst sound file ex) x-smafs */
412 if (*category & MEDIA_SVC_CATEGORY_ETC) {
413 int prefix_len = strlen(content_category[0].content_type);
415 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
416 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
417 *category ^= MEDIA_SVC_CATEGORY_ETC;
418 *category |= MEDIA_SVC_CATEGORY_SOUND;
423 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
424 *category ^= MEDIA_SVC_CATEGORY_ETC;
425 *category |= MEDIA_SVC_CATEGORY_SOUND;
429 /*check music file in soun files. */
430 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
431 int prefix_len = strlen(content_category[0].content_type) + 1;
433 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
434 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
435 *category ^= MEDIA_SVC_CATEGORY_SOUND;
436 *category |= MEDIA_SVC_CATEGORY_MUSIC;
441 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
442 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
443 *category ^= MEDIA_SVC_CATEGORY_SOUND;
444 *category |= MEDIA_SVC_CATEGORY_ETC;
446 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
447 /*some video files don't have video stream. in this case it is categorize as music. */
449 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
450 ext = strrchr(path, '.');
452 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
457 err = mm_file_get_stream_info(path, &audio, &video);
459 if (audio > 0 && video == 0) {
460 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
461 *category |= MEDIA_SVC_CATEGORY_MUSIC;
464 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
469 return MS_MEDIA_ERR_NONE;
472 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
474 int ret = MS_MEDIA_ERR_NONE;
477 media_svc_media_type_e type;
479 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
480 if (ret != MS_MEDIA_ERR_NONE) {
481 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
484 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
485 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
486 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
487 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
488 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
496 drm_contentifo is not NULL, if the file is OMA DRM.
497 If the file is not OMA DRM, drm_contentinfo must be NULL.
499 static int __media_svc_get_mime_type(const char *path, char *mimetype)
502 return MS_MEDIA_ERR_INVALID_PARAMETER;
504 /*in case of normal files or failure to get mime in drm */
505 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
506 media_svc_error("aul_get_mime_from_file fail");
507 return MS_MEDIA_ERR_INTERNAL;
510 return MS_MEDIA_ERR_NONE;
513 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
517 for (i = strlen(file_path); i >= 0; i--) {
518 if (file_path[i] == '.') {
519 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
523 if (file_path[i] == '/') {
530 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
532 char *err_attr_name = NULL;
533 double gps_value = 0.0;
534 int mmf_error = FILEINFO_ERROR_NONE;
536 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
537 if (mmf_error == FILEINFO_ERROR_NONE) {
538 if (longitude != NULL) {
539 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
542 SAFE_FREE(err_attr_name);
545 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
546 if (mmf_error == FILEINFO_ERROR_NONE) {
547 if (latitude != NULL) {
548 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
551 SAFE_FREE(err_attr_name);
554 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
555 if (mmf_error == FILEINFO_ERROR_NONE) {
556 if (altitude != NULL) {
557 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
560 SAFE_FREE(err_attr_name);
563 return MS_MEDIA_ERR_NONE;
566 static char *__media_svc_get_thumb_path(uid_t uid)
569 char *result_passwd = NULL;
570 struct group *grpinfo = NULL;
571 if (uid == getuid()) {
572 grpinfo = getgrnam("users");
573 if (grpinfo == NULL) {
574 media_svc_error("getgrnam(users) returns NULL !");
577 len = strlen(MEDIA_SVC_THUMB_PATH_PREFIX);
579 result_passwd = strndup(MEDIA_SVC_THUMB_PATH_PREFIX, len);
581 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
582 struct passwd *userinfo = getpwuid(uid);
583 if (userinfo == NULL) {
584 media_svc_error("getpwuid(%d) returns NULL !", uid);
587 grpinfo = getgrnam("users");
588 if (grpinfo == NULL) {
589 media_svc_error("getgrnam(users) returns NULL !");
592 /* Compare git_t type and not group name */
593 if (grpinfo->gr_gid != userinfo->pw_gid) {
594 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
597 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
599 result_passwd = strndup(passwd_str, len);
602 return result_passwd;
605 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
607 int ret = MS_MEDIA_ERR_NONE;
608 unsigned char *raw_image = NULL;
611 unsigned int raw_size = 0;
612 void *resized_raw_image = NULL;
613 int resized_width = 0;
614 int resized_height = 0;
615 unsigned int buf_size = 0;
617 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
618 media_svc_debug("type [jpeg] size [%d]", size);
620 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
621 if (ret != MS_MEDIA_ERR_NONE) {
622 media_svc_error("image_util_decode_jpeg_from_memory failed");
623 *resize_image = image;
625 return MS_MEDIA_ERR_NONE;
628 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
629 media_svc_debug("No need resizing");
630 *resize_image = image;
632 SAFE_FREE(raw_image);
633 return MS_MEDIA_ERR_NONE;
636 if (width > height) {
637 resized_height = MEDIA_SVC_ARTWORK_SIZE;
638 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
640 resized_width = MEDIA_SVC_ARTWORK_SIZE;
641 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
644 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
646 resized_raw_image = malloc(buf_size);
648 if (resized_raw_image == NULL) {
649 media_svc_error("malloc failed");
650 *resize_image = image;
652 SAFE_FREE(raw_image);
653 return MS_MEDIA_ERR_NONE;
656 memset(resized_raw_image, 0, buf_size);
658 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
659 if (ret != MS_MEDIA_ERR_NONE) {
660 media_svc_error("image_util_resize failed");
661 *resize_image = image;
663 SAFE_FREE(raw_image);
664 SAFE_FREE(resized_raw_image);
665 return MS_MEDIA_ERR_NONE;
667 SAFE_FREE(raw_image);
670 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
671 if (ret != MS_MEDIA_ERR_NONE) {
672 media_svc_error("image_util_encode_jpeg_to_memory failed");
673 *resize_image = image;
675 SAFE_FREE(resized_raw_image);
676 return MS_MEDIA_ERR_NONE;
678 SAFE_FREE(resized_raw_image);
680 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
681 media_svc_debug("type [png] size [%d]", size);
682 *resize_image = image;
686 media_svc_debug("Not proper img format");
687 *resize_image = image;
694 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
696 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
699 media_svc_error("invalid image..");
700 return MS_MEDIA_ERR_INVALID_PARAMETER;
704 char *thumb_path = __media_svc_get_thumb_path(uid);
705 if (!STRING_VALID(thumb_path)) {
706 media_svc_error("fail to get thumb_path");
707 return MS_MEDIA_ERR_INTERNAL;
710 if (-1 == statfs(thumb_path, &fs)) {
711 media_svc_error("error in statfs");
712 SAFE_FREE(thumb_path);
713 return MS_MEDIA_ERR_INTERNAL;
716 SAFE_FREE(thumb_path);
718 long bsize_kbytes = fs.f_bsize >> 10;
720 if ((bsize_kbytes * fs.f_bavail) < 1024) {
721 media_svc_error("not enought space...");
722 return MS_MEDIA_ERR_INTERNAL;
727 if (image != NULL && size > 0) {
728 fp = fopen(image_path, "w");
731 media_svc_error("failed to open file");
732 return MS_MEDIA_ERR_INTERNAL;
735 nwrite = fwrite(image, 1, size, fp);
736 if (nwrite != size) {
737 media_svc_error("failed to write thumbnail");
739 return MS_MEDIA_ERR_INTERNAL;
744 return MS_MEDIA_ERR_NONE;
747 static char *_media_svc_get_title_from_filepath(const char *path)
749 char *filename = NULL;
752 int filename_len = -1;
753 int new_title_len = -1;
756 media_svc_error("path is NULL");
760 filename = g_path_get_basename(path);
761 if (!STRING_VALID(filename)) {
762 media_svc_error("wrong file name");
767 filename_len = strlen(filename);
769 ext = g_strrstr(filename, ".");
771 media_svc_error("there is no file extention");
775 new_title_len = filename_len - strlen(ext);
776 if (new_title_len < 1) {
777 media_svc_error("title length is zero");
782 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
786 media_svc_debug("extract title is [%s]", title);
791 int _media_svc_rename_file(const char *old_name, const char *new_name)
793 if ((old_name == NULL) || (new_name == NULL)) {
794 media_svc_error("invalid file name");
795 return MS_MEDIA_ERR_INVALID_PARAMETER;
798 if (rename(old_name, new_name) < 0) {
799 media_svc_stderror(" ");
800 return MS_MEDIA_ERR_INTERNAL;
803 return MS_MEDIA_ERR_NONE;
806 int _media_svc_remove_file(const char *path)
810 result = remove(path);
812 media_svc_debug("success to remove file");
813 return MS_MEDIA_ERR_NONE;
815 media_svc_stderror("fail to remove file result");
816 return MS_MEDIA_ERR_INTERNAL;
820 int _media_svc_remove_all_files_in_dir(const char *dir_path)
823 struct dirent *result;
825 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
828 dir = opendir(dir_path);
830 media_svc_error("%s is not exist", dir_path);
831 return MS_MEDIA_ERR_INVALID_PARAMETER;
834 while (!readdir_r(dir, &entry, &result)) {
838 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
841 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
843 if (stat(filename, &st) != 0) {
846 if (S_ISDIR(st.st_mode)) {
849 if (unlink(filename) != 0) {
850 media_svc_stderror("failed to remove");
852 return MS_MEDIA_ERR_INTERNAL;
857 return MS_MEDIA_ERR_NONE;
860 char *_media_svc_get_thumb_internal_path(uid_t uid)
863 char *result_passwd = NULL;
864 struct group *grpinfo = NULL;
865 if (uid == getuid()) {
866 grpinfo = getgrnam("users");
867 if (grpinfo == NULL) {
868 media_svc_error("getgrnam(users) returns NULL !");
871 len = strlen(MEDIA_SVC_THUMB_INTERNAL_PATH);
873 result_passwd = strndup(MEDIA_SVC_THUMB_INTERNAL_PATH, len);
875 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
876 struct passwd *userinfo = getpwuid(uid);
877 if (userinfo == NULL) {
878 media_svc_error("getpwuid(%d) returns NULL !", uid);
881 grpinfo = getgrnam("users");
882 if (grpinfo == NULL) {
883 media_svc_error("getgrnam(users) returns NULL !");
886 /* Compare git_t type and not group name */
887 if (grpinfo->gr_gid != userinfo->pw_gid) {
888 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
891 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
893 result_passwd = strndup(passwd_str, len);
896 return result_passwd;
899 char *_media_svc_get_thumb_external_path(uid_t uid)
902 char *result_passwd = NULL;
903 struct group *grpinfo = NULL;
904 if (uid == getuid()) {
905 grpinfo = getgrnam("users");
906 if (grpinfo == NULL) {
907 media_svc_error("getgrnam(users) returns NULL !");
910 len = strlen(MEDIA_SVC_THUMB_EXTERNAL_PATH);
912 result_passwd = strndup(MEDIA_SVC_THUMB_EXTERNAL_PATH, len);
914 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
915 struct passwd *userinfo = getpwuid(uid);
916 if (userinfo == NULL) {
917 media_svc_error("getpwuid(%d) returns NULL !", uid);
920 grpinfo = getgrnam("users");
921 if (grpinfo == NULL) {
922 media_svc_error("getgrnam(users) returns NULL !");
925 /* Compare git_t type and not group name */
926 if (grpinfo->gr_gid != userinfo->pw_gid) {
927 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
930 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
932 result_passwd = strndup(passwd_str, len);
935 return result_passwd;
938 static int __media_svc_check_thumb_dir(const char *thumb_dir)
943 dir = opendir(thumb_dir);
947 media_svc_stderror("opendir fail");
948 if (errno == ENOENT) {
949 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
950 ret = mkdir(thumb_dir, 0777);
952 media_svc_error("make fail");
959 ret = chmod(thumb_dir, 0777);
961 media_svc_stderror("chmod failed");
963 ret = chown(thumb_dir, 5000, 5000);
965 media_svc_stderror("chown failed");
969 return MS_MEDIA_ERR_NONE;
975 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)
977 int ret = MS_MEDIA_ERR_NONE;
978 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
979 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
980 char *thumb_dir = NULL;
981 char hash[255 + 1] = {0, };
982 char *thumbfile_ext = NULL;
983 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
984 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
986 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
987 media_svc_error("fail to get thumbnail path");
988 SAFE_FREE(internal_thumb_path);
989 SAFE_FREE(external_thumb_path);
990 return MS_MEDIA_ERR_INTERNAL;
993 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
995 ret = __media_svc_check_thumb_dir(thumb_dir);
996 if (ret != MS_MEDIA_ERR_NONE) {
997 media_svc_error("__media_svc_check_thumb_dir");
998 SAFE_FREE(internal_thumb_path);
999 SAFE_FREE(external_thumb_path);
1000 return MS_MEDIA_ERR_INTERNAL;
1003 memset(file_ext, 0, sizeof(file_ext));
1004 if (!__media_svc_get_file_ext(pathname, file_ext)) {
1005 media_svc_error("get file ext fail");
1008 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1009 if (ret != MS_MEDIA_ERR_NONE) {
1010 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1011 SAFE_FREE(internal_thumb_path);
1012 SAFE_FREE(external_thumb_path);
1013 return MS_MEDIA_ERR_INTERNAL;
1016 /*media_svc_debug("img format is [%s]", img_format); */
1018 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1019 thumbfile_ext = (char *)"jpg";
1020 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1021 thumbfile_ext = (char *)"png";
1022 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1023 thumbfile_ext = (char *)"gif";
1024 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1025 thumbfile_ext = (char *)"bmp";
1027 media_svc_error("Not proper img format");
1028 SAFE_FREE(internal_thumb_path);
1029 SAFE_FREE(external_thumb_path);
1030 return MS_MEDIA_ERR_INTERNAL;
1033 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1034 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1035 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1037 SAFE_FREE(internal_thumb_path);
1038 SAFE_FREE(external_thumb_path);
1040 return MS_MEDIA_ERR_NONE;
1043 int _media_svc_get_file_time(const char *full_path)
1045 struct stat statbuf;
1048 memset(&statbuf, 0, sizeof(struct stat));
1049 fd = stat(full_path, &statbuf);
1051 media_svc_error("stat(%s) fails.", full_path);
1052 return MS_MEDIA_ERR_INTERNAL;
1055 return statbuf.st_mtime;
1058 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1060 int ret = MS_MEDIA_ERR_NONE;
1062 /* Set default GPS value before extracting meta information */
1063 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1064 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1065 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1067 /* Set filename to title for all media */
1069 title = _media_svc_get_title_from_filepath(content_info->path);
1071 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1072 if (ret != MS_MEDIA_ERR_NONE)
1073 media_svc_error("strcpy error");
1076 media_svc_error("Can't extract title");
1077 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1078 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1081 /* Set default value before extracting meta information */
1082 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1083 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1085 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1086 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1088 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1089 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1091 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1092 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1094 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1095 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1097 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1098 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1100 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1101 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1103 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1104 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1106 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1107 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1110 media_svc_debug("refresh");
1111 return MS_MEDIA_ERR_NONE;
1114 content_info->played_count = 0;
1115 content_info->last_played_time = 0;
1116 content_info->last_played_position = 0;
1117 content_info->favourate = 0;
1118 content_info->media_meta.rating = 0;
1120 return MS_MEDIA_ERR_NONE;
1123 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1124 const char *path, media_svc_media_type_e *media_type, bool refresh)
1126 int ret = MS_MEDIA_ERR_NONE;
1127 char * media_uuid = NULL;
1128 char * file_name = NULL;
1129 bool drm_type = false;
1130 char mime_type[256] = {0, };
1132 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1133 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1135 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1137 memset(&st, 0, sizeof(struct stat));
1138 if (stat(path, &st) == 0) {
1139 content_info->modified_time = st.st_mtime;
1140 content_info->timeline = content_info->modified_time;
1141 content_info->size = st.st_size;
1142 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1144 media_svc_stderror("stat failed");
1148 _media_svc_set_default_value(content_info, refresh);
1150 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1152 media_svc_debug("refresh");
1153 return MS_MEDIA_ERR_NONE;
1156 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1157 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1159 content_info->storage_type = storage_type;
1160 time(&content_info->added_time);
1162 media_uuid = _media_info_generate_uuid();
1163 if (media_uuid == NULL) {
1164 _media_svc_destroy_content_info(content_info);
1165 return MS_MEDIA_ERR_INTERNAL;
1168 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1169 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1171 file_name = g_path_get_basename(path);
1172 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1173 SAFE_FREE(file_name);
1174 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1176 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1177 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1178 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1179 ret = __media_svc_get_mime_type(path, mime_type);
1180 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1182 media_svc_debug("mime [%s]", mime_type);
1183 content_info->is_drm = drm_type;
1185 ret = __media_svc_get_media_type(path, mime_type, media_type);
1186 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1188 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1189 media_svc_error("invalid media_type condition[%d]", *media_type);
1190 return MS_MEDIA_ERR_INVALID_PARAMETER;
1193 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1194 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1196 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1198 content_info->media_type = *media_type;
1201 return MS_MEDIA_ERR_NONE;
1204 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1206 int ret = MS_MEDIA_ERR_NONE;
1208 int orient_value = 0;
1210 int exif_height = 0;
1211 ExifData *ed = NULL;
1212 int has_datetaken = FALSE;
1213 int datetaken_size = 19;
1214 double fnumber = 0.0;
1218 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1219 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1220 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1221 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1223 memset(buf, 0x00, sizeof(buf));
1224 memset(description_buf, 0x00, sizeof(description_buf));
1225 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1226 memset(model_buf, 0x00, sizeof(model_buf));
1228 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1229 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1230 return MS_MEDIA_ERR_INVALID_PARAMETER;
1233 path = content_info->path;
1234 if (!STRING_VALID(path)) {
1235 media_svc_error("Invalid Path");
1236 return MS_MEDIA_ERR_INVALID_PARAMETER;
1239 /* Load an ExifData object from an EXIF file */
1240 ed = exif_data_new_from_file(path);
1243 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1244 goto GET_WIDTH_HEIGHT;
1247 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1248 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1249 if (strlen(buf) > 0) {
1250 if (strcmp(buf, "S") == 0) {
1254 content_info->media_meta.latitude = value;
1256 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1259 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1262 memset(buf, 0x00, sizeof(buf));
1264 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1265 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1266 if (strlen(buf) > 0) {
1267 if (strcmp(buf, "W") == 0) {
1271 content_info->media_meta.longitude = value;
1273 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1276 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1279 memset(buf, 0x00, sizeof(buf));
1281 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1282 if (strlen(description_buf) == 0) {
1283 /*media_svc_debug("Use 'No description'"); */
1284 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1285 if (ret != MS_MEDIA_ERR_NONE)
1286 media_svc_error("strcpy error");
1288 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1289 if (ret != MS_MEDIA_ERR_NONE)
1290 media_svc_error("strcpy error");
1293 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1294 if (ret != MS_MEDIA_ERR_NONE)
1295 media_svc_error("strcpy error");
1298 memset(buf, 0x00, sizeof(buf));
1300 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1301 if (strlen(buf) == 0) {
1302 /*media_svc_debug("time is NULL"); */
1304 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1305 if (ret != MS_MEDIA_ERR_NONE) {
1306 media_svc_error("strcpy error");
1308 has_datetaken = TRUE;
1309 /* This is same as recorded_date */
1310 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1311 if (ret != MS_MEDIA_ERR_NONE)
1312 media_svc_error("strcpy error");
1317 memset(buf, 0x00, sizeof(buf));
1319 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1320 if (strlen(buf) == 0) {
1321 /*media_svc_debug("time is NULL"); */
1323 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1324 if (ret != MS_MEDIA_ERR_NONE) {
1325 media_svc_error("strcpy error");
1327 has_datetaken = TRUE;
1328 /* This is same as recorded_date */
1329 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1330 if (ret != MS_MEDIA_ERR_NONE)
1331 media_svc_error("strcpy error");
1336 if (has_datetaken) {
1337 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1338 if (content_info->timeline == 0) {
1339 content_info->timeline = content_info->modified_time;
1341 media_svc_debug("Timeline : %ld", content_info->timeline);
1345 /* Get exposure_time value from exif. */
1346 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1347 if (strlen(exposure_time_buf) == 0) {
1348 //media_svc_debug("exposure_time_buf is NULL");
1350 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1351 if (ret != MS_MEDIA_ERR_NONE)
1352 media_svc_error("strcpy error");
1356 /* Get fnumber value from exif. */
1357 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1358 if (fnumber > 0.0) {
1359 content_info->media_meta.fnumber = fnumber;
1361 content_info->media_meta.fnumber = 0.0;
1364 content_info->media_meta.fnumber = 0.0;
1367 /* Get iso value from exif. */
1368 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1370 content_info->media_meta.iso = iso;
1372 content_info->media_meta.iso = 0;
1375 content_info->media_meta.iso = 0;
1378 /* Get model value from exif. */
1379 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1380 if (strlen(model_buf) == 0) {
1381 //media_svc_debug("model_buf is NULL");
1383 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1384 if (ret != MS_MEDIA_ERR_NONE)
1385 media_svc_error("strcpy error");
1389 /* Get orientation value from exif. */
1390 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1391 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1392 content_info->media_meta.orientation = orient_value;
1394 content_info->media_meta.orientation = 0;
1397 content_info->media_meta.orientation = 0;
1400 /* Get width value from exif. */
1401 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1402 if (exif_width > 0) {
1403 content_info->media_meta.width = exif_width;
1405 content_info->media_meta.width = 0;
1408 content_info->media_meta.width = 0;
1411 /* Get height value from exif. */
1412 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1413 if (exif_height > 0) {
1414 content_info->media_meta.height = exif_height;
1416 content_info->media_meta.height = 0;
1419 content_info->media_meta.height = 0;
1422 if (ed != NULL) exif_data_unref(ed);
1426 if (content_info->media_meta.width == 0 ||
1427 content_info->media_meta.height == 0) {
1428 /*Get image width, height*/
1429 unsigned int img_width = 0;
1430 unsigned int img_height = 0;
1431 ImgCodecType img_type = IMG_CODEC_NONE;
1433 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1435 if (content_info->media_meta.width == 0)
1436 content_info->media_meta.width = img_width;
1438 if (content_info->media_meta.height == 0)
1439 content_info->media_meta.height = img_height;
1442 return MS_MEDIA_ERR_NONE;
1445 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1447 MMHandleType tag = 0;
1450 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1451 int mmf_error = FILEINFO_ERROR_NONE;
1452 char *err_attr_name = NULL;
1455 int ret = MS_MEDIA_ERR_NONE;
1457 /*Get Content Tag attribute ===========*/
1458 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1460 if (mmf_error == FILEINFO_ERROR_NONE) {
1461 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1462 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1463 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1464 if (ret != MS_MEDIA_ERR_NONE)
1465 media_svc_error("strcpy error");
1467 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1469 SAFE_FREE(err_attr_name);
1470 /*media_svc_debug("album - unknown"); */
1473 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1474 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1475 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1476 if (ret != MS_MEDIA_ERR_NONE)
1477 media_svc_error("strcpy error");
1478 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1480 SAFE_FREE(err_attr_name);
1481 /*media_svc_debug("artist - unknown"); */
1484 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1485 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1486 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1487 if (ret != MS_MEDIA_ERR_NONE)
1488 media_svc_error("strcpy error");
1489 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1491 SAFE_FREE(err_attr_name);
1492 /*media_svc_debug("album_artist - unknown"); */
1495 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1496 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1497 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1498 if (ret != MS_MEDIA_ERR_NONE)
1499 media_svc_error("strcpy error");
1501 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1502 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1504 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1505 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1509 SAFE_FREE(err_attr_name);
1510 /*media_svc_debug("genre - unknown"); */
1513 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1514 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1516 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1517 if (ret != MS_MEDIA_ERR_NONE)
1518 media_svc_error("strcpy error");
1520 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1524 for (idx = 0; idx < size; idx++) {
1526 media_svc_debug("SPACE [%s]", p);
1530 media_svc_debug("Not SPACE [%s]", p);
1531 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1532 if (ret != MS_MEDIA_ERR_NONE)
1533 media_svc_error("strcpy error");
1539 media_svc_debug("Can't extract title. All string is space");
1540 title = _media_svc_get_title_from_filepath(content_info->path);
1542 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1543 if (ret != MS_MEDIA_ERR_NONE)
1544 media_svc_error("strcpy error");
1547 media_svc_error("Can't extract title");
1552 SAFE_FREE(err_attr_name);
1553 title = _media_svc_get_title_from_filepath(content_info->path);
1555 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1556 if (ret != MS_MEDIA_ERR_NONE)
1557 media_svc_error("strcpy error");
1560 media_svc_error("Can't extract title");
1564 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1565 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1566 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1567 if (ret != MS_MEDIA_ERR_NONE)
1568 media_svc_error("strcpy error");
1569 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1571 SAFE_FREE(err_attr_name);
1574 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1575 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1576 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1577 if (ret != MS_MEDIA_ERR_NONE)
1578 media_svc_error("strcpy error");
1579 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1580 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1582 /*media_svc_debug("composer - unknown"); */
1583 SAFE_FREE(err_attr_name);
1586 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1587 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1588 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1589 if (ret != MS_MEDIA_ERR_NONE)
1590 media_svc_error("strcpy error");
1591 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1592 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1594 /*media_svc_debug("copyright - unknown"); */
1595 SAFE_FREE(err_attr_name);
1598 mmf_error = mm_file_destroy_tag_attrs(tag);
1599 if (mmf_error != FILEINFO_ERROR_NONE) {
1600 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1603 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1604 char *no_tag_title = NULL;
1605 media_svc_error("no tag information");
1607 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1609 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1610 if (ret != MS_MEDIA_ERR_NONE)
1611 media_svc_error("strcpy error");
1612 SAFE_FREE(no_tag_title);
1614 media_svc_error("Can't extract title");
1617 content_info->album_id = album_id;
1620 return MS_MEDIA_ERR_NONE;
1623 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1625 MMHandleType content = 0;
1626 MMHandleType tag = 0;
1628 unsigned char *image = NULL;
1629 unsigned int size = 0;
1630 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1631 int mmf_error = FILEINFO_ERROR_NONE;
1632 char *err_attr_name = NULL;
1634 bool extract_thumbnail = FALSE;
1635 bool append_album = FALSE;
1637 int ret = MS_MEDIA_ERR_NONE;
1639 unsigned int resize_size = 0;
1640 unsigned char *resize_image = NULL;
1642 /*Get Content Tag attribute ===========*/
1643 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1645 if (mmf_error == FILEINFO_ERROR_NONE) {
1646 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1647 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1648 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1649 if (ret != MS_MEDIA_ERR_NONE)
1650 media_svc_error("strcpy error");
1652 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1654 SAFE_FREE(err_attr_name);
1655 /*media_svc_debug("album - unknown"); */
1658 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1659 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1660 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1661 if (ret != MS_MEDIA_ERR_NONE)
1662 media_svc_error("strcpy error");
1663 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1665 SAFE_FREE(err_attr_name);
1666 /*media_svc_debug("artist - unknown"); */
1669 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1670 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1671 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1672 if (ret != MS_MEDIA_ERR_NONE)
1673 media_svc_error("strcpy error");
1674 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1676 SAFE_FREE(err_attr_name);
1677 /*media_svc_debug("album_artist - unknown"); */
1680 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1681 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1682 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1683 if (ret != MS_MEDIA_ERR_NONE)
1684 media_svc_error("strcpy error");
1686 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1687 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1689 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1690 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1694 SAFE_FREE(err_attr_name);
1695 /*media_svc_debug("genre - unknown"); */
1698 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1699 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1701 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1702 if (ret != MS_MEDIA_ERR_NONE)
1703 media_svc_error("strcpy error");
1705 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1709 for (idx = 0; idx < size; idx++) {
1711 media_svc_debug("SPACE [%s]", p);
1715 media_svc_debug("Not SPACE [%s]", p);
1716 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1717 if (ret != MS_MEDIA_ERR_NONE)
1718 media_svc_error("strcpy error");
1724 media_svc_debug("Can't extract title. All string is space");
1725 title = _media_svc_get_title_from_filepath(content_info->path);
1727 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1728 if (ret != MS_MEDIA_ERR_NONE)
1729 media_svc_error("strcpy error");
1732 media_svc_error("Can't extract title");
1737 SAFE_FREE(err_attr_name);
1738 title = _media_svc_get_title_from_filepath(content_info->path);
1740 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1741 if (ret != MS_MEDIA_ERR_NONE)
1742 media_svc_error("strcpy error");
1745 media_svc_error("Can't extract title");
1749 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1750 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1751 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1752 if (ret != MS_MEDIA_ERR_NONE)
1753 media_svc_error("strcpy error");
1754 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1756 SAFE_FREE(err_attr_name);
1759 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1760 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1761 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1762 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1763 char time_info[64] = {0, };
1764 char p_value[64] = {0, };
1766 memset(time_info, 0x00, sizeof(time_info));
1767 memset(p_value, 0x00, sizeof(p_value));
1768 strncpy(p_value, p, size);
1769 for (idx = 0; idx < size; idx++) {
1770 if (p_value[idx] == '-') {
1771 time_info[idx] = ':';
1772 } else if (p_value[idx] != '\0') {
1773 time_info[idx] = p_value[idx];
1775 media_svc_error("strcpy error");
1779 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1781 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1784 if (ret != MS_MEDIA_ERR_NONE) {
1785 media_svc_error("strcpy error");
1787 /* This is same as datetaken */
1789 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1791 int datetaken_size = 19;
1792 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1794 if (ret != MS_MEDIA_ERR_NONE)
1795 media_svc_error("strcpy error");
1797 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1798 if (content_info->timeline == 0) {
1799 content_info->timeline = content_info->modified_time;
1801 media_svc_debug("Timeline : %ld", content_info->timeline);
1804 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1806 SAFE_FREE(err_attr_name);
1809 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1810 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1811 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1812 if (ret != MS_MEDIA_ERR_NONE)
1813 media_svc_error("strcpy error");
1814 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1815 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1817 /*media_svc_debug("composer - unknown"); */
1818 SAFE_FREE(err_attr_name);
1821 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1822 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1823 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1824 if (ret != MS_MEDIA_ERR_NONE)
1825 media_svc_error("strcpy error");
1826 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1827 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1829 /*media_svc_debug("copyright - unknown"); */
1830 SAFE_FREE(err_attr_name);
1833 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1834 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1835 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1836 if (ret != MS_MEDIA_ERR_NONE)
1837 media_svc_error("strcpy error");
1839 SAFE_FREE(err_attr_name);
1842 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1843 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1845 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1846 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1847 if (ret != MS_MEDIA_ERR_NONE)
1848 media_svc_error("strcpy error");
1850 media_svc_debug("Wrong Year Information [%s]", p);
1853 SAFE_FREE(err_attr_name);
1856 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1857 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1858 content_info->media_meta.rating = atoi(p);
1860 SAFE_FREE(err_attr_name);
1861 content_info->media_meta.rating = 0;
1864 /*Initialize album_id to 0. below code will set the album_id*/
1865 content_info->album_id = album_id;
1866 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1868 if (ret != MS_MEDIA_ERR_NONE) {
1869 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1870 media_svc_debug("album does not exist. So start to make album art");
1871 extract_thumbnail = TRUE;
1872 append_album = TRUE;
1874 extract_thumbnail = TRUE;
1875 append_album = FALSE;
1878 content_info->album_id = album_id;
1879 append_album = FALSE;
1881 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1882 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1884 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1885 extract_thumbnail = TRUE;
1887 media_svc_debug("album already exists. don't need to make album art");
1888 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1889 extract_thumbnail = TRUE;
1893 /*Do not extract artwork for the USB Storage content*/
1894 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1895 extract_thumbnail = FALSE;
1897 if (extract_thumbnail == TRUE) {
1898 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1899 if (mmf_error != FILEINFO_ERROR_NONE) {
1900 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1901 SAFE_FREE(err_attr_name);
1903 /*media_svc_debug("artwork size1 [%d]", size); */
1906 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1907 if (mmf_error != FILEINFO_ERROR_NONE) {
1908 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1909 SAFE_FREE(err_attr_name);
1911 /*media_svc_debug("artwork size2 [%d]", size); */
1913 if (image != NULL && size > 0) {
1914 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1915 int artwork_mime_size = -1;
1917 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1918 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1919 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1920 if (ret != MS_MEDIA_ERR_NONE) {
1921 media_svc_error("Fail to Get Thumbnail Path");
1923 /* albumart resizing */
1924 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1926 SAFE_FREE(err_attr_name);
1929 if (strlen(thumb_path) > 0) {
1930 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1931 if (ret != MS_MEDIA_ERR_NONE) {
1932 media_svc_error("Fail to Save Thumbnail Image");
1934 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1935 if (ret != MS_MEDIA_ERR_NONE)
1936 media_svc_error("strcpy error");
1942 if (append_album == TRUE) {
1943 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1944 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1945 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1947 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1949 content_info->album_id = album_id;
1952 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1953 double longitude = 0.0;
1954 double latitude = 0.0;
1955 double altitude = 0.0;
1957 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1958 content_info->media_meta.longitude = longitude;
1959 content_info->media_meta.latitude = latitude;
1960 content_info->media_meta.altitude = altitude;
1962 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1963 if (mmf_error != FILEINFO_ERROR_NONE) {
1965 SAFE_FREE(err_attr_name);
1968 media_svc_debug("CDIS : %d", cdis_value);
1971 mmf_error = mm_file_destroy_tag_attrs(tag);
1972 if (mmf_error != FILEINFO_ERROR_NONE) {
1973 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1976 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1977 char *no_tag_title = NULL;
1978 media_svc_error("no tag information");
1980 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1982 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1983 if (ret != MS_MEDIA_ERR_NONE)
1984 media_svc_error("strcpy error");
1985 SAFE_FREE(no_tag_title);
1987 media_svc_error("Can't extract title");
1990 content_info->album_id = album_id;
1993 /*Get Content attribute ===========*/
1994 if (cdis_value == 1) {
1995 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1997 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2000 if (mmf_error == FILEINFO_ERROR_NONE) {
2001 /*Common attribute*/
2002 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2003 if (mmf_error != FILEINFO_ERROR_NONE) {
2004 SAFE_FREE(err_attr_name);
2005 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2007 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2010 /*Sound/Music attribute*/
2011 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2013 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2014 if (mmf_error != FILEINFO_ERROR_NONE) {
2015 SAFE_FREE(err_attr_name);
2016 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2018 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2021 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2022 if (mmf_error != FILEINFO_ERROR_NONE) {
2023 SAFE_FREE(err_attr_name);
2024 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2026 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2029 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2030 if (mmf_error != FILEINFO_ERROR_NONE) {
2031 SAFE_FREE(err_attr_name);
2032 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2034 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2037 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2038 if (mmf_error != FILEINFO_ERROR_NONE) {
2039 SAFE_FREE(err_attr_name);
2040 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2042 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2044 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2045 int audio_bitrate = 0;
2046 int video_bitrate = 0;
2048 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2049 if (mmf_error != FILEINFO_ERROR_NONE) {
2050 SAFE_FREE(err_attr_name);
2051 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2053 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2056 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2057 if (mmf_error != FILEINFO_ERROR_NONE) {
2058 SAFE_FREE(err_attr_name);
2059 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2061 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2064 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2066 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2067 if (mmf_error != FILEINFO_ERROR_NONE) {
2068 SAFE_FREE(err_attr_name);
2069 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2071 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2074 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2075 if (mmf_error != FILEINFO_ERROR_NONE) {
2076 SAFE_FREE(err_attr_name);
2077 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2079 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2083 media_svc_error("Not support type");
2084 mmf_error = mm_file_destroy_content_attrs(content);
2085 if (mmf_error != FILEINFO_ERROR_NONE) {
2086 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2088 return MS_MEDIA_ERR_INVALID_PARAMETER;
2091 mmf_error = mm_file_destroy_content_attrs(content);
2092 if (mmf_error != FILEINFO_ERROR_NONE) {
2093 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2096 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2099 return MS_MEDIA_ERR_NONE;
2102 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2104 media_svc_retm_if(content_info == NULL, "content info is NULL");
2106 /* Delete media_svc_content_info_s */
2107 SAFE_FREE(content_info->media_uuid);
2108 SAFE_FREE(content_info->path);
2109 SAFE_FREE(content_info->file_name);
2110 SAFE_FREE(content_info->mime_type);
2111 SAFE_FREE(content_info->folder_uuid);
2112 SAFE_FREE(content_info->thumbnail_path);
2113 SAFE_FREE(content_info->storage_uuid);
2115 /* Delete media_svc_content_meta_s */
2116 SAFE_FREE(content_info->media_meta.title);
2117 SAFE_FREE(content_info->media_meta.album);
2118 SAFE_FREE(content_info->media_meta.artist);
2119 SAFE_FREE(content_info->media_meta.album_artist);
2120 SAFE_FREE(content_info->media_meta.genre);
2121 SAFE_FREE(content_info->media_meta.composer);
2122 SAFE_FREE(content_info->media_meta.year);
2123 SAFE_FREE(content_info->media_meta.recorded_date);
2124 SAFE_FREE(content_info->media_meta.copyright);
2125 SAFE_FREE(content_info->media_meta.track_num);
2126 SAFE_FREE(content_info->media_meta.description);
2127 SAFE_FREE(content_info->media_meta.datetaken);
2128 SAFE_FREE(content_info->media_meta.exposure_time);
2129 SAFE_FREE(content_info->media_meta.model);
2130 SAFE_FREE(content_info->media_meta.weather);
2131 SAFE_FREE(content_info->media_meta.category);
2132 SAFE_FREE(content_info->media_meta.keyword);
2133 SAFE_FREE(content_info->media_meta.location_tag);
2134 SAFE_FREE(content_info->media_meta.content_name);
2135 SAFE_FREE(content_info->media_meta.age_rating);
2136 SAFE_FREE(content_info->media_meta.author);
2137 SAFE_FREE(content_info->media_meta.provider);
2139 SAFE_FREE(content_info->media_meta.title_pinyin);
2140 SAFE_FREE(content_info->media_meta.album_pinyin);
2141 SAFE_FREE(content_info->media_meta.artist_pinyin);
2142 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2143 SAFE_FREE(content_info->media_meta.genre_pinyin);
2144 SAFE_FREE(content_info->media_meta.composer_pinyin);
2145 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2146 SAFE_FREE(content_info->media_meta.description_pinyin);
2151 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2153 if (STRING_VALID(path)) {
2154 char *internal_path = _media_svc_get_path(uid);
2155 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2156 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2157 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2158 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2159 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2160 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2162 media_svc_error("Invalid Path");
2163 SAFE_FREE(internal_path);
2164 return MS_MEDIA_ERR_INVALID_PARAMETER;
2166 SAFE_FREE(internal_path);
2168 media_svc_error("INVALID parameter");
2169 return MS_MEDIA_ERR_INVALID_PARAMETER;
2172 return MS_MEDIA_ERR_NONE;
2175 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2178 size_t i, count = 0;
2179 size_t oldlen = strlen(olds);
2180 if (oldlen < 1) return s;
2181 size_t newlen = strlen(news);
2183 if (newlen != oldlen) {
2184 for (i = 0; s[i] != '\0';) {
2185 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2188 } else i = strlen(s);
2191 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2192 if (result == NULL) return NULL;
2196 if (memcmp(s, olds, oldlen) == 0) {
2197 memcpy(sr, news, newlen);
2200 } else *sr++ = *s++;
2208 bool _media_svc_is_drm_file(const char *path)
2213 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)
2215 int ret = MS_MEDIA_ERR_NONE;
2217 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2219 if (ret != MS_MEDIA_ERR_NONE) {
2220 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2221 ret = MS_MEDIA_ERR_INTERNAL;
2223 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2229 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2231 int ret = MS_MEDIA_ERR_NONE;
2233 pinyin_name_s *pinyinname = NULL;
2237 if (!STRING_VALID(src_str)) {
2238 media_svc_debug("String is invalid");
2242 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2243 if (ret == MS_MEDIA_ERR_NONE) {
2244 if (STRING_VALID(pinyinname[0].pinyin_name))
2245 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2247 *pinyin_str = strdup(src_str); //Return Original Non China Character
2250 _media_svc_pinyin_free(pinyinname, size);
2255 bool _media_svc_check_pinyin_support(void)
2261 int _media_svc_get_ini_value()
2263 if (g_ini_value == -1) {
2264 dictionary *dict = NULL;
2266 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2268 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2272 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2273 iniparser_freedict(dict);
2275 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2280 char* _media_svc_get_title_from_path(const char *path)
2282 char *filename = NULL;
2285 int filename_len = -1;
2286 int new_title_len = -1;
2289 media_svc_error("path is NULL");
2293 filename = g_path_get_basename(path);
2294 if (!STRING_VALID(filename)) {
2295 media_svc_error("wrong file name");
2296 SAFE_FREE(filename);
2300 filename_len = strlen(filename);
2302 ext = g_strrstr(filename, ".");
2304 media_svc_error("there is no file extention");
2308 new_title_len = filename_len - strlen(ext);
2309 if (new_title_len < 1) {
2310 media_svc_error("title length is zero");
2311 SAFE_FREE(filename);
2315 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2317 SAFE_FREE(filename);
2319 media_svc_debug("extract title is [%s]", title);
2324 #define BUF_LENGHT 256
2326 void _media_svc_print_stderror(void)
2328 char buf[BUF_LENGHT] = {0,};
2330 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));